Salome HOME
Merge branch 'BR_MULTI_BATHS' into HEAD
[modules/hydro.git] / src / HYDROData / HYDROData_PolylineXY.cxx
old mode 100755 (executable)
new mode 100644 (file)
index d418144..fc97524
@@ -1,3 +1,20 @@
+// Copyright (C) 2014-2015  EDF-R&D
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include "HYDROData_PolylineXY.h"
 
@@ -5,6 +22,8 @@
 #include "HYDROData_Document.h"
 #include "HYDROData_ShapesTool.h"
 #include "HYDROData_Tool.h"
+#include "HYDROData_PolylineOperator.h"
+#include "HYDROData_TopoCurve.h"
 
 #include <BRep_Builder.hxx>
 #include <BRepBuilderAPI_MakeEdge.hxx>
 #include <BRepBuilderAPI_MakePolygon.hxx>
 #include <BRepBuilderAPI_MakeFace.hxx>
 #include <BRepOffsetAPI_NormalProjection.hxx>
+#include <BRepAdaptor_Curve.hxx>
+
+#ifndef LIGHT_MODE
+#include <GEOMBase.h>
+#endif
 
 #include <GeomAPI_ProjectPointOnCurve.hxx>
 #include <GeomAdaptor_Curve.hxx>
@@ -19,6 +43,8 @@
 #include <Geom_BSplineCurve.hxx>
 
 #include <GCPnts_AbscissaPoint.hxx>
+#include <GCPnts_QuasiUniformDeflection.hxx>
+#include <GCPnts_UniformDeflection.hxx>
 
 #include <ImageComposer_MetaTypes.h>
 
@@ -35,7 +61,9 @@
 #include <TColStd_ListIteratorOfListOfReal.hxx>
 
 #include <TColStd_Array1OfReal.hxx>
+#include <TColgp_Array1OfPnt.hxx>
 
+#include <TDataStd_AsciiString.hxx>
 #include <TDataStd_BooleanList.hxx>
 #include <TDataStd_ExtStringList.hxx>
 #include <TDataStd_IntegerList.hxx>
 #include <QPainterPath>
 #include <QVariant>
 
+#define _DEVDEBUG_
+#include "HYDRO_trace.hxx"
+#include <BRepTools.hxx>
+#include <sstream>
+#include <cmath>
+
 static const Standard_GUID GUID_IS_UNEDITABLE("e5799736-9030-4051-91a4-2e58321fa153");
 
 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
@@ -96,7 +130,8 @@ IMPLEMENT_STANDARD_HANDLE(HYDROData_PolylineXY, HYDROData_IPolyline)
 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_PolylineXY, HYDROData_IPolyline)
 
 HYDROData_PolylineXY::HYDROData_PolylineXY()
-: HYDROData_IPolyline()
+: HYDROData_IPolyline(),
+  myIsInCustomFlag( false )
 {
 }
 
@@ -104,38 +139,73 @@ HYDROData_PolylineXY::~HYDROData_PolylineXY()
 {
 }
 
-QStringList HYDROData_PolylineXY::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
+QStringList HYDROData_PolylineXY::DumpToPython( const QString&       thePyScriptPath,
+                                                MapOfTreatedObjects& theTreatedObjects ) const
 {
   QStringList aResList = dumpObjectCreation( theTreatedObjects );
   QString aPolylineName = GetObjPyName();
 
-  // Set polilyne data
-  NCollection_Sequence<TCollection_AsciiString>           aSectNames;
-  NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
-  NCollection_Sequence<bool>                              aSectClosures;
-  GetSections( aSectNames, aSectTypes, aSectClosures );
+  // Set the wire color
+  QStringList aWireColorDef;
 
-  for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
+  QColor aWireColor = GetWireColor();
+  setPythonObjectColor( aWireColorDef, aWireColor, DefaultWireColor(), "SetWireColor" );
+  
+  if ( !aWireColorDef.isEmpty() )
   {
-    const TCollection_AsciiString& aSectName = aSectNames.Value( i );
-    const SectionType& aSectType = aSectTypes.Value( i );
-    bool aSectClosure = aSectClosures.Value( i );
-
-    aResList << QString( "%1.AddSection( \"%2\", %3, %4 );" ).arg( aPolylineName )
-                .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
+    aResList << aWireColorDef;
+    aResList << QString( "" );
+  }
 
-    HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
-    for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
+  bool anIsEditable = IsEditable();
+  if ( !anIsEditable )
+  {
+    // If polyline is not editable we try to import the shape from geom
+    TCollection_AsciiString aGeomObjectEntry = GetGeomObjectEntry();
+    if ( !aGeomObjectEntry.IsEmpty() )
     {
-      const Point& aSectPoint = aSectPointsList.Value( k );
+      QString aSalomeObjName = HYDROData_Tool::GenerateNameForPython( theTreatedObjects, "polyline_sobj" );
+      aResList << QString( "%1 = salome.myStudy.FindObjectID( \"%2\" )" )
+                  .arg( aSalomeObjName ).arg( aGeomObjectEntry.ToCString() );
 
-      aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) );" ).arg( aPolylineName )
-        .arg( i - 1 ).arg( aSectPoint.X() ).arg( aSectPoint.Y() );
+      aResList << QString( "%1.ImportFromGeomIOR( %2.GetIOR() )" )
+                  .arg( aPolylineName ).arg( aSalomeObjName );
+
+      aResList << QString( "%1.SetGeomObjectEntry( \"%2\" )" )
+                  .arg( aPolylineName ).arg( aGeomObjectEntry.ToCString() );
     }
   }
