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::UpdateShape3D()
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 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
180 Handle(HYDROData_ProfileUZ) aProfileUZ;
182 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
183 if ( aLabel.IsNull() )
186 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
187 if ( aProfileUZ.IsNull() && theIsCreate )
189 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
190 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
196 int HYDROData_Profile::NbPoints() const
198 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
199 return aProfileUZ.IsNull() ? 0 : aProfileUZ->getNbPoints();
202 void HYDROData_Profile::RemovePoints()
204 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
205 if ( !aProfileUZ.IsNull() )
209 void HYDROData_Profile::SetParametricPoints( const CurveCreator::Coordinates& theCoords )
213 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
215 CurveCreator::Coordinates::const_iterator aBegIter = theCoords.begin();
216 CurveCreator::Coordinates::const_iterator anEndIter = theCoords.end();
217 while ( aBegIter != anEndIter )
219 const CurveCreator::TypeCoord& aCoordX = *aBegIter++;
220 if ( aBegIter == anEndIter )
223 const CurveCreator::TypeCoord& aCoordY = *aBegIter++;
225 aProfileUZ->addPoint( aCoordX, aCoordY, 0 );
229 CurveCreator::Coordinates HYDROData_Profile::GetParametricPoints() const
231 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
232 return aProfileUZ.IsNull() ? CurveCreator::Coordinates() : aProfileUZ->getPoints();
235 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
238 if ( thePoints.Length() < 2 )
241 gp_XY aFirstPoint, aLastPoint;
243 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
244 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
246 const ProfilePoint& aPoint = thePoints.Value( i );
247 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
250 aFirstPoint = aPointXY;
252 aLastPoint = aPointXY;
254 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
256 aProfileUZ->addPoint( 0, aDistance, aPoint.Z() );
259 SetFirstPoint( aFirstPoint );
260 SetLastPoint( aLastPoint );
263 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
265 ProfilePoints aResPoints;
267 gp_XY aFirstPoint, aLastPoint;
268 if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
271 CurveCreator::Coordinates aParametricPoints = GetParametricPoints();
272 if ( aParametricPoints.size() < 4 )
275 CurveCreator::Coordinates::const_iterator aBegIter = aParametricPoints.begin();
276 CurveCreator::Coordinates::const_iterator anEndIter = aParametricPoints.end()--;
278 gp_Pnt2d aFirstParPoint( *aBegIter++, *aBegIter++ );
279 gp_Pnt2d aLastParPoint( *anEndIter--, *anEndIter );
281 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
282 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
284 // Add first point as is
285 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
287 // Compute all other points
288 while ( aBegIter != anEndIter )
290 const CurveCreator::TypeCoord& aCoordU = *aBegIter++;
291 if ( aBegIter == anEndIter )
294 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aCoordU, 0 ) );
296 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
298 double aRatio = aParLen / ( aGeoDistance - aParLen );
300 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
301 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
304 const CurveCreator::TypeCoord& aCoordZ = *aBegIter++;
306 ProfilePoint aCompPoint( aParX, aParY, aCoordZ );
307 aResPoints.Append( aCompPoint );
310 // Add last point as is
311 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
316 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
318 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
321 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
323 TCollection_AsciiString aRes;
325 Handle(TDataStd_AsciiString) anAsciiStr;
326 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
327 aRes = anAsciiStr->Get();
332 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
333 const TCollection_AsciiString& theFileName )
335 if ( theDoc.IsNull() || theFileName.IsEmpty() )
338 OSD_File aFile( theFileName );
339 if ( !aFile.IsReadable() )
342 aFile.Open( OSD_ReadOnly, OSD_Protection() );
343 if ( !aFile.IsOpen() )
348 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
350 while ( aRes && !aFile.IsAtEnd() )
352 Handle(HYDROData_Profile) aNewProfile =
353 Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
355 aRes = aNewProfile->ImportFromFile( aFile );
357 aCreatedProfiles.Append( aNewProfile );
363 for ( int i = 1, n = aCreatedProfiles.Length(); i < n ; ++i )
365 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
368 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
369 aProfile->SetName( aProfileName );
371 aProfile->SetFilePath( theFileName );
382 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
384 // Try to open the file
385 OSD_File aFile( theFileName );
386 if ( !aFile.IsReadable() )
389 aFile.Open( OSD_ReadOnly, OSD_Protection() );
390 if ( !aFile.IsOpen() )
393 bool aRes = ImportFromFile( aFile );
401 SetFilePath( theFileName );
407 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
409 if ( !theFile.IsOpen() )
414 bool anIsParametric = false;
415 bool anIsGeoref = false;
417 CurveCreator::Coordinates aPointsUZ;
418 ProfilePoints aPointsXYZ;
420 double aPrevVal = -DBL_MAX;
421 while ( !theFile.IsAtEnd() )
423 Standard_Integer aNbRead = 0;
424 TCollection_AsciiString aLine;
425 theFile.ReadLine( aLine, 1024, aNbRead );
427 aLine.LeftAdjust(); aLine.RightAdjust();
428 if ( aLine.IsEmpty() )
431 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
432 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
433 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
435 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
436 aValY.IsEmpty() || !aValY.IsRealValue() )
442 if ( !anIsParametric && !anIsGeoref )
444 anIsParametric = aValZ.IsEmpty();
445 anIsGeoref = !aValZ.IsEmpty();
448 double aCoordX = aValX.RealValue();
449 double aCoordY = aValY.RealValue();
451 if ( anIsParametric )
453 if ( aCoordX < aPrevVal )
455 // Move back readed line
456 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
460 aPointsUZ.push_back( aCoordX );
461 aPointsUZ.push_back( aCoordY );
467 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
473 double aCoordZ = aValZ.RealValue();
475 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
476 aPointsXYZ.Append( aPoint );
480 aRes = aRes && ( anIsParametric && !aPointsUZ.empty() ||
481 anIsGeoref && !aPointsXYZ.IsEmpty() );
484 // Update profile points
485 if ( anIsParametric )
487 SetParametricPoints( aPointsUZ );
489 else if ( anIsGeoref )
491 SetProfilePoints( aPointsXYZ );