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