]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Obstacle.cxx
Salome HOME
Remove extra includes.
[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 #include "HYDROData_GeomTool.h"
9
10 #include <Basics_Utils.hxx>
11
12 #include <BRepTools.hxx>
13 #include <BRep_Builder.hxx>
14
15 #include <IGESControl_Reader.hxx>
16 #include <IGESData_IGESModel.hxx>
17
18 #include <STEPControl_Reader.hxx>
19
20 #include <Interface_Static.hxx>
21
22 #include <TopoDS.hxx>
23 #include <TopoDS_Iterator.hxx>
24 #include <TopoDS_Shape.hxx>
25 #include <TopoDS_Edge.hxx>
26
27 #include <TDataStd_AsciiString.hxx>
28 #include <TDataStd_RealArray.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 = theStudy.FindObjectID( \"%2\" );" )
69     //            .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
70     QString aGeomObjectName( GetGeomObjectName().ToCString() );
71     if ( !aGeomObjectName.isEmpty() ) {
72       aResList << QString( "%1 = theStudy.FindObjectByName( \"%2\", \"GEOM\" )[0];" )
73                   .arg( aSalomeObjName ).arg( aGeomObjectName );
74
75       aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
76                   .arg( anObstacleName ).arg( aSalomeObjName );
77
78       aResList << QString( "%1.SetGeomObjectEntry( %2.GetID() );" )
79                   .arg( anObstacleName ).arg( aSalomeObjName );
80     }
81     aResList << QString( "" );
82   }
83   else if ( !aFilePath.isEmpty() )
84   {
85     aResList << QString( "%1.ImportFromFile( \"%2\" );" )
86                 .arg( anObstacleName ).arg( aFilePath );
87     aResList << QString( "" );
88   }
89
90   // Write the translation points
91   double aDx, aDy, aDz;
92   if ( getTranslation( aDx, aDy, aDz ) )
93   {
94     aResList << QString( "%1.Translate( %2, %3, %4 );" )
95                 .arg( anObstacleName ).arg( aDx ).arg( aDy ).arg( aDz );
96     aResList << QString( "" );
97   }
98
99   aResList << QString( "%1.Update();" ).arg( anObstacleName );
100   aResList << QString( "" );
101
102   return aResList;
103 }
104
105 void HYDROData_Obstacle::Update()
106 {
107   removeGroupObjects();
108   createGroupObjects();
109   checkAndSetAltitudeObject();
110
111   HYDROData_Entity::Update();
112 }
113
114 bool HYDROData_Obstacle::IsHas2dPrs() const
115 {
116   return true;
117 }
118
119 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
120 {
121   return getTopShape();
122 }
123
124 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
125 {
126   return getShape3D();
127 }
128
129 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
130 {
131   TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
132   HYDROData_ArtificialObject::SetShape3D( theShape );
133   HYDROData_ArtificialObject::SetTopShape( aShape2d );
134 }
135
136 QColor HYDROData_Obstacle::DefaultFillingColor()
137 {
138   return QColor( Qt::yellow );
139 }
140
141 QColor HYDROData_Obstacle::DefaultBorderColor()
142 {
143   return QColor( Qt::transparent );
144 }
145
146 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
147 {
148   // Check the file existence
149   QFileInfo aFileInfo( theFilePath );
150   if ( !aFileInfo.exists() ) {
151     return false;
152   }
153
154   bool aRes = false;
155   TopoDS_Shape aShape;
156
157   // Import file
158   QString aFileSuf = aFileInfo.suffix().toLower();
159   if ( aFileSuf == "brep" ) {
160     aShape = ImportBREP( theFilePath );
161   } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
162     aShape = ImportIGES( theFilePath );
163   } else if ( aFileSuf == "step" ) {
164     aShape = ImportSTEP( theFilePath );
165   }
166  
167   // Check the result shape
168   aRes = !aShape.IsNull();
169
170   // Set shape to the obstacle in case of success
171   if ( aRes ) {
172     SetShape3D( aShape );
173     SetFilePath( theFilePath );
174   }
175
176   return aRes;
177 }
178
179 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
180 {
181   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
182   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
183 }
184
185 QString HYDROData_Obstacle::GetFilePath() const
186 {
187   QString aRes;
188
189   TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
190   if ( !aLabel.IsNull() )
191   {
192     Handle(TDataStd_AsciiString) anAsciiStr;
193     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
194       aRes = QString( anAsciiStr->Get().ToCString() );
195   }
196
197   return aRes;
198 }
199
200 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
201 {
202   bool aRes = false;
203
204   if ( theIOR.IsEmpty() ) {
205     return aRes;
206   }
207   
208   int aDocId = -1;
209   if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
210     return aRes;
211   }
212
213   // TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
214   TopoDS_Shape aShape = HYDROData_GeomTool::GetShapeFromIOR( aDocId, theIOR.ToCString() );
215
216   if ( !aShape.IsNull() ) {
217     SetShape3D( aShape );
218     aRes = true;
219   }
220   
221   return aRes;
222 }
223
224 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
225 {
226   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
227 }
228
229 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
230 {
231   TCollection_AsciiString aRes;
232
233   TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
234   if ( !aLabel.IsNull() )
235   {
236     Handle(TDataStd_AsciiString) anAsciiStr;
237     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
238       aRes = anAsciiStr->Get();
239   }
240
241   return aRes;
242 }
243
244 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
245 {
246   TCollection_AsciiString aRes;
247
248   int aDocId = -1;
249   if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
250     return aRes;
251   }
252
253   SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( aDocId );
254
255   if ( !aDSStudy->_is_nil() ) {
256     TCollection_AsciiString anEntry = GetGeomObjectEntry();
257     SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
258     if ( !aSObject->_is_nil() ) {
259       aRes = TCollection_AsciiString( aSObject->GetName() );
260     }
261   }
262
263   return aRes;
264 }
265
266 void HYDROData_Obstacle::Translate( const double theDx,
267                                     const double theDy,
268                                     const double theDz )
269 {
270   TopoDS_Shape aShape3D = GetShape3D();
271   
272   TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
273   if ( aTranslatedShape3D.IsNull() )
274     return;
275
276   SetShape3D( aTranslatedShape3D );
277
278   setTranslation( theDx, theDy, theDz );
279 }
280
281 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
282 {
283   TopoDS_Shape aResShape;
284
285   BRep_Builder aBrepBuilder;
286   BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
287
288   return aResShape;
289 }
290
291 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
292 {
293   TopoDS_Shape aResShape;
294
295   // Set "C" numeric locale to save numbers correctly
296   Kernel_Utils::Localizer loc;
297
298   IGESControl_Reader aReader;
299
300   Interface_Static::SetCVal("xstep.cascade.unit","M");
301
302   try {
303     OCC_CATCH_SIGNALS;
304
305     IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
306
307     if (status == IFSelect_RetDone) {
308       // Rescale units
309       Handle(IGESData_IGESModel) aModel =
310         Handle(IGESData_IGESModel)::DownCast(aReader.Model());
311       if (!aModel.IsNull()) {
312         IGESData_GlobalSection aGS = aModel->GlobalSection();
313         aGS.SetUnitFlag(6);
314         aModel->SetGlobalSection(aGS);
315       }
316     
317       aReader.ClearShapes();
318       aReader.TransferRoots();
319
320       aResShape = aReader.OneShape();
321     } 
322     else {
323       aResShape.Nullify();
324     }
325   }
326   catch(Standard_Failure) {
327     aResShape.Nullify();
328   }
329
330   return aResShape;
331 }
332
333 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
334 {
335   TopoDS_Shape aResShape;
336
337   // Set "C" numeric locale to save numbers correctly
338   Kernel_Utils::Localizer loc;
339
340   STEPControl_Reader aReader;
341
342   // Convert to METERS
343   Interface_Static::SetCVal("xstep.cascade.unit","M");
344   Interface_Static::SetIVal("read.step.ideas", 1);
345   Interface_Static::SetIVal("read.step.nonmanifold", 1);
346
347   BRep_Builder B;
348   TopoDS_Compound compound;
349   B.MakeCompound(compound);
350
351   try {
352     OCC_CATCH_SIGNALS;
353
354     IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
355
356     if (status == IFSelect_RetDone) {
357       // Rescale units
358       // set UnitFlag to units from file
359       TColStd_SequenceOfAsciiString anUnitLengthNames;
360       TColStd_SequenceOfAsciiString anUnitAngleNames;
361       TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
362       aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
363       if (anUnitLengthNames.Length() > 0) {
364         TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
365         if (aLenUnits == "millimetre")
366           Interface_Static::SetCVal("xstep.cascade.unit", "MM");
367         else if (aLenUnits == "centimetre")
368           Interface_Static::SetCVal("xstep.cascade.unit", "CM");
369         else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
370           Interface_Static::SetCVal("xstep.cascade.unit", "M");
371         else if (aLenUnits == "INCH")
372           Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
373         else {
374           // The file contains not supported units
375           return aResShape;
376         }
377       }
378         
379       Standard_Boolean failsonly = Standard_False;
380       aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
381
382       // Root transfers
383       Standard_Integer nbr = aReader.NbRootsForTransfer();
384       aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
385
386       for (Standard_Integer n = 1; n <= nbr; n++) {
387         Standard_Boolean ok = aReader.TransferRoot(n);
388         // Collecting resulting entities
389         Standard_Integer nbs = aReader.NbShapes();
390         if (!ok || nbs == 0) {
391           continue; // skip empty root
392         } 
393         else if (nbr == 1 && nbs == 1) { // For a single entity
394           aResShape = aReader.Shape(1);
395           // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
396           // It should be removed after patching OCCT for bug OCC22436
397           // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
398           if (aResShape.ShapeType() == TopAbs_COMPOUND) {
399             int nbSub1 = 0;
400             TopoDS_Shape currShape;
401             TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
402             for (; It.More(); It.Next()) {
403               nbSub1++;
404               currShape = It.Value();
405             }
406             if (nbSub1 == 1)
407               aResShape = currShape;
408           }
409           // END workaround
410           break;
411         }
412
413         for (Standard_Integer i = 1; i <= nbs; i++) {
414           TopoDS_Shape aShape = aReader.Shape(i);
415           if (aShape.IsNull()) {
416             continue;
417           }
418           else {
419             B.Add(compound, aShape);
420           }
421         }
422       }
423   
424       if (aResShape.IsNull())
425         aResShape = compound;
426
427       // Check if any BRep entity has been read, there must be at least a vertex
428       if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
429         // No geometrical data in the imported file
430         return TopoDS_Shape();
431       }
432     }
433     else {
434       aResShape.Nullify();
435     }
436   }
437   catch (Standard_Failure) {
438     aResShape.Nullify();
439   }
440
441   return aResShape;
442 }
443
444 QColor HYDROData_Obstacle::getDefaultFillingColor() const
445 {
446   return DefaultFillingColor();
447 }
448
449 QColor HYDROData_Obstacle::getDefaultBorderColor() const
450 {
451   return DefaultBorderColor();
452 }
453
454 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
455 {
456   return KIND_OBSTACLE_ALTITUDE;
457 }
458
459 void HYDROData_Obstacle::createGroupObjects()
460 {
461   TopoDS_Shape anObstacleShape = GetTopShape();
462   if ( !anObstacleShape.IsNull() )
463   {
464     TopTools_SequenceOfShape aWireEdges;
465     HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
466     if ( !aWireEdges.IsEmpty() )
467     {
468       QString aWireGroupName = GetName() + "_Outer_Wire";
469
470       Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
471       anExtWireGroup->SetName( aWireGroupName );
472      
473       anExtWireGroup->SetShapes( aWireEdges );
474     }
475   }
476 }
477
478 void HYDROData_Obstacle::setTranslation( const double theDx, 
479                                          const double theDy, 
480                                          const double theDz )
481 {
482   TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
483
484   double aCurDx = theDx;
485   double aCurDy = theDy;
486   double aCurDz = theDz;
487
488   double aPrevDx, aPrevDy, aPrevDz;
489   if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
490   {
491     aCurDx += aPrevDx;
492     aCurDy += aPrevDy;
493     aCurDz += aPrevDz;
494   }
495   
496   Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
497   aCoeffsArray->SetValue( 1, aCurDx );
498   aCoeffsArray->SetValue( 2, aCurDy );
499   aCoeffsArray->SetValue( 3, aCurDz );
500 }
501
502 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
503 {
504   theDx = theDy = theDz = 0.0;
505
506   TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
507   if ( aLabel.IsNull() )
508     return false;
509
510   Handle(TDataStd_RealArray) aCoeffsArray;
511   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
512     return false;
513
514   theDx = aCoeffsArray->Value( 1 );
515   theDy = aCoeffsArray->Value( 2 );
516   theDz = aCoeffsArray->Value( 3 );
517
518   return true;
519 }
520
521 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
522 {
523   Translate( theDx, theDy, 0 );
524 }
525