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"
27 #include <Basics_Utils.hxx>
29 #include <BRepTools.hxx>
30 #include <BRep_Builder.hxx>
32 #include <IGESControl_Reader.hxx>
33 #include <IGESData_IGESModel.hxx>
35 #include <STEPControl_Reader.hxx>
37 #include <Interface_Static.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopoDS_Edge.hxx>
44 #include <TDataStd_AsciiString.hxx>
45 #include <TDataStd_RealArray.hxx>
47 #include <TColStd_SequenceOfAsciiString.hxx>
49 #include <TopExp_Explorer.hxx>
54 #include <QStringList>
56 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
58 #include <HYDROData_Projection.h>
60 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
61 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
64 HYDROData_Obstacle::HYDROData_Obstacle()
65 : HYDROData_ArtificialObject()
69 HYDROData_Obstacle::~HYDROData_Obstacle()
73 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
75 QStringList aResList = dumpObjectCreation( theTreatedObjects );
77 QString anObstacleName = GetObjPyName();
79 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
80 QString aFilePath = GetFilePath();
82 if ( !aGeomObjectEntry.IsEmpty() )
84 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
85 // aResList << QString( "%1 = theStudy.FindObjectID( \"%2\" );" )
86 // .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
87 QString aGeomObjectName( GetGeomObjectName().ToCString() );
88 if ( !aGeomObjectName.isEmpty() ) {
89 aResList << QString( "%1 = theStudy.FindObjectByName( \"%2\", \"GEOM\" )[0];" )
90 .arg( aSalomeObjName ).arg( aGeomObjectName );
92 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
93 .arg( anObstacleName ).arg( aSalomeObjName );
95 aResList << QString( "%1.SetGeomObjectEntry( %2.GetID() );" )
96 .arg( anObstacleName ).arg( aSalomeObjName );
98 aResList << QString( "" );
100 else if ( !aFilePath.isEmpty() )
102 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
103 .arg( anObstacleName ).arg( aFilePath );
104 aResList << QString( "" );
107 // Write the translation points
108 double aDx, aDy, aDz;
109 if ( getTranslation( aDx, aDy, aDz ) )
111 aResList << QString( "%1.Translate( %2, %3, %4 );" )
112 .arg( anObstacleName ).arg( aDx ).arg( aDy ).arg( aDz );
113 aResList << QString( "" );
116 aResList << QString( "%1.Update();" ).arg( anObstacleName );
117 aResList << QString( "" );
122 void HYDROData_Obstacle::Update()
124 removeGroupObjects();
125 createGroupObjects();
126 checkAndSetAltitudeObject();
128 HYDROData_Entity::Update();
131 bool HYDROData_Obstacle::IsHas2dPrs() const
136 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
138 return getTopShape();
141 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
146 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
148 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
149 HYDROData_ArtificialObject::SetShape3D( theShape );
150 HYDROData_ArtificialObject::SetTopShape( aShape2d );
153 QColor HYDROData_Obstacle::DefaultFillingColor()
155 return QColor( Qt::yellow );
158 QColor HYDROData_Obstacle::DefaultBorderColor()
160 return QColor( Qt::transparent );
163 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
165 // Check the file existence
166 QFileInfo aFileInfo( theFilePath );
167 if ( !aFileInfo.exists() ) {
175 QString aFileSuf = aFileInfo.suffix().toLower();
176 if ( aFileSuf == "brep" ) {
177 aShape = ImportBREP( theFilePath );
178 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
179 aShape = ImportIGES( theFilePath );
180 } else if ( aFileSuf == "step" ) {
181 aShape = ImportSTEP( theFilePath );
184 // Check the result shape
185 aRes = !aShape.IsNull();
187 // Set shape to the obstacle in case of success
189 SetShape3D( aShape );
190 SetFilePath( theFilePath );
196 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
198 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
199 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
202 QString HYDROData_Obstacle::GetFilePath() const
206 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
207 if ( !aLabel.IsNull() )
209 Handle(TDataStd_AsciiString) anAsciiStr;
210 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
211 aRes = QString( anAsciiStr->Get().ToCString() );
217 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
221 if ( theIOR.IsEmpty() ) {
226 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
230 // TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
231 TopoDS_Shape aShape = HYDROData_GeomTool::GetShapeFromIOR( aDocId, theIOR.ToCString() );
233 if ( !aShape.IsNull() ) {
234 SetShape3D( aShape );
241 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
243 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
246 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
248 TCollection_AsciiString aRes;
250 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
251 if ( !aLabel.IsNull() )
253 Handle(TDataStd_AsciiString) anAsciiStr;
254 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
255 aRes = anAsciiStr->Get();
261 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
263 TCollection_AsciiString aRes;
266 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
270 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( aDocId );
272 if ( !aDSStudy->_is_nil() ) {
273 TCollection_AsciiString anEntry = GetGeomObjectEntry();
274 SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
275 if ( !aSObject->_is_nil() ) {
276 aRes = TCollection_AsciiString( aSObject->GetName() );
283 void HYDROData_Obstacle::Translate( const double theDx,
287 TopoDS_Shape aShape3D = GetShape3D();
289 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
290 if ( aTranslatedShape3D.IsNull() )
293 SetShape3D( aTranslatedShape3D );
295 setTranslation( theDx, theDy, theDz );
298 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
300 TopoDS_Shape aResShape;
302 BRep_Builder aBrepBuilder;
303 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
308 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
310 TopoDS_Shape aResShape;
312 // Set "C" numeric locale to save numbers correctly
313 Kernel_Utils::Localizer loc;
315 IGESControl_Reader aReader;
317 Interface_Static::SetCVal("xstep.cascade.unit","M");
322 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
324 if (status == IFSelect_RetDone) {
326 Handle(IGESData_IGESModel) aModel =
327 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
328 if (!aModel.IsNull()) {
329 IGESData_GlobalSection aGS = aModel->GlobalSection();
331 aModel->SetGlobalSection(aGS);
334 aReader.ClearShapes();
335 aReader.TransferRoots();
337 aResShape = aReader.OneShape();
343 catch(Standard_Failure) {
350 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
352 TopoDS_Shape aResShape;
354 // Set "C" numeric locale to save numbers correctly
355 Kernel_Utils::Localizer loc;
357 STEPControl_Reader aReader;
360 Interface_Static::SetCVal("xstep.cascade.unit","M");
361 Interface_Static::SetIVal("read.step.ideas", 1);
362 Interface_Static::SetIVal("read.step.nonmanifold", 1);
365 TopoDS_Compound compound;
366 B.MakeCompound(compound);
371 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
373 if (status == IFSelect_RetDone) {
375 // set UnitFlag to units from file
376 TColStd_SequenceOfAsciiString anUnitLengthNames;
377 TColStd_SequenceOfAsciiString anUnitAngleNames;
378 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
379 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
380 if (anUnitLengthNames.Length() > 0) {
381 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
382 if (aLenUnits == "millimetre")
383 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
384 else if (aLenUnits == "centimetre")
385 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
386 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
387 Interface_Static::SetCVal("xstep.cascade.unit", "M");
388 else if (aLenUnits == "INCH")
389 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
391 // The file contains not supported units
396 Standard_Boolean failsonly = Standard_False;
397 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
400 Standard_Integer nbr = aReader.NbRootsForTransfer();
401 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
403 for (Standard_Integer n = 1; n <= nbr; n++) {
404 Standard_Boolean ok = aReader.TransferRoot(n);
405 // Collecting resulting entities
406 Standard_Integer nbs = aReader.NbShapes();
407 if (!ok || nbs == 0) {
408 continue; // skip empty root
410 else if (nbr == 1 && nbs == 1) { // For a single entity
411 aResShape = aReader.Shape(1);
412 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
413 // It should be removed after patching OCCT for bug OCC22436
414 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
415 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
417 TopoDS_Shape currShape;
418 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
419 for (; It.More(); It.Next()) {
421 currShape = It.Value();
424 aResShape = currShape;
430 for (Standard_Integer i = 1; i <= nbs; i++) {
431 TopoDS_Shape aShape = aReader.Shape(i);
432 if (aShape.IsNull()) {
436 B.Add(compound, aShape);
441 if (aResShape.IsNull())
442 aResShape = compound;
444 // Check if any BRep entity has been read, there must be at least a vertex
445 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
446 // No geometrical data in the imported file
447 return TopoDS_Shape();
454 catch (Standard_Failure) {
461 QColor HYDROData_Obstacle::getDefaultFillingColor() const
463 return DefaultFillingColor();
466 QColor HYDROData_Obstacle::getDefaultBorderColor() const
468 return DefaultBorderColor();
471 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
473 return KIND_OBSTACLE_ALTITUDE;
476 void HYDROData_Obstacle::createGroupObjects()
478 TopoDS_Shape anObstacleShape = GetTopShape();
479 if ( !anObstacleShape.IsNull() )
481 TopTools_SequenceOfShape aWireEdges;
482 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
483 if ( !aWireEdges.IsEmpty() )
485 QString aWireGroupName = GetName() + "_Outer_Wire";
487 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
488 anExtWireGroup->SetName( aWireGroupName );
490 anExtWireGroup->SetShapes( aWireEdges );
495 void HYDROData_Obstacle::setTranslation( const double theDx,
499 TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
501 double aCurDx = theDx;
502 double aCurDy = theDy;
503 double aCurDz = theDz;
505 double aPrevDx, aPrevDy, aPrevDz;
506 if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
513 Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
514 aCoeffsArray->SetValue( 1, aCurDx );
515 aCoeffsArray->SetValue( 2, aCurDy );
516 aCoeffsArray->SetValue( 3, aCurDz );
519 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
521 theDx = theDy = theDz = 0.0;
523 TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
524 if ( aLabel.IsNull() )
527 Handle(TDataStd_RealArray) aCoeffsArray;
528 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
531 theDx = aCoeffsArray->Value( 1 );
532 theDy = aCoeffsArray->Value( 2 );
533 theDz = aCoeffsArray->Value( 3 );
538 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
540 Translate( theDx, theDy, 0 );