0024203: Command "sameparameter" in DRAW on attached edge set tolerance equal to...
authornbv <nbv@opencascade.com>
Mon, 20 Jan 2014 10:56:15 +0000 (14:56 +0400)
committerbugmaster <bugmaster@opencascade.com>
Thu, 30 Jan 2014 09:01:08 +0000 (13:01 +0400)
Main problem:
Algorithm of locale extrema, which is called by "sameparameter" command, cannot find extrema because extremal point is far from initial point. Therefore, reparametrization of edge's curve (for same-parameter) cannot be made properly.

Solve:
Using of global extrema (see Approx_SameParameter.cxx).

As the given edge contains two 2d-curves and the second from them is problem, for select needed curve, an interface of "mk2dcurve" DRAW-command is changed. Now there can be used an index of curve (by default, index = 1, as earlier), see help for more detail information.

Test "heal advanced Z3":
Now checkshape finds only two invalid subshapes. Earlier, it found four subshapes. I think it is not regression. Therefore, test case was changed.

Tolerance reducing.

test

src/Approx/Approx_SameParameter.cxx
src/BRepLib/BRepLib.cxx
src/BRepTest/BRepTest_CurveCommands.cxx
src/Extrema/Extrema_GLocateExtPC.gxx
tests/bugs/modalg_5/bug24203 [new file with mode: 0644]
tests/heal/data/advanced/Z3

index 3a95c4f..e4376c7 100644 (file)
 #include <GeomAdaptor_HCurve.hxx>
 #include <GeomAdaptor_Surface.hxx>
 #include <GeomAdaptor_HSurface.hxx>
-//#include <GCPnts_UniformDeflection.hxx>
 #include <GCPnts_QuasiUniformDeflection.hxx>
 #include <Extrema_LocateExtPC.hxx>
 #include <AdvApprox_ApproxAFunction.hxx>
 #include <GeomLib_MakeCurvefromApprox.hxx>
 #include <Precision.hxx>
-
-#define MAX_ARRAY_SIZE 1000 // IFV, Jan 2000
+#include <Extrema_ExtPC.hxx>
 
 #ifdef DEB
 #ifdef DRAW
@@ -50,32 +48,32 @@ static Standard_Integer NbCurve = 0;
 
 
 static void ProjectPointOnCurve(const Standard_Real      InitValue,
-                               const gp_Pnt             APoint,
-                               const Standard_Real      Tolerance,
-                               const Standard_Integer   NumIteration,
-                               const Adaptor3d_Curve&     Curve,
-                               Standard_Boolean&        Status,
-                               Standard_Real&           Result)
+  const gp_Pnt             APoint,
+  const Standard_Real      Tolerance,
+  const Standard_Integer   NumIteration,
+  const Adaptor3d_Curve&     Curve,
+  Standard_Boolean&        Status,
+  Standard_Real&           Result)
 {
   Standard_Integer num_iter = 0,
-  not_done = 1,
-  ii ;
-  
+    not_done = 1,
+    ii ;
+
   gp_Pnt a_point ;
   gp_Vec   vector,
-  d1,
-  d2 ;
+    d1,
+    d2 ;
   Standard_Real func,
-  func_derivative,
-  param = InitValue ;
+    func_derivative,
+    param = InitValue ;
   Status = Standard_False ;
   Standard_Real Toler = 1.0e-12;
   do {
     num_iter += 1 ;
     Curve.D2(param,
-             a_point,
-            d1,
-            d2) ;
+      a_point,
+      d1,
+      d2) ;
     for (ii = 1 ; ii <= 3 ; ii++) {
       vector.SetCoord(ii, APoint.Coord(ii) - a_point.Coord(ii)) ;
     }
@@ -87,19 +85,19 @@ static void ProjectPointOnCurve(const Standard_Real      InitValue,
       Status = Standard_True ;
     }
     else
-      { // fixing a bug PRO18577 : avoid divizion by zero
-       if( Abs(func_derivative) > Toler )  {
-         param -= func / func_derivative ;
-       }
-       param = Max(param,Curve.FirstParameter()) ;
-       param = Min(param,Curve.LastParameter())  ;
-       Status = Standard_True ;
+    { // fixing a bug PRO18577 : avoid divizion by zero
+      if( Abs(func_derivative) > Toler )  {
+        param -= func / func_derivative ;
       }
+      param = Max(param,Curve.FirstParameter()) ;
+      param = Min(param,Curve.LastParameter())  ;
+      //Status = Standard_True ;
+    }
   } 
   while (not_done && num_iter <= NumIteration) ;
   Result = param ;
 }  
-     
+
 
 
 //=======================================================================
