0024328: Revolution of a wire generates two interfered faces.
[occt.git] / src / BOPTools / BOPTools_AlgoTools.cxx
index c6e3bf3..f0c6145 100644 (file)
@@ -31,6 +31,7 @@
 #include <gp_Cone.hxx>
 #include <gp_Sphere.hxx>
 #include <gp_Torus.hxx>
+#include <gp_Lin.hxx>
 //
 #include <Geom2d_Curve.hxx>
 #include <Geom_Surface.hxx>
@@ -39,7 +40,6 @@
 #include <Geom_Curve.hxx>
 #include <GeomAPI_ProjectPointOnSurf.hxx>
 #include <Geom2dInt_Geom2dCurveTool.hxx>
-#include <GeomAdaptor_Surface.hxx>
 //
 #include <TopAbs_Orientation.hxx>
 //
@@ -53,6 +53,7 @@
 #include <BRep_Tool.hxx>
 #include <BRepLib.hxx>
 #include <BRepAdaptor_Curve2d.hxx>
+#include <BRepAdaptor_Surface.hxx>
 #include <BRepClass3d_SolidClassifier.hxx>
 #include <TopExp.hxx>
 #include <TopExp_Explorer.hxx>
@@ -87,7 +88,7 @@ static
   TopAbs_Orientation Orientation(const TopoDS_Edge& anE,
                                  const TopoDS_Face& aF);
 
-static 
+static
   void GetFaceDir(const TopoDS_Edge& aE,
                   const TopoDS_Face& aF,
                   const gp_Pnt& aP,
@@ -95,25 +96,25 @@ static
                   const gp_Dir& aDTgt,
                   gp_Dir& aDN,
                   gp_Dir& aDB,
-                  Handle(BOPInt_Context)& theContext);
-static 
+                  Handle(BOPInt_Context)& theContext,
+                  GeomAPI_ProjectPointOnSurf& aProjPL);
+static
   Standard_Boolean FindPointInFace(const TopoDS_Edge& aE,
                                    const TopoDS_Face& aF,
                                    const gp_Pnt& aP,
-                                   const Standard_Real aT,
                                    gp_Dir& aDB,
                                    gp_Pnt& aPOut,
                                    Handle(BOPInt_Context)& theContext,
-                                   const GeomAdaptor_Surface& aGAS);
+                                   GeomAPI_ProjectPointOnSurf& aProjPL);
 
 //=======================================================================
 // function: MakeConnexityBlocks
 // purpose: 
 //=======================================================================
