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