Salome HOME
Test case bugs/N4. A fix by PKV.
[modules/geom.git] / src / GEOMAlgo / GEOMAlgo_Builder_2.cxx
index dc60d69672ac0b7f9b5145f6d9c0601d2240d061..0272529f943e46f9ef74d29141523a79582aca6c 100755 (executable)
@@ -1,28 +1,27 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
 //
-//  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License.
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 // File:        GEOMAlgo_Builder_2.cxx
-// Created:     
-// Author:      Peter KURNEV 
-//
+// Author:      Peter KURNEV
+
 #include <GEOMAlgo_Builder.hxx>
 
 #include <TColStd_IndexedMapOfInteger.hxx>
@@ -69,6 +68,8 @@
 #include <BOPTools_CArray1OfESInterference.hxx>
 
 #include <NMTDS_ShapesDataStructure.hxx>
+#include <NMTDS_InterfPool.hxx>
+
 #include <NMTTools_PaveFiller.hxx>
 #include <NMTTools_ListOfCoupleOfShape.hxx>
 #include <NMTTools_Tools.hxx>
 #include <GEOMAlgo_Tools3D.hxx>
 #include <GEOMAlgo_WireEdgeSet.hxx>
 #include <GEOMAlgo_BuilderFace.hxx>
-#include <NMTDS_InterfPool.hxx>
+
+#include <GEOMAlgo_ShapeSet.hxx>
+//
+#include <NMTDS_BoxBndTree.hxx>
+#include <NCollection_UBTreeFiller.hxx>
+#include <Bnd_Box.hxx>
+#include <BRepBndLib.hxx>
+#include <TopTools_DataMapOfIntegerShape.hxx>
+#include <TColStd_ListOfInteger.hxx>
+#include <TColStd_ListIteratorOfListOfInteger.hxx>
+#include <TopTools_DataMapOfShapeInteger.hxx>
 
 static
   void UpdateCandidates(const Standard_Integer ,
                         const Standard_Integer ,
                         NMTTools_IndexedDataMapOfIndexedMapOfInteger& );
 
+static
+  Standard_Boolean IsClosed(const TopoDS_Edge& ,
+                           const TopoDS_Face&,
+                           Standard_Boolean&  );
+
 //=======================================================================
 //function : FillImagesFaces
-//purpose  : 
+//purpose  :
 //=======================================================================
-  void GEOMAlgo_Builder::FillImagesFaces()
+void GEOMAlgo_Builder::FillImagesFaces()
 {
   myErrorStatus=0;
   //
@@ -104,11 +120,12 @@ static
   FillImagesFaces1();
   FillInternalVertices();
 }
+
 //=======================================================================
 // function: FillIn2DParts
-// purpose: 
+// purpose:
 //=======================================================================
-  void GEOMAlgo_Builder::FillIn2DParts()
+void GEOMAlgo_Builder::FillIn2DParts()
 {
   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
   NMTTools_PaveFiller* pPF=myPaveFiller;
@@ -141,20 +158,16 @@ static
     aLSpIn.Clear();
     //
     // 1. In Parts
-    for (j=1; j<=aNbCBP; ++j) {
-      NMTTools_ListOfCommonBlock& aLCB=aCBP(j);
-      aItCB.Initialize(aLCB);
-      for (; aItCB.More(); aItCB.Next()) {
-        NMTTools_CommonBlock& aCB=aItCB.Value();
-        if (aCB.IsPaveBlockOnFace(nF)) {
-          const BOPTools_PaveBlock& aPB1=aCB.PaveBlock1();
-          nSpIn=aPB1.Edge();
-          const TopoDS_Shape& aSpIn=aDS.Shape(nSpIn);
-          if (aMFence.Add(aSpIn)){
-            aLSpIn.Append(aSpIn);
-          }
-        }
-      }
+    BOPTools_ListOfPaveBlock aLPBIn;
+    //
+    pPF->RealSplitsInFace(nF, aLPBIn);
+    //
+    aItPB.Initialize(aLPBIn);
+    for (; aItPB.More(); aItPB.Next()) {
+      const BOPTools_PaveBlock& aPB1=aItPB.Value();
+      nSpIn=aPB1.Edge();
+      const TopoDS_Shape& aSpIn=aDS.Shape(nSpIn);
+      aLSpIn.Append(aSpIn);
     }
     //
     // 2. Section Parts
@@ -188,19 +201,21 @@ static
     }
   }//for (nF=1; nF<=aNbS; ++nF) {
 }
