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