+  else
+  {
+    // Set polyline data
+    NCollection_Sequence<TCollection_AsciiString>           aSectNames;
+    NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
+    NCollection_Sequence<bool>                              aSectClosures;
+    GetSections( aSectNames, aSectTypes, aSectClosures );
+
+    for ( int i = 1, n = aSectNames.Size(); i <= n; ++i )
+    {
+      const TCollection_AsciiString& aSectName = aSectNames.Value( i );
+      const SectionType& aSectType = aSectTypes.Value( i );
+      bool aSectClosure = aSectClosures.Value( i );
+
+      aResList << QString( "%1.AddSection( \"%2\", %3, %4 )" ).arg( aPolylineName )
+                  .arg( aSectName.ToCString() ).arg( aSectType ).arg( aSectClosure );
+
+      HYDROData_IPolyline::PointsList aSectPointsList = GetPoints( i - 1 );
+      for ( int k = 1, aNbPoints = aSectPointsList.Size(); k <= aNbPoints; ++k )
+      {
+        const Point& aSectPoint = aSectPointsList.Value( k );
 
+        QString anXStr = QString::number( aSectPoint.X(), 'f', 2 );
+        QString anYStr = QString::number( aSectPoint.Y(), 'f', 2 );
+        aResList << QString( "%1.AddPoint( %2, gp_XY( %3, %4 ) )" ).arg( aPolylineName )
+          .arg( i - 1 ).arg( anXStr ).arg( anYStr );
+      }
+    }
+  }
   aResList << QString( "" );
-  aResList << QString( "%1.Update();" ).arg( aPolylineName );
+  aResList << QString( "%1.Update()" ).arg( aPolylineName );
   aResList << QString( "" );
 
   return aResList;
@@ -153,12 +223,43 @@ QVariant HYDROData_PolylineXY::GetDataVariant()
 
 QColor HYDROData_PolylineXY::DefaultWireColor()
 {
-  return QColor( Qt::red );
+  return QColor( Qt::black );
 }
 
