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