1 // GEOM OBJECT : interactive object for Geometry entities visualization
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : GEOM_AssemblyBuilder.cxx
25 // Author : Christophe ATTANASIO
30 \class GEOM_AssemblyBuilder GEOM_AssemblyBuilder.h
34 #include "GEOM_AssemblyBuilder.h"
35 #include "GEOM_Actor.h"
36 #include "utilities.h"
38 // Open CASCADE Includes
39 #include <TopExp_Explorer.hxx>
40 #include <Bnd_Box.hxx>
41 #include <BRepMesh_IncrementalMesh.hxx>
42 #include <Poly_Triangulation.hxx>
43 #include <BRepBndLib.hxx>
44 #include <BRep_Tool.hxx>
45 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
47 #include <TopTools_ListOfShape.hxx>
48 #include <TopoDS_Iterator.hxx>
53 #define MAX2(X, Y) ( Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
54 #define MAX3(X, Y, Z) ( MAX2 ( MAX2(X,Y) , Z) )
60 void GEOM_AssemblyBuilder::InitProperties(vtkProperty* IsoProp,
61 vtkProperty* FaceProp,
62 vtkProperty* EdgeFProp,
63 vtkProperty* EdgeSProp,
64 vtkProperty* EdgeIProp,
65 vtkProperty* VertexProp,
66 vtkProperty* IsoPVProp,
67 vtkProperty* EdgePVProp,
68 vtkProperty* VertexPVProp)
70 // Shading like default OCC material
71 FaceProp->SetRepresentationToSurface();
72 FaceProp->SetInterpolationToGouraud();
73 FaceProp->SetAmbient(1.0);
74 FaceProp->SetDiffuse(1.0);
75 FaceProp->SetSpecular(0.4);
76 FaceProp->SetAmbientColor(0.329412, 0.223529, 0.027451);
77 FaceProp->SetDiffuseColor(0.780392, 0.568627, 0.113725);
78 FaceProp->SetSpecularColor(0.992157, 0.941176, 0.807843);
81 IsoProp->SetRepresentationToWireframe();
82 IsoProp->SetAmbientColor(0.5, 0.5, 0.5);
83 IsoProp->SetDiffuseColor(0.5, 0.5, 0.5);
84 IsoProp->SetSpecularColor(0.5, 0.5, 0.5);
87 IsoPVProp->SetRepresentationToWireframe();
88 IsoPVProp->SetAmbientColor(0, 1, 1);
89 IsoPVProp->SetDiffuseColor(0, 1, 1);
90 IsoPVProp->SetSpecularColor(0, 1, 1);
92 // Wireframe for shared edge
93 EdgeSProp->SetRepresentationToWireframe();
94 EdgeSProp->SetAmbientColor(1, 1, 0);
95 EdgeSProp->SetDiffuseColor(1, 1, 0);
96 EdgeSProp->SetSpecularColor(1, 1, 0);
98 // Wireframe for free edge
99 EdgeFProp->SetRepresentationToWireframe();
100 EdgeFProp->SetAmbientColor(0, 1, 0);
101 EdgeFProp->SetDiffuseColor(0, 1, 0);
102 EdgeFProp->SetSpecularColor(0, 1, 0);
104 // Wireframe for isolated edge
105 EdgeIProp->SetRepresentationToWireframe();
106 EdgeIProp->SetAmbientColor(1, 0, 0);
107 EdgeIProp->SetDiffuseColor(1, 0, 0);
108 EdgeIProp->SetSpecularColor(1, 0, 0);
110 // Wireframe for Preview edge
111 EdgePVProp->SetRepresentationToWireframe();
112 EdgePVProp->SetAmbientColor(1, 1, 0);
113 EdgePVProp->SetDiffuseColor(1, 1, 0);
114 EdgePVProp->SetSpecularColor(1, 1, 0);
116 // Wireframe for vertex
117 VertexProp->SetRepresentationToWireframe();
118 VertexProp->SetAmbientColor(1, 1, 0);
119 VertexProp->SetDiffuseColor(1, 1, 0);
120 VertexProp->SetSpecularColor(1, 1, 0);
122 // Wireframe for vertex
123 VertexPVProp->SetRepresentationToWireframe();
124 VertexPVProp->SetAmbientColor(0, 1, 1);
125 VertexPVProp->SetDiffuseColor(0, 1, 1);
126 VertexPVProp->SetSpecularColor(0, 1, 1);
130 void GEOM_AssemblyBuilder::MeshShape(const TopoDS_Shape myShape,
131 Standard_Real deflection,
132 Standard_Boolean forced)
134 // Mesh the shape if necessary
135 Standard_Boolean alreadymesh = Standard_True;
137 TopLoc_Location aLoc;
139 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
140 const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
141 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
142 if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
145 if(!alreadymesh || forced) {
147 // Compute default deflection
149 BRepBndLib::Add(myShape, B);
150 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
151 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
152 deflection = MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) * 0.001 *4;
154 BRepMesh_IncrementalMesh MESH(myShape,deflection);
160 vtkActorCollection* GEOM_AssemblyBuilder::BuildActors(const TopoDS_Shape& myShape,
161 Standard_Real deflection,
162 Standard_Integer mode,
163 Standard_Boolean forced) {
165 vtkActorCollection* AISActors = vtkActorCollection::New();
167 if(myShape.ShapeType() == TopAbs_COMPOUND) {
168 TopoDS_Iterator anItr(myShape);
169 for(; anItr.More(); anItr.Next()) {
170 vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(anItr.Value(), deflection, mode, forced);
171 theActors->InitTraversal();
172 vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
173 while(!(anActor==NULL)) {
174 AISActors->AddItem(anActor);
175 anActor = (vtkActor*)theActors->GetNextActor();
179 // Create graphics properties
181 vtkProperty* IsoProp = vtkProperty::New();
182 vtkProperty* FaceProp = vtkProperty::New();
183 vtkProperty* EdgeFProp = vtkProperty::New();
184 vtkProperty* EdgeSProp = vtkProperty::New();
185 vtkProperty* EdgeIProp = vtkProperty::New();
186 vtkProperty* VertexProp = vtkProperty::New();
188 vtkProperty* IsoPVProp = vtkProperty::New();
189 vtkProperty* EdgePVProp = vtkProperty::New();
190 vtkProperty* VertexPVProp = vtkProperty::New();
192 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
194 MeshShape(myShape,deflection,forced);
196 if ( myShape.ShapeType() <= 4 && myShape.ShapeType() != TopAbs_COMPOUND) {
199 // look if edges are free or shared
200 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
201 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
205 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
207 GEOM_Actor* FaceActor = GEOM_Actor::New();
208 FaceActor->SetShadingProperty(FaceProp);
209 FaceActor->SetWireframeProperty(IsoProp);
211 FaceActor->SetPreviewProperty(IsoPVProp);
213 FaceActor->setInputShape(ex.Current(),deflection,mode);
215 AISActors->AddItem(FaceActor);
218 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
219 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
221 if (BRep_Tool::Degenerated(aEdge)) {
225 // compute the number of faces
226 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
227 GEOM_Actor* EdgeActor = GEOM_Actor::New();
228 EdgeActor->SubShapeOn();
229 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
232 case 0 : // isolated edge
234 EdgeActor->SetShadingProperty(EdgeIProp);
235 EdgeActor->SetWireframeProperty(EdgeIProp);
239 case 1 :// edge in only one face
241 EdgeActor->SetShadingProperty(EdgeFProp);
242 EdgeActor->SetWireframeProperty(EdgeFProp);
246 default : // edge shared by at least two faces
248 EdgeActor->SetShadingProperty(EdgeSProp);
249 EdgeActor->SetWireframeProperty(EdgeSProp);
252 EdgeActor->SetPreviewProperty(EdgePVProp);
253 AISActors->AddItem(EdgeActor);
256 } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
258 for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
259 const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
261 if (BRep_Tool::Degenerated(aEdge)) {
265 GEOM_Actor* EdgeActor = GEOM_Actor::New();
266 EdgeActor->setInputShape(ex.Current(),deflection,mode);
267 EdgeActor->SetShadingProperty(EdgeIProp);
268 EdgeActor->SetWireframeProperty(EdgeIProp);
269 EdgeActor->SetPreviewProperty(EdgePVProp);
271 AISActors->AddItem(EdgeActor);
273 } else if ( myShape.ShapeType() == TopAbs_EDGE ) { // EDGE Actor
274 GEOM_Actor* EdgeActor = GEOM_Actor::New();
275 EdgeActor->setInputShape(myShape,deflection,mode);
276 EdgeActor->SetShadingProperty(EdgeIProp);
277 EdgeActor->SetWireframeProperty(EdgeIProp);
278 EdgeActor->SetPreviewProperty(EdgePVProp);
280 AISActors->AddItem(EdgeActor);
281 } else if ( myShape.ShapeType() == TopAbs_VERTEX ) { // VERTEX Actor
282 GEOM_Actor* VertexActor = GEOM_Actor::New();
283 VertexActor->setInputShape(myShape,deflection,mode);
284 VertexActor->SetShadingProperty(VertexProp);
285 VertexActor->SetWireframeProperty(VertexProp);
286 VertexActor->SetPreviewProperty(VertexPVProp);
288 AISActors->AddItem(VertexActor);
298 //-------------------------------------------------------------
300 //-------------------------------------------------------------
301 vtkAssembly* GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
302 Standard_Real deflection,
303 Standard_Integer mode,
304 Standard_Boolean forced)
306 // Create a new vtkAssembly
308 vtkAssembly* myVTKShape = vtkAssembly::New();
311 // Create graphics properties
313 vtkProperty* IsoProp = vtkProperty::New();
314 vtkProperty* FaceProp = vtkProperty::New();
315 vtkProperty* EdgeFProp = vtkProperty::New();
316 vtkProperty* EdgeSProp = vtkProperty::New();
317 vtkProperty* EdgeIProp = vtkProperty::New();
318 vtkProperty* VertexProp = vtkProperty::New();
319 vtkProperty* EdgePVProp = vtkProperty::New();
320 vtkProperty* VertexPVProp = vtkProperty::New();
321 vtkProperty* IsoPVProp = vtkProperty::New();
323 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
325 MeshShape(myShape,deflection,forced);
330 // look if edges are free or shared
331 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
332 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
336 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
337 //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
339 GEOM_Actor* FaceActor = GEOM_Actor::New();
340 FaceActor->SetShadingProperty(FaceProp);
341 FaceActor->SetWireframeProperty(IsoProp);
343 vtkAssembly* myFaceAssembly = vtkAssembly::New();
346 FaceActor->setInputShape(ex.Current(),deflection,mode);
347 myFaceAssembly->AddPart(FaceActor);
350 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
351 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
353 if (BRep_Tool::Degenerated(aEdge)) {
358 // compute the number of faces
359 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
360 GEOM_Actor* EdgeActor = GEOM_Actor::New();
363 case 0 : // isolated edge
365 EdgeActor->SetShadingProperty(EdgeIProp);
366 EdgeActor->SetWireframeProperty(EdgeIProp);
370 case 1 :// edge in only one face
372 EdgeActor->SetShadingProperty(EdgeFProp);
373 EdgeActor->SetWireframeProperty(EdgeFProp);
377 default : // edge shared by at least two faces
379 EdgeActor->SetShadingProperty(EdgeSProp);
380 EdgeActor->SetWireframeProperty(EdgeSProp);
384 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
385 myFaceAssembly->AddPart(EdgeActor);
387 myVTKShape->AddPart(myFaceAssembly);
394 //-------------------------------------------------------------
395 // CHANGE SPECIFIC DISPLAY MODE
396 //-------------------------------------------------------------
397 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
401 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
405 //-------------------------------------------------------------
407 //-------------------------------------------------------------
409 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
414 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)