0024981: IntTools_FaceFace enters to infinite loop on the attached case
authoremv <emv@opencascade.com>
Wed, 11 Jun 2014 06:41:15 +0000 (10:41 +0400)
committerapn <apn@opencascade.com>
Wed, 11 Jun 2014 06:41:58 +0000 (10:41 +0400)
class IntTools_Tools
method
  Standard_Boolean IntTools_Tools::AdjustPeriodic
      (const Standard_Real thePar,
       const Standard_Real theParMin,
       const Standard_Real theParMax,
       const Standard_Real thePeriod,
       Standard_Real &theNewPar,
       Standard_Real &theOffset,
       const Standard_Real theEps)
The new function has been implemented for fast adjustment of pcurves to the range of surface.
Test case correction for issue CR24981

src/BOPTools/BOPTools_AlgoTools2D.cxx
src/IntTools/IntTools_BeanFaceIntersector.cxx
src/IntTools/IntTools_FClass2d.cxx
src/IntTools/IntTools_FaceFace.cxx
src/IntTools/IntTools_LineConstructor.cxx
src/IntTools/IntTools_Tools.cdl
src/IntTools/IntTools_Tools.cxx
tests/bugs/modalg_4/bug6272_6
tests/bugs/modalg_5/bug24981 [new file with mode: 0644]

index 2798ee3..e6ea3c6 100644 (file)
@@ -70,6 +70,7 @@
 #include <BOPCol_IndexedMapOfShape.hxx>
 
 #include <BOPTools.hxx>
+#include <IntTools_Tools.hxx>
 
 static 
   Standard_Boolean CheckEdgeLength (const TopoDS_Edge& );
@@ -296,10 +297,9 @@ void BOPTools_AlgoTools2D::AdjustPCurveOnFace
    const Handle(Geom2d_Curve)& aC2D, 
    Handle(Geom2d_Curve)& aC2DA)
 {
-  Standard_Boolean mincond, maxcond, decalu, decalv;
-  Standard_Integer k, iCnt;
+  Standard_Boolean mincond, maxcond;
   Standard_Real UMin, UMax, VMin, VMax, aT, u2, v2, du, dv, aDelta;
-  Standard_Real aUPeriod, aUP2, aUP1, aUNew, aDif, aUx;
+  Standard_Real aUPeriod;
   //
   aDelta=Precision::PConfusion();
   
@@ -317,65 +317,24 @@ void BOPTools_AlgoTools2D::AdjustPCurveOnFace
  
   du = 0.;
   if (aBAS.IsUPeriodic()) {
-    aUPeriod=aBAS.UPeriod(); 
-    mincond = (u2 < UMin-aDelta);
-    maxcond = (u2 > UMax+aDelta); 
-    
-    decalu = mincond || maxcond;
-    if (decalu) {
-      //du = ( mincond ) ? UPeriod : -UPeriod;
-      //
-      iCnt=1;
-      aUP2=aUPeriod+aUPeriod+aDelta;
-      aUP1=aUPeriod+aDelta;
-      //
-      if (u2 > aUP2) {
-        k=1;
-        do {
-          aUx=u2-k*aUPeriod;
-          iCnt = k++;
-        } while (aUx >= aUP1);
-      }
-      else if (u2 < -aUP2) {
-        k=1;
-        do {
-          aUx=u2+k*aUPeriod;
-          iCnt = (k++) + 1;
-        } while (aUx <= -aUP1);
-      }
-      du = ( mincond ) ? aUPeriod : -aUPeriod;
-      du=iCnt*du;
-    }
+    Standard_Real newu;
+    aUPeriod = aBAS.UPeriod(); 
     //
-    aUNew=u2+du;
-    if (aUNew<(UMin-aDelta) || 
-        aUNew>(UMax+aDelta)) {
-      // So previous correction was wrong.
-      // Try to be closer to UMin or UMax.
-      du=0.;
-      if (u2>UMax){
-        aDif=u2-UMax;
-        if (aDif < 4.e-7) {
-          du=-aDif;
-        }
-      }
-    }
-  } // if (BAHS->IsUPeriodic())
-  //
+    IntTools_Tools::AdjustPeriodic(u2, UMin, UMax, aUPeriod, newu, du);
+  }
   // dv
   dv = 0.;
   if (aBAS.IsVPeriodic()) {
     Standard_Real aVPeriod, aVm, aVr, aVmid, dVm, dVr;
     //
-    aVPeriod=aBAS.VPeriod();
+    aVPeriod = aBAS.VPeriod();
     mincond = (VMin - v2 > aDelta);
     maxcond = (v2 - VMax > aDelta);
-    decalv = mincond || maxcond;
-    if (decalv) {
+    //
+    if (mincond || maxcond) {
       dv = ( mincond ) ? aVPeriod : -aVPeriod;
     }
     //
-    //xf
     if ((VMax-VMin<aVPeriod) && dv) {
       aVm=v2;
       aVr=v2+dv;
@@ -386,7 +345,6 @@ void BOPTools_AlgoTools2D::AdjustPCurveOnFace
         dv=0.;
       }
     }
-    //xt
   }
   //
   {
index 0a7e6cb..e90c382 100644 (file)
 #include <ElCLib.hxx>
 #include <ElSLib.hxx>
 
-static Standard_Boolean AdjustPeriodic(const Standard_Real U, 
-                                       const Standard_Real UFirst,
-                                       const Standard_Real ULast,
-                                       const Standard_Real Period,
-                                       Standard_Real&      UResult);
-
 static Standard_Boolean SetEmptyResultRange(const Standard_Real      theParameter, 
                                             IntTools_MarkedRangeSet& theMarkedRange);
 
@@ -645,37 +639,39 @@ void IntTools_BeanFaceIntersector::ComputeAroundExactIntersection()
         Standard_Real V = aPoint.V();
         
         if(UIsNotValid || VIsNotValid) {
-          // modified by NIZHNY-MKK  Thu Jun 17 12:50:39 2004
           Standard_Boolean bUCorrected = Standard_True;
           
           if(UIsNotValid) {
-            // modified by NIZHNY-MKK  Thu Jun 17 12:50:37 2004
             bUCorrected = Standard_False;
             solutionIsValid = Standard_False;
-            
+            //
             if(mySurface.IsUPeriodic()) {
-              Standard_Real aNewU = U;
-              
-              if(AdjustPeriodic(U, myUMinParameter, myUMaxParameter, mySurface.UPeriod(), aNewU)) {
-                solutionIsValid = Standard_True;
-                // modified by NIZHNY-MKK  Thu Jun 17 12:51:01 2004
-                bUCorrected = Standard_True;
-                U = aNewU;
-              }
+              Standard_Real aNewU, aUPeriod, aEps, du;
+              //
+              aUPeriod = mySurface.UPeriod();
+              aEps = Epsilon(aUPeriod);
+              //
+              IntTools_Tools::AdjustPeriodic(U, myUMinParameter, myUMaxParameter, 
+                                             aUPeriod, aNewU, du, aEps);
+              solutionIsValid = Standard_True;
+              bUCorrected = Standard_True;
+              U = aNewU;
             }
           }
-          // modified by NIZHNY-MKK  Thu Jun 17 12:51:03 2004
           //   if(solutionIsValid && VIsNotValid) {
           if(bUCorrected && VIsNotValid) {
             solutionIsValid = Standard_False;
-            
+            //
             if(mySurface.IsVPeriodic()) {
-              Standard_Real aNewV = V;
-              
-              if(AdjustPeriodic(V, myVMinParameter, myVMaxParameter, mySurface.VPeriod(), aNewV)) {
-                solutionIsValid = Standard_True;
-                V = aNewV;
-              }
+              Standard_Real aNewV, aVPeriod, aEps, dv;
+              //
+              aVPeriod = mySurface.VPeriod();
+              aEps = Epsilon(aVPeriod);
+              //
+              IntTools_Tools::AdjustPeriodic(V, myVMinParameter, myVMaxParameter, 
+                                             aVPeriod, aNewV, dv, aEps);
+              solutionIsValid = Standard_True;
+              V = aNewV;
             }
           }
         }
@@ -1389,27 +1385,6 @@ void IntTools_BeanFaceIntersector::ComputeRangeFromStartPoint(const Standard_Boo
 }
 
 // ---------------------------------------------------------------------------------
-// static function: AdjustPeriodic
-// purpose:  
-// ---------------------------------------------------------------------------------
-static Standard_Boolean AdjustPeriodic(const Standard_Real U, 
-                                       const Standard_Real UFirst,
-                                       const Standard_Real ULast,
-                                       const Standard_Real Period,
-                                       Standard_Real&      UResult) {
-  UResult = U;
-  Standard_Real u = U;
-  Standard_Real Eps = Epsilon(Period);
-  while (Eps < (UFirst-u)) u += Period;
-  while (Eps > (ULast -u)) u -= Period;
-  if ( u < UFirst) 
-    return Standard_False;
-  
-  UResult = u;
-  return Standard_True;
-}
-
-// ---------------------------------------------------------------------------------
 // static function: SetEmptyResultRange
 // purpose:  
 // ---------------------------------------------------------------------------------
index 9cbc0c9..c677d2b 100644 (file)
@@ -44,6 +44,7 @@
 #include <TopoDS_Wire.hxx>
 #include <TColStd_DataMapOfIntegerInteger.hxx>
 #include <TColgp_SequenceOfVec2d.hxx>
+#include <IntTools_Tools.hxx>
 
 //=======================================================================
 //function : IntTools_FClass2d:IntTools:_FClass2d
@@ -57,7 +58,7 @@ IntTools_FClass2d::IntTools_FClass2d()
 //purpose  : 
 //=======================================================================
   IntTools_FClass2d::IntTools_FClass2d(const TopoDS_Face& aFace,
-                                      const Standard_Real TolUV) 
+                                       const Standard_Real TolUV) 
 : Toluv(TolUV), Face(aFace)  
 {
   Init(Face, Toluv);
@@ -75,7 +76,7 @@ IntTools_FClass2d::IntTools_FClass2d()
 //purpose  : 
 //=======================================================================
   void IntTools_FClass2d::Init(const TopoDS_Face& aFace,
-                              const Standard_Real TolUV) 
+                               const Standard_Real TolUV) 
 {
   Standard_Boolean WireIsNotEmpty, Ancienpnt3dinitialise, degenerated;
   Standard_Integer nbpnts, firstpoint, NbEdges;
@@ -149,12 +150,12 @@ IntTools_FClass2d::IntTools_FClass2d()
       edge = aWExp.Current();
       Or = edge.Orientation();
       if(!(Or==TopAbs_FORWARD || Or==TopAbs_REVERSED)) {
-       continue;
+        continue;
       }
       //
       aC2D=BRep_Tool::CurveOnSurface(edge, Face, pfbid, plbid);
       if (aC2D.IsNull()) {
-       return;
+        return;
       }
       //
       BRepAdaptor_Curve2d C(edge,Face);
@@ -162,8 +163,8 @@ IntTools_FClass2d::IntTools_FClass2d()
       //------------------------------------------
       degenerated=Standard_False;
       if(BRep_Tool::Degenerated(edge) ||
-        BRep_Tool::IsClosed(edge, Face)) {
-       degenerated=Standard_True;
+         BRep_Tool::IsClosed(edge, Face)) {
+        degenerated=Standard_True;
       }
       //
       TopExp::Vertices(edge,Va,Vb);
@@ -171,21 +172,21 @@ IntTools_FClass2d::IntTools_FClass2d()
       TolVertex1=0.;
       TolVertex=0.;
       if (Va.IsNull()) {
-       degenerated=Standard_True;
-      }        
+        degenerated=Standard_True;
+      }        
       else {
-       TolVertex1=BRep_Tool::Tolerance(Va);
+        TolVertex1=BRep_Tool::Tolerance(Va);
       }
       if (Vb.IsNull()){
-       degenerated=Standard_True;
-      }        
+        degenerated=Standard_True;
+      }        
       else {
-       TolVertex=BRep_Tool::Tolerance(Vb);
+        TolVertex=BRep_Tool::Tolerance(Vb);
       }
-       // 
+        // 
       if(TolVertex<TolVertex1) {
-       TolVertex=TolVertex1;
-      }        
+        TolVertex=TolVertex1;
+      }        
       //
       //-- Verification of cases when forgotten to code degenereted
       if(!degenerated) {
@@ -211,26 +212,26 @@ IntTools_FClass2d::IntTools_FClass2d()
       //-- ----------------------------------------
       Tole = BRep_Tool::Tolerance(edge);
       if(Tole>Tol) {
-       Tol=Tole;
+        Tol=Tole;
       }
       //
       // NbSamples +> nbs
       nbs = Geom2dInt_Geom2dCurveTool::NbSamples(C);
       if (nbs > 2) {
-       nbs*=4;
+        nbs*=4;
       }
       du = (plbid-pfbid)/(Standard_Real)(nbs-1);
       //
       if(Or==TopAbs_FORWARD) { 
-       u = pfbid;
-       uFirst=pfbid;
-       uLast=plbid;
+        u = pfbid;
+        uFirst=pfbid;
+        uLast=plbid;
       }
       else { 
-       u = plbid;
-       uFirst=plbid;
-       uLast=pfbid;
-       du=-du;
+        u = plbid;
+        uFirst=plbid;
+        uLast=pfbid;
+        du=-du;
       }
       //
       // aPrms
@@ -238,18 +239,18 @@ IntTools_FClass2d::IntTools_FClass2d()
       TColStd_Array1OfReal aPrms(1, aNbs1);
       //
       if (nbs==2) {
-       Standard_Real aCoef=0.0025;
-       aPrms(1)=uFirst;
-       aPrms(2)=uFirst+aCoef*(uLast-uFirst);
-       aPrms(3)=uLast;
+        Standard_Real aCoef=0.0025;
+        aPrms(1)=uFirst;
+        aPrms(2)=uFirst+aCoef*(uLast-uFirst);
+        aPrms(3)=uLast;
       }
       else if (nbs>2) {
-       aNbs1=nbs;
-       aPrms(1)=uFirst;
-       for (iX=2; iX<aNbs1; ++iX) {
-         aPrms(iX)=u+(iX-1)*du;
-       }
-       aPrms(aNbs1)=uLast;
+        aNbs1=nbs;
+        aPrms(1)=uFirst;
+        for (iX=2; iX<aNbs1; ++iX) {
+          aPrms(iX)=u+(iX-1)*du;
+        }
+        aPrms(aNbs1)=uLast;
       }
       //
       //-- ------------------------------------------------------------
@@ -259,75 +260,75 @@ IntTools_FClass2d::IntTools_FClass2d()
       //-- afar from the last saved point
       Avant = nbpnts;
       for(iX=firstpoint; iX<=aNbs1; iX++) {
-       Standard_Boolean IsRealCurve3d;
-       Standard_Integer ii;
-       Standard_Real aDstX;
-       gp_Pnt2d P2d;
-       gp_Pnt P3d;
-       //
-       u=aPrms(iX);
-       P2d = C.Value(u);
-       if(P2d.X()<Umin) Umin = P2d.X();
-       if(P2d.X()>Umax) Umax = P2d.X();
-       if(P2d.Y()<Vmin) Vmin = P2d.Y();
-       if(P2d.Y()>Vmax) Vmax = P2d.Y();
-       //
-       aDstX=RealLast();       
-       if(degenerated==Standard_False) { 
-         P3d=C3d.Value(u);
-         if(nbpnts>1) {
-           if(Ancienpnt3dinitialise) { 
-             aDstX=P3d.SquareDistance(Ancienpnt3d);
-           }
-         }
-       }
-       //
-       IsRealCurve3d = Standard_True; 
-       if (aDstX < aPrCf2)  {
-         if(iX>1) {
-           Standard_Real aDstX1;
-           gp_Pnt MidP3d;
-           //
-           MidP3d = C3d.Value(0.5*(u+aPrms(iX-1)));
-           aDstX1=P3d.SquareDistance( MidP3d );
-           if (aDstX1 < aPrCf2){
-             IsRealCurve3d = Standard_False;
-           }
-         }
-       }
-       //
-       if (IsRealCurve3d) {
-         if(degenerated==Standard_False) { 
-           Ancienpnt3d=P3d;
-           Ancienpnt3dinitialise=Standard_True;
-         }
-         nbpnts++;
-         SeqPnt2d.Append(P2d);
-       }
-       //
-       ii=nbpnts;
-       if(ii>(Avant+4)) { 
-         Standard_Real ul, dU, dV;
-         gp_Pnt2d Pp;
-         //
-         gp_Lin2d Lin(SeqPnt2d(ii-2),gp_Dir2d(gp_Vec2d(SeqPnt2d(ii-2),SeqPnt2d(ii))));
-         ul = ElCLib::Parameter(Lin,SeqPnt2d(ii-1));
-         Pp = ElCLib::Value(ul,Lin);
-         dU = Abs(Pp.X()-SeqPnt2d(ii-1).X());
-         dV = Abs(Pp.Y()-SeqPnt2d(ii-1).Y());
-         if(dU>FlecheU) {
-           FlecheU = dU;
-         }
-         if(dV>FlecheV) {
-           FlecheV = dV;
-         }
-       }
+        Standard_Boolean IsRealCurve3d;
+        Standard_Integer ii;
+        Standard_Real aDstX;
+        gp_Pnt2d P2d;
+        gp_Pnt P3d;
+        //
+        u=aPrms(iX);
+        P2d = C.Value(u);
+        if(P2d.X()<Umin) Umin = P2d.X();
+        if(P2d.X()>Umax) Umax = P2d.X();
+        if(P2d.Y()<Vmin) Vmin = P2d.Y();
+        if(P2d.Y()>Vmax) Vmax = P2d.Y();
+        //
+        aDstX=RealLast();        
+        if(degenerated==Standard_False) { 
+          P3d=C3d.Value(u);
+          if(nbpnts>1) {
+            if(Ancienpnt3dinitialise) { 
+              aDstX=P3d.SquareDistance(Ancienpnt3d);
+            }
+          }
+        }
+        //
+        IsRealCurve3d = Standard_True; 
+        if (aDstX < aPrCf2)  {
+          if(iX>1) {
+            Standard_Real aDstX1;
+            gp_Pnt MidP3d;
+            //
+            MidP3d = C3d.Value(0.5*(u+aPrms(iX-1)));
+            aDstX1=P3d.SquareDistance( MidP3d );
+            if (aDstX1 < aPrCf2){
+              IsRealCurve3d = Standard_False;
+            }
+          }
+        }
+        //
+        if (IsRealCurve3d) {
+          if(degenerated==Standard_False) { 
+            Ancienpnt3d=P3d;
+            Ancienpnt3dinitialise=Standard_True;
+          }
+          nbpnts++;
+          SeqPnt2d.Append(P2d);
+        }
+        //
+        ii=nbpnts;
+        if(ii>(Avant+4)) { 
+          Standard_Real ul, dU, dV;
+          gp_Pnt2d Pp;
+          //
+          gp_Lin2d Lin(SeqPnt2d(ii-2),gp_Dir2d(gp_Vec2d(SeqPnt2d(ii-2),SeqPnt2d(ii))));
+          ul = ElCLib::Parameter(Lin,SeqPnt2d(ii-1));
+          Pp = ElCLib::Value(ul,Lin);
+          dU = Abs(Pp.X()-SeqPnt2d(ii-1).X());
+          dV = Abs(Pp.Y()-SeqPnt2d(ii-1).Y());
+          if(dU>FlecheU) {
+            FlecheU = dU;
+          }
+          if(dV>FlecheV) {
+            FlecheV = dV;
+          }
+        }
       }// for(iX=firstpoint; iX<=aNbs1; iX++) {
       //
       if(BadWire) {
-       continue; //if face has several wires and one of them is bad,
-                 //it is necessary to process all of them for correct
-                 //calculation of Umin, Umax, Vmin, Vmax - ifv, 23.08.06 
+        continue; //if face has several wires and one of them is bad,
+                  //it is necessary to process all of them for correct
+                  //calculation of Umin, Umax, Vmin, Vmax - ifv, 23.08.06 
       }
       //
       if(firstpoint==1) firstpoint=2;
@@ -340,7 +341,7 @@ IntTools_FClass2d::IntTools_FClass2d()
       C.D1(aU, aP, aV);
 
       if(Or == TopAbs_REVERSED)
-       aV.Reverse();
+        aV.Reverse();
 
       aD1Next.Append(aV);
 
@@ -349,18 +350,18 @@ IntTools_FClass2d::IntTools_FClass2d()
       C.D1(aU, aP, aV);
 
       if(Or == TopAbs_REVERSED)
-       aV.Reverse();
+        aV.Reverse();
 
       if (NbEdges > 0)
-       aD1Prev.Append(aV);
+        aD1Prev.Append(aV);
       else
-       aD1Prev.Prepend(aV);
+        aD1Prev.Prepend(aV);
 
       // Fill the map anIndexMap.
       if (Avant > 0)
-       anIndexMap.Bind(Avant, aD1Next.Length());
+        anIndexMap.Bind(Avant, aD1Next.Length());
       else
-       anIndexMap.Bind(1, aD1Next.Length());
+        anIndexMap.Bind(1, aD1Next.Length());
     } //for(;aWExp.More(); aWExp.Next()) {
     // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     //
@@ -381,91 +382,91 @@ IntTools_FClass2d::IntTools_FClass2d()
       //
       PClass.Init(anInitPnt);
       if(nbpnts>3) { 
-       Standard_Integer im2=nbpnts-2;
-       Standard_Integer im1=nbpnts-1;
-       Standard_Integer im0=1;
-       Standard_Integer ii;
-       Standard_Real    angle = 0.0;
-       Standard_Real aX0, aY0, aX1, aY1, aS;
-       //
-       aS=0.;
-       //
-
-       Standard_Integer iFlag=1;
-       PClass(im2)=SeqPnt2d.Value(im2);
-       PClass(im1)=SeqPnt2d.Value(im1);
-       PClass(nbpnts)=SeqPnt2d.Value(nbpnts);
-       for(ii=1; ii<nbpnts; ii++,im0++,im1++,im2++) { 
-         if(im2>=nbpnts) im2=1;
-         if(im1>=nbpnts) im1=1;
-         PClass(ii)=SeqPnt2d.Value(ii);
-         //
-         const gp_Pnt2d& aP2D1=PClass(im1);
-         const gp_Pnt2d& aP2D0=PClass(im0);
-         //aP2D0 is next to aP2D1
-         aP2D0.Coord(aX0, aY0);
-         aP2D1.Coord(aX1, aY1);
-         aS=aS+(aY0+aY1)*(aX1-aX0); 
-
-         gp_Vec2d A(PClass(im2),PClass(im1));
-         gp_Vec2d B(PClass(im1),PClass(im0));
-
-         Standard_Real N = A.Magnitude() * B.Magnitude();
-         if(N>1e-16) { 
-           Standard_Real a=A.Angle(B);
-           //  
-           if (anIndexMap.IsBound(im1)) {
-             Standard_Integer  anInd  = anIndexMap.Find(im1);
-             const gp_Vec2d   &aVPrev = aD1Prev.Value(anInd);
-             const gp_Vec2d   &aVNext = aD1Next.Value(anInd);
-
-             Standard_Real aN = aVPrev.Magnitude() * aVNext.Magnitude();
-             if(aN > 1e-16) { 
-               Standard_Real aDerivAngle = aVPrev.Angle(aVNext);
-               //ifv 23.08.06
-               if(Abs(aDerivAngle) <= Precision::Angular()) aDerivAngle = 0.; 
-               //ifv 23.08.06 : if edges continuity > G1, |aDerivAngle| ~0,
-               //but can has wrong sign and causes condition aDerivAngle * a < 0.
-               //that is wrong in such situation
-               if (iFlag && aDerivAngle * a < 0.) {
-                 iFlag=0;
-                 // Bad case.
-                 angle = 0.;
-               }
-             }
-           }
-           angle+=a;
-         }
-       }//for(ii=1; ii<nbpnts; ii++,im0++,im1++,im2++) { 
-       if (!iFlag) {
-         angle = 0.; 
-       }
-       if(aS>0.){
-         myIsHole=Standard_False;
-       }
-       //
-       if(FlecheU<Toluv)
-         FlecheU = Toluv;
-
-       if(FlecheV<Toluv)
-         FlecheV = Toluv;
-
-       TabClass.Append((void *)new CSLib_Class2d(PClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
-       //
-       if((angle<2 && angle>-2)||(angle>10)||(angle<-10)) { 
-         BadWire=1;
-         TabOrien.Append(-1);
-       } 
-       else {  
-         TabOrien.Append((angle>0.0)? 1 : 0);
-       }
+        Standard_Integer im2=nbpnts-2;
+        Standard_Integer im1=nbpnts-1;
+        Standard_Integer im0=1;
+        Standard_Integer ii;
+        Standard_Real    angle = 0.0;
+        Standard_Real aX0, aY0, aX1, aY1, aS;
+        //
+        aS=0.;
+        //
+
+        Standard_Integer iFlag=1;
+        PClass(im2)=SeqPnt2d.Value(im2);
+        PClass(im1)=SeqPnt2d.Value(im1);
+        PClass(nbpnts)=SeqPnt2d.Value(nbpnts);
+        for(ii=1; ii<nbpnts; ii++,im0++,im1++,im2++) { 
+          if(im2>=nbpnts) im2=1;
+          if(im1>=nbpnts) im1=1;
+          PClass(ii)=SeqPnt2d.Value(ii);
+          //
+          const gp_Pnt2d& aP2D1=PClass(im1);
+          const gp_Pnt2d& aP2D0=PClass(im0);
+          //aP2D0 is next to aP2D1
+          aP2D0.Coord(aX0, aY0);
+          aP2D1.Coord(aX1, aY1);
+          aS=aS+(aY0+aY1)*(aX1-aX0); 
+
+          gp_Vec2d A(PClass(im2),PClass(im1));
+          gp_Vec2d B(PClass(im1),PClass(im0));
+
+          Standard_Real N = A.Magnitude() * B.Magnitude();
+          if(N>1e-16) { 
+            Standard_Real a=A.Angle(B);
+            //  
+            if (anIndexMap.IsBound(im1)) {
+              Standard_Integer  anInd  = anIndexMap.Find(im1);
+              const gp_Vec2d   &aVPrev = aD1Prev.Value(anInd);
+              const gp_Vec2d   &aVNext = aD1Next.Value(anInd);
+
+              Standard_Real aN = aVPrev.Magnitude() * aVNext.Magnitude();
+              if(aN > 1e-16) { 
+                Standard_Real aDerivAngle = aVPrev.Angle(aVNext);
+                //ifv 23.08.06
+                if(Abs(aDerivAngle) <= Precision::Angular()) aDerivAngle = 0.; 
+                //ifv 23.08.06 : if edges continuity > G1, |aDerivAngle| ~0,
+                //but can has wrong sign and causes condition aDerivAngle * a < 0.
+                //that is wrong in such situation
+                if (iFlag && aDerivAngle * a < 0.) {
+                  iFlag=0;
+                  // Bad case.
+                  angle = 0.;
+                }
+              }
+            }
+            angle+=a;
+          }
+        }//for(ii=1; ii<nbpnts; ii++,im0++,im1++,im2++) { 
+        if (!iFlag) {
+          angle = 0.; 
+        }
+        if(aS>0.){
+          myIsHole=Standard_False;
+        }
+        //
+        if(FlecheU<Toluv)
+          FlecheU = Toluv;
+
+        if(FlecheV<Toluv)
+          FlecheV = Toluv;
+
+        TabClass.Append((void *)new CSLib_Class2d(PClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
+        //
+        if((angle<2 && angle>-2)||(angle>10)||(angle<-10)) { 
+          BadWire=1;
+          TabOrien.Append(-1);
+        } 
+        else {         
+          TabOrien.Append((angle>0.0)? 1 : 0);
+        }
       } 
       else { 
-       BadWire=1;
-       TabOrien.Append(-1);
-       TColgp_Array1OfPnt2d PPClass(1,2);
-       PPClass.Init(anInitPnt);
-       TabClass.Append((void *)new CSLib_Class2d(PPClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
+        BadWire=1;
+        TabOrien.Append(-1);
+        TColgp_Array1OfPnt2d PPClass(1,2);
+        PPClass.Init(anInitPnt);
+        TabClass.Append((void *)new CSLib_Class2d(PPClass,FlecheU,FlecheV,Umin,Vmin,Umax,Vmax));
       }
     }// else if(WireIsNotEmpty)
   } // for(; aExpF.More();  aExpF.Next()) {
@@ -551,31 +552,13 @@ IntTools_FClass2d::IntTools_FClass2d()
   vrecadre = Standard_False;
   //
   if (RecadreOnPeriodic) {
-    
+    Standard_Real du, dv;
     if (IsUPer) {
-      if (uu < Umin)
-        while (uu < Umin) {
-          uu += uperiod;
-        }
-      else {
-        while (uu >= Umin){
-          uu -= uperiod;
-        }
-        uu += uperiod;
-      }
+      IntTools_Tools::AdjustPeriodic(uu, Umin, Umax, uperiod, uu, du);
     }// if (IsUPer) {
-    
+    //
     if (IsVPer) {
-      if (vv < Vmin)
-        while (vv < Vmin){
-          vv += vperiod;
-        }
-      else {
-        while (vv >= Vmin) {
-          vv -= vperiod;
-        }
-        vv += vperiod;
-      }
+      IntTools_Tools::AdjustPeriodic(vv, Vmin, Vmax, vperiod, vv, dv);
     }//if (IsVPer) {
   }
   //
@@ -675,8 +658,8 @@ IntTools_FClass2d::IntTools_FClass2d()
 //purpose  : 
 //=======================================================================
   TopAbs_State IntTools_FClass2d::TestOnRestriction(const gp_Pnt2d& _Puv,
-                                                   const Standard_Real Tol,
-                                                   const Standard_Boolean RecadreOnPeriodic) const
+                                                    const Standard_Real Tol,
+                                                    const Standard_Boolean RecadreOnPeriodic) const
 { 
   Standard_Integer nbtabclass = TabClass.Length();
   if (nbtabclass == 0)
@@ -699,66 +682,49 @@ IntTools_FClass2d::IntTools_FClass2d()
   Standard_Boolean urecadre = Standard_False, vrecadre = Standard_False;
   Standard_Integer dedans = 1;
 
-  if (RecadreOnPeriodic)
-    {
-      if (IsUPer)
-       {
-         if (uu < Umin)
-           while (uu < Umin)
-             uu += uperiod;
-         else
-           {
-             while (uu >= Umin)
-               uu -= uperiod;
-             uu += uperiod;
-           }
-       }
-      if (IsVPer)
-       {
-         if (vv < Vmin)
-           while (vv < Vmin)
-             vv += vperiod;
-         else
-           {
-             while (vv >= Vmin)
-               vv -= vperiod;
-             vv += vperiod;
-           }
-       }
-    }
-  
+  if (RecadreOnPeriodic) {
+    Standard_Real du, dv;
+    if (IsUPer) {
+      IntTools_Tools::AdjustPeriodic(uu, Umin, Umax, uperiod, uu, du);
+    }// if (IsUPer) {
+    //
+    if (IsVPer) {
+      IntTools_Tools::AdjustPeriodic(vv, Vmin, Vmax, vperiod, vv, dv);
+    }//if (IsVPer) {
+  }
+  //
   for (;;) {
     dedans = 1;
     gp_Pnt2d Puv(u,v);
     
     if(TabOrien(1)!=-1) { 
       for(Standard_Integer n=1; n<=nbtabclass; n++) { 
-       Standard_Integer cur = ((CSLib_Class2d *)TabClass(n))->SiDans_OnMode(Puv,Tol);
-       if(cur==1) { 
-         if(TabOrien(n)==0) { 
-           dedans = -1; 
-           break;
-         }
-       }
-       else if(cur==-1) { 
-         if(TabOrien(n)==1) {  
-           dedans = -1; 
-           break;
-         }
-       }
-       else { 
-         dedans = 0;
-         break;
-       }
+        Standard_Integer cur = ((CSLib_Class2d *)TabClass(n))->SiDans_OnMode(Puv,Tol);
+        if(cur==1) { 
+          if(TabOrien(n)==0) { 
+            dedans = -1; 
+            break;
+          }
+        }
+        else if(cur==-1) { 
+          if(TabOrien(n)==1) {  
+            dedans = -1; 
+            break;
+          }
+        }
+        else { 
+          dedans = 0;
+          break;
+        }
       }
       if(dedans==0) {
-       Status = TopAbs_ON;
+        Status = TopAbs_ON;
       }
       if(dedans == 1) {
-       Status = TopAbs_IN;
+        Status = TopAbs_IN;
       }
       if(dedans == -1) {
-       Status = TopAbs_OUT;
+        Status = TopAbs_OUT;
       }
     }
     else {  //-- TabOrien(1)=-1  Wrong  Wire 
@@ -774,27 +740,27 @@ IntTools_FClass2d::IntTools_FClass2d()
     
     if (!urecadre)
       {
-       u = uu;
-       urecadre = Standard_True;
+        u = uu;
+        urecadre = Standard_True;
       }
     else
       if (IsUPer)
-       u += uperiod;
+        u += uperiod;
     if (u > Umax || !IsUPer)
       {
-       if (!vrecadre)
-         {
-           v = vv;
-           vrecadre = Standard_True;
-         }
-       else
-         if (IsVPer)
-           v += vperiod;
-       
-       u = uu;
-       
-       if (v > Vmax || !IsVPer)
-         return Status;
+        if (!vrecadre)
+          {
+            v = vv;
+            vrecadre = Standard_True;
+          }
+        else
+          if (IsVPer)
+            v += vperiod;
+        
+        u = uu;
+        
+        if (v > Vmax || !IsVPer)
+          return Status;
       }
   } //for (;;)
 }
index bcba29a..f31df2a 100644 (file)
@@ -123,79 +123,72 @@ static
 //
 static
   void TolR3d(const TopoDS_Face& ,
-             const TopoDS_Face& ,
-             Standard_Real& );
+              const TopoDS_Face& ,
+              Standard_Real& );
 static 
   Handle(Geom_Curve) MakeBSpline (const Handle(IntPatch_WLine)&,
-                                 const Standard_Integer,
-                                 const Standard_Integer);
+                                  const Standard_Integer,
+                                  const Standard_Integer);
 
 static 
   void Parameters(const Handle(GeomAdaptor_HSurface)&,
-                 const Handle(GeomAdaptor_HSurface)&,
-                 const gp_Pnt&,
-                 Standard_Real&,
-                 Standard_Real&,
-                 Standard_Real&,
-                 Standard_Real&);
+                  const Handle(GeomAdaptor_HSurface)&,
+                  const gp_Pnt&,
+                  Standard_Real&,
+                  Standard_Real&,
+                  Standard_Real&,
+                  Standard_Real&);
 
 static 
   void BuildPCurves (Standard_Real f,Standard_Real l,Standard_Real& Tol,
-                    const Handle (Geom_Surface)& S,
-                    const Handle (Geom_Curve)&   C,
-                    Handle (Geom2d_Curve)& C2d);
+                     const Handle (Geom_Surface)& S,
+                     const Handle (Geom_Curve)&   C,
+                     Handle (Geom2d_Curve)& C2d);
 
 static 
   void CorrectSurfaceBoundaries(const TopoDS_Face&  theFace,
-                               const Standard_Real theTolerance,
-                               Standard_Real&      theumin,
-                               Standard_Real&      theumax, 
-                               Standard_Real&      thevmin, 
-                               Standard_Real&      thevmax);
+                                const Standard_Real theTolerance,
+                                Standard_Real&      theumin,
+                                Standard_Real&      theumax, 
+                                Standard_Real&      thevmin, 
+                                Standard_Real&      thevmax);
 static
   Standard_Boolean NotUseSurfacesForApprox
           (const TopoDS_Face& aF1,
-          const TopoDS_Face& aF2,
-          const Handle(IntPatch_WLine)& WL,
-          const Standard_Integer ifprm,
-          const Standard_Integer ilprm);
+           const TopoDS_Face& aF2,
+           const Handle(IntPatch_WLine)& WL,
+           const Standard_Integer ifprm,
+           const Standard_Integer ilprm);
 
 static 
   Handle(IntPatch_WLine) ComputePurgedWLine(const Handle(IntPatch_WLine)& theWLine);
 
 static 
