Salome HOME
'fr' and 'ja' translations for 0022617: [CEA 1060] In OCC view, add "Show vertices...
[modules/geom.git] / src / VTKExport / VTKExport.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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->AddInputConnection( myVertexSource->GetOutputPort() );
85       myAppendFilter->AddInputConnection( myIsolatedEdgeSource->GetOutputPort() );
86       myAppendFilter->AddInputConnection( myOneFaceEdgeSource->GetOutputPort() );
87       myAppendFilter->AddInputConnection( mySharedEdgeSource->GetOutputPort() );
88       //myAppendFilter->AddInputConnection( myWireframeFaceSource->GetOutputPort() ); // iso-lines are unnecessary
89       myAppendFilter->AddInputConnection( myShadingFaceSource->GetOutputPort() );
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                       0, // all vertices were added above
123                       myIsolatedEdgeSource,
124                       myOneFaceEdgeSource,
125                       mySharedEdgeSource,
126                       myWireframeFaceSource,
127                       myShadingFaceSource );
128
129       myAppendFilter->Update();
130
131       // Set "C" numeric locale to save numbers correctly
132       Kernel_Utils::Localizer loc;
133
134       vtkPolyDataWriter* aWriter = vtkPolyDataWriter::New(); 
135       aWriter->SetInputConnection( myAppendFilter->GetOutputPort() );
136       aWriter->SetFileName( theFileName.ToCString() );
137       aWriter->Write();
138       aWriter->Delete();
139
140       myVertexSource->Delete();
141       myIsolatedEdgeSource->Delete();
142       myOneFaceEdgeSource->Delete();
143       mySharedEdgeSource->Delete();
144       myWireframeFaceSource->Delete();
145       myShadingFaceSource->Delete();
146
147       myAppendFilter->Delete();
148
149       if(!wasMeshed)
150         BRepTools::Clean(theShape);
151
152       return 1;
153     }
154     catch(Standard_Failure)
155     {
156       //THROW_SALOME_CORBA_EXCEPTION("Exception catched in VTKExport", SALOME::BAD_PARAM);
157     }
158     return 0;
159   }
160 }