Salome HOME
Update copyright
[modules/geom.git] / src / VTKExport / VTKExport.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File:        VTKExport.cxx
24 // Author:      Oleg UVAROV
25 //
26 #include "utilities.h"
27
28 #include <Basics_Utils.hxx>
29
30 #include <OCC2VTK_Tools.h>
31
32 #include <GEOM_VertexSource.h>
33 #include <GEOM_EdgeSource.h>
34 #include <GEOM_WireframeFace.h>
35 #include <GEOM_ShadingFace.h>
36
37 #include <vtkAppendPolyData.h>  
38 #include <vtkPolyDataWriter.h>  
39
40 #include <TCollection_AsciiString.hxx>
41 #include <TopExp.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopoDS.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <Poly_Triangulation.hxx>
46 #include <BRep_Tool.hxx>
47 #include <BRepTools.hxx>
48
49 #ifdef WIN32
50 # if defined VTKEXPORT_EXPORTS || defined VTKExport_EXPORTS
51 #  define VTKEXPORT_EXPORT __declspec( dllexport )
52 # else
53 #  define VTKEXPORT_EXPORT __declspec( dllimport )
54 # endif
55 #else
56 # define VTKEXPORT_EXPORT
57 #endif
58
59 //=============================================================================
60 /*!
61  *
62  */
63 //=============================================================================
64
65 extern "C"
66 {
67   VTKEXPORT_EXPORT
68   int Export(const TopoDS_Shape& theShape,
69              const TCollection_AsciiString& theFileName,
70              const TCollection_AsciiString& theFormatName)
71   {
72     MESSAGE("Export VTK into file " << theFileName.ToCString());
73
74     try
75     {
76       GEOM_VertexSource* myVertexSource = GEOM_VertexSource::New();
77       GEOM_EdgeSource* myIsolatedEdgeSource = GEOM_EdgeSource::New();
78       GEOM_EdgeSource* myOneFaceEdgeSource = GEOM_EdgeSource::New();
79       GEOM_EdgeSource* mySharedEdgeSource = GEOM_EdgeSource::New();
80       GEOM_WireframeFace* myWireframeFaceSource = GEOM_WireframeFace::New();
81       GEOM_ShadingFace* myShadingFaceSource = GEOM_ShadingFace::New();
82
83       vtkAppendPolyData* myAppendFilter = vtkAppendPolyData::New();
84       myAppendFilter->AddInput( myVertexSource->GetOutput() );
85       myAppendFilter->AddInput( myIsolatedEdgeSource->GetOutput() );
86       myAppendFilter->AddInput( myOneFaceEdgeSource->GetOutput() );
87       myAppendFilter->AddInput( mySharedEdgeSource->GetOutput() );
88       //myAppendFilter->AddInput( myWireframeFaceSource->GetOutput() ); // iso-lines are unnecessary
89       myAppendFilter->AddInput( myShadingFaceSource->GetOutput() );
90
91       float aDeflection = 0.001;
92       bool anIsVector = false;
93
94       // Is shape triangulated?
95       bool wasMeshed = true;
96       TopExp_Explorer ex;
97       TopLoc_Location aLoc;
98       for (ex.Init(theShape, TopAbs_FACE); ex.More(); ex.Next()) {
99         const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
100         Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
101         if(aPoly.IsNull()) { 
102           wasMeshed = false;
103           break; 
104         }
105       }
106
107       GEOM::MeshShape( theShape, aDeflection );
108
109       TopExp_Explorer aVertexExp( theShape, TopAbs_VERTEX );
110       for( ; aVertexExp.More(); aVertexExp.Next() )
111       {
112         const TopoDS_Vertex& aVertex = TopoDS::Vertex( aVertexExp.Current() );
113         myVertexSource->AddVertex( aVertex );
114       }
115
116       TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
117       TopExp::MapShapesAndAncestors( theShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
118   
119       GEOM::SetShape( theShape,
120                       anEdgeMap,
121                       anIsVector,
122                       myIsolatedEdgeSource,
123                       myOneFaceEdgeSource,
124                       mySharedEdgeSource,
125                       myWireframeFaceSource,
126                       myShadingFaceSource );
127
128       myAppendFilter->Update();
129
130       // Set "C" numeric locale to save numbers correctly
131       Kernel_Utils::Localizer loc;
132
133       vtkPolyDataWriter* aWriter = vtkPolyDataWriter::New(); 
134       aWriter->SetInput( myAppendFilter->GetOutput() );
135       aWriter->SetFileName( theFileName.ToCString() );
136       aWriter->Write();
137       aWriter->Delete();
138
139       myVertexSource->Delete();
140       myIsolatedEdgeSource->Delete();
141       myOneFaceEdgeSource->Delete();
142       mySharedEdgeSource->Delete();
143       myWireframeFaceSource->Delete();
144       myShadingFaceSource->Delete();
145
146       myAppendFilter->Delete();
147
148       if(!wasMeshed)
149         BRepTools::Clean(theShape);
150
151       return 1;
152     }
153     catch(Standard_Failure)
154     {
155       //THROW_SALOME_CORBA_EXCEPTION("Exception catched in VTKExport", SALOME::BAD_PARAM);
156     }
157     return 0;
158   }
159 }