-  void BOPTools_AlgoTools::MakeConnexityBlocks (const TopoDS_Shape& theS,
-                                            const TopAbs_ShapeEnum theType1,
-                                            const TopAbs_ShapeEnum theType2,
-                                            BOPCol_ListOfShape& theLCB)
+void BOPTools_AlgoTools::MakeConnexityBlocks (const TopoDS_Shape& theS,
+                                              const TopAbs_ShapeEnum theType1,
+                                              const TopAbs_ShapeEnum theType2,
+                                              BOPCol_ListOfShape& theLCB)
 {
   Standard_Integer  aNbF, aNbAdd, aNbAdd1, i;
   BRep_Builder aBB;
@@ -196,7 +197,7 @@ static
 // function: OrientFacesOnShell
 // purpose: 
 //=======================================================================
-  void BOPTools_AlgoTools::OrientFacesOnShell (TopoDS_Shape& aShell)
+void BOPTools_AlgoTools::OrientFacesOnShell (TopoDS_Shape& aShell)
 {
   Standard_Boolean bIsProcessed1, bIsProcessed2;
   Standard_Integer i, aNbE, aNbF, j;
@@ -324,8 +325,8 @@ static
 //function : Orientation
 //purpose  :
 //=======================================================================
-  TopAbs_Orientation Orientation(const TopoDS_Edge& anE,
-                                 const TopoDS_Face& aF)
+TopAbs_Orientation Orientation(const TopoDS_Edge& anE,
+                               const TopoDS_Face& aF)
 {
   TopAbs_Orientation anOr=TopAbs_INTERNAL;
 
@@ -348,10 +349,10 @@ static
 // function: MakeConnexityBlock.
 // purpose: 
 //=======================================================================
-  void BOPTools_AlgoTools::MakeConnexityBlock (BOPCol_ListOfShape& theLFIn,
-                                           BOPCol_IndexedMapOfShape& theMEAvoid,
-                                           BOPCol_ListOfShape& theLCB,
-                                           const Handle(NCollection_BaseAllocator)& theAllocator)
+void BOPTools_AlgoTools::MakeConnexityBlock (BOPCol_ListOfShape& theLFIn,
+                                             BOPCol_IndexedMapOfShape& theMEAvoid,
+                                             BOPCol_ListOfShape& theLCB,
+                                             const Handle(NCollection_BaseAllocator)& theAllocator)
 {
   Standard_Integer  aNbF, aNbAdd1, aNbAdd, i;
   TopExp_Explorer aExp;
@@ -428,10 +429,10 @@ static
 // function:  ComputeStateByOnePoint
 // purpose: 
 //=======================================================================
-  TopAbs_State BOPTools_AlgoTools::ComputeStateByOnePoint(const TopoDS_Shape& theS,
-                                                      const TopoDS_Solid& theRef,
-                                                      const Standard_Real theTol,
-                                                      Handle(BOPInt_Context)& theContext)
+TopAbs_State BOPTools_AlgoTools::ComputeStateByOnePoint(const TopoDS_Shape& theS,
+                                                        const TopoDS_Solid& theRef,
+                                                        const Standard_Real theTol,
+                                                        Handle(BOPInt_Context)& theContext)
 {
   TopAbs_State aState;
   TopAbs_ShapeEnum aType;
@@ -453,11 +454,11 @@ static
 // function:  ComputeState
 // purpose: 
 //=======================================================================
-  TopAbs_State BOPTools_AlgoTools::ComputeState(const TopoDS_Face& theF,
-                                            const TopoDS_Solid& theRef,
-                                            const Standard_Real theTol,
-                                            BOPCol_IndexedMapOfShape& theBounds,
-                                            Handle(BOPInt_Context)& theContext)
+TopAbs_State BOPTools_AlgoTools::ComputeState(const TopoDS_Face& theF,
+                                              const TopoDS_Solid& theRef,
+                                              const Standard_Real theTol,
+                                              BOPCol_IndexedMapOfShape& theBounds,
+                                              Handle(BOPInt_Context)& theContext)
 {
   TopAbs_State aState;
   TopExp_Explorer aExp; 
@@ -495,10 +496,10 @@ static
 // function:  ComputeState
 // purpose: 
 //=======================================================================
-  TopAbs_State BOPTools_AlgoTools::ComputeState(const TopoDS_Vertex& theV,
-                                            const TopoDS_Solid& theRef,
-                                            const Standard_Real theTol,
-                                            Handle(BOPInt_Context)& theContext)
+TopAbs_State BOPTools_AlgoTools::ComputeState(const TopoDS_Vertex& theV,
+                                              const TopoDS_Solid& theRef,
+                                              const Standard_Real theTol,
+                                              Handle(BOPInt_Context)& theContext)
 {
   TopAbs_State aState;
   gp_Pnt aP3D; 
@@ -511,10 +512,10 @@ static
 // function:  ComputeState
 // purpose: 
 //=======================================================================
-  TopAbs_State BOPTools_AlgoTools::ComputeState(const TopoDS_Edge& theE,
-                                            const TopoDS_Solid& theRef,
-                                            const Standard_Real theTol,
-                                            Handle(BOPInt_Context)& theContext)
+TopAbs_State BOPTools_AlgoTools::ComputeState(const TopoDS_Edge& theE,
+                                              const TopoDS_Solid& theRef,
+                                              const Standard_Real theTol,
+                                              Handle(BOPInt_Context)& theContext)
 {
   Standard_Real aT1, aT2, aT = 0.;
   TopAbs_State aState;
@@ -561,10 +562,10 @@ static
 // function:  ComputeState
 // purpose: 
 //=======================================================================
-  TopAbs_State BOPTools_AlgoTools::ComputeState(const gp_Pnt& theP,
-                                            const TopoDS_Solid& theRef,
-                                            const Standard_Real theTol,
-                                            Handle(BOPInt_Context)& theContext)
+TopAbs_State BOPTools_AlgoTools::ComputeState(const gp_Pnt& theP,
+                                              const TopoDS_Solid& theRef,
+                                              const Standard_Real theTol,
+                                              Handle(BOPInt_Context)& theContext)
 {
   TopAbs_State aState;
   //
@@ -579,22 +580,33 @@ static
 //function : IsInternalFace
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::IsInternalFace(const TopoDS_Face& theFace,
-                                                      const TopoDS_Solid& theSolid,
-                                                      BOPCol_IndexedDataMapOfShapeListOfShape& theMEF,
-                                                      const Standard_Real theTol,
-                                                      Handle(BOPInt_Context)& theContext)
+Standard_Integer BOPTools_AlgoTools::IsInternalFace
+  (const TopoDS_Face& theFace,
+   const TopoDS_Solid& theSolid,
+   BOPCol_IndexedDataMapOfShapeListOfShape& theMEF,
+   const Standard_Real theTol,
+   Handle(BOPInt_Context)& theContext)
 {
-  Standard_Boolean bRet, bDegenerated;
-  Standard_Integer aNbF;
+  Standard_Boolean bDegenerated;
+  Standard_Integer aNbF, iRet, iFound;
   TopAbs_Orientation aOr;
-  TopoDS_Edge aEL;
+  TopoDS_Edge aE1;
   TopExp_Explorer aExp;
   BOPCol_ListIteratorOfListOfShape aItF;
   //
-  bRet=Standard_False;
+  // For all invoked functions: [::IsInternalFace(...)] 
+  // the returned value iRet means:
+  // iRet=0;  - state is not IN
+  // iRet=1;  - state is IN
+  // iRet=2;  - state can not be found by the method of angles
   //
+  // For this function the returned value iRet means:
+  // iRet=0;  - state is not IN
+  // iRet=1;  - state is IN
+  //
+  iRet=0; 
   // 1 Try to find an edge from theFace in theMEF
+  iFound=0;
   aExp.Init(theFace, TopAbs_EDGE);
   for(; aExp.More(); aExp.Next()) {
     const TopoDS_Edge& aE=(*(TopoDS_Edge*)(&aExp.Current()));
@@ -602,6 +614,8 @@ static
       continue;
     }
     //
+    ++iFound;
+    //
     aOr=aE.Orientation();
     if (aOr==TopAbs_INTERNAL) {
       continue;
@@ -614,64 +628,85 @@ static
     BOPCol_ListOfShape& aLF=theMEF.ChangeFromKey(aE);
     aNbF=aLF.Extent();
     if (!aNbF) {
-      return bRet; // it can not be so
+      return iRet; // it can not be so
     }
+    //
     else if (aNbF==1) {
       // aE is internal edge on aLF.First()
       const TopoDS_Face& aF1=(*(TopoDS_Face*)(&aLF.First()));
-      bRet=BOPTools_AlgoTools::IsInternalFace(theFace, aE, aF1, aF1, theContext);
-      return bRet;
+      BOPTools_AlgoTools::GetEdgeOnFace(aE, aF1, aE1);
+      if (aE1.Orientation()!=TopAbs_INTERNAL) {
+        iRet=2; 
+        break;
+      }
+      //
+      iRet=BOPTools_AlgoTools::IsInternalFace(theFace, aE, aF1, aF1, theContext);
+      break;
     }
+    //
     else if (aNbF==2) {
       const TopoDS_Face& aF1=(*(TopoDS_Face*)(&aLF.First()));
       const TopoDS_Face& aF2=(*(TopoDS_Face*)(&aLF.Last()));
       //
       if (aF2.IsSame(aF1) && BRep_Tool::IsClosed(aE, aF1)) {
         // treat as it was for 1 face
-        bRet=BOPTools_AlgoTools::IsInternalFace(theFace, aE, aF1, aF2, theContext);
-        return bRet;
+        iRet=BOPTools_AlgoTools::IsInternalFace(theFace, aE, aF1, aF2, theContext);
+        break;
       }
     }
+    //
     if (aNbF%2) {
-      return bRet; // it can not be so
+      iRet=0;
+      return iRet; // it can not be so
     }
     else { // aNbF=2,4,6,8,...
-      bRet=BOPTools_AlgoTools::IsInternalFace(theFace, aE, aLF, theContext);
-      return bRet;
+      iRet=BOPTools_AlgoTools::IsInternalFace(theFace, aE, aLF, theContext);
+      break;
     }
   }//for(; aExp.More(); aExp.Next()) {
   //
+  if (!iFound) {
+    // the face has no shared edges with the solid
+    iRet=2;
+  }
+  //
+  if (iRet!=2) {
+    return iRet;
+  }
+  //
   //========================================
   // 2. Classify face using classifier
   //
   TopAbs_State aState;
   BOPCol_IndexedMapOfShape aBounds;
   //
+  BOPTools::MapShapes(theSolid, TopAbs_EDGE, aBounds);
+  //
   aState=BOPTools_AlgoTools::ComputeState(theFace, theSolid, theTol, aBounds, theContext);
-  bRet=(aState==TopAbs_IN);
   //
-  return bRet;
+  iRet=(aState==TopAbs_IN)? 1 : 0;
+  //
+  return iRet;
 }
 //=======================================================================
 //function : IsInternalFace
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::IsInternalFace(const TopoDS_Face& theFace,
-                                                      const TopoDS_Edge& theEdge,
-                                                      BOPCol_ListOfShape& theLF,
-                                                      Handle(BOPInt_Context)& theContext)
+Standard_Integer BOPTools_AlgoTools::IsInternalFace(const TopoDS_Face& theFace,
+                                                    const TopoDS_Edge& theEdge,
+                                                    BOPCol_ListOfShape& theLF,
+                                                    Handle(BOPInt_Context)& theContext)
 {
-  Standard_Boolean bRet;
-  Standard_Boolean aNbF;
+  Standard_Integer aNbF, iRet;
   //
-  bRet=Standard_False;
+  iRet=0;
   //
   aNbF=theLF.Extent();
   if (aNbF==2) {
     const TopoDS_Face& aF1=(*(TopoDS_Face*)(&theLF.First()));
     const TopoDS_Face& aF2=(*(TopoDS_Face*)(&theLF.Last()));
-    bRet=BOPTools_AlgoTools::IsInternalFace(theFace, theEdge, aF1, aF2, theContext);
-    return bRet;
+    iRet=BOPTools_AlgoTools::IsInternalFace(theFace, theEdge, aF1, aF2, theContext);
+    return iRet;
   }
   //
   else {
@@ -686,25 +721,27 @@ static
       //
       const TopoDS_Face& aF1=(*(TopoDS_Face*)(&aCSFF.Shape1()));
       const TopoDS_Face& aF2=(*(TopoDS_Face*)(&aCSFF.Shape2()));
-      bRet=BOPTools_AlgoTools::IsInternalFace(theFace, theEdge, aF1, aF2, theContext);
-      if (bRet) {
-        return bRet;
+      iRet=BOPTools_AlgoTools::IsInternalFace(theFace, theEdge, aF1, aF2, theContext);
+      if (iRet) {
+        return iRet;
       }
     }
   }
-  return bRet;
+  return iRet;
 }
 //=======================================================================
 //function : IsInternalFace
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::IsInternalFace(const TopoDS_Face& theFace,
-                                                      const TopoDS_Edge& theEdge,
-                                                      const TopoDS_Face& theFace1,
-                                                      const TopoDS_Face& theFace2,
-                                                      Handle(BOPInt_Context)& theContext)
+Standard_Integer BOPTools_AlgoTools::IsInternalFace
+  (const TopoDS_Face& theFace,
+   const TopoDS_Edge& theEdge,
+   const TopoDS_Face& theFace1,
+   const TopoDS_Face& theFace2,
+   Handle(BOPInt_Context)& theContext)
 {
   Standard_Boolean bRet;
+  Standard_Integer iRet;
   TopoDS_Edge aE1, aE2;
   TopoDS_Face aFOff;
   BOPTools_ListOfCoupleOfShape theLCSOff;
@@ -733,28 +770,41 @@ static
   aCS2.SetShape2(theFace2);
   theLCSOff.Append(aCS2);
   //
-  GetFaceOff(aE1, theFace1, theLCSOff, aFOff, theContext);
+  bRet=GetFaceOff(aE1, theFace1, theLCSOff, aFOff, theContext);
   //
-  bRet = theFace.IsEqual(aFOff);
-  return bRet;
+  iRet=0; // theFace is not internal
+  if (theFace.IsEqual(aFOff)) {
+    // theFace is internal
+    iRet=1;  
+    if (!bRet) {
+      // theFace seems to be internal  
+      iRet=2;
+    }
+  }
+  return iRet;
 }
 //=======================================================================
 //function : GetFaceOff
 //purpose  : 
 //=======================================================================
-  void BOPTools_AlgoTools::GetFaceOff(const TopoDS_Edge& theE1,
-                                      const TopoDS_Face& theF1,
-                                      BOPTools_ListOfCoupleOfShape& theLCSOff,
-                                      TopoDS_Face& theFOff,
-                                      Handle(BOPInt_Context)& theContext)
+Standard_Boolean BOPTools_AlgoTools::GetFaceOff
+  (const TopoDS_Edge& theE1,
+   const TopoDS_Face& theF1,
+   BOPTools_ListOfCoupleOfShape& theLCSOff,
+   TopoDS_Face& theFOff,
+   Handle(BOPInt_Context)& theContext)
 {
+  Standard_Boolean bRet;
   Standard_Real aT, aT1, aT2, aAngle, aTwoPI, aAngleMin;
+  Standard_Real aUmin, aUsup, aVmin, aVsup;
   gp_Pnt aPn1, aPn2, aPx;
   gp_Dir aDN1, aDN2, aDBF, aDBF2, aDTF;
   gp_Vec aVTgt;
   TopAbs_Orientation aOr;
   Handle(Geom_Curve)aC3D;
+  Handle(Geom_Plane) aPL;
   BOPTools_ListIteratorOfListOfCoupleOfShape aIt;
+  GeomAPI_ProjectPointOnSurf aProjPL;
   //
   aAngleMin=100.;
   aTwoPI=M_PI+M_PI;
@@ -766,10 +816,15 @@ static
   gp_Dir aDTgt(aVTgt), aDTgt2;
   aOr = theE1.Orientation();
   //
-  GetFaceDir(theE1, theF1, aPx, aT, aDTgt, aDN1, aDBF, theContext);
+  aPL = new Geom_Plane(aPx, aDTgt);
+  aPL->Bounds(aUmin, aUsup, aVmin, aVsup);
+  aProjPL.Init(aPL, aUmin, aUsup, aVmin, aVsup);
+  //
+  GetFaceDir(theE1, theF1, aPx, aT, aDTgt, aDN1, aDBF, theContext, aProjPL);
   //
   aDTF=aDN1^aDBF;
   //
+  bRet=Standard_True;
   aIt.Initialize(theLCSOff);
   for (; aIt.More(); aIt.Next()) {
     const BOPTools_CoupleOfShape& aCS=aIt.Value();
@@ -777,7 +832,7 @@ static
     const TopoDS_Face& aF2=(*(TopoDS_Face*)(&aCS.Shape2()));
     //
     aDTgt2 = (aE2.Orientation()==aOr) ? aDTgt : aDTgt.Reversed();
-    GetFaceDir(aE2, aF2, aPx, aT, aDTgt2, aDN2, aDBF2, theContext);
+    GetFaceDir(aE2, aF2, aPx, aT, aDTgt2, aDN2, aDBF2, theContext, aProjPL);
     //Angle
     aAngle=AngleWithRef(aDBF, aDBF2, aDTF);
     //
@@ -798,15 +853,20 @@ static
       aAngleMin=aAngle;
       theFOff=aF2;
     }
+    else if (aAngle==aAngleMin) {
+      // the minimal angle can not be found
+      bRet=Standard_False;  
+    }
   }
+  return bRet;
 }
 //=======================================================================
 //function : GetEdgeOff
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::GetEdgeOff(const TopoDS_Edge& theE1,
-                                              const TopoDS_Face& theF2,
-                                              TopoDS_Edge& theE2)
+Standard_Boolean BOPTools_AlgoTools::GetEdgeOff(const TopoDS_Edge& theE1,
+                                                const TopoDS_Face& theF2,
+                                                TopoDS_Edge& theE2)
 {
   Standard_Boolean bFound;
   TopAbs_Orientation aOr1, aOr1C, aOr2;
@@ -835,9 +895,9 @@ static
 //function : AreFacesSameDomain
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::AreFacesSameDomain(const TopoDS_Face& theF1,
-                                                          const TopoDS_Face& theF2,
-                                                          Handle(BOPInt_Context)& theContext)
+Standard_Boolean BOPTools_AlgoTools::AreFacesSameDomain(const TopoDS_Face& theF1,
+                                                        const TopoDS_Face& theF2,
+                                                        Handle(BOPInt_Context)& theContext)
 {
   Standard_Boolean bFlag;
   Standard_Integer iErr;
@@ -881,9 +941,9 @@ static
 //function : CheckSameGeom
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::CheckSameGeom(const TopoDS_Face& theF1,
-                                                 const TopoDS_Face& theF2,
-                                                 Handle(BOPInt_Context)& theContext)
+Standard_Boolean BOPTools_AlgoTools::CheckSameGeom(const TopoDS_Face& theF1,
+                                                   const TopoDS_Face& theF2,
+                                                   Handle(BOPInt_Context)& theContext)
 {
   Standard_Boolean bRet;
   Standard_Real aTolF1, aTolF2, aTol;
@@ -910,8 +970,8 @@ static
 // function: Sense
 // purpose: 
 //=======================================================================
-  Standard_Integer BOPTools_AlgoTools::Sense (const TopoDS_Face& theF1,
-                                          const TopoDS_Face& theF2)
+Standard_Integer BOPTools_AlgoTools::Sense (const TopoDS_Face& theF1,
+                                            const TopoDS_Face& theF2)
 {
   Standard_Integer iSense=0;
   gp_Dir aDNF1, aDNF2;
@@ -956,9 +1016,9 @@ static
 // function: IsSplitToReverse
 // purpose: 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::IsSplitToReverse(const TopoDS_Shape& theSp,
-                                                    const TopoDS_Shape& theSr,
-                                                    Handle(BOPInt_Context)& theContext)
+Standard_Boolean BOPTools_AlgoTools::IsSplitToReverse(const TopoDS_Shape& theSp,
+                                                      const TopoDS_Shape& theSr,
+                                                      Handle(BOPInt_Context)& theContext)
 {
   Standard_Boolean bRet;
   TopAbs_ShapeEnum aType;
@@ -990,9 +1050,9 @@ static
 //function :IsSplitToReverse
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::IsSplitToReverse(const TopoDS_Face& theFSp,
-                                                    const TopoDS_Face& theFSr,
-                                                    Handle(BOPInt_Context)& theContext)
+Standard_Boolean BOPTools_AlgoTools::IsSplitToReverse(const TopoDS_Face& theFSp,
+                                                      const TopoDS_Face& theFSr,
+                                                      Handle(BOPInt_Context)& theContext)
 {
   Standard_Boolean bRet, bFound, bInFace;
   Standard_Real aT1, aT2, aT, aU, aV, aScPr;
@@ -1079,9 +1139,9 @@ static
 //function :IsSplitToReverse
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::IsSplitToReverse(const TopoDS_Edge& aEF1,
-                                                    const TopoDS_Edge& aEF2,
-                                                    Handle(BOPInt_Context)& theContext)
+Standard_Boolean BOPTools_AlgoTools::IsSplitToReverse(const TopoDS_Edge& aEF1,
+                                                      const TopoDS_Edge& aEF2,
+                                                      Handle(BOPInt_Context)& theContext)
 {
   Standard_Boolean bRet, bIsDegenerated;
   //
@@ -1130,8 +1190,8 @@ static
 //function : IsHole
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::IsHole(const TopoDS_Shape& aW,
-                                          const TopoDS_Shape& aFace)
+Standard_Boolean BOPTools_AlgoTools::IsHole(const TopoDS_Shape& aW,
+                                            const TopoDS_Shape& aFace)
 {
   Standard_Boolean bIsHole;
   Standard_Integer i, aNbS;
@@ -1203,8 +1263,8 @@ static
 // function: MakeContainer
 // purpose: 
 //=======================================================================
-  void BOPTools_AlgoTools::MakeContainer(const TopAbs_ShapeEnum theType,
-                                     TopoDS_Shape& theC)
+void BOPTools_AlgoTools::MakeContainer(const TopAbs_ShapeEnum theType,
+                                       TopoDS_Shape& theC)
 {
   BRep_Builder aBB;
   //
@@ -1253,12 +1313,12 @@ static
 // function: MakePCurve
 // purpose: 
 //=======================================================================
-  void  BOPTools_AlgoTools::MakePCurve(const TopoDS_Edge& aE,
-                                       const TopoDS_Face& aF1,
-                                       const TopoDS_Face& aF2,
-                                       const IntTools_Curve& aIC,
-                                       const Standard_Boolean bPC1,
-                                       const Standard_Boolean bPC2)
+void BOPTools_AlgoTools::MakePCurve(const TopoDS_Edge& aE,
+                                    const TopoDS_Face& aF1,
+                                    const TopoDS_Face& aF2,
+                                    const IntTools_Curve& aIC,
+                                    const Standard_Boolean bPC1,
+                                    const Standard_Boolean bPC2)
 
 {
   Standard_Integer i;
@@ -1314,13 +1374,13 @@ static
 // function: MakeEdge
 // purpose: 
 //=======================================================================
-  void  BOPTools_AlgoTools::MakeEdge(const IntTools_Curve& theIC,
-                                 const TopoDS_Vertex& theV1,
-                                 const Standard_Real theT1,
-                                 const TopoDS_Vertex& theV2,
-                                 const Standard_Real theT2,
-                                 const Standard_Real theTolR3D,
-                                 TopoDS_Edge& theE)
+void BOPTools_AlgoTools::MakeEdge(const IntTools_Curve& theIC,
+                                  const TopoDS_Vertex& theV1,
+                                  const Standard_Real theT1,
+                                  const TopoDS_Vertex& theV2,
+                                  const Standard_Real theT2,
+                                  const Standard_Real theTolR3D,
+                                  TopoDS_Edge& theE)
 {
   Standard_Real aTolV;
   BRep_Builder aBB;
@@ -1343,9 +1403,9 @@ static
 // function: ComputeVV
 // purpose: 
 //=======================================================================
-  Standard_Integer BOPTools_AlgoTools::ComputeVV(const TopoDS_Vertex& aV1, 
-                                             const gp_Pnt& aP2,
-                                             const Standard_Real aTolP2)
+Standard_Integer BOPTools_AlgoTools::ComputeVV(const TopoDS_Vertex& aV1, 
+                                               const gp_Pnt& aP2,
+                                               const Standard_Real aTolP2)
 {
   Standard_Real aTolV1, aTolSum, aTolSum2, aD2;
   gp_Pnt aP1;
@@ -1367,8 +1427,8 @@ static
 // function: ComputeVV
 // purpose: 
 //=======================================================================
-  Standard_Integer BOPTools_AlgoTools::ComputeVV(const TopoDS_Vertex& aV1, 
-                                             const TopoDS_Vertex& aV2)
+Standard_Integer BOPTools_AlgoTools::ComputeVV(const TopoDS_Vertex& aV1, 
+                                               const TopoDS_Vertex& aV2)
 {
   Standard_Real aTolV1, aTolV2, aTolSum, aTolSum2, aD2;
   gp_Pnt aP1, aP2;
@@ -1391,8 +1451,8 @@ static
 // function: MakeVertex
 // purpose : 
 //=======================================================================
-  void BOPTools_AlgoTools::MakeVertex(BOPCol_ListOfShape& aLV,
-                                  TopoDS_Vertex& aVnew)
+void BOPTools_AlgoTools::MakeVertex(BOPCol_ListOfShape& aLV,
+                                    TopoDS_Vertex& aVnew)
 {
   Standard_Integer aNb;
   Standard_Real aTi, aDi, aDmax;
@@ -1435,9 +1495,9 @@ static
 //function : GetEdgeOnFace
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::GetEdgeOnFace(const TopoDS_Edge& theE1,
-                                                 const TopoDS_Face& theF2,
-                                                 TopoDS_Edge& theE2)
+Standard_Boolean BOPTools_AlgoTools::GetEdgeOnFace(const TopoDS_Edge& theE1,
+                                                   const TopoDS_Face& theF2,
+                                                   TopoDS_Edge& theE2)
 {
   Standard_Boolean bFound;
   TopoDS_Iterator aItF, aItW;
@@ -1470,7 +1530,7 @@ Standard_Boolean FindFacePairs (const TopoDS_Edge& theE,
 {
   Standard_Boolean bFound;
   Standard_Integer i, aNbCEF;
-  TopAbs_Orientation aOr, aOrC;
+  TopAbs_Orientation aOr, aOrC = TopAbs_FORWARD;
   BOPCol_MapOfShape aMFP;
   TopoDS_Face aF1, aF2;
   TopoDS_Edge aEL, aE1;
@@ -1608,10 +1668,10 @@ Standard_Real fsqrt(Standard_Real val)
 // function: IsBlockInOnFace
 // purpose: 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::IsBlockInOnFace (const IntTools_Range& aShrR,
-                                                        const TopoDS_Face& aF,
-                                                        const TopoDS_Edge& aE1,
-                                                        Handle(BOPInt_Context)& aContext)
+Standard_Boolean BOPTools_AlgoTools::IsBlockInOnFace (const IntTools_Range& aShrR,
+                                                      const TopoDS_Face& aF,
+                                                      const TopoDS_Edge& aE1,
+                                                      Handle(BOPInt_Context)& aContext)
 {
   Standard_Boolean bFlag;
   Standard_Real f1, l1, ULD, VLD;
@@ -1698,8 +1758,8 @@ Standard_Real fsqrt(Standard_Real val)
 //function : IsMicroEdge
 //purpose  : 
 //=======================================================================
-  Standard_Boolean BOPTools_AlgoTools::IsMicroEdge(const TopoDS_Edge& aE,
-                                                   const Handle(BOPInt_Context)& aCtx) 
+Standard_Boolean BOPTools_AlgoTools::IsMicroEdge(const TopoDS_Edge& aE,
+                                                 const Handle(BOPInt_Context)& aCtx) 
 {
   Standard_Boolean bRet;
   Standard_Integer iErr;
@@ -1736,14 +1796,15 @@ Standard_Real fsqrt(Standard_Real val)
 //function : GetFaceDir
 //purpose  : Get binormal direction for the face in the point aP
 //=======================================================================
-  void GetFaceDir(const TopoDS_Edge& aE,
-                  const TopoDS_Face& aF,
-                  const gp_Pnt& aP,
-                  const Standard_Real aT,
-                  const gp_Dir& aDTgt,
-                  gp_Dir& aDN,
-                  gp_Dir& aDB,
-                  Handle(BOPInt_Context)& theContext)
+void GetFaceDir(const TopoDS_Edge& aE,
+                const TopoDS_Face& aF,
+                const gp_Pnt& aP,
+                const Standard_Real aT,
+                const gp_Dir& aDTgt,
+                gp_Dir& aDN,
+                gp_Dir& aDB,
+                Handle(BOPInt_Context)& theContext,
+                GeomAPI_ProjectPointOnSurf& aProjPL)
 {
   BOPTools_AlgoTools3D::GetNormalToFaceOnEdge(aE, aF, aT, aDN);
   if (aF.Orientation()==TopAbs_REVERSED){
@@ -1751,22 +1812,13 @@ Standard_Real fsqrt(Standard_Real val)
   }
   aDB = aDN^aDTgt;
   //
-  Handle(Geom_Surface) aS = BRep_Tool::Surface(aF);
-  GeomAdaptor_Surface aGAS(aS);
-  if (aGAS.GetType()!=GeomAbs_Plane) {
-    gp_Pnt aPx;
-    if (!FindPointInFace(aE, aF, aP, aT, aDB, aPx, theContext, aGAS)) {
-      gp_Pnt2d aPx2D;
-      Handle(Geom_Plane) aPL;
-      GeomAPI_ProjectPointOnSurf aProj;
-      //
-      BOPTools_AlgoTools3D::PointNearEdge(aE, aF, aT, aPx2D, aPx, theContext);
-      aPL = new Geom_Plane(aP, aDTgt);
-      aProj.Init(aPx, aPL);
-      aPx = aProj.NearestPoint();
-      gp_Vec aVec(aP, aPx);
-      aDB.SetXYZ(aVec.XYZ());
-    }
+  gp_Pnt aPx;
+  if (!FindPointInFace(aE, aF, aP, aDB, aPx, theContext, aProjPL)) {
+    BOPTools_AlgoTools3D::GetApproxNormalToFaceOnEdge(aE, aF, aT, aPx, aDN, theContext);
+    aProjPL.Perform(aPx);
+    aPx = aProjPL.NearestPoint();
+    gp_Vec aVec(aP, aPx);
+    aDB.SetXYZ(aVec.XYZ());
   }
 }
 
@@ -1774,41 +1826,44 @@ Standard_Real fsqrt(Standard_Real val)
 //function : FindPointInFace
 //purpose  : Find a point in the face in direction of <aDB>
 //=======================================================================
-  Standard_Boolean FindPointInFace(const TopoDS_Edge& aE,
-                                   const TopoDS_Face& aF,
-                                   const gp_Pnt& aP,
-                                   const Standard_Real aT,
-                                   gp_Dir& aDB,
-                                   gp_Pnt& aPOut,
-                                   Handle(BOPInt_Context)& theContext,
-                                   const GeomAdaptor_Surface& aGAS) 
+Standard_Boolean FindPointInFace(const TopoDS_Edge& aE,
+                                 const TopoDS_Face& aF,
+                                 const gp_Pnt& aP,
+                                 gp_Dir& aDB,
+                                 gp_Pnt& aPOut,
+                                 Handle(BOPInt_Context)& theContext,
+                                 GeomAPI_ProjectPointOnSurf& aProjPL) 
 {
   Standard_Integer aNbItMax;
   Standard_Real aDt, aDtMin, aTolE, aTolF, aDist;
   Standard_Boolean bRet;
   gp_Pnt aP1;
+  BRepAdaptor_Surface aBAS;
   //
   bRet = Standard_False;
   aTolE = BRep_Tool::Tolerance(aE);
   aTolF = BRep_Tool::Tolerance(aF);
   aDt = 2*(aTolE+aTolF);
+  aBAS.Initialize(aF, Standard_False);
   //
   aDtMin=5.e-4;
   if (aDt < aDtMin) {
     Standard_Real aR;
-    GeomAbs_SurfaceType aSType=aGAS.GetType();
+    GeomAbs_SurfaceType aSType=aBAS.GetType();
     switch (aSType) {
     case GeomAbs_Cylinder:
-      aR = aGAS.Cylinder().Radius();
+      aR = aBAS.Cylinder().Radius();
       break;
-    case GeomAbs_Cone:
-      aR = aGAS.Cone().RefRadius();
+    case GeomAbs_Cone: {
+      gp_Lin aL(aBAS.Cone().Axis());
+      aR = aL.Distance(aP);
       break;
+    }
     case GeomAbs_Sphere:
-      aR = aGAS.Sphere().Radius();
+      aR = aBAS.Sphere().Radius();
       break;
     case GeomAbs_Torus:
-      aR = aGAS.Torus().MinorRadius();
+      aR = aBAS.Torus().MinorRadius();
       break;
     case GeomAbs_SurfaceOfRevolution:
       aR=1.;
@@ -1842,6 +1897,9 @@ Standard_Real fsqrt(Standard_Real val)
     aPOut = aProj.NearestPoint();
     aDist = aProj.LowerDistance();
     //
+    aProjPL.Perform(aPOut);
+    aPOut = aProjPL.NearestPoint();
+    //
     gp_Vec aV(aP, aPOut);
     aDB.SetXYZ(aV.XYZ());
   } while (aDist>Precision::Angular() && --aNbItMax);