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 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
140 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
141 HYDROData_ArtificialObject::SetShape3D( theShape );
142 HYDROData_ArtificialObject::SetTopShape( aShape2d );
145 QColor HYDROData_Obstacle::DefaultFillingColor() const
147 return QColor( Qt::yellow );
150 QColor HYDROData_Obstacle::DefaultBorderColor() const
152 return QColor( Qt::transparent );
155 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
157 // Check the file existence
158 QFileInfo aFileInfo( theFilePath );
159 if ( !aFileInfo.exists() ) {
167 QString aFileSuf = aFileInfo.suffix().toLower();
168 if ( aFileSuf == "brep" ) {
169 aShape = ImportBREP( theFilePath );
170 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
171 aShape = ImportIGES( theFilePath );
172 } else if ( aFileSuf == "step" ) {
173 aShape = ImportSTEP( theFilePath );
176 // Check the result shape
177 aRes = !aShape.IsNull();
179 // Set shape to the obstacle in case of success
181 SetShape3D( aShape );
182 SetFilePath( theFilePath );
188 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
190 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
191 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
194 QString HYDROData_Obstacle::GetFilePath() const
198 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
199 if ( !aLabel.IsNull() )
201 Handle(TDataStd_AsciiString) anAsciiStr;
202 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
203 aRes = QString( anAsciiStr->Get().ToCString() );
209 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
214 if ( theIOR.IsEmpty() ) {
219 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
223 // TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
224 TopoDS_Shape aShape = HYDROData_GeomTool::GetShapeFromIOR( aDocId, theIOR.ToCString() );
226 if ( !aShape.IsNull() ) {
227 SetShape3D( aShape );
235 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
237 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
240 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
242 TCollection_AsciiString aRes;
244 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
245 if ( !aLabel.IsNull() )
247 Handle(TDataStd_AsciiString) anAsciiStr;
248 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
249 aRes = anAsciiStr->Get();
255 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
257 TCollection_AsciiString aRes;
260 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
265 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( aDocId );
267 if ( !aDSStudy->_is_nil() ) {
268 TCollection_AsciiString anEntry = GetGeomObjectEntry();
269 SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
270 if ( !aSObject->_is_nil() ) {
271 aRes = TCollection_AsciiString( aSObject->GetName() );
278 void HYDROData_Obstacle::Translate( const double theDx,
282 TopoDS_Shape aShape3D = GetShape3D();
284 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
285 if ( aTranslatedShape3D.IsNull() )
288 SetShape3D( aTranslatedShape3D );
290 setTranslation( theDx, theDy, theDz );
293 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
295 TopoDS_Shape aResShape;
297 BRep_Builder aBrepBuilder;
298 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
303 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
305 TopoDS_Shape aResShape;
308 // Set "C" numeric locale to save numbers correctly
309 Kernel_Utils::Localizer loc;
311 IGESControl_Reader aReader;
313 Interface_Static::SetCVal("xstep.cascade.unit","M");
318 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
320 if (status == IFSelect_RetDone) {
322 Handle(IGESData_IGESModel) aModel =
323 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
324 if (!aModel.IsNull()) {
325 IGESData_GlobalSection aGS = aModel->GlobalSection();
327 aModel->SetGlobalSection(aGS);
330 aReader.ClearShapes();
331 aReader.TransferRoots();
333 aResShape = aReader.OneShape();
339 catch(Standard_Failure) {
347 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
349 TopoDS_Shape aResShape;
352 // Set "C" numeric locale to save numbers correctly
353 Kernel_Utils::Localizer loc;
355 STEPControl_Reader aReader;
358 Interface_Static::SetCVal("xstep.cascade.unit","M");
359 Interface_Static::SetIVal("read.step.ideas", 1);
360 Interface_Static::SetIVal("read.step.nonmanifold", 1);
363 TopoDS_Compound compound;
364 B.MakeCompound(compound);
369 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
371 if (status == IFSelect_RetDone) {
373 // set UnitFlag to units from file
374 TColStd_SequenceOfAsciiString anUnitLengthNames;
375 TColStd_SequenceOfAsciiString anUnitAngleNames;
376 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
377 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
378 if (anUnitLengthNames.Length() > 0) {
379 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
380 if (aLenUnits == "millimetre")
381 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
382 else if (aLenUnits == "centimetre")
383 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
384 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
385 Interface_Static::SetCVal("xstep.cascade.unit", "M");
386 else if (aLenUnits == "INCH")
387 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
389 // The file contains not supported units
394 Standard_Boolean failsonly = Standard_False;
395 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
398 Standard_Integer nbr = aReader.NbRootsForTransfer();
399 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
401 for (Standard_Integer n = 1; n <= nbr; n++) {
402 Standard_Boolean ok = aReader.TransferRoot(n);
403 // Collecting resulting entities
404 Standard_Integer nbs = aReader.NbShapes();
405 if (!ok || nbs == 0) {
406 continue; // skip empty root
408 else if (nbr == 1 && nbs == 1) { // For a single entity
409 aResShape = aReader.Shape(1);
410 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
411 // It should be removed after patching OCCT for bug OCC22436
412 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
413 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
415 TopoDS_Shape currShape;
416 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
417 for (; It.More(); It.Next()) {
419 currShape = It.Value();
422 aResShape = currShape;
428 for (Standard_Integer i = 1; i <= nbs; i++) {
429 TopoDS_Shape aShape = aReader.Shape(i);
430 if (aShape.IsNull()) {
434 B.Add(compound, aShape);
439 if (aResShape.IsNull())
440 aResShape = compound;
442 // Check if any BRep entity has been read, there must be at least a vertex
443 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
444 // No geometrical data in the imported file
445 return TopoDS_Shape();
452 catch (Standard_Failure) {
460 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
462 return KIND_OBSTACLE_ALTITUDE;
465 void HYDROData_Obstacle::createGroupObjects()
467 TopoDS_Shape anObstacleShape = GetTopShape();
468 if ( !anObstacleShape.IsNull() )
470 TopTools_SequenceOfShape aWireEdges;
471 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
472 if ( !aWireEdges.IsEmpty() )
474 QString aWireGroupName = GetName() + "_Outer_Wire";
476 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
477 anExtWireGroup->SetName( aWireGroupName );
479 anExtWireGroup->SetShapes( aWireEdges );
484 void HYDROData_Obstacle::setTranslation( const double theDx,
488 TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
490 double aCurDx = theDx;
491 double aCurDy = theDy;
492 double aCurDz = theDz;
494 double aPrevDx, aPrevDy, aPrevDz;
495 if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
502 Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
503 aCoeffsArray->SetValue( 1, aCurDx );
504 aCoeffsArray->SetValue( 2, aCurDy );
505 aCoeffsArray->SetValue( 3, aCurDz );
508 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
510 theDx = theDy = theDz = 0.0;
512 TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
513 if ( aLabel.IsNull() )
516 Handle(TDataStd_RealArray) aCoeffsArray;
517 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
520 theDx = aCoeffsArray->Value( 1 );
521 theDy = aCoeffsArray->Value( 2 );
522 theDz = aCoeffsArray->Value( 3 );
527 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
529 Translate( theDx, theDy, 0 );