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 #define PYTHON_PROFILE_ID "KIND_PROFILE"
33 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
34 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
36 HYDROData_Profile::HYDROData_Profile()
41 HYDROData_Profile::~HYDROData_Profile()
45 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
49 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
50 if ( aDocument.IsNull() )
53 QString aDocName = aDocument->GetDocPyName();
54 QString aProfileName = GetName();
56 aResList << QString( "%1 = %2.CreateObject( %3 );" )
57 .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
58 aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
63 TopoDS_Shape HYDROData_Profile::GetTopShape() const
67 gp_XY aFirstPoint, aLastPoint;
68 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
71 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
72 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
74 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
75 TopoDS_Edge anEdge = aMakeEdge;
77 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
83 TopoDS_Shape HYDROData_Profile::GetShape3D() const
88 void HYDROData_Profile::Update()
90 HYDROData_Object::Update();
93 Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
94 if ( !aProfile.IsNull() )
96 ProfilePoints aProfilePoints = GetProfilePoints();
97 HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
99 aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
104 QColor HYDROData_Profile::DefaultFillingColor()
106 return QColor( Qt::transparent );
109 QColor HYDROData_Profile::DefaultBorderColor()
111 return QColor( Qt::black );
114 QColor HYDROData_Profile::getDefaultFillingColor() const
116 return DefaultFillingColor();
119 QColor HYDROData_Profile::getDefaultBorderColor() const
121 return DefaultBorderColor();
124 bool HYDROData_Profile::IsValid() const
126 gp_XY aFirstPoint, aLastPoint;
127 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
130 int aNbPoints = NbPoints();
131 return aNbPoints > 1;
134 void HYDROData_Profile::SetLeftPoint( const gp_XY& thePoint )
136 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
138 Handle(TDataStd_RealArray) anArray;
139 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
140 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
142 anArray->SetValue( 0, thePoint.X() );
143 anArray->SetValue( 1, thePoint.Y() );
148 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint ) const
150 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
151 if ( aLabel.IsNull() )
154 Handle(TDataStd_RealArray) anArray;
155 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
158 thePoint.SetX( anArray->Value( 0 ) );
159 thePoint.SetY( anArray->Value( 1 ) );
164 void HYDROData_Profile::SetRightPoint( const gp_XY& thePoint )
166 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
168 Handle(TDataStd_RealArray) anArray;
169 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
170 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
172 anArray->SetValue( 0, thePoint.X() );
173 anArray->SetValue( 1, thePoint.Y() );
178 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint ) const
180 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
181 if ( aLabel.IsNull() )
184 Handle(TDataStd_RealArray) anArray;
185 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
188 thePoint.SetX( anArray->Value( 0 ) );
189 thePoint.SetY( anArray->Value( 1 ) );
194 void HYDROData_Profile::Invalidate()
196 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
197 if ( !aFirstLabel.IsNull() )
198 aFirstLabel.ForgetAllAttributes();
200 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
201 if ( !aLastLabel.IsNull() )
202 aLastLabel.ForgetAllAttributes();
207 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
209 Handle(HYDROData_ProfileUZ) aProfileUZ;
211 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
212 if ( aLabel.IsNull() )
215 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
216 if ( aProfileUZ.IsNull() && theIsCreate )
218 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
219 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
225 int HYDROData_Profile::NbPoints() const
227 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
228 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
231 void HYDROData_Profile::RemovePoints()
233 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
234 if ( !aProfileUZ.IsNull() )
236 aProfileUZ->RemoveSections();
241 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
245 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
246 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
248 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
249 aProfileUZ->AddPoint( 0, aPoint );
255 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
257 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
258 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
261 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
264 if ( thePoints.Length() < 2 )
267 gp_XY aFirstPoint, aLastPoint;
269 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
270 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
272 const ProfilePoint& aPoint = thePoints.Value( i );
273 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
276 aFirstPoint = aPointXY;
278 aLastPoint = aPointXY;
280 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
282 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
283 aProfileUZ->AddPoint( 0, aParPoint );
286 SetLeftPoint( aFirstPoint );
287 SetRightPoint( aLastPoint );
290 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
292 ProfilePoints aResPoints;
294 gp_XY aFirstPoint, aLastPoint;
295 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
298 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
299 if ( aParametricPoints.Length() < 2 )
302 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
303 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
305 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
306 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
308 // Add first point as is
309 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
311 // Compute all other points
312 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
314 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
316 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
318 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
320 double aRatio = aParLen / ( aGeoDistance - aParLen );
322 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
323 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
325 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
326 aResPoints.Append( aCompPoint );
329 // Add last point as is
330 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
335 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
337 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
340 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
342 TCollection_AsciiString aRes;
344 Handle(TDataStd_AsciiString) anAsciiStr;
345 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
346 aRes = anAsciiStr->Get();
351 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
352 const TCollection_AsciiString& theFileName,
353 NCollection_Sequence<int>& theBadProfilesIds )
355 if ( theDoc.IsNull() || theFileName.IsEmpty() )
358 OSD_File aFile( theFileName );
359 if ( !aFile.IsReadable() )
362 aFile.Open( OSD_ReadOnly, OSD_Protection() );
363 if ( !aFile.IsOpen() )
366 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
369 Handle(HYDROData_Profile) aNewProfile;
370 for ( ; !aFile.IsAtEnd(); ++aProfileId )
372 if ( aNewProfile.IsNull() )
373 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
375 bool anIsRead = false;
376 if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
378 aCreatedProfiles.Append( aNewProfile );
379 aNewProfile.Nullify();
383 theBadProfilesIds.Append( aProfileId );
387 if ( !aNewProfile.IsNull() )
388 aNewProfile->Remove();
393 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
395 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
397 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
398 aProfile->SetName( aProfileName );
400 aProfile->SetFilePath( theFileName );
402 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
405 return aCreatedProfiles.Length();
408 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
414 // Try to open the file
415 OSD_File aFile( theFileName );
416 if ( !aFile.IsReadable() )
419 aFile.Open( OSD_ReadOnly, OSD_Protection() );
420 if ( !aFile.IsOpen() )
423 bool aRes = ImportFromFile( aFile, theIsRead );
431 SetFilePath( theFileName );
437 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
443 if ( !theFile.IsOpen() )
448 bool anIsParametric = false;
449 bool anIsGeoref = false;
451 HYDROData_ProfileUZ::PointsList aPointsUZ;
452 ProfilePoints aPointsXYZ;
454 double aPrevVal = -DBL_MAX;
455 while ( !theFile.IsAtEnd() )
457 Standard_Integer aNbRead = 0;
458 TCollection_AsciiString aLine;
459 theFile.ReadLine( aLine, 1024, aNbRead );
461 aLine.LeftAdjust(); aLine.RightAdjust();
462 if ( aLine.IsEmpty() )
464 if ( !anIsParametric && !anIsGeoref )
465 continue; // Definition is not started yet
467 break; // Next profile started
470 // Set flag of read status to true
474 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
475 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
476 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
478 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
479 aValY.IsEmpty() || !aValY.IsRealValue() )
485 if ( !anIsParametric && !anIsGeoref )
487 anIsParametric = aValZ.IsEmpty();
488 anIsGeoref = !aValZ.IsEmpty();
491 double aCoordX = aValX.RealValue();
492 double aCoordY = aValY.RealValue();
494 if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
495 boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
498 if ( anIsParametric )
500 if ( aCoordX < aPrevVal )
502 // Move back readed line
503 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
507 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
508 aPointsUZ.Append( aPoint );
514 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
520 double aCoordZ = aValZ.RealValue();
521 if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
524 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
525 aPointsXYZ.Append( aPoint );
529 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
530 anIsGeoref && !aPointsXYZ.IsEmpty() );
533 // Update profile points
534 if ( anIsParametric )
536 SetParametricPoints( aPointsUZ );
538 else if ( anIsGeoref )
540 SetProfilePoints( aPointsXYZ );