Salome HOME
Initial merge of branch 'BR_HYDRO_IMPS_2016' into BR_PORTING_OCCT_7
[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     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
229
230   anArray->SetValue( 0, aLPoint.X() );
231   anArray->SetValue( 1, aLPoint.Y() );
232
233   Changed( Geom_3d );
234 }
235
236 bool HYDROData_Profile::GetLeftPoint( gp_XY& thePoint, bool IsConvertToGlobal,
237                                       bool CanUseDefault ) const
238 {
239   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
240   if ( aParametricPoints.Length() < 2 )
241     return false;
242
243   thePoint = GetParametricPoints().First();
244
245   //thePoint.SetX( 0 );
246   thePoint.SetY( 0 ); //default left point of not-georeferenced profile
247   TDF_Label aLabel = myLab.FindChild( DataTag_FirstPoint, false );
248   if ( aLabel.IsNull() )
249   {
250     return CanUseDefault;
251   }
252
253   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
254   Handle(TDataStd_RealArray) anArray;
255   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
256   {
257     return CanUseDefault;
258   }
259
260   thePoint.SetX( anArray->Value( 0 ) );
261   thePoint.SetY( anArray->Value( 1 ) );
262
263   if( IsConvertToGlobal )
264     aDoc->Transform( thePoint, false );
265
266   return true;
267 }
268
269 void HYDROData_Profile::SetRightPoint( const gp_XY& theGPoint, bool IsConvertFromGlobal )
270 {
271   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint );
272
273   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
274   gp_XY aLPoint = theGPoint;
275   if( IsConvertFromGlobal )
276     aDoc->Transform( aLPoint, true );
277
278   Handle(TDataStd_RealArray) anArray;
279   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
280     anArray = TDataStd_RealArray::Set( aLabel, 0, 1 );
281
282   anArray->SetValue( 0, aLPoint.X() );
283   anArray->SetValue( 1, aLPoint.Y() );
284
285   Changed( Geom_3d );
286 }
287
288 bool HYDROData_Profile::GetRightPoint( gp_XY& thePoint, bool IsConvertToGlobal,
289                                        bool CanUseDefault ) const
290 {
291   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
292   if ( aParametricPoints.Length() < 2 )
293     return false;
294
295   thePoint = GetParametricPoints().Last();
296   thePoint.SetY( 0 );
297
298   TDF_Label aLabel = myLab.FindChild( DataTag_LastPoint, false );
299   if ( aLabel.IsNull() )
300   {
301     return CanUseDefault;
302   }
303
304   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
305   Handle(TDataStd_RealArray) anArray;
306   if ( !aLabel.FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
307   {
308     return CanUseDefault;
309   }
310
311   thePoint.SetX( anArray->Value( 0 ) );
312   thePoint.SetY( anArray->Value( 1 ) );
313
314   if( IsConvertToGlobal )
315     aDoc->Transform( thePoint, false );
316
317   return true;
318 }
319
320 void HYDROData_Profile::Invalidate()
321 {
322   TDF_Label aFirstLabel = myLab.FindChild( DataTag_FirstPoint, false );
323   if ( !aFirstLabel.IsNull() )
324     aFirstLabel.ForgetAllAttributes();
325
326   TDF_Label aLastLabel = myLab.FindChild( DataTag_LastPoint, false );
327   if ( !aLastLabel.IsNull() )
328     aLastLabel.ForgetAllAttributes();
329
330   Changed( Geom_3d );
331 }
332
333 Handle(HYDROData_ProfileUZ) HYDROData_Profile::GetProfileUZ( const bool theIsCreate ) const
334 {
335   Handle(HYDROData_ProfileUZ) aProfileUZ;
336
337   TDF_Label aLabel = myLab.FindChild( DataTag_ChildProfileUZ, theIsCreate );
338   if ( aLabel.IsNull() )
339     return aProfileUZ;
340
341   aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast( HYDROData_Iterator::Object( aLabel ) );
342   if ( aProfileUZ.IsNull() && theIsCreate )
343   {
344     aProfileUZ = Handle(HYDROData_ProfileUZ)::DownCast(
345       HYDROData_Iterator::CreateObject( aLabel, KIND_PROFILEUZ ) );
346   }
347
348   return aProfileUZ;
349 }
350
351 int HYDROData_Profile::NbPoints() const
352 {
353   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
354   return aProfileUZ.IsNull() ? 0 : aProfileUZ->NbPoints();
355 }
356
357 void HYDROData_Profile::RemovePoints()
358 {
359   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
360   if ( !aProfileUZ.IsNull() )
361   {
362     aProfileUZ->RemoveSections();
363     Changed( Geom_3d );
364   }
365 }
366
367 void HYDROData_Profile::SetParametricPoints( const HYDROData_ProfileUZ::PointsList& thePoints )
368 {
369   RemovePoints();
370
371   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
372   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
373   {
374     const HYDROData_ProfileUZ::Point& aPoint = thePoints.Value( i );
375     aProfileUZ->AddPoint( 0, aPoint );
376   }
377
378   Changed( Geom_3d );
379 }
380
381 HYDROData_ProfileUZ::PointsList HYDROData_Profile::GetParametricPoints() const
382 {
383   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ( false );
384   return aProfileUZ.IsNull() ? HYDROData_ProfileUZ::PointsList() : aProfileUZ->GetPoints();
385 }
386
387 void HYDROData_Profile::SetProfilePoints( const ProfilePoints& thePoints, bool IsConvertFromGlobal )
388 {
389   RemovePoints();
390   if ( thePoints.Length() < 2 )
391     return;
392
393   gp_XY aFirstPoint, aLastPoint;
394
395   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( Label() );
396   Handle(HYDROData_ProfileUZ) aProfileUZ = GetProfileUZ();
397   for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
398   {
399     ProfilePoint aPoint = thePoints.Value( i );
400     if( IsConvertFromGlobal )
401       aDoc->Transform( aPoint, true );
402
403     gp_XY aPointXY( aPoint.X(), aPoint.Y() );
404
405     if ( i == 1 )
406       aFirstPoint = aPointXY;
407     else if ( i == n )
408       aLastPoint = aPointXY;
409
410     double aDistance = gp_Pnt2d( aFirstPoint ).Distance( aPointXY );
411     
412     HYDROData_ProfileUZ::Point aParPoint( aDistance, aPoint.Z() );
413     aProfileUZ->AddPoint( 0, aParPoint );
414   }
415
416   SetLeftPoint( aFirstPoint, false );//already converted to local CS
417   SetRightPoint( aLastPoint, false );
418 }
419
420 HYDROData_Profile::ProfilePoints HYDROData_Profile::GetProfilePoints
421   ( bool IsConvertToGlobal, bool CanUseDefaultLeftRight ) const
422 {
423   ProfilePoints aResPoints;
424
425   gp_XY aFirstPoint, aLastPoint;
426   if ( !GetLeftPoint( aFirstPoint, IsConvertToGlobal, CanUseDefaultLeftRight ) ||
427        !GetRightPoint( aLastPoint, IsConvertToGlobal, CanUseDefaultLeftRight ) )
428     return aResPoints;
429
430   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
431   if ( aParametricPoints.Length() < 2 )
432     return aResPoints;
433
434   const HYDROData_ProfileUZ::Point& aFirstParPoint = aParametricPoints.First();
435   const HYDROData_ProfileUZ::Point& aLastParPoint = aParametricPoints.Last();
436
437   double aFullLength = aLastPoint.X() - aFirstPoint.X();
438   double aParFullLength = aLastParPoint.X() - aFirstParPoint.X();
439
440   // Add first point as is
441   aResPoints.Append( ProfilePoint( aFirstPoint.X(), aFirstPoint.Y(), aFirstParPoint.Y() ) );
442
443   // Compute all other points
444   for ( int i = 2, n = aParametricPoints.Length(); i < n ; ++i )
445   {
446     const HYDROData_ProfileUZ::Point& aParPoint = aParametricPoints.Value( i );
447
448     double aParPointDist = aParPoint.X() - aFirstParPoint.X();
449     double aRatio = aParPointDist / aParFullLength;
450
451     double aParX = aFirstPoint.X() * (1-aRatio) + aLastPoint.X() * aRatio;
452     double aParY = aFirstPoint.Y() * (1-aRatio) + aLastPoint.Y() * aRatio;
453
454     ProfilePoint aCompPoint( aParX, aParY, aParPoint.Y() );
455     aResPoints.Append( aCompPoint );
456   }
457
458   // Add last point as is
459   aResPoints.Append( ProfilePoint( aLastPoint.X(), aLastPoint.Y(), aLastParPoint.Y() ) );
460
461   return aResPoints;
462 }
463
464 void HYDROData_Profile::SetFilePath( const TCollection_AsciiString& theFilePath )
465 {
466   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), theFilePath );
467 }
468
469 TCollection_AsciiString HYDROData_Profile::GetFilePath() const
470 {
471   TCollection_AsciiString aRes;
472
473   Handle(TDataStd_AsciiString) anAsciiStr;
474   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
475     aRes = anAsciiStr->Get();
476
477   return aRes;
478 }
479
480 int HYDROData_Profile::ImportFromFile( const Handle(HYDROData_Document)& theDoc,
481                                        const TCollection_AsciiString&    theFileName,
482                                        NCollection_Sequence<int>&        theBadProfilesIds,
483                                        bool isToProject )
484 {
485   if ( theDoc.IsNull() || theFileName.IsEmpty() )
486     return 0;
487
488   OSD_File aFile( theFileName );
489   if ( !aFile.IsReadable() )
490     return 0;
491
492   aFile.Open( OSD_ReadOnly, OSD_Protection() );
493   if ( !aFile.IsOpen() )
494     return 0;
495
496   NCollection_Sequence<Handle(HYDROData_Profile)> aCreatedProfiles;
497
498   int aProfileId = 1;
499   Handle(HYDROData_Profile) aNewProfile;
500   for ( ; !aFile.IsAtEnd(); ++aProfileId )
501   {
502     if ( aNewProfile.IsNull() )
503       aNewProfile = Handle(HYDROData_Profile)::DownCast( theDoc->CreateObject( KIND_PROFILE ) );
504     
505     bool anIsRead = false;
506     if ( aNewProfile->ImportFromFile( aFile, isToProject, &anIsRead ) )
507     {
508       aCreatedProfiles.Append( aNewProfile );
509       aNewProfile.Nullify();
510     }
511     else if ( anIsRead )
512     {
513       theBadProfilesIds.Append( aProfileId );
514     }
515   }
516
517   if ( !aNewProfile.IsNull() )
518     aNewProfile->Remove();
519
520   // Close the file
521   aFile.Close();
522
523   for ( int i = 1, n = aCreatedProfiles.Length(); i <= n ; ++i )
524   {
525     Handle(HYDROData_Profile) aProfile = aCreatedProfiles.Value( i );
526
527     QString aProfileName = HYDROData_Tool::GenerateObjectName( theDoc, "Profile" );
528     aProfile->SetName( aProfileName );
529
530     aProfile->SetFilePath( theFileName );
531
532     aProfile->SetBorderColor( aProfile->DefaultBorderColor() );
533   }
534
535   return aCreatedProfiles.Length();
536 }
537
538 bool HYDROData_Profile::ImportFromFile( const TCollection_AsciiString& theFileName,
539                                         bool isToProject,
540                                         bool* isNotEmpty )
541 {
542   if( isNotEmpty )
543     *isNotEmpty = false;
544
545   // Try to open the file
546   OSD_File aFile( theFileName );
547   if ( !aFile.IsReadable() )
548     return false;
549
550   aFile.Open( OSD_ReadOnly, OSD_Protection() );
551   if ( !aFile.IsOpen() )
552     return false;
553
554   bool aRes = ImportFromFile( aFile, isToProject, isNotEmpty );
555
556   // Close the file
557   aFile.Close();
558
559   if ( aRes )
560   {
561     // Update file path
562     SetFilePath( theFileName );
563   }
564
565   return aRes;
566 }
567
568 bool HYDROData_Profile::ImportFromFile( OSD_File& theFile,
569                                         bool isToProject,
570                                         bool* isNotEmpty )
571 {
572   if( isNotEmpty )
573     *isNotEmpty = false;
574
575   if ( !theFile.IsOpen() )
576     return false;
577
578   bool aRes = true;
579
580   bool anIsParametric = false;
581   bool anIsGeoref     = false;
582
583   HYDROData_ProfileUZ::PointsList aPointsUZ;
584   ProfilePoints                   aPointsXYZ;
585
586   double aPrevVal = -DBL_MAX;
587   while ( !theFile.IsAtEnd() )
588   {
589     Standard_Integer aNbRead = 0;
590     TCollection_AsciiString aLine;
591     theFile.ReadLine( aLine, 1024, aNbRead );
592
593     aLine.LeftAdjust(); aLine.RightAdjust();
594     if ( aLine.IsEmpty() )
595     {
596       if ( !anIsParametric && !anIsGeoref )
597         continue; // Definition is not started yet
598
599       break; // Next profile started
600     }
601
602     // Set flag of read status to true
603     if( isNotEmpty )
604       *isNotEmpty = true;
605
606     TCollection_AsciiString aValX = aLine.Token( " \t", 1 );
607     TCollection_AsciiString aValY = aLine.Token( " \t", 2 );
608     TCollection_AsciiString aValZ = aLine.Token( " \t", 3 );
609
610     if ( aValX.IsEmpty() || !aValX.IsRealValue() ||
611          aValY.IsEmpty() || !aValY.IsRealValue() )
612     {
613       aRes = false;
614       break;
615     }
616
617     if ( !anIsParametric && !anIsGeoref )
618     {
619       anIsParametric = aValZ.IsEmpty();
620       anIsGeoref = !aValZ.IsEmpty();
621     }
622
623     double aCoordX = aValX.RealValue();
624     double aCoordY = aValY.RealValue();
625
626     if ( HYDROData_Tool::IsNan( aCoordX ) || HYDROData_Tool::IsInf( aCoordX ) ||
627          HYDROData_Tool::IsNan( aCoordY ) || HYDROData_Tool::IsInf( aCoordY ) )
628       aRes = false;
629
630     if ( anIsParametric )
631     {
632       if ( aCoordX < aPrevVal )
633       {
634         // Move back readed line
635         theFile.Seek( -( aNbRead + 1 ), OSD_FromHere );
636         break;
637       }
638
639       HYDROData_ProfileUZ::Point aPoint( aCoordX, aCoordY );
640       aPointsUZ.Append( aPoint );
641
642       aPrevVal = aCoordX;
643     }
644     else
645     {
646       if ( aValZ.IsEmpty() || !aValZ.IsRealValue() )
647       {
648         aRes = false;
649         break;
650       }
651
652       double aCoordZ = aValZ.RealValue();
653       if ( HYDROData_Tool::IsNan( aCoordZ ) || HYDROData_Tool::IsInf( aCoordZ ) )
654         aRes = false;
655
656       ProfilePoint aPoint( aCoordX, aCoordY, aCoordZ );
657       aPointsXYZ.Append( aPoint );
658     }
659   }
660   
661   aRes = aRes && ( anIsParametric && !aPointsUZ.IsEmpty() || 
662                    anIsGeoref && !aPointsXYZ.IsEmpty() );
663   if ( aRes )
664   {
665     // Update profile points
666     if ( anIsParametric )
667     {
668       SetParametricPoints( aPointsUZ );
669     }
670     else if ( anIsGeoref )
671     {
672       if( isToProject )
673         ProjectProfilePoints( aPointsXYZ );
674       SetProfilePoints( aPointsXYZ, true );
675     }
676
677     Update();
678   }
679
680   return aRes;
681 }
682
683 void HYDROData_Profile::UpdateLocalCS( double theDx, double theDy )
684 {
685   gp_XY aDelta( theDx, theDy );
686   gp_XY aPnt;
687
688   GetLeftPoint( aPnt, false );
689   aPnt += aDelta;
690   SetLeftPoint( aPnt, false );
691
692   GetRightPoint( aPnt, false );
693   aPnt += aDelta;
694   SetRightPoint( aPnt, false );
695 }
696
697 HYDROData_Profile::ProfilePoint HYDROData_Profile::GetBottomPoint() const
698 {
699   ProfilePoint aBottom;
700
701   // Get parametric points
702   HYDROData_ProfileUZ::PointsList aParametricPoints = GetParametricPoints();
703   if ( aParametricPoints.Length() < 1 ) {
704     return aBottom;
705   }
706
707   // Calculate midvalue for U parameter
708   Standard_Real anUMidValue = aParametricPoints.First().X();
709   Standard_Real anUMinValue = anUMidValue;
710   Standard_Real anUMaxValue = anUMidValue;
711
712   for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
713     const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
714     Standard_Real anU = aParPoint.X();
715
716     if ( anU < anUMinValue ) {
717       anUMinValue = anU;
718     } else if ( anU > anUMaxValue ) {
719       anUMaxValue = anU;
720     }
721   }
722
723   anUMidValue = ( anUMinValue + anUMaxValue ) / 2;
724
725   // Find index of the parametric point with minimal Z value
726   int aBottomIndex = 1;
727   HYDROData_IPolyline::Point aParBottom = aParametricPoints.First();
728
729   for ( int i = 2, aNbPoints = aParametricPoints.Size(); i <= aNbPoints; i++ ) {
730     const HYDROData_IPolyline::Point& aParPoint = aParametricPoints.Value( i );
731     if ( aParPoint.Y() < aParBottom.Y() ) {
732       aBottomIndex = i;
733       aParBottom = aParPoint;
734     } else if ( aParPoint.Y() == aParBottom.Y() ) {
735       // Check which point is neares to the U = 0.5
736       if ( fabs( aParPoint.X() - anUMidValue ) < fabs( aParBottom.X() - anUMidValue ) ) {
737         aBottomIndex = i;
738         aParBottom = aParPoint;
739       }
740     }
741   }
742
743   // Find the corresponding profile point
744   ProfilePoints aProfilePoints = GetProfilePoints( false );
745   if ( aBottomIndex >= 1 && aBottomIndex <= aProfilePoints.Length() ) {
746     aBottom = aProfilePoints.Value( aBottomIndex );
747   }
748
749   return aBottom;
750 }
751
752  HYDROData_Profile::ProfilePoint HYDROData_Profile::GetMiddlePoint( bool CanUseDefault ) const
753  {
754    ProfilePoint aMiddlePoint;
755   
756    gp_XY aLeftPnt, aRightPnt;
757    if ( GetLeftPoint( aLeftPnt, true, CanUseDefault ) && GetRightPoint( aRightPnt, true, CanUseDefault ) ) {
758      gp_XYZ aPnt1( aLeftPnt.X(), aLeftPnt.Y(), 0. );
759      gp_XYZ aPnt2( aRightPnt.X(), aRightPnt.Y(), 0. );
760      gp_Pnt aMiddlePoint2d( 0.5 * ( aPnt1 + aPnt2 ) ); 
761
762      gp_Lin aMidLin( aMiddlePoint2d, gp::DZ() );
763      TopoDS_Edge aMidEdge = BRepLib_MakeEdge( aMidLin );
764
765      TopoDS_Iterator anIt( TopoDS::Wire( GetShape3D() )  );
766      for ( ; anIt.More(); anIt.Next()) {
767        const TopoDS_Edge& anEdge = TopoDS::Edge( anIt.Value() );
768
769        /*
770        Standard_Real aStart, anEnd;
771        Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aStart, anEnd );
772        gp_Pnt aMiddlePointOnCurve = aCurve->Value( ( aStart + anEnd ) / 2 );
773        */
774
775        BRepExtrema_ExtCC ExtremaEE( aMidEdge, anEdge);
776        if (ExtremaEE.IsDone() && ExtremaEE.NbExt() != 0) {
777          for ( Standard_Integer i = 1; i <= ExtremaEE.NbExt(); i++ ) {
778            if ( ExtremaEE.SquareDistance(i) <= Precision::Confusion() ) {
779              aMiddlePoint = ExtremaEE.PointOnE1(i).XYZ();
780              break;
781            }
782          }
783        }
784      }
785    }
786
787    return aMiddlePoint;
788  }
789
790 void HYDROData_Profile::ProjectProfilePoints( ProfilePoints& thePoints )
791 {
792   int low = thePoints.Lower(), up = thePoints.Upper();
793   gp_Pnt aFirst = thePoints.Value( low );
794   gp_Pnt aLast = thePoints.Value( up );
795   gp_Vec d( aFirst, aLast );
796   gp_Vec n( d.Y(), -d.X(), 0 );
797
798   Handle(Geom_Plane) aPlane = new Geom_Plane( aFirst, gp_Dir( n ) );
799   for( int i=low; i<=up; i++ )
800   {
801     gp_XYZ p = thePoints.Value( i );
802     gp_Pnt pp = GeomAPI_ProjectPointOnSurf( p, aPlane );
803     thePoints.SetValue( i, pp.XYZ() );
804   }
805 }