Salome HOME
fix for test cases bugs_13/N8 and imps_04/E5
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BooleanDriver.cxx
index 5dabd57a4f17d3d3a0e43056141fb6cd7e9c7af7..4d6a72f2abb8358fbea33ded131924e70bf312ed 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015  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
@@ -6,7 +6,7 @@
 // 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.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 #include <GEOMImpl_GlueDriver.hxx>
 #include <GEOM_Function.hxx>
 #include <GEOMUtils.hxx>
+#include <BlockFix_BlockFixAPI.hxx>
+#include <ShHealOper_ShapeProcess.hxx>
 
 #include <TNaming_CopyShape.hxx>
 
-#include <ShapeFix_ShapeTolerance.hxx>
-#include <ShapeFix_Shape.hxx>
-
 #include <BRep_Builder.hxx>
-#include <BRepAlgo.hxx>
 #include <BRepAlgoAPI_Common.hxx>
 #include <BRepAlgoAPI_Cut.hxx>
 #include <BRepAlgoAPI_Fuse.hxx>
 #include <BRepAlgoAPI_Section.hxx>
-#include <BRepCheck_Analyzer.hxx>
 #include <BOPAlgo_CheckerSI.hxx>
 #include <BOPDS_DS.hxx>
 
 // here check level is decreased to more appropriate value to avoid problems with performance).
 #define BOP_SELF_INTERSECTIONS_LEVEL 4
 
+/**
+ * This function performs extra edges removal.
+ *
+ * \param theShape the shape to be processed.
+ * \return the modified shape or null shape in case of failure.
+ */
+static TopoDS_Shape RemoveExtraEdges(const TopoDS_Shape &theShape)
+{
+  TopoDS_Shape aResult;
+
+  if (!theShape.IsNull()) {
+    BlockFix_BlockFixAPI aTool;
+
+    aTool.OptimumNbFaces() = 0;
+    aTool.SetShape(theShape);
+    aTool.Perform();
+    TopoDS_Shape aShape = aTool.Shape();
+
+    if (GEOMUtils::CheckShape(aShape)) {
+      aResult = aShape;
+    }
+    else {
+      TopoDS_Shape aFixed;
+      ShHealOper_ShapeProcess aHealer;
+      aHealer.Perform(aResult, aFixed);
+      if (aHealer.isDone() && GEOMUtils::CheckShape(aFixed))
+        aResult = aFixed;
+    }
+  }
+
+  return aResult;
+}
+
 //=======================================================================
 //function : GetID
 //purpose  :
@@ -91,6 +121,7 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
   GEOMImpl_IBoolean aCI (aFunction);
   Standard_Integer aType = aFunction->GetType();
   const Standard_Boolean isCheckSelfInte = aCI.GetCheckSelfIntersection();
+  const Standard_Boolean isRmExtraEdges  = aCI.GetRmExtraEdges();
 
   TopoDS_Shape aShape;
 
@@ -107,11 +138,7 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
 
       if (!aShape1.IsNull() && !aShape2.IsNull()) {
         // check arguments for Mantis issue 0021019
-        BRepCheck_Analyzer ana (aShape1, Standard_True);
-        if (!ana.IsValid())
-          StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
-        ana.Init(aShape2);
-        if (!ana.IsValid())
+        if (!GEOMUtils::CheckShape(aShape1, true) || !GEOMUtils::CheckShape(aShape2, true))
           StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
 
         if (isCheckSelfInte) {
@@ -139,8 +166,13 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
 
         aShape = performOperation (aShapeCopy1, aShapeCopy2, aType);
 
-        if (aShape.IsNull())
+        if (isRmExtraEdges) {
+          aShape = RemoveExtraEdges(aShape);
+        }
+
+        if (aShape.IsNull()) {
           return 0;
+        }
       }
     }
     break;