@@ -109,31 +107,31 @@ static void ProjectPointOnCurve(const Standard_Real      InitValue,
 
 class Approx_SameParameter_Evaluator : public AdvApprox_EvaluatorFunction
 {
- public:
+public:
   Approx_SameParameter_Evaluator (const TColStd_Array1OfReal& theFlatKnots, 
-                                  const TColStd_Array1OfReal& thePoles,
-                                  const Handle(Adaptor2d_HCurve2d)& theHCurve2d) 
+    const TColStd_Array1OfReal& thePoles,
+    const Handle(Adaptor2d_HCurve2d)& theHCurve2d) 
     : FlatKnots(theFlatKnots), Poles(thePoles), HCurve2d(theHCurve2d) {}
 
   virtual void Evaluate (Standard_Integer *Dimension,
-                        Standard_Real     StartEnd[2],
-                         Standard_Real    *Parameter,
-                         Standard_Integer *DerivativeRequest,
-                         Standard_Real    *Result, // [Dimension]
-                         Standard_Integer *ErrorCode);
-  
- private:
+    Standard_Real     StartEnd[2],
+    Standard_Real    *Parameter,
+    Standard_Integer *DerivativeRequest,
+    Standard_Real    *Result, // [Dimension]
+    Standard_Integer *ErrorCode);
+
+private:
   const TColStd_Array1OfReal& FlatKnots;
   const TColStd_Array1OfReal& Poles;
   Handle(Adaptor2d_HCurve2d) HCurve2d;
 };
 
 void Approx_SameParameter_Evaluator::Evaluate (Standard_Integer *,/*Dimension*/
-                                               Standard_Real    /*StartEnd*/[2],
-                                               Standard_Real    *Parameter,
-                                               Standard_Integer *DerivativeRequest,
-                                               Standard_Real    *Result,
-                                               Standard_Integer *ReturnCode) 
+  Standard_Real    /*StartEnd*/[2],
+  Standard_Real    *Parameter,
+  Standard_Integer *DerivativeRequest,
+  Standard_Real    *Result,
+  Standard_Integer *ReturnCode) 
 { 
   gp_Pnt2d Point ;
   gp_Vec2d Vector ;
@@ -146,16 +144,16 @@ void Approx_SameParameter_Evaluator::Evaluate (Standard_Integer *,/*Dimension*/
   // evaluate the 1D bspline that represents the change in parameterization
   //
   BSplCLib::Eval(*Parameter,
-                Standard_False,
-                *DerivativeRequest,
-                extrap_mode[0],
-                3,
-                FlatKnots,
-                1,
-                PolesArray[0],
-                 eval_result[0]) ;
-  
-  
+    Standard_False,
+    *DerivativeRequest,
+    extrap_mode[0],
+    3,
+    FlatKnots,
+    1,
+    PolesArray[0],
+    eval_result[0]) ;
+
+
   if (*DerivativeRequest == 0){
     HCurve2d->D0(eval_result[0],Point);
     Point.Coord(Result[0],Result[1]);
@@ -169,24 +167,22 @@ void Approx_SameParameter_Evaluator::Evaluate (Standard_Integer *,/*Dimension*/
 }
 
 static Standard_Real ComputeTolReached(const Handle(Adaptor3d_HCurve)& c3d,
-                                      const Adaptor3d_CurveOnSurface& cons,
-                                      const Standard_Integer        nbp)
+  const Adaptor3d_CurveOnSurface& cons,
+  const Standard_Integer        nbp)
 {
   Standard_Real d2 = 0.;
-  Standard_Integer nn = nbp;
-  Standard_Real unsurnn = 1./nn;
-  Standard_Real first = c3d->FirstParameter();
-  Standard_Real last  = c3d->LastParameter();
-  for(Standard_Integer i = 0; i <= nn; i++){
-    Standard_Real t = unsurnn*i;
+  const Standard_Real first = c3d->FirstParameter();
+  const Standard_Real last  = c3d->LastParameter();
+  for(Standard_Integer i = 0; i <= nbp; i++){
+    Standard_Real t = IntToReal(i)/IntToReal(nbp);
     Standard_Real u = first*(1.-t) + last*t;
     gp_Pnt Pc3d = c3d->Value(u);
     gp_Pnt Pcons = cons.Value(u);
     if (Precision::IsInfinite(Pcons.X()) ||
-       Precision::IsInfinite(Pcons.Y()) ||
-       Precision::IsInfinite(Pcons.Z())) {
-      d2=Precision::Infinite();
-      break;
+      Precision::IsInfinite(Pcons.Y()) ||
+      Precision::IsInfinite(Pcons.Z())) {
+        d2=Precision::Infinite();
+        break;
     }
     Standard_Real temp = Pc3d.SquareDistance(Pcons);
     if(temp > d2) d2 = temp;
@@ -197,15 +193,15 @@ static Standard_Real ComputeTolReached(const Handle(Adaptor3d_HCurve)& c3d,
 }
 
 static Standard_Boolean Check(const TColStd_Array1OfReal& FlatKnots, 
-                              const TColStd_Array1OfReal& Poles,
-                              const Standard_Integer nbp,
-                             const TColStd_Array1OfReal& pc3d,
-//                           const TColStd_Array1OfReal& pcons,
-                             const TColStd_Array1OfReal& ,
-                             const Handle(Adaptor3d_HCurve)& c3d,
-                             const Adaptor3d_CurveOnSurface& cons,
-                             Standard_Real& tol,
-                             const Standard_Real oldtol)
+  const TColStd_Array1OfReal& Poles,
+  const Standard_Integer nbp,
+  const TColStd_Array1OfReal& pc3d,
+  //                         const TColStd_Array1OfReal& pcons,
+  const TColStd_Array1OfReal& ,
+  const Handle(Adaptor3d_HCurve)& c3d,
+  const Adaptor3d_CurveOnSurface& cons,
+  Standard_Real& tol,
+  const Standard_Real oldtol)
 {
   Standard_Real d = tol;
   Standard_Integer extrap_mode[2] ;
@@ -226,7 +222,7 @@ static Standard_Boolean Check(const TColStd_Array1OfReal& FlatKnots,
     gp_Pnt Pc3d = c3d->Value(tc3d);
     Standard_Real tcons;
     BSplCLib::Eval(tc3d,Standard_False,0,extrap_mode[0],
-                  3,FlatKnots,1, (Standard_Real&)Poles(1),tcons);
+      3,FlatKnots,1, (Standard_Real&)Poles(1),tcons);
     gp_Pnt Pcons = cons.Value(tcons);
     Standard_Real temp = Pc3d.SquareDistance(Pcons);
     if(temp >= dglis) dglis = temp;
@@ -246,7 +242,7 @@ static Standard_Boolean Check(const TColStd_Array1OfReal& FlatKnots,
     gp_Pnt Pc3d = c3d->Value(tc3d);
     Standard_Real tcons;
     BSplCLib::Eval(tc3d,Standard_False,0,extrap_mode[0],
-                  3,FlatKnots,1, (Standard_Real&)Poles(1),tcons);
+      3,FlatKnots,1, (Standard_Real&)Poles(1),tcons);
     gp_Pnt Pcons = cons.Value(tcons);
     Standard_Real temp = Pc3d.SquareDistance(Pcons);
     if(temp >= dglis) dglis = temp;
@@ -261,25 +257,25 @@ static Standard_Boolean Check(const TColStd_Array1OfReal& FlatKnots,
   Standard_Real d2 = 0.;
   Standard_Integer nn = 2*nbp;
   Standard_Real unsurnn = 1./nn;
-//  Modified by skv - Wed Jun  2 11:49:59 2004 OCC5898 Begin
-// Correction of the interval of valid values. This condition has no sensible
-// grounds. But it is better then the old one (which is commented out) because
-// it fixes the bug OCC5898. To develop more or less sensible criterion it is
-// necessary to deeply investigate this problem which is not possible in frames
-// of debugging.
-
-//   Standard_Real firstborne= 2*pc3d(1)-pc3d(nbp);
-//   Standard_Real lastborne= 2*pc3d(nbp)-pc3d(1);
+  //  Modified by skv - Wed Jun  2 11:49:59 2004 OCC5898 Begin
+  // Correction of the interval of valid values. This condition has no sensible
+  // grounds. But it is better then the old one (which is commented out) because
+  // it fixes the bug OCC5898. To develop more or less sensible criterion it is
+  // necessary to deeply investigate this problem which is not possible in frames
+  // of debugging.
+
+  //   Standard_Real firstborne= 2*pc3d(1)-pc3d(nbp);
+  //   Standard_Real lastborne= 2*pc3d(nbp)-pc3d(1);
   Standard_Real firstborne= 3.*pc3d(1)   - 2.*pc3d(nbp);
   Standard_Real lastborne = 3.*pc3d(nbp) - 2.*pc3d(1);
-//  Modified by skv - Wed Jun  2 11:50:03 2004 OCC5898 End
+  //  Modified by skv - Wed Jun  2 11:50:03 2004 OCC5898 End
   for(i = 0; i <= nn; i++){
     Standard_Real t = unsurnn*i;
     Standard_Real tc3d = pc3d(1)*(1.-t) + pc3d(nbp)*t;
     gp_Pnt Pc3d = c3d->Value(tc3d);
     Standard_Real tcons;
     BSplCLib::Eval(tc3d,Standard_False,0,extrap_mode[0],
-                  3,FlatKnots,1, (Standard_Real&)Poles(1),tcons);
+      3,FlatKnots,1, (Standard_Real&)Poles(1),tcons);
     if (tcons < firstborne || tcons > lastborne) {
       tol=Precision::Infinite();
       return Standard_False;
@@ -303,10 +299,10 @@ static Standard_Boolean Check(const TColStd_Array1OfReal& FlatKnots,
 //=======================================================================
 
 Approx_SameParameter::Approx_SameParameter(const Handle(Geom_Curve)&   C3D,
-                                          const Handle(Geom2d_Curve)& C2D,
-                                          const Handle(Geom_Surface)& S,
-                                          const Standard_Real         Tol):
- mySameParameter(Standard_True), myDone(Standard_False)
+  const Handle(Geom2d_Curve)& C2D,
+  const Handle(Geom_Surface)& S,
+  const Standard_Real         Tol):
+mySameParameter(Standard_True), myDone(Standard_False)
 {
   myHCurve2d = new Geom2dAdaptor_HCurve(C2D);
   myC3d      = new GeomAdaptor_HCurve(C3D);
@@ -321,10 +317,10 @@ Approx_SameParameter::Approx_SameParameter(const Handle(Geom_Curve)&   C3D,
 //=======================================================================
 
 Approx_SameParameter::Approx_SameParameter(const Handle(Adaptor3d_HCurve)&   C3D,
-                                          const Handle(Geom2d_Curve)&     C2D,
-                                          const Handle(Adaptor3d_HSurface)& S,
-                                          const Standard_Real            Tol):
- mySameParameter(Standard_True), myDone(Standard_False)
+  const Handle(Geom2d_Curve)&     C2D,
+  const Handle(Adaptor3d_HSurface)& S,
+  const Standard_Real            Tol):
+mySameParameter(Standard_True), myDone(Standard_False)
 {
   myC3d = C3D;
   mySurf = S;
@@ -339,10 +335,10 @@ Approx_SameParameter::Approx_SameParameter(const Handle(Adaptor3d_HCurve)&   C3D
 //=======================================================================
 
 Approx_SameParameter::Approx_SameParameter(const Handle(Adaptor3d_HCurve)&   C3D,
-                                          const Handle(Adaptor2d_HCurve2d)& C2D,
-                                          const Handle(Adaptor3d_HSurface)& S,
-                                          const Standard_Real            Tol):
- mySameParameter(Standard_True), myDone(Standard_False)
+  const Handle(Adaptor2d_HCurve2d)& C2D,
+  const Handle(Adaptor3d_HSurface)& S,
+  const Standard_Real            Tol):
+mySameParameter(Standard_True), myDone(Standard_False)
 {
   myC3d = C3D;
   mySurf = S;
@@ -355,9 +351,12 @@ Approx_SameParameter::Approx_SameParameter(const Handle(Adaptor3d_HCurve)&   C3D
 //function : Build
 //purpose  : 
 //=======================================================================
-
 void Approx_SameParameter::Build(const Standard_Real Tolerance)
 {
+  const Standard_Real anErrorMAX = 1.0e15;
+  const Standard_Integer aMaxArraySize = 1000;
+  const Standard_Integer NCONTROL = 22;
+
   Standard_Integer ii ;
   Adaptor3d_CurveOnSurface CurveOnSurface(myHCurve2d,mySurf);
   Standard_Real fcons = CurveOnSurface.FirstParameter();
@@ -412,11 +411,10 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
   //Take a multiple of the sample pof CheckShape,
   //at least the control points will be correct. No comment!!!
 
-  Standard_Integer NCONTROL = 22;
 #ifdef DEB
   Standard_Integer nbcoups = 0;
 #endif
-  
+
   Standard_Boolean interpolok = 0;
   Standard_Real tolsov = 1.e200;
   //Take parameters with  constant step on the curve on surface
@@ -428,9 +426,9 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
 
   Standard_Real wcons = fcons;
   Standard_Real wc3d  = fc3d;
-  
-  Standard_Real qpcons[MAX_ARRAY_SIZE], qnewpcons[MAX_ARRAY_SIZE], 
-                qpc3d[MAX_ARRAY_SIZE], qnewpc3d[MAX_ARRAY_SIZE];
+
+  Standard_Real qpcons[aMaxArraySize], qnewpcons[aMaxArraySize], 
+    qpc3d[aMaxArraySize], qnewpc3d[aMaxArraySize];
   Standard_Real * pcons = qpcons; Standard_Real * newpcons = qnewpcons;
   Standard_Real * pc3d = qpc3d; Standard_Real * newpc3d = qnewpc3d;
 
@@ -445,51 +443,51 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
 
   Standard_Integer New_NCONTROL = NCONTROL;
   if(Continuity < GeomAbs_C1) {
-     Standard_Integer NbInt = myHCurve2d->NbIntervals(GeomAbs_C1) + 1;
-     TColStd_Array1OfReal Param_de_decoupeC1 (1, NbInt);
-     myHCurve2d->Intervals(Param_de_decoupeC1, GeomAbs_C1);
-     TColStd_SequenceOfReal new_par;
-     Standard_Integer inter = 1;
-     ii =1;
-     new_par.Append(fcons);
-
-     while(Param_de_decoupeC1(inter) <= fcons + deltamin) inter++;
-     while(Param_de_decoupeC1(NbInt) >= lcons - deltamin) NbInt--;
-
-     while(inter <= NbInt || ii < NCONTROL) {
-       if(Param_de_decoupeC1(inter) < pcons[ii]) {
-        new_par.Append(Param_de_decoupeC1(inter));
-        if((pcons[ii] - Param_de_decoupeC1(inter)) <= deltamin) {
-          ii++;
-          if(ii > NCONTROL) {ii = NCONTROL;}
-        }
-        inter++;
-       }
-       else {
-        if((Param_de_decoupeC1(inter) - pcons[ii]) > deltamin) {
-          new_par.Append(pcons[ii]);
-        }
-        ii++;
-       }
-     }
-     
-     new_par.Append(lcons);
-     New_NCONTROL = new_par.Length() - 1;
-     //simple protection if New_NCONTROL > allocated elements in array
-     if (New_NCONTROL > MAX_ARRAY_SIZE) {
-       mySameParameter = Standard_False;
-       return;
-     }
-     for(ii = 1; ii <= New_NCONTROL; ii++){
-       pcons[ii] = pc3d[ii] = new_par.Value(ii + 1);
-     }
-     pc3d[New_NCONTROL]  = lc3d;
-   }
-
-  
+    Standard_Integer NbInt = myHCurve2d->NbIntervals(GeomAbs_C1) + 1;
+    TColStd_Array1OfReal Param_de_decoupeC1 (1, NbInt);
+    myHCurve2d->Intervals(Param_de_decoupeC1, GeomAbs_C1);
+    TColStd_SequenceOfReal new_par;
+    Standard_Integer inter = 1;
+    ii =1;
+    new_par.Append(fcons);
+
+    while(Param_de_decoupeC1(inter) <= fcons + deltamin) inter++;
+    while(Param_de_decoupeC1(NbInt) >= lcons - deltamin) NbInt--;
+
+    while(inter <= NbInt || ii < NCONTROL) {
+      if(Param_de_decoupeC1(inter) < pcons[ii]) {
+        new_par.Append(Param_de_decoupeC1(inter));
+        if((pcons[ii] - Param_de_decoupeC1(inter)) <= deltamin) {
+          ii++;
+          if(ii > NCONTROL) {ii = NCONTROL;}
+        }
+        inter++;
+      }
+      else {
+        if((Param_de_decoupeC1(inter) - pcons[ii]) > deltamin) {
+          new_par.Append(pcons[ii]);
+        }
+        ii++;
+      }
+    }
+
+    new_par.Append(lcons);
+    New_NCONTROL = new_par.Length() - 1;
+    //simple protection if New_NCONTROL > allocated elements in array
+    if (New_NCONTROL > aMaxArraySize) {
+      mySameParameter = Standard_False;
+      return;
+    }
+    for(ii = 1; ii <= New_NCONTROL; ii++){
+      pcons[ii] = pc3d[ii] = new_par.Value(ii + 1);
+    }
+    pc3d[New_NCONTROL]  = lc3d;
+  }
+
+
   Extrema_LocateExtPC Projector;
   Projector.Initialize(myC3d->Curve(),fc3d,lc3d,Tol);
-  
+
   Standard_Integer count = 1;
   Standard_Real previousp = fc3d, initp=0, curp;//, deltamin = 50*Tolp;
   Standard_Real bornesup = lc3d - deltamin;
@@ -501,7 +499,7 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
     dist2 = Pcons.SquareDistance(Pc3d);
     use_parameter = (dist2 <= Tol2  && (pc3d[ii] > pc3d[count-1] + deltamin)) ;
     if(use_parameter) {
-      
+
       if(dist2 > dmax2) dmax2 = dist2;
       initp = previousp = pc3d[count] = pc3d[ii];
       pcons[count] = pcons[ii];
@@ -512,34 +510,72 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
       projok = mySameParameter = Standard_False;
       Projector.Perform(Pcons, initp);
       if (Projector.IsDone()) {
-       curp = Projector.Point().Parameter();
-       Standard_Real dist_2 = Projector.SquareDistance();
-       if(dist_2 > besttol2) besttol2 = dist_2;
-       projok = 1;
+        curp = Projector.Point().Parameter();
+        Standard_Real dist_2 = Projector.SquareDistance();
+        if(dist_2 > besttol2) besttol2 = dist_2;
+        projok = 1;
       }
-      else {
-       ProjectPointOnCurve(initp,Pcons,Tol,30,myC3d->Curve(),projok,curp);
+      else
+      {
+        ProjectPointOnCurve(initp,Pcons,Tol,30,myC3d->Curve(),projok,curp);
       }
-      if(projok){
-       if(curp > previousp + deltamin && curp < bornesup){
-         initp = previousp = pc3d[count] = curp;
-         pcons[count] = pcons[ii];
-         count++;
-       }
+      
+      if(projok)
+      {
+        if(curp > previousp + deltamin && curp < bornesup){
+          initp = previousp = pc3d[count] = curp;
+          pcons[count] = pcons[ii];
+          count++;
+        }
       }
-      else {
-#ifdef DEB 
-       // JAG
-       cout << "Projection not done" << endl;
+      else
+      {
+        Extrema_ExtPC PR(Pcons,myC3d->Curve(),fc3d,lc3d,Tol);
+        if(PR.IsDone())
+        {
+          const Standard_Integer aNbExt = PR.NbExt();
+          if(aNbExt > 0)
+          {
+            Standard_Integer anIndMin = 0;
+            Standard_Real aDistMin = RealLast();
+            for(Standard_Integer i = 1; i <= aNbExt; i++)
+            {
+              const gp_Pnt &aP = PR.Point(i).Value();
+              Standard_Real aDist2 = aP.SquareDistance(Pcons);
+              if(aDist2 < aDistMin)
+              {
+                aDistMin = aDist2;
+                anIndMin = i;
+              }
+            }
+            curp = PR.Point(anIndMin).Parameter();
+            if(curp > previousp + deltamin && curp < bornesup)
+            {
+              initp = previousp = pc3d[count] = curp;
+              pcons[count] = pcons[ii];
+              count++;
+              projok = Standard_True;
+            }
+          }
+        }
+      }
+
+      if(!projok)
+      {
+        //Projector
+#ifdef DEB
+        // JAG
+        cout << "Projection not done" << endl;
 #endif
       }
     }
   }
+
   if(mySameParameter){
     myTolReached = 1.5*sqrt(dmax2);
     return;
   }
+
   if(!extrok) { // If not already SameP and tangent to mill, abandon.
     mySameParameter = Standard_False;
 #ifdef DEB
@@ -571,8 +607,11 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
 #endif
   }
 #endif
-    
-  while(!interpolok){
+
+  Standard_Boolean hasCountChanged = Standard_False;
+
+  while(!interpolok)
+  {
     // The tables and their limits for the interpolation.
     Standard_Integer num_knots = count + 7;
     Standard_Integer num_poles = count + 3;
@@ -582,79 +621,79 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
     TColStd_Array1OfReal    Poles(1,num_poles) ;
     TColStd_Array1OfReal    InterpolationParameters(1,num_poles) ;
     TColStd_Array1OfReal    FlatKnots(1,num_knots) ; 
-    
+
     // Fill tables taking attention to end values.
     ContactOrder.Init(0);
     ContactOrder(2) = ContactOrder(num_poles - 1) = 1;
-    
+
     FlatKnots(1) = FlatKnots(2) = FlatKnots(3) = FlatKnots(4) = fc3d;
     FlatKnots(num_poles + 1) = FlatKnots(num_poles + 2) = 
       FlatKnots(num_poles + 3) = FlatKnots(num_poles + 4) = lc3d;
-    
+
     Poles(1) = fcons; Poles(num_poles) = lcons;
     Poles(2) = tangent[0]; Poles(num_poles - 1) = tangent[1];
-    
+
     InterpolationParameters(1) = InterpolationParameters(2) = fc3d;
     InterpolationParameters(num_poles - 1) = InterpolationParameters(num_poles) = lc3d;
-    
+
     for (ii = 3; ii <= num_poles - 2; ii++) {
       Poles(ii) = Paramcons(ii - 1);
       InterpolationParameters(ii) = FlatKnots(ii+2) = Paramc3d(ii - 1);
     }
     Standard_Integer inversion_problem;
     BSplCLib::Interpolate(3,FlatKnots,InterpolationParameters,ContactOrder,
-                         1,Poles(1),inversion_problem);
+      1,Poles(1),inversion_problem);
     if(inversion_problem) {
       Standard_ConstructionError::Raise();
     }
 
     //-------------------------------------------
 #ifdef DEB
-  if (AffichFw) {
-    nbcoups ++;
-    char Name[17];
-    Name[0] = '\0';
-    Standard_Integer nnn = 100;
-    TColgp_Array1OfPnt2d    DEBP2d  (0,nnn);
-    TColStd_Array1OfInteger DEBMults(0,nnn); 
-    DEBMults.Init(1); DEBMults(0) = 2; DEBMults(nnn) = 2;
-    TColStd_Array1OfReal    DEBKnots(0,nnn);
-    Standard_Real du = (lc3d - fc3d) / nnn;
-    Standard_Real u3d = fc3d;
-    Standard_Integer extrap_mode[2] ;
-    extrap_mode[0] = extrap_mode[1] = 3;
-    Standard_Real eval_result[2] ;
-    Standard_Integer DerivativeRequest = 0;
-    Standard_Real *PolesArray =
-      (Standard_Real *) &Poles(Poles.Lower()) ;
-
-    for (Standard_Integer DEBi = 0; DEBi <= nnn; DEBi++) {
-      DEBKnots(DEBi) = DEBi;
-      BSplCLib::Eval(u3d,
-                    Standard_False,
-                    DerivativeRequest,
-                    extrap_mode[0],
-                    3,
-                    FlatKnots,
-                    1,
-                    PolesArray[0],
-                    eval_result[0]) ;
-
-      DEBP2d  (DEBi) = gp_Pnt2d(u3d,eval_result[0]);
-      u3d += du;
-    }
+    if (AffichFw) {
+      nbcoups ++;
+      char Name[17];
+      Name[0] = '\0';
+      Standard_Integer nnn = 100;
+      TColgp_Array1OfPnt2d    DEBP2d  (0,nnn);
+      TColStd_Array1OfInteger DEBMults(0,nnn); 
+      DEBMults.Init(1); DEBMults(0) = 2; DEBMults(nnn) = 2;
+      TColStd_Array1OfReal    DEBKnots(0,nnn);
+      Standard_Real du = (lc3d - fc3d) / nnn;
+      Standard_Real u3d = fc3d;
+      Standard_Integer extrap_mode[2] ;
+      extrap_mode[0] = extrap_mode[1] = 3;
+      Standard_Real eval_result[2] ;
+      Standard_Integer DerivativeRequest = 0;
+      Standard_Real *PolesArray =
+        (Standard_Real *) &Poles(Poles.Lower()) ;
+
+      for (Standard_Integer DEBi = 0; DEBi <= nnn; DEBi++) {
+        DEBKnots(DEBi) = DEBi;
+        BSplCLib::Eval(u3d,
+          Standard_False,
+          DerivativeRequest,
+          extrap_mode[0],
+          3,
+          FlatKnots,
+          1,
+          PolesArray[0],
+          eval_result[0]) ;
+
+        DEBP2d  (DEBi) = gp_Pnt2d(u3d,eval_result[0]);
+        u3d += du;
+      }
 
-    Handle(Geom2d_BSplineCurve) DEBBS = 
-      new Geom2d_BSplineCurve(DEBP2d,DEBKnots,DEBMults,1);
-    Sprintf(Name,"DEBC2d_%d_%d",NbCurve,nbcoups );
+      Handle(Geom2d_BSplineCurve) DEBBS = 
+        new Geom2d_BSplineCurve(DEBP2d,DEBKnots,DEBMults,1);
+      Sprintf(Name,"DEBC2d_%d_%d",NbCurve,nbcoups );
 #ifdef DRAW
-    DrawTrSurf::Set(Name,DEBBS);
+      DrawTrSurf::Set(Name,DEBBS);
 #endif
-  }
+    }
 #endif
-//-------------------------------------------    
+    //-------------------------------------------    
 
-//-------------------------------------------
+    //-------------------------------------------
     // Test if par2d(par3d) is monotonous function or not           ----- IFV, Jan 2000
     // and try to insert new point to improve BSpline interpolation
 
@@ -667,46 +706,46 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
 
     Standard_Integer newcount = 0;
     for (ii = 0; ii < count; ii++) {
-      
+
       newpcons[newcount] = pcons[ii];
       newpc3d[newcount] = pc3d[ii];
       newcount++;
 
-      if(count - ii + newcount == MAX_ARRAY_SIZE) continue;
+      if(count - ii + newcount == aMaxArraySize) continue;
 
       BSplCLib::Eval(.5*(pc3d[ii]+pc3d[ii+1]), Standard_False, DerivativeRequest,
-                    extrap_mode[0], 3, FlatKnots, 1, PolesArray[0], eval_result[0]);
-                    
+        extrap_mode[0], 3, FlatKnots, 1, PolesArray[0], eval_result[0]);
+
       if(eval_result[0] < pcons[ii] || eval_result[0] > pcons[ii+1]) {
-       Standard_Real ucons = 0.5*(pcons[ii]+pcons[ii+1]);
-       Standard_Real uc3d  = 0.5*(pc3d[ii]+pc3d[ii+1]);
-       
-       CurveOnSurface.D0(ucons,Pcons);
-       Projector.Perform(Pcons, uc3d);
-       if (Projector.IsDone()) {
-         curp = Projector.Point().Parameter();
-         Standard_Real dist_2 = Projector.SquareDistance();
-         if(dist_2 > besttol2) besttol2 = dist_2;
-         projok = 1;
-       }
-       else {
-         ProjectPointOnCurve(uc3d,Pcons,Tol,30,myC3d->Curve(),projok,curp);
-       }
-       if(projok){
-         if(curp > pc3d[ii] + deltamin && curp < pc3d[ii+1] - deltamin){
-           newpc3d[newcount] = curp;
-           newpcons[newcount] = ucons;
-           newcount ++;
-         }
-       }
-       else {
+        Standard_Real ucons = 0.5*(pcons[ii]+pcons[ii+1]);
+        Standard_Real uc3d  = 0.5*(pc3d[ii]+pc3d[ii+1]);
+
+        CurveOnSurface.D0(ucons,Pcons);
+        Projector.Perform(Pcons, uc3d);
+        if (Projector.IsDone()) {
+          curp = Projector.Point().Parameter();
+          Standard_Real dist_2 = Projector.SquareDistance();
+          if(dist_2 > besttol2) besttol2 = dist_2;
+          projok = 1;
+        }
+        else {
+          ProjectPointOnCurve(uc3d,Pcons,Tol,30,myC3d->Curve(),projok,curp);
+        }
+        if(projok){
+          if(curp > pc3d[ii] + deltamin && curp < pc3d[ii+1] - deltamin){
+            newpc3d[newcount] = curp;
+            newpcons[newcount] = ucons;
+            newcount ++;
+          }
+        }
+        else {
 #ifdef DEB 
-         // JAG
-         cout << "Projection not done" << endl;
+          // JAG
+          cout << "Projection not done" << endl;
 #endif
-       }
+        }
       }
-     
+
     }
 
     newpc3d[newcount] = pc3d[count];
@@ -719,14 +758,14 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
     pcons = newpcons;
     newpcons = temp;
 
-    if((count != newcount) && newcount < MAX_ARRAY_SIZE) { count = newcount; continue;}
+    if((count != newcount) && newcount < aMaxArraySize) { count = newcount; continue;}
 
     count = newcount;
 
     Standard_Real algtol = sqrt(besttol2);
 
     interpolok = Check (FlatKnots, Poles, count+1, Paramc3d, Paramcons, 
-                        myC3d, CurveOnSurface, algtol, tolsov);
+      myC3d, CurveOnSurface, algtol, tolsov);
 
     if (Precision::IsInfinite(algtol)) {
       mySameParameter = Standard_False;
@@ -738,15 +777,15 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
 
     tolsov = algtol;
 
-    interpolok = (interpolok || count >= MAX_ARRAY_SIZE);
+    interpolok = (interpolok || count >= aMaxArraySize);
 
     if(interpolok) {
-        Standard_Real besttol = sqrt(besttol2);
+      Standard_Real besttol = sqrt(besttol2);
 #ifdef DEB
       if (Voir) {
-       if(algtol > besttol){
-         cout<<"SameParameter : Tol can't be reached before approx"<<endl;
-       }
+        if(algtol > besttol){
+          cout<<"SameParameter : Tol can't be reached before approx"<<endl;
+        }
       }
 #endif
       Handle(TColStd_HArray1OfReal) tol1d,tol2d,tol3d;
@@ -756,58 +795,85 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
 
       Approx_SameParameter_Evaluator ev (FlatKnots, Poles, myHCurve2d); 
       AdvApprox_ApproxAFunction  anApproximator(2,0,0,tol1d,tol2d,tol3d,fc3d,lc3d,
-                                               Continuity,11,40,ev);
+        Continuity,11,40,ev);
 
       if (anApproximator.IsDone() || anApproximator.HasResult()) {
-       GeomLib_MakeCurvefromApprox  aCurveBuilder(anApproximator) ;
-       myCurve2d = aCurveBuilder.Curve2dFromTwo1d(1,2) ;
-       myHCurve2d = new Geom2dAdaptor_HCurve(myCurve2d);
-       CurveOnSurface.Load(myHCurve2d);
-       myTolReached = ComputeTolReached(myC3d,CurveOnSurface,NCONTROL);
-       myDone = Standard_True;
+        Adaptor3d_CurveOnSurface ACS = CurveOnSurface;
+        GeomLib_MakeCurvefromApprox  aCurveBuilder(anApproximator) ;
+        Handle(Geom2d_BSplineCurve) aC2d = aCurveBuilder.Curve2dFromTwo1d(1,2) ;
+        Handle(Adaptor2d_HCurve2d) aHCurve2d = new Geom2dAdaptor_HCurve(aC2d);
+        CurveOnSurface.Load(aHCurve2d);
+
+        myTolReached = ComputeTolReached(myC3d,CurveOnSurface,NCONTROL);
+
+        if(myTolReached > anErrorMAX)
+        {
+          //This tolerance is too big. Probably, we will not can get 
+          //edge with sameparameter in this case.
+
+          myDone = Standard_False;
+          return;
+        }
+
+        if( (myTolReached < 250.0*besttol) || 
+            (count >= aMaxArraySize-2) || 
+            !hasCountChanged) //if count does not change after adding new point
+                              //(else we can have circularity)
+        {
+          myCurve2d = aC2d;
+          myHCurve2d  = new Geom2dAdaptor_HCurve(myCurve2d);
+          myDone = Standard_True;
+        }
+        else
+        {
+          interpolok = Standard_False;
+          CurveOnSurface = ACS;
+        }
       } 
     }
-    else {
+    
+    if(!interpolok)
+    {
 #ifdef DEB
       if (Voir)
-       cout<<"SameParameter : Not enough points, enrich"<<endl;
+        cout<<"SameParameter : Not enough points, enrich"<<endl;
 #endif
 
-      Standard_Integer newcount = 0;
+      newcount = 0;
       for(Standard_Integer n = 0; n < count; n++){
-       newpc3d[newcount] = pc3d[n];
-       newpcons[newcount] = pcons[n];
-       newcount ++;
-
-       if(count - n + newcount == MAX_ARRAY_SIZE) continue;
-
-       Standard_Real ucons = 0.5*(pcons[n]+pcons[n+1]);
-       Standard_Real uc3d  = 0.5*(pc3d[n]+pc3d[n+1]);
-       
-       CurveOnSurface.D0(ucons,Pcons);
-       Projector.Perform(Pcons, uc3d);
-       if (Projector.IsDone()) {
-         curp = Projector.Point().Parameter();
-         Standard_Real dist_2 = Projector.SquareDistance();
-         if(dist_2 > besttol2) besttol2 = dist_2;
-         projok = 1;
-       }
-       else {
-         ProjectPointOnCurve(uc3d,Pcons,Tol,30,myC3d->Curve(),projok,curp);
-       }
-       if(projok){
-         if(curp > pc3d[n] + deltamin && curp < pc3d[n+1] - deltamin){
-           newpc3d[newcount] = curp;
-           newpcons[newcount] = ucons;
-           newcount ++;
-         }
-       }
-       else {
+        newpc3d[newcount] = pc3d[n];
+        newpcons[newcount] = pcons[n];
+        newcount ++;
+
+        if(count - n + newcount == aMaxArraySize) continue;
+
+        Standard_Real ucons = 0.5*(pcons[n]+pcons[n+1]);
+        Standard_Real uc3d  = 0.5*(pc3d[n]+pc3d[n+1]);
+
+        CurveOnSurface.D0(ucons,Pcons);
+        Projector.Perform(Pcons, uc3d);
+        if (Projector.IsDone()) {
+          curp = Projector.Point().Parameter();
+          Standard_Real dist_2 = Projector.SquareDistance();
+          if(dist_2 > besttol2) besttol2 = dist_2;
+          projok = 1;
+        }
+        else {
+          ProjectPointOnCurve(uc3d,Pcons,Tol,30,myC3d->Curve(),projok,curp);
+        }
+        if(projok){
+          if(curp > pc3d[n] + deltamin && curp < pc3d[n+1] - deltamin){
+            newpc3d[newcount] = curp;
+            newpcons[newcount] = ucons;
+            newcount ++;
+          }
+        }
+        else {
 #ifdef DEB 
-         // JAG
-         cout << "Projection not done" << endl;
+          // JAG
+          cout << "Projection not done" << endl;
 #endif
-       }
+        }
       }
       newpc3d[newcount] = pc3d[count];
       newpcons[newcount] = pcons[count];
@@ -818,7 +884,16 @@ void Approx_SameParameter::Build(const Standard_Real Tolerance)
       tempx = pcons;
       pcons = newpcons;
       newpcons = tempx;
-      count = newcount;
+      
+      if(count != newcount)
+      {
+        count = newcount;
+        hasCountChanged = Standard_True;
+      }
+      else
+      {
+        hasCountChanged = Standard_False;
+      }
     }
   }
 }
index c169450..d5194f6 100644 (file)
@@ -134,14 +134,14 @@ const Handle(Geom_Plane)&  BRepLib::Plane()
 //=======================================================================
 
 Standard_Boolean  BRepLib::CheckSameRange(const TopoDS_Edge& AnEdge,
-                                         const Standard_Real Tolerance) 
+  const Standard_Real Tolerance) 
 {
   Standard_Boolean  IsSameRange = Standard_True,
-  first_time_in = Standard_True ;
+    first_time_in = Standard_True ;
 
   BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
     ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
-  
+
   Standard_Real first, last;
   Standard_Real current_first =0., current_last =0. ;
   Handle(BRep_GCurve) geometric_representation_ptr ;
@@ -150,115 +150,115 @@ Standard_Boolean  BRepLib::CheckSameRange(const TopoDS_Edge& AnEdge,
     geometric_representation_ptr =
       Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
     if (!geometric_representation_ptr.IsNull()) {
-      
-        first = geometric_representation_ptr->First();
-        last =  geometric_representation_ptr->Last();
-        if (first_time_in ) {
-         current_first = first ;
-         current_last = last   ;
-         first_time_in = Standard_False ;
-       }
-        else {
-         IsSameRange = (Abs(current_first - first) <= Tolerance) 
-           && (Abs(current_last -last) <= Tolerance ) ;
-       }
+
+      first = geometric_representation_ptr->First();
+      last =  geometric_representation_ptr->Last();
+      if (first_time_in ) {
+        current_first = first ;
+        current_last = last   ;
+        first_time_in = Standard_False ;
       }
+      else {
+        IsSameRange = (Abs(current_first - first) <= Tolerance) 
+          && (Abs(current_last -last) <= Tolerance ) ;
+      }
+    }
     an_Iterator.Next() ;
   }
   return IsSameRange ;
 }
-  
+
 //=======================================================================
 //function : SameRange
 //purpose  : 
 //=======================================================================
 
 void BRepLib::SameRange(const TopoDS_Edge& AnEdge,
-                       const Standard_Real Tolerance) 
+  const Standard_Real Tolerance) 
 {
   BRep_ListIteratorOfListOfCurveRepresentation an_Iterator
     ((*((Handle(BRep_TEdge)*)&AnEdge.TShape()))->ChangeCurves());
-  
+
   Handle(Geom2d_Curve) Curve2dPtr, Curve2dPtr2, NewCurve2dPtr, NewCurve2dPtr2;
   TopLoc_Location LocalLoc ;
 
   Standard_Boolean first_time_in = Standard_True,
-  has_curve,
-  has_closed_curve ;
+    has_curve,
+    has_closed_curve ;
   Handle(BRep_GCurve) geometric_representation_ptr ;
   Standard_Real first,
-  current_first,
-  last,
-  current_last ;
+    current_first,
+    last,
+    current_last ;
 
   const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
-                                               LocalLoc,
-                                               current_first,
-                                               current_last);
+    LocalLoc,
+    current_first,
+    current_last);
   if (!C.IsNull()) {
     first_time_in = Standard_False ;
   }
-  
+
   while (an_Iterator.More()) {
     geometric_representation_ptr =
       Handle(BRep_GCurve)::DownCast(an_Iterator.Value());
     if (! geometric_representation_ptr.IsNull()) {
       has_closed_curve =
-       has_curve = Standard_False ;
+        has_curve = Standard_False ;
       first = geometric_representation_ptr->First();
       last =  geometric_representation_ptr->Last();
       if (geometric_representation_ptr->IsCurveOnSurface()) {
-       Curve2dPtr = geometric_representation_ptr->PCurve() ; 
-       has_curve = Standard_True ;
+        Curve2dPtr = geometric_representation_ptr->PCurve() ; 
+        has_curve = Standard_True ;
       }
       if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
-       Curve2dPtr2 = geometric_representation_ptr->PCurve2() ;
-       has_closed_curve = Standard_True ;
+        Curve2dPtr2 = geometric_representation_ptr->PCurve2() ;
+        has_closed_curve = Standard_True ;
       }
       if (has_curve || has_closed_curve) {
-       if (first_time_in) {
-         current_first = first ;
-         current_last = last ;
-         first_time_in = Standard_False ;
+        if (first_time_in) {
+          current_first = first ;
+          current_last = last ;
+          first_time_in = Standard_False ;
         }
-       
+
         if (Abs(first - current_first) > Precision::Confusion() ||
-           Abs(last - current_last) > Precision::Confusion() )
-         {
-           if (has_curve)
-             {
-               GeomLib::SameRange(Tolerance,
-                                  Curve2dPtr,
-                                  geometric_representation_ptr->First(),
-                                  geometric_representation_ptr->Last(),
-                                  current_first,
-                                  current_last,
-                                  NewCurve2dPtr);
-               geometric_representation_ptr->PCurve(NewCurve2dPtr) ;
-             }
-           if (has_closed_curve)
-             {
-               GeomLib::SameRange(Tolerance,
-                                  Curve2dPtr2,
-                                  geometric_representation_ptr->First(),
-                                  geometric_representation_ptr->Last(),
-                                  current_first,
-                                  current_last,
-                                  NewCurve2dPtr2);
-               geometric_representation_ptr->PCurve2(NewCurve2dPtr2) ;
-             }
-         }
+          Abs(last - current_last) > Precision::Confusion() )
+        {
+          if (has_curve)
+          {
+            GeomLib::SameRange(Tolerance,
+              Curve2dPtr,
+              geometric_representation_ptr->First(),
+              geometric_representation_ptr->Last(),
+              current_first,
+              current_last,
+              NewCurve2dPtr);
+            geometric_representation_ptr->PCurve(NewCurve2dPtr) ;
+          }
+          if (has_closed_curve)
+          {
+            GeomLib::SameRange(Tolerance,
+              Curve2dPtr2,
+              geometric_representation_ptr->First(),
+              geometric_representation_ptr->Last(),
+              current_first,
+              current_last,
+              NewCurve2dPtr2);
+            geometric_representation_ptr->PCurve2(NewCurve2dPtr2) ;
+          }
+        }
       }
     }
     an_Iterator.Next() ;
   }
   BRep_Builder B;
   B.Range(TopoDS::Edge(AnEdge),
-         current_first,
-         current_last) ;
+    current_first,
+    current_last) ;
 
   B.SameRange(AnEdge,
-             Standard_True) ;
+    Standard_True) ;
 }
 
 //=======================================================================
@@ -267,7 +267,7 @@ void BRepLib::SameRange(const TopoDS_Edge& AnEdge,
 //=======================================================================
 
 static Standard_Integer evaluateMaxSegment(const Standard_Integer aMaxSegment,
-                                          const Adaptor3d_CurveOnSurface& aCurveOnSurface)
+  const Adaptor3d_CurveOnSurface& aCurveOnSurface)
 {
   if (aMaxSegment != 0) return aMaxSegment;
 
@@ -275,7 +275,7 @@ static Standard_Integer evaluateMaxSegment(const Standard_Integer aMaxSegment,
   Handle(Adaptor2d_HCurve2d) aCurv2d = aCurveOnSurface.GetCurve();
 
   Standard_Real aNbSKnots = 0, aNbC2dKnots = 0;
-  
+
   if (aSurf->GetType() == GeomAbs_BSplineSurface) {
     Handle(Geom_BSplineSurface) aBSpline = aSurf->BSpline();
     aNbSKnots = Max(aBSpline->NbUKnots(), aBSpline->NbVKnots());
@@ -293,22 +293,22 @@ static Standard_Integer evaluateMaxSegment(const Standard_Integer aMaxSegment,
 //=======================================================================
 
 Standard_Boolean  BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
-                                       const Standard_Real Tolerance,
-                                       const GeomAbs_Shape Continuity,
-                                       const Standard_Integer MaxDegree,
-                                       const Standard_Integer MaxSegment)
+  const Standard_Real Tolerance,
+  const GeomAbs_Shape Continuity,
+  const Standard_Integer MaxDegree,
+  const Standard_Integer MaxSegment)
 {
   Standard_Integer //ErrorCode,
-//                   ReturnCode = 0,
-                   ii,
-//                   num_knots,
-                   jj;
+    //                   ReturnCode = 0,
+    ii,
+    //                   num_knots,
+    jj;
 
   TopLoc_Location LocalLoc,L[2],LC;
   Standard_Real f,l,fc,lc, first[2], last[2],
-  tolerance,
-  max_deviation,
-  average_deviation ;
+    tolerance,
+    max_deviation,
+    average_deviation ;
   Handle(Geom2d_Curve) Curve2dPtr, Curve2dArray[2]  ;
   Handle(Geom_Surface) SurfacePtr, SurfaceArray[2]  ;
 
@@ -319,17 +319,17 @@ Standard_Boolean  BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
   const Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,LocalLoc,f,l);
   if (!C.IsNull()) 
     return Standard_True;
-//
-// this should not exists but UpdateEdge makes funny things 
-// if the edge is not same range 
-//
+  //
+  // this should not exists but UpdateEdge makes funny things 
+  // if the edge is not same range 
+  //
   if (! CheckSameRange(AnEdge,
-                      Precision::Confusion())) {
-    SameRange(AnEdge,
-             Tolerance) ;
+    Precision::Confusion())) {
+      SameRange(AnEdge,
+        Tolerance) ;
   }
 
-    
+
 
   // search a curve on a plane
   Handle(Geom_Surface) S;
@@ -362,8 +362,8 @@ Standard_Boolean  BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
 
     BRep_Builder B;
     Standard_Boolean is_closed ;
-     is_closed = AnEdge.Closed() ;
-    
+    is_closed = AnEdge.Closed() ;
+
     B.UpdateEdge(AnEdge,C3d,LocalLoc,0.0e0);
     BRep_Tool::Range(AnEdge, S, LC, First, Last);
     B.Range(AnEdge, First, Last); //Do not forget 3D range.(PRO6412)
@@ -380,49 +380,49 @@ Standard_Boolean  BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
     if (!BRep_Tool::Degenerated(AnEdge)) {
       jj = 0 ;
       for (ii = 0 ; ii < 3 ; ii++ ) {
-       BRep_Tool::CurveOnSurface(TopoDS::Edge(AnEdge),
-                                 Curve2dPtr,
-                                 SurfacePtr,
-                                 LocalLoc,
-                                 fc,
-                                 lc,
-                                  ii) ;
-       
-       if (!Curve2dPtr.IsNull() && jj < 2){
-         Curve2dArray[jj] = Curve2dPtr ;
-         SurfaceArray[jj] = SurfacePtr ;
-         L[jj] = LocalLoc ;
-         first[jj] = fc ;
-         last[jj] = lc ;
-         jj += 1 ;
-       }
+        BRep_Tool::CurveOnSurface(TopoDS::Edge(AnEdge),
+          Curve2dPtr,
+          SurfacePtr,
+          LocalLoc,
+          fc,
+          lc,
+          ii) ;
+
+        if (!Curve2dPtr.IsNull() && jj < 2){
+          Curve2dArray[jj] = Curve2dPtr ;
+          SurfaceArray[jj] = SurfacePtr ;
+          L[jj] = LocalLoc ;
+          first[jj] = fc ;
+          last[jj] = lc ;
+          jj += 1 ;
+        }
       }
       f = first[0] ;
       l = last[0] ;
       Curve2dPtr = Curve2dArray[0] ;
       SurfacePtr = SurfaceArray[0] ;
-      
+
       Geom2dAdaptor_Curve     AnAdaptor3dCurve2d (Curve2dPtr, f, l) ;
       GeomAdaptor_Surface     AnAdaptor3dSurface (SurfacePtr) ;
       Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
-       new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
+        new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
       Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
-       new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
+        new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
       Adaptor3d_CurveOnSurface  CurveOnSurface( AnAdaptor3dCurve2dPtr,
-                                            AnAdaptor3dSurfacePtr) ;
-      
+        AnAdaptor3dSurfacePtr) ;
+
       Handle(Geom_Curve) NewCurvePtr ;
 
       GeomLib::BuildCurve3d(Tolerance,
-                           CurveOnSurface,
-                           f,
-                           l,
-                           NewCurvePtr,
-                           max_deviation,
-                           average_deviation,
-                           Continuity,
-                           MaxDegree,
-                           evaluateMaxSegment(MaxSegment,CurveOnSurface)) ;
+        CurveOnSurface,
+        f,
+        l,
+        NewCurvePtr,
+        max_deviation,
+        average_deviation,
+        Continuity,
+        MaxDegree,
+        evaluateMaxSegment(MaxSegment,CurveOnSurface)) ;
       BRep_Builder B;  
       tolerance = BRep_Tool::Tolerance(AnEdge) ;
       //Patch
@@ -433,24 +433,24 @@ Standard_Boolean  BRepLib::BuildCurve3d(const TopoDS_Edge& AnEdge,
       Standard_Boolean is_closed ;
       is_closed = AnEdge.Closed() ;
       B.UpdateEdge(TopoDS::Edge(AnEdge),
-                  NewCurvePtr,
-                  L[0],
-                  max_deviation) ;
+        NewCurvePtr,
+        L[0],
+        max_deviation) ;
       TopoDS_Edge  E = AnEdge ;
       E.Closed(is_closed) ;
       if (jj == 1 ) {
-//
-// if there is only one curve on surface attached to the edge
-// than it can be qualified sameparameter
-//
-       B.SameParameter(TopoDS::Edge(AnEdge),
-                       Standard_True) ;
+        //
+        // if there is only one curve on surface attached to the edge
+        // than it can be qualified sameparameter
+        //
+        B.SameParameter(TopoDS::Edge(AnEdge),
+          Standard_True) ;
       }
     }
     else {
       return Standard_False ;
     }
-       
+
   }         
   return Standard_True;
 }
