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