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 #pragma warning ( disable: 4251 )
32 #include <Basics_Utils.hxx>
36 #pragma warning ( default: 4251 )
39 #include <BRepTools.hxx>
40 #include <BRep_Builder.hxx>
42 #include <IGESControl_Reader.hxx>
43 #include <IGESData_IGESModel.hxx>
45 #include <STEPControl_Reader.hxx>
47 #include <Interface_Static.hxx>
50 #include <TopoDS_Iterator.hxx>
51 #include <TopoDS_Shape.hxx>
52 #include <TopoDS_Edge.hxx>
54 #include <TDataStd_AsciiString.hxx>
55 #include <TDataStd_RealArray.hxx>
57 #include <TColStd_SequenceOfAsciiString.hxx>
59 #include <TopExp_Explorer.hxx>
62 #pragma warning ( disable: 4251 )
68 #include <QStringList>
71 #pragma warning ( default: 4251 )
74 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
76 #include <HYDROData_Projection.h>
79 #include "HYDRO_trace.hxx"
81 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
84 HYDROData_Obstacle::HYDROData_Obstacle()
85 : HYDROData_ArtificialObject( Geom_3d )
89 HYDROData_Obstacle::~HYDROData_Obstacle()
93 QStringList HYDROData_Obstacle::DumpToPython( const QString& thePyScriptPath,
94 MapOfTreatedObjects& theTreatedObjects ) const
96 QStringList aResList = dumpObjectCreation( theTreatedObjects );
98 QString anObstacleName = GetObjPyName();
100 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
101 QString aFilePath = GetFilePath();
103 if ( !aGeomObjectEntry.IsEmpty() )
105 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
106 // aResList << QString( "%1 = theStudy.FindObjectID( \"%2\" )" )
107 // .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
108 QString aGeomObjectName( GetGeomObjectName().ToCString() );
109 if ( !aGeomObjectName.isEmpty() ) {
110 aResList << QString( "%1 = theStudy.FindObjectByName( \"%2\", \"GEOM\" )[0];" )
111 .arg( aSalomeObjName ).arg( aGeomObjectName );
113 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() )" )
114 .arg( anObstacleName ).arg( aSalomeObjName );
116 aResList << QString( "%1.SetGeomObjectEntry( %2.GetID() )" )
117 .arg( anObstacleName ).arg( aSalomeObjName );
119 aResList << QString( "" );
121 else if ( !aFilePath.isEmpty() )
123 aResList << QString( "%1.ImportFromFile( \"%2\" )" )
124 .arg( anObstacleName ).arg( aFilePath );
125 aResList << QString( "" );
128 // Write the translation points
129 double aDx, aDy, aDz;
130 if ( getTranslation( aDx, aDy, aDz ) )
132 aResList << QString( "%1.Translate( %2, %3, %4 )" )
133 .arg( anObstacleName ).arg( aDx ).arg( aDy ).arg( aDz );
134 aResList << QString( "" );
137 aResList << QString( "%1.Update()" ).arg( anObstacleName );
138 aResList << QString( "" );
143 void HYDROData_Obstacle::Update()
145 RemoveGroupObjects();
146 createGroupObjects();
147 checkAndSetAltitudeObject();
149 HYDROData_Entity::Update();
152 bool HYDROData_Obstacle::IsHas2dPrs() const
157 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
159 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
160 HYDROData_ArtificialObject::SetShape3D( theShape );
161 HYDROData_ArtificialObject::SetTopShape( aShape2d );
164 QColor HYDROData_Obstacle::DefaultFillingColor() const
166 return QColor( Qt::yellow );
169 QColor HYDROData_Obstacle::DefaultBorderColor() const
171 return QColor( Qt::transparent );
174 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
176 // Check the file existence
177 QFileInfo aFileInfo( theFilePath );
178 if ( !aFileInfo.exists() ) {
186 QString aFileSuf = aFileInfo.suffix().toLower();
187 if ( aFileSuf == "brep" ) {
188 aShape = ImportBREP( theFilePath );
189 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
190 aShape = ImportIGES( theFilePath );
191 } else if ( aFileSuf == "step" ) {
192 aShape = ImportSTEP( theFilePath );
195 // Check the result shape
196 aRes = !aShape.IsNull();
198 // Set shape to the obstacle in case of success
200 SetShape3D( aShape );
201 SetFilePath( theFilePath );
207 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
209 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
210 Handle(TDataStd_AsciiString) anAttr = TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
211 anAttr->SetID(TDataStd_AsciiString::GetID());
214 QString HYDROData_Obstacle::GetFilePath() const
218 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
219 if ( !aLabel.IsNull() )
221 Handle(TDataStd_AsciiString) anAsciiStr;
222 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
223 aRes = QString( anAsciiStr->Get().ToCString() );
229 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
234 if ( theIOR.IsEmpty() ) {
238 // TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
239 TopoDS_Shape aShape = HYDROData_GeomTool::GetShapeFromIOR( theIOR.ToCString() );
241 if ( !aShape.IsNull() ) {
242 SetShape3D( aShape );
250 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
252 Handle(TDataStd_AsciiString) anAttr = TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
253 anAttr->SetID(TDataStd_AsciiString::GetID());
256 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
258 TCollection_AsciiString aRes;
260 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
261 if ( !aLabel.IsNull() )
263 Handle(TDataStd_AsciiString) anAsciiStr;
264 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
265 aRes = anAsciiStr->Get();
271 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
273 TCollection_AsciiString aRes;
276 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudy();
278 if ( !aDSStudy->_is_nil() ) {
279 TCollection_AsciiString anEntry = GetGeomObjectEntry();
280 SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
281 if ( !aSObject->_is_nil() ) {
282 aRes = TCollection_AsciiString( aSObject->GetName() );
289 void HYDROData_Obstacle::Translate( const double theDx,
293 TopoDS_Shape aShape3D = GetShape3D();
295 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
296 if ( aTranslatedShape3D.IsNull() )
299 SetShape3D( aTranslatedShape3D );
301 setTranslation( theDx, theDy, theDz );
304 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
306 TopoDS_Shape aResShape;
308 BRep_Builder aBrepBuilder;
309 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
314 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
316 TopoDS_Shape aResShape;
319 // Set "C" numeric locale to save numbers correctly
320 Kernel_Utils::Localizer loc;
322 IGESControl_Reader aReader;
324 Interface_Static::SetCVal("xstep.cascade.unit","M");
329 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
331 if (status == IFSelect_RetDone) {
333 Handle(IGESData_IGESModel) aModel =
334 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
335 if (!aModel.IsNull()) {
336 IGESData_GlobalSection aGS = aModel->GlobalSection();
338 aModel->SetGlobalSection(aGS);
341 aReader.ClearShapes();
342 aReader.TransferRoots();
344 aResShape = aReader.OneShape();
350 catch(Standard_Failure) {
358 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
360 TopoDS_Shape aResShape;
363 // Set "C" numeric locale to save numbers correctly
364 Kernel_Utils::Localizer loc;
366 STEPControl_Reader aReader;
369 Interface_Static::SetCVal("xstep.cascade.unit","M");
370 Interface_Static::SetIVal("read.step.ideas", 1);
371 Interface_Static::SetIVal("read.step.nonmanifold", 1);
374 TopoDS_Compound compound;
375 B.MakeCompound(compound);
380 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
382 if (status == IFSelect_RetDone) {
384 // set UnitFlag to units from file
385 TColStd_SequenceOfAsciiString anUnitLengthNames;
386 TColStd_SequenceOfAsciiString anUnitAngleNames;
387 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
388 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
389 if (anUnitLengthNames.Length() > 0) {
390 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
391 if (aLenUnits == "millimetre")
392 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
393 else if (aLenUnits == "centimetre")
394 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
395 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
396 Interface_Static::SetCVal("xstep.cascade.unit", "M");
397 else if (aLenUnits == "INCH")
398 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
400 // The file contains not supported units
405 Standard_Boolean failsonly = Standard_False;
406 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
409 Standard_Integer nbr = aReader.NbRootsForTransfer();
410 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
412 for (Standard_Integer n = 1; n <= nbr; n++) {
413 Standard_Boolean ok = aReader.TransferRoot(n);
414 // Collecting resulting entities
415 Standard_Integer nbs = aReader.NbShapes();
416 if (!ok || nbs == 0) {
417 continue; // skip empty root
419 else if (nbr == 1 && nbs == 1) { // For a single entity
420 aResShape = aReader.Shape(1);
421 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
422 // It should be removed after patching OCCT for bug OCC22436
423 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
424 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
426 TopoDS_Shape currShape;
427 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
428 for (; It.More(); It.Next()) {
430 currShape = It.Value();
433 aResShape = currShape;
439 for (Standard_Integer i = 1; i <= nbs; i++) {
440 TopoDS_Shape aShape = aReader.Shape(i);
441 if (aShape.IsNull()) {
445 B.Add(compound, aShape);
450 if (aResShape.IsNull())
451 aResShape = compound;
453 // Check if any BRep entity has been read, there must be at least a vertex
454 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
455 // No geometrical data in the imported file
456 return TopoDS_Shape();
463 catch (Standard_Failure) {
471 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
473 DEBTRACE("HYDROData_Obstacle::getAltitudeObjectType");
474 return KIND_OBSTACLE_ALTITUDE;
477 void HYDROData_Obstacle::createGroupObjects()
479 TopoDS_Shape anObstacleShape = GetTopShape();
480 if ( !anObstacleShape.IsNull() )
482 TopTools_SequenceOfShape aWireEdges;
483 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
484 if ( !aWireEdges.IsEmpty() )
486 QString aWireGroupName = GetName() + "_Outer_Wire";
488 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
489 anExtWireGroup->SetName( aWireGroupName );
491 anExtWireGroup->SetShapes( aWireEdges );
496 void HYDROData_Obstacle::setTranslation( const double theDx,
500 TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
502 double aCurDx = theDx;
503 double aCurDy = theDy;
504 double aCurDz = theDz;
506 double aPrevDx, aPrevDy, aPrevDz;
507 if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
514 Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
515 aCoeffsArray->SetID(TDataStd_RealArray::GetID());
516 aCoeffsArray->SetValue( 1, aCurDx );
517 aCoeffsArray->SetValue( 2, aCurDy );
518 aCoeffsArray->SetValue( 3, aCurDz );
521 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
523 theDx = theDy = theDz = 0.0;
525 TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
526 if ( aLabel.IsNull() )
529 Handle(TDataStd_RealArray) aCoeffsArray;
530 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
533 theDx = aCoeffsArray->Value( 1 );
534 theDy = aCoeffsArray->Value( 2 );
535 theDz = aCoeffsArray->Value( 3 );
540 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
542 Translate( theDx, theDy, 0 );