@@ -463,7 +463,7 @@ Standard_Boolean  BRepLib::BuildCurves3d(const TopoDS_Shape& S)
 
 {
   return BRepLib::BuildCurves3d(S,
-                               1.0e-5) ;
+    1.0e-5) ;
 }
 
 //=======================================================================
@@ -472,22 +472,22 @@ Standard_Boolean  BRepLib::BuildCurves3d(const TopoDS_Shape& S)
 //=======================================================================
 
 Standard_Boolean  BRepLib::BuildCurves3d(const TopoDS_Shape& S,
-                                        const Standard_Real Tolerance,
-                                        const GeomAbs_Shape Continuity,
-                                        const Standard_Integer MaxDegree,
-                                        const Standard_Integer MaxSegment)
+  const Standard_Real Tolerance,
+  const GeomAbs_Shape Continuity,
+  const Standard_Integer MaxDegree,
+  const Standard_Integer MaxSegment)
 {
   Standard_Boolean boolean_value,
-  ok = Standard_True;
+    ok = Standard_True;
   TopTools_MapOfShape a_counter ;
   TopExp_Explorer ex(S,TopAbs_EDGE);
 
   while (ex.More()) {
     if (a_counter.Add(ex.Current())) {
       boolean_value = 
-       BuildCurve3d(TopoDS::Edge(ex.Current()),
-                    Tolerance, Continuity,
-                    MaxDegree, MaxSegment);
+        BuildCurve3d(TopoDS::Edge(ex.Current()),
+        Tolerance, Continuity,
+        MaxDegree, MaxSegment);
       ok = ok && boolean_value ;
     }
     ex.Next();
@@ -500,216 +500,216 @@ Standard_Boolean  BRepLib::BuildCurves3d(const TopoDS_Shape& S,
 //=======================================================================
 
 Standard_Boolean  BRepLib::UpdateEdgeTol(const TopoDS_Edge& AnEdge,
-                                        const Standard_Real MinToleranceRequested,
-                                        const Standard_Real MaxToleranceToCheck)
-  {
-    
-    Standard_Integer curve_on_surface_index,
-      curve_index,
-      not_done,
-      has_closed_curve,
-      has_curve,
-      jj,
-      ii,
-      geom_reference_curve_flag = 0,
-      max_sampling_points = 90,
-      min_sampling_points = 30 ;
-    
-    Standard_Real factor = 100.0e0,
- //     sampling_array[2],
-      safe_factor = 1.4e0,
-      current_last,
-      current_first,
-      max_distance,
-      coded_edge_tolerance,
-      edge_tolerance = 0.0e0 ;
-    Handle(TColStd_HArray1OfReal) parameters_ptr ;
-    Handle(BRep_GCurve) geometric_representation_ptr ;
-
-    if (BRep_Tool::Degenerated(AnEdge)) return Standard_False ;
-    coded_edge_tolerance = BRep_Tool::Tolerance(AnEdge) ;
-    if (coded_edge_tolerance > MaxToleranceToCheck) return Standard_False ;
-
-    const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&AnEdge.TShape());
-    BRep_ListOfCurveRepresentation& list_curve_rep = TE->ChangeCurves() ;
-    BRep_ListIteratorOfListOfCurveRepresentation an_iterator(list_curve_rep),
-      second_iterator(list_curve_rep) ;
-    Handle(Geom2d_Curve) curve2d_ptr, new_curve2d_ptr;
-    Handle(Geom_Surface) surface_ptr ;
-    TopLoc_Location local_location ;
-    GCPnts_QuasiUniformDeflection  a_sampler ;
-    GeomAdaptor_Curve  geom_reference_curve ;
-    Adaptor3d_CurveOnSurface  curve_on_surface_reference ; 
-    Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
-                                           local_location,
-                                           current_first,
-                                           current_last);
-    curve_on_surface_index = -1 ;
-    if (!C.IsNull()) {
-      if (! local_location.IsIdentity()) {
-       C = Handle(Geom_Curve)::
-         DownCast(C-> Transformed(local_location.Transformation()) ) ;
-      }
-      geom_reference_curve.Load(C) ;
-      geom_reference_curve_flag = 1 ;
-            a_sampler.Initialize(geom_reference_curve,
-                  MinToleranceRequested * factor,
-                  current_first,
-                  current_last) ;
+  const Standard_Real MinToleranceRequested,
+  const Standard_Real MaxToleranceToCheck)
+{
+
+  Standard_Integer curve_on_surface_index,
+    curve_index,
+    not_done,
+    has_closed_curve,
+    has_curve,
+    jj,
+    ii,
+    geom_reference_curve_flag = 0,
+    max_sampling_points = 90,
+    min_sampling_points = 30 ;
+
+  Standard_Real factor = 100.0e0,
+    //     sampling_array[2],
+    safe_factor = 1.4e0,
+    current_last,
+    current_first,
+    max_distance,
+    coded_edge_tolerance,
+    edge_tolerance = 0.0e0 ;
+  Handle(TColStd_HArray1OfReal) parameters_ptr ;
+  Handle(BRep_GCurve) geometric_representation_ptr ;
+
+  if (BRep_Tool::Degenerated(AnEdge)) return Standard_False ;
+  coded_edge_tolerance = BRep_Tool::Tolerance(AnEdge) ;
+  if (coded_edge_tolerance > MaxToleranceToCheck) return Standard_False ;
+
+  const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*)&AnEdge.TShape());
+  BRep_ListOfCurveRepresentation& list_curve_rep = TE->ChangeCurves() ;
+  BRep_ListIteratorOfListOfCurveRepresentation an_iterator(list_curve_rep),
+    second_iterator(list_curve_rep) ;
+  Handle(Geom2d_Curve) curve2d_ptr, new_curve2d_ptr;
+  Handle(Geom_Surface) surface_ptr ;
+  TopLoc_Location local_location ;
+  GCPnts_QuasiUniformDeflection  a_sampler ;
+  GeomAdaptor_Curve  geom_reference_curve ;
+  Adaptor3d_CurveOnSurface  curve_on_surface_reference ; 
+  Handle(Geom_Curve) C = BRep_Tool::Curve(AnEdge,
+    local_location,
+    current_first,
+    current_last);
+  curve_on_surface_index = -1 ;
+  if (!C.IsNull()) {
+    if (! local_location.IsIdentity()) {
+      C = Handle(Geom_Curve)::
+        DownCast(C-> Transformed(local_location.Transformation()) ) ;
     }
-    else {
-      not_done = 1 ;
-      curve_on_surface_index = 0 ;  
-
-      while (not_done && an_iterator.More()) {
-       geometric_representation_ptr =
-         Handle(BRep_GCurve)::DownCast(second_iterator.Value());
-       if (!geometric_representation_ptr.IsNull() 
-           && geometric_representation_ptr->IsCurveOnSurface()) {
-         curve2d_ptr = geometric_representation_ptr->PCurve() ;
-         local_location = geometric_representation_ptr->Location() ;
-         current_first = geometric_representation_ptr->First();
-         //first = geometric_representation_ptr->First();
-         current_last =  geometric_representation_ptr->Last();
-         // must be inverted 
-         //
-         if (! local_location.IsIdentity() ) {
-           surface_ptr = Handle(Geom_Surface)::
-             DownCast( geometric_representation_ptr->Surface()->
-                       Transformed(local_location.Transformation()) ) ;
-         }
-         else {
-           surface_ptr = 
-             geometric_representation_ptr->Surface() ;
-         }
-         not_done = 0 ;
-       }
-       curve_on_surface_index += 1 ;
+    geom_reference_curve.Load(C) ;
+    geom_reference_curve_flag = 1 ;
+    a_sampler.Initialize(geom_reference_curve,
+      MinToleranceRequested * factor,
+      current_first,
+      current_last) ;
+  }
+  else {
+    not_done = 1 ;
+    curve_on_surface_index = 0 ;  
+
+    while (not_done && an_iterator.More()) {
+      geometric_representation_ptr =
+        Handle(BRep_GCurve)::DownCast(second_iterator.Value());
+      if (!geometric_representation_ptr.IsNull() 
+        && geometric_representation_ptr->IsCurveOnSurface()) {
+          curve2d_ptr = geometric_representation_ptr->PCurve() ;
+          local_location = geometric_representation_ptr->Location() ;
+          current_first = geometric_representation_ptr->First();
+          //first = geometric_representation_ptr->First();
+          current_last =  geometric_representation_ptr->Last();
+          // must be inverted 
+          //
+          if (! local_location.IsIdentity() ) {
+            surface_ptr = Handle(Geom_Surface)::
+              DownCast( geometric_representation_ptr->Surface()->
+              Transformed(local_location.Transformation()) ) ;
+          }
+          else {
+            surface_ptr = 
+              geometric_representation_ptr->Surface() ;
+          }
+          not_done = 0 ;
       }
-      Geom2dAdaptor_Curve     AnAdaptor3dCurve2d (curve2d_ptr) ;
-      GeomAdaptor_Surface     AnAdaptor3dSurface (surface_ptr) ;
-      Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
-       new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
-      Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
-       new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
-      curve_on_surface_reference.Load( AnAdaptor3dCurve2dPtr) ;
-      curve_on_surface_reference.Load( AnAdaptor3dSurfacePtr) ;
-            a_sampler.Initialize(curve_on_surface_reference,
-                  MinToleranceRequested * factor,
-                  current_first,
-                  current_last) ;
-    }
-    TColStd_Array1OfReal   sampling_parameters(1,a_sampler.NbPoints()) ;
-    for (ii = 1 ; ii <= a_sampler.NbPoints() ; ii++) {
-      sampling_parameters(ii) = a_sampler.Parameter(ii) ;
+      curve_on_surface_index += 1 ;
     }
-    if (a_sampler.NbPoints() < min_sampling_points) {
-      GeomLib::DensifyArray1OfReal(min_sampling_points,
-                                 sampling_parameters,
-                                 parameters_ptr) ;
-    }
-    else if (a_sampler.NbPoints() > max_sampling_points) {
-      GeomLib::RemovePointsFromArray(max_sampling_points,
-                                   sampling_parameters,
-                                   parameters_ptr) ; 
-    }
-    else {
-      jj = 1 ;
-      parameters_ptr =
-       new TColStd_HArray1OfReal(1,sampling_parameters.Length()) ;
-      for (ii = sampling_parameters.Lower() ; ii <= sampling_parameters.Upper() ; ii++) {
-       parameters_ptr->ChangeArray1()(jj) =
-         sampling_parameters(ii) ;
-       jj +=1 ;
-      }
+    Geom2dAdaptor_Curve     AnAdaptor3dCurve2d (curve2d_ptr) ;
+    GeomAdaptor_Surface     AnAdaptor3dSurface (surface_ptr) ;
+    Handle(Geom2dAdaptor_HCurve) AnAdaptor3dCurve2dPtr =
+      new Geom2dAdaptor_HCurve(AnAdaptor3dCurve2d) ;
+    Handle(GeomAdaptor_HSurface) AnAdaptor3dSurfacePtr =
+      new GeomAdaptor_HSurface (AnAdaptor3dSurface) ;
+    curve_on_surface_reference.Load( AnAdaptor3dCurve2dPtr) ;
+    curve_on_surface_reference.Load( AnAdaptor3dSurfacePtr) ;
+    a_sampler.Initialize(curve_on_surface_reference,
+      MinToleranceRequested * factor,
+      current_first,
+      current_last) ;
+  }
+  TColStd_Array1OfReal   sampling_parameters(1,a_sampler.NbPoints()) ;
+  for (ii = 1 ; ii <= a_sampler.NbPoints() ; ii++) {
+    sampling_parameters(ii) = a_sampler.Parameter(ii) ;
+  }
+  if (a_sampler.NbPoints() < min_sampling_points) {
+    GeomLib::DensifyArray1OfReal(min_sampling_points,
+      sampling_parameters,
+      parameters_ptr) ;
+  }
+  else if (a_sampler.NbPoints() > max_sampling_points) {
+    GeomLib::RemovePointsFromArray(max_sampling_points,
+      sampling_parameters,
+      parameters_ptr) ; 
+  }
+  else {
+    jj = 1 ;
+    parameters_ptr =
+      new TColStd_HArray1OfReal(1,sampling_parameters.Length()) ;
+    for (ii = sampling_parameters.Lower() ; ii <= sampling_parameters.Upper() ; ii++) {
+      parameters_ptr->ChangeArray1()(jj) =
+        sampling_parameters(ii) ;
+      jj +=1 ;
     }
-        
-    curve_index = 0 ;
+  }
+
+  curve_index = 0 ;
+
+  while (second_iterator.More()) {
+    geometric_representation_ptr =
+      Handle(BRep_GCurve)::DownCast(second_iterator.Value());
+    if (! geometric_representation_ptr.IsNull() && 
+      curve_index != curve_on_surface_index) {
+        has_closed_curve =
+          has_curve = Standard_False ;
+        //     first = geometric_representation_ptr->First();
+        //     last =  geometric_representation_ptr->Last();
+        local_location = geometric_representation_ptr->Location() ;
+        if (geometric_representation_ptr->IsCurveOnSurface()) {
+          curve2d_ptr = geometric_representation_ptr->PCurve() ; 
+          has_curve = Standard_True ;
+        }
+        if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
+          curve2d_ptr = geometric_representation_ptr->PCurve2() ;
+          has_closed_curve = Standard_True ;
+        }
+
+        if (has_curve ||
+          has_closed_curve) {
+            if (! local_location.IsIdentity() ) {
+              surface_ptr = Handle(Geom_Surface)::
+                DownCast( geometric_representation_ptr->Surface()->
+                Transformed(local_location.Transformation()) ) ;
+            }
+            else {
+              surface_ptr = 
+                geometric_representation_ptr->Surface() ;
+            }
+            Geom2dAdaptor_Curve     an_adaptor_curve2d (curve2d_ptr) ;
+            GeomAdaptor_Surface     an_adaptor_surface(surface_ptr) ;
+            Handle(Geom2dAdaptor_HCurve) an_adaptor_curve2d_ptr =
+              new Geom2dAdaptor_HCurve(an_adaptor_curve2d) ;
+            Handle(GeomAdaptor_HSurface) an_adaptor_surface_ptr =
+              new GeomAdaptor_HSurface (an_adaptor_surface) ;
+            Adaptor3d_CurveOnSurface a_curve_on_surface(an_adaptor_curve2d_ptr,
+              an_adaptor_surface_ptr) ;
+
+            if (BRep_Tool::SameParameter(AnEdge)) {
+
+              GeomLib::EvalMaxParametricDistance(a_curve_on_surface,
+                geom_reference_curve,
+                MinToleranceRequested,
+                parameters_ptr->Array1(),
+                max_distance) ;
+            }
+            else if (geom_reference_curve_flag) {
+              GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
+                geom_reference_curve,
+                MinToleranceRequested,
+                parameters_ptr->Array1(),
+                max_distance) ;
+            }
+            else {
+
+              GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
+                curve_on_surface_reference,
+                MinToleranceRequested,
+                parameters_ptr->Array1(),
+                max_distance) ;
+            }
+            max_distance *= safe_factor ;
+            edge_tolerance = Max(max_distance, edge_tolerance) ;
+        }
 
-    while (second_iterator.More()) {
-      geometric_representation_ptr =
-       Handle(BRep_GCurve)::DownCast(second_iterator.Value());
-      if (! geometric_representation_ptr.IsNull() && 
-         curve_index != curve_on_surface_index) {
-       has_closed_curve =
-         has_curve = Standard_False ;
-//     first = geometric_representation_ptr->First();
-//     last =  geometric_representation_ptr->Last();
-       local_location = geometric_representation_ptr->Location() ;
-       if (geometric_representation_ptr->IsCurveOnSurface()) {
-         curve2d_ptr = geometric_representation_ptr->PCurve() ; 
-         has_curve = Standard_True ;
-       }
-       if (geometric_representation_ptr->IsCurveOnClosedSurface()) {
-         curve2d_ptr = geometric_representation_ptr->PCurve2() ;
-         has_closed_curve = Standard_True ;
-       }
-       
-       if (has_curve ||
-           has_closed_curve) {
-         if (! local_location.IsIdentity() ) {
-           surface_ptr = Handle(Geom_Surface)::
-             DownCast( geometric_representation_ptr->Surface()->
-                     Transformed(local_location.Transformation()) ) ;
-         }
-         else {
-           surface_ptr = 
-             geometric_representation_ptr->Surface() ;
-         }
-         Geom2dAdaptor_Curve     an_adaptor_curve2d (curve2d_ptr) ;
-         GeomAdaptor_Surface     an_adaptor_surface(surface_ptr) ;
-         Handle(Geom2dAdaptor_HCurve) an_adaptor_curve2d_ptr =
-           new Geom2dAdaptor_HCurve(an_adaptor_curve2d) ;
-         Handle(GeomAdaptor_HSurface) an_adaptor_surface_ptr =
-           new GeomAdaptor_HSurface (an_adaptor_surface) ;
-         Adaptor3d_CurveOnSurface a_curve_on_surface(an_adaptor_curve2d_ptr,
-                                                   an_adaptor_surface_ptr) ;
-       
-         if (BRep_Tool::SameParameter(AnEdge)) {
-           
-           GeomLib::EvalMaxParametricDistance(a_curve_on_surface,
-                                              geom_reference_curve,
-                                              MinToleranceRequested,
-                                              parameters_ptr->Array1(),
-                                              max_distance) ;
-         }
-         else if (geom_reference_curve_flag) {
-           GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
-                                                  geom_reference_curve,
-                                                  MinToleranceRequested,
-                                                  parameters_ptr->Array1(),
-                                                  max_distance) ;
-         }
-         else {
-           
-           GeomLib::EvalMaxDistanceAlongParameter(a_curve_on_surface,
-                                                  curve_on_surface_reference,
-                                                  MinToleranceRequested,
-                                                  parameters_ptr->Array1(),
-                                                  max_distance) ;
-         }
-         max_distance *= safe_factor ;
-         edge_tolerance = Max(max_distance, edge_tolerance) ;
-       }
-       
 
-      }
-      curve_index += 1 ;
-      second_iterator.Next() ; 
     }
-    
-    TE->Tolerance(edge_tolerance);
-    return Standard_True ;
-    
+    curve_index += 1 ;
+    second_iterator.Next() ; 
   }
+
+  TE->Tolerance(edge_tolerance);
+  return Standard_True ;
+
+}
 //=======================================================================
 //function : UpdateEdgeTolerance
 //purpose  : 
 //=======================================================================
 
 Standard_Boolean BRepLib::UpdateEdgeTolerance(const TopoDS_Shape& S,
-                                             const Standard_Real MinToleranceRequested,
-                                             const Standard_Real MaxToleranceToCheck) 
+  const Standard_Real MinToleranceRequested,
+  const Standard_Real MaxToleranceToCheck) 
 {
   TopExp_Explorer ex(S,TopAbs_EDGE);
   TopTools_MapOfShape  a_counter ;
@@ -720,11 +720,11 @@ Standard_Boolean BRepLib::UpdateEdgeTolerance(const TopoDS_Shape& S,
   while (ex.More()) {
     if (a_counter.Add(ex.Current())) {
       local_flag =
-       BRepLib::UpdateEdgeTol(TopoDS::Edge(ex.Current()),
-                              MinToleranceRequested,
-                              MaxToleranceToCheck) ;
+        BRepLib::UpdateEdgeTol(TopoDS::Edge(ex.Current()),
+        MinToleranceRequested,
+        MaxToleranceToCheck) ;
       if (local_flag && ! return_status) {
-       return_status = Standard_True ;
+        return_status = Standard_True ;
       }
     }
     ex.Next();
@@ -738,7 +738,7 @@ Standard_Boolean BRepLib::UpdateEdgeTolerance(const TopoDS_Shape& S,
 //=======================================================================
 
 static void SetEdgeTol(const TopoDS_Edge& E,
-                      const TopoDS_Face& F)
+  const TopoDS_Face& F)
 {
   TopLoc_Location L;
   const Handle(Geom_Surface)& S = BRep_Tool::Surface(F,L);
@@ -763,7 +763,7 @@ static void SetEdgeTol(const TopoDS_Edge& E,
 
   Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve();
   Handle(GeomAdaptor_HSurface) HS = new GeomAdaptor_HSurface();
-  
+
   TopLoc_Location LC;
   Standard_Real First, Last;
   GeomAdaptor_Curve& GAC = HC->ChangeCurve();
@@ -772,11 +772,11 @@ static void SetEdgeTol(const TopoDS_Edge& E,
 
   if (!LC.IsIdentity()) {
     GP = Handle(Geom_Plane)::DownCast(
-        GP->Transformed(LC.Transformation()));
+      GP->Transformed(LC.Transformation()));
   }
   GeomAdaptor_Surface& GAS = HS->ChangeSurface();
   GAS.Load(GP);
-    
+
   ProjLib_ProjectedCurve Proj(HS,HC);
   Handle(Geom2d_Curve) pc = Geom2dAdaptor::MakeCurve(Proj);
 
@@ -802,8 +802,8 @@ static void SetEdgeTol(const TopoDS_Edge& E,
 //purpose  : 
 //=======================================================================
 void  BRepLib::SameParameter(const TopoDS_Shape& S,
-                            const Standard_Real Tolerance,
-                            const Standard_Boolean forced) 
+  const Standard_Real Tolerance,
+  const Standard_Boolean forced) 
 {
   TopExp_Explorer ex(S,TopAbs_EDGE);
   TopTools_MapOfShape  Done;
@@ -812,8 +812,8 @@ void  BRepLib::SameParameter(const TopoDS_Shape& S,
   while (ex.More()) {
     if (Done.Add(ex.Current())) {
       if (forced) {
-       brB.SameRange(TopoDS::Edge(ex.Current()), Standard_False);
-       brB.SameParameter(TopoDS::Edge(ex.Current()), Standard_False);
+        brB.SameRange(TopoDS::Edge(ex.Current()), Standard_False);
+        brB.SameParameter(TopoDS::Edge(ex.Current()), Standard_False);
       }
       BRepLib::SameParameter(TopoDS::Edge(ex.Current()),Tolerance);
     }
@@ -843,10 +843,10 @@ void  BRepLib::SameParameter(const TopoDS_Shape& S,
 //  for vertex extremities it is required to find something else
 //================================================================
 static Standard_Boolean EvalTol(const Handle(Geom2d_Curve)& pc,
-                               const Handle(Geom_Surface)& s,
-                               const GeomAdaptor_Curve&    gac,
-                               const Standard_Real         tol,
-                               Standard_Real&              tolbail)
+  const Handle(Geom_Surface)& s,
+  const GeomAdaptor_Curve&    gac,
+  const Standard_Real         tol,
+  Standard_Real&              tolbail)
 {
   Standard_Integer ok = 0;
   Standard_Real f = gac.FirstParameter();
@@ -872,33 +872,30 @@ static Standard_Boolean EvalTol(const Handle(Geom2d_Curve)& pc,
 }
 
 static Standard_Real ComputeTol(const Handle(Adaptor3d_HCurve)& c3d,
-                               const Handle(Adaptor2d_HCurve2d)& c2d,
-                               const Handle(Adaptor3d_HSurface)& surf,
-                               const Standard_Integer        nbp)
+  const Handle(Adaptor2d_HCurve2d)& c2d,
+  const Handle(Adaptor3d_HSurface)& surf,
+  const Standard_Integer        nbp)
 
 {
 
   TColStd_Array1OfReal dist(1,nbp+10);
   dist.Init(-1.);
 
-
   Adaptor3d_CurveOnSurface  cons(c2d,surf);
   Standard_Real d2 = 0.;
-  Standard_Integer nn = nbp;
-  Standard_Real unsurnn = 1./nn;
   Standard_Real first = c3d->FirstParameter();
   Standard_Real last  = c3d->LastParameter();
   Standard_Integer i = 0;
-  for(i = 0; i <= nn; i++){
-    Standard_Real t = unsurnn*i;
-    Standard_Real u = first*(1.-t) + last*t;
+  for(i = 0; i <= nbp; i++){
+    const Standard_Real t = IntToReal(i)/IntToReal(nbp);
+    const Standard_Real u = first*(1.-t) + last*t;
     gp_Pnt Pc3d = c3d->Value(u);
     gp_Pnt Pcons = cons.Value(u);
     if (Precision::IsInfinite(Pcons.X()) ||
-       Precision::IsInfinite(Pcons.Y()) ||
-       Precision::IsInfinite(Pcons.Z())) {
-      d2=Precision::Infinite();
-      break;
+      Precision::IsInfinite(Pcons.Y()) ||
+      Precision::IsInfinite(Pcons.Z())) {
+        d2=Precision::Infinite();
+        break;
     }
     Standard_Real temp = Pc3d.SquareDistance(Pcons);
 
@@ -922,25 +919,23 @@ static Standard_Real ComputeTol(const Handle(Adaptor3d_HCurve)& c3d,
       else N2++;
     }
 
-  if( N1 > N2 && N2 != 0 ) N3 = 100*N2/(N1+N2);
-  if( N3 < 10 && N3 != 0 ) {
-    ana = Standard_True;
-    for( i = 1; i<= nbp+10; i++)
-      if( dist(i) > 0 && dist(i) < 1.0 )
-       if( dist(i) > D2 ) D2 = dist(i);
-  }
+    if( N1 > N2 && N2 != 0 ) N3 = 100*N2/(N1+N2);
+    if( N3 < 10 && N3 != 0 ) {
+      ana = Standard_True;
+      for( i = 1; i<= nbp+10; i++)
+        if( dist(i) > 0 && dist(i) < 1.0 )
+          if( dist(i) > D2 ) D2 = dist(i);
+    }
 
-  //d2 = 1.5*sqrt(d2);
-  d2 = (!ana) ? 1.5*sqrt(d2) : 1.5*sqrt(D2);
-  if(d2<1.e-7) d2 = 1.e-7;
+    //d2 = 1.5*sqrt(d2);
+    d2 = (!ana) ? 1.5*sqrt(d2) : 1.5*sqrt(D2);
+    if(d2<1.e-7) d2 = 1.e-7;
 
-  return d2;
+    return d2;
 }
 
-
-
 void BRepLib::SameParameter(const TopoDS_Edge&  AnEdge,
-                           const Standard_Real Tolerance)
+  const Standard_Real Tolerance)
 {
   if (BRep_Tool::SameParameter(AnEdge)) return;
 
@@ -959,7 +954,7 @@ void BRepLib::SameParameter(const TopoDS_Edge&  AnEdge,
   const Handle(BRep_TEdge)& TE = *((Handle(BRep_TEdge)*) &AnEdge.TShape());
   BRep_ListOfCurveRepresentation& CList = TE->ChangeCurves();
   BRep_ListIteratorOfListOfCurveRepresentation It(CList);
-  
+
   Standard_Boolean NotDone = Standard_True;
 
   while (NotDone && It.More()) {
@@ -980,10 +975,10 @@ void BRepLib::SameParameter(const TopoDS_Edge&  AnEdge,
   Standard_Boolean m_TrimmedPeriodical = Standard_False;
   Handle(Standard_Type) TheType = C3d->DynamicType();
   if( TheType == STANDARD_TYPE(Geom_TrimmedCurve))
-    {
-      const Handle(Geom_Curve)& gtC = (*((Handle(Geom_TrimmedCurve)*)&C3d))->BasisCurve();
-      m_TrimmedPeriodical = gtC->IsPeriodic();
-    }
+  {
+    const Handle(Geom_Curve)& gtC = (*((Handle(Geom_TrimmedCurve)*)&C3d))->BasisCurve();
+    m_TrimmedPeriodical = gtC->IsPeriodic();
+  }
   // modified by NIZHNY-OCC486  Tue Aug 27 17:15:17 2002 .
 
   BRep_Builder B;
@@ -994,10 +989,10 @@ void BRepLib::SameParameter(const TopoDS_Edge&  AnEdge,
     //if (Udeb > f3d) f3d = Udeb;
     //if (l3d > Ufin) l3d = Ufin;
     if(!m_TrimmedPeriodical)
-      {
-       if (Udeb > f3d) f3d = Udeb;
-       if (l3d > Ufin) l3d = Ufin;
-      }
+    {
+      if (Udeb > f3d) f3d = Udeb;
+      if (l3d > Ufin) l3d = Ufin;
+    }
     // modified by NIZHNY-OCC486  Tue Aug 27 17:17:55 2002 .
   }
   if(!L3d.IsIdentity()){
@@ -1008,9 +1003,9 @@ void BRepLib::SameParameter(const TopoDS_Edge&  AnEdge,
   Standard_Boolean IsSameP = 1;
   Standard_Real maxdist = 0.;
 
-//  Modified by skv - Thu Jun  3 12:39:19 2004 OCC5898 Begin
+  //  Modified by skv - Thu Jun  3 12:39:19 2004 OCC5898 Begin
   Standard_Real anEdgeTol = BRep_Tool::Tolerance(AnEdge);
-//  Modified by skv - Thu Jun  3 12:39:20 2004 OCC5898 End
+  //  Modified by skv - Thu Jun  3 12:39:20 2004 OCC5898 End
   Standard_Boolean SameRange = BRep_Tool::SameRange(AnEdge);
   Standard_Boolean YaPCu = Standard_False;
   It.Initialize(CList);
@@ -1027,252 +1022,253 @@ void BRepLib::SameParameter(const TopoDS_Edge&  AnEdge,
       TopLoc_Location PCLoc = GCurve->Location();
       S = GCurve->Surface();
       if (!PCLoc.IsIdentity() ) {
-       S = Handle(Geom_Surface)::DownCast(S->Transformed(PCLoc.Transformation()));
+        S = Handle(Geom_Surface)::DownCast(S->Transformed(PCLoc.Transformation()));
       }
+
       GAS.Load(S);
       if (GCurve->IsCurveOnClosedSurface()) {
-       PC[1] = GCurve->PCurve2();
+        PC[1] = GCurve->PCurve2();
       }
-      
+
       // Eval tol2d to compute SameRange
       Standard_Real UResol = Max(GAS.UResolution(Tolerance), Precision::PConfusion());
       Standard_Real VResol = Max(GAS.VResolution(Tolerance), Precision::PConfusion());
       Standard_Real Tol2d  = Min(UResol, VResol);
       for(Standard_Integer i = 0; i < 2; i++){
-       Handle(Geom2d_Curve) curPC = PC[i];
-       Standard_Boolean updatepc = 0;
-       if(curPC.IsNull()) break;
-       if(!SameRange){
-         GeomLib::SameRange(Tol2d,
-                            PC[i],GCurve->First(),GCurve->Last(),
-                            f3d,l3d,curPC);
-
-         updatepc = (curPC != PC[i]);
-
-       }
-       Standard_Boolean goodpc = 1;
-       GAC2d.Load(curPC,f3d,l3d);
-
-       Standard_Real error = ComputeTol(HC, HC2d, HS, NCONTROL);
-
-       if(GAC2d.GetType() == GeomAbs_BSplineCurve && 
-          GAC2d.Continuity() == GeomAbs_C0) {
-         Handle(Geom2d_BSplineCurve) bs2d = GAC2d.BSpline();
-         Handle(Geom2d_BSplineCurve) bs2dsov = bs2d;
-         Standard_Real fC0 = bs2d->FirstParameter(), lC0 = bs2d->LastParameter();
-         Standard_Boolean repar = Standard_True;
-         gp_Pnt2d OriginPoint;
-         bs2d->D0(fC0, OriginPoint);
-         Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d, Tol2d);
-         isBSP = Standard_True; 
-
-         if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
-           gp_Pnt2d NewOriginPoint;
-           bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
-           if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
-              Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion()    ) {
-
-             TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
-             bs2d->Knots(Knotbs2d);
-
-             for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
-               bs2d->D0(Knotbs2d(Index), NewOriginPoint);
-               if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
-                  Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion()    ) continue;
-               
-               bs2d->SetOrigin(Index);
-               break;
-             }
-           }
-         }
-
-         if(bs2d->Continuity() == GeomAbs_C0) {
-           Standard_Real tolbail;
-           if(EvalTol(curPC,S,GAC,Tolerance,tolbail)){
-             bs2d = bs2dsov;
-             Standard_Real UResbail = GAS.UResolution(tolbail);
-             Standard_Real VResbail = GAS.VResolution(tolbail);
-             Standard_Real Tol2dbail  = Min(UResbail,VResbail);
-             bs2d->D0(bs2d->FirstParameter(), OriginPoint); 
-
-             Standard_Integer nbp = bs2d->NbPoles();
-             TColgp_Array1OfPnt2d poles(1,nbp);
-             bs2d->Poles(poles);
-             gp_Pnt2d p = poles(1), p1;
-             Standard_Real d = Precision::Infinite();
-             for(Standard_Integer ip = 2; ip <= nbp; ip++) {
-               p1 = poles(ip);
-               d = Min(d,p.SquareDistance(p1));
-               p = p1;
-             }
-             d = sqrt(d)*.1;
-
-             Tol2dbail = Max(Min(Tol2dbail,d),Tol2d);
-
-             Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d,Tol2dbail);
-
-             if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
-               gp_Pnt2d NewOriginPoint;
-               bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
-               if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
-                  Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion()    ) {
-
-                 TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
-                 bs2d->Knots(Knotbs2d);
-
-                 for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
-                   bs2d->D0(Knotbs2d(Index), NewOriginPoint);
-                   if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
-                      Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion()    ) continue;
-                   
-                   bs2d->SetOrigin(Index);
-                   break;
-                 }
-               }
-             }
-             
-             
-             if(bs2d->Continuity() == GeomAbs_C0) {
-               goodpc = 1;
-               bs2d = bs2dsov;
-               repar = Standard_False;
-             }
-           }
-           else goodpc = 0;
-         }
-
-         if(goodpc){
-           if(repar) {
-             Standard_Integer NbKnots = bs2d->NbKnots();
-             TColStd_Array1OfReal Knots(1,NbKnots);
-             bs2d->Knots(Knots);
-             //            BSplCLib::Reparametrize(f3d,l3d,Knots);
-             BSplCLib::Reparametrize(fC0,lC0,Knots);
-             bs2d->SetKnots(Knots);
-             GAC2d.Load(bs2d,f3d,l3d);
-             curPC = bs2d;
-             Standard_Boolean updatepcsov = updatepc;
-             updatepc = Standard_True;
-
-             Standard_Real error1 = ComputeTol(HC, HC2d, HS, NCONTROL);
-             if(error1 > error) {
-               bs2d = bs2dsov;
-               GAC2d.Load(bs2d,f3d,l3d);
-               curPC = bs2d;
-               updatepc = updatepcsov;
-               isANA = Standard_True;
-             }
-             else {
-               error = error1;
-             }
-           }
-
-           //check, if new BSpline "good" or not --------- IFV, Jan of 2000
-           GeomAbs_Shape cont = bs2d->Continuity();
-           Standard_Boolean IsBad = Standard_False;
-
-           if(cont > GeomAbs_C0 && error > Max(1.e-3,Tolerance)) {
-             Standard_Integer NbKnots = bs2d->NbKnots();
-             TColStd_Array1OfReal Knots(1,NbKnots);
-             bs2d->Knots(Knots);
-             Standard_Real critratio = 10.; 
-             Standard_Real dtprev = Knots(2) - Knots(1), dtratio = 1.;
-             Standard_Real dtmin = dtprev;
-             Standard_Real dtcur;
-             for(Standard_Integer j = 2; j < NbKnots; j++) {
-               dtcur = Knots(j+1) - Knots(j);
-               dtmin = Min(dtmin, dtcur);
-
-               if(IsBad) continue;
-
-               if(dtcur > dtprev) dtratio = dtcur/dtprev;
-               else dtratio = dtprev/dtcur;
-               if(dtratio > critratio) {IsBad = Standard_True;}
-               dtprev = dtcur;
-               
-             }
-             if(IsBad) {
-               // To avoid failures in Approx_CurvilinearParameter 
-               bs2d->Resolution(Max(1.e-3,Tolerance), dtcur);
-               if(dtmin < dtcur) IsBad = Standard_False;
-             }
-           }
-
-
-           if(IsBad ) { //if BSpline "bad", try to reparametrize it
-                                         // by its curve length
-
-//           GeomAbs_Shape cont = bs2d->Continuity();
-             if(cont > GeomAbs_C2) cont = GeomAbs_C2;
-             Standard_Integer maxdeg = bs2d->Degree();
-             if(maxdeg == 1) maxdeg = 14;
-             Approx_CurvilinearParameter AppCurPar(HC2d, HS, Max(1.e-3,Tolerance),
-                                                   cont, maxdeg, 10);
-             if(AppCurPar.IsDone() || AppCurPar.HasResult()) {
-               bs2d = AppCurPar.Curve2d1();
-               GAC2d.Load(bs2d,f3d,l3d);
-               curPC = bs2d;
-
-               if(Abs(bs2d->FirstParameter() - fC0) > Tol2d ||
-                  Abs(bs2d->LastParameter() - lC0) > Tol2d    ) {
-                 Standard_Integer NbKnots = bs2d->NbKnots();
-                 TColStd_Array1OfReal Knots(1,NbKnots);
-                 bs2d->Knots(Knots);
-//               BSplCLib::Reparametrize(f3d,l3d,Knots);
-                 BSplCLib::Reparametrize(fC0,lC0,Knots);
-                 bs2d->SetKnots(Knots);
-                 GAC2d.Load(bs2d,f3d,l3d);
-                 curPC = bs2d;
-
-               }
-             }
-           }
-
-    
-         }
-       }
-
-
-       if(goodpc){
-//       Approx_SameParameter SameP(HC,HC2d,HS,Tolerance);
-         Standard_Real aTol = (isANA && isBSP) ? 1.e-7 : Tolerance;
-         Approx_SameParameter SameP(HC,HC2d,HS,aTol);
-         
-         if (SameP.IsSameParameter()) {
-           maxdist = Max(maxdist,SameP.TolReached());
-           if(updatepc){
-             if (i == 0) GCurve->PCurve(curPC);
-             else GCurve->PCurve2(curPC);
-           }
-         }
-         else if (SameP.IsDone()) {
-           Standard_Real tolreached = SameP.TolReached();
-           if(tolreached < error) {
-             curPC = SameP.Curve2d();
-             updatepc = Standard_True;
-             maxdist = Max(maxdist,tolreached);
-           }
-           else {
-             maxdist = Max(maxdist, error);
-           }
-           if(updatepc){
-             if (i == 0) GCurve->PCurve(curPC);
-             else GCurve->PCurve2(curPC);
-           }
-         }
-         else IsSameP = 0;
-       
-       }
-       else IsSameP = 0;
-
-//  Modified by skv - Thu Jun  3 12:39:19 2004 OCC5898 Begin
-       if (!IsSameP) {
-         if (anEdgeTol > error) {
-           maxdist = Max(maxdist, anEdgeTol);
-           IsSameP = Standard_True;
-         }
-       }
-//  Modified by skv - Thu Jun  3 12:39:20 2004 OCC5898 End
+        Handle(Geom2d_Curve) curPC = PC[i];
+        Standard_Boolean updatepc = 0;
+        if(curPC.IsNull()) break;
+        if(!SameRange){
+          GeomLib::SameRange(Tol2d,
+            PC[i],GCurve->First(),GCurve->Last(),
+            f3d,l3d,curPC);
+
+          updatepc = (curPC != PC[i]);
+
+        }
+        Standard_Boolean goodpc = 1;
+        GAC2d.Load(curPC,f3d,l3d);
+
+        Standard_Real error = ComputeTol(HC, HC2d, HS, NCONTROL);
+
+        if(GAC2d.GetType() == GeomAbs_BSplineCurve && 
+          GAC2d.Continuity() == GeomAbs_C0) {
+            Handle(Geom2d_BSplineCurve) bs2d = GAC2d.BSpline();
+            Handle(Geom2d_BSplineCurve) bs2dsov = bs2d;
+            Standard_Real fC0 = bs2d->FirstParameter(), lC0 = bs2d->LastParameter();
+            Standard_Boolean repar = Standard_True;
+            gp_Pnt2d OriginPoint;
+            bs2d->D0(fC0, OriginPoint);
+            Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d, Tol2d);
+            isBSP = Standard_True; 
+
+            if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
+              gp_Pnt2d NewOriginPoint;
+              bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
+              if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
+                Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion()    ) {
+
+                  TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
+                  bs2d->Knots(Knotbs2d);
+
+                  for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
+                    bs2d->D0(Knotbs2d(Index), NewOriginPoint);
+                    if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
+                      Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion()    ) continue;
+
+                    bs2d->SetOrigin(Index);
+                    break;
+                  }
+              }
+            }
+
+            if(bs2d->Continuity() == GeomAbs_C0) {
+              Standard_Real tolbail;
+              if(EvalTol(curPC,S,GAC,Tolerance,tolbail)){
+                bs2d = bs2dsov;
+                Standard_Real UResbail = GAS.UResolution(tolbail);
+                Standard_Real VResbail = GAS.VResolution(tolbail);
+                Standard_Real Tol2dbail  = Min(UResbail,VResbail);
+                bs2d->D0(bs2d->FirstParameter(), OriginPoint); 
+
+                Standard_Integer nbp = bs2d->NbPoles();
+                TColgp_Array1OfPnt2d poles(1,nbp);
+                bs2d->Poles(poles);
+                gp_Pnt2d p = poles(1), p1;
+                Standard_Real d = Precision::Infinite();
+                for(Standard_Integer ip = 2; ip <= nbp; ip++) {
+                  p1 = poles(ip);
+                  d = Min(d,p.SquareDistance(p1));
+                  p = p1;
+                }
+                d = sqrt(d)*.1;
+
+                Tol2dbail = Max(Min(Tol2dbail,d),Tol2d);
+
+                Geom2dConvert::C0BSplineToC1BSplineCurve(bs2d,Tol2dbail);
+
+                if(bs2d->IsPeriodic()) { // -------- IFV, Jan 2000
+                  gp_Pnt2d NewOriginPoint;
+                  bs2d->D0(bs2d->FirstParameter(), NewOriginPoint);
+                  if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
+                    Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion()    ) {
+
+                      TColStd_Array1OfReal Knotbs2d (1, bs2d->NbKnots());
+                      bs2d->Knots(Knotbs2d);
+
+                      for(Standard_Integer Index = 1; Index <= bs2d->NbKnots(); Index++) {
+                        bs2d->D0(Knotbs2d(Index), NewOriginPoint);
+                        if(Abs(OriginPoint.X() - NewOriginPoint.X()) > Precision::PConfusion() ||
+                          Abs(OriginPoint.Y() - NewOriginPoint.Y()) > Precision::PConfusion()    ) continue;
+
+                        bs2d->SetOrigin(Index);
+                        break;
+                      }
+                  }
+                }
+
+
+                if(bs2d->Continuity() == GeomAbs_C0) {
+                  goodpc = 1;
+                  bs2d = bs2dsov;
+                  repar = Standard_False;
+                }
+              }
+              else goodpc = 0;
+            }
+
+            if(goodpc){
+              if(repar) {
+                Standard_Integer NbKnots = bs2d->NbKnots();
+                TColStd_Array1OfReal Knots(1,NbKnots);
+                bs2d->Knots(Knots);
+                //         BSplCLib::Reparametrize(f3d,l3d,Knots);
+                BSplCLib::Reparametrize(fC0,lC0,Knots);
+                bs2d->SetKnots(Knots);
+                GAC2d.Load(bs2d,f3d,l3d);
+                curPC = bs2d;
+                Standard_Boolean updatepcsov = updatepc;
+                updatepc = Standard_True;
+
+                Standard_Real error1 = ComputeTol(HC, HC2d, HS, NCONTROL);
+                if(error1 > error) {
+                  bs2d = bs2dsov;
+                  GAC2d.Load(bs2d,f3d,l3d);
+                  curPC = bs2d;
+                  updatepc = updatepcsov;
+                  isANA = Standard_True;
+                }
+                else {
+                  error = error1;
+                }
+              }
+
+              //check, if new BSpline "good" or not --------- IFV, Jan of 2000
+              GeomAbs_Shape cont = bs2d->Continuity();
+              Standard_Boolean IsBad = Standard_False;
+
+              if(cont > GeomAbs_C0 && error > Max(1.e-3,Tolerance)) {
+                Standard_Integer NbKnots = bs2d->NbKnots();
+                TColStd_Array1OfReal Knots(1,NbKnots);
+                bs2d->Knots(Knots);
+                Standard_Real critratio = 10.; 
+                Standard_Real dtprev = Knots(2) - Knots(1), dtratio = 1.;
+                Standard_Real dtmin = dtprev;
+                Standard_Real dtcur;
+                for(Standard_Integer j = 2; j < NbKnots; j++) {
+                  dtcur = Knots(j+1) - Knots(j);
+                  dtmin = Min(dtmin, dtcur);
+
+                  if(IsBad) continue;
+
+                  if(dtcur > dtprev) dtratio = dtcur/dtprev;
+                  else dtratio = dtprev/dtcur;
+                  if(dtratio > critratio) {IsBad = Standard_True;}
+                  dtprev = dtcur;
+
+                }
+                if(IsBad) {
+                  // To avoid failures in Approx_CurvilinearParameter 
+                  bs2d->Resolution(Max(1.e-3,Tolerance), dtcur);
+                  if(dtmin < dtcur) IsBad = Standard_False;
+                }
+              }
+
+
+              if(IsBad ) { //if BSpline "bad", try to reparametrize it
+                // by its curve length
+
+                //           GeomAbs_Shape cont = bs2d->Continuity();
+                if(cont > GeomAbs_C2) cont = GeomAbs_C2;
+                Standard_Integer maxdeg = bs2d->Degree();
+                if(maxdeg == 1) maxdeg = 14;
+                Approx_CurvilinearParameter AppCurPar(HC2d, HS, Max(1.e-3,Tolerance),
+                  cont, maxdeg, 10);
+                if(AppCurPar.IsDone() || AppCurPar.HasResult()) {
+                  bs2d = AppCurPar.Curve2d1();
+                  GAC2d.Load(bs2d,f3d,l3d);
+                  curPC = bs2d;
+
+                  if(Abs(bs2d->FirstParameter() - fC0) > Tol2d ||
+                    Abs(bs2d->LastParameter() - lC0) > Tol2d    ) {
+                      Standard_Integer NbKnots = bs2d->NbKnots();
+                      TColStd_Array1OfReal Knots(1,NbKnots);
+                      bs2d->Knots(Knots);
+                      //                 BSplCLib::Reparametrize(f3d,l3d,Knots);
+                      BSplCLib::Reparametrize(fC0,lC0,Knots);
+                      bs2d->SetKnots(Knots);
+                      GAC2d.Load(bs2d,f3d,l3d);
+                      curPC = bs2d;
+
+                  }
+                }
+              }
+
+
+            }
+        }
+
+
+        if(goodpc){
+          //     Approx_SameParameter SameP(HC,HC2d,HS,Tolerance);
+          Standard_Real aTol = (isANA && isBSP) ? 1.e-7 : Tolerance;
+          Approx_SameParameter SameP(HC,HC2d,HS,aTol);
+
+          if (SameP.IsSameParameter()) {
+            maxdist = Max(maxdist,SameP.TolReached());
+            if(updatepc){
+              if (i == 0) GCurve->PCurve(curPC);
+              else GCurve->PCurve2(curPC);
+            }
+          }
+          else if (SameP.IsDone()) {
+            Standard_Real tolreached = SameP.TolReached();
+            if(tolreached < error) {
+              curPC = SameP.Curve2d();
+              updatepc = Standard_True;
+              maxdist = Max(maxdist,tolreached);
+            }
+            else {
+              maxdist = Max(maxdist, error);
+            }
+            if(updatepc){
+              if (i == 0) GCurve->PCurve(curPC);
+              else GCurve->PCurve2(curPC);
+            }
+          }
+          else IsSameP = 0;
+
+        }
+        else IsSameP = 0;
+
+        //  Modified by skv - Thu Jun  3 12:39:19 2004 OCC5898 Begin
+        if (!IsSameP) {
+          if (anEdgeTol > error) {
+            maxdist = Max(maxdist, anEdgeTol);
+            IsSameP = Standard_True;
+          }
+        }
+        //  Modified by skv - Thu Jun  3 12:39:20 2004 OCC5898 End
       }
     }
     It.Next() ;
