1 // Copyright (C) 2014 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "VTKPlugin_ExportDriver.hxx"
22 #include "VTKPlugin_IExport.hxx"
25 #include <utilities.h>
26 #include <Basics_Utils.hxx>
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"
37 #include <TCollection_AsciiString.hxx>
39 #include <TopExp_Explorer.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <Poly_Triangulation.hxx>
43 #include <BRep_Tool.hxx>
44 #include <BRepTools.hxx>
47 #include <vtkAppendPolyData.h>
48 #include <vtkPolyDataWriter.h>
50 //=======================================================================
53 //=======================================================================
54 const Standard_GUID& VTKPlugin_ExportDriver::GetID()
56 static Standard_GUID aGUID("0966443c-6f3c-4ebe-ba46-c0833786d817");
60 //=======================================================================
61 //function : VTKPlugin_ExportDriver
63 //=======================================================================
64 VTKPlugin_ExportDriver::VTKPlugin_ExportDriver()
68 //=======================================================================
71 //=======================================================================
72 Standard_Integer VTKPlugin_ExportDriver::Execute( TFunction_Logbook& log ) const
74 if (Label().IsNull()) return 0;
75 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction( Label() );
77 VTKPlugin_IExport aData (aFunction);
79 // retrieve the being exported shape
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 );
88 TCollection_AsciiString aFileName = aData.GetFileName();
89 float aDeflection = float( aData.GetDeflection() );
91 MESSAGE( "Export VTK into file " << aFileName );
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();
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() );
109 bool anIsVector = false;
111 // Is shape triangulated?
112 bool wasMeshed = true;
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);
124 GEOM::MeshShape( aShape, aDeflection );
126 TopExp_Explorer aVertexExp( aShape, TopAbs_VERTEX );
127 for( ; aVertexExp.More(); aVertexExp.Next() )
129 const TopoDS_Vertex& aVertex = TopoDS::Vertex( aVertexExp.Current() );
130 myVertexSource->AddVertex( aVertex );
133 TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
134 TopExp::MapShapesAndAncestors( aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
136 GEOM::SetShape( aShape,
140 myIsolatedEdgeSource,
143 myWireframeFaceSource,
144 myShadingFaceSource );
146 myAppendFilter->Update();
148 // Set "C" numeric locale to save numbers correctly
149 Kernel_Utils::Localizer loc;
151 vtkPolyDataWriter* aWriter = vtkPolyDataWriter::New();
152 aWriter->SetInputConnection( myAppendFilter->GetOutputPort() );
153 aWriter->SetFileName( aFileName.ToCString() );
157 myVertexSource->Delete();
158 myIsolatedEdgeSource->Delete();
159 myOneFaceEdgeSource->Delete();
160 mySharedEdgeSource->Delete();
161 myWireframeFaceSource->Delete();
162 myShadingFaceSource->Delete();
164 myAppendFilter->Delete();
167 BRepTools::Clean(aShape);
171 catch( Standard_Failure )
173 //THROW_SALOME_CORBA_EXCEPTION("Exception catched in ExportVTK", SALOME::BAD_PARAM);
178 //=======================================================================
179 //function : MustExecute
181 //=======================================================================
182 Standard_Boolean VTKPlugin_ExportDriver::MustExecute( const TFunction_Logbook& ) const
184 return Standard_True;
187 //================================================================================
189 * \brief Returns a name of creation operation and names and values of creation parameters
191 //================================================================================
192 bool VTKPlugin_ExportDriver::
193 GetCreationInformation( std::string& theOperationName,
194 std::vector<GEOM_Param>& theParams )
199 IMPLEMENT_STANDARD_HANDLE( VTKPlugin_ExportDriver,GEOM_BaseDriver );
200 IMPLEMENT_STANDARD_RTTIEXT( VTKPlugin_ExportDriver,GEOM_BaseDriver );