-  Standard_Real AdjustPeriodic(const Standard_Real theParameter,
-                              const Standard_Real parmin,
-                              const Standard_Real parmax,
-                              const Standard_Real thePeriod,
-                              Standard_Real&      theOffset);
-
-static 
   Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine,
-                                           const Standard_Integer                         ideb,
-                                           const Standard_Integer                         ifin,
-                                           const Standard_Boolean                         onFirst);
+                                            const Standard_Integer                         ideb,
+                                            const Standard_Integer                         ifin,
+                                            const Standard_Boolean                         onFirst);
 
 static 
   Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
-                                       const Handle(GeomAdaptor_HSurface)&            theSurface1, 
-                                       const Handle(GeomAdaptor_HSurface)&            theSurface2,
-                                       const TopoDS_Face&                             theFace1,
-                                       const TopoDS_Face&                             theFace2,
-                                       const IntTools_LineConstructor&                theLConstructor,
-                                       const Standard_Boolean                         theAvoidLConstructor,
-                                       IntPatch_SequenceOfLine&                       theNewLines,
-                                       Standard_Real&                                 theReachedTol3d,
+                                        const Handle(GeomAdaptor_HSurface)&            theSurface1, 
+                                        const Handle(GeomAdaptor_HSurface)&            theSurface2,
+                                        const TopoDS_Face&                             theFace1,
+                                        const TopoDS_Face&                             theFace2,
+                                        const IntTools_LineConstructor&                theLConstructor,
+                                        const Standard_Boolean                         theAvoidLConstructor,
+                                        IntPatch_SequenceOfLine&                       theNewLines,
+                                        Standard_Real&                                 theReachedTol3d,
                                         const Handle(IntTools_Context)& );
 
 static 
   Standard_Boolean ParameterOutOfBoundary(const Standard_Real       theParameter, 
-                                         const Handle(Geom_Curve)& theCurve, 
-                                         const TopoDS_Face&        theFace1, 
-                                         const TopoDS_Face&        theFace2,
-                                         const Standard_Real       theOtherParameter,
-                                         const Standard_Boolean    bIncreasePar,
-                                         Standard_Real&            theNewParameter,
+                                          const Handle(Geom_Curve)& theCurve, 
+                                          const TopoDS_Face&        theFace1, 
+                                          const TopoDS_Face&        theFace2,
+                                          const Standard_Real       theOtherParameter,
+                                          const Standard_Boolean    bIncreasePar,
+                                          Standard_Real&            theNewParameter,
                                           const Handle(IntTools_Context)& );
 
 static 
@@ -203,99 +196,99 @@ static
 
 static 
   Standard_Boolean IsPointOnBoundary(const Standard_Real theParameter,
-                                    const Standard_Real theFirstBoundary,
-                                    const Standard_Real theSecondBoundary,
-                                    const Standard_Real theResolution,
-                                    Standard_Boolean&   IsOnFirstBoundary);
+                                     const Standard_Real theFirstBoundary,
+                                     const Standard_Real theSecondBoundary,
+                                     const Standard_Real theResolution,
+                                     Standard_Boolean&   IsOnFirstBoundary);
 static
   Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
-                            const gp_Pnt2d&     theLastPoint,
-                            const Standard_Real theUmin, 
-                            const Standard_Real theUmax,
-                            const Standard_Real theVmin,
-                            const Standard_Real theVmax,
-                            gp_Pnt2d&           theNewPoint);
+                             const gp_Pnt2d&     theLastPoint,
+                             const Standard_Real theUmin, 
+                             const Standard_Real theUmax,
+                             const Standard_Real theVmin,
+                             const Standard_Real theVmax,
+                             gp_Pnt2d&           theNewPoint);
 
 
 static 
   Standard_Integer ComputeTangentZones( const Handle(GeomAdaptor_HSurface)& theSurface1,
-                                      const Handle(GeomAdaptor_HSurface)&  theSurface2,
-                                      const TopoDS_Face&                   theFace1,
-                                      const TopoDS_Face&                   theFace2,
-                                      Handle(TColgp_HArray1OfPnt2d)&       theResultOnS1,
-                                      Handle(TColgp_HArray1OfPnt2d)&       theResultOnS2,
-                                      Handle(TColStd_HArray1OfReal)&       theResultRadius,
+                                       const Handle(GeomAdaptor_HSurface)&  theSurface2,
+                                       const TopoDS_Face&                   theFace1,
+                                       const TopoDS_Face&                   theFace2,
+                                       Handle(TColgp_HArray1OfPnt2d)&       theResultOnS1,
+                                       Handle(TColgp_HArray1OfPnt2d)&       theResultOnS2,
+                                       Handle(TColStd_HArray1OfReal)&       theResultRadius,
                                        const Handle(IntTools_Context)& );
 
 static
   Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
-                            const gp_Pnt2d&     theLastPoint,
-                            const Standard_Real theUmin, 
-                            const Standard_Real theUmax,
-                            const Standard_Real theVmin,
-                            const Standard_Real theVmax,
-                            const gp_Pnt2d&     theTanZoneCenter,
-                            const Standard_Real theZoneRadius,
-                            Handle(GeomAdaptor_HSurface) theGASurface,
-                            gp_Pnt2d&           theNewPoint);
+                             const gp_Pnt2d&     theLastPoint,
+                             const Standard_Real theUmin, 
+                             const Standard_Real theUmax,
+                             const Standard_Real theVmin,
+                             const Standard_Real theVmax,
+                             const gp_Pnt2d&     theTanZoneCenter,
+                             const Standard_Real theZoneRadius,
+                             Handle(GeomAdaptor_HSurface) theGASurface,
+                             gp_Pnt2d&           theNewPoint);
 
 static
   Standard_Boolean IsInsideTanZone(const gp_Pnt2d&     thePoint,
-                                  const gp_Pnt2d&     theTanZoneCenter,
-                                  const Standard_Real theZoneRadius,
-                                  Handle(GeomAdaptor_HSurface) theGASurface);
+                                   const gp_Pnt2d&     theTanZoneCenter,
+                                   const Standard_Real theZoneRadius,
+                                   Handle(GeomAdaptor_HSurface) theGASurface);
 
 static
   gp_Pnt2d AdjustByNeighbour(const gp_Pnt2d&     theaNeighbourPoint,
-                            const gp_Pnt2d&     theOriginalPoint,
-                            Handle(GeomAdaptor_HSurface) theGASurface);
+                             const gp_Pnt2d&     theOriginalPoint,
+                             Handle(GeomAdaptor_HSurface) theGASurface);
 static
   Standard_Boolean  ApproxWithPCurves(const gp_Cylinder& theCyl, 
-                                     const gp_Sphere& theSph);
+                                      const gp_Sphere& theSph);
 
 static void  PerformPlanes(const Handle(GeomAdaptor_HSurface)& theS1, 
-                          const Handle(GeomAdaptor_HSurface)& theS2, 
-                          const Standard_Real TolAng, 
-                          const Standard_Real TolTang, 
-                          const Standard_Boolean theApprox1,
-                          const Standard_Boolean theApprox2,
+                           const Handle(GeomAdaptor_HSurface)& theS2, 
+                           const Standard_Real TolAng, 
+                           const Standard_Real TolTang, 
+                           const Standard_Boolean theApprox1,
+                           const Standard_Boolean theApprox2,
                            IntTools_SequenceOfCurves& theSeqOfCurve, 
-                          Standard_Boolean& theTangentFaces);
+                           Standard_Boolean& theTangentFaces);
 
 static Standard_Boolean ClassifyLin2d(const Handle(GeomAdaptor_HSurface)& theS, 
-                                     const gp_Lin2d& theLin2d, 
-                                     const Standard_Real theTol,
-                                     Standard_Real& theP1, 
-                                     Standard_Real& theP2);
+                                      const gp_Lin2d& theLin2d, 
+                                      const Standard_Real theTol,
+                                      Standard_Real& theP1, 
+                                      Standard_Real& theP2);
 //
 static
   void ApproxParameters(const Handle(GeomAdaptor_HSurface)& aHS1,
-                       const Handle(GeomAdaptor_HSurface)& aHS2,
-                       Standard_Integer& iDegMin,
-                       Standard_Integer& iNbIter,
-                       Standard_Integer& iDegMax);
+                        const Handle(GeomAdaptor_HSurface)& aHS2,
+                        Standard_Integer& iDegMin,
+                        Standard_Integer& iNbIter,
+                        Standard_Integer& iDegMax);
 
 static
   void Tolerances(const Handle(GeomAdaptor_HSurface)& aHS1,
-                 const Handle(GeomAdaptor_HSurface)& aHS2,
+                  const Handle(GeomAdaptor_HSurface)& aHS2,
                   Standard_Real& aTolTang);
 
 static
   Standard_Boolean SortTypes(const GeomAbs_SurfaceType aType1,
-                            const GeomAbs_SurfaceType aType2);
+                             const GeomAbs_SurfaceType aType2);
 static
   Standard_Integer IndexType(const GeomAbs_SurfaceType aType);
 
 //
 static
   Standard_Real MaxSquareDistance (const Standard_Real aT,
-                                  const Handle(Geom_Curve)& aC3D,
-                                  const Handle(Geom2d_Curve)& aC2D1,
-                                  const Handle(Geom2d_Curve)& aC2D2,
-                                  const Handle(GeomAdaptor_HSurface) myHS1,
-                                  const Handle(GeomAdaptor_HSurface) myHS2,
-                                  const TopoDS_Face& aF1,
-                                  const TopoDS_Face& aF2,
+                                   const Handle(Geom_Curve)& aC3D,
+                                   const Handle(Geom2d_Curve)& aC2D1,
+                                   const Handle(Geom2d_Curve)& aC2D2,
+                                   const Handle(GeomAdaptor_HSurface) myHS1,
+                                   const Handle(GeomAdaptor_HSurface) myHS2,
+                                   const TopoDS_Face& aF1,
+                                   const TopoDS_Face& aF2,
                                    const Handle(IntTools_Context)& aCtx);
 
 static
@@ -305,15 +298,15 @@ static
 //
 static
   Standard_Real FindMaxSquareDistance (const Standard_Real aA,
-                                      const Standard_Real aB,
-                                      const Standard_Real aEps,
-                                      const Handle(Geom_Curve)& aC3D,
-                                      const Handle(Geom2d_Curve)& aC2D1,
-                                      const Handle(Geom2d_Curve)& aC2D2,
-                                      const Handle(GeomAdaptor_HSurface)& myHS1,
-                                      const Handle(GeomAdaptor_HSurface)& myHS2,
-                                      const TopoDS_Face& aF1,
-                                      const TopoDS_Face& aF2,
+                                       const Standard_Real aB,
+                                       const Standard_Real aEps,
+                                       const Handle(Geom_Curve)& aC3D,
+                                       const Handle(Geom2d_Curve)& aC2D1,
+                                       const Handle(Geom2d_Curve)& aC2D2,
+                                       const Handle(GeomAdaptor_HSurface)& myHS1,
+                                       const Handle(GeomAdaptor_HSurface)& myHS2,
+                                       const TopoDS_Face& aF1,
+                                       const TopoDS_Face& aF2,
                                        const Handle(IntTools_Context)& aCtx);
 
 //=======================================================================
@@ -420,9 +413,9 @@ Standard_Real IntTools_FaceFace::TolReached2d() const
 //
 // =======================================================================
 void IntTools_FaceFace::SetParameters(const Standard_Boolean ToApproxC3d,
-                                     const Standard_Boolean ToApproxC2dOnS1,
-                                     const Standard_Boolean ToApproxC2dOnS2,
-                                     const Standard_Real ApproximationTolerance) 
+                                      const Standard_Boolean ToApproxC2dOnS1,
+                                      const Standard_Boolean ToApproxC2dOnS2,
+                                      const Standard_Real ApproximationTolerance) 
 {
   myApprox = ToApproxC3d;
   myApprox1 = ToApproxC2dOnS1;
@@ -555,9 +548,9 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       aItP2S.Initialize(myListOfPnts);
       for (; aItP2S.More(); aItP2S.Next())
       {
-       IntSurf_PntOn2S& aP2S=aItP2S.Value();
-       aP2S.Parameters(aU1,aV1,aU2,aV2);
-       aP2S.SetValue(aU2,aV2,aU1,aV1);
+        IntSurf_PntOn2S& aP2S=aItP2S.Value();
+        aP2S.Parameters(aU1,aV1,aU2,aV2);
+        aP2S.SetValue(aU2,aV2,aU1,aV1);
       }
     }
   }
@@ -612,26 +605,26 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     if(!myTangentFaces) {
       const Standard_Integer NbLinPP = mySeqOfCurve.Length();
       if(NbLinPP) {
-       Standard_Real aTolFMax;
-       myTolReached3d = 1.e-7;
-       aTolFMax=Max(aTolF1, aTolF2);
+        Standard_Real aTolFMax;
+        myTolReached3d = 1.e-7;
+        aTolFMax=Max(aTolF1, aTolF2);
         if (aTolFMax>myTolReached3d) {
-         myTolReached3d=aTolFMax;
-       }
+          myTolReached3d=aTolFMax;
+        }
         //
-       myTolReached2d = myTolReached3d;
+        myTolReached2d = myTolReached3d;
 
         if (bReverse) {
-         Handle(Geom2d_Curve) aC2D1, aC2D2;
+          Handle(Geom2d_Curve) aC2D1, aC2D2;
           const Standard_Integer aNbLin = mySeqOfCurve.Length();
           for (Standard_Integer i = 1; i <= aNbLin; ++i) {
-           IntTools_Curve& aIC=mySeqOfCurve(i);
-           aC2D1=aIC.FirstCurve2d();
-           aC2D2=aIC.SecondCurve2d();
-           aIC.SetFirstCurve2d(aC2D2);
-           aIC.SetSecondCurve2d(aC2D1);
-         }
-       }
+            IntTools_Curve& aIC=mySeqOfCurve(i);
+            aC2D1=aIC.FirstCurve2d();
+            aC2D2=aIC.SecondCurve2d();
+            aIC.SetFirstCurve2d(aC2D2);
+            aIC.SetSecondCurve2d(aC2D1);
+          }
+        }
       }
     }
     return;
@@ -745,13 +738,13 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       aExp.Init(aF, TopAbs_EDGE);
       for(; aExp.More(); aExp.Next())
       {
-       const TopoDS_Edge& aE=TopoDS::Edge(aExp.Current());
+        const TopoDS_Edge& aE=TopoDS::Edge(aExp.Current());
 
         if(BRep_Tool::Degenerated(aE))
         {
-         RestrictLine = Standard_True;
-         break;
-       }
+          RestrictLine = Standard_True;
+          break;
+        }
       }
     }
   }
@@ -786,11 +779,11 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       const Standard_Integer aNbLin=mySeqOfCurve.Length();
       for (Standard_Integer i=1; i<=aNbLin; ++i)
       {
-       IntTools_Curve& aIC=mySeqOfCurve(i);
-       aC2D1=aIC.FirstCurve2d();
-       aC2D2=aIC.SecondCurve2d();
-       aIC.SetFirstCurve2d(aC2D2);
-       aIC.SetSecondCurve2d(aC2D1);
+        IntTools_Curve& aIC=mySeqOfCurve(i);
+        aC2D1=aIC.FirstCurve2d();
+        aC2D2=aIC.SecondCurve2d();
+        aIC.SetFirstCurve2d(aC2D2);
+        aIC.SetSecondCurve2d(aC2D1);
       }
     }
 
@@ -810,13 +803,13 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       //
       if (!bReverse)
       {
-       aPntOn2Faces.SetP1(aPntOnF1);
-       aPntOn2Faces.SetP2(aPntOnF2);
+        aPntOn2Faces.SetP1(aPntOnF1);
+        aPntOn2Faces.SetP2(aPntOnF2);
       }
       else
       {
-       aPntOn2Faces.SetP2(aPntOnF1);
-       aPntOn2Faces.SetP1(aPntOnF2);
+        aPntOn2Faces.SetP2(aPntOnF1);
+        aPntOn2Faces.SetP1(aPntOnF2);
       }
 
       myPnts.Append(aPntOn2Faces);
@@ -855,20 +848,20 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       aTL1=aIL1->ArcType();
       aTL2=aIL2->ArcType();
       if (aTL1==IntPatch_Lin && aTL2==IntPatch_Lin) {
-       Standard_Real aD, aDTresh, dTol;
-       gp_Lin aL1, aL2;
-       //
-       dTol=1.e-8;
-       aDTresh=1.5e-6;
-       //
-       aL1=Handle(IntPatch_GLine)::DownCast(aIL1)->Line();
-       aL2=Handle(IntPatch_GLine)::DownCast(aIL2)->Line();
-       aD=aL1.Distance(aL2);
-       aD=0.5*aD;
-       if (aD<aDTresh) {
-         myTolReached3d=aD+dTol;
-       }
-       return;
+        Standard_Real aD, aDTresh, dTol;
+        gp_Lin aL1, aL2;
+        //
+        dTol=1.e-8;
+        aDTresh=1.5e-6;
+        //
+        aL1=Handle(IntPatch_GLine)::DownCast(aIL1)->Line();
+        aL2=Handle(IntPatch_GLine)::DownCast(aIL2)->Line();
+        aD=aL1.Distance(aL2);
+        aD=0.5*aD;
+        if (aD<aDTresh) {
+          myTolReached3d=aD+dTol;
+        }
+        return;
       }
     }
     //ZZ
@@ -881,34 +874,34 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       aNbLin=mySeqOfCurve.Length();
       //
       for (i=1; i<=aNbLin; ++i) {
-       const IntTools_Curve& aIC=mySeqOfCurve(i);
-       const Handle(Geom_Curve)& aC3D=aIC.Curve();
-       const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
-       const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
-       //
-       if (aC3D.IsNull()) {
-         continue;
-       }
-       const Handle(Geom_BSplineCurve)& aBC=
-         Handle(Geom_BSplineCurve)::DownCast(aC3D);
-       if (aBC.IsNull()) {
-         continue;
-       }
-       //
-       aT1=aBC->FirstParameter();
-       aT2=aBC->LastParameter();
-       //
-       aEps=0.01*(aT2-aT1);
-       dT=(aT2-aT1)/aNbP;
-       for (j=1; j<aNbP; ++j) {
-         aT11=aT1+j*dT;
-         aT12=aT11+dT;
-         aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
-                                   myHS1, myHS2, myFace1, myFace2, myContext);
-         if (aD2>aD2Max) {
-           aD2Max=aD2;
-         }
-       }
+        const IntTools_Curve& aIC=mySeqOfCurve(i);
+        const Handle(Geom_Curve)& aC3D=aIC.Curve();
+        const Handle(Geom2d_Curve)& aC2D1=aIC.FirstCurve2d();
+        const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
+        //
+        if (aC3D.IsNull()) {
+          continue;
+        }
+        const Handle(Geom_BSplineCurve)& aBC=
+          Handle(Geom_BSplineCurve)::DownCast(aC3D);
+        if (aBC.IsNull()) {
+          continue;
+        }
+        //
+        aT1=aBC->FirstParameter();
+        aT2=aBC->LastParameter();
+        //
+        aEps=0.01*(aT2-aT1);
+        dT=(aT2-aT1)/aNbP;
+        for (j=1; j<aNbP; ++j) {
+          aT11=aT1+j*dT;
+          aT12=aT11+dT;
+          aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
+                                    myHS1, myHS2, myFace1, myFace2, myContext);
+          if (aD2>aD2Max) {
+            aD2Max=aD2;
+          }
+        }
       }//for (i=1; i<=aNbLin; ++i) {
       //
       myTolReached3d=sqrt(aD2Max);
@@ -932,40 +925,40 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
   //t
   //IFV Bug OCC20297 
   else if((aType1 == GeomAbs_Cylinder && aType2 == GeomAbs_Plane) ||
-         (aType2 == GeomAbs_Cylinder && aType1 == GeomAbs_Plane)) {
+          (aType2 == GeomAbs_Cylinder && aType1 == GeomAbs_Plane)) {
     if(aNbLin == 1) {
       const Handle(IntPatch_Line)& aIL1 = myIntersector.Line(1);
       if(aIL1->ArcType() == IntPatch_Circle) {
-       gp_Circ aCir = Handle(IntPatch_GLine)::DownCast(aIL1)->Circle();
-       gp_XYZ aCirDir = aCir.Axis().Direction().XYZ();
-       gp_XYZ aPlDir;
-       gp_Pln aPln;
-       if(aType1 == GeomAbs_Plane) {
-         aPln = myHS1->Surface().Plane();
-       }
-       else {
-         aPln = myHS2->Surface().Plane();
-       }
-       aPlDir = aPln.Axis().Direction().XYZ();
-       Standard_Real cs = aCirDir*aPlDir;
-       if(cs < 0.) aPlDir.Reverse();
-       Standard_Real eps = 1.e-14;
-       if(!aPlDir.IsEqual(aCirDir, eps)) {
-         Standard_Integer aNbP = 11;
-         Standard_Real dt = 2.*M_PI / (aNbP - 1), t;
-         for(t = 0.; t < 2.*M_PI; t += dt) {
-           Standard_Real d = aPln.Distance(ElCLib::Value(t, aCir)); 
-           if(myTolReached3d < d) myTolReached3d = d;
-         }
-         myTolReached3d *= 1.1;
-       }
+        gp_Circ aCir = Handle(IntPatch_GLine)::DownCast(aIL1)->Circle();
+        gp_XYZ aCirDir = aCir.Axis().Direction().XYZ();
+        gp_XYZ aPlDir;
+        gp_Pln aPln;
+        if(aType1 == GeomAbs_Plane) {
+          aPln = myHS1->Surface().Plane();
+        }
+        else {
+          aPln = myHS2->Surface().Plane();
+        }
+        aPlDir = aPln.Axis().Direction().XYZ();
+        Standard_Real cs = aCirDir*aPlDir;
+        if(cs < 0.) aPlDir.Reverse();
+        Standard_Real eps = 1.e-14;
+        if(!aPlDir.IsEqual(aCirDir, eps)) {
+          Standard_Integer aNbP = 11;
+          Standard_Real dt = 2.*M_PI / (aNbP - 1), t;
+          for(t = 0.; t < 2.*M_PI; t += dt) {
+            Standard_Real d = aPln.Distance(ElCLib::Value(t, aCir)); 
+            if(myTolReached3d < d) myTolReached3d = d;
+          }
+          myTolReached3d *= 1.1;
+        }
       } //aIL1->ArcType() == IntPatch_Circle
     } //aNbLin == 1
   } // aType1 == GeomAbs_Cylinder && aType2 == GeomAbs_Plane) 
   //End IFV Bug OCC20297
   //
   else if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Torus) ||
-          (aType2==GeomAbs_Plane && aType1==GeomAbs_Torus)) {
+           (aType2==GeomAbs_Plane && aType1==GeomAbs_Torus)) {
     aNbLin=mySeqOfCurve.Length();
     if (aNbLin!=1) {
       return;
@@ -998,7 +991,7 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     for (i=0; i<aNbP; ++i) {
       aT=aT1+i*dT;
       if (i==aNbP-1) {
-       aT=aT2;
+        aT=aT2;
       }
       //
       aC3D->D0(aT, aP);
@@ -1007,14 +1000,14 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       aPP=ElSLib::Value(aUP, aVP, aPln);
       aDP=aP.SquareDistance(aPP);
       if (aDP>aDmax) {
-       aDmax=aDP;
+        aDmax=aDP;
       }
       //
       ElSLib::Parameters(aTorus, aP, aUT, aVT);
       aPT=ElSLib::Value(aUT, aVT, aTorus);
       aDT=aP.SquareDistance(aPT);
       if (aDT>aDmax) {
-       aDmax=aDT;
+        aDmax=aDT;
       }
     }
     //
@@ -1025,7 +1018,7 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
   }// if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Torus) ||
   //
   else if ((aType1==GeomAbs_SurfaceOfRevolution && aType2==GeomAbs_Cylinder) ||
-          (aType2==GeomAbs_SurfaceOfRevolution && aType1==GeomAbs_Cylinder)) {
+           (aType2==GeomAbs_SurfaceOfRevolution && aType1==GeomAbs_Cylinder)) {
     Standard_Integer j, aNbP;
     Standard_Real aT, aT1, aT2, dT, aD2max, aD2;
     //
@@ -1040,12 +1033,12 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
       //
       if (aC3D.IsNull()) {
-       continue;
+        continue;
       }
       const Handle(Geom_BSplineCurve)& aBC=
-       Handle(Geom_BSplineCurve)::DownCast(aC3D);
+        Handle(Geom_BSplineCurve)::DownCast(aC3D);
       if (aBC.IsNull()) {
-       return;
+        return;
       }
       //
       aT1=aBC->FirstParameter();
@@ -1053,16 +1046,16 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       //
       dT=(aT2-aT1)/(aNbP-1);
       for (j=0; j<aNbP; ++j) {
-       aT=aT1+j*dT;
-       if (j==aNbP-1) {
-         aT=aT2;
-       }
-       //
-       aD2=MaxSquareDistance(aT, aC3D, aC2D1, aC2D2,
-                             myHS1, myHS2, myFace1, myFace2, myContext);
-       if (aD2>aD2max) {
-         aD2max=aD2;
-       }
+        aT=aT1+j*dT;
+        if (j==aNbP-1) {
+          aT=aT2;
+        }
+        //
+        aD2=MaxSquareDistance(aT, aC3D, aC2D1, aC2D2,
+                              myHS1, myHS2, myFace1, myFace2, myContext);
+        if (aD2>aD2max) {
+          aD2max=aD2;
+        }
       }//for (j=0; j<aNbP; ++j) {
      
     }//for (i=1; i<=aNbLin; ++i) {
@@ -1073,7 +1066,7 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     }
   }//if((aType1==GeomAbs_SurfaceOfRevolution ...
   else if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Sphere) ||
-          (aType2==GeomAbs_Plane && aType1==GeomAbs_Sphere)) {
+           (aType2==GeomAbs_Plane && aType1==GeomAbs_Sphere)) {
     Standard_Integer  j, aNbP;
     Standard_Real aT1, aT2, dT, aD2max, aD2, aEps, aT11, aT12;
     //
@@ -1088,37 +1081,37 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       const Handle(Geom2d_Curve)& aC2D2=aIC.SecondCurve2d();
       //
       const Handle(Geom2d_BSplineCurve)& aBC2D1=
-       Handle(Geom2d_BSplineCurve)::DownCast(aC2D1);
+        Handle(Geom2d_BSplineCurve)::DownCast(aC2D1);
       const Handle(Geom2d_BSplineCurve)& aBC2D2=
-       Handle(Geom2d_BSplineCurve)::DownCast(aC2D2);
+        Handle(Geom2d_BSplineCurve)::DownCast(aC2D2);
       //
       if (aBC2D1.IsNull() && aBC2D2.IsNull()) {
-       return;
+        return;
       }
       //
       if (!aBC2D1.IsNull()) {
-       aT1=aBC2D1->FirstParameter();
-       aT2=aBC2D1->LastParameter();
+        aT1=aBC2D1->FirstParameter();
+        aT2=aBC2D1->LastParameter();
       }
       else {
-       aT1=aBC2D2->FirstParameter();
-       aT2=aBC2D2->LastParameter();
+        aT1=aBC2D2->FirstParameter();
+        aT2=aBC2D2->LastParameter();
       }
       //
       aEps=0.01*(aT2-aT1);
       dT=(aT2-aT1)/aNbP;
       for (j=0; j<aNbP; ++j) {
-       aT11=aT1+j*dT;
-       aT12=aT11+dT;
-       if (j==aNbP-1) {
-         aT12=aT2;
-       }
-       //
-       aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
-                                 myHS1, myHS2, myFace1, myFace2, myContext);
-       if (aD2>aD2max) {
-         aD2max=aD2;
-       }
+        aT11=aT1+j*dT;
+        aT12=aT11+dT;
+        if (j==aNbP-1) {
+          aT12=aT2;
+        }
+        //
+        aD2=FindMaxSquareDistance(aT11, aT12, aEps, aC3D, aC2D1, aC2D2,
+                                  myHS1, myHS2, myFace1, myFace2, myContext);
+        if (aD2>aD2max) {
+          aD2max=aD2;
+        }
       }//for (j=0; j<aNbP; ++j) {
      
     }//for (i=1; i<=aNbLin; ++i) {
@@ -1175,8 +1168,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 //purpose  : 
 //=======================================================================
   void IntTools_FaceFace::MakeCurve(const Standard_Integer Index,
-                                   const Handle(Adaptor3d_TopolTool)& dom1,
-                                   const Handle(Adaptor3d_TopolTool)& dom2) 
+                                    const Handle(Adaptor3d_TopolTool)& dom1,
+                                    const Handle(Adaptor3d_TopolTool)& dom2) 
 {
   Standard_Boolean bDone, rejectSurface, reApprox, bAvoidLineConstructor;
   Standard_Boolean ok, bPCurvesOk;
@@ -1258,17 +1251,17 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
   case IntPatch_Hyperbola: {
     if (typl == IntPatch_Lin) {
       newc = 
-       new Geom_Line (Handle(IntPatch_GLine)::DownCast(L)->Line());
+        new Geom_Line (Handle(IntPatch_GLine)::DownCast(L)->Line());
     }
 
     else if (typl == IntPatch_Parabola) {
       newc = 
-       new Geom_Parabola(Handle(IntPatch_GLine)::DownCast(L)->Parabola());
+        new Geom_Parabola(Handle(IntPatch_GLine)::DownCast(L)->Parabola());
     }
     
     else if (typl == IntPatch_Hyperbola) {
       newc = 
-       new Geom_Hyperbola (Handle(IntPatch_GLine)::DownCast(L)->Hyperbola());
+        new Geom_Hyperbola (Handle(IntPatch_GLine)::DownCast(L)->Hyperbola());
     }
     //
     // myTolReached3d
@@ -1281,73 +1274,73 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       myLConstruct.Part(i, fprm, lprm);
       
       if (!Precision::IsNegativeInfinite(fprm) && 
-         !Precision::IsPositiveInfinite(lprm)) {
-       //
-       IntTools_Curve aCurve;
-       //
-       Handle(Geom_TrimmedCurve) aCT3D=new Geom_TrimmedCurve(newc, fprm, lprm);
-       aCurve.SetCurve(aCT3D);
-       if (typl == IntPatch_Parabola) {
-         Standard_Real aTolF1, aTolF2, aTolBase;
-         
-         aTolF1 = BRep_Tool::Tolerance(myFace1);
-         aTolF2 = BRep_Tool::Tolerance(myFace2);
-         aTolBase=aTolF1+aTolF2;
-         myTolReached3d=IntTools_Tools::CurveTolerance(aCT3D, aTolBase);
-       }
-       //
-       aCurve.SetCurve(new Geom_TrimmedCurve(newc, fprm, lprm));
-       if(myApprox1) { 
-         Handle (Geom2d_Curve) C2d;
-         BuildPCurves(fprm, lprm, Tolpc, myHS1->ChangeSurface().Surface(), newc, C2d);
-         if(Tolpc>myTolReached2d || myTolReached2d==0.) { 
-           myTolReached2d=Tolpc;
-         }
-           //     
-           aCurve.SetFirstCurve2d(new Geom2d_TrimmedCurve(C2d,fprm,lprm));
-         }
-         else { 
-           Handle(Geom2d_BSplineCurve) H1;
-           //
-           aCurve.SetFirstCurve2d(H1);
-         }
-       
-       if(myApprox2) { 
-         Handle (Geom2d_Curve) C2d;
-         BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
-         if(Tolpc>myTolReached2d || myTolReached2d==0.) { 
-           myTolReached2d=Tolpc;
-         }
-         //
-         aCurve.SetSecondCurve2d(new Geom2d_TrimmedCurve(C2d,fprm,lprm));
-         }
-       else { 
-         Handle(Geom2d_BSplineCurve) H1;
-         //
-         aCurve.SetSecondCurve2d(H1);
-       }
-       mySeqOfCurve.Append(aCurve);
+          !Precision::IsPositiveInfinite(lprm)) {
+        //
+        IntTools_Curve aCurve;
+        //
+        Handle(Geom_TrimmedCurve) aCT3D=new Geom_TrimmedCurve(newc, fprm, lprm);
+        aCurve.SetCurve(aCT3D);
+        if (typl == IntPatch_Parabola) {
+          Standard_Real aTolF1, aTolF2, aTolBase;
+          
+          aTolF1 = BRep_Tool::Tolerance(myFace1);
+          aTolF2 = BRep_Tool::Tolerance(myFace2);
+          aTolBase=aTolF1+aTolF2;
+          myTolReached3d=IntTools_Tools::CurveTolerance(aCT3D, aTolBase);
+        }
+        //
+        aCurve.SetCurve(new Geom_TrimmedCurve(newc, fprm, lprm));
+        if(myApprox1) { 
+          Handle (Geom2d_Curve) C2d;
+          BuildPCurves(fprm, lprm, Tolpc, myHS1->ChangeSurface().Surface(), newc, C2d);
+          if(Tolpc>myTolReached2d || myTolReached2d==0.) { 
+            myTolReached2d=Tolpc;
+          }
+            //            
+            aCurve.SetFirstCurve2d(new Geom2d_TrimmedCurve(C2d,fprm,lprm));
+          }
+          else { 
+            Handle(Geom2d_BSplineCurve) H1;
+            //
+            aCurve.SetFirstCurve2d(H1);
+          }
+        
+        if(myApprox2) { 
+          Handle (Geom2d_Curve) C2d;
+          BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
+          if(Tolpc>myTolReached2d || myTolReached2d==0.) { 
+            myTolReached2d=Tolpc;
+          }
+          //
+          aCurve.SetSecondCurve2d(new Geom2d_TrimmedCurve(C2d,fprm,lprm));
+          }
+        else { 
+          Handle(Geom2d_BSplineCurve) H1;
+          //
+          aCurve.SetSecondCurve2d(H1);
+        }
+        mySeqOfCurve.Append(aCurve);
       } // end of if (!Precision::IsNegativeInfinite(fprm) &&  !Precision::IsPositiveInfinite(lprm))
       else {
-       //  on regarde si on garde
-       //
-       Standard_Boolean bFNIt, bLPIt;
-       Standard_Real aTestPrm, dT=100.;
-
-       bFNIt=Precision::IsNegativeInfinite(fprm);
-       bLPIt=Precision::IsPositiveInfinite(lprm);
-       
-       aTestPrm=0.;
-       
-       if (bFNIt && !bLPIt) {
-         aTestPrm=lprm-dT;
-       }
-       else if (!bFNIt && bLPIt) {
-         aTestPrm=fprm+dT;
-       }
-       
-       gp_Pnt ptref(newc->Value(aTestPrm));
-       //
+        //  on regarde si on garde
+        //
+        Standard_Boolean bFNIt, bLPIt;
+        Standard_Real aTestPrm, dT=100.;
+
+        bFNIt=Precision::IsNegativeInfinite(fprm);
+        bLPIt=Precision::IsPositiveInfinite(lprm);
+        
+        aTestPrm=0.;
+        
+        if (bFNIt && !bLPIt) {
+          aTestPrm=lprm-dT;
+        }
+        else if (!bFNIt && bLPIt) {
+          aTestPrm=fprm+dT;
+        }
+        
+        gp_Pnt ptref(newc->Value(aTestPrm));
+        //
         GeomAbs_SurfaceType typS1 = myHS1->GetType();
         GeomAbs_SurfaceType typS2 = myHS2->GetType();
         if( typS1 == GeomAbs_SurfaceOfExtrusion ||
@@ -1362,18 +1355,18 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
           continue;
         }
 
-       Standard_Real u1, v1, u2, v2, Tol;
-       
-       Tol = Precision::Confusion();
-       Parameters(myHS1, myHS2, ptref,  u1, v1, u2, v2);
-       ok = (dom1->Classify(gp_Pnt2d(u1, v1), Tol) != TopAbs_OUT);
-       if(ok) { 
-         ok = (dom2->Classify(gp_Pnt2d(u2,v2),Tol) != TopAbs_OUT); 
-       }
-       if (ok) {
-         Handle(Geom2d_BSplineCurve) H1;
-         mySeqOfCurve.Append(IntTools_Curve(newc, H1, H1));
-       }
+        Standard_Real u1, v1, u2, v2, Tol;
+        
+        Tol = Precision::Confusion();
+        Parameters(myHS1, myHS2, ptref,  u1, v1, u2, v2);
+        ok = (dom1->Classify(gp_Pnt2d(u1, v1), Tol) != TopAbs_OUT);
+        if(ok) { 
+          ok = (dom2->Classify(gp_Pnt2d(u2,v2),Tol) != TopAbs_OUT); 
+        }
+        if (ok) {
+          Handle(Geom2d_BSplineCurve) H1;
+          mySeqOfCurve.Append(IntTools_Curve(newc, H1, H1));
+        }
       }
     }// end of for (i=1; i<=myLConstruct.NbParts(); i++)
   }// case IntPatch_Lin:  case IntPatch_Parabola:  case IntPatch_Hyperbola:
