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