-TopoDS_Shape HYDROData_PolylineXY::GetShape() const
+bool HYDROData_PolylineXY::ImportFromGeomIOR( const TCollection_AsciiString& theIOR )
 {
-  return getPolylineShape();
+#ifdef LIGHT_MODE
+  return false;
+#else
+  if ( theIOR.IsEmpty() )
+    return false;
+
+  TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( theIOR.ToCString() );
+  if ( aShape.IsNull() )
+    return false;
+
+  return ImportShape( aShape, false, NULL );
+#endif
+}
+
+void HYDROData_PolylineXY::SetGeomObjectEntry( const TCollection_AsciiString& theEntry )
+{
+  TDataStd_AsciiString::Set( myLab.FindChild( DataTag_GeomObjectEntry ), theEntry );
+}
+
+TCollection_AsciiString HYDROData_PolylineXY::GetGeomObjectEntry() const
+{
+  TCollection_AsciiString aRes;
+
+  TDF_Label aLabel = myLab.FindChild( DataTag_GeomObjectEntry, false );
+  if ( !aLabel.IsNull() )
+  {
+    Handle(TDataStd_AsciiString) anAsciiStr;
+    if ( aLabel.FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
+      aRes = anAsciiStr->Get();
+  }
+
+  return aRes;
 }
 
 bool convertEdgeToSection( const TopoDS_Edge&                                       theEdge,
@@ -166,7 +267,9 @@ bool convertEdgeToSection( const TopoDS_Edge&
                            NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
                            NCollection_Sequence<bool>&                              theSectClosures,
                            NCollection_Sequence<HYDROData_PolylineXY::PointsList>&  theSectPoints,
-                           const bool                                               theIsCanBeClosed )
+                           bool                                                     IsCanBeClosed,
+                           bool                                                     IsInterpolationAllowed,
+                           double                                                   theDeflection )
 {
   Standard_Real aFirst = 0.0, aLast = 0.0;
   Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
@@ -179,7 +282,7 @@ bool convertEdgeToSection( const TopoDS_Edge&
   HYDROData_PolylineXY::SectionType aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
   HYDROData_PolylineXY::PointsList aPointsList;
 
-  if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
+  if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
   {
     Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
 
@@ -193,28 +296,23 @@ bool convertEdgeToSection( const TopoDS_Edge&
     HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
     aPointsList.Append( aSectLastPoint );
   }
-  else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) )
+  else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
   {
     aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
 
-    Handle(Geom_BSplineCurve) aGeomSpline = 
-      Handle(Geom_BSplineCurve)::DownCast( anEdgeGeomCurve );
+    BRepAdaptor_Curve anAdaptorCurve( theEdge );
+    GCPnts_QuasiUniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
+    //GCPnts_UniformDeflection aDiscrete( anAdaptorCurve, theDeflection );
 
-    int aNbKnots = aGeomSpline->NbKnots();
+    int aNbPoints = aDiscrete.NbPoints();
 
-    TColStd_Array1OfReal aSplineKnots( 1, aNbKnots );
-    aGeomSpline->Knots( aSplineKnots );
+    // Decrease the number of imported poles because of last one 
+    // pole is the closing point which are the start point
+    if ( anIsEdgeClosed ) aNbPoints--;
 
-    // Decrease the number of imported knots because of last one 
-    // knot is the closing point which are the start point
-    if ( anIsEdgeClosed ) aNbKnots--;
-
-    for ( int i = 1; i <= aNbKnots; ++i )
+    for ( int i = 1; i <= aNbPoints; ++i )
     {
-      const Standard_Real& aKnot = aSplineKnots.Value( i );
-
-      gp_Pnt aPoint;
-      aGeomSpline->D0( aKnot, aPoint );
+      const gp_Pnt& aPoint = aDiscrete.Value( i );
 
       HYDROData_PolylineXY::Point aSectPoint( aPoint.X(), aPoint.Y() );
       aPointsList.Append( aSectPoint );
@@ -237,11 +335,299 @@ bool convertEdgeToSection( const TopoDS_Edge&
   return true;
 }
 
-bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape )
+
+bool convertEdgesToSections( const TopoDS_Edge&                                       theEdge,
+                             NCollection_Sequence<TCollection_AsciiString>&           theSectNames,
+                             NCollection_Sequence<HYDROData_PolylineXY::SectionType>& theSectTypes,
+                             NCollection_Sequence<bool>&                              theSectClosures,
+                             NCollection_Sequence<HYDROData_PolylineXY::PointsList>&  theSectPoints,
+                             bool                                                     IsCanBeClosed,
+                             bool                                                     IsInterpolationAllowed,
+                             double                                                   theDeflection,
+                             const Handle( HYDROData_PolylineXY )&                    theOldPolyline )
+{
+  DEBTRACE("convertEdgesToSections")
+  Standard_Real aFirst = 0.0, aLast = 0.0;
+  Handle(Geom_Curve) anEdgeGeomCurve = BRep_Tool::Curve( theEdge, aFirst, aLast );
+  if ( anEdgeGeomCurve.IsNull() )
+    return false;
+
+  bool isPrevious = (theSectTypes.Size() > 0);
+  DEBTRACE("nb sections: " << theSectTypes.Size());
+  HYDROData_PolylineXY::SectionType prevSectType = HYDROData_PolylineXY::SECTION_SPLINE;;
+  HYDROData_PolylineXY::PointsList prevPointList;
+  bool isPrevClosed = true;
+  if (isPrevious)
+    {
+       prevSectType = theSectTypes.Last();
+       prevPointList = theSectPoints.Last();
+       isPrevClosed = theSectClosures.Last();
+    }
+
+  bool anIsEdgeClosed = anEdgeGeomCurve->IsClosed();
+  anIsEdgeClosed &= IsCanBeClosed; // on split, resulting edges are normally not closed...
+  HYDROData_PolylineXY::SectionType aSectionType ;
+  if( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_Line) ) )
+    {
+      aSectionType = HYDROData_PolylineXY::SECTION_POLYLINE;
+    }
+  else if ( anEdgeGeomCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) || IsInterpolationAllowed )
+    {
+      aSectionType = HYDROData_PolylineXY::SECTION_SPLINE;
+    }
+  else
+    {
+      // Other curve types are not supported
+      return false;
+    }
+
+  bool isNewSection = !isPrevious || isPrevClosed || anIsEdgeClosed || prevSectType != aSectionType;
+  DEBTRACE(isNewSection <<": " << !isPrevious << " " << isPrevClosed << " " << anIsEdgeClosed << " " << (prevSectType != aSectionType));
+
+  HYDROData_PolylineXY::PointsList aPointsList;
+  if (!isNewSection)
+    {
+      aPointsList = prevPointList;
+    }
+
+  if( aSectionType == HYDROData_PolylineXY::SECTION_POLYLINE )
+    {
+      DEBTRACE("SECTION_POLYLINE");
+      Handle(Geom_Line) aGeomLine = Handle(Geom_Line)::DownCast( anEdgeGeomCurve );
+
+      gp_Pnt aFirstPoint, aLastPoint;
+      aGeomLine->D0( aFirst, aFirstPoint );
+      aGeomLine->D0( aLast, aLastPoint );
+      HYDROData_PolylineXY::Point aSectFirstPoint( aFirstPoint.X(), aFirstPoint.Y() );
+      HYDROData_PolylineXY::Point aSectLastPoint( aLastPoint.X(), aLastPoint.Y() );
+     if (!isNewSection)
+        {
+          if (aSectFirstPoint == prevPointList.Last())
+            {
+              DEBTRACE("points shared: a");//aPointsList.Append( aSectFirstPoint );
+              aPointsList.Append( aSectLastPoint );
+            }
+          else if (aSectLastPoint == prevPointList.Last())
+            {
+              DEBTRACE("points shared: b");//aPointsList.Append( aSectLastPoint );
+              aPointsList.Append( aSectFirstPoint );
+            }
+          else if (aSectFirstPoint == prevPointList.First())
+            {
+              DEBTRACE("points shared: c");//aPointsList.Prepend( aSectFirstPoint );
+              aPointsList.Prepend( aSectLastPoint );
+            }
+          else if (aSectLastPoint == prevPointList.First())
+            {
+              DEBTRACE("points shared: d");//aPointsList.Prepend( aSectLastPoint );
+              aPointsList.Prepend( aSectFirstPoint );
+            }
+          else
+            {
+              DEBTRACE("no point shared")
+              isNewSection = true; // no point shared, new section
+              aPointsList.Clear();
+              aPointsList.Append( aSectFirstPoint );
+              aPointsList.Append( aSectLastPoint );
+            }
+        }
+     else
+       {
+         DEBTRACE("new section");
+         aPointsList.Append( aSectFirstPoint );
+         aPointsList.Append( aSectLastPoint );
+       }
+    }
+  else // aSectionType == HYDROData_PolylineXY::SECTION_SPLINE
+    {
+      DEBTRACE("SECTION_SPLINE");
+      isNewSection = true;
+      aPointsList.Clear();
+
+      BRepAdaptor_Curve anAdaptorCurve(theEdge);
+      if (theOldPolyline.IsNull()) // --- no previous polyline: build a set of points from scratch for the spline
+        {
+          GCPnts_QuasiUniformDeflection aDiscrete(anAdaptorCurve, theDeflection);
+
+          int aNbPoints = aDiscrete.NbPoints();
+
+          // Decrease the number of imported poles because of last one
+          // pole is the closing point which are the start point
+          if (anIsEdgeClosed)
+            aNbPoints--;
+
+          for (int i = 1; i <= aNbPoints; ++i)
+            {
+              const gp_Pnt& aPoint = aDiscrete.Value(i);
+              HYDROData_PolylineXY::Point aSectPoint(aPoint.X(), aPoint.Y());
+              aPointsList.Append(aSectPoint);
+            }
+
+          gp_Pnt endPts[] = {gp_Pnt(aPointsList.First().X(),aPointsList.First().Y(), 0),
+                             gp_Pnt(aPointsList.Last().X(),aPointsList.Last().Y(), 0) };
+          DEBTRACE("curve start: "<< endPts[0].X() << " " << endPts[0].Y());
+          DEBTRACE("curve end: "<< endPts[1].X() << " " << endPts[1].Y());
+        }
+      else // --- split of a previous polyline: try to retrieve old sets of points and add intersection points
+        {
+          const gp_Pnt aEndPs[] = { anAdaptorCurve.Value(anAdaptorCurve.FirstParameter()).XYZ(),
+                                    anAdaptorCurve.Value(anAdaptorCurve.LastParameter()).XYZ() };
+          double midPar = (anAdaptorCurve.LastParameter() + anAdaptorCurve.FirstParameter())/2;
+          gp_Pnt midPnt;
+          anAdaptorCurve.D0(midPar, midPnt);
+          DEBTRACE("curve first point: " << aEndPs[0].X() << " " << aEndPs[0].Y() << " " << aEndPs[0].Z());
+          DEBTRACE("curve last point: " << aEndPs[1].X() << " " << aEndPs[1].Y() << " " << aEndPs[1].Z());
+          DEBTRACE("curve mid point: " << midPnt.X() << " " << midPnt.Y() << " " << midPnt.Z());
+
+          std::vector<TopoDS_Wire> aCurves;
+          HYDROData_PolylineOperator::GetWires(theOldPolyline, aCurves);
+
+          int nbSections = theOldPolyline->NbSections();
+          DEBTRACE("nbSections: "<< nbSections << ", nbCurves: " << aCurves.size() );
+          for (int isec = 0; isec < nbSections; isec++)
+            {
+              DEBTRACE("section: "<< isec);
+              bool isOldSectionclosed = theOldPolyline->IsClosedSection(isec);
+              TopoDS_Wire aWire = aCurves[isec]; // we suppose sections and wires are in the same order
+              TopExp_Explorer anExp(aWire, TopAbs_EDGE);
+              TopoDS_Edge anEdge = TopoDS::Edge(anExp.Current()); // the first is OK: only one normally with splines
+              BRepAdaptor_Curve adaptorOldCurve(anEdge);
+              double pfirst = adaptorOldCurve.FirstParameter();
+              double plast = adaptorOldCurve.LastParameter();
+              DEBTRACE("previous curve first last : "<< pfirst << " " << plast);
+              double p[3] = {-1, -1};
+              double d0= ProjectPointToCurve(aEndPs[0].XYZ(), adaptorOldCurve,p[0]);
+              double d1= ProjectPointToCurve(aEndPs[1].XYZ(), adaptorOldCurve,p[1]);
+              double d2= ProjectPointToCurve(midPnt.XYZ(), adaptorOldCurve, p[2]);
+              DEBTRACE("d0: "<<d0<<" d1: "<<d1<<" d2: "<<d2<<" p0: "<<p[0]<<" p1: "<<p[1]<<" p2: "<<p[2]);
+              if ((d0 < 1.e-3) && (d1 < 1.e-3) && (d2 < 1.e-3)) // we got the good old curve (and the good section)
+                {
+                  double pmin = p[0];
+                  double pmax = p[1];
+                  bool forward = true;
+                  DEBTRACE("isOldSectionclosed: " << isOldSectionclosed);
+                  if (!isOldSectionclosed) // no need to check first and last points on an open curve
+                    {
+                      if (pmin > pmax)
+                        {
+                          pmin = p[1];
+                          pmax = p[0];
+                          forward = false;
+                        }
+                    }
+                  else // old section closed: check if we use first or last points...
+                    {
+                      if((pmin < pmax) && ((abs(pmin - pfirst)> 1.e-3) && (abs(pmax - plast) >1.e-3))) // internal points forward
+                        forward = true;
+                      else if ((pmin > pmax) && ((abs(pmin - plast)> 1.e-3) && (abs(pmax - pfirst) >1.e-3))) // internal points reverse
+                        {
+                          pmin = p[1];
+                          pmax = p[0];
+                          forward = false;
+                        }
+                      else if ((abs(pmin - plast) <1.e-3) && (p[2] < pmax)) // forward, replace pmin par pfirst
+                          pmin = pfirst;
+                      else if ((abs(pmin - plast) <1.e-3) && (p[2] > pmax)) // reverse
+                        {
+                          pmin = p[1];
+                          pmax = p[0];
+                          forward = false;
+                        }
+                      else if ((abs(pmax - pfirst) <1.e-3) && (p[2] < pmin)) // reverse
+                        {
+                          pmin = p[1];
+                          pmax = p[0];
+                          forward = false;
+                        }
+                      else if ((abs(pmax - pfirst) <1.e-3) && (p[2] > pmin)) // forward, replace pmax par plast
+                        pmax = plast;
+                   }
+                  DEBTRACE("forward: "<< forward << " pmin " << pmin <<  " pmax " << pmax);
+                  HYDROData_PolylineXY::Point aFirstPoint, aLastPoint;
+                  if (forward)
+                    {
+                      aFirstPoint = HYDROData_PolylineXY::Point(aEndPs[0].X(), aEndPs[0].Y());
+                      aLastPoint = HYDROData_PolylineXY::Point(aEndPs[1].X(), aEndPs[1].Y());
+                    }
+                  else
+                    {
+                      aFirstPoint = HYDROData_PolylineXY::Point(aEndPs[1].X(), aEndPs[1].Y());
+                      aLastPoint = HYDROData_PolylineXY::Point(aEndPs[0].X(), aEndPs[0].Y());
+                    }
+                  aPointsList.Append(aFirstPoint);
+
+                  HYDROData_PolylineXY::PointsList aSectPoints = theOldPolyline->GetPoints(isec, false);
+                  int nbPoints = aSectPoints.Length();
+                  DEBTRACE("nbPoints " << nbPoints);
+                  if (forward)
+                    for (int i=1; i<=nbPoints; i++)
+                      {
+                        HYDROData_PolylineXY::Point aPoint = aSectPoints.Value(i);
+                        gp_XYZ p(aPoint.X(), aPoint.Y(), 0);
+                        double param =-1;
+                        double d = ProjectPointToCurve(p, adaptorOldCurve, param);
+                        if ((param > pmin) && (param < pmax))
+                          {
+                            DEBTRACE("param: " << param);
+                            aPointsList.Append(aPoint);
+                          }
+                      }
+                  else
+                    for (int i=nbPoints; i>0; i--)
+                      {
+                        HYDROData_PolylineXY::Point aPoint = aSectPoints.Value(i);
+                        gp_XYZ p(aPoint.X(), aPoint.Y(), 0);
+                        double param =-1;
+                        double d = ProjectPointToCurve(p, adaptorOldCurve, param);
+                        if ((param > pmin) && (param < pmax))
+                          {
+                            DEBTRACE("param: " << param);
+                            aPointsList.Append(aPoint);
+                          }
+                      }
+
+                  aPointsList.Append(aLastPoint);
+                }
+
+            }
+        }
+    }
+
+  if ( aPointsList.IsEmpty() )
+    return false;
+
+  TCollection_AsciiString aSectName = getUniqueSectionName( theSectNames );
+  if (isNewSection)
+    {
+      DEBTRACE("isNewSection");
+      theSectNames.Append( aSectName );
+      theSectTypes.Append( aSectionType );
+      theSectClosures.Append( anIsEdgeClosed );
+      theSectPoints.Append( aPointsList );
+    }
+  else
+    {
+      DEBTRACE("sameSection");
+      theSectPoints.SetValue(theSectPoints.Length(), aPointsList);
+    }
+
+  return true;
+}
+
+bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape,
+                                        bool IsInterpolationAllowed,
+                                        const Handle( HYDROData_PolylineXY )& theOldPolyline,
+                                        bool IsClosureAllowed,
+                                        double theDeviation )
 {
+  DEBTRACE("ImportShape");
   if ( theShape.IsNull() )
     return false;
 
+  //std::string brepName = this->GetName().toStdString();
+  //brepName += ".brep";
+  //BRepTools::Write( theShape, brepName.c_str() );
+
   RemoveSections();
 
   bool anIsCanBeImported = false;
@@ -253,11 +639,17 @@ bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape )
 
   if ( theShape.ShapeType() == TopAbs_EDGE )
   {
+      DEBTRACE("TopAbs_EDGE");
     TopoDS_Edge anEdge = TopoDS::Edge( theShape );
-    anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, true );
+//    anIsCanBeImported = convertEdgeToSection( anEdge, aSectNames, aSectTypes,
+//      aSectClosures, aSectPoints, true, IsInterpolationAllowed, theDeviation );
+    anIsCanBeImported = convertEdgesToSections( anEdge, aSectNames, aSectTypes, aSectClosures,
+                                                aSectPoints, IsClosureAllowed, IsInterpolationAllowed,
+                                                theDeviation, theOldPolyline );
   }
   else if ( theShape.ShapeType() == TopAbs_WIRE )
   {
+      DEBTRACE("TopAbs_WIRE");
     TopTools_SequenceOfShape anEdges;
     HYDROData_ShapesTool::ExploreShapeToShapes( theShape, TopAbs_EDGE, anEdges );
 
@@ -265,7 +657,9 @@ bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape )
     for ( int i = 1, n = anEdges.Length(); i <= n && anIsCanBeImported; ++i )
     {
       TopoDS_Edge aWireEdge = TopoDS::Edge( anEdges.Value( i ) );
-      anIsCanBeImported = convertEdgeToSection( aWireEdge, aSectNames, aSectTypes, aSectClosures, aSectPoints, false );
+      anIsCanBeImported = convertEdgesToSections( aWireEdge, aSectNames, aSectTypes, aSectClosures,
+                                                  aSectPoints, IsClosureAllowed, IsInterpolationAllowed,
+                                                  theDeviation, theOldPolyline );
     }
   }
 
