Salome HOME
Update GUI documentation for bugs 16559
[modules/geom.git] / src / OBJECT / GEOM_AISShape.cxx
1 //  GEOM OBJECT : interactive object for Geometry entities visualization
2 //
3 //  Copyright (C) 2003  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 //
24 //  File   : GEOM_AISShape.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : GEOM
27 //  $Header$
28
29 /*!
30   \class GEOM_AISShape GEOM_AISShape.hxx
31   \brief ....
32 */
33
34 #include "GEOM_AISShape.ixx"
35 #include "SALOME_InteractiveObject.hxx"
36
37 #include "utilities.h"
38
39 // Open CASCADE Includes
40 #include <AIS_Drawer.hxx>
41 #include <AIS_InteractiveContext.hxx>
42 #include <Graphic3d_AspectFillArea3d.hxx>
43 #include <Prs3d_Drawer.hxx>
44 #include <Prs3d_IsoAspect.hxx>
45 #include <Prs3d_LineAspect.hxx>
46 #include <Prs3d_ShadingAspect.hxx>
47 #include <SelectBasics_SensitiveEntity.hxx>
48 #include <SelectMgr_EntityOwner.hxx>
49 #include <StdSelect_BRepOwner.hxx>
50 #include <SelectMgr_IndexedMapOfOwner.hxx>
51 #include <SelectMgr_Selection.hxx>
52 #include <StdSelect_DisplayMode.hxx>
53 #include <StdPrs_WFShape.hxx>
54 #include <StdPrs_ShadedShape.hxx>
55 #include <TColStd_IndexedMapOfInteger.hxx>
56 #include <TColStd_ListIteratorOfListOfInteger.hxx>
57 #include <TColStd_ListOfInteger.hxx>
58 #include <TopExp.hxx>
59 #include <TopoDS_Shape.hxx>
60 #include <TopTools_IndexedMapOfShape.hxx>
61
62 using namespace std;
63
64 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
65                              const Handle(AIS_InteractiveContext)& theIC,
66                              SelectMgr_IndexedMapOfOwner& theMap )
67 {
68   if ( theObj.IsNull() || theIC.IsNull() )
69     return;
70
71   TColStd_ListOfInteger modes;
72   theIC->ActivatedModes( theObj, modes );
73
74   TColStd_ListIteratorOfListOfInteger itr( modes );
75   for (; itr.More(); itr.Next() ) {
76     int m = itr.Value();
77     if ( !theObj->HasSelection( m ) )
78       continue;
79
80     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
81
82     for ( sel->Init(); sel->More(); sel->Next() ) {
83       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
84       if ( entity.IsNull() )
85         continue;
86
87       Handle(SelectMgr_EntityOwner) owner =
88         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
89       if ( !owner.IsNull() )
90         theMap.Add( owner );
91     }
92   }
93 }
94
95 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
96                              const TopoDS_Shape& aMainShape,
97                              const SelectMgr_IndexedMapOfOwner& anAllMap, 
98                              SelectMgr_IndexedMapOfOwner& aToHiliteMap )
99 {
100   TopTools_IndexedMapOfShape aMapOfShapes;
101   TopExp::MapShapes(aMainShape, aMapOfShapes);
102
103   for  ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
104     Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
105     if ( anOwner.IsNull() || !anOwner->HasShape() )
106       continue;
107
108     const TopoDS_Shape& aSubShape = anOwner->Shape();
109     Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
110     if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
111       continue;
112     
113     if ( !aToHiliteMap.Contains( anOwner ) )
114       aToHiliteMap.Add( anOwner );
115   }
116 }
117
118 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
119                              const Standard_CString aName)
120   : SALOME_AISShape(shape), myName(aName)
121 {
122   myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
123 }
124
125 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
126   SetOwner( io );
127 }
128
129 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
130   Handle(SALOME_InteractiveObject) IO;
131   if ( !GetOwner().IsNull() )
132     IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
133   return IO;
134 }
135
136 Standard_Boolean GEOM_AISShape::hasIO(){
137   return !getIO().IsNull();
138 }
139
140 void GEOM_AISShape::setName(const Standard_CString aName)
141 {
142   myName = aName;
143
144   Handle(SALOME_InteractiveObject) IO = getIO();
145   if ( !IO.IsNull() )
146     IO->setName(aName);
147 }
148
149 Standard_CString GEOM_AISShape::getName(){
150   return myName.ToCString();
151 }
152
153 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
154                             const Handle(Prs3d_Presentation)& aPrs,
155                             const Standard_Integer aMode)
156 {  
157   if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
158   
159   StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
160
161   switch (d) {
162   case StdSelect_DM_Wireframe: 
163     {
164       StdPrs_WFShape::Add(aPrs,myshape,myDrawer);
165       break;
166     }
167   case StdSelect_DM_Shading:
168     {
169       myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
170       
171       Graphic3d_MaterialAspect aMatAspect;
172       aMatAspect.SetAmbient( 0.5 );
173       aMatAspect.SetDiffuse( 0.5 );
174       aMatAspect.SetEmissive( 0.5 );
175       aMatAspect.SetShininess(0.5 );
176       aMatAspect.SetSpecular( 0.5 );
177       
178       myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
179       myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Graphic3d_NOM_JADE);
180       
181       Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
182       Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
183       FMat.SetTransparency(myTransparency); BMat.SetTransparency(myTransparency);
184       myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
185       myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
186
187       //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
188       //       P->SetPrimitivesAspect(a4bis);
189       //        G->SetGroupPrimitivesAspect(a4bis);
190       //a4bis->SetInteriorColor(myShadingColor);
191       myDrawer->ShadingAspect()->SetColor(myShadingColor);
192
193       // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
194       //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
195       AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
196       break;
197     }
198   }
199   //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
200 }
201
202 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
203 {
204   if(aValue<0.0 || aValue>1.0) return;
205   
206   if(aValue<=0.05) 
207     {
208       UnsetTransparency();
209       return;
210     }
211
212   Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
213   Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
214   FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
215   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
216   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
217   myTransparency = aValue;
218 }
219
220 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
221 {
222   myShadingColor = aCol;
223 }
224
225 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap, 
226                                        const Standard_Boolean aHighlight )
227 {
228   Handle(AIS_InteractiveObject) anObj = this;
229   Handle(AIS_InteractiveContext) anIC = GetContext();
230   if ( anIC.IsNull() || !anIC->HasOpenedContext() ) 
231     return;
232
233   Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
234   anIC->SetAutomaticHilight( false );
235
236   anIC->ClearSelected( false );
237
238   if ( aHighlight ) {
239     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
240
241     // Get entity owners for all activated selection modes
242     getEntityOwners( anObj, anIC, anAllMap );
243
244     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
245     indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
246
247
248     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
249       anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
250   }
251
252   anIC->SetAutomaticHilight( isAutoHilight );
253   anIC->HilightSelected( false );
254 }