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