@@ -307,11 +701,11 @@ bool HYDROData_PolylineXY::ImportShape( const TopoDS_Shape& theShape )
     if( aProj.IsDone() )
       aResult = aProj.Shape();
 
-    setPolylineShape( aResult );
+    SetShape( aResult );
   }
 
   setEditable( anIsCanBeImported );
-
+  Update();
   return true;
 }
 
@@ -322,14 +716,15 @@ TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&
   TopoDS_Wire aWire;
   if( theType == SECTION_POLYLINE )
   {
+    int aNbPoints = thePoints.Length();
     BRepBuilderAPI_MakePolygon aMakeWire;
-    for ( int i = 1, n = thePoints.Length(); i <= n ; ++i )
+    for ( int i = 1, n = aNbPoints; i <= n ; ++i )
     {
       gp_XYZ aPoint = thePoints.Value( i );
       gp_Pnt aPnt( aPoint.X(), aPoint.Y(), aPoint.Z() );
       aMakeWire.Add( aPnt );
     }
-    if( theIsClosed )
+    if( theIsClosed && ( aNbPoints > 2 ) )
       aMakeWire.Close();
 
     if ( aMakeWire.IsDone() )
@@ -341,9 +736,10 @@ TopoDS_Wire HYDROData_PolylineXY::BuildWire( const SectionType&
 
     if ( thePoints.Size() > 1 )
     {
-      HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
+      Handle(Geom_BSplineCurve) aCurve = 
+        HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
 
-      TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aBSpline.Curve() ).Edge();
+      TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge( aCurve ).Edge();
       aMakeWire.Add( anEdge );
     }
     aMakeWire.Build();
@@ -379,8 +775,9 @@ void HYDROData_PolylineXY::BuildPainterPath( QPainterPath&
   }
   else
   {
-    HYDROData_BSplineOperation aBSpline( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
-    aBSpline.ComputePath( thePath );
+    Handle(Geom_BSplineCurve) aCurve = 
+      HYDROData_BSplineOperation::ComputeCurve( thePoints, theIsClosed, LOCAL_SELECTION_TOLERANCE );
+    HYDROData_BSplineOperation::ComputePath( aCurve, thePath );
   }
 }
 
@@ -389,7 +786,7 @@ void HYDROData_PolylineXY::Update()
   if ( !IsEditable() )
   {
     // If polyline is not editable we no need to update it wire
-    SetToUpdate( false );
+    ClearChanged();
     return;
   }
 
@@ -400,7 +797,7 @@ void HYDROData_PolylineXY::Update()
   NCollection_Sequence<bool>                              aSectClosures;
   GetSections( aSectNames, aSectTypes, aSectClosures );
 
-  BRepBuilderAPI_MakeWire aMakeWire;
+  //BRepBuilderAPI_MakeWire aMakeWire;
 
   TopTools_ListOfShape aSectionWiresList;
 
@@ -426,7 +823,7 @@ void HYDROData_PolylineXY::Update()
     TopoDS_Wire aSectionWire = BuildWire( aSectionType, anIsSectionClosed, aPoints );
     if ( !aSectionWire.IsNull() ) {
       aSectionWiresList.Append( aSectionWire );
-      aMakeWire.Add( aSectionWire );
+      //aMakeWire.Add( aSectionWire );
     }
   }
 // all input wires in the <aSectionWiresList>
