Salome HOME
7d0469ca666487a7b60f89e18154611947cab1e2
[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 #include <TDataStd_RealArray.hxx>
30
31 #include <TColStd_SequenceOfAsciiString.hxx>
32
33 #include <TopExp_Explorer.hxx>
34
35 #include <QColor>
36 #include <QFile>
37 #include <QFileInfo>
38 #include <QStringList>
39
40 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
41
42 #include <HYDROData_Projection.h>
43
44 #include <SALOMEDSClient.hxx>
45 #include <SALOME_NamingService.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   if ( theIOR.IsEmpty() )
207     return false;
208
209   TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
210   if ( aShape.IsNull() )
211     return false;
212
213   SetShape3D( aShape );
214   
215   return true;
216 }
217
218 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
219 {
220   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
221 }
222
223 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
224 {
225   TCollection_AsciiString aRes;
226
227   TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
228   if ( !aLabel.IsNull() )
229   {
230     Handle(TDataStd_AsciiString) anAsciiStr;
231     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
232       aRes = anAsciiStr->Get();
233   }
234
235   return aRes;
236 }
237
238 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
239 {
240   TCollection_AsciiString aRes;
241
242   int aDocId = -1;
243   if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
244     return aRes;
245   }
246
247   int argc = 0;
248   char** argv = 0;
249   CORBA::ORB_var anORB = CORBA::ORB_init( argc, argv, "omniORB4"/*CORBA::ORB_ID*/ );
250   SALOME_NamingService aNameService( anORB );
251   
252   CORBA::Object_var aSMObject = aNameService.Resolve("/myStudyManager");
253   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
254   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID( aDocId );
255
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   return aRes;
263 }
264
265 void HYDROData_Obstacle::Translate( const double theDx,
266                                     const double theDy,
267                                     const double theDz )
268 {
269   TopoDS_Shape aShape3D = GetShape3D();
270   
271   TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
272   if ( aTranslatedShape3D.IsNull() )
273     return;
274
275   SetShape3D( aTranslatedShape3D );
276
277   setTranslation( theDx, theDy, theDz );
278 }
279
280 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
281 {
282   TopoDS_Shape aResShape;
283
284   BRep_Builder aBrepBuilder;
285   BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
286
287   return aResShape;
288 }
289
290 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
291 {
292   TopoDS_Shape aResShape;
293
294   // Set "C" numeric locale to save numbers correctly
295   Kernel_Utils::Localizer loc;
296
297   IGESControl_Reader aReader;
298
299   Interface_Static::SetCVal("xstep.cascade.unit","M");
300
301   try {
302     OCC_CATCH_SIGNALS;
303
304     IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
305
306     if (status == IFSelect_RetDone) {
307       // Rescale units
308       Handle(IGESData_IGESModel) aModel =
309         Handle(IGESData_IGESModel)::DownCast(aReader.Model());
310       if (!aModel.IsNull()) {
311         IGESData_GlobalSection aGS = aModel->GlobalSection();
312         aGS.SetUnitFlag(6);
313         aModel->SetGlobalSection(aGS);
314       }
315     
316       aReader.ClearShapes();
317       aReader.TransferRoots();
318
319       aResShape = aReader.OneShape();
320     } 
321     else {
322       aResShape.Nullify();
323     }
324   }
325   catch(Standard_Failure) {
326     aResShape.Nullify();
327   }
328
329   return aResShape;
330 }
331
332 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
333 {
334   TopoDS_Shape aResShape;
335
336   // Set "C" numeric locale to save numbers correctly
337   Kernel_Utils::Localizer loc;
338
339   STEPControl_Reader aReader;
340
341   // Convert to METERS
342   Interface_Static::SetCVal("xstep.cascade.unit","M");
343   Interface_Static::SetIVal("read.step.ideas", 1);
344   Interface_Static::SetIVal("read.step.nonmanifold", 1);
345
346   BRep_Builder B;
347   TopoDS_Compound compound;
348   B.MakeCompound(compound);
349
350   try {
351     OCC_CATCH_SIGNALS;
352
353     IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
354
355     if (status == IFSelect_RetDone) {
356       // Rescale units
357       // set UnitFlag to units from file
358       TColStd_SequenceOfAsciiString anUnitLengthNames;
359       TColStd_SequenceOfAsciiString anUnitAngleNames;
360       TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
361       aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
362       if (anUnitLengthNames.Length() > 0) {
363         TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
364         if (aLenUnits == "millimetre")
365           Interface_Static::SetCVal("xstep.cascade.unit", "MM");
366         else if (aLenUnits == "centimetre")
367           Interface_Static::SetCVal("xstep.cascade.unit", "CM");
368         else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
369           Interface_Static::SetCVal("xstep.cascade.unit", "M");
370         else if (aLenUnits == "INCH")
371           Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
372         else {
373           // The file contains not supported units
374           return aResShape;
375         }
376       }
377         
378       Standard_Boolean failsonly = Standard_False;
379       aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
380
381       // Root transfers
382       Standard_Integer nbr = aReader.NbRootsForTransfer();
383       aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
384
385       for (Standard_Integer n = 1; n <= nbr; n++) {
386         Standard_Boolean ok = aReader.TransferRoot(n);
387         // Collecting resulting entities
388         Standard_Integer nbs = aReader.NbShapes();
389         if (!ok || nbs == 0) {
390           continue; // skip empty root
391         } 
392         else if (nbr == 1 && nbs == 1) { // For a single entity
393           aResShape = aReader.Shape(1);
394           // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
395           // It should be removed after patching OCCT for bug OCC22436
396           // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
397           if (aResShape.ShapeType() == TopAbs_COMPOUND) {
398             int nbSub1 = 0;
399             TopoDS_Shape currShape;
400             TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
401             for (; It.More(); It.Next()) {
402               nbSub1++;
403               currShape = It.Value();
404             }
405             if (nbSub1 == 1)
406               aResShape = currShape;
407           }
408           // END workaround
409           break;
410         }
411
412         for (Standard_Integer i = 1; i <= nbs; i++) {
413           TopoDS_Shape aShape = aReader.Shape(i);
414           if (aShape.IsNull()) {
415             continue;
416           }
417           else {
418             B.Add(compound, aShape);
419           }
420         }
421       }
422   
423       if (aResShape.IsNull())
424         aResShape = compound;
425
426       // Check if any BRep entity has been read, there must be at least a vertex
427       if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
428         // No geometrical data in the imported file
429         return TopoDS_Shape();
430       }
431     }
432     else {
433       aResShape.Nullify();
434     }
435   }
436   catch (Standard_Failure) {
437     aResShape.Nullify();
438   }
439
440   return aResShape;
441 }
442
443 QColor HYDROData_Obstacle::getDefaultFillingColor() const
444 {
445   return DefaultFillingColor();
446 }
447
448 QColor HYDROData_Obstacle::getDefaultBorderColor() const
449 {
450   return DefaultBorderColor();
451 }
452
453 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
454 {
455   return KIND_OBSTACLE_ALTITUDE;
456 }
457
458 void HYDROData_Obstacle::createGroupObjects()
459 {
460   TopoDS_Shape anObstacleShape = GetTopShape();
461   if ( !anObstacleShape.IsNull() )
462   {
463     TopTools_SequenceOfShape aWireEdges;
464     HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
465     if ( !aWireEdges.IsEmpty() )
466     {
467       QString aWireGroupName = GetName() + "_Outer_Wire";
468
469       Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
470       anExtWireGroup->SetName( aWireGroupName );
471      
472       anExtWireGroup->SetShapes( aWireEdges );
473     }
474   }
475 }
476
477 void HYDROData_Obstacle::setTranslation( const double theDx, 
478                                          const double theDy, 
479                                          const double theDz )
480 {
481   TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
482
483   double aCurDx = theDx;
484   double aCurDy = theDy;
485   double aCurDz = theDz;
486
487   double aPrevDx, aPrevDy, aPrevDz;
488   if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
489   {
490     aCurDx += aPrevDx;
491     aCurDy += aPrevDy;
492     aCurDz += aPrevDz;
493   }
494   
495   Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
496   aCoeffsArray->SetValue( 1, aCurDx );
497   aCoeffsArray->SetValue( 2, aCurDy );
498   aCoeffsArray->SetValue( 3, aCurDz );
499 }
500
501 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
502 {
503   theDx = theDy = theDz = 0.0;
504
505   TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
506   if ( aLabel.IsNull() )
507     return false;
508
509   Handle(TDataStd_RealArray) aCoeffsArray;
510   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
511     return false;
512
513   theDx = aCoeffsArray->Value( 1 );
514   theDy = aCoeffsArray->Value( 2 );
515   theDz = aCoeffsArray->Value( 3 );
516
517   return true;
518 }
519
520 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
521 {
522   Translate( theDx, theDy, 0 );
523 }
524