2 #include "HYDROData_Obstacle.h"
4 #include "HYDROData_Document.h"
5 #include "HYDROData_ShapesGroup.h"
6 #include "HYDROData_ShapesTool.h"
7 #include "HYDROData_Tool.h"
8 #include "HYDROData_GeomTool.h"
10 #include <Basics_Utils.hxx>
12 #include <BRepTools.hxx>
13 #include <BRep_Builder.hxx>
15 #include <IGESControl_Reader.hxx>
16 #include <IGESData_IGESModel.hxx>
18 #include <STEPControl_Reader.hxx>
20 #include <Interface_Static.hxx>
23 #include <TopoDS_Iterator.hxx>
24 #include <TopoDS_Shape.hxx>
25 #include <TopoDS_Edge.hxx>
27 #include <TDataStd_AsciiString.hxx>
28 #include <TDataStd_RealArray.hxx>
30 #include <TColStd_SequenceOfAsciiString.hxx>
32 #include <TopExp_Explorer.hxx>
37 #include <QStringList>
39 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
41 #include <HYDROData_Projection.h>
43 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
44 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
47 HYDROData_Obstacle::HYDROData_Obstacle()
48 : HYDROData_ArtificialObject()
52 HYDROData_Obstacle::~HYDROData_Obstacle()
56 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
58 QStringList aResList = dumpObjectCreation( theTreatedObjects );
60 QString anObstacleName = GetObjPyName();
62 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
63 QString aFilePath = GetFilePath();
65 if ( !aGeomObjectEntry.IsEmpty() )
67 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
68 // aResList << QString( "%1 = theStudy.FindObjectID( \"%2\" );" )
69 // .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
70 QString aGeomObjectName( GetGeomObjectName().ToCString() );
71 if ( !aGeomObjectName.isEmpty() ) {
72 aResList << QString( "%1 = theStudy.FindObjectByName( \"%2\", \"GEOM\" )[0];" )
73 .arg( aSalomeObjName ).arg( aGeomObjectName );
75 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
76 .arg( anObstacleName ).arg( aSalomeObjName );
78 aResList << QString( "%1.SetGeomObjectEntry( %2.GetID() );" )
79 .arg( anObstacleName ).arg( aSalomeObjName );
81 aResList << QString( "" );
83 else if ( !aFilePath.isEmpty() )
85 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
86 .arg( anObstacleName ).arg( aFilePath );
87 aResList << QString( "" );
90 // Write the translation points
92 if ( getTranslation( aDx, aDy, aDz ) )
94 aResList << QString( "%1.Translate( %2, %3, %4 );" )
95 .arg( anObstacleName ).arg( aDx ).arg( aDy ).arg( aDz );
96 aResList << QString( "" );
99 aResList << QString( "%1.Update();" ).arg( anObstacleName );
100 aResList << QString( "" );
105 void HYDROData_Obstacle::Update()
107 removeGroupObjects();
108 createGroupObjects();
109 checkAndSetAltitudeObject();
111 HYDROData_Entity::Update();
114 bool HYDROData_Obstacle::IsHas2dPrs() const
119 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
121 return getTopShape();
124 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
129 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
131 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
132 HYDROData_ArtificialObject::SetShape3D( theShape );
133 HYDROData_ArtificialObject::SetTopShape( aShape2d );
136 QColor HYDROData_Obstacle::DefaultFillingColor()
138 return QColor( Qt::yellow );
141 QColor HYDROData_Obstacle::DefaultBorderColor()
143 return QColor( Qt::transparent );
146 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
148 // Check the file existence
149 QFileInfo aFileInfo( theFilePath );
150 if ( !aFileInfo.exists() ) {
158 QString aFileSuf = aFileInfo.suffix().toLower();
159 if ( aFileSuf == "brep" ) {
160 aShape = ImportBREP( theFilePath );
161 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
162 aShape = ImportIGES( theFilePath );
163 } else if ( aFileSuf == "step" ) {
164 aShape = ImportSTEP( theFilePath );
167 // Check the result shape
168 aRes = !aShape.IsNull();
170 // Set shape to the obstacle in case of success
172 SetShape3D( aShape );
173 SetFilePath( theFilePath );
179 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
181 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
182 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
185 QString HYDROData_Obstacle::GetFilePath() const
189 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
190 if ( !aLabel.IsNull() )
192 Handle(TDataStd_AsciiString) anAsciiStr;
193 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
194 aRes = QString( anAsciiStr->Get().ToCString() );
200 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
204 if ( theIOR.IsEmpty() ) {
209 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
213 // TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
214 TopoDS_Shape aShape = HYDROData_GeomTool::GetShapeFromIOR( aDocId, theIOR.ToCString() );
216 if ( !aShape.IsNull() ) {
217 SetShape3D( aShape );
224 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
226 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
229 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
231 TCollection_AsciiString aRes;
233 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
234 if ( !aLabel.IsNull() )
236 Handle(TDataStd_AsciiString) anAsciiStr;
237 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
238 aRes = anAsciiStr->Get();
244 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
246 TCollection_AsciiString aRes;
249 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
253 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( aDocId );
255 if ( !aDSStudy->_is_nil() ) {
256 TCollection_AsciiString anEntry = GetGeomObjectEntry();
257 SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
258 if ( !aSObject->_is_nil() ) {
259 aRes = TCollection_AsciiString( aSObject->GetName() );
266 void HYDROData_Obstacle::Translate( const double theDx,
270 TopoDS_Shape aShape3D = GetShape3D();
272 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
273 if ( aTranslatedShape3D.IsNull() )
276 SetShape3D( aTranslatedShape3D );
278 setTranslation( theDx, theDy, theDz );
281 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
283 TopoDS_Shape aResShape;
285 BRep_Builder aBrepBuilder;
286 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
291 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
293 TopoDS_Shape aResShape;
295 // Set "C" numeric locale to save numbers correctly
296 Kernel_Utils::Localizer loc;
298 IGESControl_Reader aReader;
300 Interface_Static::SetCVal("xstep.cascade.unit","M");
305 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
307 if (status == IFSelect_RetDone) {
309 Handle(IGESData_IGESModel) aModel =
310 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
311 if (!aModel.IsNull()) {
312 IGESData_GlobalSection aGS = aModel->GlobalSection();
314 aModel->SetGlobalSection(aGS);
317 aReader.ClearShapes();
318 aReader.TransferRoots();
320 aResShape = aReader.OneShape();
326 catch(Standard_Failure) {
333 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
335 TopoDS_Shape aResShape;
337 // Set "C" numeric locale to save numbers correctly
338 Kernel_Utils::Localizer loc;
340 STEPControl_Reader aReader;
343 Interface_Static::SetCVal("xstep.cascade.unit","M");
344 Interface_Static::SetIVal("read.step.ideas", 1);
345 Interface_Static::SetIVal("read.step.nonmanifold", 1);
348 TopoDS_Compound compound;
349 B.MakeCompound(compound);
354 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
356 if (status == IFSelect_RetDone) {
358 // set UnitFlag to units from file
359 TColStd_SequenceOfAsciiString anUnitLengthNames;
360 TColStd_SequenceOfAsciiString anUnitAngleNames;
361 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
362 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
363 if (anUnitLengthNames.Length() > 0) {
364 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
365 if (aLenUnits == "millimetre")
366 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
367 else if (aLenUnits == "centimetre")
368 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
369 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
370 Interface_Static::SetCVal("xstep.cascade.unit", "M");
371 else if (aLenUnits == "INCH")
372 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
374 // The file contains not supported units
379 Standard_Boolean failsonly = Standard_False;
380 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
383 Standard_Integer nbr = aReader.NbRootsForTransfer();
384 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
386 for (Standard_Integer n = 1; n <= nbr; n++) {
387 Standard_Boolean ok = aReader.TransferRoot(n);
388 // Collecting resulting entities
389 Standard_Integer nbs = aReader.NbShapes();
390 if (!ok || nbs == 0) {
391 continue; // skip empty root
393 else if (nbr == 1 && nbs == 1) { // For a single entity
394 aResShape = aReader.Shape(1);
395 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
396 // It should be removed after patching OCCT for bug OCC22436
397 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
398 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
400 TopoDS_Shape currShape;
401 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
402 for (; It.More(); It.Next()) {
404 currShape = It.Value();
407 aResShape = currShape;
413 for (Standard_Integer i = 1; i <= nbs; i++) {
414 TopoDS_Shape aShape = aReader.Shape(i);
415 if (aShape.IsNull()) {
419 B.Add(compound, aShape);
424 if (aResShape.IsNull())
425 aResShape = compound;
427 // Check if any BRep entity has been read, there must be at least a vertex
428 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
429 // No geometrical data in the imported file
430 return TopoDS_Shape();
437 catch (Standard_Failure) {
444 QColor HYDROData_Obstacle::getDefaultFillingColor() const
446 return DefaultFillingColor();
449 QColor HYDROData_Obstacle::getDefaultBorderColor() const
451 return DefaultBorderColor();
454 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
456 return KIND_OBSTACLE_ALTITUDE;
459 void HYDROData_Obstacle::createGroupObjects()
461 TopoDS_Shape anObstacleShape = GetTopShape();
462 if ( !anObstacleShape.IsNull() )
464 TopTools_SequenceOfShape aWireEdges;
465 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
466 if ( !aWireEdges.IsEmpty() )
468 QString aWireGroupName = GetName() + "_Outer_Wire";
470 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
471 anExtWireGroup->SetName( aWireGroupName );
473 anExtWireGroup->SetShapes( aWireEdges );
478 void HYDROData_Obstacle::setTranslation( const double theDx,
482 TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
484 double aCurDx = theDx;
485 double aCurDy = theDy;
486 double aCurDz = theDz;
488 double aPrevDx, aPrevDy, aPrevDz;
489 if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
496 Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
497 aCoeffsArray->SetValue( 1, aCurDx );
498 aCoeffsArray->SetValue( 2, aCurDy );
499 aCoeffsArray->SetValue( 3, aCurDz );
502 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
504 theDx = theDy = theDz = 0.0;
506 TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
507 if ( aLabel.IsNull() )
510 Handle(TDataStd_RealArray) aCoeffsArray;
511 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
514 theDx = aCoeffsArray->Value( 1 );
515 theDy = aCoeffsArray->Value( 2 );
516 theDz = aCoeffsArray->Value( 3 );
521 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
523 Translate( theDx, theDy, 0 );