]> SALOME platform Git repositories - modules/geom.git/blob - src/OBJECT/GEOM_AISShape.cxx
Salome HOME
Mantis issues 0020573 and 0020603: Partition problems. A fix by Peter KURNEV.
[modules/geom.git] / src / OBJECT / GEOM_AISShape.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  GEOM OBJECT : interactive object for Geometry entities visualization
23 //  File   : GEOM_AISShape.cxx
24 //  Author : Nicolas REJNERI
25 //  Module : GEOM
26 //  $Header$
27 //
28 /*!
29   \class GEOM_AISShape GEOM_AISShape.hxx
30   \brief ....
31 */
32
33 #include "GEOM_AISShape.ixx"
34 #include "SALOME_InteractiveObject.hxx"
35
36 // Open CASCADE Includes
37 #include <AIS_Drawer.hxx>
38 #include <AIS_InteractiveContext.hxx>
39 #include <Graphic3d_AspectFillArea3d.hxx>
40 #include <Prs3d_ShadingAspect.hxx>
41 #include <SelectBasics_SensitiveEntity.hxx>
42 #include <SelectMgr_EntityOwner.hxx>
43 #include <StdSelect_BRepOwner.hxx>
44 #include <SelectMgr_IndexedMapOfOwner.hxx>
45 #include <SelectMgr_Selection.hxx>
46 #include <StdSelect_DisplayMode.hxx>
47 #include <StdPrs_WFShape.hxx>
48 #include <TColStd_IndexedMapOfInteger.hxx>
49 #include <TColStd_ListIteratorOfListOfInteger.hxx>
50 #include <TColStd_ListOfInteger.hxx>
51 #include <TopExp.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopTools_IndexedMapOfShape.hxx>
54
55 #include <BRep_Tool.hxx>
56 #include <TopExp.hxx>
57 #include <TopExp_Explorer.hxx>
58 #include <TopoDS.hxx>
59 #include <TopoDS_Edge.hxx>
60 #include <TopoDS_Shape.hxx>
61 #include <TopoDS_Vertex.hxx>
62 #include <gp_Pnt.hxx>
63 #include <gp_Dir.hxx>
64 #include <gp_Vec.hxx>
65 #include <Prs3d_Arrow.hxx>
66 #include <GeomAdaptor_Curve.hxx>
67 #include <GCPnts_AbscissaPoint.hxx>
68
69 using namespace std;
70
71 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
72                              const Handle(AIS_InteractiveContext)& theIC,
73                              SelectMgr_IndexedMapOfOwner& theMap )
74 {
75   if ( theObj.IsNull() || theIC.IsNull() )
76     return;
77
78   TColStd_ListOfInteger modes;
79   theIC->ActivatedModes( theObj, modes );
80
81   TColStd_ListIteratorOfListOfInteger itr( modes );
82   for (; itr.More(); itr.Next() ) {
83     int m = itr.Value();
84     if ( !theObj->HasSelection( m ) )
85       continue;
86
87     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
88
89     for ( sel->Init(); sel->More(); sel->Next() ) {
90       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
91       if ( entity.IsNull() )
92         continue;
93
94       Handle(SelectMgr_EntityOwner) owner =
95         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
96       if ( !owner.IsNull() )
97         theMap.Add( owner );
98     }
99   }
100 }
101
102 static void indicesToOwners( const TColStd_IndexedMapOfInteger& aIndexMap,
103                              const TopoDS_Shape& aMainShape,
104                              const SelectMgr_IndexedMapOfOwner& anAllMap, 
105                              SelectMgr_IndexedMapOfOwner& aToHiliteMap )
106 {
107   TopTools_IndexedMapOfShape aMapOfShapes;
108   TopExp::MapShapes(aMainShape, aMapOfShapes);
109
110   for  ( Standard_Integer i = 1, n = anAllMap.Extent(); i <= n; i++ ) {
111     Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(anAllMap( i ));
112     if ( anOwner.IsNull() || !anOwner->HasShape() )
113       continue;
114
115     const TopoDS_Shape& aSubShape = anOwner->Shape();
116     Standard_Integer aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
117     if ( !aSubShapeId || !aIndexMap.Contains( aSubShapeId ) )
118       continue;
119     
120     if ( !aToHiliteMap.Contains( anOwner ) )
121       aToHiliteMap.Add( anOwner );
122   }
123 }
124
125 GEOM_AISShape::GEOM_AISShape(const TopoDS_Shape& shape,
126                              const Standard_CString aName)
127   : SALOME_AISShape(shape), myName(aName), myDisplayVectors(false)
128 {
129   myShadingColor = Quantity_Color( Quantity_NOC_GOLDENROD );
130 }
131
132 void GEOM_AISShape::setIO(const Handle(SALOME_InteractiveObject)& io){
133   SetOwner( io );
134 }
135
136 Handle(SALOME_InteractiveObject) GEOM_AISShape::getIO(){
137   Handle(SALOME_InteractiveObject) IO;
138   if ( !GetOwner().IsNull() )
139     IO = Handle(SALOME_InteractiveObject)::DownCast( GetOwner() );
140   return IO;
141 }
142
143 Standard_Boolean GEOM_AISShape::hasIO(){
144   return !getIO().IsNull();
145 }
146
147 void GEOM_AISShape::setName(const Standard_CString aName)
148 {
149   myName = aName;
150
151   Handle(SALOME_InteractiveObject) IO = getIO();
152   if ( !IO.IsNull() )
153     IO->setName(aName);
154 }
155
156 Standard_CString GEOM_AISShape::getName(){
157   return myName.ToCString();
158 }
159
160 void GEOM_AISShape::Compute(const Handle(PrsMgr_PresentationManager3d)& aPresentationManager,
161                             const Handle(Prs3d_Presentation)& aPrs,
162                             const Standard_Integer aMode)
163 {  
164   if (IsInfinite()) aPrs->SetInfiniteState(Standard_True); //pas de prise en compte lors du FITALL
165   
166   StdSelect_DisplayMode d = (StdSelect_DisplayMode) aMode;
167
168   switch (d) {
169   case StdSelect_DM_Wireframe: 
170     {
171       StdPrs_WFShape::Add(aPrs,myshape,myDrawer);
172       break;
173     }
174   case StdSelect_DM_Shading:
175     {
176       myDrawer->ShadingAspect()->Aspect()->SetDistinguishOn();
177       
178       Graphic3d_MaterialAspect aMatAspect;
179       aMatAspect.SetAmbient( 0.5 );
180       aMatAspect.SetDiffuse( 0.5 );
181       aMatAspect.SetEmissive( 0.5 );
182       aMatAspect.SetShininess(0.5 );
183       aMatAspect.SetSpecular( 0.5 );
184       
185       myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(aMatAspect);
186       myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(Graphic3d_NOM_JADE);
187       
188       Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
189       Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
190       FMat.SetTransparency(myTransparency); BMat.SetTransparency(myTransparency);
191       myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
192       myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
193
194       //Handle(Graphic3d_AspectFillArea3d) a4bis = myDrawer->ShadingAspect()->Aspect();
195       //       P->SetPrimitivesAspect(a4bis);
196       //        G->SetGroupPrimitivesAspect(a4bis);
197       //a4bis->SetInteriorColor(myShadingColor);
198       myDrawer->ShadingAspect()->SetColor(myShadingColor);
199
200       // PAL12113: AIS_Shape::Compute() works correctly with shapes containing no faces
201       //StdPrs_ShadedShape::Add(aPrs,myshape,myDrawer);
202       AIS_Shape::Compute(aPresentationManager, aPrs, aMode);
203       break;
204     }
205   }
206
207   if (isShowVectors())
208   {
209     TopExp_Explorer Exp ( myshape, TopAbs_EDGE );
210     for ( ; Exp.More(); Exp.Next() ) {
211       TopoDS_Vertex aV1, aV2;
212       TopoDS_Edge anEdgeE = TopoDS::Edge(Exp.Current());
213
214       if ( anEdgeE.IsNull() ) continue;
215
216       TopExp::Vertices(anEdgeE, aV1, aV2);
217       gp_Pnt aP1 = BRep_Tool::Pnt(aV1);
218       gp_Pnt aP2 = BRep_Tool::Pnt(aV2);
219
220       double fp,lp;
221       gp_Vec aDirVec;
222       Handle(Geom_Curve) C = BRep_Tool::Curve(anEdgeE,fp,lp);
223
224       if ( C.IsNull() ) continue;
225
226       if ( anEdgeE.Orientation() == TopAbs_FORWARD )
227         C->D1(lp, aP2, aDirVec);
228       else {
229         C->D1(fp, aP1, aDirVec);
230         aP2 = aP1;
231       }
232
233       GeomAdaptor_Curve aAdC;
234       aAdC.Load(C, fp, lp);
235       Standard_Real aDist = GCPnts_AbscissaPoint::Length(aAdC, fp, lp); 
236      
237       if (aDist > gp::Resolution()) {
238         gp_Dir aDir;
239         if ( anEdgeE.Orientation() == TopAbs_FORWARD )
240           aDir = aDirVec;
241         else
242           aDir = -aDirVec;
243
244         Prs3d_Arrow::Draw(aPrs, aP2, aDir, PI/180.*5., aDist/10.);
245       }
246     }
247   }
248   //  aPrs->ReCompute(); // for hidden line recomputation if necessary...
249 }
250
251 void GEOM_AISShape::SetTransparency(const Standard_Real aValue)
252 {
253   if(aValue<0.0 || aValue>1.0) return;
254   
255   if(aValue<=0.05) 
256     {
257       UnsetTransparency();
258       return;
259     }
260
261   Graphic3d_MaterialAspect FMat = myDrawer->ShadingAspect()->Aspect()->FrontMaterial();
262   Graphic3d_MaterialAspect BMat = myDrawer->ShadingAspect()->Aspect()->BackMaterial();
263   FMat.SetTransparency(aValue); BMat.SetTransparency(aValue);
264   myDrawer->ShadingAspect()->Aspect()->SetFrontMaterial(FMat);
265   myDrawer->ShadingAspect()->Aspect()->SetBackMaterial(BMat);
266   myTransparency = aValue;
267 }
268
269 void GEOM_AISShape::SetShadingColor(const Quantity_Color &aCol)
270 {
271   myShadingColor = aCol;
272 }
273
274 void GEOM_AISShape::highlightSubShapes(const TColStd_IndexedMapOfInteger& aIndexMap, 
275                                        const Standard_Boolean aHighlight )
276 {
277   Handle(AIS_InteractiveObject) anObj = this;
278   Handle(AIS_InteractiveContext) anIC = GetContext();
279   if ( anIC.IsNull() || !anIC->HasOpenedContext() ) 
280     return;
281
282   Standard_Boolean isAutoHilight = anIC->AutomaticHilight();
283   anIC->SetAutomaticHilight( false );
284
285   anIC->ClearSelected( false );
286
287   if ( aHighlight ) {
288     SelectMgr_IndexedMapOfOwner anAllMap, aToHiliteMap;
289
290     // Get entity owners for all activated selection modes
291     getEntityOwners( anObj, anIC, anAllMap );
292
293     // Convert <aIndexMap> into the map of owners to highlight/unhighlight
294     indicesToOwners( aIndexMap, Shape(), anAllMap, aToHiliteMap );
295
296
297     for ( Standard_Integer i = 1, n = aToHiliteMap.Extent(); i <= n; i++ )
298       anIC->AddOrRemoveSelected( aToHiliteMap( i ), false );
299   }
300
301   anIC->SetAutomaticHilight( isAutoHilight );
302   anIC->HilightSelected( false );
303 }
304
305 void GEOM_AISShape::SetDisplayVectors(bool isDisplayed)
306 {
307   myDisplayVectors = isDisplayed;
308 }