Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / OBJECT / GEOM_AISShape.cxx
1 // Copyright (C) 2007-2013  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_AISShape.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : GEOM
27
28 /*!
29   \class GEOM_AISShape GEOM_AISShape.hxx
30   \brief ....
31 */
32
33 #include "GEOM_AISShape.ixx"
34 #include "SALOME_InteractiveObject.hxx"
35 #include "GEOM_AISVector.hxx"
36
37 // Open CASCADE Includes
38 #include <AIS_Drawer.hxx>
39 #include <AIS_InteractiveContext.hxx>
40
41 #include <BRep_Tool.hxx>
42
43 #include <GCPnts_AbscissaPoint.hxx>
44 #include <GeomAdaptor_Curve.hxx>
45 #include <gp_Pnt.hxx>
46 #include <gp_Dir.hxx>
47 #include <gp_Vec.hxx>
48 #include <Graphic3d_AspectFillArea3d.hxx>
49 #include <Graphic3d_AspectLine3d.hxx>
50
51 #include <Prs3d_ShadingAspect.hxx>
52 #include <Prs3d_Arrow.hxx>
53 #include <Prs3d_IsoAspect.hxx>
54
55 #include <SelectBasics_SensitiveEntity.hxx>
56 #include <SelectMgr_EntityOwner.hxx>
57 #include <StdSelect_BRepOwner.hxx>
58 #include <SelectMgr_IndexedMapOfOwner.hxx>
59 #include <SelectMgr_Selection.hxx>
60 #include <StdSelect_DisplayMode.hxx>
61 #include <StdPrs_WFDeflectionShape.hxx>
62
63 #include <TColStd_IndexedMapOfInteger.hxx>
64 #include <TColStd_ListIteratorOfListOfInteger.hxx>
65 #include <TColStd_ListOfInteger.hxx>
66 #include <TopExp.hxx>
67 #include <TopoDS_Shape.hxx>
68 #include <TopTools_IndexedMapOfShape.hxx>
69 #include <TopExp.hxx>
70 #include <TopExp_Explorer.hxx>
71 #include <TopoDS.hxx>
72 #include <TopoDS_Edge.hxx>
73 #include <TopoDS_Shape.hxx>
74 #include <TopoDS_Vertex.hxx>
75
76 #include <TColStd_SequenceOfInteger.hxx>
77
78 #include <V3d_View.hxx>
79
80 #include <SalomeApp_Tools.h>
81
82 #include <SUIT_Session.h>
83 #include <SUIT_ResourceMgr.h>
84
85 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::myTopLevelDm = GEOM_AISShape::TopKeepCurrent;
86 Quantity_Color GEOM_AISShape::myTopLevelColor;
87
88
89 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
90                              const Handle(AIS_InteractiveContext)& theIC,
91                              SelectMgr_IndexedMapOfOwner& theMap )
92 {
93   if ( theObj.IsNull() || theIC.IsNull() )
94     return;
95
96   TColStd_ListOfInteger modes;
97   theIC->ActivatedModes( theObj, modes );
98
99   TColStd_ListIteratorOfListOfInteger itr( modes );
100   for (; itr.More(); itr.Next() ) {
101     int m = itr.Value();
102     if ( !theObj->HasSelection( m ) )
103       continue;
104
105     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
106
107     for ( sel->Init(); sel->More(); sel->Next() ) {
108       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
109       if ( entity.IsNull() )
110         continue;
111
112       Handle(SelectMgr_EntityOwner) owner =
113         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
114       if ( !owner.IsNull() )
115         theMap.Add( owner );
116     }
117   }
118 }
119
120 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
121                              const TopoDS_Shape& aMainShape,
122                              const SelectMgr_IndexedMapOfOwner& anAllMap, 
123                              SelectMgr_IndexedMapOfOwner& aToHiliteMap )
124 {
125   TopTools_IndexedMapOfShape aMapOfShapes;
126   TopExp::MapShapes(aMainShape, aMapOfShapes);
127
128   for  ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
129     Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
130     if ( anOwner.IsNull() || !anOwner->HasShape() )
131       continue;
132
133     const TopoDS_Shape& aSubShape = anOwner->Shape();
134     Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
135     if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
136       continue;
137     
138     if ( !aToHiliteMap.Contains( anOwner ) )
139       aToHiliteMap.Add( anOwner );
140   }
141 }
142
143 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
144                              const Standard_CString aName)
145   : SALOME_AISShape(shape), myName(aName), myDisplayVectors(false)
146 {
147   myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
148   myPrevDisplayMode = 0;
149   storeBoundaryColors();
150
151
152   myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
153
154   myUIsoNumber = -1;
155   myVIsoNumber = -1;
156
157   myTopLevel = Standard_False;
158   Graphic3d_MaterialAspect aMatAspect;
159   if ( !HasMaterial() ) {
160         aMatAspect.SetAmbient( 0.5 );
161         aMatAspect.SetDiffuse( 0.5 );
162         aMatAspect.SetEmissive( 0.5 );
163         aMatAspect.SetShininess(0.5 );
164         aMatAspect.SetSpecular( 0.5 );
165         
166         myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
167         myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
168   }
169 }
170
171 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
172   SetOwner( io );
173 }
174
175 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
176   Handle(SALOME_InteractiveObject) IO;
177   if ( !GetOwner().IsNull() )
178     IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
179   return IO;
180 }
181
182 Standard_Boolean GEOM_AISShape::hasIO(){
183   return !getIO().IsNull();
184 }
185
186 void GEOM_AISShape::setName(const Standard_CString aName)
187 {
188   myName = aName;
189
190   Handle(SALOME_InteractiveObject) IO = getIO();
191   if ( !IO.IsNull() )
192     IO->setName(aName);
193 }
194
195 Standard_CString GEOM_AISShape::getName(){
196   return myName.ToCString();
197 }
198
199 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
200                             const Handle(Prs3d_Presentation)& aPrs,
201                             const Standard_Integer aMode)
202 {  
203   if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
204
205   Handle(AIS_InteractiveContext) anIC = GetContext();
206  
207   //   StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
208   bool isTopLev = isTopLevel() && switchTopLevel();
209   switch (aMode) {
210     case 0://StdSelect_DM_Wireframe: 
211     {
212       restoreIsoNumbers();
213       // Restore wireframe edges colors
214       restoreBoundaryColors();
215       if(isTopLev) {
216               SetColor(topLevelColor());
217               Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
218               anAspect->SetColor( topLevelColor() );
219               Attributes()->SetWireAspect( anAspect );
220       }
221      StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);      
222       break;
223     }
224     case 1://StdSelect_DM_Shading:
225     {
226       restoreIsoNumbers();
227       shadingMode(aPresentationManager, aPrs, aMode);
228       // Store wireframe edges colors
229       storeBoundaryColors();
230       break;
231     }
232     case 3: //StdSelect_DM_HLR:
233     {
234       if(!isTopLev)
235               AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
236       else 
237               shadingMode(aPresentationManager, aPrs, AIS_Shaded);
238       break;
239     }
240   }
241
242   if ( aMode == ShadingWithEdges ) {
243     // Temporary store number of iso lines in order to recover its later 
244     // when display mode is achnged to 'Wirefame' or 'Shading'.
245     // Iso lines are not displayed in 'Shading with edges' mode.
246     storeIsoNumbers();
247
248     // Reset number of iso lines to 0
249     resetIsoNumbers();
250
251     //Shaded faces
252     shadingMode(aPresentationManager, aPrs, AIS_Shaded);
253
254     // Store wireframe edges colors
255     storeBoundaryColors();
256
257     // Coloring edges
258     Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
259     anAspect->SetColor( myEdgesInShadingColor );
260     myDrawer->SetUnFreeBoundaryAspect( anAspect );
261     
262     anAspect = myDrawer->FreeBoundaryAspect();
263     anAspect->SetColor( myEdgesInShadingColor );
264     myDrawer->SetFreeBoundaryAspect( anAspect );
265
266     // Add edges to presentation
267     StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
268   }
269
270   if (isShowVectors())
271   {
272     const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
273     TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
274     for ( ; Exp.More(); Exp.Next() ) {
275       TopoDS_Vertex aV1, aV2;
276       TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
277       if ( !isVector )
278         // draw curve direction (issue 0021087)
279         anEdgeE.Orientation( TopAbs_FORWARD );
280
281       if ( anEdgeE.IsNull() ) continue;
282
283       TopExp::Vertices(anEdgeE, aV1, aV2);
284       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
285       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
286
287       double fp,lp;
288       gp_Vec aDirVec;
289       Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
290
291       if ( C.IsNull() ) continue;
292
293       if ( anEdgeE.Orientation() == TopAbs_FORWARD )
294         C->D1(lp, aP2, aDirVec);
295       else {
296         C->D1(fp, aP1, aDirVec);
297         aP2 = aP1;
298       }
299
300       GeomAdaptor_Curve aAdC;
301       aAdC.Load(C, fp, lp);
302       Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp); 
303      
304       if (aDist > gp::Resolution()) {
305         gp_Dir aDir;
306         if ( anEdgeE.Orientation() == TopAbs_FORWARD )
307           aDir = aDirVec;
308         else
309           aDir = -aDirVec;
310
311         Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
312       }
313     }
314   }
315   //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
316 }
317
318 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
319 {
320   myShadingColor = aCol;
321 }
322
323 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
324 {
325   myEdgesInShadingColor = aCol;
326 }
327
328 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap, 
329                                        const Standard_Boolean aHighlight )
330 {
331   Handle(AIS_InteractiveObject) anObj = this;
332   Handle(AIS_InteractiveContext) anIC = GetContext();
333   if ( anIC.IsNull() || !anIC->HasOpenedContext() ) 
334     return;
335
336   Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
337   anIC->SetAutomaticHilight( false );
338
339   anIC->ClearSelected( false );
340
341   if ( aHighlight ) {
342     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
343
344     // Get entity owners for all activated selection modes
345     getEntityOwners( anObj, anIC, anAllMap );
346
347     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
348     indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
349
350
351     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
352       anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
353   }
354
355   anIC->SetAutomaticHilight( isAutoHilight );
356   anIC->HilightSelected( false );
357 }
358
359 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
360 {
361   myDisplayVectors = isDisplayed;
362 }
363
364 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
365                                 const Handle(Prs3d_Presentation)& aPrs,
366                                 const Standard_Integer aMode)
367 {
368   myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
369
370   Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
371   aMatAspect.SetTransparency(Transparency());
372   Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
373   Graphic3d_MaterialAspect currentBackMaterial  = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
374   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
375   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial  );
376
377   if( isTopLevel() && switchTopLevel() )
378     myDrawer->ShadingAspect()->SetColor( topLevelColor() );
379   else { 
380     if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
381       myDrawer->ShadingAspect()->SetColor(myShadingColor);
382     else
383       myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
384   }
385
386   // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
387   //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
388   AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
389 }
390
391 void GEOM_AISShape::storeIsoNumbers()
392 {
393   myUIsoNumber = myDrawer->UIsoAspect()->Number();
394   myVIsoNumber = myDrawer->VIsoAspect()->Number();
395 }
396
397 void GEOM_AISShape::restoreIsoNumbers()
398 {
399   if ( myUIsoNumber > 0 ) {
400     // Restore number of U iso lines
401     Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
402     anAspect->SetNumber( myUIsoNumber );
403     myDrawer->SetUIsoAspect( anAspect );
404   }
405   
406   if ( myVIsoNumber > 0 ) {
407     // Restore number of V iso lines
408     Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
409     anAspect->SetNumber( myVIsoNumber );
410     myDrawer->SetVIsoAspect( anAspect );
411   }
412 }
413
414 void GEOM_AISShape::resetIsoNumbers()
415 {
416   Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
417   anAspect->SetNumber( 0 );
418   myDrawer->SetUIsoAspect( anAspect );
419   
420   anAspect = myDrawer->VIsoAspect();
421   anAspect->SetNumber( 0 );
422   myDrawer->SetVIsoAspect( anAspect );
423 }
424
425 void GEOM_AISShape::storeBoundaryColors()
426 {
427   Aspect_TypeOfLine aLT;
428   Standard_Real aW;
429
430   myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
431   myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
432 }
433  
434 void GEOM_AISShape::restoreBoundaryColors()
435 {
436   Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
437   anAspect->SetColor( myFreeBoundaryColor );
438   myDrawer->SetFreeBoundaryAspect( anAspect );
439
440   anAspect = myDrawer->UnFreeBoundaryAspect();
441   anAspect->SetColor( myUnFreeBoundaryColor );
442   myDrawer->SetUnFreeBoundaryAspect( anAspect );
443 }
444
445
446 Standard_Boolean GEOM_AISShape::isTopLevel() {
447   return myTopLevel;
448 }
449
450 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
451   if(f) {
452     if(f != myTopLevel)
453       myPrevDisplayMode = DisplayMode();
454     Standard_Integer dm;
455     switch(topLevelDisplayMode()) {
456       case TopKeepCurrent : dm = myPrevDisplayMode; break;
457       case TopWireFrame : dm = AIS_WireFrame; break;     
458       case TopShadingWithEdges : dm = ShadingWithEdges; break;
459       default : dm = AIS_Shaded; break;
460     }
461     SetDisplayMode(dm);
462   } else {
463     if(f != myTopLevel)
464       SetDisplayMode(myPrevDisplayMode);
465   }
466   myTopLevel = f;
467 }
468
469 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
470   myPrevDisplayMode = mode;
471 }
472
473 Quantity_Color GEOM_AISShape::topLevelColor() {
474   return myTopLevelColor;
475 }
476
477 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
478   myTopLevelColor = c;
479 }
480
481 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
482   return myTopLevelDm;
483 }
484 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
485   myTopLevelDm = dm;
486 }
487
488 Standard_Boolean GEOM_AISShape::switchTopLevel() {
489         return myTopLevelDm != TopShowAdditionalWActor;
490 }
491
492 Standard_Boolean GEOM_AISShape::toActivate() {
493         return Standard_True;
494 }