Salome HOME
Fix regression: storeViewParameters() does not work for OCC view
[modules/geom.git] / src / OBJECT / GEOM_AssemblyBuilder.cxx
1 //  Copyright (C) 2007-2010  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
23 //  GEOM OBJECT : interactive object for Geometry entities visualization
24 //  File   : GEOM_AssemblyBuilder.cxx
25 //  Author : Christophe ATTANASIO
26 //  Module : GEOM
27 //  $Header$
28 //
29 /*!
30   \class GEOM_AssemblyBuilder GEOM_AssemblyBuilder.h
31   \brief ....
32 */
33
34 #include "GEOM_AssemblyBuilder.h"
35 #include "GEOM_Actor.h"
36
37 #include <OCC2VTK_Tools.h>
38
39 #include <SUIT_Session.h>
40 #include <SUIT_ResourceMgr.h>
41
42 #include <vtkProperty.h>
43 #include <vtkAssembly.h>
44 #include <vtkActorCollection.h>
45
46 // Open CASCADE Includes
47 #include <TopExp_Explorer.hxx>
48 #include <BRep_Tool.hxx>
49 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
50 #include <TopExp.hxx>
51 #include <TopoDS_Iterator.hxx>
52
53 // Qt includes
54 #include <QColor>
55
56 #include "utilities.h"
57
58 void GEOM_AssemblyBuilder::InitProperties(vtkProperty* IsoProp,
59                                           vtkProperty* FaceProp,
60                                           vtkProperty* EdgeFProp,
61                                           vtkProperty* EdgeSProp,
62                                           vtkProperty* EdgeIProp,
63                                           vtkProperty* VertexProp,
64                                           vtkProperty* IsoPVProp,
65                                           vtkProperty* EdgePVProp,
66                                           vtkProperty* VertexPVProp)
67 {
68   // Shading like default OCC material
69   FaceProp->SetRepresentationToSurface();
70   FaceProp->SetInterpolationToGouraud();
71   FaceProp->SetAmbient(1.0);
72   FaceProp->SetDiffuse(1.0);
73   FaceProp->SetSpecular(0.4);
74   FaceProp->SetAmbientColor(0.329412, 0.223529, 0.027451);
75   FaceProp->SetDiffuseColor(0.780392, 0.568627, 0.113725);
76   FaceProp->SetSpecularColor(0.992157, 0.941176, 0.807843);
77
78   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
79   QColor aColor;
80
81   // Wireframe for iso
82   aColor = aResMgr->colorValue( "Geometry", "isos_color", QColor( int(0.5*255), int(0.5*255), int(0.5*255) ) );
83   float red = aColor.red()/255.0;
84   float green = aColor.green()/255.0;
85   float blue = aColor.blue()/255.0;
86   IsoProp->SetRepresentationToWireframe();
87   IsoProp->SetAmbientColor(red, green, blue);
88   IsoProp->SetDiffuseColor(red, green, blue);
89   IsoProp->SetSpecularColor(red, green, blue);
90   
91   // Wireframe for iso
92   IsoPVProp->SetRepresentationToWireframe();
93   IsoPVProp->SetAmbientColor(0, 1, 1);
94   IsoPVProp->SetDiffuseColor(0, 1, 1);
95   IsoPVProp->SetSpecularColor(0, 1, 1);
96
97   // Wireframe for shared edge 
98   aColor = aResMgr->colorValue( "Geometry", "wireframe_color", QColor( 255, 255, 0 ) );
99   red = aColor.red()/255.0;
100   green = aColor.green()/255.0;
101   blue = aColor.blue()/255.0;
102   EdgeSProp->SetRepresentationToWireframe();
103   EdgeSProp->SetAmbientColor(red, green, blue);
104   EdgeSProp->SetDiffuseColor(red, green, blue);
105   EdgeSProp->SetSpecularColor(red, green, blue);
106   
107   // Wireframe for free edge 
108   aColor = aResMgr->colorValue( "Geometry", "free_bound_color", QColor( 0, 255, 0 ) );
109   red = aColor.red()/255.0;
110   green = aColor.green()/255.0;
111   blue = aColor.blue()/255.0;
112   EdgeFProp->SetRepresentationToWireframe();
113   EdgeFProp->SetAmbientColor(red, green, blue);
114   EdgeFProp->SetDiffuseColor(red, green, blue);
115   EdgeFProp->SetSpecularColor(red, green, blue);
116
117   // Wireframe for isolated edge 
118   aColor = aResMgr->colorValue( "Geometry", "line_color", QColor( 255, 0, 0 ) );
119   red = aColor.red()/255.0;
120   green = aColor.green()/255.0;
121   blue = aColor.blue()/255.0;
122   EdgeIProp->SetRepresentationToWireframe();
123   EdgeIProp->SetAmbientColor(red, green, blue);
124   EdgeIProp->SetDiffuseColor(red, green, blue);
125   
126   // Wireframe for Preview edge 
127   EdgePVProp->SetRepresentationToWireframe();
128   EdgePVProp->SetAmbientColor(1, 1, 0);
129   EdgePVProp->SetDiffuseColor(1, 1, 0);
130   EdgePVProp->SetSpecularColor(1, 1, 0);
131
132   // Wireframe for vertex 
133   aColor = aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) );
134   red = aColor.red()/255.0;
135   green = aColor.green()/255.0;
136   blue = aColor.blue()/255.0;
137   VertexProp->SetRepresentationToWireframe();
138   VertexProp->SetAmbientColor(red, green, blue);
139   VertexProp->SetDiffuseColor(red, green, blue);
140   VertexProp->SetSpecularColor(red, green, blue);
141   
142   // Wireframe for vertex 
143   VertexPVProp->SetRepresentationToWireframe();
144   VertexPVProp->SetAmbientColor(0, 1, 1);
145   VertexPVProp->SetDiffuseColor(0, 1, 1);
146   VertexPVProp->SetSpecularColor(0, 1, 1);
147 }
148
149
150 vtkActorCollection* GEOM_AssemblyBuilder::BuildActors(const TopoDS_Shape& myShape,
151                                                       Standard_Real    deflection,
152                                                       Standard_Integer mode,
153                                                       Standard_Boolean forced,
154                                                       Standard_Boolean isVector)
155 {
156   /*
157   vtkProperty* IsoProp = vtkProperty::New();
158   vtkProperty* FaceProp = vtkProperty::New();
159   vtkProperty* EdgeFProp = vtkProperty::New();
160   vtkProperty* EdgeSProp = vtkProperty::New();
161   vtkProperty* EdgeIProp = vtkProperty::New();
162   vtkProperty* VertexProp = vtkProperty::New();
163   vtkProperty* IsoPVProp = vtkProperty::New();
164   vtkProperty* EdgePVProp = vtkProperty::New();
165   vtkProperty* VertexPVProp = vtkProperty::New();
166   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
167   */
168
169   vtkActorCollection* AISActors = vtkActorCollection::New();
170   GEOM::MeshShape(myShape,deflection,forced);
171   GEOM_Actor* aGeomActor = GEOM_Actor::New();
172   aGeomActor->SetShape(myShape,(float)deflection,false,isVector);
173   AISActors->AddItem(aGeomActor);
174
175   aGeomActor->Delete();
176   
177   /*
178   if(myShape.ShapeType() == TopAbs_COMPOUND) {
179     TopoDS_Iterator anItr(myShape);
180     for(; anItr.More(); anItr.Next()) {
181       vtkActorCollection* theActors =
182         GEOM_AssemblyBuilder::BuildActors(anItr.Value(), deflection, mode, forced);
183       theActors->InitTraversal();
184       vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
185       while(!(anActor==NULL)) {
186         AISActors->AddItem(anActor);
187         anActor = (vtkActor*)theActors->GetNextActor();
188       }
189     }
190   }
191   // Create graphics properties
192
193   vtkProperty* IsoProp = vtkProperty::New();
194   vtkProperty* FaceProp = vtkProperty::New();
195   vtkProperty* EdgeFProp = vtkProperty::New();
196   vtkProperty* EdgeSProp = vtkProperty::New();
197   vtkProperty* EdgeIProp = vtkProperty::New();
198   vtkProperty* VertexProp = vtkProperty::New();
199
200   vtkProperty* IsoPVProp = vtkProperty::New();
201   vtkProperty* EdgePVProp = vtkProperty::New();
202   vtkProperty* VertexPVProp = vtkProperty::New();
203
204   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
205
206   MeshShape(myShape,deflection,forced);
207
208   if ( myShape.ShapeType() <= 4 && myShape.ShapeType() != TopAbs_COMPOUND) {
209     
210     // FACE Actor
211     // look if edges are free or shared 
212     TopTools_IndexedDataMapOfShapeListOfShape edgemap;
213     TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
214     
215     TopExp_Explorer ex;
216     
217     for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
218       
219       GEOM_Actor* FaceActor = GEOM_Actor::New();
220       FaceActor->SetShadingProperty(FaceProp);
221       FaceActor->SetWireframeProperty(IsoProp);
222
223       FaceActor->SetPreviewProperty(IsoPVProp);
224       
225       FaceActor->setInputShape(ex.Current(),deflection,mode);
226       
227       AISActors->AddItem(FaceActor);
228       
229       TopExp_Explorer ex2;
230       for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
231         const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
232         
233         if (BRep_Tool::Degenerated(aEdge)) {    
234           continue;
235         }
236         
237         // compute the number of faces
238         Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
239         GEOM_Actor* EdgeActor = GEOM_Actor::New();
240         EdgeActor->SubShapeOn();
241         EdgeActor->setInputShape(ex2.Current(),deflection,mode);
242         
243         switch (nbf) {
244           
245         case 0 : // isolated edge
246           {
247             EdgeActor->SetShadingProperty(EdgeIProp);
248             EdgeActor->SetWireframeProperty(EdgeIProp);
249           }
250           break;
251           
252         case 1 :// edge in only one face
253           {
254             EdgeActor->SetShadingProperty(EdgeFProp);
255             EdgeActor->SetWireframeProperty(EdgeFProp);
256           }
257           break;
258           
259         default :   // edge shared by at least two faces      
260           {
261             EdgeActor->SetShadingProperty(EdgeSProp);
262             EdgeActor->SetWireframeProperty(EdgeSProp);
263           }
264         }
265         
266         EdgeActor->SetPreviewProperty(EdgePVProp);
267         AISActors->AddItem(EdgeActor);
268       }
269     }
270   } else if ( myShape.ShapeType() == TopAbs_WIRE ) { // WIRE Actor
271     TopExp_Explorer ex;
272     for (ex.Init(myShape, TopAbs_EDGE); ex.More(); ex.Next()) {
273       const TopoDS_Edge& aEdge = TopoDS::Edge(ex.Current());
274         
275       if (BRep_Tool::Degenerated(aEdge)) {    
276         continue;
277       }
278         
279       GEOM_Actor* EdgeActor = GEOM_Actor::New();
280       EdgeActor->setInputShape(ex.Current(),deflection,mode);
281       EdgeActor->SetShadingProperty(EdgeIProp);
282       EdgeActor->SetWireframeProperty(EdgeIProp);
283       EdgeActor->SetPreviewProperty(EdgePVProp);
284       
285       AISActors->AddItem(EdgeActor);
286     }
287   } else if ( myShape.ShapeType() == TopAbs_EDGE ) { // EDGE Actor
288     GEOM_Actor* EdgeActor = GEOM_Actor::New();
289     EdgeActor->setInputShape(myShape,deflection,mode,isVector);
290     EdgeActor->SetShadingProperty(EdgeIProp);
291     EdgeActor->SetWireframeProperty(EdgeIProp);
292     EdgeActor->SetPreviewProperty(EdgePVProp);
293     
294     AISActors->AddItem(EdgeActor);
295   } else if ( myShape.ShapeType() == TopAbs_VERTEX ) { // VERTEX Actor
296     GEOM_Actor* VertexActor = GEOM_Actor::New();
297     VertexActor->setInputShape(myShape,deflection,mode);
298     VertexActor->SetShadingProperty(VertexProp);
299     VertexActor->SetWireframeProperty(VertexProp);
300     VertexActor->SetPreviewProperty(VertexPVProp);
301     
302     AISActors->AddItem(VertexActor);
303   
304   } 
305   */
306   
307   
308   return AISActors;
309
310 }
311
312
313
314 //-------------------------------------------------------------
315 // BUILD ASSEMBLY
316 //-------------------------------------------------------------
317 vtkAssembly*  GEOM_AssemblyBuilder::BuildAssembly(const TopoDS_Shape& myShape,
318                                                       Standard_Real deflection,
319                                                       Standard_Integer mode,
320                                                       Standard_Boolean forced)
321 {
322   // Create a new vtkAssembly
323
324   vtkAssembly* myVTKShape = vtkAssembly::New();
325   /*
326   // Create graphics properties
327
328   vtkProperty* IsoProp = vtkProperty::New();
329   vtkProperty* FaceProp = vtkProperty::New();
330   vtkProperty* EdgeFProp = vtkProperty::New();
331   vtkProperty* EdgeSProp = vtkProperty::New();
332   vtkProperty* EdgeIProp = vtkProperty::New();
333   vtkProperty* VertexProp = vtkProperty::New();
334   vtkProperty* EdgePVProp = vtkProperty::New();
335   vtkProperty* VertexPVProp = vtkProperty::New();
336   vtkProperty* IsoPVProp = vtkProperty::New();
337
338   InitProperties(IsoProp,FaceProp,EdgeFProp,EdgeSProp,EdgeIProp,VertexProp,IsoPVProp,EdgePVProp,VertexPVProp);
339
340   MeshShape(myShape,deflection,forced);
341
342   
343   // FACE Actor
344   
345   // look if edges are free or shared 
346   TopTools_IndexedDataMapOfShapeListOfShape edgemap;
347   TopExp::MapShapesAndAncestors(myShape,TopAbs_EDGE,TopAbs_FACE,edgemap);
348   
349   TopExp_Explorer ex;
350
351   for (ex.Init(myShape, TopAbs_FACE); ex.More(); ex.Next()) {
352     //const TopoDS_Face& aFace = TopoDS::Face(ex.Current());
353     
354     GEOM_Actor* FaceActor = GEOM_Actor::New();
355     FaceActor->SetShadingProperty(FaceProp);
356     FaceActor->SetWireframeProperty(IsoProp);
357     
358     vtkAssembly* myFaceAssembly = vtkAssembly::New();
359
360    
361     FaceActor->setInputShape(ex.Current(),deflection,mode);
362     myFaceAssembly->AddPart(FaceActor);
363     
364     TopExp_Explorer ex2;
365     for (ex2.Init(ex.Current(), TopAbs_EDGE); ex2.More(); ex2.Next()) {
366       const TopoDS_Edge& aEdge = TopoDS::Edge(ex2.Current());
367  
368       if (BRep_Tool::Degenerated(aEdge)) {    
369         continue;
370       }
371       
372     
373       // compute the number of faces
374       Standard_Integer nbf = edgemap.FindFromKey(ex2.Current()).Extent();
375       GEOM_Actor* EdgeActor = GEOM_Actor::New();
376       switch (nbf) {
377         
378       case 0 : // isolated edge
379         {
380           EdgeActor->SetShadingProperty(EdgeIProp);
381           EdgeActor->SetWireframeProperty(EdgeIProp);
382         }
383         break;
384         
385       case 1 :// edge in only one face
386         {
387           EdgeActor->SetShadingProperty(EdgeFProp);
388           EdgeActor->SetWireframeProperty(EdgeFProp);
389         }
390         break;
391         
392       default :   // edge shared by at least two faces      
393         {
394           EdgeActor->SetShadingProperty(EdgeSProp);
395           EdgeActor->SetWireframeProperty(EdgeSProp);
396         }
397       }
398   
399       EdgeActor->setInputShape(ex2.Current(),deflection,mode);
400       myFaceAssembly->AddPart(EdgeActor);
401     }
402     myVTKShape->AddPart(myFaceAssembly);
403   }
404
405   */
406   
407   return myVTKShape;
408  
409 }
410
411 //-------------------------------------------------------------
412 // CHANGE SPECIFIC DISPLAY MODE
413 //-------------------------------------------------------------
414 void  GEOM_AssemblyBuilder::SwitchDisplayMode(vtkAssembly* aOCCAssembly)
415 {
416 }
417
418 void  GEOM_AssemblyBuilder::SwitchDisplayMode(vtkActorCollection* aOCCAssembly)
419 {
420 }
421
422 //-------------------------------------------------------------
423 // DISPLAY/ERASE
424 //-------------------------------------------------------------
425
426 void GEOM_AssemblyBuilder::DisplayErase(vtkAssembly* mySALOMEAssembly)
427 {
428 }
429
430
431 void GEOM_AssemblyBuilder::DisplayErase(vtkActorCollection* mySALOMEActors)
432 {
433 }
434
435
436
437
438