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_Drawer.hxx>
35 #include <AIS_InteractiveContext.hxx>
36 #include <BRep_Tool.hxx>
37 #include <GCPnts_AbscissaPoint.hxx>
38 #include <GeomAdaptor_Curve.hxx>
42 #include <Graphic3d_AspectFillArea3d.hxx>
43 #include <Graphic3d_AspectLine3d.hxx>
44 #include <Graphic3d_AspectMarker3d.hxx>
45 #include <Graphic3d_AspectText3d.hxx>
47 #include <Prs3d_ShadingAspect.hxx>
48 #include <Prs3d_Arrow.hxx>
49 #include <Prs3d_IsoAspect.hxx>
50 #if OCC_VERSION_LARGE > 0x06070200
51 #include <Prs3d_VertexDrawMode.hxx>
54 #include <SelectBasics_SensitiveEntity.hxx>
55 #include <SelectMgr_EntityOwner.hxx>
56 #include <StdSelect_BRepOwner.hxx>
57 #include <SelectMgr_IndexedMapOfOwner.hxx>
58 #include <SelectMgr_Selection.hxx>
59 #include <StdSelect_DisplayMode.hxx>
60 #include <StdPrs_ShadedShape.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>
81 #include <SUIT_Session.h>
82 #include <SUIT_ResourceMgr.h>
84 IMPLEMENT_STANDARD_HANDLE (GEOM_AISShape, SALOME_AISShape)
85 IMPLEMENT_STANDARD_RTTIEXT(GEOM_AISShape, SALOME_AISShape)
87 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::myTopLevelDm = GEOM_AISShape::TopKeepCurrent;
88 Quantity_Color GEOM_AISShape::myTopLevelColor;
91 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
92 const Handle(AIS_InteractiveContext)& theIC,
93 SelectMgr_IndexedMapOfOwner& theMap )
95 if ( theObj.IsNull() || theIC.IsNull() )
98 TColStd_ListOfInteger modes;
99 theIC->ActivatedModes( theObj, modes );
101 TColStd_ListIteratorOfListOfInteger itr( modes );
102 for (; itr.More(); itr.Next() ) {
104 if ( !theObj->HasSelection( m ) )
107 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
109 for ( sel->Init(); sel->More(); sel->Next() ) {
110 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
111 if ( entity.IsNull() )
114 Handle(SelectMgr_EntityOwner) owner =
115 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
116 if ( !owner.IsNull() )
122 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
123 const TopoDS_Shape& aMainShape,
124 const SelectMgr_IndexedMapOfOwner& anAllMap,
125 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
127 TopTools_IndexedMapOfShape aMapOfShapes;
128 TopExp::MapShapes(aMainShape, aMapOfShapes);
130 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
131 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
132 if ( anOwner.IsNull() || !anOwner->HasShape() )
135 const TopoDS_Shape& aSubShape = anOwner->Shape();
136 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
137 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
140 if ( !aToHiliteMap.Contains( anOwner ) )
141 aToHiliteMap.Add( anOwner );
145 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
146 const Standard_CString aName)
147 : SALOME_AISShape(shape),
149 myDisplayVectors(false),
150 myDisplayVertices(false),
151 myFieldDataType(GEOM::FDT_Double),
153 myFieldStepRangeMin(0),
154 myFieldStepRangeMax(0)
156 //rnv: Commented to avoid bug with local selection
157 //SetHilightMode( CustomHighlight ); // override setting the mode to 0 inside AIS_Shape constructor
159 myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
160 myPrevDisplayMode = 0;
162 myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
164 myTopLevel = Standard_False;
165 Graphic3d_MaterialAspect aMatAspect;
166 if ( !HasMaterial() ) {
167 aMatAspect.SetAmbient( 0.5 );
168 aMatAspect.SetDiffuse( 0.5 );
169 aMatAspect.SetEmissive( 0.5 );
170 aMatAspect.SetShininess(0.5 );
171 aMatAspect.SetSpecular( 0.5 );
173 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
174 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
178 GEOM_AISShape::~GEOM_AISShape()
182 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
186 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
187 Handle(SALOME_InteractiveObject) IO;
188 if ( !GetOwner().IsNull() )
189 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
193 Standard_Boolean GEOM_AISShape::hasIO(){
194 return !getIO().IsNull();
197 void GEOM_AISShape::setName(const Standard_CString aName)
201 Handle(SALOME_InteractiveObject) IO = getIO();
206 Standard_CString GEOM_AISShape::getName(){
207 return myName.ToCString();
210 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
211 const Handle(Prs3d_Presentation)& aPrs,
212 const Standard_Integer aMode)
214 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
216 Handle(AIS_InteractiveContext) anIC = GetContext();
217 // AKL: use old behavior to avoid keeping object's wireframe
218 // if to change shape properties (for example: 'Clear Top Level State','Color', 'Isos')
219 // calling popup menu over(!) the shape in OCC viewer.
220 anIC->SetToHilightSelected( false );
222 bool anIsField = !myFieldStepData.isEmpty();
223 bool anIsColorField = anIsField && myFieldDataType != GEOM::FDT_String;
224 bool anIsTextField = anIsField && myFieldDataType == GEOM::FDT_String;
226 #if OCC_VERSION_LARGE > 0x06070200
227 if (isShowVertices())
228 myDrawer->SetVertexDrawMode(Prs3d_VDM_All);
231 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
232 bool isTopLev = isTopLevel() && switchTopLevel();
235 case CustomHighlight:
238 SetColor(topLevelColor());
239 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
240 anAspect->SetColor( topLevelColor() );
241 Attributes()->SetWireAspect( anAspect );
243 if( !isTopLev && anIsColorField && myFieldDimension == 1 )
244 drawField( aPrs, false, aMode == CustomHighlight );
246 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
251 shadingMode(aPresentationManager, aPrs, aMode);
254 case ShadingWithEdges:
256 shadingMode(aPresentationManager, aPrs, Shading);
257 myDrawer->SetFaceBoundaryDraw( Standard_True );
258 Handle(Prs3d_LineAspect) aBoundaryAspect =
259 new Prs3d_LineAspect ( myEdgesInShadingColor, Aspect_TOL_SOLID, myOwnWidth );
260 myDrawer->SetFaceBoundaryAspect (aBoundaryAspect);
266 #ifdef USE_TEXTURED_SHAPE
267 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
269 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
272 shadingMode(aPresentationManager, aPrs, Shading);
278 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
279 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
280 for ( ; Exp.More(); Exp.Next() ) {
281 TopoDS_Vertex aV1, aV2;
282 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
284 // draw curve direction (issue 0021087)
285 anEdgeE.Orientation( TopAbs_FORWARD );
287 if ( anEdgeE.IsNull() ) continue;
289 TopExp::Vertices(anEdgeE, aV1, aV2);
290 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
291 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
295 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
297 if ( C.IsNull() ) continue;
299 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
300 C->D1(lp, aP2, aDirVec);
302 C->D1(fp, aP1, aDirVec);
306 GeomAdaptor_Curve aAdC;
307 aAdC.Load(C, fp, lp);
308 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
310 if (aDist > gp::Resolution()) {
312 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
317 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
322 // draw color field on vertices
323 if( anIsColorField && myFieldDimension == 0 && aMode != CustomHighlight )
328 drawField( aPrs, true );
330 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
333 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
335 myShadingColor = aCol;
338 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
340 myEdgesInShadingColor = aCol;
343 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
344 const Standard_Boolean aHighlight )
346 Handle(AIS_InteractiveObject) anObj = this;
347 Handle(AIS_InteractiveContext) anIC = GetContext();
348 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
351 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
352 anIC->SetAutomaticHilight( false );
354 anIC->ClearSelected( false );
357 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
359 // Get entity owners for all activated selection modes
360 getEntityOwners( anObj, anIC, anAllMap );
362 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
363 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
366 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
367 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
370 anIC->SetAutomaticHilight( isAutoHilight );
371 anIC->HilightSelected( false );
374 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
376 myDisplayVectors = isDisplayed;
379 void GEOM_AISShape::SetDisplayVertices(bool isDisplayed)
381 myDisplayVertices = isDisplayed;
384 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
385 const Handle(Prs3d_Presentation)& aPrs,
386 const Standard_Integer aMode)
388 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
390 Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
391 aMatAspect.SetTransparency(Transparency());
392 Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
393 Graphic3d_MaterialAspect currentBackMaterial = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
394 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
395 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial );
397 if( isTopLevel() && switchTopLevel() )
398 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
400 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
401 myDrawer->ShadingAspect()->SetColor(myShadingColor);
403 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
406 bool anIsColorField = !myFieldStepData.isEmpty() && myFieldDataType != GEOM::FDT_String;
407 if( anIsColorField && ( myFieldDimension == 2 || myFieldDimension == 3 || myFieldDimension == -1 ) )
413 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
414 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
415 #ifdef USE_TEXTURED_SHAPE
416 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
418 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
423 Standard_Boolean GEOM_AISShape::isTopLevel() {
427 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
430 myPrevDisplayMode = DisplayMode();
432 switch(topLevelDisplayMode()) {
433 case TopKeepCurrent : dm = myPrevDisplayMode; break;
434 case TopWireFrame : dm = Wireframe; break;
435 case TopShadingWithEdges : dm = ShadingWithEdges; break;
436 default : dm = Shading; break;
441 SetDisplayMode(myPrevDisplayMode);
446 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
447 myPrevDisplayMode = mode;
450 Quantity_Color GEOM_AISShape::topLevelColor() {
451 return myTopLevelColor;
454 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
458 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
461 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
465 Standard_Boolean GEOM_AISShape::switchTopLevel() {
466 return myTopLevelDm != TopShowAdditionalWActor;
469 Standard_Boolean GEOM_AISShape::toActivate() {
470 return ( myTopLevel && myTopLevelDm == TopShowAdditionalWActor ) ? false : true;
473 void GEOM_AISShape::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
474 const int theFieldDimension,
475 const QList<QVariant>& theFieldStepData,
476 const TCollection_AsciiString& theFieldStepName,
477 const double theFieldStepRangeMin,
478 const double theFieldStepRangeMax )
480 myFieldDataType = theFieldDataType;
481 myFieldDimension = theFieldDimension;
482 myFieldStepData = theFieldStepData;
483 myFieldStepName = theFieldStepName;
484 myFieldStepRangeMin = theFieldStepRangeMin;
485 myFieldStepRangeMax = theFieldStepRangeMax;
488 void GEOM_AISShape::getFieldStepInfo( GEOM::field_data_type& theFieldDataType,
489 int& theFieldDimension,
490 QList<QVariant>& theFieldStepData,
491 TCollection_AsciiString& theFieldStepName,
492 double& theFieldStepRangeMin,
493 double& theFieldStepRangeMax ) const
495 theFieldDataType = myFieldDataType;
496 theFieldDimension = myFieldDimension;
497 theFieldStepData = myFieldStepData;
498 theFieldStepName = myFieldStepName;
499 theFieldStepRangeMin = myFieldStepRangeMin;
500 theFieldStepRangeMax = myFieldStepRangeMax;
503 void GEOM_AISShape::drawField( const Handle(Prs3d_Presentation)& thePrs,
504 const bool theIsString,
505 const bool theIsHighlight )
507 if( myFieldStepData.isEmpty() )
510 QListIterator<QVariant> aFieldStepDataIter( myFieldStepData );
512 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
513 switch( myFieldDimension )
515 case 0: aShapeType = TopAbs_VERTEX; break;
516 case 1: aShapeType = TopAbs_EDGE; break;
517 case 2: aShapeType = TopAbs_FACE; break;
518 case 3: aShapeType = TopAbs_SOLID; break;
519 case -1: aShapeType = TopAbs_VERTEX; break;
522 TopTools_IndexedMapOfShape aShapeMap;
523 TopExp::MapShapes( myshape, aShapeMap );
525 TColStd_IndexedMapOfInteger anIndexMap;
527 TopExp_Explorer anExp;
528 for( anExp.Init( myshape, aShapeType ); anExp.More(); anExp.Next() )
530 TopoDS_Shape aSubShape = anExp.Current();
531 if( !aSubShape.IsNull() )
533 Standard_Integer aSubShapeIndex = aShapeMap.FindIndex( aSubShape );
534 if( anIndexMap.Contains( aSubShapeIndex ) )
537 anIndexMap.Add( aSubShapeIndex );
539 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
543 if( aFieldStepDataIter.hasNext() )
545 const QVariant& aVariant = aFieldStepDataIter.next();
547 aString = aVariant.toString();
549 aQColor = aVariant.value<QColor>();
557 if( computeMassCenter( aSubShape, aCenter ) )
559 Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
561 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
562 anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
563 aGroup->SetPrimitivesAspect( anAspectText3d );
565 aGroup->Text( aString.toLatin1().constData(), aVertex, 14 );
570 Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
571 if( myFieldDimension == 0 )
573 TopoDS_Vertex aVertexShape = TopoDS::Vertex( aSubShape );
574 if( !aVertexShape.IsNull() )
576 gp_Pnt aPnt = BRep_Tool::Pnt( aVertexShape );
578 Handle(Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d();
579 anAspectMarker3d->SetColor( aColor );
580 anAspectMarker3d->SetType( Aspect_TOM_POINT );
581 anAspectMarker3d->SetScale( 10.0 );
582 aGroup->SetPrimitivesAspect( anAspectMarker3d );
584 Handle(Graphic3d_ArrayOfPoints) anArray = new Graphic3d_ArrayOfPoints( 1 );
585 anArray->AddVertex( aPnt.X(), aPnt.Y(), aPnt.Z() );
587 aGroup->AddPrimitiveArray( anArray );
590 else if( myFieldDimension == 1 )
592 myDrawer->WireAspect()->SetColor( aColor );
594 myDrawer->WireAspect()->SetWidth( myOwnWidth );
596 myDrawer->WireAspect()->SetWidth( myOwnWidth + 4 );
597 StdPrs_WFDeflectionShape::Add( thePrs, aSubShape, myDrawer );
599 else if( myFieldDimension == 2 ||
600 myFieldDimension == 3 ||
601 myFieldDimension == -1 )
603 myDrawer->ShadingAspect()->SetColor( aColor );
604 StdPrs_ShadedShape::Add( thePrs, aSubShape, myDrawer );
611 Standard_Boolean GEOM_AISShape::computeMassCenter( const TopoDS_Shape& theShape,
614 Standard_Real aX = 0, aY = 0, aZ = 0;
615 Standard_Integer aNbPoints = 0;
617 TopExp_Explorer anExp;
618 for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
620 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
621 if( !aVertex.IsNull() )
623 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
632 return Standard_False;
634 theCenter.SetCoord( aX / (Standard_Real)aNbPoints,
635 aY / (Standard_Real)aNbPoints,
636 aZ / (Standard_Real)aNbPoints );
637 return Standard_True;