@@ -1387,11 +1380,11 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 
     if (typl == IntPatch_Circle) {
       newc = new Geom_Circle
-       (Handle(IntPatch_GLine)::DownCast(L)->Circle());
+        (Handle(IntPatch_GLine)::DownCast(L)->Circle());
     }
     else { //IntPatch_Ellipse
       newc = new Geom_Ellipse
-       (Handle(IntPatch_GLine)::DownCast(L)->Ellipse());
+        (Handle(IntPatch_GLine)::DownCast(L)->Ellipse());
     }
     //
     // myTolReached3d
@@ -1409,59 +1402,59 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       myLConstruct.Part(i, fprm, lprm);
 
       if (fprm < aNul && lprm > aNul) {
-       // interval that goes through 0. is divided on two intervals;
-       while (fprm<aNul || fprm>aPeriod) fprm=fprm+aPeriod;
-       while (lprm<aNul || lprm>aPeriod) lprm=lprm+aPeriod;
-       //
-       if((aPeriod - fprm) > Tolpc) {
-         aSeqFprm.Append(fprm);
-         aSeqLprm.Append(aPeriod);
-       }
-       else {
-         gp_Pnt P1 = newc->Value(fprm);
-         gp_Pnt P2 = newc->Value(aPeriod);
-         Standard_Real aTolDist = BRep_Tool::Tolerance(myFace1) + BRep_Tool::Tolerance(myFace2);
-         aTolDist = (myTolReached3d > aTolDist) ? myTolReached3d : aTolDist;
-
-         if(P1.Distance(P2) > aTolDist) {
-           Standard_Real anewpar = fprm;
+        // interval that goes through 0. is divided on two intervals;
+        while (fprm<aNul || fprm>aPeriod) fprm=fprm+aPeriod;
+        while (lprm<aNul || lprm>aPeriod) lprm=lprm+aPeriod;
+        //
+        if((aPeriod - fprm) > Tolpc) {
+          aSeqFprm.Append(fprm);
+          aSeqLprm.Append(aPeriod);
+        }
+        else {
+          gp_Pnt P1 = newc->Value(fprm);
+          gp_Pnt P2 = newc->Value(aPeriod);
+          Standard_Real aTolDist = BRep_Tool::Tolerance(myFace1) + BRep_Tool::Tolerance(myFace2);
+          aTolDist = (myTolReached3d > aTolDist) ? myTolReached3d : aTolDist;
+
+          if(P1.Distance(P2) > aTolDist) {
+            Standard_Real anewpar = fprm;
 
             if(ParameterOutOfBoundary(fprm, newc, myFace1, myFace2, 
                                       lprm, Standard_False, anewpar, myContext)) {
-             fprm = anewpar;
-           }
-           aSeqFprm.Append(fprm);
-           aSeqLprm.Append(aPeriod);
-         }
-       }
-
-       //
-       if((lprm - aNul) > Tolpc) {
-         aSeqFprm.Append(aNul);
-         aSeqLprm.Append(lprm);
-       }
-       else {
-         gp_Pnt P1 = newc->Value(aNul);
-         gp_Pnt P2 = newc->Value(lprm);
-         Standard_Real aTolDist = BRep_Tool::Tolerance(myFace1) + BRep_Tool::Tolerance(myFace2);
-         aTolDist = (myTolReached3d > aTolDist) ? myTolReached3d : aTolDist;
-
-         if(P1.Distance(P2) > aTolDist) {
-           Standard_Real anewpar = lprm;
+              fprm = anewpar;
+            }
+            aSeqFprm.Append(fprm);
+            aSeqLprm.Append(aPeriod);
+          }
+        }
+
+        //
+        if((lprm - aNul) > Tolpc) {
+          aSeqFprm.Append(aNul);
+          aSeqLprm.Append(lprm);
+        }
+        else {
+          gp_Pnt P1 = newc->Value(aNul);
+          gp_Pnt P2 = newc->Value(lprm);
+          Standard_Real aTolDist = BRep_Tool::Tolerance(myFace1) + BRep_Tool::Tolerance(myFace2);
+          aTolDist = (myTolReached3d > aTolDist) ? myTolReached3d : aTolDist;
+
+          if(P1.Distance(P2) > aTolDist) {
+            Standard_Real anewpar = lprm;
 
             if(ParameterOutOfBoundary(lprm, newc, myFace1, myFace2, 
                                       fprm, Standard_True, anewpar, myContext)) {
-             lprm = anewpar;
-           }
-           aSeqFprm.Append(aNul);
-           aSeqLprm.Append(lprm);
-         }
-       }
+              lprm = anewpar;
+            }
+            aSeqFprm.Append(aNul);
+            aSeqLprm.Append(lprm);
+          }
+        }
       }
       else {
-       // usual interval 
-       aSeqFprm.Append(fprm);
-       aSeqLprm.Append(lprm);
+        // usual interval 
+        aSeqFprm.Append(fprm);
+        aSeqLprm.Append(lprm);
       }
     }
     
@@ -1473,161 +1466,161 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       //
       Standard_Real aRealEpsilon=RealEpsilon();
       if (Abs(fprm) > aRealEpsilon || Abs(lprm-2.*M_PI) > aRealEpsilon) {
-       //==============================================
-       ////
-       IntTools_Curve aCurve;
-       Handle(Geom_TrimmedCurve) aTC3D=new Geom_TrimmedCurve(newc,fprm,lprm);
-       aCurve.SetCurve(aTC3D);
-       fprm=aTC3D->FirstParameter();
-       lprm=aTC3D->LastParameter ();
-       ////    
-       if (typl == IntPatch_Circle || typl == IntPatch_Ellipse) {//// 
-         if(myApprox1) { 
-           Handle (Geom2d_Curve) C2d;
-           BuildPCurves(fprm,lprm,Tolpc,myHS1->ChangeSurface().Surface(),newc,C2d);
-           if(Tolpc>myTolReached2d || myTolReached2d==0) { 
-             myTolReached2d=Tolpc;
-           }
-           //
-           aCurve.SetFirstCurve2d(C2d);
-         }
-         else { //// 
-           Handle(Geom2d_BSplineCurve) H1;
-           aCurve.SetFirstCurve2d(H1);
-         }
-
-
-         if(myApprox2) { 
-           Handle (Geom2d_Curve) C2d;
-           BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
-           if(Tolpc>myTolReached2d || myTolReached2d==0) { 
-             myTolReached2d=Tolpc;
-           }
-           //
-           aCurve.SetSecondCurve2d(C2d);
-         }
-         else { 
-           Handle(Geom2d_BSplineCurve) H1;
-           aCurve.SetSecondCurve2d(H1);
-         }
-       }
-       
-       else { 
-         Handle(Geom2d_BSplineCurve) H1;
-         aCurve.SetFirstCurve2d(H1);
-         aCurve.SetSecondCurve2d(H1);
-       }
-       mySeqOfCurve.Append(aCurve);
-         //==============================================      
+        //==============================================
+        ////
+        IntTools_Curve aCurve;
+        Handle(Geom_TrimmedCurve) aTC3D=new Geom_TrimmedCurve(newc,fprm,lprm);
+        aCurve.SetCurve(aTC3D);
+        fprm=aTC3D->FirstParameter();
+        lprm=aTC3D->LastParameter ();
+        ////         
+        if (typl == IntPatch_Circle || typl == IntPatch_Ellipse) {//// 
+          if(myApprox1) { 
+            Handle (Geom2d_Curve) C2d;
+            BuildPCurves(fprm,lprm,Tolpc,myHS1->ChangeSurface().Surface(),newc,C2d);
+            if(Tolpc>myTolReached2d || myTolReached2d==0) { 
+              myTolReached2d=Tolpc;
+            }
+            //
+            aCurve.SetFirstCurve2d(C2d);
+          }
+          else { //// 
+            Handle(Geom2d_BSplineCurve) H1;
+            aCurve.SetFirstCurve2d(H1);
+          }
+
+
+          if(myApprox2) { 
+            Handle (Geom2d_Curve) C2d;
+            BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
+            if(Tolpc>myTolReached2d || myTolReached2d==0) { 
+              myTolReached2d=Tolpc;
+            }
+            //
+            aCurve.SetSecondCurve2d(C2d);
+          }
+          else { 
+            Handle(Geom2d_BSplineCurve) H1;
+            aCurve.SetSecondCurve2d(H1);
+          }
+        }
+        
+        else { 
+          Handle(Geom2d_BSplineCurve) H1;
+          aCurve.SetFirstCurve2d(H1);
+          aCurve.SetSecondCurve2d(H1);
+        }
+        mySeqOfCurve.Append(aCurve);
+          //==============================================        
       } //if (Abs(fprm) > RealEpsilon() || Abs(lprm-2.*M_PI) > RealEpsilon())
 
       else {
-       //  on regarde si on garde
-       //
-       if (aNbParts==1) {
-//       if (Abs(fprm) < RealEpsilon() &&  Abs(lprm-2.*M_PI) < RealEpsilon()) {
-         if (Abs(fprm) <= aRealEpsilon && Abs(lprm-2.*M_PI) <= aRealEpsilon) {
-           IntTools_Curve aCurve;
-           Handle(Geom_TrimmedCurve) aTC3D=new Geom_TrimmedCurve(newc,fprm,lprm);
-           aCurve.SetCurve(aTC3D);
-           fprm=aTC3D->FirstParameter();
-           lprm=aTC3D->LastParameter ();
-           
-           if(myApprox1) { 
-             Handle (Geom2d_Curve) C2d;
-             BuildPCurves(fprm,lprm,Tolpc,myHS1->ChangeSurface().Surface(),newc,C2d);
-             if(Tolpc>myTolReached2d || myTolReached2d==0) { 
-               myTolReached2d=Tolpc;
-             }
-             //
-             aCurve.SetFirstCurve2d(C2d);
-           }
-           else { //// 
-             Handle(Geom2d_BSplineCurve) H1;
-             aCurve.SetFirstCurve2d(H1);
-           }
-
-           if(myApprox2) { 
-             Handle (Geom2d_Curve) C2d;
-             BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
-             if(Tolpc>myTolReached2d || myTolReached2d==0) { 
-               myTolReached2d=Tolpc;
-             }
-             //
-             aCurve.SetSecondCurve2d(C2d);
-           }
-           else { 
-             Handle(Geom2d_BSplineCurve) H1;
-             aCurve.SetSecondCurve2d(H1);
-           }
-           mySeqOfCurve.Append(aCurve);
-           break;
-         }
-       }
-       //
-       Standard_Real aTwoPIdiv17, u1, v1, u2, v2, Tol;
-
-       aTwoPIdiv17=2.*M_PI/17.;
-
-       for (j=0; j<=17; j++) {
-         gp_Pnt ptref (newc->Value (j*aTwoPIdiv17));
-         Tol = Precision::Confusion();
-
-         Parameters(myHS1, myHS2, ptref, u1, v1, u2, v2);
-         ok = (dom1->Classify(gp_Pnt2d(u1,v1),Tol) != TopAbs_OUT);
-         if(ok) { 
-           ok = (dom2->Classify(gp_Pnt2d(u2,v2),Tol) != TopAbs_OUT);
-         }
-         if (ok) {
-           IntTools_Curve aCurve;
-           aCurve.SetCurve(newc);
-           //==============================================
-           if (typl == IntPatch_Circle || typl == IntPatch_Ellipse) {
-             
-             if(myApprox1) { 
-               Handle (Geom2d_Curve) C2d;
-               BuildPCurves(fprm, lprm, Tolpc, myHS1->ChangeSurface().Surface(), newc, C2d);
-               if(Tolpc>myTolReached2d || myTolReached2d==0) { 
-                 myTolReached2d=Tolpc;
-               }
-               // 
-               aCurve.SetFirstCurve2d(C2d);
-             }
-             else { 
-               Handle(Geom2d_BSplineCurve) H1;
-               aCurve.SetFirstCurve2d(H1);
-             }
-               
-             if(myApprox2) { 
-               Handle (Geom2d_Curve) C2d;
-               BuildPCurves(fprm, lprm, Tolpc,myHS2->ChangeSurface().Surface(), newc, C2d);
-               if(Tolpc>myTolReached2d || myTolReached2d==0) { 
-                 myTolReached2d=Tolpc;
-               }
-               //              
-               aCurve.SetSecondCurve2d(C2d);
-             }
-               
-             else { 
-               Handle(Geom2d_BSplineCurve) H1;
-               aCurve.SetSecondCurve2d(H1);
-             }
-           }//  end of if (typl == IntPatch_Circle || typl == IntPatch_Ellipse)
-            
-           else { 
-             Handle(Geom2d_BSplineCurve) H1;
-             //        
-             aCurve.SetFirstCurve2d(H1);
-             aCurve.SetSecondCurve2d(H1);
-           }
-           //==============================================    
-           //
-           mySeqOfCurve.Append(aCurve);
-           break;
-
-           }//  end of if (ok) {
-         }//  end of for (Standard_Integer j=0; j<=17; j++)
-       }//  end of else { on regarde si on garde
+        //  on regarde si on garde
+        //
+        if (aNbParts==1) {
+//           if (Abs(fprm) < RealEpsilon() &&  Abs(lprm-2.*M_PI) < RealEpsilon()) {
+          if (Abs(fprm) <= aRealEpsilon && Abs(lprm-2.*M_PI) <= aRealEpsilon) {
+            IntTools_Curve aCurve;
+            Handle(Geom_TrimmedCurve) aTC3D=new Geom_TrimmedCurve(newc,fprm,lprm);
+            aCurve.SetCurve(aTC3D);
+            fprm=aTC3D->FirstParameter();
+            lprm=aTC3D->LastParameter ();
+            
+            if(myApprox1) { 
+              Handle (Geom2d_Curve) C2d;
+              BuildPCurves(fprm,lprm,Tolpc,myHS1->ChangeSurface().Surface(),newc,C2d);
+              if(Tolpc>myTolReached2d || myTolReached2d==0) { 
+                myTolReached2d=Tolpc;
+              }
+              //
+              aCurve.SetFirstCurve2d(C2d);
+            }
+            else { //// 
+              Handle(Geom2d_BSplineCurve) H1;
+              aCurve.SetFirstCurve2d(H1);
+            }
+
+            if(myApprox2) { 
+              Handle (Geom2d_Curve) C2d;
+              BuildPCurves(fprm,lprm,Tolpc,myHS2->ChangeSurface().Surface(),newc,C2d);
+              if(Tolpc>myTolReached2d || myTolReached2d==0) { 
+                myTolReached2d=Tolpc;
+              }
+              //
+              aCurve.SetSecondCurve2d(C2d);
+            }
+            else { 
+              Handle(Geom2d_BSplineCurve) H1;
+              aCurve.SetSecondCurve2d(H1);
+            }
+            mySeqOfCurve.Append(aCurve);
+            break;
+          }
+        }
+        //
+        Standard_Real aTwoPIdiv17, u1, v1, u2, v2, Tol;
+
+        aTwoPIdiv17=2.*M_PI/17.;
+
+        for (j=0; j<=17; j++) {
+          gp_Pnt ptref (newc->Value (j*aTwoPIdiv17));
+          Tol = Precision::Confusion();
+
+          Parameters(myHS1, myHS2, ptref, u1, v1, u2, v2);
+          ok = (dom1->Classify(gp_Pnt2d(u1,v1),Tol) != TopAbs_OUT);
+          if(ok) { 
+            ok = (dom2->Classify(gp_Pnt2d(u2,v2),Tol) != TopAbs_OUT);
+          }
+          if (ok) {
+            IntTools_Curve aCurve;
+            aCurve.SetCurve(newc);
+            //==============================================
+            if (typl == IntPatch_Circle || typl == IntPatch_Ellipse) {
+              
+              if(myApprox1) { 
+                Handle (Geom2d_Curve) C2d;
+                BuildPCurves(fprm, lprm, Tolpc, myHS1->ChangeSurface().Surface(), newc, C2d);
+                if(Tolpc>myTolReached2d || myTolReached2d==0) { 
+                  myTolReached2d=Tolpc;
+                }
+                // 
+                aCurve.SetFirstCurve2d(C2d);
+              }
+              else { 
+                Handle(Geom2d_BSplineCurve) H1;
+                aCurve.SetFirstCurve2d(H1);
+              }
+                
+              if(myApprox2) { 
+                Handle (Geom2d_Curve) C2d;
+                BuildPCurves(fprm, lprm, Tolpc,myHS2->ChangeSurface().Surface(), newc, C2d);
+                if(Tolpc>myTolReached2d || myTolReached2d==0) { 
+                  myTolReached2d=Tolpc;
+                }
+                //                 
+                aCurve.SetSecondCurve2d(C2d);
+              }
+                
+              else { 
+                Handle(Geom2d_BSplineCurve) H1;
+                aCurve.SetSecondCurve2d(H1);
+              }
+            }//  end of if (typl == IntPatch_Circle || typl == IntPatch_Ellipse)
+             
+            else { 
+              Handle(Geom2d_BSplineCurve) H1;
+              //         
+              aCurve.SetFirstCurve2d(H1);
+              aCurve.SetSecondCurve2d(H1);
+            }
+            //==============================================        
+            //
+            mySeqOfCurve.Append(aCurve);
+            break;
+
+            }//  end of if (ok) {
+          }//  end of for (Standard_Integer j=0; j<=17; j++)
+        }//  end of else { on regarde si on garde
       }// for (i=1; i<=myLConstruct.NbParts(); i++)
     }// IntPatch_Circle: IntPatch_Ellipse:
     break;
@@ -1639,21 +1632,21 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     switch (typs) {
       case GeomAbs_Plane:
         quad1.SetValue(myHS1->Surface().Plane());
-       break;
+        break;
       case GeomAbs_Cylinder:
-       quad1.SetValue(myHS1->Surface().Cylinder());
-       break;
+        quad1.SetValue(myHS1->Surface().Cylinder());
+        break;
       case GeomAbs_Cone:
-       quad1.SetValue(myHS1->Surface().Cone());
-       break;
+        quad1.SetValue(myHS1->Surface().Cone());
+        break;
       case GeomAbs_Sphere:
-       quad1.SetValue(myHS1->Surface().Sphere());
-       break;
+        quad1.SetValue(myHS1->Surface().Sphere());
+        break;
     case GeomAbs_Torus:
       quad1.SetValue(myHS1->Surface().Torus());
       break;
       default:
-       Standard_ConstructionError::Raise("GeomInt_IntSS::MakeCurve 1");
+        Standard_ConstructionError::Raise("GeomInt_IntSS::MakeCurve 1");
       }
       
     typs = myHS2->Surface().GetType();
@@ -1661,21 +1654,21 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     switch (typs) {
       case GeomAbs_Plane:
         quad2.SetValue(myHS2->Surface().Plane());
-       break;
+        break;
       case GeomAbs_Cylinder:
-       quad2.SetValue(myHS2->Surface().Cylinder());
-       break;
+        quad2.SetValue(myHS2->Surface().Cylinder());
+        break;
       case GeomAbs_Cone:
-       quad2.SetValue(myHS2->Surface().Cone());
-       break;
+        quad2.SetValue(myHS2->Surface().Cone());
+        break;
       case GeomAbs_Sphere:
-       quad2.SetValue(myHS2->Surface().Sphere());
-       break;
+        quad2.SetValue(myHS2->Surface().Sphere());
+        break;
     case GeomAbs_Torus:
       quad2.SetValue(myHS2->Surface().Torus());
       break;
       default:
-       Standard_ConstructionError::Raise("GeomInt_IntSS::MakeCurve 2");
+        Standard_ConstructionError::Raise("GeomInt_IntSS::MakeCurve 2");
       }
     //
     //=========
@@ -1684,22 +1677,22 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     if (!myApprox) {
       aNbParts=myLConstruct.NbParts();
       for (i=1; i<=aNbParts; i++) {
-       myLConstruct.Part(i, fprm, lprm);
-       Handle(IntPatch_WLine) WL = 
-         convert.MakeWLine(Handle(IntPatch_ALine)::DownCast(L), fprm, lprm);
-       //
-       Handle(Geom2d_BSplineCurve) H1;
-       Handle(Geom2d_BSplineCurve) H2;
-
-       if(myApprox1) {
-         H1 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_True);
-       }
-       
-       if(myApprox2) {
-         H2 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_False);
-       }
-       //       
-       mySeqOfCurve.Append(IntTools_Curve(MakeBSpline(WL,1,WL->NbPnts()), H1, H2));
+        myLConstruct.Part(i, fprm, lprm);
+        Handle(IntPatch_WLine) WL = 
+          convert.MakeWLine(Handle(IntPatch_ALine)::DownCast(L), fprm, lprm);
+        //
+        Handle(Geom2d_BSplineCurve) H1;
+        Handle(Geom2d_BSplineCurve) H2;
+
+        if(myApprox1) {
+          H1 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_True);
+        }
+        
+        if(myApprox2) {
+          H2 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_False);
+        }
+        //          
+        mySeqOfCurve.Append(IntTools_Curve(MakeBSpline(WL,1,WL->NbPnts()), H1, H2));
       }
     } // if (!myApprox)
 
@@ -1707,105 +1700,105 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       GeomInt_WLApprox theapp3d;
       // 
       Standard_Real tol2d = myTolApprox;
-      //       
+      //         
       theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_True);
       
       aNbParts=myLConstruct.NbParts();
       for (i=1; i<=aNbParts; i++) {
-       myLConstruct.Part(i, fprm, lprm);
-       Handle(IntPatch_WLine) WL = 
-         convert.MakeWLine(Handle(IntPatch_ALine):: DownCast(L),fprm,lprm);
-
-       theapp3d.Perform(myHS1,myHS2,WL,Standard_True,myApprox1,myApprox2, 1, WL->NbPnts());
-
-       if (!theapp3d.IsDone()) {
-         //
-         Handle(Geom2d_BSplineCurve) H1;
-         Handle(Geom2d_BSplineCurve) H2;
-
-         if(myApprox1) {
-           H1 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_True);
-         }
-         
-         if(myApprox2) {
-           H2 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_False);
-         }
-         //     
-         mySeqOfCurve.Append(IntTools_Curve(MakeBSpline(WL,1,WL->NbPnts()), H1, H2));
-       }
-
-       else {
-         if(myApprox1 || myApprox2) { 
-           if( theapp3d.TolReached2d()>myTolReached2d || myTolReached2d==0) { 
-             myTolReached2d = theapp3d.TolReached2d();
-           }
-         }
-         
-         if( theapp3d.TolReached3d()>myTolReached3d || myTolReached3d==0) { 
-           myTolReached3d = theapp3d.TolReached3d();
-         }
-
-         Standard_Integer aNbMultiCurves, nbpoles;
-         aNbMultiCurves=theapp3d.NbMultiCurves();
-         for (j=1; j<=aNbMultiCurves; j++) {
-           const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
-           nbpoles = mbspc.NbPoles();
-           
-           TColgp_Array1OfPnt tpoles(1, nbpoles);
-           mbspc.Curve(1, tpoles);
-           Handle(Geom_BSplineCurve) BS=new Geom_BSplineCurve(tpoles,
-                                                              mbspc.Knots(),
-                                                              mbspc.Multiplicities(),
-                                                              mbspc.Degree());
-           
-           GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
-           Check.FixTangent(Standard_True,Standard_True);
-           // 
-           IntTools_Curve aCurve;
-           aCurve.SetCurve(BS);
-           
-           if(myApprox1) { 
-             TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
-             mbspc.Curve(2,tpoles2d);
-             Handle(Geom2d_BSplineCurve) BS2=new Geom2d_BSplineCurve(tpoles2d,
-                                                                     mbspc.Knots(),
-                                                                     mbspc.Multiplicities(),
-                                                                     mbspc.Degree());
-
-             GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
-             newCheck.FixTangent(Standard_True,Standard_True);
-             //                
-             aCurve.SetFirstCurve2d(BS2);
-           }
-           else {
-             Handle(Geom2d_BSplineCurve) H1;
-             aCurve.SetFirstCurve2d(H1);
-           }
-           
-           if(myApprox2) { 
-             TColgp_Array1OfPnt2d tpoles2d(1, nbpoles);
-             Standard_Integer TwoOrThree;
-             TwoOrThree=myApprox1 ? 3 : 2;
-             mbspc.Curve(TwoOrThree, tpoles2d);
-             Handle(Geom2d_BSplineCurve) BS2 =new Geom2d_BSplineCurve(tpoles2d,
-                                                                      mbspc.Knots(),
-                                                                      mbspc.Multiplicities(),
-                                                                      mbspc.Degree());
-               
-             GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
-             newCheck.FixTangent(Standard_True,Standard_True);
-             //        
-             aCurve.SetSecondCurve2d(BS2);
-           }
-           else { 
-             Handle(Geom2d_BSplineCurve) H2;
-             aCurve.SetSecondCurve2d(H2);
-           }
-           // 
-           mySeqOfCurve.Append(aCurve);
-
-         }// for (j=1; j<=aNbMultiCurves; j++) {
-       }// else from if (!theapp3d.IsDone())
+        myLConstruct.Part(i, fprm, lprm);
+        Handle(IntPatch_WLine) WL = 
+          convert.MakeWLine(Handle(IntPatch_ALine):: DownCast(L),fprm,lprm);
+
+        theapp3d.Perform(myHS1,myHS2,WL,Standard_True,myApprox1,myApprox2, 1, WL->NbPnts());
+
+        if (!theapp3d.IsDone()) {
+          //
+          Handle(Geom2d_BSplineCurve) H1;
+          Handle(Geom2d_BSplineCurve) H2;
+
+          if(myApprox1) {
+            H1 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_True);
+          }
+          
+          if(myApprox2) {
+            H2 = MakeBSpline2d(WL, 1, WL->NbPnts(), Standard_False);
+          }
+          //          
+          mySeqOfCurve.Append(IntTools_Curve(MakeBSpline(WL,1,WL->NbPnts()), H1, H2));
+        }
+
+        else {
+          if(myApprox1 || myApprox2) { 
+            if( theapp3d.TolReached2d()>myTolReached2d || myTolReached2d==0) { 
+              myTolReached2d = theapp3d.TolReached2d();
+            }
+          }
+          
+          if( theapp3d.TolReached3d()>myTolReached3d || myTolReached3d==0) { 
+            myTolReached3d = theapp3d.TolReached3d();
+          }
+
+          Standard_Integer aNbMultiCurves, nbpoles;
+          aNbMultiCurves=theapp3d.NbMultiCurves();
+          for (j=1; j<=aNbMultiCurves; j++) {
+            const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
+            nbpoles = mbspc.NbPoles();
+            
+            TColgp_Array1OfPnt tpoles(1, nbpoles);
+            mbspc.Curve(1, tpoles);
+            Handle(Geom_BSplineCurve) BS=new Geom_BSplineCurve(tpoles,
+                                                               mbspc.Knots(),
+                                                               mbspc.Multiplicities(),
+                                                               mbspc.Degree());
+            
+            GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
+            Check.FixTangent(Standard_True,Standard_True);
+            // 
+            IntTools_Curve aCurve;
+            aCurve.SetCurve(BS);
+            
+            if(myApprox1) { 
+              TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
+              mbspc.Curve(2,tpoles2d);
+              Handle(Geom2d_BSplineCurve) BS2=new Geom2d_BSplineCurve(tpoles2d,
+                                                                      mbspc.Knots(),
+                                                                      mbspc.Multiplicities(),
+                                                                      mbspc.Degree());
+
+              GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
+              newCheck.FixTangent(Standard_True,Standard_True);
+              //                 
+              aCurve.SetFirstCurve2d(BS2);
+            }
+            else {
+              Handle(Geom2d_BSplineCurve) H1;
+              aCurve.SetFirstCurve2d(H1);
+            }
+            
+            if(myApprox2) { 
+              TColgp_Array1OfPnt2d tpoles2d(1, nbpoles);
+              Standard_Integer TwoOrThree;
+              TwoOrThree=myApprox1 ? 3 : 2;
+              mbspc.Curve(TwoOrThree, tpoles2d);
+              Handle(Geom2d_BSplineCurve) BS2 =new Geom2d_BSplineCurve(tpoles2d,
+                                                                       mbspc.Knots(),
+                                                                       mbspc.Multiplicities(),
+                                                                       mbspc.Degree());
+                
+              GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
+              newCheck.FixTangent(Standard_True,Standard_True);
+              //         
+              aCurve.SetSecondCurve2d(BS2);
+            }
+            else { 
+              Handle(Geom2d_BSplineCurve) H2;
+              aCurve.SetSecondCurve2d(H2);
+            }
+            // 
+            mySeqOfCurve.Append(aCurve);
+
+          }// for (j=1; j<=aNbMultiCurves; j++) {
+        }// else from if (!theapp3d.IsDone())
       }// for (i=1; i<=aNbParts; i++) {
     }// else { // myApprox=TRUE
   }// case IntPatch_Analytic:
@@ -1820,33 +1813,33 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
     if (!myApprox) {
       aNbParts = 1;
       if(!bAvoidLineConstructor){
-       aNbParts=myLConstruct.NbParts();
+        aNbParts=myLConstruct.NbParts();
       }
       for (i=1; i<=aNbParts; ++i) {
-       Handle(Geom2d_BSplineCurve) H1, H2;
-       Handle(Geom_Curve) aBSp;
-       //
-       if(bAvoidLineConstructor) {
-         ifprm = 1;
-         ilprm = WL->NbPnts();
-       }
-       else {
-         myLConstruct.Part(i, fprm, lprm);
-         ifprm=(Standard_Integer)fprm;
-         ilprm=(Standard_Integer)lprm;
-       }
-       //
-       if(myApprox1) {
-         H1 = MakeBSpline2d(WL, ifprm, ilprm, Standard_True);
-       }
-       //
-       if(myApprox2) {
-         H2 = MakeBSpline2d(WL, ifprm, ilprm, Standard_False);
-       }
-       //        
-       aBSp=MakeBSpline(WL, ifprm, ilprm);
-       IntTools_Curve aIC(aBSp, H1, H2);
-       mySeqOfCurve.Append(aIC);
+        Handle(Geom2d_BSplineCurve) H1, H2;
+        Handle(Geom_Curve) aBSp;
+        //
+        if(bAvoidLineConstructor) {
+          ifprm = 1;
+          ilprm = WL->NbPnts();
+        }
+        else {
+          myLConstruct.Part(i, fprm, lprm);
+          ifprm=(Standard_Integer)fprm;
+          ilprm=(Standard_Integer)lprm;
+        }
+        //
+        if(myApprox1) {
+          H1 = MakeBSpline2d(WL, ifprm, ilprm, Standard_True);
+        }
+        //
+        if(myApprox2) {
+          H2 = MakeBSpline2d(WL, ifprm, ilprm, Standard_False);
+        }
+        //           
+        aBSp=MakeBSpline(WL, ifprm, ilprm);
+        IntTools_Curve aIC(aBSp, H1, H2);
+        mySeqOfCurve.Append(aIC);
       }// for (i=1; i<=aNbParts; ++i) {
     }// if (!myApprox) {
     //
