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