1 // Copyright (C) 2007-2011 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.
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
22 // GEOM OBJECT : interactive object for Geometry entities visualization
23 // File : GEOM_AISShape.cxx
24 // Author : Nicolas REJNERI
28 \class GEOM_AISShape GEOM_AISShape.hxx
32 #include "GEOM_AISShape.ixx"
33 #include "SALOME_InteractiveObject.hxx"
34 #include "GEOM_AISVector.hxx"
36 // Open CASCADE Includes
37 #include <AIS_Drawer.hxx>
38 #include <AIS_InteractiveContext.hxx>
40 #include <BRep_Tool.hxx>
42 #include <GCPnts_AbscissaPoint.hxx>
43 #include <GeomAdaptor_Curve.hxx>
47 #include <Graphic3d_AspectFillArea3d.hxx>
48 #include <Graphic3d_AspectLine3d.hxx>
50 #include <Prs3d_ShadingAspect.hxx>
51 #include <Prs3d_Arrow.hxx>
52 #include <Prs3d_IsoAspect.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_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 <SalomeApp_Tools.h>
77 #include <SUIT_Session.h>
78 #include <SUIT_ResourceMgr.h>
81 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
82 const Handle(AIS_InteractiveContext)& theIC,
83 SelectMgr_IndexedMapOfOwner& theMap )
85 if ( theObj.IsNull() || theIC.IsNull() )
88 TColStd_ListOfInteger modes;
89 theIC->ActivatedModes( theObj, modes );
91 TColStd_ListIteratorOfListOfInteger itr( modes );
92 for (; itr.More(); itr.Next() ) {
94 if ( !theObj->HasSelection( m ) )
97 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
99 for ( sel->Init(); sel->More(); sel->Next() ) {
100 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
101 if ( entity.IsNull() )
104 Handle(SelectMgr_EntityOwner) owner =
105 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
106 if ( !owner.IsNull() )
112 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
113 const TopoDS_Shape& aMainShape,
114 const SelectMgr_IndexedMapOfOwner& anAllMap,
115 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
117 TopTools_IndexedMapOfShape aMapOfShapes;
118 TopExp::MapShapes(aMainShape, aMapOfShapes);
120 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
121 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
122 if ( anOwner.IsNull() || !anOwner->HasShape() )
125 const TopoDS_Shape& aSubShape = anOwner->Shape();
126 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
127 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
130 if ( !aToHiliteMap.Contains( anOwner ) )
131 aToHiliteMap.Add( anOwner );
135 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
136 const Standard_CString aName)
137 : SALOME_AISShape(shape), myName(aName), myDisplayVectors(false)
139 myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
141 storeBoundaryColors();
143 myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
148 myTopLevel = Standard_False;
151 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
155 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
156 Handle(SALOME_InteractiveObject) IO;
157 if ( !GetOwner().IsNull() )
158 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
162 Standard_Boolean GEOM_AISShape::hasIO(){
163 return !getIO().IsNull();
166 void GEOM_AISShape::setName(const Standard_CString aName)
170 Handle(SALOME_InteractiveObject) IO = getIO();
175 Standard_CString GEOM_AISShape::getName(){
176 return myName.ToCString();
179 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
180 const Handle(Prs3d_Presentation)& aPrs,
181 const Standard_Integer aMode)
183 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
185 Handle(AIS_InteractiveContext) anIC = GetContext();
187 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
189 case 0://StdSelect_DM_Wireframe:
194 // Restore wireframe edges colors
195 restoreBoundaryColors();
199 SetColor(topLevelColor());
201 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
202 anAspect->SetColor( topLevelColor() );
203 Attributes()->SetWireAspect( anAspect );
206 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
210 case 1://StdSelect_DM_Shading:
214 shadingMode(aPresentationManager, aPrs, aMode);
216 // Store wireframe edges colors
217 storeBoundaryColors();
221 case 3: //StdSelect_DM_HLR:
224 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
226 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
231 if ( aMode == ShadingWithEdges ) {
232 // Temporary store number of iso lines in order to recover its later
233 // when display mode is achnged to 'Wirefame' or 'Shading'.
234 // Iso lines are not displayed in 'Shading with edges' mode.
237 // Reset number of iso lines to 0
241 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
243 // Store wireframe edges colors
244 storeBoundaryColors();
247 Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
248 anAspect->SetColor( myEdgesInShadingColor );
249 myDrawer->SetUnFreeBoundaryAspect( anAspect );
251 anAspect = myDrawer->FreeBoundaryAspect();
252 anAspect->SetColor( myEdgesInShadingColor );
253 myDrawer->SetFreeBoundaryAspect( anAspect );
255 // Add edges to presentation
256 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
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.);
304 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
307 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
309 if(aValue<0.0 || aValue>1.0) return;
317 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
318 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
319 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
320 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
321 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
322 myTransparency = aValue;
325 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
327 myShadingColor = aCol;
330 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
332 myEdgesInShadingColor = aCol;
335 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
336 const Standard_Boolean aHighlight )
338 Handle(AIS_InteractiveObject) anObj = this;
339 Handle(AIS_InteractiveContext) anIC = GetContext();
340 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
343 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
344 anIC->SetAutomaticHilight( false );
346 anIC->ClearSelected( false );
349 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
351 // Get entity owners for all activated selection modes
352 getEntityOwners( anObj, anIC, anAllMap );
354 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
355 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
358 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
359 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
362 anIC->SetAutomaticHilight( isAutoHilight );
363 anIC->HilightSelected( false );
366 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
368 myDisplayVectors = isDisplayed;
371 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
372 const Handle(Prs3d_Presentation)& aPrs,
373 const Standard_Integer aMode)
375 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
377 Graphic3d_MaterialAspect aMatAspect;
378 if ( !HasMaterial() ) {
379 aMatAspect.SetAmbient( 0.5 );
380 aMatAspect.SetDiffuse( 0.5 );
381 aMatAspect.SetEmissive( 0.5 );
382 aMatAspect.SetShininess(0.5 );
383 aMatAspect.SetSpecular( 0.5 );
385 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
386 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Graphic3d_NOM_JADE);
389 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
390 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
391 FMat.SetTransparency(myTransparency); BMat.SetTransparency(myTransparency);
392 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
393 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
395 //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
396 // P->SetPrimitivesAspect(a4bis);
397 // G->SetGroupPrimitivesAspect(a4bis);
398 //a4bis->SetInteriorColor(myShadingColor);
399 myDrawer->ShadingAspect()->SetColor(isTopLevel() ? topLevelColor() : myShadingColor);
401 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
402 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
403 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
406 void GEOM_AISShape::storeIsoNumbers()
408 myUIsoNumber = myDrawer->UIsoAspect()->Number();
409 myVIsoNumber = myDrawer->VIsoAspect()->Number();
412 void GEOM_AISShape::restoreIsoNumbers()
414 if ( myUIsoNumber > 0 ) {
415 // Restore number of U iso lines
416 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
417 anAspect->SetNumber( myUIsoNumber );
418 myDrawer->SetUIsoAspect( anAspect );
421 if ( myVIsoNumber > 0 ) {
422 // Restore number of V iso lines
423 Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
424 anAspect->SetNumber( myVIsoNumber );
425 myDrawer->SetVIsoAspect( anAspect );
429 void GEOM_AISShape::resetIsoNumbers()
431 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
432 anAspect->SetNumber( 0 );
433 myDrawer->SetUIsoAspect( anAspect );
435 anAspect = myDrawer->VIsoAspect();
436 anAspect->SetNumber( 0 );
437 myDrawer->SetVIsoAspect( anAspect );
440 void GEOM_AISShape::storeBoundaryColors()
442 Aspect_TypeOfLine aLT;
445 myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
446 myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
449 void GEOM_AISShape::restoreBoundaryColors()
451 Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
452 anAspect->SetColor( myFreeBoundaryColor );
453 myDrawer->SetFreeBoundaryAspect( anAspect );
455 anAspect = myDrawer->UnFreeBoundaryAspect();
456 anAspect->SetColor( myUnFreeBoundaryColor );
457 myDrawer->SetUnFreeBoundaryAspect( anAspect );
461 Standard_Boolean GEOM_AISShape::isTopLevel() {
465 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
469 Quantity_Color GEOM_AISShape::topLevelColor() {
470 SUIT_Session* session = SUIT_Session::session();
471 SUIT_ResourceMgr* resMgr = session->resourceMgr();
472 QColor c = resMgr->colorValue( "Geometry", "toplevel_color", QColor( 170, 85, 0 ) );
473 return SalomeApp_Tools::color(c);