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