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 HYDROData_Object::Update();
69 BRepBuilderAPI_MakeWire aMakeWire;
71 ProfilePoints aProfilePoints = GetProfilePoints();
72 for ( int i = 1, n = aProfilePoints.Length(); i < n ; ++i )
74 ProfilePoint aFirstPoint = aProfilePoints.Value( i );
75 ProfilePoint aSecPoint = aProfilePoints.Value( i + 1 );
77 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
78 gp_Pnt aPnt2( aSecPoint.X(), aSecPoint.Y(), aSecPoint.Z() );
80 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
81 TopoDS_Edge anEdge = aMakeEdge;
83 aMakeWire.Add( anEdge );
87 if ( aMakeWire.IsDone() )
94 * Dump object to Python script representation.
96 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
100 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
101 if ( aDocument.IsNull() )
104 QString aDocName = aDocument->GetDocPyName();
105 QString aProfileName = GetName();
107 aResList << QString( "%1 = %2.CreateObject( %3 );" )
108 .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
109 aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
114 bool HYDROData_Profile::IsValid() const
116 gp_XY aFirstPoint, aLastPoint;
117 if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
120 int aNbPoints = NbPoints();
121 return aNbPoints > 1;
124 void HYDROData_Profile::SetFirstPoint( const gp_XY& thePoint )
126 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
128 Handle(TDataStd_RealArray) anArray;
129 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
130 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
132 anArray->SetValue( 0, thePoint.X() );
133 anArray->SetValue( 1, thePoint.Y() );
138 bool HYDROData_Profile::GetFirstPoint( gp_XY& thePoint ) const
140 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
141 if ( aLabel.IsNull() )
144 Handle(TDataStd_RealArray) anArray;
145 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
148 thePoint.SetX( anArray->Value( 0 ) );
149 thePoint.SetY( anArray->Value( 1 ) );
154 void HYDROData_Profile::SetLastPoint( const gp_XY& thePoint )
156 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
158 Handle(TDataStd_RealArray) anArray;
159 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
160 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
162 anArray->SetValue( 0, thePoint.X() );
163 anArray->SetValue( 1, thePoint.Y() );
168 bool HYDROData_Profile::GetLastPoint( gp_XY& thePoint ) const
170 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
171 if ( aLabel.IsNull() )
174 Handle(TDataStd_RealArray) anArray;
175 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
178 thePoint.SetX( anArray->Value( 0 ) );
179 thePoint.SetY( anArray->Value( 1 ) );
184 void HYDROData_Profile::Invalidate()
186 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
187 if ( !aFirstLabel.IsNull() )
188 aFirstLabel.ForgetAllAttributes();
190 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
191 if ( !aLastLabel.IsNull() )
192 aLastLabel.ForgetAllAttributes();
197 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
199 Handle(HYDROData_ProfileUZ) aProfileUZ;
201 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
202 if ( aLabel.IsNull() )
205 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
206 if ( aProfileUZ.IsNull() && theIsCreate )
208 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
209 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
215 int HYDROData_Profile::NbPoints() const
217 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
218 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
221 void HYDROData_Profile::RemovePoints()
223 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
224 if ( !aProfileUZ.IsNull() )
226 aProfileUZ->RemoveSections();
231 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
235 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
236 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
238 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
239 aProfileUZ->AddPoint( 0, aPoint );
245 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
247 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
248 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
251 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
254 if ( thePoints.Length() < 2 )
257 gp_XY aFirstPoint, aLastPoint;
259 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
260 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
262 const ProfilePoint& aPoint = thePoints.Value( i );
263 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
266 aFirstPoint = aPointXY;
268 aLastPoint = aPointXY;
270 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
272 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
273 aProfileUZ->AddPoint( 0, aParPoint );
276 SetFirstPoint( aFirstPoint );
277 SetLastPoint( aLastPoint );
280 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
282 ProfilePoints aResPoints;
284 gp_XY aFirstPoint, aLastPoint;
285 if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
288 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
289 if ( aParametricPoints.Length() < 2 )
292 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
293 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
295 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
296 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
298 // Add first point as is
299 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
301 // Compute all other points
302 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
304 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
306 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
308 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
310 double aRatio = aParLen / ( aGeoDistance - aParLen );
312 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
313 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
315 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
316 aResPoints.Append( aCompPoint );
319 // Add last point as is
320 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
325 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
327 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
330 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
332 TCollection_AsciiString aRes;
334 Handle(TDataStd_AsciiString) anAsciiStr;
335 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
336 aRes = anAsciiStr->Get();
341 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
342 const TCollection_AsciiString& theFileName )
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;
357 Handle(HYDROData_Profile) aNewProfile;
358 while ( !aFile.IsAtEnd() )
360 if ( aNewProfile.IsNull() )
361 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
363 if ( aNewProfile->ImportFromFile( aFile ) )
365 aCreatedProfiles.Append( aNewProfile );
366 aNewProfile.Nullify();
370 if ( !aNewProfile.IsNull() )
371 aNewProfile->Remove();
376 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
378 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
380 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
381 aProfile->SetName( aProfileName );
383 aProfile->SetFilePath( theFileName );
386 return !aCreatedProfiles.IsEmpty();
389 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
391 // Try to open the file
392 OSD_File aFile( theFileName );
393 if ( !aFile.IsReadable() )
396 aFile.Open( OSD_ReadOnly, OSD_Protection() );
397 if ( !aFile.IsOpen() )
400 bool aRes = ImportFromFile( aFile );
408 SetFilePath( theFileName );
414 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
416 if ( !theFile.IsOpen() )
421 bool anIsParametric = false;
422 bool anIsGeoref = false;
424 HYDROData_ProfileUZ::PointsList aPointsUZ;
425 ProfilePoints aPointsXYZ;
427 double aPrevVal = -DBL_MAX;
428 while ( !theFile.IsAtEnd() )
430 Standard_Integer aNbRead = 0;
431 TCollection_AsciiString aLine;
432 theFile.ReadLine( aLine, 1024, aNbRead );
434 aLine.LeftAdjust(); aLine.RightAdjust();
435 if ( aLine.IsEmpty() )
437 if ( !anIsParametric && !anIsGeoref )
438 continue; // Definition is not started yet
440 break; // Next profile started
443 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
444 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
445 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
447 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
448 aValY.IsEmpty() || !aValY.IsRealValue() )
454 if ( !anIsParametric && !anIsGeoref )
456 anIsParametric = aValZ.IsEmpty();
457 anIsGeoref = !aValZ.IsEmpty();
460 double aCoordX = aValX.RealValue();
461 double aCoordY = aValY.RealValue();
463 if ( anIsParametric )
465 if ( aCoordX < aPrevVal )
467 // Move back readed line
468 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
472 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
473 aPointsUZ.Append( aPoint );
479 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
485 double aCoordZ = aValZ.RealValue();
487 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
488 aPointsXYZ.Append( aPoint );
492 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
493 anIsGeoref && !aPointsXYZ.IsEmpty() );
496 // Update profile points
497 if ( anIsParametric )
499 SetParametricPoints( aPointsUZ );
501 else if ( anIsGeoref )
503 SetProfilePoints( aPointsXYZ );