0027772: Foundation Classes - define Standard_Boolean using C++ type "bool" instead...
[occt.git] / src / LocOpe / LocOpe_SplitShape.cxx
index 54a44a1..14a6c4f 100644 (file)
 #include <TopTools_MapIteratorOfMapOfShape.hxx>
 #include <TopTools_MapOfOrientedShape.hxx>
 #include <TopTools_MapOfShape.hxx>
+#include <ShapeAnalysis_Edge.hxx>
+#include <Geom2dAPI_ProjectPointOnCurve.hxx>
+
+#include <Geom_Surface.hxx>
 
 static Standard_Boolean IsInside(const TopoDS_Face&,
                                  const TopoDS_Wire&,
@@ -66,17 +70,16 @@ static Standard_Boolean IsInside(const TopoDS_Face&,
 
 static void GetDirection(const TopoDS_Edge&,
                          const TopoDS_Face&,
-                         Standard_Real&,
                          gp_Pnt2d&,
-                         gp_Vec2d&);
+                         gp_Vec2d&, 
+                         Standard_Boolean isFirstEnd);
 
-static void ChoixUV(const TopoDS_Edge&,
+static Standard_Boolean ChoixUV(const TopoDS_Edge&,
                     const TopoDS_Face&,
                     const TopTools_IndexedMapOfShape&,
                     TopoDS_Edge&,
                     gp_Pnt2d&,
-                    gp_Vec2d&,
-                    const Standard_Real tol);
+                    gp_Vec2d&);
 
 static TopoDS_Shape ChooseDirection(const TopoDS_Shape&,
                                     const TopoDS_Vertex&,
@@ -86,9 +89,6 @@ static TopoDS_Shape ChooseDirection(const TopoDS_Shape&,
 inline Standard_Boolean SameUV(const gp_Pnt2d& P1, const gp_Pnt2d& P2, 
                                const BRepAdaptor_Surface& theBAS)//const Standard_Real tol)
 {
-  //  Standard_Real tol = Precision::Confusion();
-  //  return P1.SquareDistance(P2) < 10*tol;
-  //gka
   Standard_Boolean isSame = Standard_True;
   if(theBAS.IsUPeriodic())
     isSame = (fabs(P1.X() - P2.X()) < theBAS.UPeriod() *0.5);
@@ -159,7 +159,7 @@ void LocOpe_SplitShape::Add(const TopoDS_Vertex& V,
                             const TopoDS_Edge& E)
 {
   if (!CanSplit(E)) {
-    Standard_ConstructionError::Raise();
+    return;
   }
 
   BRep_Builder B;
@@ -169,22 +169,31 @@ void LocOpe_SplitShape::Add(const TopoDS_Vertex& V,
   }
   TopTools_ListIteratorOfListOfShape itl(le);
   Standard_Real f,l;
-
+  TopTools_ListOfShape aNewList;
   for (; itl.More(); itl.Next()) {
     const TopoDS_Edge& edg = TopoDS::Edge(itl.Value());
     BRep_Tool::Range(edg,f,l);
-    if (P>f && P <l) {
+    if (P > f + Precision::PConfusion() && P < l - Precision::PConfusion()) {
       break;
+     
     }
+    aNewList.Append(edg);
   }
   if (!itl.More()) {
-    Standard_ConstructionError::Raise();
+    return;
   }
   TopoDS_Edge edg = TopoDS::Edge(itl.Value());
   le.Remove(itl);
   if (V.Orientation() == TopAbs_FORWARD ||
     V.Orientation() == TopAbs_REVERSED) {
 
+      edg.Orientation(TopAbs_FORWARD);
+      TopoDS_Vertex aCurV1, aCurV2;
+      TopExp::Vertices(edg, aCurV1, aCurV2);
+      Standard_Real aPar1 = BRep_Tool::Parameter(aCurV1,edg);
+      
+      Standard_Real aPar2 = BRep_Tool::Parameter(aCurV2,edg);
+
       TopoDS_Shape aLocalShape = edg.EmptyCopied();
       TopoDS_Edge E1 = TopoDS::Edge(aLocalShape);
       aLocalShape = edg.EmptyCopied();
@@ -194,37 +203,50 @@ void LocOpe_SplitShape::Add(const TopoDS_Vertex& V,
       E1.Orientation(TopAbs_FORWARD);
       E2.Orientation(TopAbs_FORWARD);
       TopoDS_Vertex newVtx = V;
+      Standard_Real aTolSplitV = BRep_Tool::Tolerance(V);
+      
+      aCurV1.Orientation(TopAbs_FORWARD);
+     
+      B.Add(E1,aCurV1);
+      //for degenerated edges tolerance of vertices should be set to maximal value
+      //from tolerance of the vertex of the edge and tolerance of splitting vertex
+      Standard_Real aTolV1 = ( BRep_Tool::Degenerated(edg) ?
+        Max(BRep_Tool::Tolerance(aCurV1), aTolSplitV) : BRep_Tool::Tolerance(aCurV1));
+     
+      B.UpdateVertex(aCurV1,aPar1,E1, aTolV1);
       newVtx.Orientation(TopAbs_REVERSED);
       B.Add(E1,newVtx);
       B.UpdateVertex(newVtx,P,E1,BRep_Tool::Tolerance(V));
       newVtx.Orientation(TopAbs_FORWARD);
       B.Add(E2,newVtx);
       B.UpdateVertex(newVtx,P,E2,BRep_Tool::Tolerance(V));
-      edg.Orientation(TopAbs_FORWARD);
-      TopExp_Explorer exp;
-      for (exp.Init(edg,TopAbs_VERTEX); exp.More(); exp.Next()) {
-        //    for (TopExp_Explorer exp(edg,TopAbs_VERTEX); exp.More(); exp.Next()) {
-        const TopoDS_Vertex& vtx = TopoDS::Vertex(exp.Current());
-        f = BRep_Tool::Parameter(vtx,edg);
-        if (f < P) {
-          B.Add(E1,vtx);
-          B.UpdateVertex(vtx,f,E1,BRep_Tool::Tolerance(vtx));
-        }
-        else {
-          B.Add(E2,vtx);
-          B.UpdateVertex(vtx,f,E2,BRep_Tool::Tolerance(vtx));
-        }
+      
+      aCurV2.Orientation(TopAbs_REVERSED);
+      B.Add(E2,aCurV2);
+
+      //for degenerated edges tolerance of vertices should be set to maximal value
+      //from tolerance of the vertex of the edge and tolerance of splitting vertex
+      Standard_Real aTolV2 = ( BRep_Tool::Degenerated(edg) ? aTolV1 : BRep_Tool::Tolerance(aCurV2));
+      B.UpdateVertex(aCurV2,aPar2,E2,aTolV2);
+      
+      aNewList.Append(E1);
+      aNewList.Append(E2);
+      for (; itl.More(); itl.Next()) 
+      {
+        const TopoDS_Edge& edg1 = TopoDS::Edge(itl.Value());
+        aNewList.Append(edg1);
       }
-      le.Append(E1);
-      le.Append(E2);
+      myMap.UnBind(E);
+      myMap.Bind(E, aNewList);
+
   }
   else {
     TopoDS_Shape aLocalShape = edg.EmptyCopied();
     TopoDS_Edge E1 = TopoDS::Edge(aLocalShape);
-    //    TopoDS_Edge E1 = TopoDS::Edge(edg.EmptyCopied());
+   
     TopExp_Explorer exp;
     for (exp.Init(edg,TopAbs_VERTEX); exp.More(); exp.Next()) {
-      //    for (TopExp_Explorer exp(edg,TopAbs_VERTEX); exp.More(); exp.Next()) {
+     
       const TopoDS_Vertex& vtx = TopoDS::Vertex(exp.Current());
       f = BRep_Tool::Parameter(vtx,edg);
       B.Add(E1,vtx);
@@ -241,12 +263,12 @@ void LocOpe_SplitShape::Add(const TopoDS_Vertex& V,
 //purpose  : adds the list of wires on the face <F>
 //=======================================================================
 
-void LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires,
+Standard_Boolean LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires,
                             const TopoDS_Face& F)
 {
 
   if (myDone) {
-    Standard_ConstructionError::Raise();
+    return Standard_False;
   }
 
   TopTools_ListOfShape& lf = myMap(F);
@@ -261,25 +283,25 @@ void LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires,
 
   BRepTools::Update(F);
 
+  TopTools_ListOfShape aLInside;
   for (; itl.More(); itl.Next())
   {
     const TopoDS_Face& fac = TopoDS::Face(itl.Value());
-    Standard_Boolean AllWiresInside = Standard_True;
     TopTools_ListIteratorOfListOfShape itwires(Lwires);
     for (; itwires.More(); itwires.Next())
     {
       const TopoDS_Wire& aWire = TopoDS::Wire(itwires.Value());
-      if (!IsInside(fac, aWire))
+      if (IsInside(fac, aWire))
       {
-        AllWiresInside = Standard_False;
-        break;
+        aLInside.Append(aWire);
+        
       }
     }
-    if (AllWiresInside)
+    if(aLInside.Extent())
       break;
   }
-  if (!itl.More()) {
-    Standard_ConstructionError::Raise();
+  if (!aLInside.Extent() || !itl.More()) {
+    return Standard_False;
   }
 
   TopoDS_Face FaceRef = TopoDS::Face(itl.Value());
@@ -289,7 +311,7 @@ void LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires,
   TopTools_ListOfShape NewWires;
 
   TopTools_DataMapOfShapeInteger SectionsTimes;
-  for (itl.Initialize(Lwires); itl.More(); itl.Next())
+  for (itl.Initialize(aLInside); itl.More(); itl.Next())
     SectionsTimes.Bind(itl.Value(), 2);
   
   TopTools_ListOfShape BreakVertices;
@@ -298,7 +320,7 @@ void LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires,
   TopTools_DataMapOfShapeShape VerWireMap;
   Standard_Integer i;
   TopExp_Explorer ExploF, ExploW;
-  for (itl.Initialize(Lwires); itl.More(); itl.Next())
+  for (itl.Initialize(aLInside); itl.More(); itl.Next())
   {
     const TopoDS_Wire& aSection = TopoDS::Wire(itl.Value());
     TopoDS_Vertex Ver [2];
@@ -327,7 +349,7 @@ void LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires,
   }  
   
   TopTools_DataMapOfShapeListOfShape VerSecMap;
-  for (itl.Initialize(Lwires); itl.More(); itl.Next())
+  for (itl.Initialize(aLInside); itl.More(); itl.Next())
   {
     const TopoDS_Wire& aWire = TopoDS::Wire(itl.Value());
     TopoDS_Vertex V1, V2;
@@ -524,7 +546,7 @@ void LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires,
   ///////////////////
   
   // JAG 10.11.95 Codage des regularites
-  for (itl.Initialize(Lwires); itl.More(); itl.Next())
+  for (itl.Initialize(aLInside); itl.More(); itl.Next())
     for (ExploW.Init(itl.Value(), TopAbs_EDGE); ExploW.More(); ExploW.Next())
     {
       const TopoDS_Edge& edg = TopoDS::Edge(ExploW.Current());
@@ -532,6 +554,7 @@ void LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires,
         BB.Continuity(edg,F,F,GeomAbs_CN);
       }
     }
+  return Standard_True;
 }
 
 
@@ -540,12 +563,12 @@ void LocOpe_SplitShape::Add(const TopTools_ListOfShape& Lwires,
 //purpose  : 
 //=======================================================================
 
-void LocOpe_SplitShape::Add(const TopoDS_Wire& W,
+Standard_Boolean LocOpe_SplitShape::Add(const TopoDS_Wire& W,
                             const TopoDS_Face& F)
 {
 
   if (myDone) {
-    Standard_ConstructionError::Raise();
+    return Standard_False;
   }
 
 
@@ -557,16 +580,18 @@ void LocOpe_SplitShape::Add(const TopoDS_Wire& W,
   try {
     OCC_CATCH_SIGNALS
     if (!LocOpe::Closed(W,F)) {
-      AddOpenWire(W,F);
+      if(!AddOpenWire(W,F))
+        return Standard_False;
     }
     else {
-      AddClosedWire(W,F);
+      if(!AddClosedWire(W,F))
+        return Standard_False;
     }
   } catch (Standard_Failure ) {
 #ifdef OCCT_DEBUG
     cout << "Warning: SpliShape internal problem detected, some faces may be lost. Check input edges/wires" <<endl;
 #endif
-    return;
+    return Standard_False;
   }
   // JAG 10.11.95 Codage des regularites
   BRep_Builder B;
@@ -576,6 +601,7 @@ void LocOpe_SplitShape::Add(const TopoDS_Wire& W,
       B.Continuity(edg,F,F,GeomAbs_CN);
     }
   }
+  return Standard_True;
 }
 
 
@@ -585,7 +611,7 @@ void LocOpe_SplitShape::Add(const TopoDS_Wire& W,
 //purpose  : 
 //=======================================================================
 
-void LocOpe_SplitShape::AddClosedWire(const TopoDS_Wire& W,
+Standard_Boolean LocOpe_SplitShape::AddClosedWire(const TopoDS_Wire& W,
                                       const TopoDS_Face& F)
 {
   TopExp_Explorer exp;
@@ -596,19 +622,14 @@ void LocOpe_SplitShape::AddClosedWire(const TopoDS_Wire& W,
   TopoDS_Wire outerW;
   for (; itl.More(); itl.Next()) {
     const TopoDS_Face& fac = TopoDS::Face(itl.Value());
-    /*
-    outerW = BRepTools::OuterWire(fac);
-    if (IsInside(F,W,outerW)) {
-    break;
-    }
-    */
+
     if (IsInside(fac,W)) {
       break;
     }
 
   }
   if (!itl.More()) {
-    Standard_ConstructionError::Raise();
+    return Standard_False;
   }
 
   BRep_Builder B;
@@ -658,16 +679,60 @@ void LocOpe_SplitShape::AddClosedWire(const TopoDS_Wire& W,
   B.Add(newRef,W.Oriented(TopAbs::Reverse(orWire)));
   lf.Append(newRef);
   lf.Append(newFace);
-
+  return Standard_True;
 }
 
+//=======================================================================
+//function : checkOverlapping
+//purpose  : 
+//=======================================================================
+
+static Standard_Boolean checkOverlapping(const TopoDS_Edge& theEdge1, const TopoDS_Edge& theEdge2,
+  const TopoDS_Face& theFace)
+{
+
+  BRepAdaptor_Surface anAdS(theFace,Standard_False );
+   
+  Standard_Real MaxTol = (BRep_Tool::Tolerance(theEdge1) + BRep_Tool::Tolerance(theEdge2));
+  Standard_Real aMaxTol2d = Max(anAdS.UResolution(MaxTol),anAdS.VResolution(MaxTol));
+  Standard_Real aTolAng = M_PI/180.;
+  Geom2dAPI_ProjectPointOnCurve proj;
+  Standard_Real aF1, aL1,aF2, aL2;
+
+  Handle(Geom2d_Curve) aCrv1 = BRep_Tool::CurveOnSurface(theEdge1, theFace, aF1, aL1);
+  Handle(Geom2d_Curve) aCrv2 = BRep_Tool::CurveOnSurface(theEdge2, theFace, aF2, aL2);
+  Standard_Integer j =1, nbP = 4;
+  Standard_Real aStep = ( aL2 - aF2)/nbP;
+  for( ; j < nbP; j++)
+  {
+    Standard_Real par2 = aF2 + aStep * j;
+    gp_Pnt2d aP2d; 
+    gp_Vec2d aV2;
+    aCrv2->D1(par2, aP2d, aV2);
+
+    proj.Init(aP2d,aCrv1, aF1, aL1);
+    //check intermediate points
+    if(!proj.NbPoints() ||  proj.LowerDistance() > aMaxTol2d)
+      return Standard_False;
+    Standard_Real par1 = proj.LowerDistanceParameter();
+    gp_Pnt2d aP2d1; 
+    gp_Vec2d aV1;
+    aCrv1->D1(par1, aP2d1, aV1);
+
+     if( !aV1.IsParallel(aV2, aTolAng))
+      return Standard_False;
+  }
+  return Standard_True;
+
+}
 
 //=======================================================================
 //function : AddOpenWire
 //purpose  : 
 //=======================================================================
 
-void LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W,
+Standard_Boolean LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W,
                                     const TopoDS_Face& F)
 {
   // On cherche la face descendante de F qui continent le wire
@@ -723,7 +788,7 @@ void LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W,
     }
   }
   if (!itl.More()) {
-    Standard_ConstructionError::Raise();
+    return Standard_False;
   }
 
   TopoDS_Face FaceRef = TopoDS::Face(itl.Value());
@@ -820,27 +885,8 @@ void LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W,
       }
     }
     aLocalFace  = FaceRef.Oriented(wfirst.Orientation());
-    C2d = BRep_Tool::CurveOnSurface(LastEdge, TopoDS::Face(aLocalFace), f, l);
-    Standard_Real dpar = (l - f)*0.01;
-    if (LastEdge.Orientation() == TopAbs_FORWARD) {
-      C2d->D1(l,plast,dlast);
-      if (dlast.Magnitude() < gp::Resolution())
-      {
-        gp_Pnt2d PrevPnt = C2d->Value(l - dpar);
-        dlast.SetXY(plast.XY() - PrevPnt.XY());
-      }
-    }
-    else {
-      C2d->D1(f,plast,dlast);
-      if (dlast.Magnitude() < gp::Resolution())
-      {
-        gp_Pnt2d NextPnt = C2d->Value(f + dpar);
-        dlast.SetXY(NextPnt.XY() - plast.XY());
-      }
-      dlast.Reverse();
-    }
-
+    GetDirection(LastEdge, TopoDS::Face(aLocalFace),plast , dlast, Standard_False);
+   
     Standard_Boolean cond;
 
     if(IsPeriodic) {
@@ -861,12 +907,10 @@ void LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W,
 
         orient = edg.Orientation();
         TopExp::Vertices(edg,vdeb,vfin);
-        if (orient == TopAbs_FORWARD && Vlast.IsSame(vdeb)) {
-          PossE.Add(edg.Oriented(orient));
-        }
-        else if (orient == TopAbs_REVERSED && Vlast.IsSame(vfin)) {
-          PossE.Add(edg.Oriented(orient));
-        }
+        
+        if ((orient == TopAbs_FORWARD && Vlast.IsSame(vdeb)) || 
+          (orient == TopAbs_REVERSED && Vlast.IsSame(vfin))) 
+          PossE.Add(edg);
       }
       nbPoss = PossE.Extent();
       if (nbPoss == 0)
@@ -878,41 +922,24 @@ void LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W,
       if (nbPoss == 1) {
         aNextEdge = TopoDS::Edge (PossE.FindKey (1));
         TopoDS_Shape aLocalFaceTemp  = FaceRef.Oriented(wfirst.Orientation());
-        C2d = BRep_Tool::CurveOnSurface(aNextEdge,
-                                        TopoDS::Face(aLocalFaceTemp), f, l);
-        Standard_Real dparnew = (l - f)*0.01;
-
-        if (aNextEdge.Orientation() == TopAbs_FORWARD) {
-          C2d->D1(l,plast,dlast);
-          if (dlast.Magnitude() < gp::Resolution())
-          {
-            gp_Pnt2d PrevPnt = C2d->Value(l - dparnew);
-            dlast.SetXY(plast.XY() - PrevPnt.XY());
-          }
-        }
-        else {
-          C2d->D1(f,plast,dlast);
-          if (dlast.Magnitude() < gp::Resolution())
-          {
-            gp_Pnt2d NextPnt = C2d->Value(f + dparnew);
-            dlast.SetXY(NextPnt.XY() - plast.XY());
-          }
-          dlast.Reverse();
-        }
+        GetDirection(aNextEdge, TopoDS::Face(aLocalFaceTemp),plast , dlast, Standard_False);
+       
       }
       else if (nbPoss > 1) {
         // Faire choix en U,V...
         TopoDS_Shape aLocalFaceTemp  = FaceRef.Oriented(wfirst.Orientation());
         
-        ChoixUV(LastEdge, TopoDS::Face(aLocalFaceTemp), PossE,
-                aNextEdge, plast, dlast, toll);
+        if(!ChoixUV(LastEdge, TopoDS::Face(aLocalFaceTemp), PossE,
+                aNextEdge, plast, dlast))
+                return Standard_False;
+
       }
 
       if (nbPoss >= 1) {
         if (aNextEdge.IsNull())
         {
           // loop is not closed. Split is not possible
-          Standard_ConstructionError::Raise("Split is not possible: split loop is not closed"); 
+          return Standard_False;
         }
 
         if (MapE.Contains(aNextEdge)) 
@@ -935,23 +962,65 @@ void LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W,
       //MODIFICATION PIERRE SMEYERS : si pas de possibilite, on sort avec erreur
       else{
         cout<<"erreur Spliter : pas de chainage du wire"<<endl;
-        Standard_ConstructionError::Raise();        
+        return Standard_False;
       }
       //fin MODIF.
       
       tol1 = Max(BAS.UResolution(tol1), BAS.VResolution(tol1));
       
     }
-    
+   
+    Standard_Integer nbAddBound =0;
     TopTools_ListIteratorOfListOfShape lexp(WiresFirst);
+    TopoDS_Shape anE1, anE2;
     for (; lexp.More(); lexp.Next()) {    
       const TopoDS_Edge& edg = TopoDS::Edge(lexp.Value());
       if (!MapE.Contains(edg)) {
         B.Add(newW2,edg);
         MapE.Add(edg);
+        nbAddBound++;
+        if(anE1.IsNull())
+          anE1 = edg;
+        else
+          anE2 = edg;
+        
       }
     }        
-   
+    //check overlapping edges for second face
+    if(nbAddBound <2)
+      return Standard_False;
+    if(nbAddBound ==2 && !anE1.IsNull() && !anE2.IsNull())
+    {
+      if(checkOverlapping(TopoDS::Edge(anE1), TopoDS::Edge(anE2),FaceRef ))
+        return Standard_False;
+      
+    }
+
+    nbAddBound =0;
+    
+    TopoDS_Shape anE11, anE12;
+    TopoDS_Iterator anItE(newW1, Standard_False);
+    for( ; anItE.More(); anItE.Next())
+    {
+      if( anItE.Value().ShapeType() != TopAbs_EDGE)
+        continue;
+      nbAddBound++;
+      if(anE11.IsNull())
+        anE11 = anItE.Value();
+      else
+        anE12 = anItE.Value();
+     
+    }
+    //check overlapping edges for first face
+    if(nbAddBound <2)
+      return Standard_False;
+    if(nbAddBound  ==2 && !anE11.IsNull() && !anE12.IsNull())
+    {
+      if(checkOverlapping(TopoDS::Edge(anE11), TopoDS::Edge(anE12), FaceRef))
+        return Standard_False;
+      
+    }
+
     TopoDS_Face newF1,newF2;
     aLocalFace = FaceRef.EmptyCopied();
     newF1 = TopoDS::Face(aLocalFace);
@@ -963,10 +1032,7 @@ void LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W,
     // modifs JAG 97.05.28
 
     B.Add(newF1,newW1);
-    //BRepTools::Write(newF1, "k:/queries/WrongBOP/NewF1.brep");
     B.Add(newF2,newW2);
-    //BRepTools::Write(newF2, "k:/queries/WrongBOP/NewF2.brep");
-    
     for (exp.Init(FaceRef.Oriented(TopAbs_FORWARD),TopAbs_WIRE); exp.More(); exp.Next()) {
       const TopoDS_Wire& wir = TopoDS::Wire(exp.Current());
       if (!wir.IsSame(wfirst)) {
@@ -1086,6 +1152,7 @@ void LocOpe_SplitShape::AddOpenWire(const TopoDS_Wire& W,
       
     }
   }
+  return Standard_True;
 }
 
 
@@ -1190,7 +1257,33 @@ void LocOpe_SplitShape::Put(const TopoDS_Shape& S)
   }
 }
 
+static void updateToleraces(const TopoDS_Face& theFace, const TopTools_DataMapOfShapeListOfShape& theMap)
+{
+  TopExp_Explorer aExpE(theFace, TopAbs_EDGE);
+  for (; aExpE.More(); aExpE.Next())
+  {
+    if (!theMap.IsBound(aExpE.Current()))
+      continue;
+    const TopTools_ListOfShape& lEdges = theMap(aExpE.Current());
+    if (lEdges.Extent() <= 1)
+      continue;
 
+    TopTools_ListIteratorOfListOfShape itrE(lEdges);
+    ShapeAnalysis_Edge aSae;
+    
+    for (; itrE.More(); itrE.Next())
+    {
+      TopoDS_Edge aCurE = TopoDS::Edge(itrE.Value());
+      Standard_Real amaxdev = 0.;
+      if (aSae.CheckSameParameter(aCurE, theFace, amaxdev))
+      {
+        BRep_Builder aB;
+        aB.UpdateEdge(aCurE, amaxdev);
+      }
+    }
+  }
+
+}
 //=======================================================================
 //function : Rebuild
 //purpose  : 
@@ -1199,7 +1292,8 @@ void LocOpe_SplitShape::Put(const TopoDS_Shape& S)
 Standard_Boolean LocOpe_SplitShape::Rebuild(const TopoDS_Shape& S)
 
 {
-
+  if (S.ShapeType() == TopAbs_FACE)
+    updateToleraces(TopoDS::Face(S), myMap);
   TopTools_ListIteratorOfListOfShape itr(myMap(S));
   if (itr.More()) {
     if (itr.Value().IsSame(S)) {
@@ -1368,44 +1462,35 @@ static Standard_Boolean IsInside(const TopoDS_Face& F,
 //=======================================================================
 static void GetDirection(const TopoDS_Edge& theEdge,
                          const TopoDS_Face& theFace,
-                         Standard_Real& theTol,
                          gp_Pnt2d& thePnt,
-                         gp_Vec2d& theDir)
+                         gp_Vec2d& theDir,
+                         Standard_Boolean isFirstEnd)
 {
   Standard_Real aFirst, aLast;
   Handle(Geom2d_Curve) aC2d = BRep_Tool::CurveOnSurface (theEdge, theFace, aFirst, aLast);
 
   TopAbs_Orientation anOr = theEdge.Orientation();
   TopoDS_Vertex aVtx;
-  if (anOr == TopAbs_FORWARD)
+  Standard_Boolean takeFirst = ((anOr == TopAbs_FORWARD && isFirstEnd) ||
+    (anOr == TopAbs_REVERSED && !isFirstEnd));
+  Standard_Real dpar = (aLast - aFirst)*0.01;
+  gp_Pnt2d aP2d;
+  if (takeFirst)
   {
-    aVtx = TopExp::FirstVertex (theEdge);
     aC2d->D0 (aFirst, thePnt);
+    gp_Pnt2d aNextPnt = aC2d->Value(aFirst + dpar);
+    theDir = gp_Vec2d(thePnt, aNextPnt);
   }
-  else
-  {
-    aVtx = TopExp::LastVertex (theEdge);
-    aC2d->D0 (aLast, thePnt);
-  }
-
-  BRepAdaptor_Surface aSurf (theFace, Standard_False);
-  theTol = BRep_Tool::Tolerance (aVtx);
-  Standard_Real aTol = Max (aSurf.UResolution (theTol), aSurf.VResolution (theTol));
-  aTol = Min (aTol, (aLast - aFirst)*0.1);
-
-  gp_Pnt2d aP2d;
 
-  if (anOr == TopAbs_FORWARD)
-  {
-      aFirst += aTol;
-      aC2d->D0 (aFirst, aP2d);
-  }
   else
   {
-      aLast -= aTol;
-      aC2d->D0 (aLast, aP2d);
+    aC2d->D0 (aLast, thePnt);
+    gp_Pnt2d aPrevPnt = aC2d->Value(aLast - dpar);
+    theDir = gp_Vec2d( aPrevPnt, thePnt );
   }
-  theDir = gp_Vec2d (thePnt, aP2d);
+  if(anOr == TopAbs_REVERSED)
+    theDir.Reverse();
 }
 
 //=======================================================================
@@ -1413,16 +1498,13 @@ static void GetDirection(const TopoDS_Edge& theEdge,
 //purpose  : 
 //=======================================================================
 
-static void ChoixUV(const TopoDS_Edge& Last,
+Standard_Boolean ChoixUV(const TopoDS_Edge& Last,
                     const TopoDS_Face& F,
                     const TopTools_IndexedMapOfShape& Poss,
                     TopoDS_Edge& theResEdge,
                     gp_Pnt2d& plst,
-                    gp_Vec2d& dlst,
-                    const Standard_Real toll)
+                    gp_Vec2d& dlst)
 {
-
-  Standard_Real f,l;
   gp_Pnt2d p2d;
   gp_Vec2d v2d;
   gp_Pnt aPCur, aPlst;
@@ -1430,27 +1512,21 @@ static void ChoixUV(const TopoDS_Edge& Last,
   BRepAdaptor_Surface surf(F,Standard_False); // no restriction
   surf.D0 (plst.X(), plst.Y(), aPlst);
 
-  Standard_Real tol;
-
   gp_Dir2d ref2d(dlst);
 
   Handle(Geom2d_Curve) C2d;
-  Standard_Real dpar;
+  
 
   Standard_Integer index = 0, imin=0;
-  Standard_Real  angmax = -M_PI, dist, ang;
-
-
+  Standard_Real  angmax = -M_PI, ang;
+  
   for (index = 1; index <= Poss.Extent(); index++) {
     TopoDS_Edge anEdge = TopoDS::Edge (Poss.FindKey (index));
-    GetDirection (anEdge, F, tol, p2d, v2d);
+    
+    GetDirection(anEdge, F, p2d, v2d, Standard_True);
 
     surf.D0 (p2d.X(), p2d.Y(), aPCur);
 
-    tol = Max(toll, tol); tol *= tol;
-
-    dist = aPCur.SquareDistance(aPlst);
-
     if (!Last.IsSame(anEdge)) {
       ang = ref2d.Angle(gp_Dir2d(v2d));
     }
@@ -1458,7 +1534,7 @@ static void ChoixUV(const TopoDS_Edge& Last,
       ang = -M_PI;
     }
 
-    if ((dist - tol < Epsilon(1.0))  && (ang > angmax)) {
+    if ( (ang > angmax)) {
       imin = index;
       angmax = ang;
     }
@@ -1467,29 +1543,10 @@ static void ChoixUV(const TopoDS_Edge& Last,
   if (imin)
   {
     theResEdge = TopoDS::Edge (Poss.FindKey (imin));
-    C2d = BRep_Tool::CurveOnSurface (theResEdge, F, f, l);
-    dpar = (l - f)*0.01;
-    if (theResEdge.Orientation() == TopAbs_FORWARD)
-    {
-      C2d->D1 (l, plst, dlst);
-      if (dlst.Magnitude() < gp::Resolution())
-      {
-        gp_Pnt2d PrevPnt = C2d->Value(l - dpar);
-        dlst.SetXY(plst.XY() - PrevPnt.XY());
-      }
-    }
-    else
-    {
-      C2d->D1 (f, plst, dlst);
-      if (dlst.Magnitude() < gp::Resolution())
-      {
-        gp_Pnt2d NextPnt = C2d->Value(f + dpar);
-        dlst.SetXY(NextPnt.XY() - plst.XY());
-      }
-      dlst.Reverse();
-    }
+    GetDirection(theResEdge, F, plst, dlst, Standard_False);
   }
 
+  return imin != 0;
 }
 
 //=======================================================================