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