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>
25 #include <QStringList>
27 #define PYTHON_PROFILE_ID "KIND_PROFILE"
29 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
30 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
32 HYDROData_Profile::HYDROData_Profile()
37 HYDROData_Profile::~HYDROData_Profile()
41 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
45 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
46 if ( aDocument.IsNull() )
49 QString aDocName = aDocument->GetDocPyName();
50 QString aProfileName = GetName();
52 aResList << QString( "%1 = %2.CreateObject( %3 );" )
53 .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
54 aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
59 TopoDS_Shape HYDROData_Profile::GetTopShape() const
63 gp_XY aFirstPoint, aLastPoint;
64 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
67 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
68 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
70 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
71 TopoDS_Edge anEdge = aMakeEdge;
73 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
79 TopoDS_Shape HYDROData_Profile::GetShape3D() const
84 void HYDROData_Profile::Update()
86 HYDROData_Object::Update();
88 BRepBuilderAPI_MakeWire aMakeWire;
90 ProfilePoints aProfilePoints = GetProfilePoints();
91 for ( int i = 1, n = aProfilePoints.Length(); i < n ; ++i )
93 ProfilePoint aFirstPoint = aProfilePoints.Value( i );
94 ProfilePoint aSecPoint = aProfilePoints.Value( i + 1 );
96 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
97 gp_Pnt aPnt2( aSecPoint.X(), aSecPoint.Y(), aSecPoint.Z() );
99 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
100 TopoDS_Edge anEdge = aMakeEdge;
102 aMakeWire.Add( anEdge );
106 if ( aMakeWire.IsDone() )
112 QColor HYDROData_Profile::DefaultFillingColor()
114 return QColor( Qt::transparent );
117 QColor HYDROData_Profile::DefaultBorderColor()
119 return QColor( Qt::black );
122 QColor HYDROData_Profile::getDefaultFillingColor() const
124 return DefaultFillingColor();
127 QColor HYDROData_Profile::getDefaultBorderColor() const
129 return DefaultBorderColor();
132 bool HYDROData_Profile::IsValid() const
134 gp_XY aFirstPoint, aLastPoint;
135 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
138 int aNbPoints = NbPoints();
139 return aNbPoints > 1;
142 void HYDROData_Profile::SetLeftPoint( const gp_XY& thePoint )
144 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
146 Handle(TDataStd_RealArray) anArray;
147 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
148 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
150 anArray->SetValue( 0, thePoint.X() );
151 anArray->SetValue( 1, thePoint.Y() );
156 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint ) const
158 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
159 if ( aLabel.IsNull() )
162 Handle(TDataStd_RealArray) anArray;
163 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
166 thePoint.SetX( anArray->Value( 0 ) );
167 thePoint.SetY( anArray->Value( 1 ) );
172 void HYDROData_Profile::SetRightPoint( const gp_XY& thePoint )
174 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
176 Handle(TDataStd_RealArray) anArray;
177 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
178 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
180 anArray->SetValue( 0, thePoint.X() );
181 anArray->SetValue( 1, thePoint.Y() );
186 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint ) const
188 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
189 if ( aLabel.IsNull() )
192 Handle(TDataStd_RealArray) anArray;
193 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
196 thePoint.SetX( anArray->Value( 0 ) );
197 thePoint.SetY( anArray->Value( 1 ) );
202 void HYDROData_Profile::Invalidate()
204 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
205 if ( !aFirstLabel.IsNull() )
206 aFirstLabel.ForgetAllAttributes();
208 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
209 if ( !aLastLabel.IsNull() )
210 aLastLabel.ForgetAllAttributes();
215 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
217 Handle(HYDROData_ProfileUZ) aProfileUZ;
219 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
220 if ( aLabel.IsNull() )
223 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
224 if ( aProfileUZ.IsNull() && theIsCreate )
226 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
227 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
233 int HYDROData_Profile::NbPoints() const
235 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
236 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
239 void HYDROData_Profile::RemovePoints()
241 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
242 if ( !aProfileUZ.IsNull() )
244 aProfileUZ->RemoveSections();
249 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
253 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
254 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
256 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
257 aProfileUZ->AddPoint( 0, aPoint );
263 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
265 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
266 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
269 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
272 if ( thePoints.Length() < 2 )
275 gp_XY aFirstPoint, aLastPoint;
277 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
278 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
280 const ProfilePoint& aPoint = thePoints.Value( i );
281 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
284 aFirstPoint = aPointXY;
286 aLastPoint = aPointXY;
288 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
290 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
291 aProfileUZ->AddPoint( 0, aParPoint );
294 SetLeftPoint( aFirstPoint );
295 SetRightPoint( aLastPoint );
298 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
300 ProfilePoints aResPoints;
302 gp_XY aFirstPoint, aLastPoint;
303 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
306 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
307 if ( aParametricPoints.Length() < 2 )
310 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
311 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
313 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
314 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
316 // Add first point as is
317 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
319 // Compute all other points
320 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
322 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
324 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
326 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
328 double aRatio = aParLen / ( aGeoDistance - aParLen );
330 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
331 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
333 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
334 aResPoints.Append( aCompPoint );
337 // Add last point as is
338 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
343 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
345 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
348 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
350 TCollection_AsciiString aRes;
352 Handle(TDataStd_AsciiString) anAsciiStr;
353 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
354 aRes = anAsciiStr->Get();
359 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
360 const TCollection_AsciiString& theFileName )
362 if ( theDoc.IsNull() || theFileName.IsEmpty() )
365 OSD_File aFile( theFileName );
366 if ( !aFile.IsReadable() )
369 aFile.Open( OSD_ReadOnly, OSD_Protection() );
370 if ( !aFile.IsOpen() )
373 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
375 Handle(HYDROData_Profile) aNewProfile;
376 while ( !aFile.IsAtEnd() )
378 if ( aNewProfile.IsNull() )
379 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
381 if ( aNewProfile->ImportFromFile( aFile ) )
383 aCreatedProfiles.Append( aNewProfile );
384 aNewProfile.Nullify();
388 if ( !aNewProfile.IsNull() )
389 aNewProfile->Remove();
394 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
396 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
398 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
399 aProfile->SetName( aProfileName );
401 aProfile->SetFilePath( theFileName );
403 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
406 return !aCreatedProfiles.IsEmpty();
409 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
411 // Try to open the file
412 OSD_File aFile( theFileName );
413 if ( !aFile.IsReadable() )
416 aFile.Open( OSD_ReadOnly, OSD_Protection() );
417 if ( !aFile.IsOpen() )
420 bool aRes = ImportFromFile( aFile );
428 SetFilePath( theFileName );
434 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
436 if ( !theFile.IsOpen() )
441 bool anIsParametric = false;
442 bool anIsGeoref = false;
444 HYDROData_ProfileUZ::PointsList aPointsUZ;
445 ProfilePoints aPointsXYZ;
447 double aPrevVal = -DBL_MAX;
448 while ( !theFile.IsAtEnd() )
450 Standard_Integer aNbRead = 0;
451 TCollection_AsciiString aLine;
452 theFile.ReadLine( aLine, 1024, aNbRead );
454 aLine.LeftAdjust(); aLine.RightAdjust();
455 if ( aLine.IsEmpty() )
457 if ( !anIsParametric && !anIsGeoref )
458 continue; // Definition is not started yet
460 break; // Next profile started
463 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
464 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
465 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
467 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
468 aValY.IsEmpty() || !aValY.IsRealValue() )
474 if ( !anIsParametric && !anIsGeoref )
476 anIsParametric = aValZ.IsEmpty();
477 anIsGeoref = !aValZ.IsEmpty();
480 double aCoordX = aValX.RealValue();
481 double aCoordY = aValY.RealValue();
483 if ( anIsParametric )
485 if ( aCoordX < aPrevVal )
487 // Move back readed line
488 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
492 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
493 aPointsUZ.Append( aPoint );
499 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
505 double aCoordZ = aValZ.RealValue();
507 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
508 aPointsXYZ.Append( aPoint );
512 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
513 anIsGeoref && !aPointsXYZ.IsEmpty() );
516 // Update profile points
517 if ( anIsParametric )
519 SetParametricPoints( aPointsUZ );
521 else if ( anIsGeoref )
523 SetProfilePoints( aPointsXYZ );