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