+
 //=======================================================================
 // function: BuildSplitFaces
-// purpose: 
+// purpose:
 //=======================================================================
-  void GEOMAlgo_Builder::BuildSplitFaces()
+void GEOMAlgo_Builder::BuildSplitFaces()
 {
   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
   NMTTools_PaveFiller* pPF=myPaveFiller;
   NMTDS_InterfPool* pIP=pPF->IP();
   BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
-  IntTools_Context& aCtx= pPF->ChangeContext();
+  const Handle(IntTools_Context)& aCtx= pPF->Context();
   //
   Standard_Boolean bToReverse, bIsClosed, bIsDegenerated;
+  Standard_Boolean bFlagClosed;
   Standard_Integer i, aNb, aNbF, nF;
   TopTools_MapOfShape aMFence;
   TColStd_IndexedMapOfInteger aMFP;
@@ -253,6 +268,7 @@ static
         aNbSE=aLSE.Extent();
         if (aNbSE) {
           aMFP.Add(i);
+          break;
         }
       }
     }
@@ -271,7 +287,7 @@ static
     //
     aMFence.Clear();
     //
-    // 2.1. Fill WES 
+    // 2.1. Fill WES
     GEOMAlgo_WireEdgeSet aWES;
     aWES.SetFace(aFF);
     //
@@ -296,7 +312,7 @@ static
       }
       //
       bIsDegenerated=BRep_Tool::Degenerated(aE);
-      bIsClosed=BRep_Tool::IsClosed(aE, aF);
+      bIsClosed=IsClosed(aE, aF, bFlagClosed);
       //
       const TopTools_ListOfShape& aLIE=myImages.Image(aE);
       aIt.Initialize(aLIE);
@@ -330,7 +346,16 @@ static
             aWES.AddStartElement(aSp);
           }
           continue;
-        }// if (aMFence.Add(aSp))
+        }//  if (bIsClosed){
+       //
+       //modified by NIZNHY-PKV Wed Nov 28 13:50:34 2012f
+       if (!bIsClosed && bFlagClosed) {
+         if (!BRep_Tool::IsClosed(aSp, aF)){
+           BOPTools_Tools3D::DoSplitSEAMOnFace(aSp, aF);
+         }
+       }
+       //modified by NIZNHY-PKV Wed Nov 28 13:50:36 2012t
+       //
         //
         aSp.Orientation(anOriE);
         bToReverse=BOPTools_Tools3D::IsSplitToReverse1(aSp, aE, aCtx);
@@ -357,69 +382,60 @@ static
     }
     //
     // 2.2. Build images Faces
-    GEOMAlgo_BuilderFace aBF;
+    TopTools_ListOfShape aLFR;
+    GEOMAlgo_ShapeSet aS1, aS2;
     //
-    aBF.SetFace(aFF);
-    aBF.SetContext(aCtx);
     const TopTools_ListOfShape& aSE=aWES.StartElements();
