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>
78 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
79 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
82 HYDROData_Obstacle::HYDROData_Obstacle()
83 : HYDROData_ArtificialObject( Geom_3d )
87 HYDROData_Obstacle::~HYDROData_Obstacle()
91 QStringList HYDROData_Obstacle::DumpToPython( const QString& thePyScriptPath,
92 MapOfTreatedObjects& theTreatedObjects ) const
94 QStringList aResList = dumpObjectCreation( theTreatedObjects );
96 QString anObstacleName = GetObjPyName();
98 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
99 QString aFilePath = GetFilePath();
101 if ( !aGeomObjectEntry.IsEmpty() )
103 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
104 // aResList << QString( "%1 = theStudy.FindObjectID( \"%2\" );" )
105 // .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
106 QString aGeomObjectName( GetGeomObjectName().ToCString() );
107 if ( !aGeomObjectName.isEmpty() ) {
108 aResList << QString( "%1 = theStudy.FindObjectByName( \"%2\", \"GEOM\" )[0];" )
109 .arg( aSalomeObjName ).arg( aGeomObjectName );
111 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
112 .arg( anObstacleName ).arg( aSalomeObjName );
114 aResList << QString( "%1.SetGeomObjectEntry( %2.GetID() );" )
115 .arg( anObstacleName ).arg( aSalomeObjName );
117 aResList << QString( "" );
119 else if ( !aFilePath.isEmpty() )
121 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
122 .arg( anObstacleName ).arg( aFilePath );
123 aResList << QString( "" );
126 // Write the translation points
127 double aDx, aDy, aDz;
128 if ( getTranslation( aDx, aDy, aDz ) )
130 aResList << QString( "%1.Translate( %2, %3, %4 );" )
131 .arg( anObstacleName ).arg( aDx ).arg( aDy ).arg( aDz );
132 aResList << QString( "" );
135 aResList << QString( "%1.Update();" ).arg( anObstacleName );
136 aResList << QString( "" );
141 void HYDROData_Obstacle::Update()
143 RemoveGroupObjects();
144 createGroupObjects();
145 checkAndSetAltitudeObject();
147 HYDROData_Entity::Update();
150 bool HYDROData_Obstacle::IsHas2dPrs() const
155 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
157 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
158 HYDROData_ArtificialObject::SetShape3D( theShape );
159 HYDROData_ArtificialObject::SetTopShape( aShape2d );
162 QColor HYDROData_Obstacle::DefaultFillingColor() const
164 return QColor( Qt::yellow );
167 QColor HYDROData_Obstacle::DefaultBorderColor() const
169 return QColor( Qt::transparent );
172 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
174 // Check the file existence
175 QFileInfo aFileInfo( theFilePath );
176 if ( !aFileInfo.exists() ) {
184 QString aFileSuf = aFileInfo.suffix().toLower();
185 if ( aFileSuf == "brep" ) {
186 aShape = ImportBREP( theFilePath );
187 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
188 aShape = ImportIGES( theFilePath );
189 } else if ( aFileSuf == "step" ) {
190 aShape = ImportSTEP( theFilePath );
193 // Check the result shape
194 aRes = !aShape.IsNull();
196 // Set shape to the obstacle in case of success
198 SetShape3D( aShape );
199 SetFilePath( theFilePath );
205 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
207 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
208 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
211 QString HYDROData_Obstacle::GetFilePath() const
215 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
216 if ( !aLabel.IsNull() )
218 Handle(TDataStd_AsciiString) anAsciiStr;
219 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
220 aRes = QString( anAsciiStr->Get().ToCString() );
226 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
231 if ( theIOR.IsEmpty() ) {
236 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
240 // TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
241 TopoDS_Shape aShape = HYDROData_GeomTool::GetShapeFromIOR( aDocId, theIOR.ToCString() );
243 if ( !aShape.IsNull() ) {
244 SetShape3D( aShape );
252 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
254 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
257 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
259 TCollection_AsciiString aRes;
261 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
262 if ( !aLabel.IsNull() )
264 Handle(TDataStd_AsciiString) anAsciiStr;
265 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
266 aRes = anAsciiStr->Get();
272 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
274 TCollection_AsciiString aRes;
277 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
282 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( aDocId );
284 if ( !aDSStudy->_is_nil() ) {
285 TCollection_AsciiString anEntry = GetGeomObjectEntry();
286 SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
287 if ( !aSObject->_is_nil() ) {
288 aRes = TCollection_AsciiString( aSObject->GetName() );
295 void HYDROData_Obstacle::Translate( const double theDx,
299 TopoDS_Shape aShape3D = GetShape3D();
301 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
302 if ( aTranslatedShape3D.IsNull() )
305 SetShape3D( aTranslatedShape3D );
307 setTranslation( theDx, theDy, theDz );
310 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
312 TopoDS_Shape aResShape;
314 BRep_Builder aBrepBuilder;
315 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
320 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
322 TopoDS_Shape aResShape;
325 // Set "C" numeric locale to save numbers correctly
326 Kernel_Utils::Localizer loc;
328 IGESControl_Reader aReader;
330 Interface_Static::SetCVal("xstep.cascade.unit","M");
335 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
337 if (status == IFSelect_RetDone) {
339 Handle(IGESData_IGESModel) aModel =
340 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
341 if (!aModel.IsNull()) {
342 IGESData_GlobalSection aGS = aModel->GlobalSection();
344 aModel->SetGlobalSection(aGS);
347 aReader.ClearShapes();
348 aReader.TransferRoots();
350 aResShape = aReader.OneShape();
356 catch(Standard_Failure) {
364 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
366 TopoDS_Shape aResShape;
369 // Set "C" numeric locale to save numbers correctly
370 Kernel_Utils::Localizer loc;
372 STEPControl_Reader aReader;
375 Interface_Static::SetCVal("xstep.cascade.unit","M");
376 Interface_Static::SetIVal("read.step.ideas", 1);
377 Interface_Static::SetIVal("read.step.nonmanifold", 1);
380 TopoDS_Compound compound;
381 B.MakeCompound(compound);
386 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
388 if (status == IFSelect_RetDone) {
390 // set UnitFlag to units from file
391 TColStd_SequenceOfAsciiString anUnitLengthNames;
392 TColStd_SequenceOfAsciiString anUnitAngleNames;
393 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
394 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
395 if (anUnitLengthNames.Length() > 0) {
396 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
397 if (aLenUnits == "millimetre")
398 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
399 else if (aLenUnits == "centimetre")
400 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
401 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
402 Interface_Static::SetCVal("xstep.cascade.unit", "M");
403 else if (aLenUnits == "INCH")
404 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
406 // The file contains not supported units
411 Standard_Boolean failsonly = Standard_False;
412 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
415 Standard_Integer nbr = aReader.NbRootsForTransfer();
416 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
418 for (Standard_Integer n = 1; n <= nbr; n++) {
419 Standard_Boolean ok = aReader.TransferRoot(n);
420 // Collecting resulting entities
421 Standard_Integer nbs = aReader.NbShapes();
422 if (!ok || nbs == 0) {
423 continue; // skip empty root
425 else if (nbr == 1 && nbs == 1) { // For a single entity
426 aResShape = aReader.Shape(1);
427 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
428 // It should be removed after patching OCCT for bug OCC22436
429 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
430 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
432 TopoDS_Shape currShape;
433 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
434 for (; It.More(); It.Next()) {
436 currShape = It.Value();
439 aResShape = currShape;
445 for (Standard_Integer i = 1; i <= nbs; i++) {
446 TopoDS_Shape aShape = aReader.Shape(i);
447 if (aShape.IsNull()) {
451 B.Add(compound, aShape);
456 if (aResShape.IsNull())
457 aResShape = compound;
459 // Check if any BRep entity has been read, there must be at least a vertex
460 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
461 // No geometrical data in the imported file
462 return TopoDS_Shape();
469 catch (Standard_Failure) {
477 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
479 return KIND_OBSTACLE_ALTITUDE;
482 void HYDROData_Obstacle::createGroupObjects()
484 TopoDS_Shape anObstacleShape = GetTopShape();
485 if ( !anObstacleShape.IsNull() )
487 TopTools_SequenceOfShape aWireEdges;
488 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
489 if ( !aWireEdges.IsEmpty() )
491 QString aWireGroupName = GetName() + "_Outer_Wire";
493 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
494 anExtWireGroup->SetName( aWireGroupName );
496 anExtWireGroup->SetShapes( aWireEdges );
501 void HYDROData_Obstacle::setTranslation( const double theDx,
505 TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
507 double aCurDx = theDx;
508 double aCurDy = theDy;
509 double aCurDz = theDz;
511 double aPrevDx, aPrevDy, aPrevDz;
512 if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
519 Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
520 aCoeffsArray->SetValue( 1, aCurDx );
521 aCoeffsArray->SetValue( 2, aCurDy );
522 aCoeffsArray->SetValue( 3, aCurDz );
525 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
527 theDx = theDy = theDz = 0.0;
529 TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
530 if ( aLabel.IsNull() )
533 Handle(TDataStd_RealArray) aCoeffsArray;
534 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
537 theDx = aCoeffsArray->Value( 1 );
538 theDy = aCoeffsArray->Value( 2 );
539 theDz = aCoeffsArray->Value( 3 );
544 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
546 Translate( theDx, theDy, 0 );