1 // Copyright (C) 2007-2015 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
24 \class GEOM_AISShape GEOM_AISShape.hxx
28 #include "GEOM_AISShape.hxx"
29 #include "GEOM_AISVector.hxx"
31 #include <GEOMUtils.hxx>
33 #include <Basics_OCCTVersion.hxx>
35 // Open CASCADE Includes
36 #include <AIS_InteractiveContext.hxx>
37 #include <BRep_Tool.hxx>
38 #include <GCPnts_AbscissaPoint.hxx>
39 #include <GeomAdaptor_Curve.hxx>
43 #include <Graphic3d_AspectFillArea3d.hxx>
44 #include <Graphic3d_AspectLine3d.hxx>
45 #include <Graphic3d_AspectMarker3d.hxx>
46 #include <Graphic3d_AspectText3d.hxx>
48 #include <Prs3d_ShadingAspect.hxx>
49 #include <Prs3d_Arrow.hxx>
50 #include <Prs3d_IsoAspect.hxx>
51 #if OCC_VERSION_LARGE > 0x06070200
52 #include <Prs3d_VertexDrawMode.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_ShadedShape.hxx>
62 #include <StdPrs_WFDeflectionShape.hxx>
64 #include <TColStd_IndexedMapOfInteger.hxx>
65 #include <TColStd_ListIteratorOfListOfInteger.hxx>
66 #include <TColStd_ListOfInteger.hxx>
68 #include <TopoDS_Shape.hxx>
69 #include <TopTools_IndexedMapOfShape.hxx>
71 #include <TopExp_Explorer.hxx>
73 #include <TopoDS_Edge.hxx>
74 #include <TopoDS_Shape.hxx>
75 #include <TopoDS_Vertex.hxx>
77 #include <TColStd_SequenceOfInteger.hxx>
79 #include <V3d_View.hxx>
81 #include <SalomeApp_Tools.h>
82 #include <SUIT_Session.h>
83 #include <SUIT_ResourceMgr.h>
85 IMPLEMENT_STANDARD_HANDLE (GEOM_AISShape, SALOME_AISShape)
86 IMPLEMENT_STANDARD_RTTIEXT(GEOM_AISShape, SALOME_AISShape)
88 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::myTopLevelDm = GEOM_AISShape::TopKeepCurrent;
89 Quantity_Color GEOM_AISShape::myTopLevelColor;
92 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
93 const Handle(AIS_InteractiveContext)& theIC,
94 SelectMgr_IndexedMapOfOwner& theMap )
96 if ( theObj.IsNull() || theIC.IsNull() )
99 TColStd_ListOfInteger modes;
100 theIC->ActivatedModes( theObj, modes );
102 TColStd_ListIteratorOfListOfInteger itr( modes );
103 for (; itr.More(); itr.Next() ) {
105 if ( !theObj->HasSelection( m ) )
108 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
110 for ( sel->Init(); sel->More(); sel->Next() ) {
111 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
112 if ( entity.IsNull() )
115 Handle(SelectMgr_EntityOwner) owner =
116 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
117 if ( !owner.IsNull() )
123 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
124 const TopoDS_Shape& aMainShape,
125 const SelectMgr_IndexedMapOfOwner& anAllMap,
126 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
128 TopTools_IndexedMapOfShape aMapOfShapes;
129 TopExp::MapShapes(aMainShape, aMapOfShapes);
131 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
132 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
133 if ( anOwner.IsNull() || !anOwner->HasShape() )
136 const TopoDS_Shape& aSubShape = anOwner->Shape();
137 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
138 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
141 if ( !aToHiliteMap.Contains( anOwner ) )
142 aToHiliteMap.Add( anOwner );
146 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
147 const Standard_CString aName)
148 : SALOME_AISShape(shape),
150 myDisplayVectors(false),
151 myDisplayVertices(false),
152 myFieldDataType(GEOM::FDT_Double),
154 myFieldStepRangeMin(0),
155 myFieldStepRangeMax(0)
157 //rnv: Commented to avoid bug with local selection
158 //SetHilightMode( CustomHighlight ); // override setting the mode to 0 inside AIS_Shape constructor
160 myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
161 myPrevDisplayMode = 0;
163 myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
165 myTopLevel = Standard_False;
166 Graphic3d_MaterialAspect aMatAspect;
167 if ( !HasMaterial() ) {
168 aMatAspect.SetAmbient( 0.5 );
169 aMatAspect.SetDiffuse( 0.5 );
170 aMatAspect.SetEmissive( 0.5 );
171 aMatAspect.SetShininess(0.5 );
172 aMatAspect.SetSpecular( 0.5 );
174 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
175 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
179 GEOM_AISShape::~GEOM_AISShape()
183 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
187 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
188 Handle(SALOME_InteractiveObject) IO;
189 if ( !GetOwner().IsNull() )
190 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
194 Standard_Boolean GEOM_AISShape::hasIO(){
195 return !getIO().IsNull();
198 void GEOM_AISShape::setName(const Standard_CString aName)
202 Handle(SALOME_InteractiveObject) IO = getIO();
207 Standard_CString GEOM_AISShape::getName(){
208 return myName.ToCString();
211 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
212 const Handle(Prs3d_Presentation)& aPrs,
213 const Standard_Integer aMode)
215 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
217 Handle(AIS_InteractiveContext) anIC = GetContext();
218 // AKL: use old behavior to avoid keeping object's wireframe
219 // if to change shape properties (for example: 'Clear Top Level State','Color', 'Isos')
220 // calling popup menu over(!) the shape in OCC viewer.
221 anIC->SetToHilightSelected( false );
223 bool anIsField = !myFieldStepData.isEmpty();
224 bool anIsColorField = anIsField && myFieldDataType != GEOM::FDT_String;
225 bool anIsTextField = anIsField && myFieldDataType == GEOM::FDT_String;
227 #if OCC_VERSION_LARGE > 0x06070200
228 if (isShowVertices())
229 myDrawer->SetVertexDrawMode(Prs3d_VDM_All);
232 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
233 bool isTopLev = isTopLevel() && switchTopLevel();
236 case CustomHighlight:
239 SetColor(topLevelColor());
240 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
241 anAspect->SetColor( topLevelColor() );
242 Attributes()->SetWireAspect( anAspect );
244 if( !isTopLev && anIsColorField && myFieldDimension == 1 )
245 drawField( aPrs, false, aMode == CustomHighlight );
247 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
252 shadingMode(aPresentationManager, aPrs, aMode);
255 case ShadingWithEdges:
257 shadingMode(aPresentationManager, aPrs, Shading);
258 if( anIsColorField && myFieldDimension == 1 )
261 myDrawer->SetFaceBoundaryDraw( Standard_True );
267 #ifdef USE_TEXTURED_SHAPE
268 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
270 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
273 shadingMode(aPresentationManager, aPrs, Shading);
279 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
280 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
281 for ( ; Exp.More(); Exp.Next() ) {
282 TopoDS_Vertex aV1, aV2;
283 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
285 // draw curve direction (issue 0021087)
286 anEdgeE.Orientation( TopAbs_FORWARD );
288 if ( anEdgeE.IsNull() ) continue;
290 TopExp::Vertices(anEdgeE, aV1, aV2);
291 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
292 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
296 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
298 if ( C.IsNull() ) continue;
300 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
301 C->D1(lp, aP2, aDirVec);
303 C->D1(fp, aP1, aDirVec);
307 GeomAdaptor_Curve aAdC;
308 aAdC.Load(C, fp, lp);
309 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
311 if (aDist > gp::Resolution()) {
313 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
318 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
323 // draw color field on vertices
324 if( anIsColorField && myFieldDimension == 0 && aMode != CustomHighlight )
329 drawField( aPrs, true );
334 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
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::SetLabelColor(const Quantity_Color &aCol) {
351 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
352 const Standard_Boolean aHighlight )
354 Handle(AIS_InteractiveObject) anObj = this;
355 Handle(AIS_InteractiveContext) anIC = GetContext();
356 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
359 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
360 anIC->SetAutomaticHilight( false );
362 anIC->ClearSelected( false );
365 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
367 // Get entity owners for all activated selection modes
368 getEntityOwners( anObj, anIC, anAllMap );
370 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
371 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
374 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
375 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
378 anIC->SetAutomaticHilight( isAutoHilight );
379 anIC->HilightSelected( false );
382 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
384 myDisplayVectors = isDisplayed;
387 void GEOM_AISShape::SetDisplayVertices(bool isDisplayed)
389 myDisplayVertices = isDisplayed;
392 void GEOM_AISShape::SetDisplayName(bool isDisplayed)
394 myDisplayName = isDisplayed;
397 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
398 const Handle(Prs3d_Presentation)& aPrs,
399 const Standard_Integer aMode)
401 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
403 Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
404 aMatAspect.SetTransparency(Transparency());
405 Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
406 Graphic3d_MaterialAspect currentBackMaterial = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
407 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
408 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial );
410 if( isTopLevel() && switchTopLevel() )
411 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
413 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
414 myDrawer->ShadingAspect()->SetColor(myShadingColor);
416 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
419 bool anIsColorField = !myFieldStepData.isEmpty() && myFieldDataType != GEOM::FDT_String;
420 if( anIsColorField && ( myFieldDimension == 2 || myFieldDimension == 3 || myFieldDimension == -1 ) )
426 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
427 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
428 #ifdef USE_TEXTURED_SHAPE
429 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
431 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
436 Standard_Boolean GEOM_AISShape::isTopLevel() {
440 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
443 myPrevDisplayMode = DisplayMode();
445 switch(topLevelDisplayMode()) {
446 case TopKeepCurrent : dm = myPrevDisplayMode; break;
447 case TopWireFrame : dm = Wireframe; break;
448 case TopShadingWithEdges : dm = ShadingWithEdges; break;
449 default : dm = Shading; break;
454 SetDisplayMode(myPrevDisplayMode);
459 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
460 myPrevDisplayMode = mode;
463 Quantity_Color GEOM_AISShape::topLevelColor() {
464 return myTopLevelColor;
467 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
471 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
474 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
478 Standard_Boolean GEOM_AISShape::switchTopLevel() {
479 return myTopLevelDm != TopShowAdditionalWActor;
482 Standard_Boolean GEOM_AISShape::toActivate() {
483 return ( myTopLevel && myTopLevelDm == TopShowAdditionalWActor ) ? false : true;
486 void GEOM_AISShape::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
487 const int theFieldDimension,
488 const QList<QVariant>& theFieldStepData,
489 const TCollection_AsciiString& theFieldStepName,
490 const double theFieldStepRangeMin,
491 const double theFieldStepRangeMax )
493 myFieldDataType = theFieldDataType;
494 myFieldDimension = theFieldDimension;
495 myFieldStepData = theFieldStepData;
496 myFieldStepName = theFieldStepName;
497 myFieldStepRangeMin = theFieldStepRangeMin;
498 myFieldStepRangeMax = theFieldStepRangeMax;
501 void GEOM_AISShape::getFieldStepInfo( GEOM::field_data_type& theFieldDataType,
502 int& theFieldDimension,
503 QList<QVariant>& theFieldStepData,
504 TCollection_AsciiString& theFieldStepName,
505 double& theFieldStepRangeMin,
506 double& theFieldStepRangeMax ) const
508 theFieldDataType = myFieldDataType;
509 theFieldDimension = myFieldDimension;
510 theFieldStepData = myFieldStepData;
511 theFieldStepName = myFieldStepName;
512 theFieldStepRangeMin = myFieldStepRangeMin;
513 theFieldStepRangeMax = myFieldStepRangeMax;
516 void GEOM_AISShape::drawField( const Handle(Prs3d_Presentation)& thePrs,
517 const bool theIsString,
518 const bool theIsHighlight )
520 if( myFieldStepData.isEmpty() )
523 QListIterator<QVariant> aFieldStepDataIter( myFieldStepData );
525 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
526 switch( myFieldDimension )
528 case 0: aShapeType = TopAbs_VERTEX; break;
529 case 1: aShapeType = TopAbs_EDGE; break;
530 case 2: aShapeType = TopAbs_FACE; break;
531 case 3: aShapeType = TopAbs_SOLID; break;
532 case -1: aShapeType = TopAbs_VERTEX; break;
535 TopTools_IndexedMapOfShape aShapeMap;
536 TopExp::MapShapes( myshape, aShapeMap );
538 TColStd_IndexedMapOfInteger anIndexMap;
540 TopExp_Explorer anExp;
541 for( anExp.Init( myshape, aShapeType ); anExp.More(); anExp.Next() )
543 TopoDS_Shape aSubShape = anExp.Current();
544 if( !aSubShape.IsNull() )
546 Standard_Integer aSubShapeIndex = aShapeMap.FindIndex( aSubShape );
547 if( anIndexMap.Contains( aSubShapeIndex ) )
550 anIndexMap.Add( aSubShapeIndex );
552 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
556 if( aFieldStepDataIter.hasNext() )
558 const QVariant& aVariant = aFieldStepDataIter.next();
560 aString = aVariant.toString();
562 aQColor = aVariant.value<QColor>();
570 if( computeMassCenter( aSubShape, aCenter ) )
572 Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
574 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
575 anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
576 anAspectText3d->SetColor( myLabelColor );
577 aGroup->SetPrimitivesAspect( anAspectText3d );
579 aGroup->Text( aString.toLatin1().constData(), aVertex, 14 );
584 Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
585 if( myFieldDimension == 0 )
587 TopoDS_Vertex aVertexShape = TopoDS::Vertex( aSubShape );
588 if( !aVertexShape.IsNull() )
590 gp_Pnt aPnt = BRep_Tool::Pnt( aVertexShape );
592 Handle(Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d();
593 anAspectMarker3d->SetColor( aColor );
594 anAspectMarker3d->SetType( Aspect_TOM_POINT );
595 anAspectMarker3d->SetScale( 10.0 );
596 aGroup->SetPrimitivesAspect( anAspectMarker3d );
598 Handle(Graphic3d_ArrayOfPoints) anArray = new Graphic3d_ArrayOfPoints( 1 );
599 anArray->AddVertex( aPnt.X(), aPnt.Y(), aPnt.Z() );
601 aGroup->AddPrimitiveArray( anArray );
604 else if( myFieldDimension == 1 )
606 myDrawer->WireAspect()->SetColor( aColor );
608 myDrawer->WireAspect()->SetWidth( myOwnWidth );
610 myDrawer->WireAspect()->SetWidth( myOwnWidth + 4 );
611 StdPrs_WFDeflectionShape::Add( thePrs, aSubShape, myDrawer );
613 else if( myFieldDimension == 2 ||
614 myFieldDimension == 3 ||
615 myFieldDimension == -1 )
617 myDrawer->ShadingAspect()->SetColor( aColor );
618 StdPrs_ShadedShape::Add( thePrs, aSubShape, myDrawer );
625 void GEOM_AISShape::drawName( const Handle(Prs3d_Presentation)& thePrs )
627 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
629 gp_Ax3 anAx3 = GEOMUtils::GetPosition(myshape);
630 gp_Pnt aCenter = anAx3.Location();
632 Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
634 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
635 anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
636 anAspectText3d->SetColor( myLabelColor );
637 aGroup->SetPrimitivesAspect( anAspectText3d );
639 const char* aName = getIO()->getName();
640 aGroup->Text( TCollection_ExtendedString( aName ), aVertex, 16 );
643 Standard_Boolean GEOM_AISShape::computeMassCenter( const TopoDS_Shape& theShape,
646 Standard_Real aX = 0, aY = 0, aZ = 0;
647 Standard_Integer aNbPoints = 0;
649 TopExp_Explorer anExp;
650 for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
652 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
653 if( !aVertex.IsNull() )
655 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
664 return Standard_False;
666 theCenter.SetCoord( aX / (Standard_Real)aNbPoints,
667 aY / (Standard_Real)aNbPoints,
668 aZ / (Standard_Real)aNbPoints );
669 return Standard_True;