@@ -1870,346 +1863,346 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
       Standard_Boolean anWithPC = Standard_True;
 
       if(typs1 == GeomAbs_Cylinder && typs2 == GeomAbs_Sphere) {
-       anWithPC = 
-         ApproxWithPCurves(myHS1->Surface().Cylinder(), myHS2->Surface().Sphere());
+        anWithPC = 
+          ApproxWithPCurves(myHS1->Surface().Cylinder(), myHS2->Surface().Sphere());
       }
       else if (typs1 == GeomAbs_Sphere && typs2 == GeomAbs_Cylinder) {
-       anWithPC = 
-         ApproxWithPCurves(myHS2->Surface().Cylinder(), myHS1->Surface().Sphere());
+        anWithPC = 
+          ApproxWithPCurves(myHS2->Surface().Cylinder(), myHS1->Surface().Sphere());
       }
       //
       if(!anWithPC) {
         myTolApprox = aTolApproxImp;//1.e-5; 
-       anApprox1 = Standard_False;
-       anApprox2 = Standard_False;
-       //      
-       tol2d = myTolApprox;
+        anApprox1 = Standard_False;
+        anApprox2 = Standard_False;
+        //         
+        tol2d = myTolApprox;
       }
-       
+        
       if(myHS1 == myHS2) { 
-       theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_False, aParType);
-       rejectSurface = Standard_True;
+        theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_False, aParType);
+        rejectSurface = Standard_True;
       }
       else { 
-       if(reApprox && !rejectSurface)
-         theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_False, aParType);
-       else {
-         Standard_Integer iDegMax, iDegMin, iNbIter;
-         //
-         ApproxParameters(myHS1, myHS2, iDegMin, iDegMax, iNbIter);
-         theapp3d.SetParameters(myTolApprox, tol2d, iDegMin, iDegMax, iNbIter, Standard_True, aParType);
-       }
+        if(reApprox && !rejectSurface)
+          theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_False, aParType);
+        else {
+          Standard_Integer iDegMax, iDegMin, iNbIter;
+          //
+          ApproxParameters(myHS1, myHS2, iDegMin, iDegMax, iNbIter);
+          theapp3d.SetParameters(myTolApprox, tol2d, iDegMin, iDegMax, iNbIter, Standard_True, aParType);
+        }
       }
       //
       Standard_Real aReachedTol = Precision::Confusion();
       bIsDecomposited=DecompositionOfWLine(WL,
-                                          myHS1, 
-                                          myHS2, 
-                                          myFace1, 
-                                          myFace2, 
-                                          myLConstruct, 
-                                          bAvoidLineConstructor, 
-                                          aSeqOfL, 
-                                          aReachedTol,
-                                          myContext);
+                                           myHS1, 
+                                           myHS2, 
+                                           myFace1, 
+                                           myFace2, 
+                                           myLConstruct, 
+                                           bAvoidLineConstructor, 
+                                           aSeqOfL, 
+                                           aReachedTol,
+                                           myContext);
       if ( bIsDecomposited && ( myTolReached3d < aReachedTol ) ) {
-       myTolReached3d = aReachedTol;
+        myTolReached3d = aReachedTol;
       }
       //
       aNbSeqOfL=aSeqOfL.Length();
       //
       if (bIsDecomposited) {
-       nbiter=aNbSeqOfL;
+        nbiter=aNbSeqOfL;
       }
       else {
-       nbiter=1;
-       aNbParts=1;
-       if (!bAvoidLineConstructor) {
-         aNbParts=myLConstruct.NbParts();
-         nbiter=aNbParts;
-       }
+        nbiter=1;
+        aNbParts=1;
+        if (!bAvoidLineConstructor) {
+          aNbParts=myLConstruct.NbParts();
+          nbiter=aNbParts;
+        }
       }
       //
       for(i = 1; i <= nbiter; ++i) {
-       if(bIsDecomposited) {
-         WL = Handle(IntPatch_WLine)::DownCast(aSeqOfL.Value(i));
-         ifprm = 1;
-         ilprm = WL->NbPnts();
-       }
-       else {
-         if(bAvoidLineConstructor) {
-           ifprm = 1;
-           ilprm = WL->NbPnts();
-         }
-         else {
-           myLConstruct.Part(i, fprm, lprm);
-           ifprm = (Standard_Integer)fprm;
-           ilprm = (Standard_Integer)lprm;
-         }
-       }
-       //-- lbr : 
-       //-- Si une des surfaces est un plan , on approxime en 2d
-       //-- sur cette surface et on remonte les points 2d en 3d.
-       if(typs1 == GeomAbs_Plane) { 
-         theapp3d.Perform(myHS1, myHS2, WL, Standard_False,Standard_True, myApprox2,ifprm,ilprm);
-       }         
-       else if(typs2 == GeomAbs_Plane) { 
-         theapp3d.Perform(myHS1,myHS2,WL,Standard_False,myApprox1,Standard_True,ifprm,ilprm);
-       }
-       else { 
-         //
-         if (myHS1 != myHS2){
-           if ((typs1==GeomAbs_BezierSurface || typs1==GeomAbs_BSplineSurface) &&
-               (typs2==GeomAbs_BezierSurface || typs2==GeomAbs_BSplineSurface)) {
-            
-             theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_True, aParType);
-             
-             Standard_Boolean bUseSurfaces;
-             bUseSurfaces=NotUseSurfacesForApprox(myFace1, myFace2, WL, ifprm,  ilprm);
-             if (bUseSurfaces) {
-               // ######
-               rejectSurface = Standard_True;
-               // ######
-               theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_False, aParType);
-             }
-           }
-         }
-         //
-         theapp3d.Perform(myHS1,myHS2,WL,Standard_True,anApprox1,anApprox2,ifprm,ilprm);
-       }
-         //      
+        if(bIsDecomposited) {
+          WL = Handle(IntPatch_WLine)::DownCast(aSeqOfL.Value(i));
+          ifprm = 1;
+          ilprm = WL->NbPnts();
+        }
+        else {
+          if(bAvoidLineConstructor) {
+            ifprm = 1;
+            ilprm = WL->NbPnts();
+          }
+          else {
+            myLConstruct.Part(i, fprm, lprm);
+            ifprm = (Standard_Integer)fprm;
+            ilprm = (Standard_Integer)lprm;
+          }
+        }
+        //-- lbr : 
+        //-- Si une des surfaces est un plan , on approxime en 2d
+        //-- sur cette surface et on remonte les points 2d en 3d.
+        if(typs1 == GeomAbs_Plane) { 
+          theapp3d.Perform(myHS1, myHS2, WL, Standard_False,Standard_True, myApprox2,ifprm,ilprm);
+        }          
+        else if(typs2 == GeomAbs_Plane) { 
+          theapp3d.Perform(myHS1,myHS2,WL,Standard_False,myApprox1,Standard_True,ifprm,ilprm);
+        }
+        else { 
+          //
+          if (myHS1 != myHS2){
+            if ((typs1==GeomAbs_BezierSurface || typs1==GeomAbs_BSplineSurface) &&
+                (typs2==GeomAbs_BezierSurface || typs2==GeomAbs_BSplineSurface)) {
+             
+              theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_True, aParType);
+              
+              Standard_Boolean bUseSurfaces;
+              bUseSurfaces=NotUseSurfacesForApprox(myFace1, myFace2, WL, ifprm,  ilprm);
+              if (bUseSurfaces) {
+                // ######
+                rejectSurface = Standard_True;
+                // ######
+                theapp3d.SetParameters(myTolApprox, tol2d, 4, 8, 0, Standard_False, aParType);
+              }
+            }
+          }
+          //
+          theapp3d.Perform(myHS1,myHS2,WL,Standard_True,anApprox1,anApprox2,ifprm,ilprm);
+        }
+          //           
         if (!theapp3d.IsDone()) {
-         Handle(Geom2d_BSplineCurve) H1;
+          Handle(Geom2d_BSplineCurve) H1;
           Handle(Geom2d_BSplineCurve) H2;
-         //      
-         Handle(Geom_Curve) aBSp=MakeBSpline(WL,ifprm, ilprm);
+          //           
+          Handle(Geom_Curve) aBSp=MakeBSpline(WL,ifprm, ilprm);
           //
-         if(myApprox1) {
-           H1 = MakeBSpline2d(WL, ifprm, ilprm, Standard_True);
-         }
+          if(myApprox1) {
+            H1 = MakeBSpline2d(WL, ifprm, ilprm, Standard_True);
+          }
           //
-         if(myApprox2) {
-           H2 = MakeBSpline2d(WL, ifprm, ilprm, Standard_False);
-         }
-         //      
-         IntTools_Curve aIC(aBSp, H1, H2);
-         mySeqOfCurve.Append(aIC);
-       }
-       
-       else {
-         if(myApprox1 || myApprox2 || (typs1==GeomAbs_Plane || typs2==GeomAbs_Plane)) { 
-           if( theapp3d.TolReached2d()>myTolReached2d || myTolReached2d==0.) { 
-             myTolReached2d = theapp3d.TolReached2d();
-           }
-         }
-         if(typs1==GeomAbs_Plane || typs2==GeomAbs_Plane) { 
-           myTolReached3d = myTolReached2d;
-           //
-           if (typs1==GeomAbs_Torus || typs2==GeomAbs_Torus) {
-             if (myTolReached3d<1.e-6) {
-               myTolReached3d = theapp3d.TolReached3d();
-               myTolReached3d=1.e-6;
-             }
-           }
-         }
-         else  if( theapp3d.TolReached3d()>myTolReached3d || myTolReached3d==0.) { 
-           myTolReached3d = theapp3d.TolReached3d();
-         }
-         
-         Standard_Integer aNbMultiCurves, nbpoles;
-         aNbMultiCurves=theapp3d.NbMultiCurves(); 
-         for (j=1; j<=aNbMultiCurves; j++) {
-           if(typs1 == GeomAbs_Plane) {
-             const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
-             nbpoles = mbspc.NbPoles();
-             
-             TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
-             TColgp_Array1OfPnt   tpoles(1,nbpoles);
-             
-             mbspc.Curve(1,tpoles2d);
-             const gp_Pln&  Pln = myHS1->Surface().Plane();
-             //
-             Standard_Integer ik; 
-             for(ik = 1; ik<= nbpoles; ik++) { 
-               tpoles.SetValue(ik,
-                               ElSLib::Value(tpoles2d.Value(ik).X(),
-                                             tpoles2d.Value(ik).Y(),
-                                             Pln));
-             }
-             //
-             Handle(Geom_BSplineCurve) BS = 
-               new Geom_BSplineCurve(tpoles,
-                                     mbspc.Knots(),
-                                     mbspc.Multiplicities(),
-                                     mbspc.Degree());
-             GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
-             Check.FixTangent(Standard_True, Standard_True);
-             //        
-             IntTools_Curve aCurve;
-             aCurve.SetCurve(BS);
-
-             if(myApprox1) { 
-               Handle(Geom2d_BSplineCurve) BS1 = 
-                 new Geom2d_BSplineCurve(tpoles2d,
-                                         mbspc.Knots(),
-                                         mbspc.Multiplicities(),
-                                         mbspc.Degree());
-               GeomLib_Check2dBSplineCurve Check1(BS1,TOLCHECK,TOLANGCHECK);
-               Check1.FixTangent(Standard_True,Standard_True);
-               //
-               // ############################################
-               if(!rejectSurface && !reApprox) {
-                 Standard_Boolean isValid = IsCurveValid(BS1);
-                 if(!isValid) {
-                   reApprox = Standard_True;
-                   goto reapprox;
-                 }
-               }
-               // ############################################
-               aCurve.SetFirstCurve2d(BS1);
-             }
-             else {
-               Handle(Geom2d_BSplineCurve) H1;
-               aCurve.SetFirstCurve2d(H1);
-             }
-
-             if(myApprox2) { 
-               mbspc.Curve(2, tpoles2d);
-               
-               Handle(Geom2d_BSplineCurve) BS2 = new Geom2d_BSplineCurve(tpoles2d,
-                                                                         mbspc.Knots(),
-                                                                         mbspc.Multiplicities(),
-                                                                         mbspc.Degree());
-               GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
-               newCheck.FixTangent(Standard_True,Standard_True);
-               
-               // ###########################################
-               if(!rejectSurface && !reApprox) {
-                 Standard_Boolean isValid = IsCurveValid(BS2);
-                 if(!isValid) {
-                   reApprox = Standard_True;
-                   goto reapprox;
-                 }
-               }
-               // ###########################################
-               // 
-               aCurve.SetSecondCurve2d(BS2);
-             }
-             else { 
-               Handle(Geom2d_BSplineCurve) H2;
-               //              
-                 aCurve.SetSecondCurve2d(H2);
-             }
-             //
-             mySeqOfCurve.Append(aCurve);
+          if(myApprox2) {
+            H2 = MakeBSpline2d(WL, ifprm, ilprm, Standard_False);
+          }
+          //           
+          IntTools_Curve aIC(aBSp, H1, H2);
+          mySeqOfCurve.Append(aIC);
+        }
+        
+        else {
+          if(myApprox1 || myApprox2 || (typs1==GeomAbs_Plane || typs2==GeomAbs_Plane)) { 
+            if( theapp3d.TolReached2d()>myTolReached2d || myTolReached2d==0.) { 
+              myTolReached2d = theapp3d.TolReached2d();
+            }
+          }
+          if(typs1==GeomAbs_Plane || typs2==GeomAbs_Plane) { 
+            myTolReached3d = myTolReached2d;
+            //
+            if (typs1==GeomAbs_Torus || typs2==GeomAbs_Torus) {
+              if (myTolReached3d<1.e-6) {
+                myTolReached3d = theapp3d.TolReached3d();
+                myTolReached3d=1.e-6;
+              }
+            }
+          }
+          else  if( theapp3d.TolReached3d()>myTolReached3d || myTolReached3d==0.) { 
+            myTolReached3d = theapp3d.TolReached3d();
+          }
+          
+          Standard_Integer aNbMultiCurves, nbpoles;
+          aNbMultiCurves=theapp3d.NbMultiCurves(); 
+          for (j=1; j<=aNbMultiCurves; j++) {
+            if(typs1 == GeomAbs_Plane) {
+              const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
+              nbpoles = mbspc.NbPoles();
+              
+              TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
+              TColgp_Array1OfPnt   tpoles(1,nbpoles);
+              
+              mbspc.Curve(1,tpoles2d);
+              const gp_Pln&  Pln = myHS1->Surface().Plane();
+              //
+              Standard_Integer ik; 
+              for(ik = 1; ik<= nbpoles; ik++) { 
+                tpoles.SetValue(ik,
+                                ElSLib::Value(tpoles2d.Value(ik).X(),
+                                              tpoles2d.Value(ik).Y(),
+                                              Pln));
+              }
+              //
+              Handle(Geom_BSplineCurve) BS = 
+                new Geom_BSplineCurve(tpoles,
+                                      mbspc.Knots(),
+                                      mbspc.Multiplicities(),
+                                      mbspc.Degree());
+              GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
+              Check.FixTangent(Standard_True, Standard_True);
+              //         
+              IntTools_Curve aCurve;
+              aCurve.SetCurve(BS);
+
+              if(myApprox1) { 
+                Handle(Geom2d_BSplineCurve) BS1 = 
+                  new Geom2d_BSplineCurve(tpoles2d,
+                                          mbspc.Knots(),
+                                          mbspc.Multiplicities(),
+                                          mbspc.Degree());
+                GeomLib_Check2dBSplineCurve Check1(BS1,TOLCHECK,TOLANGCHECK);
+                Check1.FixTangent(Standard_True,Standard_True);
+                //
+                // ############################################
+                if(!rejectSurface && !reApprox) {
+                  Standard_Boolean isValid = IsCurveValid(BS1);
+                  if(!isValid) {
+                    reApprox = Standard_True;
+                    goto reapprox;
+                  }
+                }
+                // ############################################
+                aCurve.SetFirstCurve2d(BS1);
+              }
+              else {
+                Handle(Geom2d_BSplineCurve) H1;
+                aCurve.SetFirstCurve2d(H1);
+              }
+
+              if(myApprox2) { 
+                mbspc.Curve(2, tpoles2d);
+                
+                Handle(Geom2d_BSplineCurve) BS2 = new Geom2d_BSplineCurve(tpoles2d,
+                                                                          mbspc.Knots(),
+                                                                          mbspc.Multiplicities(),
+                                                                          mbspc.Degree());
+                GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
+                newCheck.FixTangent(Standard_True,Standard_True);
+                
+                // ###########################################
+                if(!rejectSurface && !reApprox) {
+                  Standard_Boolean isValid = IsCurveValid(BS2);
+                  if(!isValid) {
+                    reApprox = Standard_True;
+                    goto reapprox;
+                  }
+                }
+                // ###########################################
+                // 
+                aCurve.SetSecondCurve2d(BS2);
+              }
+              else { 
+                Handle(Geom2d_BSplineCurve) H2;
+                //                 
+                  aCurve.SetSecondCurve2d(H2);
+              }
+              //
+              mySeqOfCurve.Append(aCurve);
             }//if(typs1 == GeomAbs_Plane) {
-           
-           else if(typs2 == GeomAbs_Plane) { 
-             const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
-             nbpoles = mbspc.NbPoles();
-             
-             TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
-             TColgp_Array1OfPnt   tpoles(1,nbpoles);
-             mbspc.Curve((myApprox1==Standard_True)? 2 : 1,tpoles2d);
-             const gp_Pln&  Pln = myHS2->Surface().Plane();
-             //
-             Standard_Integer ik; 
-             for(ik = 1; ik<= nbpoles; ik++) { 
-               tpoles.SetValue(ik,
-                               ElSLib::Value(tpoles2d.Value(ik).X(),
-                                             tpoles2d.Value(ik).Y(),
-                                             Pln));
-               
-             }
-             //
-             Handle(Geom_BSplineCurve) BS=new Geom_BSplineCurve(tpoles,
-                                                                mbspc.Knots(),
-                                                                mbspc.Multiplicities(),
-                                                                mbspc.Degree());
-             GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
-             Check.FixTangent(Standard_True,Standard_True);
-             //        
-             IntTools_Curve aCurve;
-             aCurve.SetCurve(BS);
-
-             if(myApprox2) {
-               Handle(Geom2d_BSplineCurve) BS1=new Geom2d_BSplineCurve(tpoles2d,
-                                                                       mbspc.Knots(),
-                                                                       mbspc.Multiplicities(),
-                                                                       mbspc.Degree());
-               GeomLib_Check2dBSplineCurve Check1(BS1,TOLCHECK,TOLANGCHECK);
-               Check1.FixTangent(Standard_True,Standard_True);
-               //      
-               // ###########################################
-               if(!rejectSurface && !reApprox) {
-                 Standard_Boolean isValid = IsCurveValid(BS1);
-                 if(!isValid) {
-                   reApprox = Standard_True;
-                   goto reapprox;
-                 }
-               }
+            
+            else if(typs2 == GeomAbs_Plane) { 
+              const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
+              nbpoles = mbspc.NbPoles();
+              
+              TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
+              TColgp_Array1OfPnt   tpoles(1,nbpoles);
+              mbspc.Curve((myApprox1==Standard_True)? 2 : 1,tpoles2d);
+              const gp_Pln&  Pln = myHS2->Surface().Plane();
+              //
+              Standard_Integer ik; 
+              for(ik = 1; ik<= nbpoles; ik++) { 
+                tpoles.SetValue(ik,
+                                ElSLib::Value(tpoles2d.Value(ik).X(),
+                                              tpoles2d.Value(ik).Y(),
+                                              Pln));
+                
+              }
+              //
+              Handle(Geom_BSplineCurve) BS=new Geom_BSplineCurve(tpoles,
+                                                                 mbspc.Knots(),
+                                                                 mbspc.Multiplicities(),
+                                                                 mbspc.Degree());
+              GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
+              Check.FixTangent(Standard_True,Standard_True);
+              //         
+              IntTools_Curve aCurve;
+              aCurve.SetCurve(BS);
+
+              if(myApprox2) {
+                Handle(Geom2d_BSplineCurve) BS1=new Geom2d_BSplineCurve(tpoles2d,
+                                                                        mbspc.Knots(),
+                                                                        mbspc.Multiplicities(),
+                                                                        mbspc.Degree());
+                GeomLib_Check2dBSplineCurve Check1(BS1,TOLCHECK,TOLANGCHECK);
+                Check1.FixTangent(Standard_True,Standard_True);
+                //         
+                // ###########################################
+                if(!rejectSurface && !reApprox) {
+                  Standard_Boolean isValid = IsCurveValid(BS1);
+                  if(!isValid) {
+                    reApprox = Standard_True;
+                    goto reapprox;
+                  }
+                }
                 // ###########################################
                 bPCurvesOk = CheckPCurve(BS1, myFace2);
-               aCurve.SetSecondCurve2d(BS1);
-             }
-             else {
-               Handle(Geom2d_BSplineCurve) H2;
-               aCurve.SetSecondCurve2d(H2);
-             }
-             
-             if(myApprox1) { 
-               mbspc.Curve(1,tpoles2d);
-               Handle(Geom2d_BSplineCurve) BS2=new Geom2d_BSplineCurve(tpoles2d,
-                                                                       mbspc.Knots(),
-                                                                       mbspc.Multiplicities(),
-                                                                       mbspc.Degree());
-               GeomLib_Check2dBSplineCurve Check2(BS2,TOLCHECK,TOLANGCHECK);
-               Check2.FixTangent(Standard_True,Standard_True);
-               //
-               // ###########################################
-               if(!rejectSurface && !reApprox) {
-                 Standard_Boolean isValid = IsCurveValid(BS2);
-                 if(!isValid) {
-                   reApprox = Standard_True;
-                   goto reapprox;
-                 }
-               }
+                aCurve.SetSecondCurve2d(BS1);
+              }
+              else {
+                Handle(Geom2d_BSplineCurve) H2;
+                aCurve.SetSecondCurve2d(H2);
+              }
+              
+              if(myApprox1) { 
+                mbspc.Curve(1,tpoles2d);
+                Handle(Geom2d_BSplineCurve) BS2=new Geom2d_BSplineCurve(tpoles2d,
+                                                                        mbspc.Knots(),
+                                                                        mbspc.Multiplicities(),
+                                                                        mbspc.Degree());
+                GeomLib_Check2dBSplineCurve Check2(BS2,TOLCHECK,TOLANGCHECK);
+                Check2.FixTangent(Standard_True,Standard_True);
+                //
+                // ###########################################
+                if(!rejectSurface && !reApprox) {
+                  Standard_Boolean isValid = IsCurveValid(BS2);
+                  if(!isValid) {
+                    reApprox = Standard_True;
+                    goto reapprox;
+                  }
+                }
                 // ###########################################
                 bPCurvesOk = bPCurvesOk && CheckPCurve(BS2, myFace1);
-               aCurve.SetFirstCurve2d(BS2);
-             }
-             else { 
-               Handle(Geom2d_BSplineCurve) H1;
-               //              
-               aCurve.SetFirstCurve2d(H1);
-             }
-             //
+                aCurve.SetFirstCurve2d(BS2);
+              }
+              else { 
+                Handle(Geom2d_BSplineCurve) H1;
+                //                 
+                aCurve.SetFirstCurve2d(H1);
+              }
+              //
               //if points of the pcurves are out of the faces bounds
               //create 3d and 2d curves without approximation
               if (!bPCurvesOk) {
                 Handle(Geom2d_BSplineCurve) H1, H2;
-               bPCurvesOk = Standard_True;
-                //       
+                bPCurvesOk = Standard_True;
+                //           
                 Handle(Geom_Curve) aBSp=MakeBSpline(WL,ifprm, ilprm);
                 
                 if(myApprox1) {
                   H1 = MakeBSpline2d(WL, ifprm, ilprm, Standard_True);
-                 bPCurvesOk = CheckPCurve(H1, myFace1);
+                  bPCurvesOk = CheckPCurve(H1, myFace1);
                 }
                 
                 if(myApprox2) {
                   H2 = MakeBSpline2d(WL, ifprm, ilprm, Standard_False);
-                 bPCurvesOk = bPCurvesOk && CheckPCurve(H2, myFace2);
+                  bPCurvesOk = bPCurvesOk && CheckPCurve(H2, myFace2);
                 }
                 //
-               //if pcurves created without approximation are out of the 
-               //faces bounds, use approximated 3d and 2d curves
-               if (bPCurvesOk) {
-                 IntTools_Curve aIC(aBSp, H1, H2);
-                 mySeqOfCurve.Append(aIC);
-               } else {
-                 mySeqOfCurve.Append(aCurve);
-               }
+                //if pcurves created without approximation are out of the 
+                //faces bounds, use approximated 3d and 2d curves
+                if (bPCurvesOk) {
+                  IntTools_Curve aIC(aBSp, H1, H2);
+                  mySeqOfCurve.Append(aIC);
+                } else {
+                  mySeqOfCurve.Append(aCurve);
+                }
               } else {
                 mySeqOfCurve.Append(aCurve);
               }
@@ -2224,93 +2217,93 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
               bIsValid1=Standard_True;
               bIsValid2=Standard_True;
               //
-             const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
-             nbpoles = mbspc.NbPoles();
-             TColgp_Array1OfPnt tpoles(1,nbpoles);
-             mbspc.Curve(1,tpoles);
+              const AppParCurves_MultiBSpCurve& mbspc = theapp3d.Value(j);
+              nbpoles = mbspc.NbPoles();
+              TColgp_Array1OfPnt tpoles(1,nbpoles);
+              mbspc.Curve(1,tpoles);
               BS=new Geom_BSplineCurve(tpoles,
-                                                                mbspc.Knots(),
-                                                                mbspc.Multiplicities(),
-                                                                mbspc.Degree());
-             GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
-             Check.FixTangent(Standard_True,Standard_True);
-             //                
-             aCurve.SetCurve(BS);
+                                                                 mbspc.Knots(),
+                                                                 mbspc.Multiplicities(),
+                                                                 mbspc.Degree());
+              GeomLib_CheckBSplineCurve Check(BS,TOLCHECK,TOLANGCHECK);
+              Check.FixTangent(Standard_True,Standard_True);
+              //                 
+              aCurve.SetCurve(BS);
               aCurve.SetFirstCurve2d(aH2D);
               aCurve.SetSecondCurve2d(aH2D);
               //
-             if(myApprox1) { 
-               if(anApprox1) {
+              if(myApprox1) { 
+                if(anApprox1) {
                   Handle(Geom2d_BSplineCurve) BS1;
-                 TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
-                 mbspc.Curve(2,tpoles2d);
+                  TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
+                  mbspc.Curve(2,tpoles2d);
                   //
                   BS1=new Geom2d_BSplineCurve(tpoles2d,
-                                                                       mbspc.Knots(),
-                                                                       mbspc.Multiplicities(),
-                                                                       mbspc.Degree());
-                 GeomLib_Check2dBSplineCurve newCheck(BS1,TOLCHECK,TOLANGCHECK);
-                 newCheck.FixTangent(Standard_True,Standard_True);
-                 //    
+                                                                        mbspc.Knots(),
+                                                                        mbspc.Multiplicities(),
+                                                                        mbspc.Degree());
+                  GeomLib_Check2dBSplineCurve newCheck(BS1,TOLCHECK,TOLANGCHECK);
+                  newCheck.FixTangent(Standard_True,Standard_True);
+                  //         
                   if (!reApprox) {
                     bIsValid1=CheckPCurve(BS1, myFace1);
                   }
                   //
-                 aCurve.SetFirstCurve2d(BS1);
-               }
-               else {
-                 Handle(Geom2d_BSplineCurve) BS1;
-                 fprm = BS->FirstParameter();
-                 lprm = BS->LastParameter();
-
-                 Handle(Geom2d_Curve) C2d;
-                 Standard_Real aTol = myTolApprox;
-                 BuildPCurves(fprm, lprm, aTol, myHS1->ChangeSurface().Surface(), BS, C2d);
-                 BS1 = Handle(Geom2d_BSplineCurve)::DownCast(C2d);
-                 aCurve.SetFirstCurve2d(BS1);
-               }
+                  aCurve.SetFirstCurve2d(BS1);
+                }
+                else {
+                  Handle(Geom2d_BSplineCurve) BS1;
+                  fprm = BS->FirstParameter();
+                  lprm = BS->LastParameter();
+
+                  Handle(Geom2d_Curve) C2d;
+                  Standard_Real aTol = myTolApprox;
+                  BuildPCurves(fprm, lprm, aTol, myHS1->ChangeSurface().Surface(), BS, C2d);
+                  BS1 = Handle(Geom2d_BSplineCurve)::DownCast(C2d);
+                  aCurve.SetFirstCurve2d(BS1);
+                }
               } // if(myApprox1) { 
-               //              
-             if(myApprox2) { 
-               if(anApprox2) {
+                //                 
+              if(myApprox2) { 
+                if(anApprox2) {
                   Handle(Geom2d_BSplineCurve) BS2;
-                 TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
-                 mbspc.Curve((myApprox1==Standard_True)? 3 : 2,tpoles2d);
+                  TColgp_Array1OfPnt2d tpoles2d(1,nbpoles);
+                  mbspc.Curve((myApprox1==Standard_True)? 3 : 2,tpoles2d);
                   BS2=new Geom2d_BSplineCurve(tpoles2d,
-                                                                       mbspc.Knots(),
-                                                                       mbspc.Multiplicities(),
-                                                                       mbspc.Degree());
-                 GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
-                 newCheck.FixTangent(Standard_True,Standard_True);
-               //              
+                                                                        mbspc.Knots(),
+                                                                        mbspc.Multiplicities(),
+                                                                        mbspc.Degree());
+                  GeomLib_Check2dBSplineCurve newCheck(BS2,TOLCHECK,TOLANGCHECK);
+                  newCheck.FixTangent(Standard_True,Standard_True);
+                //                 
                   if (!reApprox) {
                     bIsValid2=CheckPCurve(BS2, myFace2);        
                   }
-                 aCurve.SetSecondCurve2d(BS2);
-               }
-               else {
-                 Handle(Geom2d_BSplineCurve) BS2;
-                 fprm = BS->FirstParameter();
-                 lprm = BS->LastParameter();
-
-                 Handle(Geom2d_Curve) C2d;
-                 Standard_Real aTol = myTolApprox;
-                 BuildPCurves(fprm, lprm, aTol, myHS2->ChangeSurface().Surface(), BS, C2d);
-                 BS2 = Handle(Geom2d_BSplineCurve)::DownCast(C2d);
-                 aCurve.SetSecondCurve2d(BS2);
-               }
+                  aCurve.SetSecondCurve2d(BS2);
+                }
+                else {
+                  Handle(Geom2d_BSplineCurve) BS2;
+                  fprm = BS->FirstParameter();
+                  lprm = BS->LastParameter();
+
+                  Handle(Geom2d_Curve) C2d;
+                  Standard_Real aTol = myTolApprox;
+                  BuildPCurves(fprm, lprm, aTol, myHS2->ChangeSurface().Surface(), BS, C2d);
+                  BS2 = Handle(Geom2d_BSplineCurve)::DownCast(C2d);
+                  aCurve.SetSecondCurve2d(BS2);
+                }
               } //if(myApprox2) { 
               if (!bIsValid1 || !bIsValid2) {
                 myTolApprox=aTolApproxImp;//1.e-5;
                 tol2d = myTolApprox;
                 reApprox = Standard_True;
                 goto reapprox;
-             }
-               //              
-             mySeqOfCurve.Append(aCurve);
-           }
-         }
-       }
+              }
+                //                 
+              mySeqOfCurve.Append(aCurve);
+            }
+          }
+        }
       }
     }// else { // X
   }// case IntPatch_Walking:{
@@ -2328,107 +2321,92 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 //function : BuildPCurves
 //purpose  : 
 //=======================================================================
