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