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