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