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