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