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