Salome HOME
0acdacdf811e897b7714fcc372055939a6687142
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineXY.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_PolylineXY.h"
20
21 #include "HYDROData_BSplineOperation.h"
22 #include "HYDROData_Document.h"
23 #include "HYDROData_ShapesTool.h"
24 #include "HYDROData_Tool.h"
25 #include "HYDROData_PolylineOperator.h"
26 #include "HYDROData_TopoCurve.h"
27
28 #include <BRep_Builder.hxx>
29 #include <BRepBuilderAPI_MakeEdge.hxx>
30 #include <BRepBuilderAPI_MakeWire.hxx>
31 #include <BRepBuilderAPI_MakePolygon.hxx>
32 #include <BRepBuilderAPI_MakeFace.hxx>
33 #include <BRepOffsetAPI_NormalProjection.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35
36 #ifndef LIGHT_MODE
37 #include <GEOMBase.h>
38 #endif
39
40 #include <GeomAPI_ProjectPointOnCurve.hxx>
41 #include <GeomAdaptor_Curve.hxx>
42 #include <Geom_Line.hxx>
43 #include <Geom_BSplineCurve.hxx>
44
45 #include <GCPnts_AbscissaPoint.hxx>
46 #include <GCPnts_QuasiUniformDeflection.hxx>
47 #include <GCPnts_UniformDeflection.hxx>
48
49 #include <ImageComposer_MetaTypes.h>
50
51 #include <gp_Pnt.hxx>
52 #include <gp_XY.hxx>
53 #include <gp_Pln.hxx>
54
55 #include <NCollection_Map.hxx>
56
57 #include <TCollection_ExtendedString.hxx>
58
59 #include <TDataStd_ListIteratorOfListOfByte.hxx>
60 #include <TColStd_ListIteratorOfListOfInteger.hxx>
61 #include <TColStd_ListIteratorOfListOfReal.hxx>
62
63 #include <TColStd_Array1OfReal.hxx>
64 #include <TColgp_Array1OfPnt.hxx>
65
66 #include <TDataStd_AsciiString.hxx>
67 #include <TDataStd_BooleanList.hxx>
68 #include <TDataStd_ExtStringList.hxx>
69 #include <TDataStd_IntegerList.hxx>
70 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
71 #include <TDataStd_RealList.hxx>
72 #include <TDataStd_UAttribute.hxx>
73
74 #include <TopoDS_Iterator.hxx>
75 #include <TopTools_ListIteratorOfListOfShape.hxx>
76 #include <TopTools_HSequenceOfShape.hxx>
77 #include <TopExp_Explorer.hxx>
78 #include <ShapeAnalysis_FreeBounds.hxx>
79 #include <TopoDS.hxx>
80
81 #include <QColor>
82 #include <QPainterPath>
83 #include <QVariant>
84
85 #define _DEVDEBUG_
86 #include "HYDRO_trace.hxx"
87 #include <BRepTools.hxx>
88 #include <sstream>
89
90 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
91
92 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
93
94 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
95 {
96   NCollection_Map<TCollection_AsciiString> aNamesMap;
97
98   for ( int i = 1, n = theNamesSeq.Size(); i <= n; ++i )
99   {
100     const TCollection_AsciiString& aSectName = theNamesSeq.Value( i );
101     aNamesMap.Add( aSectName );
102   }
103
104   TCollection_AsciiString aResName;
105
106   int aPrefIdx = 1;
107   do
108   {
109     aResName = TCollection_AsciiString( "Section_" ) + aPrefIdx;
110     ++aPrefIdx;
111   }
112   while ( aNamesMap.Contains( aResName ) );
113
114   return aResName;
115 }
116
117 TCollection_AsciiString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
118 {
119   NCollection_Sequence<TCollection_AsciiString> aNamesSeq;
120
121   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
122   for ( ; aNamesIter.More(); aNamesIter.Next() )
123     aNamesSeq.Append( aNamesIter.Value() );
124
125   return getUniqueSectionName( aNamesSeq );
126 }
127
128 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
129 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
130
131 HYDROData_PolylineXY::HYDROData_PolylineXY()
132 : HYDROData_IPolyline(),
133   myIsInCustomFlag( false )
134 {
135 }
136
137 HYDROData_PolylineXY::~HYDROData_PolylineXY()
138 {
139 }
140
141 QStringList HYDROData_PolylineXY::DumpToPython( const QString&       thePyScriptPath,
142                                                 MapOfTreatedObjects& theTreatedObjects ) const
143 {
144   QStringList aResList = dumpObjectCreation( theTreatedObjects );
145   QString aPolylineName = GetObjPyName();
146
147   // Set the wire color
148   QStringList aWireColorDef;
149
150   QColor aWireColor = GetWireColor();
151   setPythonObjectColor( aWireColorDef, aWireColor, DefaultWireColor(), "SetWireColor" );
152   
153   if ( !aWireColorDef.isEmpty() )
154   {
155     aResList << aWireColorDef;
156     aResList << QString( "" );
157   }
158
159   bool anIsEditable = IsEditable();
160   if ( !anIsEditable )
161   {
162     // If polyline is not editable we try to import the shape from geom
163     TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
164     if ( !aGeomObjectEntry.IsEmpty() )
165     {
166       QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "polyline_sobj" );
167       aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" )" )
168                   .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
169
170       aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() )" )
171                   .arg( aPolylineName ).arg( aSalomeObjName );
172
173       aResList << QString( "%1.SetGeomObjectEntry( \"%2\" )" )
174                   .arg( aPolylineName ).arg( aGeomObjectEntry.ToCString() );
175     }
176   }
177   else
178   {
179     // Set polyline data
180     NCollection_Sequence<TCollection_AsciiString>           aSectNames;
181     NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
182     NCollection_Sequence<bool>                              aSectClosures;
183     GetSections( aSectNames, aSectTypes, aSectClosures );
184
185     for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
186     {
187       const TCollection_AsciiString& aSectName = aSectNames.Value( i );
188       const SectionType& aSectType = aSectTypes.Value( i );
189       bool aSectClosure = aSectClosures.Value( i );
190
191       aResList << QString( "%1.AddSection( \"%2\", %3, %4 )" ).arg( aPolylineName )
192                   .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
193
194       HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
195       for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
196       {
197         const Point& aSectPoint = aSectPointsList.Value( k );
198
199         QString anXStr = QString::number( aSectPoint.X(), 'f', 2 );
200         QString anYStr = QString::number( aSectPoint.Y(), 'f', 2 );
201         aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) )" ).arg( aPolylineName )
202           .arg( i - 1 ).arg( anXStr ).arg( anYStr );
203       }
204     }
205   }
206   aResList << QString( "" );
207   aResList << QString( "%1.Update()" ).arg( aPolylineName );
208   aResList << QString( "" );
209
210   return aResList;
211 }
212
213 QVariant HYDROData_PolylineXY::GetDataVariant()
214 {
215   QPainterPath aPath = GetPainterPath();
216
217   QVariant aVarData;
218   aVarData.setValue<QPainterPath>( aPath );
219   
220   return aVarData;
221 }
222
223 QColor HYDROData_PolylineXY::DefaultWireColor()
224 {
225   return QColor( Qt::black );
226 }
227
228 bool HYDROData_PolylineXY::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
229 {
230 #ifdef LIGHT_MODE
231   return false;
232 #else
233   if ( theIOR.IsEmpty() )
234     return false;
235
236   TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
237   if ( aShape.IsNull() )
238     return false;
239
240   return ImportShape( aShape );
241 #endif
242 }
243
244 void HYDROData_PolylineXY::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
245 {
246   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
247 }
248
249 TCollection_AsciiString HYDROData_PolylineXY::GetGeomObjectEntry() const
250 {
251   TCollection_AsciiString aRes;
252
253   TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
254   if ( !aLabel.IsNull() )
255   {
256     Handle(TDataStd_AsciiString) anAsciiStr;
257     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
258       aRes = anAsciiStr->Get();
259   }
260
261   return aRes;
262 }
263
264 bool convertEdgeToSection( const TopoDS_Edge&                                       theEdge,
265                            NCollection_Sequence<TCollection_AsciiString>&           theSectNames,
266                            NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
267                            NCollection_Sequence<bool>&                              theSectClosures,
268                            NCollection_Sequence<HYDROData_PolylineXY::PointsList>&  theSectPoints,
269                            bool                                                     IsCanBeClosed,
270                            bool                                                     IsInterpolationAllowed,
271                            double                                                   theDeflection )
272 {
273   Standard_Real aFirst = 0.0, aLast = 0.0;
274   Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
275   if ( anEdgeGeomCurve.IsNull() )
276     return false;
277
278   TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
279   bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
280
281   HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
282   HYDROData_PolylineXY::PointsList aPointsList;
283
284   if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
285   {
286     Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
287
288     gp_Pnt aFirstPoint, aLastPoint;
289     aGeomLine->D0( aFirst, aFirstPoint );
290     aGeomLine->D0( aLast, aLastPoint );
291
292     HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
293     aPointsList.Append( aSectFirstPoint );
294
295     HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
296     aPointsList.Append( aSectLastPoint );
297   }
298   else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
299   {
300     aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
301
302     BRepAdaptor_Curve anAdaptorCurve( theEdge );
303     GCPnts_QuasiUniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
304     //GCPnts_UniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
305
306     int aNbPoints = aDiscrete.NbPoints();
307
308     // Decrease the number of imported poles because of last one 
309     // pole is the closing point which are the start point
310     if ( anIsEdgeClosed ) aNbPoints--;
311
312     for ( int i = 1; i <= aNbPoints; ++i )
313     {
314       const gp_Pnt& aPoint = aDiscrete.Value( i );
315
316       HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
317       aPointsList.Append( aSectPoint );
318     }
319   }
320   else
321   {
322     // Other curve types are not supported
323     return false;
324   }
325
326   if ( aPointsList.IsEmpty() )
327     return false;
328
329   theSectNames.Append( aSectName );
330   theSectTypes.Append( aSectionType );
331   theSectClosures.Append( anIsEdgeClosed );
332   theSectPoints.Append( aPointsList );
333
334   return true;
335 }
336
337
338 bool convertEdgesToSections( const TopoDS_Edge&                                       theEdge,
339                              NCollection_Sequence<TCollection_AsciiString>&           theSectNames,
340                              NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
341                              NCollection_Sequence<bool>&                              theSectClosures,
342                              NCollection_Sequence<HYDROData_PolylineXY::PointsList>&  theSectPoints,
343                              bool                                                     IsCanBeClosed,
344                              bool                                                     IsInterpolationAllowed,
345                              double                                                   theDeflection )
346 {
347   DEBTRACE("convertEdgesToSections")
348   Standard_Real aFirst = 0.0, aLast = 0.0;
349   Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
350   if ( anEdgeGeomCurve.IsNull() )
351     return false;
352
353   bool isPrevious = (theSectTypes.Size() > 0);
354   DEBTRACE("nb sections: " << theSectTypes.Size());
355   HYDROData_PolylineXY::SectionType prevSectType = HYDROData_PolylineXY::SECTION_SPLINE;;
356   HYDROData_PolylineXY::PointsList prevPointList;
357   bool isPrevClosed = true;
358   if (isPrevious)
359     {
360        prevSectType = theSectTypes.Last();
361        prevPointList = theSectPoints.Last();
362        isPrevClosed = theSectClosures.Last();
363     }
364
365   bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
366   HYDROData_PolylineXY::SectionType aSectionType ;
367   if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
368     {
369       aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
370     }
371   else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
372     {
373       aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
374     }
375   else
376     {
377       // Other curve types are not supported
378       return false;
379     }
380
381   bool isNewSection = !isPrevious || isPrevClosed || anIsEdgeClosed || prevSectType != aSectionType;
382   DEBTRACE(isNewSection <<": " << !isPrevious << " " << isPrevClosed << " " << anIsEdgeClosed << " " << (prevSectType != aSectionType));
383
384   HYDROData_PolylineXY::PointsList aPointsList;
385   if (!isNewSection)
386     {
387       aPointsList = prevPointList;
388     }
389
390   if( aSectionType == HYDROData_PolylineXY::SECTION_POLYLINE )
391     {
392       DEBTRACE("SECTION_POLYLINE");
393       Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
394
395       gp_Pnt aFirstPoint, aLastPoint;
396       aGeomLine->D0( aFirst, aFirstPoint );
397       aGeomLine->D0( aLast, aLastPoint );
398       HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
399       HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
400      if (!isNewSection)
401         {
402           if (aSectFirstPoint == prevPointList.Last())
403             {
404               DEBTRACE("points shared: a");//aPointsList.Append( aSectFirstPoint );
405               aPointsList.Append( aSectLastPoint );
406             }
407           else if (aSectLastPoint == prevPointList.Last())
408             {
409               DEBTRACE("points shared: b");//aPointsList.Append( aSectLastPoint );
410               aPointsList.Append( aSectFirstPoint );
411             }
412           else if (aSectFirstPoint == prevPointList.First())
413             {
414               DEBTRACE("points shared: c");//aPointsList.Prepend( aSectFirstPoint );
415               aPointsList.Prepend( aSectLastPoint );
416             }
417           else if (aSectLastPoint == prevPointList.First())
418             {
419               DEBTRACE("points shared: d");//aPointsList.Prepend( aSectLastPoint );
420               aPointsList.Prepend( aSectFirstPoint );
421             }
422           else
423             {
424               DEBTRACE("no point shared")
425               isNewSection = true; // no point shared, new section
426               aPointsList.Clear();
427               aPointsList.Append( aSectFirstPoint );
428               aPointsList.Append( aSectLastPoint );
429             }
430         }
431      else
432        {
433          DEBTRACE("new section");
434          aPointsList.Append( aSectFirstPoint );
435          aPointsList.Append( aSectLastPoint );
436        }
437     }
438   else // aSectionType == HYDROData_PolylineXY::SECTION_SPLINE
439     {
440       DEBTRACE("SECTION_SPLINE");
441       isNewSection = true;
442       aPointsList.Clear();
443
444       BRepAdaptor_Curve anAdaptorCurve( theEdge );
445       GCPnts_QuasiUniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
446
447       int aNbPoints = aDiscrete.NbPoints();
448
449       // Decrease the number of imported poles because of last one
450       // pole is the closing point which are the start point
451       if ( anIsEdgeClosed ) aNbPoints--;
452
453       for ( int i = 1; i <= aNbPoints; ++i )
454       {
455         const gp_Pnt& aPoint = aDiscrete.Value( i );
456
457         HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
458         aPointsList.Append( aSectPoint );
459       }
460     }
461
462   if ( aPointsList.IsEmpty() )
463     return false;
464
465   TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
466   if (isNewSection)
467     {
468       DEBTRACE("isNewSection");
469       theSectNames.Append( aSectName );
470       theSectTypes.Append( aSectionType );
471       theSectClosures.Append( anIsEdgeClosed );
472       theSectPoints.Append( aPointsList );
473     }
474   else
475     {
476       DEBTRACE("sameSection");
477       theSectPoints.SetValue(theSectPoints.Length(), aPointsList);
478     }
479
480   return true;
481 }
482
483 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape,
484                                         bool IsInterpolationAllowed,
485                                         double theDeviation )
486 {
487   DEBTRACE("ImportShape");
488   if ( theShape.IsNull() )
489     return false;
490
491   RemoveSections();
492
493   bool anIsCanBeImported = false;
494
495   NCollection_Sequence<TCollection_AsciiString> aSectNames;
496   NCollection_Sequence<SectionType>             aSectTypes;
497   NCollection_Sequence<bool>                    aSectClosures;
498   NCollection_Sequence<PointsList>              aSectPoints;
499
500   if ( theShape.ShapeType() == TopAbs_EDGE )
501   {
502       DEBTRACE("TopAbs_EDGE");
503     TopoDS_Edge anEdge = TopoDS::Edge( theShape );
504     anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes,
505       aSectClosures, aSectPoints, true, IsInterpolationAllowed, theDeviation );
506   }
507   else if ( theShape.ShapeType() == TopAbs_WIRE )
508   {
509       DEBTRACE("TopAbs_WIRE");
510     TopTools_SequenceOfShape anEdges;
511     HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
512
513     anIsCanBeImported = !anEdges.IsEmpty();
514     for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
515     {
516       TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
517       anIsCanBeImported = convertEdgesToSections( aWireEdge, aSectNames, aSectTypes,
518         aSectClosures, aSectPoints, false, IsInterpolationAllowed, theDeviation );
519     }
520   }
521
522   if ( anIsCanBeImported )
523   {
524     for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
525     {
526       const TCollection_AsciiString& aSectName = aSectNames.Value( i );
527       const SectionType& aSectType = aSectTypes.Value( i );
528       bool anIsSectionClosed = aSectClosures.Value( i );
529       const PointsList& aSectPointsList = aSectPoints( i );
530
531       AddSection( aSectName, aSectType, anIsSectionClosed );
532       SetPoints( i - 1, aSectPointsList );
533     }
534   }
535   else
536   {
537     TopoDS_Shape aShape = theShape;
538
539     if ( theShape.ShapeType() == TopAbs_EDGE )
540     {
541       // We make the wire from incoming edge because of other algorithms
542       // are waiting at least the wire from polyline
543       TopoDS_Edge anEdge = TopoDS::Edge( theShape );
544       BRepBuilderAPI_MakeWire aMakeWire( anEdge );
545       aMakeWire.Build();
546       if ( aMakeWire.IsDone() )
547         aShape = aMakeWire.Wire();
548     }
549
550     gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
551     BRepBuilderAPI_MakeFace aMakeFace( aPlane );
552     aMakeFace.Build();
553     BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
554     aProj.Add( aShape );
555     aProj.Build();
556     TopoDS_Shape aResult;
557     if( aProj.IsDone() )
558       aResult = aProj.Shape();
559
560     SetShape( aResult );
561   }
562
563   setEditable( anIsCanBeImported );
564
565   return true;
566 }
567
568 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&                  theType,
569                                              const bool&                         theIsClosed,
570                                              const NCollection_Sequence<gp_XYZ>& thePoints )
571 {
572   TopoDS_Wire aWire;
573   if( theType == SECTION_POLYLINE )
574   {
575     int aNbPoints = thePoints.Length();
576     BRepBuilderAPI_MakePolygon aMakeWire;
577     for ( int i = 1, n = aNbPoints; i <= n ; ++i )
578     {
579       gp_XYZ aPoint = thePoints.Value( i );
580       gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
581       aMakeWire.Add( aPnt );
582     }
583     if( theIsClosed && ( aNbPoints > 2 ) )
584       aMakeWire.Close();
585
586     if ( aMakeWire.IsDone() )
587       aWire = aMakeWire.Wire();
588   }
589   else //if( theType == PolylineSection::SECTION_SPLINE )
590   {
591     BRepBuilderAPI_MakeWire aMakeWire;
592
593     if ( thePoints.Size() > 1 )
594     {
595       Handle(Geom_BSplineCurve) aCurve = 
596         HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
597
598       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aCurve ).Edge();
599       aMakeWire.Add( anEdge );
600     }
601     aMakeWire.Build();
602     if ( aMakeWire.IsDone() )
603       aWire = aMakeWire;
604   }
605
606   return aWire;
607 }
608
609 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&                       thePath,
610                                              const SectionType&                  theType,
611                                              const bool&                         theIsClosed,
612                                              const NCollection_Sequence<gp_XYZ>& thePoints )
613 {
614   if ( thePoints.IsEmpty() )
615     return;
616
617   if ( theType == SECTION_POLYLINE )
618   {
619     const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
620     thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
621
622     for( int i = 2, n = thePoints.Size(); i <= n; ++i )
623     {
624       const gp_XYZ& aSectPoint = thePoints.Value( i );
625
626       thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
627     }
628
629     if( theIsClosed )
630       thePath.closeSubpath();
631   }
632   else
633   {
634     Handle(Geom_BSplineCurve) aCurve = 
635       HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
636     HYDROData_BSplineOperation::ComputePath( aCurve, thePath );
637   }
638 }
639
640 void HYDROData_PolylineXY::Update()
641 {
642   if ( !IsEditable() )
643   {
644     // If polyline is not editable we no need to update it wire
645     ClearChanged();
646     return;
647   }
648
649   HYDROData_IPolyline::Update();
650
651   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
652   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
653   NCollection_Sequence<bool>                              aSectClosures;
654   GetSections( aSectNames, aSectTypes, aSectClosures );
655
656   //BRepBuilderAPI_MakeWire aMakeWire;
657
658   TopTools_ListOfShape aSectionWiresList;
659
660   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
661   {
662     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
663     SectionType aSectionType = aSectTypes.Value( aSectionId );
664     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
665
666     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
667     if ( aSectPointsList.IsEmpty() )
668       continue;
669     
670     NCollection_Sequence<gp_XYZ> aPoints;
671     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
672     {
673       const Point& aSectPoint = aSectPointsList.Value( i );
674
675       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
676       aPoints.Append( aPoint );
677     }
678
679     TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
680     if ( !aSectionWire.IsNull() ) {
681       aSectionWiresList.Append( aSectionWire );
682       //aMakeWire.Add( aSectionWire );
683     }
684   }
685 // all input wires in the <aSectionWiresList>
686   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
687   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
688   TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
689   for(;it.More();it.Next())
690   {
691     TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
692     for(;it2.More();it2.Next()) 
693       aSeqEdges->Append(it2.Current());
694   }
695
696   BRep_Builder aBB;
697   TopoDS_Compound aCmp;
698   TopoDS_Shape aResult;
699   aBB.MakeCompound(aCmp);
700   if(aSeqEdges->Length() >1)
701   {
702     ShapeAnalysis_FreeBounds::ConnectEdgesToWires( aSeqEdges, 1E-5, Standard_True, aSeqWires );
703
704     if( aSeqWires->Length()==1 )
705       aResult = aSeqWires->Value( 1 );
706     else
707     {
708       for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
709       {
710         const TopoDS_Shape& aS1 = aSeqWires->Value(i);
711         aBB.Add(aCmp, aS1);
712       }
713       aResult = aCmp;
714     }
715   }
716   else if (aSeqEdges->Length() == 1)
717   {
718     BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
719     if (mkWire.IsDone())
720       aResult = mkWire.Wire();
721   }
722
723   SetShape( aResult );
724 }
725
726 bool HYDROData_PolylineXY::IsHas2dPrs() const
727 {
728   return true;
729 }
730
731 bool HYDROData_PolylineXY::IsEditable() const
732 {
733   return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
734 }
735
736 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
737 {
738   if ( !theIsEditable )
739     TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
740   else
741     myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
742 }
743
744 /**
745  * Returns true if polyline is closed
746  */
747 bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
748 {
749   bool anIsClosed = false;
750
751   TopoDS_Shape aShape = GetShape();
752   if ( aShape.IsNull() )
753     return anIsClosed;
754
755   TopTools_SequenceOfShape aWires;
756   HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
757
758   int aNbWires = aWires.Length();
759   if ( theIsSimpleCheck )
760   {
761     anIsClosed = aNbWires > 0;
762     for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
763     {
764       const TopoDS_Shape& aWire = aWires.Value( i );
765       anIsClosed = BRep_Tool::IsClosed( aWire );
766     }
767   }
768   else
769   {
770     anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
771   }
772
773   return anIsClosed;
774 }
775
776 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
777                                           const int thePointIndex ) const
778 {
779   double aResDistance = -1;
780   if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
781     return aResDistance;
782
783   if ( thePointIndex == 0 )
784     return 0.0;
785
786   SectionType aSectionType = GetSectionType( theSectionIndex );
787   bool anIsSectionClosed = IsClosedSection( theSectionIndex );
788   PointsList aSectPointsList = GetPoints( theSectionIndex );
789   if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size()  )
790     return aResDistance;
791
792   if ( aSectionType == SECTION_POLYLINE )
793   {
794     aResDistance = 0.0;
795   
796     Point aPrevPoint = aSectPointsList.Value( 1 );
797     for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
798     {
799       const Point& aSectPoint = aSectPointsList.Value( i );
800       aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
801       aPrevPoint = aSectPoint;
802
803       if ( thePointIndex == i - 1 )
804         break;
805     }
806   }
807   else
808   {
809     gp_XYZ aPointToTest;
810
811     int aSectNbPoints = aSectPointsList.Size();
812     NCollection_Sequence<gp_XYZ> aPoints;
813     for( int i = 1 ; i <= aSectNbPoints; ++i )
814     {
815       const Point& aSectPoint = aSectPointsList.Value( i );
816
817       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
818       aPoints.Append( aPoint );
819
820       if ( thePointIndex == i - 1 )
821         aPointToTest = aPoint;
822     }
823
824     Handle(Geom_BSplineCurve) aCurve = 
825       HYDROData_BSplineOperation::ComputeCurve( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
826
827     Quantity_Parameter aFirstParam = aCurve->FirstParameter();
828     Quantity_Parameter aSecondParam = aCurve->LastParameter();
829
830     if ( thePointIndex != aSectNbPoints - 1 )
831     {
832       GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aCurve );
833       aSecondParam = aProject.LowerDistanceParameter();
834     }
835
836     GeomAdaptor_Curve anAdap( aCurve );
837     
838     aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
839   }
840
841   return aResDistance;
842 }
843
844 //double HYDROData_PolylineXY::GetMinDistance( const int theSectionIndex,
845 //                                             const Point& thePoint ) const
846 //{
847 //  double aResDistance = -1;
848 //  if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
849 //    return aResDistance;
850 //
851 //  std::vector<TopoDS_Wire> aCurves;
852 //  HYDROData_PolylineOperator::GetWires(this, aCurves);
853 //  if (aCurves.size() != NbSections())
854 //    {
855 //      DEBTRACE("nbCurves != nbSections " << aCurves.size() << " " << NbSections());
856 //      return aResDistance;
857 //    }
858 //  TopoDS_Wire aWire = aCurves[theSectionIndex];
859 //  TopTools_SequenceOfShape anEdges;
860 //  HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
861 //  for ( int i = 1, n = anEdges.Length(); i <= n; ++i )
862 //    {
863 //      DEBTRACE("i: "<< i);
864 //      TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
865 //      gp_XYZ aPointz = gp_XYZ(thePoint.X(), thePoint.Y(), 0);
866 //      Adaptor3d_Curve theCurve = BRepAdaptor_Curve(aWireEdge);
867 //      double theParameter = -1;
868 //      double d = ProjectPointToCurve(aPointz, theCurve, theParameter);
869 //      if (d<aResDistance) aResDistance = d;
870 //      DEBTRACE("d[" << i <<"]=" << d);
871 //    }
872 //  DEBTRACE("distance section "<<theSectionIndex<<" point="<<aResDistance<<" ("<<thePoint.X()<<", "<< thePoint.Y()<<")");
873 //  return aResDistance;
874 //}
875
876 int HYDROData_PolylineXY::NbSections() const
877 {
878   Handle(TDataStd_ExtStringList) aNamesList;
879   Handle(TDataStd_IntegerList)   aTypesList;
880   Handle(TDataStd_BooleanList)   aClosuresList;
881   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
882
883   return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
884 }
885
886 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
887                                        const SectionType              theSectionType,
888                                        const bool                     theIsClosed )
889 {
890   Handle(TDataStd_ExtStringList) aNamesList;
891   Handle(TDataStd_IntegerList)   aTypesList;
892   Handle(TDataStd_BooleanList)   aClosuresList;
893   getSectionsLists( aNamesList, aTypesList, aClosuresList );
894
895   TCollection_ExtendedString aSectName( theSectName );
896   if ( aSectName.Length() <= 0 )
897     aSectName = getUniqueSectionName( aNamesList );
898
899   aNamesList->Append( aSectName );
900   aTypesList->Append( theSectionType );
901   aClosuresList->Append( theIsClosed );
902
903   Changed( Geom_2d );
904 }
905
906 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
907 {
908   TCollection_AsciiString aResName;
909
910   Handle(TDataStd_ExtStringList) aNamesList;
911   Handle(TDataStd_IntegerList) aTypesList;
912   Handle(TDataStd_BooleanList) aClosuresList;
913   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
914   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
915     return aResName;
916
917   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
918   for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
919
920   if ( aNamesIter.More() )
921     aResName = aNamesIter.Value();
922
923   return aResName;
924 }
925
926 void HYDROData_PolylineXY::SetSectionName( const int                      theSectionIndex, 
927                                            const TCollection_AsciiString& theSectionName )
928 {
929   Handle(TDataStd_ExtStringList) aNamesList;
930   Handle(TDataStd_IntegerList) aTypesList;
931   Handle(TDataStd_BooleanList) aClosuresList;
932   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
933   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
934     return;
935
936   TDataStd_ListOfExtendedString anOldNamesList;
937   anOldNamesList = aNamesList->List();
938
939   // Refill the existing list
940   aNamesList->Clear();
941
942   TCollection_ExtendedString aNewSectName = theSectionName;
943
944   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
945   for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
946     aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
947 }
948
949 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
950 {
951   Handle(TDataStd_ExtStringList) aNamesList;
952   Handle(TDataStd_IntegerList) aTypesList;
953   Handle(TDataStd_BooleanList) aClosuresList;
954   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
955   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
956     return SECTION_POLYLINE;
957
958   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
959   for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
960
961   return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
962 }
963
964 void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex, 
965                                            const SectionType theSectionType )
966 {
967   Handle(TDataStd_ExtStringList) aNamesList;
968   Handle(TDataStd_IntegerList) aTypesList;
969   Handle(TDataStd_BooleanList) aClosuresList;
970   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
971   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
972     return;
973
974   TColStd_ListOfInteger anOldTypesList;
975   anOldTypesList = aTypesList->List();
976
977   // Refill the existing list
978   aTypesList->Clear();
979
980   TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
981   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
982     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
983
984   Changed( Geom_2d );
985 }
986
987 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
988 {
989   Handle(TDataStd_ExtStringList) aNamesList;
990   Handle(TDataStd_IntegerList) aTypesList;
991   Handle(TDataStd_BooleanList) aClosuresList;
992   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
993   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
994     return false;
995
996   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
997   for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
998
999   return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
1000 }
1001
1002 void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex, 
1003                                              const bool theIsClosed )
1004 {
1005   Handle(TDataStd_ExtStringList) aNamesList;
1006   Handle(TDataStd_IntegerList) aTypesList;
1007   Handle(TDataStd_BooleanList) aClosuresList;
1008   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1009   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
1010     return;
1011
1012   TDataStd_ListOfByte anOldClosuresList;
1013   anOldClosuresList = aClosuresList->List();
1014
1015   // Refill the existing list
1016   aClosuresList->Clear();
1017
1018   TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
1019   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
1020     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
1021
1022   Changed( Geom_2d );
1023 }
1024
1025 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
1026                                         NCollection_Sequence<SectionType>&             theSectTypes,
1027                                         NCollection_Sequence<bool>&                    theSectClosures ) const
1028 {
1029   theSectNames.Clear();
1030   theSectTypes.Clear();
1031   theSectClosures.Clear();
1032
1033   if( IsCustom() )
1034   {
1035     const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1036   }
1037
1038   Handle(TDataStd_ExtStringList) aNamesList;
1039   Handle(TDataStd_IntegerList) aTypesList;
1040   Handle(TDataStd_BooleanList) aClosuresList;
1041   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1042   if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
1043     return;
1044
1045   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
1046   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
1047   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
1048   for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
1049           aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
1050   {
1051     const TCollection_ExtendedString& aSectName = aNamesIter.Value();
1052     SectionType aSectType = (SectionType)aTypesIter.Value();
1053     bool aSectClosures = aClosuresIter.Value();
1054
1055     theSectNames.Append( aSectName );
1056     theSectTypes.Append( aSectType );
1057     theSectClosures.Append( aSectClosures );
1058   }
1059 }
1060
1061 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
1062 {
1063   Handle(TDataStd_ExtStringList) aNamesList;
1064   Handle(TDataStd_IntegerList)   aTypesList;
1065   Handle(TDataStd_BooleanList)   aClosuresList;
1066   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1067   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
1068     return;
1069
1070   if ( aNamesList->Extent() == 1 )
1071   {
1072     removeSectionsLists();
1073     removePointsLists();
1074   }
1075   else
1076   {
1077     TDataStd_ListOfExtendedString anOldNamesList;
1078     anOldNamesList = aNamesList->List();
1079
1080     TColStd_ListOfInteger anOldTypesList;
1081     anOldTypesList = aTypesList->List();
1082
1083     TDataStd_ListOfByte anOldClosuresList;
1084     anOldClosuresList = aClosuresList->List();
1085
1086     // Refill the existing lists
1087     aNamesList->Clear();
1088     aTypesList->Clear();
1089     aClosuresList->Clear();
1090
1091     TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
1092     TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
1093     TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
1094     for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
1095                      aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
1096     {
1097       if ( i == theSectionIndex )
1098         continue; // skip index to remove
1099
1100       aNamesList->Append( aNamesIter.Value() );
1101       aTypesList->Append( aTypesIter.Value() );
1102       aClosuresList->Append( (bool)aClosuresIter.Value() );
1103     }
1104
1105     // Remove points that belongs to removed section
1106     removePointsLists( theSectionIndex );
1107   }
1108
1109   Changed( Geom_2d );
1110 }
1111
1112 void HYDROData_PolylineXY::RemoveSections()
1113 {
1114   removeSectionsLists();
1115   removePointsLists();
1116   Changed( Geom_2d );
1117 }
1118
1119 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
1120                                      const Point& thePoint,
1121                                      const int    thePointIndex )
1122 {
1123   Handle(TDataStd_RealList) aListX, aListY;
1124   getPointsLists( theSectionIndex, aListX, aListY );
1125
1126   if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
1127   {
1128     aListX->Append( thePoint.X() );
1129     aListY->Append( thePoint.Y() );
1130   }
1131   else
1132   {
1133     TColStd_ListOfReal anOldListX;
1134     anOldListX = aListX->List();
1135
1136     TColStd_ListOfReal anOldListY;
1137     anOldListY = aListY->List();
1138
1139     // Refill the existing lists
1140     aListX->Clear();
1141     aListY->Clear();
1142
1143     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1144     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1145     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1146     {
1147       double aCoordX = anIterX.Value();
1148       double aCoordY = anIterY.Value();
1149
1150       if ( i == thePointIndex )
1151       {
1152         // Insert our new point
1153         aListX->Append( thePoint.X() );
1154         aListY->Append( thePoint.Y() );
1155       }
1156
1157       aListX->Append( aCoordX );
1158       aListY->Append( aCoordY );
1159     }
1160   }
1161
1162   Changed( Geom_2d );
1163 }
1164
1165 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
1166                                      const Point& thePoint,
1167                                      const int    thePointIndex )
1168 {
1169   Handle(TDataStd_RealList) aListX, aListY;
1170   getPointsLists( theSectionIndex, aListX, aListY );
1171
1172   if ( thePointIndex < 0 )
1173   {
1174     aListX->Prepend( thePoint.X() );
1175     aListY->Prepend( thePoint.Y() );
1176   }
1177   else if ( thePointIndex >= aListX->Extent() )
1178   {
1179     aListX->Append( thePoint.X() );
1180     aListY->Append( thePoint.Y() );
1181   }
1182   else
1183   {
1184     TColStd_ListOfReal anOldListX;
1185     anOldListX = aListX->List();
1186
1187     TColStd_ListOfReal anOldListY;
1188     anOldListY = aListY->List();
1189
1190     // Refill the existing lists
1191     aListX->Clear();
1192     aListY->Clear();
1193
1194     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1195     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1196     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1197     {
1198       double aCoordX = anIterX.Value();
1199       double aCoordY = anIterY.Value();
1200
1201       if ( i == thePointIndex )
1202       {
1203         // Insert our new point instead of old one
1204         aCoordX = thePoint.X();
1205         aCoordY = thePoint.Y();
1206       }
1207
1208       aListX->Append( aCoordX );
1209       aListY->Append( aCoordY );
1210     }
1211   }
1212
1213   Changed( Geom_2d );
1214 }
1215
1216 void HYDROData_PolylineXY::SetPoints( const int         theSectionIndex,
1217                                       const PointsList& thePoints )
1218 {
1219   Handle(TDataStd_RealList) aListX, aListY;
1220   getPointsLists( theSectionIndex, aListX, aListY );
1221
1222   aListX->Clear();
1223   aListY->Clear();
1224
1225   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1226   {
1227     const Point& aPoint = thePoints.Value( i );
1228     aListX->Append( aPoint.X() );
1229     aListY->Append( aPoint.Y() );
1230   }
1231 }
1232
1233 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1234                                         const int thePointIndex )
1235 {
1236   Handle(TDataStd_RealList) aListX, aListY;
1237   getPointsLists( theSectionIndex, aListX, aListY, false );
1238   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1239     return;
1240
1241   if ( aListX->Extent() == 1 )
1242   {
1243     removePointsLists( theSectionIndex );
1244   }
1245   else
1246   {
1247     TColStd_ListOfReal anOldListX;
1248     anOldListX = aListX->List();
1249
1250     TColStd_ListOfReal anOldListY;
1251     anOldListY = aListY->List();
1252
1253     // Refill the existing lists
1254     aListX->Clear();
1255     aListY->Clear();
1256
1257     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1258     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1259     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1260     {
1261       if ( i == thePointIndex )
1262         continue; // skip index to remove
1263
1264       aListX->Append( anIterX.Value() );
1265       aListY->Append( anIterY.Value() );
1266     }
1267   }
1268
1269   Changed( Geom_2d );
1270 }
1271
1272 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
1273 {
1274   PointsList aResList;
1275
1276   if( IsCustom() )
1277   {
1278     const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1279   }
1280  
1281   Handle(TDataStd_RealList) aListX, aListY;
1282   getPointsLists( theSectionIndex, aListX, aListY, false );
1283   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1284     return aResList;
1285
1286   TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1287   TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1288   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
1289   for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1290   {
1291     Point aPoint( anIterX.Value(), anIterY.Value() );
1292     if (IsConvertToGlobal)
1293       aDoc->Transform( aPoint, false );
1294     aResList.Append( aPoint );
1295   }
1296
1297   return aResList;
1298 }
1299
1300 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1301 {
1302   QPainterPath aPath;
1303
1304   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1305   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1306   NCollection_Sequence<bool>                              aSectClosures;
1307   GetSections( aSectNames, aSectTypes, aSectClosures );
1308
1309   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1310   {
1311     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1312     SectionType aSectionType = aSectTypes.Value( aSectionId );
1313     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1314
1315     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1316     if ( aSectPointsList.IsEmpty() )
1317       continue;
1318
1319     NCollection_Sequence<gp_XYZ> aPoints;
1320     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1321     {
1322       const Point& aSectPoint = aSectPointsList.Value( i );
1323
1324       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1325       aPoints.Append( aPoint );
1326     }
1327
1328     BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1329   }
1330
1331   return aPath;
1332 }
1333
1334 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1335 {
1336   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1337   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1338   NCollection_Sequence<bool>                              aSectClosures;
1339   GetSections( aSectNames, aSectTypes, aSectClosures );
1340
1341   gp_XY aDelta( theDx, theDy );
1342   for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1343   {
1344     PointsList aPoints = GetPoints( i );
1345     for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1346     {
1347       Point& aPoint = aPoints.ChangeValue( j );
1348       aPoint += aDelta;
1349     }
1350     SetPoints( i, aPoints );
1351   }
1352   Changed( Geom_2d );
1353 }
1354
1355 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1356 {
1357   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1358   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1359   NCollection_Sequence<bool>                              aSectClosures;
1360   GetSections( aSectNames, aSectTypes, aSectClosures );
1361
1362   for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1363     PointsList aPoints = GetPoints( i );
1364     for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1365       Point& aPoint = aPoints.ChangeValue( j );
1366
1367       QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1368
1369       aPoint.SetX( aTrsfPoint.x() );
1370       aPoint.SetY( aTrsfPoint.y() );
1371     }
1372     SetPoints( i, aPoints );
1373   }
1374
1375   Update();
1376 }
1377
1378 bool HYDROData_PolylineXY::IsCustom() const
1379 {
1380   if( myIsInCustomFlag )
1381     return false;
1382
1383   bool isNull = GetShape().IsNull();
1384   int aNbPoints = 0;
1385
1386   HYDROData_PolylineXY* aThat = const_cast<HYDROData_PolylineXY*>( this );
1387   aThat->myIsInCustomFlag = true;
1388   for( int i=0, n=NbSections(); i<n; i++ )
1389     aNbPoints += NbPoints( i );
1390   aThat->myIsInCustomFlag = false;
1391
1392   return !isNull && aNbPoints == 0;
1393 }
1394
1395 bool HYDROData_PolylineXY::GetIsInCustomFlag() const
1396 {
1397   return myIsInCustomFlag;
1398 }
1399
1400 void HYDROData_PolylineXY::SetIsInCustomFlag( bool theValue )
1401 {
1402   myIsInCustomFlag = theValue;
1403 }
1404
1405 void HYDROData_PolylineXY::Interpolate()
1406 {
1407   ImportShape( GetShape(), true );
1408 }