0024002: Overall code and build procedure refactoring -- automatic
[occt.git] / src / BOPAlgo / BOPAlgo_BOP.cxx
index 47a392c..234bfd8 100644 (file)
 // Alternatively, this file may be used under the terms of Open CASCADE
 // commercial license or contractual agreement.
 
-#include <BOPAlgo_BOP.ixx>
-
-#include <TopAbs_ShapeEnum.hxx>
-
-#include <TopoDS_Compound.hxx>
-#include <TopoDS_Iterator.hxx>
-#include <TopoDS_Edge.hxx>
-#include <BRep_Builder.hxx>
-#include <TopExp_Explorer.hxx>
 
+#include <BOPAlgo_BOP.hxx>
+#include <BOPAlgo_BuilderSolid.hxx>
+#include <BOPAlgo_PaveFiller.hxx>
+#include <BOPCol_DataMapOfShapeShape.hxx>
+#include <BOPCol_IndexedDataMapOfShapeListOfShape.hxx>
+#include <BOPCol_IndexedMapOfShape.hxx>
 #include <BOPCol_ListOfShape.hxx>
 #include <BOPCol_MapOfShape.hxx>
-#include <BOPCol_IndexedMapOfShape.hxx>
-#include <BOPCol_IndexedDataMapOfShapeListOfShape.hxx>
-#include <BOPCol_DataMapOfShapeShape.hxx>
-
 #include <BOPDS_DS.hxx>
-
 #include <BOPTools.hxx>
 #include <BOPTools_AlgoTools.hxx>
 #include <BOPTools_AlgoTools3D.hxx>
-#include <BOPTools_AlgoTools.hxx>
-
-#include <BOPAlgo_BuilderSolid.hxx>
-
+#include <BOPTools_Set.hxx>
+#include <BOPTools_SetMapHasher.hxx>
+#include <BRep_Builder.hxx>
 #include <BRep_Tool.hxx>
+#include <NCollection_DataMap.hxx>
+#include <NCollection_IncAllocator.hxx>
+#include <TopAbs_ShapeEnum.hxx>
+#include <TopExp_Explorer.hxx>
+#include <TopoDS_Compound.hxx>
+#include <TopoDS_Edge.hxx>
+#include <TopoDS_Iterator.hxx>
+#include <TopoDS_Shape.hxx>
+#include <TopTools_ListIteratorOfListOfShape.hxx>
 
+//
+typedef NCollection_DataMap  
+  <BOPTools_Set, 
+  TopoDS_Shape, 
+  BOPTools_SetMapHasher> BOPTools_DataMapOfSetShape; 
+//
+typedef BOPTools_DataMapOfSetShape::Iterator 
+  BOPTools_DataMapIteratorOfDataMapOfSetShape; 
 
 static
   TopAbs_ShapeEnum TypeToExplore(const Standard_Integer theDim);
@@ -117,6 +125,21 @@ void BOPAlgo_BOP::AddTool(const TopoDS_Shape& theShape)
   }
 }
 //=======================================================================
+//function : SetTools
+//purpose  : 
+//=======================================================================
+void BOPAlgo_BOP::SetTools(const BOPCol_ListOfShape& theShapes)
+{
+  BOPCol_ListIteratorOfListOfShape aIt;
+  //
+  myTools.Clear();
+  aIt.Initialize(theShapes);
+  for (; aIt.More(); aIt.Next()) {
+    const TopoDS_Shape& aS = aIt.Value();
+    AddTool(aS);
+  }
+}
+//=======================================================================
 //function : CheckData
 //purpose  : 
 //=======================================================================
@@ -128,15 +151,26 @@ void BOPAlgo_BOP::CheckData()
   //
   myErrorStatus=0;
   //
+  if (!(myOperation==BOPAlgo_COMMON ||
+        myOperation==BOPAlgo_FUSE || 
+        myOperation==BOPAlgo_CUT|| 
+        myOperation==BOPAlgo_CUT21)) {
+    // non-licit operation
+    myErrorStatus=14;
+    return;
+  }
+  //
   aNbArgs=myArguments.Extent();
   if (!aNbArgs) {
-    myErrorStatus=100; // invalid number of Arguments
+    // invalid number of Arguments
+    myErrorStatus=100; 
     return;
   }
   //
   aNbTools=myTools.Extent();
