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->NbPoints();
202 void HYDROData_Profile::RemovePoints()
204 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
205 if ( !aProfileUZ.IsNull() )
206 aProfileUZ->RemoveSections();
209 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
213 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
214 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
216 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
217 aProfileUZ->AddPoint( 0, aPoint );
221 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
223 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
224 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
227 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
230 if ( thePoints.Length() < 2 )
233 gp_XY aFirstPoint, aLastPoint;
235 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
236 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
238 const ProfilePoint& aPoint = thePoints.Value( i );
239 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
242 aFirstPoint = aPointXY;
244 aLastPoint = aPointXY;
246 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
248 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
249 aProfileUZ->AddPoint( 0, aParPoint );
252 SetFirstPoint( aFirstPoint );
253 SetLastPoint( aLastPoint );
256 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
258 ProfilePoints aResPoints;
260 gp_XY aFirstPoint, aLastPoint;
261 if ( !GetFirstPoint( aFirstPoint ) || !GetLastPoint( aLastPoint ) )
264 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
265 if ( aParametricPoints.Length() < 2 )
268 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
269 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
271 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
272 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
274 // Add first point as is
275 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
277 // Compute all other points
278 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
280 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
282 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
284 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
286 double aRatio = aParLen / ( aGeoDistance - aParLen );
288 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
289 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
291 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
292 aResPoints.Append( aCompPoint );
295 // Add last point as is
296 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
301 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
303 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
306 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
308 TCollection_AsciiString aRes;
310 Handle(TDataStd_AsciiString) anAsciiStr;
311 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
312 aRes = anAsciiStr->Get();
317 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
318 const TCollection_AsciiString& theFileName )
320 if ( theDoc.IsNull() || theFileName.IsEmpty() )
323 OSD_File aFile( theFileName );
324 if ( !aFile.IsReadable() )
327 aFile.Open( OSD_ReadOnly, OSD_Protection() );
328 if ( !aFile.IsOpen() )
333 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
335 while ( aRes && !aFile.IsAtEnd() )
337 Handle(HYDROData_Profile) aNewProfile =
338 Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
340 aRes = aNewProfile->ImportFromFile( aFile );
342 aCreatedProfiles.Append( aNewProfile );
348 for ( int i = 1, n = aCreatedProfiles.Length(); i < n ; ++i )
350 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
353 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
354 aProfile->SetName( aProfileName );
356 aProfile->SetFilePath( theFileName );
367 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
369 // Try to open the file
370 OSD_File aFile( theFileName );
371 if ( !aFile.IsReadable() )
374 aFile.Open( OSD_ReadOnly, OSD_Protection() );
375 if ( !aFile.IsOpen() )
378 bool aRes = ImportFromFile( aFile );
386 SetFilePath( theFileName );
392 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
394 if ( !theFile.IsOpen() )
399 bool anIsParametric = false;
400 bool anIsGeoref = false;
402 HYDROData_ProfileUZ::PointsList aPointsUZ;
403 ProfilePoints aPointsXYZ;
405 double aPrevVal = -DBL_MAX;
406 while ( !theFile.IsAtEnd() )
408 Standard_Integer aNbRead = 0;
409 TCollection_AsciiString aLine;
410 theFile.ReadLine( aLine, 1024, aNbRead );
412 aLine.LeftAdjust(); aLine.RightAdjust();
413 if ( aLine.IsEmpty() )
416 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
417 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
418 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
420 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
421 aValY.IsEmpty() || !aValY.IsRealValue() )
427 if ( !anIsParametric && !anIsGeoref )
429 anIsParametric = aValZ.IsEmpty();
430 anIsGeoref = !aValZ.IsEmpty();
433 double aCoordX = aValX.RealValue();
434 double aCoordY = aValY.RealValue();
436 if ( anIsParametric )
438 if ( aCoordX < aPrevVal )
440 // Move back readed line
441 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
445 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
446 aPointsUZ.Append( aPoint );
452 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
458 double aCoordZ = aValZ.RealValue();
460 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
461 aPointsXYZ.Append( aPoint );
465 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
466 anIsGeoref && !aPointsXYZ.IsEmpty() );
469 // Update profile points
470 if ( anIsParametric )
472 SetParametricPoints( aPointsUZ );
474 else if ( anIsGeoref )
476 SetProfilePoints( aPointsXYZ );