@@ -1291,9 +1287,9 @@ void BRepLib::SameParameter(const TopoDS_Edge&  AnEdge,
       TopoDS_Vertex V1,V2;
       TopExp::Vertices(AnEdge,V1,V2);
       if (!V1.IsNull())
-       B.UpdateVertex(V1,maxdist);
+        B.UpdateVertex(V1,maxdist);
       if (!V2.IsNull())
-       B.UpdateVertex(V2,maxdist);
+        B.UpdateVertex(V2,maxdist);
       TE->Modified(Standard_True);
       TE->Tolerance(maxdist);
     }
@@ -1306,11 +1302,11 @@ void BRepLib::SameParameter(const TopoDS_Edge&  AnEdge,
 //purpose  : 
 //=======================================================================
 void  BRepLib::UpdateTolerances(const TopoDS_Shape& aShape,
-                               const Standard_Boolean verifyTolerance) 
+  const Standard_Boolean verifyTolerance) 
 {
 
-// Harmonize tolerances
-// with rule Tolerance(VERTEX)>=Tolerance(EDGE)>=Tolerance(FACE)
+  // Harmonize tolerances
+  // with rule Tolerance(VERTEX)>=Tolerance(EDGE)>=Tolerance(FACE)
   BRep_Builder B;
   Standard_Real tol=0;
   if (verifyTolerance) {
@@ -1324,47 +1320,47 @@ void  BRepLib::UpdateTolerances(const TopoDS_Shape& aShape,
       const TopoDS_Face& curf=TopoDS::Face(ex.Current());
       S = BRep_Tool::Surface(curf, l);
       if (!S.IsNull()) {
-       aB.SetVoid();
-       BRepBndLib::Add(curf,aB);
-       if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
-         S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
-       }
-       GeomAdaptor_Surface AS(S);
-       switch (AS.GetType()) {
-       case GeomAbs_Plane: 
-       case GeomAbs_Cylinder: 
-       case GeomAbs_Cone: 
-         {
-           tol=Precision::Confusion();
-           break;
-         }
-       case GeomAbs_Sphere: 
-       case GeomAbs_Torus: 
-         {
-           tol=Precision::Confusion()*2;
-           break;
-         }
-       default:
-         tol=Precision::Confusion()*4;
-       }
-       if (!aB.IsWhole()) {
-         aB.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
-         dMax=1.;
-         if (!aB.IsOpenXmin() && !aB.IsOpenXmax()) dMax=aXmax-aXmin;
-         if (!aB.IsOpenYmin() && !aB.IsOpenYmax()) aYmin=aYmax-aYmin;
-         if (!aB.IsOpenZmin() && !aB.IsOpenZmax()) aZmin=aZmax-aZmin;
-         if (aYmin>dMax) dMax=aYmin;
-         if (aZmin>dMax) dMax=aZmin;
-         tol=tol*dMax;
-         // Do not process tolerances > 1.
-         if (tol>1.) tol=0.99;
-       }
-       const Handle(BRep_TFace)& Tf = *((Handle(BRep_TFace)*)&curf.TShape());
-       Tf->Tolerance(tol);
+        aB.SetVoid();
+        BRepBndLib::Add(curf,aB);
+        if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
+          S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
+        }
+        GeomAdaptor_Surface AS(S);
+        switch (AS.GetType()) {
+        case GeomAbs_Plane: 
+        case GeomAbs_Cylinder: 
+        case GeomAbs_Cone: 
+          {
+            tol=Precision::Confusion();
+            break;
+          }
+        case GeomAbs_Sphere: 
+        case GeomAbs_Torus: 
+          {
+            tol=Precision::Confusion()*2;
+            break;
+          }
+        default:
+          tol=Precision::Confusion()*4;
+        }
+        if (!aB.IsWhole()) {
+          aB.Get(aXmin, aYmin, aZmin, aXmax, aYmax, aZmax);
+          dMax=1.;
+          if (!aB.IsOpenXmin() && !aB.IsOpenXmax()) dMax=aXmax-aXmin;
+          if (!aB.IsOpenYmin() && !aB.IsOpenYmax()) aYmin=aYmax-aYmin;
+          if (!aB.IsOpenZmin() && !aB.IsOpenZmax()) aZmin=aZmax-aZmin;
+          if (aYmin>dMax) dMax=aYmin;
+          if (aZmin>dMax) dMax=aZmin;
+          tol=tol*dMax;
+          // Do not process tolerances > 1.
+          if (tol>1.) tol=0.99;
+        }
+        const Handle(BRep_TFace)& Tf = *((Handle(BRep_TFace)*)&curf.TShape());
+        Tf->Tolerance(tol);
       }
     }
   }
