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