Salome HOME
4d8179d2763810a622a1d60a43370673737accd0
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineXY.cxx
1
2 #include "HYDROData_PolylineXY.h"
3
4 #include "HYDROData_BSplineOperation.h"
5 #include "HYDROData_Document.h"
6 #include "HYDROData_ShapesTool.h"
7 #include "HYDROData_Tool.h"
8
9 #include <BRep_Builder.hxx>
10 #include <BRepBuilderAPI_MakeEdge.hxx>
11 #include <BRepBuilderAPI_MakeWire.hxx>
12
13 #include <GeomAPI_ProjectPointOnCurve.hxx>
14 #include <GeomAdaptor_Curve.hxx>
15 #include <Geom_Line.hxx>
16 #include <Geom_BSplineCurve.hxx>
17
18 #include <GCPnts_AbscissaPoint.hxx>
19
20 #include <ImageComposer_MetaTypes.h>
21
22 #include <gp_Pnt.hxx>
23 #include <gp_XY.hxx>
24
25 #include <NCollection_Map.hxx>
26
27 #include <TCollection_ExtendedString.hxx>
28
29 #include <TDataStd_ListIteratorOfListOfByte.hxx>
30 #include <TColStd_ListIteratorOfListOfInteger.hxx>
31 #include <TColStd_ListIteratorOfListOfReal.hxx>
32
33 #include <TColStd_Array1OfReal.hxx>
34
35 #include <TDataStd_BooleanList.hxx>
36 #include <TDataStd_ExtStringList.hxx>
37 #include <TDataStd_IntegerList.hxx>
38 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
39 #include <TDataStd_RealList.hxx>
40 #include <TDataStd_UAttribute.hxx>
41
42 #include <TopoDS_Iterator.hxx>
43 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 #include <TopTools_HSequenceOfShape.hxx>
45 #include <TopExp_Explorer.hxx>
46 #include <ShapeAnalysis_FreeBounds.hxx>
47 #include <TopoDS.hxx>
48 #include <QColor>
49 #include <QPainterPath>
50 #include <QVariant>
51
52 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
53
54 #define PYTHON_POLYLINEXY_ID "KIND_POLYLINEXY"
55
56 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
57
58 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
59 {
60   NCollection_Map<TCollection_AsciiString> aNamesMap;
61
62   for ( int i = 1, n = theNamesSeq.Size(); i <= n; ++i )
63   {
64     const TCollection_AsciiString& aSectName = theNamesSeq.Value( i );
65     aNamesMap.Add( aSectName );
66   }
67
68   TCollection_AsciiString aResName;
69
70   int aPrefIdx = 1;
71   do
72   {
73     aResName = TCollection_AsciiString( "Section_" ) + aPrefIdx;
74     ++aPrefIdx;
75   }
76   while ( aNamesMap.Contains( aResName ) );
77
78   return aResName;
79 }
80
81 TCollection_AsciiString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
82 {
83   NCollection_Sequence<TCollection_AsciiString> aNamesSeq;
84
85   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
86   for ( ; aNamesIter.More(); aNamesIter.Next() )
87     aNamesSeq.Append( aNamesIter.Value() );
88
89   return getUniqueSectionName( aNamesSeq );
90 }
91
92 IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
93 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
94
95 HYDROData_PolylineXY::HYDROData_PolylineXY()
96 : HYDROData_IPolyline()
97 {
98 }
99
100 HYDROData_PolylineXY::~HYDROData_PolylineXY()
101 {
102 }
103
104 QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
105 {
106   QStringList aResList;
107
108   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
109   if ( aDocument.IsNull() )
110     return aResList;
111                              
112   QString aDocName = aDocument->GetDocPyName();
113   QString aPolylineName = GetName();
114
115   aResList << QString( "%1 = %2.CreateObject( %3 );" )
116               .arg( aPolylineName ).arg( aDocName ).arg( PYTHON_POLYLINEXY_ID );
117   aResList << QString( "%1.SetName( \"%1\" );" ).arg( aPolylineName );
118
119   // Set polilyne data
120   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
121   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
122   NCollection_Sequence<bool>                              aSectClosures;
123   GetSections( aSectNames, aSectTypes, aSectClosures );
124
125   for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
126   {
127     const TCollection_AsciiString& aSectName = aSectNames.Value( i );
128     const SectionType& aSectType = aSectTypes.Value( i );
129     bool aSectClosure = aSectClosures.Value( i );
130
131     aResList << QString( "%1.AddSection( \"%2\", %3, %4 );" ).arg( aPolylineName )
132                 .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
133
134     HYDROData_PolylineXY::PointsList aSectPointsList = GetPoints( i );
135     for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
136     {
137       const Point& aSectPoint = aSectPointsList.Value( k );
138
139       aResList << QString( "%1.AddPoint( %2, QPointF( %3, %4 ) );" ).arg( aPolylineName )
140         .arg( i - 1 ).arg( aSectPoint.X() ).arg( aSectPoint.Y() );
141     }
142   }
143
144   return aResList;
145 }
146
147 QVariant HYDROData_PolylineXY::GetDataVariant()
148 {
149   QPainterPath aPath = GetPainterPath();
150
151   QVariant aVarData;
152   aVarData.setValue<QPainterPath>( aPath );
153   
154   return aVarData;
155 }
156
157 QColor HYDROData_PolylineXY::DefaultWireColor()
158 {
159   return QColor( Qt::red );
160 }
161
162 TopoDS_Shape HYDROData_PolylineXY::GetShape() const
163 {
164   return getPolylineShape();
165 }
166
167 bool convertEdgeToSection( const TopoDS_Edge&                                       theEdge,
168                            NCollection_Sequence<TCollection_AsciiString>&           theSectNames,
169                            NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
170                            NCollection_Sequence<bool>&                              theSectClosures,
171                            NCollection_Sequence<HYDROData_PolylineXY::PointsList>&  theSectPoints,
172                            const bool                                               theIsCanBeClosed )
173 {
174   Standard_Real aFirst = 0.0, aLast = 0.0;
175   Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
176   if ( anEdgeGeomCurve.IsNull() )
177     return false;
178
179   TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
180   bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
181
182   HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
183   HYDROData_PolylineXY::PointsList aPointsList;
184
185   if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
186   {
187     Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
188
189     gp_Pnt aFirstPoint, aLastPoint;
190     aGeomLine->D0( aFirst, aFirstPoint );
191     aGeomLine->D0( aLast, aLastPoint );
192
193     HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
194     aPointsList.Append( aSectFirstPoint );
195
196     HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
197     aPointsList.Append( aSectLastPoint );
198   }
199   else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) )
200   {
201     aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
202
203     Handle(Geom_BSplineCurve) aGeomSpline = 
204       Handle(Geom_BSplineCurve)::DownCast( anEdgeGeomCurve );
205
206     int aNbKnots = aGeomSpline->NbKnots();
207
208     TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
209     aGeomSpline->Knots( aSplineKnots );
210
211     // Decrease the number of imported knots because of last one 
212     // knot is the closing point which are the start point
213     if ( anIsEdgeClosed ) aNbKnots--;
214
215     for ( int i = 1; i <= aNbKnots; ++i )
216     {
217       const Standard_Real& aKnot = aSplineKnots.Value( i );
218
219       gp_Pnt aPoint;
220       aGeomSpline->D0( aKnot, aPoint );
221
222       HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
223       aPointsList.Append( aSectPoint );
224     }
225   }
226   else
227   {
228     // Other curve types are not supported
229     return false;
230   }
231
232   if ( aPointsList.IsEmpty() )
233     return false;
234
235   theSectNames.Append( aSectName );
236   theSectTypes.Append( aSectionType );
237   theSectClosures.Append( anIsEdgeClosed );
238   theSectPoints.Append( aPointsList );
239
240   return true;
241 }
242
243 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape )
244 {
245   if ( theShape.IsNull() )
246     return false;
247
248   RemoveSections();
249
250   bool anIsCanBeImported = false;
251
252   NCollection_Sequence<TCollection_AsciiString> aSectNames;
253   NCollection_Sequence<SectionType>             aSectTypes;
254   NCollection_Sequence<bool>                    aSectClosures;
255   NCollection_Sequence<PointsList>              aSectPoints;
256
257   if ( theShape.ShapeType() == TopAbs_EDGE )
258   {
259     TopoDS_Edge anEdge = TopoDS::Edge( theShape );
260     anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, true );
261   }
262   else if ( theShape.ShapeType() == TopAbs_WIRE )
263   {
264     TopTools_SequenceOfShape anEdges;
265     HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
266
267     anIsCanBeImported = !anEdges.IsEmpty();
268     for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
269     {
270       TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
271       anIsCanBeImported = convertEdgeToSection( aWireEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, false );
272     }
273   }
274
275   if ( anIsCanBeImported )
276   {
277     for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
278     {
279       const TCollection_AsciiString& aSectName = aSectNames.Value( i );
280       const SectionType& aSectType = aSectTypes.Value( i );
281       bool anIsSectionClosed = aSectClosures.Value( i );
282       const PointsList& aSectPointsList = aSectPoints( i );
283
284       AddSection( aSectName, aSectType, anIsSectionClosed );
285       SetPoints( i - 1, aSectPointsList );
286     }
287   }
288   else
289   {
290     setPolylineShape( theShape );
291   }
292
293   setEditable( anIsCanBeImported );
294
295   return true;
296 }
297
298 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&                  theType,
299                                              const bool&                         theIsClosed,
300                                              const NCollection_Sequence<gp_XYZ>& thePoints )
301 {
302   BRepBuilderAPI_MakeWire aMakeWire;
303   
304   if( theType == SECTION_POLYLINE )
305   {
306     for( int i = 1, n = thePoints.Size(); i <= n; ++i )
307     {
308       const gp_XYZ& aFirstPoint = thePoints.Value( i );
309
310       gp_XYZ aLastPoint;
311       if ( i == n )
312       {
313         if( theIsClosed )
314           aLastPoint = thePoints.Value( 1 );
315         else
316           break;
317       }
318       else
319       {
320         aLastPoint = thePoints.Value( i + 1 );
321       }
322
323       gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
324       gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), aLastPoint.Z() );
325
326       if ( aPnt1.IsEqual( aPnt2, LOCAL_SELECTION_TOLERANCE ) )
327         continue;
328
329       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
330       aMakeWire.Add( anEdge );
331     }
332   }
333   else //if( theType == PolylineSection::SECTION_SPLINE )
334   {
335     HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
336
337     TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
338     aMakeWire.Add( anEdge );
339   }
340
341   TopoDS_Wire aWire;
342   aMakeWire.Build();
343   if ( aMakeWire.IsDone() )
344     aWire = aMakeWire;
345
346   return aWire;
347 }
348
349 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&                       thePath,
350                                              const SectionType&                  theType,
351                                              const bool&                         theIsClosed,
352                                              const NCollection_Sequence<gp_XYZ>& thePoints )
353 {
354   if ( thePoints.IsEmpty() )
355     return;
356
357   if ( theType == SECTION_POLYLINE )
358   {
359     const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
360     thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
361
362     for( int i = 2, n = thePoints.Size(); i <= n; ++i )
363     {
364       const gp_XYZ& aSectPoint = thePoints.Value( i );
365
366       thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
367     }
368
369     if( theIsClosed )
370       thePath.closeSubpath();
371   }
372   else
373   {
374     HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
375     aBSpline.ComputePath( thePath );
376   }
377 }
378
379 void HYDROData_PolylineXY::Update()
380 {
381   if ( !IsEditable() )
382   {
383     // If polyline is not editable we no need to update it wire
384     SetToUpdate( false );
385     return;
386   }
387
388   HYDROData_IPolyline::Update();
389
390   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
391   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
392   NCollection_Sequence<bool>                              aSectClosures;
393   GetSections( aSectNames, aSectTypes, aSectClosures );
394
395   BRepBuilderAPI_MakeWire aMakeWire;
396
397   TopTools_ListOfShape aSectionWiresList;
398
399   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
400   {
401     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
402     SectionType aSectionType = aSectTypes.Value( aSectionId );
403     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
404
405     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
406     if ( aSectPointsList.IsEmpty() )
407       continue;
408     
409     NCollection_Sequence<gp_XYZ> aPoints;
410     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
411     {
412       const Point& aSectPoint = aSectPointsList.Value( i );
413
414       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
415       aPoints.Append( aPoint );
416     }
417
418     TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
419     if ( !aSectionWire.IsNull() ) {
420       aSectionWiresList.Append( aSectionWire );
421       aMakeWire.Add( aSectionWire );
422     }
423   }
424 // all input wires in the <aSectionWiresList>
425   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
426   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
427   TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
428   for(;it.More();it.Next())
429   {
430     TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
431     for(;it2.More();it2.Next()) 
432       aSeqEdges->Append(it2.Current());
433   }
434
435   BRep_Builder aBB;
436   TopoDS_Compound aCmp;
437   TopoDS_Shape aResult;
438   aBB.MakeCompound(aCmp);
439   if(aSeqEdges->Length() >1)
440   {
441     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,1E-5,Standard_False,aSeqWires);
442
443     if( aSeqWires->Length()==1 )
444       aResult = aSeqWires->Value( 1 );
445     else
446     {
447       for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
448       {
449         const TopoDS_Shape& aS1 = aSeqWires->Value(i);
450         aBB.Add(aCmp, aS1);
451       }
452       aResult = aCmp;
453     }
454   }
455   else if (aSeqEdges->Length() == 1)
456   {
457     BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
458     if (mkWire.IsDone())
459       aResult = mkWire.Wire();
460   }
461
462   setPolylineShape( aResult );
463 }
464
465 bool HYDROData_PolylineXY::IsEditable() const
466 {
467   return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
468 }
469
470 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
471 {
472   if ( !theIsEditable )
473     TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
474   else
475     myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
476 }
477
478 /**
479  * Returns true if polyline is closed
480  */
481 bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
482 {
483   bool anIsClosed = false;
484
485   TopoDS_Shape aShape = GetShape();
486   if ( aShape.IsNull() )
487     return anIsClosed;
488
489   if ( aShape.ShapeType() == TopAbs_EDGE )
490   {
491     //For unrecognized curves from GEOM module
492     anIsClosed = BRep_Tool::IsClosed( aShape );
493     return anIsClosed;
494   }
495
496   TopTools_SequenceOfShape aWires;
497   HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
498
499   int aNbWires = aWires.Length();
500   if ( theIsSimpleCheck )
501   {
502     anIsClosed = aNbWires > 0;
503     for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
504     {
505       const TopoDS_Shape& aWire = aWires.Value( i );
506       anIsClosed = BRep_Tool::IsClosed( aWire );
507     }
508   }
509   else
510   {
511     anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
512   }
513
514   return anIsClosed;
515 }
516
517 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
518                                           const int thePointIndex ) const
519 {
520   double aResDistance = -1;
521   if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
522     return aResDistance;
523
524   if ( thePointIndex == 0 )
525     return 0.0;
526
527   SectionType aSectionType = GetSectionType( theSectionIndex );
528   bool anIsSectionClosed = IsClosedSection( theSectionIndex );
529   PointsList aSectPointsList = GetPoints( theSectionIndex );
530   if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size()  )
531     return aResDistance;
532
533   if ( aSectionType == SECTION_POLYLINE )
534   {
535     aResDistance = 0.0;
536   
537     Point aPrevPoint = aSectPointsList.Value( 1 );
538     for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
539     {
540       const Point& aSectPoint = aSectPointsList.Value( i );
541       aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
542       aPrevPoint = aSectPoint;
543
544       if ( thePointIndex == i - 1 )
545         break;
546     }
547   }
548   else
549   {
550     gp_XYZ aPointToTest;
551
552     int aSectNbPoints = aSectPointsList.Size();
553     NCollection_Sequence<gp_XYZ> aPoints;
554     for( int i = 1 ; i <= aSectNbPoints; ++i )
555     {
556       const Point& aSectPoint = aSectPointsList.Value( i );
557
558       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
559       aPoints.Append( aPoint );
560
561       if ( thePointIndex == i - 1 )
562         aPointToTest = aPoint;
563     }
564
565     HYDROData_BSplineOperation aBSpline( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
566
567     Quantity_Parameter aFirstParam = aBSpline.Curve()->FirstParameter();
568     Quantity_Parameter aSecondParam = aBSpline.Curve()->LastParameter();
569
570     if ( thePointIndex != aSectNbPoints - 1 )
571     {
572       GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aBSpline.Curve() );
573       aSecondParam = aProject.LowerDistanceParameter();
574     }
575
576     GeomAdaptor_Curve anAdap( aBSpline.Curve() );
577     
578     aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
579   }
580
581   return aResDistance;
582 }
583
584 int HYDROData_PolylineXY::NbSections() const
585 {
586   Handle(TDataStd_ExtStringList) aNamesList;
587   Handle(TDataStd_IntegerList)   aTypesList;
588   Handle(TDataStd_BooleanList)   aClosuresList;
589   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
590
591   return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
592 }
593
594 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
595                                        const SectionType              theSectionType,
596                                        const bool                     theIsClosed )
597 {
598   Handle(TDataStd_ExtStringList) aNamesList;
599   Handle(TDataStd_IntegerList)   aTypesList;
600   Handle(TDataStd_BooleanList)   aClosuresList;
601   getSectionsLists( aNamesList, aTypesList, aClosuresList );
602
603   TCollection_ExtendedString aSectName( theSectName );
604   if ( aSectName.Length() <= 0 )
605     aSectName = getUniqueSectionName( aNamesList );
606
607   aNamesList->Append( aSectName );
608   aTypesList->Append( theSectionType );
609   aClosuresList->Append( theIsClosed );
610
611   SetToUpdate( true );
612 }
613
614 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
615 {
616   TCollection_AsciiString aResName;
617
618   Handle(TDataStd_ExtStringList) aNamesList;
619   Handle(TDataStd_IntegerList) aTypesList;
620   Handle(TDataStd_BooleanList) aClosuresList;
621   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
622   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
623     return aResName;
624
625   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
626   for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
627
628   if ( aNamesIter.More() )
629     aResName = aNamesIter.Value();
630
631   return aResName;
632 }
633
634 void HYDROData_PolylineXY::SetSectionName( const int                      theSectionIndex, 
635                                            const TCollection_AsciiString& theSectionName )
636 {
637   Handle(TDataStd_ExtStringList) aNamesList;
638   Handle(TDataStd_IntegerList) aTypesList;
639   Handle(TDataStd_BooleanList) aClosuresList;
640   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
641   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
642     return;
643
644   TDataStd_ListOfExtendedString anOldNamesList;
645   anOldNamesList = aNamesList->List();
646
647   // Refill the existing list
648   aNamesList->Clear();
649
650   TCollection_ExtendedString aNewSectName = theSectionName;
651
652   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
653   for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
654     aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
655
656   SetToUpdate( true );
657 }
658
659 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
660 {
661   Handle(TDataStd_ExtStringList) aNamesList;
662   Handle(TDataStd_IntegerList) aTypesList;
663   Handle(TDataStd_BooleanList) aClosuresList;
664   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
665   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
666     return SECTION_POLYLINE;
667
668   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
669   for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
670
671   return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
672 }
673
674 void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex, 
675                                            const SectionType theSectionType )
676 {
677   Handle(TDataStd_ExtStringList) aNamesList;
678   Handle(TDataStd_IntegerList) aTypesList;
679   Handle(TDataStd_BooleanList) aClosuresList;
680   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
681   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
682     return;
683
684   TColStd_ListOfInteger anOldTypesList;
685   anOldTypesList = aTypesList->List();
686
687   // Refill the existing list
688   aTypesList->Clear();
689
690   TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
691   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
692     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
693
694   SetToUpdate( true );
695 }
696
697 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
698 {
699   Handle(TDataStd_ExtStringList) aNamesList;
700   Handle(TDataStd_IntegerList) aTypesList;
701   Handle(TDataStd_BooleanList) aClosuresList;
702   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
703   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
704     return false;
705
706   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
707   for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
708
709   return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
710 }
711
712 void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex, 
713                                              const bool theIsClosed )
714 {
715   Handle(TDataStd_ExtStringList) aNamesList;
716   Handle(TDataStd_IntegerList) aTypesList;
717   Handle(TDataStd_BooleanList) aClosuresList;
718   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
719   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
720     return;
721
722   TDataStd_ListOfByte anOldClosuresList;
723   anOldClosuresList = aClosuresList->List();
724
725   // Refill the existing list
726   aClosuresList->Clear();
727
728   TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
729   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
730     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
731
732   SetToUpdate( true );
733 }
734
735 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
736                                         NCollection_Sequence<SectionType>&             theSectTypes,
737                                         NCollection_Sequence<bool>&                    theSectClosures ) const
738 {
739   theSectNames.Clear();
740   theSectTypes.Clear();
741   theSectClosures.Clear();
742
743   Handle(TDataStd_ExtStringList) aNamesList;
744   Handle(TDataStd_IntegerList) aTypesList;
745   Handle(TDataStd_BooleanList) aClosuresList;
746   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
747   if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
748     return;
749
750   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
751   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
752   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
753   for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
754           aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
755   {
756     const TCollection_ExtendedString& aSectName = aNamesIter.Value();
757     SectionType aSectType = (SectionType)aTypesIter.Value();
758     bool aSectClosures = aClosuresIter.Value();
759
760     theSectNames.Append( aSectName );
761     theSectTypes.Append( aSectType );
762     theSectClosures.Append( aSectClosures );
763   }
764 }
765
766 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
767 {
768   Handle(TDataStd_ExtStringList) aNamesList;
769   Handle(TDataStd_IntegerList)   aTypesList;
770   Handle(TDataStd_BooleanList)   aClosuresList;
771   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
772   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
773     return;
774
775   if ( aNamesList->Extent() == 1 )
776   {
777     removeSectionsLists();
778     removePointsLists();
779   }
780   else
781   {
782     TDataStd_ListOfExtendedString anOldNamesList;
783     anOldNamesList = aNamesList->List();
784
785     TColStd_ListOfInteger anOldTypesList;
786     anOldTypesList = aTypesList->List();
787
788     TDataStd_ListOfByte anOldClosuresList;
789     anOldClosuresList = aClosuresList->List();
790
791     // Refill the existing lists
792     aNamesList->Clear();
793     aTypesList->Clear();
794     aClosuresList->Clear();
795
796     TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
797     TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
798     TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
799     for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
800                      aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
801     {
802       if ( i == theSectionIndex )
803         continue; // skip index to remove
804
805       aNamesList->Append( aNamesIter.Value() );
806       aTypesList->Append( aTypesIter.Value() );
807       aClosuresList->Append( (bool)aClosuresIter.Value() );
808     }
809
810     // Remove points that belongs to removed section
811     removePointsLists( theSectionIndex );
812   }
813
814   SetToUpdate( true );
815 }
816
817 void HYDROData_PolylineXY::RemoveSections()
818 {
819   removeSectionsLists();
820   removePointsLists();
821   SetToUpdate( true );
822 }
823
824 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
825                                      const Point& thePoint,
826                                      const int    thePointIndex )
827 {
828   Handle(TDataStd_RealList) aListX, aListY;
829   getPointsLists( theSectionIndex, aListX, aListY );
830
831   if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
832   {
833     aListX->Append( thePoint.X() );
834     aListY->Append( thePoint.Y() );
835   }
836   else
837   {
838     TColStd_ListOfReal anOldListX;
839     anOldListX = aListX->List();
840
841     TColStd_ListOfReal anOldListY;
842     anOldListY = aListY->List();
843
844     // Refill the existing lists
845     aListX->Clear();
846     aListY->Clear();
847
848     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
849     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
850     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
851     {
852       double aCoordX = anIterX.Value();
853       double aCoordY = anIterY.Value();
854
855       if ( i == thePointIndex )
856       {
857         // Insert our new point
858         aListX->Append( thePoint.X() );
859         aListY->Append( thePoint.Y() );
860       }
861
862       aListX->Append( aCoordX );
863       aListY->Append( aCoordY );
864     }
865   }
866
867   SetToUpdate( true );
868 }
869
870 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
871                                      const Point& thePoint,
872                                      const int    thePointIndex )
873 {
874   Handle(TDataStd_RealList) aListX, aListY;
875   getPointsLists( theSectionIndex, aListX, aListY );
876
877   if ( thePointIndex < 0 )
878   {
879     aListX->Prepend( thePoint.X() );
880     aListY->Prepend( thePoint.Y() );
881   }
882   else if ( thePointIndex >= aListX->Extent() )
883   {
884     aListX->Append( thePoint.X() );
885     aListY->Append( thePoint.Y() );
886   }
887   else
888   {
889     TColStd_ListOfReal anOldListX;
890     anOldListX = aListX->List();
891
892     TColStd_ListOfReal anOldListY;
893     anOldListY = aListY->List();
894
895     // Refill the existing lists
896     aListX->Clear();
897     aListY->Clear();
898
899     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
900     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
901     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
902     {
903       double aCoordX = anIterX.Value();
904       double aCoordY = anIterY.Value();
905
906       if ( i == thePointIndex )
907       {
908         // Insert our new point instead of old one
909         aCoordX = thePoint.X();
910         aCoordY = thePoint.Y();
911       }
912
913       aListX->Append( aCoordX );
914       aListY->Append( aCoordY );
915     }
916   }
917
918   SetToUpdate( true );
919 }
920
921 void HYDROData_PolylineXY::SetPoints( const int         theSectionIndex,
922                                       const PointsList& thePoints )
923 {
924   Handle(TDataStd_RealList) aListX, aListY;
925   getPointsLists( theSectionIndex, aListX, aListY );
926
927   aListX->Clear();
928   aListY->Clear();
929
930   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
931   {
932     const Point& aPoint = thePoints.Value( i );
933     aListX->Append( aPoint.X() );
934     aListY->Append( aPoint.Y() );
935   }
936 }
937
938 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
939                                         const int thePointIndex )
940 {
941   Handle(TDataStd_RealList) aListX, aListY;
942   getPointsLists( theSectionIndex, aListX, aListY, false );
943   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
944     return;
945
946   if ( aListX->Extent() == 1 )
947   {
948     removePointsLists( theSectionIndex );
949   }
950   else
951   {
952     TColStd_ListOfReal anOldListX;
953     anOldListX = aListX->List();
954
955     TColStd_ListOfReal anOldListY;
956     anOldListY = aListY->List();
957
958     // Refill the existing lists
959     aListX->Clear();
960     aListY->Clear();
961
962     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
963     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
964     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
965     {
966       if ( i == thePointIndex )
967         continue; // skip index to remove
968
969       aListX->Append( anIterX.Value() );
970       aListY->Append( anIterY.Value() );
971     }
972   }
973
974   SetToUpdate( true );
975 }
976
977 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex ) const
978 {
979   PointsList aResList;
980
981   Handle(TDataStd_RealList) aListX, aListY;
982   getPointsLists( theSectionIndex, aListX, aListY, false );
983   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
984     return aResList;
985
986   TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
987   TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
988   for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
989   {
990     Point aPoint( anIterX.Value(), anIterY.Value() );
991     aResList.Append( aPoint );
992   }
993
994   return aResList;
995 }
996
997 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
998 {
999   QPainterPath aPath;
1000
1001   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1002   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1003   NCollection_Sequence<bool>                              aSectClosures;
1004   GetSections( aSectNames, aSectTypes, aSectClosures );
1005
1006   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1007   {
1008     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1009     SectionType aSectionType = aSectTypes.Value( aSectionId );
1010     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1011
1012     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1013     if ( aSectPointsList.IsEmpty() )
1014       continue;
1015
1016     NCollection_Sequence<gp_XYZ> aPoints;
1017     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1018     {
1019       const Point& aSectPoint = aSectPointsList.Value( i );
1020
1021       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1022       aPoints.Append( aPoint );
1023     }
1024
1025     BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1026   }
1027
1028   return aPath;
1029 }
1030
1031