- void BuildPCurves (Standard_Real f,
-                   Standard_Real l,
-                   Standard_Real& Tol,
-                   const Handle (Geom_Surface)& S,
-                   const Handle (Geom_Curve)&   C,
-                   Handle (Geom2d_Curve)& C2d)
+void BuildPCurves (Standard_Real f,
+                   Standard_Real l,
+                   Standard_Real& Tol,
+                   const Handle (Geom_Surface)& S,
+                   const Handle (Geom_Curve)&   C,
+                   Handle (Geom2d_Curve)& C2d)
 {
-
+  if (!C2d.IsNull()) {
+    return;
+  }
+  //
   Standard_Real umin,umax,vmin,vmax;
   // 
-
-  if (C2d.IsNull())
-  {
-    // in class ProjLib_Function the range of parameters is shrank by 1.e-09
-    if((l - f) > 2.e-09)
-    {
+  S->Bounds(umin, umax, vmin, vmax);
+  // in class ProjLib_Function the range of parameters is shrank by 1.e-09
+  if((l - f) > 2.e-09) {
+    C2d = GeomProjLib::Curve2d(C,f,l,S,umin,umax,vmin,vmax,Tol);
+    //
+    if (C2d.IsNull()) {
+      // proj. a circle that goes through the pole on a sphere to the sphere     
+      Tol += Precision::Confusion();
       C2d = GeomProjLib::Curve2d(C,f,l,S,Tol);
-      //
-      if (C2d.IsNull()) {
-        // proj. a circle that goes through the pole on a sphere to the sphere     
-        Tol += Precision::Confusion();
-        C2d = GeomProjLib::Curve2d(C,f,l,S,Tol);
-      }
     }
-    else
-    {
-      if((l - f) > Epsilon(Abs(f)))
-      {
-        GeomAPI_ProjectPointOnSurf aProjector1, aProjector2;
-        gp_Pnt P1 = C->Value(f);
-        gp_Pnt P2 = C->Value(l);
-        aProjector1.Init(P1, S);
-        aProjector2.Init(P2, S);
-
-        if(aProjector1.IsDone() && aProjector2.IsDone()) {
-          Standard_Real U=0., V=0.;
-          aProjector1.LowerDistanceParameters(U, V);
-          gp_Pnt2d p1(U, V);
-
-          aProjector2.LowerDistanceParameters(U, V);
-          gp_Pnt2d p2(U, V);
-
-          if(p1.Distance(p2) > gp::Resolution()) {
-            TColgp_Array1OfPnt2d poles(1,2);
-            TColStd_Array1OfReal knots(1,2);
-            TColStd_Array1OfInteger mults(1,2);
-            poles(1) = p1;
-            poles(2) = p2;
-            knots(1) = f;
-            knots(2) = l;
-            mults(1) = mults(2) = 2;
-
-            C2d = new Geom2d_BSplineCurve(poles,knots,mults,1);
-
-            // compute reached tolerance.begin
-            gp_Pnt PMid = C->Value((f + l) * 0.5);
-            aProjector1.Perform(PMid);
-
-            if(aProjector1.IsDone()) {
-              aProjector1.LowerDistanceParameters(U, V);
-              gp_Pnt2d pmidproj(U, V);
-              gp_Pnt2d pmidcurve2d = C2d->Value((f + l) * 0.5);
-              Standard_Real adist = pmidcurve2d.Distance(pmidproj);
-              Tol = (adist > Tol) ? adist : Tol;
-            }
-            // compute reached tolerance.end
+  }
+  else {
+    if((l - f) > Epsilon(Abs(f))) {
+      GeomAPI_ProjectPointOnSurf aProjector1, aProjector2;
+      gp_Pnt P1 = C->Value(f);
+      gp_Pnt P2 = C->Value(l);
+      aProjector1.Init(P1, S);
+      aProjector2.Init(P2, S);
+      
+      if(aProjector1.IsDone() && aProjector2.IsDone()) {
+        Standard_Real U=0., V=0.;
+        aProjector1.LowerDistanceParameters(U, V);
+        gp_Pnt2d p1(U, V);
+        
+        aProjector2.LowerDistanceParameters(U, V);
+        gp_Pnt2d p2(U, V);
+        
+        if(p1.Distance(p2) > gp::Resolution()) {
+          TColgp_Array1OfPnt2d poles(1,2);
+          TColStd_Array1OfReal knots(1,2);
+          TColStd_Array1OfInteger mults(1,2);
+          poles(1) = p1;
+          poles(2) = p2;
+          knots(1) = f;
+          knots(2) = l;
+          mults(1) = mults(2) = 2;
+          
+          C2d = new Geom2d_BSplineCurve(poles,knots,mults,1);
+          
+          // compute reached tolerance.begin
+          gp_Pnt PMid = C->Value((f + l) * 0.5);
+          aProjector1.Perform(PMid);
+          
+          if(aProjector1.IsDone()) {
+            aProjector1.LowerDistanceParameters(U, V);
+            gp_Pnt2d pmidproj(U, V);
+            gp_Pnt2d pmidcurve2d = C2d->Value((f + l) * 0.5);
+            Standard_Real adist = pmidcurve2d.Distance(pmidproj);
+            Tol = (adist > Tol) ? adist : Tol;
           }
+          // compute reached tolerance.end
         }
       }
     }
+  }
+  //
+  if (S->IsUPeriodic() && !C2d.IsNull()) {
+    // Recadre dans le domaine UV de la face
+    Standard_Real aTm, U0, aEps, period, du, U0x;
+    Standard_Boolean bAdjust;
     //
-    S->Bounds(umin, umax, vmin, vmax);
-
-    if (S->IsUPeriodic() && !C2d.IsNull())
-    {
-      // Recadre dans le domaine UV de la face
-      Standard_Real period, U0, du, aEps; 
-
-      du =0.0;
-      aEps=Precision::PConfusion();
-      period = S->UPeriod();
-      gp_Pnt2d Pf = C2d->Value(f);
-      U0=Pf.X();
-      //
-      gp_Pnt2d Pl = C2d->Value(l);
-
-      U0 = Min(Pl.X(), U0);
-      //       while(U0-umin<aEps) { 
-      while(U0-umin<-aEps) { 
-        U0+=period;
-        du+=period;
-      }
-      //
-      while(U0-umax>aEps) { 
-        U0-=period;
-        du-=period;
-      }
-      if (du != 0) {
-        gp_Vec2d T1(du,0.);
-        C2d->Translate(T1);
-      }
+    aEps = Precision::PConfusion();
+    period = S->UPeriod();
+    //
+    aTm = .5*(f + l);
+    gp_Pnt2d pm = C2d->Value(aTm);
+    U0 = pm.X();
+    //
+    bAdjust = 
+      IntTools_Tools::AdjustPeriodic(U0, umin, umax, period, U0x, du, aEps);
+    if (bAdjust) {
+      gp_Vec2d T1(du, 0.);
+      C2d->Translate(T1);
     }
   }
 }
@@ -2438,12 +2416,12 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 //purpose  : 
 //=======================================================================
  void Parameters(const Handle(GeomAdaptor_HSurface)& HS1,
-                const Handle(GeomAdaptor_HSurface)& HS2,
-                const gp_Pnt& Ptref,
-                Standard_Real& U1,
-                Standard_Real& V1,
-                Standard_Real& U2,
-                Standard_Real& V2)
+                 const Handle(GeomAdaptor_HSurface)& HS2,
+                 const gp_Pnt& Ptref,
+                 Standard_Real& U1,
+                 Standard_Real& V1,
+                 Standard_Real& U2,
+                 Standard_Real& V2)
 {
 
   IntSurf_Quadric quad1,quad2;
@@ -2499,8 +2477,8 @@ static Standard_Boolean isTreatAnalityc(const TopoDS_Face& theF1,
 //purpose  : 
 //=======================================================================
 Handle(Geom_Curve) MakeBSpline  (const Handle(IntPatch_WLine)& WL,
-                                const Standard_Integer ideb,
-                                const Standard_Integer ifin)
+                                 const Standard_Integer ideb,
+                                 const Standard_Integer ifin)
 {
   Standard_Integer i,nbpnt = ifin-ideb+1;
   TColgp_Array1OfPnt poles(1,nbpnt);
@@ -2523,9 +2501,9 @@ Handle(Geom_Curve) MakeBSpline  (const Handle(IntPatch_WLine)& WL,
 //purpose  : 
 //=======================================================================
 Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine,
-                                         const Standard_Integer ideb,
-                                         const Standard_Integer ifin,
-                                         const Standard_Boolean onFirst)
+                                          const Standard_Integer ideb,
+                                          const Standard_Integer ifin,
+                                          const Standard_Boolean onFirst)
 {
   Standard_Integer i, nbpnt = ifin-ideb+1;
   TColgp_Array1OfPnt2d poles(1,nbpnt);
@@ -2536,9 +2514,9 @@ Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine
   for(i = 1, ipidebm1 = i+ideb-1; i <= nbpnt; ipidebm1++, i++) {
       Standard_Real U, V;
       if(onFirst)
-       theWLine->Point(ipidebm1).ParametersOnS1(U, V);
+        theWLine->Point(ipidebm1).ParametersOnS1(U, V);
       else
-       theWLine->Point(ipidebm1).ParametersOnS2(U, V);
+        theWLine->Point(ipidebm1).ParametersOnS2(U, V);
       poles(i).SetCoord(U, V);
       mults(i) = 1;
       knots(i) = i-1;
@@ -2568,13 +2546,13 @@ Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine
       //
       aNbC=IntTools_Tools::SplitCurve(aIC, aSeqCvs);
       if (aNbC) {
-       for (j=1; j<=aNbC; ++j) {
-         const IntTools_Curve& aICNew=aSeqCvs(j);
-         aNewCvs.Append(aICNew);
-       }
+        for (j=1; j<=aNbC; ++j) {
+          const IntTools_Curve& aICNew=aSeqCvs(j);
+          aNewCvs.Append(aICNew);
+        }
       }
       else {
-       aNewCvs.Append(aIC);
+        aNewCvs.Append(aIC);
       }
     }
     else {
@@ -2594,21 +2572,21 @@ Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine
       //
       aCType1=aNewCvs(1).Type();
       if (aCType1==GeomAbs_Line) {
-       IntTools_SequenceOfCurves aSeqIn, aSeqOut;
-       //
-       for (i=1; i<=aNbCurves; ++i) {
-         const IntTools_Curve& aIC=aNewCvs(i);
-         aSeqIn.Append(aIC);
-       }
-       //
-       IntTools_Tools::RejectLines(aSeqIn, aSeqOut);
-       //
-       aNewCvs.Clear();
-       aNbCurves=aSeqOut.Length(); 
-       for (i=1; i<=aNbCurves; ++i) {
-         const IntTools_Curve& aIC=aSeqOut(i);
-         aNewCvs.Append(aIC);
-       }
+        IntTools_SequenceOfCurves aSeqIn, aSeqOut;
+        //
+        for (i=1; i<=aNbCurves; ++i) {
+          const IntTools_Curve& aIC=aNewCvs(i);
+          aSeqIn.Append(aIC);
+        }
+        //
+        IntTools_Tools::RejectLines(aSeqIn, aSeqOut);
+        //
+        aNewCvs.Clear();
+        aNbCurves=aSeqOut.Length(); 
+        for (i=1; i<=aNbCurves; ++i) {
+          const IntTools_Curve& aIC=aSeqOut(i);
+          aNewCvs.Append(aIC);
+        }
       }
     }
   }// if ((aType1==GeomAbs_Plane && aType2==GeomAbs_Cone)...
@@ -2715,22 +2693,22 @@ Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine
       aXP=anAdaptorSurface.UPeriod();
       dXfact=theumax-theumin;
       if (dXfact-aTolPA>aXP) {
-       aXmid=0.5*(theumax+theumin);
-       aNbP=RealToInt(aXmid/aXP);
-       if (aXmid<0.) {
-         aNbP=aNbP-1;
-       }
-       aX1=aNbP*aXP;
-       if (theumin>aTolPA) {
-         aX1=theumin+aNbP*aXP;
-       }
-       aX2=aX1+aXP;
-       if (theumin<aX1) {
-         theumin=aX1;
-       }
-       if (theumax>aX2) {
-         theumax=aX2;
-       }
+        aXmid=0.5*(theumax+theumin);
+        aNbP=RealToInt(aXmid/aXP);
+        if (aXmid<0.) {
+          aNbP=aNbP-1;
+        }
+        aX1=aNbP*aXP;
+        if (theumin>aTolPA) {
+          aX1=theumin+aNbP*aXP;
+        }
+        aX2=aX1+aXP;
+        if (theumin<aX1) {
+          theumin=aX1;
+        }
+        if (theumax>aX2) {
+          theumax=aX2;
+        }
       }
     }
     // V
@@ -2738,22 +2716,22 @@ Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine
       aXP=anAdaptorSurface.VPeriod();
       dXfact=thevmax-thevmin;
       if (dXfact-aTolPA>aXP) {
-       aXmid=0.5*(thevmax+thevmin);
-       aNbP=RealToInt(aXmid/aXP);
-       if (aXmid<0.) {
-         aNbP=aNbP-1;
-       }
-       aX1=aNbP*aXP;
-       if (thevmin>aTolPA) {
-         aX1=thevmin+aNbP*aXP;
-       }
-       aX2=aX1+aXP;
-       if (thevmin<aX1) {
-         thevmin=aX1;
-       }
-       if (thevmax>aX2) {
-         thevmax=aX2;
-       }
+        aXmid=0.5*(thevmax+thevmin);
+        aNbP=RealToInt(aXmid/aXP);
+        if (aXmid<0.) {
+          aNbP=aNbP-1;
+        }
+        aX1=aNbP*aXP;
+        if (thevmin>aTolPA) {
+          aX1=thevmin+aNbP*aXP;
+        }
+        aX2=aX1+aXP;
+        if (thevmin<aX1) {
+          thevmin=aX1;
+        }
+        if (thevmax>aX2) {
+          thevmax=aX2;
+        }
       }
     }
   }
@@ -2767,43 +2745,43 @@ Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine
 
     for(anExp.Init(theFace, TopAbs_EDGE); anExp.More(); anExp.Next()) {
       if(BRep_Tool::IsClosed(TopoDS::Edge(anExp.Current()), theFace)) {
-       correct = Standard_True;
-       Standard_Real f, l;
-       TopoDS_Edge anEdge = TopoDS::Edge(anExp.Current());
-       
-       for(Standard_Integer i = 0; i < 2; i++) {
-         if(i==0) {
-           anEdge.Orientation(TopAbs_FORWARD);
-         }
-         else {
-           anEdge.Orientation(TopAbs_REVERSED);
-         }
-         Handle(Geom2d_Curve) aCurve = BRep_Tool::CurveOnSurface(anEdge, theFace, f, l);
-         
-         if(aCurve.IsNull()) {
-           correct = Standard_False;
-           break;
-         }
-         Handle(Geom2d_Line) aLine = Handle(Geom2d_Line)::DownCast(aCurve);
-
-         if(aLine.IsNull()) {
-           correct = Standard_False;
-           break;
-         }
-         gp_Dir2d anUDir(1., 0.);
-         gp_Dir2d aVDir(0., 1.);
-         Standard_Real anAngularTolerance = Precision::Angular();
-
-         correctU = correctU || aLine->Position().Direction().IsParallel(aVDir, anAngularTolerance);
-         correctV = correctV || aLine->Position().Direction().IsParallel(anUDir, anAngularTolerance);
-         
-         gp_Pnt2d pp1 = aCurve->Value(f);
-         aBox.Add(pp1);
-         gp_Pnt2d pp2 = aCurve->Value(l);
-         aBox.Add(pp2);
-       }
-       if(!correct)
-         break;
+        correct = Standard_True;
+        Standard_Real f, l;
+        TopoDS_Edge anEdge = TopoDS::Edge(anExp.Current());
+        
+        for(Standard_Integer i = 0; i < 2; i++) {
+          if(i==0) {
+            anEdge.Orientation(TopAbs_FORWARD);
+          }
+          else {
+            anEdge.Orientation(TopAbs_REVERSED);
+          }
+          Handle(Geom2d_Curve) aCurve = BRep_Tool::CurveOnSurface(anEdge, theFace, f, l);
+          
+          if(aCurve.IsNull()) {
+            correct = Standard_False;
+            break;
+          }
+          Handle(Geom2d_Line) aLine = Handle(Geom2d_Line)::DownCast(aCurve);
+
+          if(aLine.IsNull()) {
+            correct = Standard_False;
+            break;
+          }
+          gp_Dir2d anUDir(1., 0.);
+          gp_Dir2d aVDir(0., 1.);
+          Standard_Real anAngularTolerance = Precision::Angular();
+
+          correctU = correctU || aLine->Position().Direction().IsParallel(aVDir, anAngularTolerance);
+          correctV = correctV || aLine->Position().Direction().IsParallel(anUDir, anAngularTolerance);
+          
+          gp_Pnt2d pp1 = aCurve->Value(f);
+          aBox.Add(pp1);
+          gp_Pnt2d pp2 = aCurve->Value(l);
+          aBox.Add(pp2);
+        }
+        if(!correct)
+          break;
       }
     }
 
@@ -2812,20 +2790,20 @@ Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine
       aBox.Get(umin, vmin, umax, vmax);
 
       if(isuperiodic && correctU) {
-       
-       if(theumin < umin)
-         theumin = umin;
-       
-       if(theumax > umax) {
-         theumax = umax;
-       }
+        
+        if(theumin < umin)
+          theumin = umin;
+        
+        if(theumax > umax) {
+          theumax = umax;
+        }
       }
       if(isvperiodic && correctV) {
-       
-       if(thevmin < vmin)
-         thevmin = vmin;
-       if(thevmax > vmax)
-         thevmax = vmax;
+        
+        if(thevmin < vmin)
+          thevmin = vmin;
+        if(thevmax > vmax)
+          thevmax = vmax;
       }
     }
   }
@@ -2843,21 +2821,21 @@ Handle(Geom2d_BSplineCurve) MakeBSpline2d(const Handle(IntPatch_WLine)& theWLine
 
 static
   Standard_Boolean IsDegeneratedZone(const gp_Pnt2d& aP2d,
-                                    const Handle(Geom_Surface)& aS,
-                                    const Standard_Integer iDir);
+                                     const Handle(Geom_Surface)& aS,
+                                     const Standard_Integer iDir);
 static
   Standard_Boolean IsPointInDegeneratedZone(const IntSurf_PntOn2S& aP2S,
-                                           const TopoDS_Face& aF1,
-                                           const TopoDS_Face& aF2);
+                                            const TopoDS_Face& aF1,
+                                            const TopoDS_Face& aF2);
 //=======================================================================
 //function :  NotUseSurfacesForApprox
 //purpose  : 
 //=======================================================================
 Standard_Boolean NotUseSurfacesForApprox(const TopoDS_Face& aF1,
-                                        const TopoDS_Face& aF2,
-                                        const Handle(IntPatch_WLine)& WL,
-                                        const Standard_Integer ifprm,
-                                        const Standard_Integer ilprm)
+                                         const TopoDS_Face& aF2,
+                                         const Handle(IntPatch_WLine)& WL,
+                                         const Standard_Integer ifprm,
+                                         const Standard_Integer ilprm)
 {
   Standard_Boolean bPInDZ;
 
@@ -2879,9 +2857,9 @@ Standard_Boolean NotUseSurfacesForApprox(const TopoDS_Face& aF1,
 //purpose  : 
 //=======================================================================
 Standard_Boolean IsPointInDegeneratedZone(const IntSurf_PntOn2S& aP2S,
-                                         const TopoDS_Face& aF1,
-                                         const TopoDS_Face& aF2)
-                                         
+                                          const TopoDS_Face& aF1,
+                                          const TopoDS_Face& aF2)
+                                          
 {
   Standard_Boolean bFlag=Standard_True;
   Standard_Real US11, US12, VS11, VS12, US21, US22, VS21, VS22;
@@ -2964,8 +2942,8 @@ Standard_Boolean IsPointInDegeneratedZone(const IntSurf_PntOn2S& aP2S,
 //purpose  : 
 //=======================================================================
 Standard_Boolean IsDegeneratedZone(const gp_Pnt2d& aP2d,
-                                  const Handle(Geom_Surface)& aS,
-                                  const Standard_Integer iDir)
+                                   const Handle(Geom_Surface)& aS,
+                                   const Standard_Integer iDir)
 {
   Standard_Boolean bFlag=Standard_True;
   Standard_Real US1, US2, VS1, VS2, dY, dX, d1, d2, dD;
@@ -3076,26 +3054,26 @@ Handle(IntPatch_WLine) ComputePurgedWLine(const Handle(IntPatch_WLine)& theWLine
     while(k <= anEndIndex) {
       
       if(i != k) {
-       IntSurf_PntOn2S p1 = aLineOn2S->Value(i);
-       IntSurf_PntOn2S p2 = aLineOn2S->Value(k);
-       
-       if(p1.Value().IsEqual(p2.Value(), gp::Resolution())) {
-         aTmpWLine = aLocalWLine;
-         aLocalWLine = new IntPatch_WLine(aLineOn2S, Standard_False);
-
-         for(v = 1; v <= aTmpWLine->NbVertex(); v++) {
-           IntPatch_Point aVertex = aTmpWLine->Vertex(v);
-           Standard_Integer avertexindex = (Standard_Integer)aVertex.ParameterOnLine();
-
-           if(avertexindex >= k) {
-             aVertex.SetParameter(aVertex.ParameterOnLine() - 1.);
-           }
-           aLocalWLine->AddVertex(aVertex);
-         }
-         aLineOn2S->RemovePoint(k);
-         anEndIndex--;
-         continue;
-       }
+        IntSurf_PntOn2S p1 = aLineOn2S->Value(i);
+        IntSurf_PntOn2S p2 = aLineOn2S->Value(k);
+        
+        if(p1.Value().IsEqual(p2.Value(), gp::Resolution())) {
+          aTmpWLine = aLocalWLine;
+          aLocalWLine = new IntPatch_WLine(aLineOn2S, Standard_False);
+
+          for(v = 1; v <= aTmpWLine->NbVertex(); v++) {
+            IntPatch_Point aVertex = aTmpWLine->Vertex(v);
+            Standard_Integer avertexindex = (Standard_Integer)aVertex.ParameterOnLine();
+
+            if(avertexindex >= k) {
+              aVertex.SetParameter(aVertex.ParameterOnLine() - 1.);
+            }
+            aLocalWLine->AddVertex(aVertex);
+          }
+          aLineOn2S->RemovePoint(k);
+          anEndIndex--;
+          continue;
+        }
       }
       k++;
     }
@@ -3112,8 +3090,8 @@ Handle(IntPatch_WLine) ComputePurgedWLine(const Handle(IntPatch_WLine)& theWLine
 //purpose  : 
 //=======================================================================
 void TolR3d(const TopoDS_Face& aF1,
-           const TopoDS_Face& aF2,
-           Standard_Real& myTolReached3d)
+            const TopoDS_Face& aF2,
+            Standard_Real& myTolReached3d)
 {
   Standard_Real aTolF1, aTolF2, aTolFMax, aTolTresh;
       
@@ -3127,39 +3105,14 @@ void TolR3d(const TopoDS_Face& aF1,
   }
 }
 //=======================================================================
-//function : AdjustPeriodic
-//purpose  : 
-//=======================================================================
-Standard_Real AdjustPeriodic(const Standard_Real theParameter,
-                            const Standard_Real parmin,
-                            const Standard_Real parmax,
-                            const Standard_Real thePeriod,
-                            Standard_Real&      theOffset) 
-{
-  Standard_Real aresult;
-  //
-  theOffset = 0.;
-  aresult = theParameter;
-  while(aresult < parmin) {
-    aresult += thePeriod;
-    theOffset += thePeriod;
-  }
-
-  while(aresult > parmax) {
-    aresult -= thePeriod;
-    theOffset -= thePeriod;
-  }
-  return aresult;
-}
-//=======================================================================
 //function : IsPointOnBoundary
 //purpose  : 
 //=======================================================================
 Standard_Boolean IsPointOnBoundary(const Standard_Real theParameter,
-                                  const Standard_Real theFirstBoundary,
-                                  const Standard_Real theSecondBoundary,
-                                  const Standard_Real theResolution,
-                                  Standard_Boolean&   IsOnFirstBoundary) 
+                                   const Standard_Real theFirstBoundary,
+                                   const Standard_Real theSecondBoundary,
+                                   const Standard_Real theResolution,
+                                   Standard_Boolean&   IsOnFirstBoundary) 
 {
   Standard_Boolean bRet;
   Standard_Integer i;
@@ -3185,12 +3138,12 @@ Standard_Boolean IsPointOnBoundary(const Standard_Real theParameter,
 // purpose:
 // ------------------------------------------------------------------------------------------------
 Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
-                          const gp_Pnt2d&     theLastPoint,
-                          const Standard_Real theUmin, 
-                          const Standard_Real theUmax,
-                          const Standard_Real theVmin,
-                          const Standard_Real theVmax,
-                          gp_Pnt2d&           theNewPoint) {
+                           const gp_Pnt2d&     theLastPoint,
+                           const Standard_Real theUmin, 
+                           const Standard_Real theUmax,
+                           const Standard_Real theVmin,
+                           const Standard_Real theVmax,
+                           gp_Pnt2d&           theNewPoint) {
   
   gp_Vec2d aVec(theFirstPoint, theLastPoint);
   Standard_Integer i = 0, j = 0;
@@ -3207,9 +3160,9 @@ Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
       anOtherVecNormal.SetY(0.);
 
       if(i < 2)
-       aprojpoint.SetX(theUmin);
+        aprojpoint.SetX(theUmin);
       else
-       aprojpoint.SetX(theUmax);
+        aprojpoint.SetX(theUmax);
     }
     else {
       anOtherVec.SetX(1.);
@@ -3218,9 +3171,9 @@ Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
       anOtherVecNormal.SetY(1.);
 
       if(i < 2)
-       aprojpoint.SetY(theVmin);
+        aprojpoint.SetY(theVmin);
       else
-       aprojpoint.SetY(theVmax);
+        aprojpoint.SetY(theVmax);
     }
     gp_Vec2d anormvec = aVec;
     anormvec.Normalize();
@@ -3247,7 +3200,7 @@ Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
       gp_Pnt2d acurpoint(aprojpoint.XY() + (anOtherVec.XY()*anoffset));
       gp_Vec2d acurvec(theLastPoint, acurpoint);
       if ( bIsOut )
-       acurvec.Reverse();
+        acurvec.Reverse();
 
       Standard_Real aDotX, anAngleX;
       //
@@ -3255,20 +3208,20 @@ Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
       anAngleX = aVec.Angle(acurvec);
       //
       if(aDotX > 0. && fabs(anAngleX) < Precision::PConfusion()) {
-       if((i % 2) == 0) {
-         if((acurpoint.Y() >= theVmin) &&
-            (acurpoint.Y() <= theVmax)) {
-           theNewPoint = acurpoint;
-           return Standard_True;
-         }
-       }
-       else {
-         if((acurpoint.X() >= theUmin) &&
-            (acurpoint.X() <= theUmax)) {
-           theNewPoint = acurpoint;
-           return Standard_True;
-         }
-       }
+        if((i % 2) == 0) {
+          if((acurpoint.Y() >= theVmin) &&
+             (acurpoint.Y() <= theVmax)) {
+            theNewPoint = acurpoint;
+            return Standard_True;
+          }
+        }
+        else {
+          if((acurpoint.X() >= theUmin) &&
+             (acurpoint.X() <= theUmax)) {
+            theNewPoint = acurpoint;
+            return Standard_True;
+          }
+        }
       }
     }
   }
@@ -3281,15 +3234,15 @@ Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
 // purpose: Find point on the boundary of radial tangent zone
 // ------------------------------------------------------------------------------------------------
 Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
-                          const gp_Pnt2d&     theLastPoint,
-                          const Standard_Real theUmin, 
-                          const Standard_Real theUmax,
-                          const Standard_Real theVmin,
-                          const Standard_Real theVmax,
-                          const gp_Pnt2d&     theTanZoneCenter,
-                          const Standard_Real theZoneRadius,
-                          Handle(GeomAdaptor_HSurface) theGASurface,
-                          gp_Pnt2d&           theNewPoint) {
+                           const gp_Pnt2d&     theLastPoint,
+                           const Standard_Real theUmin, 
+                           const Standard_Real theUmax,
+                           const Standard_Real theVmin,
+                           const Standard_Real theVmax,
+                           const gp_Pnt2d&     theTanZoneCenter,
+                           const Standard_Real theZoneRadius,
+                           Handle(GeomAdaptor_HSurface) theGASurface,
+                           gp_Pnt2d&           theNewPoint) {
   theNewPoint = theLastPoint;
 
   if ( !IsInsideTanZone( theLastPoint, theTanZoneCenter, theZoneRadius, theGASurface) )
@@ -3330,9 +3283,9 @@ Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
     gp_Pnt2d aPInt = anIntersector.Point( i );
     if ( aPInt.SquareDistance( theFirstPoint ) < aMinDist ) {
       if ( ( aPInt.X() >= theUmin ) && ( aPInt.X() <= theUmax ) &&
-          ( aPInt.Y() >= theVmin ) && ( aPInt.Y() <= theVmax ) ) {
-       theNewPoint = aPInt;
-       aFound = Standard_True;
+           ( aPInt.Y() >= theVmin ) && ( aPInt.Y() <= theVmax ) ) {
+        theNewPoint = aPInt;
+        aFound = Standard_True;
       }
     }
   }
@@ -3345,9 +3298,9 @@ Standard_Boolean FindPoint(const gp_Pnt2d&     theFirstPoint,
 // purpose: Check if point is inside a radial tangent zone
 // ------------------------------------------------------------------------------------------------
 Standard_Boolean IsInsideTanZone(const gp_Pnt2d&     thePoint,
-                                const gp_Pnt2d&     theTanZoneCenter,
-                                const Standard_Real theZoneRadius,
-                                Handle(GeomAdaptor_HSurface) theGASurface) {
+                                 const gp_Pnt2d&     theTanZoneCenter,
+                                 const Standard_Real theZoneRadius,
+                                 Handle(GeomAdaptor_HSurface) theGASurface) {
 
   Standard_Real aUResolution = theGASurface->UResolution( theZoneRadius );
   Standard_Real aVResolution = theGASurface->VResolution( theZoneRadius );
@@ -3363,7 +3316,7 @@ Standard_Boolean IsInsideTanZone(const gp_Pnt2d&     thePoint,
 // purpose: Check if tangent zone exists
 // ------------------------------------------------------------------------------------------------
 Standard_Boolean CheckTangentZonesExist( const Handle(GeomAdaptor_HSurface)& theSurface1,
-                                       const Handle(GeomAdaptor_HSurface)&  theSurface2 ) 
+                                        const Handle(GeomAdaptor_HSurface)&  theSurface2 ) 
 {
   if ( ( theSurface1->GetType() != GeomAbs_Torus ) ||
       ( theSurface2->GetType() != GeomAbs_Torus ) )
@@ -3390,12 +3343,12 @@ Standard_Boolean CheckTangentZonesExist( const Handle(GeomAdaptor_HSurface)& the
 // purpose: 
 // ------------------------------------------------------------------------------------------------
 Standard_Integer ComputeTangentZones( const Handle(GeomAdaptor_HSurface)& theSurface1,
-                                    const Handle(GeomAdaptor_HSurface)&  theSurface2,
-                                    const TopoDS_Face&                   theFace1,
-                                    const TopoDS_Face&                   theFace2,
-                                    Handle(TColgp_HArray1OfPnt2d)&       theResultOnS1,
-                                    Handle(TColgp_HArray1OfPnt2d)&       theResultOnS2,
-                                    Handle(TColStd_HArray1OfReal)&       theResultRadius,
+                                     const Handle(GeomAdaptor_HSurface)&  theSurface2,
+                                     const TopoDS_Face&                   theFace1,
+                                     const TopoDS_Face&                   theFace2,
+                                     Handle(TColgp_HArray1OfPnt2d)&       theResultOnS1,
+                                     Handle(TColgp_HArray1OfPnt2d)&       theResultOnS2,
+                                     Handle(TColStd_HArray1OfReal)&       theResultRadius,
                                      const Handle(IntTools_Context)& aContext)
 {
   Standard_Integer aResult = 0;
@@ -3458,80 +3411,80 @@ Standard_Integer ComputeTangentZones( const Handle(GeomAdaptor_HSurface)& theSur
     GeomAdaptor_Curve aC1( new Geom_Circle(aCircle1) );
     GeomAdaptor_Curve aC2( new Geom_Circle(aCircle2) );
     Extrema_ExtCC anExtrema(aC1, aC2, 0, 2. * M_PI, 0, 2. * M_PI, 
-                           Precision::PConfusion(), Precision::PConfusion());
-       
+                            Precision::PConfusion(), Precision::PConfusion());
+            
     if ( anExtrema.IsDone() ) {
 
       Standard_Integer i = 0;
       for ( i = 1; i <= anExtrema.NbExt(); i++ ) {
-       if ( anExtrema.SquareDistance(i) > aCriteria * aCriteria )
-         continue;
-
-       Extrema_POnCurv P1, P2;
-       anExtrema.Points( i, P1, P2 );
-
-       Standard_Boolean bFoundResult = Standard_True;
-       gp_Pnt2d pr1, pr2;
-
-       Standard_Integer surfit = 0;
-       for ( surfit = 0; surfit < 2; surfit++ ) {
-         GeomAPI_ProjectPointOnSurf& aProjector = 
-           (surfit == 0) ? aContext->ProjPS(theFace1) : aContext->ProjPS(theFace2);
-
-         gp_Pnt aP3d = (surfit == 0) ? P1.Value() : P2.Value();
-         aProjector.Perform(aP3d);
-
-         if(!aProjector.IsDone())
-           bFoundResult = Standard_False;
-         else {
-           if(aProjector.LowerDistance() > aCriteria) {
-             bFoundResult = Standard_False;
-           }
-           else {
-             Standard_Real foundU = 0, foundV = 0;
-             aProjector.LowerDistanceParameters(foundU, foundV);
-             if ( surfit == 0 )
-               pr1 = gp_Pnt2d( foundU, foundV );
-             else
-               pr2 = gp_Pnt2d( foundU, foundV );
-           }
-         }
-       }
-       if ( bFoundResult ) {
-         aSeqResultS1.Append( pr1 );
-         aSeqResultS2.Append( pr2 );
-         aSeqResultRad.Append( aCriteria );
-
-         // torus is u and v periodic
-         const Standard_Real twoPI = M_PI + M_PI;
-         Standard_Real arr1tmp[2] = {pr1.X(), pr1.Y()};
-         Standard_Real arr2tmp[2] = {pr2.X(), pr2.Y()};
-
-         // iteration on period bounds
-         for ( Standard_Integer k1 = 0; k1 < 2; k1++ ) {
-           Standard_Real aBound = ( k1 == 0 ) ? 0 : twoPI;
-           Standard_Real aShift = ( k1 == 0 ) ? twoPI : -twoPI;
-
-           // iteration on surfaces
-           for ( Standard_Integer k2 = 0; k2 < 2; k2++ ) {
-             Standard_Real* arr1 = ( k2 == 0 ) ? arr1tmp : arr2tmp;
-             Standard_Real* arr2 = ( k2 != 0 ) ? arr1tmp : arr2tmp;
-             TColgp_SequenceOfPnt2d& aSeqS1 = ( k2 == 0 ) ? aSeqResultS1 : aSeqResultS2; 
-             TColgp_SequenceOfPnt2d& aSeqS2 = ( k2 != 0 ) ? aSeqResultS1 : aSeqResultS2; 
-
-             if (fabs(arr1[0] - aBound) < Precision::PConfusion()) {
-               aSeqS1.Append( gp_Pnt2d( arr1[0] + aShift, arr1[1] ) );
-               aSeqS2.Append( gp_Pnt2d( arr2[0], arr2[1] ) );
-               aSeqResultRad.Append( aCriteria );
-             }
-             if (fabs(arr1[1] - aBound) < Precision::PConfusion()) {
-               aSeqS1.Append( gp_Pnt2d( arr1[0], arr1[1] + aShift) );
-               aSeqS2.Append( gp_Pnt2d( arr2[0], arr2[1] ) );
-               aSeqResultRad.Append( aCriteria );
-             }
-           }
-         } //
-       }
+        if ( anExtrema.SquareDistance(i) > aCriteria * aCriteria )
+          continue;
+
+        Extrema_POnCurv P1, P2;
+        anExtrema.Points( i, P1, P2 );
+
+        Standard_Boolean bFoundResult = Standard_True;
+        gp_Pnt2d pr1, pr2;
+
+        Standard_Integer surfit = 0;
+        for ( surfit = 0; surfit < 2; surfit++ ) {
+          GeomAPI_ProjectPointOnSurf& aProjector = 
+            (surfit == 0) ? aContext->ProjPS(theFace1) : aContext->ProjPS(theFace2);
+
+          gp_Pnt aP3d = (surfit == 0) ? P1.Value() : P2.Value();
+          aProjector.Perform(aP3d);
+
+          if(!aProjector.IsDone())
+            bFoundResult = Standard_False;
+          else {
+            if(aProjector.LowerDistance() > aCriteria) {
+              bFoundResult = Standard_False;
+            }
+            else {
+              Standard_Real foundU = 0, foundV = 0;
+              aProjector.LowerDistanceParameters(foundU, foundV);
+              if ( surfit == 0 )
+                pr1 = gp_Pnt2d( foundU, foundV );
+              else
+                pr2 = gp_Pnt2d( foundU, foundV );
+            }
+          }
+        }
+        if ( bFoundResult ) {
+          aSeqResultS1.Append( pr1 );
+          aSeqResultS2.Append( pr2 );
+          aSeqResultRad.Append( aCriteria );
+
+          // torus is u and v periodic
+          const Standard_Real twoPI = M_PI + M_PI;
+          Standard_Real arr1tmp[2] = {pr1.X(), pr1.Y()};
+          Standard_Real arr2tmp[2] = {pr2.X(), pr2.Y()};
+
+          // iteration on period bounds
+          for ( Standard_Integer k1 = 0; k1 < 2; k1++ ) {
+            Standard_Real aBound = ( k1 == 0 ) ? 0 : twoPI;
+            Standard_Real aShift = ( k1 == 0 ) ? twoPI : -twoPI;
+
+            // iteration on surfaces
+            for ( Standard_Integer k2 = 0; k2 < 2; k2++ ) {
+              Standard_Real* arr1 = ( k2 == 0 ) ? arr1tmp : arr2tmp;
+              Standard_Real* arr2 = ( k2 != 0 ) ? arr1tmp : arr2tmp;
+              TColgp_SequenceOfPnt2d& aSeqS1 = ( k2 == 0 ) ? aSeqResultS1 : aSeqResultS2; 
+              TColgp_SequenceOfPnt2d& aSeqS2 = ( k2 != 0 ) ? aSeqResultS1 : aSeqResultS2; 
+
+              if (fabs(arr1[0] - aBound) < Precision::PConfusion()) {
+                aSeqS1.Append( gp_Pnt2d( arr1[0] + aShift, arr1[1] ) );
+                aSeqS2.Append( gp_Pnt2d( arr2[0], arr2[1] ) );
+                aSeqResultRad.Append( aCriteria );
+              }
+              if (fabs(arr1[1] - aBound) < Precision::PConfusion()) {
+                aSeqS1.Append( gp_Pnt2d( arr1[0], arr1[1] + aShift) );
+                aSeqS2.Append( gp_Pnt2d( arr2[0], arr2[1] ) );
+                aSeqResultRad.Append( aCriteria );
+              }
+            }
+          } //
+        }
       }
     }
   }
@@ -3556,8 +3509,8 @@ Standard_Integer ComputeTangentZones( const Handle(GeomAdaptor_HSurface)& theSur
 // purpose:
 // ------------------------------------------------------------------------------------------------
 gp_Pnt2d AdjustByNeighbour(const gp_Pnt2d&     theaNeighbourPoint,
-                          const gp_Pnt2d&     theOriginalPoint,
-                          Handle(GeomAdaptor_HSurface) theGASurface) {
+                           const gp_Pnt2d&     theOriginalPoint,
+                           Handle(GeomAdaptor_HSurface) theGASurface) {
   
   gp_Pnt2d ap1 = theaNeighbourPoint;
   gp_Pnt2d ap2 = theOriginalPoint;
@@ -3572,8 +3525,8 @@ gp_Pnt2d AdjustByNeighbour(const gp_Pnt2d&     theaNeighbourPoint,
       Standard_Real dd = ap1.SquareDistance( aPTest );
 
       if ( dd < aSqDistMin ) {
-       ap2 = aPTest;
-       aSqDistMin = dd;
+        ap2 = aPTest;
+        aSqDistMin = dd;
       }
     }
   }
@@ -3587,8 +3540,8 @@ gp_Pnt2d AdjustByNeighbour(const gp_Pnt2d&     theaNeighbourPoint,
       Standard_Real dd = ap1.SquareDistance( aPTest );
 
       if ( dd < aSqDistMin ) {
-       ap2 = aPTest;
-       aSqDistMin = dd;
+        ap2 = aPTest;
+        aSqDistMin = dd;
       }
     }
   }
@@ -3600,14 +3553,14 @@ gp_Pnt2d AdjustByNeighbour(const gp_Pnt2d&     theaNeighbourPoint,
 // purpose:
 // ------------------------------------------------------------------------------------------------
 Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
-                                     const Handle(GeomAdaptor_HSurface)&            theSurface1, 
-                                     const Handle(GeomAdaptor_HSurface)&            theSurface2,
-                                     const TopoDS_Face&                             theFace1,
-                                     const TopoDS_Face&                             theFace2,
-                                     const IntTools_LineConstructor&                theLConstructor,
-                                     const Standard_Boolean                         theAvoidLConstructor,
-                                     IntPatch_SequenceOfLine&                       theNewLines,
-                                     Standard_Real&                                 theReachedTol3d,
+                                      const Handle(GeomAdaptor_HSurface)&            theSurface1, 
+                                      const Handle(GeomAdaptor_HSurface)&            theSurface2,
+                                      const TopoDS_Face&                             theFace1,
+                                      const TopoDS_Face&                             theFace2,
+                                      const IntTools_LineConstructor&                theLConstructor,
+                                      const Standard_Boolean                         theAvoidLConstructor,
+                                      IntPatch_SequenceOfLine&                       theNewLines,
+                                      Standard_Real&                                 theReachedTol3d,
                                       const Handle(IntTools_Context)& aContext) 
 {
 
@@ -3639,7 +3592,7 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
   Handle(TColgp_HArray1OfPnt2d) aTanZoneS2;
   Handle(TColStd_HArray1OfReal) aTanZoneRadius;
   Standard_Integer aNbZone = ComputeTangentZones( theSurface1, theSurface2, theFace1, theFace2,
-                                                aTanZoneS1, aTanZoneS2, aTanZoneRadius, aContext);
+                                                 aTanZoneS1, aTanZoneS2, aTanZoneRadius, aContext);
   
   //
   nblines=0;
@@ -3665,83 +3618,83 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
       Handle(GeomAdaptor_HSurface) aGASurface = (!i) ? theSurface1 : theSurface2;
       aGASurface->ChangeSurface().Surface()->Bounds(umin, umax, vmin, vmax);
       if(!i) {
-       aPoint.ParametersOnS1(U, V);
+        aPoint.ParametersOnS1(U, V);
       }
       else {
-       aPoint.ParametersOnS2(U, V);
+        aPoint.ParametersOnS2(U, V);
       }
       // U, V
       for(j = 0; j < 2; j++) {
-       isperiodic = (!j) ? aGASurface->IsUPeriodic() : aGASurface->IsVPeriodic();
-       if(!isperiodic){
-         continue;
-       }
-       //
-       if (!j) {
-         aResolution=aGASurface->UResolution(aTol);
-         aPeriod=aGASurface->UPeriod();
-         alowerboundary=umin;
-         aupperboundary=umax;
-         aParameter=U;
-       }
-       else {
-         aResolution=aGASurface->VResolution(aTol);
-         aPeriod=aGASurface->VPeriod();
-         alowerboundary=vmin;
-         aupperboundary=vmax;
-         aParameter=V;
-       }
-       
-       anoffset = 0.;
-       anAdjustPar = AdjustPeriodic(aParameter, 
-                                    alowerboundary, 
-                                    aupperboundary, 
-                                    aPeriod, 
-                                    anoffset);
-       //
-       bIsOnFirstBoundary = Standard_True;// ?
-       bIsPointOnBoundary=
-         IsPointOnBoundary(anAdjustPar, 
-                           alowerboundary, 
-                           aupperboundary,
-                           aResolution, 
-                           bIsOnFirstBoundary);
-       //
-       if(bIsPointOnBoundary) {
-         bIsCurrentPointOnBoundary = Standard_True;
-         break;
-       }
-       else {
-         // check if a point belong to a tangent zone. Begin
-         Standard_Integer zIt = 0;
-         for ( zIt = 1; zIt <= aNbZone; zIt++ ) {
-           gp_Pnt2d aPZone = (i == 0) ? aTanZoneS1->Value(zIt) : aTanZoneS2->Value(zIt);
-           Standard_Real aZoneRadius = aTanZoneRadius->Value(zIt);
-
-           if ( IsInsideTanZone(gp_Pnt2d( U, V ), aPZone, aZoneRadius, aGASurface ) ) {
-             // set boundary flag to split the curve by a tangent zone
-             bIsPointOnBoundary = Standard_True;
-             bIsCurrentPointOnBoundary = Standard_True;
-             if ( theReachedTol3d < aZoneRadius ) {
-               theReachedTol3d = aZoneRadius;
-             }
-             break;
-           }
-         }
-       }
+        isperiodic = (!j) ? aGASurface->IsUPeriodic() : aGASurface->IsVPeriodic();
+        if(!isperiodic){
+          continue;
+        }
+        //
+        if (!j) {
+          aResolution=aGASurface->UResolution(aTol);
+          aPeriod=aGASurface->UPeriod();
+          alowerboundary=umin;
+          aupperboundary=umax;
+          aParameter=U;
+        }
+        else {
+          aResolution=aGASurface->VResolution(aTol);
+          aPeriod=aGASurface->VPeriod();
+          alowerboundary=vmin;
+          aupperboundary=vmax;
+          aParameter=V;
+        }
+        
+        IntTools_Tools::AdjustPeriodic(aParameter, 
+                                       alowerboundary, 
+                                       aupperboundary, 
+                                       aPeriod,
+                                       anAdjustPar,
+                                       anoffset);
+        //
+        bIsOnFirstBoundary = Standard_True;// ?
+        bIsPointOnBoundary=
+          IsPointOnBoundary(anAdjustPar, 
+                            alowerboundary, 
+                            aupperboundary,
+                            aResolution, 
+                            bIsOnFirstBoundary);
+        //
+        if(bIsPointOnBoundary) {
+          bIsCurrentPointOnBoundary = Standard_True;
+          break;
+        }
+        else {
+          // check if a point belong to a tangent zone. Begin
+          Standard_Integer zIt = 0;
+          for ( zIt = 1; zIt <= aNbZone; zIt++ ) {
+            gp_Pnt2d aPZone = (i == 0) ? aTanZoneS1->Value(zIt) : aTanZoneS2->Value(zIt);
+            Standard_Real aZoneRadius = aTanZoneRadius->Value(zIt);
+
+            if ( IsInsideTanZone(gp_Pnt2d( U, V ), aPZone, aZoneRadius, aGASurface ) ) {
+              // set boundary flag to split the curve by a tangent zone
+              bIsPointOnBoundary = Standard_True;
+              bIsCurrentPointOnBoundary = Standard_True;
+              if ( theReachedTol3d < aZoneRadius ) {
+                theReachedTol3d = aZoneRadius;
+              }
+              break;
+            }
+          }
+        }
       }//for(j = 0; j < 2; j++) {
 
       if(bIsCurrentPointOnBoundary){
-       break;
+        break;
       }
     }//for(i = 0; i < 2; ++i) {
     //
     if((bIsCurrentPointOnBoundary != bIsPrevPointOnBoundary)) {
       if(!aListOfPointIndex.IsEmpty()) {
-       nblines++;
-       anArrayOfLines.SetValue(nblines, aListOfPointIndex);
-       anArrayOfLineType.SetValue(nblines, bIsPrevPointOnBoundary);
-       aListOfPointIndex.Clear();
+        nblines++;
+        anArrayOfLines.SetValue(nblines, aListOfPointIndex);
+        anArrayOfLineType.SetValue(nblines, bIsPrevPointOnBoundary);
+        aListOfPointIndex.Clear();
       }
       bIsPrevPointOnBoundary = bIsCurrentPointOnBoundary;
     }
@@ -3778,10 +3731,10 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
       Standard_Integer aneighbourindex = (j == 0) ? (i - 1) : (i + 1);
 
       if((aneighbourindex < 1) || (aneighbourindex > nblines))
-       continue;
+        continue;
 
       if(anArrayOfLineType.Value(aneighbourindex) == 0)
-       continue;
+        continue;
       const TColStd_ListOfInteger& aNeighbour = anArrayOfLines.Value(aneighbourindex);
       Standard_Integer anIndex = (j == 0) ? aNeighbour.Last() : aNeighbour.First();
       const IntSurf_PntOn2S& aPoint = theWLine->Point(anIndex);
@@ -3790,355 +3743,357 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
       
       for(Standard_Integer surfit = 0; surfit < 2; surfit++) {
 
-       Handle(GeomAdaptor_HSurface) aGASurface = (surfit == 0) ? theSurface1 : theSurface2;
-       Standard_Real umin=0., umax=0., vmin=0., vmax=0.;
-       aGASurface->ChangeSurface().Surface()->Bounds(umin, umax, vmin, vmax);
-       Standard_Real U=0., V=0.;
+        Handle(GeomAdaptor_HSurface) aGASurface = (surfit == 0) ? theSurface1 : theSurface2;
+        Standard_Real umin=0., umax=0., vmin=0., vmax=0.;
+        aGASurface->ChangeSurface().Surface()->Bounds(umin, umax, vmin, vmax);
+        Standard_Real U=0., V=0.;
 
-       if(surfit == 0)
-         aNewP.ParametersOnS1(U, V);
-       else
-         aNewP.ParametersOnS2(U, V);
-       Standard_Integer nbboundaries = 0;
+        if(surfit == 0)
+          aNewP.ParametersOnS1(U, V);
+        else
+          aNewP.ParametersOnS2(U, V);
+        Standard_Integer nbboundaries = 0;
 
-       Standard_Boolean bIsNearBoundary = Standard_False;
-       Standard_Integer aZoneIndex = 0;
-       Standard_Integer bIsUBoundary = Standard_False; // use if nbboundaries == 1
-       Standard_Integer bIsFirstBoundary = Standard_False; // use if nbboundaries == 1
-       
+        Standard_Boolean bIsNearBoundary = Standard_False;
+        Standard_Integer aZoneIndex = 0;
+        Standard_Integer bIsUBoundary = Standard_False; // use if nbboundaries == 1
+        Standard_Integer bIsFirstBoundary = Standard_False; // use if nbboundaries == 1
+        
 
-       for(Standard_Integer parit = 0; parit < 2; parit++) {
-         Standard_Boolean isperiodic = (parit == 0) ? aGASurface->IsUPeriodic() : aGASurface->IsVPeriodic();
+        for(Standard_Integer parit = 0; parit < 2; parit++) {
+          Standard_Boolean isperiodic = (parit == 0) ? aGASurface->IsUPeriodic() : aGASurface->IsVPeriodic();
 
-         Standard_Real aResolution = (parit == 0) ? aGASurface->UResolution(aTol) : aGASurface->VResolution(aTol);
-         Standard_Real alowerboundary = (parit == 0) ? umin : vmin;
-         Standard_Real aupperboundary = (parit == 0) ? umax : vmax;
+          Standard_Real aResolution = (parit == 0) ? aGASurface->UResolution(aTol) : aGASurface->VResolution(aTol);
+          Standard_Real alowerboundary = (parit == 0) ? umin : vmin;
+          Standard_Real aupperboundary = (parit == 0) ? umax : vmax;
 
-         Standard_Real aParameter = (parit == 0) ? U : V;
-         Standard_Boolean bIsOnFirstBoundary = Standard_True;
+          Standard_Real aParameter = (parit == 0) ? U : V;
+          Standard_Boolean bIsOnFirstBoundary = Standard_True;
   
-         if(!isperiodic) {
-           bIsPointOnBoundary=
-             IsPointOnBoundary(aParameter, alowerboundary, aupperboundary, aResolution, bIsOnFirstBoundary);
-           if(bIsPointOnBoundary) {
-             bIsUBoundary = (parit == 0);
-             bIsFirstBoundary = bIsOnFirstBoundary;
-             nbboundaries++;
-           }
-         }
-         else {
-           Standard_Real aPeriod     = (parit == 0) ? aGASurface->UPeriod() : aGASurface->VPeriod();
-           Standard_Real anoffset = 0.;
-           Standard_Real anAdjustPar = AdjustPeriodic(aParameter, alowerboundary, aupperboundary, aPeriod, anoffset);
-
-           bIsPointOnBoundary=
-             IsPointOnBoundary(anAdjustPar, alowerboundary, aupperboundary, aResolution, bIsOnFirstBoundary);
-           if(bIsPointOnBoundary) {
-             bIsUBoundary = (parit == 0);
-             bIsFirstBoundary = bIsOnFirstBoundary;
-             nbboundaries++;
-           }
-           else {
-             //check neighbourhood of boundary
-             Standard_Real anEpsilon = aResolution * 100.;
-             Standard_Real aPart = ( aupperboundary - alowerboundary ) * 0.1;
-             anEpsilon = ( anEpsilon > aPart ) ? aPart : anEpsilon;
-               
-             bIsNearBoundary = IsPointOnBoundary(anAdjustPar, alowerboundary, aupperboundary, 
-                                                 anEpsilon, bIsOnFirstBoundary);
-
-           }
-         }
-       }
-
-       // check if a point belong to a tangent zone. Begin
-       for ( Standard_Integer zIt = 1; zIt <= aNbZone; zIt++ ) {
-         gp_Pnt2d aPZone = (surfit == 0) ? aTanZoneS1->Value(zIt) : aTanZoneS2->Value(zIt);
-         Standard_Real aZoneRadius = aTanZoneRadius->Value(zIt);
-
-         Standard_Integer aneighbourpointindex1 = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
-         const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneighbourpointindex1);
-         Standard_Real nU1, nV1;
-           
-         if(surfit == 0)
-           aNeighbourPoint.ParametersOnS1(nU1, nV1);
-         else
-           aNeighbourPoint.ParametersOnS2(nU1, nV1);
-         gp_Pnt2d ap1(nU1, nV1);
-         gp_Pnt2d ap2 = AdjustByNeighbour( ap1, gp_Pnt2d( U, V ), aGASurface );
-
-
-         if ( IsInsideTanZone( ap2, aPZone, aZoneRadius, aGASurface ) ) {
-           aZoneIndex = zIt;
-           bIsNearBoundary = Standard_True;
-           if ( theReachedTol3d < aZoneRadius ) {
-             theReachedTol3d = aZoneRadius;
-           }
-         }
-       }
-       // check if a point belong to a tangent zone. End
-       Standard_Boolean bComputeLineEnd = Standard_False;
-
-       if(nbboundaries == 2) {
-         //xf
-         bComputeLineEnd = Standard_True;
-         //xt
-       }
-       else if(nbboundaries == 1) {
-         Standard_Boolean isperiodic = (bIsUBoundary) ? aGASurface->IsUPeriodic() : aGASurface->IsVPeriodic();
-
-         if(isperiodic) {
-           Standard_Real alowerboundary = (bIsUBoundary) ? umin : vmin;
-           Standard_Real aupperboundary = (bIsUBoundary) ? umax : vmax;
-           Standard_Real aPeriod     = (bIsUBoundary) ? aGASurface->UPeriod() : aGASurface->VPeriod();
-           Standard_Real aParameter = (bIsUBoundary) ? U : V;
-           Standard_Real anoffset = 0.;
-           Standard_Real anAdjustPar = AdjustPeriodic(aParameter, alowerboundary, aupperboundary, aPeriod, anoffset);
-
-           Standard_Real adist = (bIsFirstBoundary) ? fabs(anAdjustPar - alowerboundary) : fabs(anAdjustPar - aupperboundary);
-           Standard_Real anotherPar = (bIsFirstBoundary) ? (aupperboundary - adist) : (alowerboundary + adist);
-           anotherPar += anoffset;
-           Standard_Integer aneighbourpointindex = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
-           const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneighbourpointindex);
-           Standard_Real nU1, nV1;
-
-           if(surfit == 0)
-             aNeighbourPoint.ParametersOnS1(nU1, nV1);
-           else
-             aNeighbourPoint.ParametersOnS2(nU1, nV1);
-           
-           Standard_Real adist1 = (bIsUBoundary) ? fabs(nU1 - U) : fabs(nV1 - V);
-           Standard_Real adist2 = (bIsUBoundary) ? fabs(nU1 - anotherPar) : fabs(nV1 - anotherPar);
-           bComputeLineEnd = Standard_True;
-           Standard_Boolean bCheckAngle1 = Standard_False;
-           Standard_Boolean bCheckAngle2 = Standard_False;
-           gp_Vec2d aNewVec;
-           Standard_Real anewU = (bIsUBoundary) ? anotherPar : U;
-           Standard_Real anewV = (bIsUBoundary) ? V : anotherPar;
-
-           if(((adist1 - adist2) > Precision::PConfusion()) && 
-              (adist2 < (aPeriod / 4.))) {
-             bCheckAngle1 = Standard_True;
-             aNewVec = gp_Vec2d(gp_Pnt2d(nU1, nV1), gp_Pnt2d(anewU, anewV));
-
-             if(aNewVec.SquareMagnitude() < (gp::Resolution() * gp::Resolution())) {
-               aNewP.SetValue((surfit == 0), anewU, anewV);
-               bCheckAngle1 = Standard_False;
-             }
-           }
-           else if(adist1 < (aPeriod / 4.)) {
-             bCheckAngle2 = Standard_True;
-             aNewVec = gp_Vec2d(gp_Pnt2d(nU1, nV1), gp_Pnt2d(U, V));
-
-             if(aNewVec.SquareMagnitude() < (gp::Resolution() * gp::Resolution())) {
-               bCheckAngle2 = Standard_False;
-             }
-           }
-
-           if(bCheckAngle1 || bCheckAngle2) {
-             // assume there are at least two points in line (see "if" above)
-             Standard_Integer anindexother = aneighbourpointindex;
-
-             while((anindexother <= aListOfIndex.Last()) && (anindexother >= aListOfIndex.First())) {
-               anindexother = (j == 0) ? (anindexother + 1) : (anindexother - 1);
-               const IntSurf_PntOn2S& aPrevNeighbourPoint = theWLine->Point(anindexother);
-               Standard_Real nU2, nV2;
-               
-               if(surfit == 0)
-                 aPrevNeighbourPoint.ParametersOnS1(nU2, nV2);
-               else
-                 aPrevNeighbourPoint.ParametersOnS2(nU2, nV2);
-               gp_Vec2d aVecOld(gp_Pnt2d(nU2, nV2), gp_Pnt2d(nU1, nV1));
-
-               if(aVecOld.SquareMagnitude() <= (gp::Resolution() * gp::Resolution())) {
-                 continue;
-               }
-               else {
-                 Standard_Real anAngle = aNewVec.Angle(aVecOld);
-
-                 if((fabs(anAngle) < (M_PI * 0.25)) && (aNewVec.Dot(aVecOld) > 0.)) {
-
-                   if(bCheckAngle1) {
-                     Standard_Real U1, U2, V1, V2;
-                     IntSurf_PntOn2S atmppoint = aNewP;
-                     atmppoint.SetValue((surfit == 0), anewU, anewV);
-                     atmppoint.Parameters(U1, V1, U2, V2);
-                     gp_Pnt P1 = theSurface1->Value(U1, V1);
-                     gp_Pnt P2 = theSurface2->Value(U2, V2);
-                     gp_Pnt P0 = aPoint.Value();
-
-                     if(P0.IsEqual(P1, aTol) &&
-                        P0.IsEqual(P2, aTol) &&
-                        P1.IsEqual(P2, aTol)) {
-                       bComputeLineEnd = Standard_False;
-                       aNewP.SetValue((surfit == 0), anewU, anewV);
-                     }
-                   }
-
-                   if(bCheckAngle2) {
-                     bComputeLineEnd = Standard_False;
-                   }
-                 }
-                 break;
-               }
-             } // end while(anindexother...)
-           }
-         }
-       }
-       else if ( bIsNearBoundary ) {
-         bComputeLineEnd = Standard_True;
-       }
-
-       if(bComputeLineEnd) {
-
-         gp_Pnt2d anewpoint;
-         Standard_Boolean found = Standard_False;
-
-         if ( bIsNearBoundary ) {
-           // re-compute point near natural boundary or near tangent zone
-           Standard_Real u1, v1, u2, v2;
-           aNewP.Parameters( u1, v1, u2, v2 );
-           if(surfit == 0)
-             anewpoint = gp_Pnt2d( u1, v1 );
-           else
-             anewpoint = gp_Pnt2d( u2, v2 );
-           
-           Standard_Integer aneighbourpointindex1 = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
-           const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneighbourpointindex1);
-           Standard_Real nU1, nV1;
-           
-           if(surfit == 0)
-             aNeighbourPoint.ParametersOnS1(nU1, nV1);
-           else
-             aNeighbourPoint.ParametersOnS2(nU1, nV1);
-           gp_Pnt2d ap1(nU1, nV1);
-           gp_Pnt2d ap2;
-
-
-           if ( aZoneIndex ) {
-             // exclude point from a tangent zone
-             anewpoint = AdjustByNeighbour( ap1, anewpoint, aGASurface );
-             gp_Pnt2d aPZone = (surfit == 0) ? aTanZoneS1->Value(aZoneIndex) : aTanZoneS2->Value(aZoneIndex);
-             Standard_Real aZoneRadius = aTanZoneRadius->Value(aZoneIndex);
-
-             if ( FindPoint(ap1, anewpoint, umin, umax, vmin, vmax, 
-                            aPZone, aZoneRadius, aGASurface, ap2) ) {
-               anewpoint = ap2;
-               found = Standard_True;
-             }
-           }
-           else if ( aGASurface->IsUPeriodic() || aGASurface->IsVPeriodic() ) {
-             // re-compute point near boundary if shifted on a period
-             ap2 = AdjustByNeighbour( ap1, anewpoint, aGASurface );
-
-             if ( ( ap2.X() < umin ) || ( ap2.X() > umax ) ||
-                 ( ap2.Y() < vmin ) || ( ap2.Y() > vmax ) ) {
-               found = FindPoint(ap1, ap2, umin, umax, vmin, vmax, anewpoint);
-             }
-             else {
-               anewpoint = ap2;
-               aNewP.SetValue( (surfit == 0), anewpoint.X(), anewpoint.Y() );
-             }
-           }
-         }
-         else {
-
-           Standard_Integer aneighbourpointindex1 = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
-           const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneighbourpointindex1);
-           Standard_Real nU1, nV1;
-
-           if(surfit == 0)
-             aNeighbourPoint.ParametersOnS1(nU1, nV1);
-           else
-             aNeighbourPoint.ParametersOnS2(nU1, nV1);
-           gp_Pnt2d ap1(nU1, nV1);
-           gp_Pnt2d ap2(nU1, nV1);
-           Standard_Integer aneighbourpointindex2 = aneighbourpointindex1;
-
-           while((aneighbourpointindex2 <= aListOfIndex.Last()) && (aneighbourpointindex2 >= aListOfIndex.First())) {
-             aneighbourpointindex2 = (j == 0) ? (aneighbourpointindex2 + 1) : (aneighbourpointindex2 - 1);
-             const IntSurf_PntOn2S& aPrevNeighbourPoint = theWLine->Point(aneighbourpointindex2);
-             Standard_Real nU2, nV2;
-
-             if(surfit == 0)
-               aPrevNeighbourPoint.ParametersOnS1(nU2, nV2);
-             else
-               aPrevNeighbourPoint.ParametersOnS2(nU2, nV2);
-             ap2.SetX(nU2);
-             ap2.SetY(nV2);
-
-             if(ap1.SquareDistance(ap2) > (gp::Resolution() * gp::Resolution())) {
-               break;
-             }
-           }  
-           found = FindPoint(ap2, ap1, umin, umax, vmin, vmax, anewpoint);
-         }
-
-         if(found) {
-           // check point
-           Standard_Real aCriteria = BRep_Tool::Tolerance(theFace1) + BRep_Tool::Tolerance(theFace2);
-           GeomAPI_ProjectPointOnSurf& aProjector = 
-             (surfit == 0) ? aContext->ProjPS(theFace2) : aContext->ProjPS(theFace1);
-           Handle(GeomAdaptor_HSurface) aSurface = (surfit == 0) ? theSurface1 : theSurface2;
-
-           Handle(GeomAdaptor_HSurface) aSurfaceOther = (surfit == 0) ? theSurface2 : theSurface1;
-
-           gp_Pnt aP3d = aSurface->Value(anewpoint.X(), anewpoint.Y());
-           aProjector.Perform(aP3d);
-
-           if(aProjector.IsDone()) {
-             if(aProjector.LowerDistance() < aCriteria) {
-               Standard_Real foundU = U, foundV = V;
-               aProjector.LowerDistanceParameters(foundU, foundV);
-
-               //Correction of projected coordinates. Begin
-               //Note, it may be shifted on a period
-               Standard_Integer aneindex1 = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
-               const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneindex1);
-               Standard_Real nUn, nVn;
-               
-               if(surfit == 0)
-                 aNeighbourPoint.ParametersOnS2(nUn, nVn);
-               else
-                 aNeighbourPoint.ParametersOnS1(nUn, nVn);
-               gp_Pnt2d aNeighbour2d(nUn, nVn);
-               gp_Pnt2d anAdjustedPoint = AdjustByNeighbour( aNeighbour2d, gp_Pnt2d(foundU, foundV), aSurfaceOther );
-               foundU = anAdjustedPoint.X();
-               foundV = anAdjustedPoint.Y();
-
-               if ( ( anAdjustedPoint.X() < umin ) && ( anAdjustedPoint.X() > umax ) &&
-                   ( anAdjustedPoint.Y() < vmin ) && ( anAdjustedPoint.Y() > vmax ) ) {
-                 // attempt to roughly re-compute point
-                 foundU = ( foundU < umin ) ? umin : foundU;
-                 foundU = ( foundU > umax ) ? umax : foundU;
-                 foundV = ( foundV < vmin ) ? vmin : foundV;
-                 foundV = ( foundV > vmax ) ? vmax : foundV;
-
-                 GeomAPI_ProjectPointOnSurf& aProjector2 = 
-                   (surfit == 0) ? aContext->ProjPS(theFace1) : aContext->ProjPS(theFace2);
-
-                 aP3d = aSurfaceOther->Value(foundU, foundV);
-                 aProjector2.Perform(aP3d);
-                 
-                 if(aProjector2.IsDone()) {
-                   if(aProjector2.LowerDistance() < aCriteria) {
-                     Standard_Real foundU2 = anewpoint.X(), foundV2 = anewpoint.Y();
-                     aProjector2.LowerDistanceParameters(foundU2, foundV2);
-                     anewpoint.SetX(foundU2);
-                     anewpoint.SetY(foundV2);
-                   }
-                 }
-               }
-               //Correction of projected coordinates. End
-
-               if(surfit == 0)
-                 aNewP.SetValue(aP3d, anewpoint.X(), anewpoint.Y(), foundU, foundV);
-               else
-                 aNewP.SetValue(aP3d, foundU, foundV, anewpoint.X(), anewpoint.Y());
-             }
-           }
-         }
-       }
+          if(!isperiodic) {
+            bIsPointOnBoundary=
+              IsPointOnBoundary(aParameter, alowerboundary, aupperboundary, aResolution, bIsOnFirstBoundary);
+            if(bIsPointOnBoundary) {
+              bIsUBoundary = (parit == 0);
+              bIsFirstBoundary = bIsOnFirstBoundary;
+              nbboundaries++;
+            }
+          }
+          else {
+            Standard_Real aPeriod     = (parit == 0) ? aGASurface->UPeriod() : aGASurface->VPeriod();
+            Standard_Real anoffset, anAdjustPar;
+            IntTools_Tools::AdjustPeriodic(aParameter, alowerboundary, aupperboundary,
+                                           aPeriod, anAdjustPar, anoffset);
+
+            bIsPointOnBoundary=
+              IsPointOnBoundary(anAdjustPar, alowerboundary, aupperboundary, aResolution, bIsOnFirstBoundary);
+            if(bIsPointOnBoundary) {
+              bIsUBoundary = (parit == 0);
+              bIsFirstBoundary = bIsOnFirstBoundary;
+              nbboundaries++;
+            }
+            else {
+              //check neighbourhood of boundary
+              Standard_Real anEpsilon = aResolution * 100.;
+              Standard_Real aPart = ( aupperboundary - alowerboundary ) * 0.1;
+              anEpsilon = ( anEpsilon > aPart ) ? aPart : anEpsilon;
+                
+              bIsNearBoundary = IsPointOnBoundary(anAdjustPar, alowerboundary, aupperboundary, 
+                                                  anEpsilon, bIsOnFirstBoundary);
+
+            }
+          }
+        }
+
+        // check if a point belong to a tangent zone. Begin
+        for ( Standard_Integer zIt = 1; zIt <= aNbZone; zIt++ ) {
+          gp_Pnt2d aPZone = (surfit == 0) ? aTanZoneS1->Value(zIt) : aTanZoneS2->Value(zIt);
+          Standard_Real aZoneRadius = aTanZoneRadius->Value(zIt);
+
+          Standard_Integer aneighbourpointindex1 = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
+          const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneighbourpointindex1);
+          Standard_Real nU1, nV1;
+            
+          if(surfit == 0)
+            aNeighbourPoint.ParametersOnS1(nU1, nV1);
+          else
+            aNeighbourPoint.ParametersOnS2(nU1, nV1);
+          gp_Pnt2d ap1(nU1, nV1);
+          gp_Pnt2d ap2 = AdjustByNeighbour( ap1, gp_Pnt2d( U, V ), aGASurface );
+
+
+          if ( IsInsideTanZone( ap2, aPZone, aZoneRadius, aGASurface ) ) {
+            aZoneIndex = zIt;
+            bIsNearBoundary = Standard_True;
+            if ( theReachedTol3d < aZoneRadius ) {
+              theReachedTol3d = aZoneRadius;
+            }
+          }
+        }
+        // check if a point belong to a tangent zone. End
+        Standard_Boolean bComputeLineEnd = Standard_False;
+
+        if(nbboundaries == 2) {
+          //xf
+          bComputeLineEnd = Standard_True;
+          //xt
+        }
+        else if(nbboundaries == 1) {
+          Standard_Boolean isperiodic = (bIsUBoundary) ? aGASurface->IsUPeriodic() : aGASurface->IsVPeriodic();
+
+          if(isperiodic) {
+            Standard_Real alowerboundary = (bIsUBoundary) ? umin : vmin;
+            Standard_Real aupperboundary = (bIsUBoundary) ? umax : vmax;
+            Standard_Real aPeriod     = (bIsUBoundary) ? aGASurface->UPeriod() : aGASurface->VPeriod();
+            Standard_Real aParameter = (bIsUBoundary) ? U : V;
+            Standard_Real anoffset, anAdjustPar;
+            IntTools_Tools::AdjustPeriodic(aParameter, alowerboundary, aupperboundary, 
+                                           aPeriod, anAdjustPar, anoffset);
+
+            Standard_Real adist = (bIsFirstBoundary) ? fabs(anAdjustPar - alowerboundary) : fabs(anAdjustPar - aupperboundary);
+            Standard_Real anotherPar = (bIsFirstBoundary) ? (aupperboundary - adist) : (alowerboundary + adist);
+            anotherPar += anoffset;
+            Standard_Integer aneighbourpointindex = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
+            const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneighbourpointindex);
+            Standard_Real nU1, nV1;
+
+            if(surfit == 0)
+              aNeighbourPoint.ParametersOnS1(nU1, nV1);
+            else
+              aNeighbourPoint.ParametersOnS2(nU1, nV1);
+            
+            Standard_Real adist1 = (bIsUBoundary) ? fabs(nU1 - U) : fabs(nV1 - V);
+            Standard_Real adist2 = (bIsUBoundary) ? fabs(nU1 - anotherPar) : fabs(nV1 - anotherPar);
+            bComputeLineEnd = Standard_True;
+            Standard_Boolean bCheckAngle1 = Standard_False;
+            Standard_Boolean bCheckAngle2 = Standard_False;
+            gp_Vec2d aNewVec;
+            Standard_Real anewU = (bIsUBoundary) ? anotherPar : U;
+            Standard_Real anewV = (bIsUBoundary) ? V : anotherPar;
+
+            if(((adist1 - adist2) > Precision::PConfusion()) && 
+               (adist2 < (aPeriod / 4.))) {
+              bCheckAngle1 = Standard_True;
+              aNewVec = gp_Vec2d(gp_Pnt2d(nU1, nV1), gp_Pnt2d(anewU, anewV));
+
+              if(aNewVec.SquareMagnitude() < (gp::Resolution() * gp::Resolution())) {
+                aNewP.SetValue((surfit == 0), anewU, anewV);
+                bCheckAngle1 = Standard_False;
+              }
+            }
+            else if(adist1 < (aPeriod / 4.)) {
+              bCheckAngle2 = Standard_True;
+              aNewVec = gp_Vec2d(gp_Pnt2d(nU1, nV1), gp_Pnt2d(U, V));
+
+              if(aNewVec.SquareMagnitude() < (gp::Resolution() * gp::Resolution())) {
+                bCheckAngle2 = Standard_False;
+              }
+            }
+
+            if(bCheckAngle1 || bCheckAngle2) {
+              // assume there are at least two points in line (see "if" above)
+              Standard_Integer anindexother = aneighbourpointindex;
+
+              while((anindexother <= aListOfIndex.Last()) && (anindexother >= aListOfIndex.First())) {
+                anindexother = (j == 0) ? (anindexother + 1) : (anindexother - 1);
+                const IntSurf_PntOn2S& aPrevNeighbourPoint = theWLine->Point(anindexother);
+                Standard_Real nU2, nV2;
+                
+                if(surfit == 0)
+                  aPrevNeighbourPoint.ParametersOnS1(nU2, nV2);
+                else
+                  aPrevNeighbourPoint.ParametersOnS2(nU2, nV2);
+                gp_Vec2d aVecOld(gp_Pnt2d(nU2, nV2), gp_Pnt2d(nU1, nV1));
+
+                if(aVecOld.SquareMagnitude() <= (gp::Resolution() * gp::Resolution())) {
+                  continue;
+                }
+                else {
+                  Standard_Real anAngle = aNewVec.Angle(aVecOld);
+
+                  if((fabs(anAngle) < (M_PI * 0.25)) && (aNewVec.Dot(aVecOld) > 0.)) {
+
+                    if(bCheckAngle1) {
+                      Standard_Real U1, U2, V1, V2;
+                      IntSurf_PntOn2S atmppoint = aNewP;
+                      atmppoint.SetValue((surfit == 0), anewU, anewV);
+                      atmppoint.Parameters(U1, V1, U2, V2);
+                      gp_Pnt P1 = theSurface1->Value(U1, V1);
+                      gp_Pnt P2 = theSurface2->Value(U2, V2);
+                      gp_Pnt P0 = aPoint.Value();
+
+                      if(P0.IsEqual(P1, aTol) &&
+                         P0.IsEqual(P2, aTol) &&
+                         P1.IsEqual(P2, aTol)) {
+                        bComputeLineEnd = Standard_False;
+                        aNewP.SetValue((surfit == 0), anewU, anewV);
+                      }
+                    }
+
+                    if(bCheckAngle2) {
+                      bComputeLineEnd = Standard_False;
+                    }
+                  }
+                  break;
+                }
+              } // end while(anindexother...)
+            }
+          }
+        }
+        else if ( bIsNearBoundary ) {
+          bComputeLineEnd = Standard_True;
+        }
+
+        if(bComputeLineEnd) {
+
+          gp_Pnt2d anewpoint;
+          Standard_Boolean found = Standard_False;
+
+          if ( bIsNearBoundary ) {
+            // re-compute point near natural boundary or near tangent zone
+            Standard_Real u1, v1, u2, v2;
+            aNewP.Parameters( u1, v1, u2, v2 );
+            if(surfit == 0)
+              anewpoint = gp_Pnt2d( u1, v1 );
+            else
+              anewpoint = gp_Pnt2d( u2, v2 );
+            
+            Standard_Integer aneighbourpointindex1 = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
+            const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneighbourpointindex1);
+            Standard_Real nU1, nV1;
+            
+            if(surfit == 0)
+              aNeighbourPoint.ParametersOnS1(nU1, nV1);
+            else
+              aNeighbourPoint.ParametersOnS2(nU1, nV1);
+            gp_Pnt2d ap1(nU1, nV1);
+            gp_Pnt2d ap2;
+
+
+            if ( aZoneIndex ) {
+              // exclude point from a tangent zone
+              anewpoint = AdjustByNeighbour( ap1, anewpoint, aGASurface );
+              gp_Pnt2d aPZone = (surfit == 0) ? aTanZoneS1->Value(aZoneIndex) : aTanZoneS2->Value(aZoneIndex);
+              Standard_Real aZoneRadius = aTanZoneRadius->Value(aZoneIndex);
+
+              if ( FindPoint(ap1, anewpoint, umin, umax, vmin, vmax, 
+                             aPZone, aZoneRadius, aGASurface, ap2) ) {
+                anewpoint = ap2;
+                found = Standard_True;
+              }
+            }
+            else if ( aGASurface->IsUPeriodic() || aGASurface->IsVPeriodic() ) {
+              // re-compute point near boundary if shifted on a period
+              ap2 = AdjustByNeighbour( ap1, anewpoint, aGASurface );
+
+              if ( ( ap2.X() < umin ) || ( ap2.X() > umax ) ||
+                  ( ap2.Y() < vmin ) || ( ap2.Y() > vmax ) ) {
+                found = FindPoint(ap1, ap2, umin, umax, vmin, vmax, anewpoint);
+              }
+              else {
+                anewpoint = ap2;
+                aNewP.SetValue( (surfit == 0), anewpoint.X(), anewpoint.Y() );
+              }
+            }
+          }
+          else {
+
+            Standard_Integer aneighbourpointindex1 = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
+            const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneighbourpointindex1);
+            Standard_Real nU1, nV1;
+
+            if(surfit == 0)
+              aNeighbourPoint.ParametersOnS1(nU1, nV1);
+            else
+              aNeighbourPoint.ParametersOnS2(nU1, nV1);
+            gp_Pnt2d ap1(nU1, nV1);
+            gp_Pnt2d ap2(nU1, nV1);
+            Standard_Integer aneighbourpointindex2 = aneighbourpointindex1;
+
+            while((aneighbourpointindex2 <= aListOfIndex.Last()) && (aneighbourpointindex2 >= aListOfIndex.First())) {
+              aneighbourpointindex2 = (j == 0) ? (aneighbourpointindex2 + 1) : (aneighbourpointindex2 - 1);
+              const IntSurf_PntOn2S& aPrevNeighbourPoint = theWLine->Point(aneighbourpointindex2);
+              Standard_Real nU2, nV2;
+
+              if(surfit == 0)
+                aPrevNeighbourPoint.ParametersOnS1(nU2, nV2);
+              else
+                aPrevNeighbourPoint.ParametersOnS2(nU2, nV2);
+              ap2.SetX(nU2);
+              ap2.SetY(nV2);
+
+              if(ap1.SquareDistance(ap2) > (gp::Resolution() * gp::Resolution())) {
+                break;
+              }
+            }  
+            found = FindPoint(ap2, ap1, umin, umax, vmin, vmax, anewpoint);
+          }
+
+          if(found) {
+            // check point
+            Standard_Real aCriteria = BRep_Tool::Tolerance(theFace1) + BRep_Tool::Tolerance(theFace2);
+            GeomAPI_ProjectPointOnSurf& aProjector = 
+              (surfit == 0) ? aContext->ProjPS(theFace2) : aContext->ProjPS(theFace1);
+            Handle(GeomAdaptor_HSurface) aSurface = (surfit == 0) ? theSurface1 : theSurface2;
+
+            Handle(GeomAdaptor_HSurface) aSurfaceOther = (surfit == 0) ? theSurface2 : theSurface1;
+
+            gp_Pnt aP3d = aSurface->Value(anewpoint.X(), anewpoint.Y());
+            aProjector.Perform(aP3d);
+
+            if(aProjector.IsDone()) {
+              if(aProjector.LowerDistance() < aCriteria) {
+                Standard_Real foundU = U, foundV = V;
+                aProjector.LowerDistanceParameters(foundU, foundV);
+
+                //Correction of projected coordinates. Begin
+                //Note, it may be shifted on a period
+                Standard_Integer aneindex1 = (j == 0) ? aListOfIndex.First() : aListOfIndex.Last();
+                const IntSurf_PntOn2S& aNeighbourPoint = theWLine->Point(aneindex1);
+                Standard_Real nUn, nVn;
+                
+                if(surfit == 0)
+                  aNeighbourPoint.ParametersOnS2(nUn, nVn);
+                else
+                  aNeighbourPoint.ParametersOnS1(nUn, nVn);
+                gp_Pnt2d aNeighbour2d(nUn, nVn);
+                gp_Pnt2d anAdjustedPoint = AdjustByNeighbour( aNeighbour2d, gp_Pnt2d(foundU, foundV), aSurfaceOther );
+                foundU = anAdjustedPoint.X();
+                foundV = anAdjustedPoint.Y();
+
+                if ( ( anAdjustedPoint.X() < umin ) && ( anAdjustedPoint.X() > umax ) &&
+                    ( anAdjustedPoint.Y() < vmin ) && ( anAdjustedPoint.Y() > vmax ) ) {
+                  // attempt to roughly re-compute point
+                  foundU = ( foundU < umin ) ? umin : foundU;
+                  foundU = ( foundU > umax ) ? umax : foundU;
+                  foundV = ( foundV < vmin ) ? vmin : foundV;
+                  foundV = ( foundV > vmax ) ? vmax : foundV;
+
+                  GeomAPI_ProjectPointOnSurf& aProjector2 = 
+                    (surfit == 0) ? aContext->ProjPS(theFace1) : aContext->ProjPS(theFace2);
+
+                  aP3d = aSurfaceOther->Value(foundU, foundV);
+                  aProjector2.Perform(aP3d);
+                  
+                  if(aProjector2.IsDone()) {
+                    if(aProjector2.LowerDistance() < aCriteria) {
+                      Standard_Real foundU2 = anewpoint.X(), foundV2 = anewpoint.Y();
+                      aProjector2.LowerDistanceParameters(foundU2, foundV2);
+                      anewpoint.SetX(foundU2);
+                      anewpoint.SetY(foundV2);
+                    }
+                  }
+                }
+                //Correction of projected coordinates. End
+
+                if(surfit == 0)
+                  aNewP.SetValue(aP3d, anewpoint.X(), anewpoint.Y(), foundU, foundV);
+                else
+                  aNewP.SetValue(aP3d, foundU, foundV, anewpoint.X(), anewpoint.Y());
+              }
+            }
+          }
+        }
       }
       aSeqOfPntOn2S->Add(aNewP);
       aListOfFLIndex.Append(aSeqOfPntOn2S->NbPoints());
