Salome HOME
refs #651: wrapping land cover map by Python
[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     ClearChanged();
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   Changed( Geom_2d );
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
754 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
755 {
756   Handle(TDataStd_ExtStringList) aNamesList;
757   Handle(TDataStd_IntegerList) aTypesList;
758   Handle(TDataStd_BooleanList) aClosuresList;
759   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
760   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
761     return SECTION_POLYLINE;
762
763   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
764   for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
765
766   return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
767 }
768
769 void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex, 
770                                            const SectionType theSectionType )
771 {
772   Handle(TDataStd_ExtStringList) aNamesList;
773   Handle(TDataStd_IntegerList) aTypesList;
774   Handle(TDataStd_BooleanList) aClosuresList;
775   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
776   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
777     return;
778
779   TColStd_ListOfInteger anOldTypesList;
780   anOldTypesList = aTypesList->List();
781
782   // Refill the existing list
783   aTypesList->Clear();
784
785   TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
786   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
787     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
788
789   Changed( Geom_2d );
790 }
791
792 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
793 {
794   Handle(TDataStd_ExtStringList) aNamesList;
795   Handle(TDataStd_IntegerList) aTypesList;
796   Handle(TDataStd_BooleanList) aClosuresList;
797   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
798   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
799     return false;
800
801   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
802   for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
803
804   return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
805 }
806
807 void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex, 
808                                              const bool theIsClosed )
809 {
810   Handle(TDataStd_ExtStringList) aNamesList;
811   Handle(TDataStd_IntegerList) aTypesList;
812   Handle(TDataStd_BooleanList) aClosuresList;
813   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
814   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
815     return;
816
817   TDataStd_ListOfByte anOldClosuresList;
818   anOldClosuresList = aClosuresList->List();
819
820   // Refill the existing list
821   aClosuresList->Clear();
822
823   TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
824   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
825     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
826
827   Changed( Geom_2d );
828 }
829
830 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
831                                         NCollection_Sequence<SectionType>&             theSectTypes,
832                                         NCollection_Sequence<bool>&                    theSectClosures ) const
833 {
834   theSectNames.Clear();
835   theSectTypes.Clear();
836   theSectClosures.Clear();
837
838   Handle(TDataStd_ExtStringList) aNamesList;
839   Handle(TDataStd_IntegerList) aTypesList;
840   Handle(TDataStd_BooleanList) aClosuresList;
841   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
842   if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
843     return;
844
845   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
846   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
847   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
848   for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
849           aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
850   {
851     const TCollection_ExtendedString& aSectName = aNamesIter.Value();
852     SectionType aSectType = (SectionType)aTypesIter.Value();
853     bool aSectClosures = aClosuresIter.Value();
854
855     theSectNames.Append( aSectName );
856     theSectTypes.Append( aSectType );
857     theSectClosures.Append( aSectClosures );
858   }
859 }
860
861 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
862 {
863   Handle(TDataStd_ExtStringList) aNamesList;
864   Handle(TDataStd_IntegerList)   aTypesList;
865   Handle(TDataStd_BooleanList)   aClosuresList;
866   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
867   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
868     return;
869
870   if ( aNamesList->Extent() == 1 )
871   {
872     removeSectionsLists();
873     removePointsLists();
874   }
875   else
876   {
877     TDataStd_ListOfExtendedString anOldNamesList;
878     anOldNamesList = aNamesList->List();
879
880     TColStd_ListOfInteger anOldTypesList;
881     anOldTypesList = aTypesList->List();
882
883     TDataStd_ListOfByte anOldClosuresList;
884     anOldClosuresList = aClosuresList->List();
885
886     // Refill the existing lists
887     aNamesList->Clear();
888     aTypesList->Clear();
889     aClosuresList->Clear();
890
891     TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
892     TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
893     TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
894     for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
895                      aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
896     {
897       if ( i == theSectionIndex )
898         continue; // skip index to remove
899
900       aNamesList->Append( aNamesIter.Value() );
901       aTypesList->Append( aTypesIter.Value() );
902       aClosuresList->Append( (bool)aClosuresIter.Value() );
903     }
904
905     // Remove points that belongs to removed section
906     removePointsLists( theSectionIndex );
907   }
908
909   Changed( Geom_2d );
910 }
911
912 void HYDROData_PolylineXY::RemoveSections()
913 {
914   removeSectionsLists();
915   removePointsLists();
916   Changed( Geom_2d );
917 }
918
919 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
920                                      const Point& thePoint,
921                                      const int    thePointIndex )
922 {
923   Handle(TDataStd_RealList) aListX, aListY;
924   getPointsLists( theSectionIndex, aListX, aListY );
925
926   if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
927   {
928     aListX->Append( thePoint.X() );
929     aListY->Append( thePoint.Y() );
930   }
931   else
932   {
933     TColStd_ListOfReal anOldListX;
934     anOldListX = aListX->List();
935
936     TColStd_ListOfReal anOldListY;
937     anOldListY = aListY->List();
938
939     // Refill the existing lists
940     aListX->Clear();
941     aListY->Clear();
942
943     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
944     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
945     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
946     {
947       double aCoordX = anIterX.Value();
948       double aCoordY = anIterY.Value();
949
950       if ( i == thePointIndex )
951       {
952         // Insert our new point
953         aListX->Append( thePoint.X() );
954         aListY->Append( thePoint.Y() );
955       }
956
957       aListX->Append( aCoordX );
958       aListY->Append( aCoordY );
959     }
960   }
961
962   Changed( Geom_2d );
963 }
964
965 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
966                                      const Point& thePoint,
967                                      const int    thePointIndex )
968 {
969   Handle(TDataStd_RealList) aListX, aListY;
970   getPointsLists( theSectionIndex, aListX, aListY );
971
972   if ( thePointIndex < 0 )
973   {
974     aListX->Prepend( thePoint.X() );
975     aListY->Prepend( thePoint.Y() );
976   }
977   else if ( thePointIndex >= aListX->Extent() )
978   {
979     aListX->Append( thePoint.X() );
980     aListY->Append( thePoint.Y() );
981   }
982   else
983   {
984     TColStd_ListOfReal anOldListX;
985     anOldListX = aListX->List();
986
987     TColStd_ListOfReal anOldListY;
988     anOldListY = aListY->List();
989
990     // Refill the existing lists
991     aListX->Clear();
992     aListY->Clear();
993
994     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
995     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
996     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
997     {
998       double aCoordX = anIterX.Value();
999       double aCoordY = anIterY.Value();
1000
1001       if ( i == thePointIndex )
1002       {
1003         // Insert our new point instead of old one
1004         aCoordX = thePoint.X();
1005         aCoordY = thePoint.Y();
1006       }
1007
1008       aListX->Append( aCoordX );
1009       aListY->Append( aCoordY );
1010     }
1011   }
1012
1013   Changed( Geom_2d );
1014 }
1015
1016 void HYDROData_PolylineXY::SetPoints( const int         theSectionIndex,
1017                                       const PointsList& thePoints )
1018 {
1019   Handle(TDataStd_RealList) aListX, aListY;
1020   getPointsLists( theSectionIndex, aListX, aListY );
1021
1022   aListX->Clear();
1023   aListY->Clear();
1024
1025   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1026   {
1027     const Point& aPoint = thePoints.Value( i );
1028     aListX->Append( aPoint.X() );
1029     aListY->Append( aPoint.Y() );
1030   }
1031 }
1032
1033 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1034                                         const int thePointIndex )
1035 {
1036   Handle(TDataStd_RealList) aListX, aListY;
1037   getPointsLists( theSectionIndex, aListX, aListY, false );
1038   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1039     return;
1040
1041   if ( aListX->Extent() == 1 )
1042   {
1043     removePointsLists( theSectionIndex );
1044   }
1045   else
1046   {
1047     TColStd_ListOfReal anOldListX;
1048     anOldListX = aListX->List();
1049
1050     TColStd_ListOfReal anOldListY;
1051     anOldListY = aListY->List();
1052
1053     // Refill the existing lists
1054     aListX->Clear();
1055     aListY->Clear();
1056
1057     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1058     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1059     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1060     {
1061       if ( i == thePointIndex )
1062         continue; // skip index to remove
1063
1064       aListX->Append( anIterX.Value() );
1065       aListY->Append( anIterY.Value() );
1066     }
1067   }
1068
1069   Changed( Geom_2d );
1070 }
1071
1072 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
1073 {
1074   PointsList aResList;
1075
1076   if( IsCustom() )
1077   {
1078     //TODO: make interpolation to fill the list
1079   }
1080  
1081   Handle(TDataStd_RealList) aListX, aListY;
1082   getPointsLists( theSectionIndex, aListX, aListY, false );
1083   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1084     return aResList;
1085
1086   TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1087   TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1088   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
1089   for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1090   {
1091     Point aPoint( anIterX.Value(), anIterY.Value() );
1092     if (IsConvertToGlobal)
1093       aDoc->Transform( aPoint, false );
1094     aResList.Append( aPoint );
1095   }
1096
1097   return aResList;
1098 }
1099
1100 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1101 {
1102   QPainterPath aPath;
1103
1104   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1105   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1106   NCollection_Sequence<bool>                              aSectClosures;
1107   GetSections( aSectNames, aSectTypes, aSectClosures );
1108
1109   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1110   {
1111     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1112     SectionType aSectionType = aSectTypes.Value( aSectionId );
1113     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1114
1115     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1116     if ( aSectPointsList.IsEmpty() )
1117       continue;
1118
1119     NCollection_Sequence<gp_XYZ> aPoints;
1120     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1121     {
1122       const Point& aSectPoint = aSectPointsList.Value( i );
1123
1124       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1125       aPoints.Append( aPoint );
1126     }
1127
1128     BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1129   }
1130
1131   return aPath;
1132 }
1133
1134 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1135 {
1136   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1137   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1138   NCollection_Sequence<bool>                              aSectClosures;
1139   GetSections( aSectNames, aSectTypes, aSectClosures );
1140
1141   gp_XY aDelta( theDx, theDy );
1142   for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1143   {
1144     PointsList aPoints = GetPoints( i );
1145     for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1146     {
1147       Point& aPoint = aPoints.ChangeValue( j );
1148       aPoint += aDelta;
1149     }
1150     SetPoints( i, aPoints );
1151   }
1152   Changed( Geom_2d );
1153 }
1154
1155 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1156 {
1157   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1158   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1159   NCollection_Sequence<bool>                              aSectClosures;
1160   GetSections( aSectNames, aSectTypes, aSectClosures );
1161
1162   for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1163     PointsList aPoints = GetPoints( i );
1164     for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1165       Point& aPoint = aPoints.ChangeValue( j );
1166
1167       QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1168
1169       aPoint.SetX( aTrsfPoint.x() );
1170       aPoint.SetY( aTrsfPoint.y() );
1171     }
1172     SetPoints( i, aPoints );
1173   }
1174
1175   Update();
1176 }
1177
1178 bool HYDROData_PolylineXY::IsCustom() const
1179 {
1180   bool isNull = GetShape().IsNull();
1181   int aNbPoints = 0;
1182
1183   //TODO: to check if there is no points
1184   //for( int i=0, n=NbSections(); i<n; i++ )
1185   //  aNbPoints += NbPoints( i );
1186
1187   return !isNull && aNbPoints == 0;
1188 }