Salome HOME
7cba7edbe6ff272308c52f87e3eba8ecb00d701d
[modules/geom.git] / src / OBJECT / GEOM_AISShape.cxx
1 // Copyright (C) 2007-2012  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   myCurrentMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();  
170 }
171
172 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
173   SetOwner( io );
174 }
175
176 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
177   Handle(SALOME_InteractiveObject) IO;
178   if ( !GetOwner().IsNull() )
179     IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
180   return IO;
181 }
182
183 Standard_Boolean GEOM_AISShape::hasIO(){
184   return !getIO().IsNull();
185 }
186
187 void GEOM_AISShape::setName(const Standard_CString aName)
188 {
189   myName = aName;
190
191   Handle(SALOME_InteractiveObject) IO = getIO();
192   if ( !IO.IsNull() )
193     IO->setName(aName);
194 }
195
196 Standard_CString GEOM_AISShape::getName(){
197   return myName.ToCString();
198 }
199
200 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
201                             const Handle(Prs3d_Presentation)& aPrs,
202                             const Standard_Integer aMode)
203 {  
204   if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
205
206   Handle(AIS_InteractiveContext) anIC = GetContext();
207  
208   //   StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
209   bool isTopLev = (isTopLevel() && topLevelDisplayMode() != TopShowAdditionalWActor);
210   switch (aMode) {
211     case 0://StdSelect_DM_Wireframe: 
212     {
213       restoreIsoNumbers();
214       // Restore wireframe edges colors
215       restoreBoundaryColors();
216       if(isTopLev) {
217               SetColor(topLevelColor());
218               Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
219               anAspect->SetColor( topLevelColor() );
220               Attributes()->SetWireAspect( anAspect );
221       }
222      StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);      
223       break;
224     }
225     case 1://StdSelect_DM_Shading:
226     {
227       restoreIsoNumbers();
228       shadingMode(aPresentationManager, aPrs, aMode);
229       // Store wireframe edges colors
230       storeBoundaryColors();
231       break;
232     }
233     case 3: //StdSelect_DM_HLR:
234     {
235       if(!isTopLev)
236               AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
237       else 
238               shadingMode(aPresentationManager, aPrs, AIS_Shaded);
239       break;
240     }
241   }
242
243   if ( aMode == ShadingWithEdges ) {
244     // Temporary store number of iso lines in order to recover its later 
245     // when display mode is achnged to 'Wirefame' or 'Shading'.
246     // Iso lines are not displayed in 'Shading with edges' mode.
247     storeIsoNumbers();
248
249     // Reset number of iso lines to 0
250     resetIsoNumbers();
251
252     //Shaded faces
253     shadingMode(aPresentationManager, aPrs, AIS_Shaded);
254
255     // Store wireframe edges colors
256     storeBoundaryColors();
257
258     // Coloring edges
259     Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
260     anAspect->SetColor( myEdgesInShadingColor );
261     myDrawer->SetUnFreeBoundaryAspect( anAspect );
262     
263     anAspect = myDrawer->FreeBoundaryAspect();
264     anAspect->SetColor( myEdgesInShadingColor );
265     myDrawer->SetFreeBoundaryAspect( anAspect );
266
267     // Add edges to presentation
268     StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
269   }
270
271   if (isShowVectors())
272   {
273     const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
274     TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
275     for ( ; Exp.More(); Exp.Next() ) {
276       TopoDS_Vertex aV1, aV2;
277       TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
278       if ( !isVector )
279         // draw curve direction (issue 0021087)
280         anEdgeE.Orientation( TopAbs_FORWARD );
281
282       if ( anEdgeE.IsNull() ) continue;
283
284       TopExp::Vertices(anEdgeE, aV1, aV2);
285       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
286       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
287
288       double fp,lp;
289       gp_Vec aDirVec;
290       Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
291
292       if ( C.IsNull() ) continue;
293
294       if ( anEdgeE.Orientation() == TopAbs_FORWARD )
295         C->D1(lp, aP2, aDirVec);
296       else {
297         C->D1(fp, aP1, aDirVec);
298         aP2 = aP1;
299       }
300
301       GeomAdaptor_Curve aAdC;
302       aAdC.Load(C, fp, lp);
303       Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp); 
304      
305       if (aDist > gp::Resolution()) {
306         gp_Dir aDir;
307         if ( anEdgeE.Orientation() == TopAbs_FORWARD )
308           aDir = aDirVec;
309         else
310           aDir = -aDirVec;
311
312         Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
313       }
314     }
315   }
316   //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
317 }
318
319 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
320 {
321   if(aValue<0.0 || aValue>1.0) return;
322   
323   if(aValue<=0.05) 
324     {
325       UnsetTransparency();
326       return;
327     }
328
329   Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
330   FMat.SetTransparency(aValue);
331   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
332   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(FMat);
333   myCurrentMaterial = FMat;
334   myTransparency = aValue;
335 }
336
337 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
338 {
339   myShadingColor = aCol;
340 }
341
342 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
343 {
344   myEdgesInShadingColor = aCol;
345 }
346
347 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap, 
348                                        const Standard_Boolean aHighlight )
349 {
350   Handle(AIS_InteractiveObject) anObj = this;
351   Handle(AIS_InteractiveContext) anIC = GetContext();
352   if ( anIC.IsNull() || !anIC->HasOpenedContext() ) 
353     return;
354
355   Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
356   anIC->SetAutomaticHilight( false );
357
358   anIC->ClearSelected( false );
359
360   if ( aHighlight ) {
361     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
362
363     // Get entity owners for all activated selection modes
364     getEntityOwners( anObj, anIC, anAllMap );
365
366     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
367     indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
368
369
370     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
371       anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
372   }
373
374   anIC->SetAutomaticHilight( isAutoHilight );
375   anIC->HilightSelected( false );
376 }
377
378 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
379 {
380   myDisplayVectors = isDisplayed;
381 }
382
383 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
384                                 const Handle(Prs3d_Presentation)& aPrs,
385                                 const Standard_Integer aMode)
386 {
387   myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
388
389   Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
390   aMatAspect.SetTransparency(myTransparency);
391   myCurrentMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
392   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : myCurrentMaterial );
393   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial( isTopLevel() ? aMatAspect : myCurrentMaterial );
394
395       //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
396       //       P->SetPrimitivesAspect(a4bis);
397       //        G->SetGroupPrimitivesAspect(a4bis);
398       //a4bis->SetInteriorColor(myShadingColor);
399   if( isTopLevel() && topLevelDisplayMode() != TopShowAdditionalWActor )
400     myDrawer->ShadingAspect()->SetColor( topLevelColor() );
401   else { 
402     if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
403       myDrawer->ShadingAspect()->SetColor(myShadingColor);
404     else
405       myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
406   }
407
408   // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
409   //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
410   AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
411 }
412
413 void GEOM_AISShape::storeIsoNumbers()
414 {
415   myUIsoNumber = myDrawer->UIsoAspect()->Number();
416   myVIsoNumber = myDrawer->VIsoAspect()->Number();
417 }
418
419 void GEOM_AISShape::restoreIsoNumbers()
420 {
421   if ( myUIsoNumber > 0 ) {
422     // Restore number of U iso lines
423     Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
424     anAspect->SetNumber( myUIsoNumber );
425     myDrawer->SetUIsoAspect( anAspect );
426   }
427   
428   if ( myVIsoNumber > 0 ) {
429     // Restore number of V iso lines
430     Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
431     anAspect->SetNumber( myVIsoNumber );
432     myDrawer->SetVIsoAspect( anAspect );
433   }
434 }
435
436 void GEOM_AISShape::resetIsoNumbers()
437 {
438   Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
439   anAspect->SetNumber( 0 );
440   myDrawer->SetUIsoAspect( anAspect );
441   
442   anAspect = myDrawer->VIsoAspect();
443   anAspect->SetNumber( 0 );
444   myDrawer->SetVIsoAspect( anAspect );
445 }
446
447 void GEOM_AISShape::storeBoundaryColors()
448 {
449   Aspect_TypeOfLine aLT;
450   Standard_Real aW;
451
452   myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
453   myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
454 }
455  
456 void GEOM_AISShape::restoreBoundaryColors()
457 {
458   Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
459   anAspect->SetColor( myFreeBoundaryColor );
460   myDrawer->SetFreeBoundaryAspect( anAspect );
461
462   anAspect = myDrawer->UnFreeBoundaryAspect();
463   anAspect->SetColor( myUnFreeBoundaryColor );
464   myDrawer->SetUnFreeBoundaryAspect( anAspect );
465 }
466
467
468 Standard_Boolean GEOM_AISShape::isTopLevel() {
469   return myTopLevel;
470 }
471
472 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
473   if(f) {
474     if(f != myTopLevel)
475       myPrevDisplayMode = DisplayMode();
476     Standard_Integer dm;
477     switch(topLevelDisplayMode()) {
478       case TopKeepCurrent : dm = myPrevDisplayMode; break;
479       case TopWireFrame : dm = AIS_WireFrame; break;     
480       case TopShadingWithEdges : dm = ShadingWithEdges; break;
481       default : dm = AIS_Shaded; break;
482     }
483     SetDisplayMode(dm);
484   } else {
485     if(f != myTopLevel)
486       SetDisplayMode(myPrevDisplayMode);
487   }
488   myTopLevel = f;
489 }
490
491 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
492   myPrevDisplayMode = mode;
493 }
494
495 Quantity_Color GEOM_AISShape::topLevelColor() {
496   return myTopLevelColor;
497 }
498
499 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
500   myTopLevelColor = c;
501 }
502
503 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
504   return myTopLevelDm;
505 }
506 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
507   myTopLevelDm = dm;
508 }
509
510 Standard_Boolean GEOM_AISShape::switchTopLevel() {
511         return myTopLevelDm != TopShowAdditionalWActor;
512 }
513
514 Standard_Boolean GEOM_AISShape::toActivate() {
515         return Standard_True;
516 }