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