0029151: GCC 7.1 warnings "this statement may fall through" [-Wimplicit-fallthrough=]
[occt.git] / src / IntPatch / IntPatch_ImpPrmIntersection.cxx
old mode 100755 (executable)
new mode 100644 (file)
index f6b8393..180ec90
-// File:      IntPatch_ImpPrmIntersection.cxx
-// Created:   Thu May  7 08:47:45 1992
-// Author:    Jacques GOUSSARD
-// Copyright: Matra Datavision 1992
-
-#include <IntPatch_ImpPrmIntersection.ixx>
-
-#include <Standard_ConstructionError.hxx>
-#include <IntPatch_SequenceOfLine.hxx>
-#include <TColStd_Array1OfInteger.hxx>
-#include <IntSurf_PntOn2S.hxx>
-#include <IntSurf_LineOn2S.hxx>
-#include <IntSurf.hxx>
-
-#include <Adaptor2d_HCurve2d.hxx>
-#include <IntSurf_PathPoint.hxx>
-#include <IntSurf_SequenceOfPathPoint.hxx>
-#include <IntPatch_TheIWalking.hxx>
+// Created on: 1992-05-07
+// Created by: Jacques GOUSSARD
+// Copyright (c) 1992-1999 Matra Datavision
+// Copyright (c) 1999-2014 OPEN CASCADE SAS
+//
+// This file is part of Open CASCADE Technology software library.
+//
+// This library is free software; you can redistribute it and/or modify it under
+// the terms of the GNU Lesser General Public License version 2.1 as published
+// by the Free Software Foundation, with special exception defined in the file
+// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
+// distribution for complete text of the license and disclaimer of any warranty.
+//
+// Alternatively, this file may be used under the terms of Open CASCADE
+// commercial license or contractual agreement.
+
+#include <IntPatch_ImpPrmIntersection.hxx>
+
+#include <Adaptor3d_HSurface.hxx>
+#include <Adaptor3d_TopolTool.hxx>
+#include <ElCLib.hxx>
+#include <IntPatch_ArcFunction.hxx>
+#include <IntPatch_PointLine.hxx>
+#include <IntPatch_RLine.hxx>
+#include <IntPatch_RstInt.hxx>
+#include <IntPatch_SpecialPoints.hxx>
 #include <IntPatch_TheIWLineOfTheIWalking.hxx>
-#include <IntPatch_ThePathPointOfTheSOnBounds.hxx>
-#include <IntPatch_TheSegmentOfTheSOnBounds.hxx>
+#include <IntPatch_TheIWalking.hxx>
 #include <IntPatch_TheSurfFunction.hxx>
-#include <IntPatch_RLine.hxx>
 #include <IntPatch_WLine.hxx>
-#include <IntPatch_ArcFunction.hxx>
-#include <IntPatch_RstInt.hxx>
+#include <IntSurf.hxx>
+#include <IntSurf_Quadric.hxx>
+#include <IntSurf_QuadricTool.hxx>
+#include <IntSurf_SequenceOfPathPoint.hxx>
+#include <TopAbs_Orientation.hxx>
+#include <TopTrans_CurveTransition.hxx>
+#include <math_Matrix.hxx>
+#include <math_Vector.hxx>
+
+#ifndef OCCT_DEBUG
+#define No_Standard_RangeError
+#define No_Standard_OutOfRange
+#endif
+
+static Standard_Boolean DecomposeResult(const Handle(IntPatch_PointLine)& theLine,
+                                        const Standard_Boolean       IsReversed,
+                                        const IntSurf_Quadric&       theQuad,
+                                        const Handle(Adaptor3d_TopolTool)& thePDomain,
+                                        const Handle(Adaptor3d_HSurface)&  theQSurf,
+                                        const Handle(Adaptor3d_HSurface)&  theOtherSurf,
+                                        const Standard_Real                theArcTol,
+                                        const Standard_Real                theTolTang,
+                                        IntPatch_SequenceOfLine&           theLines);
+static 
+  void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
+  IntSurf_SequenceOfPathPoint& seqpdep,
+  const Handle(Adaptor3d_TopolTool)& Domain,
+  IntPatch_TheSurfFunction& Func,
+  const Handle(Adaptor3d_HSurface)& PSurf,
+  TColStd_Array1OfInteger& Destination);
+static 
+  void Recadre(const Standard_Boolean ,
+  GeomAbs_SurfaceType typeS1,
+  GeomAbs_SurfaceType typeS2,
+  IntPatch_Point&  pt,
+  const Handle(IntPatch_TheIWLineOfTheIWalking)& iwline,
+  Standard_Integer Param,
+  Standard_Real U1,
+  Standard_Real V1,
+  Standard_Real U2,
+  Standard_Real V2);
+
+static 
+  Standard_Boolean IsCoincide(IntPatch_TheSurfFunction& theFunc,
+                              const Handle(IntPatch_PointLine)& theLine,
+                              const Handle(Adaptor2d_HCurve2d)& theArc,
+                              const Standard_Boolean isTheSurface1Using,
+                              const Standard_Real theToler3D,
+                              const Standard_Real theToler2D,
+                              const Standard_Real thePeriod);
 
 //=======================================================================
-//function : IntPatch_ImpPrmIntersection
+//function : IsSeamOrPole
 //purpose  : 
 //=======================================================================
+static IntPatch_SpecPntType IsSeamOrPole(const Handle(Adaptor3d_HSurface)& theQSurf,
+                                         const Handle(IntSurf_LineOn2S)& theLine,
+                                         const Standard_Boolean IsReversed,
+                                         const Standard_Integer theRefIndex,
+                                         const Standard_Real theDeltaMax)
+{
+  if((theRefIndex < 1) || (theRefIndex >= theLine->NbPoints()))
+    return IntPatch_SPntNone;
+
+  //Parameters on Quadric and on parametric for reference point
+  Standard_Real aUQRef, aVQRef, aUPRef, aVPRef;
+  Standard_Real aUQNext, aVQNext, aUPNext, aVPNext;
 
+  if(IsReversed)
+  {
+    theLine->Value(theRefIndex).Parameters  (aUPRef, aVPRef, aUQRef, aVQRef);
+    theLine->Value(theRefIndex+1).Parameters(aUPNext, aVPNext, aUQNext, aVQNext);
+  }
+  else
+  {
+    theLine->Value(theRefIndex).Parameters  (aUQRef, aVQRef, aUPRef, aVPRef);
+    theLine->Value(theRefIndex+1).Parameters(aUQNext, aVQNext, aUPNext, aVPNext);
+  }
+
+  const GeomAbs_SurfaceType aType = theQSurf->GetType();
+
+  const Standard_Real aDeltaU = Abs(aUQRef - aUQNext);
+
+  if((aType != GeomAbs_Torus) && (aDeltaU < theDeltaMax))
+    return IntPatch_SPntNone;
+
+  switch(aType)
+  {
+  case GeomAbs_Cylinder:
+    return IntPatch_SPntSeamU;
+
+  case GeomAbs_Torus:
+    {
+      const Standard_Real aDeltaV = Abs(aVQRef - aVQNext);
+
+      if((aDeltaU >= theDeltaMax) && (aDeltaV >= theDeltaMax))
+        return IntPatch_SPntSeamUV;
+
+      if(aDeltaU >= theDeltaMax)
+        return IntPatch_SPntSeamU;
+
+      if(aDeltaV >= theDeltaMax)
+        return IntPatch_SPntSeamV;
+    }
+
+    break;
+  case GeomAbs_Sphere:
+  case GeomAbs_Cone:
+    return IntPatch_SPntPoleSeamU;
+  default:
+    break;
+  }
+
+  return IntPatch_SPntNone;
+}
+
+//=======================================================================
+//function : IntPatch_ImpPrmIntersection
+//purpose  : 
+//=======================================================================
 IntPatch_ImpPrmIntersection::IntPatch_ImpPrmIntersection ()
- : done(Standard_False),
-   empt(Standard_False),
-   myIsStartPnt(Standard_False),
-   myUStart(0.0),
-   myVStart(0.0)
 : done(Standard_False),
+  empt(Standard_False),
+  myIsStartPnt(Standard_False),
+  myUStart(0.0),
+  myVStart(0.0)
 { }
 
 
@@ -45,19 +163,19 @@ IntPatch_ImpPrmIntersection::IntPatch_ImpPrmIntersection ()
 //=======================================================================
 
 IntPatch_ImpPrmIntersection::IntPatch_ImpPrmIntersection
-       (const Handle(Adaptor3d_HSurface)&    Surf1,
-        const Handle(Adaptor3d_TopolTool)&   D1,
-        const Handle(Adaptor3d_HSurface)&    Surf2,
-        const Handle(Adaptor3d_TopolTool)&   D2,
-        const Standard_Real    TolArc,
-        const Standard_Real    TolTang,
-        const Standard_Real    Fleche,
-        const Standard_Real    Pas)
- : done(Standard_False),
-   empt(Standard_False),
-   myIsStartPnt(Standard_False),
-   myUStart(0.0),
-   myVStart(0.0)
+  (const Handle(Adaptor3d_HSurface)&    Surf1,
+  const Handle(Adaptor3d_TopolTool)&   D1,
+  const Handle(Adaptor3d_HSurface)&    Surf2,
+  const Handle(Adaptor3d_TopolTool)&   D2,
+  const Standard_Real    TolArc,
+  const Standard_Real    TolTang,
+  const Standard_Real    Fleche,
+  const Standard_Real    Pas)
 : done(Standard_False),
+  empt(Standard_False),
+  myIsStartPnt(Standard_False),
+  myUStart(0.0),
+  myVStart(0.0)
 {
   Perform(Surf1,D1,Surf2,D2,TolArc,TolTang,Fleche,Pas);
 }
@@ -69,67 +187,22 @@ IntPatch_ImpPrmIntersection::IntPatch_ImpPrmIntersection
 //=======================================================================
 
 void IntPatch_ImpPrmIntersection::SetStartPoint(const Standard_Real U,
-                                                const Standard_Real V)
+  const Standard_Real V)
 {
   myIsStartPnt = Standard_True;
   myUStart = U; myVStart = V;
 }
 
-#ifndef DEB
-#define No_Standard_RangeError
-#define No_Standard_OutOfRange
-#endif
-
-
-#include <math_Vector.hxx>
-#include <math_Matrix.hxx>
-#include <TopTrans_CurveTransition.hxx>
-#include <TopAbs_State.hxx>
-#include <TopAbs_Orientation.hxx>
-#include <TColStd_Array1OfInteger.hxx>
-#include <TColStd_Array1OfReal.hxx>
-
-#include <IntSurf_SequenceOfInteriorPoint.hxx>
-#include <IntSurf_QuadricTool.hxx>
-#include <GeomAbs_SurfaceType.hxx>
-
-
-static void DecomposeResult(Handle(IntPatch_Line)&   Line,
-                           Standard_Boolean         IsReversed,
-                           IntSurf_Quadric&         Quad,
-                           Handle(Adaptor3d_TopolTool)&    PDomain,
-                           Handle(Adaptor3d_HSurface)&              QSurf,
-                           Standard_Real            TolArc,
-                           IntPatch_SequenceOfLine& Lines);
-static 
-  void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
-                       IntSurf_SequenceOfPathPoint& seqpdep,
-                       const Handle(Adaptor3d_TopolTool)& Domain,
-                       IntPatch_TheSurfFunction& Func,
-                       const Handle(Adaptor3d_HSurface)& PSurf,
-                       TColStd_Array1OfInteger& Destination);
-static 
-  void Recadre(const Standard_Boolean ,
-              GeomAbs_SurfaceType typeS1,
-              GeomAbs_SurfaceType typeS2,
-              IntPatch_Point&  pt,
-              const Handle(IntPatch_TheIWLineOfTheIWalking)& iwline,
-              Standard_Integer Param,
-              Standard_Real U1,
-              Standard_Real V1,
-              Standard_Real U2,
-              Standard_Real V2);
-
 //=======================================================================
 //function : ComputeTangency
 //purpose  : 
 //=======================================================================
 void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
-                     IntSurf_SequenceOfPathPoint& seqpdep,
-                     const Handle(Adaptor3d_TopolTool)& Domain,
-                     IntPatch_TheSurfFunction& Func,
-                     const Handle(Adaptor3d_HSurface)& PSurf,
-                     TColStd_Array1OfInteger& Destination)
+  IntSurf_SequenceOfPathPoint& seqpdep,
+  const Handle(Adaptor3d_TopolTool)& Domain,
+  IntPatch_TheSurfFunction& Func,
+  const Handle(Adaptor3d_HSurface)& PSurf,
+  TColStd_Array1OfInteger& Destination)
 {
   Standard_Integer i,k, NbPoints, seqlength;
   Standard_Real theparam,test;
@@ -147,9 +220,10 @@ void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
   gp_Pnt2d p2d;
   gp_Vec2d d2d;
   //
-  static math_Vector X(1, 2);
-  static math_Vector F(1, 1);
-  static math_Matrix D(1, 1, 1, 2); 
+  double aX[2], aF[1], aD[1][2];
+  math_Vector X(aX, 1, 2);
+  math_Vector F(aF, 1, 1);
+  math_Matrix D(aD, 1, 1, 1, 2); 
   //
   seqlength = 0;
   NbPoints = solrst.NbPoints();
@@ -160,174 +234,174 @@ void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
       theparam = PStart.Parameter();
       arcorien = Domain->Orientation(thearc);
       ispassing = (arcorien == TopAbs_INTERNAL || 
-                  arcorien == TopAbs_EXTERNAL);
-      
+        arcorien == TopAbs_EXTERNAL);
+
       thearc->D0(theparam,p2d);
       X(1) = p2d.X(); 
       X(2) = p2d.Y();
       PPoint.SetValue(PStart.Value(),X(1),X(2));
-      
+
       Func.Values(X,F,D);
       if (Func.IsTangent()) {
-       PPoint.SetTangency(Standard_True);
+        PPoint.SetTangency(Standard_True);
         Destination(i) = seqlength+1;
-       if (!PStart.IsNew()) {
-         vtx = PStart.Vertex();
-         for (k=i+1; k<=NbPoints; k++) {
-           if (Destination(k) ==0) {
-             PStart = solrst.Point(k);
-             if (!PStart.IsNew()) {
-               vtxbis = PStart.Vertex();
-               if (Domain->Identical(vtx,vtxbis)) {
-                 thearc   = PStart.Arc();
-                 theparam = PStart.Parameter();
-                 arcorien = Domain->Orientation(thearc);
-                 ispassing = ispassing && (arcorien == TopAbs_INTERNAL ||
-                                           arcorien == TopAbs_EXTERNAL);
-                 
-                 thearc->D0(theparam,p2d);
-                 PPoint.AddUV(p2d.X(),p2d.Y());
-                 Destination(k) = seqlength+1;
-               }
-             }
-           }
-         }
-       }
-       PPoint.SetPassing(ispassing);
-       seqpdep.Append(PPoint);
-       seqlength++;
+        if (!PStart.IsNew()) {
+          vtx = PStart.Vertex();
+          for (k=i+1; k<=NbPoints; k++) {
+            if (Destination(k) ==0) {
+              PStart = solrst.Point(k);
+              if (!PStart.IsNew()) {
+                vtxbis = PStart.Vertex();
+                if (Domain->Identical(vtx,vtxbis)) {
+                  thearc   = PStart.Arc();
+                  theparam = PStart.Parameter();
+                  arcorien = Domain->Orientation(thearc);
+                  ispassing = ispassing && (arcorien == TopAbs_INTERNAL ||
+                    arcorien == TopAbs_EXTERNAL);
+
+                  thearc->D0(theparam,p2d);
+                  PPoint.AddUV(p2d.X(),p2d.Y());
+                  Destination(k) = seqlength+1;
+                }
+              }
+            }
+          }
+        }
+        PPoint.SetPassing(ispassing);
+        seqpdep.Append(PPoint);
+        seqlength++;
       }
       else { // on a un point de depart potentiel
 
-       vectg = Func.Direction3d();
-       dirtg = Func.Direction2d();
-      
-       PSurf->D1(X(1),X(2),ptbid,d1u,d1v);
-       thearc->D1(theparam,p2d,d2d);
-       v2.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
-       v1 = d1u.Crossed(d1v);
-
-       test = vectg.Dot(v1.Crossed(v2));
-       if (PStart.IsNew()) {
-         if ((test < 0. && arcorien == TopAbs_FORWARD) ||
-             (test > 0. && arcorien == TopAbs_REVERSED)) {
-           vectg.Reverse();
-           dirtg.Reverse();
-         }
-         PPoint.SetDirections(vectg,dirtg);
-         PPoint.SetPassing(ispassing);
+        vectg = Func.Direction3d();
+        dirtg = Func.Direction2d();
+
+        PSurf->D1(X(1),X(2),ptbid,d1u,d1v);
+        thearc->D1(theparam,p2d,d2d);
+        v2.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
+        v1 = d1u.Crossed(d1v);
+
+        test = vectg.Dot(v1.Crossed(v2));
+        if (PStart.IsNew()) {
+          if ((test < 0. && arcorien == TopAbs_FORWARD) ||
+            (test > 0. && arcorien == TopAbs_REVERSED)) {
+              vectg.Reverse();
+              dirtg.Reverse();
+          }
+          PPoint.SetDirections(vectg,dirtg);
+          PPoint.SetPassing(ispassing);
           Destination(i) = seqlength+1;
-         seqpdep.Append(PPoint);
-         seqlength++;
-       }
-       else { // traiter la transition complexe
-         gp_Dir bidnorm(1.,1.,1.);
-         Standard_Real tole = 1.e-8;
-         TopAbs_Orientation LocTrans;
-         TopTrans_CurveTransition comptrans;
-         comptrans.Reset(vectg,bidnorm,0.);
-         if (arcorien == TopAbs_FORWARD || 
-             arcorien == TopAbs_REVERSED) {
-           // pour essai
-
-           vtx = PStart.Vertex();
-           vtxorien = Domain->Orientation(vtx);
-           if (Abs(test) <= tole) {
-             LocTrans = TopAbs_EXTERNAL; // et pourquoi pas INTERNAL
-           }
-           else {
-             if ((test > 0.)&& arcorien == TopAbs_FORWARD ||
-                 (test < 0.)&& arcorien == TopAbs_REVERSED){
-               LocTrans = TopAbs_FORWARD;
-             }
-             else {
-               LocTrans = TopAbs_REVERSED;
-             }
-             if (arcorien == TopAbs_REVERSED) {v2.Reverse();}
-           }
-
-           comptrans.Compare(tole,v2,bidnorm,0.,LocTrans,vtxorien);
-         }
+          seqpdep.Append(PPoint);
+          seqlength++;
+        }
+        else { // traiter la transition complexe
+          gp_Dir bidnorm(1.,1.,1.);
+          Standard_Real tole = 1.e-8;
+          TopAbs_Orientation LocTrans;
+          TopTrans_CurveTransition comptrans;
+          comptrans.Reset(vectg,bidnorm,0.);
+          if (arcorien == TopAbs_FORWARD || 
+            arcorien == TopAbs_REVERSED) {
+              // pour essai
+
+              vtx = PStart.Vertex();
+              vtxorien = Domain->Orientation(vtx);
+              if (Abs(test) <= tole) {
+                LocTrans = TopAbs_EXTERNAL; // et pourquoi pas INTERNAL
+              }
+              else {
+                if (((test > 0.)&& arcorien == TopAbs_FORWARD) ||
+                  ((test < 0.)&& arcorien == TopAbs_REVERSED)){
+                    LocTrans = TopAbs_FORWARD;
+                }
+                else {
+                  LocTrans = TopAbs_REVERSED;
+                }
+                if (arcorien == TopAbs_REVERSED) {v2.Reverse();}
+              }
+
+              comptrans.Compare(tole,v2,bidnorm,0.,LocTrans,vtxorien);
+          }
           Destination(i) = seqlength+1;
-         for (k= i+1; k<=NbPoints; k++) {
-           if (Destination(k) == 0) {
-             PStart = solrst.Point(k);
-             if (!PStart.IsNew()) {
-               vtxbis = PStart.Vertex();
-               if (Domain->Identical(vtx,vtxbis)) {
-                 thearc   = PStart.Arc();
-                 theparam = PStart.Parameter();
-                 arcorien = Domain->Orientation(thearc);
-                 
-                 PPoint.AddUV(X(1),X(2));
-
-                 thearc->D1(theparam,p2d,d2d);
-                 PPoint.AddUV(p2d.X(),p2d.Y());
-
-                 if (arcorien == TopAbs_FORWARD || 
-                     arcorien == TopAbs_REVERSED) {
-                   ispassing = Standard_False;
-                   v2.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
-
-                   test = vectg.Dot(v1.Crossed(v2));
-                   vtxorien = Domain->Orientation(PStart.Vertex());
-                   if (Abs(test) <= tole) {
-                     LocTrans = TopAbs_EXTERNAL; // et pourquoi pas INTERNAL
-                   }
-                   else {
-                     if ((test > 0.)&& arcorien == TopAbs_FORWARD ||
-                         (test < 0.)&& arcorien == TopAbs_REVERSED){
-                       LocTrans = TopAbs_FORWARD;
-                     }
-                     else {
-                       LocTrans = TopAbs_REVERSED;
-                     }
-                     if (arcorien == TopAbs_REVERSED) {v2.Reverse();}
-                   }
-
-                   comptrans.Compare(tole,v2,bidnorm,0.,LocTrans,vtxorien);
-                 }
-                 Destination(k) = seqlength+1;
-               }
-             }
-           }
-         }
+          for (k= i+1; k<=NbPoints; k++) {
+            if (Destination(k) == 0) {
+              PStart = solrst.Point(k);
+              if (!PStart.IsNew()) {
+                vtxbis = PStart.Vertex();
+                if (Domain->Identical(vtx,vtxbis)) {
+                  thearc   = PStart.Arc();
+                  theparam = PStart.Parameter();
+                  arcorien = Domain->Orientation(thearc);
+
+                  PPoint.AddUV(X(1),X(2));
+
+                  thearc->D1(theparam,p2d,d2d);
+                  PPoint.AddUV(p2d.X(),p2d.Y());
+
+                  if (arcorien == TopAbs_FORWARD || 
+                    arcorien == TopAbs_REVERSED) {
+                      ispassing = Standard_False;
+                      v2.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
+
+                      test = vectg.Dot(v1.Crossed(v2));
+                      vtxorien = Domain->Orientation(PStart.Vertex());
+                      if (Abs(test) <= tole) {
+                        LocTrans = TopAbs_EXTERNAL; // et pourquoi pas INTERNAL
+                      }
+                      else {
+                        if (((test > 0.)&& arcorien == TopAbs_FORWARD) ||
+                          ((test < 0.)&& arcorien == TopAbs_REVERSED)){
+                            LocTrans = TopAbs_FORWARD;
+                        }
+                        else {
+                          LocTrans = TopAbs_REVERSED;
+                        }
+                        if (arcorien == TopAbs_REVERSED) {v2.Reverse();}
+                      }
+
+                      comptrans.Compare(tole,v2,bidnorm,0.,LocTrans,vtxorien);
+                  }
+                  Destination(k) = seqlength+1;
+                }
+              }
+            }
+          }
           fairpt = Standard_True;
-         if (!ispassing) {
-           TopAbs_State Before = comptrans.StateBefore();
-           TopAbs_State After  = comptrans.StateAfter();
-           if ((Before == TopAbs_UNKNOWN)||(After == TopAbs_UNKNOWN)) {
-             fairpt = Standard_False;
-           }
-           else if (Before == TopAbs_IN) {
-             if (After == TopAbs_IN) {
-               ispassing = Standard_True;
-             }
-             else {
-               vectg.Reverse();
-               dirtg.Reverse();
-             }
-           }
-           else {
-             if (After !=TopAbs_IN) {
-               fairpt = Standard_False;
-             }
-           }
-         }
-         if (fairpt) {
-           PPoint.SetDirections(vectg,dirtg);
-           PPoint.SetPassing(ispassing);
-           seqpdep.Append(PPoint);
-           seqlength++;
-         }
-         else { // il faut remettre en "ordre" si on ne garde pas le point.
-           for (k=i; k <=NbPoints ; k++) {
-             if (Destination(k)==seqlength + 1) {
-               Destination(k) = -Destination(k);
-             }
-           }
-         }
-       }
+          if (!ispassing) {
+            TopAbs_State Before = comptrans.StateBefore();
+            TopAbs_State After  = comptrans.StateAfter();
+            if ((Before == TopAbs_UNKNOWN)||(After == TopAbs_UNKNOWN)) {
+              fairpt = Standard_False;
+            }
+            else if (Before == TopAbs_IN) {
+              if (After == TopAbs_IN) {
+                ispassing = Standard_True;
+              }
+              else {
+                vectg.Reverse();
+                dirtg.Reverse();
+              }
+            }
+            else {
+              if (After !=TopAbs_IN) {
+                fairpt = Standard_False;
+              }
+            }
+          }
+          if (fairpt) {
+            PPoint.SetDirections(vectg,dirtg);
+            PPoint.SetPassing(ispassing);
+            seqpdep.Append(PPoint);
+            seqlength++;
+          }
+          else { // il faut remettre en "ordre" si on ne garde pas le point.
+            for (k=i; k <=NbPoints ; k++) {
+              if (Destination(k)==seqlength + 1) {
+                Destination(k) = -Destination(k);
+              }
+            }
+          }
+        }
       }
     }
   }
