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