2 #include "HYDROData_Profile.h"
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Iterator.h"
6 #include "HYDROData_Tool.h"
8 #include <BRepBuilderAPI_MakeEdge.hxx>
9 #include <BRepBuilderAPI_MakeWire.hxx>
13 #include <gp_Pnt2d.hxx>
15 #include <TDataStd_AsciiString.hxx>
16 #include <TDataStd_RealArray.hxx>
18 #include <TopoDS_Edge.hxx>
19 #include <TopoDS_Wire.hxx>
21 #include <OSD_File.hxx>
22 #include <OSD_Protection.hxx>
24 #include <QStringList>
26 #define PYTHON_PROFILE_ID "KIND_PROFILE"
28 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
29 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
31 HYDROData_Profile::HYDROData_Profile()
36 HYDROData_Profile::~HYDROData_Profile()
40 TopoDS_Shape HYDROData_Profile::GetTopShape() const
44 gp_XY aFirstPoint, aLastPoint;
45 if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
48 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
49 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
51 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
52 TopoDS_Edge anEdge = aMakeEdge;
54 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
60 TopoDS_Shape HYDROData_Profile::GetShape3D() const
65 void HYDROData_Profile::Update()
67 BRepBuilderAPI_MakeWire aMakeWire;
69 ProfilePoints aProfilePoints = GetProfilePoints();
70 for ( int i = 1, n = aProfilePoints.Length(); i < n ; ++i )
72 ProfilePoint aFirstPoint = aProfilePoints.Value( i );
73 ProfilePoint aSecPoint = aProfilePoints.Value( i + 1 );
75 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
76 gp_Pnt aPnt2( aSecPoint.X(), aSecPoint.Y(), aSecPoint.Z() );
78 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
79 TopoDS_Edge anEdge = aMakeEdge;
81 aMakeWire.Add( anEdge );
85 if ( aMakeWire.IsDone() )
92 * Dump object to Python script representation.
94 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
98 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
99 if ( aDocument.IsNull() )
102 QString aDocName = aDocument->GetDocPyName();
103 QString aProfileName = GetName();
105 aResList << QString( "%1 = %2.CreateObject( %3 );" )
106 .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
107 aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
112 bool HYDROData_Profile::IsValid() const
114 gp_XY aFirstPoint, aLastPoint;
115 if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
118 int aNbPoints = NbPoints();
119 return aNbPoints > 1;
122 void HYDROData_Profile::SetFirstPoint( const gp_XY& thePoint )
124 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
126 Handle(TDataStd_RealArray) anArray;
127 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
128 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
130 anArray->SetValue( 0, thePoint.X() );
131 anArray->SetValue( 1, thePoint.Y() );
134 bool HYDROData_Profile::GetFirstPoint( gp_XY& thePoint ) const
136 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
137 if ( aLabel.IsNull() )
140 Handle(TDataStd_RealArray) anArray;
141 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
144 thePoint.SetX( anArray->Value( 0 ) );
145 thePoint.SetY( anArray->Value( 1 ) );
150 void HYDROData_Profile::SetLastPoint( const gp_XY& thePoint )
152 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
154 Handle(TDataStd_RealArray) anArray;
155 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
156 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
158 anArray->SetValue( 0, thePoint.X() );
159 anArray->SetValue( 1, thePoint.Y() );
162 bool HYDROData_Profile::GetLastPoint( gp_XY& thePoint ) const
164 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
165 if ( aLabel.IsNull() )
168 Handle(TDataStd_RealArray) anArray;
169 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
172 thePoint.SetX( anArray->Value( 0 ) );
173 thePoint.SetY( anArray->Value( 1 ) );
178 void HYDROData_Profile::Invalidate()
180 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
181 if ( !aFirstLabel.IsNull() )
182 aFirstLabel.ForgetAllAttributes();
184 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
185 if ( !aLastLabel.IsNull() )
186 aLastLabel.ForgetAllAttributes();
189 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
191 Handle(HYDROData_ProfileUZ) aProfileUZ;
193 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
194 if ( aLabel.IsNull() )
197 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
198 if ( aProfileUZ.IsNull() && theIsCreate )
200 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
201 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
207 int HYDROData_Profile::NbPoints() const
209 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
210 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
213 void HYDROData_Profile::RemovePoints()
215 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
216 if ( !aProfileUZ.IsNull() )
217 aProfileUZ->RemoveSections();
220 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
224 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
225 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
227 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
228 aProfileUZ->AddPoint( 0, aPoint );
232 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
234 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
235 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
238 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
241 if ( thePoints.Length() < 2 )
244 gp_XY aFirstPoint, aLastPoint;
246 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
247 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
249 const ProfilePoint& aPoint = thePoints.Value( i );
250 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
253 aFirstPoint = aPointXY;
255 aLastPoint = aPointXY;
257 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
259 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
260 aProfileUZ->AddPoint( 0, aParPoint );
263 SetFirstPoint( aFirstPoint );
264 SetLastPoint( aLastPoint );
267 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
269 ProfilePoints aResPoints;
271 gp_XY aFirstPoint, aLastPoint;
272 if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
275 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
276 if ( aParametricPoints.Length() < 2 )
279 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
280 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
282 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
283 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
285 // Add first point as is
286 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
288 // Compute all other points
289 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
291 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
293 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
295 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
297 double aRatio = aParLen / ( aGeoDistance - aParLen );
299 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
300 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
302 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
303 aResPoints.Append( aCompPoint );
306 // Add last point as is
307 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
312 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
314 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
317 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
319 TCollection_AsciiString aRes;
321 Handle(TDataStd_AsciiString) anAsciiStr;
322 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
323 aRes = anAsciiStr->Get();
328 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
329 const TCollection_AsciiString& theFileName )
331 if ( theDoc.IsNull() || theFileName.IsEmpty() )
334 OSD_File aFile( theFileName );
335 if ( !aFile.IsReadable() )
338 aFile.Open( OSD_ReadOnly, OSD_Protection() );
339 if ( !aFile.IsOpen() )
342 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
344 Handle(HYDROData_Profile) aNewProfile;
345 while ( !aFile.IsAtEnd() )
347 if ( aNewProfile.IsNull() )
348 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
350 if ( aNewProfile->ImportFromFile( aFile ) )
352 aCreatedProfiles.Append( aNewProfile );
353 aNewProfile.Nullify();
357 if ( !aNewProfile.IsNull() )
358 aNewProfile->Remove();
363 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
365 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
367 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
368 aProfile->SetName( aProfileName );
370 aProfile->SetFilePath( theFileName );
373 return !aCreatedProfiles.IsEmpty();
376 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
378 // Try to open the file
379 OSD_File aFile( theFileName );
380 if ( !aFile.IsReadable() )
383 aFile.Open( OSD_ReadOnly, OSD_Protection() );
384 if ( !aFile.IsOpen() )
387 bool aRes = ImportFromFile( aFile );
395 SetFilePath( theFileName );
401 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
403 if ( !theFile.IsOpen() )
408 bool anIsParametric = false;
409 bool anIsGeoref = false;
411 HYDROData_ProfileUZ::PointsList aPointsUZ;
412 ProfilePoints aPointsXYZ;
414 double aPrevVal = -DBL_MAX;
415 while ( !theFile.IsAtEnd() )
417 Standard_Integer aNbRead = 0;
418 TCollection_AsciiString aLine;
419 theFile.ReadLine( aLine, 1024, aNbRead );
421 aLine.LeftAdjust(); aLine.RightAdjust();
422 if ( aLine.IsEmpty() )
424 if ( !anIsParametric && !anIsGeoref )
425 continue; // Definition is not started yet
427 break; // Next profile started
430 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
431 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
432 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
434 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
435 aValY.IsEmpty() || !aValY.IsRealValue() )
441 if ( !anIsParametric && !anIsGeoref )
443 anIsParametric = aValZ.IsEmpty();
444 anIsGeoref = !aValZ.IsEmpty();
447 double aCoordX = aValX.RealValue();
448 double aCoordY = aValY.RealValue();
450 if ( anIsParametric )
452 if ( aCoordX < aPrevVal )
454 // Move back readed line
455 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
459 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
460 aPointsUZ.Append( aPoint );
466 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
472 double aCoordZ = aValZ.RealValue();
474 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
475 aPointsXYZ.Append( aPoint );
479 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
480 anIsGeoref && !aPointsXYZ.IsEmpty() );
483 // Update profile points
484 if ( anIsParametric )
486 SetParametricPoints( aPointsUZ );
488 else if ( anIsGeoref )
490 SetProfilePoints( aPointsXYZ );