Salome HOME
3a628a74c4b8e321e955cc780f3bca15a1e05bc6
[modules/hydro.git] / src / HYDROData / HYDROData_Profile.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_Profile.h"
24
25 #include "HYDROData_Document.h"
26 #include "HYDROData_Iterator.h"
27 #include "HYDROData_Tool.h"
28 #include "HYDROData_PolylineXY.h"
29
30 #include <boost/math/special_functions/fpclassify.hpp>
31
32 #include <BRepBuilderAPI_MakeEdge.hxx>
33 #include <BRepBuilderAPI_MakeWire.hxx>
34 #include <BRepBuilderAPI_MakePolygon.hxx>
35
36 #include <gp_XY.hxx>
37 #include <gp_XYZ.hxx>
38 #include <gp_Pnt2d.hxx>
39
40 #include <TDataStd_AsciiString.hxx>
41 #include <TDataStd_RealArray.hxx>
42
43 #include <TopoDS_Edge.hxx>
44 #include <TopoDS_Wire.hxx>
45
46 #include <OSD_File.hxx>
47 #include <OSD_Protection.hxx>
48
49 #include <QColor>
50 #include <QStringList>
51
52 IMPLEMENT_STANDARD_HANDLE(HYDROData_Profile, HYDROData_Object)
53 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Profile, HYDROData_Object)
54
55 HYDROData_Profile::HYDROData_Profile()
56 : HYDROData_Object()
57 {
58 }
59
60 HYDROData_Profile::~HYDROData_Profile()
61 {
62 }
63
64 QStringList HYDROData_Profile::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
65 {
66   QStringList aResList = dumpObjectCreation( theTreatedObjects );
67   QString aProfileName = GetObjPyName();
68
69   //TCollection_AsciiString aFilePath = GetFilePath();
70   //if ( !aFilePath.IsEmpty() ) 
71   //{
72   //  aResList << QString( "%1.ImportFromFile( \"%2\" );" )
73   //            .arg( aName ).arg( aFilePath.ToCString() );
74   //}
75
76   bool anIsValidProfile = IsValid();
77   
78   QStringList aPntsDefinition;
79   QString aPntsListName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "profile_points" );
80
81   QString aGap = QString().fill( ' ', aPntsListName.length() + 5 );
82   if ( anIsValidProfile )
83   {
84     HYDROData_Profile::ProfilePoints aPointsList = GetProfilePoints( true );
85     for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
86     {
87       const ProfilePoint& aPoint = aPointsList.Value( k );
88       aPntsDefinition << QString( aGap + "gp_XYZ( %1, %2, %3 )%4" )
89                          .arg( aPoint.X() ).arg( aPoint.Y() ).arg( aPoint.Z() )
90                          .arg( ( k < aNbPoints ? "," : "" ) );
91     }
92   }
93   else
94   {
95     HYDROData_IPolyline::PointsList aPointsList = GetParametricPoints();
96     for ( int k = 1, aNbPoints = aPointsList.Size(); k <= aNbPoints; ++k )
97     {
98       const HYDROData_IPolyline::Point& aPoint = aPointsList.Value( k );
99       aPntsDefinition << QString( aGap + "gp_XY( %1, %2 )%3" )
100                          .arg( aPoint.X() ).arg( aPoint.Y() )
101                          .arg( ( k < aNbPoints ? "," : "" ) );
102     }
103   }
104
105   if ( !aPntsDefinition.isEmpty() )
106   {
107     QString& aFirstStr = aPntsDefinition.first();
108     aFirstStr = aFirstStr.trimmed();
109     aFirstStr.prepend( QString( "%1 = [ " ).arg( aPntsListName ) );
110     
111     aPntsDefinition.last().append( " ];" );
112
113     aResList << aPntsDefinition;
114     
115     aResList << QString( "%1.%3( %2 );" )
116                 .arg( aProfileName ).arg( aPntsListName )
117                 .arg( anIsValidProfile ? "SetProfilePoints" : "SetParametricPoints" );
118   
119     aResList << QString( "" );
120   }
121
122   // Set a polyline type if it is not default
123   Handle(HYDROData_ProfileUZ) aPrf = GetProfileUZ( false );
124   if ( !aPrf.IsNull() )
125   {
126     HYDROData_IPolyline::SectionType aSecType = aPrf->GetSectionType( 0 );
127     if ( aSecType != HYDROData_IPolyline::SECTION_POLYLINE )
128     {
129       aResList << QString( "%1.GetProfileUZ().SetSectionType( 0, %2 );" )
130                   .arg( aProfileName ).arg( "HYDROData_IPolyline.SECTION_SPLINE" );
131       aResList << QString( "" );
132     }
133   }
134
135   aResList << QString( "%1.Update();" ).arg( aProfileName );
136   aResList << QString( "" );
137
138   return aResList;
139 }
140
141 TopoDS_Shape HYDROData_Profile::GetTopShape() const
142 {
143   TopoDS_Wire aWire;
144
145   gp_XY aFirstPoint, aLastPoint;
146   if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
147     return aWire;
148
149   gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), 0 );
150   gp_Pnt aPnt2( aLastPoint.X(),  aLastPoint.Y(),  0 );
151
152   BRepBuilderAPI_MakeEdge aMakeEdge( aPnt1, aPnt2 );
153   TopoDS_Edge anEdge = aMakeEdge;
154
155   BRepBuilderAPI_MakeWire aMakeWire( anEdge );
156   aWire = aMakeWire;
157
158   return aWire;
159 }
160
161 TopoDS_Shape HYDROData_Profile::GetShape3D() const
162 {
163   return getShape3D();
164 }
165
166 void HYDROData_Profile::Update()
167 {
168   HYDROData_Object::Update();
169
170   TopoDS_Wire aWire;
171   Handle(HYDROData_ProfileUZ) aProfile = GetProfileUZ( false );
172   if ( !aProfile.IsNull() )
173   {
174     ProfilePoints aProfilePoints = GetProfilePoints( false );
175     HYDROData_IPolyline::SectionType aSectionType = aProfile->GetSectionType( 0 );
176
177     aWire = HYDROData_PolylineXY::BuildWire( aSectionType, false, aProfilePoints );
178   }
179   SetShape3D( aWire );
180 }
181
182 QColor HYDROData_Profile::DefaultFillingColor()
183 {
184   return QColor( Qt::transparent );
185 }
186
187 QColor HYDROData_Profile::DefaultBorderColor()
188 {
189   return QColor( Qt::black );
190 }
191
192 QColor HYDROData_Profile::getDefaultFillingColor() const
193 {
194   return DefaultFillingColor();
195 }
196
197 QColor HYDROData_Profile::getDefaultBorderColor() const
198 {
199   return DefaultBorderColor();
200 }
201
202 bool HYDROData_Profile::IsValid() const
203 {
204   gp_XY aFirstPoint, aLastPoint;
205   if ( !GetLeftPoint( aFirstPoint, false ) || !GetRightPoint( aLastPoint, false ) )
206     return false;
207
208   int aNbPoints = NbPoints();
209   return aNbPoints > 1;
210 }
211
212 void HYDROData_Profile::SetLeftPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
213 {
214   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint );
215   if ( aLabel.IsNull() )
216     return;
217
218   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
219   gp_XY aLPoint = theGPoint;
220   if( IsConvertFromGlobal )
221     aDoc->Transform( aLPoint, true );
222
223   Handle(TDataStd_RealArray) anArray;
224   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
225     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
226
227   anArray->SetValue( 0, aLPoint.X() );
228   anArray->SetValue( 1, aLPoint.Y() );
229
230   SetToUpdate( true );
231 }
232
233 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
234 {
235   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
236   if ( aLabel.IsNull() )
237     return false;
238
239   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
240   Handle(TDataStd_RealArray) anArray;
241   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
242     return false;
243
244   thePoint.SetX( anArray->Value( 0 ) );
245   thePoint.SetY( anArray->Value( 1 ) );
246
247   if( IsConvertToGlobal )
248     aDoc->Transform( thePoint, false );
249
250   return true;
251 }
252
253 void HYDROData_Profile::SetRightPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
254 {
255   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
256
257   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
258   gp_XY aLPoint = theGPoint;
259   if( IsConvertFromGlobal )
260     aDoc->Transform( aLPoint, true );
261
262   Handle(TDataStd_RealArray) anArray;
263   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
264     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
265
266   anArray->SetValue( 0, aLPoint.X() );
267   anArray->SetValue( 1, aLPoint.Y() );
268
269   SetToUpdate( true );
270 }
271
272 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint, bool IsConvertToGlobal ) const
273 {
274   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
275   if ( aLabel.IsNull() )
276     return false;
277
278   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
279   Handle(TDataStd_RealArray) anArray;
280   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
281     return false;
282
283   thePoint.SetX( anArray->Value( 0 ) );
284   thePoint.SetY( anArray->Value( 1 ) );
285
286   if( IsConvertToGlobal )
287     aDoc->Transform( thePoint, false );
288
289   return true;
290 }
291
292 void HYDROData_Profile::Invalidate()
293 {
294   TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
295   if ( !aFirstLabel.IsNull() )
296     aFirstLabel.ForgetAllAttributes();
297
298   TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
299   if ( !aLastLabel.IsNull() )
300     aLastLabel.ForgetAllAttributes();
301
302   SetToUpdate( true );
303 }
304
305 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
306 {
307   Handle(HYDROData_ProfileUZ) aProfileUZ;
308
309   TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
310   if ( aLabel.IsNull() )
311     return aProfileUZ;
312
313   aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
314   if ( aProfileUZ.IsNull() && theIsCreate )
315   {
316     aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
317       HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
318   }
319
320   return aProfileUZ;
321 }
322
323 int HYDROData_Profile::NbPoints() const
324 {
325   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
326   return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
327 }
328
329 void HYDROData_Profile::RemovePoints()
330 {
331   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
332   if ( !aProfileUZ.IsNull() )
333   {
334     aProfileUZ->RemoveSections();
335     SetToUpdate( true );
336   }
337 }
338
339 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
340 {
341   RemovePoints();
342
343   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
344   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
345   {
346     const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
347     aProfileUZ->AddPoint( 0, aPoint );
348   }
349
350   SetToUpdate( true );
351 }
352
353 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
354 {
355   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
356   return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
357 }
358
359 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints, bool IsConvertFromGlobal )
360 {
361   RemovePoints();
362   if ( thePoints.Length() < 2 )
363     return;
364
365   gp_XY aFirstPoint, aLastPoint;
366
367   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
368   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
369   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
370   {
371     ProfilePoint aPoint = thePoints.Value( i );
372     if( IsConvertFromGlobal )
373       aDoc->Transform( aPoint, true );
374
375     gp_XY aPointXY( aPoint.X(), aPoint.Y() );
376
377     if ( i == 1 )
378       aFirstPoint = aPointXY;
379     else if ( i == n )
380       aLastPoint = aPointXY;
381
382     double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
383     
384     HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
385     aProfileUZ->AddPoint( 0, aParPoint );
386   }
387
388   SetLeftPoint( aFirstPoint, false );//already converted to local CS
389   SetRightPoint( aLastPoint, false );
390 }
391
392 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints( bool IsConvertToGlobal ) const
393 {
394   ProfilePoints aResPoints;
395
396   gp_XY aFirstPoint, aLastPoint;
397   if ( !GetLeftPoint( aFirstPoint, IsConvertToGlobal ) ||
398        !GetRightPoint( aLastPoint, IsConvertToGlobal ) )
399     return aResPoints;
400
401   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
402   if ( aParametricPoints.Length() < 2 )
403     return aResPoints;
404
405   const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
406   const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
407
408   double aGeoDistance = gp_Pnt2d( aFirstPoint ).Distance( aLastPoint );
409   double aParCommonDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aLastParPoint.X(), 0 ) );
410
411   // Add first point as is
412   aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
413
414   // Compute all other points
415   for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
416   {
417     const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
418
419     double aParPointDist = gp_Pnt2d( aFirstParPoint.X(), 0 ).Distance( gp_Pnt2d( aParPoint.X(), 0 ) );
420     
421     double aParLen = ( aParPointDist / aParCommonDist ) * aGeoDistance;
422
423     double aRatio = aParLen / ( aGeoDistance - aParLen );
424
425     double aParX = ( aFirstPoint.X() + aRatio * aLastPoint.X() ) / ( 1 + aRatio );
426     double aParY = ( aFirstPoint.Y() + aRatio * aLastPoint.Y() ) / ( 1 + aRatio );
427
428     ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
429     aResPoints.Append( aCompPoint );
430   }
431
432   // Add last point as is
433   aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
434
435   return aResPoints;
436 }
437
438 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
439 {
440   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
441 }
442
443 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
444 {
445   TCollection_AsciiString aRes;
446
447   Handle(TDataStd_AsciiString) anAsciiStr;
448   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
449     aRes = anAsciiStr->Get();
450
451   return aRes;
452 }
453
454 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
455                                        const TCollection_AsciiString&    theFileName,
456                                        NCollection_Sequence<int>&        theBadProfilesIds )
457 {
458   if ( theDoc.IsNull() || theFileName.IsEmpty() )
459     return 0;
460
461   OSD_File aFile( theFileName );
462   if ( !aFile.IsReadable() )
463     return 0;
464
465   aFile.Open( OSD_ReadOnly, OSD_Protection() );
466   if ( !aFile.IsOpen() )
467     return 0;
468
469   NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
470
471   int aProfileId = 1;
472   Handle(HYDROData_Profile) aNewProfile;
473   for ( ; !aFile.IsAtEnd(); ++aProfileId )
474   {
475     if ( aNewProfile.IsNull() )
476       aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
477     
478     bool anIsRead = false;
479     if ( aNewProfile->ImportFromFile( aFile, &anIsRead ) )
480     {
481       aCreatedProfiles.Append( aNewProfile );
482       aNewProfile.Nullify();
483     }
484     else if ( anIsRead )
485     {
486       theBadProfilesIds.Append( aProfileId );
487     }
488   }
489
490   if ( !aNewProfile.IsNull() )
491     aNewProfile->Remove();
492
493   // Close the file
494   aFile.Close();
495
496   for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
497   {
498     Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
499
500     QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
501     aProfile->SetName( aProfileName );
502
503     aProfile->SetFilePath( theFileName );
504
505     aProfile->SetBorderColor( HYDROData_Profile::DefaultBorderColor() );
506   }
507
508   return aCreatedProfiles.Length();
509 }
510
511 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
512                                         bool*                          theIsRead )
513 {
514   if ( theIsRead )
515     *theIsRead = false;
516
517   // Try to open the file
518   OSD_File aFile( theFileName );
519   if ( !aFile.IsReadable() )
520     return false;
521
522   aFile.Open( OSD_ReadOnly, OSD_Protection() );
523   if ( !aFile.IsOpen() )
524     return false;
525
526   bool aRes = ImportFromFile( aFile, theIsRead );
527
528   // Close the file
529   aFile.Close();
530
531   if ( aRes )
532   {
533     // Update file path
534     SetFilePath( theFileName );
535   }
536
537   return aRes;
538 }
539
540 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
541                                         bool*     theIsRead )
542 {
543   if ( theIsRead )
544     *theIsRead = false;
545
546   if ( !theFile.IsOpen() )
547     return false;
548
549   bool aRes = true;
550
551   bool anIsParametric = false;
552   bool anIsGeoref     = false;
553
554   HYDROData_ProfileUZ::PointsList aPointsUZ;
555   ProfilePoints                   aPointsXYZ;
556
557   double aPrevVal = -DBL_MAX;
558   while ( !theFile.IsAtEnd() )
559   {
560     Standard_Integer aNbRead = 0;
561     TCollection_AsciiString aLine;
562     theFile.ReadLine( aLine, 1024, aNbRead );
563
564     aLine.LeftAdjust(); aLine.RightAdjust();
565     if ( aLine.IsEmpty() )
566     {
567       if ( !anIsParametric && !anIsGeoref )
568         continue; // Definition is not started yet
569
570       break; // Next profile started
571     }
572
573     // Set flag of read status to true
574     if ( theIsRead )
575       *theIsRead = true;
576
577     TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
578     TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
579     TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
580
581     if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
582          aValY.IsEmpty() || !aValY.IsRealValue() )
583     {
584       aRes = false;
585       break;
586     }
587
588     if ( !anIsParametric && !anIsGeoref )
589     {
590       anIsParametric = aValZ.IsEmpty();
591       anIsGeoref = !aValZ.IsEmpty();
592     }
593
594     double aCoordX = aValX.RealValue();
595     double aCoordY = aValY.RealValue();
596
597     if ( boost::math::isnan( aCoordX ) || boost::math::isinf( aCoordX ) ||
598          boost::math::isnan( aCoordY ) || boost::math::isinf( aCoordY ) )
599       aRes = false;
600
601     if ( anIsParametric )
602     {
603       if ( aCoordX < aPrevVal )
604       {
605         // Move back readed line
606         theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
607         break;
608       }
609
610       HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
611       aPointsUZ.Append( aPoint );
612
613       aPrevVal = aCoordX;
614     }
615     else
616     {
617       if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
618       {
619         aRes = false;
620         break;
621       }
622
623       double aCoordZ = aValZ.RealValue();
624       if ( boost::math::isnan( aCoordZ ) || boost::math::isinf( aCoordZ ) )
625         aRes = false;
626
627       ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
628       aPointsXYZ.Append( aPoint );
629     }
630   }
631   
632   aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() || 
633                    anIsGeoref && !aPointsXYZ.IsEmpty() );
634   if ( aRes )
635   {
636     // Update profile points
637     if ( anIsParametric )
638     {
639       SetParametricPoints( aPointsUZ );
640     }
641     else if ( anIsGeoref )
642     {
643       SetProfilePoints( aPointsXYZ, true );
644     }
645
646     Update();
647   }
648
649   return aRes;
650 }
651
652 void HYDROData_Profile::UpdateLocalCS( double theDx, double theDy )
653 {
654   gp_XY aDelta( theDx, theDy );
655   gp_XY aPnt;
656
657   GetLeftPoint( aPnt, false );
658   aPnt += aDelta;
659   SetLeftPoint( aPnt, false );
660
661   GetRightPoint( aPnt, false );
662   aPnt += aDelta;
663   SetRightPoint( aPnt, false );
664 }
665
666 HYDROData_Profile::ProfilePoint HYDROData_Profile::GetBottomPoint() const
667 {
668   ProfilePoint aBottom;
669
670   // Get parametric points
671   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
672   if ( aParametricPoints.Length() < 1 ) {
673     return aBottom;
674   }
675
676   // Calculate midvalue for U parameter
677   Standard_Real anUMidValue = aParametricPoints.First().X();
678   Standard_Real anUMinValue = anUMidValue;
679   Standard_Real anUMaxValue = anUMidValue;
680
681   for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
682     const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
683     Standard_Real anU = aParPoint.X();
684
685     if ( anU < anUMinValue ) {
686       anUMinValue = anU;
687     } else if ( anU > anUMaxValue ) {
688       anUMaxValue = anU;
689     }
690   }
691
692   anUMidValue = ( anUMinValue + anUMaxValue ) / 2;
693
694   // Find index of the parametric point with minimal Z value
695   int aBottomIndex = 1;
696   HYDROData_IPolyline::Point aParBottom = aParametricPoints.First();
697
698   for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
699     const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
700     if ( aParPoint.Y() < aParBottom.Y() ) {
701       aBottomIndex = i;
702       aParBottom = aParPoint;
703     } else if ( aParPoint.Y() == aParBottom.Y() ) {
704       // Check which point is neares to the U = 0.5
705       if ( fabs( aParPoint.X() - anUMidValue ) < fabs( aParBottom.X() - anUMidValue ) ) {
706         aBottomIndex = i;
707         aParBottom = aParPoint;
708       }
709     }
710   }
711
712   // Find the corresponding profile point
713   ProfilePoints aProfilePoints = GetProfilePoints( false );
714   if ( aBottomIndex >= 1 && aBottomIndex <= aProfilePoints.Length() ) {
715     aBottom = aProfilePoints.Value( aBottomIndex );
716   }
717
718   return aBottom;
719 }
720