1 // Copyright (C) 2007-2012 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);
169 myCurrentMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
172 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
176 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
177 Handle(SALOME_InteractiveObject) IO;
178 if ( !GetOwner().IsNull() )
179 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
183 Standard_Boolean GEOM_AISShape::hasIO(){
184 return !getIO().IsNull();
187 void GEOM_AISShape::setName(const Standard_CString aName)
191 Handle(SALOME_InteractiveObject) IO = getIO();
196 Standard_CString GEOM_AISShape::getName(){
197 return myName.ToCString();
200 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
201 const Handle(Prs3d_Presentation)& aPrs,
202 const Standard_Integer aMode)
204 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
206 Handle(AIS_InteractiveContext) anIC = GetContext();
208 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
209 bool isTopLev = (isTopLevel() && topLevelDisplayMode() != TopShowAdditionalWActor);
211 case 0://StdSelect_DM_Wireframe:
214 // Restore wireframe edges colors
215 restoreBoundaryColors();
217 SetColor(topLevelColor());
218 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
219 anAspect->SetColor( topLevelColor() );
220 Attributes()->SetWireAspect( anAspect );
222 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
225 case 1://StdSelect_DM_Shading:
228 shadingMode(aPresentationManager, aPrs, aMode);
229 // Store wireframe edges colors
230 storeBoundaryColors();
233 case 3: //StdSelect_DM_HLR:
236 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
238 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
243 if ( aMode == ShadingWithEdges ) {
244 // Temporary store number of iso lines in order to recover its later
245 // when display mode is achnged to 'Wirefame' or 'Shading'.
246 // Iso lines are not displayed in 'Shading with edges' mode.
249 // Reset number of iso lines to 0
253 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
255 // Store wireframe edges colors
256 storeBoundaryColors();
259 Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
260 anAspect->SetColor( myEdgesInShadingColor );
261 myDrawer->SetUnFreeBoundaryAspect( anAspect );
263 anAspect = myDrawer->FreeBoundaryAspect();
264 anAspect->SetColor( myEdgesInShadingColor );
265 myDrawer->SetFreeBoundaryAspect( anAspect );
267 // Add edges to presentation
268 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
273 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
274 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
275 for ( ; Exp.More(); Exp.Next() ) {
276 TopoDS_Vertex aV1, aV2;
277 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
279 // draw curve direction (issue 0021087)
280 anEdgeE.Orientation( TopAbs_FORWARD );
282 if ( anEdgeE.IsNull() ) continue;
284 TopExp::Vertices(anEdgeE, aV1, aV2);
285 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
286 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
290 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
292 if ( C.IsNull() ) continue;
294 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
295 C->D1(lp, aP2, aDirVec);
297 C->D1(fp, aP1, aDirVec);
301 GeomAdaptor_Curve aAdC;
302 aAdC.Load(C, fp, lp);
303 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
305 if (aDist > gp::Resolution()) {
307 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
312 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
316 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
319 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
321 if(aValue<0.0 || aValue>1.0) return;
329 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
330 FMat.SetTransparency(aValue);
331 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
332 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(FMat);
333 myCurrentMaterial = FMat;
334 myTransparency = aValue;
337 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
339 myShadingColor = aCol;
342 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
344 myEdgesInShadingColor = aCol;
347 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
348 const Standard_Boolean aHighlight )
350 Handle(AIS_InteractiveObject) anObj = this;
351 Handle(AIS_InteractiveContext) anIC = GetContext();
352 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
355 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
356 anIC->SetAutomaticHilight( false );
358 anIC->ClearSelected( false );
361 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
363 // Get entity owners for all activated selection modes
364 getEntityOwners( anObj, anIC, anAllMap );
366 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
367 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
370 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
371 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
374 anIC->SetAutomaticHilight( isAutoHilight );
375 anIC->HilightSelected( false );
378 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
380 myDisplayVectors = isDisplayed;
383 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
384 const Handle(Prs3d_Presentation)& aPrs,
385 const Standard_Integer aMode)
387 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
389 Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
390 aMatAspect.SetTransparency(myTransparency);
391 myCurrentMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
392 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : myCurrentMaterial );
393 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial( isTopLevel() ? aMatAspect : myCurrentMaterial );
395 //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
396 // P->SetPrimitivesAspect(a4bis);
397 // G->SetGroupPrimitivesAspect(a4bis);
398 //a4bis->SetInteriorColor(myShadingColor);
399 if( isTopLevel() && topLevelDisplayMode() != TopShowAdditionalWActor )
400 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
402 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
403 myDrawer->ShadingAspect()->SetColor(myShadingColor);
405 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
408 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
409 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
410 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
413 void GEOM_AISShape::storeIsoNumbers()
415 myUIsoNumber = myDrawer->UIsoAspect()->Number();
416 myVIsoNumber = myDrawer->VIsoAspect()->Number();
419 void GEOM_AISShape::restoreIsoNumbers()
421 if ( myUIsoNumber > 0 ) {
422 // Restore number of U iso lines
423 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
424 anAspect->SetNumber( myUIsoNumber );
425 myDrawer->SetUIsoAspect( anAspect );
428 if ( myVIsoNumber > 0 ) {
429 // Restore number of V iso lines
430 Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
431 anAspect->SetNumber( myVIsoNumber );
432 myDrawer->SetVIsoAspect( anAspect );
436 void GEOM_AISShape::resetIsoNumbers()
438 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
439 anAspect->SetNumber( 0 );
440 myDrawer->SetUIsoAspect( anAspect );
442 anAspect = myDrawer->VIsoAspect();
443 anAspect->SetNumber( 0 );
444 myDrawer->SetVIsoAspect( anAspect );
447 void GEOM_AISShape::storeBoundaryColors()
449 Aspect_TypeOfLine aLT;
452 myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
453 myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
456 void GEOM_AISShape::restoreBoundaryColors()
458 Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
459 anAspect->SetColor( myFreeBoundaryColor );
460 myDrawer->SetFreeBoundaryAspect( anAspect );
462 anAspect = myDrawer->UnFreeBoundaryAspect();
463 anAspect->SetColor( myUnFreeBoundaryColor );
464 myDrawer->SetUnFreeBoundaryAspect( anAspect );
468 Standard_Boolean GEOM_AISShape::isTopLevel() {
472 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
475 myPrevDisplayMode = DisplayMode();
477 switch(topLevelDisplayMode()) {
478 case TopKeepCurrent : dm = myPrevDisplayMode; break;
479 case TopWireFrame : dm = AIS_WireFrame; break;
480 case TopShadingWithEdges : dm = ShadingWithEdges; break;
481 default : dm = AIS_Shaded; break;
486 SetDisplayMode(myPrevDisplayMode);
491 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
492 myPrevDisplayMode = mode;
495 Quantity_Color GEOM_AISShape::topLevelColor() {
496 return myTopLevelColor;
499 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
503 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
506 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
510 Standard_Boolean GEOM_AISShape::switchTopLevel() {
511 return myTopLevelDm != TopShowAdditionalWActor;
514 Standard_Boolean GEOM_AISShape::toActivate() {
515 return Standard_True;