1 // Copyright (C) 2007-2014 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, or (at your option) any later version.
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 #include <Basics_OCCTVersion.hxx>
39 // Open CASCADE Includes
40 #include <AIS_Drawer.hxx>
41 #include <AIS_InteractiveContext.hxx>
43 #include <BRep_Tool.hxx>
45 #include <GCPnts_AbscissaPoint.hxx>
46 #include <GeomAdaptor_Curve.hxx>
50 #include <Graphic3d_AspectFillArea3d.hxx>
51 #include <Graphic3d_AspectLine3d.hxx>
52 #include <Graphic3d_AspectMarker3d.hxx>
53 #include <Graphic3d_AspectText3d.hxx>
55 #include <Prs3d_ShadingAspect.hxx>
56 #include <Prs3d_Arrow.hxx>
57 #include <Prs3d_IsoAspect.hxx>
58 #if OCC_VERSION_LARGE > 0x06070200
59 #include <Prs3d_VertexDrawMode.hxx>
62 #include <SelectBasics_SensitiveEntity.hxx>
63 #include <SelectMgr_EntityOwner.hxx>
64 #include <StdSelect_BRepOwner.hxx>
65 #include <SelectMgr_IndexedMapOfOwner.hxx>
66 #include <SelectMgr_Selection.hxx>
67 #include <StdSelect_DisplayMode.hxx>
68 #include <StdPrs_ShadedShape.hxx>
69 #include <StdPrs_WFDeflectionShape.hxx>
71 #include <TColStd_IndexedMapOfInteger.hxx>
72 #include <TColStd_ListIteratorOfListOfInteger.hxx>
73 #include <TColStd_ListOfInteger.hxx>
75 #include <TopoDS_Shape.hxx>
76 #include <TopTools_IndexedMapOfShape.hxx>
78 #include <TopExp_Explorer.hxx>
80 #include <TopoDS_Edge.hxx>
81 #include <TopoDS_Shape.hxx>
82 #include <TopoDS_Vertex.hxx>
84 #include <TColStd_SequenceOfInteger.hxx>
86 #include <V3d_View.hxx>
88 #include <SalomeApp_Tools.h>
90 #include <SUIT_Session.h>
91 #include <SUIT_ResourceMgr.h>
93 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::myTopLevelDm = GEOM_AISShape::TopKeepCurrent;
94 Quantity_Color GEOM_AISShape::myTopLevelColor;
97 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
98 const Handle(AIS_InteractiveContext)& theIC,
99 SelectMgr_IndexedMapOfOwner& theMap )
101 if ( theObj.IsNull() || theIC.IsNull() )
104 TColStd_ListOfInteger modes;
105 theIC->ActivatedModes( theObj, modes );
107 TColStd_ListIteratorOfListOfInteger itr( modes );
108 for (; itr.More(); itr.Next() ) {
110 if ( !theObj->HasSelection( m ) )
113 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
115 for ( sel->Init(); sel->More(); sel->Next() ) {
116 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
117 if ( entity.IsNull() )
120 Handle(SelectMgr_EntityOwner) owner =
121 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
122 if ( !owner.IsNull() )
128 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
129 const TopoDS_Shape& aMainShape,
130 const SelectMgr_IndexedMapOfOwner& anAllMap,
131 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
133 TopTools_IndexedMapOfShape aMapOfShapes;
134 TopExp::MapShapes(aMainShape, aMapOfShapes);
136 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
137 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
138 if ( anOwner.IsNull() || !anOwner->HasShape() )
141 const TopoDS_Shape& aSubShape = anOwner->Shape();
142 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
143 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
146 if ( !aToHiliteMap.Contains( anOwner ) )
147 aToHiliteMap.Add( anOwner );
151 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
152 const Standard_CString aName)
153 : SALOME_AISShape(shape),
155 myDisplayVectors(false),
156 myDisplayVertices(false),
157 myFieldDataType(GEOM::FDT_Double),
159 myFieldStepRangeMin(0),
160 myFieldStepRangeMax(0)
162 //rnv: Commented to avoid bug with local selection
163 //SetHilightMode( CustomHighlight ); // override setting the mode to 0 inside AIS_Shape constructor
165 myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
166 myPrevDisplayMode = 0;
168 myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
170 myTopLevel = Standard_False;
171 Graphic3d_MaterialAspect aMatAspect;
172 if ( !HasMaterial() ) {
173 aMatAspect.SetAmbient( 0.5 );
174 aMatAspect.SetDiffuse( 0.5 );
175 aMatAspect.SetEmissive( 0.5 );
176 aMatAspect.SetShininess(0.5 );
177 aMatAspect.SetSpecular( 0.5 );
179 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
180 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
184 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
188 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
189 Handle(SALOME_InteractiveObject) IO;
190 if ( !GetOwner().IsNull() )
191 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
195 Standard_Boolean GEOM_AISShape::hasIO(){
196 return !getIO().IsNull();
199 void GEOM_AISShape::setName(const Standard_CString aName)
203 Handle(SALOME_InteractiveObject) IO = getIO();
208 Standard_CString GEOM_AISShape::getName(){
209 return myName.ToCString();
212 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
213 const Handle(Prs3d_Presentation)& aPrs,
214 const Standard_Integer aMode)
216 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
218 Handle(AIS_InteractiveContext) anIC = GetContext();
220 bool anIsField = !myFieldStepData.isEmpty();
221 bool anIsColorField = anIsField && myFieldDataType != GEOM::FDT_String;
222 bool anIsTextField = anIsField && myFieldDataType == GEOM::FDT_String;
224 #if OCC_VERSION_LARGE > 0x06070200
225 if (isShowVertices())
226 myDrawer->SetVertexDrawMode(Prs3d_VDM_All);
229 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
230 bool isTopLev = isTopLevel() && switchTopLevel();
232 case 0://StdSelect_DM_Wireframe:
233 case CustomHighlight:
236 SetColor(topLevelColor());
237 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
238 anAspect->SetColor( topLevelColor() );
239 Attributes()->SetWireAspect( anAspect );
241 if( !isTopLev && anIsColorField && myFieldDimension == 1 )
242 drawField( aPrs, false, aMode == CustomHighlight );
244 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
247 case 1://StdSelect_DM_Shading:
249 shadingMode(aPresentationManager, aPrs, aMode);
252 case 2: { //ShadingWithEdges
254 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
255 myDrawer->SetFaceBoundaryDraw( Standard_True );
256 Handle(Prs3d_LineAspect) aBoundaryAspect =
257 new Prs3d_LineAspect ( myEdgesInShadingColor, Aspect_TOL_SOLID, myOwnWidth );
258 myDrawer->SetFaceBoundaryAspect (aBoundaryAspect);
261 case 3: //StdSelect_DM_HLR:
264 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
266 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
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.);
316 // draw color field on vertices
317 if( anIsColorField && myFieldDimension == 0 && aMode != CustomHighlight )
322 drawField( aPrs, true );
324 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
327 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
329 myShadingColor = aCol;
332 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
334 myEdgesInShadingColor = aCol;
337 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
338 const Standard_Boolean aHighlight )
340 Handle(AIS_InteractiveObject) anObj = this;
341 Handle(AIS_InteractiveContext) anIC = GetContext();
342 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
345 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
346 anIC->SetAutomaticHilight( false );
348 anIC->ClearSelected( false );
351 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
353 // Get entity owners for all activated selection modes
354 getEntityOwners( anObj, anIC, anAllMap );
356 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
357 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
360 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
361 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
364 anIC->SetAutomaticHilight( isAutoHilight );
365 anIC->HilightSelected( false );
368 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
370 myDisplayVectors = isDisplayed;
373 void GEOM_AISShape::SetDisplayVertices(bool isDisplayed)
375 myDisplayVertices = isDisplayed;
378 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
379 const Handle(Prs3d_Presentation)& aPrs,
380 const Standard_Integer aMode)
382 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
384 Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
385 aMatAspect.SetTransparency(Transparency());
386 Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
387 Graphic3d_MaterialAspect currentBackMaterial = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
388 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
389 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial );
391 if( isTopLevel() && switchTopLevel() )
392 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
394 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
395 myDrawer->ShadingAspect()->SetColor(myShadingColor);
397 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
400 bool anIsColorField = !myFieldStepData.isEmpty() && myFieldDataType != GEOM::FDT_String;
401 if( anIsColorField && ( myFieldDimension == 2 || myFieldDimension == 3 || myFieldDimension == -1 ) )
407 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
408 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
409 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
413 Standard_Boolean GEOM_AISShape::isTopLevel() {
417 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
420 myPrevDisplayMode = DisplayMode();
422 switch(topLevelDisplayMode()) {
423 case TopKeepCurrent : dm = myPrevDisplayMode; break;
424 case TopWireFrame : dm = AIS_WireFrame; break;
425 case TopShadingWithEdges : dm = ShadingWithEdges; break;
426 default : dm = AIS_Shaded; break;
431 SetDisplayMode(myPrevDisplayMode);
436 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
437 myPrevDisplayMode = mode;
440 Quantity_Color GEOM_AISShape::topLevelColor() {
441 return myTopLevelColor;
444 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
448 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
451 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
455 Standard_Boolean GEOM_AISShape::switchTopLevel() {
456 return myTopLevelDm != TopShowAdditionalWActor;
459 Standard_Boolean GEOM_AISShape::toActivate() {
460 return Standard_True;
463 void GEOM_AISShape::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
464 const int theFieldDimension,
465 const QList<QVariant>& theFieldStepData,
466 const TCollection_AsciiString& theFieldStepName,
467 const double theFieldStepRangeMin,
468 const double theFieldStepRangeMax )
470 myFieldDataType = theFieldDataType;
471 myFieldDimension = theFieldDimension;
472 myFieldStepData = theFieldStepData;
473 myFieldStepName = theFieldStepName;
474 myFieldStepRangeMin = theFieldStepRangeMin;
475 myFieldStepRangeMax = theFieldStepRangeMax;
478 void GEOM_AISShape::getFieldStepInfo( GEOM::field_data_type& theFieldDataType,
479 int& theFieldDimension,
480 QList<QVariant>& theFieldStepData,
481 TCollection_AsciiString& theFieldStepName,
482 double& theFieldStepRangeMin,
483 double& theFieldStepRangeMax ) const
485 theFieldDataType = myFieldDataType;
486 theFieldDimension = myFieldDimension;
487 theFieldStepData = myFieldStepData;
488 theFieldStepName = myFieldStepName;
489 theFieldStepRangeMin = myFieldStepRangeMin;
490 theFieldStepRangeMax = myFieldStepRangeMax;
493 void GEOM_AISShape::drawField( const Handle(Prs3d_Presentation)& thePrs,
494 const bool theIsString,
495 const bool theIsHighlight )
497 if( myFieldStepData.isEmpty() )
500 QListIterator<QVariant> aFieldStepDataIter( myFieldStepData );
502 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
503 switch( myFieldDimension )
505 case 0: aShapeType = TopAbs_VERTEX; break;
506 case 1: aShapeType = TopAbs_EDGE; break;
507 case 2: aShapeType = TopAbs_FACE; break;
508 case 3: aShapeType = TopAbs_SOLID; break;
509 case -1: aShapeType = TopAbs_VERTEX; break;
512 TopTools_IndexedMapOfShape aShapeMap;
513 TopExp::MapShapes( myshape, aShapeMap );
515 TColStd_IndexedMapOfInteger anIndexMap;
517 TopExp_Explorer anExp;
518 for( anExp.Init( myshape, aShapeType ); anExp.More(); anExp.Next() )
520 TopoDS_Shape aSubShape = anExp.Current();
521 if( !aSubShape.IsNull() )
523 Standard_Integer aSubShapeIndex = aShapeMap.FindIndex( aSubShape );
524 if( anIndexMap.Contains( aSubShapeIndex ) )
527 anIndexMap.Add( aSubShapeIndex );
529 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
533 if( aFieldStepDataIter.hasNext() )
535 const QVariant& aVariant = aFieldStepDataIter.next();
537 aString = aVariant.toString();
539 aQColor = aVariant.value<QColor>();
547 if( computeMassCenter( aSubShape, aCenter ) )
549 Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
551 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
552 anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
553 aGroup->SetPrimitivesAspect( anAspectText3d );
555 aGroup->Text( aString.toLatin1().constData(), aVertex, 14 );
560 Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
561 if( myFieldDimension == 0 )
563 TopoDS_Vertex aVertexShape = TopoDS::Vertex( aSubShape );
564 if( !aVertexShape.IsNull() )
566 gp_Pnt aPnt = BRep_Tool::Pnt( aVertexShape );
568 Handle(Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d();
569 anAspectMarker3d->SetColor( aColor );
570 anAspectMarker3d->SetType( Aspect_TOM_POINT );
571 anAspectMarker3d->SetScale( 10.0 );
572 aGroup->SetPrimitivesAspect( anAspectMarker3d );
574 Handle(Graphic3d_ArrayOfPoints) anArray = new Graphic3d_ArrayOfPoints( 1 );
575 anArray->AddVertex( aPnt.X(), aPnt.Y(), aPnt.Z() );
577 aGroup->AddPrimitiveArray( anArray );
580 else if( myFieldDimension == 1 )
582 myDrawer->WireAspect()->SetColor( aColor );
584 myDrawer->WireAspect()->SetWidth( myOwnWidth );
586 myDrawer->WireAspect()->SetWidth( myOwnWidth + 4 );
587 StdPrs_WFDeflectionShape::Add( thePrs, aSubShape, myDrawer );
589 else if( myFieldDimension == 2 ||
590 myFieldDimension == 3 ||
591 myFieldDimension == -1 )
593 myDrawer->ShadingAspect()->SetColor( aColor );
594 StdPrs_ShadedShape::Add( thePrs, aSubShape, myDrawer );
601 Standard_Boolean GEOM_AISShape::computeMassCenter( const TopoDS_Shape& theShape,
604 Standard_Real aX = 0, aY = 0, aZ = 0;
605 Standard_Integer aNbPoints = 0;
607 TopExp_Explorer anExp;
608 for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
610 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
611 if( !aVertex.IsNull() )
613 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
622 return Standard_False;
624 theCenter.SetCoord( aX / (Standard_Real)aNbPoints,
625 aY / (Standard_Real)aNbPoints,
626 aZ / (Standard_Real)aNbPoints );
627 return Standard_True;