1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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
22 // GEOM OBJECT : interactive object for Geometry entities visualization
23 // File : GEOM_AssemblyBuilder.cxx
24 // Author : Christophe ATTANASIO
29 \class GEOM_AssemblyBuilder GEOM_AssemblyBuilder.h
33 #include "GEOM_AssemblyBuilder.h"
34 #include "GEOM_Actor.h"
36 #include <SUIT_Session.h>
38 #include <vtkProperty.h>
40 // Open CASCADE Includes
41 #include <TopExp_Explorer.hxx>
42 #include <Bnd_Box.hxx>
43 #include <BRepMesh_IncrementalMesh.hxx>
44 #include <Poly_Triangulation.hxx>
45 #include <BRepBndLib.hxx>
46 #include <BRep_Tool.hxx>
47 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopoDS_Iterator.hxx>
52 #include "utilities.h"
57 #define MAX2(X, Y) ( Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
58 #define MAX3(X, Y, Z) ( MAX2 ( MAX2(X,Y) , Z) )
64 void GEOM_AssemblyBuilder::InitProperties(vtkProperty* IsoProp,
65 vtkProperty* FaceProp,
66 vtkProperty* EdgeFProp,
67 vtkProperty* EdgeSProp,
68 vtkProperty* EdgeIProp,
69 vtkProperty* VertexProp,
70 vtkProperty* IsoPVProp,
71 vtkProperty* EdgePVProp,
72 vtkProperty* VertexPVProp)
74 // Shading like default OCC material
75 FaceProp->SetRepresentationToSurface();
76 FaceProp->SetInterpolationToGouraud();
77 FaceProp->SetAmbient(1.0);
78 FaceProp->SetDiffuse(1.0);
79 FaceProp->SetSpecular(0.4);
80 FaceProp->SetAmbientColor(0.329412, 0.223529, 0.027451);
81 FaceProp->SetDiffuseColor(0.780392, 0.568627, 0.113725);
82 FaceProp->SetSpecularColor(0.992157, 0.941176, 0.807843);
84 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
88 aColor = aResMgr->colorValue( "Geometry", "isos_color", QColor( int(0.5*255), int(0.5*255), int(0.5*255) ) );
89 float red = aColor.red()/255.0;
90 float green = aColor.green()/255.0;
91 float blue = aColor.blue()/255.0;
92 IsoProp->SetRepresentationToWireframe();
93 IsoProp->SetAmbientColor(red, green, blue);
94 IsoProp->SetDiffuseColor(red, green, blue);
95 IsoProp->SetSpecularColor(red, green, blue);
98 IsoPVProp->SetRepresentationToWireframe();
99 IsoPVProp->SetAmbientColor(0, 1, 1);
100 IsoPVProp->SetDiffuseColor(0, 1, 1);
101 IsoPVProp->SetSpecularColor(0, 1, 1);
103 // Wireframe for shared edge
104 aColor = aResMgr->colorValue( "Geometry", "wireframe_color", QColor( 255, 255, 0 ) );
105 red = aColor.red()/255.0;
106 green = aColor.green()/255.0;
107 blue = aColor.blue()/255.0;
108 EdgeSProp->SetRepresentationToWireframe();
109 EdgeSProp->SetAmbientColor(red, green, blue);
110 EdgeSProp->SetDiffuseColor(red, green, blue);
111 EdgeSProp->SetSpecularColor(red, green, blue);
113 // Wireframe for free edge
114 aColor = aResMgr->colorValue( "Geometry", "free_bound_color", QColor( 0, 255, 0 ) );
115 red = aColor.red()/255.0;
116 green = aColor.green()/255.0;
117 blue = aColor.blue()/255.0;
118 EdgeFProp->SetRepresentationToWireframe();
119 EdgeFProp->SetAmbientColor(red, green, blue);
120 EdgeFProp->SetDiffuseColor(red, green, blue);
121 EdgeFProp->SetSpecularColor(red, green, blue);
123 // Wireframe for isolated edge
124 aColor = aResMgr->colorValue( "Geometry", "line_color", QColor( 255, 0, 0 ) );
125 red = aColor.red()/255.0;
126 green = aColor.green()/255.0;
127 blue = aColor.blue()/255.0;
128 EdgeIProp->SetRepresentationToWireframe();
129 EdgeIProp->SetAmbientColor(red, green, blue);
130 EdgeIProp->SetDiffuseColor(red, green, blue);
132 // Wireframe for Preview edge
133 EdgePVProp->SetRepresentationToWireframe();
134 EdgePVProp->SetAmbientColor(1, 1, 0);
135 EdgePVProp->SetDiffuseColor(1, 1, 0);
136 EdgePVProp->SetSpecularColor(1, 1, 0);
138 // Wireframe for vertex
139 aColor = aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) );
140 red = aColor.red()/255.0;
141 green = aColor.green()/255.0;
142 blue = aColor.blue()/255.0;
143 VertexProp->SetRepresentationToWireframe();
144 VertexProp->SetAmbientColor(red, green, blue);
145 VertexProp->SetDiffuseColor(red, green, blue);
146 VertexProp->SetSpecularColor(red, green, blue);
148 // Wireframe for vertex
149 VertexPVProp->SetRepresentationToWireframe();
150 VertexPVProp->SetAmbientColor(0, 1, 1);
151 VertexPVProp->SetDiffuseColor(0, 1, 1);
152 VertexPVProp->SetSpecularColor(0, 1, 1);
156 void GEOM_AssemblyBuilder::MeshShape(const TopoDS_Shape myShape,
157 Standard_Real deflection,
158 Standard_Boolean forced)
160 // Mesh the shape if necessary
161 Standard_Boolean alreadymesh = Standard_True;
163 TopLoc_Location aLoc;
165 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
166 const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
167 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
168 if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
171 if(!alreadymesh || forced) {
173 // Compute default deflection
175 BRepBndLib::Add(myShape, B);
176 if ( B.IsVoid() ) return; // NPAL15983 (Bug when displaying empty groups)
177 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
178 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
179 deflection = MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) * 0.001 *4;
181 BRepMesh_IncrementalMesh MESH(myShape,deflection);
187 vtkActorCollection* GEOM_AssemblyBuilder::BuildActors(const TopoDS_Shape& myShape,
188 Standard_Real deflection,
189 Standard_Integer mode,
190 Standard_Boolean forced,
191 Standard_Boolean isVector)
194 vtkProperty* IsoProp = vtkProperty::New();
195 vtkProperty* FaceProp = vtkProperty::New();
196 vtkProperty* EdgeFProp = vtkProperty::New();
197 vtkProperty* EdgeSProp = vtkProperty::New();
198 vtkProperty* EdgeIProp = vtkProperty::New();
199 vtkProperty* VertexProp = vtkProperty::New();
200 vtkProperty* IsoPVProp = vtkProperty::New();
201 vtkProperty* EdgePVProp = vtkProperty::New();
202 vtkProperty* VertexPVProp = vtkProperty::New();
203 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
206 vtkActorCollection* AISActors = vtkActorCollection::New();
207 MeshShape(myShape,deflection,forced);
208 GEOM_Actor* aGeomActor = GEOM_Actor::New();
209 aGeomActor->SetShape(myShape,(float)deflection,false,isVector);
210 AISActors->AddItem(aGeomActor);
212 aGeomActor->Delete();
215 if(myShape.ShapeType() == TopAbs_COMPOUND) {
216 TopoDS_Iterator anItr(myShape);
217 for(; anItr.More(); anItr.Next()) {
218 vtkActorCollection* theActors =
219 GEOM_AssemblyBuilder::BuildActors(anItr.Value(), deflection, mode, forced);
220 theActors->InitTraversal();
221 vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
222 while(!(anActor==NULL)) {
223 AISActors->AddItem(anActor);
224 anActor = (vtkActor*)theActors->GetNextActor();
228 // Create graphics properties
230 vtkProperty* IsoProp = vtkProperty::New();
231 vtkProperty* FaceProp = vtkProperty::New();
232 vtkProperty* EdgeFProp = vtkProperty::New();
233 vtkProperty* EdgeSProp = vtkProperty::New();
234 vtkProperty* EdgeIProp = vtkProperty::New();
235 vtkProperty* VertexProp = vtkProperty::New();
237 vtkProperty* IsoPVProp = vtkProperty::New();
238 vtkProperty* EdgePVProp = vtkProperty::New();
239 vtkProperty* VertexPVProp = vtkProperty::New();
241 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
243 MeshShape(myShape,deflection,forced);
245 if ( myShape.ShapeType() <= 4 && myShape.ShapeType() != TopAbs_COMPOUND) {
248 // look if edges are free or shared
249 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
250 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
254 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
256 GEOM_Actor* FaceActor = GEOM_Actor::New();
257 FaceActor->SetShadingProperty(FaceProp);
258 FaceActor->SetWireframeProperty(IsoProp);
260 FaceActor->SetPreviewProperty(IsoPVProp);
262 FaceActor->setInputShape(ex.Current(),deflection,mode);
264 AISActors->AddItem(FaceActor);
267 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
268 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
270 if (BRep_Tool::Degenerated(aEdge)) {
274 // compute the number of faces
275 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
276 GEOM_Actor* EdgeActor = GEOM_Actor::New();
277 EdgeActor->SubShapeOn();
278 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
282 case 0 : // isolated edge
284 EdgeActor->SetShadingProperty(EdgeIProp);
285 EdgeActor->SetWireframeProperty(EdgeIProp);
289 case 1 :// edge in only one face
291 EdgeActor->SetShadingProperty(EdgeFProp);
292 EdgeActor->SetWireframeProperty(EdgeFProp);
296 default : // edge shared by at least two faces
298 EdgeActor->SetShadingProperty(EdgeSProp);
299 EdgeActor->SetWireframeProperty(EdgeSProp);
303 EdgeActor->SetPreviewProperty(EdgePVProp);
304 AISActors->AddItem(EdgeActor);
307 } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
309 for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
310 const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
312 if (BRep_Tool::Degenerated(aEdge)) {
316 GEOM_Actor* EdgeActor = GEOM_Actor::New();
317 EdgeActor->setInputShape(ex.Current(),deflection,mode);
318 EdgeActor->SetShadingProperty(EdgeIProp);
319 EdgeActor->SetWireframeProperty(EdgeIProp);
320 EdgeActor->SetPreviewProperty(EdgePVProp);
322 AISActors->AddItem(EdgeActor);
324 } else if ( myShape.ShapeType() == TopAbs_EDGE ) { // EDGE Actor
325 GEOM_Actor* EdgeActor = GEOM_Actor::New();
326 EdgeActor->setInputShape(myShape,deflection,mode,isVector);
327 EdgeActor->SetShadingProperty(EdgeIProp);
328 EdgeActor->SetWireframeProperty(EdgeIProp);
329 EdgeActor->SetPreviewProperty(EdgePVProp);
331 AISActors->AddItem(EdgeActor);
332 } else if ( myShape.ShapeType() == TopAbs_VERTEX ) { // VERTEX Actor
333 GEOM_Actor* VertexActor = GEOM_Actor::New();
334 VertexActor->setInputShape(myShape,deflection,mode);
335 VertexActor->SetShadingProperty(VertexProp);
336 VertexActor->SetWireframeProperty(VertexProp);
337 VertexActor->SetPreviewProperty(VertexPVProp);
339 AISActors->AddItem(VertexActor);
351 //-------------------------------------------------------------
353 //-------------------------------------------------------------
354 vtkAssembly* GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
355 Standard_Real deflection,
356 Standard_Integer mode,
357 Standard_Boolean forced)
359 // Create a new vtkAssembly
361 vtkAssembly* myVTKShape = vtkAssembly::New();
363 // Create graphics properties
365 vtkProperty* IsoProp = vtkProperty::New();
366 vtkProperty* FaceProp = vtkProperty::New();
367 vtkProperty* EdgeFProp = vtkProperty::New();
368 vtkProperty* EdgeSProp = vtkProperty::New();
369 vtkProperty* EdgeIProp = vtkProperty::New();
370 vtkProperty* VertexProp = vtkProperty::New();
371 vtkProperty* EdgePVProp = vtkProperty::New();
372 vtkProperty* VertexPVProp = vtkProperty::New();
373 vtkProperty* IsoPVProp = vtkProperty::New();
375 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
377 MeshShape(myShape,deflection,forced);
382 // look if edges are free or shared
383 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
384 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
388 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
389 //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
391 GEOM_Actor* FaceActor = GEOM_Actor::New();
392 FaceActor->SetShadingProperty(FaceProp);
393 FaceActor->SetWireframeProperty(IsoProp);
395 vtkAssembly* myFaceAssembly = vtkAssembly::New();
398 FaceActor->setInputShape(ex.Current(),deflection,mode);
399 myFaceAssembly->AddPart(FaceActor);
402 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
403 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
405 if (BRep_Tool::Degenerated(aEdge)) {
410 // compute the number of faces
411 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
412 GEOM_Actor* EdgeActor = GEOM_Actor::New();
415 case 0 : // isolated edge
417 EdgeActor->SetShadingProperty(EdgeIProp);
418 EdgeActor->SetWireframeProperty(EdgeIProp);
422 case 1 :// edge in only one face
424 EdgeActor->SetShadingProperty(EdgeFProp);
425 EdgeActor->SetWireframeProperty(EdgeFProp);
429 default : // edge shared by at least two faces
431 EdgeActor->SetShadingProperty(EdgeSProp);
432 EdgeActor->SetWireframeProperty(EdgeSProp);
436 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
437 myFaceAssembly->AddPart(EdgeActor);
439 myVTKShape->AddPart(myFaceAssembly);
448 //-------------------------------------------------------------
449 // CHANGE SPECIFIC DISPLAY MODE
450 //-------------------------------------------------------------
451 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
455 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
459 //-------------------------------------------------------------
461 //-------------------------------------------------------------
463 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
468 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)