Salome HOME
20629aa0d3505300b9d2ea50f274bb3e98b7982d
[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, false );
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   anIsEdgeClosed &= IsCanBeClosed; // on split, resulting edges are normally not closed...
367   HYDROData_PolylineXY::SectionType aSectionType ;
368   if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
369     {
370       aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
371     }
372   else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
373     {
374       aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
375     }
376   else
377     {
378       // Other curve types are not supported
379       return false;
380     }
381
382   bool isNewSection = !isPrevious || isPrevClosed || anIsEdgeClosed || prevSectType != aSectionType;
383   DEBTRACE(isNewSection <<": " << !isPrevious << " " << isPrevClosed << " " << anIsEdgeClosed << " " << (prevSectType != aSectionType));
384
385   HYDROData_PolylineXY::PointsList aPointsList;
386   if (!isNewSection)
387     {
388       aPointsList = prevPointList;
389     }
390
391   if( aSectionType == HYDROData_PolylineXY::SECTION_POLYLINE )
392     {
393       DEBTRACE("SECTION_POLYLINE");
394       Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
395
396       gp_Pnt aFirstPoint, aLastPoint;
397       aGeomLine->D0( aFirst, aFirstPoint );
398       aGeomLine->D0( aLast, aLastPoint );
399       HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
400       HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
401      if (!isNewSection)
402         {
403           if (aSectFirstPoint == prevPointList.Last())
404             {
405               DEBTRACE("points shared: a");//aPointsList.Append( aSectFirstPoint );
406               aPointsList.Append( aSectLastPoint );
407             }
408           else if (aSectLastPoint == prevPointList.Last())
409             {
410               DEBTRACE("points shared: b");//aPointsList.Append( aSectLastPoint );
411               aPointsList.Append( aSectFirstPoint );
412             }
413           else if (aSectFirstPoint == prevPointList.First())
414             {
415               DEBTRACE("points shared: c");//aPointsList.Prepend( aSectFirstPoint );
416               aPointsList.Prepend( aSectLastPoint );
417             }
418           else if (aSectLastPoint == prevPointList.First())
419             {
420               DEBTRACE("points shared: d");//aPointsList.Prepend( aSectLastPoint );
421               aPointsList.Prepend( aSectFirstPoint );
422             }
423           else
424             {
425               DEBTRACE("no point shared")
426               isNewSection = true; // no point shared, new section
427               aPointsList.Clear();
428               aPointsList.Append( aSectFirstPoint );
429               aPointsList.Append( aSectLastPoint );
430             }
431         }
432      else
433        {
434          DEBTRACE("new section");
435          aPointsList.Append( aSectFirstPoint );
436          aPointsList.Append( aSectLastPoint );
437        }
438     }
439   else // aSectionType == HYDROData_PolylineXY::SECTION_SPLINE
440     {
441       DEBTRACE("SECTION_SPLINE");
442       isNewSection = true;
443       aPointsList.Clear();
444
445       BRepAdaptor_Curve anAdaptorCurve( theEdge );
446       GCPnts_QuasiUniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
447
448       int aNbPoints = aDiscrete.NbPoints();
449
450       // Decrease the number of imported poles because of last one
451       // pole is the closing point which are the start point
452       if ( anIsEdgeClosed ) aNbPoints--;
453
454       for ( int i = 1; i <= aNbPoints; ++i )
455       {
456         const gp_Pnt& aPoint = aDiscrete.Value( i );
457
458         HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
459         aPointsList.Append( aSectPoint );
460       }
461     }
462
463   if ( aPointsList.IsEmpty() )
464     return false;
465
466   TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
467   if (isNewSection)
468     {
469       DEBTRACE("isNewSection");
470       theSectNames.Append( aSectName );
471       theSectTypes.Append( aSectionType );
472       theSectClosures.Append( anIsEdgeClosed );
473       theSectPoints.Append( aPointsList );
474     }
475   else
476     {
477       DEBTRACE("sameSection");
478       theSectPoints.SetValue(theSectPoints.Length(), aPointsList);
479     }
480
481   return true;
482 }
483
484 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape,
485                                         bool IsInterpolationAllowed,
486                                         bool IsClosureAllowed,
487                                         double theDeviation )
488 {
489   DEBTRACE("ImportShape");
490   if ( theShape.IsNull() )
491     return false;
492
493   RemoveSections();
494
495   bool anIsCanBeImported = false;
496
497   NCollection_Sequence<TCollection_AsciiString> aSectNames;
498   NCollection_Sequence<SectionType>             aSectTypes;
499   NCollection_Sequence<bool>                    aSectClosures;
500   NCollection_Sequence<PointsList>              aSectPoints;
501
502   if ( theShape.ShapeType() == TopAbs_EDGE )
503   {
504       DEBTRACE("TopAbs_EDGE");
505     TopoDS_Edge anEdge = TopoDS::Edge( theShape );
506 //    anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes,
507 //      aSectClosures, aSectPoints, true, IsInterpolationAllowed, theDeviation );
508     anIsCanBeImported = convertEdgesToSections( anEdge, aSectNames, aSectTypes,
509       aSectClosures, aSectPoints, IsClosureAllowed, IsInterpolationAllowed, theDeviation );
510   }
511   else if ( theShape.ShapeType() == TopAbs_WIRE )
512   {
513       DEBTRACE("TopAbs_WIRE");
514     TopTools_SequenceOfShape anEdges;
515     HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
516
517     anIsCanBeImported = !anEdges.IsEmpty();
518     for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
519     {
520       TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
521       anIsCanBeImported = convertEdgesToSections( aWireEdge, aSectNames, aSectTypes,
522         aSectClosures, aSectPoints, IsClosureAllowed, IsInterpolationAllowed, theDeviation );
523     }
524   }
525
526   if ( anIsCanBeImported )
527   {
528     for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
529     {
530       const TCollection_AsciiString& aSectName = aSectNames.Value( i );
531       const SectionType& aSectType = aSectTypes.Value( i );
532       bool anIsSectionClosed = aSectClosures.Value( i );
533       const PointsList& aSectPointsList = aSectPoints( i );
534
535       AddSection( aSectName, aSectType, anIsSectionClosed );
536       SetPoints( i - 1, aSectPointsList );
537     }
538   }
539   else
540   {
541     TopoDS_Shape aShape = theShape;
542
543     if ( theShape.ShapeType() == TopAbs_EDGE )
544     {
545       // We make the wire from incoming edge because of other algorithms
546       // are waiting at least the wire from polyline
547       TopoDS_Edge anEdge = TopoDS::Edge( theShape );
548       BRepBuilderAPI_MakeWire aMakeWire( anEdge );
549       aMakeWire.Build();
550       if ( aMakeWire.IsDone() )
551         aShape = aMakeWire.Wire();
552     }
553
554     gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
555     BRepBuilderAPI_MakeFace aMakeFace( aPlane );
556     aMakeFace.Build();
557     BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
558     aProj.Add( aShape );
559     aProj.Build();
560     TopoDS_Shape aResult;
561     if( aProj.IsDone() )
562       aResult = aProj.Shape();
563
564     SetShape( aResult );
565   }
566
567   setEditable( anIsCanBeImported );
568
569   return true;
570 }
571
572 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&                  theType,
573                                              const bool&                         theIsClosed,
574                                              const NCollection_Sequence<gp_XYZ>& thePoints )
575 {
576   TopoDS_Wire aWire;
577   if( theType == SECTION_POLYLINE )
578   {
579     int aNbPoints = thePoints.Length();
580     BRepBuilderAPI_MakePolygon aMakeWire;
581     for ( int i = 1, n = aNbPoints; i <= n ; ++i )
582     {
583       gp_XYZ aPoint = thePoints.Value( i );
584       gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
585       aMakeWire.Add( aPnt );
586     }
587     if( theIsClosed && ( aNbPoints > 2 ) )
588       aMakeWire.Close();
589
590     if ( aMakeWire.IsDone() )
591       aWire = aMakeWire.Wire();
592   }
593   else //if( theType == PolylineSection::SECTION_SPLINE )
594   {
595     BRepBuilderAPI_MakeWire aMakeWire;
596
597     if ( thePoints.Size() > 1 )
598     {
599       Handle(Geom_BSplineCurve) aCurve = 
600         HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
601
602       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aCurve ).Edge();
603       aMakeWire.Add( anEdge );
604     }
605     aMakeWire.Build();
606     if ( aMakeWire.IsDone() )
607       aWire = aMakeWire;
608   }
609
610   return aWire;
611 }
612
613 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&                       thePath,
614                                              const SectionType&                  theType,
615                                              const bool&                         theIsClosed,
616                                              const NCollection_Sequence<gp_XYZ>& thePoints )
617 {
618   if ( thePoints.IsEmpty() )
619     return;
620
621   if ( theType == SECTION_POLYLINE )
622   {
623     const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
624     thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
625
626     for( int i = 2, n = thePoints.Size(); i <= n; ++i )
627     {
628       const gp_XYZ& aSectPoint = thePoints.Value( i );
629
630       thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
631     }
632
633     if( theIsClosed )
634       thePath.closeSubpath();
635   }
636   else
637   {
638     Handle(Geom_BSplineCurve) aCurve = 
639       HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
640     HYDROData_BSplineOperation::ComputePath( aCurve, thePath );
641   }
642 }
643
644 void HYDROData_PolylineXY::Update()
645 {
646   if ( !IsEditable() )
647   {
648     // If polyline is not editable we no need to update it wire
649     ClearChanged();
650     return;
651   }
652
653   HYDROData_IPolyline::Update();
654
655   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
656   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
657   NCollection_Sequence<bool>                              aSectClosures;
658   GetSections( aSectNames, aSectTypes, aSectClosures );
659
660   //BRepBuilderAPI_MakeWire aMakeWire;
661
662   TopTools_ListOfShape aSectionWiresList;
663
664   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
665   {
666     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
667     SectionType aSectionType = aSectTypes.Value( aSectionId );
668     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
669
670     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
671     if ( aSectPointsList.IsEmpty() )
672       continue;
673     
674     NCollection_Sequence<gp_XYZ> aPoints;
675     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
676     {
677       const Point& aSectPoint = aSectPointsList.Value( i );
678
679       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
680       aPoints.Append( aPoint );
681     }
682
683     TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
684     if ( !aSectionWire.IsNull() ) {
685       aSectionWiresList.Append( aSectionWire );
686       //aMakeWire.Add( aSectionWire );
687     }
688   }
689 // all input wires in the <aSectionWiresList>
690   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
691   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
692   TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
693   for(;it.More();it.Next())
694   {
695     TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
696     for(;it2.More();it2.Next()) 
697       aSeqEdges->Append(it2.Current());
698   }
699
700   BRep_Builder aBB;
701   TopoDS_Compound aCmp;
702   TopoDS_Shape aResult;
703   aBB.MakeCompound(aCmp);
704   if(aSeqEdges->Length() >1)
705   {
706     ShapeAnalysis_FreeBounds::ConnectEdgesToWires( aSeqEdges, 1E-5, Standard_True, aSeqWires );
707
708     if( aSeqWires->Length()==1 )
709       aResult = aSeqWires->Value( 1 );
710     else
711     {
712       for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
713       {
714         const TopoDS_Shape& aS1 = aSeqWires->Value(i);
715         aBB.Add(aCmp, aS1);
716       }
717       aResult = aCmp;
718     }
719   }
720   else if (aSeqEdges->Length() == 1)
721   {
722     BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
723     if (mkWire.IsDone())
724       aResult = mkWire.Wire();
725   }
726
727   SetShape( aResult );
728 }
729
730 bool HYDROData_PolylineXY::IsHas2dPrs() const
731 {
732   return true;
733 }
734
735 bool HYDROData_PolylineXY::IsEditable() const
736 {
737   return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
738 }
739
740 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
741 {
742   if ( !theIsEditable )
743     TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
744   else
745     myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
746 }
747
748 /**
749  * Returns true if polyline is closed
750  */
751 bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
752 {
753   bool anIsClosed = false;
754
755   TopoDS_Shape aShape = GetShape();
756   if ( aShape.IsNull() )
757     return anIsClosed;
758
759   TopTools_SequenceOfShape aWires;
760   HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
761
762   int aNbWires = aWires.Length();
763   if ( theIsSimpleCheck )
764   {
765     anIsClosed = aNbWires > 0;
766     for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
767     {
768       const TopoDS_Shape& aWire = aWires.Value( i );
769       anIsClosed = BRep_Tool::IsClosed( aWire );
770     }
771   }
772   else
773   {
774     anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
775   }
776
777   return anIsClosed;
778 }
779
780 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
781                                           const int thePointIndex ) const
782 {
783   double aResDistance = -1;
784   if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
785     return aResDistance;
786
787   if ( thePointIndex == 0 )
788     return 0.0;
789
790   SectionType aSectionType = GetSectionType( theSectionIndex );
791   bool anIsSectionClosed = IsClosedSection( theSectionIndex );
792   PointsList aSectPointsList = GetPoints( theSectionIndex );
793   if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size()  )
794     return aResDistance;
795
796   if ( aSectionType == SECTION_POLYLINE )
797   {
798     aResDistance = 0.0;
799   
800     Point aPrevPoint = aSectPointsList.Value( 1 );
801     for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
802     {
803       const Point& aSectPoint = aSectPointsList.Value( i );
804       aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
805       aPrevPoint = aSectPoint;
806
807       if ( thePointIndex == i - 1 )
808         break;
809     }
810   }
811   else
812   {
813     gp_XYZ aPointToTest;
814
815     int aSectNbPoints = aSectPointsList.Size();
816     NCollection_Sequence<gp_XYZ> aPoints;
817     for( int i = 1 ; i <= aSectNbPoints; ++i )
818     {
819       const Point& aSectPoint = aSectPointsList.Value( i );
820
821       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
822       aPoints.Append( aPoint );
823
824       if ( thePointIndex == i - 1 )
825         aPointToTest = aPoint;
826     }
827
828     Handle(Geom_BSplineCurve) aCurve = 
829       HYDROData_BSplineOperation::ComputeCurve( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
830
831     Quantity_Parameter aFirstParam = aCurve->FirstParameter();
832     Quantity_Parameter aSecondParam = aCurve->LastParameter();
833
834     if ( thePointIndex != aSectNbPoints - 1 )
835     {
836       GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aCurve );
837       aSecondParam = aProject.LowerDistanceParameter();
838     }
839
840     GeomAdaptor_Curve anAdap( aCurve );
841     
842     aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
843   }
844
845   return aResDistance;
846 }
847
848 //double HYDROData_PolylineXY::GetMinDistance( const int theSectionIndex,
849 //                                             const Point& thePoint ) const
850 //{
851 //  double aResDistance = -1;
852 //  if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
853 //    return aResDistance;
854 //
855 //  std::vector<TopoDS_Wire> aCurves;
856 //  HYDROData_PolylineOperator::GetWires(this, aCurves);
857 //  if (aCurves.size() != NbSections())
858 //    {
859 //      DEBTRACE("nbCurves != nbSections " << aCurves.size() << " " << NbSections());
860 //      return aResDistance;
861 //    }
862 //  TopoDS_Wire aWire = aCurves[theSectionIndex];
863 //  TopTools_SequenceOfShape anEdges;
864 //  HYDROData_ShapesTool::ExploreShapeToShapes( aWire, TopAbs_EDGE, anEdges );
865 //  for ( int i = 1, n = anEdges.Length(); i <= n; ++i )
866 //    {
867 //      DEBTRACE("i: "<< i);
868 //      TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
869 //      gp_XYZ aPointz = gp_XYZ(thePoint.X(), thePoint.Y(), 0);
870 //      Adaptor3d_Curve theCurve = BRepAdaptor_Curve(aWireEdge);
871 //      double theParameter = -1;
872 //      double d = ProjectPointToCurve(aPointz, theCurve, theParameter);
873 //      if (d<aResDistance) aResDistance = d;
874 //      DEBTRACE("d[" << i <<"]=" << d);
875 //    }
876 //  DEBTRACE("distance section "<<theSectionIndex<<" point="<<aResDistance<<" ("<<thePoint.X()<<", "<< thePoint.Y()<<")");
877 //  return aResDistance;
878 //}
879
880 int HYDROData_PolylineXY::NbSections() const
881 {
882   Handle(TDataStd_ExtStringList) aNamesList;
883   Handle(TDataStd_IntegerList)   aTypesList;
884   Handle(TDataStd_BooleanList)   aClosuresList;
885   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
886
887   return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
888 }
889
890 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
891                                        const SectionType              theSectionType,
892                                        const bool                     theIsClosed )
893 {
894   Handle(TDataStd_ExtStringList) aNamesList;
895   Handle(TDataStd_IntegerList)   aTypesList;
896   Handle(TDataStd_BooleanList)   aClosuresList;
897   getSectionsLists( aNamesList, aTypesList, aClosuresList );
898
899   TCollection_ExtendedString aSectName( theSectName );
900   if ( aSectName.Length() <= 0 )
901     aSectName = getUniqueSectionName( aNamesList );
902
903   aNamesList->Append( aSectName );
904   aTypesList->Append( theSectionType );
905   aClosuresList->Append( theIsClosed );
906
907   Changed( Geom_2d );
908 }
909
910 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
911 {
912   TCollection_AsciiString aResName;
913
914   Handle(TDataStd_ExtStringList) aNamesList;
915   Handle(TDataStd_IntegerList) aTypesList;
916   Handle(TDataStd_BooleanList) aClosuresList;
917   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
918   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
919     return aResName;
920
921   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
922   for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
923
924   if ( aNamesIter.More() )
925     aResName = aNamesIter.Value();
926
927   return aResName;
928 }
929
930 void HYDROData_PolylineXY::SetSectionName( const int                      theSectionIndex, 
931                                            const TCollection_AsciiString& theSectionName )
932 {
933   Handle(TDataStd_ExtStringList) aNamesList;
934   Handle(TDataStd_IntegerList) aTypesList;
935   Handle(TDataStd_BooleanList) aClosuresList;
936   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
937   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
938     return;
939
940   TDataStd_ListOfExtendedString anOldNamesList;
941   anOldNamesList = aNamesList->List();
942
943   // Refill the existing list
944   aNamesList->Clear();
945
946   TCollection_ExtendedString aNewSectName = theSectionName;
947
948   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
949   for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
950     aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
951 }
952
953 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
954 {
955   Handle(TDataStd_ExtStringList) aNamesList;
956   Handle(TDataStd_IntegerList) aTypesList;
957   Handle(TDataStd_BooleanList) aClosuresList;
958   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
959   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
960     return SECTION_POLYLINE;
961
962   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
963   for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
964
965   return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
966 }
967
968 void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex, 
969                                            const SectionType theSectionType )
970 {
971   Handle(TDataStd_ExtStringList) aNamesList;
972   Handle(TDataStd_IntegerList) aTypesList;
973   Handle(TDataStd_BooleanList) aClosuresList;
974   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
975   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
976     return;
977
978   TColStd_ListOfInteger anOldTypesList;
979   anOldTypesList = aTypesList->List();
980
981   // Refill the existing list
982   aTypesList->Clear();
983
984   TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
985   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
986     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
987
988   Changed( Geom_2d );
989 }
990
991 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
992 {
993   Handle(TDataStd_ExtStringList) aNamesList;
994   Handle(TDataStd_IntegerList) aTypesList;
995   Handle(TDataStd_BooleanList) aClosuresList;
996   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
997   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
998     return false;
999
1000   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
1001   for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
1002
1003   return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
1004 }
1005
1006 void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex, 
1007                                              const bool theIsClosed )
1008 {
1009   Handle(TDataStd_ExtStringList) aNamesList;
1010   Handle(TDataStd_IntegerList) aTypesList;
1011   Handle(TDataStd_BooleanList) aClosuresList;
1012   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1013   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
1014     return;
1015
1016   TDataStd_ListOfByte anOldClosuresList;
1017   anOldClosuresList = aClosuresList->List();
1018
1019   // Refill the existing list
1020   aClosuresList->Clear();
1021
1022   TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
1023   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
1024     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
1025
1026   Changed( Geom_2d );
1027 }
1028
1029 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
1030                                         NCollection_Sequence<SectionType>&             theSectTypes,
1031                                         NCollection_Sequence<bool>&                    theSectClosures ) const
1032 {
1033   theSectNames.Clear();
1034   theSectTypes.Clear();
1035   theSectClosures.Clear();
1036
1037   if( IsCustom() )
1038   {
1039     const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1040   }
1041
1042   Handle(TDataStd_ExtStringList) aNamesList;
1043   Handle(TDataStd_IntegerList) aTypesList;
1044   Handle(TDataStd_BooleanList) aClosuresList;
1045   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1046   if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
1047     return;
1048
1049   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
1050   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
1051   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
1052   for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
1053           aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
1054   {
1055     const TCollection_ExtendedString& aSectName = aNamesIter.Value();
1056     SectionType aSectType = (SectionType)aTypesIter.Value();
1057     bool aSectClosures = aClosuresIter.Value();
1058
1059     theSectNames.Append( aSectName );
1060     theSectTypes.Append( aSectType );
1061     theSectClosures.Append( aSectClosures );
1062   }
1063 }
1064
1065 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
1066 {
1067   Handle(TDataStd_ExtStringList) aNamesList;
1068   Handle(TDataStd_IntegerList)   aTypesList;
1069   Handle(TDataStd_BooleanList)   aClosuresList;
1070   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1071   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
1072     return;
1073
1074   if ( aNamesList->Extent() == 1 )
1075   {
1076     removeSectionsLists();
1077     removePointsLists();
1078   }
1079   else
1080   {
1081     TDataStd_ListOfExtendedString anOldNamesList;
1082     anOldNamesList = aNamesList->List();
1083
1084     TColStd_ListOfInteger anOldTypesList;
1085     anOldTypesList = aTypesList->List();
1086
1087     TDataStd_ListOfByte anOldClosuresList;
1088     anOldClosuresList = aClosuresList->List();
1089
1090     // Refill the existing lists
1091     aNamesList->Clear();
1092     aTypesList->Clear();
1093     aClosuresList->Clear();
1094
1095     TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
1096     TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
1097     TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
1098     for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
1099                      aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
1100     {
1101       if ( i == theSectionIndex )
1102         continue; // skip index to remove
1103
1104       aNamesList->Append( aNamesIter.Value() );
1105       aTypesList->Append( aTypesIter.Value() );
1106       aClosuresList->Append( (bool)aClosuresIter.Value() );
1107     }
1108
1109     // Remove points that belongs to removed section
1110     removePointsLists( theSectionIndex );
1111   }
1112
1113   Changed( Geom_2d );
1114 }
1115
1116 void HYDROData_PolylineXY::RemoveSections()
1117 {
1118   removeSectionsLists();
1119   removePointsLists();
1120   Changed( Geom_2d );
1121 }
1122
1123 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
1124                                      const Point& thePoint,
1125                                      const int    thePointIndex )
1126 {
1127   Handle(TDataStd_RealList) aListX, aListY;
1128   getPointsLists( theSectionIndex, aListX, aListY );
1129
1130   if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
1131   {
1132     aListX->Append( thePoint.X() );
1133     aListY->Append( thePoint.Y() );
1134   }
1135   else
1136   {
1137     TColStd_ListOfReal anOldListX;
1138     anOldListX = aListX->List();
1139
1140     TColStd_ListOfReal anOldListY;
1141     anOldListY = aListY->List();
1142
1143     // Refill the existing lists
1144     aListX->Clear();
1145     aListY->Clear();
1146
1147     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1148     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1149     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1150     {
1151       double aCoordX = anIterX.Value();
1152       double aCoordY = anIterY.Value();
1153
1154       if ( i == thePointIndex )
1155       {
1156         // Insert our new point
1157         aListX->Append( thePoint.X() );
1158         aListY->Append( thePoint.Y() );
1159       }
1160
1161       aListX->Append( aCoordX );
1162       aListY->Append( aCoordY );
1163     }
1164   }
1165
1166   Changed( Geom_2d );
1167 }
1168
1169 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
1170                                      const Point& thePoint,
1171                                      const int    thePointIndex )
1172 {
1173   Handle(TDataStd_RealList) aListX, aListY;
1174   getPointsLists( theSectionIndex, aListX, aListY );
1175
1176   if ( thePointIndex < 0 )
1177   {
1178     aListX->Prepend( thePoint.X() );
1179     aListY->Prepend( thePoint.Y() );
1180   }
1181   else if ( thePointIndex >= aListX->Extent() )
1182   {
1183     aListX->Append( thePoint.X() );
1184     aListY->Append( thePoint.Y() );
1185   }
1186   else
1187   {
1188     TColStd_ListOfReal anOldListX;
1189     anOldListX = aListX->List();
1190
1191     TColStd_ListOfReal anOldListY;
1192     anOldListY = aListY->List();
1193
1194     // Refill the existing lists
1195     aListX->Clear();
1196     aListY->Clear();
1197
1198     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1199     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1200     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1201     {
1202       double aCoordX = anIterX.Value();
1203       double aCoordY = anIterY.Value();
1204
1205       if ( i == thePointIndex )
1206       {
1207         // Insert our new point instead of old one
1208         aCoordX = thePoint.X();
1209         aCoordY = thePoint.Y();
1210       }
1211
1212       aListX->Append( aCoordX );
1213       aListY->Append( aCoordY );
1214     }
1215   }
1216
1217   Changed( Geom_2d );
1218 }
1219
1220 void HYDROData_PolylineXY::SetPoints( const int         theSectionIndex,
1221                                       const PointsList& thePoints )
1222 {
1223   Handle(TDataStd_RealList) aListX, aListY;
1224   getPointsLists( theSectionIndex, aListX, aListY );
1225
1226   aListX->Clear();
1227   aListY->Clear();
1228
1229   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1230   {
1231     const Point& aPoint = thePoints.Value( i );
1232     aListX->Append( aPoint.X() );
1233     aListY->Append( aPoint.Y() );
1234   }
1235 }
1236
1237 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1238                                         const int thePointIndex )
1239 {
1240   Handle(TDataStd_RealList) aListX, aListY;
1241   getPointsLists( theSectionIndex, aListX, aListY, false );
1242   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1243     return;
1244
1245   if ( aListX->Extent() == 1 )
1246   {
1247     removePointsLists( theSectionIndex );
1248   }
1249   else
1250   {
1251     TColStd_ListOfReal anOldListX;
1252     anOldListX = aListX->List();
1253
1254     TColStd_ListOfReal anOldListY;
1255     anOldListY = aListY->List();
1256
1257     // Refill the existing lists
1258     aListX->Clear();
1259     aListY->Clear();
1260
1261     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1262     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1263     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1264     {
1265       if ( i == thePointIndex )
1266         continue; // skip index to remove
1267
1268       aListX->Append( anIterX.Value() );
1269       aListY->Append( anIterY.Value() );
1270     }
1271   }
1272
1273   Changed( Geom_2d );
1274 }
1275
1276 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
1277 {
1278   PointsList aResList;
1279
1280   if( IsCustom() )
1281   {
1282     const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1283   }
1284  
1285   Handle(TDataStd_RealList) aListX, aListY;
1286   getPointsLists( theSectionIndex, aListX, aListY, false );
1287   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1288     return aResList;
1289
1290   TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1291   TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1292   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
1293   for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1294   {
1295     Point aPoint( anIterX.Value(), anIterY.Value() );
1296     if (IsConvertToGlobal)
1297       aDoc->Transform( aPoint, false );
1298     aResList.Append( aPoint );
1299   }
1300
1301   return aResList;
1302 }
1303
1304 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1305 {
1306   QPainterPath aPath;
1307
1308   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1309   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1310   NCollection_Sequence<bool>                              aSectClosures;
1311   GetSections( aSectNames, aSectTypes, aSectClosures );
1312
1313   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1314   {
1315     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1316     SectionType aSectionType = aSectTypes.Value( aSectionId );
1317     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1318
1319     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1320     if ( aSectPointsList.IsEmpty() )
1321       continue;
1322
1323     NCollection_Sequence<gp_XYZ> aPoints;
1324     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1325     {
1326       const Point& aSectPoint = aSectPointsList.Value( i );
1327
1328       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1329       aPoints.Append( aPoint );
1330     }
1331
1332     BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1333   }
1334
1335   return aPath;
1336 }
1337
1338 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1339 {
1340   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1341   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1342   NCollection_Sequence<bool>                              aSectClosures;
1343   GetSections( aSectNames, aSectTypes, aSectClosures );
1344
1345   gp_XY aDelta( theDx, theDy );
1346   for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1347   {
1348     PointsList aPoints = GetPoints( i );
1349     for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1350     {
1351       Point& aPoint = aPoints.ChangeValue( j );
1352       aPoint += aDelta;
1353     }
1354     SetPoints( i, aPoints );
1355   }
1356   Changed( Geom_2d );
1357 }
1358
1359 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1360 {
1361   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1362   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1363   NCollection_Sequence<bool>                              aSectClosures;
1364   GetSections( aSectNames, aSectTypes, aSectClosures );
1365
1366   for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1367     PointsList aPoints = GetPoints( i );
1368     for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1369       Point& aPoint = aPoints.ChangeValue( j );
1370
1371       QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1372
1373       aPoint.SetX( aTrsfPoint.x() );
1374       aPoint.SetY( aTrsfPoint.y() );
1375     }
1376     SetPoints( i, aPoints );
1377   }
1378
1379   Update();
1380 }
1381
1382 bool HYDROData_PolylineXY::IsCustom() const
1383 {
1384   if( myIsInCustomFlag )
1385     return false;
1386
1387   bool isNull = GetShape().IsNull();
1388   int aNbPoints = 0;
1389
1390   HYDROData_PolylineXY* aThat = const_cast<HYDROData_PolylineXY*>( this );
1391   aThat->myIsInCustomFlag = true;
1392   for( int i=0, n=NbSections(); i<n; i++ )
1393     aNbPoints += NbPoints( i );
1394   aThat->myIsInCustomFlag = false;
1395
1396   return !isNull && aNbPoints == 0;
1397 }
1398
1399 bool HYDROData_PolylineXY::GetIsInCustomFlag() const
1400 {
1401   return myIsInCustomFlag;
1402 }
1403
1404 void HYDROData_PolylineXY::SetIsInCustomFlag( bool theValue )
1405 {
1406   myIsInCustomFlag = theValue;
1407 }
1408
1409 void HYDROData_PolylineXY::Interpolate()
1410 {
1411   ImportShape( GetShape(), true );
1412 }