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