]> SALOME platform Git repositories - modules/geom.git/blob - src/OBJECT/GEOM_AISShape.cxx
Salome HOME
Mantis issue 0021465: EDF 2067 GEOM: Extrusion along a path leads to a self-intersect...
[modules/geom.git] / src / OBJECT / GEOM_AISShape.cxx
1 // Copyright (C) 2007-2011  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 //  GEOM OBJECT : interactive object for Geometry entities visualization
23 //  File   : GEOM_AISShape.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : GEOM
26
27 /*!
28   \class GEOM_AISShape GEOM_AISShape.hxx
29   \brief ....
30 */
31
32 #include "GEOM_AISShape.ixx"
33 #include "SALOME_InteractiveObject.hxx"
34 #include "GEOM_AISVector.hxx"
35
36 // Open CASCADE Includes
37 #include <AIS_Drawer.hxx>
38 #include <AIS_InteractiveContext.hxx>
39
40 #include <BRep_Tool.hxx>
41
42 #include <GCPnts_AbscissaPoint.hxx>
43 #include <GeomAdaptor_Curve.hxx>
44 #include <gp_Pnt.hxx>
45 #include <gp_Dir.hxx>
46 #include <gp_Vec.hxx>
47 #include <Graphic3d_AspectFillArea3d.hxx>
48 #include <Graphic3d_AspectLine3d.hxx>
49
50 #include <Prs3d_ShadingAspect.hxx>
51 #include <Prs3d_Arrow.hxx>
52 #include <Prs3d_IsoAspect.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   storeBoundaryColors();
137
138   myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
139
140   myUIsoNumber = -1;
141   myVIsoNumber = -1;
142 }
143
144 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
145   SetOwner( io );
146 }
147
148 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
149   Handle(SALOME_InteractiveObject) IO;
150   if ( !GetOwner().IsNull() )
151     IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
152   return IO;
153 }
154
155 Standard_Boolean GEOM_AISShape::hasIO(){
156   return !getIO().IsNull();
157 }
158
159 void GEOM_AISShape::setName(const Standard_CString aName)
160 {
161   myName = aName;
162
163   Handle(SALOME_InteractiveObject) IO = getIO();
164   if ( !IO.IsNull() )
165     IO->setName(aName);
166 }
167
168 Standard_CString GEOM_AISShape::getName(){
169   return myName.ToCString();
170 }
171
172 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
173                             const Handle(Prs3d_Presentation)& aPrs,
174                             const Standard_Integer aMode)
175 {  
176   if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
177   
178 //   StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
179   switch (aMode) {
180     case 0://StdSelect_DM_Wireframe: 
181     {
182       restoreIsoNumbers();
183
184       // Restore wireframe edges colors
185       restoreBoundaryColors();
186
187       StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
188       break;
189     }
190     case 1://StdSelect_DM_Shading:
191     {
192       restoreIsoNumbers();
193
194       shadingMode(aPresentationManager, aPrs, aMode);
195
196       // Store wireframe edges colors
197       storeBoundaryColors();
198
199       break;
200     }
201     case 3: //StdSelect_DM_HLR:
202     {
203       AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
204       break;
205     }
206   }
207
208   if ( aMode == ShadingWithEdges ) {
209     // Temporary store number of iso lines in order to recover its later 
210     // when display mode is achnged to 'Wirefame' or 'Shading'.
211     // Iso lines are not displayed in 'Shading with edges' mode.
212     storeIsoNumbers();
213
214     // Reset number of iso lines to 0
215     resetIsoNumbers();
216
217     //Shaded faces
218     shadingMode(aPresentationManager, aPrs, AIS_Shaded);
219
220     // Store wireframe edges colors
221     storeBoundaryColors();
222
223     // Coloring edges
224     Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
225     anAspect->SetColor( myEdgesInShadingColor );
226     myDrawer->SetUnFreeBoundaryAspect( anAspect );
227     
228     anAspect = myDrawer->FreeBoundaryAspect();
229     anAspect->SetColor( myEdgesInShadingColor );
230     myDrawer->SetFreeBoundaryAspect( anAspect );
231
232     // Add edges to presentation
233     StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
234   }
235
236   if (isShowVectors())
237   {
238     const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
239     TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
240     for ( ; Exp.More(); Exp.Next() ) {
241       TopoDS_Vertex aV1, aV2;
242       TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
243       if ( !isVector )
244         // draw curve direction (issue 0021087)
245         anEdgeE.Orientation( TopAbs_FORWARD );
246
247       if ( anEdgeE.IsNull() ) continue;
248
249       TopExp::Vertices(anEdgeE, aV1, aV2);
250       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
251       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
252
253       double fp,lp;
254       gp_Vec aDirVec;
255       Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
256
257       if ( C.IsNull() ) continue;
258
259       if ( anEdgeE.Orientation() == TopAbs_FORWARD )
260         C->D1(lp, aP2, aDirVec);
261       else {
262         C->D1(fp, aP1, aDirVec);
263         aP2 = aP1;
264       }
265
266       GeomAdaptor_Curve aAdC;
267       aAdC.Load(C, fp, lp);
268       Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp); 
269      
270       if (aDist > gp::Resolution()) {
271         gp_Dir aDir;
272         if ( anEdgeE.Orientation() == TopAbs_FORWARD )
273           aDir = aDirVec;
274         else
275           aDir = -aDirVec;
276
277         Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
278       }
279     }
280   }
281   //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
282 }
283
284 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
285 {
286   if(aValue<0.0 || aValue>1.0) return;
287   
288   if(aValue<=0.05) 
289     {
290       UnsetTransparency();
291       return;
292     }
293
294   Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
295   Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
296   FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
297   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
298   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
299   myTransparency = aValue;
300 }
301
302 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
303 {
304   myShadingColor = aCol;
305 }
306
307 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
308 {
309   myEdgesInShadingColor = aCol;
310 }
311
312 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap, 
313                                        const Standard_Boolean aHighlight )
314 {
315   Handle(AIS_InteractiveObject) anObj = this;
316   Handle(AIS_InteractiveContext) anIC = GetContext();
317   if ( anIC.IsNull() || !anIC->HasOpenedContext() ) 
318     return;
319
320   Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
321   anIC->SetAutomaticHilight( false );
322
323   anIC->ClearSelected( false );
324
325   if ( aHighlight ) {
326     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
327
328     // Get entity owners for all activated selection modes
329     getEntityOwners( anObj, anIC, anAllMap );
330
331     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
332     indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
333
334
335     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
336       anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
337   }
338
339   anIC->SetAutomaticHilight( isAutoHilight );
340   anIC->HilightSelected( false );
341 }
342
343 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
344 {
345   myDisplayVectors = isDisplayed;
346 }
347
348 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
349                                 const Handle(Prs3d_Presentation)& aPrs,
350                                 const Standard_Integer aMode)
351 {
352   myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
353
354       Graphic3d_MaterialAspect aMatAspect;
355       if ( !HasMaterial() ) {
356         aMatAspect.SetAmbient( 0.5 );
357         aMatAspect.SetDiffuse( 0.5 );
358         aMatAspect.SetEmissive( 0.5 );
359         aMatAspect.SetShininess(0.5 );
360         aMatAspect.SetSpecular( 0.5 );
361         
362         myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
363         myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Graphic3d_NOM_JADE);
364       }
365       
366       Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
367       Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
368       FMat.SetTransparency(myTransparency); BMat.SetTransparency(myTransparency);
369       myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
370       myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
371
372       //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
373       //       P->SetPrimitivesAspect(a4bis);
374       //        G->SetGroupPrimitivesAspect(a4bis);
375       //a4bis->SetInteriorColor(myShadingColor);
376       myDrawer->ShadingAspect()->SetColor(myShadingColor);
377
378       // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
379       //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
380       AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
381 }
382
383 void GEOM_AISShape::storeIsoNumbers()
384 {
385   myUIsoNumber = myDrawer->UIsoAspect()->Number();
386   myVIsoNumber = myDrawer->VIsoAspect()->Number();
387 }
388
389 void GEOM_AISShape::restoreIsoNumbers()
390 {
391   if ( myUIsoNumber > 0 ) {
392     // Restore number of U iso lines
393     Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
394     anAspect->SetNumber( myUIsoNumber );
395     myDrawer->SetUIsoAspect( anAspect );
396   }
397   
398   if ( myVIsoNumber > 0 ) {
399     // Restore number of V iso lines
400     Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
401     anAspect->SetNumber( myVIsoNumber );
402     myDrawer->SetVIsoAspect( anAspect );
403   }
404 }
405
406 void GEOM_AISShape::resetIsoNumbers()
407 {
408   Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
409   anAspect->SetNumber( 0 );
410   myDrawer->SetUIsoAspect( anAspect );
411   
412   anAspect = myDrawer->VIsoAspect();
413   anAspect->SetNumber( 0 );
414   myDrawer->SetVIsoAspect( anAspect );
415 }
416
417 void GEOM_AISShape::storeBoundaryColors()
418 {
419   Aspect_TypeOfLine aLT;
420   Standard_Real aW;
421
422   myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
423   myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
424 }
425  
426 void GEOM_AISShape::restoreBoundaryColors()
427 {
428   Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
429   anAspect->SetColor( myFreeBoundaryColor );
430   myDrawer->SetFreeBoundaryAspect( anAspect );
431
432   anAspect = myDrawer->UnFreeBoundaryAspect();
433   anAspect->SetColor( myUnFreeBoundaryColor );
434   myDrawer->SetUnFreeBoundaryAspect( anAspect );
435 }