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 <vtkProperty.h>
39 // Open CASCADE Includes
40 #include <TopExp_Explorer.hxx>
41 #include <Bnd_Box.hxx>
42 #include <BRepMesh_IncrementalMesh.hxx>
43 #include <Poly_Triangulation.hxx>
44 #include <BRepBndLib.hxx>
45 #include <BRep_Tool.hxx>
46 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
48 #include <TopTools_ListOfShape.hxx>
49 #include <TopoDS_Iterator.hxx>
51 #include "utilities.h"
56 #define MAX2(X, Y) ( Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
57 #define MAX3(X, Y, Z) ( MAX2 ( MAX2(X,Y) , Z) )
63 void GEOM_AssemblyBuilder::InitProperties(vtkProperty* IsoProp,
64 vtkProperty* FaceProp,
65 vtkProperty* EdgeFProp,
66 vtkProperty* EdgeSProp,
67 vtkProperty* EdgeIProp,
68 vtkProperty* VertexProp,
69 vtkProperty* IsoPVProp,
70 vtkProperty* EdgePVProp,
71 vtkProperty* VertexPVProp)
73 // Shading like default OCC material
74 FaceProp->SetRepresentationToSurface();
75 FaceProp->SetInterpolationToGouraud();
76 FaceProp->SetAmbient(1.0);
77 FaceProp->SetDiffuse(1.0);
78 FaceProp->SetSpecular(0.4);
79 FaceProp->SetAmbientColor(0.329412, 0.223529, 0.027451);
80 FaceProp->SetDiffuseColor(0.780392, 0.568627, 0.113725);
81 FaceProp->SetSpecularColor(0.992157, 0.941176, 0.807843);
84 IsoProp->SetRepresentationToWireframe();
85 IsoProp->SetAmbientColor(0.5, 0.5, 0.5);
86 IsoProp->SetDiffuseColor(0.5, 0.5, 0.5);
87 IsoProp->SetSpecularColor(0.5, 0.5, 0.5);
90 IsoPVProp->SetRepresentationToWireframe();
91 IsoPVProp->SetAmbientColor(0, 1, 1);
92 IsoPVProp->SetDiffuseColor(0, 1, 1);
93 IsoPVProp->SetSpecularColor(0, 1, 1);
95 // Wireframe for shared edge
96 EdgeSProp->SetRepresentationToWireframe();
97 EdgeSProp->SetAmbientColor(1, 1, 0);
98 EdgeSProp->SetDiffuseColor(1, 1, 0);
99 EdgeSProp->SetSpecularColor(1, 1, 0);
101 // Wireframe for free edge
102 EdgeFProp->SetRepresentationToWireframe();
103 EdgeFProp->SetAmbientColor(0, 1, 0);
104 EdgeFProp->SetDiffuseColor(0, 1, 0);
105 EdgeFProp->SetSpecularColor(0, 1, 0);
107 // Wireframe for isolated edge
108 EdgeIProp->SetRepresentationToWireframe();
109 EdgeIProp->SetAmbientColor(1, 0, 0);
110 EdgeIProp->SetDiffuseColor(1, 0, 0);
111 EdgeIProp->SetSpecularColor(1, 0, 0);
113 // Wireframe for Preview edge
114 EdgePVProp->SetRepresentationToWireframe();
115 EdgePVProp->SetAmbientColor(1, 1, 0);
116 EdgePVProp->SetDiffuseColor(1, 1, 0);
117 EdgePVProp->SetSpecularColor(1, 1, 0);
119 // Wireframe for vertex
120 VertexProp->SetRepresentationToWireframe();
121 VertexProp->SetAmbientColor(1, 1, 0);
122 VertexProp->SetDiffuseColor(1, 1, 0);
123 VertexProp->SetSpecularColor(1, 1, 0);
125 // Wireframe for vertex
126 VertexPVProp->SetRepresentationToWireframe();
127 VertexPVProp->SetAmbientColor(0, 1, 1);
128 VertexPVProp->SetDiffuseColor(0, 1, 1);
129 VertexPVProp->SetSpecularColor(0, 1, 1);
133 void GEOM_AssemblyBuilder::MeshShape(const TopoDS_Shape myShape,
134 Standard_Real deflection,
135 Standard_Boolean forced)
137 // Mesh the shape if necessary
138 Standard_Boolean alreadymesh = Standard_True;
140 TopLoc_Location aLoc;
142 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
143 const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
144 Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation(aFace,aLoc);
145 if(aPoly.IsNull()) { alreadymesh = Standard_False; break; }
148 if(!alreadymesh || forced) {
150 // Compute default deflection
152 BRepBndLib::Add(myShape, B);
153 Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
154 B.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
155 deflection = MAX3( aXmax-aXmin , aYmax-aYmin , aZmax-aZmin) * 0.001 *4;
157 BRepMesh_IncrementalMesh MESH(myShape,deflection);
163 vtkActorCollection* GEOM_AssemblyBuilder::BuildActors(const TopoDS_Shape& myShape,
164 Standard_Real deflection,
165 Standard_Integer mode,
166 Standard_Boolean forced) {
168 vtkActorCollection* AISActors = vtkActorCollection::New();
170 if(myShape.ShapeType() == TopAbs_COMPOUND) {
171 TopoDS_Iterator anItr(myShape);
172 for(; anItr.More(); anItr.Next()) {
173 vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(anItr.Value(), deflection, mode, forced);
174 theActors->InitTraversal();
175 vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
176 while(!(anActor==NULL)) {
177 AISActors->AddItem(anActor);
178 anActor = (vtkActor*)theActors->GetNextActor();
182 // Create graphics properties
184 vtkProperty* IsoProp = vtkProperty::New();
185 vtkProperty* FaceProp = vtkProperty::New();
186 vtkProperty* EdgeFProp = vtkProperty::New();
187 vtkProperty* EdgeSProp = vtkProperty::New();
188 vtkProperty* EdgeIProp = vtkProperty::New();
189 vtkProperty* VertexProp = vtkProperty::New();
191 vtkProperty* IsoPVProp = vtkProperty::New();
192 vtkProperty* EdgePVProp = vtkProperty::New();
193 vtkProperty* VertexPVProp = vtkProperty::New();
195 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
197 MeshShape(myShape,deflection,forced);
199 if ( myShape.ShapeType() <= 4 && myShape.ShapeType() != TopAbs_COMPOUND) {
202 // look if edges are free or shared
203 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
204 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
208 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
210 GEOM_Actor* FaceActor = GEOM_Actor::New();
211 FaceActor->SetShadingProperty(FaceProp);
212 FaceActor->SetWireframeProperty(IsoProp);
214 FaceActor->SetPreviewProperty(IsoPVProp);
216 FaceActor->setInputShape(ex.Current(),deflection,mode);
218 AISActors->AddItem(FaceActor);
221 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
222 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
224 if (BRep_Tool::Degenerated(aEdge)) {
228 // compute the number of faces
229 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
230 GEOM_Actor* EdgeActor = GEOM_Actor::New();
231 EdgeActor->SubShapeOn();
232 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
235 case 0 : // isolated edge
237 EdgeActor->SetShadingProperty(EdgeIProp);
238 EdgeActor->SetWireframeProperty(EdgeIProp);
242 case 1 :// edge in only one face
244 EdgeActor->SetShadingProperty(EdgeFProp);
245 EdgeActor->SetWireframeProperty(EdgeFProp);
249 default : // edge shared by at least two faces
251 EdgeActor->SetShadingProperty(EdgeSProp);
252 EdgeActor->SetWireframeProperty(EdgeSProp);
255 EdgeActor->SetPreviewProperty(EdgePVProp);
256 AISActors->AddItem(EdgeActor);
259 } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
261 for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
262 const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
264 if (BRep_Tool::Degenerated(aEdge)) {
268 GEOM_Actor* EdgeActor = GEOM_Actor::New();
269 EdgeActor->setInputShape(ex.Current(),deflection,mode);
270 EdgeActor->SetShadingProperty(EdgeIProp);
271 EdgeActor->SetWireframeProperty(EdgeIProp);
272 EdgeActor->SetPreviewProperty(EdgePVProp);
274 AISActors->AddItem(EdgeActor);
276 } else if ( myShape.ShapeType() == TopAbs_EDGE ) { // EDGE Actor
277 GEOM_Actor* EdgeActor = GEOM_Actor::New();
278 EdgeActor->setInputShape(myShape,deflection,mode);
279 EdgeActor->SetShadingProperty(EdgeIProp);
280 EdgeActor->SetWireframeProperty(EdgeIProp);
281 EdgeActor->SetPreviewProperty(EdgePVProp);
283 AISActors->AddItem(EdgeActor);
284 } else if ( myShape.ShapeType() == TopAbs_VERTEX ) { // VERTEX Actor
285 GEOM_Actor* VertexActor = GEOM_Actor::New();
286 VertexActor->setInputShape(myShape,deflection,mode);
287 VertexActor->SetShadingProperty(VertexProp);
288 VertexActor->SetWireframeProperty(VertexProp);
289 VertexActor->SetPreviewProperty(VertexPVProp);
291 AISActors->AddItem(VertexActor);
301 //-------------------------------------------------------------
303 //-------------------------------------------------------------
304 vtkAssembly* GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
305 Standard_Real deflection,
306 Standard_Integer mode,
307 Standard_Boolean forced)
309 // Create a new vtkAssembly
311 vtkAssembly* myVTKShape = vtkAssembly::New();
314 // Create graphics properties
316 vtkProperty* IsoProp = vtkProperty::New();
317 vtkProperty* FaceProp = vtkProperty::New();
318 vtkProperty* EdgeFProp = vtkProperty::New();
319 vtkProperty* EdgeSProp = vtkProperty::New();
320 vtkProperty* EdgeIProp = vtkProperty::New();
321 vtkProperty* VertexProp = vtkProperty::New();
322 vtkProperty* EdgePVProp = vtkProperty::New();
323 vtkProperty* VertexPVProp = vtkProperty::New();
324 vtkProperty* IsoPVProp = vtkProperty::New();
326 InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
328 MeshShape(myShape,deflection,forced);
333 // look if edges are free or shared
334 TopTools_IndexedDataMapOfShapeListOfShape edgemap;
335 TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
339 for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
340 //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
342 GEOM_Actor* FaceActor = GEOM_Actor::New();
343 FaceActor->SetShadingProperty(FaceProp);
344 FaceActor->SetWireframeProperty(IsoProp);
346 vtkAssembly* myFaceAssembly = vtkAssembly::New();
349 FaceActor->setInputShape(ex.Current(),deflection,mode);
350 myFaceAssembly->AddPart(FaceActor);
353 for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
354 const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
356 if (BRep_Tool::Degenerated(aEdge)) {
361 // compute the number of faces
362 Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
363 GEOM_Actor* EdgeActor = GEOM_Actor::New();
366 case 0 : // isolated edge
368 EdgeActor->SetShadingProperty(EdgeIProp);
369 EdgeActor->SetWireframeProperty(EdgeIProp);
373 case 1 :// edge in only one face
375 EdgeActor->SetShadingProperty(EdgeFProp);
376 EdgeActor->SetWireframeProperty(EdgeFProp);
380 default : // edge shared by at least two faces
382 EdgeActor->SetShadingProperty(EdgeSProp);
383 EdgeActor->SetWireframeProperty(EdgeSProp);
387 EdgeActor->setInputShape(ex2.Current(),deflection,mode);
388 myFaceAssembly->AddPart(EdgeActor);
390 myVTKShape->AddPart(myFaceAssembly);
397 //-------------------------------------------------------------
398 // CHANGE SPECIFIC DISPLAY MODE
399 //-------------------------------------------------------------
400 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
404 void GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
408 //-------------------------------------------------------------
410 //-------------------------------------------------------------
412 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
417 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)