@@ -337,39 +411,45 @@ void ComputeTangency (const IntPatch_TheSOnBounds& solrst,
 //purpose  : 
 //=======================================================================
 void Recadre(const Standard_Boolean ,
-             GeomAbs_SurfaceType typeS1,
-             GeomAbs_SurfaceType typeS2,
-             IntPatch_Point&  pt,
-             const Handle(IntPatch_TheIWLineOfTheIWalking)& iwline,
-             Standard_Integer Param,
-             Standard_Real U1,
-             Standard_Real V1,
-             Standard_Real U2,
-             Standard_Real V2)
+  GeomAbs_SurfaceType typeS1,
+  GeomAbs_SurfaceType typeS2,
+  IntPatch_Point&  pt,
+  const Handle(IntPatch_TheIWLineOfTheIWalking)& iwline,
+  Standard_Integer Param,
+  Standard_Real U1,
+  Standard_Real V1,
+  Standard_Real U2,
+  Standard_Real V2)
 {
   Standard_Real U1p,V1p,U2p,V2p;
   iwline->Line()->Value(Param).Parameters(U1p,V1p,U2p,V2p);
   switch(typeS1)
   {
-    case GeomAbs_Torus:
-      while(V1<(V1p-1.5*M_PI)) V1+=M_PI+M_PI;
-      while(V1>(V1p+1.5*M_PI)) V1-=M_PI+M_PI;
-    case GeomAbs_Cylinder:
-    case GeomAbs_Cone:
-    case GeomAbs_Sphere:
-      while(U1<(U1p-1.5*M_PI)) U1+=M_PI+M_PI;
-      while(U1>(U1p+1.5*M_PI)) U1-=M_PI+M_PI;
+  case GeomAbs_Torus:
+    while(V1<(V1p-1.5*M_PI)) V1+=M_PI+M_PI;
+    while(V1>(V1p+1.5*M_PI)) V1-=M_PI+M_PI;
+    Standard_FALLTHROUGH
+  case GeomAbs_Cylinder:
+  case GeomAbs_Cone:
+  case GeomAbs_Sphere:
+    while(U1<(U1p-1.5*M_PI)) U1+=M_PI+M_PI;
+    while(U1>(U1p+1.5*M_PI)) U1-=M_PI+M_PI;
+  default:
+    break;
   }
   switch(typeS2)
   { 
-    case GeomAbs_Torus:
-      while(V2<(V2p-1.5*M_PI)) V2+=M_PI+M_PI;
-      while(V2>(V2p+1.5*M_PI)) V2-=M_PI+M_PI;
-    case GeomAbs_Cylinder:
-    case GeomAbs_Cone:
-    case GeomAbs_Sphere:
-      while(U2<(U2p-1.5*M_PI)) U2+=M_PI+M_PI;
-      while(U2>(U2p+1.5*M_PI)) U2-=M_PI+M_PI;
+  case GeomAbs_Torus:
+    while(V2<(V2p-1.5*M_PI)) V2+=M_PI+M_PI;
+    while(V2>(V2p+1.5*M_PI)) V2-=M_PI+M_PI;
+    Standard_FALLTHROUGH
+  case GeomAbs_Cylinder:
+  case GeomAbs_Cone:
+  case GeomAbs_Sphere:
+    while(U2<(U2p-1.5*M_PI)) U2+=M_PI+M_PI;
+    while(U2>(U2p+1.5*M_PI)) U2-=M_PI+M_PI;
+  default:
+    break;
   }
   pt.SetParameters(U1,V1,U2,V2);
 }
