1 // Copyright (C) 2007-2010 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
23 // GEOM OBJECT : interactive object for Geometry entities visualization
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>
38 #include <SUIT_ResourceMgr.h>
40 #include <vtkProperty.h>
41 #include <vtkAssembly.h>
42 #include <vtkActorCollection.h>
44 // Open CASCADE Includes
45 #include <TopExp_Explorer.hxx>
46 #include <Bnd_Box.hxx>
47 #include <BRepMesh_IncrementalMesh.hxx>
48 #include <Poly_Triangulation.hxx>
49 #include <BRepBndLib.hxx>
50 #include <BRep_Tool.hxx>
51 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
53 #include <TopTools_ListOfShape.hxx>
54 #include <TopoDS_Iterator.hxx>
59 #include "utilities.h"
63 #define MAX2(X, Y) ( Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
64 #define MAX3(X, Y, Z) ( MAX2 ( MAX2(X,Y) , Z) )
70 void GEOM_AssemblyBuilder::InitProperties(vtkProperty* IsoProp,
71 vtkProperty* FaceProp,
72 vtkProperty* EdgeFProp,
73 vtkProperty* EdgeSProp,
74 vtkProperty* EdgeIProp,
75 vtkProperty* VertexProp,
76 vtkProperty* IsoPVProp,
77 vtkProperty* EdgePVProp,
78 vtkProperty* VertexPVProp)
80 // Shading like default OCC material
81 FaceProp->SetRepresentationToSurface();
82 FaceProp->SetInterpolationToGouraud();
83 FaceProp->SetAmbient(1.0);
84 FaceProp->SetDiffuse(1.0);
85 FaceProp->SetSpecular(0.4);
86 FaceProp->SetAmbientColor(0.329412, 0.223529, 0.027451);
87 FaceProp->SetDiffuseColor(0.780392, 0.568627, 0.113725);
88 FaceProp->SetSpecularColor(0.992157, 0.941176, 0.807843);
90 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
94 aColor = aResMgr->colorValue( "Geometry", "isos_color", QColor( int(0.5*255), int(0.5*255), int(0.5*255) ) );
95 float red = aColor.red()/255.0;
96 float green = aColor.green()/255.0;
97 float blue = aColor.blue()/255.0;
98 IsoProp->SetRepresentationToWireframe();
99 IsoProp->SetAmbientColor(red, green, blue);
100 IsoProp->SetDiffuseColor(red, green, blue);
101 IsoProp->SetSpecularColor(red, green, blue);
104 IsoPVProp->SetRepresentationToWireframe();
105 IsoPVProp->SetAmbientColor(0, 1, 1);
106 IsoPVProp->SetDiffuseColor(0, 1, 1);
107 IsoPVProp->SetSpecularColor(0, 1, 1);
109 // Wireframe for shared edge
110 aColor = aResMgr->colorValue( "Geometry", "wireframe_color", QColor( 255, 255, 0 ) );
111 red = aColor.red()/255.0;
112 green = aColor.green()/255.0;
113 blue = aColor.blue()/255.0;
114 EdgeSProp->SetRepresentationToWireframe();
115 EdgeSProp->SetAmbientColor(red, green, blue);
116 EdgeSProp->SetDiffuseColor(red, green, blue);
117 EdgeSProp->SetSpecularColor(red, green, blue);
119 // Wireframe for free edge
120 aColor = aResMgr->colorValue( "Geometry", "free_bound_color", QColor( 0, 255, 0 ) );
121 red = aColor.red()/255.0;
122 green = aColor.green()/255.0;
123 blue = aColor.blue()/255.0;
124 EdgeFProp->SetRepresentationToWireframe();
125 EdgeFProp->SetAmbientColor(red, green, blue);
126 EdgeFProp->SetDiffuseColor(red, green, blue);
127 EdgeFProp->SetSpecularColor(red, green, blue);
129 // Wireframe for isolated edge
130 aColor = aResMgr->colorValue( "Geometry", "line_color", QColor( 255, 0, 0 ) );
131 red = aColor.red()/255.0;
132 green = aColor.green()/255.0;
133 blue = aColor.blue()/255.0;
134 EdgeIProp->SetRepresentationToWireframe();
135 EdgeIProp->SetAmbientColor(red, green, blue);
136 EdgeIProp->SetDiffuseColor(red, green, blue);
138 // Wireframe for Preview edge
139 EdgePVProp->SetRepresentationToWireframe();
140 EdgePVProp->SetAmbientColor(1, 1, 0);
141 EdgePVProp->SetDiffuseColor(1, 1, 0);
142 EdgePVProp->SetSpecularColor(1, 1, 0);
144 // Wireframe for vertex
145 aColor = aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) );
146 red = aColor.red()/255.0;
147 green = aColor.green()/255.0;
148 blue = aColor.blue()/255.0;
149 VertexProp->SetRepresentationToWireframe();
150 VertexProp->SetAmbientColor(red, green, blue);
151 VertexProp->SetDiffuseColor(red, green, blue);
152 VertexProp->SetSpecularColor(red, green, blue);
154 // Wireframe for vertex
155 VertexPVProp->SetRepresentationToWireframe();
156 VertexPVProp->SetAmbientColor(0, 1, 1);
157 VertexPVProp->SetDiffuseColor(0, 1, 1);
158 VertexPVProp->SetSpecularColor(0, 1, 1);
162 void GEOM_AssemblyBuilder::MeshShape(const TopoDS_Shape myShape,
163 Standard_Real deflection,
164 Standard_Boolean forced)
166 // Mesh the shape if necessary
167 Standard_Boolean alreadymesh = Standard_True;
169 TopLoc_Location aLoc;
171 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
172 const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
173 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
174 if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
177 if(!alreadymesh || forced) {
179 // Compute default deflection
181 BRepBndLib::Add(myShape, B);
182 if ( B.IsVoid() ) return; // NPAL15983 (Bug when displaying empty groups)
183 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
184 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
185 deflection = MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) * 0.001 *4;
187 BRepMesh_IncrementalMesh MESH(myShape,deflection);
193 vtkActorCollection* GEOM_AssemblyBuilder::BuildActors(const TopoDS_Shape& myShape,
194 Standard_Real deflection,
195 Standard_Integer mode,
196 Standard_Boolean forced,
197 Standard_Boolean isVector)
200 vtkProperty* IsoProp = vtkProperty::New();
201 vtkProperty* FaceProp = vtkProperty::New();
202 vtkProperty* EdgeFProp = vtkProperty::New();
203 vtkProperty* EdgeSProp = vtkProperty::New();
204 vtkProperty* EdgeIProp = vtkProperty::New();
205 vtkProperty* VertexProp = vtkProperty::New();
206 vtkProperty* IsoPVProp = vtkProperty::New();
207 vtkProperty* EdgePVProp = vtkProperty::New();
208 vtkProperty* VertexPVProp = vtkProperty::New();
209 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
212 vtkActorCollection* AISActors = vtkActorCollection::New();
213 MeshShape(myShape,deflection,forced);
214 GEOM_Actor* aGeomActor = GEOM_Actor::New();
215 aGeomActor->SetShape(myShape,(float)deflection,false,isVector);
216 AISActors->AddItem(aGeomActor);
218 aGeomActor->Delete();
221 if(myShape.ShapeType() == TopAbs_COMPOUND) {
222 TopoDS_Iterator anItr(myShape);
223 for(; anItr.More(); anItr.Next()) {
224 vtkActorCollection* theActors =
225 GEOM_AssemblyBuilder::BuildActors(anItr.Value(), deflection, mode, forced);
226 theActors->InitTraversal();
227 vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
228 while(!(anActor==NULL)) {
229 AISActors->AddItem(anActor);
230 anActor = (vtkActor*)theActors->GetNextActor();
234 // Create graphics properties
236 vtkProperty* IsoProp = vtkProperty::New();
237 vtkProperty* FaceProp = vtkProperty::New();
238 vtkProperty* EdgeFProp = vtkProperty::New();
239 vtkProperty* EdgeSProp = vtkProperty::New();
240 vtkProperty* EdgeIProp = vtkProperty::New();
241 vtkProperty* VertexProp = vtkProperty::New();
243 vtkProperty* IsoPVProp = vtkProperty::New();
244 vtkProperty* EdgePVProp = vtkProperty::New();
245 vtkProperty* VertexPVProp = vtkProperty::New();
247 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
249 MeshShape(myShape,deflection,forced);
251 if ( myShape.ShapeType() <= 4 && myShape.ShapeType() != TopAbs_COMPOUND) {
254 // look if edges are free or shared
255 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
256 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
260 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
262 GEOM_Actor* FaceActor = GEOM_Actor::New();
263 FaceActor->SetShadingProperty(FaceProp);
264 FaceActor->SetWireframeProperty(IsoProp);
266 FaceActor->SetPreviewProperty(IsoPVProp);
268 FaceActor->setInputShape(ex.Current(),deflection,mode);
270 AISActors->AddItem(FaceActor);
273 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
274 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
276 if (BRep_Tool::Degenerated(aEdge)) {
280 // compute the number of faces
281 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
282 GEOM_Actor* EdgeActor = GEOM_Actor::New();
283 EdgeActor->SubShapeOn();
284 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
288 case 0 : // isolated edge
290 EdgeActor->SetShadingProperty(EdgeIProp);
291 EdgeActor->SetWireframeProperty(EdgeIProp);
295 case 1 :// edge in only one face
297 EdgeActor->SetShadingProperty(EdgeFProp);
298 EdgeActor->SetWireframeProperty(EdgeFProp);
302 default : // edge shared by at least two faces
304 EdgeActor->SetShadingProperty(EdgeSProp);
305 EdgeActor->SetWireframeProperty(EdgeSProp);
309 EdgeActor->SetPreviewProperty(EdgePVProp);
310 AISActors->AddItem(EdgeActor);
313 } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
315 for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
316 const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
318 if (BRep_Tool::Degenerated(aEdge)) {
322 GEOM_Actor* EdgeActor = GEOM_Actor::New();
323 EdgeActor->setInputShape(ex.Current(),deflection,mode);
324 EdgeActor->SetShadingProperty(EdgeIProp);
325 EdgeActor->SetWireframeProperty(EdgeIProp);
326 EdgeActor->SetPreviewProperty(EdgePVProp);
328 AISActors->AddItem(EdgeActor);
330 } else if ( myShape.ShapeType() == TopAbs_EDGE ) { // EDGE Actor
331 GEOM_Actor* EdgeActor = GEOM_Actor::New();
332 EdgeActor->setInputShape(myShape,deflection,mode,isVector);
333 EdgeActor->SetShadingProperty(EdgeIProp);
334 EdgeActor->SetWireframeProperty(EdgeIProp);
335 EdgeActor->SetPreviewProperty(EdgePVProp);
337 AISActors->AddItem(EdgeActor);
338 } else if ( myShape.ShapeType() == TopAbs_VERTEX ) { // VERTEX Actor
339 GEOM_Actor* VertexActor = GEOM_Actor::New();
340 VertexActor->setInputShape(myShape,deflection,mode);
341 VertexActor->SetShadingProperty(VertexProp);
342 VertexActor->SetWireframeProperty(VertexProp);
343 VertexActor->SetPreviewProperty(VertexPVProp);
345 AISActors->AddItem(VertexActor);
357 //-------------------------------------------------------------
359 //-------------------------------------------------------------
360 vtkAssembly* GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
361 Standard_Real deflection,
362 Standard_Integer mode,
363 Standard_Boolean forced)
365 // Create a new vtkAssembly
367 vtkAssembly* myVTKShape = vtkAssembly::New();
369 // Create graphics properties
371 vtkProperty* IsoProp = vtkProperty::New();
372 vtkProperty* FaceProp = vtkProperty::New();
373 vtkProperty* EdgeFProp = vtkProperty::New();
374 vtkProperty* EdgeSProp = vtkProperty::New();
375 vtkProperty* EdgeIProp = vtkProperty::New();
376 vtkProperty* VertexProp = vtkProperty::New();
377 vtkProperty* EdgePVProp = vtkProperty::New();
378 vtkProperty* VertexPVProp = vtkProperty::New();
379 vtkProperty* IsoPVProp = vtkProperty::New();
381 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
383 MeshShape(myShape,deflection,forced);
388 // look if edges are free or shared
389 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
390 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
394 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
395 //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
397 GEOM_Actor* FaceActor = GEOM_Actor::New();
398 FaceActor->SetShadingProperty(FaceProp);
399 FaceActor->SetWireframeProperty(IsoProp);
401 vtkAssembly* myFaceAssembly = vtkAssembly::New();
404 FaceActor->setInputShape(ex.Current(),deflection,mode);
405 myFaceAssembly->AddPart(FaceActor);
408 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
409 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
411 if (BRep_Tool::Degenerated(aEdge)) {
416 // compute the number of faces
417 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
418 GEOM_Actor* EdgeActor = GEOM_Actor::New();
421 case 0 : // isolated edge
423 EdgeActor->SetShadingProperty(EdgeIProp);
424 EdgeActor->SetWireframeProperty(EdgeIProp);
428 case 1 :// edge in only one face
430 EdgeActor->SetShadingProperty(EdgeFProp);
431 EdgeActor->SetWireframeProperty(EdgeFProp);
435 default : // edge shared by at least two faces
437 EdgeActor->SetShadingProperty(EdgeSProp);
438 EdgeActor->SetWireframeProperty(EdgeSProp);
442 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
443 myFaceAssembly->AddPart(EdgeActor);
445 myVTKShape->AddPart(myFaceAssembly);
454 //-------------------------------------------------------------
455 // CHANGE SPECIFIC DISPLAY MODE
456 //-------------------------------------------------------------
457 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
461 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
465 //-------------------------------------------------------------
467 //-------------------------------------------------------------
469 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
474 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)