Salome HOME
Mantis issue 0021071: Regression in GetInPlace. A fix by PKV on Partition error.
[modules/geom.git] / src / OBJECT / GEOM_AISShape.cxx
1 //  Copyright (C) 2007-2010  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
23 //  GEOM OBJECT : interactive object for Geometry entities visualization
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
41 #include <BRep_Tool.hxx>
42
43 #include <GCPnts_AbscissaPoint.hxx>
44 #include <GeomAdaptor_Curve.hxx>
45 #include <gp_Pnt.hxx>
46 #include <gp_Dir.hxx>
47 #include <gp_Vec.hxx>
48 #include <Graphic3d_AspectFillArea3d.hxx>
49
50 #include <Prs3d_ShadingAspect.hxx>
51 #include <Prs3d_Arrow.hxx>
52
53 #include <SelectBasics_SensitiveEntity.hxx>
54 #include <SelectMgr_EntityOwner.hxx>
55 #include <StdSelect_BRepOwner.hxx>
56 #include <SelectMgr_IndexedMapOfOwner.hxx>
57 #include <SelectMgr_Selection.hxx>
58 #include <StdSelect_DisplayMode.hxx>
59 #include <StdPrs_WFDeflectionShape.hxx>
60
61 #include <TColStd_IndexedMapOfInteger.hxx>
62 #include <TColStd_ListIteratorOfListOfInteger.hxx>
63 #include <TColStd_ListOfInteger.hxx>
64 #include <TopExp.hxx>
65 #include <TopoDS_Shape.hxx>
66 #include <TopTools_IndexedMapOfShape.hxx>
67 #include <TopExp.hxx>
68 #include <TopExp_Explorer.hxx>
69 #include <TopoDS.hxx>
70 #include <TopoDS_Edge.hxx>
71 #include <TopoDS_Shape.hxx>
72 #include <TopoDS_Vertex.hxx>
73
74
75 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
76                              const Handle(AIS_InteractiveContext)& theIC,
77                              SelectMgr_IndexedMapOfOwner& theMap )
78 {
79   if ( theObj.IsNull() || theIC.IsNull() )
80     return;
81
82   TColStd_ListOfInteger modes;
83   theIC->ActivatedModes( theObj, modes );
84
85   TColStd_ListIteratorOfListOfInteger itr( modes );
86   for (; itr.More(); itr.Next() ) {
87     int m = itr.Value();
88     if ( !theObj->HasSelection( m ) )
89       continue;
90
91     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
92
93     for ( sel->Init(); sel->More(); sel->Next() ) {
94       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
95       if ( entity.IsNull() )
96         continue;
97
98       Handle(SelectMgr_EntityOwner) owner =
99         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
100       if ( !owner.IsNull() )
101         theMap.Add( owner );
102     }
103   }
104 }
105
106 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
107                              const TopoDS_Shape& aMainShape,
108                              const SelectMgr_IndexedMapOfOwner& anAllMap, 
109                              SelectMgr_IndexedMapOfOwner& aToHiliteMap )
110 {
111   TopTools_IndexedMapOfShape aMapOfShapes;
112   TopExp::MapShapes(aMainShape, aMapOfShapes);
113
114   for  ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
115     Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
116     if ( anOwner.IsNull() || !anOwner->HasShape() )
117       continue;
118
119     const TopoDS_Shape& aSubShape = anOwner->Shape();
120     Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
121     if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
122       continue;
123     
124     if ( !aToHiliteMap.Contains( anOwner ) )
125       aToHiliteMap.Add( anOwner );
126   }
127 }
128
129 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
130                              const Standard_CString aName)
131   : SALOME_AISShape(shape), myName(aName), myDisplayVectors(false)
132 {
133   myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
134 }
135
136 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
137   SetOwner( io );
138 }
139
140 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
141   Handle(SALOME_InteractiveObject) IO;
142   if ( !GetOwner().IsNull() )
143     IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
144   return IO;
145 }
146
147 Standard_Boolean GEOM_AISShape::hasIO(){
148   return !getIO().IsNull();
149 }
150
151 void GEOM_AISShape::setName(const Standard_CString aName)
152 {
153   myName = aName;
154
155   Handle(SALOME_InteractiveObject) IO = getIO();
156   if ( !IO.IsNull() )
157     IO->setName(aName);
158 }
159
160 Standard_CString GEOM_AISShape::getName(){
161   return myName.ToCString();
162 }
163
164 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
165                             const Handle(Prs3d_Presentation)& aPrs,
166                             const Standard_Integer aMode)
167 {  
168   if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
169   
170   StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
171
172   switch (d) {
173   case StdSelect_DM_Wireframe: 
174     {
175       StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
176       break;
177     }
178   case StdSelect_DM_Shading:
179     {
180       myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
181       
182       Graphic3d_MaterialAspect aMatAspect;
183       aMatAspect.SetAmbient( 0.5 );
184       aMatAspect.SetDiffuse( 0.5 );
185       aMatAspect.SetEmissive( 0.5 );
186       aMatAspect.SetShininess(0.5 );
187       aMatAspect.SetSpecular( 0.5 );
188       
189       myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
190       myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Graphic3d_NOM_JADE);
191       
192       Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
193       Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
194       FMat.SetTransparency(myTransparency); BMat.SetTransparency(myTransparency);
195       myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
196       myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
197
198       //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
199       //       P->SetPrimitivesAspect(a4bis);
200       //        G->SetGroupPrimitivesAspect(a4bis);
201       //a4bis->SetInteriorColor(myShadingColor);
202       myDrawer->ShadingAspect()->SetColor(myShadingColor);
203
204       // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
205       //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
206       AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
207       break;
208     }
209   }
210
211   if (isShowVectors())
212   {
213     TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
214     for ( ; Exp.More(); Exp.Next() ) {
215       TopoDS_Vertex aV1, aV2;
216       TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
217
218       if ( anEdgeE.IsNull() ) continue;
219
220       TopExp::Vertices(anEdgeE, aV1, aV2);
221       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
222       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
223
224       double fp,lp;
225       gp_Vec aDirVec;
226       Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
227
228       if ( C.IsNull() ) continue;
229
230       if ( anEdgeE.Orientation() == TopAbs_FORWARD )
231         C->D1(lp, aP2, aDirVec);
232       else {
233         C->D1(fp, aP1, aDirVec);
234         aP2 = aP1;
235       }
236
237       GeomAdaptor_Curve aAdC;
238       aAdC.Load(C, fp, lp);
239       Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp); 
240      
241       if (aDist > gp::Resolution()) {
242         gp_Dir aDir;
243         if ( anEdgeE.Orientation() == TopAbs_FORWARD )
244           aDir = aDirVec;
245         else
246           aDir = -aDirVec;
247
248         Prs3d_Arrow::Draw(aPrs, aP2, aDir, PI/180.*5., aDist/10.);
249       }
250     }
251   }
252   //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
253 }
254
255 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
256 {
257   if(aValue<0.0 || aValue>1.0) return;
258   
259   if(aValue<=0.05) 
260     {
261       UnsetTransparency();
262       return;
263     }
264
265   Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
266   Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
267   FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
268   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
269   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
270   myTransparency = aValue;
271 }
272
273 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
274 {
275   myShadingColor = aCol;
276 }
277
278 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap, 
279                                        const Standard_Boolean aHighlight )
280 {
281   Handle(AIS_InteractiveObject) anObj = this;
282   Handle(AIS_InteractiveContext) anIC = GetContext();
283   if ( anIC.IsNull() || !anIC->HasOpenedContext() ) 
284     return;
285
286   Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
287   anIC->SetAutomaticHilight( false );
288
289   anIC->ClearSelected( false );
290
291   if ( aHighlight ) {
292     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
293
294     // Get entity owners for all activated selection modes
295     getEntityOwners( anObj, anIC, anAllMap );
296
297     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
298     indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
299
300
301     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
302       anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
303   }
304
305   anIC->SetAutomaticHilight( isAutoHilight );
306   anIC->HilightSelected( false );
307 }
308
309 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
310 {
311   myDisplayVectors = isDisplayed;
312 }