-    //
-    //DEB f
-    /*
-    {
-      TopoDS_Compound aCx;
-      BRep_Builder aBBx;
-      TopTools_ListIteratorOfListOfShape aItx;
-      //
-      aBBx.MakeCompound(aCx);
-      aBBx.Add(aCx, aFF);
-      aItx.Initialize(aSE);
-      for (; aItx.More(); aItx.Next()) {
-        TopoDS_Shape& aEx=aItx.Value();
-        aBBx.Add(aCx, aEx);
-      }
-      int a=0;
+    aS1.Add(aSE);
+    aS2.Add(aFF, TopAbs_EDGE);
+    if (aS1.IsEqual(aS2)) {
+      aLFR.Append(aF);
     }
-    */
-    //DEB t
-    //
-    aBF.SetShapes(aSE);
-    //
-    aBF.Perform();
-    //
-    const TopTools_ListOfShape& aLF=aBF.Areas();
-    //
-    TopTools_ListOfShape aLFR;
-    //
-    aIt.Initialize(aLF);
-    for (; aIt.More(); aIt.Next()) {
-      TopoDS_Shape& aFR=aIt.Value();
-      if (anOriF==TopAbs_REVERSED) {
-        aFR.Orientation(TopAbs_REVERSED);
+    else {
+      GEOMAlgo_BuilderFace aBF;
+      //
+      aBF.SetFace(aFF);
+      aBF.SetContext(aCtx);
+      aBF.SetShapes(aSE);
+      // <-DEB
+      aBF.Perform();
+      //
+      const TopTools_ListOfShape& aLF=aBF.Areas();
+      aIt.Initialize(aLF);
+      for (; aIt.More(); aIt.Next()) {
+        TopoDS_Shape& aFR=aIt.Value();
+        if (anOriF==TopAbs_REVERSED) {
+          aFR.Orientation(TopAbs_REVERSED);
+        }
+        aLFR.Append(aFR);
       }
-      aLFR.Append(aFR);
     }
     //
     // 2.3. Collect draft images Faces
     mySplitFaces.Bind(aF, aLFR);
   }//for (i=1; i<=aNbF; ++i)
 }
+
 //=======================================================================
 // function: FillSameDomainFaces
-// purpose: 
+// purpose:
 //=======================================================================