@@ -160,10 +192,9 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
         aShape = aRefShape->GetValue();
        
         if (!aShape.IsNull()) {
-          BRepCheck_Analyzer anAna (aShape, Standard_True);
-          if (!anAna.IsValid()) {
+          // check arguments for Mantis issue 0021019
+          if (!GEOMUtils::CheckShape(aShape, true))
             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
-          }
 
           BOPAlgo_CheckerSI aCSI;  // checker of self-interferences
 
@@ -188,11 +219,9 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
           for (i = 2; i <= nbShapes; i++) {
            aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(i));
            aShape2 = aRefShape->GetValue();
-           anAna.Init(aShape2);
-           
-           if (!anAna.IsValid()) {
+            
+            if (!GEOMUtils::CheckShape(aShape2, true))
              StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
-           }
            
             if (isCheckSelfInte) {
               BOPCol_ListOfShape aList2;
@@ -208,6 +237,10 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
             aShapeCopy.Nullify();
             TNaming_CopyShape::CopyTool(aShape2, aMapTShapes, aShapeCopy);
            aShape = performOperation (aShape, aShapeCopy, aSimpleType);
+
+            if (isRmExtraEdges) {
+              aShape = RemoveExtraEdges(aShape);
+            }
            
            if (aShape.IsNull()) {
              return 0;
@@ -225,11 +258,8 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
 
       if (!aShape.IsNull()) {
         // check arguments for Mantis issue 0021019
-        BRepCheck_Analyzer anAna (aShape, Standard_True);
-
-        if (!anAna.IsValid()) {
+        if (!GEOMUtils::CheckShape(aShape, true))
           StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
-        }
 
        BOPAlgo_CheckerSI aCSI;  // checker of self-interferences
 
@@ -260,11 +290,9 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
         for (i = 1; i <= nbShapes; i++) {
           aRefTool = Handle(GEOM_Function)::DownCast(aTools->Value(i));
           aTool = aRefTool->GetValue();
-          anAna.Init(aTool);
 
-          if (!anAna.IsValid()) {
+          if (!GEOMUtils::CheckShape(aTool, true))
             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
-          }
 
           if (isCheckSelfInte) {
             BOPCol_ListOfShape aList2;
@@ -517,91 +545,8 @@ TopoDS_Shape GEOMImpl_BooleanDriver::performOperation
   }
 
   // 08.07.2008 skl for bug 19761 from Mantis
-  BRepCheck_Analyzer ana (aShape, Standard_True);
-  ana.Init(aShape);
-  if (!ana.IsValid()) {
-    ShapeFix_ShapeTolerance aSFT;
-    aSFT.LimitTolerance(aShape, Precision::Confusion(),
-                        Precision::Confusion(), TopAbs_SHAPE);
-    Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
-    aSfs->Perform();
-    aShape = aSfs->Shape();
-    ana.Init(aShape);
-    if (!ana.IsValid())
-      Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
-  }
-
-  // BEGIN: Mantis issue 0021060: always limit tolerance of BOP result
-  // 1. Get shape parameters for comparison
-  int nbTypes [TopAbs_SHAPE];
-  {
-    for (int iType = 0; iType < TopAbs_SHAPE; ++iType)
-      nbTypes[iType] = 0;
-    nbTypes[aShape.ShapeType()]++;
-
-    TopTools_MapOfShape aMapOfShape;
-    aMapOfShape.Add(aShape);
-    TopTools_ListOfShape aListOfShape;
-    aListOfShape.Append(aShape);
-
-    TopTools_ListIteratorOfListOfShape itL (aListOfShape);
-    for (; itL.More(); itL.Next()) {
-      TopoDS_Iterator it (itL.Value());
-      for (; it.More(); it.Next()) {
-        TopoDS_Shape s = it.Value();
-        if (aMapOfShape.Add(s)) {
-          aListOfShape.Append(s);
-          nbTypes[s.ShapeType()]++;
-        }
-      }
-    }
-  }
-
-  // 2. Limit tolerance
-  TopoDS_Shape aShapeCopy;
-  TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
-  TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
-  ShapeFix_ShapeTolerance aSFT;
-  aSFT.LimitTolerance(aShapeCopy, Precision::Confusion(), Precision::Confusion(), TopAbs_SHAPE);
-  Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
-  aSfs->Perform();
-  aShapeCopy = aSfs->Shape();
-
-  // 3. Check parameters
-  ana.Init(aShapeCopy);
-  if (ana.IsValid()) {
-    int iType, nbTypesCopy [TopAbs_SHAPE];
-
-    for (iType = 0; iType < TopAbs_SHAPE; ++iType)
-      nbTypesCopy[iType] = 0;
-    nbTypesCopy[aShapeCopy.ShapeType()]++;
-
-    TopTools_MapOfShape aMapOfShape;
-    aMapOfShape.Add(aShapeCopy);
-    TopTools_ListOfShape aListOfShape;
-    aListOfShape.Append(aShapeCopy);
-
-    TopTools_ListIteratorOfListOfShape itL (aListOfShape);
-    for (; itL.More(); itL.Next()) {
-      TopoDS_Iterator it (itL.Value());
-      for (; it.More(); it.Next()) {
-        TopoDS_Shape s = it.Value();
-        if (aMapOfShape.Add(s)) {
-          aListOfShape.Append(s);
-          nbTypesCopy[s.ShapeType()]++;
-        }
-      }
-    }
-
-    bool isEqual = true;
-    for (iType = 0; iType < TopAbs_SHAPE && isEqual; ++iType) {
-      if (nbTypes[iType] != nbTypesCopy[iType])
-        isEqual = false;
-    }
-    if (isEqual)
-      aShape = aShapeCopy;
-  }
-  // END: Mantis issue 0021060
+  if ( !GEOMUtils::CheckShape(aShape, true) && !GEOMUtils::FixShapeTolerance(aShape) )
+    Standard_ConstructionError::Raise("Boolean operation aborted : non valid shape result");
 
   return aShape;
 }
@@ -621,40 +566,50 @@ GetCreationInformation(std::string&             theOperationName,
 
   GEOMImpl_IBoolean aCI (function);
   Standard_Integer aType = function->GetType();
+  Standard_Boolean isCheckSelfInte = aCI.GetCheckSelfIntersection();
 
   switch ( aType ) {
   case BOOLEAN_COMMON:
     theOperationName = "COMMON";
     AddParam( theParams, "Object 1", aCI.GetShape1() );
     AddParam( theParams, "Object 2", aCI.GetShape2() );
+    AddParam( theParams, "Check self-intersections", isCheckSelfInte );
     break;
   case BOOLEAN_CUT:
     theOperationName = "CUT";
     AddParam( theParams, "Main Object", aCI.GetShape1() );
     AddParam( theParams, "Tool Object", aCI.GetShape2() );
+    AddParam( theParams, "Check self-intersections", isCheckSelfInte );
     break;
   case BOOLEAN_FUSE:
     theOperationName = "FUSE";
     AddParam( theParams, "Object 1", aCI.GetShape1() );
     AddParam( theParams, "Object 2", aCI.GetShape2() );
+    AddParam( theParams, "Check self-intersections", isCheckSelfInte );
+    AddParam( theParams, "Remove extra edges", aCI.GetRmExtraEdges() );
     break;
   case BOOLEAN_SECTION:
     theOperationName = "SECTION";
     AddParam( theParams, "Object 1", aCI.GetShape1() );
     AddParam( theParams, "Object 2", aCI.GetShape2() );
+    AddParam( theParams, "Check self-intersections", isCheckSelfInte );
     break;
   case BOOLEAN_COMMON_LIST:
     theOperationName = "COMMON";
     AddParam( theParams, "Selected objects", aCI.GetShapes() );
+    AddParam( theParams, "Check self-intersections", isCheckSelfInte );
     break;
   case BOOLEAN_FUSE_LIST:
     theOperationName = "FUSE";
     AddParam( theParams, "Selected objects", aCI.GetShapes() );
+    AddParam( theParams, "Check self-intersections", isCheckSelfInte );
+    AddParam( theParams, "Remove extra edges", aCI.GetRmExtraEdges() );
     break;
   case BOOLEAN_CUT_LIST:
     theOperationName = "CUT";
     AddParam( theParams, "Main Object", aCI.GetShape1() );
     AddParam( theParams, "Tool Objects", aCI.GetShapes() );
+    AddParam( theParams, "Check self-intersections", isCheckSelfInte );
     break;
   default:
     return false;