2 #include "HYDROData_Obstacle.h"
4 #include "HYDROData_Document.h"
5 #include "HYDROData_ShapesGroup.h"
6 #include "HYDROData_ShapesTool.h"
8 #include <Basics_Utils.hxx>
10 #include <BRepTools.hxx>
11 #include <BRep_Builder.hxx>
13 #include <IGESControl_Reader.hxx>
14 #include <IGESData_IGESModel.hxx>
16 #include <STEPControl_Reader.hxx>
18 #include <Interface_Static.hxx>
21 #include <TopoDS_Iterator.hxx>
22 #include <TopoDS_Shape.hxx>
23 #include <TopoDS_Edge.hxx>
25 #include <TDataStd_AsciiString.hxx>
27 #include <TColStd_SequenceOfAsciiString.hxx>
29 #include <TopExp_Explorer.hxx>
34 #include <QStringList>
36 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic
38 #include <HYDROData_Projection.h>
40 #define PYTHON_OBSTACLE_ID "KIND_OBSTACLE"
42 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
43 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
46 HYDROData_Obstacle::HYDROData_Obstacle()
47 : HYDROData_ArtificialObject()
51 HYDROData_Obstacle::~HYDROData_Obstacle()
55 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
59 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
60 if ( aDocument.IsNull() )
63 QString aDocName = aDocument->GetDocPyName();
64 QString anObstacleName = GetName();
66 aResList << QString( "%1 = %2.CreateObject( %3 );" )
67 .arg( anObstacleName ).arg( aDocName ).arg( PYTHON_OBSTACLE_ID );
68 aResList << QString( "%1.SetName( \"%2\" );" )
69 .arg( anObstacleName ).arg( anObstacleName );
70 aResList << QString( "" );
77 void HYDROData_Obstacle::Update()
79 HYDROData_Entity::Update();
85 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
90 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
95 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
97 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
98 HYDROData_ArtificialObject::SetShape3D( theShape );
99 HYDROData_ArtificialObject::SetTopShape( aShape2d );
102 QColor HYDROData_Obstacle::DefaultFillingColor()
104 return QColor( Qt::yellow );
107 QColor HYDROData_Obstacle::DefaultBorderColor()
109 return QColor( Qt::transparent );
112 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
114 // Check the file existence
115 QFileInfo aFileInfo( theFilePath );
116 if ( !aFileInfo.exists() ) {
124 QString aFileSuf = aFileInfo.suffix().toLower();
125 if ( aFileSuf == "brep" ) {
126 aShape = ImportBREP( theFilePath );
127 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
128 aShape = ImportIGES( theFilePath );
129 } else if ( aFileSuf == "step" ) {
130 aShape = ImportSTEP( theFilePath );
133 // Check the result shape
134 aRes = !aShape.IsNull();
136 // Set shape to the obstacle in case of success
138 SetShape3D( aShape );
139 SetFilePath( theFilePath );
145 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
147 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
148 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
151 QString HYDROData_Obstacle::GetFilePath() const
155 Handle(TDataStd_AsciiString) anAsciiStr;
156 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
157 aRes = QString( anAsciiStr->Get().ToCString() );
162 void HYDROData_Obstacle::SetGeomObjectEntry( const QString& theEntry )
164 TCollection_AsciiString anAsciiStr( theEntry.toStdString().c_str() );
165 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), anAsciiStr );
168 QString HYDROData_Obstacle::GetGeomObjectEntry() const
172 Handle(TDataStd_AsciiString) anAsciiStr;
173 if ( myLab.FindChild( DataTag_GeomObjectEntry ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
174 aRes = QString( anAsciiStr->Get().ToCString() );
179 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
181 TopoDS_Shape aResShape;
183 BRep_Builder aBrepBuilder;
184 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
189 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
191 TopoDS_Shape aResShape;
193 // Set "C" numeric locale to save numbers correctly
194 Kernel_Utils::Localizer loc;
196 IGESControl_Reader aReader;
198 Interface_Static::SetCVal("xstep.cascade.unit","M");
203 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
205 if (status == IFSelect_RetDone) {
207 Handle(IGESData_IGESModel) aModel =
208 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
209 if (!aModel.IsNull()) {
210 IGESData_GlobalSection aGS = aModel->GlobalSection();
212 aModel->SetGlobalSection(aGS);
215 aReader.ClearShapes();
216 aReader.TransferRoots();
218 aResShape = aReader.OneShape();
224 catch(Standard_Failure) {
231 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
233 TopoDS_Shape aResShape;
235 // Set "C" numeric locale to save numbers correctly
236 Kernel_Utils::Localizer loc;
238 STEPControl_Reader aReader;
241 Interface_Static::SetCVal("xstep.cascade.unit","M");
242 Interface_Static::SetIVal("read.step.ideas", 1);
243 Interface_Static::SetIVal("read.step.nonmanifold", 1);
246 TopoDS_Compound compound;
247 B.MakeCompound(compound);
252 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
254 if (status == IFSelect_RetDone) {
256 // set UnitFlag to units from file
257 TColStd_SequenceOfAsciiString anUnitLengthNames;
258 TColStd_SequenceOfAsciiString anUnitAngleNames;
259 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
260 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
261 if (anUnitLengthNames.Length() > 0) {
262 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
263 if (aLenUnits == "millimetre")
264 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
265 else if (aLenUnits == "centimetre")
266 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
267 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
268 Interface_Static::SetCVal("xstep.cascade.unit", "M");
269 else if (aLenUnits == "INCH")
270 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
272 // The file contains not supported units
277 Standard_Boolean failsonly = Standard_False;
278 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
281 Standard_Integer nbr = aReader.NbRootsForTransfer();
282 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
284 for (Standard_Integer n = 1; n <= nbr; n++) {
285 Standard_Boolean ok = aReader.TransferRoot(n);
286 // Collecting resulting entities
287 Standard_Integer nbs = aReader.NbShapes();
288 if (!ok || nbs == 0) {
289 continue; // skip empty root
291 else if (nbr == 1 && nbs == 1) { // For a single entity
292 aResShape = aReader.Shape(1);
293 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
294 // It should be removed after patching OCCT for bug OCC22436
295 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
296 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
298 TopoDS_Shape currShape;
299 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
300 for (; It.More(); It.Next()) {
302 currShape = It.Value();
305 aResShape = currShape;
311 for (Standard_Integer i = 1; i <= nbs; i++) {
312 TopoDS_Shape aShape = aReader.Shape(i);
313 if (aShape.IsNull()) {
317 B.Add(compound, aShape);
322 if (aResShape.IsNull())
323 aResShape = compound;
325 // Check if any BRep entity has been read, there must be at least a vertex
326 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
327 // No geometrical data in the imported file
328 return TopoDS_Shape();
335 catch (Standard_Failure) {
342 QColor HYDROData_Obstacle::getDefaultFillingColor() const
344 return DefaultFillingColor();
347 QColor HYDROData_Obstacle::getDefaultBorderColor() const
349 return DefaultBorderColor();
352 void HYDROData_Obstacle::createGroupObjects()
354 TopoDS_Shape anObstacleShape = GetTopShape();
355 if ( !anObstacleShape.IsNull() )
357 TopTools_SequenceOfShape aWireEdges;
358 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
359 if ( !aWireEdges.IsEmpty() )
361 QString aWireGroupName = GetName() + "_Outer_Wire";
363 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
364 anExtWireGroup->SetName( aWireGroupName );
366 anExtWireGroup->SetShapes( aWireEdges );
371 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
373 return KIND_OBSTACLE_ALTITUDE;