2 #include "HYDROData_Obstacle.h"
4 #include "HYDROData_Document.h"
6 #include <Basics_Utils.hxx>
8 #include <BRepTools.hxx>
9 #include <BRep_Builder.hxx>
11 #include <IGESControl_Reader.hxx>
12 #include <IGESData_IGESModel.hxx>
14 #include <STEPControl_Reader.hxx>
16 #include <Interface_Static.hxx>
18 #include <TopoDS_Iterator.hxx>
19 #include <TopoDS_Shape.hxx>
21 #include <TDataStd_AsciiString.hxx>
23 #include <TColStd_SequenceOfAsciiString.hxx>
25 #include <TopExp_Explorer.hxx>
30 #include <QStringList>
32 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
34 #include <HYDROData_Projection.h>
36 #define PYTHON_OBSTACLE_ID "KIND_OBSTACLE"
38 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
39 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
42 HYDROData_Obstacle::HYDROData_Obstacle()
43 : HYDROData_ArtificialObject()
47 HYDROData_Obstacle::~HYDROData_Obstacle()
51 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
55 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
56 if ( aDocument.IsNull() )
59 QString aDocName = aDocument->GetDocPyName();
60 QString anObstacleName = GetName();
62 aResList << QString( "%1 = %2.CreateObject( %3 );" )
63 .arg( anObstacleName ).arg( aDocName ).arg( PYTHON_OBSTACLE_ID );
64 aResList << QString( "%1.SetName( \"%2\" );" )
65 .arg( anObstacleName ).arg( anObstacleName );
66 aResList << QString( "" );
73 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
79 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
85 QColor HYDROData_Obstacle::DefaultFillingColor()
87 return QColor( Qt::yellow );
90 QColor HYDROData_Obstacle::DefaultBorderColor()
92 return QColor( Qt::transparent );
95 QColor HYDROData_Obstacle::getDefaultFillingColor() const
97 return DefaultFillingColor();
100 QColor HYDROData_Obstacle::getDefaultBorderColor() const
102 return DefaultBorderColor();
105 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
107 // Check the file existence
108 QFileInfo aFileInfo( theFilePath );
109 if ( !aFileInfo.exists() ) {
117 QString aFileSuf = aFileInfo.suffix().toLower();
118 if ( aFileSuf == "brep" ) {
119 aShape = ImportBREP( theFilePath );
120 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
121 aShape = ImportIGES( theFilePath );
122 } else if ( aFileSuf == "step" ) {
123 aShape = ImportSTEP( theFilePath );
126 // Check the result shape
127 aRes = !aShape.IsNull();
129 // Set shape to the obstacle in case of success
131 SetShape3D( aShape );
132 SetFilePath( theFilePath );
138 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
140 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
141 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
144 QString HYDROData_Obstacle::GetFilePath() const
148 Handle(TDataStd_AsciiString) anAsciiStr;
149 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
150 aRes = QString( anAsciiStr->Get().ToCString() );
155 void HYDROData_Obstacle::SetGeomObjectEntry( const QString& theEntry )
157 TCollection_AsciiString anAsciiStr( theEntry.toStdString().c_str() );
158 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), anAsciiStr );
161 QString HYDROData_Obstacle::GetGeomObjectEntry() const
165 Handle(TDataStd_AsciiString) anAsciiStr;
166 if ( myLab.FindChild( DataTag_GeomObjectEntry ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
167 aRes = QString( anAsciiStr->Get().ToCString() );
172 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
174 TopoDS_Shape aResShape;
176 BRep_Builder aBrepBuilder;
177 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
182 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
184 TopoDS_Shape aResShape;
186 // Set "C" numeric locale to save numbers correctly
187 Kernel_Utils::Localizer loc;
189 IGESControl_Reader aReader;
191 Interface_Static::SetCVal("xstep.cascade.unit","M");
196 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
198 if (status == IFSelect_RetDone) {
200 Handle(IGESData_IGESModel) aModel =
201 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
202 if (!aModel.IsNull()) {
203 IGESData_GlobalSection aGS = aModel->GlobalSection();
205 aModel->SetGlobalSection(aGS);
208 aReader.ClearShapes();
209 aReader.TransferRoots();
211 aResShape = aReader.OneShape();
217 catch(Standard_Failure) {
224 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
226 TopoDS_Shape aResShape;
228 // Set "C" numeric locale to save numbers correctly
229 Kernel_Utils::Localizer loc;
231 STEPControl_Reader aReader;
234 Interface_Static::SetCVal("xstep.cascade.unit","M");
235 Interface_Static::SetIVal("read.step.ideas", 1);
236 Interface_Static::SetIVal("read.step.nonmanifold", 1);
239 TopoDS_Compound compound;
240 B.MakeCompound(compound);
245 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
247 if (status == IFSelect_RetDone) {
249 // set UnitFlag to units from file
250 TColStd_SequenceOfAsciiString anUnitLengthNames;
251 TColStd_SequenceOfAsciiString anUnitAngleNames;
252 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
253 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
254 if (anUnitLengthNames.Length() > 0) {
255 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
256 if (aLenUnits == "millimetre")
257 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
258 else if (aLenUnits == "centimetre")
259 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
260 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
261 Interface_Static::SetCVal("xstep.cascade.unit", "M");
262 else if (aLenUnits == "INCH")
263 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
265 // The file contains not supported units
270 Standard_Boolean failsonly = Standard_False;
271 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
274 Standard_Integer nbr = aReader.NbRootsForTransfer();
275 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
277 for (Standard_Integer n = 1; n <= nbr; n++) {
278 Standard_Boolean ok = aReader.TransferRoot(n);
279 // Collecting resulting entities
280 Standard_Integer nbs = aReader.NbShapes();
281 if (!ok || nbs == 0) {
282 continue; // skip empty root
284 else if (nbr == 1 && nbs == 1) { // For a single entity
285 aResShape = aReader.Shape(1);
286 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
287 // It should be removed after patching OCCT for bug OCC22436
288 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
289 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
291 TopoDS_Shape currShape;
292 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
293 for (; It.More(); It.Next()) {
295 currShape = It.Value();
298 aResShape = currShape;
304 for (Standard_Integer i = 1; i <= nbs; i++) {
305 TopoDS_Shape aShape = aReader.Shape(i);
306 if (aShape.IsNull()) {
310 B.Add(compound, aShape);
315 if (aResShape.IsNull())
316 aResShape = compound;
318 // Check if any BRep entity has been read, there must be at least a vertex
319 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
320 // No geometrical data in the imported file
321 return TopoDS_Shape();
328 catch (Standard_Failure) {
335 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
337 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
338 HYDROData_ArtificialObject::SetShape3D( theShape );
339 HYDROData_ArtificialObject::SetTopShape( aShape2d );