-  if (!aNbTools) {
-    myErrorStatus=100; // invalid number of Tools
+  if (!aNbTools) { 
+    // invalid number of Tools
+    myErrorStatus=100;
     return;
   }
   //
@@ -150,6 +184,9 @@ void BOPAlgo_BOP::CheckData()
     return;
   }
   //
+  // myDims
+  myDims[0]=-1;
+  myDims[1]=-1;
   for (i=0; i<2; ++i) {
     const BOPCol_ListOfShape& aLS=(!i)? myArguments : myTools;
     aItLS.Initialize(aLS);
@@ -162,7 +199,8 @@ void BOPAlgo_BOP::CheckData()
       //
       iDim=BOPTools_AlgoTools::Dimension(aS);
       if (iDim<0) {
-        myErrorStatus=13; // non-homogenious argument
+        // non-homogenious argument
+        myErrorStatus=13; 
         return;
       }
       //
@@ -171,28 +209,27 @@ void BOPAlgo_BOP::CheckData()
         continue;
       }
       //
-      if (iDim!=myDims[i]) {
-        myErrorStatus=13; // non-homogenious argument
+      if (iDim!=myDims[i]) { 
+        // non-homogenious argument
+        myErrorStatus=13;
         return;
       }
     }
   }
   //
-  if (myOperation==BOPAlgo_UNKNOWN) {
-    myErrorStatus=14; // non-licit operation
-    return;
-  }
-  else if (myDims[0]<myDims[1]) {
+  if (myDims[0]<myDims[1]) {
     if (myOperation==BOPAlgo_FUSE ||
-        myOperation==BOPAlgo_CUT21) {
-      myErrorStatus=14; // non-licit operation for the arguments
+        myOperation==BOPAlgo_CUT21) { 
+      // non-licit operation for the arguments
+      myErrorStatus=14;
       return;
     }
   }
   else if (myDims[0]>myDims[1]) {
     if (myOperation==BOPAlgo_FUSE ||
         myOperation==BOPAlgo_CUT) {
-      myErrorStatus=14; // non-licit operation for the arguments
+      // non-licit operation for the arguments
+      myErrorStatus=14; 
       return;
     }
   }
@@ -246,8 +283,6 @@ void BOPAlgo_BOP::Prepare()
       }
         break;
         //
-      case BOPAlgo_COMMON:
-      case BOPAlgo_SECTION:
       default:
         break;
       }
@@ -326,6 +361,9 @@ void BOPAlgo_BOP::Perform()
   //
   pPF=new BOPAlgo_PaveFiller(aAllocator);
   pPF->SetArguments(aLS);
+  pPF->SetRunParallel(myRunParallel);
+  pPF->SetProgressIndicator(myProgressIndicator);
+  pPF->SetFuzzyValue(myFuzzyValue);
   //
   pPF->Perform();
   //
@@ -333,10 +371,10 @@ void BOPAlgo_BOP::Perform()
   PerformInternal(*pPF);
 }
 //=======================================================================
-//function : PerformInternal
+//function : PerformInternal1
 //purpose  : 
 //=======================================================================
