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.
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.
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
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "HYDROData_Obstacle.h"
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"
28 #include <Basics_Utils.hxx>
31 #include <BRepTools.hxx>
32 #include <BRep_Builder.hxx>
34 #include <IGESControl_Reader.hxx>
35 #include <IGESData_IGESModel.hxx>
37 #include <STEPControl_Reader.hxx>
39 #include <Interface_Static.hxx>
42 #include <TopoDS_Iterator.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Edge.hxx>
46 #include <TDataStd_AsciiString.hxx>
47 #include <TDataStd_RealArray.hxx>
49 #include <TColStd_SequenceOfAsciiString.hxx>
51 #include <TopExp_Explorer.hxx>
56 #include <QStringList>
58 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
60 #include <HYDROData_Projection.h>
62 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
63 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
66 HYDROData_Obstacle::HYDROData_Obstacle()
67 : HYDROData_ArtificialObject()
71 HYDROData_Obstacle::~HYDROData_Obstacle()
75 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
77 QStringList aResList = dumpObjectCreation( theTreatedObjects );
79 QString anObstacleName = GetObjPyName();
81 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
82 QString aFilePath = GetFilePath();
84 if ( !aGeomObjectEntry.IsEmpty() )
86 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
87 // aResList << QString( "%1 = theStudy.FindObjectID( \"%2\" );" )
88 // .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
89 QString aGeomObjectName( GetGeomObjectName().ToCString() );
90 if ( !aGeomObjectName.isEmpty() ) {
91 aResList << QString( "%1 = theStudy.FindObjectByName( \"%2\", \"GEOM\" )[0];" )
92 .arg( aSalomeObjName ).arg( aGeomObjectName );
94 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
95 .arg( anObstacleName ).arg( aSalomeObjName );
97 aResList << QString( "%1.SetGeomObjectEntry( %2.GetID() );" )
98 .arg( anObstacleName ).arg( aSalomeObjName );
100 aResList << QString( "" );
102 else if ( !aFilePath.isEmpty() )
104 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
105 .arg( anObstacleName ).arg( aFilePath );
106 aResList << QString( "" );
109 // Write the translation points
110 double aDx, aDy, aDz;
111 if ( getTranslation( aDx, aDy, aDz ) )
113 aResList << QString( "%1.Translate( %2, %3, %4 );" )
114 .arg( anObstacleName ).arg( aDx ).arg( aDy ).arg( aDz );
115 aResList << QString( "" );
118 aResList << QString( "%1.Update();" ).arg( anObstacleName );
119 aResList << QString( "" );
124 void HYDROData_Obstacle::Update()
126 removeGroupObjects();
127 createGroupObjects();
128 checkAndSetAltitudeObject();
130 HYDROData_Entity::Update();
133 bool HYDROData_Obstacle::IsHas2dPrs() const
138 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
140 return getTopShape();
143 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
148 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
150 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
151 HYDROData_ArtificialObject::SetShape3D( theShape );
152 HYDROData_ArtificialObject::SetTopShape( aShape2d );
155 QColor HYDROData_Obstacle::DefaultFillingColor()
157 return QColor( Qt::yellow );
160 QColor HYDROData_Obstacle::DefaultBorderColor()
162 return QColor( Qt::transparent );
165 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
167 // Check the file existence
168 QFileInfo aFileInfo( theFilePath );
169 if ( !aFileInfo.exists() ) {
177 QString aFileSuf = aFileInfo.suffix().toLower();
178 if ( aFileSuf == "brep" ) {
179 aShape = ImportBREP( theFilePath );
180 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
181 aShape = ImportIGES( theFilePath );
182 } else if ( aFileSuf == "step" ) {
183 aShape = ImportSTEP( theFilePath );
186 // Check the result shape
187 aRes = !aShape.IsNull();
189 // Set shape to the obstacle in case of success
191 SetShape3D( aShape );
192 SetFilePath( theFilePath );
198 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
200 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
201 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
204 QString HYDROData_Obstacle::GetFilePath() const
208 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
209 if ( !aLabel.IsNull() )
211 Handle(TDataStd_AsciiString) anAsciiStr;
212 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
213 aRes = QString( anAsciiStr->Get().ToCString() );
219 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
224 if ( theIOR.IsEmpty() ) {
229 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
233 // TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
234 TopoDS_Shape aShape = HYDROData_GeomTool::GetShapeFromIOR( aDocId, theIOR.ToCString() );
236 if ( !aShape.IsNull() ) {
237 SetShape3D( aShape );
245 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
247 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
250 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
252 TCollection_AsciiString aRes;
254 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
255 if ( !aLabel.IsNull() )
257 Handle(TDataStd_AsciiString) anAsciiStr;
258 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
259 aRes = anAsciiStr->Get();
265 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
267 TCollection_AsciiString aRes;
270 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
275 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( aDocId );
277 if ( !aDSStudy->_is_nil() ) {
278 TCollection_AsciiString anEntry = GetGeomObjectEntry();
279 SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
280 if ( !aSObject->_is_nil() ) {
281 aRes = TCollection_AsciiString( aSObject->GetName() );
288 void HYDROData_Obstacle::Translate( const double theDx,
292 TopoDS_Shape aShape3D = GetShape3D();
294 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
295 if ( aTranslatedShape3D.IsNull() )
298 SetShape3D( aTranslatedShape3D );
300 setTranslation( theDx, theDy, theDz );
303 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
305 TopoDS_Shape aResShape;
307 BRep_Builder aBrepBuilder;
308 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
313 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
315 TopoDS_Shape aResShape;
318 // Set "C" numeric locale to save numbers correctly
319 Kernel_Utils::Localizer loc;
321 IGESControl_Reader aReader;
323 Interface_Static::SetCVal("xstep.cascade.unit","M");
328 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
330 if (status == IFSelect_RetDone) {
332 Handle(IGESData_IGESModel) aModel =
333 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
334 if (!aModel.IsNull()) {
335 IGESData_GlobalSection aGS = aModel->GlobalSection();
337 aModel->SetGlobalSection(aGS);
340 aReader.ClearShapes();
341 aReader.TransferRoots();
343 aResShape = aReader.OneShape();
349 catch(Standard_Failure) {
357 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
359 TopoDS_Shape aResShape;
362 // Set "C" numeric locale to save numbers correctly
363 Kernel_Utils::Localizer loc;
365 STEPControl_Reader aReader;
368 Interface_Static::SetCVal("xstep.cascade.unit","M");
369 Interface_Static::SetIVal("read.step.ideas", 1);
370 Interface_Static::SetIVal("read.step.nonmanifold", 1);
373 TopoDS_Compound compound;
374 B.MakeCompound(compound);
379 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
381 if (status == IFSelect_RetDone) {
383 // set UnitFlag to units from file
384 TColStd_SequenceOfAsciiString anUnitLengthNames;
385 TColStd_SequenceOfAsciiString anUnitAngleNames;
386 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
387 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
388 if (anUnitLengthNames.Length() > 0) {
389 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
390 if (aLenUnits == "millimetre")
391 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
392 else if (aLenUnits == "centimetre")
393 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
394 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
395 Interface_Static::SetCVal("xstep.cascade.unit", "M");
396 else if (aLenUnits == "INCH")
397 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
399 // The file contains not supported units
404 Standard_Boolean failsonly = Standard_False;
405 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
408 Standard_Integer nbr = aReader.NbRootsForTransfer();
409 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
411 for (Standard_Integer n = 1; n <= nbr; n++) {
412 Standard_Boolean ok = aReader.TransferRoot(n);
413 // Collecting resulting entities
414 Standard_Integer nbs = aReader.NbShapes();
415 if (!ok || nbs == 0) {
416 continue; // skip empty root
418 else if (nbr == 1 && nbs == 1) { // For a single entity
419 aResShape = aReader.Shape(1);
420 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
421 // It should be removed after patching OCCT for bug OCC22436
422 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
423 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
425 TopoDS_Shape currShape;
426 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
427 for (; It.More(); It.Next()) {
429 currShape = It.Value();
432 aResShape = currShape;
438 for (Standard_Integer i = 1; i <= nbs; i++) {
439 TopoDS_Shape aShape = aReader.Shape(i);
440 if (aShape.IsNull()) {
444 B.Add(compound, aShape);
449 if (aResShape.IsNull())
450 aResShape = compound;
452 // Check if any BRep entity has been read, there must be at least a vertex
453 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
454 // No geometrical data in the imported file
455 return TopoDS_Shape();
462 catch (Standard_Failure) {
470 QColor HYDROData_Obstacle::getDefaultFillingColor() const
472 return DefaultFillingColor();
475 QColor HYDROData_Obstacle::getDefaultBorderColor() const
477 return DefaultBorderColor();
480 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
482 return KIND_OBSTACLE_ALTITUDE;
485 void HYDROData_Obstacle::createGroupObjects()
487 TopoDS_Shape anObstacleShape = GetTopShape();
488 if ( !anObstacleShape.IsNull() )
490 TopTools_SequenceOfShape aWireEdges;
491 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
492 if ( !aWireEdges.IsEmpty() )
494 QString aWireGroupName = GetName() + "_Outer_Wire";
496 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
497 anExtWireGroup->SetName( aWireGroupName );
499 anExtWireGroup->SetShapes( aWireEdges );
504 void HYDROData_Obstacle::setTranslation( const double theDx,
508 TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
510 double aCurDx = theDx;
511 double aCurDy = theDy;
512 double aCurDz = theDz;
514 double aPrevDx, aPrevDy, aPrevDz;
515 if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
522 Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
523 aCoeffsArray->SetValue( 1, aCurDx );
524 aCoeffsArray->SetValue( 2, aCurDy );
525 aCoeffsArray->SetValue( 3, aCurDz );
528 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
530 theDx = theDy = theDz = 0.0;
532 TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
533 if ( aLabel.IsNull() )
536 Handle(TDataStd_RealArray) aCoeffsArray;
537 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
540 theDx = aCoeffsArray->Value( 1 );
541 theDy = aCoeffsArray->Value( 2 );
542 theDz = aCoeffsArray->Value( 3 );
547 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
549 Translate( theDx, theDy, 0 );