1 // Copyright (C) 2007-2015 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
20 #include "OCC2VTK_Tools.h"
22 #include "GEOM_VertexSource.h"
23 #include "GEOM_EdgeSource.h"
24 #include "GEOM_WireframeFace.h"
25 #include "GEOM_ShadingFace.h"
26 #include "GEOMUtils.hxx"
28 #include <Bnd_Box.hxx>
29 #include <BRep_Tool.hxx>
30 #include <BRepTools.hxx>
32 #include <BRepBndLib.hxx>
33 #include <BRepMesh_IncrementalMesh.hxx>
34 #include <Poly_Triangulation.hxx>
35 #include <TopExp_Explorer.hxx>
37 #include <TopTools_ListOfShape.hxx>
41 #include <vtkAppendPolyData.h>
42 #include <vtkPolyData.h>
43 #include <BRepBuilderAPI_Copy.hxx>
47 void ShapeToVTK( const TopoDS_Shape& theShape,
48 const TopTools_IndexedDataMapOfShapeListOfShape& theEdgeMap,
50 GEOM_VertexSource* theStandaloneVertexSource,
51 GEOM_EdgeSource* theIsolatedEdgeSource,
52 GEOM_EdgeSource* theOneFaceEdgeSource,
53 GEOM_EdgeSource* theSharedEdgeSource,
54 GEOM_WireframeFace* theWireframeFaceSource,
55 GEOM_ShadingFace* theShadingFaceSource )
57 if (theShape.ShapeType() == TopAbs_COMPOUND) {
58 TopoDS_Iterator anItr(theShape);
59 for (; anItr.More(); anItr.Next()) {
60 ShapeToVTK( anItr.Value(),theEdgeMap,theIsVector,
61 theStandaloneVertexSource,
62 theIsolatedEdgeSource,
65 theWireframeFaceSource,
66 theShadingFaceSource );
70 switch (theShape.ShapeType()) {
72 TopExp_Explorer anEdgeExp(theShape,TopAbs_EDGE);
73 for (; anEdgeExp.More(); anEdgeExp.Next()){
74 const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
75 if (!BRep_Tool::Degenerated(anEdge))
76 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
81 const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
82 if (!BRep_Tool::Degenerated(anEdge))
83 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
87 if ( theStandaloneVertexSource ) {
88 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
89 theStandaloneVertexSource->AddVertex(aVertex);
94 TopExp_Explorer aFaceExp (theShape,TopAbs_FACE);
95 for(; aFaceExp.More(); aFaceExp.Next()) {
96 const TopoDS_Face& aFace = TopoDS::Face(aFaceExp.Current());
97 theWireframeFaceSource->AddFace(aFace);
98 theShadingFaceSource->AddFace(aFace);
99 TopExp_Explorer anEdgeExp(aFaceExp.Current(), TopAbs_EDGE);
100 for(; anEdgeExp.More(); anEdgeExp.Next()) {
101 const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
102 if(!BRep_Tool::Degenerated(anEdge)){
103 // compute the number of faces
104 int aNbOfFaces = theEdgeMap.FindFromKey(anEdge).Extent();
106 case 0: // isolated edge
107 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
109 case 1: // edge in only one face
110 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
112 default: // edge shared by at least two faces
113 theSharedEdgeSource->AddEdge(anEdge,theIsVector);
122 vtkPolyData* GetVTKData( const TopoDS_Shape& theShape, float theDeflection )
124 vtkPolyData* ret = 0;
126 BRepBuilderAPI_Copy aCopy(theShape);
127 if (aCopy.IsDone() ) {
129 TopoDS_Shape aShape = aCopy.Shape();
132 GEOM_VertexSource* myVertexSource = GEOM_VertexSource::New();
133 GEOM_EdgeSource* myIsolatedEdgeSource = GEOM_EdgeSource::New();
134 GEOM_EdgeSource* myOneFaceEdgeSource = GEOM_EdgeSource::New();
135 GEOM_EdgeSource* mySharedEdgeSource = GEOM_EdgeSource::New();
136 GEOM_WireframeFace* myWireframeFaceSource = GEOM_WireframeFace::New();
137 GEOM_ShadingFace* myShadingFaceSource = GEOM_ShadingFace::New();
139 vtkAppendPolyData* myAppendFilter = vtkAppendPolyData::New();
140 myAppendFilter->AddInputConnection( myVertexSource->GetOutputPort() );
141 myAppendFilter->AddInputConnection( myIsolatedEdgeSource->GetOutputPort() );
142 myAppendFilter->AddInputConnection( myOneFaceEdgeSource->GetOutputPort() );
143 myAppendFilter->AddInputConnection( mySharedEdgeSource->GetOutputPort() );
144 myAppendFilter->AddInputConnection( myShadingFaceSource->GetOutputPort() );
146 bool anIsVector = false;
148 GEOMUtils::MeshShape( aShape, theDeflection );
149 TopExp_Explorer aVertexExp( aShape, TopAbs_VERTEX );
150 for( ; aVertexExp.More(); aVertexExp.Next() ) {
151 const TopoDS_Vertex& aVertex = TopoDS::Vertex( aVertexExp.Current() );
152 myVertexSource->AddVertex( aVertex );
155 TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
156 TopExp::MapShapesAndAncestors( aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
162 myIsolatedEdgeSource,
165 myWireframeFaceSource,
166 myShadingFaceSource );
168 myAppendFilter->Update();
170 myVertexSource->Delete();
171 myIsolatedEdgeSource->Delete();
172 myOneFaceEdgeSource->Delete();
173 mySharedEdgeSource->Delete();
174 myWireframeFaceSource->Delete();
175 myShadingFaceSource->Delete();
177 ret = vtkPolyData::New();
178 ret->ShallowCopy(myAppendFilter->GetOutput());
179 myAppendFilter->Delete();
181 catch(Standard_Failure) {