-void BOPAlgo_BOP::PerformInternal(const BOPAlgo_PaveFiller& theFiller)
+void BOPAlgo_BOP::PerformInternal1(const BOPAlgo_PaveFiller& theFiller)
 {
   myErrorStatus=0;
   myWarningStatus=0;
@@ -376,31 +414,23 @@ void BOPAlgo_BOP::PerformInternal(const BOPAlgo_PaveFiller& theFiller)
   if (myErrorStatus) {
     return;
   }
-  
+  //
   BuildResult(TopAbs_EDGE);
   if (myErrorStatus) {
     return;
   }
-  //-------------------------------- SECTION 
-  if (myOperation==BOPAlgo_SECTION) {
-    BuildSection();
-    PrepareHistory();
-    PostTreat();
-    return;
-  }
-  //-------------------------------- 
   //
   // 3.3 Wires
   FillImagesContainers(TopAbs_WIRE);
   if (myErrorStatus) {
     return;
   }
-  
+  //
   BuildResult(TopAbs_WIRE);
   if (myErrorStatus) {
     return;
   }
-  
+  //
   // 3.4 Faces
   FillImagesFaces();
   if (myErrorStatus) {
@@ -411,43 +441,46 @@ void BOPAlgo_BOP::PerformInternal(const BOPAlgo_PaveFiller& theFiller)
   if (myErrorStatus) {
     return;
   }
+  //
   // 3.5 Shells
-  
   FillImagesContainers(TopAbs_SHELL);
   if (myErrorStatus) {
     return;
   }
-  
+  //
   BuildResult(TopAbs_SHELL);
   if (myErrorStatus) {
     return;
   }
+  //
   // 3.6 Solids
   FillImagesSolids();
   if (myErrorStatus) {
     return;
   }
-  
+  //
   BuildResult(TopAbs_SOLID);
   if (myErrorStatus) {
     return;
   }
+  //
   // 3.7 CompSolids
   FillImagesContainers(TopAbs_COMPSOLID);
   if (myErrorStatus) {
     return;
   }
-  
+  //
   BuildResult(TopAbs_COMPSOLID);
   if (myErrorStatus) {
     return;
   }
+  //
   // 3.8 Compounds
   FillImagesCompounds();
   if (myErrorStatus) {
     return;
   }
-  
+  //
   BuildResult(TopAbs_COMPOUND);
   if (myErrorStatus) {
     return;
@@ -479,7 +512,10 @@ void BOPAlgo_BOP::BuildRC()
   BRep_Builder aBB;
   TopExp_Explorer aExp;
   BOPCol_DataMapOfShapeShape aDMSSA;
-  BOPCol_ListIteratorOfListOfShape aItLS, aItIm;
+  BOPCol_ListIteratorOfListOfShape aItLS, aItIm; 
+  Standard_Boolean bHasInterf;
+  Standard_Integer iX;
+  BOPTools_DataMapOfSetShape aDMSTS;
   //
   myErrorStatus=0;
   //
@@ -531,6 +567,17 @@ void BOPAlgo_BOP::BuildRC()
       for (; aExp.More(); aExp.Next()) {
         const TopoDS_Shape aSIm=aExp.Current();
         aDMSSA.Bind(aSIm, aSIm);
+        if (aTmin==TopAbs_SOLID) {
+          iX=myDS->Index(aSIm);
+          bHasInterf=myDS->HasInterf(iX);
+          if (!bHasInterf) {
+            BOPTools_Set aST;
+            //
+            aST.Add(aSIm, TopAbs_FACE);
+            //
+            aDMSTS.Bind(aST, aSIm);
+          }
+        }
       }
     }
   } //for (; aItLS.More(); aItLS.Next())
@@ -569,7 +616,7 @@ void BOPAlgo_BOP::BuildRC()
           }
         }
       }
-    }
+    }// if (myImages.IsBound(aST)){
     else {
       aExp.Init(aST, aTmin);
       for (; aExp.More(); aExp.Next()) {
@@ -587,10 +634,32 @@ void BOPAlgo_BOP::BuildRC()
             const TopoDS_Shape& aSImA=aDMSSA.Find(aSIm);
             aBB.Add(aC, aSImA);
           }
+          else {
+             if (aTmin==TopAbs_SOLID) {
+               BOPTools_Set aST;
+               //
+               aST.Add(aSIm, TopAbs_FACE);
+               //
+               if (aDMSTS.IsBound(aST)) {
+                 const TopoDS_Shape& aSImA=aDMSTS.Find(aST);
+                 aBB.Add(aC, aSImA);
+               }
+             }
+          }
         }
         else {// ie cut or cut21
           if (!bIsBound) {
-            aBB.Add(aC, aSIm);
+            if (aTmin==TopAbs_SOLID) {
+              BOPTools_Set aST;
+              //
+              aST.Add(aSIm, TopAbs_FACE);
+              //
+              bIsBound=aDMSTS.IsBound(aST); 
+            }
+            //
+            if (!bIsBound) {
+              aBB.Add(aC, aSIm);
+            }
           }
         }
       }
