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