-  
+
   //Process edges
   TopTools_IndexedDataMapOfShapeListOfShape parents;
   TopExp::MapShapesAndAncestors(aShape, TopAbs_EDGE, TopAbs_FACE, parents);
@@ -1403,37 +1399,37 @@ void  BRepLib::UpdateTolerances(const TopoDS_Shape& aShape,
       BRep_ListIteratorOfListOfCurveRepresentation itcr(TE->Curves());
       const TopLoc_Location& Eloc = E.Location();
       while (itcr.More()) {
-       // For each CurveRepresentation, check the provided parameter
-       const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
-       const TopLoc_Location& loc = cr->Location();
-       TopLoc_Location L = (Eloc * loc);
-       if (cr->IsCurve3D()) {
-         const Handle(Geom_Curve)& C = cr->Curve3D();
-         if (!C.IsNull()) { // edge non degenerated
-           p3d = C->Value(par);
-           p3d.Transform(L.Transformation());
-           box.Add(p3d);
-         }
-       }
-       else if (cr->IsCurveOnSurface()) {
-         const Handle(Geom_Surface)& Su = cr->Surface();
-         const Handle(Geom2d_Curve)& PC = cr->PCurve();
-         Handle(Geom2d_Curve) PC2;
-         if (cr->IsCurveOnClosedSurface()) {
-           PC2 = cr->PCurve2();
-         }
-         gp_Pnt2d p2d = PC->Value(par);
-         p3d = Su->Value(p2d.X(),p2d.Y());
-         p3d.Transform(L.Transformation());
-         box.Add(p3d);
-         if (!PC2.IsNull()) {
-           p2d = PC2->Value(par);
-           p3d = Su->Value(p2d.X(),p2d.Y());
-           p3d.Transform(L.Transformation());
-           box.Add(p3d);
-         }
-       }
-       itcr.Next();
+        // For each CurveRepresentation, check the provided parameter
+        const Handle(BRep_CurveRepresentation)& cr = itcr.Value();
+        const TopLoc_Location& loc = cr->Location();
+        TopLoc_Location L = (Eloc * loc);
+        if (cr->IsCurve3D()) {
+          const Handle(Geom_Curve)& C = cr->Curve3D();
+          if (!C.IsNull()) { // edge non degenerated
+            p3d = C->Value(par);
+            p3d.Transform(L.Transformation());
+            box.Add(p3d);
+          }
+        }
+        else if (cr->IsCurveOnSurface()) {
+          const Handle(Geom_Surface)& Su = cr->Surface();
+          const Handle(Geom2d_Curve)& PC = cr->PCurve();
+          Handle(Geom2d_Curve) PC2;
+          if (cr->IsCurveOnClosedSurface()) {
+            PC2 = cr->PCurve2();
+          }
+          gp_Pnt2d p2d = PC->Value(par);
+          p3d = Su->Value(p2d.X(),p2d.Y());
+          p3d.Transform(L.Transformation());
+          box.Add(p3d);
+          if (!PC2.IsNull()) {
+            p2d = PC2->Value(par);
+            p3d = Su->Value(p2d.X(),p2d.Y());
+            p3d.Transform(L.Transformation());
+            box.Add(p3d);
+          }
+        }
+        itcr.Next();
       }
     }
     Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
