]> SALOME platform Git repositories - modules/geom.git/blob - src/OBJECT/GEOM_AISShape.cxx
Salome HOME
Mantis issue 0021191: GlueEdges and GlueFaces problem with tolerance 1. A fix by...
[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 #include <SalomeApp_Tools.h>
76
77 #include <SUIT_Session.h>
78 #include <SUIT_ResourceMgr.h>
79
80
81 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
82                              const Handle(AIS_InteractiveContext)& theIC,
83                              SelectMgr_IndexedMapOfOwner& theMap )
84 {
85   if ( theObj.IsNull() || theIC.IsNull() )
86     return;
87
88   TColStd_ListOfInteger modes;
89   theIC->ActivatedModes( theObj, modes );
90
91   TColStd_ListIteratorOfListOfInteger itr( modes );
92   for (; itr.More(); itr.Next() ) {
93     int m = itr.Value();
94     if ( !theObj->HasSelection( m ) )
95       continue;
96
97     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
98
99     for ( sel->Init(); sel->More(); sel->Next() ) {
100       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
101       if ( entity.IsNull() )
102         continue;
103
104       Handle(SelectMgr_EntityOwner) owner =
105         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
106       if ( !owner.IsNull() )
107         theMap.Add( owner );
108     }
109   }
110 }
111
112 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
113                              const TopoDS_Shape& aMainShape,
114                              const SelectMgr_IndexedMapOfOwner& anAllMap, 
115                              SelectMgr_IndexedMapOfOwner& aToHiliteMap )
116 {
117   TopTools_IndexedMapOfShape aMapOfShapes;
118   TopExp::MapShapes(aMainShape, aMapOfShapes);
119
120   for  ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
121     Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
122     if ( anOwner.IsNull() || !anOwner->HasShape() )
123       continue;
124
125     const TopoDS_Shape& aSubShape = anOwner->Shape();
126     Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
127     if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
128       continue;
129     
130     if ( !aToHiliteMap.Contains( anOwner ) )
131       aToHiliteMap.Add( anOwner );
132   }
133 }
134
135 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
136                              const Standard_CString aName)
137   : SALOME_AISShape(shape), myName(aName), myDisplayVectors(false)
138 {
139   myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
140
141   storeBoundaryColors();
142
143   myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
144
145   myUIsoNumber = -1;
146   myVIsoNumber = -1;
147
148   myTopLevel = Standard_False;
149 }
150
151 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
152   SetOwner( io );
153 }
154
155 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
156   Handle(SALOME_InteractiveObject) IO;
157   if ( !GetOwner().IsNull() )
158     IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
159   return IO;
160 }
161
162 Standard_Boolean GEOM_AISShape::hasIO(){
163   return !getIO().IsNull();
164 }
165
166 void GEOM_AISShape::setName(const Standard_CString aName)
167 {
168   myName = aName;
169
170   Handle(SALOME_InteractiveObject) IO = getIO();
171   if ( !IO.IsNull() )
172     IO->setName(aName);
173 }
174
175 Standard_CString GEOM_AISShape::getName(){
176   return myName.ToCString();
177 }
178
179 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
180                             const Handle(Prs3d_Presentation)& aPrs,
181                             const Standard_Integer aMode)
182 {  
183   if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
184
185   Handle(AIS_InteractiveContext) anIC = GetContext();
186   
187   //   StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
188   switch (aMode) {
189     case 0://StdSelect_DM_Wireframe: 
190     {
191
192       restoreIsoNumbers();
193
194       // Restore wireframe edges colors
195       restoreBoundaryColors();
196
197
198       if(isTopLevel()) {
199         SetColor(topLevelColor());
200
201         Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
202         anAspect->SetColor( topLevelColor() );
203         Attributes()->SetWireAspect( anAspect );
204       }
205
206       StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
207       
208       break;
209     }
210     case 1://StdSelect_DM_Shading:
211     {
212       restoreIsoNumbers();
213
214       shadingMode(aPresentationManager, aPrs, aMode);
215
216       // Store wireframe edges colors
217       storeBoundaryColors();
218
219       break;
220     }
221     case 3: //StdSelect_DM_HLR:
222     {
223       if(!isTopLevel())
224         AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
225       else 
226         shadingMode(aPresentationManager, aPrs, AIS_Shaded);
227       break;
228     }
229   }
230
231   if ( aMode == ShadingWithEdges ) {
232     // Temporary store number of iso lines in order to recover its later 
233     // when display mode is achnged to 'Wirefame' or 'Shading'.
234     // Iso lines are not displayed in 'Shading with edges' mode.
235     storeIsoNumbers();
236
237     // Reset number of iso lines to 0
238     resetIsoNumbers();
239
240     //Shaded faces
241     shadingMode(aPresentationManager, aPrs, AIS_Shaded);
242
243     // Store wireframe edges colors
244     storeBoundaryColors();
245
246     // Coloring edges
247     Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
248     anAspect->SetColor( myEdgesInShadingColor );
249     myDrawer->SetUnFreeBoundaryAspect( anAspect );
250     
251     anAspect = myDrawer->FreeBoundaryAspect();
252     anAspect->SetColor( myEdgesInShadingColor );
253     myDrawer->SetFreeBoundaryAspect( anAspect );
254
255     // Add edges to presentation
256     StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
257   }
258
259   if (isShowVectors())
260   {
261     const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
262     TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
263     for ( ; Exp.More(); Exp.Next() ) {
264       TopoDS_Vertex aV1, aV2;
265       TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
266       if ( !isVector )
267         // draw curve direction (issue 0021087)
268         anEdgeE.Orientation( TopAbs_FORWARD );
269
270       if ( anEdgeE.IsNull() ) continue;
271
272       TopExp::Vertices(anEdgeE, aV1, aV2);
273       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
274       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
275
276       double fp,lp;
277       gp_Vec aDirVec;
278       Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
279
280       if ( C.IsNull() ) continue;
281
282       if ( anEdgeE.Orientation() == TopAbs_FORWARD )
283         C->D1(lp, aP2, aDirVec);
284       else {
285         C->D1(fp, aP1, aDirVec);
286         aP2 = aP1;
287       }
288
289       GeomAdaptor_Curve aAdC;
290       aAdC.Load(C, fp, lp);
291       Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp); 
292      
293       if (aDist > gp::Resolution()) {
294         gp_Dir aDir;
295         if ( anEdgeE.Orientation() == TopAbs_FORWARD )
296           aDir = aDirVec;
297         else
298           aDir = -aDirVec;
299
300         Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
301       }
302     }
303   }
304   //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
305 }
306
307 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
308 {
309   if(aValue<0.0 || aValue>1.0) return;
310   
311   if(aValue<=0.05) 
312     {
313       UnsetTransparency();
314       return;
315     }
316
317   Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
318   Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
319   FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
320   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
321   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
322   myTransparency = aValue;
323 }
324
325 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
326 {
327   myShadingColor = aCol;
328 }
329
330 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
331 {
332   myEdgesInShadingColor = aCol;
333 }
334
335 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap, 
336                                        const Standard_Boolean aHighlight )
337 {
338   Handle(AIS_InteractiveObject) anObj = this;
339   Handle(AIS_InteractiveContext) anIC = GetContext();
340   if ( anIC.IsNull() || !anIC->HasOpenedContext() ) 
341     return;
342
343   Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
344   anIC->SetAutomaticHilight( false );
345
346   anIC->ClearSelected( false );
347
348   if ( aHighlight ) {
349     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
350
351     // Get entity owners for all activated selection modes
352     getEntityOwners( anObj, anIC, anAllMap );
353
354     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
355     indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
356
357
358     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
359       anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
360   }
361
362   anIC->SetAutomaticHilight( isAutoHilight );
363   anIC->HilightSelected( false );
364 }
365
366 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
367 {
368   myDisplayVectors = isDisplayed;
369 }
370
371 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
372                                 const Handle(Prs3d_Presentation)& aPrs,
373                                 const Standard_Integer aMode)
374 {
375   myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
376
377       Graphic3d_MaterialAspect aMatAspect;
378       if ( !HasMaterial() ) {
379         aMatAspect.SetAmbient( 0.5 );
380         aMatAspect.SetDiffuse( 0.5 );
381         aMatAspect.SetEmissive( 0.5 );
382         aMatAspect.SetShininess(0.5 );
383         aMatAspect.SetSpecular( 0.5 );
384         
385         myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
386         myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Graphic3d_NOM_JADE);
387       }
388       
389       Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
390       Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
391       FMat.SetTransparency(myTransparency); BMat.SetTransparency(myTransparency);
392       myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
393       myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
394
395       //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
396       //       P->SetPrimitivesAspect(a4bis);
397       //        G->SetGroupPrimitivesAspect(a4bis);
398       //a4bis->SetInteriorColor(myShadingColor);
399       myDrawer->ShadingAspect()->SetColor(isTopLevel() ? topLevelColor() : myShadingColor);
400
401       // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
402       //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
403       AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
404 }
405
406 void GEOM_AISShape::storeIsoNumbers()
407 {
408   myUIsoNumber = myDrawer->UIsoAspect()->Number();
409   myVIsoNumber = myDrawer->VIsoAspect()->Number();
410 }
411
412 void GEOM_AISShape::restoreIsoNumbers()
413 {
414   if ( myUIsoNumber > 0 ) {
415     // Restore number of U iso lines
416     Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
417     anAspect->SetNumber( myUIsoNumber );
418     myDrawer->SetUIsoAspect( anAspect );
419   }
420   
421   if ( myVIsoNumber > 0 ) {
422     // Restore number of V iso lines
423     Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
424     anAspect->SetNumber( myVIsoNumber );
425     myDrawer->SetVIsoAspect( anAspect );
426   }
427 }
428
429 void GEOM_AISShape::resetIsoNumbers()
430 {
431   Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
432   anAspect->SetNumber( 0 );
433   myDrawer->SetUIsoAspect( anAspect );
434   
435   anAspect = myDrawer->VIsoAspect();
436   anAspect->SetNumber( 0 );
437   myDrawer->SetVIsoAspect( anAspect );
438 }
439
440 void GEOM_AISShape::storeBoundaryColors()
441 {
442   Aspect_TypeOfLine aLT;
443   Standard_Real aW;
444
445   myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
446   myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
447 }
448  
449 void GEOM_AISShape::restoreBoundaryColors()
450 {
451   Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
452   anAspect->SetColor( myFreeBoundaryColor );
453   myDrawer->SetFreeBoundaryAspect( anAspect );
454
455   anAspect = myDrawer->UnFreeBoundaryAspect();
456   anAspect->SetColor( myUnFreeBoundaryColor );
457   myDrawer->SetUnFreeBoundaryAspect( anAspect );
458 }
459
460
461 Standard_Boolean GEOM_AISShape::isTopLevel() {
462   return myTopLevel;
463 }
464
465 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
466   myTopLevel = f;
467 }
468
469 Quantity_Color GEOM_AISShape::topLevelColor() {
470   SUIT_Session* session = SUIT_Session::session();
471   SUIT_ResourceMgr* resMgr = session->resourceMgr();
472   QColor c = resMgr->colorValue( "Geometry", "toplevel_color", QColor( 170, 85, 0 ) );
473   return SalomeApp_Tools::color(c);
474 }