Salome HOME
Common part of dump to python is moved to HYDROData_Entity.
[modules/hydro.git] / src / HYDROData / HYDROData_Obstacle.cxx
1
2 #include "HYDROData_Obstacle.h"
3
4 #include "HYDROData_Document.h"
5 #include "HYDROData_ShapesGroup.h"
6 #include "HYDROData_ShapesTool.h"
7
8 #include <Basics_Utils.hxx>
9
10 #include <BRepTools.hxx>
11 #include <BRep_Builder.hxx>
12
13 #include <IGESControl_Reader.hxx>
14 #include <IGESData_IGESModel.hxx>
15
16 #include <STEPControl_Reader.hxx>
17
18 #include <Interface_Static.hxx>
19
20 #include <TopoDS.hxx>
21 #include <TopoDS_Iterator.hxx>
22 #include <TopoDS_Shape.hxx>
23 #include <TopoDS_Edge.hxx>
24
25 #include <TDataStd_AsciiString.hxx>
26
27 #include <TColStd_SequenceOfAsciiString.hxx>
28
29 #include <TopExp_Explorer.hxx>
30
31 #include <QColor>
32 #include <QFile>
33 #include <QFileInfo>
34 #include <QStringList>
35
36 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
37
38 #include <HYDROData_Projection.h>
39
40 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
41 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
42
43
44 HYDROData_Obstacle::HYDROData_Obstacle()
45 : HYDROData_ArtificialObject()
46 {
47 }
48
49 HYDROData_Obstacle::~HYDROData_Obstacle()
50 {
51 }
52
53 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
54 {
55   QStringList aResList = HYDROData_Entity::DumpToPython( theTreatedObjects );
56
57   // TODO
58
59   return aResList;
60 }
61
62 void HYDROData_Obstacle::Update()
63 {
64   removeGroupObjects();
65   createGroupObjects();
66   checkAndSetAltitudeObject();
67
68   HYDROData_Entity::Update();
69 }
70
71 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
72 {
73   return getTopShape();
74 }
75
76 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
77 {
78   return getShape3D();
79 }
80
81 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
82 {
83   TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
84   HYDROData_ArtificialObject::SetShape3D( theShape );
85   HYDROData_ArtificialObject::SetTopShape( aShape2d );
86 }
87
88 QColor HYDROData_Obstacle::DefaultFillingColor()
89 {
90   return QColor( Qt::yellow );
91 }
92
93 QColor HYDROData_Obstacle::DefaultBorderColor()
94 {
95   return QColor( Qt::transparent );
96 }
97
98 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
99 {
100   // Check the file existence
101   QFileInfo aFileInfo( theFilePath );
102   if ( !aFileInfo.exists() ) {
103     return false;
104   }
105
106   bool aRes = false;
107   TopoDS_Shape aShape;
108
109   // Import file
110   QString aFileSuf = aFileInfo.suffix().toLower();
111   if ( aFileSuf == "brep" ) {
112     aShape = ImportBREP( theFilePath );
113   } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
114     aShape = ImportIGES( theFilePath );
115   } else if ( aFileSuf == "step" ) {
116     aShape = ImportSTEP( theFilePath );
117   }
118  
119   // Check the result shape
120   aRes = !aShape.IsNull();
121
122   // Set shape to the obstacle in case of success
123   if ( aRes ) {
124     SetShape3D( aShape );
125     SetFilePath( theFilePath );
126   }
127
128   return aRes;
129 }
130
131 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
132 {
133   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
134   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
135 }
136
137 QString HYDROData_Obstacle::GetFilePath() const
138 {
139   QString aRes;
140
141   Handle(TDataStd_AsciiString) anAsciiStr;
142   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
143     aRes = QString( anAsciiStr->Get().ToCString() );
144
145   return aRes;
146 }
147
148 void HYDROData_Obstacle::SetGeomObjectEntry( const QString& theEntry )
149 {
150   TCollection_AsciiString anAsciiStr( theEntry.toStdString().c_str() );
151   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), anAsciiStr );
152 }
153
154 QString HYDROData_Obstacle::GetGeomObjectEntry() const
155 {
156   QString aRes;
157
158   Handle(TDataStd_AsciiString) anAsciiStr;
159   if ( myLab.FindChild( DataTag_GeomObjectEntry ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
160     aRes = QString( anAsciiStr->Get().ToCString() );
161
162   return aRes;
163 }
164
165 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
166 {
167   TopoDS_Shape aResShape;
168
169   BRep_Builder aBrepBuilder;
170   BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
171
172   return aResShape;
173 }
174
175 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
176 {
177   TopoDS_Shape aResShape;
178
179   // Set "C" numeric locale to save numbers correctly
180   Kernel_Utils::Localizer loc;
181
182   IGESControl_Reader aReader;
183
184   Interface_Static::SetCVal("xstep.cascade.unit","M");
185
186   try {
187     OCC_CATCH_SIGNALS;
188
189     IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
190
191     if (status == IFSelect_RetDone) {
192       // Rescale units
193       Handle(IGESData_IGESModel) aModel =
194         Handle(IGESData_IGESModel)::DownCast(aReader.Model());
195       if (!aModel.IsNull()) {
196         IGESData_GlobalSection aGS = aModel->GlobalSection();
197         aGS.SetUnitFlag(6);
198         aModel->SetGlobalSection(aGS);
199       }
200     
201       aReader.ClearShapes();
202       aReader.TransferRoots();
203
204       aResShape = aReader.OneShape();
205     } 
206     else {
207       aResShape.Nullify();
208     }
209   }
210   catch(Standard_Failure) {
211     aResShape.Nullify();
212   }
213
214   return aResShape;
215 }
216
217 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
218 {
219   TopoDS_Shape aResShape;
220
221   // Set "C" numeric locale to save numbers correctly
222   Kernel_Utils::Localizer loc;
223
224   STEPControl_Reader aReader;
225
226   // Convert to METERS
227   Interface_Static::SetCVal("xstep.cascade.unit","M");
228   Interface_Static::SetIVal("read.step.ideas", 1);
229   Interface_Static::SetIVal("read.step.nonmanifold", 1);
230
231   BRep_Builder B;
232   TopoDS_Compound compound;
233   B.MakeCompound(compound);
234
235   try {
236     OCC_CATCH_SIGNALS;
237
238     IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
239
240     if (status == IFSelect_RetDone) {
241       // Rescale units
242       // set UnitFlag to units from file
243       TColStd_SequenceOfAsciiString anUnitLengthNames;
244       TColStd_SequenceOfAsciiString anUnitAngleNames;
245       TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
246       aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
247       if (anUnitLengthNames.Length() > 0) {
248         TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
249         if (aLenUnits == "millimetre")
250           Interface_Static::SetCVal("xstep.cascade.unit", "MM");
251         else if (aLenUnits == "centimetre")
252           Interface_Static::SetCVal("xstep.cascade.unit", "CM");
253         else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
254           Interface_Static::SetCVal("xstep.cascade.unit", "M");
255         else if (aLenUnits == "INCH")
256           Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
257         else {
258           // The file contains not supported units
259           return aResShape;
260         }
261       }
262         
263       Standard_Boolean failsonly = Standard_False;
264       aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
265
266       // Root transfers
267       Standard_Integer nbr = aReader.NbRootsForTransfer();
268       aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
269
270       for (Standard_Integer n = 1; n <= nbr; n++) {
271         Standard_Boolean ok = aReader.TransferRoot(n);
272         // Collecting resulting entities
273         Standard_Integer nbs = aReader.NbShapes();
274         if (!ok || nbs == 0) {
275           continue; // skip empty root
276         } 
277         else if (nbr == 1 && nbs == 1) { // For a single entity
278           aResShape = aReader.Shape(1);
279           // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
280           // It should be removed after patching OCCT for bug OCC22436
281           // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
282           if (aResShape.ShapeType() == TopAbs_COMPOUND) {
283             int nbSub1 = 0;
284             TopoDS_Shape currShape;
285             TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
286             for (; It.More(); It.Next()) {
287               nbSub1++;
288               currShape = It.Value();
289             }
290             if (nbSub1 == 1)
291               aResShape = currShape;
292           }
293           // END workaround
294           break;
295         }
296
297         for (Standard_Integer i = 1; i <= nbs; i++) {
298           TopoDS_Shape aShape = aReader.Shape(i);
299           if (aShape.IsNull()) {
300             continue;
301           }
302           else {
303             B.Add(compound, aShape);
304           }
305         }
306       }
307   
308       if (aResShape.IsNull())
309         aResShape = compound;
310
311       // Check if any BRep entity has been read, there must be at least a vertex
312       if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
313         // No geometrical data in the imported file
314         return TopoDS_Shape();
315       }
316     }
317     else {
318       aResShape.Nullify();
319     }
320   }
321   catch (Standard_Failure) {
322     aResShape.Nullify();
323   }
324
325   return aResShape;
326 }
327
328 QColor HYDROData_Obstacle::getDefaultFillingColor() const
329 {
330   return DefaultFillingColor();
331 }
332
333 QColor HYDROData_Obstacle::getDefaultBorderColor() const
334 {
335   return DefaultBorderColor();
336 }
337
338 void HYDROData_Obstacle::createGroupObjects()
339 {
340   TopoDS_Shape anObstacleShape = GetTopShape();
341   if ( !anObstacleShape.IsNull() )
342   {
343     TopTools_SequenceOfShape aWireEdges;
344     HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
345     if ( !aWireEdges.IsEmpty() )
346     {
347       QString aWireGroupName = GetName() + "_Outer_Wire";
348
349       Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
350       anExtWireGroup->SetName( aWireGroupName );
351      
352       anExtWireGroup->SetShapes( aWireEdges );
353     }
354   }
355 }
356
357 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
358 {
359   return KIND_OBSTACLE_ALTITUDE;
360 }
361
362
363