Salome HOME
Bug #261: Problems with selection of GEOM objects in HYDRO module after opening of...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_VTKPrsShape.cxx
1 // Copyright (C) 2007-2013  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
23 #include "HYDROGUI_VTKPrsShape.h"
24
25 #include <HYDROData_Entity.h>
26 #include <HYDROData_Channel.h>
27 #include <HYDROData_Document.h>
28 #include <HYDROData_DummyObject3D.h>
29 #include <HYDROData_Image.h>
30 #include <HYDROData_ImmersibleZone.h>
31 #include <HYDROData_Obstacle.h>
32 #include <HYDROData_PolylineXY.h>
33 #include <HYDROData_Polyline3D.h>
34 #include <HYDROData_Profile.h>
35 #include <HYDROData_Region.h>
36 #include <HYDROData_Stream.h>
37 #include <HYDROData_Zone.h>
38
39 #include <BRepBuilderAPI_MakeEdge.hxx>
40 #include <BRepBuilderAPI_MakeWire.hxx>
41 #include <BRepBuilderAPI_MakeFace.hxx>
42
43 #include <gp_Pnt.hxx>
44
45 #include <Graphic3d_AspectFillArea3d.hxx>
46 #include <Graphic3d_MaterialAspect.hxx>
47
48 #include <TopoDS.hxx>
49 #include <TopoDS_Wire.hxx>
50 #include <TopoDS_Face.hxx>
51
52 #include <TopExp_Explorer.hxx>
53
54 #include <BRep_Builder.hxx>
55
56 #include <Precision.hxx>
57
58 #include <HYDROGUI_Actor.h>
59 #include <vtkScalarBarActor.h>
60
61 #include <QString>
62
63 // Hard-coded value of shape deflection coefficient for VTK viewer
64 const double VTK_MIN_DEFLECTION = 0.001;
65
66
67 //=======================================================================
68 // name    : HYDROGUI_VTKPrsShape
69 // Purpose : Constructor
70 //=======================================================================
71 HYDROGUI_VTKPrsShape::HYDROGUI_VTKPrsShape( const Handle(HYDROData_Entity)& theObject )
72 : HYDROGUI_VTKPrs( theObject ),
73 myDisplayMode( GEOM_Actor::eWireframe )
74 {
75 }
76
77 //=======================================================================
78 // name    : HYDROGUI_VTKPrsShape
79 // Purpose : Destructor
80 //=======================================================================
81 HYDROGUI_VTKPrsShape::~HYDROGUI_VTKPrsShape()
82 {
83 }
84
85 //================================================================
86 // Function : compute
87 // Purpose  : 
88 //================================================================
89 void HYDROGUI_VTKPrsShape::compute()
90 {
91   if ( !getObject().IsNull() )
92   {
93     buildShape();
94
95     if ( !myTopoShape.IsNull() )
96     {
97       HYDROGUI_Actor* anActor = HYDROGUI_Actor::New();
98       anActor->SetShape( myTopoShape, VTK_MIN_DEFLECTION );
99       anActor->setDisplayMode( myDisplayMode );
100       anActor->setIO( getIO() );
101       AddObject( anActor );
102
103       //double bounds[6];
104       //anActor->GetBounds( bounds );
105
106       anActor->Delete();
107     }
108   }
109 }
110
111
112 void HYDROGUI_VTKPrsShape::buildShape()
113 {
114   Handle(HYDROData_Entity) anObject = getObject();
115   // Try to retrieve information from object
116   if ( !anObject.IsNull() )
117   {
118     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( anObject->Label() );
119   
120     if ( anObject->IsKind( STANDARD_TYPE(HYDROData_ImmersibleZone) ) )
121     {
122       Handle(HYDROData_ImmersibleZone) aZoneObj =
123         Handle(HYDROData_ImmersibleZone)::DownCast( anObject );
124
125       TopoDS_Shape aZoneShape = aZoneObj->GetTopShape();
126       if ( !aZoneShape.IsNull() ) {
127         if ( aZoneShape.ShapeType() == TopAbs_FACE ) {
128           TopoDS_Face aZoneFace = TopoDS::Face( aZoneShape );
129           setFace( aZoneFace, false, false );
130         } else {
131           myTopoShape = aZoneShape;
132         }
133       }
134
135       QColor aFillingColor = aZoneObj->GetFillingColor();
136       QColor aBorderColor = aZoneObj->GetBorderColor();
137
138       //setFillingColor( aFillingColor, false, false );
139       //setBorderColor( aBorderColor, false, false );
140     }
141     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_PolylineXY) ) )
142     {
143       Handle(HYDROData_PolylineXY) aPolyline =
144         Handle(HYDROData_PolylineXY)::DownCast( anObject );
145
146       TopoDS_Shape aPolylineShape = aPolyline->GetShape();
147
148       if ( !aPolylineShape.IsNull() ) {
149         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
150           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
151           setWire( aPolylineWire, false, false );  
152         } else {
153           myTopoShape = aPolylineShape;
154           myDisplayMode = GEOM_Actor::eWireframe;
155         }
156       }
157
158       QColor aWireColor = aPolyline->GetWireColor();
159       //setBorderColor( aWireColor, false, false );
160     }
161     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Polyline3D) ) )
162     {
163       Handle(HYDROData_Polyline3D) aPolyline =
164         Handle(HYDROData_Polyline3D)::DownCast( anObject );
165
166       TopoDS_Shape aPolylineShape = aPolyline->GetShape3D();
167
168       if ( !aPolylineShape.IsNull() ) {
169         if ( aPolylineShape.ShapeType() == TopAbs_WIRE ) {
170           TopoDS_Wire aPolylineWire = TopoDS::Wire( aPolylineShape );
171           setWire( aPolylineWire, false, false );  
172         } else {
173           myTopoShape = aPolylineShape;
174           myDisplayMode = GEOM_Actor::eWireframe;
175         }
176       }
177
178       QColor aWireColor = aPolyline->GetBorderColor();
179       //setBorderColor( aWireColor, false, false );
180     }
181     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Zone) ) )
182     {
183       Handle(HYDROData_Zone) aZone =
184         Handle(HYDROData_Zone)::DownCast( anObject );
185
186       TopoDS_Face aZoneFace = TopoDS::Face( aZone->GetShape() );
187
188       setFace( aZoneFace, false, false );
189       if (aZone->IsMergingNeed() && aZone->GetMergeType() == HYDROData_Zone::Merge_UNKNOWN )
190       {
191         // Red color for a zone with bathymetry conflict
192         //setFillingColor( Qt::red );
193       }
194       else
195       {
196         // Generate the filling color for zone
197         QStringList aGeomObjectsNames;
198
199         HYDROData_SequenceOfObjects aRefObjects = aZone->GetGeometryObjects();
200         HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
201         for ( ; anIter.More(); anIter.Next() )
202         {
203           Handle(HYDROData_Object) aRefbject = 
204             Handle(HYDROData_Object)::DownCast( anIter.Value() );
205           if ( aRefbject.IsNull() )
206             continue;
207
208           QString aRefObjectName = aRefbject->GetName();
209           if ( aRefObjectName.isEmpty() )
210             continue;
211
212           aGeomObjectsNames.append( aRefObjectName );
213         }
214
215         //setFillingColor( HYDROGUI_Tool::GenerateFillingColor( aDocument, aGeomObjectsNames ) );
216       }
217     }
218     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Image) ) )
219     {
220     }
221     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Profile) ) )
222     {
223       Handle(HYDROData_Profile) aProfile =
224         Handle(HYDROData_Profile)::DownCast( anObject );
225
226       TopoDS_Wire aProfileWire;
227
228       if ( aProfile->IsValid() ) {
229         TopoDS_Shape aProfileShape = aProfile->GetShape3D();
230
231         if ( !aProfileShape.IsNull() && 
232              aProfileShape.ShapeType() == TopAbs_WIRE ) {
233           aProfileWire = TopoDS::Wire( aProfileShape );
234         }
235       }
236
237       setWire( aProfileWire, false, false );  
238
239       QColor aWireColor = aProfile->GetBorderColor();
240       //setBorderColor( aWireColor, false, false );
241     }
242     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_Stream) ) ||
243               anObject->IsKind( STANDARD_TYPE(HYDROData_Channel) ) ||
244               anObject->IsKind( STANDARD_TYPE(HYDROData_Obstacle) ) )
245     {
246       Handle(HYDROData_Object) aGeomObject =
247         Handle(HYDROData_Object)::DownCast( anObject );
248
249       TopoDS_Shape anObjShape = aGeomObject->GetTopShape();
250
251       setShape( anObjShape, false, false );
252
253       QColor aFillingColor = aGeomObject->GetFillingColor();
254       QColor aBorderColor = aGeomObject->GetBorderColor();
255
256       //setFillingColor( aFillingColor, false, false );
257       //setBorderColor( aBorderColor, false, false );
258     }
259     else if ( anObject->IsKind( STANDARD_TYPE(HYDROData_DummyObject3D) ) )
260     {
261       Handle(HYDROData_DummyObject3D) anObject3D =
262         Handle(HYDROData_DummyObject3D)::DownCast( anObject );
263       TopoDS_Shape aShape3D = anObject3D->GetShape();
264
265       setShape( aShape3D, false, false );
266
267       QColor aFillingColor = anObject3D->GetFillingColor();
268       QColor aBorderColor = anObject3D->GetBorderColor();
269
270       //setFillingColor( aFillingColor, false, false );
271       //setBorderColor( aBorderColor, false, false );
272     }
273   }
274 }
275
276 void HYDROGUI_VTKPrsShape::setWire( const TopoDS_Wire& theWire,
277                                     const bool         theToDisplay,
278                                     const bool         theIsUpdateViewer )
279 {
280   myTopoShape = theWire;
281   myDisplayMode = GEOM_Actor::eWireframe;
282 }
283
284 void HYDROGUI_VTKPrsShape::setFaces( const TopoDS_Compound& theWires,
285                                      const bool             theToDisplay,
286                                      const bool             theIsUpdateViewer )
287 {
288   TopExp_Explorer anExp( theWires, TopAbs_WIRE );
289   TopoDS_Compound aCompound;
290   BRep_Builder aBuilder;
291     aBuilder.MakeCompound( aCompound );
292
293   for ( ; anExp.More(); anExp.Next() ) {
294     TopoDS_Wire aWire = TopoDS::Wire( anExp.Current() );
295     if ( aWire.IsNull() ) {
296       continue;
297     }
298
299     BRepBuilderAPI_MakeFace aMakeFace( aWire, Standard_True );
300     aMakeFace.Build();
301     if( aMakeFace.IsDone() ) {
302       aBuilder.Add( aCompound, aMakeFace.Face() );
303     }
304   }
305
306   myTopoShape = aCompound;
307   myDisplayMode = GEOM_Actor::eShading;
308 }
309
310 void HYDROGUI_VTKPrsShape::setFace( const TopoDS_Wire& theWire,
311                                     const bool         theToDisplay,
312                                     const bool         theIsUpdateViewer )
313 {
314   BRepBuilderAPI_MakeFace aFaceBuilder( theWire, Standard_True );
315   aFaceBuilder.Build();
316   if( aFaceBuilder.IsDone() )
317   {
318     TopoDS_Face aFace = aFaceBuilder.Face();
319     setFace( aFace, theToDisplay, theIsUpdateViewer );
320   }
321 }
322
323 void HYDROGUI_VTKPrsShape::setFace( const TopoDS_Face& theFace,
324                                     const bool         theToDisplay,
325                                     const bool         theIsUpdateViewer )
326 {
327   myTopoShape = theFace;
328   myDisplayMode = GEOM_Actor::eShading;
329 }
330
331 void HYDROGUI_VTKPrsShape::setShape( const TopoDS_Shape& theShape,
332                                      const bool          theToDisplay,
333                                      const bool          theIsUpdateViewer )
334 {
335   myTopoShape = theShape;
336   myDisplayMode = GEOM_Actor::eShading;
337 }