]> SALOME platform Git repositories - modules/geom.git/blob - src/VTKExport/VTKExport.cxx
Salome HOME
Merge from V5_1_main branch 24/11/2010
[modules/geom.git] / src / VTKExport / VTKExport.cxx
1 //  Copyright (C) 2007-2010  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
46 #ifdef WIN32
47 # if defined VTKEXPORT_EXPORTS || defined VTKExport_EXPORTS
48 #  define VTKEXPORT_EXPORT __declspec( dllexport )
49 # else
50 #  define VTKEXPORT_EXPORT __declspec( dllimport )
51 # endif
52 #else
53 # define VTKEXPORT_EXPORT
54 #endif
55
56 //=============================================================================
57 /*!
58  *
59  */
60 //=============================================================================
61
62 extern "C"
63 {
64   VTKEXPORT_EXPORT
65   int Export(const TopoDS_Shape& theShape,
66              const TCollection_AsciiString& theFileName,
67              const TCollection_AsciiString& theFormatName)
68   {
69     MESSAGE("Export VTK into file " << theFileName.ToCString());
70
71     try
72     {
73       GEOM_VertexSource* myVertexSource = GEOM_VertexSource::New();
74       GEOM_EdgeSource* myIsolatedEdgeSource = GEOM_EdgeSource::New();
75       GEOM_EdgeSource* myOneFaceEdgeSource = GEOM_EdgeSource::New();
76       GEOM_EdgeSource* mySharedEdgeSource = GEOM_EdgeSource::New();
77       GEOM_WireframeFace* myWireframeFaceSource = GEOM_WireframeFace::New();
78       GEOM_ShadingFace* myShadingFaceSource = GEOM_ShadingFace::New();
79
80       vtkAppendPolyData* myAppendFilter = vtkAppendPolyData::New();
81       myAppendFilter->AddInput( myVertexSource->GetOutput() );
82       myAppendFilter->AddInput( myIsolatedEdgeSource->GetOutput() );
83       myAppendFilter->AddInput( myOneFaceEdgeSource->GetOutput() );
84       myAppendFilter->AddInput( mySharedEdgeSource->GetOutput() );
85       //myAppendFilter->AddInput( myWireframeFaceSource->GetOutput() ); // iso-lines are unnecessary
86       myAppendFilter->AddInput( myShadingFaceSource->GetOutput() );
87
88       float aDeflection = 1.0;
89       bool anIsForced = true;
90       bool anIsRelative = false;
91       bool anIsVector = false;
92
93       GEOM::MeshShape( theShape, aDeflection, anIsForced );
94
95       TopExp_Explorer aVertexExp( theShape, TopAbs_VERTEX );
96       for( ; aVertexExp.More(); aVertexExp.Next() )
97       {
98         const TopoDS_Vertex& aVertex = TopoDS::Vertex( aVertexExp.Current() );
99         myVertexSource->AddVertex( aVertex );
100       }
101
102       GEOM::MeshShape2( theShape, aDeflection, anIsRelative );
103
104       TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
105       TopExp::MapShapesAndAncestors( theShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
106   
107       GEOM::SetShape( theShape,
108                       anEdgeMap,
109                       anIsVector,
110                       myIsolatedEdgeSource,
111                       myOneFaceEdgeSource,
112                       mySharedEdgeSource,
113                       myWireframeFaceSource,
114                       myShadingFaceSource );
115
116       myAppendFilter->Update();
117
118       // Set "C" numeric locale to save numbers correctly
119       Kernel_Utils::Localizer loc;
120
121       vtkPolyDataWriter* aWriter = vtkPolyDataWriter::New(); 
122       aWriter->SetInput( myAppendFilter->GetOutput() );
123       aWriter->SetFileName( theFileName.ToCString() );
124       aWriter->Write();
125       aWriter->Delete();
126
127       myVertexSource->Delete();
128       myIsolatedEdgeSource->Delete();
129       myOneFaceEdgeSource->Delete();
130       mySharedEdgeSource->Delete();
131       myWireframeFaceSource->Delete();
132       myShadingFaceSource->Delete();
133
134       myAppendFilter->Delete();
135
136       return 1;
137     }
138     catch(Standard_Failure)
139     {
140       //THROW_SALOME_CORBA_EXCEPTION("Exception catched in VTKExport", SALOME::BAD_PARAM);
141     }
142     return 0;
143   }
144 }