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>
76 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
77 const Handle(AIS_InteractiveContext)& theIC,
78 SelectMgr_IndexedMapOfOwner& theMap )
80 if ( theObj.IsNull() || theIC.IsNull() )
83 TColStd_ListOfInteger modes;
84 theIC->ActivatedModes( theObj, modes );
86 TColStd_ListIteratorOfListOfInteger itr( modes );
87 for (; itr.More(); itr.Next() ) {
89 if ( !theObj->HasSelection( m ) )
92 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
94 for ( sel->Init(); sel->More(); sel->Next() ) {
95 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
96 if ( entity.IsNull() )
99 Handle(SelectMgr_EntityOwner) owner =
100 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
101 if ( !owner.IsNull() )
107 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
108 const TopoDS_Shape& aMainShape,
109 const SelectMgr_IndexedMapOfOwner& anAllMap,
110 SelectMgr_IndexedMapOfOwner& aToHiliteMap )
112 TopTools_IndexedMapOfShape aMapOfShapes;
113 TopExp::MapShapes(aMainShape, aMapOfShapes);
115 for ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
116 Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
117 if ( anOwner.IsNull() || !anOwner->HasShape() )
120 const TopoDS_Shape& aSubShape = anOwner->Shape();
121 Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
122 if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
125 if ( !aToHiliteMap.Contains( anOwner ) )
126 aToHiliteMap.Add( anOwner );
130 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
131 const Standard_CString aName)
132 : SALOME_AISShape(shape), myName(aName), myDisplayVectors(false)
134 myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
136 storeBoundaryColors();
138 myEdgesInShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
144 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
148 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
149 Handle(SALOME_InteractiveObject) IO;
150 if ( !GetOwner().IsNull() )
151 IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
155 Standard_Boolean GEOM_AISShape::hasIO(){
156 return !getIO().IsNull();
159 void GEOM_AISShape::setName(const Standard_CString aName)
163 Handle(SALOME_InteractiveObject) IO = getIO();
168 Standard_CString GEOM_AISShape::getName(){
169 return myName.ToCString();
172 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
173 const Handle(Prs3d_Presentation)& aPrs,
174 const Standard_Integer aMode)
176 if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
178 // StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
180 case 0://StdSelect_DM_Wireframe:
184 // Restore wireframe edges colors
185 restoreBoundaryColors();
187 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
190 case 1://StdSelect_DM_Shading:
194 shadingMode(aPresentationManager, aPrs, aMode);
196 // Store wireframe edges colors
197 storeBoundaryColors();
201 case 3: //StdSelect_DM_HLR:
203 AIS_TexturedShape::Compute(aPresentationManager, aPrs, aMode);
208 if ( aMode == ShadingWithEdges ) {
209 // Temporary store number of iso lines in order to recover its later
210 // when display mode is achnged to 'Wirefame' or 'Shading'.
211 // Iso lines are not displayed in 'Shading with edges' mode.
214 // Reset number of iso lines to 0
218 shadingMode(aPresentationManager, aPrs, AIS_Shaded);
220 // Store wireframe edges colors
221 storeBoundaryColors();
224 Handle(Prs3d_LineAspect) anAspect = myDrawer->UnFreeBoundaryAspect();
225 anAspect->SetColor( myEdgesInShadingColor );
226 myDrawer->SetUnFreeBoundaryAspect( anAspect );
228 anAspect = myDrawer->FreeBoundaryAspect();
229 anAspect->SetColor( myEdgesInShadingColor );
230 myDrawer->SetFreeBoundaryAspect( anAspect );
232 // Add edges to presentation
233 StdPrs_WFDeflectionShape::Add(aPrs,myshape,myDrawer);
238 const bool isVector = IsKind(STANDARD_TYPE(GEOM_AISVector));
239 TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
240 for ( ; Exp.More(); Exp.Next() ) {
241 TopoDS_Vertex aV1, aV2;
242 TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
244 // draw curve direction (issue 0021087)
245 anEdgeE.Orientation( TopAbs_FORWARD );
247 if ( anEdgeE.IsNull() ) continue;
249 TopExp::Vertices(anEdgeE, aV1, aV2);
250 gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
251 gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
255 Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
257 if ( C.IsNull() ) continue;
259 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
260 C->D1(lp, aP2, aDirVec);
262 C->D1(fp, aP1, aDirVec);
266 GeomAdaptor_Curve aAdC;
267 aAdC.Load(C, fp, lp);
268 Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp);
270 if (aDist > gp::Resolution()) {
272 if ( anEdgeE.Orientation() == TopAbs_FORWARD )
277 Prs3d_Arrow::Draw(aPrs, aP2, aDir, M_PI/180.*5., aDist/10.);
281 // aPrs->ReCompute(); // for hidden line recomputation if necessary...
284 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
286 if(aValue<0.0 || aValue>1.0) return;
294 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
295 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
296 FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
297 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
298 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
299 myTransparency = aValue;
302 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
304 myShadingColor = aCol;
307 void GEOM_AISShape::SetEdgesInShadingColor(const Quantity_Color &aCol)
309 myEdgesInShadingColor = aCol;
312 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap,
313 const Standard_Boolean aHighlight )
315 Handle(AIS_InteractiveObject) anObj = this;
316 Handle(AIS_InteractiveContext) anIC = GetContext();
317 if ( anIC.IsNull() || !anIC->HasOpenedContext() )
320 Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
321 anIC->SetAutomaticHilight( false );
323 anIC->ClearSelected( false );
326 SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
328 // Get entity owners for all activated selection modes
329 getEntityOwners( anObj, anIC, anAllMap );
331 // Convert <aIndexMap> into the map of owners to highlight/unhighlight
332 indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
335 for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
336 anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
339 anIC->SetAutomaticHilight( isAutoHilight );
340 anIC->HilightSelected( false );
343 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
345 myDisplayVectors = isDisplayed;
348 void GEOM_AISShape::shadingMode(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
349 const Handle(Prs3d_Presentation)& aPrs,
350 const Standard_Integer aMode)
352 myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
354 Graphic3d_MaterialAspect aMatAspect;
355 if ( !HasMaterial() ) {
356 aMatAspect.SetAmbient( 0.5 );
357 aMatAspect.SetDiffuse( 0.5 );
358 aMatAspect.SetEmissive( 0.5 );
359 aMatAspect.SetShininess(0.5 );
360 aMatAspect.SetSpecular( 0.5 );
362 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
363 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Graphic3d_NOM_JADE);
366 Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
367 Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
368 FMat.SetTransparency(myTransparency); BMat.SetTransparency(myTransparency);
369 myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
370 myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
372 //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
373 // P->SetPrimitivesAspect(a4bis);
374 // G->SetGroupPrimitivesAspect(a4bis);
375 //a4bis->SetInteriorColor(myShadingColor);
376 myDrawer->ShadingAspect()->SetColor(myShadingColor);
378 // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
379 //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
380 AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
383 void GEOM_AISShape::storeIsoNumbers()
385 myUIsoNumber = myDrawer->UIsoAspect()->Number();
386 myVIsoNumber = myDrawer->VIsoAspect()->Number();
389 void GEOM_AISShape::restoreIsoNumbers()
391 if ( myUIsoNumber > 0 ) {
392 // Restore number of U iso lines
393 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
394 anAspect->SetNumber( myUIsoNumber );
395 myDrawer->SetUIsoAspect( anAspect );
398 if ( myVIsoNumber > 0 ) {
399 // Restore number of V iso lines
400 Handle(Prs3d_IsoAspect) anAspect = myDrawer->VIsoAspect();
401 anAspect->SetNumber( myVIsoNumber );
402 myDrawer->SetVIsoAspect( anAspect );
406 void GEOM_AISShape::resetIsoNumbers()
408 Handle(Prs3d_IsoAspect) anAspect = myDrawer->UIsoAspect();
409 anAspect->SetNumber( 0 );
410 myDrawer->SetUIsoAspect( anAspect );
412 anAspect = myDrawer->VIsoAspect();
413 anAspect->SetNumber( 0 );
414 myDrawer->SetVIsoAspect( anAspect );
417 void GEOM_AISShape::storeBoundaryColors()
419 Aspect_TypeOfLine aLT;
422 myDrawer->FreeBoundaryAspect()->Aspect()->Values( myFreeBoundaryColor, aLT, aW);
423 myDrawer->UnFreeBoundaryAspect()->Aspect()->Values( myUnFreeBoundaryColor, aLT, aW);
426 void GEOM_AISShape::restoreBoundaryColors()
428 Handle(Prs3d_LineAspect) anAspect = myDrawer->FreeBoundaryAspect();
429 anAspect->SetColor( myFreeBoundaryColor );
430 myDrawer->SetFreeBoundaryAspect( anAspect );
432 anAspect = myDrawer->UnFreeBoundaryAspect();
433 anAspect->SetColor( myUnFreeBoundaryColor );
434 myDrawer->SetUnFreeBoundaryAspect( anAspect );