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