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