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