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