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 <boost/math/special_functions/fpclassify.hpp>
11 #include <BRepBuilderAPI_MakeEdge.hxx>
12 #include <BRepBuilderAPI_MakeWire.hxx>
13 #include <BRepBuilderAPI_MakePolygon.hxx>
17 #include <gp_Pnt2d.hxx>
19 #include <TDataStd_AsciiString.hxx>
20 #include <TDataStd_RealArray.hxx>
22 #include <TopoDS_Edge.hxx>
23 #include <TopoDS_Wire.hxx>
25 #include <OSD_File.hxx>
26 #include <OSD_Protection.hxx>
29 #include <QStringList>
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
45 QStringList aResList = dumpObjectCreation( theTreatedObjects );
46 QString aProfileName = GetObjPyName();
48 //TCollection_AsciiString aFilePath = GetFilePath();
49 //if ( !aFilePath.IsEmpty() )
51 // aResList << QString( "%1.ImportFromFile( \"%2\" );" )
52 // .arg( aName ).arg( aFilePath.ToCString() );
55 bool anIsValidProfile = IsValid();
57 QStringList aPntsDefinition;
58 QString aPntsListName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "profile_points" );
60 QString aGap = QString().fill( ' ', aPntsListName.length() + 5 );
61 if ( anIsValidProfile )
63 HYDROData_Profile::ProfilePoints aPointsList = GetProfilePoints();
64 for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
66 const ProfilePoint& aPoint = aPointsList.Value( k );
67 aPntsDefinition << QString( aGap + "gp_XYZ( %1, %2, %3 )%4" )
68 .arg( aPoint.X() ).arg( aPoint.Y() ).arg( aPoint.Z() )
69 .arg( ( k < aNbPoints ? "," : "" ) );
74 HYDROData_IPolyline::PointsList aPointsList = GetParametricPoints();
75 for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
77 const HYDROData_IPolyline::Point& aPoint = aPointsList.Value( k );
78 aPntsDefinition << QString( aGap + "gp_XY( %1, %2 )%3" )
79 .arg( aPoint.X() ).arg( aPoint.Y() )
80 .arg( ( k < aNbPoints ? "," : "" ) );
84 if ( !aPntsDefinition.isEmpty() )
86 QString& aFirstStr = aPntsDefinition.first();
87 aFirstStr = aFirstStr.trimmed();
88 aFirstStr.prepend( QString( "%1 = [ " ).arg( aPntsListName ) );
90 aPntsDefinition.last().append( " ];" );
92 aResList << aPntsDefinition;
94 aResList << QString( "%1.%3( %2 );" )
95 .arg( aProfileName ).arg( aPntsListName )
96 .arg( anIsValidProfile ? "SetProfilePoints" : "SetParametricPoints" );
98 aResList << QString( "" );
101 // Set a polyline type if it is not default
102 Handle(HYDROData_ProfileUZ) aPrf = GetProfileUZ( false );
103 if ( !aPrf.IsNull() )
105 HYDROData_IPolyline::SectionType aSecType = aPrf->GetSectionType( 0 );
106 if ( aSecType != HYDROData_IPolyline::SECTION_POLYLINE )
108 aResList << QString( "%1.GetProfileUZ().SetSectionType( 0, %2 );" )
109 .arg( aProfileName ).arg( "HYDROData_IPolyline.SECTION_SPLINE" );
110 aResList << QString( "" );
114 aResList << QString( "%1.Update();" ).arg( aProfileName );
115 aResList << QString( "" );
120 TopoDS_Shape HYDROData_Profile::GetTopShape() const
124 gp_XY aFirstPoint, aLastPoint;
125 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
128 gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
129 gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), 0 );
131 BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
132 TopoDS_Edge anEdge = aMakeEdge;
134 BRepBuilderAPI_MakeWire aMakeWire( anEdge );
140 TopoDS_Shape HYDROData_Profile::GetShape3D() const
145 void HYDROData_Profile::Update()
147 HYDROData_Object::Update();
150 Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
151 if ( !aProfile.IsNull() )
153 ProfilePoints aProfilePoints = GetProfilePoints();
154 HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
156 aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
161 QColor HYDROData_Profile::DefaultFillingColor()
163 return QColor( Qt::transparent );
166 QColor HYDROData_Profile::DefaultBorderColor()
168 return QColor( Qt::black );
171 QColor HYDROData_Profile::getDefaultFillingColor() const
173 return DefaultFillingColor();
176 QColor HYDROData_Profile::getDefaultBorderColor() const
178 return DefaultBorderColor();
181 bool HYDROData_Profile::IsValid() const
183 gp_XY aFirstPoint, aLastPoint;
184 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
187 int aNbPoints = NbPoints();
188 return aNbPoints > 1;
191 void HYDROData_Profile::SetLeftPoint( const gp_XY& thePoint )
193 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
195 Handle(TDataStd_RealArray) anArray;
196 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
197 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
199 anArray->SetValue( 0, thePoint.X() );
200 anArray->SetValue( 1, thePoint.Y() );
205 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint ) const
207 TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
208 if ( aLabel.IsNull() )
211 Handle(TDataStd_RealArray) anArray;
212 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
215 thePoint.SetX( anArray->Value( 0 ) );
216 thePoint.SetY( anArray->Value( 1 ) );
221 void HYDROData_Profile::SetRightPoint( const gp_XY& thePoint )
223 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
225 Handle(TDataStd_RealArray) anArray;
226 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
227 anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
229 anArray->SetValue( 0, thePoint.X() );
230 anArray->SetValue( 1, thePoint.Y() );
235 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint ) const
237 TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
238 if ( aLabel.IsNull() )
241 Handle(TDataStd_RealArray) anArray;
242 if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
245 thePoint.SetX( anArray->Value( 0 ) );
246 thePoint.SetY( anArray->Value( 1 ) );
251 void HYDROData_Profile::Invalidate()
253 TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
254 if ( !aFirstLabel.IsNull() )
255 aFirstLabel.ForgetAllAttributes();
257 TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
258 if ( !aLastLabel.IsNull() )
259 aLastLabel.ForgetAllAttributes();
264 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
266 Handle(HYDROData_ProfileUZ) aProfileUZ;
268 TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
269 if ( aLabel.IsNull() )
272 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
273 if ( aProfileUZ.IsNull() && theIsCreate )
275 aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
276 HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
282 int HYDROData_Profile::NbPoints() const
284 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
285 return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
288 void HYDROData_Profile::RemovePoints()
290 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
291 if ( !aProfileUZ.IsNull() )
293 aProfileUZ->RemoveSections();
298 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
302 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
303 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
305 const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
306 aProfileUZ->AddPoint( 0, aPoint );
312 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
314 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
315 return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
318 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints )
321 if ( thePoints.Length() < 2 )
324 gp_XY aFirstPoint, aLastPoint;
326 Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
327 for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
329 const ProfilePoint& aPoint = thePoints.Value( i );
330 gp_XY aPointXY( aPoint.X(), aPoint.Y() );
333 aFirstPoint = aPointXY;
335 aLastPoint = aPointXY;
337 double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
339 HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
340 aProfileUZ->AddPoint( 0, aParPoint );
343 SetLeftPoint( aFirstPoint );
344 SetRightPoint( aLastPoint );
347 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints() const
349 ProfilePoints aResPoints;
351 gp_XY aFirstPoint, aLastPoint;
352 if ( !GetLeftPoint( aFirstPoint ) || !GetRightPoint( aLastPoint ) )
355 HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
356 if ( aParametricPoints.Length() < 2 )
359 const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
360 const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
362 double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
363 double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
365 // Add first point as is
366 aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
368 // Compute all other points
369 for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
371 const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
373 double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
375 double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
377 double aRatio = aParLen / ( aGeoDistance - aParLen );
379 double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
380 double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
382 ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
383 aResPoints.Append( aCompPoint );
386 // Add last point as is
387 aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
392 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
394 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
397 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
399 TCollection_AsciiString aRes;
401 Handle(TDataStd_AsciiString) anAsciiStr;
402 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
403 aRes = anAsciiStr->Get();
408 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
409 const TCollection_AsciiString& theFileName,
410 NCollection_Sequence<int>& theBadProfilesIds )
412 if ( theDoc.IsNull() || theFileName.IsEmpty() )
415 OSD_File aFile( theFileName );
416 if ( !aFile.IsReadable() )
419 aFile.Open( OSD_ReadOnly, OSD_Protection() );
420 if ( !aFile.IsOpen() )
423 NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
426 Handle(HYDROData_Profile) aNewProfile;
427 for ( ; !aFile.IsAtEnd(); ++aProfileId )
429 if ( aNewProfile.IsNull() )
430 aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
432 bool anIsRead = false;
433 if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
435 aCreatedProfiles.Append( aNewProfile );
436 aNewProfile.Nullify();
440 theBadProfilesIds.Append( aProfileId );
444 if ( !aNewProfile.IsNull() )
445 aNewProfile->Remove();
450 for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
452 Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
454 QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
455 aProfile->SetName( aProfileName );
457 aProfile->SetFilePath( theFileName );
459 aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
462 return aCreatedProfiles.Length();
465 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
471 // Try to open the file
472 OSD_File aFile( theFileName );
473 if ( !aFile.IsReadable() )
476 aFile.Open( OSD_ReadOnly, OSD_Protection() );
477 if ( !aFile.IsOpen() )
480 bool aRes = ImportFromFile( aFile, theIsRead );
488 SetFilePath( theFileName );
494 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
500 if ( !theFile.IsOpen() )
505 bool anIsParametric = false;
506 bool anIsGeoref = false;
508 HYDROData_ProfileUZ::PointsList aPointsUZ;
509 ProfilePoints aPointsXYZ;
511 double aPrevVal = -DBL_MAX;
512 while ( !theFile.IsAtEnd() )
514 Standard_Integer aNbRead = 0;
515 TCollection_AsciiString aLine;
516 theFile.ReadLine( aLine, 1024, aNbRead );
518 aLine.LeftAdjust(); aLine.RightAdjust();
519 if ( aLine.IsEmpty() )
521 if ( !anIsParametric && !anIsGeoref )
522 continue; // Definition is not started yet
524 break; // Next profile started
527 // Set flag of read status to true
531 TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
532 TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
533 TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
535 if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
536 aValY.IsEmpty() || !aValY.IsRealValue() )
542 if ( !anIsParametric && !anIsGeoref )
544 anIsParametric = aValZ.IsEmpty();
545 anIsGeoref = !aValZ.IsEmpty();
548 double aCoordX = aValX.RealValue();
549 double aCoordY = aValY.RealValue();
551 if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
552 boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
555 if ( anIsParametric )
557 if ( aCoordX < aPrevVal )
559 // Move back readed line
560 theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
564 HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
565 aPointsUZ.Append( aPoint );
571 if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
577 double aCoordZ = aValZ.RealValue();
578 if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
581 ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
582 aPointsXYZ.Append( aPoint );
586 aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() ||
587 anIsGeoref && !aPointsXYZ.IsEmpty() );
590 // Update profile points
591 if ( anIsParametric )
593 SetParametricPoints( aPointsUZ );
595 else if ( anIsGeoref )
597 SetProfilePoints( aPointsXYZ );