2 #include "HYDROData_Obstacle.h"
4 #include "HYDROData_Document.h"
5 #include "HYDROData_ShapesGroup.h"
6 #include "HYDROData_ShapesTool.h"
7 #include "HYDROData_Tool.h"
9 #include <Basics_Utils.hxx>
11 #include <BRepTools.hxx>
12 #include <BRep_Builder.hxx>
16 #include <IGESControl_Reader.hxx>
17 #include <IGESData_IGESModel.hxx>
19 #include <STEPControl_Reader.hxx>
21 #include <Interface_Static.hxx>
24 #include <TopoDS_Iterator.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopoDS_Edge.hxx>
28 #include <TDataStd_AsciiString.hxx>
30 #include <TColStd_SequenceOfAsciiString.hxx>
32 #include <TopExp_Explorer.hxx>
37 #include <QStringList>
39 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
41 #include <HYDROData_Projection.h>
43 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
44 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
47 HYDROData_Obstacle::HYDROData_Obstacle()
48 : HYDROData_ArtificialObject()
52 HYDROData_Obstacle::~HYDROData_Obstacle()
56 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
58 QStringList aResList = dumpObjectCreation( theTreatedObjects );
60 QString anObstacleName = GetObjPyName();
62 QString aGeomObjectEntry = GetGeomObjectEntry();
63 QString aFilePath = GetFilePath();
65 if ( !aGeomObjectEntry.isEmpty() )
67 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
68 aResList << QString( "%1 = salome.sg.IDToObject( \"%2\" );" )
69 .arg( aSalomeObjName ).arg( aGeomObjectEntry );
71 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
72 .arg( anObstacleName ).arg( aSalomeObjName );
74 else if ( !aFilePath.isEmpty() )
76 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
77 .arg( anObstacleName ).arg( aFilePath );
80 aResList << QString( "" );
82 aResList << QString( "%1.Update();" ).arg( anObstacleName );
83 aResList << QString( "" );
88 void HYDROData_Obstacle::Update()
92 checkAndSetAltitudeObject();
94 HYDROData_Entity::Update();
97 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
102 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
107 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
109 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
110 HYDROData_ArtificialObject::SetShape3D( theShape );
111 HYDROData_ArtificialObject::SetTopShape( aShape2d );
114 QColor HYDROData_Obstacle::DefaultFillingColor()
116 return QColor( Qt::yellow );
119 QColor HYDROData_Obstacle::DefaultBorderColor()
121 return QColor( Qt::transparent );
124 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
126 // Check the file existence
127 QFileInfo aFileInfo( theFilePath );
128 if ( !aFileInfo.exists() ) {
136 QString aFileSuf = aFileInfo.suffix().toLower();
137 if ( aFileSuf == "brep" ) {
138 aShape = ImportBREP( theFilePath );
139 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
140 aShape = ImportIGES( theFilePath );
141 } else if ( aFileSuf == "step" ) {
142 aShape = ImportSTEP( theFilePath );
145 // Check the result shape
146 aRes = !aShape.IsNull();
148 // Set shape to the obstacle in case of success
150 SetShape3D( aShape );
151 SetFilePath( theFilePath );
157 bool HYDROData_Obstacle::ImportFromGeomIOR( const QString& theIOR )
159 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR );
160 if ( aShape.IsNull() )
163 SetShape3D( aShape );
164 SetGeomObjectEntry( theIOR );
169 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
171 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
172 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
175 QString HYDROData_Obstacle::GetFilePath() const
179 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
180 if ( !aLabel.IsNull() )
182 Handle(TDataStd_AsciiString) anAsciiStr;
183 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
184 aRes = QString( anAsciiStr->Get().ToCString() );
190 void HYDROData_Obstacle::SetGeomObjectEntry( const QString& theEntry )
192 TCollection_AsciiString anAsciiStr( theEntry.toStdString().c_str() );
193 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), anAsciiStr );
196 QString HYDROData_Obstacle::GetGeomObjectEntry() const
200 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
201 if ( !aLabel.IsNull() )
203 Handle(TDataStd_AsciiString) anAsciiStr;
204 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
205 aRes = QString( anAsciiStr->Get().ToCString() );
211 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
213 TopoDS_Shape aResShape;
215 BRep_Builder aBrepBuilder;
216 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
221 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
223 TopoDS_Shape aResShape;
225 // Set "C" numeric locale to save numbers correctly
226 Kernel_Utils::Localizer loc;
228 IGESControl_Reader aReader;
230 Interface_Static::SetCVal("xstep.cascade.unit","M");
235 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
237 if (status == IFSelect_RetDone) {
239 Handle(IGESData_IGESModel) aModel =
240 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
241 if (!aModel.IsNull()) {
242 IGESData_GlobalSection aGS = aModel->GlobalSection();
244 aModel->SetGlobalSection(aGS);
247 aReader.ClearShapes();
248 aReader.TransferRoots();
250 aResShape = aReader.OneShape();
256 catch(Standard_Failure) {
263 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
265 TopoDS_Shape aResShape;
267 // Set "C" numeric locale to save numbers correctly
268 Kernel_Utils::Localizer loc;
270 STEPControl_Reader aReader;
273 Interface_Static::SetCVal("xstep.cascade.unit","M");
274 Interface_Static::SetIVal("read.step.ideas", 1);
275 Interface_Static::SetIVal("read.step.nonmanifold", 1);
278 TopoDS_Compound compound;
279 B.MakeCompound(compound);
284 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
286 if (status == IFSelect_RetDone) {
288 // set UnitFlag to units from file
289 TColStd_SequenceOfAsciiString anUnitLengthNames;
290 TColStd_SequenceOfAsciiString anUnitAngleNames;
291 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
292 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
293 if (anUnitLengthNames.Length() > 0) {
294 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
295 if (aLenUnits == "millimetre")
296 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
297 else if (aLenUnits == "centimetre")
298 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
299 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
300 Interface_Static::SetCVal("xstep.cascade.unit", "M");
301 else if (aLenUnits == "INCH")
302 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
304 // The file contains not supported units
309 Standard_Boolean failsonly = Standard_False;
310 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
313 Standard_Integer nbr = aReader.NbRootsForTransfer();
314 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
316 for (Standard_Integer n = 1; n <= nbr; n++) {
317 Standard_Boolean ok = aReader.TransferRoot(n);
318 // Collecting resulting entities
319 Standard_Integer nbs = aReader.NbShapes();
320 if (!ok || nbs == 0) {
321 continue; // skip empty root
323 else if (nbr == 1 && nbs == 1) { // For a single entity
324 aResShape = aReader.Shape(1);
325 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
326 // It should be removed after patching OCCT for bug OCC22436
327 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
328 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
330 TopoDS_Shape currShape;
331 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
332 for (; It.More(); It.Next()) {
334 currShape = It.Value();
337 aResShape = currShape;
343 for (Standard_Integer i = 1; i <= nbs; i++) {
344 TopoDS_Shape aShape = aReader.Shape(i);
345 if (aShape.IsNull()) {
349 B.Add(compound, aShape);
354 if (aResShape.IsNull())
355 aResShape = compound;
357 // Check if any BRep entity has been read, there must be at least a vertex
358 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
359 // No geometrical data in the imported file
360 return TopoDS_Shape();
367 catch (Standard_Failure) {
374 QColor HYDROData_Obstacle::getDefaultFillingColor() const
376 return DefaultFillingColor();
379 QColor HYDROData_Obstacle::getDefaultBorderColor() const
381 return DefaultBorderColor();
384 void HYDROData_Obstacle::createGroupObjects()
386 TopoDS_Shape anObstacleShape = GetTopShape();
387 if ( !anObstacleShape.IsNull() )
389 TopTools_SequenceOfShape aWireEdges;
390 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
391 if ( !aWireEdges.IsEmpty() )
393 QString aWireGroupName = GetName() + "_Outer_Wire";
395 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
396 anExtWireGroup->SetName( aWireGroupName );
398 anExtWireGroup->SetShapes( aWireEdges );
403 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
405 return KIND_OBSTACLE_ALTITUDE;