@@ -739,7 +808,8 @@ void BOPAlgo_BOP::BuildShape()
 //=======================================================================
 void BOPAlgo_BOP::BuildSolid()
 {
-  Standard_Integer i, aNbF, aNbSx, iX, iErr;
+  Standard_Boolean bHasInterf, bHasSharedFaces;
+  Standard_Integer i, aNbF, aNbSx, iX, iErr, aNbZ;
   TopAbs_Orientation aOr, aOr1;
   TopoDS_Iterator aIt;
   TopoDS_Shape aRC;
@@ -749,13 +819,73 @@ void BOPAlgo_BOP::BuildSolid()
   BOPCol_IndexedDataMapOfShapeListOfShape aMFS, aMEF;
   BOPCol_ListIteratorOfListOfShape aItLS;
   BOPCol_ListOfShape aSFS;
-  BOPAlgo_BuilderSolid aSB;
+  BOPAlgo_BuilderSolid aSB; 
+  BOPCol_MapOfShape aMSA, aMZ;
+  BOPTools_DataMapOfSetShape aDMSTS;
+  BOPTools_DataMapIteratorOfDataMapOfSetShape aItDMSTS;
   //
   myErrorStatus=0;
   //
+  // Map of of Solids of Arguments
+  for (i=0; i<2; ++i) {
+    const BOPCol_ListOfShape& aLSA=(i) ? myArguments : myTools;
+    aItLS.Initialize(aLSA);
+    for (; aItLS.More(); aItLS.Next()) {
+      const TopoDS_Shape& aSA=aItLS.Value();
+      aExp.Init(aSA, TopAbs_SOLID);
+      for (; aExp.More(); aExp.Next()) {
+        const TopoDS_Shape& aZA=aExp.Current();
+        aMSA.Add(aZA);
+        //
+        BOPTools::MapShapesAndAncestors(aZA, 
+                                        TopAbs_FACE, 
+                                        TopAbs_SOLID, 
+                                        aMFS);
+      }
+    }
+  }
+  //
+  aNbF=aMFS.Extent();
+  for (i=1; i<aNbF; ++i) {
+    //const TopoDS_Shape& aFA=aMFZA.FindKey(i);
+    const BOPCol_ListOfShape& aLZA=aMFS(i);
+    aNbZ=aLZA.Extent();
+    if (aNbZ > 1) {
+      aItLS.Initialize(aLZA);
+      for(; aItLS.More(); aItLS.Next()) {
+        const TopoDS_Shape& aZA=aItLS.Value();
+        aMZ.Add(aZA);
+      }
+    }
+  }
+  //
+  aMFS.Clear();
+  //
   aIt.Initialize(myRC);
   for (; aIt.More(); aIt.Next()) {
     const TopoDS_Shape& aSx=aIt.Value(); 
+    if (aMSA.Contains(aSx)) {
+      iX=myDS->Index(aSx);
+      bHasInterf=myDS->HasInterf(iX);
+      bHasSharedFaces=aMZ.Contains(aSx);
+      //
+      if (!bHasInterf && !bHasSharedFaces) {
+        // It means that the solid aSx will be added
+        // to the result as is. 
+        // The solid aSx will not participate 
+        // in creation of a new solid(s).
+        BOPTools_Set aST;
+        //
+        aST.Add(aSx, TopAbs_FACE);
+        //
+        if (!aDMSTS.IsBound(aST)) {
+          aDMSTS.Bind(aST, aSx);
+        }
+        
+        continue; 
+      }
+    } 
+    //
     aExp.Init(aSx, TopAbs_FACE);
     for (; aExp.More(); aExp.Next()) {
       const TopoDS_Shape& aFx=aExp.Current();
@@ -783,7 +913,7 @@ void BOPAlgo_BOP::BuildSolid()
         }
       }
     }
-  }
+  } // for (; aIt.More(); aIt.Next()) {
   //faces that will be added in the end;
   BOPCol_ListOfShape aLF, aLFx; 
   // SFS
@@ -845,10 +975,17 @@ void BOPAlgo_BOP::BuildSolid()
     const TopoDS_Shape& aSR=aItLS.Value();
     aBB.Add(aRC, aSR);
   }
+  //
+  aItDMSTS.Initialize(aDMSTS);
+  for (; aItDMSTS.More(); aItDMSTS.Next()) {
+    const TopoDS_Shape& aSx=aItDMSTS.Value();
+    aBB.Add(aRC, aSx);
+  }
+  //
   myShape=aRC;
 }
 //=======================================================================
-//function : IsBoundImages
+//function : IsBoundSplits
 //purpose  : 
 //=======================================================================
 Standard_Boolean BOPAlgo_BOP::IsBoundSplits