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 <Basics_OCCTVersion.hxx>
33 // Open CASCADE Includes
34 #include <AIS_InteractiveContext.hxx>
35 #include <BRep_Tool.hxx>
36 #include <GCPnts_AbscissaPoint.hxx>
37 #include <GeomAdaptor_Curve.hxx>
41 #include <Graphic3d_AspectFillArea3d.hxx>
42 #include <Graphic3d_AspectLine3d.hxx>
43 #include <Graphic3d_AspectMarker3d.hxx>
44 #include <Graphic3d_AspectText3d.hxx>
46 #include <Prs3d_ShadingAspect.hxx>
47 #include <Prs3d_Arrow.hxx>
48 #include <Prs3d_IsoAspect.hxx>
49 #if OCC_VERSION_LARGE > 0x06070200
50 #include <Prs3d_VertexDrawMode.hxx>
53 #include <SelectBasics_SensitiveEntity.hxx>
54 #include <SelectMgr_EntityOwner.hxx>
55 #include <StdSelect_BRepOwner.hxx>
56 #include <SelectMgr_IndexedMapOfOwner.hxx>
57 #include <SelectMgr_Selection.hxx>
58 #include <StdSelect_DisplayMode.hxx>
59 #include <StdPrs_ShadedShape.hxx>
60 #include <StdPrs_WFDeflectionShape.hxx>
62 #include <TColStd_IndexedMapOfInteger.hxx>
63 #include <TColStd_ListIteratorOfListOfInteger.hxx>
64 #include <TColStd_ListOfInteger.hxx>
66 #include <TopoDS_Shape.hxx>
67 #include <TopTools_IndexedMapOfShape.hxx>
69 #include <TopExp_Explorer.hxx>
71 #include <TopoDS_Edge.hxx>
72 #include <TopoDS_Shape.hxx>
73 #include <TopoDS_Vertex.hxx>
75 #include <TColStd_SequenceOfInteger.hxx>
77 #include <V3d_View.hxx>
79 #include <SalomeApp_Tools.h>
80 #include <SUIT_Session.h>
81 #include <SUIT_ResourceMgr.h>
83 IMPLEMENT_STANDARD_HANDLE (GEOM_AISShape, SALOME_AISShape)
84 IMPLEMENT_STANDARD_RTTIEXT(GEOM_AISShape, SALOME_AISShape)
86 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::myTopLevelDm = GEOM_AISShape::TopKeepCurrent;
87 Quantity_Color GEOM_AISShape::myTopLevelColor;
90 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
91 const Handle(AIS_InteractiveContext)& theIC,
92 SelectMgr_IndexedMapOfOwner& theMap )
94 if ( theObj.IsNull() || theIC.IsNull() )
97 TColStd_ListOfInteger modes;
98 theIC->ActivatedModes( theObj, modes );
100 TColStd_ListIteratorOfListOfInteger itr( modes );
101 for (; itr.More(); itr.Next() ) {
103 if ( !theObj->HasSelection( m ) )
106 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
108 for ( sel->Init(); sel->More(); sel->Next() ) {
109 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
110 if ( entity.IsNull() )
113 Handle(SelectMgr_EntityOwner) owner =
114 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
115 if ( !owner.IsNull() )
121 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
122 const TopoDS_Shape& aMainShape,
123 const SelectMgr_IndexedMapOfOwner& anAllMap,
124 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
126 TopTools_IndexedMapOfShape aMapOfShapes;
127 TopExp::MapShapes(aMainShape, aMapOfShapes);
129 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
130 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
131 if ( anOwner.IsNull() || !anOwner->HasShape() )
134 const TopoDS_Shape& aSubShape = anOwner->Shape();
135 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
136 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
139 if ( !aToHiliteMap.Contains( anOwner ) )
140 aToHiliteMap.Add( anOwner );
144 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
145 const Standard_CString aName)
146 : SALOME_AISShape(shape),
148 myDisplayVectors(false),
149 myDisplayVertices(false),
150 myFieldDataType(GEOM::FDT_Double),
152 myFieldStepRangeMin(0),
153 myFieldStepRangeMax(0)
155 //rnv: Commented to avoid bug with local selection
156 //SetHilightMode( CustomHighlight ); // override setting the mode to 0 inside AIS_Shape constructor
158 myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
159 myPrevDisplayMode = 0;
161 myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
163 myTopLevel = Standard_False;
164 Graphic3d_MaterialAspect aMatAspect;
165 if ( !HasMaterial() ) {
166 aMatAspect.SetAmbient( 0.5 );
167 aMatAspect.SetDiffuse( 0.5 );
168 aMatAspect.SetEmissive( 0.5 );
169 aMatAspect.SetShininess(0.5 );
170 aMatAspect.SetSpecular( 0.5 );
172 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
173 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
177 GEOM_AISShape::~GEOM_AISShape()
181 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
185 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
186 Handle(SALOME_InteractiveObject) IO;
187 if ( !GetOwner().IsNull() )
188 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
192 Standard_Boolean GEOM_AISShape::hasIO(){
193 return !getIO().IsNull();
196 void GEOM_AISShape::setName(const Standard_CString aName)
200 Handle(SALOME_InteractiveObject) IO = getIO();
205 Standard_CString GEOM_AISShape::getName(){
206 return myName.ToCString();
209 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
210 const Handle(Prs3d_Presentation)& aPrs,
211 const Standard_Integer aMode)
213 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
215 Handle(AIS_InteractiveContext) anIC = GetContext();
216 // AKL: use old behavior to avoid keeping object's wireframe
217 // if to change shape properties (for example: 'Clear Top Level State','Color', 'Isos')
218 // calling popup menu over(!) the shape in OCC viewer.
219 anIC->SetToHilightSelected( false );
221 bool anIsField = !myFieldStepData.isEmpty();
222 bool anIsColorField = anIsField && myFieldDataType != GEOM::FDT_String;
223 bool anIsTextField = anIsField && myFieldDataType == GEOM::FDT_String;
225 #if OCC_VERSION_LARGE > 0x06070200
226 if (isShowVertices())
227 myDrawer->SetVertexDrawMode(Prs3d_VDM_All);
230 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
231 bool isTopLev = isTopLevel() && switchTopLevel();
234 case CustomHighlight:
237 SetColor(topLevelColor());
238 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
239 anAspect->SetColor( topLevelColor() );
240 Attributes()->SetWireAspect( anAspect );
242 if( !isTopLev && anIsColorField && myFieldDimension == 1 )
243 drawField( aPrs, false, aMode == CustomHighlight );
245 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
250 shadingMode(aPresentationManager, aPrs, aMode);
253 case ShadingWithEdges:
255 shadingMode(aPresentationManager, aPrs, Shading);
256 myDrawer->SetFaceBoundaryDraw( Standard_True );
257 Handle(Prs3d_LineAspect) aBoundaryAspect =
258 new Prs3d_LineAspect ( myEdgesInShadingColor, Aspect_TOL_SOLID, myOwnWidth );
259 myDrawer->SetFaceBoundaryAspect (aBoundaryAspect);
265 #ifdef USE_TEXTURED_SHAPE
266 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
268 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
271 shadingMode(aPresentationManager, aPrs, Shading);
277 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
278 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
279 for ( ; Exp.More(); Exp.Next() ) {
280 TopoDS_Vertex aV1, aV2;
281 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
283 // draw curve direction (issue 0021087)
284 anEdgeE.Orientation( TopAbs_FORWARD );
286 if ( anEdgeE.IsNull() ) continue;
288 TopExp::Vertices(anEdgeE, aV1, aV2);
289 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
290 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
294 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
296 if ( C.IsNull() ) continue;
298 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
299 C->D1(lp, aP2, aDirVec);
301 C->D1(fp, aP1, aDirVec);
305 GeomAdaptor_Curve aAdC;
306 aAdC.Load(C, fp, lp);
307 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
309 if (aDist > gp::Resolution()) {
311 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
316 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
321 // draw color field on vertices
322 if( anIsColorField && myFieldDimension == 0 && aMode != CustomHighlight )
327 drawField( aPrs, true );
329 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
332 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
334 myShadingColor = aCol;
337 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
339 myEdgesInShadingColor = aCol;
342 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
343 const Standard_Boolean aHighlight )
345 Handle(AIS_InteractiveObject) anObj = this;
346 Handle(AIS_InteractiveContext) anIC = GetContext();
347 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
350 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
351 anIC->SetAutomaticHilight( false );
353 anIC->ClearSelected( false );
356 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
358 // Get entity owners for all activated selection modes
359 getEntityOwners( anObj, anIC, anAllMap );
361 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
362 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
365 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
366 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
369 anIC->SetAutomaticHilight( isAutoHilight );
370 anIC->HilightSelected( false );
373 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
375 myDisplayVectors = isDisplayed;
378 void GEOM_AISShape::SetDisplayVertices(bool isDisplayed)
380 myDisplayVertices = 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(Transparency());
391 Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
392 Graphic3d_MaterialAspect currentBackMaterial = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
393 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
394 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial );
396 if( isTopLevel() && switchTopLevel() )
397 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
399 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
400 myDrawer->ShadingAspect()->SetColor(myShadingColor);
402 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
405 bool anIsColorField = !myFieldStepData.isEmpty() && myFieldDataType != GEOM::FDT_String;
406 if( anIsColorField && ( myFieldDimension == 2 || myFieldDimension == 3 || myFieldDimension == -1 ) )
412 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
413 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
414 #ifdef USE_TEXTURED_SHAPE
415 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
417 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
422 Standard_Boolean GEOM_AISShape::isTopLevel() {
426 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
429 myPrevDisplayMode = DisplayMode();
431 switch(topLevelDisplayMode()) {
432 case TopKeepCurrent : dm = myPrevDisplayMode; break;
433 case TopWireFrame : dm = Wireframe; break;
434 case TopShadingWithEdges : dm = ShadingWithEdges; break;
435 default : dm = Shading; break;
440 SetDisplayMode(myPrevDisplayMode);
445 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
446 myPrevDisplayMode = mode;
449 Quantity_Color GEOM_AISShape::topLevelColor() {
450 return myTopLevelColor;
453 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
457 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
460 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
464 Standard_Boolean GEOM_AISShape::switchTopLevel() {
465 return myTopLevelDm != TopShowAdditionalWActor;
468 Standard_Boolean GEOM_AISShape::toActivate() {
469 return ( myTopLevel && myTopLevelDm == TopShowAdditionalWActor ) ? false : true;
472 void GEOM_AISShape::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
473 const int theFieldDimension,
474 const QList<QVariant>& theFieldStepData,
475 const TCollection_AsciiString& theFieldStepName,
476 const double theFieldStepRangeMin,
477 const double theFieldStepRangeMax )
479 myFieldDataType = theFieldDataType;
480 myFieldDimension = theFieldDimension;
481 myFieldStepData = theFieldStepData;
482 myFieldStepName = theFieldStepName;
483 myFieldStepRangeMin = theFieldStepRangeMin;
484 myFieldStepRangeMax = theFieldStepRangeMax;
487 void GEOM_AISShape::getFieldStepInfo( GEOM::field_data_type& theFieldDataType,
488 int& theFieldDimension,
489 QList<QVariant>& theFieldStepData,
490 TCollection_AsciiString& theFieldStepName,
491 double& theFieldStepRangeMin,
492 double& theFieldStepRangeMax ) const
494 theFieldDataType = myFieldDataType;
495 theFieldDimension = myFieldDimension;
496 theFieldStepData = myFieldStepData;
497 theFieldStepName = myFieldStepName;
498 theFieldStepRangeMin = myFieldStepRangeMin;
499 theFieldStepRangeMax = myFieldStepRangeMax;
502 void GEOM_AISShape::drawField( const Handle(Prs3d_Presentation)& thePrs,
503 const bool theIsString,
504 const bool theIsHighlight )
506 if( myFieldStepData.isEmpty() )
509 QListIterator<QVariant> aFieldStepDataIter( myFieldStepData );
511 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
512 switch( myFieldDimension )
514 case 0: aShapeType = TopAbs_VERTEX; break;
515 case 1: aShapeType = TopAbs_EDGE; break;
516 case 2: aShapeType = TopAbs_FACE; break;
517 case 3: aShapeType = TopAbs_SOLID; break;
518 case -1: aShapeType = TopAbs_VERTEX; break;
521 TopTools_IndexedMapOfShape aShapeMap;
522 TopExp::MapShapes( myshape, aShapeMap );
524 TColStd_IndexedMapOfInteger anIndexMap;
526 TopExp_Explorer anExp;
527 for( anExp.Init( myshape, aShapeType ); anExp.More(); anExp.Next() )
529 TopoDS_Shape aSubShape = anExp.Current();
530 if( !aSubShape.IsNull() )
532 Standard_Integer aSubShapeIndex = aShapeMap.FindIndex( aSubShape );
533 if( anIndexMap.Contains( aSubShapeIndex ) )
536 anIndexMap.Add( aSubShapeIndex );
538 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
542 if( aFieldStepDataIter.hasNext() )
544 const QVariant& aVariant = aFieldStepDataIter.next();
546 aString = aVariant.toString();
548 aQColor = aVariant.value<QColor>();
556 if( computeMassCenter( aSubShape, aCenter ) )
558 Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
560 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
561 anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
562 aGroup->SetPrimitivesAspect( anAspectText3d );
564 aGroup->Text( aString.toLatin1().constData(), aVertex, 14 );
569 Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
570 if( myFieldDimension == 0 )
572 TopoDS_Vertex aVertexShape = TopoDS::Vertex( aSubShape );
573 if( !aVertexShape.IsNull() )
575 gp_Pnt aPnt = BRep_Tool::Pnt( aVertexShape );
577 Handle(Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d();
578 anAspectMarker3d->SetColor( aColor );
579 anAspectMarker3d->SetType( Aspect_TOM_POINT );
580 anAspectMarker3d->SetScale( 10.0 );
581 aGroup->SetPrimitivesAspect( anAspectMarker3d );
583 Handle(Graphic3d_ArrayOfPoints) anArray = new Graphic3d_ArrayOfPoints( 1 );
584 anArray->AddVertex( aPnt.X(), aPnt.Y(), aPnt.Z() );
586 aGroup->AddPrimitiveArray( anArray );
589 else if( myFieldDimension == 1 )
591 myDrawer->WireAspect()->SetColor( aColor );
593 myDrawer->WireAspect()->SetWidth( myOwnWidth );
595 myDrawer->WireAspect()->SetWidth( myOwnWidth + 4 );
596 StdPrs_WFDeflectionShape::Add( thePrs, aSubShape, myDrawer );
598 else if( myFieldDimension == 2 ||
599 myFieldDimension == 3 ||
600 myFieldDimension == -1 )
602 myDrawer->ShadingAspect()->SetColor( aColor );
603 StdPrs_ShadedShape::Add( thePrs, aSubShape, myDrawer );
610 Standard_Boolean GEOM_AISShape::computeMassCenter( const TopoDS_Shape& theShape,
613 Standard_Real aX = 0, aY = 0, aZ = 0;
614 Standard_Integer aNbPoints = 0;
616 TopExp_Explorer anExp;
617 for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
619 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
620 if( !aVertex.IsNull() )
622 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
631 return Standard_False;
633 theCenter.SetCoord( aX / (Standard_Real)aNbPoints,
634 aY / (Standard_Real)aNbPoints,
635 aZ / (Standard_Real)aNbPoints );
636 return Standard_True;