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 TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
63 QString aFilePath = GetFilePath();
65 if ( !aGeomObjectEntry.IsEmpty() )
67 QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "obstacle_sobj" );
68 aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" );" )
69 .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
71 aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() );" )
72 .arg( anObstacleName ).arg( aSalomeObjName );
74 aResList << QString( "%1.SetGeomObjectEntry( \"%2\" );" )
75 .arg( anObstacleName ).arg( aGeomObjectEntry.ToCString() );
77 else if ( !aFilePath.isEmpty() )
79 aResList << QString( "%1.ImportFromFile( \"%2\" );" )
80 .arg( anObstacleName ).arg( aFilePath );
83 aResList << QString( "" );
85 aResList << QString( "%1.Update();" ).arg( anObstacleName );
86 aResList << QString( "" );
91 void HYDROData_Obstacle::Update()
95 checkAndSetAltitudeObject();
97 HYDROData_Entity::Update();
100 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
102 return getTopShape();
105 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
110 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
112 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
113 HYDROData_ArtificialObject::SetShape3D( theShape );
114 HYDROData_ArtificialObject::SetTopShape( aShape2d );
117 QColor HYDROData_Obstacle::DefaultFillingColor()
119 return QColor( Qt::yellow );
122 QColor HYDROData_Obstacle::DefaultBorderColor()
124 return QColor( Qt::transparent );
127 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
129 // Check the file existence
130 QFileInfo aFileInfo( theFilePath );
131 if ( !aFileInfo.exists() ) {
139 QString aFileSuf = aFileInfo.suffix().toLower();
140 if ( aFileSuf == "brep" ) {
141 aShape = ImportBREP( theFilePath );
142 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
143 aShape = ImportIGES( theFilePath );
144 } else if ( aFileSuf == "step" ) {
145 aShape = ImportSTEP( theFilePath );
148 // Check the result shape
149 aRes = !aShape.IsNull();
151 // Set shape to the obstacle in case of success
153 SetShape3D( aShape );
154 SetFilePath( theFilePath );
160 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
162 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
163 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
166 QString HYDROData_Obstacle::GetFilePath() const
170 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
171 if ( !aLabel.IsNull() )
173 Handle(TDataStd_AsciiString) anAsciiStr;
174 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
175 aRes = QString( anAsciiStr->Get().ToCString() );
181 bool HYDROData_Obstacle::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
183 if ( theIOR.IsEmpty() )
186 TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
187 if ( aShape.IsNull() )
190 SetShape3D( aShape );
195 void HYDROData_Obstacle::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
197 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
200 TCollection_AsciiString HYDROData_Obstacle::GetGeomObjectEntry() const
202 TCollection_AsciiString aRes;
204 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
205 if ( !aLabel.IsNull() )
207 Handle(TDataStd_AsciiString) anAsciiStr;
208 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
209 aRes = anAsciiStr->Get();
215 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
217 TopoDS_Shape aResShape;
219 BRep_Builder aBrepBuilder;
220 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
225 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
227 TopoDS_Shape aResShape;
229 // Set "C" numeric locale to save numbers correctly
230 Kernel_Utils::Localizer loc;
232 IGESControl_Reader aReader;
234 Interface_Static::SetCVal("xstep.cascade.unit","M");
239 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
241 if (status == IFSelect_RetDone) {
243 Handle(IGESData_IGESModel) aModel =
244 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
245 if (!aModel.IsNull()) {
246 IGESData_GlobalSection aGS = aModel->GlobalSection();
248 aModel->SetGlobalSection(aGS);
251 aReader.ClearShapes();
252 aReader.TransferRoots();
254 aResShape = aReader.OneShape();
260 catch(Standard_Failure) {
267 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
269 TopoDS_Shape aResShape;
271 // Set "C" numeric locale to save numbers correctly
272 Kernel_Utils::Localizer loc;
274 STEPControl_Reader aReader;
277 Interface_Static::SetCVal("xstep.cascade.unit","M");
278 Interface_Static::SetIVal("read.step.ideas", 1);
279 Interface_Static::SetIVal("read.step.nonmanifold", 1);
282 TopoDS_Compound compound;
283 B.MakeCompound(compound);
288 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
290 if (status == IFSelect_RetDone) {
292 // set UnitFlag to units from file
293 TColStd_SequenceOfAsciiString anUnitLengthNames;
294 TColStd_SequenceOfAsciiString anUnitAngleNames;
295 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
296 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
297 if (anUnitLengthNames.Length() > 0) {
298 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
299 if (aLenUnits == "millimetre")
300 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
301 else if (aLenUnits == "centimetre")
302 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
303 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
304 Interface_Static::SetCVal("xstep.cascade.unit", "M");
305 else if (aLenUnits == "INCH")
306 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
308 // The file contains not supported units
313 Standard_Boolean failsonly = Standard_False;
314 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
317 Standard_Integer nbr = aReader.NbRootsForTransfer();
318 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
320 for (Standard_Integer n = 1; n <= nbr; n++) {
321 Standard_Boolean ok = aReader.TransferRoot(n);
322 // Collecting resulting entities
323 Standard_Integer nbs = aReader.NbShapes();
324 if (!ok || nbs == 0) {
325 continue; // skip empty root
327 else if (nbr == 1 && nbs == 1) { // For a single entity
328 aResShape = aReader.Shape(1);
329 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
330 // It should be removed after patching OCCT for bug OCC22436
331 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
332 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
334 TopoDS_Shape currShape;
335 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
336 for (; It.More(); It.Next()) {
338 currShape = It.Value();
341 aResShape = currShape;
347 for (Standard_Integer i = 1; i <= nbs; i++) {
348 TopoDS_Shape aShape = aReader.Shape(i);
349 if (aShape.IsNull()) {
353 B.Add(compound, aShape);
358 if (aResShape.IsNull())
359 aResShape = compound;
361 // Check if any BRep entity has been read, there must be at least a vertex
362 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
363 // No geometrical data in the imported file
364 return TopoDS_Shape();
371 catch (Standard_Failure) {
378 QColor HYDROData_Obstacle::getDefaultFillingColor() const
380 return DefaultFillingColor();
383 QColor HYDROData_Obstacle::getDefaultBorderColor() const
385 return DefaultBorderColor();
388 void HYDROData_Obstacle::createGroupObjects()
390 TopoDS_Shape anObstacleShape = GetTopShape();
391 if ( !anObstacleShape.IsNull() )
393 TopTools_SequenceOfShape aWireEdges;
394 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
395 if ( !aWireEdges.IsEmpty() )
397 QString aWireGroupName = GetName() + "_Outer_Wire";
399 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
400 anExtWireGroup->SetName( aWireGroupName );
402 anExtWireGroup->SetShapes( aWireEdges );
407 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
409 return KIND_OBSTACLE_ALTITUDE;