@@ -379,19 +459,19 @@ void Recadre(const Standard_Boolean ,
 //purpose  : 
 //=======================================================================
 void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Surf1,
-                                          const Handle(Adaptor3d_TopolTool)& D1,
-                                          const Handle(Adaptor3d_HSurface)& Surf2,
-                                          const Handle(Adaptor3d_TopolTool)& D2,
-                                          const Standard_Real TolArc,
-                                          const Standard_Real TolTang,
-                                          const Standard_Real Fleche,
-                                          const Standard_Real Pas)
+  const Handle(Adaptor3d_TopolTool)& D1,
+  const Handle(Adaptor3d_HSurface)& Surf2,
+  const Handle(Adaptor3d_TopolTool)& D2,
+  const Standard_Real TolArc,
+  const Standard_Real TolTang,
+  const Standard_Real Fleche,
+  const Standard_Real Pas)
 {
   Standard_Boolean reversed, procf, procl, dofirst, dolast;
-  Standard_Integer indfirst, indlast, ind2, i,j,k, NbSegm;
+  Standard_Integer indfirst = 0, indlast = 0, ind2, NbSegm;
   Standard_Integer NbPointIns, NbPointRst, Nblines, Nbpts, NbPointDep;
   Standard_Real U1,V1,U2,V2,paramf,paraml,currentparam;
-  
+
   IntPatch_TheSegmentOfTheSOnBounds thesegm;
   IntSurf_PathPoint PPoint;
 
@@ -399,7 +479,7 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
   Handle(IntPatch_WLine) wline;
   IntPatch_ThePathPointOfTheSOnBounds PStart,PStartf,PStartl;
   IntPatch_Point ptdeb,ptfin,ptbis;
-  
+
   IntPatch_IType typ;
   IntSurf_Transition TLine,TArc;
   IntSurf_TypeTrans trans1,trans2;
@@ -407,10 +487,10 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
   gp_Vec tgline,tgrst,norm1,norm2,d1u,d1v;
   gp_Dir DirNormale;
   gp_Vec VecNormale;
-  
+
   gp_Pnt2d p2d;
   gp_Vec2d d2d;
-  
+
   Handle(Adaptor2d_HCurve2d) currentarc;
   GeomAbs_SurfaceType typeS1, typeS2;
   IntSurf_Quadric Quad;
@@ -419,7 +499,7 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
   //
   typeS1 = Surf1->GetType();
   typeS2 = Surf2->GetType();
-  
+
   paramf =0.;
   paraml =0.;
   trans1 = IntSurf_Undecided;
@@ -431,45 +511,49 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
   spnt.Clear();
   //
   reversed = Standard_False;
-  switch (typeS1) {
-    case GeomAbs_Plane:
-      Quad.SetValue(Surf1->Plane());
-      break;
+  switch (typeS1)
+  {
+  case GeomAbs_Plane:
+    Quad.SetValue(Surf1->Plane());
+    break;
 
-    case GeomAbs_Cylinder:
-      Quad.SetValue(Surf1->Cylinder());
-      break;
+  case GeomAbs_Cylinder:
+    Quad.SetValue(Surf1->Cylinder());
+    break;
 
-    case GeomAbs_Sphere:
-      Quad.SetValue(Surf1->Sphere());
-      break;
+  case GeomAbs_Sphere:
+    Quad.SetValue(Surf1->Sphere());
+    break;
 
-    case GeomAbs_Cone:
-      Quad.SetValue(Surf1->Cone());
-      break;
+  case GeomAbs_Cone:
+    Quad.SetValue(Surf1->Cone());
+    break;
 
-    default: {
+  default:
+    {
       reversed = Standard_True;
-      switch (typeS2) {
-        case GeomAbs_Plane:
-         Quad.SetValue(Surf2->Plane());
-         break;
-
-       case GeomAbs_Cylinder:
-         Quad.SetValue(Surf2->Cylinder());
-         break;
-         
-       case GeomAbs_Sphere:
-         Quad.SetValue(Surf2->Sphere());
-         break;
-
-       case GeomAbs_Cone:
-         Quad.SetValue(Surf2->Cone());
-         break;
-       default: {
-         Standard_ConstructionError::Raise();
-         break;
-       }
+      switch (typeS2)
+      {
+      case GeomAbs_Plane:
+        Quad.SetValue(Surf2->Plane());
+        break;
+
+      case GeomAbs_Cylinder:
+        Quad.SetValue(Surf2->Cylinder());
+        break;
+
+      case GeomAbs_Sphere:
+        Quad.SetValue(Surf2->Sphere());
+        break;
+
+      case GeomAbs_Cone:
+        Quad.SetValue(Surf2->Cone());
+        break;
+      default:
+        {
+          throw Standard_ConstructionError();
+          break;
+        }
       } 
     }
     break;
@@ -512,24 +596,67 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
     }
   }
   //
-  // Recherche des points interieurs
-  if (!reversed) {
-    if (myIsStartPnt)
-      solins.Perform(Func,Surf2,myUStart,myVStart);
+  Standard_Boolean SearchIns = Standard_True;
+  if(Quad.TypeQuadric() == GeomAbs_Plane && solrst.NbSegments() > 0)
+  {
+    //For such kind of cases it is possible that whole surface is on one side of plane,
+    //plane only touches surface and does not cross it,
+    //so no inner points exist.
+    SearchIns = Standard_False;
+    Handle(Adaptor3d_TopolTool) T;
+    if(reversed)
+    {
+      T = D1;
+    }
     else
-      solins.Perform(Func,Surf2,D2,TolTang);
+    {
+      T = D2;
+    }
+    Standard_Integer aNbSamples = 0;
+    aNbSamples = T->NbSamples();
+    gp_Pnt2d s2d;
+    gp_Pnt s3d;
+    Standard_Real aValf[1], aUVap[2];
+    math_Vector Valf(aValf,1,1), UVap(aUVap,1,2);
+    T->SamplePoint(1,s2d, s3d);
+    UVap(1)=s2d.X(); 
+    UVap(2)=s2d.Y();
+    Func.Value(UVap,Valf);
+    Standard_Real rvalf = Sign(1.,Valf(1));
+    for(Standard_Integer i = 2; i <= aNbSamples; ++i)
+    {
+      T->SamplePoint(i,s2d, s3d);
+      UVap(1)=s2d.X(); 
+      UVap(2)=s2d.Y();
+      Func.Value(UVap,Valf);
+      if(rvalf * Valf(1) < 0.)
+      {
+        SearchIns = Standard_True;
+        break;
+      }   
+    }
   }
-  else {
-    if (myIsStartPnt)
-      solins.Perform(Func,Surf1,myUStart,myVStart);
-    else
-      solins.Perform(Func,Surf1,D1,TolTang);
+  // Recherche des points interieurs
+  NbPointIns = 0;
+  if(SearchIns) {
+    if (!reversed) {
+      if (myIsStartPnt)
+        solins.Perform(Func,Surf2,myUStart,myVStart);
+      else
+        solins.Perform(Func,Surf2,D2,TolTang);
+    }
+    else {
+      if (myIsStartPnt)
+        solins.Perform(Func,Surf1,myUStart,myVStart);
+      else
+        solins.Perform(Func,Surf1,D1,TolTang);
+    }
+    NbPointIns = solins.NbPoints();
+    for (Standard_Integer i=1; i <= NbPointIns; i++) {
+      seqpins.Append(solins.Value(i));
+    }
   }
   //
-  NbPointIns = solins.NbPoints();
-  for (i=1; i <= NbPointIns; i++) {
-    seqpins.Append(solins.Value(i));
-  }
   NbPointDep=seqpdep.Length();
   //
   if (NbPointDep || NbPointIns) {
@@ -543,7 +670,7 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
     if(!iwalk.IsDone()) {
       return;
     }
-    
+
     Standard_Real Vmin, Vmax, TolV = 1.e-14;
     if (!reversed) { //Surf1 is quadric
       Vmin = Surf1->FirstVParameter();
@@ -555,328 +682,338 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
     }
     //
     Nblines = iwalk.NbLines();
-    for (j=1; j<=Nblines; j++) {
+    for (Standard_Integer j=1; j<=Nblines; j++) {
       const Handle(IntPatch_TheIWLineOfTheIWalking)&  iwline  = iwalk.Value(j);
       const Handle(IntSurf_LineOn2S)&                 thelin  = iwline->Line();
-      
+
       Nbpts = thelin->NbPoints();
       if(Nbpts>=2) { 
-       
-       tgline = iwline->TangentVector(k);      
-       if(k>=1 && k<=Nbpts) { } else { k=Nbpts>>1; } 
-       valpt = thelin->Value(k).Value();       
-       
-       if (!reversed) {
-         thelin->Value(k).ParametersOnS2(U2,V2);
-         norm1 = Quad.Normale(valpt);
-         Surf2->D1(U2,V2,ptbid,d1u,d1v);
-         norm2 = d1u.Crossed(d1v);
-       }
-       else {
-         thelin->Value(k).ParametersOnS1(U2,V2);
-         norm2 = Quad.Normale(valpt);
-         Surf1->D1(U2,V2,ptbid,d1u,d1v);
-         norm1 = d1u.Crossed(d1v);
-       }
-       if (tgline.DotCross(norm2,norm1) > 0.) {
-         trans1 = IntSurf_Out;
-         trans2 = IntSurf_In;
-       }
-       else {
-         trans1 = IntSurf_In;
-         trans2 = IntSurf_Out;
-       }
-
-       //
-       Standard_Real AnU1,AnU2,AnV2;
-
-       GeomAbs_SurfaceType typQuad = Quad.TypeQuadric();
-       Standard_Boolean arecadr=Standard_False;
-       valpt = thelin->Value(1).Value();
-       Quad.Parameters(valpt,AnU1,V1);
-
-       if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
-       if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
-
-       if(reversed) { 
-         thelin->SetUV(1,Standard_False,AnU1,V1); //-- on va lire u2,v2
-         thelin->Value(1).ParametersOnS1(AnU2,AnV2);
-       }
-       else { 
-         thelin->SetUV(1,Standard_True,AnU1,V1);  //-- on va lire u1,v1 
-         thelin->Value(1).ParametersOnS2(AnU2,AnV2);
-       }
-       
-       if(typQuad==GeomAbs_Cylinder || 
-          typQuad==GeomAbs_Cone || 
-          typQuad==GeomAbs_Sphere) { 
-         arecadr=Standard_True; 
-       } 
-       //
-       for (k=2; k<=Nbpts; ++k) {
-         valpt = thelin->Value(k).Value();
-         Quad.Parameters(valpt,U1,V1);
-         //
-         if((V1 < Vmin) && (Vmin-V1 < TolV)) {
-           V1 = Vmin;
-         }
-         if((V1 > Vmax) && (V1-Vmax < TolV)) {
-           V1 = Vmax;
-         }
-         //
-         if(arecadr) {
-           //modified by NIZNHY-PKV Fri Mar 28 15:06:01 2008f
-           Standard_Real aCf, aTwoPI;
-           //
-           aCf=0.;
-           aTwoPI=M_PI+M_PI;
-           if ((U1-AnU1) >  1.5*M_PI) { 
-             while ((U1-AnU1) > (1.5*M_PI+aCf*aTwoPI)) {
-               aCf=aCf+1.;
-             }
-             U1=U1-aCf*aTwoPI;
-           } 
-           //
-           else {
-             while ((U1-AnU1) < (-1.5*M_PI-aCf*aTwoPI)) {
-               aCf=aCf+1.;
-             }
-             U1=U1+aCf*aTwoPI;
-           }
-           // was:
-           //if ((U1-AnU1) >  1.5*M_PI) { 
-           //  U1-=M_PI+M_PI;
-           //}
-           //else if ((U1-AnU1) < -1.5*M_PI) { 
-           //  U1+=M_PI+M_PI; 
-           //}
-           //modified by NIZNHY-PKV Fri Mar 28 15:06:11 2008t
-         }
-         //
-         if(reversed) { 
-           thelin->SetUV(k,Standard_False,U1,V1);
-           
-           thelin->Value(k).ParametersOnS1(U2,V2);
-           switch(typeS1) { 
-           case GeomAbs_Cylinder:
-           case GeomAbs_Cone:
-           case GeomAbs_Sphere:
-           case GeomAbs_Torus:
-             while(U2<(AnU2-1.5*M_PI)) U2+=M_PI+M_PI;
-             while(U2>(AnU2+1.5*M_PI)) U2-=M_PI+M_PI;
-             break;
-           default: 
-             break;
-           }
-           if(typeS2==GeomAbs_Torus) { 
-             while(V2<(AnV2-1.5*M_PI)) V2+=M_PI+M_PI;
-             while(V2>(AnV2+1.5*M_PI)) V2-=M_PI+M_PI;
-           }
-           thelin->SetUV(k,Standard_True,U2,V2);
-         }
-         else { 
-           thelin->SetUV(k,Standard_True,U1,V1);
-
-           thelin->Value(k).ParametersOnS2(U2,V2);
-           switch(typeS2) { 
-           case GeomAbs_Cylinder:
-           case GeomAbs_Cone:
-           case GeomAbs_Sphere:
-           case GeomAbs_Torus:
-             while(U2<(AnU2-1.5*M_PI)) U2+=M_PI+M_PI;
-             while(U2>(AnU2+1.5*M_PI)) U2-=M_PI+M_PI;
-             break;
-           default: 
-             break;
-           }
-           if(typeS2==GeomAbs_Torus) { 
-             while(V2<(AnV2-1.5*M_PI)) V2+=M_PI+M_PI;
-             while(V2>(AnV2+1.5*M_PI)) V2-=M_PI+M_PI;
-           }
-           thelin->SetUV(k,Standard_False,U2,V2);
-
-         }
-
-         AnU1=U1;
-         AnU2=U2;
-         AnV2=V2;
-       }
-       // <-A
-       wline = new IntPatch_WLine(thelin,Standard_False,trans1,trans2);
-       
-       if (   iwline->HasFirstPoint() 
-           && iwline->IsTangentAtBegining() == Standard_False) {
-         indfirst = iwline->FirstPointIndex();
-         PPoint = seqpdep(indfirst);
-         tgline = PPoint.Direction3d();
-         Standard_Integer themult = PPoint.Multiplicity();
-         for (i=NbPointRst; i>=1; i--) {
-           if (Destination(i) == indfirst) {
-             if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
-               Quad.Parameters(PPoint.Value(),U1,V1);
-
-               if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
-               if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
-
-               PPoint.Parameters(themult,U2,V2);
-               Surf2->D1(U2,V2,ptbid,d1u,d1v); //-- @@@@
-             }
-             else {  //-- typeS1 != Pln && Cyl && Sph && Cone
-               Quad.Parameters(PPoint.Value(),U2,V2);
-
-               if((V2 < Vmin) && (Vmin-V2 < TolV)) V2 = Vmin;
-               if((V2 > Vmax) && (V2-Vmax < TolV)) V2 = Vmax;
-
-               PPoint.Parameters(themult,U1,V1);
-               Surf1->D1(U1,V1,ptbid,d1u,d1v); //-- @@@@
-             }
-
-             VecNormale = d1u.Crossed(d1v);                      
-             //-- Modif du 27 Septembre 94 (Recadrage des pts U,V) 
-             ptdeb.SetValue(PPoint.Value(),TolArc,Standard_False);
-             ptdeb.SetParameters(U1,V1,U2,V2);
-             ptdeb.SetParameter(1.);
-             
-             Recadre(reversed,typeS1,typeS2,ptdeb,iwline,1,U1,V1,U2,V2);
-             
-             currentarc = solrst.Point(i).Arc();
-             currentparam = solrst.Point(i).Parameter();
-             currentarc->D1(currentparam,p2d,d2d);
-             tgrst.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
-             
-             Standard_Real squaremagnitudeVecNormale = VecNormale.SquareMagnitude();
-             if(squaremagnitudeVecNormale > 1e-13) { 
-               DirNormale=VecNormale;
-               IntSurf::MakeTransition(tgline,tgrst,DirNormale,TLine,TArc);
-             }
-             else { 
-               TLine.SetValue(Standard_True,IntSurf_Undecided);
-               TArc.SetValue(Standard_True,IntSurf_Undecided);
-             }
-             
-             ptdeb.SetArc(reversed,currentarc,currentparam,TLine,TArc);
-             if (!solrst.Point(i).IsNew()) {
-               ptdeb.SetVertex(reversed,solrst.Point(i).Vertex());
-             }
-             wline->AddVertex(ptdeb);
-             if (themult == 0) {
-               wline->SetFirstPoint(wline->NbVertex());
-             }
-             
-             themult--;
-           }
-         }
-       }
-       else if (iwline->IsTangentAtBegining()) {
-         gp_Pnt psol = thelin->Value(1).Value();
-         thelin->Value(1).ParametersOnS1(U1,V1);
-         thelin->Value(1).ParametersOnS2(U2,V2);
-         ptdeb.SetValue(psol,TolArc,Standard_True);
-         ptdeb.SetParameters(U1,V1,U2,V2);
-         ptdeb.SetParameter(1.);
-         wline->AddVertex(ptdeb);
-         wline->SetFirstPoint(wline->NbVertex());
-       }
-       else { 
-         gp_Pnt psol = thelin->Value(1).Value();
-         thelin->Value(1).ParametersOnS1(U1,V1);
-         thelin->Value(1).ParametersOnS2(U2,V2);
-         ptdeb.SetValue(psol,TolArc,Standard_False);
-         ptdeb.SetParameters(U1,V1,U2,V2);
-         ptdeb.SetParameter(1.);
-         wline->AddVertex(ptdeb);
-         wline->SetFirstPoint(wline->NbVertex());
-       }
-       
-       
-       if (   iwline->HasLastPoint() 
-           && iwline->IsTangentAtEnd() == Standard_False) {
-         indlast = iwline->LastPointIndex();
-         PPoint = seqpdep(indlast);
-         tgline = PPoint.Direction3d().Reversed();
-         Standard_Integer themult = PPoint.Multiplicity();
-         for (i=NbPointRst; i >=1; i--) {
-           if (Destination(i) == indlast) {
-             if (!reversed) {
-               Quad.Parameters(PPoint.Value(),U1,V1);
-
-               if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
-               if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
-
-               PPoint.Parameters(themult,U2,V2);
-               Surf2->D1(U2,V2,ptbid,d1u,d1v); //-- @@@@
-               VecNormale = d1u.Crossed(d1v);                    //-- @@@@
-             }
-             else {
-               Quad.Parameters(PPoint.Value(),U2,V2);
-
-               if((V2 < Vmin) && (Vmin-V2 < TolV)) V2 = Vmin;
-               if((V2 > Vmax) && (V2-Vmax < TolV)) V2 = Vmax;
-
-               PPoint.Parameters(themult,U1,V1);
-               Surf1->D1(U1,V1,ptbid,d1u,d1v); //-- @@@@
-               VecNormale = d1u.Crossed(d1v);                    //-- @@@@
-             }
-             
-             ptfin.SetValue(PPoint.Value(),TolArc,Standard_False);
-             ptfin.SetParameters(U1,V1,U2,V2);
-             ptfin.SetParameter(Nbpts);
-             
-             Recadre(reversed,typeS1,typeS2,ptfin,iwline,Nbpts-1,U1,V1,U2,V2);
-             
-             currentarc = solrst.Point(i).Arc();
-             currentparam = solrst.Point(i).Parameter();
-             currentarc->D1(currentparam,p2d,d2d);
-             tgrst.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
-             
-
-             Standard_Real squaremagnitudeVecNormale = VecNormale.SquareMagnitude();
-             if(squaremagnitudeVecNormale > 1e-13) { 
-               DirNormale=VecNormale;
-               IntSurf::MakeTransition(tgline,tgrst,DirNormale,TLine,TArc);
-             }
-             else { 
-               TLine.SetValue(Standard_True,IntSurf_Undecided);
-               TArc.SetValue(Standard_True,IntSurf_Undecided);
-             }
-             
-             
-             ptfin.SetArc(reversed,currentarc,currentparam,TLine,TArc);
-             if (!solrst.Point(i).IsNew()) {
-               ptfin.SetVertex(reversed,solrst.Point(i).Vertex());
-             }
-             wline->AddVertex(ptfin);
-             if (themult == 0) {
-               wline->SetLastPoint(wline->NbVertex());
-             }
-             
-             themult--;
-           }
-         }
-       }
-       else if (iwline->IsTangentAtEnd()) {
-         gp_Pnt psol = thelin->Value(Nbpts).Value();
-         thelin->Value(Nbpts).ParametersOnS1(U1,V1);
-         thelin->Value(Nbpts).ParametersOnS2(U2,V2);
-         ptfin.SetValue(psol,TolArc,Standard_True);
-         ptfin.SetParameters(U1,V1,U2,V2);
-         ptfin.SetParameter(Nbpts);
-         wline->AddVertex(ptfin);
-         wline->SetLastPoint(wline->NbVertex());
-       }
-       else { 
-         gp_Pnt psol = thelin->Value(Nbpts).Value();
-         thelin->Value(Nbpts).ParametersOnS1(U1,V1);
-         thelin->Value(Nbpts).ParametersOnS2(U2,V2);
-         ptfin.SetValue(psol,TolArc,Standard_False);
-         ptfin.SetParameters(U1,V1,U2,V2);
-         ptfin.SetParameter(Nbpts);
-         wline->AddVertex(ptfin);
-         wline->SetLastPoint(wline->NbVertex());
-       }
-       //
-       // Il faut traiter les points de passage.
-       slin.Append(wline);
+        Standard_Integer k = 0;
+        tgline = iwline->TangentVector(k);     
+        if(k>=1 && k<=Nbpts) { } else { k=Nbpts>>1; } 
+        valpt = thelin->Value(k).Value();      
+
+        if (!reversed) {
+          thelin->Value(k).ParametersOnS2(U2,V2);
+          norm1 = Quad.Normale(valpt);
+          Surf2->D1(U2,V2,ptbid,d1u,d1v);
+          norm2 = d1u.Crossed(d1v);
+        }
+        else {
+          thelin->Value(k).ParametersOnS1(U2,V2);
+          norm2 = Quad.Normale(valpt);
+          Surf1->D1(U2,V2,ptbid,d1u,d1v);
+          norm1 = d1u.Crossed(d1v);
+        }
+        if (tgline.DotCross(norm2,norm1) > 0.) {
+          trans1 = IntSurf_Out;
+          trans2 = IntSurf_In;
+        }
+        else {
+          trans1 = IntSurf_In;
+          trans2 = IntSurf_Out;
+        }
+
+        //
+        Standard_Real AnU1,AnU2,AnV2;
+
+        GeomAbs_SurfaceType typQuad = Quad.TypeQuadric();
+        Standard_Boolean arecadr=Standard_False;
+        valpt = thelin->Value(1).Value();
+        Quad.Parameters(valpt,AnU1,V1);
+
+        if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
+        if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
+
+        if(reversed) { 
+          thelin->SetUV(1,Standard_False,AnU1,V1); //-- on va lire u2,v2
+          thelin->Value(1).ParametersOnS1(AnU2,AnV2);
+        }
+        else { 
+          thelin->SetUV(1,Standard_True,AnU1,V1);  //-- on va lire u1,v1 
+          thelin->Value(1).ParametersOnS2(AnU2,AnV2);
+        }
+
+        if(typQuad==GeomAbs_Cylinder || 
+          typQuad==GeomAbs_Cone || 
+          typQuad==GeomAbs_Sphere) { 
+            arecadr=Standard_True; 
+        } 
+        //
+        for (k=2; k<=Nbpts; ++k) {
+          valpt = thelin->Value(k).Value();
+          Quad.Parameters(valpt,U1,V1);
+          //
+          if((V1 < Vmin) && (Vmin-V1 < TolV)) {
+            V1 = Vmin;
+          }
+          if((V1 > Vmax) && (V1-Vmax < TolV)) {
+            V1 = Vmax;
+          }
+          //
+          if(arecadr) {
+            //modified by NIZNHY-PKV Fri Mar 28 15:06:01 2008f
+            Standard_Real aCf, aTwoPI;
+            //
+            aCf=0.;
+            aTwoPI=M_PI+M_PI;
+            if ((U1-AnU1) >  1.5*M_PI) { 
+              while ((U1-AnU1) > (1.5*M_PI+aCf*aTwoPI)) {
+                aCf=aCf+1.;
+              }
+              U1=U1-aCf*aTwoPI;
+            } 
+            //
+            else {
+              while ((U1-AnU1) < (-1.5*M_PI-aCf*aTwoPI)) {
+                aCf=aCf+1.;
+              }
+              U1=U1+aCf*aTwoPI;
+            }
+            // was:
+            //if ((U1-AnU1) >  1.5*M_PI) { 
+            //  U1-=M_PI+M_PI;
+            //}
+            //else if ((U1-AnU1) < -1.5*M_PI) { 
+            //  U1+=M_PI+M_PI; 
+            //}
+            //modified by NIZNHY-PKV Fri Mar 28 15:06:11 2008t
+          }
+          //
+          if(reversed) { 
+            thelin->SetUV(k,Standard_False,U1,V1);
+
+            thelin->Value(k).ParametersOnS1(U2,V2);
+            switch(typeS1) { 
+            case GeomAbs_Cylinder:
+            case GeomAbs_Cone:
+            case GeomAbs_Sphere:
+            case GeomAbs_Torus:
+              while(U2<(AnU2-1.5*M_PI)) U2+=M_PI+M_PI;
+              while(U2>(AnU2+1.5*M_PI)) U2-=M_PI+M_PI;
+              break;
+            default: 
+              break;
+            }
+            if(typeS2==GeomAbs_Torus) { 
+              while(V2<(AnV2-1.5*M_PI)) V2+=M_PI+M_PI;
+              while(V2>(AnV2+1.5*M_PI)) V2-=M_PI+M_PI;
+            }
+            thelin->SetUV(k,Standard_True,U2,V2);
+          }
+          else { 
+            thelin->SetUV(k,Standard_True,U1,V1);
+
+            thelin->Value(k).ParametersOnS2(U2,V2);
+            switch(typeS2) { 
+            case GeomAbs_Cylinder:
+            case GeomAbs_Cone:
+            case GeomAbs_Sphere:
+            case GeomAbs_Torus:
+              while(U2<(AnU2-1.5*M_PI)) U2+=M_PI+M_PI;
+              while(U2>(AnU2+1.5*M_PI)) U2-=M_PI+M_PI;
+              break;
+            default: 
+              break;
+            }
+            if(typeS2==GeomAbs_Torus) { 
+              while(V2<(AnV2-1.5*M_PI)) V2+=M_PI+M_PI;
+              while(V2>(AnV2+1.5*M_PI)) V2-=M_PI+M_PI;
+            }
+            thelin->SetUV(k,Standard_False,U2,V2);
+
+          }
+
+          AnU1=U1;
+          AnU2=U2;
+          AnV2=V2;
+        }
+        // <-A
+        wline = new IntPatch_WLine(thelin,Standard_False,trans1,trans2);
+
+#ifdef INTPATCH_IMPPRMINTERSECTION_DEBUG
+        wline->Dump(0);
+#endif
+
+        if (   iwline->HasFirstPoint() 
+          && iwline->IsTangentAtBegining() == Standard_False) 
+        {
+          indfirst = iwline->FirstPointIndex();
+          PPoint = seqpdep(indfirst);
+          tgline = PPoint.Direction3d();
+          Standard_Integer themult = PPoint.Multiplicity();
+          for (Standard_Integer i=NbPointRst; i>=1; i--) {
+            if (Destination(i) == indfirst) {
+              if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
+                Quad.Parameters(PPoint.Value(),U1,V1);
+
+                if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
+                if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
+
+                PPoint.Parameters(themult,U2,V2);
+                Surf2->D1(U2,V2,ptbid,d1u,d1v); //-- @@@@
+              }
+              else {  //-- typeS1 != Pln && Cyl && Sph && Cone
+                Quad.Parameters(PPoint.Value(),U2,V2);
+
+                if((V2 < Vmin) && (Vmin-V2 < TolV)) V2 = Vmin;
+                if((V2 > Vmax) && (V2-Vmax < TolV)) V2 = Vmax;
+
+                PPoint.Parameters(themult,U1,V1);
+                Surf1->D1(U1,V1,ptbid,d1u,d1v); //-- @@@@
+              }
+
+              VecNormale = d1u.Crossed(d1v);                      
+              //-- Modif du 27 Septembre 94 (Recadrage des pts U,V) 
+              ptdeb.SetValue(PPoint.Value(),TolArc,Standard_False);
+              ptdeb.SetParameters(U1,V1,U2,V2);
+              ptdeb.SetParameter(1.);
+
+              Recadre(reversed,typeS1,typeS2,ptdeb,iwline,1,U1,V1,U2,V2);
+
+              currentarc = solrst.Point(i).Arc();
+              currentparam = solrst.Point(i).Parameter();
+              currentarc->D1(currentparam,p2d,d2d);
+              tgrst.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
+
+              Standard_Real squaremagnitudeVecNormale = VecNormale.SquareMagnitude();
+              if(squaremagnitudeVecNormale > 1e-13) { 
+                DirNormale=VecNormale;
+                IntSurf::MakeTransition(tgline,tgrst,DirNormale,TLine,TArc);
+              }
+              else { 
+                TLine.SetValue(Standard_True,IntSurf_Undecided);
+                TArc.SetValue(Standard_True,IntSurf_Undecided);
+              }
+
+              ptdeb.SetArc(reversed,currentarc,currentparam,TLine,TArc);
+              if (!solrst.Point(i).IsNew()) {
+                ptdeb.SetVertex(reversed,solrst.Point(i).Vertex());
+              }
+              wline->AddVertex(ptdeb);
+              if (themult == 0) {
+                wline->SetFirstPoint(wline->NbVertex());
+              }
+
+              themult--;
+            }
+          }
+        }
+        else if (iwline->IsTangentAtBegining()) 
+        {
+          gp_Pnt psol = thelin->Value(1).Value();
+          thelin->Value(1).ParametersOnS1(U1,V1);
+          thelin->Value(1).ParametersOnS2(U2,V2);
+          ptdeb.SetValue(psol,TolArc,Standard_True);
+          ptdeb.SetParameters(U1,V1,U2,V2);
+          ptdeb.SetParameter(1.);
+          wline->AddVertex(ptdeb);
+          wline->SetFirstPoint(wline->NbVertex());
+        }
+        else 
+        { 
+          gp_Pnt psol = thelin->Value(1).Value();
+          thelin->Value(1).ParametersOnS1(U1,V1);
+          thelin->Value(1).ParametersOnS2(U2,V2);
+          ptdeb.SetValue(psol,TolArc,Standard_False);
+          ptdeb.SetParameters(U1,V1,U2,V2);
+          ptdeb.SetParameter(1.);
+          wline->AddVertex(ptdeb);
+          wline->SetFirstPoint(wline->NbVertex());
+        }
+
+
+        if (   iwline->HasLastPoint() 
+          && iwline->IsTangentAtEnd() == Standard_False) 
+        {
+          indlast = iwline->LastPointIndex();
+          PPoint = seqpdep(indlast);
+          tgline = PPoint.Direction3d().Reversed();
+          Standard_Integer themult = PPoint.Multiplicity();
+          for (Standard_Integer i=NbPointRst; i >=1; i--) {
+            if (Destination(i) == indlast) {
+              if (!reversed) {
+                Quad.Parameters(PPoint.Value(),U1,V1);
+
+                if((V1 < Vmin) && (Vmin-V1 < TolV)) V1 = Vmin;
+                if((V1 > Vmax) && (V1-Vmax < TolV)) V1 = Vmax;
+
+                PPoint.Parameters(themult,U2,V2);
+                Surf2->D1(U2,V2,ptbid,d1u,d1v); //-- @@@@
+                VecNormale = d1u.Crossed(d1v);                    //-- @@@@
+              }
+              else {
+                Quad.Parameters(PPoint.Value(),U2,V2);
+
+                if((V2 < Vmin) && (Vmin-V2 < TolV)) V2 = Vmin;
+                if((V2 > Vmax) && (V2-Vmax < TolV)) V2 = Vmax;
+
+                PPoint.Parameters(themult,U1,V1);
+                Surf1->D1(U1,V1,ptbid,d1u,d1v); //-- @@@@
+                VecNormale = d1u.Crossed(d1v);                    //-- @@@@
+              }
+
+              ptfin.SetValue(PPoint.Value(),TolArc,Standard_False);
+              ptfin.SetParameters(U1,V1,U2,V2);
+              ptfin.SetParameter(Nbpts);
+
+              Recadre(reversed,typeS1,typeS2,ptfin,iwline,Nbpts-1,U1,V1,U2,V2);
+
+              currentarc = solrst.Point(i).Arc();
+              currentparam = solrst.Point(i).Parameter();
+              currentarc->D1(currentparam,p2d,d2d);
+              tgrst.SetLinearForm(d2d.X(),d1u,d2d.Y(),d1v);
+
+
+              Standard_Real squaremagnitudeVecNormale = VecNormale.SquareMagnitude();
+              if(squaremagnitudeVecNormale > 1e-13) { 
+                DirNormale=VecNormale;
+                IntSurf::MakeTransition(tgline,tgrst,DirNormale,TLine,TArc);
+              }
+              else { 
+                TLine.SetValue(Standard_True,IntSurf_Undecided);
+                TArc.SetValue(Standard_True,IntSurf_Undecided);
+              }
+
+
+              ptfin.SetArc(reversed,currentarc,currentparam,TLine,TArc);
+              if (!solrst.Point(i).IsNew()) {
+                ptfin.SetVertex(reversed,solrst.Point(i).Vertex());
+              }
+              wline->AddVertex(ptfin);
+              if (themult == 0) {
+                wline->SetLastPoint(wline->NbVertex());
+              }
+
+              themult--;
+            }
+          }
+        }
+        else if (iwline->IsTangentAtEnd()) 
+        {
+          gp_Pnt psol = thelin->Value(Nbpts).Value();
+          thelin->Value(Nbpts).ParametersOnS1(U1,V1);
+          thelin->Value(Nbpts).ParametersOnS2(U2,V2);
+          ptfin.SetValue(psol,TolArc,Standard_True);
+          ptfin.SetParameters(U1,V1,U2,V2);
+          ptfin.SetParameter(Nbpts);
+          wline->AddVertex(ptfin);
+          wline->SetLastPoint(wline->NbVertex());
+        }
+        else 
+        { 
+          gp_Pnt psol = thelin->Value(Nbpts).Value();
+          thelin->Value(Nbpts).ParametersOnS1(U1,V1);
+          thelin->Value(Nbpts).ParametersOnS2(U2,V2);
+          ptfin.SetValue(psol,TolArc,Standard_False);
+          ptfin.SetParameters(U1,V1,U2,V2);
+          ptfin.SetParameter(Nbpts);
+          wline->AddVertex(ptfin);
+          wline->SetLastPoint(wline->NbVertex());
+        }
+        //
+        // Il faut traiter les points de passage.
+        slin.Append(wline);
       }// if(Nbpts>=2) {
     }// for (j=1; j<=Nblines; j++) {
 
@@ -885,78 +1022,78 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
 
 
     Nblines = slin.Length();
-    for (j=1; j<=Nblines-1; j++) {
+    for (Standard_Integer j=1; j<=Nblines-1; j++) {
       dofirst = dolast = Standard_False;
       const  Handle(IntPatch_Line)& slinj = slin(j);
-      const Handle(IntPatch_WLine)& wlin1 = *((Handle(IntPatch_WLine)*)&slinj);
+      Handle(IntPatch_WLine) wlin1 (Handle(IntPatch_WLine)::DownCast (slinj));
       if (wlin1->HasFirstPoint()) {
-       ptdeb = wlin1->FirstPoint(indfirst);
-       if (ptdeb.IsTangencyPoint()) {
-         dofirst = Standard_True;
-       }
+        ptdeb = wlin1->FirstPoint(indfirst);
+        if (ptdeb.IsTangencyPoint()) {
+          dofirst = Standard_True;
+        }
       }
       if (wlin1->HasLastPoint()) {
-       ptfin = wlin1->LastPoint(indlast);
-       if (ptfin.IsTangencyPoint()) {
-         dolast = Standard_True;
-       }
+        ptfin = wlin1->LastPoint(indlast);
+        if (ptfin.IsTangencyPoint()) {
+          dolast = Standard_True;
+        }
       }
-      
+
       if (dofirst || dolast) {
-       for (k=j+1; k<=Nblines;k++) {
-         const  Handle(IntPatch_Line)& slink = slin(k);
-         const  Handle(IntPatch_WLine)& wlin2 = *((Handle(IntPatch_WLine)*)&slink);
-         if (wlin2->HasFirstPoint()) {
-           ptbis = wlin2->FirstPoint(ind2);
-           if (ptbis.IsTangencyPoint()) {
-             if (dofirst ) {
-               if (ptdeb.Value().Distance(ptbis.Value()) <= TolArc) {
-                 ptdeb.SetMultiple(Standard_True);
-                 if (!ptbis.IsMultiple()) {
-                   ptbis.SetMultiple(Standard_True);
-                   wlin2->Replace(ind2,ptbis);
-                 }
-               }
-             }
-             if (dolast ) {
-               if (ptfin.Value().Distance(ptbis.Value()) <= TolArc) {
-                 ptfin.SetMultiple(Standard_True);
-                 if (!ptbis.IsMultiple()) {
-                   ptbis.SetMultiple(Standard_True);
-                   wlin2->Replace(ind2,ptbis);
-                 }
-               }
-             }
-           }
-         }
-         if (wlin2->HasLastPoint()) {
-           ptbis = wlin2->LastPoint(ind2);
-           if (ptbis.IsTangencyPoint()) {
-             if (dofirst ) {
-               if (ptdeb.Value().Distance(ptbis.Value()) <= TolArc) {
-                 ptdeb.SetMultiple(Standard_True);
-                 if (!ptbis.IsMultiple()) {
-                   ptbis.SetMultiple(Standard_True);
-                   wlin2->Replace(ind2,ptbis);
-                 }
-               }
-             }
-             if (dolast ) {
-               if (ptfin.Value().Distance(ptbis.Value()) <= TolArc) {
-                 ptfin.SetMultiple(Standard_True);
-                 if (!ptbis.IsMultiple()) {
-                   ptbis.SetMultiple(Standard_True);
-                   wlin2->Replace(ind2,ptbis);
-                 }
-               }
-             }
-           }
-         }
-       }
-       if(dofirst) 
-         wlin1->Replace(indfirst,ptdeb);
-       if(dolast) 
-         wlin1->Replace(indlast,ptfin);
+        for (Standard_Integer k=j+1; k<=Nblines;k++) {
+          const  Handle(IntPatch_Line)& slink = slin(k);
+          Handle(IntPatch_WLine) wlin2 (Handle(IntPatch_WLine)::DownCast (slink));
+          if (wlin2->HasFirstPoint()) {
+            ptbis = wlin2->FirstPoint(ind2);
+            if (ptbis.IsTangencyPoint()) {
+              if (dofirst ) {
+                if (ptdeb.Value().Distance(ptbis.Value()) <= TolArc) {
+                  ptdeb.SetMultiple(Standard_True);
+                  if (!ptbis.IsMultiple()) {
+                    ptbis.SetMultiple(Standard_True);
+                    wlin2->Replace(ind2,ptbis);
+                  }
+                }
+              }
+              if (dolast ) {
+                if (ptfin.Value().Distance(ptbis.Value()) <= TolArc) {
+                  ptfin.SetMultiple(Standard_True);
+                  if (!ptbis.IsMultiple()) {
+                    ptbis.SetMultiple(Standard_True);
+                    wlin2->Replace(ind2,ptbis);
+                  }
+                }
+              }
+            }
+          }
+          if (wlin2->HasLastPoint()) {
+            ptbis = wlin2->LastPoint(ind2);
+            if (ptbis.IsTangencyPoint()) {
+              if (dofirst ) {
+                if (ptdeb.Value().Distance(ptbis.Value()) <= TolArc) {
+                  ptdeb.SetMultiple(Standard_True);
+                  if (!ptbis.IsMultiple()) {
+                    ptbis.SetMultiple(Standard_True);
+                    wlin2->Replace(ind2,ptbis);
+                  }
+                }
+              }
+              if (dolast ) {
+                if (ptfin.Value().Distance(ptbis.Value()) <= TolArc) {
+                  ptfin.SetMultiple(Standard_True);
+                  if (!ptbis.IsMultiple()) {
+                    ptbis.SetMultiple(Standard_True);
+                    wlin2->Replace(ind2,ptbis);
+                  }
+                }
+              }
+            }
+          }
+        }
+        if(dofirst) 
+          wlin1->Replace(indfirst,ptdeb);
+        if(dolast) 
+          wlin1->Replace(indlast,ptfin);
       }
     }
   }// if (seqpdep.Length() != 0 || seqpins.Length() != 0) {
@@ -964,8 +1101,40 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
   // Treatment the segments
   NbSegm = solrst.NbSegments();
   if (NbSegm) {
-    for(i=1; i<=NbSegm; i++) {
+    for(Standard_Integer i=1; i<=NbSegm; i++) {
       thesegm = solrst.Segment(i);  
+      //Check if segment is degenerated
+      if(thesegm.HasFirstPoint() && thesegm.HasLastPoint())
+      {
+        Standard_Real tol2 = Precision::Confusion();
+        tol2 *= tol2;
+        const gp_Pnt& aPf = thesegm.FirstPoint().Value();
+        const gp_Pnt& aPl = thesegm.LastPoint().Value();
+        if(aPf.SquareDistance(aPl) <= tol2)
+        {
+          //segment can be degenerated - check inner point
+          paramf = thesegm.FirstPoint().Parameter();
+          paraml = thesegm.LastPoint().Parameter();
+          gp_Pnt2d _p2d = 
+            thesegm.Curve()->Value(.57735 * paramf + 0.42265 * paraml);
+          gp_Pnt aPm;
+          if(reversed)
+          {
+            Surf1->D0(_p2d.X(), _p2d.Y(), aPm);
+          }
+          else
+          {
+            Surf2->D0(_p2d.X(), _p2d.Y(), aPm);
+          }
+          if(aPm.SquareDistance(aPf) <= tol2)
+          {
+            //Degenerated
+            continue;
+          }
+        }
+      }
+
+
       //----------------------------------------------------------------------      
       // on cree une ligne d intersection contenant uniquement le segment.
       // VOIR POUR LA TRANSITION DE LA LIGNE
@@ -982,348 +1151,440 @@ void IntPatch_ImpPrmIntersection::Perform (const Handle(Adaptor3d_HSurface)& Sur
 
       IntPatch_Point _thepointAtBeg;
       IntPatch_Point _thepointAtEnd;
-      
+
       Standard_Boolean TransitionOK=Standard_False;
 
       if(thesegm.HasFirstPoint()) { 
-       Standard_Real _u1,_v1,_u2,_v2;
-
-       dofirst = Standard_True;
-       PStartf = thesegm.FirstPoint();
-       paramf = PStartf.Parameter();
-
-       gp_Pnt2d _p2d     = thesegm.Curve()->Value(paramf);
-       Handle(Adaptor3d_HVertex) _vtx;
-       if(PStartf.IsNew()==Standard_False) 
-         _vtx= PStartf.Vertex();
-       const gp_Pnt& _Pp = PStartf.Value();
-       _thepointAtBeg.SetValue(_Pp,PStartf.Tolerance(),Standard_False);
-       if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
-         Quad.Parameters(_Pp,_u1,_v1);
-         _u2=_p2d.X(); _v2=_p2d.Y();
-       }
-       else {  //-- typeS1 != Pln && Cyl && Sph && Cone
-         Quad.Parameters(_Pp,_u2,_v2);
-         _u1=_p2d.X(); _v1=_p2d.Y();
-       }
-       _thepointAtBeg.SetParameters(_u1,_v1,_u2,_v2);
-       _thepointAtBeg.SetParameter(paramf);
-       if(PStartf.IsNew()==Standard_False) 
-         _thepointAtBeg.SetVertex(reversed,_vtx);
-       _thepointAtBeg.SetArc(reversed,thesegm.Curve(),paramf,TLineUnk,TArcUnk);
-
-       
-       gp_Vec d1u1,d1v1,d1u2,d1v2; gp_Vec2d _d2d;
-       Surf1->D1(_u1,_v1,ptbid,d1u1,d1v1);
-       norm1 = d1u1.Crossed(d1v1);
-       Surf2->D1(_u2,_v2,ptbid,d1u2,d1v2);
-       norm2 = d1u2.Crossed(d1v2);
-       
-       thesegm.Curve()->D1(paramf,_p2d,_d2d);
-       if(reversed) { 
-         tgline.SetLinearForm(_d2d.X(),d1u1,_d2d.Y(),d1v1);
-       }
-       else { 
-         tgline.SetLinearForm(_d2d.X(),d1u2,_d2d.Y(),d1v2);
-       }
-       _u1=tgline.DotCross(norm2,norm1);
-       TransitionOK=Standard_True;
-       if (_u1 > 0.00000001) {
-         trans1 = IntSurf_Out;
-         trans2 = IntSurf_In;
-       }
-       else if(_u1 < -0.00000001) { 
-         trans1 = IntSurf_In;
-         trans2 = IntSurf_Out;
-       }
-       else { 
-         TransitionOK=Standard_False;
-       }
+        Standard_Real _u1,_v1,_u2,_v2;
+
+        dofirst = Standard_True;
+        PStartf = thesegm.FirstPoint();
+        paramf = PStartf.Parameter();
+
+        gp_Pnt2d _p2d     = thesegm.Curve()->Value(paramf);
+        Handle(Adaptor3d_HVertex) _vtx;
+        if(PStartf.IsNew()==Standard_False) 
+          _vtx= PStartf.Vertex();
+        const gp_Pnt& _Pp = PStartf.Value();
+        _thepointAtBeg.SetValue(_Pp,PStartf.Tolerance(),Standard_False);
+        if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
+          Quad.Parameters(_Pp,_u1,_v1);
+          _u2=_p2d.X(); _v2=_p2d.Y();
+        }
+        else {  //-- typeS1 != Pln && Cyl && Sph && Cone
+          Quad.Parameters(_Pp,_u2,_v2);
+          _u1=_p2d.X(); _v1=_p2d.Y();
+        }
+        _thepointAtBeg.SetParameters(_u1,_v1,_u2,_v2);
+        _thepointAtBeg.SetParameter(paramf);
+        if(PStartf.IsNew()==Standard_False) 
+          _thepointAtBeg.SetVertex(reversed,_vtx);
+        _thepointAtBeg.SetArc(reversed,thesegm.Curve(),paramf,TLineUnk,TArcUnk);
+
+
+        gp_Vec d1u1,d1v1,d1u2,d1v2; gp_Vec2d _d2d;
+        Surf1->D1(_u1,_v1,ptbid,d1u1,d1v1);
+        norm1 = d1u1.Crossed(d1v1);
+        Surf2->D1(_u2,_v2,ptbid,d1u2,d1v2);
+        norm2 = d1u2.Crossed(d1v2);
+
+        thesegm.Curve()->D1(paramf,_p2d,_d2d);
+        if(reversed) { 
+          tgline.SetLinearForm(_d2d.X(),d1u1,_d2d.Y(),d1v1);
+        }
+        else { 
+          tgline.SetLinearForm(_d2d.X(),d1u2,_d2d.Y(),d1v2);
+        }
+        _u1=tgline.DotCross(norm2,norm1);
+        TransitionOK=Standard_True;
+        if (_u1 > 0.00000001) {
+          trans1 = IntSurf_Out;
+          trans2 = IntSurf_In;
+        }
+        else if(_u1 < -0.00000001) { 
+          trans1 = IntSurf_In;
+          trans2 = IntSurf_Out;
+        }
+        else { 
+          TransitionOK=Standard_False;
+        }
       }
       if(thesegm.HasLastPoint()) {  
-       Standard_Real _u1,_v1,_u2,_v2;
-
-       dolast = Standard_True;
-       PStartl = thesegm.LastPoint();
-       paraml = PStartl.Parameter();
-       
-       gp_Pnt2d _p2d = thesegm.Curve()->Value(paraml);
-       Handle(Adaptor3d_HVertex) _vtx;
-       if(PStartl.IsNew()==Standard_False) 
-         _vtx = PStartl.Vertex();
-       const gp_Pnt& _Pp = PStartl.Value();
-       IntPatch_Point _thepoint;
-       _thepointAtEnd.SetValue(_Pp,PStartl.Tolerance(),Standard_False);
-       if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
-         Quad.Parameters(_Pp,_u1,_v1);
-         _u2=_p2d.X(); _v2=_p2d.Y();
-       }
-       else {  //-- typeS1 != Pln && Cyl && Sph && Cone
-         Quad.Parameters(_Pp,_u2,_v2);
-         _u1=_p2d.X(); _v1=_p2d.Y();
-       }
-       _thepointAtEnd.SetParameters(_u1,_v1,_u2,_v2);
-       _thepointAtEnd.SetParameter(paraml);
-       if(PStartl.IsNew()==Standard_False)
-         _thepointAtEnd.SetVertex(reversed,_vtx);
-       _thepointAtEnd.SetArc(reversed,thesegm.Curve(),paraml,TLineUnk,TArcUnk);
-
-       
-       
-       gp_Vec d1u1,d1v1,d1u2,d1v2; gp_Vec2d _d2d;
-       Surf1->D1(_u1,_v1,ptbid,d1u1,d1v1);
-       norm1 = d1u1.Crossed(d1v1);
-       Surf2->D1(_u2,_v2,ptbid,d1u2,d1v2);
-       norm2 = d1u2.Crossed(d1v2);
-       
-       thesegm.Curve()->D1(paraml,_p2d,_d2d);
-       if(reversed) { 
-         tgline.SetLinearForm(_d2d.X(),d1u1,_d2d.Y(),d1v1);
-       }
-       else { 
-         tgline.SetLinearForm(_d2d.X(),d1u2,_d2d.Y(),d1v2);
-       }
-       _u1=tgline.DotCross(norm2,norm1);
-       TransitionOK=Standard_True;
-       if (_u1 > 0.00000001) {
-         trans1 = IntSurf_Out;
-         trans2 = IntSurf_In;
-       }
-       else if(_u1 < -0.00000001) { 
-         trans1 = IntSurf_In;
-         trans2 = IntSurf_Out;
-       }
-       else { 
-         TransitionOK=Standard_False;
-       }       
+        Standard_Real _u1,_v1,_u2,_v2;
+
+        dolast = Standard_True;
+        PStartl = thesegm.LastPoint();
+        paraml = PStartl.Parameter();
+
+        gp_Pnt2d _p2d = thesegm.Curve()->Value(paraml);
+        Handle(Adaptor3d_HVertex) _vtx;
+        if(PStartl.IsNew()==Standard_False) 
+          _vtx = PStartl.Vertex();
+        const gp_Pnt& _Pp = PStartl.Value();
+        IntPatch_Point _thepoint;
+        _thepointAtEnd.SetValue(_Pp,PStartl.Tolerance(),Standard_False);
+        if (!reversed) { //-- typeS1 = Pln || Cyl || Sph || Cone
+          Quad.Parameters(_Pp,_u1,_v1);
+          _u2=_p2d.X(); _v2=_p2d.Y();
+        }
+        else {  //-- typeS1 != Pln && Cyl && Sph && Cone
+          Quad.Parameters(_Pp,_u2,_v2);
+          _u1=_p2d.X(); _v1=_p2d.Y();
+        }
+        _thepointAtEnd.SetParameters(_u1,_v1,_u2,_v2);
+        _thepointAtEnd.SetParameter(paraml);
+        if(PStartl.IsNew()==Standard_False)
+          _thepointAtEnd.SetVertex(reversed,_vtx);
+        _thepointAtEnd.SetArc(reversed,thesegm.Curve(),paraml,TLineUnk,TArcUnk);
+
+
+
+        gp_Vec d1u1,d1v1,d1u2,d1v2; gp_Vec2d _d2d;
+        Surf1->D1(_u1,_v1,ptbid,d1u1,d1v1);
+        norm1 = d1u1.Crossed(d1v1);
+        Surf2->D1(_u2,_v2,ptbid,d1u2,d1v2);
+        norm2 = d1u2.Crossed(d1v2);
+
+        thesegm.Curve()->D1(paraml,_p2d,_d2d);
+        if(reversed) { 
+          tgline.SetLinearForm(_d2d.X(),d1u1,_d2d.Y(),d1v1);
+        }
+        else { 
+          tgline.SetLinearForm(_d2d.X(),d1u2,_d2d.Y(),d1v2);
+        }
+        _u1=tgline.DotCross(norm2,norm1);
+        TransitionOK=Standard_True;
+        if (_u1 > 0.00000001) {
+          trans1 = IntSurf_Out;
+          trans2 = IntSurf_In;
+        }
+        else if(_u1 < -0.00000001) { 
+          trans1 = IntSurf_In;
+          trans2 = IntSurf_Out;
+        }
+        else { 
+          TransitionOK=Standard_False;
+        }      
       }
       if(TransitionOK==Standard_False) { 
-       //-- rline = new IntPatch_RLine (thesegm.Curve(),reversed,Standard_False);
-       rline =  new IntPatch_RLine (Standard_False);
-       if(reversed) { 
-         rline->SetArcOnS1(thesegm.Curve());
-       }
-       else { 
-         rline->SetArcOnS2(thesegm.Curve());
-       }
+        //-- rline = new IntPatch_RLine (thesegm.Curve(),reversed,Standard_False);
+        rline =  new IntPatch_RLine (Standard_False);
+        if(reversed) { 
+          rline->SetArcOnS1(thesegm.Curve());
+        }
+        else { 
+          rline->SetArcOnS2(thesegm.Curve());
+        }
       }
       else { 
-       //-- rline = new IntPatch_RLine (thesegm.Curve(),reversed,Standard_False,trans1,trans2);
-       rline =  new IntPatch_RLine (Standard_False,trans1,trans2);
-       if(reversed) { 
-         rline->SetArcOnS1(thesegm.Curve());
-       }
-       else { 
-         rline->SetArcOnS2(thesegm.Curve());
-       }
+        //-- rline = new IntPatch_RLine (thesegm.Curve(),reversed,Standard_False,trans1,trans2);
+        rline =  new IntPatch_RLine (Standard_False,trans1,trans2);
+        if(reversed) { 
+          rline->SetArcOnS1(thesegm.Curve());
+        }
+        else { 
+          rline->SetArcOnS2(thesegm.Curve());
+        }
       }
 
       //------------------------------
       //-- Ajout des points 
       //--
       if (thesegm.HasFirstPoint()) {
-       rline->AddVertex(_thepointAtBeg);
-       rline->SetFirstPoint(rline->NbVertex());
+        rline->AddVertex(_thepointAtBeg);
+        rline->SetFirstPoint(rline->NbVertex());
       }
-      
+
       if (thesegm.HasLastPoint()) {
-       rline->AddVertex(_thepointAtEnd);
-       rline->SetLastPoint(rline->NbVertex());
+        rline->AddVertex(_thepointAtEnd);
+        rline->SetLastPoint(rline->NbVertex());
       }
 
       // Polygone sur restriction solution
       if (dofirst && dolast) {
-       Standard_Real prm;
-       gp_Pnt ptpoly;
-       IntSurf_PntOn2S p2s;
-       Handle(IntSurf_LineOn2S) Thelin = new IntSurf_LineOn2S ();
-       Handle(Adaptor2d_HCurve2d) arcsegm = thesegm.Curve();
-       Standard_Integer nbsample = 100;
-
-       if (!reversed) {
-         for (j=1; j<=nbsample; j++) {
-           prm = paramf + (j-1)*(paraml-paramf)/(nbsample-1);
-           arcsegm->D0(prm,p2d);
-           Surf2->D0(p2d.X(),p2d.Y(),ptpoly);
-
-           Quad.Parameters(ptpoly,U1,V1);
-           p2s.SetValue(ptpoly,U1,V1,p2d.X(),p2d.Y());
-           Thelin->Add(p2s);
-         }
-       }
-       else {
-         for (j=1; j<=nbsample; j++) {
-           prm = paramf + (j-1)*(paraml-paramf)/(nbsample-1);
-           arcsegm->D0(prm,p2d);
-           Surf1->D0(p2d.X(),p2d.Y(),ptpoly);
-
-           Quad.Parameters(ptpoly,U2,V2);
-           p2s.SetValue(ptpoly,p2d.X(),p2d.Y(),U2,V2);
-           Thelin->Add(p2s);
-         }
-       }
-       rline->Add(Thelin);
+        Standard_Real prm;
+        gp_Pnt ptpoly;
+        IntSurf_PntOn2S p2s;
+        Handle(IntSurf_LineOn2S) Thelin = new IntSurf_LineOn2S ();
+        Handle(Adaptor2d_HCurve2d) arcsegm = thesegm.Curve();
+        Standard_Integer nbsample = 100;
+
+        if (!reversed) {
+          for (Standard_Integer j=1; j<=nbsample; j++) {
+            prm = paramf + (j-1)*(paraml-paramf)/(nbsample-1);
+            arcsegm->D0(prm,p2d);
+            Surf2->D0(p2d.X(),p2d.Y(),ptpoly);
+
+            Quad.Parameters(ptpoly,U1,V1);
+            p2s.SetValue(ptpoly,U1,V1,p2d.X(),p2d.Y());
+            Thelin->Add(p2s);
+          }
+        }
+        else {
+          for (Standard_Integer j=1; j<=nbsample; j++) {
+            prm = paramf + (j-1)*(paraml-paramf)/(nbsample-1);
+            arcsegm->D0(prm,p2d);
+            Surf1->D0(p2d.X(),p2d.Y(),ptpoly);
+
+            Quad.Parameters(ptpoly,U2,V2);
+            p2s.SetValue(ptpoly,p2d.X(),p2d.Y(),U2,V2);
+            Thelin->Add(p2s);
+          }
+        }
+        rline->Add(Thelin);
       }
 
       if (dofirst || dolast) {
-       Nblines = slin.Length();
-       for (j=1; j<=Nblines; j++) {
-         const Handle(IntPatch_Line)& slinj = slin(j);
-         typ = slinj->ArcType();
-         if (typ == IntPatch_Walking) {
-           Nbpts = (*((Handle(IntPatch_WLine)*)&slinj))->NbVertex();
-         }
-         else {
-           Nbpts = (*((Handle(IntPatch_RLine)*)&slinj))->NbVertex();
-         }
-         for (k=1; k<=Nbpts;k++) {
-           if (typ == IntPatch_Walking) {
-             ptdeb = (*((Handle(IntPatch_WLine)*)&slinj))->Vertex(k);
-           }
-           else {
-             ptdeb = (*((Handle(IntPatch_RLine)*)&slinj))->Vertex(k);
-           }
-           if (dofirst) {
-
-             if (ptdeb.Value().Distance(PStartf.Value()) <=TolArc) {
-               ptdeb.SetMultiple(Standard_True);
-               if (typ == IntPatch_Walking) {
-                 (*((Handle(IntPatch_WLine)*)&slinj))->Replace(k,ptdeb);
-               }
-               else {
-                 (*((Handle(IntPatch_RLine)*)&slinj))->Replace(k,ptdeb);
-               }
-               ptdeb.SetParameter(paramf);
-               rline->AddVertex(ptdeb);
-               if (!procf){
-                 procf=Standard_True;
-                 rline->SetFirstPoint(rline->NbVertex());
-               }
-             }
-           }
-           if (dolast) {
-             if(dofirst) { //-- on recharge le ptdeb
-               if (typ == IntPatch_Walking) {
-                 ptdeb = (*((Handle(IntPatch_WLine)*)&slinj))->Vertex(k);
-               }
-               else {
-                 ptdeb = (*((Handle(IntPatch_RLine)*)&slinj))->Vertex(k);
-               }
-             }
-             if (ptdeb.Value().Distance(PStartl.Value()) <=TolArc) {
-               ptdeb.SetMultiple(Standard_True);
-               if (typ == IntPatch_Walking) {
-                 (*((Handle(IntPatch_WLine)*)&slinj))->Replace(k,ptdeb);
-               }
-               else {
-                 (*((Handle(IntPatch_RLine)*)&slinj))->Replace(k,ptdeb);
-               }
-               ptdeb.SetParameter(paraml);
-               rline->AddVertex(ptdeb);
-               if (!procl){
-                 procl=Standard_True;
-                 rline->SetLastPoint(rline->NbVertex());
-               }
-             }
-           }
-         }
-       }
+        Nblines = slin.Length();
+        for (Standard_Integer j=1; j<=Nblines; j++) {
+          const Handle(IntPatch_Line)& slinj = slin(j);
+          typ = slinj->ArcType();
+          if (typ == IntPatch_Walking) {
+            Nbpts = Handle(IntPatch_WLine)::DownCast (slinj)->NbVertex();
+          }
+          else {
+            Nbpts = Handle(IntPatch_RLine)::DownCast (slinj)->NbVertex();
+          }
+          for (Standard_Integer k=1; k<=Nbpts;k++) {
+            if (typ == IntPatch_Walking) {
+              ptdeb = Handle(IntPatch_WLine)::DownCast (slinj)->Vertex(k);
+            }
+            else {
+              ptdeb = Handle(IntPatch_RLine)::DownCast (slinj)->Vertex(k);
+            }
+            if (dofirst) {
+
+              if (ptdeb.Value().Distance(PStartf.Value()) <=TolArc) {
+                ptdeb.SetMultiple(Standard_True);
+                if (typ == IntPatch_Walking) {
+                  Handle(IntPatch_WLine)::DownCast (slinj)->Replace(k,ptdeb);
+                }
+                else {
+                  Handle(IntPatch_RLine)::DownCast (slinj)->Replace(k,ptdeb);
+                }
+                ptdeb.SetParameter(paramf);
+                rline->AddVertex(ptdeb);
+                if (!procf){
+                  procf=Standard_True;
+                  rline->SetFirstPoint(rline->NbVertex());
+                }
+              }
+            }
+            if (dolast) {
+              if(dofirst) { //-- on recharge le ptdeb
+                if (typ == IntPatch_Walking) {
+                  ptdeb = Handle(IntPatch_WLine)::DownCast (slinj)->Vertex(k);
+                }
+                else {
+                  ptdeb = Handle(IntPatch_RLine)::DownCast (slinj)->Vertex(k);
+                }
+              }
+              if (ptdeb.Value().Distance(PStartl.Value()) <=TolArc) {
+                ptdeb.SetMultiple(Standard_True);
+                if (typ == IntPatch_Walking) {
+                  Handle(IntPatch_WLine)::DownCast (slinj)->Replace(k,ptdeb);
+                }
+                else {
+                  Handle(IntPatch_RLine)::DownCast (slinj)->Replace(k,ptdeb);
+                }
+                ptdeb.SetParameter(paraml);
+                rline->AddVertex(ptdeb);
+                if (!procl){
+                  procl=Standard_True;
+                  rline->SetLastPoint(rline->NbVertex());
+                }
+              }
+            }
+          }
+        }
       }
       slin.Append(rline);
     }
   }// if (NbSegm) 
   //
   // on traite les restrictions de la surface implicite
-  for (i=1; i<=slin.Length(); i++)
+
+  for (Standard_Integer i=1, aNbLin = slin.Length(); i<=aNbLin; i++)
   {
+    Handle(IntPatch_PointLine) aL = Handle(IntPatch_PointLine)::DownCast(slin(i));
+    
     if (!reversed)
-      IntPatch_RstInt::PutVertexOnLine(slin(i),Surf1,D1,Surf2,Standard_True,TolTang);
+      IntPatch_RstInt::PutVertexOnLine(aL,Surf1,D1,Surf2,Standard_True,TolTang);
     else
-      IntPatch_RstInt::PutVertexOnLine(slin(i),Surf2,D2,Surf1,Standard_False,TolTang);
+      IntPatch_RstInt::PutVertexOnLine(aL,Surf2,D2,Surf1,Standard_False,TolTang);
+
+    if (aL->NbPnts() <= 2)
+    {
+      Standard_Boolean aCond = aL->NbPnts() < 2;
+      if (!aCond)
+        aCond = (aL->Point(1).IsSame(aL->Point(2), Precision::Confusion()));
+
+      if (aCond)
+      {
+        slin.Remove(i);
+        i--;
+        aNbLin--;
+        continue;
+      }
+    }
+
+    if(aL->ArcType() == IntPatch_Walking)
+    {
+      const Handle(IntPatch_WLine) aWL = Handle(IntPatch_WLine)::DownCast(aL);
+      slin.Append(aWL);
+      slin.Remove(i);
+      i--;
+      aNbLin--;
+    }
   }
-  empt = (slin.Length() == 0 && spnt.Length() == 0);
-  done = Standard_True;
-  //
-  // post processing for cones and spheres
-  if((Quad.TypeQuadric() == GeomAbs_Cone ||
-      Quad.TypeQuadric() == GeomAbs_Sphere) && 
-     slin.Length()) {
 
-    Handle(Adaptor3d_TopolTool) PDomain = (reversed) ? D1 : D2;
-    Handle(Adaptor3d_HSurface) QSurf = (reversed) ? Surf2 : Surf1;
+  // Now slin is filled as follows: lower indices correspond to Restriction line,
+  // after (higher indices) - only Walking-line.
 
-    IntPatch_SequenceOfLine dslin;
-    for(i = 1; i <= slin.Length(); i++ )
-      DecomposeResult(slin(i),reversed,Quad,PDomain,QSurf,TolArc,dslin);
+  const Standard_Real aTol3d = Max(Func.Tolerance(), TolTang); 
+  const Handle(Adaptor3d_HSurface)& aQSurf = (reversed) ? Surf2 : Surf1;
+  const Handle(Adaptor3d_HSurface)& anOtherSurf = (reversed) ? Surf1 : Surf2;
 
-    slin.Clear();
-    for(i = 1; i <= dslin.Length(); i++ )
-      slin.Append(dslin(i));
-  }
-}
+  for (Standard_Integer i = 1; i <= slin.Length(); i++)
+  {
+    const Handle(IntPatch_PointLine)& aL1 = Handle(IntPatch_PointLine)::DownCast(slin(i));
+    const Handle(IntPatch_RLine)& aRL1 = Handle(IntPatch_RLine)::DownCast(aL1);
 
-//==================================================================
-// Decomposition of WLine goes through seam and/or apex routines
-//==================================================================
+    if(aRL1.IsNull())
+    {
+      //Walking-Walking cases are not supported
+      break;
+    }
 
-// reject all points and vertices not in V range on Quadric
-static Handle(IntSurf_LineOn2S) VSplitLine(Handle(IntSurf_LineOn2S)& Line,
-                                          Standard_Boolean          IsReversed,
-                                          Handle(Adaptor3d_HSurface)&               QSurf,
-                                          Handle(IntSurf_LineOn2S)& Vertices)
-{
-  Handle(IntSurf_LineOn2S) line = new IntSurf_LineOn2S();
+    const Handle(Adaptor2d_HCurve2d)& anArc = aRL1->IsArcOnS1() ? 
+                                              aRL1->ArcOnS1() :
+                                              aRL1->ArcOnS2();
+    if(anArc->Curve2d().GetType() != GeomAbs_Line)
+    {
+      //Restriction line must be isoline.
+      //Other cases are not supported by
+      //existing algorithms.
 
-  Standard_Real VF = QSurf->FirstVParameter();
-  Standard_Real VL = QSurf->LastVParameter();
+      break;
+    }
 
-  Standard_Integer i = 0;
-  Standard_Real U = 0., V = 0.;
+    Standard_Boolean isFirstDeleted = Standard_False;
 
-  for(i = 1; i <= Line->NbPoints(); i++) {
+    for(Standard_Integer j = i + 1; j <= slin.Length(); j++)
+    {
+      Handle(IntPatch_PointLine) aL2 = Handle(IntPatch_PointLine)::DownCast(slin(j));
+      Handle(IntPatch_RLine) aRL2 = Handle(IntPatch_RLine)::DownCast(aL2);
 
-    const IntSurf_PntOn2S& aP = Line->Value(i);
-    if(IsReversed)
-      aP.ParametersOnS2(U,V); // S2 - quadric
-    else
-      aP.ParametersOnS1(U,V); // S1 - quadric
+      //Here aL1 (i-th line) is Restriction-line and aL2 (j-th line) is
+      //Restriction or Walking
 
-    if(VF <= V && V <= VL)
-      line->Add(aP);
-  }
+      if(!aRL2.IsNull())
+      {
+        const Handle(Adaptor2d_HCurve2d)& anArc2 = aRL2->IsArcOnS1() ?
+                                                   aRL2->ArcOnS1() :
+                                                   aRL2->ArcOnS2();
+        if(anArc2->Curve2d().GetType() != GeomAbs_Line)
+        {
+          //Restriction line must be isoline.
+          //Other cases are not supported by
+          //existing algorithms.
 
-  Standard_Boolean isDeleted = Standard_True;
-  Standard_Integer k = 0;
-  while(isDeleted) {
-    
-    isDeleted = Standard_False;
-    k = 0;
+          continue;
+        }
+      }
 
-    for(i = 1; i <= Vertices->NbPoints(); i++) {
+      //aDir can be equal to one of following four values only
+      //(because Reastriction line is boundary of rectangular surface):
+      //either {0, 1} or {0, -1} or {1, 0} or {-1, 0}.
+      const gp_Dir2d aDir = anArc->Curve2d().Line().Direction();
 
-      const IntSurf_PntOn2S& aV = Vertices->Value(i);
-      if(IsReversed)
-       aV.ParametersOnS2(U,V); // S2 - quadric
-      else
-       aV.ParametersOnS1(U,V); // S1 - quadric
+      Standard_Real aTol2d = anOtherSurf->UResolution(aTol3d),
+                    aPeriod = anOtherSurf->IsVPeriodic() ? anOtherSurf->VPeriod() : 0.0;
 
-      if(VF <= V && V <= VL)
-       continue;
-      else {
-       k = i;
-       break;
+      if(Abs(aDir.X()) < 0.5)
+      {//Restriction directs along V-direction
+        aTol2d = anOtherSurf->VResolution(aTol3d);
+        aPeriod = anOtherSurf->IsUPeriodic() ? anOtherSurf->UPeriod() : 0.0;
+      }
+
+      const Standard_Boolean isCoincide = IsCoincide(Func, aL2, anArc, aRL1->IsArcOnS1(),
+                                                      aTol3d, aTol2d, aPeriod);
+
+      if(isCoincide)
+      {
+        if(aRL2.IsNull())
+        {//Delete Walking-line
+          slin.Remove(j);
+          j--;
+        }
+        else
+        {//Restriction-Restriction
+          const Handle(Adaptor2d_HCurve2d)& anArc2 = aRL2->IsArcOnS1() ?
+                                                     aRL2->ArcOnS1() :
+                                                     aRL2->ArcOnS2();
+
+          const Standard_Real aRange2 = anArc2->LastParameter() - 
+                                        anArc2->FirstParameter();
+          const Standard_Real aRange1 = anArc->LastParameter() -
+                                        anArc->FirstParameter();
+
+          if(aRange2 > aRange1)
+          {
+            isFirstDeleted = Standard_True;
+            break;
+          }
+          else
+          {//Delete j-th line
+            slin.Remove(j);
+            j--;
+          }
+        }
       }
+    } //for(Standard_Integer j = i + 1; j <= slin.Length(); j++)
+
+    if(isFirstDeleted)
+    {//Delete i-th line
+      slin.Remove(i--);
     }
+  }//for (Standard_Integer i = 1; i <= slin.Length(); i++)
 
-    if(k != 0) {
-      isDeleted = Standard_True;
-      Vertices->RemovePoint(k);
+  empt = (slin.Length() == 0 && spnt.Length() == 0);
+  done = Standard_True;
+
+
+  if(slin.Length() == 0)
+    return;
+
+  Standard_Boolean isDecomposeRequired =  (Quad.TypeQuadric() == GeomAbs_Cone) || 
+                                          (Quad.TypeQuadric() == GeomAbs_Sphere) ||
+                                          (Quad.TypeQuadric() == GeomAbs_Cylinder) ||
+                                          (Quad.TypeQuadric() == GeomAbs_Torus);
+
+  if(!isDecomposeRequired)
+    return;
+
+  // post processing for cones and spheres
+
+  const Handle(Adaptor3d_TopolTool)& PDomain = (reversed) ? D1 : D2;
+
+  IntPatch_SequenceOfLine dslin;
+  Standard_Boolean isDecompose = Standard_False;
+  for(Standard_Integer i = 1; i <= slin.Length(); i++ )
+  {
+    if(DecomposeResult( Handle(IntPatch_PointLine)::DownCast(slin(i)),
+                                        reversed, Quad, PDomain, aQSurf,
+                                        anOtherSurf, TolArc, aTol3d, dslin))
+    {
+      isDecompose = Standard_True;
     }
-    else
-      break;
   }
 
-  return line;
+  if(!isDecompose)
+    return;
+
+  slin.Clear();
+  for(Standard_Integer i = 1; i <= dslin.Length(); i++ )
+    slin.Append(dslin(i));
 }
 
 // correct U parameter of the start point of line on Quadric
@@ -1343,12 +1604,12 @@ static Standard_Real AdjustUFirst(Standard_Real U1,Standard_Real U2)
     else {
       Standard_Real uu = U2;
       if( U2 > (2.*M_PI) )
-       while( uu > (2.*M_PI) )
-         uu -= (2.*M_PI);
+        while( uu > (2.*M_PI) )
+          uu -= (2.*M_PI);
       else 
-       while( uu < 0.)
-         uu += (2.*M_PI);
-      
+        while( uu < 0.)
+          uu += (2.*M_PI);
+
       u = ( uu < ((2.*M_PI)-uu) ) ? 0. : (2.*M_PI);
     }
   }
@@ -1359,12 +1620,12 @@ static Standard_Real AdjustUFirst(Standard_Real U1,Standard_Real U2)
     else {
       Standard_Real uu = U2;
       if( U2 > (2.*M_PI) )
-       while( uu > (2.*M_PI) )
-         uu -= (2.*M_PI);
+        while( uu > (2.*M_PI) )
+          uu -= (2.*M_PI);
       else 
-       while( uu < 0.)
-         uu += (2.*M_PI);
-      
+        while( uu < 0.)
+          uu += (2.*M_PI);
+
       u = ( uu < ((2.*M_PI)-uu) ) ? 0. : (2.*M_PI);
     }
   }
@@ -1372,48 +1633,28 @@ static Standard_Real AdjustUFirst(Standard_Real U1,Standard_Real U2)
   else {
     if(U1 < 0.)
       while(u < 0.)
-       u += 2.*M_PI;
+        u += 2.*M_PI;
     if(U1 > (2.*M_PI))
       while(u > (2.*M_PI))
-       u -= (2.*M_PI);
+        u -= (2.*M_PI);
   }
 
   return u;
 }
 
-// adjust U parameters on Quadric
-static Standard_Real AdjustUNext(Standard_Real Un,Standard_Real Up)
-{
-  Standard_Real u = Un;
-  if( Un < 0. )
-    while(u < 0.)
-      u += (2.*M_PI);
-  else if( Un > (2.*M_PI) )
-    while( u > (2.*M_PI) )
-      u -= (2.*M_PI);
-  else if(Un == 0. || fabs(Un) <= 1.e-9)
-    u = (fabs(Up) < fabs(2.*M_PI-Up)) ? 0. : (2.*M_PI);
-  else if(Un == (2.*M_PI) || fabs(Un-(2.*M_PI)) <= 1.e-9)
-    u = (fabs(Up) < fabs(2.*M_PI-Up)) ? 0. : (2.*M_PI);
-  else //( Un > 0. && Un < (2.*M_PI) )
-    return u;
-  return u;
-}
-
 // collect vertices, reject equals
-static Handle(IntSurf_LineOn2S) GetVertices(const Handle(IntPatch_WLine)& WLine,
-                                           const Standard_Boolean  ,//IsReversed,
-                                           const Standard_Real     TOL3D,
-                                           const Standard_Real     TOL2D)
+static Handle(IntSurf_LineOn2S) GetVertices(const Handle(IntPatch_PointLine)& thePLine,
+                                            const Standard_Real           TOL3D,
+                                            const Standard_Real           TOL2D)
 {
-//  Standard_Real TOL3D = 1.e-12, TOL2D = 1.e-8;
+  //  Standard_Real TOL3D = 1.e-12, TOL2D = 1.e-8;
 
   Handle(IntSurf_LineOn2S) vertices = new IntSurf_LineOn2S();
 
   Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0.;
   Standard_Integer i = 0, k = 0;
-  Standard_Integer NbVrt = WLine->NbVertex();
-  
+  Standard_Integer NbVrt = thePLine->NbVertex();
+
   TColStd_Array1OfInteger anVrts(1,NbVrt);
   anVrts.Init(0);
 
@@ -1422,34 +1663,34 @@ static Handle(IntSurf_LineOn2S) GetVertices(const Handle(IntPatch_WLine)& WLine,
 
     if( anVrts(i) == -1 ) continue;
 
-    const IntPatch_Point& Pi = WLine->Vertex(i);
+    const IntPatch_Point& Pi = thePLine->Vertex(i);
 
     for(k = (i+1); k <= NbVrt; k++) {
 
       if( anVrts(k) == -1 ) continue;
 
-      const IntPatch_Point& Pk = WLine->Vertex(k);
+      const IntPatch_Point& Pk = thePLine->Vertex(k);
 
       if(Pi.Value().Distance(Pk.Value()) <= TOL3D) {
-       // suggest the points are equal;
-       // test 2d parameters on surface
-       Standard_Boolean sameU1 = Standard_False;
-       Standard_Boolean sameV1 = Standard_False;
-       Standard_Boolean sameU2 = Standard_False;
-       Standard_Boolean sameV2 = Standard_False;
-
-       Pi.ParametersOnS1(U1,V1);
-       Pk.ParametersOnS1(U2,V2);
-       if(fabs(U1-U2) <= TOL2D) sameU1 = Standard_True;
-       if(fabs(V1-V2) <= TOL2D) sameV1 = Standard_True;
-
-       Pi.ParametersOnS2(U1,V1);
-       Pk.ParametersOnS2(U2,V2);
-       if(fabs(U1-U2) <= TOL2D) sameU2 = Standard_True;
-       if(fabs(V1-V2) <= TOL2D) sameV2 = Standard_True;
-
-       if((sameU1 && sameV1) && (sameU2 && sameV2))
-         anVrts(k) = -1;
+        // suggest the points are equal;
+        // test 2d parameters on surface
+        Standard_Boolean sameU1 = Standard_False;
+        Standard_Boolean sameV1 = Standard_False;
+        Standard_Boolean sameU2 = Standard_False;
+        Standard_Boolean sameV2 = Standard_False;
+
+        Pi.ParametersOnS1(U1,V1);
+        Pk.ParametersOnS1(U2,V2);
+        if(fabs(U1-U2) <= TOL2D) sameU1 = Standard_True;
+        if(fabs(V1-V2) <= TOL2D) sameV1 = Standard_True;
+
+        Pi.ParametersOnS2(U1,V1);
+        Pk.ParametersOnS2(U2,V2);
+        if(fabs(U1-U2) <= TOL2D) sameU2 = Standard_True;
+        if(fabs(V1-V2) <= TOL2D) sameV2 = Standard_True;
+
+        if((sameU1 && sameV1) && (sameU2 && sameV2))
+          anVrts(k) = -1;
       }
     }
   }
@@ -1457,117 +1698,14 @@ static Handle(IntSurf_LineOn2S) GetVertices(const Handle(IntPatch_WLine)& WLine,
   // copy further processed vertices
   for(i = 1; i <= NbVrt; i++) {
     if( anVrts(i) == -1 ) continue;
-    vertices->Add(WLine->Vertex(i).PntOn2S());
+    vertices->Add(thePLine->Vertex(i).PntOn2S());
   }
   return vertices;
 }
 
-static Standard_Boolean AreSamePoints(const IntSurf_PntOn2S& P1,
-                                     const IntSurf_PntOn2S& P2)
-{
-  Standard_Boolean result = Standard_False;
-  Standard_Real T2D = 1.e-9, T3D = 1.e-8;
-  const gp_Pnt& P3D1 = P1.Value();
-  const gp_Pnt& P3D2 = P2.Value();
-  if(P3D1.Distance(P3D2) <= T3D) {
-    Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0., U3 = 0., V3 = 0., U4 = 0., V4 = 0.;
-    P1.ParametersOnS1(U1,V1);
-    P1.ParametersOnS2(U2,V2);
-    P2.ParametersOnS1(U3,V3);
-    P2.ParametersOnS2(U4,V4);
-    gp_Pnt2d P2D11(U1,V1);
-    gp_Pnt2d P2D12(U2,V2);
-    gp_Pnt2d P2D21(U3,V3);
-    gp_Pnt2d P2D22(U4,V4);
-    Standard_Boolean sameS1 = (P2D11.Distance(P2D21) <= T2D) ? Standard_True : Standard_False;
-    Standard_Boolean sameS2 = (P2D12.Distance(P2D22) <= T2D) ? Standard_True : Standard_False;
-    if(sameS1 && sameS2)
-      result = Standard_True;
-  }
-  return result;
-}
-
-static void ForcedPurgePoints(Handle(IntSurf_LineOn2S)& Result,
-                             Standard_Boolean          IsReversed,
-                             IntSurf_Quadric&          Quad)
-{
-  if(Result->NbPoints() <= 30) return;
-  Standard_Integer Index = 0, IndexLimF = 8, IndexLimL = 8;
-
-  if(Quad.TypeQuadric() == GeomAbs_Cone) {
-    Standard_Real Uapx = 0., Vapx = 0.;
-    Quad.Parameters(Quad.Cone().Apex(),Uapx,Vapx);
-    Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
-    if(IsReversed) {
-      Result->Value(1).ParametersOnS2(U1,V1);
-      Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
-    }
-    else {
-      Result->Value(1).ParametersOnS2(U1,V1);
-      Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
-    }
-
-    if(fabs(V1-Vapx) <= 1.e-3)
-      IndexLimF = 12;
-    if(fabs(V2-Vapx) <= 1.e-3)
-      IndexLimL = 12;
-  }
-
-  if(Quad.TypeQuadric() == GeomAbs_Sphere) {
-    Standard_Real Vapx1 = M_PI/2., Vapx2 = -M_PI/2.;
-    Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
-    if(IsReversed) {
-      Result->Value(1).ParametersOnS2(U1,V1);
-      Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
-    }
-    else {
-      Result->Value(1).ParametersOnS2(U1,V1);
-      Result->Value(Result->NbPoints()).ParametersOnS2(U2,V2);
-    }
-
-    if(fabs(V1-Vapx1) <= 1.e-3 || fabs(V1-Vapx2) <= 1.e-3)
-      IndexLimF = 12;
-    if(fabs(V2-Vapx1) <= 1.e-3 || fabs(V2-Vapx2) <= 1.e-3)
-      IndexLimL = 12;
-  }
-
-  while(Result->NbPoints() > 2 && Index < IndexLimF) {
-    Result->RemovePoint(2);
-    Index++;
-  }
-  Index = 0;
-  while(Result->NbPoints() > 2 && Index < IndexLimL) {
-    Result->RemovePoint(Result->NbPoints()-1);
-    Index++;
-  }
-}
-
-// DEBUG FUNCTION !!!
-#if 0
-static void DumpLine(Handle(IntSurf_LineOn2S)& Line,
-                    Standard_Boolean          IsReversed,
-                    Standard_Integer          Number)
-{
-  cout << "DUMP LINE" << endl;
-  Standard_Integer i;
-  Standard_Real U,V;
-  for(i = 1; i <= Line->NbPoints(); i++) {
-    if(i <= Number || i >= (Line->NbPoints()-Number)) {
-      if(IsReversed)
-        Line->Value(i).ParametersOnS2(U,V); // S2 - quadric
-      else
-        Line->Value(i).ParametersOnS1(U,V); // S1 - quadric
-      cout << "point p" << i << " " << U << " " << V << endl;
-    }
-  }
-  cout << endl;
-}
-#endif
-// DEBUG FUNCTION !!!
-
 static void SearchVertices(const Handle(IntSurf_LineOn2S)& Line,
-                          const Handle(IntSurf_LineOn2S)& Vertices,
-                          TColStd_Array1OfInteger&        PTypes)
+  const Handle(IntSurf_LineOn2S)& Vertices,
+  TColStd_Array1OfInteger&        PTypes)
 {
   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
   Standard_Integer ip = 0, iv = 0;
@@ -1576,9 +1714,9 @@ static void SearchVertices(const Handle(IntSurf_LineOn2S)& Line,
     Standard_Integer type = 0;
     for(iv = 1; iv <= nbv; iv++) {
       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
-      if(AreSamePoints(aP,aV)) {
-       type = iv; 
-       break;
+      if(aP.IsSame(aV, Precision::Confusion(), Precision::PConfusion())) {
+        type = iv; 
+        break;
       }
     }
     PTypes(ip) = type;
@@ -1586,7 +1724,7 @@ static void SearchVertices(const Handle(IntSurf_LineOn2S)& Line,
 }
 
 static inline Standard_Boolean IsSeamParameter(const Standard_Real U,
-                                              const Standard_Real TOL2D)
+  const Standard_Real TOL2D)
 {
   return (fabs(U) <= TOL2D || fabs(2.*M_PI - U) <= TOL2D);
 }
@@ -1597,21 +1735,21 @@ static inline Standard_Real AdjustU(const Standard_Real U)
   if(u < 0. || u > DBLPI) {
     if(u < 0.)
       while(u < 0.)
-       u += DBLPI;
+        u += DBLPI;
     else
       while(u > DBLPI)
-       u -= DBLPI;
+        u -= DBLPI;
   }
   return u;
 }
 
 static inline void Correct2DBounds(const Standard_Real UF,
-                                  const Standard_Real UL,
-                                  const Standard_Real VF,
-                                  const Standard_Real VL,
-                                  const Standard_Real TOL2D,
-                                  Standard_Real&      U,
-                                  Standard_Real&      V)
+  const Standard_Real UL,
+  const Standard_Real VF,
+  const Standard_Real VL,
+  const Standard_Real TOL2D,
+  Standard_Real&      U,
+  Standard_Real&      V)
 {
   Standard_Real Eps = 1.e-16;
   Standard_Real dUF = fabs(U - UF);
@@ -1625,9 +1763,9 @@ static inline void Correct2DBounds(const Standard_Real UF,
 }
 
 static void AdjustLine(Handle(IntSurf_LineOn2S)& Line,
-                      const Standard_Boolean    IsReversed,
-                      const Handle(Adaptor3d_HSurface)&         QSurf,
-                      const Standard_Real       TOL2D)
+  const Standard_Boolean    IsReversed,
+  const Handle(Adaptor3d_HSurface)&         QSurf,
+  const Standard_Real       TOL2D)
 {
   Standard_Real VF = QSurf->FirstVParameter();
   Standard_Real VL = QSurf->LastVParameter();
@@ -1653,10 +1791,10 @@ static void AdjustLine(Handle(IntSurf_LineOn2S)& Line,
 }
 
 static Standard_Boolean InsertSeamVertices(Handle(IntSurf_LineOn2S)&       Line,
-                                          const Standard_Boolean          IsReversed,
-                                          Handle(IntSurf_LineOn2S)&       Vertices,
-                                          const TColStd_Array1OfInteger&  PTypes,
-                                          const Standard_Real             TOL2D)
+  const Standard_Boolean          IsReversed,
+  Handle(IntSurf_LineOn2S)&       Vertices,
+  const TColStd_Array1OfInteger&  PTypes,
+  const Standard_Real             TOL2D)
 {
   Standard_Boolean result = Standard_False;
   Standard_Integer ip = 0, nbp = Line->NbPoints();
@@ -1666,116 +1804,116 @@ static Standard_Boolean InsertSeamVertices(Handle(IntSurf_LineOn2S)&       Line,
     if(ipt != 0) {
       const IntSurf_PntOn2S& aP = Line->Value(ip);
       if(IsReversed)
-       aP.ParametersOnS2(U,V); // S2 - quadric
+        aP.ParametersOnS2(U,V); // S2 - quadric
       else
-       aP.ParametersOnS1(U,V); // S1 - quadric
+        aP.ParametersOnS1(U,V); // S1 - quadric
       U = AdjustU(U);
       if(IsSeamParameter(U,TOL2D)) {
-       if(ip == 1 || ip == nbp) {
-         Standard_Real U1 = 0., V1 = 0.;
-         Standard_Integer ipp = (ip == 1) ? (ip+1) : (ip-1);
-         if(IsReversed)
-           Line->Value(ipp).ParametersOnS2(U1,V1); // S2 - quadric
-         else
-           Line->Value(ipp).ParametersOnS1(U1,V1); // S1 - quadric
-         Standard_Real u = AdjustUFirst(U,U1);
-         if(fabs(u-U) >= 1.5*M_PI) {
-           Standard_Real U2 = 0., V2 = 0.;
-           if(IsReversed) {
-             Line->Value(ip).ParametersOnS1(U2,V2); // prm
-             Line->SetUV(ip,Standard_False,u,V);
-             Line->SetUV(ip,Standard_True,U2,V2);
-           }
-           else {
-             Line->Value(ip).ParametersOnS2(U2,V2); // prm
-             Line->SetUV(ip,Standard_True,u,V);
-             Line->SetUV(ip,Standard_False,U2,V2);
-           }
-         }
-       }
-       else {
-         Standard_Integer ipp = ip - 1;
-         Standard_Integer ipn = ip + 1;
-         Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
-         if(IsReversed) {
-           Line->Value(ipp).ParametersOnS2(U1,V1); // quad
-           Line->Value(ipn).ParametersOnS2(U2,V2); // quad
-         }
-         else {
-           Line->Value(ipp).ParametersOnS1(U1,V1); // quad
-           Line->Value(ipn).ParametersOnS1(U2,V2); // quad
-         }
-         U1 = AdjustU(U1);
-         U2 = AdjustU(U2);
-         Standard_Boolean pnearZero = (fabs(U1) < fabs(2.*M_PI-U1)) ? Standard_True : Standard_False;
-         Standard_Boolean cnearZero = (fabs(U) < fabs(2.*M_PI-U)) ? Standard_True : Standard_False;
-         if(pnearZero == cnearZero) {
-           if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
-             Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
-             IntSurf_PntOn2S nP;
-             nP.SetValue(aP.Value());
-             Standard_Real U3 = 0., V3 = 0.;
-             if(IsReversed) {
-               Line->Value(ip).ParametersOnS1(U3,V3); // prm
-               nP.SetValue(Standard_False,nU,V);
-               nP.SetValue(Standard_True,U3,V3);
-             }
-             else {
-               Line->Value(ip).ParametersOnS2(U3,V3); // prm
-               nP.SetValue(Standard_True,nU,V);
-               nP.SetValue(Standard_False,U3,V3);
-             }
-             Line->InsertBefore(ipn,nP);
-             Vertices->Add(nP);
-             result = Standard_True;
-             break;
-           }
-         }
-         else {
-           if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
-             Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
-             IntSurf_PntOn2S nP;
-             nP.SetValue(aP.Value());
-             Standard_Real U3 = 0., V3 = 0.;
-             if(IsReversed) {
-               Line->Value(ip).ParametersOnS1(U3,V3); // prm
-               nP.SetValue(Standard_False,nU,V);
-               nP.SetValue(Standard_True,U3,V3);
-             }
-             else {
-               Line->Value(ip).ParametersOnS2(U3,V3); // prm
-               nP.SetValue(Standard_True,nU,V);
-               nP.SetValue(Standard_False,U3,V3);
-             }
-             Line->InsertBefore(ip,nP);
-             Vertices->Add(nP);
-             result = Standard_True;
-             break;
-           }
-           else {
-//           Line->InsertBefore(ip,Line->Value(ipn));
-//           Line->RemovePoint(ip+2);
-//           result = Standard_True;
-//           cout << "swap vertex " << endl;
-//           break;
-           }
-         }
-       }
+        if(ip == 1 || ip == nbp) {
+          Standard_Real U1 = 0., V1 = 0.;
+          Standard_Integer ipp = (ip == 1) ? (ip+1) : (ip-1);
+          if(IsReversed)
+            Line->Value(ipp).ParametersOnS2(U1,V1); // S2 - quadric
+          else
+            Line->Value(ipp).ParametersOnS1(U1,V1); // S1 - quadric
+          Standard_Real u = AdjustUFirst(U,U1);
+          if(fabs(u-U) >= 1.5*M_PI) {
+            Standard_Real U2 = 0., V2 = 0.;
+            if(IsReversed) {
+              Line->Value(ip).ParametersOnS1(U2,V2); // prm
+              Line->SetUV(ip,Standard_False,u,V);
+              Line->SetUV(ip,Standard_True,U2,V2);
+            }
+            else {
+              Line->Value(ip).ParametersOnS2(U2,V2); // prm
+              Line->SetUV(ip,Standard_True,u,V);
+              Line->SetUV(ip,Standard_False,U2,V2);
+            }
+          }
+        }
+        else {
+          Standard_Integer ipp = ip - 1;
+          Standard_Integer ipn = ip + 1;
+          Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
+          if(IsReversed) {
+            Line->Value(ipp).ParametersOnS2(U1,V1); // quad
+            Line->Value(ipn).ParametersOnS2(U2,V2); // quad
+          }
+          else {
+            Line->Value(ipp).ParametersOnS1(U1,V1); // quad
+            Line->Value(ipn).ParametersOnS1(U2,V2); // quad
+          }
+          U1 = AdjustU(U1);
+          U2 = AdjustU(U2);
+          Standard_Boolean pnearZero = (fabs(U1) < fabs(2.*M_PI-U1)) ? Standard_True : Standard_False;
+          Standard_Boolean cnearZero = (fabs(U) < fabs(2.*M_PI-U)) ? Standard_True : Standard_False;
+          if(pnearZero == cnearZero) {
+            if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
+              Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
+              IntSurf_PntOn2S nP;
+              nP.SetValue(aP.Value());
+              Standard_Real U3 = 0., V3 = 0.;
+              if(IsReversed) {
+                Line->Value(ip).ParametersOnS1(U3,V3); // prm
+                nP.SetValue(Standard_False,nU,V);
+                nP.SetValue(Standard_True,U3,V3);
+              }
+              else {
+                Line->Value(ip).ParametersOnS2(U3,V3); // prm
+                nP.SetValue(Standard_True,nU,V);
+                nP.SetValue(Standard_False,U3,V3);
+              }
+              Line->InsertBefore(ipn,nP);
+              Vertices->Add(nP);
+              result = Standard_True;
+              break;
+            }
+          }
+          else {
+            if(!IsSeamParameter(U2,TOL2D) && !IsSeamParameter(U1,TOL2D)) {
+              Standard_Real nU = (cnearZero) ? (2.*M_PI) : 0.;
+              IntSurf_PntOn2S nP;
+              nP.SetValue(aP.Value());
+              Standard_Real U3 = 0., V3 = 0.;
+              if(IsReversed) {
+                Line->Value(ip).ParametersOnS1(U3,V3); // prm
+                nP.SetValue(Standard_False,nU,V);
+                nP.SetValue(Standard_True,U3,V3);
+              }
+              else {
+                Line->Value(ip).ParametersOnS2(U3,V3); // prm
+                nP.SetValue(Standard_True,nU,V);
+                nP.SetValue(Standard_False,U3,V3);
+              }
+              Line->InsertBefore(ip,nP);
+              Vertices->Add(nP);
+              result = Standard_True;
+              break;
+            }
+            else {
+              // Line->InsertBefore(ip,Line->Value(ipn));
+              // Line->RemovePoint(ip+2);
+              // result = Standard_True;
+              // cout << "swap vertex " << endl;
+              // break;
+            }
+          }
+        }
       }
     }
   }
   return result;
 }
 
-static void ToSmooth(Handle(IntSurf_LineOn2S)& Line,
-                    Standard_Boolean          IsReversed,
-                    IntSurf_Quadric&          Quad,
-                    Standard_Boolean          IsFirst,
-                     Standard_Real&            D3D)
+static void ToSmooth( const Handle(IntSurf_LineOn2S)& Line,
+                      const Standard_Boolean          IsReversed,
+                      const IntSurf_Quadric&          Quad,
+                      const Standard_Boolean          IsFirst,
+                      Standard_Real&                  D3D)
 {
   if(Line->NbPoints() <= 10)
     return;
-  
+
   D3D = 0.;
   Standard_Integer NbTestPnts = Line->NbPoints() / 5;
   if(NbTestPnts < 5) NbTestPnts = 5;
@@ -1795,7 +1933,7 @@ static void ToSmooth(Handle(IntSurf_LineOn2S)& Line,
     }
     DDU += fabs(fabs(Uc)-fabs(Un));
     DDV += fabs(fabs(Vc)-fabs(Vn));
-    
+
     if(ip > startp) {
       Standard_Real DP = Line->Value(ip).Value().Distance(Line->Value(ip-1).Value());
       D3D += DP;
@@ -1804,7 +1942,7 @@ static void ToSmooth(Handle(IntSurf_LineOn2S)& Line,
 
   DDU /= (Standard_Real) NbTestPnts + 1;
   DDV /= (Standard_Real) NbTestPnts + 1;
-  
+
   D3D /= (Standard_Real) NbTestPnts + 1;
 
 
@@ -1813,7 +1951,6 @@ static void ToSmooth(Handle(IntSurf_LineOn2S)& Line,
   Standard_Integer Index3 = (IsFirst) ? 3 : (Line->NbPoints()-2);
 
   Standard_Boolean doU = Standard_False;
-  Standard_Boolean doV = Standard_False;
 
   Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0., U3 = 0., V3 = 0.;
 
@@ -1830,15 +1967,15 @@ static void ToSmooth(Handle(IntSurf_LineOn2S)& Line,
 
   if(!doU && Quad.TypeQuadric() == GeomAbs_Sphere) {
     if(fabs(fabs(U1)-fabs(U2)) > (M_PI/16.)) doU = Standard_True;
-    
+
     if(doU && (fabs(U1) <= 1.e-9 || fabs(U1-2.*M_PI) <= 1.e-9)) {
       if(fabs(V1-M_PI/2.) <= 1.e-9 || fabs(V1+M_PI/2.) <= 1.e-9) {}
       else {
-       doU = Standard_False;
+        doU = Standard_False;
       }
     }
   }
-  
+
   if(Quad.TypeQuadric() == GeomAbs_Cone) {
     Standard_Real Uapx = 0., Vapx = 0.;
     Quad.Parameters(Quad.Cone().Apex(),Uapx,Vapx);
@@ -1848,13 +1985,11 @@ static void ToSmooth(Handle(IntSurf_LineOn2S)& Line,
     if(doU && (fabs(U1) <= 1.e-9 || fabs(U1-2.*M_PI) <= 1.e-9)) {
       if(fabs(V1-Vapx) <= 1.e-9) {}
       else {
-       doU = Standard_False;
+        doU = Standard_False;
       }
     }
   }
 
-  if(fabs(fabs(V1)-fabs(V2)) > DDV) doV = Standard_True;
-
   if(doU) {
     Standard_Real dU = Min((DDU/10.),5.e-8);
     Standard_Real U = (U2 > U3) ? (U2 + dU) : (U2 - dU);
@@ -1870,8 +2005,8 @@ static Standard_Boolean TestMiddleOnPrm(const IntSurf_PntOn2S& aP,
                                         const IntSurf_PntOn2S& aV,
                                         const Standard_Boolean IsReversed,
                                         const Standard_Real    ArcTol,
-                                        Handle(Adaptor3d_TopolTool)&  PDomain)
-                                        
+                                        const Handle(Adaptor3d_TopolTool)&  PDomain)
+
 {
   Standard_Boolean result = Standard_False;
   Standard_Real Up = 0., Vp = 0., Uv = 0., Vv = 0.;
@@ -1891,16 +2026,16 @@ static Standard_Boolean TestMiddleOnPrm(const IntSurf_PntOn2S& aP,
   return result;
 }
 
-static void VerifyVertices(Handle(IntSurf_LineOn2S)& Line,
-                           Standard_Boolean          IsReversed,
-                           Handle(IntSurf_LineOn2S)& Vertices,
-                           Standard_Real             TOL2D,
-                           const Standard_Real       ArcTol,
-                           Handle(Adaptor3d_TopolTool)&     PDomain,
-                           IntSurf_PntOn2S&          VrtF,
-                           Standard_Boolean&         AddFirst,
-                           IntSurf_PntOn2S&          VrtL,
-                           Standard_Boolean&         AddLast)
+static void VerifyVertices( const Handle(IntSurf_LineOn2S)&    Line,
+                            const Standard_Boolean             IsReversed,
+                            const Handle(IntSurf_LineOn2S)&    Vertices,
+                            const Standard_Real                TOL2D,
+                            const Standard_Real       ArcTol,
+                            const Handle(Adaptor3d_TopolTool)& PDomain,
+                            IntSurf_PntOn2S&          VrtF,
+                            Standard_Boolean&         AddFirst,
+                            IntSurf_PntOn2S&          VrtL,
+                            Standard_Boolean&         AddLast)
 {
   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
   Standard_Integer FIndexSame = 0, FIndexNear = 0, LIndexSame = 0, LIndexNear = 0;
@@ -1938,7 +2073,7 @@ static void VerifyVertices(Handle(IntSurf_LineOn2S)& Line,
 
   for(iv = 1; iv <= nbv; iv++) {
     const IntSurf_PntOn2S& aV = Vertices->Value(iv);
-    if(AreSamePoints(aPF,aV)) {
+    if(aPF.IsSame(aV, Precision::Confusion(), Precision::PConfusion())) {
       FIndexSame = iv;
       break;
     }
@@ -1971,7 +2106,7 @@ static void VerifyVertices(Handle(IntSurf_LineOn2S)& Line,
 
   for(iv = 1; iv <= nbv; iv++) {
     const IntSurf_PntOn2S& aV = Vertices->Value(iv);
-    if(AreSamePoints(aPL,aV)) {
+    if(aPL.IsSame(aV, Precision::Confusion(), Precision::PConfusion())) {
       LIndexSame = iv;
       break;
     }
@@ -2047,7 +2182,7 @@ static void VerifyVertices(Handle(IntSurf_LineOn2S)& Line,
       }
     }
   }
-  
+
   if(LIndexSame == 0) {
     if(LIndexNear != 0) {
       const IntSurf_PntOn2S& aV = Vertices->Value(LIndexNear);
@@ -2093,12 +2228,12 @@ static void VerifyVertices(Handle(IntSurf_LineOn2S)& Line,
 }
 
 static Standard_Boolean AddVertices(Handle(IntSurf_LineOn2S)& Line,
-                                    const IntSurf_PntOn2S&    VrtF,
-                                    const Standard_Boolean    AddFirst,
-                                    const IntSurf_PntOn2S&    VrtL,
-                                    const Standard_Boolean    AddLast,
-                                    const Standard_Real       D3DF,
-                                    const Standard_Real       D3DL)
+  const IntSurf_PntOn2S&    VrtF,
+  const Standard_Boolean    AddFirst,
+  const IntSurf_PntOn2S&    VrtL,
+  const Standard_Boolean    AddLast,
+  const Standard_Real       D3DF,
+  const Standard_Real       D3DL)
 {
   Standard_Boolean result = Standard_False;
   if(AddFirst) {
@@ -2117,49 +2252,77 @@ static Standard_Boolean AddVertices(Handle(IntSurf_LineOn2S)& Line,
   }
   return result;
 }
 
-static void PutIntVertices(Handle(IntPatch_Line)&    Line,
-                           Handle(IntSurf_LineOn2S)& Result,
-                           Standard_Boolean          ,//IsReversed,
-                           Handle(IntSurf_LineOn2S)& Vertices,
-                           const Standard_Real       ArcTol)
+
+static void PutIntVertices(const Handle(IntPatch_PointLine)&    Line,
+  Handle(IntSurf_LineOn2S)& Result,
+  Standard_Boolean          theIsReversed,
+  Handle(IntSurf_LineOn2S)& Vertices,
+  const Standard_Real       ArcTol)
 {
   Standard_Integer nbp = Result->NbPoints(), nbv = Vertices->NbPoints();
 
   if(nbp < 3)
     return;
 
-  Handle(IntPatch_WLine)& WLine = (*((Handle(IntPatch_WLine)*)&Line));
+  const Handle(IntPatch_RLine) aRLine = Handle(IntPatch_RLine)::DownCast(Line);
+
   Standard_Integer ip = 0, iv = 0;
   gp_Pnt aPnt;
   IntPatch_Point thePnt;
   Standard_Real U1 = 0., V1 = 0., U2 = 0., V2 = 0.;
-  
+
   for(ip = 2; ip <= (nbp-1); ip++) {
     const IntSurf_PntOn2S& aP = Result->Value(ip);
     for(iv = 1; iv <= nbv; iv++) {
       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
-      if(AreSamePoints(aP,aV)) {
+      if(aP.IsSame(aV, Precision::Confusion(), Precision::PConfusion())) {
         aPnt = Result->Value(ip).Value();
-       Result->Value(ip).ParametersOnS1(U1,V1);
-       Result->Value(ip).ParametersOnS2(U2,V2);
-       thePnt.SetValue(aPnt,ArcTol,Standard_False);
-       thePnt.SetParameters(U1,V1,U2,V2);
-       thePnt.SetParameter((Standard_Real)ip);
-       WLine->AddVertex(thePnt);
+        Result->Value(ip).ParametersOnS1(U1,V1);
+        Result->Value(ip).ParametersOnS2(U2,V2);
+        thePnt.SetValue(aPnt,ArcTol,Standard_False);
+        thePnt.SetParameters(U1,V1,U2,V2);
+        
+        Standard_Real aParam = (Standard_Real)ip;
+
+        if(!aRLine.IsNull())
+        {
+          //In fact, aRLine is always on the parametric surface.
+          //If (theIsReversed == TRUE) then (U1, V1) - point on
+          //parametric surface, otherwise - point on quadric.
+          const Handle(Adaptor2d_HCurve2d)& anArc = aRLine->IsArcOnS1() ?
+                                                    aRLine->ArcOnS1() :
+                                                    aRLine->ArcOnS2();
+
+          const gp_Lin2d aLin(anArc->Curve2d().Line());
+          gp_Pnt2d aPSurf;
+
+          if(theIsReversed)
+          {
+            aPSurf.SetCoord(U1, V1);
+          }
+          else
+          {
+            aPSurf.SetCoord(U2, V2);
+          }
+
+          aParam = ElCLib::Parameter(aLin, aPSurf);
+        }
+        
+        thePnt.SetParameter(aParam);
+        Line->AddVertex(thePnt);
       }
     }
   }
 }
 
 static Standard_Boolean HasInternals(Handle(IntSurf_LineOn2S)& Line,
-                                     Handle(IntSurf_LineOn2S)& Vertices)
+  Handle(IntSurf_LineOn2S)& Vertices)
 {
   Standard_Integer nbp = Line->NbPoints(), nbv = Vertices->NbPoints();
   Standard_Integer ip = 0, iv = 0;
   Standard_Boolean result = Standard_False;
-  
+
   if(nbp < 3)
     return result;
 
@@ -2167,7 +2330,7 @@ static Standard_Boolean HasInternals(Handle(IntSurf_LineOn2S)& Line,
     const IntSurf_PntOn2S& aP = Line->Value(ip);
     for(iv = 1; iv <= nbv; iv++) {
       const IntSurf_PntOn2S& aV = Vertices->Value(iv);
-      if(AreSamePoints(aP,aV)) {
+      if(aP.IsSame(aV, Precision::Confusion(), Precision::PConfusion())) {
         result = Standard_True;
         break;
       }
@@ -2175,21 +2338,21 @@ static Standard_Boolean HasInternals(Handle(IntSurf_LineOn2S)& Line,
     if(result)
       break;
   }
-  
+
   return result;
 }
 static Handle(IntPatch_WLine) MakeSplitWLine (Handle(IntPatch_WLine)&        WLine,
-                                        Standard_Boolean         Tang,
-                                        IntSurf_TypeTrans        Trans1,
-                                        IntSurf_TypeTrans        Trans2,
-                                        Standard_Real            ArcTol,
-                                        Standard_Integer         ParFirst,
-                                        Standard_Integer         ParLast)
+  Standard_Boolean         Tang,
+  IntSurf_TypeTrans        Trans1,
+  IntSurf_TypeTrans        Trans2,
+  Standard_Real            ArcTol,
+  Standard_Integer         ParFirst,
+  Standard_Integer         ParLast)
 {
   Handle(IntSurf_LineOn2S) SLine = WLine->Curve();
   Handle(IntSurf_LineOn2S) sline = new IntSurf_LineOn2S();
- Standard_Integer ip = 0;
+
 Standard_Integer ip = 0;
   for(ip = ParFirst; ip <= ParLast; ip++)
     sline->Add(SLine->Value(ip));
 
@@ -2207,7 +2370,7 @@ static Handle(IntPatch_WLine) MakeSplitWLine (Handle(IntPatch_WLine)&        WLi
   TPntF.SetParameter(1.);
   wline->AddVertex(TPntF);
   wline->SetFirstPoint(1);
-       
+
   aSPnt =  sline->Value(sline->NbPoints()).Value();
   sline->Value(sline->NbPoints()).ParametersOnS1(uu1,vv1);
   sline->Value(sline->NbPoints()).ParametersOnS2(uu2,vv2);
@@ -2215,17 +2378,17 @@ static Handle(IntPatch_WLine) MakeSplitWLine (Handle(IntPatch_WLine)&        WLi
   TPntL.SetParameters(uu1,vv1,uu2,vv2);
   TPntL.SetParameter((Standard_Real)sline->NbPoints());
   wline->AddVertex(TPntL);
-  wline->SetLastPoint(sline->NbPoints());
+  wline->SetLastPoint(wline->NbVertex());
 
   return wline;
 }
 
 static Standard_Boolean SplitOnSegments(Handle(IntPatch_WLine)&        WLine,
-                                        Standard_Boolean         Tang,
-                                        IntSurf_TypeTrans        Trans1,
-                                        IntSurf_TypeTrans        Trans2,
-                                        Standard_Real            ArcTol,
-                                        IntPatch_SequenceOfLine& Segments)
+  Standard_Boolean         Tang,
+  IntSurf_TypeTrans        Trans1,
+  IntSurf_TypeTrans        Trans2,
+  Standard_Real            ArcTol,
+  IntPatch_SequenceOfLine& Segments)
 {
   Standard_Boolean result = Standard_False;
   Segments.Clear();
@@ -2234,12 +2397,15 @@ static Standard_Boolean SplitOnSegments(Handle(IntPatch_WLine)&        WLine,
   if(nbv > 3) {
     Standard_Integer iv = 0;
     for(iv = 1; iv < nbv; iv++) {
-      Standard_Integer firstPar = (Standard_Integer) WLine->Vertex(iv).ParameterOnLine();
-      Standard_Integer lastPar  = (Standard_Integer) WLine->Vertex(iv+1).ParameterOnLine();
+      Standard_Integer firstPar = 
+                    (Standard_Integer) WLine->Vertex(iv).ParameterOnLine();
+      Standard_Integer lastPar  = 
+                    (Standard_Integer) WLine->Vertex(iv+1).ParameterOnLine();
       if((lastPar - firstPar) <= 1)
         continue;
       else {
-        Handle(IntPatch_WLine) splitwline = MakeSplitWLine(WLine,Tang,Trans1,Trans2,ArcTol,firstPar,lastPar);
+        Handle(IntPatch_WLine) splitwline = MakeSplitWLine(WLine,Tang,Trans1,Trans2,
+                                                                ArcTol,firstPar,lastPar);
         Segments.Append(splitwline);
         if(!result)
           result = Standard_True;
@@ -2249,245 +2415,659 @@ static Standard_Boolean SplitOnSegments(Handle(IntPatch_WLine)&        WLine,
   return result;
 }
 
-void DecomposeResult(Handle(IntPatch_Line)&   Line,
-                    Standard_Boolean         IsReversed,
-                    IntSurf_Quadric&         Quad,
-                    Handle(Adaptor3d_TopolTool)&    PDomain,
-                    Handle(Adaptor3d_HSurface)&              QSurf,
-                    Standard_Real            ArcTol,
-                    IntPatch_SequenceOfLine& Lines)
+//=======================================================================
+//function : DecomposeResult
+//purpose  : Split <theLine> in the places where it passes through seam edge
+//            or singularity (apex of cone or pole of sphere).
+//            This passage is detected by jump of U-parameter
+//            from point to point.
+//=======================================================================
+static Standard_Boolean DecomposeResult(const Handle(IntPatch_PointLine)& theLine,
+                                        const Standard_Boolean       IsReversed,
+                                        const IntSurf_Quadric&       theQuad,
+                                        const Handle(Adaptor3d_TopolTool)& thePDomain,
+                                        const Handle(Adaptor3d_HSurface)&  theQSurf, //quadric
+                                        const Handle(Adaptor3d_HSurface)&  thePSurf, //parametric
+                                        const Standard_Real                theArcTol,
+                                        const Standard_Real                theTolTang,
+                                        IntPatch_SequenceOfLine&           theLines)
 {
-  if( Line->ArcType() != IntPatch_Walking ) {
-    Lines.Append(Line);
-    return;
+  if(theLine->ArcType() == IntPatch_Restriction)
+  {
+    const Handle(IntPatch_RLine)& aRL = Handle(IntPatch_RLine)::DownCast(theLine);
+    if(!aRL.IsNull())
+    {
+      const Handle(Adaptor2d_HCurve2d)& anArc = aRL->IsArcOnS1() ?
+                                        aRL->ArcOnS1() :
+                                        aRL->ArcOnS2();
+      if(anArc->Curve2d().GetType() != GeomAbs_Line)
+      {
+        //Restriction line must be isoline.
+        //Other cases are not supported by
+        //existing algorithms.
+
+        return Standard_False;
+      }
+    }
+  }
+  
+  const Standard_Real aDeltaUmax = M_PI_2;
+  const Standard_Real aTOL3D = 1.e-10, 
+                      aTOL2D = Precision::PConfusion(),
+                      aTOL2DS = Precision::PConfusion();
+
+  const Handle(IntSurf_LineOn2S)& aSLine = theLine->Curve();
+
+  if(aSLine->NbPoints() <= 2)
+  {
+    return Standard_False;
   }
+  
+  //Deletes repeated vertices
+  Handle(IntSurf_LineOn2S) aVLine = GetVertices(theLine,aTOL3D,aTOL2D);
+  
+  Handle(IntSurf_LineOn2S) aSSLine(aSLine);
 
-  Handle(IntPatch_WLine)& WLine = (*((Handle(IntPatch_WLine)*)&Line));
-  Handle(IntSurf_LineOn2S) SLine = WLine->Curve();
+  if(aSSLine->NbPoints() <= 1)
+    return Standard_False;
 
-  if(SLine->NbPoints() <= 2) {
-    Lines.Append(Line);
-    return;
+  AdjustLine(aSSLine,IsReversed,theQSurf,aTOL2D);
+
+  if(theLine->ArcType() == IntPatch_Walking)
+  {
+    Standard_Boolean isInserted = Standard_True;
+    while(isInserted)
+    {
+      const Standard_Integer aNbPnts = aSSLine->NbPoints();
+      TColStd_Array1OfInteger aPTypes(1,aNbPnts);
+      SearchVertices(aSSLine,aVLine,aPTypes);
+      isInserted = InsertSeamVertices(aSSLine,IsReversed,aVLine,aPTypes,aTOL2D);
+    }
   }
 
-  Standard_Real TOL3D = 1.e-10, TOL2D = 1.e-8, TOL2DS = 1.e-5;
+  const Standard_Integer aLindex = aSSLine->NbPoints();
+  Standard_Integer aFindex = 1, aBindex = 0;
 
-  Handle(IntSurf_LineOn2S) vline = GetVertices(WLine,IsReversed,TOL3D,TOL2D);
-  Handle(IntSurf_LineOn2S) SSLine = VSplitLine(SLine,IsReversed,QSurf,vline);
+  // build WLine parts (if any)
+  Standard_Boolean flNextLine = Standard_True;
+  Standard_Boolean hasBeenDecomposed = Standard_False;
+  IntPatch_SpecPntType aPrePointExist = IntPatch_SPntNone;
 
-  if(SSLine->NbPoints() <= 1)
-    return;
+  IntSurf_PntOn2S PrePoint;
+  while(flNextLine)
+  {
+    // reset variables
+    flNextLine = Standard_False;
+    Standard_Boolean isDecomposited = Standard_False;
+    Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0.;
 
-  AdjustLine(SSLine,IsReversed,QSurf,TOL2D);
+    Handle(IntSurf_LineOn2S) sline = new IntSurf_LineOn2S();
 
-  Standard_Boolean isInserted = Standard_True;
-  while(isInserted) {
-    Standard_Integer NbPnts = SSLine->NbPoints();
-    TColStd_Array1OfInteger ptypes(1,NbPnts);
-    SearchVertices(SSLine,vline,ptypes);
-    isInserted = InsertSeamVertices(SSLine,IsReversed,vline,ptypes,TOL2D);
-  }
+    //if((Lindex-Findex+1) <= 2 )
+    if((aLindex <= aFindex) && !aPrePointExist)
+    {
+      //break of "while(flNextLine)" cycle
+      break;
+    }
+
+    if(aPrePointExist)
+    {
+      const IntSurf_PntOn2S& aRefPt = aSSLine->Value(aFindex);
+
+      const Standard_Real aURes = theQSurf->UResolution(theArcTol),
+                          aVRes = theQSurf->VResolution(theArcTol);
+
+      const Standard_Real aTol2d = (aPrePointExist == IntPatch_SPntPole) ? -1.0 : 
+              (aPrePointExist == IntPatch_SPntSeamV)? aVRes :
+              (aPrePointExist == IntPatch_SPntSeamUV)? Max(aURes, aVRes) : aURes;
 
-  if(SSLine->NbPoints() == 2) {
-    Standard_Real uu1 = 0., vv1 = 0., uu2 = 0., vv2 = 0.;
-    if(IsReversed) { 
-      SSLine->Value(1).ParametersOnS2(uu1,vv1);
-      SSLine->Value(2).ParametersOnS2(uu2,vv2);
+      if(IntPatch_SpecialPoints::ContinueAfterSpecialPoint(theQSurf, thePSurf, aRefPt,
+                                                              aPrePointExist, aTol2d,
+                                                              PrePoint, IsReversed))
+      {
+        sline->Add(PrePoint);
+      }
+      else
+      {
+        //break of "while(flNextLine)" cycle
+        break;
+      }
     }
-    else {
-      SSLine->Value(1).ParametersOnS1(uu1,vv1);
-      SSLine->Value(2).ParametersOnS1(uu2,vv2);
+
+    aPrePointExist = IntPatch_SPntNone;
+
+    // analyze other points
+    for(Standard_Integer k = aFindex; k <= aLindex; k++)
+    {
+      if( k == aFindex )
+      {
+        PrePoint = aSSLine->Value(k);
+        sline->Add(PrePoint);
+        continue;
+      }
+
+      if(IsReversed)
+      {
+        aSSLine->Value(k).ParametersOnS2(U1,V1);   // S2 - quadric, set U,V by Pnt3D
+      }
+      else
+      {
+        aSSLine->Value(k).ParametersOnS1(U1,V1);    // S1 - quadric, set U,V by Pnt3D
+      }
+
+      aPrePointExist = IsSeamOrPole(theQSurf, aSSLine, IsReversed, k-1, aDeltaUmax);
+
+      if(aPrePointExist != IntPatch_SPntNone)
+      {
+        aBindex = k;
+        isDecomposited = Standard_True;
+        ////
+        const IntSurf_PntOn2S& aRefPt = aSSLine->Value(aBindex-1);
+
+        Standard_Real aCompareTol3D = Precision::Confusion();
+        Standard_Real aCompareTol2D = Precision::PConfusion();
+
+        IntSurf_PntOn2S aNewPoint = aRefPt;
+        IntPatch_SpecPntType aLastType = IntPatch_SPntNone;
+
+        if(aPrePointExist == IntPatch_SPntSeamUV)
+        {
+          aPrePointExist = IntPatch_SPntNone;
+          aLastType = IntPatch_SPntSeamUV;
+          IntPatch_SpecialPoints::AddCrossUVIsoPoint(theQSurf, thePSurf, 
+                                                        aRefPt, theTolTang,
+                                                        aNewPoint, IsReversed);
+        }
+        else if(aPrePointExist == IntPatch_SPntSeamV)
+        {//WLine goes through seam
+          aPrePointExist = IntPatch_SPntNone;
+          aLastType = IntPatch_SPntSeamV;
+
+          //Not quadric point
+          Standard_Real aU0 = 0.0, aV0 = 0.0;
+          //Quadric point
+          Standard_Real aUQuadRef = 0.0, aVQuadRef = 0.0;
+
+          if(IsReversed)
+          {
+            aRefPt.Parameters(aU0, aV0, aUQuadRef, aVQuadRef);
+          }
+          else
+          {
+            aRefPt.Parameters(aUQuadRef, aVQuadRef, aU0, aV0);
+          }
+
+          math_Vector aTol(1, 3), aStartPoint(1,3),
+            anInfBound(1, 3), aSupBound(1, 3);
+
+          //Parameters on parametric surface
+          Standard_Real aUp = 0.0, aVp = 0.0;
+          if(IsReversed)
+          {
+            aSSLine->Value(k).ParametersOnS1(aUp, aVp);
+          }
+          else
+          {
+            aSSLine->Value(k).ParametersOnS2(aUp, aVp);
+          }
+
+          aTol(1) = thePSurf->UResolution(theArcTol);
+          aTol(2) = thePSurf->VResolution(theArcTol);
+          aTol(3) = theQSurf->UResolution(theArcTol);
+          aStartPoint(1) = 0.5*(aU0 + aUp);
+          aStartPoint(2) = 0.5*(aV0 + aVp);
+          aStartPoint(3) = 0.5*(aUQuadRef + U1);
+          anInfBound(1) = thePSurf->FirstUParameter();
+          anInfBound(2) = thePSurf->FirstVParameter();
+          anInfBound(3) = theQSurf->FirstUParameter();
+          aSupBound(1) = thePSurf->LastUParameter();
+          aSupBound(2) = thePSurf->LastVParameter();
+          aSupBound(3) = theQSurf->LastUParameter();
+
+          IntPatch_SpecialPoints::
+                      AddPointOnUorVIso(theQSurf, thePSurf, aRefPt, Standard_False,
+                                        aTol, aStartPoint, anInfBound, aSupBound,
+                                        aNewPoint, IsReversed);
+        }
+        else if(aPrePointExist == IntPatch_SPntPoleSeamU)
+        {
+          aPrePointExist = IntPatch_SPntNone;          
+
+          IntPatch_Point aVert;
+          aVert.SetValue(aRefPt);
+
+          if(IntPatch_SpecialPoints::
+                      AddSingularPole(theQSurf, thePSurf, aRefPt, theTolTang,
+                                      aVert, aNewPoint, IsReversed))
+          {
+            aPrePointExist = IntPatch_SPntPole;
+            aLastType = IntPatch_SPntPole;
+            aCompareTol2D = -1.0;
+          } //if(IntPatch_AddSpecialPoints::AddSingularPole(...))
+          else
+          {//Pole is not an intersection point
+            aPrePointExist = IntPatch_SPntSeamU;
+          }
+        }
+
+        if(aPrePointExist == IntPatch_SPntSeamU)
+        {//WLine goes through seam
+          aPrePointExist = IntPatch_SPntNone;
+          aLastType = IntPatch_SPntSeamU;
+
+          //Not quadric point
+          Standard_Real aU0 = 0.0, aV0 = 0.0;
+          //Quadric point
+          Standard_Real aUQuadRef = 0.0, aVQuadRef = 0.0;
+
+          if(IsReversed)
+          {
+            aRefPt.Parameters(aU0, aV0, aUQuadRef, aVQuadRef);
+          }
+          else
+          {
+            aRefPt.Parameters(aUQuadRef, aVQuadRef, aU0, aV0);
+          }
+
+          math_Vector aTol(1, 3), aStartPoint(1,3),
+                      anInfBound(1, 3), aSupBound(1, 3);
+          
+          //Parameters on parametric surface
+          Standard_Real aUp = 0.0, aVp = 0.0;
+          if(IsReversed)
+          {
+            aSSLine->Value(k).ParametersOnS1(aUp, aVp);
+          }
+          else
+          {
+            aSSLine->Value(k).ParametersOnS2(aUp, aVp);
+          }
+
+          aTol(1) = thePSurf->UResolution(theArcTol);
+          aTol(2) = thePSurf->VResolution(theArcTol);
+          aTol(3) = theQSurf->VResolution(theArcTol);
+          aStartPoint(1) = 0.5*(aU0 + aUp);
+          aStartPoint(2) = 0.5*(aV0 + aVp);
+          aStartPoint(3) = 0.5*(aVQuadRef + V1);
+          anInfBound(1) = thePSurf->FirstUParameter();
+          anInfBound(2) = thePSurf->FirstVParameter();
+          anInfBound(3) = theQSurf->FirstVParameter();
+          aSupBound(1) = thePSurf->LastUParameter();
+          aSupBound(2) = thePSurf->LastVParameter();
+          aSupBound(3) = theQSurf->LastVParameter();
+
+          IntPatch_SpecialPoints::
+                AddPointOnUorVIso(theQSurf, thePSurf, aRefPt, Standard_True, aTol,
+                                  aStartPoint, anInfBound, aSupBound, aNewPoint,
+                                  IsReversed);
+        }
+
+        if(!aNewPoint.IsSame(aRefPt, aCompareTol3D, aCompareTol2D))
+        {
+          sline->Add(aNewPoint);
+          aPrePointExist = aLastType;
+          PrePoint = aNewPoint;
+        }
+        else
+        {
+          if(sline->NbPoints() == 1)
+          {
+            //FIRST point of the sline is the pole of the quadric.
+            //Therefore, there is no point in decomposition.
+
+            PrePoint = aRefPt;
+            aPrePointExist = aLastType;
+          }
+        }
+
+        ////
+        break;
+      } //if(Abs(U1-AnU1) > aDeltaUmax)
+
+      sline->Add(aSSLine->Value(k));
+      PrePoint = aSSLine->Value(k);
+    } //for(Standard_Integer k = aFindex; k <= aLindex; k++)
+
+    //Creation of new line as part of existing theLine.
+    //This part is defined by sline.
+
+    if(sline->NbPoints() == 1)
+    {
+      flNextLine = Standard_True;
+      aFindex = aBindex;
+
+      //Go to the next part of aSSLine
+      //because we cannot create the line
+      //with single point.
+
+      continue;
     }
-    gp_Pnt2d p1(uu1,vv1);
-    gp_Pnt2d p2(uu2,vv2);
-    if(p1.Distance(p2) > 2.e-7) {
-      Standard_Real Dist3D = SSLine->Value(1).Value().Distance(SSLine->Value(2).Value());
-      if(Dist3D > 3.e-7) {
-       gp_Pnt aSPnt;
-       IntPatch_Point TPntF,TPntL;
-       Handle(IntPatch_WLine) wline = new IntPatch_WLine(SSLine,Standard_False,Line->TransitionOnS1(),Line->TransitionOnS2());
-       aSPnt = SSLine->Value(1).Value();
-       SSLine->Value(1).ParametersOnS1(uu1,vv1);
-       SSLine->Value(1).ParametersOnS2(uu2,vv2);
-       TPntF.SetValue(aSPnt,ArcTol,Standard_False);
-       TPntF.SetParameters(uu1,vv1,uu2,vv2);
-       TPntF.SetParameter(1.);
-       wline->AddVertex(TPntF);
-       wline->SetFirstPoint(1);
-       aSPnt =  SSLine->Value(SSLine->NbPoints()).Value();
-       SSLine->Value(SSLine->NbPoints()).ParametersOnS1(uu1,vv1);
-       SSLine->Value(SSLine->NbPoints()).ParametersOnS2(uu2,vv2);
-       TPntL.SetValue(aSPnt,ArcTol,Standard_False);
-       TPntL.SetParameters(uu1,vv1,uu2,vv2);
-       TPntL.SetParameter(SSLine->NbPoints());
-       wline->AddVertex(TPntL);
-       wline->SetLastPoint(SSLine->NbPoints());
-       Lines.Append(wline);
-       return;
+
+    IntSurf_PntOn2S aVF, aVL;
+    Standard_Boolean addVF = Standard_False, addVL = Standard_False;
+    VerifyVertices(sline,IsReversed,aVLine,aTOL2DS,theArcTol,
+                                    thePDomain,aVF,addVF,aVL,addVL);
+
+    Standard_Boolean hasInternals = HasInternals(sline,aVLine);
+
+    Standard_Real D3F = 0., D3L = 0.;
+    ToSmooth(sline,IsReversed,theQuad,Standard_True,D3F);
+    ToSmooth(sline,IsReversed,theQuad,Standard_False,D3L);
+
+    //if(D3F <= 1.5e-7 && sline->NbPoints() >=3) {
+    //  D3F = sline->Value(2).Value().Distance(sline->Value(3).Value());
+    //}
+    //if(D3L <= 1.5e-7 && sline->NbPoints() >=3) {
+    //  D3L = sline->Value(sline->NbPoints()-1).Value().Distance(sline->
+    //                                  Value(sline->NbPoints()-2).Value());
+    //}
+
+    if(addVF || addVL)
+    {
+      Standard_Boolean isAdded = AddVertices(sline,aVF,addVF,aVL,addVL,D3F,D3L);
+      if(isAdded)
+      {
+        ToSmooth(sline,IsReversed,theQuad,Standard_True,D3F);
+        ToSmooth(sline,IsReversed,theQuad,Standard_False,D3L);
+      }
+    }
+
+    if(theLine->ArcType() == IntPatch_Walking)
+    {
+      IntPatch_Point aTPntF, aTPntL;
+
+      Handle(IntPatch_WLine) wline = 
+                          new IntPatch_WLine(sline,Standard_False,
+                          theLine->TransitionOnS1(),theLine->TransitionOnS2());
+
+      gp_Pnt aSPnt(sline->Value(1).Value());
+      sline->Value(1).ParametersOnS1(U1,V1);
+      sline->Value(1).ParametersOnS2(U2,V2);
+      aTPntF.SetValue(aSPnt,theArcTol,Standard_False);
+      aTPntF.SetParameters(U1,V1,U2,V2);
+      aTPntF.SetParameter(1.);
+      wline->AddVertex(aTPntF);
+      wline->SetFirstPoint(1);
+
+      if(hasInternals)
+      {
+        PutIntVertices(wline,sline,IsReversed,aVLine,theArcTol);
+      }
+
+      aSPnt =  sline->Value(sline->NbPoints()).Value();
+      sline->Value(sline->NbPoints()).ParametersOnS1(U1,V1);
+      sline->Value(sline->NbPoints()).ParametersOnS2(U2,V2);
+      aTPntL.SetValue(aSPnt,theArcTol,Standard_False);
+      aTPntL.SetParameters(U1,V1,U2,V2);
+      aTPntL.SetParameter(sline->NbPoints());
+      wline->AddVertex(aTPntL);
+      wline->SetLastPoint(wline->NbVertex());
+
+      IntPatch_SequenceOfLine segm;
+      Standard_Boolean isSplited = SplitOnSegments(wline,Standard_False,
+                      theLine->TransitionOnS1(),theLine->TransitionOnS2(),theArcTol,segm);
+
+      if(!isSplited)
+      {
+        theLines.Append(wline);
       }
       else
-       return;
+      {
+        Standard_Integer nbsegms = segm.Length();
+        Standard_Integer iseg = 0;
+        for(iseg = 1; iseg <= nbsegms; iseg++)
+          theLines.Append(segm(iseg));
+      }
     }
     else
-      return;
-  }
+    {//theLine->ArcType() == IntPatch_Restriction
+      if(!isDecomposited && !hasBeenDecomposed)
+      {
+        //The line has not been changed
+        theLines.Append(Handle(IntPatch_RLine)::DownCast(theLine));
+        return hasBeenDecomposed;
+      }
 
-  Standard_Integer NbPnts = SSLine->NbPoints();
+      IntPatch_Point aTPnt;
+      gp_Pnt2d aPSurf;
+      gp_Pnt aSPnt;
 
-  Standard_Real BSEAM = 1.5*M_PI; // delta U crossing seam
-  Standard_Real BAPEX = M_PI/16.;  // delta U crossing apex
-  
-  Standard_Integer k = 0;
-  Standard_Real U1 = 0., U2 = 0., V1 = 0., V2 = 0., AnU1 = 0., AnV1 = 0., DU1 = 0., DV1 = 0.;
-  Standard_Integer Findex = 1, Lindex = NbPnts, Bindex = 0;
+      Handle(IntPatch_RLine) aRLine = new IntPatch_RLine(*Handle(IntPatch_RLine)::DownCast(theLine));
+      
+      aRLine->ClearVertexes();
+      aRLine->SetCurve(sline);
 
-  gp_Pnt aPnt, aSPnt;
-  IntPatch_Point TPntF,TPntL,TPntS;
+      if(hasInternals)
+      {
+        PutIntVertices(aRLine,sline,IsReversed,aVLine,theArcTol);
+      }
 
-  Handle(IntSurf_LineOn2S) sline = new IntSurf_LineOn2S();
-  Handle(IntPatch_WLine) wline;
-  IntPatch_SequenceOfLine lines;
+      const Handle(Adaptor2d_HCurve2d)& anArc = aRLine->IsArcOnS1() ?
+                                                aRLine->ArcOnS1() :
+                                                aRLine->ArcOnS2();
 
-  // build WLine parts (if any)
-  nextline:;
+      Standard_Real aFPar = anArc->FirstParameter(),
+                    aLPar = anArc->LastParameter();
 
-  //if((Lindex-Findex+1) <= 2 )
-  if((Lindex-Findex+1) <= 1 )
-    return;
+      const IntSurf_PntOn2S &aRFirst = sline->Value(1),
+                            &aRLast = sline->Value(sline->NbPoints());
+
+      const gp_Lin2d aLin(anArc->Curve2d().Line());
+      
+      for(Standard_Integer aFLIndex = 0; aFLIndex < 2; aFLIndex++)
+      {
+        if(aFLIndex == 0)
+        {
+          aRFirst.Parameters(U1, V1, U2, V2);
+          aSPnt.SetXYZ(aRFirst.Value().XYZ());
+        }
+        else
+        {
+          aRLast.Parameters(U1, V1, U2, V2);
+          aSPnt.SetXYZ(aRLast.Value().XYZ());
+        }
+
+        if(IsReversed)
+        {
+          aPSurf.SetCoord(U1, V1);
+        }
+        else
+        {
+          aPSurf.SetCoord(U2, V2);
+        }
 
-  // reset variables
-  Standard_Boolean isDecomposited = Standard_False;
-  Standard_Boolean is2PIDecomposed = Standard_False;
-  U1 = 0.; V1 = 0.; U2 = 0.; V2 = 0.; AnU1 = 0.; AnV1 = 0.; DU1 = 0.; DV1 = 0.;
+        Standard_Real aPar = ElCLib::Parameter(aLin, aPSurf);
 
-  // analyze other points
-  for(k = Findex; k <= Lindex; k++) {
+        if(aFLIndex == 0)
+        {
+          aFPar = Max(aFPar, aPar);
+          aPar = aFPar;
+        }
+        else
+        {
+          aLPar = Min(aLPar, aPar);
+          aPar = aLPar;
+        }
 
-    if( k == Findex ) {
-      if(IsReversed) { 
-       SSLine->Value(k).ParametersOnS2(AnU1,V1);   // S2 - quadric, set U,V by Pnt3D
+        aTPnt.SetParameter(aPar);
+        aTPnt.SetValue(aSPnt,theArcTol,Standard_False);
+        aTPnt.SetParameters(U1, V1, U2, V2);
+
+        aRLine->AddVertex(aTPnt);
       }
-      else { 
-       SSLine->Value(k).ParametersOnS1(AnU1,V1);    // S1 - quadric, set U,V by Pnt3D
+
+      if(aLPar - aFPar > Precision::PConfusion())
+      {
+        aRLine->SetFirstPoint(1);
+        aRLine->SetLastPoint(aRLine->NbVertex());
+
+        anArc->Trim(aFPar, aLPar, theArcTol);
+
+        theLines.Append(aRLine);
       }
-      sline->Add(SSLine->Value(k));
-      AnV1 = V1;
-      continue;
     }
 
-    if(IsReversed) { 
-      SSLine->Value(k).ParametersOnS2(U1,V1);   // S2 - quadric, set U,V by Pnt3D
+    if(isDecomposited)
+    {
+      aFindex = aBindex;
+      flNextLine = hasBeenDecomposed = Standard_True;
     }
-    else { 
-      SSLine->Value(k).ParametersOnS1(U1,V1);    // S1 - quadric, set U,V by Pnt3D
+  }
+
+  return hasBeenDecomposed;
+}
+
+//=======================================================================
+//function : CheckSegmSegm
+//purpose  : Returns TRUE if the segment [theParF, theParL] is included
+//            in the segment [theRefParF, theRefParL] segment.
+//=======================================================================
+static Standard_Boolean CheckSegmSegm(const Standard_Real theRefParF,
+                                      const Standard_Real theRefParL,
+                                      const Standard_Real theParF,
+                                      const Standard_Real theParL)
+{
+  if((theParF < theRefParF) || (theParF > theRefParL))
+  {
+    return Standard_False;
+  }
+
+  if((theParL < theRefParF) || (theParL > theRefParL))
+  {
+    return Standard_False;
+  }
+
+  return Standard_True;
+}
+
+//=======================================================================
+//function : IsCoincide
+//purpose  : Check, if theLine is coincided with theArc (in 2d-space).
+//
+// Attention!!!
+//            Cases when theArc is not 2d-line adaptor are suppored by
+//          TopOpeBRep classes only (i.e. are archaic).
+//=======================================================================
+Standard_Boolean IsCoincide(IntPatch_TheSurfFunction& theFunc,
+                            const Handle(IntPatch_PointLine)& theLine,
+                            const Handle(Adaptor2d_HCurve2d)& theArc,
+                            const Standard_Boolean isTheSurface1Using, //Surf1 is parametric?
+                            const Standard_Real theToler3D,
+                            const Standard_Real theToler2D,
+                            const Standard_Real thePeriod) // Period of parametric surface in direction which is perpendicular to theArc direction.
+{
+  if(theLine->ArcType() == IntPatch_Restriction)
+  {//Restriction-restriction processing
+    const Handle(IntPatch_RLine)& aRL2 = Handle(IntPatch_RLine)::DownCast(theLine);
+    const Handle(Adaptor2d_HCurve2d)& anArc = aRL2->IsArcOnS1() ? aRL2->ArcOnS1() : aRL2->ArcOnS2();
+    
+    if(anArc->Curve2d().GetType() != GeomAbs_Line)
+    {
+      //Restriction line must be isoline.
+      //Other cases are not supported by
+      //existing algorithms.
+
+      return Standard_False;
     }
 
-    Standard_Real DeltaU = fabs(U1-AnU1);
-    if(DeltaU > BSEAM) {
-      Bindex = k;
-      isDecomposited = Standard_True;
-      is2PIDecomposed = Standard_True;
-      break;
+    const gp_Lin2d aLin1(theArc->Curve2d().Line()),
+                   aLin2(anArc->Curve2d().Line());
+
+    if(!aLin1.Direction().IsParallel(aLin2.Direction(), Precision::Angular()))
+    {
+      return Standard_False;
     }
-    else if((DeltaU > BAPEX) && (k >= (Findex+10) && k <= (Lindex-10))) {
-      Bindex = k;
-      isDecomposited = Standard_True;
-      break;
+
+    const Standard_Real aDist = 
+            theArc->Curve2d().Line().Distance(anArc->Curve2d().Line());
+    if((aDist < theToler2D) || (Abs(aDist - thePeriod) < theToler2D))
+    {
+      const Standard_Real aRf = theArc->FirstParameter(),
+                          aRl = theArc->LastParameter();
+      const Standard_Real aParf = anArc->FirstParameter(),
+                          aParl = anArc->LastParameter();
+      const gp_Pnt2d aP1(ElCLib::Value(aParf, aLin2)),
+                     aP2(ElCLib::Value(aParl, aLin2));
+
+      Standard_Real aParam1 = ElCLib::Parameter(aLin1, aP1),
+                    aParam2 = ElCLib::Parameter(aLin1, aP2);
+
+      if(CheckSegmSegm(aRf, aRl, aParam1, aParam2))
+        return Standard_True;
+
+      //Lines are parallel. Therefore, there is no point in
+      //projecting points to another line in order to check
+      //if segment second line is included in segment of first one.
+
+      return CheckSegmSegm(aParam1, aParam2, aRf, aRl);
     }
-    else {}
 
-    sline->Add(SSLine->Value(k));
-    AnU1=U1;
-    AnV1=V1;
+    return Standard_False;
   }
-  IntSurf_PntOn2S aVF,aVL;
-  Standard_Boolean addVF = Standard_False, addVL = Standard_False;
-  VerifyVertices(sline,IsReversed,vline,TOL2DS,ArcTol,PDomain,aVF,addVF,aVL,addVL);
 
-  Standard_Boolean hasInternals = HasInternals(sline,vline);
+  const Standard_Integer aNbPnts = theLine->NbPnts();
+  const Standard_Real aUAf = theArc->FirstParameter(),
+                      aUAl = theArc->LastParameter();
+  const gp_Lin2d anArcLin(theArc->Curve2d().Line());
 
-  Standard_Real D3F = 0., D3L = 0.;
-  ToSmooth(sline,IsReversed,Quad,Standard_True,D3F);
-  ToSmooth(sline,IsReversed,Quad,Standard_False,D3L);
+  math_Vector aX(1, 2), aVal(1, 1);
 
-  if(D3F <= 1.5e-7 && sline->NbPoints() >=3) {
-    D3F = sline->Value(2).Value().Distance(sline->Value(3).Value());
-  }
-  if(D3L <= 1.5e-7 && sline->NbPoints() >=3) {
-    D3L = sline->Value(sline->NbPoints()-1).Value().Distance(sline->Value(sline->NbPoints()-2).Value());
-  }
+  for(Standard_Integer aPtID = 1; aPtID <= aNbPnts; aPtID++)
+  {
+    Standard_Real aUf = 0.0, aVf = 0.0;
+    if(isTheSurface1Using)
+      theLine->Point(aPtID).ParametersOnS1(aUf, aVf);
+    else
+      theLine->Point(aPtID).ParametersOnS2(aUf, aVf);
 
-  if(addVF || addVL) {
-    Standard_Boolean isAdded = AddVertices(sline,aVF,addVF,aVL,addVL,D3F,D3L);
-    if(isAdded) {
-      ToSmooth(sline,IsReversed,Quad,Standard_True,D3F);
-      ToSmooth(sline,IsReversed,Quad,Standard_False,D3L);
+    //Take 2d-point in parametric surface (because theArc is
+    //2d-line in parametric surface).
+    const gp_Pnt2d aPloc(aUf, aVf);
+
+    const Standard_Real aRParam = ElCLib::Parameter(anArcLin, aPloc);
+
+    if((aRParam < aUAf) || (aRParam > aUAl))
+      return Standard_False;
+
+    const gp_Pnt2d aPmin(ElCLib::Value(aRParam, anArcLin));
+    
+    const Standard_Real aDist = aPloc.Distance(aPmin);
+    if((aDist < theToler2D) || (Abs(aDist - thePeriod) < theToler2D))
+    {//Considered point is in Restriction line.
+     //Go to the next point.
+      continue;
     }
-  }
 
-  if(!hasInternals) {
-    ForcedPurgePoints(sline,IsReversed,Quad);
-  }
+    //Check if intermediate points between aPloc and theArc are
+    //intersection point (i.e. if aPloc is in tangent zone between
+    //two intersected surfaces).
 
-  wline = new IntPatch_WLine(sline,Standard_False,Line->TransitionOnS1(),Line->TransitionOnS2());
+    const Standard_Real aUl = aPmin.X(), aVl = aPmin.Y();
 
-  aSPnt = sline->Value(1).Value();
-  sline->Value(1).ParametersOnS1(U1,V1);
-  sline->Value(1).ParametersOnS2(U2,V2);
-  TPntF.SetValue(aSPnt,ArcTol,Standard_False);
-  TPntF.SetParameters(U1,V1,U2,V2);
-  TPntF.SetParameter(1.);
-  wline->AddVertex(TPntF);
-  wline->SetFirstPoint(1);
+    const Standard_Integer aNbPoints = 4;
+    const Standard_Real aStepU = (aUl - aUf)/aNbPoints,
+                        aStepV = (aVl - aVf)/aNbPoints;
 
-  if(hasInternals) {
-    PutIntVertices(wline,sline,IsReversed,vline,ArcTol);
-  }
-  
-  aSPnt =  sline->Value(sline->NbPoints()).Value();
-  sline->Value(sline->NbPoints()).ParametersOnS1(U1,V1);
-  sline->Value(sline->NbPoints()).ParametersOnS2(U2,V2);
-  TPntL.SetValue(aSPnt,ArcTol,Standard_False);
-  TPntL.SetParameters(U1,V1,U2,V2);
-  TPntL.SetParameter(sline->NbPoints());
-  wline->AddVertex(TPntL);
-  wline->SetLastPoint(sline->NbPoints());
+    Standard_Real aU = aUf+aStepU, aV = aVf+aStepV;
+    for(Standard_Integer i = 1; i < aNbPoints; i++)
+    {
+      aX.Value(1) = aU;
+      aX.Value(2) = aV;
 
-  IntPatch_SequenceOfLine segm;
-  Standard_Boolean isSplited = SplitOnSegments(wline,Standard_False,Line->TransitionOnS1(),Line->TransitionOnS2(),ArcTol,segm);
+      if(!theFunc.Value(aX, aVal))
+      {
+        return Standard_False;
+      }
 
-  if(!isSplited)
-    Lines.Append(wline);
-  else {
-    Standard_Integer nbsegms = segm.Length();
-    Standard_Integer iseg = 0;
-    for(iseg = 1; iseg <= nbsegms; iseg++)
-      Lines.Append(segm(iseg));
+      if(Abs(aVal(1)) > theToler3D)
+      {
+        return Standard_False;
+      }
+      
+      aU += aStepU;
+      aV += aStepV;
+    }
   }
 
-  if(isDecomposited) {
-    Findex = Bindex;
-    sline = new IntSurf_LineOn2S();
-    goto nextline;
-  }
-}
-/*
-// <-A
-       {
-         Standard_Integer aNbPnts;
-         Standard_Real aU1,aV1,aU2,aV2;
-         gp_Pnt aPx;
-         //
-         aNbPnts=thelin->NbPoints(); 
-         printf(" WLine: aNbPnts=%d\n", aNbPnts);
-         for(i=1; i <= aNbPnts; ++i) {
-           const IntSurf_PntOn2S& aPoint = thelin->Value(i);
-           aPx=aPoint.Value();
-           aPoint.Parameters(aU1, aV1, aU2, aV2);
-           printf(" point %d %lf %lf %lf %lf %lf %lf %lf\n", 
-                  i, aPx.X(), aPx.Y(), aPx.Z(), aU1, aV1, aU2, aV2);
-         }
-       }
-*/
+  return Standard_True;
+}
\ No newline at end of file