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