Back to index

salome-geom  6.5.0
VTKExport.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // File:        VTKExport.cxx
00024 // Author:      Oleg UVAROV
00025 //
00026 #include "utilities.h"
00027 
00028 #include <Basics_Utils.hxx>
00029 
00030 #include <OCC2VTK_Tools.h>
00031 
00032 #include <GEOM_VertexSource.h>
00033 #include <GEOM_EdgeSource.h>
00034 #include <GEOM_WireframeFace.h>
00035 #include <GEOM_ShadingFace.h>
00036 
00037 #include <vtkAppendPolyData.h>  
00038 #include <vtkPolyDataWriter.h>  
00039 
00040 #include <TCollection_AsciiString.hxx>
00041 #include <TopExp.hxx>
00042 #include <TopExp_Explorer.hxx>
00043 #include <TopoDS.hxx>
00044 #include <TopoDS_Shape.hxx>
00045 #include <Poly_Triangulation.hxx>
00046 #include <BRep_Tool.hxx>
00047 #include <BRepTools.hxx>
00048 
00049 #ifdef WIN32
00050 # if defined VTKEXPORT_EXPORTS || defined VTKExport_EXPORTS
00051 #  define VTKEXPORT_EXPORT __declspec( dllexport )
00052 # else
00053 #  define VTKEXPORT_EXPORT __declspec( dllimport )
00054 # endif
00055 #else
00056 # define VTKEXPORT_EXPORT
00057 #endif
00058 
00059 //=============================================================================
00063 //=============================================================================
00064 
00065 extern "C"
00066 {
00067   VTKEXPORT_EXPORT
00068   int Export(const TopoDS_Shape& theShape,
00069              const TCollection_AsciiString& theFileName,
00070              const TCollection_AsciiString& theFormatName)
00071   {
00072     MESSAGE("Export VTK into file " << theFileName.ToCString());
00073 
00074     try
00075     {
00076       GEOM_VertexSource* myVertexSource = GEOM_VertexSource::New();
00077       GEOM_EdgeSource* myIsolatedEdgeSource = GEOM_EdgeSource::New();
00078       GEOM_EdgeSource* myOneFaceEdgeSource = GEOM_EdgeSource::New();
00079       GEOM_EdgeSource* mySharedEdgeSource = GEOM_EdgeSource::New();
00080       GEOM_WireframeFace* myWireframeFaceSource = GEOM_WireframeFace::New();
00081       GEOM_ShadingFace* myShadingFaceSource = GEOM_ShadingFace::New();
00082 
00083       vtkAppendPolyData* myAppendFilter = vtkAppendPolyData::New();
00084       myAppendFilter->AddInput( myVertexSource->GetOutput() );
00085       myAppendFilter->AddInput( myIsolatedEdgeSource->GetOutput() );
00086       myAppendFilter->AddInput( myOneFaceEdgeSource->GetOutput() );
00087       myAppendFilter->AddInput( mySharedEdgeSource->GetOutput() );
00088       //myAppendFilter->AddInput( myWireframeFaceSource->GetOutput() ); // iso-lines are unnecessary
00089       myAppendFilter->AddInput( myShadingFaceSource->GetOutput() );
00090 
00091       float aDeflection = 0.001;
00092       bool anIsVector = false;
00093 
00094       // Is shape triangulated?
00095       bool wasMeshed = true;
00096       TopExp_Explorer ex;
00097       TopLoc_Location aLoc;
00098       for (ex.Init(theShape, TopAbs_FACE); ex.More(); ex.Next()) {
00099        const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
00100        Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
00101        if(aPoly.IsNull()) { 
00102          wasMeshed = false;
00103          break; 
00104        }
00105       }
00106 
00107       GEOM::MeshShape( theShape, aDeflection );
00108 
00109       TopExp_Explorer aVertexExp( theShape, TopAbs_VERTEX );
00110       for( ; aVertexExp.More(); aVertexExp.Next() )
00111       {
00112         const TopoDS_Vertex& aVertex = TopoDS::Vertex( aVertexExp.Current() );
00113         myVertexSource->AddVertex( aVertex );
00114       }
00115 
00116       TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
00117       TopExp::MapShapesAndAncestors( theShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
00118   
00119       GEOM::SetShape( theShape,
00120                       anEdgeMap,
00121                       anIsVector,
00122                       myIsolatedEdgeSource,
00123                       myOneFaceEdgeSource,
00124                       mySharedEdgeSource,
00125                       myWireframeFaceSource,
00126                       myShadingFaceSource );
00127 
00128       myAppendFilter->Update();
00129 
00130       // Set "C" numeric locale to save numbers correctly
00131       Kernel_Utils::Localizer loc;
00132 
00133       vtkPolyDataWriter* aWriter = vtkPolyDataWriter::New(); 
00134       aWriter->SetInput( myAppendFilter->GetOutput() );
00135       aWriter->SetFileName( theFileName.ToCString() );
00136       aWriter->Write();
00137       aWriter->Delete();
00138 
00139       myVertexSource->Delete();
00140       myIsolatedEdgeSource->Delete();
00141       myOneFaceEdgeSource->Delete();
00142       mySharedEdgeSource->Delete();
00143       myWireframeFaceSource->Delete();
00144       myShadingFaceSource->Delete();
00145 
00146       myAppendFilter->Delete();
00147 
00148       if(!wasMeshed)
00149        BRepTools::Clean(theShape);
00150 
00151       return 1;
00152     }
00153     catch(Standard_Failure)
00154     {
00155       //THROW_SALOME_CORBA_EXCEPTION("Exception catched in VTKExport", SALOME::BAD_PARAM);
00156     }
00157     return 0;
00158   }
00159 }