1 // Copyright (C) 2007-2024 CEA, EDF, 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 <Poly_Triangulation.hxx>
34 #include <TopExp_Explorer.hxx>
36 #include <TopTools_ListOfShape.hxx>
40 #include <vtkAppendPolyData.h>
41 #include <vtkPolyData.h>
42 #include <BRepBuilderAPI_Copy.hxx>
46 void ShapeToVTK( const TopoDS_Shape& theShape,
47 const TopTools_IndexedDataMapOfShapeListOfShape& theEdgeMap,
49 GEOM_VertexSource* theStandaloneVertexSource,
50 GEOM_EdgeSource* theIsolatedEdgeSource,
51 GEOM_EdgeSource* theOneFaceEdgeSource,
52 GEOM_EdgeSource* theSharedEdgeSource,
53 GEOM_WireframeFace* theWireframeFaceSource,
54 GEOM_ShadingFace* theShadingFaceSource )
56 if (theShape.ShapeType() == TopAbs_COMPOUND) {
57 TopoDS_Iterator anItr(theShape);
58 for (; anItr.More(); anItr.Next()) {
59 ShapeToVTK( anItr.Value(),theEdgeMap,theIsVector,
60 theStandaloneVertexSource,
61 theIsolatedEdgeSource,
64 theWireframeFaceSource,
65 theShadingFaceSource );
69 switch (theShape.ShapeType()) {
71 TopExp_Explorer anEdgeExp(theShape,TopAbs_EDGE);
72 for (; anEdgeExp.More(); anEdgeExp.Next()){
73 const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
74 if (!BRep_Tool::Degenerated(anEdge))
75 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
80 const TopoDS_Edge& anEdge = TopoDS::Edge(theShape);
81 if (!BRep_Tool::Degenerated(anEdge))
82 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
86 if ( theStandaloneVertexSource ) {
87 const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
88 theStandaloneVertexSource->AddVertex(aVertex);
93 TopExp_Explorer aFaceExp (theShape,TopAbs_FACE);
94 for(; aFaceExp.More(); aFaceExp.Next()) {
95 const TopoDS_Face& aFace = TopoDS::Face(aFaceExp.Current());
96 theWireframeFaceSource->AddFace(aFace);
97 theShadingFaceSource->AddFace(aFace);
98 TopExp_Explorer anEdgeExp(aFaceExp.Current(), TopAbs_EDGE);
99 for(; anEdgeExp.More(); anEdgeExp.Next()) {
100 const TopoDS_Edge& anEdge = TopoDS::Edge(anEdgeExp.Current());
101 if(!BRep_Tool::Degenerated(anEdge)){
102 // compute the number of faces
103 int aNbOfFaces = theEdgeMap.FindFromKey(anEdge).Extent();
105 case 0: // isolated edge
106 theIsolatedEdgeSource->AddEdge(anEdge,theIsVector);
108 case 1: // edge in only one face
109 theOneFaceEdgeSource->AddEdge(anEdge,theIsVector);
111 default: // edge shared by at least two faces
112 theSharedEdgeSource->AddEdge(anEdge,theIsVector);
121 vtkPolyData* GetVTKData( const TopoDS_Shape& theShape, float theDeflection )
123 vtkPolyData* ret = 0;
125 BRepBuilderAPI_Copy aCopy(theShape);
126 if (aCopy.IsDone() ) {
128 TopoDS_Shape aShape = aCopy.Shape();
131 GEOM_VertexSource* myVertexSource = GEOM_VertexSource::New();
132 GEOM_EdgeSource* myIsolatedEdgeSource = GEOM_EdgeSource::New();
133 GEOM_EdgeSource* myOneFaceEdgeSource = GEOM_EdgeSource::New();
134 GEOM_EdgeSource* mySharedEdgeSource = GEOM_EdgeSource::New();
135 GEOM_WireframeFace* myWireframeFaceSource = GEOM_WireframeFace::New();
136 GEOM_ShadingFace* myShadingFaceSource = GEOM_ShadingFace::New();
138 vtkAppendPolyData* myAppendFilter = vtkAppendPolyData::New();
139 myAppendFilter->AddInputConnection( myVertexSource->GetOutputPort() );
140 myAppendFilter->AddInputConnection( myIsolatedEdgeSource->GetOutputPort() );
141 myAppendFilter->AddInputConnection( myOneFaceEdgeSource->GetOutputPort() );
142 myAppendFilter->AddInputConnection( mySharedEdgeSource->GetOutputPort() );
143 myAppendFilter->AddInputConnection( myShadingFaceSource->GetOutputPort() );
145 bool anIsVector = false;
147 GEOMUtils::MeshShape( aShape, theDeflection );
148 TopExp_Explorer aVertexExp( aShape, TopAbs_VERTEX );
149 for( ; aVertexExp.More(); aVertexExp.Next() ) {
150 const TopoDS_Vertex& aVertex = TopoDS::Vertex( aVertexExp.Current() );
151 myVertexSource->AddVertex( aVertex );
154 TopTools_IndexedDataMapOfShapeListOfShape anEdgeMap;
155 TopExp::MapShapesAndAncestors( aShape, TopAbs_EDGE, TopAbs_FACE, anEdgeMap );
161 myIsolatedEdgeSource,
164 myWireframeFaceSource,
165 myShadingFaceSource );
167 myAppendFilter->Update();
169 myVertexSource->Delete();
170 myIsolatedEdgeSource->Delete();
171 myOneFaceEdgeSource->Delete();
172 mySharedEdgeSource->Delete();
173 myWireframeFaceSource->Delete();
174 myShadingFaceSource->Delete();
176 ret = vtkPolyData::New();
177 ret->ShallowCopy(myAppendFilter->GetOutput());
178 myAppendFilter->Delete();
180 catch(Standard_Failure&) {