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();
219 // AKL: use old behavior to avoid keeping object's wireframe
220 // if to change shape properties (for example: 'Clear Top Level State','Color', 'Isos')
221 // calling popup menu over(!) the shape in OCC viewer.
222 anIC->SetToHilightSelected( false );
224 bool anIsField = !myFieldStepData.isEmpty();
225 bool anIsColorField = anIsField && myFieldDataType != GEOM::FDT_String;
226 bool anIsTextField = anIsField && myFieldDataType == GEOM::FDT_String;
228 #if OCC_VERSION_LARGE > 0x06070200
229 if (isShowVertices())
230 myDrawer->SetVertexDrawMode(Prs3d_VDM_All);
233 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
234 bool isTopLev = isTopLevel() && switchTopLevel();
236 case 0://StdSelect_DM_Wireframe:
237 case CustomHighlight:
240 SetColor(topLevelColor());
241 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
242 anAspect->SetColor( topLevelColor() );
243 Attributes()->SetWireAspect( anAspect );
245 if( !isTopLev && anIsColorField && myFieldDimension == 1 )
246 drawField( aPrs, false, aMode == CustomHighlight );
248 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
251 case 1://StdSelect_DM_Shading:
253 shadingMode(aPresentationManager, aPrs, aMode);
256 case 2: { //ShadingWithEdges
258 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
259 myDrawer->SetFaceBoundaryDraw( Standard_True );
260 Handle(Prs3d_LineAspect) aBoundaryAspect =
261 new Prs3d_LineAspect ( myEdgesInShadingColor, Aspect_TOL_SOLID, myOwnWidth );
262 myDrawer->SetFaceBoundaryAspect (aBoundaryAspect);
265 case 3: //StdSelect_DM_HLR:
268 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
270 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
276 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
277 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
278 for ( ; Exp.More(); Exp.Next() ) {
279 TopoDS_Vertex aV1, aV2;
280 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
282 // draw curve direction (issue 0021087)
283 anEdgeE.Orientation( TopAbs_FORWARD );
285 if ( anEdgeE.IsNull() ) continue;
287 TopExp::Vertices(anEdgeE, aV1, aV2);
288 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
289 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
293 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
295 if ( C.IsNull() ) continue;
297 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
298 C->D1(lp, aP2, aDirVec);
300 C->D1(fp, aP1, aDirVec);
304 GeomAdaptor_Curve aAdC;
305 aAdC.Load(C, fp, lp);
306 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
308 if (aDist > gp::Resolution()) {
310 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
315 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
320 // draw color field on vertices
321 if( anIsColorField && myFieldDimension == 0 && aMode != CustomHighlight )
326 drawField( aPrs, true );
328 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
331 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
333 myShadingColor = aCol;
336 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
338 myEdgesInShadingColor = aCol;
341 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
342 const Standard_Boolean aHighlight )
344 Handle(AIS_InteractiveObject) anObj = this;
345 Handle(AIS_InteractiveContext) anIC = GetContext();
346 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
349 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
350 anIC->SetAutomaticHilight( false );
352 anIC->ClearSelected( false );
355 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
357 // Get entity owners for all activated selection modes
358 getEntityOwners( anObj, anIC, anAllMap );
360 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
361 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
364 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
365 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
368 anIC->SetAutomaticHilight( isAutoHilight );
369 anIC->HilightSelected( false );
372 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
374 myDisplayVectors = isDisplayed;
377 void GEOM_AISShape::SetDisplayVertices(bool isDisplayed)
379 myDisplayVertices = isDisplayed;
382 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
383 const Handle(Prs3d_Presentation)& aPrs,
384 const Standard_Integer aMode)
386 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
388 Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
389 aMatAspect.SetTransparency(Transparency());
390 Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
391 Graphic3d_MaterialAspect currentBackMaterial = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
392 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
393 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial );
395 if( isTopLevel() && switchTopLevel() )
396 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
398 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
399 myDrawer->ShadingAspect()->SetColor(myShadingColor);
401 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
404 bool anIsColorField = !myFieldStepData.isEmpty() && myFieldDataType != GEOM::FDT_String;
405 if( anIsColorField && ( myFieldDimension == 2 || myFieldDimension == 3 || myFieldDimension == -1 ) )
411 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
412 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
413 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
417 Standard_Boolean GEOM_AISShape::isTopLevel() {
421 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
424 myPrevDisplayMode = DisplayMode();
426 switch(topLevelDisplayMode()) {
427 case TopKeepCurrent : dm = myPrevDisplayMode; break;
428 case TopWireFrame : dm = AIS_WireFrame; break;
429 case TopShadingWithEdges : dm = ShadingWithEdges; break;
430 default : dm = AIS_Shaded; break;
435 SetDisplayMode(myPrevDisplayMode);
440 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
441 myPrevDisplayMode = mode;
444 Quantity_Color GEOM_AISShape::topLevelColor() {
445 return myTopLevelColor;
448 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
452 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
455 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
459 Standard_Boolean GEOM_AISShape::switchTopLevel() {
460 return myTopLevelDm != TopShowAdditionalWActor;
463 Standard_Boolean GEOM_AISShape::toActivate() {
464 return ( myTopLevel && myTopLevelDm == TopShowAdditionalWActor ) ? false : true;
467 void GEOM_AISShape::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
468 const int theFieldDimension,
469 const QList<QVariant>& theFieldStepData,
470 const TCollection_AsciiString& theFieldStepName,
471 const double theFieldStepRangeMin,
472 const double theFieldStepRangeMax )
474 myFieldDataType = theFieldDataType;
475 myFieldDimension = theFieldDimension;
476 myFieldStepData = theFieldStepData;
477 myFieldStepName = theFieldStepName;
478 myFieldStepRangeMin = theFieldStepRangeMin;
479 myFieldStepRangeMax = theFieldStepRangeMax;
482 void GEOM_AISShape::getFieldStepInfo( GEOM::field_data_type& theFieldDataType,
483 int& theFieldDimension,
484 QList<QVariant>& theFieldStepData,
485 TCollection_AsciiString& theFieldStepName,
486 double& theFieldStepRangeMin,
487 double& theFieldStepRangeMax ) const
489 theFieldDataType = myFieldDataType;
490 theFieldDimension = myFieldDimension;
491 theFieldStepData = myFieldStepData;
492 theFieldStepName = myFieldStepName;
493 theFieldStepRangeMin = myFieldStepRangeMin;
494 theFieldStepRangeMax = myFieldStepRangeMax;
497 void GEOM_AISShape::drawField( const Handle(Prs3d_Presentation)& thePrs,
498 const bool theIsString,
499 const bool theIsHighlight )
501 if( myFieldStepData.isEmpty() )
504 QListIterator<QVariant> aFieldStepDataIter( myFieldStepData );
506 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
507 switch( myFieldDimension )
509 case 0: aShapeType = TopAbs_VERTEX; break;
510 case 1: aShapeType = TopAbs_EDGE; break;
511 case 2: aShapeType = TopAbs_FACE; break;
512 case 3: aShapeType = TopAbs_SOLID; break;
513 case -1: aShapeType = TopAbs_VERTEX; break;
516 TopTools_IndexedMapOfShape aShapeMap;
517 TopExp::MapShapes( myshape, aShapeMap );
519 TColStd_IndexedMapOfInteger anIndexMap;
521 TopExp_Explorer anExp;
522 for( anExp.Init( myshape, aShapeType ); anExp.More(); anExp.Next() )
524 TopoDS_Shape aSubShape = anExp.Current();
525 if( !aSubShape.IsNull() )
527 Standard_Integer aSubShapeIndex = aShapeMap.FindIndex( aSubShape );
528 if( anIndexMap.Contains( aSubShapeIndex ) )
531 anIndexMap.Add( aSubShapeIndex );
533 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
537 if( aFieldStepDataIter.hasNext() )
539 const QVariant& aVariant = aFieldStepDataIter.next();
541 aString = aVariant.toString();
543 aQColor = aVariant.value<QColor>();
551 if( computeMassCenter( aSubShape, aCenter ) )
553 Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
555 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
556 anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
557 aGroup->SetPrimitivesAspect( anAspectText3d );
559 aGroup->Text( aString.toLatin1().constData(), aVertex, 14 );
564 Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
565 if( myFieldDimension == 0 )
567 TopoDS_Vertex aVertexShape = TopoDS::Vertex( aSubShape );
568 if( !aVertexShape.IsNull() )
570 gp_Pnt aPnt = BRep_Tool::Pnt( aVertexShape );
572 Handle(Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d();
573 anAspectMarker3d->SetColor( aColor );
574 anAspectMarker3d->SetType( Aspect_TOM_POINT );
575 anAspectMarker3d->SetScale( 10.0 );
576 aGroup->SetPrimitivesAspect( anAspectMarker3d );
578 Handle(Graphic3d_ArrayOfPoints) anArray = new Graphic3d_ArrayOfPoints( 1 );
579 anArray->AddVertex( aPnt.X(), aPnt.Y(), aPnt.Z() );
581 aGroup->AddPrimitiveArray( anArray );
584 else if( myFieldDimension == 1 )
586 myDrawer->WireAspect()->SetColor( aColor );
588 myDrawer->WireAspect()->SetWidth( myOwnWidth );
590 myDrawer->WireAspect()->SetWidth( myOwnWidth + 4 );
591 StdPrs_WFDeflectionShape::Add( thePrs, aSubShape, myDrawer );
593 else if( myFieldDimension == 2 ||
594 myFieldDimension == 3 ||
595 myFieldDimension == -1 )
597 myDrawer->ShadingAspect()->SetColor( aColor );
598 StdPrs_ShadedShape::Add( thePrs, aSubShape, myDrawer );
605 Standard_Boolean GEOM_AISShape::computeMassCenter( const TopoDS_Shape& theShape,
608 Standard_Real aX = 0, aY = 0, aZ = 0;
609 Standard_Integer aNbPoints = 0;
611 TopExp_Explorer anExp;
612 for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
614 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
615 if( !aVertex.IsNull() )
617 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
626 return Standard_False;
628 theCenter.SetCoord( aX / (Standard_Real)aNbPoints,
629 aY / (Standard_Real)aNbPoints,
630 aZ / (Standard_Real)aNbPoints );
631 return Standard_True;