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