Salome HOME
Fix for the bug #262: Problem with scale Z in VTK.
[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     TopoDS_Shape aShape = theShape;
291
292     if ( theShape.ShapeType() == TopAbs_EDGE )
293     {
294       // We make the wire from incoming edge because of other algorithms
295       // are waiting at least the wire from polyline
296       TopoDS_Edge anEdge = TopoDS::Edge( theShape );
297       BRepBuilderAPI_MakeWire aMakeWire( anEdge );
298       aMakeWire.Build();
299       if ( aMakeWire.IsDone() )
300         aShape = aMakeWire.Wire();
301     }
302
303     setPolylineShape( aShape );
304   }
305
306   setEditable( anIsCanBeImported );
307
308   return true;
309 }
310
311 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&                  theType,
312                                              const bool&                         theIsClosed,
313                                              const NCollection_Sequence<gp_XYZ>& thePoints )
314 {
315   BRepBuilderAPI_MakeWire aMakeWire;
316   
317   if( theType == SECTION_POLYLINE )
318   {
319     for( int i = 1, n = thePoints.Size(); i <= n; ++i )
320     {
321       const gp_XYZ& aFirstPoint = thePoints.Value( i );
322
323       gp_XYZ aLastPoint;
324       if ( i == n )
325       {
326         if( theIsClosed )
327           aLastPoint = thePoints.Value( 1 );
328         else
329           break;
330       }
331       else
332       {
333         aLastPoint = thePoints.Value( i + 1 );
334       }
335
336       gp_Pnt aPnt1( aFirstPoint.X(), aFirstPoint.Y(), aFirstPoint.Z() );
337       gp_Pnt aPnt2( aLastPoint.X(), aLastPoint.Y(), aLastPoint.Z() );
338
339       if ( aPnt1.IsEqual( aPnt2, LOCAL_SELECTION_TOLERANCE ) )
340         continue;
341
342       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aPnt1, aPnt2 ).Edge();
343       aMakeWire.Add( anEdge );
344     }
345   }
346   else //if( theType == PolylineSection::SECTION_SPLINE )
347   {
348     HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
349
350     TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
351     aMakeWire.Add( anEdge );
352   }
353
354   TopoDS_Wire aWire;
355   aMakeWire.Build();
356   if ( aMakeWire.IsDone() )
357     aWire = aMakeWire;
358
359   return aWire;
360 }
361
362 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&                       thePath,
363                                              const SectionType&                  theType,
364                                              const bool&                         theIsClosed,
365                                              const NCollection_Sequence<gp_XYZ>& thePoints )
366 {
367   if ( thePoints.IsEmpty() )
368     return;
369
370   if ( theType == SECTION_POLYLINE )
371   {
372     const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
373     thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
374
375     for( int i = 2, n = thePoints.Size(); i <= n; ++i )
376     {
377       const gp_XYZ& aSectPoint = thePoints.Value( i );
378
379       thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
380     }
381
382     if( theIsClosed )
383       thePath.closeSubpath();
384   }
385   else
386   {
387     HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
388     aBSpline.ComputePath( thePath );
389   }
390 }
391
392 void HYDROData_PolylineXY::Update()
393 {
394   if ( !IsEditable() )
395   {
396     // If polyline is not editable we no need to update it wire
397     SetToUpdate( false );
398     return;
399   }
400
401   HYDROData_IPolyline::Update();
402
403   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
404   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
405   NCollection_Sequence<bool>                              aSectClosures;
406   GetSections( aSectNames, aSectTypes, aSectClosures );
407
408   BRepBuilderAPI_MakeWire aMakeWire;
409
410   TopTools_ListOfShape aSectionWiresList;
411
412   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
413   {
414     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
415     SectionType aSectionType = aSectTypes.Value( aSectionId );
416     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
417
418     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
419     if ( aSectPointsList.IsEmpty() )
420       continue;
421     
422     NCollection_Sequence<gp_XYZ> aPoints;
423     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
424     {
425       const Point& aSectPoint = aSectPointsList.Value( i );
426
427       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
428       aPoints.Append( aPoint );
429     }
430
431     TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
432     if ( !aSectionWire.IsNull() ) {
433       aSectionWiresList.Append( aSectionWire );
434       aMakeWire.Add( aSectionWire );
435     }
436   }
437 // all input wires in the <aSectionWiresList>
438   Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
439   Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
440   TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
441   for(;it.More();it.Next())
442   {
443     TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
444     for(;it2.More();it2.Next()) 
445       aSeqEdges->Append(it2.Current());
446   }
447
448   BRep_Builder aBB;
449   TopoDS_Compound aCmp;
450   TopoDS_Shape aResult;
451   aBB.MakeCompound(aCmp);
452   if(aSeqEdges->Length() >1)
453   {
454     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,1E-5,Standard_False,aSeqWires);
455
456     if( aSeqWires->Length()==1 )
457       aResult = aSeqWires->Value( 1 );
458     else
459     {
460       for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
461       {
462         const TopoDS_Shape& aS1 = aSeqWires->Value(i);
463         aBB.Add(aCmp, aS1);
464       }
465       aResult = aCmp;
466     }
467   }
468   else if (aSeqEdges->Length() == 1)
469   {
470     BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
471     if (mkWire.IsDone())
472       aResult = mkWire.Wire();
473   }
474
475   setPolylineShape( aResult );
476 }
477
478 bool HYDROData_PolylineXY::IsEditable() const
479 {
480   return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
481 }
482
483 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
484 {
485   if ( !theIsEditable )
486     TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
487   else
488     myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
489 }
490
491 /**
492  * Returns true if polyline is closed
493  */
494 bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
495 {
496   bool anIsClosed = false;
497
498   TopoDS_Shape aShape = GetShape();
499   if ( aShape.IsNull() )
500     return anIsClosed;
501
502   TopTools_SequenceOfShape aWires;
503   HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
504
505   int aNbWires = aWires.Length();
506   if ( theIsSimpleCheck )
507   {
508     anIsClosed = aNbWires > 0;
509     for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
510     {
511       const TopoDS_Shape& aWire = aWires.Value( i );
512       anIsClosed = BRep_Tool::IsClosed( aWire );
513     }
514   }
515   else
516   {
517     anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
518   }
519
520   return anIsClosed;
521 }
522
523 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
524                                           const int thePointIndex ) const
525 {
526   double aResDistance = -1;
527   if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
528     return aResDistance;
529
530   if ( thePointIndex == 0 )
531     return 0.0;
532
533   SectionType aSectionType = GetSectionType( theSectionIndex );
534   bool anIsSectionClosed = IsClosedSection( theSectionIndex );
535   PointsList aSectPointsList = GetPoints( theSectionIndex );
536   if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size()  )
537     return aResDistance;
538
539   if ( aSectionType == SECTION_POLYLINE )
540   {
541     aResDistance = 0.0;
542   
543     Point aPrevPoint = aSectPointsList.Value( 1 );
544     for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
545     {
546       const Point& aSectPoint = aSectPointsList.Value( i );
547       aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
548       aPrevPoint = aSectPoint;
549
550       if ( thePointIndex == i - 1 )
551         break;
552     }
553   }
554   else
555   {
556     gp_XYZ aPointToTest;
557
558     int aSectNbPoints = aSectPointsList.Size();
559     NCollection_Sequence<gp_XYZ> aPoints;
560     for( int i = 1 ; i <= aSectNbPoints; ++i )
561     {
562       const Point& aSectPoint = aSectPointsList.Value( i );
563
564       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
565       aPoints.Append( aPoint );
566
567       if ( thePointIndex == i - 1 )
568         aPointToTest = aPoint;
569     }
570
571     HYDROData_BSplineOperation aBSpline( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
572
573     Quantity_Parameter aFirstParam = aBSpline.Curve()->FirstParameter();
574     Quantity_Parameter aSecondParam = aBSpline.Curve()->LastParameter();
575
576     if ( thePointIndex != aSectNbPoints - 1 )
577     {
578       GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aBSpline.Curve() );
579       aSecondParam = aProject.LowerDistanceParameter();
580     }
581
582     GeomAdaptor_Curve anAdap( aBSpline.Curve() );
583     
584     aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
585   }
586
587   return aResDistance;
588 }
589
590 int HYDROData_PolylineXY::NbSections() const
591 {
592   Handle(TDataStd_ExtStringList) aNamesList;
593   Handle(TDataStd_IntegerList)   aTypesList;
594   Handle(TDataStd_BooleanList)   aClosuresList;
595   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
596
597   return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
598 }
599
600 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
601                                        const SectionType              theSectionType,
602                                        const bool                     theIsClosed )
603 {
604   Handle(TDataStd_ExtStringList) aNamesList;
605   Handle(TDataStd_IntegerList)   aTypesList;
606   Handle(TDataStd_BooleanList)   aClosuresList;
607   getSectionsLists( aNamesList, aTypesList, aClosuresList );
608
609   TCollection_ExtendedString aSectName( theSectName );
610   if ( aSectName.Length() <= 0 )
611     aSectName = getUniqueSectionName( aNamesList );
612
613   aNamesList->Append( aSectName );
614   aTypesList->Append( theSectionType );
615   aClosuresList->Append( theIsClosed );
616
617   SetToUpdate( true );
618 }
619
620 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
621 {
622   TCollection_AsciiString aResName;
623
624   Handle(TDataStd_ExtStringList) aNamesList;
625   Handle(TDataStd_IntegerList) aTypesList;
626   Handle(TDataStd_BooleanList) aClosuresList;
627   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
628   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
629     return aResName;
630
631   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
632   for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
633
634   if ( aNamesIter.More() )
635     aResName = aNamesIter.Value();
636
637   return aResName;
638 }
639
640 void HYDROData_PolylineXY::SetSectionName( const int                      theSectionIndex, 
641                                            const TCollection_AsciiString& theSectionName )
642 {
643   Handle(TDataStd_ExtStringList) aNamesList;
644   Handle(TDataStd_IntegerList) aTypesList;
645   Handle(TDataStd_BooleanList) aClosuresList;
646   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
647   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
648     return;
649
650   TDataStd_ListOfExtendedString anOldNamesList;
651   anOldNamesList = aNamesList->List();
652
653   // Refill the existing list
654   aNamesList->Clear();
655
656   TCollection_ExtendedString aNewSectName = theSectionName;
657
658   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
659   for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
660     aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
661
662   SetToUpdate( true );
663 }
664
665 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
666 {
667   Handle(TDataStd_ExtStringList) aNamesList;
668   Handle(TDataStd_IntegerList) aTypesList;
669   Handle(TDataStd_BooleanList) aClosuresList;
670   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
671   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
672     return SECTION_POLYLINE;
673
674   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
675   for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
676
677   return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
678 }
679
680 void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex, 
681                                            const SectionType theSectionType )
682 {
683   Handle(TDataStd_ExtStringList) aNamesList;
684   Handle(TDataStd_IntegerList) aTypesList;
685   Handle(TDataStd_BooleanList) aClosuresList;
686   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
687   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
688     return;
689
690   TColStd_ListOfInteger anOldTypesList;
691   anOldTypesList = aTypesList->List();
692
693   // Refill the existing list
694   aTypesList->Clear();
695
696   TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
697   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
698     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
699
700   SetToUpdate( true );
701 }
702
703 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
704 {
705   Handle(TDataStd_ExtStringList) aNamesList;
706   Handle(TDataStd_IntegerList) aTypesList;
707   Handle(TDataStd_BooleanList) aClosuresList;
708   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
709   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
710     return false;
711
712   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
713   for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
714
715   return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
716 }
717
718 void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex, 
719                                              const bool theIsClosed )
720 {
721   Handle(TDataStd_ExtStringList) aNamesList;
722   Handle(TDataStd_IntegerList) aTypesList;
723   Handle(TDataStd_BooleanList) aClosuresList;
724   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
725   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
726     return;
727
728   TDataStd_ListOfByte anOldClosuresList;
729   anOldClosuresList = aClosuresList->List();
730
731   // Refill the existing list
732   aClosuresList->Clear();
733
734   TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
735   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
736     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
737
738   SetToUpdate( true );
739 }
740
741 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
742                                         NCollection_Sequence<SectionType>&             theSectTypes,
743                                         NCollection_Sequence<bool>&                    theSectClosures ) const
744 {
745   theSectNames.Clear();
746   theSectTypes.Clear();
747   theSectClosures.Clear();
748
749   Handle(TDataStd_ExtStringList) aNamesList;
750   Handle(TDataStd_IntegerList) aTypesList;
751   Handle(TDataStd_BooleanList) aClosuresList;
752   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
753   if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
754     return;
755
756   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
757   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
758   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
759   for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
760           aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
761   {
762     const TCollection_ExtendedString& aSectName = aNamesIter.Value();
763     SectionType aSectType = (SectionType)aTypesIter.Value();
764     bool aSectClosures = aClosuresIter.Value();
765
766     theSectNames.Append( aSectName );
767     theSectTypes.Append( aSectType );
768     theSectClosures.Append( aSectClosures );
769   }
770 }
771
772 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
773 {
774   Handle(TDataStd_ExtStringList) aNamesList;
775   Handle(TDataStd_IntegerList)   aTypesList;
776   Handle(TDataStd_BooleanList)   aClosuresList;
777   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
778   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
779     return;
780
781   if ( aNamesList->Extent() == 1 )
782   {
783     removeSectionsLists();
784     removePointsLists();
785   }
786   else
787   {
788     TDataStd_ListOfExtendedString anOldNamesList;
789     anOldNamesList = aNamesList->List();
790
791     TColStd_ListOfInteger anOldTypesList;
792     anOldTypesList = aTypesList->List();
793
794     TDataStd_ListOfByte anOldClosuresList;
795     anOldClosuresList = aClosuresList->List();
796
797     // Refill the existing lists
798     aNamesList->Clear();
799     aTypesList->Clear();
800     aClosuresList->Clear();
801
802     TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
803     TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
804     TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
805     for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
806                      aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
807     {
808       if ( i == theSectionIndex )
809         continue; // skip index to remove
810
811       aNamesList->Append( aNamesIter.Value() );
812       aTypesList->Append( aTypesIter.Value() );
813       aClosuresList->Append( (bool)aClosuresIter.Value() );
814     }
815
816     // Remove points that belongs to removed section
817     removePointsLists( theSectionIndex );
818   }
819
820   SetToUpdate( true );
821 }
822
823 void HYDROData_PolylineXY::RemoveSections()
824 {
825   removeSectionsLists();
826   removePointsLists();
827   SetToUpdate( true );
828 }
829
830 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
831                                      const Point& thePoint,
832                                      const int    thePointIndex )
833 {
834   Handle(TDataStd_RealList) aListX, aListY;
835   getPointsLists( theSectionIndex, aListX, aListY );
836
837   if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
838   {
839     aListX->Append( thePoint.X() );
840     aListY->Append( thePoint.Y() );
841   }
842   else
843   {
844     TColStd_ListOfReal anOldListX;
845     anOldListX = aListX->List();
846
847     TColStd_ListOfReal anOldListY;
848     anOldListY = aListY->List();
849
850     // Refill the existing lists
851     aListX->Clear();
852     aListY->Clear();
853
854     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
855     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
856     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
857     {
858       double aCoordX = anIterX.Value();
859       double aCoordY = anIterY.Value();
860
861       if ( i == thePointIndex )
862       {
863         // Insert our new point
864         aListX->Append( thePoint.X() );
865         aListY->Append( thePoint.Y() );
866       }
867
868       aListX->Append( aCoordX );
869       aListY->Append( aCoordY );
870     }
871   }
872
873   SetToUpdate( true );
874 }
875
876 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
877                                      const Point& thePoint,
878                                      const int    thePointIndex )
879 {
880   Handle(TDataStd_RealList) aListX, aListY;
881   getPointsLists( theSectionIndex, aListX, aListY );
882
883   if ( thePointIndex < 0 )
884   {
885     aListX->Prepend( thePoint.X() );
886     aListY->Prepend( thePoint.Y() );
887   }
888   else if ( thePointIndex >= aListX->Extent() )
889   {
890     aListX->Append( thePoint.X() );
891     aListY->Append( thePoint.Y() );
892   }
893   else
894   {
895     TColStd_ListOfReal anOldListX;
896     anOldListX = aListX->List();
897
898     TColStd_ListOfReal anOldListY;
899     anOldListY = aListY->List();
900
901     // Refill the existing lists
902     aListX->Clear();
903     aListY->Clear();
904
905     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
906     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
907     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
908     {
909       double aCoordX = anIterX.Value();
910       double aCoordY = anIterY.Value();
911
912       if ( i == thePointIndex )
913       {
914         // Insert our new point instead of old one
915         aCoordX = thePoint.X();
916         aCoordY = thePoint.Y();
917       }
918
919       aListX->Append( aCoordX );
920       aListY->Append( aCoordY );
921     }
922   }
923
924   SetToUpdate( true );
925 }
926
927 void HYDROData_PolylineXY::SetPoints( const int         theSectionIndex,
928                                       const PointsList& thePoints )
929 {
930   Handle(TDataStd_RealList) aListX, aListY;
931   getPointsLists( theSectionIndex, aListX, aListY );
932
933   aListX->Clear();
934   aListY->Clear();
935
936   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
937   {
938     const Point& aPoint = thePoints.Value( i );
939     aListX->Append( aPoint.X() );
940     aListY->Append( aPoint.Y() );
941   }
942 }
943
944 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
945                                         const int thePointIndex )
946 {
947   Handle(TDataStd_RealList) aListX, aListY;
948   getPointsLists( theSectionIndex, aListX, aListY, false );
949   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
950     return;
951
952   if ( aListX->Extent() == 1 )
953   {
954     removePointsLists( theSectionIndex );
955   }
956   else
957   {
958     TColStd_ListOfReal anOldListX;
959     anOldListX = aListX->List();
960
961     TColStd_ListOfReal anOldListY;
962     anOldListY = aListY->List();
963
964     // Refill the existing lists
965     aListX->Clear();
966     aListY->Clear();
967
968     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
969     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
970     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
971     {
972       if ( i == thePointIndex )
973         continue; // skip index to remove
974
975       aListX->Append( anIterX.Value() );
976       aListY->Append( anIterY.Value() );
977     }
978   }
979
980   SetToUpdate( true );
981 }
982
983 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex ) const
984 {
985   PointsList aResList;
986
987   Handle(TDataStd_RealList) aListX, aListY;
988   getPointsLists( theSectionIndex, aListX, aListY, false );
989   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
990     return aResList;
991
992   TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
993   TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
994   for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
995   {
996     Point aPoint( anIterX.Value(), anIterY.Value() );
997     aResList.Append( aPoint );
998   }
999
1000   return aResList;
1001 }
1002
1003 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1004 {
1005   QPainterPath aPath;
1006
1007   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1008   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1009   NCollection_Sequence<bool>                              aSectClosures;
1010   GetSections( aSectNames, aSectTypes, aSectClosures );
1011
1012   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1013   {
1014     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1015     SectionType aSectionType = aSectTypes.Value( aSectionId );
1016     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1017
1018     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1019     if ( aSectPointsList.IsEmpty() )
1020       continue;
1021
1022     NCollection_Sequence<gp_XYZ> aPoints;
1023     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1024     {
1025       const Point& aSectPoint = aSectPointsList.Value( i );
1026
1027       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1028       aPoints.Append( aPoint );
1029     }
1030
1031     BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1032   }
1033
1034   return aPath;
1035 }
1036
1037