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