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