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"
9 #include <Basics_Utils.hxx>
11 #include <BRepTools.hxx>
12 #include <BRep_Builder.hxx>
16 #include <IGESControl_Reader.hxx>
17 #include <IGESData_IGESModel.hxx>
19 #include <STEPControl_Reader.hxx>
21 #include <Interface_Static.hxx>
24 #include <TopoDS_Iterator.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopoDS_Edge.hxx>
28 #include <TDataStd_AsciiString.hxx>
29 #include <TDataStd_RealArray.hxx>
31 #include <TColStd_SequenceOfAsciiString.hxx>
33 #include <TopExp_Explorer.hxx>
38 #include <QStringList>
40 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
42 #include <HYDROData_Projection.h>
44 #include <SALOMEDSClient.hxx>
45 #include <SALOME_NamingService.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 )
206 if ( theIOR.IsEmpty() )
209 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
210 if ( aShape.IsNull() )
213 SetShape3D( aShape );
218 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
220 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
223 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
225 TCollection_AsciiString aRes;
227 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
228 if ( !aLabel.IsNull() )
230 Handle(TDataStd_AsciiString) anAsciiStr;
231 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
232 aRes = anAsciiStr->Get();
238 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
240 TCollection_AsciiString aRes;
243 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
249 CORBA::ORB_var anORB = CORBA::ORB_init( argc, argv, "omniORB4"/*CORBA::ORB_ID*/ );
250 SALOME_NamingService aNameService( anORB );
252 CORBA::Object_var aSMObject = aNameService.Resolve("/myStudyManager");
253 SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
254 SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID( aDocId );
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() );
265 void HYDROData_Obstacle::Translate( const double theDx,
269 TopoDS_Shape aShape3D = GetShape3D();
271 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
272 if ( aTranslatedShape3D.IsNull() )
275 SetShape3D( aTranslatedShape3D );
277 setTranslation( theDx, theDy, theDz );
280 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
282 TopoDS_Shape aResShape;
284 BRep_Builder aBrepBuilder;
285 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
290 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
292 TopoDS_Shape aResShape;
294 // Set "C" numeric locale to save numbers correctly
295 Kernel_Utils::Localizer loc;
297 IGESControl_Reader aReader;
299 Interface_Static::SetCVal("xstep.cascade.unit","M");
304 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
306 if (status == IFSelect_RetDone) {
308 Handle(IGESData_IGESModel) aModel =
309 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
310 if (!aModel.IsNull()) {
311 IGESData_GlobalSection aGS = aModel->GlobalSection();
313 aModel->SetGlobalSection(aGS);
316 aReader.ClearShapes();
317 aReader.TransferRoots();
319 aResShape = aReader.OneShape();
325 catch(Standard_Failure) {
332 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
334 TopoDS_Shape aResShape;
336 // Set "C" numeric locale to save numbers correctly
337 Kernel_Utils::Localizer loc;
339 STEPControl_Reader aReader;
342 Interface_Static::SetCVal("xstep.cascade.unit","M");
343 Interface_Static::SetIVal("read.step.ideas", 1);
344 Interface_Static::SetIVal("read.step.nonmanifold", 1);
347 TopoDS_Compound compound;
348 B.MakeCompound(compound);
353 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
355 if (status == IFSelect_RetDone) {
357 // set UnitFlag to units from file
358 TColStd_SequenceOfAsciiString anUnitLengthNames;
359 TColStd_SequenceOfAsciiString anUnitAngleNames;
360 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
361 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
362 if (anUnitLengthNames.Length() > 0) {
363 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
364 if (aLenUnits == "millimetre")
365 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
366 else if (aLenUnits == "centimetre")
367 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
368 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
369 Interface_Static::SetCVal("xstep.cascade.unit", "M");
370 else if (aLenUnits == "INCH")
371 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
373 // The file contains not supported units
378 Standard_Boolean failsonly = Standard_False;
379 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
382 Standard_Integer nbr = aReader.NbRootsForTransfer();
383 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
385 for (Standard_Integer n = 1; n <= nbr; n++) {
386 Standard_Boolean ok = aReader.TransferRoot(n);
387 // Collecting resulting entities
388 Standard_Integer nbs = aReader.NbShapes();
389 if (!ok || nbs == 0) {
390 continue; // skip empty root
392 else if (nbr == 1 && nbs == 1) { // For a single entity
393 aResShape = aReader.Shape(1);
394 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
395 // It should be removed after patching OCCT for bug OCC22436
396 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
397 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
399 TopoDS_Shape currShape;
400 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
401 for (; It.More(); It.Next()) {
403 currShape = It.Value();
406 aResShape = currShape;
412 for (Standard_Integer i = 1; i <= nbs; i++) {
413 TopoDS_Shape aShape = aReader.Shape(i);
414 if (aShape.IsNull()) {
418 B.Add(compound, aShape);
423 if (aResShape.IsNull())
424 aResShape = compound;
426 // Check if any BRep entity has been read, there must be at least a vertex
427 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
428 // No geometrical data in the imported file
429 return TopoDS_Shape();
436 catch (Standard_Failure) {
443 QColor HYDROData_Obstacle::getDefaultFillingColor() const
445 return DefaultFillingColor();
448 QColor HYDROData_Obstacle::getDefaultBorderColor() const
450 return DefaultBorderColor();
453 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
455 return KIND_OBSTACLE_ALTITUDE;
458 void HYDROData_Obstacle::createGroupObjects()
460 TopoDS_Shape anObstacleShape = GetTopShape();
461 if ( !anObstacleShape.IsNull() )
463 TopTools_SequenceOfShape aWireEdges;
464 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
465 if ( !aWireEdges.IsEmpty() )
467 QString aWireGroupName = GetName() + "_Outer_Wire";
469 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
470 anExtWireGroup->SetName( aWireGroupName );
472 anExtWireGroup->SetShapes( aWireEdges );
477 void HYDROData_Obstacle::setTranslation( const double theDx,
481 TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
483 double aCurDx = theDx;
484 double aCurDy = theDy;
485 double aCurDz = theDz;
487 double aPrevDx, aPrevDy, aPrevDz;
488 if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
495 Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
496 aCoeffsArray->SetValue( 1, aCurDx );
497 aCoeffsArray->SetValue( 2, aCurDy );
498 aCoeffsArray->SetValue( 3, aCurDz );
501 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
503 theDx = theDy = theDz = 0.0;
505 TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
506 if ( aLabel.IsNull() )
509 Handle(TDataStd_RealArray) aCoeffsArray;
510 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
513 theDx = aCoeffsArray->Value( 1 );
514 theDy = aCoeffsArray->Value( 2 );
515 theDz = aCoeffsArray->Value( 3 );
520 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
522 Translate( theDx, theDy, 0 );