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