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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
37 #include <SUIT_Session.h>
39 #include <vtkProperty.h>
41 // Open CASCADE Includes
42 #include <TopExp_Explorer.hxx>
43 #include <Bnd_Box.hxx>
44 #include <BRepMesh_IncrementalMesh.hxx>
45 #include <Poly_Triangulation.hxx>
46 #include <BRepBndLib.hxx>
47 #include <BRep_Tool.hxx>
48 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
50 #include <TopTools_ListOfShape.hxx>
51 #include <TopoDS_Iterator.hxx>
53 #include "utilities.h"
58 #define MAX2(X, Y) ( Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
59 #define MAX3(X, Y, Z) ( MAX2 ( MAX2(X,Y) , Z) )
65 void GEOM_AssemblyBuilder::InitProperties(vtkProperty* IsoProp,
66 vtkProperty* FaceProp,
67 vtkProperty* EdgeFProp,
68 vtkProperty* EdgeSProp,
69 vtkProperty* EdgeIProp,
70 vtkProperty* VertexProp,
71 vtkProperty* IsoPVProp,
72 vtkProperty* EdgePVProp,
73 vtkProperty* VertexPVProp)
75 // Shading like default OCC material
76 FaceProp->SetRepresentationToSurface();
77 FaceProp->SetInterpolationToGouraud();
78 FaceProp->SetAmbient(1.0);
79 FaceProp->SetDiffuse(1.0);
80 FaceProp->SetSpecular(0.4);
81 FaceProp->SetAmbientColor(0.329412, 0.223529, 0.027451);
82 FaceProp->SetDiffuseColor(0.780392, 0.568627, 0.113725);
83 FaceProp->SetSpecularColor(0.992157, 0.941176, 0.807843);
85 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
89 aColor = aResMgr->colorValue( "Geometry", "isos_color", QColor( int(0.5*255), int(0.5*255), int(0.5*255) ) );
90 float red = aColor.red()/255.0;
91 float green = aColor.green()/255.0;
92 float blue = aColor.blue()/255.0;
93 IsoProp->SetRepresentationToWireframe();
94 IsoProp->SetAmbientColor(red, green, blue);
95 IsoProp->SetDiffuseColor(red, green, blue);
96 IsoProp->SetSpecularColor(red, green, blue);
99 IsoPVProp->SetRepresentationToWireframe();
100 IsoPVProp->SetAmbientColor(0, 1, 1);
101 IsoPVProp->SetDiffuseColor(0, 1, 1);
102 IsoPVProp->SetSpecularColor(0, 1, 1);
104 // Wireframe for shared edge
105 aColor = aResMgr->colorValue( "Geometry", "wireframe_color", QColor( 255, 255, 0 ) );
106 red = aColor.red()/255.0;
107 green = aColor.green()/255.0;
108 blue = aColor.blue()/255.0;
109 EdgeSProp->SetRepresentationToWireframe();
110 EdgeSProp->SetAmbientColor(red, green, blue);
111 EdgeSProp->SetDiffuseColor(red, green, blue);
112 EdgeSProp->SetSpecularColor(red, green, blue);
114 // Wireframe for free edge
115 aColor = aResMgr->colorValue( "Geometry", "free_bound_color", QColor( 0, 255, 0 ) );
116 red = aColor.red()/255.0;
117 green = aColor.green()/255.0;
118 blue = aColor.blue()/255.0;
119 EdgeFProp->SetRepresentationToWireframe();
120 EdgeFProp->SetAmbientColor(red, green, blue);
121 EdgeFProp->SetDiffuseColor(red, green, blue);
122 EdgeFProp->SetSpecularColor(red, green, blue);
124 // Wireframe for isolated edge
125 aColor = aResMgr->colorValue( "Geometry", "line_color", QColor( 255, 0, 0 ) );
126 red = aColor.red()/255.0;
127 green = aColor.green()/255.0;
128 blue = aColor.blue()/255.0;
129 EdgeIProp->SetRepresentationToWireframe();
130 EdgeIProp->SetAmbientColor(red, green, blue);
131 EdgeIProp->SetDiffuseColor(red, green, blue);
133 // Wireframe for Preview edge
134 EdgePVProp->SetRepresentationToWireframe();
135 EdgePVProp->SetAmbientColor(1, 1, 0);
136 EdgePVProp->SetDiffuseColor(1, 1, 0);
137 EdgePVProp->SetSpecularColor(1, 1, 0);
139 // Wireframe for vertex
140 aColor = aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) );
141 red = aColor.red()/255.0;
142 green = aColor.green()/255.0;
143 blue = aColor.blue()/255.0;
144 VertexProp->SetRepresentationToWireframe();
145 VertexProp->SetAmbientColor(red, green, blue);
146 VertexProp->SetDiffuseColor(red, green, blue);
147 VertexProp->SetSpecularColor(red, green, blue);
149 // Wireframe for vertex
150 VertexPVProp->SetRepresentationToWireframe();
151 VertexPVProp->SetAmbientColor(0, 1, 1);
152 VertexPVProp->SetDiffuseColor(0, 1, 1);
153 VertexPVProp->SetSpecularColor(0, 1, 1);
157 void GEOM_AssemblyBuilder::MeshShape(const TopoDS_Shape myShape,
158 Standard_Real deflection,
159 Standard_Boolean forced)
161 // Mesh the shape if necessary
162 Standard_Boolean alreadymesh = Standard_True;
164 TopLoc_Location aLoc;
166 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
167 const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
168 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
169 if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
172 if(!alreadymesh || forced) {
174 // Compute default deflection
176 BRepBndLib::Add(myShape, B);
177 if ( B.IsVoid() ) return; // NPAL15983 (Bug when displaying empty groups)
178 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
179 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
180 deflection = MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) * 0.001 *4;
182 BRepMesh_IncrementalMesh MESH(myShape,deflection);
188 vtkActorCollection* GEOM_AssemblyBuilder::BuildActors(const TopoDS_Shape& myShape,
189 Standard_Real deflection,
190 Standard_Integer mode,
191 Standard_Boolean forced,
192 Standard_Boolean isVector)
194 vtkActorCollection* AISActors = vtkActorCollection::New();
196 if(myShape.ShapeType() == TopAbs_COMPOUND) {
197 TopoDS_Iterator anItr(myShape);
198 for(; anItr.More(); anItr.Next()) {
199 vtkActorCollection* theActors =
200 GEOM_AssemblyBuilder::BuildActors(anItr.Value(), deflection, mode, forced);
201 theActors->InitTraversal();
202 vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
203 while(!(anActor==NULL)) {
204 AISActors->AddItem(anActor);
205 anActor = (vtkActor*)theActors->GetNextActor();
209 // Create graphics properties
211 vtkProperty* IsoProp = vtkProperty::New();
212 vtkProperty* FaceProp = vtkProperty::New();
213 vtkProperty* EdgeFProp = vtkProperty::New();
214 vtkProperty* EdgeSProp = vtkProperty::New();
215 vtkProperty* EdgeIProp = vtkProperty::New();
216 vtkProperty* VertexProp = vtkProperty::New();
218 vtkProperty* IsoPVProp = vtkProperty::New();
219 vtkProperty* EdgePVProp = vtkProperty::New();
220 vtkProperty* VertexPVProp = vtkProperty::New();
222 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
224 MeshShape(myShape,deflection,forced);
226 if ( myShape.ShapeType() <= 4 && myShape.ShapeType() != TopAbs_COMPOUND) {
229 // look if edges are free or shared
230 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
231 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
235 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
237 GEOM_Actor* FaceActor = GEOM_Actor::New();
238 FaceActor->SetShadingProperty(FaceProp);
239 FaceActor->SetWireframeProperty(IsoProp);
241 FaceActor->SetPreviewProperty(IsoPVProp);
243 FaceActor->setInputShape(ex.Current(),deflection,mode);
245 AISActors->AddItem(FaceActor);
248 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
249 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
251 if (BRep_Tool::Degenerated(aEdge)) {
255 // compute the number of faces
256 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
257 GEOM_Actor* EdgeActor = GEOM_Actor::New();
258 EdgeActor->SubShapeOn();
259 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
263 case 0 : // isolated edge
265 EdgeActor->SetShadingProperty(EdgeIProp);
266 EdgeActor->SetWireframeProperty(EdgeIProp);
270 case 1 :// edge in only one face
272 EdgeActor->SetShadingProperty(EdgeFProp);
273 EdgeActor->SetWireframeProperty(EdgeFProp);
277 default : // edge shared by at least two faces
279 EdgeActor->SetShadingProperty(EdgeSProp);
280 EdgeActor->SetWireframeProperty(EdgeSProp);
284 EdgeActor->SetPreviewProperty(EdgePVProp);
285 AISActors->AddItem(EdgeActor);
288 } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
290 for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
291 const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
293 if (BRep_Tool::Degenerated(aEdge)) {
297 GEOM_Actor* EdgeActor = GEOM_Actor::New();
298 EdgeActor->setInputShape(ex.Current(),deflection,mode);
299 EdgeActor->SetShadingProperty(EdgeIProp);
300 EdgeActor->SetWireframeProperty(EdgeIProp);
301 EdgeActor->SetPreviewProperty(EdgePVProp);
303 AISActors->AddItem(EdgeActor);
305 } else if ( myShape.ShapeType() == TopAbs_EDGE ) { // EDGE Actor
306 GEOM_Actor* EdgeActor = GEOM_Actor::New();
307 EdgeActor->setInputShape(myShape,deflection,mode,isVector);
308 EdgeActor->SetShadingProperty(EdgeIProp);
309 EdgeActor->SetWireframeProperty(EdgeIProp);
310 EdgeActor->SetPreviewProperty(EdgePVProp);
312 AISActors->AddItem(EdgeActor);
313 } else if ( myShape.ShapeType() == TopAbs_VERTEX ) { // VERTEX Actor
314 GEOM_Actor* VertexActor = GEOM_Actor::New();
315 VertexActor->setInputShape(myShape,deflection,mode);
316 VertexActor->SetShadingProperty(VertexProp);
317 VertexActor->SetWireframeProperty(VertexProp);
318 VertexActor->SetPreviewProperty(VertexPVProp);
320 AISActors->AddItem(VertexActor);
330 //-------------------------------------------------------------
332 //-------------------------------------------------------------
333 vtkAssembly* GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
334 Standard_Real deflection,
335 Standard_Integer mode,
336 Standard_Boolean forced)
338 // Create a new vtkAssembly
340 vtkAssembly* myVTKShape = vtkAssembly::New();
343 // Create graphics properties
345 vtkProperty* IsoProp = vtkProperty::New();
346 vtkProperty* FaceProp = vtkProperty::New();
347 vtkProperty* EdgeFProp = vtkProperty::New();
348 vtkProperty* EdgeSProp = vtkProperty::New();
349 vtkProperty* EdgeIProp = vtkProperty::New();
350 vtkProperty* VertexProp = vtkProperty::New();
351 vtkProperty* EdgePVProp = vtkProperty::New();
352 vtkProperty* VertexPVProp = vtkProperty::New();
353 vtkProperty* IsoPVProp = vtkProperty::New();
355 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
357 MeshShape(myShape,deflection,forced);
362 // look if edges are free or shared
363 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
364 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
368 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
369 //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
371 GEOM_Actor* FaceActor = GEOM_Actor::New();
372 FaceActor->SetShadingProperty(FaceProp);
373 FaceActor->SetWireframeProperty(IsoProp);
375 vtkAssembly* myFaceAssembly = vtkAssembly::New();
378 FaceActor->setInputShape(ex.Current(),deflection,mode);
379 myFaceAssembly->AddPart(FaceActor);
382 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
383 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
385 if (BRep_Tool::Degenerated(aEdge)) {
390 // compute the number of faces
391 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
392 GEOM_Actor* EdgeActor = GEOM_Actor::New();
395 case 0 : // isolated edge
397 EdgeActor->SetShadingProperty(EdgeIProp);
398 EdgeActor->SetWireframeProperty(EdgeIProp);
402 case 1 :// edge in only one face
404 EdgeActor->SetShadingProperty(EdgeFProp);
405 EdgeActor->SetWireframeProperty(EdgeFProp);
409 default : // edge shared by at least two faces
411 EdgeActor->SetShadingProperty(EdgeSProp);
412 EdgeActor->SetWireframeProperty(EdgeSProp);
416 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
417 myFaceAssembly->AddPart(EdgeActor);
419 myVTKShape->AddPart(myFaceAssembly);
426 //-------------------------------------------------------------
427 // CHANGE SPECIFIC DISPLAY MODE
428 //-------------------------------------------------------------
429 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
433 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
437 //-------------------------------------------------------------
439 //-------------------------------------------------------------
441 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
446 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)