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 // Open CASCADE Includes
38 #include <AIS_Drawer.hxx>
39 #include <AIS_InteractiveContext.hxx>
41 #include <BRep_Tool.hxx>
43 #include <GCPnts_AbscissaPoint.hxx>
44 #include <GeomAdaptor_Curve.hxx>
48 #include <Graphic3d_AspectFillArea3d.hxx>
49 #include <Graphic3d_AspectLine3d.hxx>
50 #include <Graphic3d_AspectMarker3d.hxx>
51 #include <Graphic3d_AspectText3d.hxx>
53 #include <Prs3d_ShadingAspect.hxx>
54 #include <Prs3d_Arrow.hxx>
55 #include <Prs3d_IsoAspect.hxx>
57 #include <SelectBasics_SensitiveEntity.hxx>
58 #include <SelectMgr_EntityOwner.hxx>
59 #include <StdSelect_BRepOwner.hxx>
60 #include <SelectMgr_IndexedMapOfOwner.hxx>
61 #include <SelectMgr_Selection.hxx>
62 #include <StdSelect_DisplayMode.hxx>
63 #include <StdPrs_ShadedShape.hxx>
64 #include <StdPrs_WFDeflectionShape.hxx>
66 #include <TColStd_IndexedMapOfInteger.hxx>
67 #include <TColStd_ListIteratorOfListOfInteger.hxx>
68 #include <TColStd_ListOfInteger.hxx>
70 #include <TopoDS_Shape.hxx>
71 #include <TopTools_IndexedMapOfShape.hxx>
73 #include <TopExp_Explorer.hxx>
75 #include <TopoDS_Edge.hxx>
76 #include <TopoDS_Shape.hxx>
77 #include <TopoDS_Vertex.hxx>
79 #include <TColStd_SequenceOfInteger.hxx>
81 #include <V3d_View.hxx>
83 #include <SalomeApp_Tools.h>
85 #include <SUIT_Session.h>
86 #include <SUIT_ResourceMgr.h>
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 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 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
182 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
183 Handle(SALOME_InteractiveObject) IO;
184 if ( !GetOwner().IsNull() )
185 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
189 Standard_Boolean GEOM_AISShape::hasIO(){
190 return !getIO().IsNull();
193 void GEOM_AISShape::setName(const Standard_CString aName)
197 Handle(SALOME_InteractiveObject) IO = getIO();
202 Standard_CString GEOM_AISShape::getName(){
203 return myName.ToCString();
206 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
207 const Handle(Prs3d_Presentation)& aPrs,
208 const Standard_Integer aMode)
210 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
212 Handle(AIS_InteractiveContext) anIC = GetContext();
214 bool anIsField = !myFieldStepData.isEmpty();
215 bool anIsColorField = anIsField && myFieldDataType != GEOM::FDT_String;
216 bool anIsTextField = anIsField && myFieldDataType == GEOM::FDT_String;
218 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
219 bool isTopLev = isTopLevel() && switchTopLevel();
221 case 0://StdSelect_DM_Wireframe:
222 case CustomHighlight:
225 SetColor(topLevelColor());
226 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
227 anAspect->SetColor( topLevelColor() );
228 Attributes()->SetWireAspect( anAspect );
230 if( !isTopLev && anIsColorField && myFieldDimension == 1 )
231 drawField( aPrs, false, aMode == CustomHighlight );
233 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
236 case 1://StdSelect_DM_Shading:
238 shadingMode(aPresentationManager, aPrs, aMode);
241 case 2: { //ShadingWithEdges
243 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
244 myDrawer->SetFaceBoundaryDraw( Standard_True );
245 Handle(Prs3d_LineAspect) aBoundaryAspect =
246 new Prs3d_LineAspect ( myEdgesInShadingColor, Aspect_TOL_SOLID, myOwnWidth );
247 myDrawer->SetFaceBoundaryAspect (aBoundaryAspect);
250 case 3: //StdSelect_DM_HLR:
253 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
255 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
261 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
262 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
263 for ( ; Exp.More(); Exp.Next() ) {
264 TopoDS_Vertex aV1, aV2;
265 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
267 // draw curve direction (issue 0021087)
268 anEdgeE.Orientation( TopAbs_FORWARD );
270 if ( anEdgeE.IsNull() ) continue;
272 TopExp::Vertices(anEdgeE, aV1, aV2);
273 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
274 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
278 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
280 if ( C.IsNull() ) continue;
282 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
283 C->D1(lp, aP2, aDirVec);
285 C->D1(fp, aP1, aDirVec);
289 GeomAdaptor_Curve aAdC;
290 aAdC.Load(C, fp, lp);
291 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
293 if (aDist > gp::Resolution()) {
295 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
300 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
305 // draw color field on vertices
306 if( anIsColorField && myFieldDimension == 0 && aMode != CustomHighlight )
311 drawField( aPrs, true );
313 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
316 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
318 myShadingColor = aCol;
321 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
323 myEdgesInShadingColor = aCol;
326 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
327 const Standard_Boolean aHighlight )
329 Handle(AIS_InteractiveObject) anObj = this;
330 Handle(AIS_InteractiveContext) anIC = GetContext();
331 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
334 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
335 anIC->SetAutomaticHilight( false );
337 anIC->ClearSelected( false );
340 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
342 // Get entity owners for all activated selection modes
343 getEntityOwners( anObj, anIC, anAllMap );
345 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
346 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
349 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
350 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
353 anIC->SetAutomaticHilight( isAutoHilight );
354 anIC->HilightSelected( false );
357 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
359 myDisplayVectors = isDisplayed;
362 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
363 const Handle(Prs3d_Presentation)& aPrs,
364 const Standard_Integer aMode)
366 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
368 Graphic3d_MaterialAspect aMatAspect(Graphic3d_NOM_PLASTIC);
369 aMatAspect.SetTransparency(Transparency());
370 Graphic3d_MaterialAspect currentFrontMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
371 Graphic3d_MaterialAspect currentBackMaterial = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
372 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : currentFrontMaterial );
373 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial ( isTopLevel() ? aMatAspect : currentBackMaterial );
375 if( isTopLevel() && switchTopLevel() )
376 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
378 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
379 myDrawer->ShadingAspect()->SetColor(myShadingColor);
381 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
384 bool anIsColorField = !myFieldStepData.isEmpty() && myFieldDataType != GEOM::FDT_String;
385 if( anIsColorField && ( myFieldDimension == 2 || myFieldDimension == 3 || myFieldDimension == -1 ) )
391 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
392 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
393 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
397 Standard_Boolean GEOM_AISShape::isTopLevel() {
401 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
404 myPrevDisplayMode = DisplayMode();
406 switch(topLevelDisplayMode()) {
407 case TopKeepCurrent : dm = myPrevDisplayMode; break;
408 case TopWireFrame : dm = AIS_WireFrame; break;
409 case TopShadingWithEdges : dm = ShadingWithEdges; break;
410 default : dm = AIS_Shaded; break;
415 SetDisplayMode(myPrevDisplayMode);
420 void GEOM_AISShape::setPrevDisplayMode(const Standard_Integer mode) {
421 myPrevDisplayMode = mode;
424 Quantity_Color GEOM_AISShape::topLevelColor() {
425 return myTopLevelColor;
428 void GEOM_AISShape::setTopLevelColor(const Quantity_Color c) {
432 GEOM_AISShape::TopLevelDispMode GEOM_AISShape::topLevelDisplayMode() {
435 void GEOM_AISShape::setTopLevelDisplayMode(const GEOM_AISShape::TopLevelDispMode dm) {
439 Standard_Boolean GEOM_AISShape::switchTopLevel() {
440 return myTopLevelDm != TopShowAdditionalWActor;
443 Standard_Boolean GEOM_AISShape::toActivate() {
444 return Standard_True;
447 void GEOM_AISShape::setFieldStepInfo( const GEOM::field_data_type theFieldDataType,
448 const int theFieldDimension,
449 const QList<QVariant>& theFieldStepData,
450 const TCollection_AsciiString& theFieldStepName,
451 const double theFieldStepRangeMin,
452 const double theFieldStepRangeMax )
454 myFieldDataType = theFieldDataType;
455 myFieldDimension = theFieldDimension;
456 myFieldStepData = theFieldStepData;
457 myFieldStepName = theFieldStepName;
458 myFieldStepRangeMin = theFieldStepRangeMin;
459 myFieldStepRangeMax = theFieldStepRangeMax;
462 void GEOM_AISShape::getFieldStepInfo( GEOM::field_data_type& theFieldDataType,
463 int& theFieldDimension,
464 QList<QVariant>& theFieldStepData,
465 TCollection_AsciiString& theFieldStepName,
466 double& theFieldStepRangeMin,
467 double& theFieldStepRangeMax ) const
469 theFieldDataType = myFieldDataType;
470 theFieldDimension = myFieldDimension;
471 theFieldStepData = myFieldStepData;
472 theFieldStepName = myFieldStepName;
473 theFieldStepRangeMin = myFieldStepRangeMin;
474 theFieldStepRangeMax = myFieldStepRangeMax;
477 void GEOM_AISShape::drawField( const Handle(Prs3d_Presentation)& thePrs,
478 const bool theIsString,
479 const bool theIsHighlight )
481 if( myFieldStepData.isEmpty() )
484 QListIterator<QVariant> aFieldStepDataIter( myFieldStepData );
486 TopAbs_ShapeEnum aShapeType = TopAbs_SHAPE;
487 switch( myFieldDimension )
489 case 0: aShapeType = TopAbs_VERTEX; break;
490 case 1: aShapeType = TopAbs_EDGE; break;
491 case 2: aShapeType = TopAbs_FACE; break;
492 case 3: aShapeType = TopAbs_SOLID; break;
493 case -1: aShapeType = TopAbs_VERTEX; break;
496 TopTools_IndexedMapOfShape aShapeMap;
497 TopExp::MapShapes( myshape, aShapeMap );
499 TColStd_IndexedMapOfInteger anIndexMap;
501 TopExp_Explorer anExp;
502 for( anExp.Init( myshape, aShapeType ); anExp.More(); anExp.Next() )
504 TopoDS_Shape aSubShape = anExp.Current();
505 if( !aSubShape.IsNull() )
507 Standard_Integer aSubShapeIndex = aShapeMap.FindIndex( aSubShape );
508 if( anIndexMap.Contains( aSubShapeIndex ) )
511 anIndexMap.Add( aSubShapeIndex );
513 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup( thePrs );
517 if( aFieldStepDataIter.hasNext() )
519 const QVariant& aVariant = aFieldStepDataIter.next();
521 aString = aVariant.toString();
523 aQColor = aVariant.value<QColor>();
531 if( computeMassCenter( aSubShape, aCenter ) )
533 Graphic3d_Vertex aVertex( aCenter.X(), aCenter.Y(), aCenter.Z() );
535 Handle(Graphic3d_AspectText3d) anAspectText3d = new Graphic3d_AspectText3d();
536 anAspectText3d->SetStyle( Aspect_TOST_ANNOTATION );
537 aGroup->SetPrimitivesAspect( anAspectText3d );
539 aGroup->Text( aString.toLatin1().constData(), aVertex, 14 );
544 Quantity_Color aColor( aQColor.redF(), aQColor.greenF(), aQColor.blueF(), Quantity_TOC_RGB );
545 if( myFieldDimension == 0 )
547 TopoDS_Vertex aVertexShape = TopoDS::Vertex( aSubShape );
548 if( !aVertexShape.IsNull() )
550 gp_Pnt aPnt = BRep_Tool::Pnt( aVertexShape );
552 Handle(Graphic3d_AspectMarker3d) anAspectMarker3d = new Graphic3d_AspectMarker3d();
553 anAspectMarker3d->SetColor( aColor );
554 anAspectMarker3d->SetType( Aspect_TOM_POINT );
555 anAspectMarker3d->SetScale( 10.0 );
556 aGroup->SetPrimitivesAspect( anAspectMarker3d );
558 Handle(Graphic3d_ArrayOfPoints) anArray = new Graphic3d_ArrayOfPoints( 1 );
559 anArray->AddVertex( aPnt.X(), aPnt.Y(), aPnt.Z() );
561 aGroup->AddPrimitiveArray( anArray );
564 else if( myFieldDimension == 1 )
566 myDrawer->WireAspect()->SetColor( aColor );
568 myDrawer->WireAspect()->SetWidth( myOwnWidth );
570 myDrawer->WireAspect()->SetWidth( myOwnWidth + 4 );
571 StdPrs_WFDeflectionShape::Add( thePrs, aSubShape, myDrawer );
573 else if( myFieldDimension == 2 ||
574 myFieldDimension == 3 ||
575 myFieldDimension == -1 )
577 myDrawer->ShadingAspect()->SetColor( aColor );
578 StdPrs_ShadedShape::Add( thePrs, aSubShape, myDrawer );
585 Standard_Boolean GEOM_AISShape::computeMassCenter( const TopoDS_Shape& theShape,
588 Standard_Real aX = 0, aY = 0, aZ = 0;
589 Standard_Integer aNbPoints = 0;
591 TopExp_Explorer anExp;
592 for( anExp.Init( theShape, TopAbs_VERTEX ); anExp.More(); anExp.Next() )
594 TopoDS_Vertex aVertex = TopoDS::Vertex( anExp.Current() );
595 if( !aVertex.IsNull() )
597 gp_Pnt aPnt = BRep_Tool::Pnt( aVertex );
606 return Standard_False;
608 theCenter.SetCoord( aX / (Standard_Real)aNbPoints,
609 aY / (Standard_Real)aNbPoints,
610 aZ / (Standard_Real)aNbPoints );
611 return Standard_True;