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>
10 #include <BRepBuilderAPI_MakePolygon.hxx>
14 #include <gp_Pnt2d.hxx>
16 #include <TDataStd_AsciiString.hxx>
17 #include <TDataStd_RealArray.hxx>
19 #include <TopoDS_Edge.hxx>
20 #include <TopoDS_Wire.hxx>
22 #include <OSD_File.hxx>
23 #include <OSD_Protection.hxx>
26 #include <QStringList>
28 #define PYTHON_PROFILE_ID "KIND_PROFILE"
30 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
31 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
33 HYDROData_Profile::HYDROData_Profile()
38 HYDROData_Profile::~HYDROData_Profile()
42 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
46 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
47 if ( aDocument.IsNull() )
50 QString aDocName = aDocument->GetDocPyName();
51 QString aProfileName = GetName();
53 aResList << QString( "%1 = %2.CreateObject( %3 );" )
54 .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
55 aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
60 TopoDS_Shape HYDROData_Profile::GetTopShape() const
64 gp_XY aFirstPoint, aLastPoint;
65 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
68 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
69 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
71 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
72 TopoDS_Edge anEdge = aMakeEdge;
74 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
80 TopoDS_Shape HYDROData_Profile::GetShape3D() const
85 void HYDROData_Profile::Update()
87 HYDROData_Object::Update();
89 BRepBuilderAPI_MakePolygon aMakeWire;
91 ProfilePoints aProfilePoints = GetProfilePoints();
92 for ( int i = 1, n = aProfilePoints.Length(); i <= n ; ++i )
94 ProfilePoint aPoint = aProfilePoints.Value( i );
95 gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
96 aMakeWire.Add( aPnt );
100 if ( aMakeWire.IsDone() )
101 aWire = aMakeWire.Wire();
103 /*BRepBuilderAPI_MakeWire aMakeWire;
105 ProfilePoints aProfilePoints = GetProfilePoints();
106 for ( int i = 1, n = aProfilePoints.Length(); i < n ; ++i )
108 ProfilePoint aFirstPoint = aProfilePoints.Value( i );
109 ProfilePoint aSecPoint = aProfilePoints.Value( i + 1 );
111 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
112 gp_Pnt aPnt2( aSecPoint.X(), aSecPoint.Y(), aSecPoint.Z() );
114 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
115 TopoDS_Edge anEdge = aMakeEdge;
117 aMakeWire.Add( anEdge );
121 if ( aMakeWire.IsDone() )
127 QColor HYDROData_Profile::DefaultFillingColor()
129 return QColor( Qt::transparent );
132 QColor HYDROData_Profile::DefaultBorderColor()
134 return QColor( Qt::black );
137 QColor HYDROData_Profile::getDefaultFillingColor() const
139 return DefaultFillingColor();
142 QColor HYDROData_Profile::getDefaultBorderColor() const
144 return DefaultBorderColor();
147 bool HYDROData_Profile::IsValid() const
149 gp_XY aFirstPoint, aLastPoint;
150 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
153 int aNbPoints = NbPoints();
154 return aNbPoints > 1;
157 void HYDROData_Profile::SetLeftPoint( const gp_XY& thePoint )
159 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
161 Handle(TDataStd_RealArray) anArray;
162 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
163 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
165 anArray->SetValue( 0, thePoint.X() );
166 anArray->SetValue( 1, thePoint.Y() );
171 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint ) const
173 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
174 if ( aLabel.IsNull() )
177 Handle(TDataStd_RealArray) anArray;
178 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
181 thePoint.SetX( anArray->Value( 0 ) );
182 thePoint.SetY( anArray->Value( 1 ) );
187 void HYDROData_Profile::SetRightPoint( const gp_XY& thePoint )
189 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
191 Handle(TDataStd_RealArray) anArray;
192 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
193 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
195 anArray->SetValue( 0, thePoint.X() );
196 anArray->SetValue( 1, thePoint.Y() );
201 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint ) const
203 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
204 if ( aLabel.IsNull() )
207 Handle(TDataStd_RealArray) anArray;
208 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
211 thePoint.SetX( anArray->Value( 0 ) );
212 thePoint.SetY( anArray->Value( 1 ) );
217 void HYDROData_Profile::Invalidate()
219 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
220 if ( !aFirstLabel.IsNull() )
221 aFirstLabel.ForgetAllAttributes();
223 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
224 if ( !aLastLabel.IsNull() )
225 aLastLabel.ForgetAllAttributes();
230 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
232 Handle(HYDROData_ProfileUZ) aProfileUZ;
234 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
235 if ( aLabel.IsNull() )
238 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
239 if ( aProfileUZ.IsNull() && theIsCreate )
241 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
242 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
248 int HYDROData_Profile::NbPoints() const
250 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
251 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
254 void HYDROData_Profile::RemovePoints()
256 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
257 if ( !aProfileUZ.IsNull() )
259 aProfileUZ->RemoveSections();
264 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
268 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
269 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
271 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
272 aProfileUZ->AddPoint( 0, aPoint );
278 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
280 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
281 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
284 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
287 if ( thePoints.Length() < 2 )
290 gp_XY aFirstPoint, aLastPoint;
292 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
293 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
295 const ProfilePoint& aPoint = thePoints.Value( i );
296 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
299 aFirstPoint = aPointXY;
301 aLastPoint = aPointXY;
303 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
305 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
306 aProfileUZ->AddPoint( 0, aParPoint );
309 SetLeftPoint( aFirstPoint );
310 SetRightPoint( aLastPoint );
313 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
315 ProfilePoints aResPoints;
317 gp_XY aFirstPoint, aLastPoint;
318 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
321 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
322 if ( aParametricPoints.Length() < 2 )
325 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
326 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
328 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
329 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
331 // Add first point as is
332 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
334 // Compute all other points
335 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
337 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
339 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
341 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
343 double aRatio = aParLen / ( aGeoDistance - aParLen );
345 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
346 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
348 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
349 aResPoints.Append( aCompPoint );
352 // Add last point as is
353 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
358 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
360 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
363 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
365 TCollection_AsciiString aRes;
367 Handle(TDataStd_AsciiString) anAsciiStr;
368 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
369 aRes = anAsciiStr->Get();
374 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
375 const TCollection_AsciiString& theFileName )
377 if ( theDoc.IsNull() || theFileName.IsEmpty() )
380 OSD_File aFile( theFileName );
381 if ( !aFile.IsReadable() )
384 aFile.Open( OSD_ReadOnly, OSD_Protection() );
385 if ( !aFile.IsOpen() )
388 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
390 Handle(HYDROData_Profile) aNewProfile;
391 while ( !aFile.IsAtEnd() )
393 if ( aNewProfile.IsNull() )
394 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
396 if ( aNewProfile->ImportFromFile( aFile ) )
398 aCreatedProfiles.Append( aNewProfile );
399 aNewProfile.Nullify();
403 if ( !aNewProfile.IsNull() )
404 aNewProfile->Remove();
409 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
411 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
413 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
414 aProfile->SetName( aProfileName );
416 aProfile->SetFilePath( theFileName );
418 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
421 return !aCreatedProfiles.IsEmpty();
424 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
426 // Try to open the file
427 OSD_File aFile( theFileName );
428 if ( !aFile.IsReadable() )
431 aFile.Open( OSD_ReadOnly, OSD_Protection() );
432 if ( !aFile.IsOpen() )
435 bool aRes = ImportFromFile( aFile );
443 SetFilePath( theFileName );
449 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
451 if ( !theFile.IsOpen() )
456 bool anIsParametric = false;
457 bool anIsGeoref = false;
459 HYDROData_ProfileUZ::PointsList aPointsUZ;
460 ProfilePoints aPointsXYZ;
462 double aPrevVal = -DBL_MAX;
463 while ( !theFile.IsAtEnd() )
465 Standard_Integer aNbRead = 0;
466 TCollection_AsciiString aLine;
467 theFile.ReadLine( aLine, 1024, aNbRead );
469 aLine.LeftAdjust(); aLine.RightAdjust();
470 if ( aLine.IsEmpty() )
472 if ( !anIsParametric && !anIsGeoref )
473 continue; // Definition is not started yet
475 break; // Next profile started
478 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
479 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
480 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
482 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
483 aValY.IsEmpty() || !aValY.IsRealValue() )
489 if ( !anIsParametric && !anIsGeoref )
491 anIsParametric = aValZ.IsEmpty();
492 anIsGeoref = !aValZ.IsEmpty();
495 double aCoordX = aValX.RealValue();
496 double aCoordY = aValY.RealValue();
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();
522 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
523 aPointsXYZ.Append( aPoint );
527 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
528 anIsGeoref && !aPointsXYZ.IsEmpty() );
531 // Update profile points
532 if ( anIsParametric )
534 SetParametricPoints( aPointsUZ );
536 else if ( anIsGeoref )
538 SetProfilePoints( aPointsXYZ );