@@ -446,7 +843,7 @@ void HYDROData_PolylineXY::Update()
   aBB.MakeCompound(aCmp);
   if(aSeqEdges->Length() >1)
   {
-    ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges,1E-5,Standard_False,aSeqWires);
+    ShapeAnalysis_FreeBounds::ConnectEdgesToWires( aSeqEdges, 1E-5, Standard_True, aSeqWires );
 
     if( aSeqWires->Length()==1 )
       aResult = aSeqWires->Value( 1 );
@@ -467,7 +864,12 @@ void HYDROData_PolylineXY::Update()
       aResult = mkWire.Wire();
   }
 
-  setPolylineShape( aResult );
+  SetShape( aResult );
+}
+
+bool HYDROData_PolylineXY::IsHas2dPrs() const
+{
+  return true;
 }
 
 bool HYDROData_PolylineXY::IsEditable() const
@@ -486,35 +888,99 @@ void HYDROData_PolylineXY::setEditable( const bool theIsEditable )
 /**
  * Returns true if polyline is closed
  */
-bool HYDROData_PolylineXY::IsClosed( const bool theIsSimpleCheck ) const
+bool HYDROData_PolylineXY::IsClosed(const bool theIsSimpleCheck) const
 {
+  //DEBTRACE("IsClosed " << theIsSimpleCheck << " " << GetName());
   bool anIsClosed = false;
 
   TopoDS_Shape aShape = GetShape();
-  if ( aShape.IsNull() )
+  if (aShape.IsNull())
     return anIsClosed;
 
   TopTools_SequenceOfShape aWires;
-  HYDROData_ShapesTool::ExploreShapeToShapes( aShape, TopAbs_WIRE, aWires );
+  HYDROData_ShapesTool::ExploreShapeToShapes(aShape, TopAbs_WIRE, aWires);
 
   int aNbWires = aWires.Length();
-  if ( theIsSimpleCheck )
-  {
-    anIsClosed = aNbWires > 0;
-    for ( int i = 1; i <= aNbWires && anIsClosed; ++i )
+  if (theIsSimpleCheck)
     {
-      const TopoDS_Shape& aWire = aWires.Value( i );
-      anIsClosed = BRep_Tool::IsClosed( aWire );
+      anIsClosed = aNbWires > 0;
+      for (int i = 1; i <= aNbWires && anIsClosed; ++i)
+        {
+          const TopoDS_Shape& aWire = aWires.Value(i);
+          anIsClosed = BRep_Tool::IsClosed(aWire);
+        }
     }
-  }
   else
-  {
-    anIsClosed = aNbWires == 1 && BRep_Tool::IsClosed( aWires.First() );
-  }
+    {
+      if (aNbWires == 1)
+        anIsClosed = BRep_Tool::IsClosed(aWires.First());
+      else
+        {
+          //DEBTRACE("aNbWires " << aNbWires);
+          Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
+          Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
+          for (int i = 1; i <= aNbWires; ++i)
+            {
+              const TopoDS_Shape& aWire = aWires.Value(i);
+              TopExp_Explorer it2(aWire, TopAbs_EDGE);
+              for (; it2.More(); it2.Next())
+                aSeqEdges->Append(it2.Current());
+            }
+          if (aSeqEdges->Length() > 1)
+            {
+              //DEBTRACE(aSeqEdges->Length());
+              ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, 1E-5, Standard_False, aSeqWires);
+              if (aSeqWires->Length() == 1)
+                {
+                  //DEBTRACE(aSeqWires->Length());
+                  const TopoDS_Wire& aPolylineWire = TopoDS::Wire(aSeqWires->Value(1));
+                  anIsClosed = BRep_Tool::IsClosed(aPolylineWire);
+                }
+            }
+        }
+    }
 
   return anIsClosed;
 }
 
