1 // Copyright (C) 2007-2012 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
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>
51 #include <Prs3d_ShadingAspect.hxx>
52 #include <Prs3d_Arrow.hxx>
53 #include <Prs3d_IsoAspect.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_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 <SalomeApp_Tools.h>
78 #include <SUIT_Session.h>
79 #include <SUIT_ResourceMgr.h>
82 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
83 const Handle(AIS_InteractiveContext)& theIC,
84 SelectMgr_IndexedMapOfOwner& theMap )
86 if ( theObj.IsNull() || theIC.IsNull() )
89 TColStd_ListOfInteger modes;
90 theIC->ActivatedModes( theObj, modes );
92 TColStd_ListIteratorOfListOfInteger itr( modes );
93 for (; itr.More(); itr.Next() ) {
95 if ( !theObj->HasSelection( m ) )
98 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
100 for ( sel->Init(); sel->More(); sel->Next() ) {
101 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
102 if ( entity.IsNull() )
105 Handle(SelectMgr_EntityOwner) owner =
106 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
107 if ( !owner.IsNull() )
113 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
114 const TopoDS_Shape& aMainShape,
115 const SelectMgr_IndexedMapOfOwner& anAllMap,
116 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
118 TopTools_IndexedMapOfShape aMapOfShapes;
119 TopExp::MapShapes(aMainShape, aMapOfShapes);
121 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
122 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
123 if ( anOwner.IsNull() || !anOwner->HasShape() )
126 const TopoDS_Shape& aSubShape = anOwner->Shape();
127 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
128 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
131 if ( !aToHiliteMap.Contains( anOwner ) )
132 aToHiliteMap.Add( anOwner );
136 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
137 const Standard_CString aName)
138 : SALOME_AISShape(shape), myName(aName), myDisplayVectors(false)
140 myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
142 storeBoundaryColors();
144 myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
149 myTopLevel = Standard_False;
150 Graphic3d_MaterialAspect aMatAspect;
151 if ( !HasMaterial() ) {
152 aMatAspect.SetAmbient( 0.5 );
153 aMatAspect.SetDiffuse( 0.5 );
154 aMatAspect.SetEmissive( 0.5 );
155 aMatAspect.SetShininess(0.5 );
156 aMatAspect.SetSpecular( 0.5 );
158 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
159 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(aMatAspect);
161 myCurrentMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
164 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
168 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
169 Handle(SALOME_InteractiveObject) IO;
170 if ( !GetOwner().IsNull() )
171 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
175 Standard_Boolean GEOM_AISShape::hasIO(){
176 return !getIO().IsNull();
179 void GEOM_AISShape::setName(const Standard_CString aName)
183 Handle(SALOME_InteractiveObject) IO = getIO();
188 Standard_CString GEOM_AISShape::getName(){
189 return myName.ToCString();
192 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
193 const Handle(Prs3d_Presentation)& aPrs,
194 const Standard_Integer aMode)
196 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
198 Handle(AIS_InteractiveContext) anIC = GetContext();
200 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
202 case 0://StdSelect_DM_Wireframe:
207 // Restore wireframe edges colors
208 restoreBoundaryColors();
212 SetColor(topLevelColor());
213 Handle(Prs3d_LineAspect) anAspect = Attributes()->WireAspect();
214 anAspect->SetColor( topLevelColor() );
215 Attributes()->SetWireAspect( anAspect );
218 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
222 case 1://StdSelect_DM_Shading:
226 shadingMode(aPresentationManager, aPrs, aMode);
228 // Store wireframe edges colors
229 storeBoundaryColors();
233 case 3: //StdSelect_DM_HLR:
236 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
238 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
243 if ( aMode == ShadingWithEdges ) {
244 // Temporary store number of iso lines in order to recover its later
245 // when display mode is achnged to 'Wirefame' or 'Shading'.
246 // Iso lines are not displayed in 'Shading with edges' mode.
249 // Reset number of iso lines to 0
253 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
255 // Store wireframe edges colors
256 storeBoundaryColors();
259 Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
260 anAspect->SetColor( myEdgesInShadingColor );
261 myDrawer->SetUnFreeBoundaryAspect( anAspect );
263 anAspect = myDrawer->FreeBoundaryAspect();
264 anAspect->SetColor( myEdgesInShadingColor );
265 myDrawer->SetFreeBoundaryAspect( anAspect );
267 // Add edges to presentation
268 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
273 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
274 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
275 for ( ; Exp.More(); Exp.Next() ) {
276 TopoDS_Vertex aV1, aV2;
277 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
279 // draw curve direction (issue 0021087)
280 anEdgeE.Orientation( TopAbs_FORWARD );
282 if ( anEdgeE.IsNull() ) continue;
284 TopExp::Vertices(anEdgeE, aV1, aV2);
285 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
286 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
290 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
292 if ( C.IsNull() ) continue;
294 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
295 C->D1(lp, aP2, aDirVec);
297 C->D1(fp, aP1, aDirVec);
301 GeomAdaptor_Curve aAdC;
302 aAdC.Load(C, fp, lp);
303 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
305 if (aDist > gp::Resolution()) {
307 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
312 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
316 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
319 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
321 if(aValue<0.0 || aValue>1.0) return;
329 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
330 FMat.SetTransparency(aValue);
331 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
332 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(FMat);
333 myCurrentMaterial = FMat;
334 myTransparency = aValue;
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::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
348 const Standard_Boolean aHighlight )
350 Handle(AIS_InteractiveObject) anObj = this;
351 Handle(AIS_InteractiveContext) anIC = GetContext();
352 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
355 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
356 anIC->SetAutomaticHilight( false );
358 anIC->ClearSelected( false );
361 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
363 // Get entity owners for all activated selection modes
364 getEntityOwners( anObj, anIC, anAllMap );
366 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
367 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
370 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
371 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
374 anIC->SetAutomaticHilight( isAutoHilight );
375 anIC->HilightSelected( false );
378 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
380 myDisplayVectors = 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(myTransparency);
391 myCurrentMaterial = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
392 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial( isTopLevel() ? aMatAspect : myCurrentMaterial );
393 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial( isTopLevel() ? aMatAspect : myCurrentMaterial );
395 //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
396 // P->SetPrimitivesAspect(a4bis);
397 // G->SetGroupPrimitivesAspect(a4bis);
398 //a4bis->SetInteriorColor(myShadingColor);
400 myDrawer->ShadingAspect()->SetColor( topLevelColor() );
402 if(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().MaterialType( Graphic3d_MATERIAL_ASPECT ))
403 myDrawer->ShadingAspect()->SetColor(myShadingColor);
405 myDrawer->ShadingAspect()->SetColor(myDrawer->ShadingAspect()->Aspect()->FrontMaterial().AmbientColor());
408 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
409 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
410 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
413 void GEOM_AISShape::storeIsoNumbers()
415 myUIsoNumber = myDrawer->UIsoAspect()->Number();
416 myVIsoNumber = myDrawer->VIsoAspect()->Number();
419 void GEOM_AISShape::restoreIsoNumbers()
421 if ( myUIsoNumber > 0 ) {
422 // Restore number of U iso lines
423 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
424 anAspect->SetNumber( myUIsoNumber );
425 myDrawer->SetUIsoAspect( anAspect );
428 if ( myVIsoNumber > 0 ) {
429 // Restore number of V iso lines
430 Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
431 anAspect->SetNumber( myVIsoNumber );
432 myDrawer->SetVIsoAspect( anAspect );
436 void GEOM_AISShape::resetIsoNumbers()
438 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
439 anAspect->SetNumber( 0 );
440 myDrawer->SetUIsoAspect( anAspect );
442 anAspect = myDrawer->VIsoAspect();
443 anAspect->SetNumber( 0 );
444 myDrawer->SetVIsoAspect( anAspect );
447 void GEOM_AISShape::storeBoundaryColors()
449 Aspect_TypeOfLine aLT;
452 myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
453 myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
456 void GEOM_AISShape::restoreBoundaryColors()
458 Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
459 anAspect->SetColor( myFreeBoundaryColor );
460 myDrawer->SetFreeBoundaryAspect( anAspect );
462 anAspect = myDrawer->UnFreeBoundaryAspect();
463 anAspect->SetColor( myUnFreeBoundaryColor );
464 myDrawer->SetUnFreeBoundaryAspect( anAspect );
468 Standard_Boolean GEOM_AISShape::isTopLevel() {
472 void GEOM_AISShape::setTopLevel(Standard_Boolean f) {
476 Quantity_Color GEOM_AISShape::topLevelColor() {
477 SUIT_Session* session = SUIT_Session::session();
478 SUIT_ResourceMgr* resMgr = session->resourceMgr();
479 QColor c = resMgr->colorValue( "Geometry", "toplevel_color", QColor( 170, 85, 0 ) );
480 return SalomeApp_Tools::color(c);