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