2 #include "HYDROData_Profile.h"
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Iterator.h"
6 #include "HYDROData_Tool.h"
7 #include "HYDROData_PolylineXY.h"
9 #include <BRepBuilderAPI_MakeEdge.hxx>
10 #include <BRepBuilderAPI_MakeWire.hxx>
11 #include <BRepBuilderAPI_MakePolygon.hxx>
15 #include <gp_Pnt2d.hxx>
17 #include <TDataStd_AsciiString.hxx>
18 #include <TDataStd_RealArray.hxx>
20 #include <TopoDS_Edge.hxx>
21 #include <TopoDS_Wire.hxx>
23 #include <OSD_File.hxx>
24 #include <OSD_Protection.hxx>
27 #include <QStringList>
29 #define PYTHON_PROFILE_ID "KIND_PROFILE"
31 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
32 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
34 HYDROData_Profile::HYDROData_Profile()
39 HYDROData_Profile::~HYDROData_Profile()
43 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
47 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
48 if ( aDocument.IsNull() )
51 QString aDocName = aDocument->GetDocPyName();
52 QString aProfileName = GetName();
54 aResList << QString( "%1 = %2.CreateObject( %3 );" )
55 .arg( aProfileName ).arg( aDocName ).arg( PYTHON_PROFILE_ID );
56 aResList << QString( "%1.SetName( \"%1\" );" ).arg( aProfileName );
61 TopoDS_Shape HYDROData_Profile::GetTopShape() const
65 gp_XY aFirstPoint, aLastPoint;
66 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
69 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
70 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
72 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
73 TopoDS_Edge anEdge = aMakeEdge;
75 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
81 TopoDS_Shape HYDROData_Profile::GetShape3D() const
86 void HYDROData_Profile::Update()
88 HYDROData_Object::Update();
91 Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
92 if ( !aProfile.IsNull() )
94 ProfilePoints aProfilePoints = GetProfilePoints();
95 HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
97 aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
102 QColor HYDROData_Profile::DefaultFillingColor()
104 return QColor( Qt::transparent );
107 QColor HYDROData_Profile::DefaultBorderColor()
109 return QColor( Qt::black );
112 QColor HYDROData_Profile::getDefaultFillingColor() const
114 return DefaultFillingColor();
117 QColor HYDROData_Profile::getDefaultBorderColor() const
119 return DefaultBorderColor();
122 bool HYDROData_Profile::IsValid() const
124 gp_XY aFirstPoint, aLastPoint;
125 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
128 int aNbPoints = NbPoints();
129 return aNbPoints > 1;
132 void HYDROData_Profile::SetLeftPoint( const gp_XY& thePoint )
134 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
136 Handle(TDataStd_RealArray) anArray;
137 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
138 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
140 anArray->SetValue( 0, thePoint.X() );
141 anArray->SetValue( 1, thePoint.Y() );
146 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint ) const
148 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
149 if ( aLabel.IsNull() )
152 Handle(TDataStd_RealArray) anArray;
153 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
156 thePoint.SetX( anArray->Value( 0 ) );
157 thePoint.SetY( anArray->Value( 1 ) );
162 void HYDROData_Profile::SetRightPoint( const gp_XY& thePoint )
164 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
166 Handle(TDataStd_RealArray) anArray;
167 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
168 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
170 anArray->SetValue( 0, thePoint.X() );
171 anArray->SetValue( 1, thePoint.Y() );
176 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint ) const
178 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
179 if ( aLabel.IsNull() )
182 Handle(TDataStd_RealArray) anArray;
183 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
186 thePoint.SetX( anArray->Value( 0 ) );
187 thePoint.SetY( anArray->Value( 1 ) );
192 void HYDROData_Profile::Invalidate()
194 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
195 if ( !aFirstLabel.IsNull() )
196 aFirstLabel.ForgetAllAttributes();
198 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
199 if ( !aLastLabel.IsNull() )
200 aLastLabel.ForgetAllAttributes();
205 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
207 Handle(HYDROData_ProfileUZ) aProfileUZ;
209 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
210 if ( aLabel.IsNull() )
213 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
214 if ( aProfileUZ.IsNull() && theIsCreate )
216 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
217 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
223 int HYDROData_Profile::NbPoints() const
225 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
226 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
229 void HYDROData_Profile::RemovePoints()
231 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
232 if ( !aProfileUZ.IsNull() )
234 aProfileUZ->RemoveSections();
239 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
243 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
244 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
246 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
247 aProfileUZ->AddPoint( 0, aPoint );
253 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
255 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
256 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
259 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
262 if ( thePoints.Length() < 2 )
265 gp_XY aFirstPoint, aLastPoint;
267 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
268 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
270 const ProfilePoint& aPoint = thePoints.Value( i );
271 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
274 aFirstPoint = aPointXY;
276 aLastPoint = aPointXY;
278 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
280 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
281 aProfileUZ->AddPoint( 0, aParPoint );
284 SetLeftPoint( aFirstPoint );
285 SetRightPoint( aLastPoint );
288 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
290 ProfilePoints aResPoints;
292 gp_XY aFirstPoint, aLastPoint;
293 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
296 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
297 if ( aParametricPoints.Length() < 2 )
300 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
301 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
303 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
304 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
306 // Add first point as is
307 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
309 // Compute all other points
310 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
312 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
314 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
316 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
318 double aRatio = aParLen / ( aGeoDistance - aParLen );
320 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
321 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
323 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
324 aResPoints.Append( aCompPoint );
327 // Add last point as is
328 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
333 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
335 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
338 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
340 TCollection_AsciiString aRes;
342 Handle(TDataStd_AsciiString) anAsciiStr;
343 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
344 aRes = anAsciiStr->Get();
349 bool HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
350 const TCollection_AsciiString& theFileName )
352 if ( theDoc.IsNull() || theFileName.IsEmpty() )
355 OSD_File aFile( theFileName );
356 if ( !aFile.IsReadable() )
359 aFile.Open( OSD_ReadOnly, OSD_Protection() );
360 if ( !aFile.IsOpen() )
363 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
365 Handle(HYDROData_Profile) aNewProfile;
366 while ( !aFile.IsAtEnd() )
368 if ( aNewProfile.IsNull() )
369 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
371 if ( aNewProfile->ImportFromFile( aFile ) )
373 aCreatedProfiles.Append( aNewProfile );
374 aNewProfile.Nullify();
378 if ( !aNewProfile.IsNull() )
379 aNewProfile->Remove();
384 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
386 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
388 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
389 aProfile->SetName( aProfileName );
391 aProfile->SetFilePath( theFileName );
393 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
396 return !aCreatedProfiles.IsEmpty();
399 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName )
401 // Try to open the file
402 OSD_File aFile( theFileName );
403 if ( !aFile.IsReadable() )
406 aFile.Open( OSD_ReadOnly, OSD_Protection() );
407 if ( !aFile.IsOpen() )
410 bool aRes = ImportFromFile( aFile );
418 SetFilePath( theFileName );
424 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile )
426 if ( !theFile.IsOpen() )
431 bool anIsParametric = false;
432 bool anIsGeoref = false;
434 HYDROData_ProfileUZ::PointsList aPointsUZ;
435 ProfilePoints aPointsXYZ;
437 double aPrevVal = -DBL_MAX;
438 while ( !theFile.IsAtEnd() )
440 Standard_Integer aNbRead = 0;
441 TCollection_AsciiString aLine;
442 theFile.ReadLine( aLine, 1024, aNbRead );
444 aLine.LeftAdjust(); aLine.RightAdjust();
445 if ( aLine.IsEmpty() )
447 if ( !anIsParametric && !anIsGeoref )
448 continue; // Definition is not started yet
450 break; // Next profile started
453 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
454 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
455 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
457 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
458 aValY.IsEmpty() || !aValY.IsRealValue() )
464 if ( !anIsParametric && !anIsGeoref )
466 anIsParametric = aValZ.IsEmpty();
467 anIsGeoref = !aValZ.IsEmpty();
470 double aCoordX = aValX.RealValue();
471 double aCoordY = aValY.RealValue();
473 if ( anIsParametric )
475 if ( aCoordX < aPrevVal )
477 // Move back readed line
478 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
482 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
483 aPointsUZ.Append( aPoint );
489 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
495 double aCoordZ = aValZ.RealValue();
497 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
498 aPointsXYZ.Append( aPoint );
502 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
503 anIsGeoref && !aPointsXYZ.IsEmpty() );
506 // Update profile points
507 if ( anIsParametric )
509 SetParametricPoints( aPointsUZ );
511 else if ( anIsGeoref )
513 SetProfilePoints( aPointsXYZ );