1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM OBJECT : interactive object for Geometry entities visualization
24 // File : GEOM_AISShape.cxx
25 // Author : Nicolas REJNERI
29 \class GEOM_AISShape GEOM_AISShape.hxx
33 #include "GEOM_AISShape.ixx"
34 #include "SALOME_InteractiveObject.hxx"
35 #include "GEOM_AISVector.hxx"
37 // Open CASCADE Includes
38 #include <AIS_Drawer.hxx>
39 #include <AIS_InteractiveContext.hxx>
41 #include <BRep_Tool.hxx>
43 #include <GCPnts_AbscissaPoint.hxx>
44 #include <GeomAdaptor_Curve.hxx>
48 #include <Graphic3d_AspectFillArea3d.hxx>
49 #include <Graphic3d_AspectLine3d.hxx>
51 #include <Prs3d_ShadingAspect.hxx>
52 #include <Prs3d_Arrow.hxx>
53 #include <Prs3d_IsoAspect.hxx>
55 #include <SelectBasics_SensitiveEntity.hxx>
56 #include <SelectMgr_EntityOwner.hxx>
57 #include <StdSelect_BRepOwner.hxx>
58 #include <SelectMgr_IndexedMapOfOwner.hxx>
59 #include <SelectMgr_Selection.hxx>
60 #include <StdSelect_DisplayMode.hxx>
61 #include <StdPrs_WFDeflectionShape.hxx>
63 #include <TColStd_IndexedMapOfInteger.hxx>
64 #include <TColStd_ListIteratorOfListOfInteger.hxx>
65 #include <TColStd_ListOfInteger.hxx>
67 #include <TopoDS_Shape.hxx>
68 #include <TopTools_IndexedMapOfShape.hxx>
70 #include <TopExp_Explorer.hxx>
72 #include <TopoDS_Edge.hxx>
73 #include <TopoDS_Shape.hxx>
74 #include <TopoDS_Vertex.hxx>
76 #include <TColStd_SequenceOfInteger.hxx>
78 #include <V3d_View.hxx>
80 #include <SalomeApp_Tools.h>
82 #include <SUIT_Session.h>
83 #include <SUIT_ResourceMgr.h>
85 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::myTopLevelDm = GEOM_AISShape::TopKeepCurrent;
86 Quantity_Color GEOM_AISShape::myTopLevelColor;
89 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
90 const Handle(AIS_InteractiveContext)& theIC,
91 SelectMgr_IndexedMapOfOwner& theMap )
93 if ( theObj.IsNull() || theIC.IsNull() )
96 TColStd_ListOfInteger modes;
97 theIC->ActivatedModes( theObj, modes );
99 TColStd_ListIteratorOfListOfInteger itr( modes );
100 for (; itr.More(); itr.Next() ) {
102 if ( !theObj->HasSelection( m ) )
105 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
107 for ( sel->Init(); sel->More(); sel->Next() ) {
108 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
109 if ( entity.IsNull() )
112 Handle(SelectMgr_EntityOwner) owner =
113 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
114 if ( !owner.IsNull() )
120 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
121 const TopoDS_Shape& aMainShape,
122 const SelectMgr_IndexedMapOfOwner& anAllMap,
123 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
125 TopTools_IndexedMapOfShape aMapOfShapes;
126 TopExp::MapShapes(aMainShape, aMapOfShapes);
128 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
129 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
130 if ( anOwner.IsNull() || !anOwner->HasShape() )
133 const TopoDS_Shape& aSubShape = anOwner->Shape();
134 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
135 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
138 if ( !aToHiliteMap.Contains( anOwner ) )
139 aToHiliteMap.Add( anOwner );
143 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
144 const Standard_CString aName)
145 : SALOME_AISShape(shape), myName(aName), myDisplayVectors(false)
147 myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
148 myPrevDisplayMode = 0;
149 storeBoundaryColors();
152 myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
157 myTopLevel = Standard_False;
158 Graphic3d_MaterialAspect aMatAspect;
159 if ( !HasMaterial() ) {
160 aMatAspect.SetAmbient( 0.5 );
161 aMatAspect.SetDiffuse( 0.5 );
162 aMatAspect.SetEmissive( 0.5 );
163 aMatAspect.SetShininess(0.5 );
164 aMatAspect.SetSpecular( 0.5 );
166 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
167 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
171 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
175 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
176 Handle(SALOME_InteractiveObject) IO;
177 if ( !GetOwner().IsNull() )
178 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
182 Standard_Boolean GEOM_AISShape::hasIO(){
183 return !getIO().IsNull();
186 void GEOM_AISShape::setName(const Standard_CString aName)
190 Handle(SALOME_InteractiveObject) IO = getIO();
195 Standard_CString GEOM_AISShape::getName(){
196 return myName.ToCString();
199 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
200 const Handle(Prs3d_Presentation)& aPrs,
201 const Standard_Integer aMode)
203 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
205 Handle(AIS_InteractiveContext) anIC = GetContext();
207 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
208 bool isTopLev = isTopLevel() && switchTopLevel();
210 case 0://StdSelect_DM_Wireframe:
213 // Restore wireframe edges colors
214 restoreBoundaryColors();
216 SetColor(topLevelColor());
217 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
218 anAspect->SetColor( topLevelColor() );
219 Attributes()->SetWireAspect( anAspect );
221 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
224 case 1://StdSelect_DM_Shading:
227 shadingMode(aPresentationManager, aPrs, aMode);
228 // Store wireframe edges colors
229 storeBoundaryColors();
232 case 3: //StdSelect_DM_HLR:
235 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
237 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
242 if ( aMode == ShadingWithEdges ) {
243 // Temporary store number of iso lines in order to recover its later
244 // when display mode is achnged to 'Wirefame' or 'Shading'.
245 // Iso lines are not displayed in 'Shading with edges' mode.
248 // Reset number of iso lines to 0
252 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
254 // Store wireframe edges colors
255 storeBoundaryColors();
258 Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
259 anAspect->SetColor( myEdgesInShadingColor );
260 myDrawer->SetUnFreeBoundaryAspect( anAspect );
262 anAspect = myDrawer->FreeBoundaryAspect();
263 anAspect->SetColor( myEdgesInShadingColor );
264 myDrawer->SetFreeBoundaryAspect( anAspect );
266 // Add edges to presentation
267 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
272 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
273 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
274 for ( ; Exp.More(); Exp.Next() ) {
275 TopoDS_Vertex aV1, aV2;
276 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
278 // draw curve direction (issue 0021087)
279 anEdgeE.Orientation( TopAbs_FORWARD );
281 if ( anEdgeE.IsNull() ) continue;
283 TopExp::Vertices(anEdgeE, aV1, aV2);
284 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
285 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
289 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
291 if ( C.IsNull() ) continue;
293 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
294 C->D1(lp, aP2, aDirVec);
296 C->D1(fp, aP1, aDirVec);
300 GeomAdaptor_Curve aAdC;
301 aAdC.Load(C, fp, lp);
302 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
304 if (aDist > gp::Resolution()) {
306 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
311 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
315 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
318 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
320 myShadingColor = aCol;
323 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
325 myEdgesInShadingColor = aCol;
328 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
329 const Standard_Boolean aHighlight )
331 Handle(AIS_InteractiveObject) anObj = this;
332 Handle(AIS_InteractiveContext) anIC = GetContext();
333 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
336 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
337 anIC->SetAutomaticHilight( false );
339 anIC->ClearSelected( false );
342 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
344 // Get entity owners for all activated selection modes
345 getEntityOwners( anObj, anIC, anAllMap );
347 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
348 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
351 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
352 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
355 anIC->SetAutomaticHilight( isAutoHilight );
356 anIC->HilightSelected( false );
359 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
361 myDisplayVectors = isDisplayed;
364 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
365 const Handle(Prs3d_Presentation)& aPrs,
366 const Standard_Integer aMode)
368 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
370 Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
371 aMatAspect.SetTransparency(Transparency());
372 Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
373 Graphic3d_MaterialAspect currentBackMaterial = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
374 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
375 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial );
377 if( isTopLevel() && switchTopLevel() )
378 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
380 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
381 myDrawer->ShadingAspect()->SetColor(myShadingColor);
383 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
386 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
387 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
388 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
391 void GEOM_AISShape::storeIsoNumbers()
393 myUIsoNumber = myDrawer->UIsoAspect()->Number();
394 myVIsoNumber = myDrawer->VIsoAspect()->Number();
397 void GEOM_AISShape::restoreIsoNumbers()
399 if ( myUIsoNumber > 0 ) {
400 // Restore number of U iso lines
401 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
402 anAspect->SetNumber( myUIsoNumber );
403 myDrawer->SetUIsoAspect( anAspect );
406 if ( myVIsoNumber > 0 ) {
407 // Restore number of V iso lines
408 Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
409 anAspect->SetNumber( myVIsoNumber );
410 myDrawer->SetVIsoAspect( anAspect );
414 void GEOM_AISShape::resetIsoNumbers()
416 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
417 anAspect->SetNumber( 0 );
418 myDrawer->SetUIsoAspect( anAspect );
420 anAspect = myDrawer->VIsoAspect();
421 anAspect->SetNumber( 0 );
422 myDrawer->SetVIsoAspect( anAspect );
425 void GEOM_AISShape::storeBoundaryColors()
427 Aspect_TypeOfLine aLT;
430 myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
431 myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
434 void GEOM_AISShape::restoreBoundaryColors()
436 Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
437 anAspect->SetColor( myFreeBoundaryColor );
438 myDrawer->SetFreeBoundaryAspect( anAspect );
440 anAspect = myDrawer->UnFreeBoundaryAspect();
441 anAspect->SetColor( myUnFreeBoundaryColor );
442 myDrawer->SetUnFreeBoundaryAspect( anAspect );
446 Standard_Boolean GEOM_AISShape::isTopLevel() {
450 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
453 myPrevDisplayMode = DisplayMode();
455 switch(topLevelDisplayMode()) {
456 case TopKeepCurrent : dm = myPrevDisplayMode; break;
457 case TopWireFrame : dm = AIS_WireFrame; break;
458 case TopShadingWithEdges : dm = ShadingWithEdges; break;
459 default : dm = AIS_Shaded; break;
464 SetDisplayMode(myPrevDisplayMode);
469 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
470 myPrevDisplayMode = mode;
473 Quantity_Color GEOM_AISShape::topLevelColor() {
474 return myTopLevelColor;
477 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
481 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
484 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
488 Standard_Boolean GEOM_AISShape::switchTopLevel() {
489 return myTopLevelDm != TopShowAdditionalWActor;
492 Standard_Boolean GEOM_AISShape::toActivate() {
493 return Standard_True;