2 #include "HYDROData_Profile.h"
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Iterator.h"
6 #include "HYDROData_Tool.h"
7 #include "HYDROData_PolylineXY.h"
9 #include <boost/math/special_functions/fpclassify.hpp>
11 #include <BRepBuilderAPI_MakeEdge.hxx>
12 #include <BRepBuilderAPI_MakeWire.hxx>
13 #include <BRepBuilderAPI_MakePolygon.hxx>
17 #include <gp_Pnt2d.hxx>
19 #include <TDataStd_AsciiString.hxx>
20 #include <TDataStd_RealArray.hxx>
22 #include <TopoDS_Edge.hxx>
23 #include <TopoDS_Wire.hxx>
25 #include <OSD_File.hxx>
26 #include <OSD_Protection.hxx>
29 #include <QStringList>
31 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
32 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
34 HYDROData_Profile::HYDROData_Profile()
39 HYDROData_Profile::~HYDROData_Profile()
43 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
45 QStringList aResList = HYDROData_Entity::DumpToPython( theTreatedObjects );
52 TopoDS_Shape HYDROData_Profile::GetTopShape() const
56 gp_XY aFirstPoint, aLastPoint;
57 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
60 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
61 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
63 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
64 TopoDS_Edge anEdge = aMakeEdge;
66 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
72 TopoDS_Shape HYDROData_Profile::GetShape3D() const
77 void HYDROData_Profile::Update()
79 HYDROData_Object::Update();
82 Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
83 if ( !aProfile.IsNull() )
85 ProfilePoints aProfilePoints = GetProfilePoints();
86 HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
88 aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
93 QColor HYDROData_Profile::DefaultFillingColor()
95 return QColor( Qt::transparent );
98 QColor HYDROData_Profile::DefaultBorderColor()
100 return QColor( Qt::black );
103 QColor HYDROData_Profile::getDefaultFillingColor() const
105 return DefaultFillingColor();
108 QColor HYDROData_Profile::getDefaultBorderColor() const
110 return DefaultBorderColor();
113 bool HYDROData_Profile::IsValid() const
115 gp_XY aFirstPoint, aLastPoint;
116 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
119 int aNbPoints = NbPoints();
120 return aNbPoints > 1;
123 void HYDROData_Profile::SetLeftPoint( const gp_XY& thePoint )
125 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
127 Handle(TDataStd_RealArray) anArray;
128 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
129 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
131 anArray->SetValue( 0, thePoint.X() );
132 anArray->SetValue( 1, thePoint.Y() );
137 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint ) const
139 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
140 if ( aLabel.IsNull() )
143 Handle(TDataStd_RealArray) anArray;
144 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
147 thePoint.SetX( anArray->Value( 0 ) );
148 thePoint.SetY( anArray->Value( 1 ) );
153 void HYDROData_Profile::SetRightPoint( const gp_XY& thePoint )
155 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
157 Handle(TDataStd_RealArray) anArray;
158 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
159 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
161 anArray->SetValue( 0, thePoint.X() );
162 anArray->SetValue( 1, thePoint.Y() );
167 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint ) const
169 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
170 if ( aLabel.IsNull() )
173 Handle(TDataStd_RealArray) anArray;
174 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
177 thePoint.SetX( anArray->Value( 0 ) );
178 thePoint.SetY( anArray->Value( 1 ) );
183 void HYDROData_Profile::Invalidate()
185 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
186 if ( !aFirstLabel.IsNull() )
187 aFirstLabel.ForgetAllAttributes();
189 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
190 if ( !aLastLabel.IsNull() )
191 aLastLabel.ForgetAllAttributes();
196 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
198 Handle(HYDROData_ProfileUZ) aProfileUZ;
200 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
201 if ( aLabel.IsNull() )
204 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
205 if ( aProfileUZ.IsNull() && theIsCreate )
207 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
208 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
214 int HYDROData_Profile::NbPoints() const
216 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
217 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
220 void HYDROData_Profile::RemovePoints()
222 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
223 if ( !aProfileUZ.IsNull() )
225 aProfileUZ->RemoveSections();
230 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
234 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
235 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
237 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
238 aProfileUZ->AddPoint( 0, aPoint );
244 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
246 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
247 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
250 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
253 if ( thePoints.Length() < 2 )
256 gp_XY aFirstPoint, aLastPoint;
258 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
259 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
261 const ProfilePoint& aPoint = thePoints.Value( i );
262 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
265 aFirstPoint = aPointXY;
267 aLastPoint = aPointXY;
269 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
271 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
272 aProfileUZ->AddPoint( 0, aParPoint );
275 SetLeftPoint( aFirstPoint );
276 SetRightPoint( aLastPoint );
279 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
281 ProfilePoints aResPoints;
283 gp_XY aFirstPoint, aLastPoint;
284 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
287 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
288 if ( aParametricPoints.Length() < 2 )
291 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
292 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
294 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
295 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
297 // Add first point as is
298 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
300 // Compute all other points
301 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
303 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
305 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
307 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
309 double aRatio = aParLen / ( aGeoDistance - aParLen );
311 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
312 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
314 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
315 aResPoints.Append( aCompPoint );
318 // Add last point as is
319 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
324 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
326 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
329 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
331 TCollection_AsciiString aRes;
333 Handle(TDataStd_AsciiString) anAsciiStr;
334 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
335 aRes = anAsciiStr->Get();
340 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
341 const TCollection_AsciiString& theFileName,
342 NCollection_Sequence<int>& theBadProfilesIds )
344 if ( theDoc.IsNull() || theFileName.IsEmpty() )
347 OSD_File aFile( theFileName );
348 if ( !aFile.IsReadable() )
351 aFile.Open( OSD_ReadOnly, OSD_Protection() );
352 if ( !aFile.IsOpen() )
355 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
358 Handle(HYDROData_Profile) aNewProfile;
359 for ( ; !aFile.IsAtEnd(); ++aProfileId )
361 if ( aNewProfile.IsNull() )
362 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
364 bool anIsRead = false;
365 if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
367 aCreatedProfiles.Append( aNewProfile );
368 aNewProfile.Nullify();
372 theBadProfilesIds.Append( aProfileId );
376 if ( !aNewProfile.IsNull() )
377 aNewProfile->Remove();
382 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
384 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
386 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
387 aProfile->SetName( aProfileName );
389 aProfile->SetFilePath( theFileName );
391 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
394 return aCreatedProfiles.Length();
397 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
403 // Try to open the file
404 OSD_File aFile( theFileName );
405 if ( !aFile.IsReadable() )
408 aFile.Open( OSD_ReadOnly, OSD_Protection() );
409 if ( !aFile.IsOpen() )
412 bool aRes = ImportFromFile( aFile, theIsRead );
420 SetFilePath( theFileName );
426 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
432 if ( !theFile.IsOpen() )
437 bool anIsParametric = false;
438 bool anIsGeoref = false;
440 HYDROData_ProfileUZ::PointsList aPointsUZ;
441 ProfilePoints aPointsXYZ;
443 double aPrevVal = -DBL_MAX;
444 while ( !theFile.IsAtEnd() )
446 Standard_Integer aNbRead = 0;
447 TCollection_AsciiString aLine;
448 theFile.ReadLine( aLine, 1024, aNbRead );
450 aLine.LeftAdjust(); aLine.RightAdjust();
451 if ( aLine.IsEmpty() )
453 if ( !anIsParametric && !anIsGeoref )
454 continue; // Definition is not started yet
456 break; // Next profile started
459 // Set flag of read status to true
463 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
464 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
465 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
467 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
468 aValY.IsEmpty() || !aValY.IsRealValue() )
474 if ( !anIsParametric && !anIsGeoref )
476 anIsParametric = aValZ.IsEmpty();
477 anIsGeoref = !aValZ.IsEmpty();
480 double aCoordX = aValX.RealValue();
481 double aCoordY = aValY.RealValue();
483 if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
484 boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
487 if ( anIsParametric )
489 if ( aCoordX < aPrevVal )
491 // Move back readed line
492 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
496 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
497 aPointsUZ.Append( aPoint );
503 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
509 double aCoordZ = aValZ.RealValue();
510 if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
513 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
514 aPointsXYZ.Append( aPoint );
518 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
519 anIsGeoref && !aPointsXYZ.IsEmpty() );
522 // Update profile points
523 if ( anIsParametric )
525 SetParametricPoints( aPointsUZ );
527 else if ( anIsGeoref )
529 SetProfilePoints( aPointsXYZ );