Salome HOME
IPAL52533: TC7.5.0: 3D Sketch - preview of angle value is wrong
[modules/geom.git] / src / VTKPlugin / VTKPlugin_ExportDriver.cxx
1 // Copyright (C) 2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 // internal includes
21 #include "VTKPlugin_ExportDriver.hxx"
22 #include "VTKPlugin_IExport.hxx"
23
24 // KERNEL includes
25 #include <utilities.h>
26 #include <Basics_Utils.hxx>
27
28 // GEOM includes
29 #include "GEOM_Function.hxx"
30 #include "OCC2VTK_Tools.h"
31 #include "GEOM_VertexSource.h"
32 #include "GEOM_EdgeSource.h"
33 #include "GEOM_WireframeFace.h"
34 #include "GEOM_ShadingFace.h"
35
36 // OOCT includes
37 #include <TCollection_AsciiString.hxx>
38 #include <TopExp.hxx>
39 #include <TopExp_Explorer.hxx>
40 #include <TopoDS.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <Poly_Triangulation.hxx>
43 #include <BRep_Tool.hxx>
44 #include <BRepTools.hxx>
45
46 // VTK includes
47 #include <vtkAppendPolyData.h>
48 #include <vtkPolyDataWriter.h>
49
50 //=======================================================================
51 //function : GetID
52 //purpose  :
53 //=======================================================================
54 const Standard_GUID& VTKPlugin_ExportDriver::GetID()
55 {
56   static Standard_GUID aGUID("0966443c-6f3c-4ebe-ba46-c0833786d817");
57   return aGUID;
58 }
59
60 //=======================================================================
61 //function : VTKPlugin_ExportDriver
62 //purpose  :
63 //=======================================================================
64 VTKPlugin_ExportDriver::VTKPlugin_ExportDriver()
65 {
66 }
67
68 //=======================================================================
69 //function : Execute
70 //purpose  :
71 //=======================================================================
72 Standard_Integer VTKPlugin_ExportDriver::Execute( TFunction_Logbook& log ) const
73 {
74   if (Label().IsNull()) return 0;
75   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
76
77   VTKPlugin_IExport aData (aFunction);
78
79   // retrieve the being exported shape
80   TopoDS_Shape aShape;
81   Handle(GEOM_Function) aRefFunction = aData.GetOriginal();
82   if( aRefFunction.IsNull() ) return 0;
83   aShape = aRefFunction->GetValue();
84   if( aShape.IsNull() ) return 0;
85   // set the result of function to be used by next operations
86   aFunction->SetValue( aShape );
87
88   TCollection_AsciiString aFileName = aData.GetFileName();
89   float aDeflection = float( aData.GetDeflection() );
90
91   MESSAGE( "Export VTK into file " << aFileName );
92   try
93   {
94     GEOM_VertexSource* myVertexSource = GEOM_VertexSource::New();
95     GEOM_EdgeSource* myIsolatedEdgeSource = GEOM_EdgeSource::New();
96     GEOM_EdgeSource* myOneFaceEdgeSource = GEOM_EdgeSource::New();
97     GEOM_EdgeSource* mySharedEdgeSource = GEOM_EdgeSource::New();
98     GEOM_WireframeFace* myWireframeFaceSource = GEOM_WireframeFace::New();
99     GEOM_ShadingFace* myShadingFaceSource = GEOM_ShadingFace::New();
100
101     vtkAppendPolyData* myAppendFilter = vtkAppendPolyData::New();
102     myAppendFilter->AddInputConnection( myVertexSource->GetOutputPort() );
103     myAppendFilter->AddInputConnection( myIsolatedEdgeSource->GetOutputPort() );
104     myAppendFilter->AddInputConnection( myOneFaceEdgeSource->GetOutputPort() );
105     myAppendFilter->AddInputConnection( mySharedEdgeSource->GetOutputPort() );
106     //myAppendFilter->AddInputConnection( myWireframeFaceSource->GetOutputPort() ); // iso-lines are unnecessary
107     myAppendFilter->AddInputConnection( myShadingFaceSource->GetOutputPort() );
108
109     bool anIsVector = false;
110
111     // Is shape triangulated?
112     bool wasMeshed = true;
113     TopExp_Explorer ex;
114     TopLoc_Location aLoc;
115     for (ex.Init(aShape, TopAbs_FACE); ex.More(); ex.Next()) {
116       const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
117       Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
118       if(aPoly.IsNull()) {
119         wasMeshed = false;
120         break;
121       }
122     }
123
124     GEOM::MeshShape( aShape, aDeflection );
125
126     TopExp_Explorer aVertexExp( aShape, TopAbs_VERTEX );
127     for( ; aVertexExp.More(); aVertexExp.Next() )
128     {
129       const TopoDS_Vertex& aVertex = TopoDS::Vertex( aVertexExp.Current() );
130       myVertexSource->AddVertex( aVertex );
131     }
132
133     TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
134     TopExp::MapShapesAndAncestors( aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
135
136     GEOM::SetShape( aShape,
137                     anEdgeMap,
138                     anIsVector,
139                     0,
140                     myIsolatedEdgeSource,
141                     myOneFaceEdgeSource,
142                     mySharedEdgeSource,
143                     myWireframeFaceSource,
144                     myShadingFaceSource );
145
146     myAppendFilter->Update();
147
148     // Set "C" numeric locale to save numbers correctly
149     Kernel_Utils::Localizer loc;
150
151     vtkPolyDataWriter* aWriter = vtkPolyDataWriter::New();
152     aWriter->SetInputConnection( myAppendFilter->GetOutputPort() );
153     aWriter->SetFileName( aFileName.ToCString() );
154     aWriter->Write();
155     aWriter->Delete();
156
157     myVertexSource->Delete();
158     myIsolatedEdgeSource->Delete();
159     myOneFaceEdgeSource->Delete();
160     mySharedEdgeSource->Delete();
161     myWireframeFaceSource->Delete();
162     myShadingFaceSource->Delete();
163
164     myAppendFilter->Delete();
165
166     if(!wasMeshed)
167       BRepTools::Clean(aShape);
168
169     return 1;
170   }
171   catch( Standard_Failure )
172   {
173     //THROW_SALOME_CORBA_EXCEPTION("Exception catched in ExportVTK", SALOME::BAD_PARAM);
174   }
175   return 0;
176 }
177
178 //=======================================================================
179 //function : MustExecute
180 //purpose  :
181 //=======================================================================
182 Standard_Boolean VTKPlugin_ExportDriver::MustExecute( const TFunction_Logbook& ) const
183 {
184   return Standard_True;
185 }
186
187 //================================================================================
188 /*!
189  * \brief Returns a name of creation operation and names and values of creation parameters
190  */
191 //================================================================================
192 bool VTKPlugin_ExportDriver::
193 GetCreationInformation( std::string&             theOperationName,
194                         std::vector<GEOM_Param>& theParams )
195 {
196   return false;
197 }
198
199 IMPLEMENT_STANDARD_HANDLE( VTKPlugin_ExportDriver,GEOM_BaseDriver );
200 IMPLEMENT_STANDARD_RTTIEXT( VTKPlugin_ExportDriver,GEOM_BaseDriver );