]> SALOME platform Git repositories - modules/geom.git/blob - src/OBJECT/GEOM_AssemblyBuilder.cxx
Salome HOME
IPAL20715 impossible to select sub-shape after apply group
[modules/geom.git] / src / OBJECT / GEOM_AssemblyBuilder.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  GEOM OBJECT : interactive object for Geometry entities visualization
23 //  File   : GEOM_AssemblyBuilder.cxx
24 //  Author : Christophe ATTANASIO
25 //  Module : GEOM
26 //  $Header$
27 //
28 /*!
29   \class GEOM_AssemblyBuilder GEOM_AssemblyBuilder.h
30   \brief ....
31 */
32
33 #include "GEOM_AssemblyBuilder.h"
34 #include "GEOM_Actor.h"
35
36 #include <SUIT_Session.h>
37
38 #include <vtkProperty.h>
39
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>
48 #include <TopExp.hxx>
49 #include <TopTools_ListOfShape.hxx>
50 #include <TopoDS_Iterator.hxx>
51
52 #include "utilities.h"
53
54 using namespace std;
55 // SALOME
56
57 #define MAX2(X, Y)      (  Abs(X) > Abs(Y)? Abs(X) : Abs(Y) )
58 #define MAX3(X, Y, Z)   ( MAX2 ( MAX2(X,Y) , Z) )
59
60
61
62
63
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)
73 {
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);
83
84   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
85   QColor aColor;
86
87   // Wireframe for iso
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);
96   
97   // Wireframe for iso
98   IsoPVProp->SetRepresentationToWireframe();
99   IsoPVProp->SetAmbientColor(0, 1, 1);
100   IsoPVProp->SetDiffuseColor(0, 1, 1);
101   IsoPVProp->SetSpecularColor(0, 1, 1);
102
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);
112   
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);
122
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);
131   
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);
137
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);
147   
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);
153 }
154
155
156 void GEOM_AssemblyBuilder::MeshShape(const TopoDS_Shape myShape,
157                                          Standard_Real deflection,
158                                          Standard_Boolean forced)
159 {
160   // Mesh the shape if necessary
161   Standard_Boolean alreadymesh = Standard_True;
162   TopExp_Explorer ex;
163   TopLoc_Location aLoc;
164
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; }
169   }
170
171   if(!alreadymesh || forced) {
172     if(deflection<=0) {
173       // Compute default deflection
174       Bnd_Box B;
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;
180     }
181     BRepMesh_IncrementalMesh MESH(myShape,deflection);
182   }
183 }
184
185
186
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)
192 {
193   /*
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);
204   */
205
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);
211
212   aGeomActor->Delete();
213   
214   /*
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();
225       }
226     }
227   }
228   // Create graphics properties
229
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();
236
237   vtkProperty* IsoPVProp = vtkProperty::New();
238   vtkProperty* EdgePVProp = vtkProperty::New();
239   vtkProperty* VertexPVProp = vtkProperty::New();
240
241   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
242
243   MeshShape(myShape,deflection,forced);
244
245   if ( myShape.ShapeType() <= 4 && myShape.ShapeType() != TopAbs_COMPOUND) {
246     
247     // FACE Actor
248     // look if edges are free or shared 
249     TopTools_IndexedDataMapOfShapeListOfShape edgemap;
250     TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
251     
252     TopExp_Explorer ex;
253     
254     for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
255       
256       GEOM_Actor* FaceActor = GEOM_Actor::New();
257       FaceActor->SetShadingProperty(FaceProp);
258       FaceActor->SetWireframeProperty(IsoProp);
259
260       FaceActor->SetPreviewProperty(IsoPVProp);
261       
262       FaceActor->setInputShape(ex.Current(),deflection,mode);
263       
264       AISActors->AddItem(FaceActor);
265       
266       TopExp_Explorer ex2;
267       for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
268         const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
269         
270         if (BRep_Tool::Degenerated(aEdge)) {    
271           continue;
272         }
273         
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);
279         
280         switch (nbf) {
281           
282         case 0 : // isolated edge
283           {
284             EdgeActor->SetShadingProperty(EdgeIProp);
285             EdgeActor->SetWireframeProperty(EdgeIProp);
286           }
287           break;
288           
289         case 1 :// edge in only one face
290           {
291             EdgeActor->SetShadingProperty(EdgeFProp);
292             EdgeActor->SetWireframeProperty(EdgeFProp);
293           }
294           break;
295           
296         default :   // edge shared by at least two faces      
297           {
298             EdgeActor->SetShadingProperty(EdgeSProp);
299             EdgeActor->SetWireframeProperty(EdgeSProp);
300           }
301         }
302         
303         EdgeActor->SetPreviewProperty(EdgePVProp);
304         AISActors->AddItem(EdgeActor);
305       }
306     }
307   } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
308     TopExp_Explorer ex;
309     for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
310       const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
311         
312       if (BRep_Tool::Degenerated(aEdge)) {    
313         continue;
314       }
315         
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);
321       
322       AISActors->AddItem(EdgeActor);
323     }
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);
330     
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);
338     
339     AISActors->AddItem(VertexActor);
340   
341   } 
342   */
343   
344   
345   return AISActors;
346
347 }
348
349
350
351 //-------------------------------------------------------------
352 // BUILD ASSEMBLY
353 //-------------------------------------------------------------
354 vtkAssembly*  GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
355                                                       Standard_Real deflection,
356                                                       Standard_Integer mode,
357                                                       Standard_Boolean forced)
358 {
359   // Create a new vtkAssembly
360
361   vtkAssembly* myVTKShape = vtkAssembly::New();
362   /*
363   // Create graphics properties
364
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();
374
375   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
376
377   MeshShape(myShape,deflection,forced);
378
379   
380   // FACE Actor
381   
382   // look if edges are free or shared 
383   TopTools_IndexedDataMapOfShapeListOfShape edgemap;
384   TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
385   
386   TopExp_Explorer ex;
387
388   for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
389     //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
390     
391     GEOM_Actor* FaceActor = GEOM_Actor::New();
392     FaceActor->SetShadingProperty(FaceProp);
393     FaceActor->SetWireframeProperty(IsoProp);
394     
395     vtkAssembly* myFaceAssembly = vtkAssembly::New();
396
397    
398     FaceActor->setInputShape(ex.Current(),deflection,mode);
399     myFaceAssembly->AddPart(FaceActor);
400     
401     TopExp_Explorer ex2;
402     for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
403       const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
404  
405       if (BRep_Tool::Degenerated(aEdge)) {    
406         continue;
407       }
408       
409     
410       // compute the number of faces
411       Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
412       GEOM_Actor* EdgeActor = GEOM_Actor::New();
413       switch (nbf) {
414         
415       case 0 : // isolated edge
416         {
417           EdgeActor->SetShadingProperty(EdgeIProp);
418           EdgeActor->SetWireframeProperty(EdgeIProp);
419         }
420         break;
421         
422       case 1 :// edge in only one face
423         {
424           EdgeActor->SetShadingProperty(EdgeFProp);
425           EdgeActor->SetWireframeProperty(EdgeFProp);
426         }
427         break;
428         
429       default :   // edge shared by at least two faces      
430         {
431           EdgeActor->SetShadingProperty(EdgeSProp);
432           EdgeActor->SetWireframeProperty(EdgeSProp);
433         }
434       }
435   
436       EdgeActor->setInputShape(ex2.Current(),deflection,mode);
437       myFaceAssembly->AddPart(EdgeActor);
438     }
439     myVTKShape->AddPart(myFaceAssembly);
440   }
441
442   */
443   
444   return myVTKShape;
445  
446 }
447
448 //-------------------------------------------------------------
449 // CHANGE SPECIFIC DISPLAY MODE
450 //-------------------------------------------------------------
451 void  GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
452 {
453 }
454
455 void  GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
456 {
457 }
458
459 //-------------------------------------------------------------
460 // DISPLAY/ERASE
461 //-------------------------------------------------------------
462
463 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
464 {
465 }
466
467
468 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)
469 {
470 }
471
472
473
474
475