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