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