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