]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_PolylineXY.cxx
Salome HOME
Corrections of examples path after install with scbi
[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_Polyline3D.h>
22 #include <HYDROData_Bathymetry.h>
23 #include <HYDROData_Iterator.h>
24 #include <HYDROData_ShapeFile.h>
25 #include <HYDROData_Profile.h>
26
27 #include "HYDROData_BSplineOperation.h"
28 #include "HYDROData_Document.h"
29 #include "HYDROData_ShapesTool.h"
30 #include "HYDROData_Tool.h"
31 #include "HYDROData_PolylineOperator.h"
32 #include "HYDROData_TopoCurve.h"
33
34 #include <BRep_Builder.hxx>
35 #include <BRepBuilderAPI_MakeEdge.hxx>
36 #include <BRepBuilderAPI_MakeWire.hxx>
37 #include <BRepBuilderAPI_MakePolygon.hxx>
38 #include <BRepBuilderAPI_MakeFace.hxx>
39 #include <BRepOffsetAPI_NormalProjection.hxx>
40 #include <BRepAdaptor_Curve.hxx>
41 #include <Quantity_Parameter.hxx>
42
43 #ifndef LIGHT_MODE
44 #include <GEOMBase.h>
45 #endif
46
47 #include <GeomAPI_ProjectPointOnCurve.hxx>
48 #include <GeomAdaptor_Curve.hxx>
49 #include <Geom_Line.hxx>
50 #include <Geom_BSplineCurve.hxx>
51
52 #include <GCPnts_AbscissaPoint.hxx>
53 #include <GCPnts_QuasiUniformDeflection.hxx>
54 #include <GCPnts_UniformDeflection.hxx>
55
56 #include <ImageComposer_MetaTypes.h>
57
58 #include <gp_Pnt.hxx>
59 #include <gp_XY.hxx>
60 #include <gp_Pln.hxx>
61
62 #include <NCollection_Map.hxx>
63
64 #include <TCollection_ExtendedString.hxx>
65
66 #include <TDataStd_ListIteratorOfListOfByte.hxx>
67 #include <TColStd_ListIteratorOfListOfInteger.hxx>
68 #include <TColStd_ListIteratorOfListOfReal.hxx>
69
70 #include <TColStd_Array1OfReal.hxx>
71 #include <TColgp_Array1OfPnt.hxx>
72
73 #include <TDataStd_AsciiString.hxx>
74 #include <TDataStd_BooleanList.hxx>
75 #include <TDataStd_ExtStringList.hxx>
76 #include <TDataStd_IntegerList.hxx>
77 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
78 #include <TDataStd_RealList.hxx>
79 #include <TDataStd_UAttribute.hxx>
80 #include <TDataStd_ExtStringArray.hxx>
81
82 #include <TopoDS_Iterator.hxx>
83 #include <TopTools_ListIteratorOfListOfShape.hxx>
84 #include <TopTools_HSequenceOfShape.hxx>
85 #include <TopExp_Explorer.hxx>
86 #include <ShapeAnalysis_FreeBounds.hxx>
87 #include <TopoDS.hxx>
88
89 #include <QColor>
90 #include <QPainterPath>
91 #include <QVariant>
92
93 //#define _DEVDEBUG_
94 #include "HYDRO_trace.hxx"
95 #include <BRepTools.hxx>
96 #include <sstream>
97 #include <cmath>
98
99 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
100
101 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
102
103 TCollection_AsciiString getUniqueSectionName( const NCollection_Sequence<TCollection_AsciiString>& theNamesSeq )
104 {
105   NCollection_Map<TCollection_AsciiString> aNamesMap;
106
107   for ( int i = 1, n = theNamesSeq.Size(); i <= n; ++i )
108   {
109     const TCollection_AsciiString& aSectName = theNamesSeq.Value( i );
110     aNamesMap.Add( aSectName );
111   }
112
113   TCollection_AsciiString aResName;
114
115   int aPrefIdx = 1;
116   do
117   {
118     aResName = TCollection_AsciiString( "Section_" ) + aPrefIdx;
119     ++aPrefIdx;
120   }
121   while ( aNamesMap.Contains( aResName ) );
122
123   return aResName;
124 }
125
126 TCollection_AsciiString getUniqueSectionName( const Handle(TDataStd_ExtStringList)& theNamesList )
127 {
128   NCollection_Sequence<TCollection_AsciiString> aNamesSeq;
129
130   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( theNamesList->List() );
131   for ( ; aNamesIter.More(); aNamesIter.Next() )
132     aNamesSeq.Append( aNamesIter.Value() );
133
134   return getUniqueSectionName( aNamesSeq );
135 }
136
137 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
138
139 HYDROData_PolylineXY::HYDROData_PolylineXY()
140 : HYDROData_IPolyline(),
141   myIsInCustomFlag( false )
142 {
143 }
144
145 HYDROData_PolylineXY::~HYDROData_PolylineXY()
146 {
147 }
148
149 QStringList HYDROData_PolylineXY::DumpToPython( const QString&       thePyScriptPath,
150                                                 MapOfTreatedObjects& theTreatedObjects ) const
151 {
152   QStringList aResList = dumpObjectCreation( theTreatedObjects );
153   QString aPolylineName = GetObjPyName();
154
155   // Set the wire color
156   QStringList aWireColorDef;
157
158   //QColor aWireColor = GetWireColor();
159   //setPythonObjectColor( aWireColorDef, aWireColor, DefaultWireColor(), "SetWireColor" );
160
161   int nbSec = NbSections();
162   for (int i = 0; i < nbSec; i++)
163   {
164     QColor aColor;
165     GetSectionColor(i, aColor);
166     setPythonPolylineSectionColor(aWireColorDef, i, aColor );
167     //aWireColorDef << "";
168   }
169   
170   if ( !aWireColorDef.isEmpty() )
171   {
172     aResList << aWireColorDef;
173     aResList << QString( "" );
174   }
175
176   bool anIsEditable = IsEditable();
177   if ( !anIsEditable )
178   {
179     // If polyline is not editable we try to import the shape from geom
180     TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
181     if ( !aGeomObjectEntry.IsEmpty() )
182     {
183       QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "polyline_sobj" );
184       aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" )" )
185                   .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
186
187       aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() )" )
188                   .arg( aPolylineName ).arg( aSalomeObjName );
189
190       aResList << QString( "%1.SetGeomObjectEntry( \"%2\" )" )
191                   .arg( aPolylineName ).arg( aGeomObjectEntry.ToCString() );
192     }
193   }
194   else
195   {
196     // Set polyline data
197     NCollection_Sequence<TCollection_AsciiString>           aSectNames;
198     NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
199     NCollection_Sequence<bool>                              aSectClosures;
200     GetSections( aSectNames, aSectTypes, aSectClosures );
201
202     for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
203     {
204       const TCollection_AsciiString& aSectName = aSectNames.Value( i );
205       const SectionType& aSectType = aSectTypes.Value( i );
206       bool aSectClosure = aSectClosures.Value( i );
207
208       aResList << QString( "%1.AddSection( \"%2\", %3, %4 )" ).arg( aPolylineName )
209                   .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
210
211       HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
212       for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
213       {
214         const Point& aSectPoint = aSectPointsList.Value( k );
215
216         QString anXStr = QString::number( aSectPoint.X(), 'f', 2 );
217         QString anYStr = QString::number( aSectPoint.Y(), 'f', 2 );
218         aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) )" ).arg( aPolylineName )
219           .arg( i - 1 ).arg( anXStr ).arg( anYStr );
220       }
221     }
222   }
223   aResList << QString( "" );
224   aResList << QString( "%1.Update()" ).arg( aPolylineName );
225   aResList << QString( "" );
226
227   return aResList;
228 }
229
230 QVariant HYDROData_PolylineXY::GetDataVariant()
231 {
232   QPainterPath aPath = GetPainterPath();
233
234   QVariant aVarData;
235   aVarData.setValue<QPainterPath>( aPath );
236   
237   return aVarData;
238 }
239
240 QColor HYDROData_PolylineXY::DefaultWireColor()
241 {
242   return QColor( Qt::black );
243 }
244
245 bool HYDROData_PolylineXY::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
246 {
247 #ifdef LIGHT_MODE
248   return false;
249 #else
250   if ( theIOR.IsEmpty() )
251     return false;
252
253   TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
254   if ( aShape.IsNull() )
255     return false;
256
257   return ImportShape( aShape, false, NULL );
258 #endif
259 }
260
261 void HYDROData_PolylineXY::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
262 {
263   Handle(TDataStd_AsciiString) anAttr = TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
264   anAttr->SetID(TDataStd_AsciiString::GetID());
265 }
266
267 TCollection_AsciiString HYDROData_PolylineXY::GetGeomObjectEntry() const
268 {
269   TCollection_AsciiString aRes;
270
271   TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
272   if ( !aLabel.IsNull() )
273   {
274     Handle(TDataStd_AsciiString) anAsciiStr;
275     if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
276       aRes = anAsciiStr->Get();
277   }
278
279   return aRes;
280 }
281
282 bool convertEdgeToSection( const TopoDS_Edge&                                       theEdge,
283                            NCollection_Sequence<TCollection_AsciiString>&           theSectNames,
284                            NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
285                            NCollection_Sequence<bool>&                              theSectClosures,
286                            NCollection_Sequence<HYDROData_PolylineXY::PointsList>&  theSectPoints,
287                            bool                                                     IsCanBeClosed,
288                            bool                                                     IsInterpolationAllowed,
289                            double                                                   theDeflection )
290 {
291   Standard_Real aFirst = 0.0, aLast = 0.0;
292   Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
293   if ( anEdgeGeomCurve.IsNull() )
294     return false;
295
296   TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
297   bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
298
299   HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
300   HYDROData_PolylineXY::PointsList aPointsList;
301
302   if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
303   {
304     Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
305
306     gp_Pnt aFirstPoint, aLastPoint;
307     aGeomLine->D0( aFirst, aFirstPoint );
308     aGeomLine->D0( aLast, aLastPoint );
309
310     HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
311     aPointsList.Append( aSectFirstPoint );
312
313     HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
314     aPointsList.Append( aSectLastPoint );
315   }
316   else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
317   {
318     aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
319
320     BRepAdaptor_Curve anAdaptorCurve( theEdge );
321     GCPnts_QuasiUniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
322     //GCPnts_UniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
323
324     int aNbPoints = aDiscrete.NbPoints();
325
326     // Decrease the number of imported poles because of last one 
327     // pole is the closing point which are the start point
328     if ( anIsEdgeClosed ) aNbPoints--;
329
330     for ( int i = 1; i <= aNbPoints; ++i )
331     {
332       const gp_Pnt& aPoint = aDiscrete.Value( i );
333
334       HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
335       aPointsList.Append( aSectPoint );
336     }
337   }
338   else
339   {
340     // Other curve types are not supported
341     return false;
342   }
343
344   if ( aPointsList.IsEmpty() )
345     return false;
346
347   theSectNames.Append( aSectName );
348   theSectTypes.Append( aSectionType );
349   theSectClosures.Append( anIsEdgeClosed );
350   theSectPoints.Append( aPointsList );
351
352   return true;
353 }
354
355
356 bool convertEdgesToSections( const TopoDS_Edge&                                       theEdge,
357                              NCollection_Sequence<TCollection_AsciiString>&           theSectNames,
358                              NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
359                              NCollection_Sequence<bool>&                              theSectClosures,
360                              NCollection_Sequence<HYDROData_PolylineXY::PointsList>&  theSectPoints,
361                              bool                                                     IsCanBeClosed,
362                              bool                                                     IsInterpolationAllowed,
363                              double                                                   theDeflection,
364                              const Handle( HYDROData_PolylineXY )&                    theOldPolyline )
365 {
366   DEBTRACE("convertEdgesToSections")
367   Standard_Real aFirst = 0.0, aLast = 0.0;
368   Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
369   if ( anEdgeGeomCurve.IsNull() )
370     return false;
371
372   bool isPrevious = (theSectTypes.Size() > 0);
373   DEBTRACE("nb sections: " << theSectTypes.Size());
374   HYDROData_PolylineXY::SectionType prevSectType = HYDROData_PolylineXY::SECTION_SPLINE;;
375   HYDROData_PolylineXY::PointsList prevPointList;
376   bool isPrevClosed = true;
377   if (isPrevious)
378     {
379        prevSectType = theSectTypes.Last();
380        prevPointList = theSectPoints.Last();
381        isPrevClosed = theSectClosures.Last();
382     }
383
384   bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
385   anIsEdgeClosed &= IsCanBeClosed; // on split, resulting edges are normally not closed...
386   HYDROData_PolylineXY::SectionType aSectionType ;
387   if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
388     {
389       aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
390     }
391   else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
392     {
393       aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
394     }
395   else
396     {
397       // Other curve types are not supported
398       return false;
399     }
400
401   bool isNewSection = !isPrevious || isPrevClosed || anIsEdgeClosed || prevSectType != aSectionType;
402   DEBTRACE(isNewSection <<": " << !isPrevious << " " << isPrevClosed << " " << anIsEdgeClosed << " " << (prevSectType != aSectionType));
403
404   HYDROData_PolylineXY::PointsList aPointsList;
405   if (!isNewSection)
406     {
407       aPointsList = prevPointList;
408     }
409
410   if( aSectionType == HYDROData_PolylineXY::SECTION_POLYLINE )
411     {
412       DEBTRACE("SECTION_POLYLINE");
413       Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
414
415       gp_Pnt aFirstPoint, aLastPoint;
416       aGeomLine->D0( aFirst, aFirstPoint );
417       aGeomLine->D0( aLast, aLastPoint );
418       HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
419       HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
420      if (!isNewSection)
421         {
422           if (aSectFirstPoint == prevPointList.Last())
423             {
424               DEBTRACE("points shared: a");//aPointsList.Append( aSectFirstPoint );
425               aPointsList.Append( aSectLastPoint );
426             }
427           else if (aSectLastPoint == prevPointList.Last())
428             {
429               DEBTRACE("points shared: b");//aPointsList.Append( aSectLastPoint );
430               aPointsList.Append( aSectFirstPoint );
431             }
432           else if (aSectFirstPoint == prevPointList.First())
433             {
434               DEBTRACE("points shared: c");//aPointsList.Prepend( aSectFirstPoint );
435               aPointsList.Prepend( aSectLastPoint );
436             }
437           else if (aSectLastPoint == prevPointList.First())
438             {
439               DEBTRACE("points shared: d");//aPointsList.Prepend( aSectLastPoint );
440               aPointsList.Prepend( aSectFirstPoint );
441             }
442           else
443             {
444               DEBTRACE("no point shared")
445               isNewSection = true; // no point shared, new section
446               aPointsList.Clear();
447               aPointsList.Append( aSectFirstPoint );
448               aPointsList.Append( aSectLastPoint );
449             }
450         }
451      else
452        {
453          DEBTRACE("new section");
454          aPointsList.Append( aSectFirstPoint );
455          aPointsList.Append( aSectLastPoint );
456        }
457     }
458   else // aSectionType == HYDROData_PolylineXY::SECTION_SPLINE
459     {
460       DEBTRACE("SECTION_SPLINE");
461       isNewSection = true;
462       aPointsList.Clear();
463
464       BRepAdaptor_Curve anAdaptorCurve(theEdge);
465       if (theOldPolyline.IsNull()) // --- no previous polyline: build a set of points from scratch for the spline
466         {
467           GCPnts_QuasiUniformDeflection aDiscrete(anAdaptorCurve, theDeflection);
468
469           int aNbPoints = aDiscrete.NbPoints();
470
471           // Decrease the number of imported poles because of last one
472           // pole is the closing point which are the start point
473           if (anIsEdgeClosed)
474             aNbPoints--;
475
476           for (int i = 1; i <= aNbPoints; ++i)
477             {
478               const gp_Pnt& aPoint = aDiscrete.Value(i);
479               HYDROData_PolylineXY::Point aSectPoint(aPoint.X(), aPoint.Y());
480               aPointsList.Append(aSectPoint);
481             }
482
483           gp_Pnt endPts[] = {gp_Pnt(aPointsList.First().X(),aPointsList.First().Y(), 0),
484                              gp_Pnt(aPointsList.Last().X(),aPointsList.Last().Y(), 0) };
485           DEBTRACE("curve start: "<< endPts[0].X() << " " << endPts[0].Y());
486           DEBTRACE("curve end: "<< endPts[1].X() << " " << endPts[1].Y());
487         }
488       else // --- split of a previous polyline: try to retrieve old sets of points and add intersection points
489         {
490           const gp_Pnt aEndPs[] = { anAdaptorCurve.Value(anAdaptorCurve.FirstParameter()).XYZ(),
491                                     anAdaptorCurve.Value(anAdaptorCurve.LastParameter()).XYZ() };
492           double midPar = (anAdaptorCurve.LastParameter() + anAdaptorCurve.FirstParameter())/2;
493           gp_Pnt midPnt;
494           anAdaptorCurve.D0(midPar, midPnt);
495           DEBTRACE("curve first point: " << aEndPs[0].X() << " " << aEndPs[0].Y() << " " << aEndPs[0].Z());
496           DEBTRACE("curve last point: " << aEndPs[1].X() << " " << aEndPs[1].Y() << " " << aEndPs[1].Z());
497           DEBTRACE("curve mid point: " << midPnt.X() << " " << midPnt.Y() << " " << midPnt.Z());
498
499           std::vector<TopoDS_Wire> aCurves;
500           HYDROData_PolylineOperator::GetWires(theOldPolyline, aCurves);
501
502           int nbSections = theOldPolyline->NbSections();
503           DEBTRACE("nbSections: "<< nbSections << ", nbCurves: " << aCurves.size() );
504           for (int isec = 0; isec < nbSections; isec++)
505             {
506               DEBTRACE("section: "<< isec);
507               bool isOldSectionclosed = theOldPolyline->IsClosedSection(isec);
508               TopoDS_Wire aWire = aCurves[isec]; // we suppose sections and wires are in the same order
509               TopExp_Explorer anExp(aWire, TopAbs_EDGE);
510               TopoDS_Edge anEdge = TopoDS::Edge(anExp.Current()); // the first is OK: only one normally with splines
511               BRepAdaptor_Curve adaptorOldCurve(anEdge);
512               double pfirst = adaptorOldCurve.FirstParameter();
513               double plast = adaptorOldCurve.LastParameter();
514               DEBTRACE("previous curve first last : "<< pfirst << " " << plast);
515               double p[3] = {-1, -1};
516               double d0= ProjectPointToCurve(aEndPs[0].XYZ(), adaptorOldCurve,p[0]);
517               double d1= ProjectPointToCurve(aEndPs[1].XYZ(), adaptorOldCurve,p[1]);
518               double d2= ProjectPointToCurve(midPnt.XYZ(), adaptorOldCurve, p[2]);
519               DEBTRACE("d0: "<<d0<<" d1: "<<d1<<" d2: "<<d2<<" p0: "<<p[0]<<" p1: "<<p[1]<<" p2: "<<p[2]);
520               if ((d0 < 1.e-3) && (d1 < 1.e-3) && (d2 < 1.e-3)) // we got the good old curve (and the good section)
521                 {
522                   double pmin = p[0];
523                   double pmax = p[1];
524                   bool forward = true;
525                   DEBTRACE("isOldSectionclosed: " << isOldSectionclosed);
526                   if (!isOldSectionclosed) // no need to check first and last points on an open curve
527                     {
528                       if (pmin > pmax)
529                         {
530                           pmin = p[1];
531                           pmax = p[0];
532                           forward = false;
533                         }
534                     }
535                   else // old section closed: check if we use first or last points...
536                     {
537                       if((pmin < pmax) && ((std::fabs(pmin - pfirst)> 1.e-3) && (std::fabs(pmax - plast) >1.e-3))) // internal points forward
538                         forward = true;
539                       else if ((pmin > pmax) && ((std::fabs(pmin - plast)> 1.e-3) && (std::fabs(pmax - pfirst) >1.e-3))) // internal points reverse
540                         {
541                           pmin = p[1];
542                           pmax = p[0];
543                           forward = false;
544                         }
545                       else if ((std::fabs(pmin - plast) <1.e-3) && (p[2] < pmax)) // forward, replace pmin par pfirst
546                           pmin = pfirst;
547                       else if ((std::fabs(pmin - plast) <1.e-3) && (p[2] > pmax)) // reverse
548                         {
549                           pmin = p[1];
550                           pmax = p[0];
551                           forward = false;
552                         }
553                       else if ((std::fabs(pmax - pfirst) <1.e-3) && (p[2] < pmin)) // reverse
554                         {
555                           pmin = p[1];
556                           pmax = p[0];
557                           forward = false;
558                         }
559                       else if ((std::fabs(pmax - pfirst) <1.e-3) && (p[2] > pmin)) // forward, replace pmax par plast
560                         pmax = plast;
561                    }
562                   DEBTRACE("forward: "<< forward << " pmin " << pmin <<  " pmax " << pmax);
563                   HYDROData_PolylineXY::Point aFirstPoint, aLastPoint;
564                   if (forward)
565                     {
566                       aFirstPoint = HYDROData_PolylineXY::Point(aEndPs[0].X(), aEndPs[0].Y());
567                       aLastPoint = HYDROData_PolylineXY::Point(aEndPs[1].X(), aEndPs[1].Y());
568                     }
569                   else
570                     {
571                       aFirstPoint = HYDROData_PolylineXY::Point(aEndPs[1].X(), aEndPs[1].Y());
572                       aLastPoint = HYDROData_PolylineXY::Point(aEndPs[0].X(), aEndPs[0].Y());
573                     }
574                   aPointsList.Append(aFirstPoint);
575
576                   HYDROData_PolylineXY::PointsList aSectPoints = theOldPolyline->GetPoints(isec, false);
577                   int nbPoints = aSectPoints.Length();
578                   DEBTRACE("nbPoints " << nbPoints);
579                   if (forward)
580                     for (int i=1; i<=nbPoints; i++)
581                       {
582                         HYDROData_PolylineXY::Point aPoint = aSectPoints.Value(i);
583                         gp_XYZ p(aPoint.X(), aPoint.Y(), 0);
584                         double param =-1;
585                         double d = ProjectPointToCurve(p, adaptorOldCurve, param);
586                         if ((param > pmin) && (param < pmax))
587                           {
588                             DEBTRACE("param: " << param);
589                             aPointsList.Append(aPoint);
590                           }
591                       }
592                   else
593                     for (int i=nbPoints; i>0; i--)
594                       {
595                         HYDROData_PolylineXY::Point aPoint = aSectPoints.Value(i);
596                         gp_XYZ p(aPoint.X(), aPoint.Y(), 0);
597                         double param =-1;
598                         double d = ProjectPointToCurve(p, adaptorOldCurve, param);
599                         if ((param > pmin) && (param < pmax))
600                           {
601                             DEBTRACE("param: " << param);
602                             aPointsList.Append(aPoint);
603                           }
604                       }
605
606                   aPointsList.Append(aLastPoint);
607                 }
608
609             }
610         }
611     }
612
613   if ( aPointsList.IsEmpty() )
614     return false;
615
616   TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
617   if (isNewSection)
618     {
619       DEBTRACE("isNewSection");
620       theSectNames.Append( aSectName );
621       theSectTypes.Append( aSectionType );
622       theSectClosures.Append( anIsEdgeClosed );
623       theSectPoints.Append( aPointsList );
624     }
625   else
626     {
627       DEBTRACE("sameSection");
628       theSectPoints.SetValue(theSectPoints.Length(), aPointsList);
629     }
630
631   return true;
632 }
633
634 bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape,
635                                         bool IsInterpolationAllowed,
636                                         const Handle( HYDROData_PolylineXY )& theOldPolyline,
637                                         bool IsClosureAllowed,
638                                         double theDeviation )
639 {
640   DEBTRACE("ImportShape");
641   if ( theShape.IsNull() )
642     return false;
643
644   //std::string brepName = this->GetName().toStdString();
645   //brepName += ".brep";
646   //BRepTools::Write( theShape, brepName.c_str() );
647
648   RemoveSections();
649
650   bool anIsCanBeImported = false;
651
652   NCollection_Sequence<TCollection_AsciiString> aSectNames;
653   NCollection_Sequence<SectionType>             aSectTypes;
654   NCollection_Sequence<bool>                    aSectClosures;
655   NCollection_Sequence<PointsList>              aSectPoints;
656
657   if ( theShape.ShapeType() == TopAbs_EDGE )
658   {
659       DEBTRACE("TopAbs_EDGE");
660     TopoDS_Edge anEdge = TopoDS::Edge( theShape );
661 //    anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes,
662 //      aSectClosures, aSectPoints, true, IsInterpolationAllowed, theDeviation );
663     anIsCanBeImported = convertEdgesToSections( anEdge, aSectNames, aSectTypes, aSectClosures,
664                                                 aSectPoints, IsClosureAllowed, IsInterpolationAllowed,
665                                                 theDeviation, theOldPolyline );
666   }
667   else if ( theShape.ShapeType() == TopAbs_WIRE )
668   {
669       DEBTRACE("TopAbs_WIRE");
670     TopTools_SequenceOfShape anEdges;
671     HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
672
673     anIsCanBeImported = !anEdges.IsEmpty();
674     for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
675     {
676       TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
677       anIsCanBeImported = convertEdgesToSections( aWireEdge, aSectNames, aSectTypes, aSectClosures,
678                                                   aSectPoints, IsClosureAllowed, IsInterpolationAllowed,
679                                                   theDeviation, theOldPolyline );
680     }
681   }
682
683   if ( anIsCanBeImported )
684   {
685     for ( int i = 1, n = aSectNames.Length(); i <= n; ++i )
686     {
687       const TCollection_AsciiString& aSectName = aSectNames.Value( i );
688       const SectionType& aSectType = aSectTypes.Value( i );
689       bool anIsSectionClosed = aSectClosures.Value( i );
690       const PointsList& aSectPointsList = aSectPoints( i );
691
692       AddSection( aSectName, aSectType, anIsSectionClosed );
693       SetPoints( i - 1, aSectPointsList );
694     }
695   }
696   else
697   {
698     TopoDS_Shape aShape = theShape;
699
700     if ( theShape.ShapeType() == TopAbs_EDGE )
701     {
702       // We make the wire from incoming edge because of other algorithms
703       // are waiting at least the wire from polyline
704       TopoDS_Edge anEdge = TopoDS::Edge( theShape );
705       BRepBuilderAPI_MakeWire aMakeWire( anEdge );
706       aMakeWire.Build();
707       if ( aMakeWire.IsDone() )
708         aShape = aMakeWire.Wire();
709     }
710
711     gp_Pln aPlane( gp_Pnt( 0, 0, 0 ), gp_Dir( 0, 0, 1 ) );
712     BRepBuilderAPI_MakeFace aMakeFace( aPlane );
713     aMakeFace.Build();
714     BRepOffsetAPI_NormalProjection aProj( aMakeFace.Face() );
715     aProj.Add( aShape );
716     aProj.Build();
717     TopoDS_Shape aResult;
718     if( aProj.IsDone() )
719       aResult = aProj.Shape();
720
721     SetShape( aResult );
722   }
723
724   setEditable( anIsCanBeImported );
725   Update();
726   return true;
727 }
728
729 TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&                  theType,
730                                              const bool&                         theIsClosed,
731                                              const NCollection_Sequence<gp_XYZ>& thePoints )
732 {
733   TopoDS_Wire aWire;
734   if( theType == SECTION_POLYLINE )
735   {
736     int aNbPoints = thePoints.Length();
737     BRepBuilderAPI_MakePolygon aMakeWire;
738     for ( int i = 1, n = aNbPoints; i <= n ; ++i )
739     {
740       gp_XYZ aPoint = thePoints.Value( i );
741       gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
742       aMakeWire.Add( aPnt );
743     }
744     if( theIsClosed && ( aNbPoints > 2 ) )
745       aMakeWire.Close();
746
747     if ( aMakeWire.IsDone() )
748       aWire = aMakeWire.Wire();
749   }
750   else //if( theType == PolylineSection::SECTION_SPLINE )
751   {
752     BRepBuilderAPI_MakeWire aMakeWire;
753
754     if ( thePoints.Size() > 1 )
755     {
756       Handle(Geom_BSplineCurve) aCurve = 
757         HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
758
759       TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aCurve ).Edge();
760       aMakeWire.Add( anEdge );
761     }
762     aMakeWire.Build();
763     if ( aMakeWire.IsDone() )
764       aWire = aMakeWire;
765   }
766
767   return aWire;
768 }
769
770 void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&                       thePath,
771                                              const SectionType&                  theType,
772                                              const bool&                         theIsClosed,
773                                              const NCollection_Sequence<gp_XYZ>& thePoints )
774 {
775   if ( thePoints.IsEmpty() )
776     return;
777
778   if ( theType == SECTION_POLYLINE )
779   {
780     const gp_XYZ& aFirstPoint = thePoints.Value( 1 );
781     thePath.moveTo( aFirstPoint.X(), aFirstPoint.Y() );
782
783     for( int i = 2, n = thePoints.Size(); i <= n; ++i )
784     {
785       const gp_XYZ& aSectPoint = thePoints.Value( i );
786
787       thePath.lineTo( aSectPoint.X(), aSectPoint.Y() );
788     }
789
790     if( theIsClosed )
791       thePath.closeSubpath();
792   }
793   else
794   {
795     Handle(Geom_BSplineCurve) aCurve = 
796       HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
797     HYDROData_BSplineOperation::ComputePath( aCurve, thePath );
798   }
799 }
800
801 void HYDROData_PolylineXY::Update()
802 {
803   if ( !IsEditable() )
804   {
805     // If polyline is not editable we no need to update it wire
806     ClearChanged();
807     return;
808   }
809
810   HYDROData_IPolyline::Update();
811
812   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
813   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
814   NCollection_Sequence<bool>                              aSectClosures;
815   GetSections( aSectNames, aSectTypes, aSectClosures );
816
817   //BRepBuilderAPI_MakeWire aMakeWire;
818
819   TopTools_ListOfShape aSectionWiresList;
820
821   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
822   {
823     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
824     SectionType aSectionType = aSectTypes.Value( aSectionId );
825     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
826
827     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
828     if ( aSectPointsList.IsEmpty() )
829       continue;
830     
831     NCollection_Sequence<gp_XYZ> aPoints;
832     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
833     {
834       const Point& aSectPoint = aSectPointsList.Value( i );
835
836       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
837       aPoints.Append( aPoint );
838     }
839
840     TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
841     if ( !aSectionWire.IsNull() ) {
842       aSectionWiresList.Append( aSectionWire );
843       //aMakeWire.Add( aSectionWire );
844     }
845   }
846 // all input wires in the <aSectionWiresList>
847
848   TopoDS_Shape aResult;
849   BRep_Builder aBB;
850   TopoDS_Compound aCmp;
851   aBB.MakeCompound(aCmp);
852   if (aSectionWiresList.Size() == 1)
853     aResult = aSectionWiresList.First();
854   else if (aSectionWiresList.Size() > 1)
855   {
856     TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
857     for(;it.More();it.Next())
858     {
859       aBB.Add(aCmp, it.Value());
860     }
861     aResult = aCmp;
862   }
863
864   //Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
865   //Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
866   //TopTools_ListIteratorOfListOfShape it(aSectionWiresList);
867   //for(;it.More();it.Next())
868   //{
869   //  TopExp_Explorer it2(it.Value(), TopAbs_EDGE);
870   //  for(;it2.More();it2.Next()) 
871   //    aSeqEdges->Append(it2.Current());
872   //}
873   //
874   //BRep_Builder aBB;
875   //TopoDS_Compound aCmp;
876   //TopoDS_Shape aResult;
877   //aBB.MakeCompound(aCmp);
878   //if(aSeqEdges->Length() >1)
879   //{
880   //  ShapeAnalysis_FreeBounds::ConnectEdgesToWires( aSeqEdges, 1E-5, Standard_True, aSeqWires );
881   //
882   //  if( aSeqWires->Length()==1 )
883   //    aResult = aSeqWires->Value( 1 );
884   //  else
885   //  {
886   //    for (Standard_Integer i = 1; i <= aSeqWires->Length();i++)
887   //    {
888   //      const TopoDS_Shape& aS1 = aSeqWires->Value(i);
889   //      aBB.Add(aCmp, aS1);
890   //    }
891   //    aResult = aCmp;
892   //  }
893   //}
894   //else if (aSeqEdges->Length() == 1)
895   //{
896   //  BRepBuilderAPI_MakeWire mkWire (TopoDS::Edge(aSeqEdges->Value(1)));
897   //  if (mkWire.IsDone())
898   //    aResult = mkWire.Wire();
899   //}
900
901   SetShape( aResult );
902 }
903
904 bool HYDROData_PolylineXY::IsHas2dPrs() const
905 {
906   return true;
907 }
908
909 bool HYDROData_PolylineXY::IsEditable() const
910 {
911   return !myLab.IsAttribute( GUID_IS_UNEDITABLE );
912 }
913
914 void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
915 {
916   if ( !theIsEditable )
917     TDataStd_UAttribute::Set( myLab, GUID_IS_UNEDITABLE );
918   else
919     myLab.ForgetAttribute( GUID_IS_UNEDITABLE );
920 }
921
922 /**
923  * Returns true if polyline is closed
924  */
925 bool HYDROData_PolylineXY::IsClosed(const bool theIsSimpleCheck) const
926 {
927   DEBTRACE("IsClosed " << theIsSimpleCheck << " " << GetName());
928   bool anIsClosed = false;
929
930   TopoDS_Shape aShape = GetShape();
931   if (aShape.IsNull())
932     return anIsClosed;
933
934   TopTools_SequenceOfShape aWires;
935   HYDROData_ShapesTool::ExploreShapeToShapes(aShape, TopAbs_WIRE, aWires);
936
937   int aNbWires = aWires.Length();
938   if (theIsSimpleCheck)
939     {
940       anIsClosed = aNbWires > 0;
941       for (int i = 1; i <= aNbWires && anIsClosed; ++i)
942         {
943           const TopoDS_Shape& aWire = aWires.Value(i);
944           anIsClosed = BRep_Tool::IsClosed(aWire);
945         }
946     }
947   else
948     {
949       if (aNbWires == 1)
950         anIsClosed = BRep_Tool::IsClosed(aWires.First());
951       else
952         {
953           DEBTRACE("aNbWires " << aNbWires);
954           Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
955           Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
956           for (int i = 1; i <= aNbWires; ++i)
957             {
958               const TopoDS_Shape& aWire = aWires.Value(i);
959               TopExp_Explorer it2(aWire, TopAbs_EDGE);
960               for (; it2.More(); it2.Next())
961                 aSeqEdges->Append(it2.Current());
962             }
963           if (aSeqEdges->Length() > 1)
964             {
965               DEBTRACE(aSeqEdges->Length());
966               ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, 1E-5, Standard_False, aSeqWires);
967               if (aSeqWires->Length() == 1)
968                 {
969                   DEBTRACE(aSeqWires->Length());
970                   const TopoDS_Wire& aPolylineWire = TopoDS::Wire(aSeqWires->Value(1));
971                   anIsClosed = BRep_Tool::IsClosed(aPolylineWire);
972                 }
973             }
974         }
975     }
976
977   return anIsClosed;
978 }
979
980 int HYDROData_PolylineXY::GetNbConnectedWires(Handle(TopTools_HSequenceOfShape)& aConnectedWires) const
981 {
982   TopoDS_Shape aShape = GetShape();
983   if (aShape.IsNull())
984     return 0;
985   int aNbconnectedWires = 0;
986   TopTools_SequenceOfShape aWires;
987   HYDROData_ShapesTool::ExploreShapeToShapes(aShape, TopAbs_WIRE, aWires);
988   int aNbWires = aWires.Length();
989   if (aNbWires == 1)
990     {
991       aNbconnectedWires = aNbWires;
992       aConnectedWires->Append(aWires.First());
993     }
994   else
995     {
996       DEBTRACE("aNbWires " << aNbWires);
997       Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
998       Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
999       for (int i = 1; i <= aNbWires; ++i)
1000         {
1001           const TopoDS_Shape& aWire = aWires.Value(i);
1002           TopExp_Explorer it2(aWire, TopAbs_EDGE);
1003           for (; it2.More(); it2.Next())
1004             aSeqEdges->Append(it2.Current());
1005         }
1006       if (aSeqEdges->Length() > 1)
1007         {
1008           DEBTRACE(aSeqEdges->Length());
1009           ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, 1E-5, Standard_False, aSeqWires);
1010         }
1011       aConnectedWires = aSeqWires;
1012       aNbconnectedWires = aConnectedWires->Length();
1013       DEBTRACE("aNbconnectedWires " << aNbconnectedWires);
1014     }
1015 return aNbconnectedWires;
1016 }
1017
1018 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
1019                                           const int thePointIndex ) const
1020 {
1021   double aResDistance = -1;
1022   if ( theSectionIndex < 0 || theSectionIndex >= NbSections() )
1023     return aResDistance;
1024
1025   if ( thePointIndex == 0 )
1026     return 0.0;
1027
1028   SectionType aSectionType = GetSectionType( theSectionIndex );
1029   bool anIsSectionClosed = IsClosedSection( theSectionIndex );
1030   PointsList aSectPointsList = GetPoints( theSectionIndex );
1031   if ( thePointIndex < 0 || thePointIndex >= aSectPointsList.Size()  )
1032     return aResDistance;
1033
1034   if ( aSectionType == SECTION_POLYLINE )
1035   {
1036     aResDistance = 0.0;
1037   
1038     Point aPrevPoint = aSectPointsList.Value( 1 );
1039     for ( int i = 2, aNbPoints = aSectPointsList.Size(); i <= aNbPoints; ++i )
1040     {
1041       const Point& aSectPoint = aSectPointsList.Value( i );
1042       aResDistance += gp_Pnt2d( aPrevPoint ).Distance( aSectPoint );
1043       aPrevPoint = aSectPoint;
1044
1045       if ( thePointIndex == i - 1 )
1046         break;
1047     }
1048   }
1049   else
1050   {
1051     gp_XYZ aPointToTest;
1052
1053     int aSectNbPoints = aSectPointsList.Size();
1054     NCollection_Sequence<gp_XYZ> aPoints;
1055     for( int i = 1 ; i <= aSectNbPoints; ++i )
1056     {
1057       const Point& aSectPoint = aSectPointsList.Value( i );
1058
1059       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1060       aPoints.Append( aPoint );
1061
1062       if ( thePointIndex == i - 1 )
1063         aPointToTest = aPoint;
1064     }
1065
1066     Handle(Geom_BSplineCurve) aCurve = 
1067       HYDROData_BSplineOperation::ComputeCurve( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
1068
1069     Quantity_Parameter aFirstParam = aCurve->FirstParameter();
1070     Quantity_Parameter aSecondParam = aCurve->LastParameter();
1071
1072     if ( thePointIndex != aSectNbPoints - 1 )
1073     {
1074       GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aCurve );
1075       aSecondParam = aProject.LowerDistanceParameter();
1076     }
1077
1078     GeomAdaptor_Curve anAdap( aCurve );
1079     
1080     aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
1081   }
1082
1083   return aResDistance;
1084 }
1085
1086 int HYDROData_PolylineXY::NbSections() const
1087 {
1088   Handle(TDataStd_ExtStringList) aNamesList;
1089   Handle(TDataStd_IntegerList)   aTypesList;
1090   Handle(TDataStd_BooleanList)   aClosuresList;
1091   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1092
1093   return !aClosuresList.IsNull() ? aClosuresList->Extent() : 0;
1094 }
1095
1096 void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectName,
1097                                        const SectionType              theSectionType,
1098                                        const bool                     theIsClosed )
1099 {
1100   Handle(TDataStd_ExtStringList) aNamesList;
1101   Handle(TDataStd_IntegerList)   aTypesList;
1102   Handle(TDataStd_BooleanList)   aClosuresList;
1103   getSectionsLists( aNamesList, aTypesList, aClosuresList );
1104
1105   TCollection_ExtendedString aSectName( theSectName );
1106   if ( aSectName.Length() <= 0 )
1107     aSectName = getUniqueSectionName( aNamesList );
1108
1109   aNamesList->Append( aSectName );
1110   aTypesList->Append( theSectionType );
1111   aClosuresList->Append( theIsClosed );
1112
1113   Changed( Geom_2d );
1114 }
1115
1116 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
1117 {
1118   TCollection_AsciiString aResName;
1119
1120   Handle(TDataStd_ExtStringList) aNamesList;
1121   Handle(TDataStd_IntegerList) aTypesList;
1122   Handle(TDataStd_BooleanList) aClosuresList;
1123   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1124   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
1125     return aResName;
1126
1127   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
1128   for ( int i = 0; aNamesIter.More() && i != theSectionIndex; aNamesIter.Next(), ++i );
1129
1130   if ( aNamesIter.More() )
1131     aResName = aNamesIter.Value();
1132
1133   return aResName;
1134 }
1135
1136 void HYDROData_PolylineXY::SetSectionName( const int                      theSectionIndex, 
1137                                            const TCollection_AsciiString& theSectionName )
1138 {
1139   Handle(TDataStd_ExtStringList) aNamesList;
1140   Handle(TDataStd_IntegerList) aTypesList;
1141   Handle(TDataStd_BooleanList) aClosuresList;
1142   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1143   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
1144     return;
1145
1146   TDataStd_ListOfExtendedString anOldNamesList;
1147   anOldNamesList = aNamesList->List();
1148
1149   // Refill the existing list
1150   aNamesList->Clear();
1151
1152   TCollection_ExtendedString aNewSectName = theSectionName;
1153
1154   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
1155   for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
1156     aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
1157 }
1158
1159 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
1160 {
1161   Handle(TDataStd_ExtStringList) aNamesList;
1162   Handle(TDataStd_IntegerList) aTypesList;
1163   Handle(TDataStd_BooleanList) aClosuresList;
1164   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1165   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
1166     return SECTION_POLYLINE;
1167
1168   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
1169   for ( int i = 0; aTypesIter.More() && i != theSectionIndex; aTypesIter.Next(), ++i );
1170
1171   return aTypesIter.More() ? (SectionType)aTypesIter.Value() : SECTION_POLYLINE;
1172 }
1173
1174 void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex, 
1175                                            const SectionType theSectionType )
1176 {
1177   Handle(TDataStd_ExtStringList) aNamesList;
1178   Handle(TDataStd_IntegerList) aTypesList;
1179   Handle(TDataStd_BooleanList) aClosuresList;
1180   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1181   if ( aTypesList.IsNull() || theSectionIndex >= aTypesList->Extent() )
1182     return;
1183
1184   TColStd_ListOfInteger anOldTypesList;
1185   anOldTypesList = aTypesList->List();
1186
1187   // Refill the existing list
1188   aTypesList->Clear();
1189
1190   TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
1191   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
1192     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
1193
1194   Changed( Geom_2d );
1195 }
1196
1197 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
1198 {
1199   Handle(TDataStd_ExtStringList) aNamesList;
1200   Handle(TDataStd_IntegerList) aTypesList;
1201   Handle(TDataStd_BooleanList) aClosuresList;
1202   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1203   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
1204     return false;
1205
1206   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
1207   for ( int i = 0; aClosuresIter.More() && i != theSectionIndex; aClosuresIter.Next(), ++i );
1208
1209   return aClosuresIter.More() ? (bool)aClosuresIter.Value() : false;
1210 }
1211
1212 void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex, 
1213                                              const bool theIsClosed )
1214 {
1215   Handle(TDataStd_ExtStringList) aNamesList;
1216   Handle(TDataStd_IntegerList) aTypesList;
1217   Handle(TDataStd_BooleanList) aClosuresList;
1218   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1219   if ( aClosuresList.IsNull() || theSectionIndex >= aClosuresList->Extent() )
1220     return;
1221
1222   TDataStd_ListOfByte anOldClosuresList;
1223   anOldClosuresList = aClosuresList->List();
1224
1225   // Refill the existing list
1226   aClosuresList->Clear();
1227
1228   TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
1229   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
1230     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
1231
1232   Changed( Geom_2d );
1233 }
1234
1235 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
1236                                         NCollection_Sequence<SectionType>&             theSectTypes,
1237                                         NCollection_Sequence<bool>&                    theSectClosures ) const
1238 {
1239   theSectNames.Clear();
1240   theSectTypes.Clear();
1241   theSectClosures.Clear();
1242
1243   if( IsCustom() )
1244   {
1245     const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1246   }
1247
1248   Handle(TDataStd_ExtStringList) aNamesList;
1249   Handle(TDataStd_IntegerList) aTypesList;
1250   Handle(TDataStd_BooleanList) aClosuresList;
1251   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1252   if ( aNamesList.IsNull() || aTypesList.IsNull() || aClosuresList.IsNull() )
1253     return;
1254
1255   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( aNamesList->List() );
1256   TColStd_ListIteratorOfListOfInteger aTypesIter( aTypesList->List() );
1257   TDataStd_ListIteratorOfListOfByte aClosuresIter( aClosuresList->List() );
1258   for ( ; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
1259           aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next() )
1260   {
1261     const TCollection_ExtendedString& aSectName = aNamesIter.Value();
1262     SectionType aSectType = (SectionType)aTypesIter.Value();
1263     bool aSectClosures = aClosuresIter.Value();
1264
1265     theSectNames.Append( aSectName );
1266     theSectTypes.Append( aSectType );
1267     theSectClosures.Append( aSectClosures );
1268   }
1269 }
1270
1271 void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
1272 {
1273   Handle(TDataStd_ExtStringList) aNamesList;
1274   Handle(TDataStd_IntegerList)   aTypesList;
1275   Handle(TDataStd_BooleanList)   aClosuresList;
1276   getSectionsLists( aNamesList, aTypesList, aClosuresList, false );
1277   if ( aNamesList.IsNull() || theSectionIndex >= aNamesList->Extent() )
1278     return;
1279
1280   if ( aNamesList->Extent() == 1 )
1281   {
1282     removeSectionsLists();
1283     removePointsLists();
1284     removeSectionColor();
1285   }
1286   else
1287   {
1288     TDataStd_ListOfExtendedString anOldNamesList;
1289     anOldNamesList = aNamesList->List();
1290
1291     TColStd_ListOfInteger anOldTypesList;
1292     anOldTypesList = aTypesList->List();
1293
1294     TDataStd_ListOfByte anOldClosuresList;
1295     anOldClosuresList = aClosuresList->List();
1296
1297     // Refill the existing lists
1298     aNamesList->Clear();
1299     aTypesList->Clear();
1300     aClosuresList->Clear();
1301
1302     TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
1303     TColStd_ListIteratorOfListOfInteger aTypesIter( anOldTypesList );
1304     TDataStd_ListIteratorOfListOfByte aClosuresIter( anOldClosuresList );
1305     for ( int i = 0; aNamesIter.More() && aTypesIter.More() && aClosuresIter.More();
1306                      aNamesIter.Next(), aTypesIter.Next(), aClosuresIter.Next(), ++i )
1307     {
1308       if ( i == theSectionIndex )
1309         continue; // skip index to remove
1310
1311       aNamesList->Append( aNamesIter.Value() );
1312       aTypesList->Append( aTypesIter.Value() );
1313       aClosuresList->Append( (bool)aClosuresIter.Value() );
1314     }
1315
1316     // Remove points that belongs to removed section
1317     removePointsLists( theSectionIndex ); 
1318     removeSectionColor (theSectionIndex);
1319   }
1320
1321   Changed( Geom_2d );
1322 }
1323
1324 void HYDROData_PolylineXY::RemoveSections()
1325 {
1326   removeSectionsLists();
1327   removePointsLists();
1328   Changed( Geom_2d );
1329 }
1330
1331 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
1332                                      const Point& thePoint,
1333                                      const int    thePointIndex )
1334 {
1335   Handle(TDataStd_RealList) aListX, aListY;
1336   getPointsLists( theSectionIndex, aListX, aListY );
1337
1338   if ( thePointIndex < 0 || thePointIndex >= aListX->Extent() )
1339   {
1340     aListX->Append( thePoint.X() );
1341     aListY->Append( thePoint.Y() );
1342   }
1343   else
1344   {
1345     TColStd_ListOfReal anOldListX;
1346     anOldListX = aListX->List();
1347
1348     TColStd_ListOfReal anOldListY;
1349     anOldListY = aListY->List();
1350
1351     // Refill the existing lists
1352     aListX->Clear();
1353     aListY->Clear();
1354
1355     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1356     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1357     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1358     {
1359       double aCoordX = anIterX.Value();
1360       double aCoordY = anIterY.Value();
1361
1362       if ( i == thePointIndex )
1363       {
1364         // Insert our new point
1365         aListX->Append( thePoint.X() );
1366         aListY->Append( thePoint.Y() );
1367       }
1368
1369       aListX->Append( aCoordX );
1370       aListY->Append( aCoordY );
1371     }
1372   }
1373
1374   Changed( Geom_2d );
1375 }
1376
1377 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
1378                                      const Point& thePoint,
1379                                      const int    thePointIndex )
1380 {
1381   Handle(TDataStd_RealList) aListX, aListY;
1382   getPointsLists( theSectionIndex, aListX, aListY );
1383
1384   if ( thePointIndex < 0 )
1385   {
1386     aListX->Prepend( thePoint.X() );
1387     aListY->Prepend( thePoint.Y() );
1388   }
1389   else if ( thePointIndex >= aListX->Extent() )
1390   {
1391     aListX->Append( thePoint.X() );
1392     aListY->Append( thePoint.Y() );
1393   }
1394   else
1395   {
1396     TColStd_ListOfReal anOldListX;
1397     anOldListX = aListX->List();
1398
1399     TColStd_ListOfReal anOldListY;
1400     anOldListY = aListY->List();
1401
1402     // Refill the existing lists
1403     aListX->Clear();
1404     aListY->Clear();
1405
1406     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1407     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1408     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1409     {
1410       double aCoordX = anIterX.Value();
1411       double aCoordY = anIterY.Value();
1412
1413       if ( i == thePointIndex )
1414       {
1415         // Insert our new point instead of old one
1416         aCoordX = thePoint.X();
1417         aCoordY = thePoint.Y();
1418       }
1419
1420       aListX->Append( aCoordX );
1421       aListY->Append( aCoordY );
1422     }
1423   }
1424
1425   Changed( Geom_2d );
1426 }
1427
1428 void HYDROData_PolylineXY::SetPoints( const int         theSectionIndex,
1429                                       const PointsList& thePoints )
1430 {
1431   Handle(TDataStd_RealList) aListX, aListY;
1432   getPointsLists( theSectionIndex, aListX, aListY );
1433
1434   aListX->Clear();
1435   aListY->Clear();
1436
1437   for ( int i = 1, n = thePoints.Length(); i <= n; ++i )
1438   {
1439     const Point& aPoint = thePoints.Value( i );
1440     aListX->Append( aPoint.X() );
1441     aListY->Append( aPoint.Y() );
1442   }
1443 }
1444
1445 void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
1446                                         const int thePointIndex )
1447 {
1448   Handle(TDataStd_RealList) aListX, aListY;
1449   getPointsLists( theSectionIndex, aListX, aListY, false );
1450   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1451     return;
1452
1453   if ( aListX->Extent() == 1 )
1454   {
1455     removePointsLists( theSectionIndex );
1456   }
1457   else
1458   {
1459     TColStd_ListOfReal anOldListX;
1460     anOldListX = aListX->List();
1461
1462     TColStd_ListOfReal anOldListY;
1463     anOldListY = aListY->List();
1464
1465     // Refill the existing lists
1466     aListX->Clear();
1467     aListY->Clear();
1468
1469     TColStd_ListIteratorOfListOfReal anIterX( anOldListX );
1470     TColStd_ListIteratorOfListOfReal anIterY( anOldListY );
1471     for ( int i = 0; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next(), ++i )
1472     {
1473       if ( i == thePointIndex )
1474         continue; // skip index to remove
1475
1476       aListX->Append( anIterX.Value() );
1477       aListY->Append( anIterY.Value() );
1478     }
1479   }
1480
1481   Changed( Geom_2d );
1482 }
1483
1484 HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
1485 {
1486   PointsList aResList;
1487
1488   if( IsCustom() )
1489   {
1490     const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
1491   }
1492  
1493   Handle(TDataStd_RealList) aListX, aListY;
1494   getPointsLists( theSectionIndex, aListX, aListY, false );
1495   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
1496     return aResList;
1497
1498   TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
1499   TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
1500   Handle(HYDROData_Document) aDoc = HYDROData_Document::Document();
1501   for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
1502   {
1503     Point aPoint( anIterX.Value(), anIterY.Value() );
1504     if (IsConvertToGlobal)
1505       aDoc->Transform( aPoint, false );
1506     aResList.Append( aPoint );
1507   }
1508
1509   return aResList;
1510 }
1511
1512 QPainterPath HYDROData_PolylineXY::GetPainterPath() const
1513 {
1514   QPainterPath aPath;
1515
1516   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1517   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1518   NCollection_Sequence<bool>                              aSectClosures;
1519   GetSections( aSectNames, aSectTypes, aSectClosures );
1520
1521   for ( int aSectionId = 1, aNbSects = aSectNames.Size(); aSectionId <= aNbSects; aSectionId++ )
1522   {
1523     TCollection_AsciiString aSectName = aSectNames.Value( aSectionId );
1524     SectionType aSectionType = aSectTypes.Value( aSectionId );
1525     bool anIsSectionClosed = aSectClosures.Value( aSectionId );
1526
1527     PointsList aSectPointsList = GetPoints( aSectionId - 1 );
1528     if ( aSectPointsList.IsEmpty() )
1529       continue;
1530
1531     NCollection_Sequence<gp_XYZ> aPoints;
1532     for( int i = 1, n = aSectPointsList.Size(); i <= n; ++i )
1533     {
1534       const Point& aSectPoint = aSectPointsList.Value( i );
1535
1536       gp_XYZ aPoint( aSectPoint.X(), aSectPoint.Y(), 0.0 );
1537       aPoints.Append( aPoint );
1538     }
1539
1540     BuildPainterPath( aPath, aSectionType, anIsSectionClosed, aPoints );
1541   }
1542
1543   return aPath;
1544 }
1545
1546 void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
1547 {
1548   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1549   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1550   NCollection_Sequence<bool>                              aSectClosures;
1551   GetSections( aSectNames, aSectTypes, aSectClosures );
1552
1553   gp_XY aDelta( theDx, theDy );
1554   for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
1555   {
1556     PointsList aPoints = GetPoints( i );
1557     for( int j = 1, n = aPoints.Size(); j <= n; ++j )
1558     {
1559       Point& aPoint = aPoints.ChangeValue( j );
1560       aPoint += aDelta;
1561     }
1562     SetPoints( i, aPoints );
1563   }
1564   Changed( Geom_2d );
1565 }
1566
1567 void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
1568 {
1569   NCollection_Sequence<TCollection_AsciiString>           aSectNames;
1570   NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
1571   NCollection_Sequence<bool>                              aSectClosures;
1572   GetSections( aSectNames, aSectTypes, aSectClosures );
1573
1574   for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
1575     PointsList aPoints = GetPoints( i );
1576     for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
1577       Point& aPoint = aPoints.ChangeValue( j );
1578
1579       QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
1580
1581       aPoint.SetX( aTrsfPoint.x() );
1582       aPoint.SetY( aTrsfPoint.y() );
1583     }
1584     SetPoints( i, aPoints );
1585   }
1586
1587   Update();
1588 }
1589
1590 bool HYDROData_PolylineXY::IsCustom() const
1591 {
1592   if( myIsInCustomFlag )
1593     return false;
1594
1595   bool isNull = GetShape().IsNull();
1596   int aNbPoints = 0;
1597
1598   HYDROData_PolylineXY* aThat = const_cast<HYDROData_PolylineXY*>( this );
1599   aThat->myIsInCustomFlag = true;
1600   for( int i=0, n=NbSections(); i<n; i++ )
1601     aNbPoints += NbPoints( i );
1602   aThat->myIsInCustomFlag = false;
1603
1604   return !isNull && aNbPoints == 0;
1605 }
1606
1607 bool HYDROData_PolylineXY::GetIsInCustomFlag() const
1608 {
1609   return myIsInCustomFlag;
1610 }
1611
1612 void HYDROData_PolylineXY::SetIsInCustomFlag( bool theValue )
1613 {
1614   myIsInCustomFlag = theValue;
1615 }
1616
1617 void HYDROData_PolylineXY::Interpolate()
1618 {
1619   ImportShape( GetShape(), true, NULL );
1620 }
1621
1622 void HYDROData_PolylineXY::SetDBFInfo( const QStringList& theDBFTable )   
1623
1624   int i = 1;
1625   Handle_TDataStd_ExtStringArray TExtStrArr = 
1626     TDataStd_ExtStringArray::Set( myLab.FindChild( DataTag_DBFTableInfo), 1, theDBFTable.size() );
1627   foreach (QString val, theDBFTable)
1628   {
1629     std::string StdVal = val.toStdString();
1630     const char* aCVal = StdVal.c_str();
1631     TExtStrArr->SetValue(i, TCollection_ExtendedString(aCVal));
1632     i++;
1633   }
1634 }
1635
1636 bool HYDROData_PolylineXY::GetDBFInfo(QStringList& theDBFTable) const
1637 {
1638   TDF_Label aLabel = myLab.FindChild( DataTag_DBFTableInfo, false );
1639   if ( !aLabel.IsNull() )
1640   {
1641     Handle(TDataStd_ExtStringArray) anExtStrArr;
1642     if ( aLabel.FindAttribute( TDataStd_ExtStringArray::GetID(), anExtStrArr ) )
1643     {
1644       for (int i = anExtStrArr->Lower(); i <= anExtStrArr->Upper(); i++ )
1645       {
1646         Standard_ExtString str = anExtStrArr->Value(i).ToExtString();
1647         TCollection_AsciiString aText (str);
1648         theDBFTable << QString(aText.ToCString());
1649       }
1650     }
1651   }
1652   else
1653     return false;
1654   return true;
1655 }
1656
1657
1658 HYDROData_SequenceOfObjects HYDROData_PolylineXY::ImportShapesFromFile(const QString& aFileName)
1659 {
1660     HYDROData_SequenceOfObjects importedEntities;
1661   if (aFileName.isEmpty())
1662     return importedEntities;
1663
1664   QString anExt = aFileName.split('.', QString::SkipEmptyParts).back();
1665   anExt.toLower();
1666   bool importXY = false;
1667   Handle(HYDROData_Document) theDocument = HYDROData_Document::Document();
1668
1669   importXY = true;
1670   if (anExt == "shp")
1671     {
1672       HYDROData_ShapeFile anImporter;
1673       NCollection_IndexedDataMap<Handle(HYDROData_Entity), SHPObject*> theEntitiesToSHPObj;
1674       NCollection_Sequence<Handle(HYDROData_Entity)> PolyEnt;
1675       int aShapeTypeOfFile = -1;
1676       int aStat = anImporter.OpenAndParse(aFileName);
1677       if (aStat == 1)
1678         {
1679           aStat = anImporter.ImportPolylines(theDocument, aFileName, theEntitiesToSHPObj,
1680                                              HYDROData_ShapeFile::ImportShapeType_All);
1681           for (int k = 1; k <= theEntitiesToSHPObj.Extent(); k++)
1682             {
1683               PolyEnt.Append(theEntitiesToSHPObj.FindKey(k));
1684             }
1685           if (aStat == 1 || aStat == 2)
1686             {
1687               //try to import DBF
1688               QString aDBFFileName;
1689               aDBFFileName = aFileName.simplified().replace(aFileName.simplified().size() - 4, 4, ".dbf");
1690               bool DBF_Stat = anImporter.DBF_OpenDBF(aDBFFileName);
1691               if (DBF_Stat)
1692                 {
1693                   QStringList aFieldList = anImporter.DBF_GetFieldList();
1694                   int nbRecords = anImporter.DBF_GetNbRecords();
1695                   if (theEntitiesToSHPObj.Extent() == nbRecords)
1696                     {
1697                       int indNameAttrFound = -1;
1698                       int k = 0;
1699                       bool bUseNameAttrFound = false;
1700                       for (QStringList::iterator it = aFieldList.begin(); it != aFieldList.end(); it++, k++)
1701                         if (QString::compare(*it, "name", Qt::CaseInsensitive) == 0)
1702                           {
1703                             indNameAttrFound = k;
1704                             break;
1705                           }
1706
1707                       if (indNameAttrFound != -1)
1708                         bUseNameAttrFound = true;
1709
1710                       std::vector<std::vector<HYDROData_ShapeFile::DBF_AttrValue>> anAttrVV;
1711                       for (int i = 0; i < aFieldList.size(); i++)
1712                         {
1713                           std::vector<HYDROData_ShapeFile::DBF_AttrValue> anAttrV;
1714                           anAttrVV.push_back(anAttrV);
1715                           anImporter.DBF_GetAttributeList(i, anAttrVV[i]);
1716                         }
1717
1718                       int indNULL = 1;
1719                       for (int i = 1; i <= theEntitiesToSHPObj.Extent(); i++)
1720                         {
1721                           Handle(HYDROData_PolylineXY) aPolylineXY = Handle(HYDROData_PolylineXY)::DownCast(
1722                               theEntitiesToSHPObj.FindKey(i));
1723                           QStringList aDBFinfo;
1724                           aDBFinfo << aFieldList; //first, the table header
1725                           for (int j = 0; j < aFieldList.size(); j++)
1726                             {
1727                               QString attr;
1728                               const HYDROData_ShapeFile::DBF_AttrValue &attrV = anAttrVV[j][i - 1];
1729                               if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_String)
1730                                 aDBFinfo << attrV.myStrVal;
1731                               else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Integer)
1732                                 aDBFinfo << QString::number(attrV.myIntVal);
1733                               else if (attrV.myFieldType == HYDROData_ShapeFile::DBF_FieldType_Double)
1734                                 aDBFinfo << QString::number(attrV.myDoubleVal);
1735                               else
1736                                 aDBFinfo << "";
1737                             }
1738                           aPolylineXY->SetDBFInfo(aDBFinfo);
1739                           if (bUseNameAttrFound)
1740                             {
1741                               QString nameOfPoly = aDBFinfo[aDBFinfo.size() / 2 + indNameAttrFound];
1742                               if (!nameOfPoly.isEmpty())
1743                                 aPolylineXY->SetName(nameOfPoly);
1744                               else
1745                                 {
1746                                   aPolylineXY->SetName("null_name_" + QString::number(indNULL));
1747                                   indNULL++;
1748                                 }
1749                             }
1750                         }
1751                     }
1752                 }
1753             }
1754
1755           if (anImporter.GetShapeType() == 13) //Polyline 3D
1756             {
1757               NCollection_Sequence<Handle(HYDROData_Entity)> CheckedPolylines = PolyEnt; //aDLG->GetCheckedPolylines();
1758               NCollection_IndexedDataMap<Handle(HYDROData_Entity), SHPObject*> CheckedEntitiesToSHPObj;
1759               for (int k = 1; k <= CheckedPolylines.Size(); k++)
1760                 {
1761                   SHPObject *const*SHPObkP = theEntitiesToSHPObj.Seek(CheckedPolylines(k));
1762                   if (SHPObkP != NULL)
1763                     CheckedEntitiesToSHPObj.Add(CheckedPolylines(k), *SHPObkP);
1764                 }
1765               NCollection_Sequence<Handle(HYDROData_Entity)> theEntitiesPoly3D;
1766               anImporter.ImportPolylines3D(theDocument, CheckedEntitiesToSHPObj, theEntitiesPoly3D,
1767                                            HYDROData_ShapeFile::ImportShapeType_All);
1768               PolyEnt.Append(theEntitiesPoly3D);
1769
1770             }
1771           anImporter.Free();
1772         }
1773       importedEntities.Append(PolyEnt);
1774     }
1775   else if (anExt == "xy" || (importXY && anExt == "xyz"))
1776     {
1777       if (!HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, true, importedEntities))
1778         DEBTRACE("no polyline imported");
1779     }
1780   else if (anExt == "xyz")
1781     {
1782       if (!HYDROData_Tool::importPolylineFromXYZ(aFileName, theDocument, false, importedEntities))
1783         DEBTRACE("no polyline imported");
1784     }
1785   return importedEntities;
1786 }
1787
1788
1789 void HYDROData_PolylineXY::ExportShapeXY(Handle(HYDROData_Document) theDocument,
1790                                          const QString& aFileName,
1791                                          const NCollection_Sequence<Handle(HYDROData_PolylineXY)>& aPolyXYSeq,
1792                                          QStringList& aNonExpList)
1793 {
1794     HYDROData_ShapeFile shp = HYDROData_ShapeFile();
1795     NCollection_Sequence<Handle(HYDROData_Polyline3D)> emptySeq;
1796     shp.Export(theDocument, aFileName, aPolyXYSeq, emptySeq, aNonExpList);
1797 }