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