@@ -4173,146 +4128,146 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
     //
     for(i = 1; i <= nblines; i++) {
       if(anArrayOfLineType.Value(i) != 0) {
-       continue;
+        continue;
       }
       const TColStd_ListOfInteger& aListOfIndex = anArrayOfLines.Value(i);
 
       if(aListOfIndex.Extent() < 2) {
-       continue;
+        continue;
       }
       const TColStd_ListOfInteger& aListOfFLIndex = anArrayOfLineEnds.Value(i);
       Standard_Boolean bhasfirstpoint = (aListOfFLIndex.Extent() == 2);
       Standard_Boolean bhaslastpoint = (aListOfFLIndex.Extent() == 2);
 
       if(!bhasfirstpoint && !aListOfFLIndex.IsEmpty()) {
-       bhasfirstpoint = (i != 1);
+        bhasfirstpoint = (i != 1);
       }
 
       if(!bhaslastpoint && !aListOfFLIndex.IsEmpty()) {
-       bhaslastpoint = (i != nblines);
+        bhaslastpoint = (i != nblines);
       }
       Standard_Boolean bIsFirstInside = ((ifprm >= aListOfIndex.First()) && (ifprm <= aListOfIndex.Last()));
       Standard_Boolean bIsLastInside =  ((ilprm >= aListOfIndex.First()) && (ilprm <= aListOfIndex.Last()));
 
       if(!bIsFirstInside && !bIsLastInside) {
-       if((ifprm < aListOfIndex.First()) && (ilprm > aListOfIndex.Last())) {
-         // append whole line, and boundaries if neccesary
-         if(bhasfirstpoint) {
-           const IntSurf_PntOn2S& aP = aSeqOfPntOn2S->Value(aListOfFLIndex.First());
-           aLineOn2S->Add(aP);
-         }
-         TColStd_ListIteratorOfListOfInteger anIt(aListOfIndex);
-
-         for(; anIt.More(); anIt.Next()) {
-           const IntSurf_PntOn2S& aP = theWLine->Point(anIt.Value());
-           aLineOn2S->Add(aP);
-         }
-
-         if(bhaslastpoint) {
-           const IntSurf_PntOn2S& aP = aSeqOfPntOn2S->Value(aListOfFLIndex.Last());
-           aLineOn2S->Add(aP);
-         }
-
-         // check end of split line (end is almost always)
-         Standard_Integer aneighbour = i + 1;
-         Standard_Boolean bIsEndOfLine = Standard_True;
-
-         if(aneighbour <= nblines) {
-           const TColStd_ListOfInteger& aListOfNeighbourIndex = anArrayOfLines.Value(aneighbour);
-
-           if((anArrayOfLineType.Value(aneighbour) != 0) &&
-              (aListOfNeighbourIndex.IsEmpty())) {
-             bIsEndOfLine = Standard_False;
-           }
-         }
-
-         if(bIsEndOfLine) {
-           if(aLineOn2S->NbPoints() > 1) {
-             Handle(IntPatch_WLine) aNewWLine = 
-               new IntPatch_WLine(aLineOn2S, Standard_False);
-             theNewLines.Append(aNewWLine);
-           }
-           aLineOn2S = new IntSurf_LineOn2S();
-         }
-       }
-       continue;
+        if((ifprm < aListOfIndex.First()) && (ilprm > aListOfIndex.Last())) {
+          // append whole line, and boundaries if neccesary
+          if(bhasfirstpoint) {
+            const IntSurf_PntOn2S& aP = aSeqOfPntOn2S->Value(aListOfFLIndex.First());
+            aLineOn2S->Add(aP);
+          }
+          TColStd_ListIteratorOfListOfInteger anIt(aListOfIndex);
+
+          for(; anIt.More(); anIt.Next()) {
+            const IntSurf_PntOn2S& aP = theWLine->Point(anIt.Value());
+            aLineOn2S->Add(aP);
+          }
+
+          if(bhaslastpoint) {
+            const IntSurf_PntOn2S& aP = aSeqOfPntOn2S->Value(aListOfFLIndex.Last());
+            aLineOn2S->Add(aP);
+          }
+
+          // check end of split line (end is almost always)
+          Standard_Integer aneighbour = i + 1;
+          Standard_Boolean bIsEndOfLine = Standard_True;
+
+          if(aneighbour <= nblines) {
+            const TColStd_ListOfInteger& aListOfNeighbourIndex = anArrayOfLines.Value(aneighbour);
+
+            if((anArrayOfLineType.Value(aneighbour) != 0) &&
+               (aListOfNeighbourIndex.IsEmpty())) {
+              bIsEndOfLine = Standard_False;
+            }
+          }
+
+          if(bIsEndOfLine) {
+            if(aLineOn2S->NbPoints() > 1) {
+              Handle(IntPatch_WLine) aNewWLine = 
+                new IntPatch_WLine(aLineOn2S, Standard_False);
+              theNewLines.Append(aNewWLine);
+            }
+            aLineOn2S = new IntSurf_LineOn2S();
+          }
+        }
+        continue;
       }
       // end if(!bIsFirstInside && !bIsLastInside)
 
       if(bIsFirstInside && bIsLastInside) {
-       // append inside points between ifprm and ilprm
-       TColStd_ListIteratorOfListOfInteger anIt(aListOfIndex);
-
-       for(; anIt.More(); anIt.Next()) {
-         if((anIt.Value() < ifprm) || (anIt.Value() > ilprm))
-           continue;
-         const IntSurf_PntOn2S& aP = theWLine->Point(anIt.Value());
-         aLineOn2S->Add(aP);
-       }
+        // append inside points between ifprm and ilprm
+        TColStd_ListIteratorOfListOfInteger anIt(aListOfIndex);
+
+        for(; anIt.More(); anIt.Next()) {
+          if((anIt.Value() < ifprm) || (anIt.Value() > ilprm))
+            continue;
+          const IntSurf_PntOn2S& aP = theWLine->Point(anIt.Value());
+          aLineOn2S->Add(aP);
+        }
       }
       else {
 
-       if(bIsFirstInside) {
-         // append points from ifprm to last point + boundary point
-         TColStd_ListIteratorOfListOfInteger anIt(aListOfIndex);
-
-         for(; anIt.More(); anIt.Next()) {
-           if(anIt.Value() < ifprm)
-             continue;
-           const IntSurf_PntOn2S& aP = theWLine->Point(anIt.Value());
-           aLineOn2S->Add(aP);
-         }
-
-         if(bhaslastpoint) {
-           const IntSurf_PntOn2S& aP = aSeqOfPntOn2S->Value(aListOfFLIndex.Last());
-           aLineOn2S->Add(aP);
-         }
-         // check end of split line (end is almost always)
-         Standard_Integer aneighbour = i + 1;
-         Standard_Boolean bIsEndOfLine = Standard_True;
-
-         if(aneighbour <= nblines) {
-           const TColStd_ListOfInteger& aListOfNeighbourIndex = anArrayOfLines.Value(aneighbour);
-
-           if((anArrayOfLineType.Value(aneighbour) != 0) &&
-              (aListOfNeighbourIndex.IsEmpty())) {
-             bIsEndOfLine = Standard_False;
-           }
-         }
-
-         if(bIsEndOfLine) {
-           if(aLineOn2S->NbPoints() > 1) {
-             Handle(IntPatch_WLine) aNewWLine = 
-               new IntPatch_WLine(aLineOn2S, Standard_False);
-             theNewLines.Append(aNewWLine);
-           }
-           aLineOn2S = new IntSurf_LineOn2S();
-         }
-       }
-       // end if(bIsFirstInside)
-
-       if(bIsLastInside) {
-         // append points from first boundary point to ilprm
-         if(bhasfirstpoint) {
-           const IntSurf_PntOn2S& aP = aSeqOfPntOn2S->Value(aListOfFLIndex.First());
-           aLineOn2S->Add(aP);
-         }
-         TColStd_ListIteratorOfListOfInteger anIt(aListOfIndex);
-
-         for(; anIt.More(); anIt.Next()) {
-           if(anIt.Value() > ilprm)
-             continue;
-           const IntSurf_PntOn2S& aP = theWLine->Point(anIt.Value());
-           aLineOn2S->Add(aP);
-         }
-       }
-       //end if(bIsLastInside)
+        if(bIsFirstInside) {
+          // append points from ifprm to last point + boundary point
+          TColStd_ListIteratorOfListOfInteger anIt(aListOfIndex);
+
+          for(; anIt.More(); anIt.Next()) {
+            if(anIt.Value() < ifprm)
+              continue;
+            const IntSurf_PntOn2S& aP = theWLine->Point(anIt.Value());
+            aLineOn2S->Add(aP);
+          }
+
+          if(bhaslastpoint) {
+            const IntSurf_PntOn2S& aP = aSeqOfPntOn2S->Value(aListOfFLIndex.Last());
+            aLineOn2S->Add(aP);
+          }
+          // check end of split line (end is almost always)
+          Standard_Integer aneighbour = i + 1;
+          Standard_Boolean bIsEndOfLine = Standard_True;
+
+          if(aneighbour <= nblines) {
+            const TColStd_ListOfInteger& aListOfNeighbourIndex = anArrayOfLines.Value(aneighbour);
+
+            if((anArrayOfLineType.Value(aneighbour) != 0) &&
+               (aListOfNeighbourIndex.IsEmpty())) {
+              bIsEndOfLine = Standard_False;
+            }
+          }
+
+          if(bIsEndOfLine) {
+            if(aLineOn2S->NbPoints() > 1) {
+              Handle(IntPatch_WLine) aNewWLine = 
+                new IntPatch_WLine(aLineOn2S, Standard_False);
+              theNewLines.Append(aNewWLine);
+            }
+            aLineOn2S = new IntSurf_LineOn2S();
+          }
+        }
+        // end if(bIsFirstInside)
+
+        if(bIsLastInside) {
+          // append points from first boundary point to ilprm
+          if(bhasfirstpoint) {
+            const IntSurf_PntOn2S& aP = aSeqOfPntOn2S->Value(aListOfFLIndex.First());
+            aLineOn2S->Add(aP);
+          }
+          TColStd_ListIteratorOfListOfInteger anIt(aListOfIndex);
+
+          for(; anIt.More(); anIt.Next()) {
+            if(anIt.Value() > ilprm)
+              continue;
+            const IntSurf_PntOn2S& aP = theWLine->Point(anIt.Value());
+            aLineOn2S->Add(aP);
+          }
+        }
+        //end if(bIsLastInside)
       }
     }
 
     if(aLineOn2S->NbPoints() > 1) {
       Handle(IntPatch_WLine) aNewWLine = 
-       new IntPatch_WLine(aLineOn2S, Standard_False);
+        new IntPatch_WLine(aLineOn2S, Standard_False);
       theNewLines.Append(aNewWLine);
     }
   }
