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 = dumpObjectCreation( theTreatedObjects );
56 QString aName = GetObjPyName();
60 aResList << QString( "" );
61 aResList << QString( "%1.Update();" ).arg( aName );
62 aResList << QString( "" );
67 void HYDROData_Obstacle::Update()
71 checkAndSetAltitudeObject();
73 HYDROData_Entity::Update();
76 TopoDS_Shape HYDROData_Obstacle::GetTopShape() const
81 TopoDS_Shape HYDROData_Obstacle::GetShape3D() const
86 void HYDROData_Obstacle::SetShape3D( const TopoDS_Shape& theShape )
88 TopoDS_Face aShape2d = HYDROData_Projection::MakeProjection( theShape );
89 HYDROData_ArtificialObject::SetShape3D( theShape );
90 HYDROData_ArtificialObject::SetTopShape( aShape2d );
93 QColor HYDROData_Obstacle::DefaultFillingColor()
95 return QColor( Qt::yellow );
98 QColor HYDROData_Obstacle::DefaultBorderColor()
100 return QColor( Qt::transparent );
103 bool HYDROData_Obstacle::ImportFromFile( const QString& theFilePath )
105 // Check the file existence
106 QFileInfo aFileInfo( theFilePath );
107 if ( !aFileInfo.exists() ) {
115 QString aFileSuf = aFileInfo.suffix().toLower();
116 if ( aFileSuf == "brep" ) {
117 aShape = ImportBREP( theFilePath );
118 } else if ( aFileSuf == "iges" || aFileSuf == "igs" ) {
119 aShape = ImportIGES( theFilePath );
120 } else if ( aFileSuf == "step" ) {
121 aShape = ImportSTEP( theFilePath );
124 // Check the result shape
125 aRes = !aShape.IsNull();
127 // Set shape to the obstacle in case of success
129 SetShape3D( aShape );
130 SetFilePath( theFilePath );
136 void HYDROData_Obstacle::SetFilePath( const QString& theFilePath )
138 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
139 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
142 QString HYDROData_Obstacle::GetFilePath() const
146 TDF_Label aLabel = myLab.FindChild( DataTag_FilePath, false );
147 if ( !aLabel.IsNull() )
149 Handle(TDataStd_AsciiString) anAsciiStr;
150 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
151 aRes = QString( anAsciiStr->Get().ToCString() );
157 void HYDROData_Obstacle::SetGeomObjectEntry( const QString& theEntry )
159 TCollection_AsciiString anAsciiStr( theEntry.toStdString().c_str() );
160 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), anAsciiStr );
163 QString HYDROData_Obstacle::GetGeomObjectEntry() const
167 TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
168 if ( !aLabel.IsNull() )
170 Handle(TDataStd_AsciiString) anAsciiStr;
171 if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
172 aRes = QString( anAsciiStr->Get().ToCString() );
178 TopoDS_Shape HYDROData_Obstacle::ImportBREP( const QString& theFilePath ) const
180 TopoDS_Shape aResShape;
182 BRep_Builder aBrepBuilder;
183 BRepTools::Read( aResShape, qPrintable(theFilePath), aBrepBuilder );
188 TopoDS_Shape HYDROData_Obstacle::ImportIGES( const QString& theFilePath ) const
190 TopoDS_Shape aResShape;
192 // Set "C" numeric locale to save numbers correctly
193 Kernel_Utils::Localizer loc;
195 IGESControl_Reader aReader;
197 Interface_Static::SetCVal("xstep.cascade.unit","M");
202 IFSelect_ReturnStatus status = aReader.ReadFile(qPrintable(theFilePath));
204 if (status == IFSelect_RetDone) {
206 Handle(IGESData_IGESModel) aModel =
207 Handle(IGESData_IGESModel)::DownCast(aReader.Model());
208 if (!aModel.IsNull()) {
209 IGESData_GlobalSection aGS = aModel->GlobalSection();
211 aModel->SetGlobalSection(aGS);
214 aReader.ClearShapes();
215 aReader.TransferRoots();
217 aResShape = aReader.OneShape();
223 catch(Standard_Failure) {
230 TopoDS_Shape HYDROData_Obstacle::ImportSTEP( const QString& theFilePath ) const
232 TopoDS_Shape aResShape;
234 // Set "C" numeric locale to save numbers correctly
235 Kernel_Utils::Localizer loc;
237 STEPControl_Reader aReader;
240 Interface_Static::SetCVal("xstep.cascade.unit","M");
241 Interface_Static::SetIVal("read.step.ideas", 1);
242 Interface_Static::SetIVal("read.step.nonmanifold", 1);
245 TopoDS_Compound compound;
246 B.MakeCompound(compound);
251 IFSelect_ReturnStatus status = aReader.ReadFile( qPrintable(theFilePath) );
253 if (status == IFSelect_RetDone) {
255 // set UnitFlag to units from file
256 TColStd_SequenceOfAsciiString anUnitLengthNames;
257 TColStd_SequenceOfAsciiString anUnitAngleNames;
258 TColStd_SequenceOfAsciiString anUnitSolidAngleNames;
259 aReader.FileUnits(anUnitLengthNames, anUnitAngleNames, anUnitSolidAngleNames);
260 if (anUnitLengthNames.Length() > 0) {
261 TCollection_AsciiString aLenUnits = anUnitLengthNames.First();
262 if (aLenUnits == "millimetre")
263 Interface_Static::SetCVal("xstep.cascade.unit", "MM");
264 else if (aLenUnits == "centimetre")
265 Interface_Static::SetCVal("xstep.cascade.unit", "CM");
266 else if (aLenUnits == "metre" || aLenUnits.IsEmpty())
267 Interface_Static::SetCVal("xstep.cascade.unit", "M");
268 else if (aLenUnits == "INCH")
269 Interface_Static::SetCVal("xstep.cascade.unit", "INCH");
271 // The file contains not supported units
276 Standard_Boolean failsonly = Standard_False;
277 aReader.PrintCheckLoad(failsonly, IFSelect_ItemsByEntity);
280 Standard_Integer nbr = aReader.NbRootsForTransfer();
281 aReader.PrintCheckTransfer(failsonly, IFSelect_ItemsByEntity);
283 for (Standard_Integer n = 1; n <= nbr; n++) {
284 Standard_Boolean ok = aReader.TransferRoot(n);
285 // Collecting resulting entities
286 Standard_Integer nbs = aReader.NbShapes();
287 if (!ok || nbs == 0) {
288 continue; // skip empty root
290 else if (nbr == 1 && nbs == 1) { // For a single entity
291 aResShape = aReader.Shape(1);
292 // ATTENTION: this is a workaround for mantis issue 0020442 remark 0010776
293 // It should be removed after patching OCCT for bug OCC22436
294 // (fix for OCCT is expected in service pack next to OCCT6.3sp12)
295 if (aResShape.ShapeType() == TopAbs_COMPOUND) {
297 TopoDS_Shape currShape;
298 TopoDS_Iterator It (aResShape, Standard_True, Standard_True);
299 for (; It.More(); It.Next()) {
301 currShape = It.Value();
304 aResShape = currShape;
310 for (Standard_Integer i = 1; i <= nbs; i++) {
311 TopoDS_Shape aShape = aReader.Shape(i);
312 if (aShape.IsNull()) {
316 B.Add(compound, aShape);
321 if (aResShape.IsNull())
322 aResShape = compound;
324 // Check if any BRep entity has been read, there must be at least a vertex
325 if ( !TopExp_Explorer( aResShape, TopAbs_VERTEX ).More() ) {
326 // No geometrical data in the imported file
327 return TopoDS_Shape();
334 catch (Standard_Failure) {
341 QColor HYDROData_Obstacle::getDefaultFillingColor() const
343 return DefaultFillingColor();
346 QColor HYDROData_Obstacle::getDefaultBorderColor() const
348 return DefaultBorderColor();
351 void HYDROData_Obstacle::createGroupObjects()
353 TopoDS_Shape anObstacleShape = GetTopShape();
354 if ( !anObstacleShape.IsNull() )
356 TopTools_SequenceOfShape aWireEdges;
357 HYDROData_ShapesTool::ExploreShapeToShapes( anObstacleShape, TopAbs_EDGE, aWireEdges );
358 if ( !aWireEdges.IsEmpty() )
360 QString aWireGroupName = GetName() + "_Outer_Wire";
362 Handle(HYDROData_ShapesGroup) anExtWireGroup = createGroupObject();
363 anExtWireGroup->SetName( aWireGroupName );
365 anExtWireGroup->SetShapes( aWireEdges );
370 ObjectKind HYDROData_Obstacle::getAltitudeObjectType() const
372 return KIND_OBSTACLE_ALTITUDE;