+int HYDROData_PolylineXY::GetNbConnectedWires(Handle(TopTools_HSequenceOfShape)& aConnectedWires) const
+{
+  TopoDS_Shape aShape = GetShape();
+  if (aShape.IsNull())
+    return 0;
+  int aNbconnectedWires = 0;
+  TopTools_SequenceOfShape aWires;
+  HYDROData_ShapesTool::ExploreShapeToShapes(aShape, TopAbs_WIRE, aWires);
+  int aNbWires = aWires.Length();
+  if (aNbWires == 1)
+    {
+      aNbconnectedWires = aNbWires;
+      aConnectedWires->Append(aWires.First());
+    }
+  else
+    {
+      //DEBTRACE("aNbWires " << aNbWires);
+      Handle(TopTools_HSequenceOfShape) aSeqWires = new TopTools_HSequenceOfShape;
+      Handle(TopTools_HSequenceOfShape) aSeqEdges = new TopTools_HSequenceOfShape;
+      for (int i = 1; i <= aNbWires; ++i)
+        {
+          const TopoDS_Shape& aWire = aWires.Value(i);
+          TopExp_Explorer it2(aWire, TopAbs_EDGE);
+          for (; it2.More(); it2.Next())
+            aSeqEdges->Append(it2.Current());
+        }
+      if (aSeqEdges->Length() > 1)
+        {
+          //DEBTRACE(aSeqEdges->Length());
+          ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdges, 1E-5, Standard_False, aSeqWires);
+        }
+      aConnectedWires = aSeqWires;
+      aNbconnectedWires = aConnectedWires->Length();
+      DEBTRACE("aNbconnectedWires " << aNbconnectedWires);
+    }
+return aNbconnectedWires;
+}
+
 double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
                                           const int thePointIndex ) const
 {
@@ -563,18 +1029,19 @@ double HYDROData_PolylineXY::GetDistance( const int theSectionIndex,
         aPointToTest = aPoint;
     }
 
-    HYDROData_BSplineOperation aBSpline( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
+    Handle(Geom_BSplineCurve) aCurve = 
+      HYDROData_BSplineOperation::ComputeCurve( aPoints, anIsSectionClosed, LOCAL_SELECTION_TOLERANCE );
 
-    Quantity_Parameter aFirstParam = aBSpline.Curve()->FirstParameter();
-    Quantity_Parameter aSecondParam = aBSpline.Curve()->LastParameter();
+    Quantity_Parameter aFirstParam = aCurve->FirstParameter();
+    Quantity_Parameter aSecondParam = aCurve->LastParameter();
 
     if ( thePointIndex != aSectNbPoints - 1 )
     {
-      GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aBSpline.Curve() );
+      GeomAPI_ProjectPointOnCurve aProject( aPointToTest, aCurve );
       aSecondParam = aProject.LowerDistanceParameter();
     }
 
-    GeomAdaptor_Curve anAdap( aBSpline.Curve() );
+    GeomAdaptor_Curve anAdap( aCurve );
     
     aResDistance = GCPnts_AbscissaPoint::Length( anAdap, aFirstParam, aSecondParam );
   }
