1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "HYDROData_Obstacle.h"
25 #include "HYDROData_Document.h"
26 #include "HYDROData_ShapesGroup.h"
27 #include "HYDROData_ShapesTool.h"
28 #include "HYDROData_Tool.h"
29 #include "HYDROData_GeomTool.h"
31 #include <Basics_Utils.hxx>
33 #include <BRepTools.hxx>
34 #include <BRep_Builder.hxx>
36 #include <IGESControl_Reader.hxx>
37 #include <IGESData_IGESModel.hxx>
39 #include <STEPControl_Reader.hxx>
41 #include <Interface_Static.hxx>
44 #include <TopoDS_Iterator.hxx>
45 #include <TopoDS_Shape.hxx>
46 #include <TopoDS_Edge.hxx>
48 #include <TDataStd_AsciiString.hxx>
49 #include <TDataStd_RealArray.hxx>
51 #include <TColStd_SequenceOfAsciiString.hxx>
53 #include <TopExp_Explorer.hxx>
58 #include <QStringList>
60 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
62 #include <HYDROData_Projection.h>
64 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
65 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
68 HYDROData_Obstacle::HYDROData_Obstacle()
69 : HYDROData_ArtificialObject()
73 HYDROData_Obstacle::~HYDROData_Obstacle()
77 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
79 QStringList aResList = dumpObjectCreation( theTreatedObjects );
81 QString anObstacleName = GetObjPyName();
83 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
84 QString aFilePath = GetFilePath();
86 if ( !aGeomObjectEntry.IsEmpty() )
88 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
89 // aResList << QString( "%1 = theStudy.FindObjectID( \"%2\" );" )
90 // .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
91 QString aGeomObjectName( GetGeomObjectName().ToCString() );
92 if ( !aGeomObjectName.isEmpty() ) {
93 aResList << QString( "%1 = theStudy.FindObjectByName( \"%2\", \"GEOM\" )[0];" )
94 .arg( aSalomeObjName ).arg( aGeomObjectName );
96 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
97 .arg( anObstacleName ).arg( aSalomeObjName );
99 aResList << QString( "%1.SetGeomObjectEntry( %2.GetID() );" )
100 .arg( anObstacleName ).arg( aSalomeObjName );
102 aResList << QString( "" );
104 else if ( !aFilePath.isEmpty() )
106 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
107 .arg( anObstacleName ).arg( aFilePath );
108 aResList << QString( "" );
111 // Write the translation points
112 double aDx, aDy, aDz;
113 if ( getTranslation( aDx, aDy, aDz ) )
115 aResList << QString( "%1.Translate( %2, %3, %4 );" )
116 .arg( anObstacleName ).arg( aDx ).arg( aDy ).arg( aDz );
117 aResList << QString( "" );
120 aResList << QString( "%1.Update();" ).arg( anObstacleName );
121 aResList << QString( "" );
126 void HYDROData_Obstacle::Update()
128 removeGroupObjects();
129 createGroupObjects();
130 checkAndSetAltitudeObject();
132 HYDROData_Entity::Update();
135 bool HYDROData_Obstacle::IsHas2dPrs() const
140 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
142 return getTopShape();
145 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
150 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
152 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
153 HYDROData_ArtificialObject::SetShape3D( theShape );
154 HYDROData_ArtificialObject::SetTopShape( aShape2d );
157 QColor HYDROData_Obstacle::DefaultFillingColor()
159 return QColor( Qt::yellow );
162 QColor HYDROData_Obstacle::DefaultBorderColor()
164 return QColor( Qt::transparent );
167 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
169 // Check the file existence
170 QFileInfo aFileInfo( theFilePath );
171 if ( !aFileInfo.exists() ) {
179 QString aFileSuf = aFileInfo.suffix().toLower();
180 if ( aFileSuf == "brep" ) {
181 aShape = ImportBREP( theFilePath );
182 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
183 aShape = ImportIGES( theFilePath );
184 } else if ( aFileSuf == "step" ) {
185 aShape = ImportSTEP( theFilePath );
188 // Check the result shape
189 aRes = !aShape.IsNull();
191 // Set shape to the obstacle in case of success
193 SetShape3D( aShape );
194 SetFilePath( theFilePath );
200 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
202 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
203 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
206 QString HYDROData_Obstacle::GetFilePath() const
210 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
211 if ( !aLabel.IsNull() )
213 Handle(TDataStd_AsciiString) anAsciiStr;
214 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
215 aRes = QString( anAsciiStr->Get().ToCString() );
221 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
225 if ( theIOR.IsEmpty() ) {
230 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
234 // TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
235 TopoDS_Shape aShape = HYDROData_GeomTool::GetShapeFromIOR( aDocId, theIOR.ToCString() );
237 if ( !aShape.IsNull() ) {
238 SetShape3D( aShape );
245 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
247 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
250 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
252 TCollection_AsciiString aRes;
254 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
255 if ( !aLabel.IsNull() )
257 Handle(TDataStd_AsciiString) anAsciiStr;
258 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
259 aRes = anAsciiStr->Get();
265 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectName() const
267 TCollection_AsciiString aRes;
270 if ( !HYDROData_Document::DocumentId( HYDROData_Document::Document( myLab ), aDocId ) || aDocId < 0 ) {
274 SALOMEDS::Study_var aDSStudy = HYDROData_GeomTool::GetStudyByID( aDocId );
276 if ( !aDSStudy->_is_nil() ) {
277 TCollection_AsciiString anEntry = GetGeomObjectEntry();
278 SALOMEDS::SObject_var aSObject = aDSStudy->FindObjectID( anEntry.ToCString() );
279 if ( !aSObject->_is_nil() ) {
280 aRes = TCollection_AsciiString( aSObject->GetName() );
287 void HYDROData_Obstacle::Translate( const double theDx,
291 TopoDS_Shape aShape3D = GetShape3D();
293 TopoDS_Shape aTranslatedShape3D = HYDROData_ShapesTool::Translated( aShape3D, theDx, theDy, theDz );
294 if ( aTranslatedShape3D.IsNull() )
297 SetShape3D( aTranslatedShape3D );
299 setTranslation( theDx, theDy, theDz );
302 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
304 TopoDS_Shape aResShape;
306 BRep_Builder aBrepBuilder;
307 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
312 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
314 TopoDS_Shape aResShape;
316 // Set "C" numeric locale to save numbers correctly
317 Kernel_Utils::Localizer loc;
319 IGESControl_Reader aReader;
321 Interface_Static::SetCVal("xstep.cascade.unit","M");
326 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
328 if (status == IFSelect_RetDone) {
330 Handle(IGESData_IGESModel) aModel =
331 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
332 if (!aModel.IsNull()) {
333 IGESData_GlobalSection aGS = aModel->GlobalSection();
335 aModel->SetGlobalSection(aGS);
338 aReader.ClearShapes();
339 aReader.TransferRoots();
341 aResShape = aReader.OneShape();
347 catch(Standard_Failure) {
354 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
356 TopoDS_Shape aResShape;
358 // Set "C" numeric locale to save numbers correctly
359 Kernel_Utils::Localizer loc;
361 STEPControl_Reader aReader;
364 Interface_Static::SetCVal("xstep.cascade.unit","M");
365 Interface_Static::SetIVal("read.step.ideas", 1);
366 Interface_Static::SetIVal("read.step.nonmanifold", 1);
369 TopoDS_Compound compound;
370 B.MakeCompound(compound);
375 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
377 if (status == IFSelect_RetDone) {
379 // set UnitFlag to units from file
380 TColStd_SequenceOfAsciiString anUnitLengthNames;
381 TColStd_SequenceOfAsciiString anUnitAngleNames;
382 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
383 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
384 if (anUnitLengthNames.Length() > 0) {
385 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
386 if (aLenUnits == "millimetre")
387 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
388 else if (aLenUnits == "centimetre")
389 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
390 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
391 Interface_Static::SetCVal("xstep.cascade.unit", "M");
392 else if (aLenUnits == "INCH")
393 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
395 // The file contains not supported units
400 Standard_Boolean failsonly = Standard_False;
401 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
404 Standard_Integer nbr = aReader.NbRootsForTransfer();
405 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
407 for (Standard_Integer n = 1; n <= nbr; n++) {
408 Standard_Boolean ok = aReader.TransferRoot(n);
409 // Collecting resulting entities
410 Standard_Integer nbs = aReader.NbShapes();
411 if (!ok || nbs == 0) {
412 continue; // skip empty root
414 else if (nbr == 1 && nbs == 1) { // For a single entity
415 aResShape = aReader.Shape(1);
416 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
417 // It should be removed after patching OCCT for bug OCC22436
418 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
419 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
421 TopoDS_Shape currShape;
422 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
423 for (; It.More(); It.Next()) {
425 currShape = It.Value();
428 aResShape = currShape;
434 for (Standard_Integer i = 1; i <= nbs; i++) {
435 TopoDS_Shape aShape = aReader.Shape(i);
436 if (aShape.IsNull()) {
440 B.Add(compound, aShape);
445 if (aResShape.IsNull())
446 aResShape = compound;
448 // Check if any BRep entity has been read, there must be at least a vertex
449 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
450 // No geometrical data in the imported file
451 return TopoDS_Shape();
458 catch (Standard_Failure) {
465 QColor HYDROData_Obstacle::getDefaultFillingColor() const
467 return DefaultFillingColor();
470 QColor HYDROData_Obstacle::getDefaultBorderColor() const
472 return DefaultBorderColor();
475 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
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 );