@@ -1446,13 +1442,13 @@ void  BRepLib::UpdateTolerances(const TopoDS_Shape& aShape,
       // Attention to sharing of the vertex by other shapes
       const Handle(BRep_TVertex)& TV = *((Handle(BRep_TVertex)*)&V.TShape());
       if (Initialized.Add(TV)) 
-       TV->Tolerance(tol);
+        TV->Tolerance(tol);
       else 
-       B.UpdateVertex(V, tol);
+        B.UpdateVertex(V, tol);
     }
     else {
-    // Update can only increase tolerance, so if the edge has a greater
-    //  tolerance than its faces it is not concerned
+      // Update can only increase tolerance, so if the edge has a greater
+      //  tolerance than its faces it is not concerned
       B.UpdateVertex(V, tol);
     }
   }
@@ -1464,7 +1460,7 @@ void  BRepLib::UpdateTolerances(const TopoDS_Shape& aShape,
 //=======================================================================
 Standard_Boolean BRepLib::OrientClosedSolid(TopoDS_Solid& solid) 
 {
-// Set material inside the solid
+  // Set material inside the solid
   BRepClass3d_SolidClassifier where(solid);
   where.PerformInfinitePoint(Precision::Confusion());
   if (where.State()==TopAbs_IN) {
@@ -1483,10 +1479,10 @@ Standard_Boolean BRepLib::OrientClosedSolid(TopoDS_Solid& solid)
 //=======================================================================
 
 static Standard_Boolean tgtfaces(const TopoDS_Edge& Ed,
-                                const TopoDS_Face& F1,
-                                const TopoDS_Face& F2,
-                                const Standard_Real ta,
-                                const Standard_Boolean couture)
+  const TopoDS_Face& F1,
+  const TopoDS_Face& F2,
+  const Standard_Real ta,
+  const Standard_Boolean couture)
 {
   Standard_Real u;
   TopoDS_Edge E = Ed;
@@ -1513,7 +1509,7 @@ static Standard_Boolean tgtfaces(const TopoDS_Edge& Ed,
   BRep_Tool::Range(E,f,l);
   Extrema_LocateExtPC ext;
   Standard_Boolean IsInitialized = Standard_False;
-  
+
   eps = (l - f)/100.;
   f += eps; // to avoid calculations on  
   l -= eps; // points of pointed squares.
@@ -1548,28 +1544,28 @@ static Standard_Boolean tgtfaces(const TopoDS_Edge& Ed,
 
     if (Nok &&(ang > ta)) { // Refine by projection
       if (! IsInitialized ) {
-       ext.Initialize(C2,f,l,Precision::PConfusion());
-       IsInitialized = Standard_True;
+        ext.Initialize(C2,f,l,Precision::PConfusion());
+        IsInitialized = Standard_True;
       }      
       ext.Perform(pp1,u);
       if(ext.IsDone() && ext.IsMin()){
-       Extrema_POnCurv poc = ext.Point();
-       Standard_Real v = poc.Parameter();
-
-       HC2d2->D0(v,p);
-       p.Coord(uu,vv);
-       HS2->D1(p.X(), p.Y(), pp2, du, dv);
-       d2 = (du.Crossed(dv));
-       norm = d2.Magnitude();
-       if (norm> 1.e-12) d2 /= norm;
-       else Nok = Standard_False;
-       if(rev2) d2.Reverse();
-       if (Nok) ang = d1.Angle(d2);
+        Extrema_POnCurv poc = ext.Point();
+        Standard_Real v = poc.Parameter();
+
+        HC2d2->D0(v,p);
+        p.Coord(uu,vv);
+        HS2->D1(p.X(), p.Y(), pp2, du, dv);
+        d2 = (du.Crossed(dv));
+        norm = d2.Magnitude();
+        if (norm> 1.e-12) d2 /= norm;
+        else Nok = Standard_False;
+        if(rev2) d2.Reverse();
+        if (Nok) ang = d1.Angle(d2);
       }
     }
     if(ang >= angmax) angmax = ang;
   }     
+
   return (angmax<=ta);
 
 }
@@ -1582,7 +1578,7 @@ static Standard_Boolean tgtfaces(const TopoDS_Edge& Ed,
 //=======================================================================
 
 void BRepLib::EncodeRegularity(const TopoDS_Shape& S,
-                              const Standard_Real TolAng)
+  const Standard_Real TolAng)
 {
   BRep_Builder B;
   TopTools_IndexedDataMapOfShapeListOfShape M;
@@ -1598,30 +1594,30 @@ void BRepLib::EncodeRegularity(const TopoDS_Shape& S,
     for(It.Initialize(M.FindFromIndex(i));It.More() && !found;It.Next()){
       if(F1.IsNull()) { F1 = TopoDS::Face(It.Value()); }
       else {
-       if(!F1.IsSame(TopoDS::Face(It.Value()))){
-         found = Standard_True;
-         F2 = TopoDS::Face(It.Value());
-       }
+        if(!F1.IsSame(TopoDS::Face(It.Value()))){
+          found = Standard_True;
+          F2 = TopoDS::Face(It.Value());
+        }
       }
     }
     if (!found && !F1.IsNull()){//is it a sewing edge?
       TopAbs_Orientation orE = E.Orientation();
       TopoDS_Edge curE;
       for(Ex.Init(F1,TopAbs_EDGE);Ex.More() && !found;Ex.Next()){
-       curE= TopoDS::Edge(Ex.Current());
-       if(E.IsSame(curE) && orE != curE.Orientation()) {
-         found = Standard_True;
-         couture = Standard_True;
-         F2 = F1;
-       }
+        curE= TopoDS::Edge(Ex.Current());
+        if(E.IsSame(curE) && orE != curE.Orientation()) {
+          found = Standard_True;
+          couture = Standard_True;
+          F2 = F1;
+        }
       }
     }
     if(found){
       if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
 
         try {
-               if(tgtfaces(E, F1, F2, TolAng, couture)){
-                 B.Continuity(E,F1,F2,GeomAbs_G1);
+          if(tgtfaces(E, F1, F2, TolAng, couture)){
+            B.Continuity(E,F1,F2,GeomAbs_G1);
           }
         }
         catch(Standard_Failure)
@@ -1638,9 +1634,9 @@ void BRepLib::EncodeRegularity(const TopoDS_Shape& S,
 //=======================================================================
 
 void BRepLib::EncodeRegularity(TopoDS_Edge& E,
-                              const TopoDS_Face& F1,
-                              const TopoDS_Face& F2,
-                              const Standard_Real TolAng)
+  const TopoDS_Face& F1,
+  const TopoDS_Face& F2,
+  const Standard_Real TolAng)
 {
   BRep_Builder B;
   if(BRep_Tool::Continuity(E,F1,F2)<=GeomAbs_C0){
@@ -1652,7 +1648,7 @@ void BRepLib::EncodeRegularity(TopoDS_Edge& E,
     catch(Standard_Failure)
     {
     }
- }
+  }
 }
 
 //=======================================================================
@@ -1661,7 +1657,7 @@ void BRepLib::EncodeRegularity(TopoDS_Edge& E,
 //=======================================================================
 
 void  BRepLib::SortFaces (const TopoDS_Shape& Sh,
-                         TopTools_ListOfShape& LF)
+  TopTools_ListOfShape& LF)
 {
   LF.Clear();
   TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
@@ -1674,37 +1670,37 @@ void  BRepLib::SortFaces (const TopoDS_Shape& Sh,
     S = BRep_Tool::Surface(F, l);
     if (!S.IsNull()) {
       if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
-       S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
+        S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
       }
       GeomAdaptor_Surface AS(S);
       switch (AS.GetType()) {
       case GeomAbs_Plane: 
-       {
-         LPlan.Append(F);
-         break;
-       }
+        {
+          LPlan.Append(F);
+          break;
+        }
       case GeomAbs_Cylinder: 
-       {
-         LCyl.Append(F);
-         break;
-       }
+        {
+          LCyl.Append(F);
+          break;
+        }
       case GeomAbs_Cone: 
-       {
-         LCon.Append(F);
-         break;
-       }
+        {
+          LCon.Append(F);
+          break;
+        }
       case GeomAbs_Sphere: 
-       {
-         LSphere.Append(F);
-         break;
-       }
+        {
+          LSphere.Append(F);
+          break;
+        }
       case GeomAbs_Torus: 
-       {
-         LTor.Append(F);
-         break;
-       }
+        {
+          LTor.Append(F);
+          break;
+        }
       default:
-       LOther.Append(F);
+        LOther.Append(F);
       }
     }
     else LTri.Append(F);
@@ -1719,57 +1715,56 @@ void  BRepLib::SortFaces (const TopoDS_Shape& Sh,
 //=======================================================================
 
 void  BRepLib::ReverseSortFaces (const TopoDS_Shape& Sh,
-                                TopTools_ListOfShape& LF)
+  TopTools_ListOfShape& LF)
 {
   LF.Clear();
   TopTools_ListOfShape LTri,LPlan,LCyl,LCon,LSphere,LTor,LOther;
   TopExp_Explorer exp(Sh,TopAbs_FACE);
   TopLoc_Location l;
   Handle(Geom_Surface) S;
-  
+
   for (; exp.More(); exp.Next()) {
     const TopoDS_Face&   F = TopoDS::Face(exp.Current());
     S = BRep_Tool::Surface(F, l);
     if (!S.IsNull()) {
       if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
-       S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
+        S = (*((Handle(Geom_RectangularTrimmedSurface)*)&S))->BasisSurface();
       }
       GeomAdaptor_Surface AS(S);
       switch (AS.GetType()) {
       case GeomAbs_Plane: 
-       {
-         LPlan.Append(F);
-         break;
-       }
+        {
+          LPlan.Append(F);
+          break;
+        }
       case GeomAbs_Cylinder: 
-       {
-         LCyl.Append(F);
-         break;
-       }
+        {
+          LCyl.Append(F);
+          break;
+        }
       case GeomAbs_Cone: 
-       {
-         LCon.Append(F);
-         break;
-       }
+        {
+          LCon.Append(F);
+          break;
+        }
       case GeomAbs_Sphere: 
-       {
-         LSphere.Append(F);
-         break;
-       }
+        {
+          LSphere.Append(F);
+          break;
+        }
       case GeomAbs_Torus: 
-       {
-         LTor.Append(F);
-         break;
-       }
+        {
+          LTor.Append(F);
+          break;
+        }
       default:
-       LOther.Append(F);
+        LOther.Append(F);
       }
     }
     else LTri.Append(F);
   }
   LF.Append(LTri); LF.Append(LOther); LF.Append(LTor ); LF.Append(LSphere);
   LF.Append(LCon); LF.Append(LCyl  ); LF.Append(LPlan);
-   
-}
 
+}
 
index 9e4a82d..a7b29b8 100644 (file)
@@ -379,7 +379,10 @@ static Standard_Integer mk2dcurve(Draw_Interpretor& di,
   if (na < 3) return 1;
 
   TopoDS_Shape S;
-  S = DBRep::Get(a[2],TopAbs_EDGE); if (S.IsNull()) return 1;
+  S = DBRep::Get(a[2],TopAbs_EDGE);
+  if (S.IsNull())
+    return 1;
+
   TopoDS_Edge E = TopoDS::Edge(S);
 
   TopLoc_Location L;
@@ -387,20 +390,38 @@ static Standard_Integer mk2dcurve(Draw_Interpretor& di,
   Handle(Geom2d_Curve) C;
   Handle(Geom_Surface) Surf;
 
+  Standard_Boolean hasFace = Standard_False;
+
   if ( na == 3 ) {
     // get the first PCurve connected to edge E
     BRep_Tool::CurveOnSurface(E,C,Surf,L,f,l);
   }
-  else if ( na == 4 ) {
-    S = DBRep::Get(a[3],TopAbs_FACE); if (S.IsNull()) return 1;
-    TopoDS_Face F = TopoDS::Face(S);
-    C = BRep_Tool::CurveOnSurface(E,F,f,l);
+  else if ( na == 4 )
+  {
+    S = DBRep::Get(a[3],TopAbs_FACE);
+    if (S.IsNull())
+    {
+      Standard_Integer ind = Draw::Atoi(a[3]);
+      BRep_Tool::CurveOnSurface(E,C,Surf,L,f,l,ind);
+    }
+    else
+    {
+      hasFace = Standard_True;
+      TopoDS_Face F = TopoDS::Face(S);
+      C = BRep_Tool::CurveOnSurface(E,F,f,l);
+    }
   }
 
   if (C.IsNull()) {
     //cout << a[2] << " has no 2d curve"; if (na == 4) cout << " on " << a[3];
     //cout << endl;
-    di << a[2] << " has no 2d curve"; if (na == 4) di << " on " << a[3];
+    di << a[2] << " has no 2d curve";
+    
+    if (hasFace)
+    {
+      di << " on " << a[3];
+    }
+
     di << "\n";
     return 1;
   }
@@ -1805,7 +1826,7 @@ void  BRepTest::CurveCommands(Draw_Interpretor& theCommands)
     mkcurve,g);
 
   theCommands.Add("mk2dcurve",
-    "mk2dcurve curve edge [face]",__FILE__,
+    "mk2dcurve curve edge [face OR index]",__FILE__,
     mk2dcurve,g);
 
   theCommands.Add("mkpoint",
index e769245..fca5c14 100644 (file)
@@ -45,9 +45,9 @@ Extrema_GLocateExtPC::Extrema_GLocateExtPC() { }
 //=======================================================================
 
 Extrema_GLocateExtPC::Extrema_GLocateExtPC (const ThePoint&     P,
-                                           const TheCurve&     C,
-                                           const Standard_Real U0,
-                                           const Standard_Real TolF)
+  const TheCurve&     C,
+  const Standard_Real U0,
+  const Standard_Real TolF)
 {
   Initialize(C, TheCurveTool::FirstParameter(C), TheCurveTool::LastParameter(C), TolF);
   Perform(P, U0);
@@ -59,11 +59,11 @@ Extrema_GLocateExtPC::Extrema_GLocateExtPC (const ThePoint&     P,
 //=======================================================================
 
 Extrema_GLocateExtPC::Extrema_GLocateExtPC (const ThePoint&     P,
-                                           const TheCurve&     C,
-                                           const Standard_Real U0, 
-                                           const Standard_Real Umin,
-                                           const Standard_Real Usup,
-                                           const Standard_Real TolF)
+  const TheCurve&     C,
+  const Standard_Real U0, 
+  const Standard_Real Umin,
+  const Standard_Real Usup,
+  const Standard_Real TolF)
 {
   Initialize(C, Umin, Usup, TolF);
   Perform(P, U0);
@@ -77,9 +77,9 @@ Extrema_GLocateExtPC::Extrema_GLocateExtPC (const ThePoint&     P,
 //=======================================================================
 
 void Extrema_GLocateExtPC::Initialize(const TheCurve&     C, 
-                                     const Standard_Real Umin,
-                                     const Standard_Real Usup,
-                                     const Standard_Real TolF)
+  const Standard_Real Umin,
+  const Standard_Real Usup,
+  const Standard_Real TolF)
 {
   myC = (Standard_Address)&C;
   mytol = TolF;
@@ -88,9 +88,9 @@ void Extrema_GLocateExtPC::Initialize(const TheCurve&     C,
   type = TheCurveTool::GetType(C);
   Standard_Real tolu = TheCurveTool::Resolution(C, Precision::Confusion());
   if ((type == GeomAbs_BSplineCurve) || 
-      (type == GeomAbs_BezierCurve)  || 
-      (type == GeomAbs_OtherCurve)) {
-    myLocExtPC.Initialize(C, Umin, Usup, tolu);
+    (type == GeomAbs_BezierCurve)  || 
+    (type == GeomAbs_OtherCurve)) {
+      myLocExtPC.Initialize(C, Umin, Usup, tolu);
   }
   else {
     myExtremPC.Initialize(C, Umin, Usup, tolu);
@@ -106,29 +106,31 @@ void Extrema_GLocateExtPC::Initialize(const TheCurve&     C,
 //=======================================================================
 
 void Extrema_GLocateExtPC::Perform(const ThePoint&     P,
-                                  const Standard_Real U0)
+  const Standard_Real U0)
 {
   Standard_Integer i, i1, i2, inter;
   Standard_Real Par, valU, valU2 = RealLast(),
-  local_u0 ;
+    local_u0 ;
   Standard_Real myintuinf=0, myintusup=0;
   local_u0 = U0 ;
-  switch(type) {
-    case GeomAbs_OtherCurve:
-    case GeomAbs_BSplineCurve: {
+  switch(type)
+  {
+  case GeomAbs_OtherCurve:
+  case GeomAbs_BSplineCurve:
+    {
       // La recherche de l extremum est faite intervalle continu C2 par
       // intervalle continu C2 de la courbe
       Standard_Integer n = TheCurveTool::NbIntervals(*((TheCurve*)myC), GeomAbs_C2);
       TColStd_Array1OfReal theInter(1, n+1);
       TheCurveTool::Intervals(*((TheCurve*)myC), theInter, GeomAbs_C2);
-//
-//  be gentle with the caller 
-//
+      //
+      //  be gentle with the caller 
+      //
       if (local_u0 < myumin) {
-       local_u0 = myumin ; 
+        local_u0 = myumin ; 
       }
       else if (local_u0 > myusup) {
-       local_u0  = myusup ;
+        local_u0  = myusup ;
       }
       // Recherche de l intervalle ou se trouve U0
       Standard_Boolean found = Standard_False;
@@ -140,120 +142,144 @@ void Extrema_GLocateExtPC::Perform(const ThePoint&     P,
         // pas, mais il n'y avait aucune raison de sortir en "return")
         myintuinf = Max(theInter(inter), myumin);
         myintusup = Min(theInter(inter+1), myusup);
-       if ((local_u0 >= myintuinf) && (local_u0 < myintusup)) found = Standard_True;
-       inter++;
+        if ((local_u0 >= myintuinf) && (local_u0 < myintusup)) found = Standard_True;
+        inter++;
       }
 
       if( found ) inter--; //IFV 16.06.00 - inter is increased after found!
 
       // Essai sur l intervalle trouve
       myLocExtPC.Initialize((*((TheCurve*)myC)), myintuinf, 
-                           myintusup, mytol);
+        myintusup, mytol);
       myLocExtPC.Perform(P, local_u0);
       myDone = myLocExtPC.IsDone();
       if (myDone) {
-       mypp = myLocExtPC.Point();
-       myismin = myLocExtPC.IsMin();
-       mydist2 = myLocExtPC.SquareDistance();
+        mypp = myLocExtPC.Point();
+        myismin = myLocExtPC.IsMin();
+        mydist2 = myLocExtPC.SquareDistance();
       }
       else {
-       Standard_Integer k = 1;
-       // Essai sur les intervalles alentours:
-       i1 = inter;
-       i2 = inter;
-       Standard_Real s1inf, s2inf, s1sup, s2sup;
-       ThePoint P1;
-       TheVector V1;
-       TheCurveTool::D1(*((TheCurve*)myC), myintuinf, P1, V1);
-       s2inf = (TheVector(P, P1)*V1);
-       TheCurveTool::D1(*((TheCurve*)myC), myintusup, P1, V1);
-       s1sup = (TheVector(P, P1)*V1);
-       
-
-       while (!myDone && (i2 > 0) && (i1 <= n)) {
-         i1 = inter + k;
-         i2 = inter - k;
-         if (i1 <= n) {
-           myintuinf = Max(theInter(i1), myumin);
-           myintusup = Min(theInter(i1+1), myusup);
-           if (myintuinf < myintusup) {
-             TheCurveTool::D1(*((TheCurve*)myC), myintuinf, P1, V1);
-             s2sup = (TheVector(P, P1)*V1);
-             if (s1sup*s2sup <= RealEpsilon()) {
-               // extremum:
-               myDone = Standard_True;
-               mypp.SetValues(myintuinf, P1);
-               myismin = (s1sup <= 0.0);
-               mydist2 = P.SquareDistance(P1);
-               break;
-             }
-             TheCurveTool::D1(*((TheCurve*)myC), myintusup, P1, V1);
-             s1sup = (TheVector(P, P1)*V1);
-             myLocExtPC.Initialize((*((TheCurve*)myC)), myintuinf, 
-                                   myintusup, mytol);
-             myLocExtPC.Perform(P, (myintuinf + myintusup)*0.5);
-             myDone = myLocExtPC.IsDone();
-             if (myDone) {
-               mypp = myLocExtPC.Point();
-               myismin = myLocExtPC.IsMin();
-               mydist2 = myLocExtPC.SquareDistance();
-               break;
-             }
-           }
-         }
-         if (i2 > 0) {
-           myintuinf = Max(theInter(i2), myumin);
-           myintusup = Min(theInter(i2+1), myusup);
-           if (myintuinf < myintusup) {
-             TheCurveTool::D1(*((TheCurve*)myC), myintusup, P1, V1);
-             s1inf = (TheVector(P, P1)*V1);
-             if (s1inf*s2inf <= RealEpsilon()) {
-               // extremum:
-               myDone = Standard_True;
-               mypp.SetValues(myintusup, P1);
-               myismin = (s1inf <= 0.0);
-               mydist2 = P.SquareDistance(P1);
-               break;
-             }
-             TheCurveTool::D1(*((TheCurve*)myC), myintuinf, P1, V1);
-             s2inf = (TheVector(P, P1)*V1);
-             myLocExtPC.Initialize((*((TheCurve*)myC)), myintuinf, 
-                                   myintusup, mytol);
-             myLocExtPC.Perform(P, (myintuinf+myintusup)*0.5 );
-             myDone = myLocExtPC.IsDone();
-             if (myDone) {
-               mypp = myLocExtPC.Point();
-               myismin = myLocExtPC.IsMin();
-               mydist2 = myLocExtPC.SquareDistance();
-               break;
-             }
-           }
-         }
-         k++;
-       }
+        Standard_Integer k = 1;
+        // Essai sur les intervalles alentours:
+        i1 = inter;
+        i2 = inter;
+        Standard_Real s1inf, s2inf, s1sup, s2sup;
+        ThePoint P1;
+        TheVector V1;
+        TheCurveTool::D1(*((TheCurve*)myC), myintuinf, P1, V1);
+        s2inf = (TheVector(P, P1)*V1);
+        TheCurveTool::D1(*((TheCurve*)myC), myintusup, P1, V1);
+        s1sup = (TheVector(P, P1)*V1);
+
+
+        while (!myDone && (i2 > 0) && (i1 <= n))
+        {
+          i1 = inter + k;
+          i2 = inter - k;
+          if (i1 <= n)
+          {
+            myintuinf = Max(theInter(i1), myumin);
+            myintusup = Min(theInter(i1+1), myusup);
+            if (myintuinf < myintusup)
+            {
+              TheCurveTool::D1(*((TheCurve*)myC), myintuinf, P1, V1);
+              s2sup = (TheVector(P, P1)*V1);
+              if (s1sup*s2sup <= RealEpsilon())
+              {
+                // extremum:
+                myDone = Standard_True;
+                mypp.SetValues(myintuinf, P1);
+                myismin = (s1sup <= 0.0);
+                mydist2 = P.SquareDistance(P1);
+                break;
+              }
+
+              TheCurveTool::D1(*((TheCurve*)myC), myintusup, P1, V1);
+              s1sup = (TheVector(P, P1)*V1);
+              myLocExtPC.Initialize((*((TheCurve*)myC)), myintuinf, 
+                myintusup, mytol);
+              myLocExtPC.Perform(P, (myintuinf + myintusup)*0.5);
+              myDone = myLocExtPC.IsDone();
+              if (myDone) {
+                mypp = myLocExtPC.Point();
+                myismin = myLocExtPC.IsMin();
+                mydist2 = myLocExtPC.SquareDistance();
+                break;
+              }
+            }
+          }
+
+          if (i2 > 0)
+          {
+            myintuinf = Max(theInter(i2), myumin);
+            myintusup = Min(theInter(i2+1), myusup);
+            if (myintuinf < myintusup)
+            {
+              TheCurveTool::D1(*((TheCurve*)myC), myintusup, P1, V1);
+              s1inf = (TheVector(P, P1)*V1);
+              if (s1inf*s2inf <= RealEpsilon())
+              {
+                // extremum:
+                myDone = Standard_True;
+                mypp.SetValues(myintusup, P1);
+                myismin = (s1inf <= 0.0);
+                mydist2 = P.SquareDistance(P1);
+                break;
+              }
+
+              TheCurveTool::D1(*((TheCurve*)myC), myintuinf, P1, V1);
+              s2inf = (TheVector(P, P1)*V1);
+              myLocExtPC.Initialize((*((TheCurve*)myC)), myintuinf, 
+                myintusup, mytol);
+              myLocExtPC.Perform(P, (myintuinf+myintusup)*0.5 );
+              myDone = myLocExtPC.IsDone();
+            
+              if (myDone)
+              {
+                mypp = myLocExtPC.Point();
+                myismin = myLocExtPC.IsMin();
+                mydist2 = myLocExtPC.SquareDistance();
+                break;
+              }
+            }
+          }
+
+          k++;
+        }
       }
     }
-      break;
-    case GeomAbs_BezierCurve: {
+
+    break;
+
+  case GeomAbs_BezierCurve:
+    {
       myLocExtPC.Perform(P, U0);
       myDone = myLocExtPC.IsDone();
     }
-      break;
-    default:{
+
+    break;
+  default:
+    {
       myExtremPC.Perform(P);
       numberext = 0;
-      if (myExtremPC.IsDone()) {
-       for (i = 1; i <= myExtremPC.NbExt(); i++) {
-         Par = myExtremPC.Point(i).Parameter();
-         valU = Abs(Par - U0);
-         if (valU <= valU2) {
-           valU2 = valU;
-           numberext = i;
-           myDone = Standard_True;
-         }
-       }
+      if (myExtremPC.IsDone())
+      {
+        for (i = 1; i <= myExtremPC.NbExt(); i++)
+        {
+          Par = myExtremPC.Point(i).Parameter();
+          valU = Abs(Par - U0);
+          if (valU <= valU2)
+          {
+            valU2 = valU;
+            numberext = i;
+            myDone = Standard_True;
+          }
+        }
       }
-      if (numberext == 0) myDone = Standard_False;
+
+      if (numberext == 0)
+        myDone = Standard_False;
+
       break;
     }
   }
diff --git a/tests/bugs/modalg_5/bug24203 b/tests/bugs/modalg_5/bug24203
new file mode 100644 (file)
index 0000000..be90f10
--- /dev/null
@@ -0,0 +1,25 @@
+puts "============"
+puts "OCC24203"
+puts "============"
+puts ""
+#######################################################################
+## Command "sameparameter" in DRAW on attached edge set tolerance equal to 116.
+#######################################################################
+
+pload DATAEXCHANGEKERNEL
+
+restore [locate_data_file bug24203_notspedge.brep] e1
+
+sameparameter e1
+
+regexp {Tolerance +MAX=([-0-9.+eE]+)} [tolerance e1] full MaxTol_1
+
+puts "MaxTolerance = $MaxTol_1"
+
+set MaxTol 0.20
+
+if { $MaxTol_1 > $MaxTol } {
+    puts "Faulty OCC24203"
+} else {
+    puts "OCC24203 OK"
+}
index 6a33ae1..7661911 100644 (file)
@@ -1,5 +1,5 @@
 if {[string compare $command "SplitAngle"] == 0 } {
-    puts "TODO OCC23127 ALL: Faulty shapes in variables faulty_1 to faulty_4 "
+    puts "TODO OCC23127 ALL: Faulty shapes in variables faulty_1 to faulty_ "
 }
 restore [locate_data_file METABO9.brep] a