-  void GEOMAlgo_Builder::FillSameDomainFaces()
+void GEOMAlgo_Builder::FillSameDomainFaces()
 {
+  Standard_Boolean bIsSDF, bHasImage1, bHasImage2, bForward;
+  Standard_Integer i, j, aNbFF, nF1, nF2, aNbPBInOn, aNbC, aNbSE;
+  Standard_Integer aNbF1, aNbF2, i2s, aNbSD;
+  TopTools_MapOfShape aMFence;
+  TopTools_ListOfShape aLX1, aLX2;
+  TopTools_ListIteratorOfListOfShape aItF1, aItF2;
+  NMTTools_ListOfCoupleOfShape aLCS;
+  //
   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
   NMTTools_PaveFiller* pPF=myPaveFiller;
   NMTDS_InterfPool* pIP=pPF->IP();
   BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
-  IntTools_Context& aCtx= pPF->ChangeContext();
+  const Handle(IntTools_Context)& aCtx= pPF->Context();
   //
-  Standard_Boolean bIsSDF;
-  Standard_Integer i, j, aNbFF, nF1, nF2, aNbPBInOn, aNbC, aNbSE;
-  TopTools_MapOfShape aMFence;
-  TopTools_ListIteratorOfListOfShape aItF1, aItF2;
-  NMTTools_ListOfCoupleOfShape aLCS;   
   //
   //mySameDomainShapes.Clear();
   //
@@ -457,20 +473,78 @@ static
     // the faces are suspected to be SDF.
     // Try to find SDF among images of nF1, nF2
     aMFence.Clear();
-    const TopTools_ListOfShape& aLF1=mySplitFaces.Image(aF1);
-    const TopTools_ListOfShape& aLF2=mySplitFaces.Image(aF2);
     //
+    //--------------------------------------------------------
+    bHasImage1=mySplitFaces.HasImage(aF1);
+    bHasImage2=mySplitFaces.HasImage(aF2);
+    //
+    aLX1.Clear();
+    if (!bHasImage1) {
+      aLX1.Append(aF1);
+    }
+    //
+    aLX2.Clear();
+    if (!bHasImage2) {
+      aLX2.Append(aF2);
+    }
+    //
+    const TopTools_ListOfShape& aLF1r=(bHasImage1)? mySplitFaces.Image(aF1) : aLX1;
+    const TopTools_ListOfShape& aLF2r=(bHasImage2)? mySplitFaces.Image(aF2) : aLX2;
+    //
+    TopTools_DataMapOfIntegerShape aMIS;
+    TColStd_ListIteratorOfListOfInteger aItLI;
+    NMTDS_BoxBndTreeSelector aSelector;
+    NMTDS_BoxBndTree aBBTree;
+    NCollection_UBTreeFiller <Standard_Integer, Bnd_Box> aTreeFiller(aBBTree);
+    //
+    aNbF1=aLF1r.Extent();
+    aNbF2=aLF2r.Extent();
+    bForward=(aNbF1<aNbF2);
+    //
+    const TopTools_ListOfShape& aLF1=bForward ? aLF1r : aLF2r;
+    const TopTools_ListOfShape& aLF2=bForward ? aLF2r : aLF1r;
+    //
+    // 1. aTreeFiller
+    aItF2.Initialize(aLF2);
+    for (i2s=1; aItF2.More(); aItF2.Next(), ++i2s) {
+      Bnd_Box aBoxF2s;
+      //
+      const TopoDS_Face& aF2s=*((TopoDS_Face*)(&aItF2.Value()));
+      //
+      BRepBndLib::Add(aF2s, aBoxF2s);
+      //
+      aMIS.Bind(i2s, aF2s);
+      //
+      aTreeFiller.Add(i2s, aBoxF2s);
+    }//for (i2s=1; aItF2.More(); aItF2.Next(), ++i2s) {
+    //
+    aTreeFiller.Fill();
+    //
+    // 2.
     aItF1.Initialize(aLF1);
-    for (; aItF1.More(); aItF1.Next()) {
-      const TopoDS_Face& aF1x=TopoDS::Face(aItF1.Value());
+    for (j=1; aItF1.More(); aItF1.Next(), ++j) {
+      Bnd_Box aBoxF1x;
+      //
+      const TopoDS_Face& aF1x=*((TopoDS_Face*)(&aItF1.Value()));
+      //
+      BRepBndLib::Add(aF1x, aBoxF1x);
       //
-      aItF2.Initialize(aLF2);
-      for (; aItF2.More(); aItF2.Next()) {
-        const TopoDS_Face& aF2y=TopoDS::Face(aItF2.Value());
+      aSelector.Clear();
+      aSelector.SetBox(aBoxF1x);
+      aNbSD=aBBTree.Select(aSelector);
+      if (!aNbSD) {
+        continue;
+      }
+      //
+      const TColStd_ListOfInteger& aLI=aSelector.Indices();
+      aItLI.Initialize(aLI);
+      for (; aItLI.More(); aItLI.Next()) {
+        i2s=aItLI.Value();
+        const TopoDS_Face& aF2y=*((TopoDS_Face*)(&aMIS.Find(i2s)));
+        //
         bIsSDF=NMTTools_Tools::AreFacesSameDomain(aF1x, aF2y, aCtx);
         if (bIsSDF) {
-          if (aMFence.Contains(aF1x) ||
-              aMFence.Contains(aF2y)) {
+          if (aMFence.Contains(aF1x) || aMFence.Contains(aF2y)) {
             continue;
           }
           aMFence.Add(aF1x);
@@ -482,54 +556,111 @@ static
           aCS.SetShape2(aF2y);
           aLCS.Append(aCS);
           //
-          if (aF1x==aF1) {
-            if (!mySplitFaces.HasImage(aF1)) {
-              mySplitFaces.Bind(aF1, aF1);
+          if (bForward) {
+            if (aF1x==aF1) {
+              if (!mySplitFaces.HasImage(aF1)) {
+                mySplitFaces.Bind(aF1, aF1);
+              }
+            }
+            if (aF2y==aF2) {
+              if (!mySplitFaces.HasImage(aF2)) {
+                mySplitFaces.Bind(aF2, aF2);
+              }
             }
           }
-          if (aF2y==aF2) {
-            if (!mySplitFaces.HasImage(aF2)) {
-              mySplitFaces.Bind(aF2, aF2);
+          else {
+            if (aF1x==aF2) {
+              if (!mySplitFaces.HasImage(aF2)) {
+                mySplitFaces.Bind(aF2, aF2);
+              }
+            }
+            if (aF2y==aF1) {
+              if (!mySplitFaces.HasImage(aF1)) {
+                mySplitFaces.Bind(aF1, aF1);
+              }
             }
           }
           //
-          
-        }
-      }
-    }
+          break;
+        }//if (bIsSDF) {
+      }//for (; aItLI.More(); aItLI.Next()) {
+    }//for (; aItF1.More(); aItF1.Next()) {
   }//for (i=1; i<=aNbFF; ++i)
-  //
+  //-------------------------------------------------------------
   aNbC=aLCS.Extent();
   if (!aNbC) {
     return;
   }
   //
   // 2. Find Chains
-  NMTTools_IndexedDataMapOfShapeIndexedMapOfShape aMC;  
+  NMTTools_IndexedDataMapOfShapeIndexedMapOfShape aMC;
+  //
+  NMTTools_Tools::FindChains(aLCS, aMC);
   //
-  NMTTools_Tools::FindChains(aLCS, aMC); 
+  Standard_Boolean bIsImage;
+  Standard_Integer aIx, aIxMin, aNbMSDF, k, aNbMFj;
+  TopoDS_Shape aFOld, aFSDmin;
+  TopTools_IndexedMapOfShape aMFj;
+  TopTools_DataMapOfShapeInteger aDMSI;
+  //
+  aItF1.Initialize(myShapes);
+  for (j=1; aItF1.More(); aItF1.Next(), ++j) {
+    const TopoDS_Shape& aSj=aItF1.Value();
+    aMFj.Clear();
+    TopExp::MapShapes(aSj, TopAbs_FACE, aMFj);
+    aNbMFj=aMFj.Extent();
+    for (k=1; k<=aNbMFj; ++k) {
+      const TopoDS_Shape& aFk=aMFj(k);
+      if (!aDMSI.IsBound(aFk)) {
+       aDMSI.Bind(aFk, j);
+      }
+    }
+  }
   //
   // 3. Fill the map of SDF mySameDomainFaces
   aNbC=aMC.Extent();
   for (i=1; i<=aNbC; ++i) {
-    const TopoDS_Shape& aF=aMC.FindKey(i);
+   // const TopoDS_Shape& aF=aMC.FindKey(i);
     const TopTools_IndexedMapOfShape& aMSDF=aMC(i);
     //
-    aNbFF=aMSDF.Extent();
-    for (j=1; j<=aNbFF; ++j) {
+    aNbMSDF=aMSDF.Extent();
+    for (j=1; j<=aNbMSDF; ++j) {
+      const TopoDS_Shape& aFSD=aMSDF(j);
+      bIsImage=mySplitFaces.IsImage(aFSD);
+      aFOld=aFSD;
+      if (bIsImage) {
+       aFOld=mySplitFaces.ImageFrom(aFSD);
+      }
+      //
+      aIx=aDMSI.Find(aFOld);
+      if (j==1) {
+       aIxMin=aIx;
+       aFSDmin=aFSD;
+       continue;
+      }
+      else {
+       if (aIx<aIxMin) {
+         aIxMin=aIx;
+         aFSDmin=aFSD;
+       }
+      }
+    }
+    //
+    for (j=1; j<=aNbMSDF; ++j) {
       const TopoDS_Shape& aFSD=aMSDF(j);
-      mySameDomainShapes.Add(aFSD, aF);
+      mySameDomainShapes.Add(aFSD, aFSDmin);
     }
   }
   //
 }
+
 //=======================================================================
 // function: FillImagesFaces1
-// purpose: 
+// purpose:
 //=======================================================================
-  void GEOMAlgo_Builder::FillImagesFaces1()
+void GEOMAlgo_Builder::FillImagesFaces1()
 {
-  Standard_Integer i, aNb, iSense;
+  Standard_Integer i, aNb, iSense, aNbLFx;
   TopoDS_Face aF, aFSp, aFSD;
   TopTools_ListOfShape aLFx;
   TopTools_ListIteratorOfListOfShape aIt;
@@ -547,19 +678,19 @@ static
       continue;
     }
     //
-    aF=TopoDS::Face(aS);
+    aF=*((TopoDS_Face*)&aS);
     //
     aLFx.Clear();
     const TopTools_ListOfShape& aLF=mySplitFaces.Image(aF);
     aIt.Initialize(aLF);
     for (; aIt.More(); aIt.Next()) {
-      aFSp=TopoDS::Face(aIt.Value());
+      aFSp=*((TopoDS_Face*)(&aIt.Value()));
       if (!mySameDomainShapes.Contains(aFSp)) {
         aLFx.Append(aFSp);
       }
       else {
         const TopoDS_Shape& aSx=mySameDomainShapes.FindFromKey(aFSp);
-        aFSD=TopoDS::Face(aSx);
+        aFSD=*((TopoDS_Face*)(&aSx));
         iSense=GEOMAlgo_Tools3D::Sense(aFSp, aFSD);
         if (iSense<0) {
           aFSD.Reverse();
@@ -567,29 +698,37 @@ static
         aLFx.Append(aFSD);
       }
     }
-    if (!myImages.HasImage(aF)) {//XX
+    //
+    if (!myImages.HasImage(aF)) {
+      aNbLFx=aLFx.Extent();
+      if (aNbLFx==1) {
+        const TopoDS_Shape& aFx=aLFx.First();
+        if (aF.IsSame(aFx)) {
+          continue;
+        }
+      }
       myImages.Bind(aF, aLFx);
     }
   }
 }
+
 //=======================================================================
 // function: FillInternalVertices
-// purpose: 
+// purpose:
 //=======================================================================
-  void GEOMAlgo_Builder::FillInternalVertices()
+void GEOMAlgo_Builder::FillInternalVertices()
 {
   const NMTDS_ShapesDataStructure& aDS=*myPaveFiller->DS();
   NMTTools_PaveFiller* pPF=myPaveFiller;
   NMTDS_InterfPool* pIP=pPF->IP();
-  IntTools_Context& aCtx= pPF->ChangeContext();
+  const Handle(IntTools_Context)& aCtx= pPF->Context();
   //
-  BOPTools_CArray1OfSSInterference& aFFs=pIP->SSInterferences();
   BOPTools_CArray1OfVSInterference& aVFs=pIP->VSInterferences();
   BOPTools_CArray1OfESInterference& aEFs=pIP->ESInterferences();
   const NMTTools_IndexedDataMapOfIndexedMapOfInteger& aMAV=pPF->AloneVertices();
   //
-  Standard_Boolean bHasImage; 
-  Standard_Integer i, j, nF, aNbS, nV, nVSD, n1, n2, iFlag; 
+  Standard_Boolean bHasImage;
+  Standard_Integer i, j, nF, aNbS, nV, nVSD, n1, n2, iFlag;
   Standard_Integer aNbVFs, aNbAVF, aNbEFs, aNbVC, aNbE, aNbV;
   Standard_Real aU1, aU2, aTol;
   NMTTools_IndexedDataMapOfIndexedMapOfInteger aMFMV;
@@ -659,7 +798,7 @@ static
       const TColStd_IndexedMapOfInteger& aMAVF=aMAV.FindFromKey(nF);
       aNbAVF=aMAVF.Extent();
       for (j=1; j<=aNbAVF; ++j) {
-        nV=aMAVF(j); 
+        nV=aMAVF(j);
         nVSD=pPF->FindSDVertex(nV);
         if (nVSD) {
           nV=nVSD;
@@ -709,7 +848,7 @@ static
         TopExp::MapShapesAndAncestors(aFx, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
       }
     }
-    else {      
+    else {
       Standard_Boolean bFaceToProcess;
       //
       TopExp::MapShapesAndAncestors(aF, TopAbs_VERTEX, TopAbs_EDGE, aMVE);
@@ -755,27 +894,27 @@ static
           for (; aIt.More(); aIt.Next()) {
             TopoDS_Face aFx=TopoDS::Face(aIt.Value());
             // update classifier
-            IntTools_FClass2d& aClsf=aCtx.FClass2d(aFx);
+            IntTools_FClass2d& aClsf=aCtx->FClass2d(aFx);
             aClsf.Init(aFx, aTol);
             //
-            iFlag=aCtx.ComputeVS (aV, aFx, aU1, aU2);
+            iFlag=aCtx->ComputeVS (aV, aFx, aU1, aU2);
             if (!iFlag) {
               aBB.Add(aFx, aV);
               break;
             }
           }
         }
-        else {  
+        else {
           const TopoDS_Face& aFx=TopoDS::Face(aF);
           // update classifier
-          IntTools_FClass2d& aClsf=aCtx.FClass2d(aFx);
+          IntTools_FClass2d& aClsf=aCtx->FClass2d(aFx);
           aClsf.Init(aFx, aTol);
           //
-          iFlag=aCtx.ComputeVS (aV, aFx, aU1, aU2);
+          iFlag=aCtx->ComputeVS (aV, aFx, aU1, aU2);
           if (!iFlag) {
             TopoDS_Face aFz;
             //
-            GEOMAlgo_Tools3D::CopyFace(aFx, aFz); 
+            GEOMAlgo_Tools3D::CopyFace(aFx, aFz);
             aBB.Add(aFz, aV);
             myImages.Bind(aF, aFz);
           }
@@ -784,9 +923,10 @@ static
     }// if (aNbV) {
   }// for (nF=1; nF<=aNb; ++nF) {
 }
+
 //=======================================================================
 // function: UpdateCandidates
-// purpose: 
+// purpose:
 //=======================================================================
 void UpdateCandidates(const Standard_Integer theNF,
                       const Standard_Integer theNV,
@@ -802,3 +942,58 @@ void UpdateCandidates(const Standard_Integer theNF,
     theMFMV.Add(theNF, aMV);
   }
 }
+
+//=======================================================================
+//function : IsClosed
+//purpose  :
+//=======================================================================
+Standard_Boolean IsClosed(const TopoDS_Edge& aE,
+                         const TopoDS_Face& aF,
+                         Standard_Boolean& bFlag)
+{
+  Standard_Boolean bRet;
+  //
+  bRet=BRep_Tool::IsClosed(aE, aF);
+  bFlag=bRet;
+  if (bRet) {
+    Standard_Integer iCnt;
+    TopoDS_Shape aE1;
+    //
+    bRet=!bRet;
+    iCnt=0;
+    TopExp_Explorer aExp(aF, TopAbs_EDGE);
+    for (; aExp.More(); aExp.Next()) {
+      const TopoDS_Shape& aEx=aExp.Current();
+      //
+      if (aEx.IsSame(aE)) {
+       ++iCnt;
+       if (iCnt==1) {
+         aE1=aEx;
+       }
+       else if (iCnt==2){
+         aE1.Reverse();
+         bRet=(aE1==aEx);
+         break;
+       }
+      }
+    }
+  }
+  return bRet;
+}
+
+/*
+    {
+      TopoDS_Compound aCx;
+      BRep_Builder aBBx;
+      TopTools_ListIteratorOfListOfShape aItx;
+      //
+      aBBx.MakeCompound(aCx);
+      aBBx.Add(aCx, aFF);
+      aItx.Initialize(aSE);
+      for (; aItx.More(); aItx.Next()) {
+        TopoDS_Shape& aEx=aItx.Value();
+        aBBx.Add(aCx, aEx);
+      }
+      int a=0;
+    }
+    */