@@ -609,7 +1076,7 @@ void HYDROData_PolylineXY::AddSection( const TCollection_AsciiString& theSectNam
   aTypesList->Append( theSectionType );
   aClosuresList->Append( theIsClosed );
 
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 TCollection_AsciiString HYDROData_PolylineXY::GetSectionName( const int theSectionIndex ) const
@@ -653,8 +1120,6 @@ void HYDROData_PolylineXY::SetSectionName( const int                      theSec
   TDataStd_ListIteratorOfListOfExtendedString aNamesIter( anOldNamesList );
   for ( int i = 0; aNamesIter.More(); aNamesIter.Next(), ++i )
     aNamesList->Append( i == theSectionIndex ? aNewSectName : aNamesIter.Value() );
-
-  SetToUpdate( true );
 }
 
 HYDROData_PolylineXY::SectionType HYDROData_PolylineXY::GetSectionType( const int theSectionIndex ) const
@@ -692,7 +1157,7 @@ void HYDROData_PolylineXY::SetSectionType( const int         theSectionIndex,
   for ( int i = 0; aTypesIter.More(); aTypesIter.Next(), ++i )
     aTypesList->Append( i == theSectionIndex ? theSectionType : aTypesIter.Value() );
 
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 bool HYDROData_PolylineXY::IsClosedSection( const int theSectionIndex ) const
@@ -730,7 +1195,7 @@ void HYDROData_PolylineXY::SetSectionClosed( const int  theSectionIndex,
   for ( int i = 0; aClosuresIter.More(); aClosuresIter.Next(), ++i )
     aClosuresList->Append( i == theSectionIndex ? theIsClosed : (bool)aClosuresIter.Value() );
 
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiString>& theSectNames,
@@ -741,6 +1206,11 @@ void HYDROData_PolylineXY::GetSections( NCollection_Sequence<TCollection_AsciiSt
   theSectTypes.Clear();
   theSectClosures.Clear();
 
+  if( IsCustom() )
+  {
+    const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
+  }
+
   Handle(TDataStd_ExtStringList) aNamesList;
   Handle(TDataStd_IntegerList) aTypesList;
   Handle(TDataStd_BooleanList) aClosuresList;
@@ -812,14 +1282,14 @@ void HYDROData_PolylineXY::RemoveSection( const int theSectionIndex )
     removePointsLists( theSectionIndex );
   }
 
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 void HYDROData_PolylineXY::RemoveSections()
 {
   removeSectionsLists();
   removePointsLists();
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
@@ -865,7 +1335,7 @@ void HYDROData_PolylineXY::AddPoint( const int    theSectionIndex,
     }
   }
 
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
@@ -916,7 +1386,7 @@ void HYDROData_PolylineXY::SetPoint( const int    theSectionIndex,
     }
   }
 
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
 void HYDROData_PolylineXY::SetPoints( const int         theSectionIndex,
@@ -972,13 +1442,18 @@ void HYDROData_PolylineXY::RemovePoint( const int theSectionIndex,
     }
   }
 
-  SetToUpdate( true );
+  Changed( Geom_2d );
 }
 
-HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex ) const
+HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theSectionIndex, bool IsConvertToGlobal ) const
 {
   PointsList aResList;
 
+  if( IsCustom() )
+  {
+    const_cast<HYDROData_PolylineXY*>( this )->Interpolate();
+  }
   Handle(TDataStd_RealList) aListX, aListY;
   getPointsLists( theSectionIndex, aListX, aListY, false );
   if ( aListX.IsNull() || aListY.IsNull() || aListX->IsEmpty() )
@@ -986,9 +1461,12 @@ HYDROData_PolylineXY::PointsList HYDROData_PolylineXY::GetPoints( const int theS
 
   TColStd_ListIteratorOfListOfReal anIterX( aListX->List() );
   TColStd_ListIteratorOfListOfReal anIterY( aListY->List() );
+  Handle(HYDROData_Document) aDoc = HYDROData_Document::Document( myLab );
   for ( ; anIterX.More() && anIterY.More(); anIterX.Next(), anIterY.Next() )
   {
     Point aPoint( anIterX.Value(), anIterY.Value() );
+    if (IsConvertToGlobal)
+      aDoc->Transform( aPoint, false );
     aResList.Append( aPoint );
   }
 
@@ -1029,4 +1507,78 @@ QPainterPath HYDROData_PolylineXY::GetPainterPath() const
   return aPath;
 }
 
+void HYDROData_PolylineXY::UpdateLocalCS( double theDx, double theDy )
+{
+  NCollection_Sequence<TCollection_AsciiString>           aSectNames;
+  NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
+  NCollection_Sequence<bool>                              aSectClosures;
+  GetSections( aSectNames, aSectTypes, aSectClosures );
+
+  gp_XY aDelta( theDx, theDy );
+  for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ )
+  {
+    PointsList aPoints = GetPoints( i );
+    for( int j = 1, n = aPoints.Size(); j <= n; ++j )
+    {
+      Point& aPoint = aPoints.ChangeValue( j );
+      aPoint += aDelta;
+    }
+    SetPoints( i, aPoints );
+  }
+  Changed( Geom_2d );
+}
+
+void HYDROData_PolylineXY::Transform( const QTransform& theTrsf )
+{
+  NCollection_Sequence<TCollection_AsciiString>           aSectNames;
+  NCollection_Sequence<HYDROData_PolylineXY::SectionType> aSectTypes;
+  NCollection_Sequence<bool>                              aSectClosures;
+  GetSections( aSectNames, aSectTypes, aSectClosures );
+
+  for ( int i = 0, aNbSects = aSectNames.Size(); i < aNbSects; i++ ) {
+    PointsList aPoints = GetPoints( i );
+    for( int j = 1, n = aPoints.Size(); j <= n; ++j ) {
+      Point& aPoint = aPoints.ChangeValue( j );
+
+      QPointF aTrsfPoint = theTrsf.map( QPointF( aPoint.X(), aPoint.Y() ) );
+
+      aPoint.SetX( aTrsfPoint.x() );
+      aPoint.SetY( aTrsfPoint.y() );
+    }
+    SetPoints( i, aPoints );
+  }
+
+  Update();
+}
+
+bool HYDROData_PolylineXY::IsCustom() const
+{
+  if( myIsInCustomFlag )
+    return false;
+
+  bool isNull = GetShape().IsNull();
+  int aNbPoints = 0;
 
+  HYDROData_PolylineXY* aThat = const_cast<HYDROData_PolylineXY*>( this );
+  aThat->myIsInCustomFlag = true;
+  for( int i=0, n=NbSections(); i<n; i++ )
+    aNbPoints += NbPoints( i );
+  aThat->myIsInCustomFlag = false;
+
+  return !isNull && aNbPoints == 0;
+}
+
+bool HYDROData_PolylineXY::GetIsInCustomFlag() const
+{
+  return myIsInCustomFlag;
+}
+
+void HYDROData_PolylineXY::SetIsInCustomFlag( bool theValue )
+{
+  myIsInCustomFlag = theValue;
+}
+
+void HYDROData_PolylineXY::Interpolate()
+{
+  ImportShape( GetShape(), true, NULL );
+}