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 IMPLEMENT_STANDARD_HANDLE(HYDROData_Obstacle,HYDROData_ArtificialObject)
41 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Obstacle,HYDROData_ArtificialObject)
44 HYDROData_Obstacle::HYDROData_Obstacle()
45 : HYDROData_ArtificialObject()
49 HYDROData_Obstacle::~HYDROData_Obstacle()
53 QStringList HYDROData_Obstacle::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
55 QStringList aResList = HYDROData_Entity::DumpToPython( theTreatedObjects );
62 void HYDROData_Obstacle::Update()
66 checkAndSetAltitudeObject();
68 HYDROData_Entity::Update();
71 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
76 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
81 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
83 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
84 HYDROData_ArtificialObject::SetShape3D( theShape );
85 HYDROData_ArtificialObject::SetTopShape( aShape2d );
88 QColor HYDROData_Obstacle::DefaultFillingColor()
90 return QColor( Qt::yellow );
93 QColor HYDROData_Obstacle::DefaultBorderColor()
95 return QColor( Qt::transparent );
98 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
100 // Check the file existence
101 QFileInfo aFileInfo( theFilePath );
102 if ( !aFileInfo.exists() ) {
110 QString aFileSuf = aFileInfo.suffix().toLower();
111 if ( aFileSuf == "brep" ) {
112 aShape = ImportBREP( theFilePath );
113 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
114 aShape = ImportIGES( theFilePath );
115 } else if ( aFileSuf == "step" ) {
116 aShape = ImportSTEP( theFilePath );
119 // Check the result shape
120 aRes = !aShape.IsNull();
122 // Set shape to the obstacle in case of success
124 SetShape3D( aShape );
125 SetFilePath( theFilePath );
131 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
133 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
134 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
137 QString HYDROData_Obstacle::GetFilePath() const
141 Handle(TDataStd_AsciiString) anAsciiStr;
142 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
143 aRes = QString( anAsciiStr->Get().ToCString() );
148 void HYDROData_Obstacle::SetGeomObjectEntry( const QString& theEntry )
150 TCollection_AsciiString anAsciiStr( theEntry.toStdString().c_str() );
151 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), anAsciiStr );
154 QString HYDROData_Obstacle::GetGeomObjectEntry() const
158 Handle(TDataStd_AsciiString) anAsciiStr;
159 if ( myLab.FindChild( DataTag_GeomObjectEntry ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
160 aRes = QString( anAsciiStr->Get().ToCString() );
165 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
167 TopoDS_Shape aResShape;
169 BRep_Builder aBrepBuilder;
170 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
175 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
177 TopoDS_Shape aResShape;
179 // Set "C" numeric locale to save numbers correctly
180 Kernel_Utils::Localizer loc;
182 IGESControl_Reader aReader;
184 Interface_Static::SetCVal("xstep.cascade.unit","M");
189 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
191 if (status == IFSelect_RetDone) {
193 Handle(IGESData_IGESModel) aModel =
194 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
195 if (!aModel.IsNull()) {
196 IGESData_GlobalSection aGS = aModel->GlobalSection();
198 aModel->SetGlobalSection(aGS);
201 aReader.ClearShapes();
202 aReader.TransferRoots();
204 aResShape = aReader.OneShape();
210 catch(Standard_Failure) {
217 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
219 TopoDS_Shape aResShape;
221 // Set "C" numeric locale to save numbers correctly
222 Kernel_Utils::Localizer loc;
224 STEPControl_Reader aReader;
227 Interface_Static::SetCVal("xstep.cascade.unit","M");
228 Interface_Static::SetIVal("read.step.ideas", 1);
229 Interface_Static::SetIVal("read.step.nonmanifold", 1);
232 TopoDS_Compound compound;
233 B.MakeCompound(compound);
238 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
240 if (status == IFSelect_RetDone) {
242 // set UnitFlag to units from file
243 TColStd_SequenceOfAsciiString anUnitLengthNames;
244 TColStd_SequenceOfAsciiString anUnitAngleNames;
245 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
246 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
247 if (anUnitLengthNames.Length() > 0) {
248 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
249 if (aLenUnits == "millimetre")
250 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
251 else if (aLenUnits == "centimetre")
252 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
253 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
254 Interface_Static::SetCVal("xstep.cascade.unit", "M");
255 else if (aLenUnits == "INCH")
256 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
258 // The file contains not supported units
263 Standard_Boolean failsonly = Standard_False;
264 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
267 Standard_Integer nbr = aReader.NbRootsForTransfer();
268 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
270 for (Standard_Integer n = 1; n <= nbr; n++) {
271 Standard_Boolean ok = aReader.TransferRoot(n);
272 // Collecting resulting entities
273 Standard_Integer nbs = aReader.NbShapes();
274 if (!ok || nbs == 0) {
275 continue; // skip empty root
277 else if (nbr == 1 && nbs == 1) { // For a single entity
278 aResShape = aReader.Shape(1);
279 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
280 // It should be removed after patching OCCT for bug OCC22436
281 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
282 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
284 TopoDS_Shape currShape;
285 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
286 for (; It.More(); It.Next()) {
288 currShape = It.Value();
291 aResShape = currShape;
297 for (Standard_Integer i = 1; i <= nbs; i++) {
298 TopoDS_Shape aShape = aReader.Shape(i);
299 if (aShape.IsNull()) {
303 B.Add(compound, aShape);
308 if (aResShape.IsNull())
309 aResShape = compound;
311 // Check if any BRep entity has been read, there must be at least a vertex
312 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
313 // No geometrical data in the imported file
314 return TopoDS_Shape();
321 catch (Standard_Failure) {
328 QColor HYDROData_Obstacle::getDefaultFillingColor() const
330 return DefaultFillingColor();
333 QColor HYDROData_Obstacle::getDefaultBorderColor() const
335 return DefaultBorderColor();
338 void HYDROData_Obstacle::createGroupObjects()
340 TopoDS_Shape anObstacleShape = GetTopShape();
341 if ( !anObstacleShape.IsNull() )
343 TopTools_SequenceOfShape aWireEdges;
344 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
345 if ( !aWireEdges.IsEmpty() )
347 QString aWireGroupName = GetName() + "_Outer_Wire";
349 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
350 anExtWireGroup->SetName( aWireGroupName );
352 anExtWireGroup->SetShapes( aWireEdges );
357 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
359 return KIND_OBSTACLE_ALTITUDE;