@@ -4327,12 +4282,12 @@ Standard_Boolean DecompositionOfWLine(const Handle(IntPatch_WLine)& theWLine,
 //                  does not lay on any boundary of given faces
 // ------------------------------------------------------------------------------------------------
 Standard_Boolean ParameterOutOfBoundary(const Standard_Real       theParameter, 
-                                       const Handle(Geom_Curve)& theCurve, 
-                                       const TopoDS_Face&        theFace1, 
-                                       const TopoDS_Face&        theFace2,
-                                       const Standard_Real       theOtherParameter,
-                                       const Standard_Boolean    bIncreasePar,
-                                       Standard_Real&            theNewParameter,
+                                        const Handle(Geom_Curve)& theCurve, 
+                                        const TopoDS_Face&        theFace1, 
+                                        const TopoDS_Face&        theFace2,
+                                        const Standard_Real       theOtherParameter,
+                                        const Standard_Boolean    bIncreasePar,
+                                        Standard_Real&            theNewParameter,
                                         const Handle(IntTools_Context)& aContext)
 {
   Standard_Boolean bIsComputed = Standard_False;
@@ -4373,10 +4328,10 @@ Standard_Boolean ParameterOutOfBoundary(const Standard_Real       theParameter,
 
     if(aState != TopAbs_ON) {
       aPrj2.Perform(aPCurrent);
-               
+                
       if(aPrj2.IsDone()) {
-       aPrj2.LowerDistanceParameters(U, V);
-       aState = aContext->StatePointFace(theFace2, gp_Pnt2d(U, V));
+        aPrj2.LowerDistanceParameters(U, V);
+        aState = aContext->StatePointFace(theFace2, gp_Pnt2d(U, V));
       }
     }
 
@@ -4392,11 +4347,11 @@ Standard_Boolean ParameterOutOfBoundary(const Standard_Real       theParameter,
 
     if(bIncreasePar) {
       if(acurpar >= theOtherParameter)
-       theNewParameter = theOtherParameter;
+        theNewParameter = theOtherParameter;
     }
     else {
       if(acurpar <= theOtherParameter)
-       theNewParameter = theOtherParameter;
+        theNewParameter = theOtherParameter;
     }
   }
   return bIsComputed;
@@ -4445,7 +4400,7 @@ Standard_Boolean IsCurveValid(Handle(Geom2d_Curve)& thePCurve)
 //purpose  : for bug 20964 only
 //=======================================================================
 Standard_Boolean ApproxWithPCurves(const gp_Cylinder& theCyl, 
-                                  const gp_Sphere& theSph)
+                                   const gp_Sphere& theSph)
 {
   Standard_Boolean bRes = Standard_True;
   Standard_Real R1 = theCyl.Radius(), R2 = theSph.Radius();
@@ -4505,13 +4460,13 @@ Standard_Boolean ApproxWithPCurves(const gp_Cylinder& theCyl,
 //purpose  : 
 //=======================================================================
 void  PerformPlanes(const Handle(GeomAdaptor_HSurface)& theS1, 
-                   const Handle(GeomAdaptor_HSurface)& theS2, 
-                   const Standard_Real TolAng, 
-                   const Standard_Real TolTang, 
-                   const Standard_Boolean theApprox1,
-                   const Standard_Boolean theApprox2,
-                   IntTools_SequenceOfCurves& theSeqOfCurve, 
-                   Standard_Boolean& theTangentFaces)
+                    const Handle(GeomAdaptor_HSurface)& theS2, 
+                    const Standard_Real TolAng, 
+                    const Standard_Real TolTang, 
+                    const Standard_Boolean theApprox1,
+                    const Standard_Boolean theApprox2,
+                    IntTools_SequenceOfCurves& theSeqOfCurve, 
+                    Standard_Boolean& theTangentFaces)
 {
 
   gp_Pln aPln1 = theS1->Surface().Plane();
@@ -4602,8 +4557,8 @@ void  PerformPlanes(const Handle(GeomAdaptor_HSurface)& theS1,
 //purpose  : 
 //=======================================================================
 static inline Standard_Boolean INTER(const Standard_Real d1, 
-                                    const Standard_Real d2, 
-                                    const Standard_Real tol) 
+                                     const Standard_Real d2, 
+                                     const Standard_Real tol) 
 {
   return (d1 > tol && d2 < -tol) || 
          (d1 < -tol && d2 > tol) ||
@@ -4611,16 +4566,16 @@ static inline Standard_Boolean INTER(const Standard_Real d1,
          ((d2 <= tol && d2 >= -tol) && (d1 > tol || d1 < -tol));
 }
 static inline Standard_Boolean COINC(const Standard_Real d1, 
-                                    const Standard_Real d2, 
-                                    const Standard_Real tol) 
+                                     const Standard_Real d2, 
+                                     const Standard_Real tol) 
 {
   return (d1 <= tol && d1 >= -tol) && (d2 <= tol && d2 >= -tol);
 }
 Standard_Boolean ClassifyLin2d(const Handle(GeomAdaptor_HSurface)& theS, 
-                              const gp_Lin2d& theLin2d, 
-                              const Standard_Real theTol,
-                              Standard_Real& theP1, 
-                              Standard_Real& theP2)
+                               const gp_Lin2d& theLin2d, 
+                               const Standard_Real theTol,
+                               Standard_Real& theP1, 
+                               Standard_Real& theP2)
 
 {
   Standard_Real xmin, xmax, ymin, ymax, d1, d2, A, B, C;
@@ -4752,10 +4707,10 @@ Standard_Boolean ClassifyLin2d(const Handle(GeomAdaptor_HSurface)& theS,
 //purpose  : 
 //=======================================================================
 void ApproxParameters(const Handle(GeomAdaptor_HSurface)& aHS1,
-                     const Handle(GeomAdaptor_HSurface)& aHS2,
-                     Standard_Integer& iDegMin,
-                     Standard_Integer& iDegMax,
-                     Standard_Integer& iNbIter)
+                      const Handle(GeomAdaptor_HSurface)& aHS2,
+                      Standard_Integer& iDegMin,
+                      Standard_Integer& iDegMax,
+                      Standard_Integer& iNbIter)
 
 {
   GeomAbs_SurfaceType aTS1, aTS2;
@@ -4800,7 +4755,7 @@ void ApproxParameters(const Handle(GeomAdaptor_HSurface)& aHS1,
 //purpose  : 
 //=======================================================================
 void Tolerances(const Handle(GeomAdaptor_HSurface)& aHS1,
-               const Handle(GeomAdaptor_HSurface)& aHS2,
+                const Handle(GeomAdaptor_HSurface)& aHS2,
                 Standard_Real& aTolTang)
 {
   GeomAbs_SurfaceType aTS1, aTS2;
@@ -4837,7 +4792,7 @@ void Tolerances(const Handle(GeomAdaptor_HSurface)& aHS1,
 //purpose  : 
 //=======================================================================
 Standard_Boolean SortTypes(const GeomAbs_SurfaceType aType1,
-                          const GeomAbs_SurfaceType aType2)
+                           const GeomAbs_SurfaceType aType2)
 {
   Standard_Boolean bRet;
   Standard_Integer aI1, aI2;
@@ -4916,7 +4871,7 @@ void DumpWLine(const Handle(IntPatch_WLine)& aWLine)
     //
     printf("point p_%d %lf %lf %lf\n", i, aX, aY, aZ);
     //printf("point p_%d %20.15lf %20.15lf %20.15lf %20.15lf %20.15lf %20.15lf %20.15lf\n",
-       //   i, aX, aY, aZ, aU1, aV1, aU2, aV2);
+        //   i, aX, aY, aZ, aU1, aV1, aU2, aV2);
   }
 }
 #endif
@@ -4940,10 +4895,10 @@ void RefineVector(gp_Vec2d& aV2D)
     aNum=fabs(aC[k]);
     if (aNum>aR1 && aNum<aR2) {
       if (aC[k]<0.) {
-       aC[k]=-1.;
-      }          
+        aC[k]=-1.;
+      }          
       else {
-       aC[k]=1.;
+        aC[k]=1.;
       }
       aC[m]=0.;
       break;
@@ -4956,15 +4911,15 @@ void RefineVector(gp_Vec2d& aV2D)
 //purpose  : 
 //=======================================================================
 Standard_Real FindMaxSquareDistance (const Standard_Real aT1,
-                                    const Standard_Real aT2,
-                                    const Standard_Real aEps,
-                                    const Handle(Geom_Curve)& aC3D,
-                                    const Handle(Geom2d_Curve)& aC2D1,
-                                    const Handle(Geom2d_Curve)& aC2D2,
-                                    const Handle(GeomAdaptor_HSurface)& myHS1,
-                                    const Handle(GeomAdaptor_HSurface)& myHS2,
-                                    const TopoDS_Face& myFace1,
-                                    const TopoDS_Face& myFace2,
+                                     const Standard_Real aT2,
+                                     const Standard_Real aEps,
+                                     const Handle(Geom_Curve)& aC3D,
+                                     const Handle(Geom2d_Curve)& aC2D1,
+                                     const Handle(Geom2d_Curve)& aC2D2,
+                                     const Handle(GeomAdaptor_HSurface)& myHS1,
+                                     const Handle(GeomAdaptor_HSurface)& myHS2,
+                                     const TopoDS_Face& myFace1,
+                                     const TopoDS_Face& myFace2,
                                      const Handle(IntTools_Context)& myContext)
 {
   Standard_Real aA, aB, aCf, aX1, aX2, aF1, aF2, aX, aF;
@@ -4974,17 +4929,17 @@ Standard_Real FindMaxSquareDistance (const Standard_Real aT1,
   aB=aT2;
   aX1=aB-(aB-aA)/aCf;  
   aF1=MaxSquareDistance(aX1, 
-                       aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
+                        aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
   aX2=aA+(aB-aA)/aCf;
   aF2=MaxSquareDistance(aX2, 
-                       aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
+                        aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
   //
   for(;;) {
     //
     if (fabs(aA-aB)<aEps) {
       aX=0.5*(aA+aB);
       aF=MaxSquareDistance(aX, 
-                       aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
+                        aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
       break;
     }
     if (aF1<aF2){
@@ -4993,7 +4948,7 @@ Standard_Real FindMaxSquareDistance (const Standard_Real aT1,
       aF1=aF2;
       aX2=aA+(aB-aA)/aCf;
       aF2=MaxSquareDistance(aX2, 
-                           aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
+                            aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
       
     }
     else {
@@ -5002,7 +4957,7 @@ Standard_Real FindMaxSquareDistance (const Standard_Real aT1,
       aF2=aF1;
       aX1=aB-(aB-aA)/aCf; 
       aF1=MaxSquareDistance(aX1, 
-                           aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
+                            aC3D, aC2D1, aC2D2, myHS1, myHS2, myFace1, myFace2, myContext);
     }
   }
   return aF;
@@ -5012,13 +4967,13 @@ Standard_Real FindMaxSquareDistance (const Standard_Real aT1,
 //purpose  : 
 //=======================================================================
 Standard_Real MaxSquareDistance (const Standard_Real aT,
-                                const Handle(Geom_Curve)& aC3D,
-                                const Handle(Geom2d_Curve)& aC2D1,
-                                const Handle(Geom2d_Curve)& aC2D2,
-                                const Handle(GeomAdaptor_HSurface) myHS1,
-                                const Handle(GeomAdaptor_HSurface) myHS2,
-                                const TopoDS_Face& aF1,
-                                const TopoDS_Face& aF2,
+                                 const Handle(Geom_Curve)& aC3D,
+                                 const Handle(Geom2d_Curve)& aC2D1,
+                                 const Handle(Geom2d_Curve)& aC2D2,
+                                 const Handle(GeomAdaptor_HSurface) myHS1,
+                                 const Handle(GeomAdaptor_HSurface) myHS2,
+                                 const TopoDS_Face& aF1,
+                                 const TopoDS_Face& aF2,
                                  const Handle(IntTools_Context)& aCtx)
 {
   Standard_Boolean bIsDone;
@@ -5045,7 +5000,7 @@ Standard_Real MaxSquareDistance (const Standard_Real aT,
       aGHS->D0(aU, aV, aPS);
       aD2=aP.SquareDistance(aPS);
       if (aD2>aD2Max) {
-       aD2Max=aD2;
+        aD2Max=aD2;
       }
     }
     //
@@ -5058,7 +5013,7 @@ Standard_Real MaxSquareDistance (const Standard_Real aT,
       aGHS->D0(aU, aV, aPS);
       aD2=aP.SquareDistance(aPS);
       if (aD2>aD2Max) {
-       aD2Max=aD2;
+        aD2Max=aD2;
       }
     }
   }
index af9a476..690c3bf 100644 (file)
 #include <ElCLib.hxx>
 #include <GeomAbs_SurfaceType.hxx>
 #include <TColStd_IndexedMapOfInteger.hxx>
+#include <IntTools_Tools.hxx>
 
 static
   void Parameters(const Handle(GeomAdaptor_HSurface)& myHS1,
-                 const gp_Pnt& Ptref,
-                 Standard_Real& U1,
-                 Standard_Real& V1);
+                  const gp_Pnt& Ptref,
+                  Standard_Real& U1,
+                  Standard_Real& V1);
 static
   void Parameters(const Handle(GeomAdaptor_HSurface)& myHS1,
-                 const Handle(GeomAdaptor_HSurface)& myHS2,
-                 const gp_Pnt& Ptref,
-                 Standard_Real& U1,
-                 Standard_Real& V1,
-                 Standard_Real& U2,
-                 Standard_Real& V2);
+                  const Handle(GeomAdaptor_HSurface)& myHS2,
+                  const gp_Pnt& Ptref,
+                  Standard_Real& U1,
+                  Standard_Real& V1,
+                  Standard_Real& U2,
+                  Standard_Real& V2);
 
 static 
   void GLinePoint(const IntPatch_IType typl,
-                 const Handle(IntPatch_GLine)& GLine,
-                 const Standard_Real aT,
-                 gp_Pnt& aP);
+                  const Handle(IntPatch_GLine)& GLine,
+                  const Standard_Real aT,
+                  gp_Pnt& aP);
 static
   void Recadre(const Handle(GeomAdaptor_HSurface)& myHS1,
-              const Handle(GeomAdaptor_HSurface)& myHS2,
-              Standard_Real& u1,
-              Standard_Real& v1,
-              Standard_Real& u2,
-              Standard_Real& v2);
+               const Handle(GeomAdaptor_HSurface)& myHS2,
+               Standard_Real& u1,
+               Standard_Real& v1,
+               Standard_Real& u2,
+               Standard_Real& v2);
 
 //=======================================================================
 //function : Perform
@@ -89,18 +90,18 @@ void IntTools_LineConstructor::Perform(const Handle(IntPatch_Line)& L)
       firstp = GeomInt_LineTool::Vertex(L,i).ParameterOnLine();
       lastp =  GeomInt_LineTool::Vertex(L,i+1).ParameterOnLine();
       if(firstp!=lastp)      {
-       const Standard_Real pmid = (firstp+lastp)*0.5;
-       const gp_Pnt Pmid = ALine->Value(pmid);
-       Parameters(myHS1,myHS2,Pmid,u1,v1,u2,v2);
-       Recadre(myHS1,myHS2,u1,v1,u2,v2);
-       const TopAbs_State in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
-       if(in1 !=  TopAbs_OUT) {
-         const TopAbs_State in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
-         if(in2 != TopAbs_OUT) { 
-           seqp.Append(firstp);
-           seqp.Append(lastp);
-         }
-       }
+        const Standard_Real pmid = (firstp+lastp)*0.5;
+        const gp_Pnt Pmid = ALine->Value(pmid);
+        Parameters(myHS1,myHS2,Pmid,u1,v1,u2,v2);
+        Recadre(myHS1,myHS2,u1,v1,u2,v2);
+        const TopAbs_State in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
+        if(in1 !=  TopAbs_OUT) {
+          const TopAbs_State in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
+          if(in2 != TopAbs_OUT) { 
+            seqp.Append(firstp);
+            seqp.Append(lastp);
+          }
+        }
       }
     }
     done = Standard_True;
@@ -115,42 +116,42 @@ void IntTools_LineConstructor::Perform(const Handle(IntPatch_Line)& L)
       firstp = GeomInt_LineTool::Vertex(L,i).ParameterOnLine();
       lastp =  GeomInt_LineTool::Vertex(L,i+1).ParameterOnLine();
       if(firstp!=lastp) { 
-       if(lastp != firstp+1)  {
-         const Standard_Integer pmid = (Standard_Integer )( (firstp+lastp)/2);
-         const IntSurf_PntOn2S& Pmid = WLine->Point(pmid);
-         Pmid.Parameters(u1,v1,u2,v2);
-         Recadre(myHS1,myHS2,u1,v1,u2,v2);
-         const TopAbs_State in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
-         if(in1 !=  TopAbs_OUT) {   
-           const TopAbs_State in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
-           if(in2 != TopAbs_OUT) {   
-             seqp.Append(firstp);
-             seqp.Append(lastp);
-           }
-         }
-       }
-       else {
-         const IntSurf_PntOn2S& Pfirst = WLine->Point((Standard_Integer)(firstp));
-         Pfirst.Parameters(u1,v1,u2,v2);
-         Recadre(myHS1,myHS2,u1,v1,u2,v2);
-         TopAbs_State in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
-         if(in1 !=  TopAbs_OUT) {  //-- !=ON donne Pb 
-           TopAbs_State in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
-           if(in2 != TopAbs_OUT) { //-- !=ON  
-             const IntSurf_PntOn2S& Plast = WLine->Point((Standard_Integer)(lastp));
-             Plast.Parameters(u1,v1,u2,v2);
-             Recadre(myHS1,myHS2,u1,v1,u2,v2);
-             in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
-             if(in1 !=  TopAbs_OUT) {  //-- !=ON donne Pb 
-               in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
-               if(in2 != TopAbs_OUT) {
-                 seqp.Append(firstp);
-                 seqp.Append(lastp);
-               }
-             }
-           }
-         }
-       }
+        if(lastp != firstp+1)  {
+          const Standard_Integer pmid = (Standard_Integer )( (firstp+lastp)/2);
+          const IntSurf_PntOn2S& Pmid = WLine->Point(pmid);
+          Pmid.Parameters(u1,v1,u2,v2);
+          Recadre(myHS1,myHS2,u1,v1,u2,v2);
+          const TopAbs_State in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
+          if(in1 !=  TopAbs_OUT) {   
+            const TopAbs_State in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
+            if(in2 != TopAbs_OUT) {   
+              seqp.Append(firstp);
+              seqp.Append(lastp);
+            }
+          }
+        }
+        else {
+          const IntSurf_PntOn2S& Pfirst = WLine->Point((Standard_Integer)(firstp));
+          Pfirst.Parameters(u1,v1,u2,v2);
+          Recadre(myHS1,myHS2,u1,v1,u2,v2);
+          TopAbs_State in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
+          if(in1 !=  TopAbs_OUT) {  //-- !=ON donne Pb 
+            TopAbs_State in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
+            if(in2 != TopAbs_OUT) { //-- !=ON  
+              const IntSurf_PntOn2S& Plast = WLine->Point((Standard_Integer)(lastp));
+              Plast.Parameters(u1,v1,u2,v2);
+              Recadre(myHS1,myHS2,u1,v1,u2,v2);
+              in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
+              if(in1 !=  TopAbs_OUT) {  //-- !=ON donne Pb 
+                in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
+                if(in2 != TopAbs_OUT) {
+                  seqp.Append(firstp);
+                  seqp.Append(lastp);
+                }
+              }
+            }
+          }
+        }
       }
     }
     //
@@ -173,47 +174,47 @@ void IntTools_LineConstructor::Perform(const Handle(IntPatch_Line)& L)
       //
       bCond=Standard_False;
       if (aST1==GeomAbs_Plane) {
-       if (aST2==GeomAbs_SurfaceOfExtrusion || 
-           aST2==GeomAbs_SurfaceOfRevolution) {//+zft
-         bCond=!bCond;
-       }
+        if (aST2==GeomAbs_SurfaceOfExtrusion || 
+            aST2==GeomAbs_SurfaceOfRevolution) {//+zft
+          bCond=!bCond;
+        }
       }
       else if (aST2==GeomAbs_Plane) {
-       if (aST1==GeomAbs_SurfaceOfExtrusion || 
-           aST1==GeomAbs_SurfaceOfRevolution) {//+zft
-         bCond=!bCond;
-       }
+        if (aST1==GeomAbs_SurfaceOfExtrusion || 
+            aST1==GeomAbs_SurfaceOfRevolution) {//+zft
+          bCond=!bCond;
+        }
       }
       //
       if (bCond) {
-       Standard_Integer aNb, anIndex, aNbTmp, jx;
-       TColStd_IndexedMapOfInteger aMap;
-       TColStd_SequenceOfReal aSeqTmp;
-       //
-       aNb=seqp.Length();
-       for(i=1; i<=aNb; ++i) {
-         lastp =seqp(i);
-         anIndex=(Standard_Integer)lastp;
-         if (!aMap.Contains(anIndex)){
-           aMap.Add(anIndex);
-           aSeqTmp.Append(lastp);
-         }
-         else {
-           aNbTmp=aSeqTmp.Length();
-           aSeqTmp.Remove(aNbTmp);
-         }
-       }
-       //
-       seqp.Clear();
-       //
-       aNb=aSeqTmp.Length()/2;
-       for(i=1; i<=aNb;++i) {
-         jx=2*i;
-         firstp=aSeqTmp(jx-1);
-         lastp =aSeqTmp(jx);
-         seqp.Append(firstp);
-         seqp.Append(lastp);
-       }
+        Standard_Integer aNb, anIndex, aNbTmp, jx;
+        TColStd_IndexedMapOfInteger aMap;
+        TColStd_SequenceOfReal aSeqTmp;
+        //
+        aNb=seqp.Length();
+        for(i=1; i<=aNb; ++i) {
+          lastp =seqp(i);
+          anIndex=(Standard_Integer)lastp;
+          if (!aMap.Contains(anIndex)){
+            aMap.Add(anIndex);
+            aSeqTmp.Append(lastp);
+          }
+          else {
+            aNbTmp=aSeqTmp.Length();
+            aSeqTmp.Remove(aNbTmp);
+          }
+        }
+        //
+        seqp.Clear();
+        //
+        aNb=aSeqTmp.Length()/2;
+        for(i=1; i<=aNb;++i) {
+          jx=2*i;
+          firstp=aSeqTmp(jx-1);
+          lastp =aSeqTmp(jx);
+          seqp.Append(firstp);
+          seqp.Append(lastp);
+        }
       }//if (bCond) {
     }
     done = Standard_True;
@@ -241,21 +242,21 @@ void IntTools_LineConstructor::Perform(const Handle(IntPatch_Line)& L)
       firstp = GeomInt_LineTool::Vertex(L,i).ParameterOnLine();
       lastp =  GeomInt_LineTool::Vertex(L,i+1).ParameterOnLine();
       if(Abs(firstp-lastp)>Precision::PConfusion()) {
-       intrvtested = Standard_True;
-       const Standard_Real pmid = (firstp+lastp)*0.5;
-       gp_Pnt Pmid;
-       GLinePoint(typl, GLine, pmid, Pmid);
-       //
-       Parameters(myHS1,myHS2,Pmid,u1,v1,u2,v2);
-       Recadre(myHS1,myHS2,u1,v1,u2,v2);
-       const TopAbs_State in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
-       if(in1 !=  TopAbs_OUT) { 
-         const TopAbs_State in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
-         if(in2 != TopAbs_OUT) { 
-           seqp.Append(firstp);
-           seqp.Append(lastp);
-         }
-       }
+        intrvtested = Standard_True;
+        const Standard_Real pmid = (firstp+lastp)*0.5;
+        gp_Pnt Pmid;
+        GLinePoint(typl, GLine, pmid, Pmid);
+        //
+        Parameters(myHS1,myHS2,Pmid,u1,v1,u2,v2);
+        Recadre(myHS1,myHS2,u1,v1,u2,v2);
+        const TopAbs_State in1 = myDom1->Classify(gp_Pnt2d(u1,v1),Tol);
+        if(in1 !=  TopAbs_OUT) { 
+          const TopAbs_State in2 = myDom2->Classify(gp_Pnt2d(u2,v2),Tol);
+          if(in2 != TopAbs_OUT) { 
+            seqp.Append(firstp);
+            seqp.Append(lastp);
+          }
+        }
       }
     }
     //
@@ -315,38 +316,38 @@ void IntTools_LineConstructor::Perform(const Handle(IntPatch_Line)& L)
     Standard_Boolean inserted = Standard_False;
     for (Standard_Integer j=1; j<=nbinserted;j++)    {
       if (Abs(prm-seqpss(j).Parameter()) <= Tol)      {
-       // accumulate
-       GeomInt_ParameterAndOrientation& valj = seqpss.ChangeValue(j);
-       if (or1 != TopAbs_INTERNAL) {
-         if (valj.Orientation1() != TopAbs_INTERNAL) {
-           if (or1 != valj.Orientation1()) {
-             valj.SetOrientation1(TopAbs_INTERNAL);
-           }
-         }
-         else {
-           valj.SetOrientation1(or1);
-         }
-       }
-       
-       if (or2 != TopAbs_INTERNAL) {
-         if (valj.Orientation2() != TopAbs_INTERNAL) {
-           if (or2 != valj.Orientation2()) {
-             valj.SetOrientation2(TopAbs_INTERNAL);
-           }
-         }
-         else {
-           valj.SetOrientation2(or2);
-         }
-       }         
-       inserted = Standard_True;
-       break;
+        // accumulate
+        GeomInt_ParameterAndOrientation& valj = seqpss.ChangeValue(j);
+        if (or1 != TopAbs_INTERNAL) {
+          if (valj.Orientation1() != TopAbs_INTERNAL) {
+            if (or1 != valj.Orientation1()) {
+              valj.SetOrientation1(TopAbs_INTERNAL);
+            }
+          }
+          else {
+            valj.SetOrientation1(or1);
+          }
+        }
+        
+        if (or2 != TopAbs_INTERNAL) {
+          if (valj.Orientation2() != TopAbs_INTERNAL) {
+            if (or2 != valj.Orientation2()) {
+              valj.SetOrientation2(TopAbs_INTERNAL);
+            }
+          }
+          else {
+            valj.SetOrientation2(or2);
+          }
+        }          
+        inserted = Standard_True;
+        break;
       }
       
       if (prm < seqpss(j).Parameter()-Tol ) {
-       // insert before position j
-       seqpss.InsertBefore(j,GeomInt_ParameterAndOrientation(prm,or1,or2));
-       inserted = Standard_True;
-       break;
+        // insert before position j
+        seqpss.InsertBefore(j,GeomInt_ParameterAndOrientation(prm,or1,or2));
+        inserted = Standard_True;
+        break;
       }
       
     }
@@ -374,12 +375,12 @@ void IntTools_LineConstructor::Perform(const Handle(IntPatch_Line)& L)
     Standard_Real U,V;
     for (i=1; i<=GeomInt_LineTool::NbVertex(L); i++ )    {
       if (!GeomInt_LineTool::Vertex(L,i).IsOnDomS1() )      {
-       GeomInt_LineTool::Vertex(L,i).ParametersOnS1(U,V);
-       gp_Pnt2d PPCC(U,V);
-       if (myDom1->Classify(PPCC,Tol) == TopAbs_OUT) {
-         done = Standard_True;
-         return;
-       }
+        GeomInt_LineTool::Vertex(L,i).ParametersOnS1(U,V);
+        gp_Pnt2d PPCC(U,V);
+        if (myDom1->Classify(PPCC,Tol) == TopAbs_OUT) {
+          done = Standard_True;
+          return;
+        }
         break;
       }
     }
@@ -399,11 +400,11 @@ void IntTools_LineConstructor::Perform(const Handle(IntPatch_Line)& L)
     Standard_Real U,V;
     for (i=1; i<=GeomInt_LineTool::NbVertex(L); i++ )    {
       if (!GeomInt_LineTool::Vertex(L,i).IsOnDomS2() )      {
-       GeomInt_LineTool::Vertex(L,i).ParametersOnS2(U,V);
-       if (myDom2->Classify(gp_Pnt2d(U,V),Tol) == TopAbs_OUT) {
-         done = Standard_True;
-         return;
-       }
+        GeomInt_LineTool::Vertex(L,i).ParametersOnS2(U,V);
+        if (myDom2->Classify(gp_Pnt2d(U,V),Tol) == TopAbs_OUT) {
+          done = Standard_True;
+          return;
+        }
         break;
       }
     }
@@ -428,49 +429,49 @@ void IntTools_LineConstructor::Perform(const Handle(IntPatch_Line)& L)
     or2 = seqpss(i).Orientation2(); 
     if (dansS1 && dansS2)    {
       if (or1 == TopAbs_REVERSED){
-       dansS1 = Standard_False;
+        dansS1 = Standard_False;
       }
       
       if (or2 == TopAbs_REVERSED){
-       dansS2 = Standard_False;
+        dansS2 = Standard_False;
       }
       if (!dansS1 || !dansS2)      {
-       lastp = seqpss(i).Parameter();
-       Standard_Real stofirst = Max(firstp, thefirst);
-       Standard_Real stolast  = Min(lastp,  thelast) ;
+        lastp = seqpss(i).Parameter();
+        Standard_Real stofirst = Max(firstp, thefirst);
+        Standard_Real stolast  = Min(lastp,  thelast) ;
 
-       if (stolast > stofirst) {
-         seqp.Append(stofirst);
-         seqp.Append(stolast);
-       }
-       if (lastp > thelast) {
-         break;
-       }
+        if (stolast > stofirst) {
+          seqp.Append(stofirst);
+          seqp.Append(stolast);
+        }
+        if (lastp > thelast) {
+          break;
+        }
       }
     }
     else    {
       if (dansS1)      {
-       if (or1 == TopAbs_REVERSED) {
-         dansS1 = Standard_False;
-       }
+        if (or1 == TopAbs_REVERSED) {
+          dansS1 = Standard_False;
+        }
       }
       else      {
-       if (or1 == TopAbs_FORWARD){
-         dansS1 = Standard_True;
-       }
+        if (or1 == TopAbs_FORWARD){
+          dansS1 = Standard_True;
+        }
       }
       if (dansS2) {
-       if (or2 == TopAbs_REVERSED) {
-         dansS2 = Standard_False;
-       }
+        if (or2 == TopAbs_REVERSED) {
+          dansS2 = Standard_False;
+        }
       }
       else {
-       if (or2 == TopAbs_FORWARD){
-         dansS2 = Standard_True;
-       }
+        if (or2 == TopAbs_FORWARD){
+          dansS2 = Standard_True;
+        }
       }
       if (dansS1 && dansS2){
-       firstp = seqpss(i).Parameter();
+        firstp = seqpss(i).Parameter();
       }
     }
   }
@@ -491,11 +492,11 @@ void IntTools_LineConstructor::Perform(const Handle(IntPatch_Line)& L)
 //purpose  : 
 //=======================================================================
 void Recadre(const Handle(GeomAdaptor_HSurface)& myHS1,
-            const Handle(GeomAdaptor_HSurface)& myHS2,
-            Standard_Real& u1,
-            Standard_Real& v1,
-            Standard_Real& u2,
-            Standard_Real& v2)
+             const Handle(GeomAdaptor_HSurface)& myHS2,
+             Standard_Real& u1,
+             Standard_Real& v1,
+             Standard_Real& u2,
+             Standard_Real& v2)
 { 
   Standard_Boolean myHS1IsUPeriodic,myHS1IsVPeriodic;
   const GeomAbs_SurfaceType typs1 = myHS1->GetType();
@@ -545,33 +546,31 @@ void Recadre(const Handle(GeomAdaptor_HSurface)& myHS1,
       break;
     }
   }
+  Standard_Real du, dv;
+  //
   if(myHS1IsUPeriodic) {
     const Standard_Real lmf = M_PI+M_PI; //-- myHS1->UPeriod();
     const Standard_Real f = myHS1->FirstUParameter();
     const Standard_Real l = myHS1->LastUParameter();
-    while(u1 < f) { u1+=lmf; } 
-    while(u1 > l) { u1-=lmf; }
+    IntTools_Tools::AdjustPeriodic(u1, f, l, lmf, u1, du);
   }
   if(myHS1IsVPeriodic) {
     const Standard_Real lmf = M_PI+M_PI; //-- myHS1->VPeriod(); 
     const Standard_Real f = myHS1->FirstVParameter();
     const Standard_Real l = myHS1->LastVParameter();
-    while(v1 < f) { v1+=lmf; } 
-    while(v1 > l) { v1-=lmf; }
+    IntTools_Tools::AdjustPeriodic(v1, f, l, lmf, v1, dv);
   }
   if(myHS2IsUPeriodic) { 
     const Standard_Real lmf = M_PI+M_PI; //-- myHS2->UPeriod();
     const Standard_Real f = myHS2->FirstUParameter();
     const Standard_Real l = myHS2->LastUParameter();
-    while(u2 < f) { u2+=lmf; } 
-    while(u2 > l) { u2-=lmf; }
+    IntTools_Tools::AdjustPeriodic(u2, f, l, lmf, u2, du);
   }
   if(myHS2IsVPeriodic) { 
     const Standard_Real lmf = M_PI+M_PI; //-- myHS2->VPeriod();
     const Standard_Real f = myHS2->FirstVParameter();
     const Standard_Real l = myHS2->LastVParameter();
-    while(v2 < f) { v2+=lmf; } 
-    while(v2 > l) { v2-=lmf; }
+    IntTools_Tools::AdjustPeriodic(v2, f, l, lmf, v2, dv);
   }
 }
 //=======================================================================
@@ -579,12 +578,12 @@ void Recadre(const Handle(GeomAdaptor_HSurface)& myHS1,
 //purpose  : 
 //=======================================================================
 void Parameters(const Handle(GeomAdaptor_HSurface)& myHS1,
-               const Handle(GeomAdaptor_HSurface)& myHS2,
-               const gp_Pnt& Ptref,
-               Standard_Real& U1,
-               Standard_Real& V1,
-               Standard_Real& U2,
-               Standard_Real& V2)
+                const Handle(GeomAdaptor_HSurface)& myHS2,
+                const gp_Pnt& Ptref,
+                Standard_Real& U1,
+                Standard_Real& V1,
+                Standard_Real& U2,
+                Standard_Real& V2)
 {
   Parameters(myHS1, Ptref, U1, V1);
   Parameters(myHS2, Ptref, U2, V2);
@@ -627,9 +626,9 @@ void Parameters(const Handle(GeomAdaptor_HSurface)& myHS1,
 //purpose  : 
 //=======================================================================
 void GLinePoint(const IntPatch_IType typl,
-               const Handle(IntPatch_GLine)& GLine,
-               const Standard_Real aT,
-               gp_Pnt& aP)
+                const Handle(IntPatch_GLine)& GLine,
+                const Standard_Real aT,
+                gp_Pnt& aP)
 {
   switch (typl) {
     case IntPatch_Lin:       
@@ -695,32 +694,32 @@ class IntTools_RealWithFlag {
 //------------
 static 
   void SortShell(const Standard_Integer, 
-                IntTools_RealWithFlag *); 
+                 IntTools_RealWithFlag *); 
 static
   void RejectDuplicates(Standard_Integer& aNbVtx, 
-                       IntTools_RealWithFlag *pVtx, 
-                       Standard_Real aTolPrm);
+                        IntTools_RealWithFlag *pVtx, 
+                        Standard_Real aTolPrm);
 static
   void RejectNearBeacons(Standard_Integer& aNbVtx, 
-                        IntTools_RealWithFlag *pVtx, 
-                        Standard_Real aTolPC1,
-                        const GeomAbs_SurfaceType aTS1,
-                        const GeomAbs_SurfaceType aTS2); 
+                         IntTools_RealWithFlag *pVtx, 
+                         Standard_Real aTolPC1,
+                         const GeomAbs_SurfaceType aTS1,
+                         const GeomAbs_SurfaceType aTS2); 
 static
   Standard_Real AdjustOnPeriod(const Standard_Real aTr,
-                              const Standard_Real aPeriod);
+                               const Standard_Real aPeriod);
 
 static
   Standard_Boolean RejectMicroCircle(const Handle(IntPatch_GLine)& aGLine,
-                                    const IntPatch_IType aType,
-                                    const Standard_Real aTol3D);
+                                     const IntPatch_IType aType,
+                                     const Standard_Real aTol3D);
 //
 //=======================================================================
 //function : TreatCircle
 //purpose  : 
 //=======================================================================
 void IntTools_LineConstructor::TreatCircle(const Handle(IntPatch_Line)& aLine,
-                                          const Standard_Real aTol)
+                                           const Standard_Real aTol)
 {  
   Standard_Boolean bRejected;
   IntPatch_IType aType;
@@ -780,8 +779,8 @@ void IntTools_LineConstructor::TreatCircle(const Handle(IntPatch_Line)& aLine,
     for(i=1; i<=aNbVtxWas; ++i) {
       aT=GeomInt_LineTool::Vertex(aLine, i).ParameterOnLine();
       if (fabs(aT) < aTolPC1 || fabs(aT-aTwoPI) < aTolPC1) {
-       bFound=!bFound;
-       break;
+        bFound=!bFound;
+        break;
       }
     }
     if (!bFound) {
@@ -789,8 +788,8 @@ void IntTools_LineConstructor::TreatCircle(const Handle(IntPatch_Line)& aLine,
       pVtx[aNbVtx-1].SetValue(aT);
       //
       for(i=0; i<aNbVtx; ++i) { 
-       aT=pVtx[i+1].Value();
-       pVtx[i].SetValue(aT);
+        aT=pVtx[i+1].Value();
+        pVtx[i].SetValue(aT);
       }
       --aNbVtx;
     }
@@ -811,8 +810,8 @@ void IntTools_LineConstructor::TreatCircle(const Handle(IntPatch_Line)& aLine,
       aP2D.SetCoord(aU2, aV2);
       aIn2=myDom2->Classify(aP2D, aTol);
       if(aIn2 != TopAbs_OUT) { 
-       seqp.Append(aT1);
-       seqp.Append(aT2);
+        seqp.Append(aT1);
+        seqp.Append(aT2);
       }
     }
   }
@@ -826,10 +825,10 @@ void IntTools_LineConstructor::TreatCircle(const Handle(IntPatch_Line)& aLine,
 //           chl/930/B5 B8 C2 C5 E2 E5 E8 F2 G8 H2 H5 H8
 //=======================================================================
 void RejectNearBeacons(Standard_Integer& aNbVtx, 
-                      IntTools_RealWithFlag *pVtx, 
-                      Standard_Real aTolPC1,
-                      const GeomAbs_SurfaceType aTS1,
-                      const GeomAbs_SurfaceType aTS2) 
+                       IntTools_RealWithFlag *pVtx, 
+                       Standard_Real aTolPC1,
+                       const GeomAbs_SurfaceType aTS1,
+                       const GeomAbs_SurfaceType aTS2) 
 {
   Standard_Integer i, j, iBcn;
   Standard_Real aT, aBcn[2];
@@ -841,33 +840,33 @@ void RejectNearBeacons(Standard_Integer& aNbVtx,
     for (j=0; j<2; ++j) {
       iBcn=-1;
       for(i=0; i<aNbVtx; ++i) {
-       aT=pVtx[i].Value();
-       if (aT==aBcn[j]) {
-         iBcn=i;
-         break;
-       }
+        aT=pVtx[i].Value();
+        if (aT==aBcn[j]) {
+          iBcn=i;
+          break;
+        }
       }
       //
       if (iBcn<0) {
-       // The beacon is not found
-       continue;
+        // The beacon is not found
+        continue;
       }
       //  
       for(i=0; i<aNbVtx; ++i) {
-       if (i!=iBcn) {
-         aT=pVtx[i].Value();
-         if (fabs(aT-aBcn[j]) < aTolPC1) {
-           pVtx[i].SetFlag(0);
-         }
-       }
+        if (i!=iBcn) {
+          aT=pVtx[i].Value();
+          if (fabs(aT-aBcn[j]) < aTolPC1) {
+            pVtx[i].SetFlag(0);
+          }
+        }
       }
     }// for (j=0; j<2; ++j) {
     //------------------------------------------
     j=0;
     for(i=0; i<aNbVtx; ++i) {
       if (pVtx[i].Flag()) {
-       pVtx[j]=pVtx[i];
-       ++j;
+        pVtx[j]=pVtx[i];
+        ++j;
       }
     }
     aNbVtx=j;
@@ -879,8 +878,8 @@ void RejectNearBeacons(Standard_Integer& aNbVtx,
 //purpose  : 
 //=======================================================================
 void RejectDuplicates(Standard_Integer& aNbVtx, 
-                     IntTools_RealWithFlag *pVtx, 
-                     Standard_Real aTolPC) 
+                      IntTools_RealWithFlag *pVtx, 
+                      Standard_Real aTolPC) 
 {
   Standard_Integer i, j;
   Standard_Real dX, aT1, aT2; 
@@ -908,7 +907,7 @@ void RejectDuplicates(Standard_Integer& aNbVtx,
 //purpose  : 
 //=======================================================================
 Standard_Real AdjustOnPeriod(const Standard_Real aTr,
-                            const Standard_Real aPeriod)
+                             const Standard_Real aPeriod)
 {
   Standard_Integer k;
   Standard_Real aT;
@@ -931,8 +930,8 @@ Standard_Real AdjustOnPeriod(const Standard_Real aTr,
 //purpose  : 
 //=======================================================================
 Standard_Boolean RejectMicroCircle(const Handle(IntPatch_GLine)& aGLine,
-                                  const IntPatch_IType aType,
-                                  const Standard_Real aTol3D)
+                                   const IntPatch_IType aType,
+                                   const Standard_Real aTol3D)
 {
   Standard_Boolean bRet;
   Standard_Real aR;
@@ -954,7 +953,7 @@ Standard_Boolean RejectMicroCircle(const Handle(IntPatch_GLine)& aGLine,
 // purpose : 
 //=======================================================================
 void SortShell(const Standard_Integer n, 
-              IntTools_RealWithFlag *a) 
+               IntTools_RealWithFlag *a) 
 {
   Standard_Integer nd, i, j, l, d=1;
   IntTools_RealWithFlag x;
index 7565585..7763165 100644 (file)
@@ -237,5 +237,15 @@ is
         theTmax  :out Real from Standard) 
     returns Integer from Standard;  
  
+    AdjustPeriodic(myclass;
+        thePar    : Real from Standard;
+        theParMin : Real from Standard;
+        theParMax : Real from Standard;
+        thePeriod : Real from Standard;
+        theNewPar : out Real from Standard;
+        theOffset : out Real from Standard;
+        theEps    : Real from Standard = 0.0)
+    returns Boolean from Standard;        
+    ---Purpose:  Adjusts the parameter <thePar> to the range [theParMin,  theParMax]
 
 end Tools;
index c7959af..38a6ed1 100644 (file)
@@ -778,3 +778,35 @@ Standard_Integer IntTools_Tools::SegPln(const gp_Lin& theLin,
   return iRet;
 }
 
+//=======================================================================
+//function : AdjustPeriodic
+//purpose  : 
+//=======================================================================
+Standard_Boolean IntTools_Tools::AdjustPeriodic(const Standard_Real thePar,
+                                                const Standard_Real theParMin,
+                                                const Standard_Real theParMax,
+                                                const Standard_Real thePeriod,
+                                                Standard_Real &theNewPar,
+                                                Standard_Real &theOffset,
+                                                const Standard_Real theEps)
+{
+  Standard_Boolean bMin, bMax;
+  //
+  theOffset = 0.;
+  theNewPar = thePar;
+  bMin = theParMin - thePar > theEps;
+  bMax = thePar - theParMax > theEps;
+  //
+  if (bMin || bMax) {
+    Standard_Real dp, aNbPer;
+    //
+    dp = (bMin) ? (theParMax - thePar) : (theParMin - thePar);
+    modf(dp / thePeriod, &aNbPer);
+    //
+    theOffset = aNbPer * thePeriod;
+    theNewPar += theOffset;
+  }
+  //
+  return (theOffset > 0.);
+}
+
index 4bc5471..4142609 100755 (executable)
@@ -1,4 +1,4 @@
-puts "TODO OCC11111 ALL: Error : The square of result shape is"
+puts "TODO ?OCC11111 ALL: Error : The square of result shape is"
 puts "TODO ?OCC11111 ALL: Error : Result shape is WRONG"
 
 puts "============"
diff --git a/tests/bugs/modalg_5/bug24981 b/tests/bugs/modalg_5/bug24981
new file mode 100644 (file)
index 0000000..3bfa8f1
--- /dev/null
@@ -0,0 +1,33 @@
+puts "========="
+puts "OCC24981"
+puts "========="
+puts ""
+###########################################################
+# IntTools_FaceFace enters to infinite loop on the attached case
+###########################################################
+
+restore [locate_data_file bug24981_shapes.brep] a
+restore [locate_data_file bug24981_tools.brep] b
+
+explode a So
+explode b So
+
+bclearobjects
+bcleartools
+baddobjects a_1
+baddtools b_1 b_2 b_3 b_4 b_5 b_6 b_7 b_8 b_9 b_10 b_11 b_12 b_13 b_14 b_15 b_16 b_17 b_18 b_19 b_20 b_21 b_22 b_23 b_24 b_25
+
+bfillds
+bbuild result
+
+set nb_v_good 122
+set nb_e_good 220
+set nb_w_good 243
+set nb_f_good 195
+set nb_sh_good 75
+set nb_sol_good 75
+set nb_compsol_good 0
+set nb_compound_good  1
+set nb_shape_good 931
+
+set 2dviewer 1