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