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