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>
61 #include "HYDRO_trace.hxx"
63 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
64 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
67 HYDROData_Obstacle::HYDROData_Obstacle()
68 : HYDROData_ArtificialObject( Geom_3d )
72 HYDROData_Obstacle::~HYDROData_Obstacle()
76 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
78 QStringList aResList = dumpObjectCreation( theTreatedObjects );
80 QString anObstacleName = GetObjPyName();
82 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
83 QString aFilePath = GetFilePath();
85 if ( !aGeomObjectEntry.IsEmpty() )
87 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
88 // aResList << QString( "%1 = theStudy.FindObjectID( \"%2\" );" )
89 // .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
90 QString aGeomObjectName( GetGeomObjectName().ToCString() );
91 if ( !aGeomObjectName.isEmpty() ) {
92 aResList << QString( "%1 = theStudy.FindObjectByName( \"%2\", \"GEOM\" )[0];" )
93 .arg( aSalomeObjName ).arg( aGeomObjectName );
95 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
96 .arg( anObstacleName ).arg( aSalomeObjName );
98 aResList << QString( "%1.SetGeomObjectEntry( %2.GetID() );" )
99 .arg( anObstacleName ).arg( aSalomeObjName );
101 aResList << QString( "" );
103 else if ( !aFilePath.isEmpty() )
105 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
106 .arg( anObstacleName ).arg( aFilePath );
107 aResList << QString( "" );
110 // Write the translation points
111 double aDx, aDy, aDz;
112 if ( getTranslation( aDx, aDy, aDz ) )
114 aResList << QString( "%1.Translate( %2, %3, %4 );" )
115 .arg( anObstacleName ).arg( aDx ).arg( aDy ).arg( aDz );
116 aResList << QString( "" );
119 aResList << QString( "%1.Update();" ).arg( anObstacleName );
120 aResList << QString( "" );
125 void HYDROData_Obstacle::Update()
127 removeGroupObjects();
128 createGroupObjects();
129 checkAndSetAltitudeObject();
131 HYDROData_Entity::Update();
134 bool HYDROData_Obstacle::IsHas2dPrs() const
139 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
141 return getTopShape();
144 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
149 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
151 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
152 HYDROData_ArtificialObject::SetShape3D( theShape );
153 HYDROData_ArtificialObject::SetTopShape( aShape2d );
156 QColor HYDROData_Obstacle::DefaultFillingColor()
158 return QColor( Qt::yellow );
161 QColor HYDROData_Obstacle::DefaultBorderColor()
163 return QColor( Qt::transparent );
166 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
168 // Check the file existence
169 QFileInfo aFileInfo( theFilePath );
170 if ( !aFileInfo.exists() ) {
178 QString aFileSuf = aFileInfo.suffix().toLower();
179 if ( aFileSuf == "brep" ) {
180 aShape = ImportBREP( theFilePath );
181 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
182 aShape = ImportIGES( theFilePath );
183 } else if ( aFileSuf == "step" ) {
184 aShape = ImportSTEP( theFilePath );
187 // Check the result shape
188 aRes = !aShape.IsNull();
190 // Set shape to the obstacle in case of success
192 SetShape3D( aShape );
193 SetFilePath( theFilePath );
199 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
201 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
202 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
205 QString HYDROData_Obstacle::GetFilePath() const
209 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
210 if ( !aLabel.IsNull() )
212 Handle(TDataStd_AsciiString) anAsciiStr;
213 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
214 aRes = QString( anAsciiStr->Get().ToCString() );
220 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 );
244 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
246 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
249 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
251 TCollection_AsciiString aRes;
253 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
254 if ( !aLabel.IsNull() )
256 Handle(TDataStd_AsciiString) anAsciiStr;
257 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
258 aRes = anAsciiStr->Get();
264 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
266 TCollection_AsciiString aRes;
269 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
273 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( aDocId );
275 if ( !aDSStudy->_is_nil() ) {
276 TCollection_AsciiString anEntry = GetGeomObjectEntry();
277 SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
278 if ( !aSObject->_is_nil() ) {
279 aRes = TCollection_AsciiString( aSObject->GetName() );
286 void HYDROData_Obstacle::Translate( const double theDx,
290 TopoDS_Shape aShape3D = GetShape3D();
292 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
293 if ( aTranslatedShape3D.IsNull() )
296 SetShape3D( aTranslatedShape3D );
298 setTranslation( theDx, theDy, theDz );
301 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
303 TopoDS_Shape aResShape;
305 BRep_Builder aBrepBuilder;
306 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
311 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
313 TopoDS_Shape aResShape;
315 // Set "C" numeric locale to save numbers correctly
316 Kernel_Utils::Localizer loc;
318 IGESControl_Reader aReader;
320 Interface_Static::SetCVal("xstep.cascade.unit","M");
325 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
327 if (status == IFSelect_RetDone) {
329 Handle(IGESData_IGESModel) aModel =
330 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
331 if (!aModel.IsNull()) {
332 IGESData_GlobalSection aGS = aModel->GlobalSection();
334 aModel->SetGlobalSection(aGS);
337 aReader.ClearShapes();
338 aReader.TransferRoots();
340 aResShape = aReader.OneShape();
346 catch(Standard_Failure) {
353 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
355 TopoDS_Shape aResShape;
357 // Set "C" numeric locale to save numbers correctly
358 Kernel_Utils::Localizer loc;
360 STEPControl_Reader aReader;
363 Interface_Static::SetCVal("xstep.cascade.unit","M");
364 Interface_Static::SetIVal("read.step.ideas", 1);
365 Interface_Static::SetIVal("read.step.nonmanifold", 1);
368 TopoDS_Compound compound;
369 B.MakeCompound(compound);
374 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
376 if (status == IFSelect_RetDone) {
378 // set UnitFlag to units from file
379 TColStd_SequenceOfAsciiString anUnitLengthNames;
380 TColStd_SequenceOfAsciiString anUnitAngleNames;
381 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
382 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
383 if (anUnitLengthNames.Length() > 0) {
384 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
385 if (aLenUnits == "millimetre")
386 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
387 else if (aLenUnits == "centimetre")
388 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
389 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
390 Interface_Static::SetCVal("xstep.cascade.unit", "M");
391 else if (aLenUnits == "INCH")
392 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
394 // The file contains not supported units
399 Standard_Boolean failsonly = Standard_False;
400 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
403 Standard_Integer nbr = aReader.NbRootsForTransfer();
404 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
406 for (Standard_Integer n = 1; n <= nbr; n++) {
407 Standard_Boolean ok = aReader.TransferRoot(n);
408 // Collecting resulting entities
409 Standard_Integer nbs = aReader.NbShapes();
410 if (!ok || nbs == 0) {
411 continue; // skip empty root
413 else if (nbr == 1 && nbs == 1) { // For a single entity
414 aResShape = aReader.Shape(1);
415 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
416 // It should be removed after patching OCCT for bug OCC22436
417 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
418 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
420 TopoDS_Shape currShape;
421 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
422 for (; It.More(); It.Next()) {
424 currShape = It.Value();
427 aResShape = currShape;
433 for (Standard_Integer i = 1; i <= nbs; i++) {
434 TopoDS_Shape aShape = aReader.Shape(i);
435 if (aShape.IsNull()) {
439 B.Add(compound, aShape);
444 if (aResShape.IsNull())
445 aResShape = compound;
447 // Check if any BRep entity has been read, there must be at least a vertex
448 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
449 // No geometrical data in the imported file
450 return TopoDS_Shape();
457 catch (Standard_Failure) {
464 QColor HYDROData_Obstacle::getDefaultFillingColor() const
466 return DefaultFillingColor();
469 QColor HYDROData_Obstacle::getDefaultBorderColor() const
471 return DefaultBorderColor();
474 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
476 DEBTRACE("HYDROData_Obstacle::getAltitudeObjectType");
477 return KIND_OBSTACLE_ALTITUDE;
480 void HYDROData_Obstacle::createGroupObjects()
482 TopoDS_Shape anObstacleShape = GetTopShape();
483 if ( !anObstacleShape.IsNull() )
485 TopTools_SequenceOfShape aWireEdges;
486 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
487 if ( !aWireEdges.IsEmpty() )
489 QString aWireGroupName = GetName() + "_Outer_Wire";
491 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
492 anExtWireGroup->SetName( aWireGroupName );
494 anExtWireGroup->SetShapes( aWireEdges );
499 void HYDROData_Obstacle::setTranslation( const double theDx,
503 TDF_Label aLabel = myLab.FindChild( DataTag_Translation );
505 double aCurDx = theDx;
506 double aCurDy = theDy;
507 double aCurDz = theDz;
509 double aPrevDx, aPrevDy, aPrevDz;
510 if ( getTranslation( aPrevDx, aPrevDy, aPrevDz ) )
517 Handle(TDataStd_RealArray) aCoeffsArray = TDataStd_RealArray::Set( aLabel, 1, 3 );
518 aCoeffsArray->SetValue( 1, aCurDx );
519 aCoeffsArray->SetValue( 2, aCurDy );
520 aCoeffsArray->SetValue( 3, aCurDz );
523 bool HYDROData_Obstacle::getTranslation( double& theDx, double& theDy, double& theDz ) const
525 theDx = theDy = theDz = 0.0;
527 TDF_Label aLabel = myLab.FindChild( DataTag_Translation, false );
528 if ( aLabel.IsNull() )
531 Handle(TDataStd_RealArray) aCoeffsArray;
532 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), aCoeffsArray ) )
535 theDx = aCoeffsArray->Value( 1 );
536 theDy = aCoeffsArray->Value( 2 );
537 theDz = aCoeffsArray->Value( 3 );
542 void HYDROData_Obstacle::UpdateLocalCS( double theDx, double theDy )
544 Translate( theDx, theDy, 0 );