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