2 #include "HYDROData_Obstacle.h"
4 #include "HYDROData_Document.h"
6 #include <Basics_Utils.hxx>
8 #include <BRepTools.hxx>
9 #include <BRep_Builder.hxx>
10 #include <IGESControl_Reader.hxx>
11 #include <IGESData_IGESModel.hxx>
12 #include <STEPControl_Reader.hxx>
13 #include <Interface_Static.hxx>
14 #include <TopoDS_Iterator.hxx>
15 #include <TopoDS_Shape.hxx>
16 #include <TDataStd_AsciiString.hxx>
17 #include <TColStd_SequenceOfAsciiString.hxx>
18 #include <TopExp_Explorer.hxx>
22 #include <QStringList>
24 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
26 #define PYTHON_OBSTACLE_ID "KIND_OBSTACLE"
28 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
29 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
32 HYDROData_Obstacle::HYDROData_Obstacle()
33 : HYDROData_ArtificialObject()
37 HYDROData_Obstacle::~HYDROData_Obstacle()
41 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
45 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
46 if ( aDocument.IsNull() )
49 QString aDocName = aDocument->GetDocPyName();
50 QString anObstacleName = GetName();
52 aResList << QString( "%1 = %2.CreateObject( %3 );" )
53 .arg( anObstacleName ).arg( aDocName ).arg( PYTHON_OBSTACLE_ID );
54 aResList << QString( "%1.SetName( \"%2\" );" )
55 .arg( anObstacleName ).arg( anObstacleName );
56 aResList << QString( "" );
63 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
69 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
75 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
77 // Check the file existence
78 QFileInfo aFileInfo( theFilePath );
79 if ( !aFileInfo.exists() ) {
87 QString aFileSuf = aFileInfo.suffix().toLower();
88 if ( aFileSuf == "brep" ) {
89 aShape = ImportBREP( theFilePath );
90 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
91 aShape = ImportIGES( theFilePath );
92 } else if ( aFileSuf == "step" ) {
93 aShape = ImportSTEP( theFilePath );
96 // Check the result shape
97 aRes = !aShape.IsNull();
99 // Set shape to the obstacle in case of success
101 SetShape3D( aShape );
102 SetFilePath( theFilePath );
108 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
110 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
111 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
114 QString HYDROData_Obstacle::GetFilePath() const
118 Handle(TDataStd_AsciiString) anAsciiStr;
119 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
120 aRes = QString( anAsciiStr->Get().ToCString() );
125 void HYDROData_Obstacle::SetGeomObjectEntry( const QString& theEntry )
127 TCollection_AsciiString anAsciiStr( theEntry.toStdString().c_str() );
128 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), anAsciiStr );
131 QString HYDROData_Obstacle::GetGeomObjectEntry() const
135 Handle(TDataStd_AsciiString) anAsciiStr;
136 if ( myLab.FindChild( DataTag_GeomObjectEntry ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
137 aRes = QString( anAsciiStr->Get().ToCString() );
142 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
144 TopoDS_Shape aResShape;
146 BRep_Builder aBrepBuilder;
147 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
152 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
154 TopoDS_Shape aResShape;
156 // Set "C" numeric locale to save numbers correctly
157 Kernel_Utils::Localizer loc;
159 IGESControl_Reader aReader;
161 Interface_Static::SetCVal("xstep.cascade.unit","M");
166 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
168 if (status == IFSelect_RetDone) {
170 Handle(IGESData_IGESModel) aModel =
171 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
172 if (!aModel.IsNull()) {
173 IGESData_GlobalSection aGS = aModel->GlobalSection();
175 aModel->SetGlobalSection(aGS);
178 aReader.ClearShapes();
179 aReader.TransferRoots();
181 aResShape = aReader.OneShape();
187 catch(Standard_Failure) {
194 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
196 TopoDS_Shape aResShape;
198 // Set "C" numeric locale to save numbers correctly
199 Kernel_Utils::Localizer loc;
201 STEPControl_Reader aReader;
204 Interface_Static::SetCVal("xstep.cascade.unit","M");
205 Interface_Static::SetIVal("read.step.ideas", 1);
206 Interface_Static::SetIVal("read.step.nonmanifold", 1);
209 TopoDS_Compound compound;
210 B.MakeCompound(compound);
215 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
217 if (status == IFSelect_RetDone) {
219 // set UnitFlag to units from file
220 TColStd_SequenceOfAsciiString anUnitLengthNames;
221 TColStd_SequenceOfAsciiString anUnitAngleNames;
222 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
223 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
224 if (anUnitLengthNames.Length() > 0) {
225 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
226 if (aLenUnits == "millimetre")
227 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
228 else if (aLenUnits == "centimetre")
229 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
230 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
231 Interface_Static::SetCVal("xstep.cascade.unit", "M");
232 else if (aLenUnits == "INCH")
233 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
235 // The file contains not supported units
240 Standard_Boolean failsonly = Standard_False;
241 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
244 Standard_Integer nbr = aReader.NbRootsForTransfer();
245 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
247 for (Standard_Integer n = 1; n <= nbr; n++) {
248 Standard_Boolean ok = aReader.TransferRoot(n);
249 // Collecting resulting entities
250 Standard_Integer nbs = aReader.NbShapes();
251 if (!ok || nbs == 0) {
252 continue; // skip empty root
254 else if (nbr == 1 && nbs == 1) { // For a single entity
255 aResShape = aReader.Shape(1);
256 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
257 // It should be removed after patching OCCT for bug OCC22436
258 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
259 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
261 TopoDS_Shape currShape;
262 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
263 for (; It.More(); It.Next()) {
265 currShape = It.Value();
268 aResShape = currShape;
274 for (Standard_Integer i = 1; i <= nbs; i++) {
275 TopoDS_Shape aShape = aReader.Shape(i);
276 if (aShape.IsNull()) {
280 B.Add(compound, aShape);
285 if (aResShape.IsNull())
286 aResShape = compound;
288 // Check if any BRep entity has been read, there must be at least a vertex
289 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
290 // No geometrical data in the imported file
291 return TopoDS_Shape();
298 catch (Standard_Failure) {