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 #include <SALOMEDSClient.hxx>
44 #include <SALOME_NamingService.hxx>
45 #include <SALOME_LifeCycleCORBA.hxx>
47 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
48 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
51 HYDROData_Obstacle::HYDROData_Obstacle()
52 : HYDROData_ArtificialObject()
56 HYDROData_Obstacle::~HYDROData_Obstacle()
60 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
62 QStringList aResList = dumpObjectCreation( theTreatedObjects );
64 QString anObstacleName = GetObjPyName();
66 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
67 QString aFilePath = GetFilePath();
69 if ( !aGeomObjectEntry.IsEmpty() )
71 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
72 // aResList << QString( "%1 = theStudy.FindObjectID( \"%2\" );" )
73 // .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
74 QString aGeomObjectName( GetGeomObjectName().ToCString() );
75 if ( !aGeomObjectName.isEmpty() ) {
76 aResList << QString( "%1 = theStudy.FindObjectByName( \"%2\", \"GEOM\" )[0];" )
77 .arg( aSalomeObjName ).arg( aGeomObjectName );
79 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
80 .arg( anObstacleName ).arg( aSalomeObjName );
82 aResList << QString( "%1.SetGeomObjectEntry( %2.GetID() );" )
83 .arg( anObstacleName ).arg( aSalomeObjName );
85 aResList << QString( "" );
87 else if ( !aFilePath.isEmpty() )
89 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
90 .arg( anObstacleName ).arg( aFilePath );
91 aResList << QString( "" );
94 // Write the translation points
96 if ( getTranslation( aDx, aDy, aDz ) )
98 aResList << QString( "%1.Translate( %2, %3, %4 );" )
99 .arg( anObstacleName ).arg( aDx ).arg( aDy ).arg( aDz );
100 aResList << QString( "" );
103 aResList << QString( "%1.Update();" ).arg( anObstacleName );
104 aResList << QString( "" );
109 void HYDROData_Obstacle::Update()
111 removeGroupObjects();
112 createGroupObjects();
113 checkAndSetAltitudeObject();
115 HYDROData_Entity::Update();
118 bool HYDROData_Obstacle::IsHas2dPrs() const
123 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
125 return getTopShape();
128 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
133 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
135 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
136 HYDROData_ArtificialObject::SetShape3D( theShape );
137 HYDROData_ArtificialObject::SetTopShape( aShape2d );
140 QColor HYDROData_Obstacle::DefaultFillingColor()
142 return QColor( Qt::yellow );
145 QColor HYDROData_Obstacle::DefaultBorderColor()
147 return QColor( Qt::transparent );
150 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
152 // Check the file existence
153 QFileInfo aFileInfo( theFilePath );
154 if ( !aFileInfo.exists() ) {
162 QString aFileSuf = aFileInfo.suffix().toLower();
163 if ( aFileSuf == "brep" ) {
164 aShape = ImportBREP( theFilePath );
165 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
166 aShape = ImportIGES( theFilePath );
167 } else if ( aFileSuf == "step" ) {
168 aShape = ImportSTEP( theFilePath );
171 // Check the result shape
172 aRes = !aShape.IsNull();
174 // Set shape to the obstacle in case of success
176 SetShape3D( aShape );
177 SetFilePath( theFilePath );
183 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
185 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
186 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
189 QString HYDROData_Obstacle::GetFilePath() const
193 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
194 if ( !aLabel.IsNull() )
196 Handle(TDataStd_AsciiString) anAsciiStr;
197 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
198 aRes = QString( anAsciiStr->Get().ToCString() );
204 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
208 if ( theIOR.IsEmpty() ) {
213 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
217 // TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
218 TopoDS_Shape aShape = HYDROData_GeomTool::GetShapeFromIOR( aDocId, theIOR.ToCString() );
220 if ( !aShape.IsNull() ) {
221 SetShape3D( aShape );
228 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
230 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
233 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
235 TCollection_AsciiString aRes;
237 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
238 if ( !aLabel.IsNull() )
240 Handle(TDataStd_AsciiString) anAsciiStr;
241 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
242 aRes = anAsciiStr->Get();
248 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
250 TCollection_AsciiString aRes;
253 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
257 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( aDocId );
259 if ( !aDSStudy->_is_nil() ) {
260 TCollection_AsciiString anEntry = GetGeomObjectEntry();
261 SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
262 if ( !aSObject->_is_nil() ) {
263 aRes = TCollection_AsciiString( aSObject->GetName() );
270 void HYDROData_Obstacle::Translate( const double theDx,
274 TopoDS_Shape aShape3D = GetShape3D();
276 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
277 if ( aTranslatedShape3D.IsNull() )
280 SetShape3D( aTranslatedShape3D );
282 setTranslation( theDx, theDy, theDz );
285 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
287 TopoDS_Shape aResShape;
289 BRep_Builder aBrepBuilder;
290 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
295 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
297 TopoDS_Shape aResShape;
299 // Set "C" numeric locale to save numbers correctly
300 Kernel_Utils::Localizer loc;
302 IGESControl_Reader aReader;
304 Interface_Static::SetCVal("xstep.cascade.unit","M");
309 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
311 if (status == IFSelect_RetDone) {
313 Handle(IGESData_IGESModel) aModel =
314 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
315 if (!aModel.IsNull()) {
316 IGESData_GlobalSection aGS = aModel->GlobalSection();
318 aModel->SetGlobalSection(aGS);
321 aReader.ClearShapes();
322 aReader.TransferRoots();
324 aResShape = aReader.OneShape();
330 catch(Standard_Failure) {
337 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
339 TopoDS_Shape aResShape;
341 // Set "C" numeric locale to save numbers correctly
342 Kernel_Utils::Localizer loc;
344 STEPControl_Reader aReader;
347 Interface_Static::SetCVal("xstep.cascade.unit","M");
348 Interface_Static::SetIVal("read.step.ideas", 1);
349 Interface_Static::SetIVal("read.step.nonmanifold", 1);
352 TopoDS_Compound compound;
353 B.MakeCompound(compound);
358 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
360 if (status == IFSelect_RetDone) {
362 // set UnitFlag to units from file
363 TColStd_SequenceOfAsciiString anUnitLengthNames;
364 TColStd_SequenceOfAsciiString anUnitAngleNames;
365 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
366 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
367 if (anUnitLengthNames.Length() > 0) {
368 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
369 if (aLenUnits == "millimetre")
370 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
371 else if (aLenUnits == "centimetre")
372 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
373 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
374 Interface_Static::SetCVal("xstep.cascade.unit", "M");
375 else if (aLenUnits == "INCH")
376 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
378 // The file contains not supported units
383 Standard_Boolean failsonly = Standard_False;
384 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
387 Standard_Integer nbr = aReader.NbRootsForTransfer();
388 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
390 for (Standard_Integer n = 1; n <= nbr; n++) {
391 Standard_Boolean ok = aReader.TransferRoot(n);
392 // Collecting resulting entities
393 Standard_Integer nbs = aReader.NbShapes();
394 if (!ok || nbs == 0) {
395 continue; // skip empty root
397 else if (nbr == 1 && nbs == 1) { // For a single entity
398 aResShape = aReader.Shape(1);
399 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
400 // It should be removed after patching OCCT for bug OCC22436
401 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
402 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
404 TopoDS_Shape currShape;
405 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
406 for (; It.More(); It.Next()) {
408 currShape = It.Value();
411 aResShape = currShape;
417 for (Standard_Integer i = 1; i <= nbs; i++) {
418 TopoDS_Shape aShape = aReader.Shape(i);
419 if (aShape.IsNull()) {
423 B.Add(compound, aShape);
428 if (aResShape.IsNull())
429 aResShape = compound;
431 // Check if any BRep entity has been read, there must be at least a vertex
432 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
433 // No geometrical data in the imported file
434 return TopoDS_Shape();
441 catch (Standard_Failure) {
448 QColor HYDROData_Obstacle::getDefaultFillingColor() const
450 return DefaultFillingColor();
453 QColor HYDROData_Obstacle::getDefaultBorderColor() const
455 return DefaultBorderColor();
458 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
460 return KIND_OBSTACLE_ALTITUDE;
463 void HYDROData_Obstacle::createGroupObjects()
465 TopoDS_Shape anObstacleShape = GetTopShape();
466 if ( !anObstacleShape.IsNull() )
468 TopTools_SequenceOfShape aWireEdges;
469 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
470 if ( !aWireEdges.IsEmpty() )
472 QString aWireGroupName = GetName() + "_Outer_Wire";
474 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
475 anExtWireGroup->SetName( aWireGroupName );
477 anExtWireGroup->SetShapes( aWireEdges );
482 void HYDROData_Obstacle::setTranslation( const double theDx,
486 TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
488 double aCurDx = theDx;
489 double aCurDy = theDy;
490 double aCurDz = theDz;
492 double aPrevDx, aPrevDy, aPrevDz;
493 if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
500 Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
501 aCoeffsArray->SetValue( 1, aCurDx );
502 aCoeffsArray->SetValue( 2, aCurDy );
503 aCoeffsArray->SetValue( 3, aCurDz );
506 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
508 theDx = theDy = theDz = 0.0;
510 TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
511 if ( aLabel.IsNull() )
514 Handle(TDataStd_RealArray) aCoeffsArray;
515 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
518 theDx = aCoeffsArray->Value( 1 );
519 theDy = aCoeffsArray->Value( 2 );
520 theDz = aCoeffsArray->Value( 3 );
525 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
527 Translate( theDx, theDy, 0 );