]> SALOME platform Git repositories - modules/geom.git/blobdiff - src/GEOMImpl/GEOMImpl_BooleanDriver.cxx
Salome HOME
Merge branch 'master' into V7_5_BR
[modules/geom.git] / src / GEOMImpl / GEOMImpl_BooleanDriver.cxx
index 8cc9f8bfdd485c6943fe148c2df092f766805b4e..32b0b04fa21b02af7d5861013d0b7f0e8a5dadd5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2014  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
@@ -26,6 +26,8 @@
 #include <GEOMImpl_GlueDriver.hxx>
 #include <GEOM_Function.hxx>
 #include <GEOMUtils.hxx>
+#include <BlockFix_BlockFixAPI.hxx>
+#include <ShHealOper_ShapeProcess.hxx>
 
 #include <TNaming_CopyShape.hxx>
 
@@ -39,6 +41,8 @@
 #include <BRepAlgoAPI_Fuse.hxx>
 #include <BRepAlgoAPI_Section.hxx>
 #include <BRepCheck_Analyzer.hxx>
+#include <BOPAlgo_CheckerSI.hxx>
+#include <BOPDS_DS.hxx>
 
 #include <TopExp_Explorer.hxx>
 #include <TopoDS_Compound.hxx>
 #include <Standard_ConstructionError.hxx>
 #include <StdFail_NotDone.hxx>
 
+// Depth of self-intersection check (see BOPAlgo_CheckerSI::SetLevelOfCheck() for more details)
+// Default value for BOPAlgo_CheckerSI gives very long computation when checking face-to-face intersections;
+// 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() == Standard_False) {
+    BlockFix_BlockFixAPI aTool;
+
+    aTool.OptimumNbFaces() = 0;
+    aTool.SetShape(theShape);
+    aTool.Perform();
+    aResult = aTool.Shape();
+
+    // Repair result
+    BRepCheck_Analyzer anAna (aResult, false);
+    Standard_Boolean isValid = anAna.IsValid();
+
+    if (!isValid) {
+      TopoDS_Shape aFixed;
+      ShHealOper_ShapeProcess aHealer;
+
+      aHealer.Perform(aResult, aFixed);
+
+      if (aHealer.isDone()) {
+        aResult = aFixed;
+        anAna.Init(aResult, false);
+        isValid = anAna.IsValid();
+      }
+    }
+
+    if (!isValid) {
+      aResult.Nullify();
+    }
+  }
+
+  return aResult;
+}
+
 //=======================================================================
 //function : GetID
 //purpose  :
@@ -83,6 +135,8 @@ 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;
 
@@ -106,10 +160,38 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
         if (!ana.IsValid())
           StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
 
-        aShape = performOperation (aShape1, aShape2, aType);
+        if (isCheckSelfInte) {
+          BOPAlgo_CheckerSI aCSI;  // checker of self-interferences
+          aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
+          BOPCol_ListOfShape aList1, aList2;
+          aList1.Append(aShape1);
+          aList2.Append(aShape2);
+          aCSI.SetArguments(aList1);
+          aCSI.Perform();
+          if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0)
+            StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
+          aCSI.SetArguments(aList2);
+          aCSI.Perform();
+          if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0)
+            StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
+        }
+
+        // Make a copy to prevent the original shape changes.
+        TopoDS_Shape aShapeCopy1;
+        TopoDS_Shape aShapeCopy2;
+        TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
+        TNaming_CopyShape::CopyTool(aShape1, aMapTShapes, aShapeCopy1);
+        TNaming_CopyShape::CopyTool(aShape2, aMapTShapes, aShapeCopy2);
 
-        if (aShape.IsNull())
+        aShape = performOperation (aShapeCopy1, aShapeCopy2, aType);
+
+        if (isRmExtraEdges) {
+          aShape = RemoveExtraEdges(aShape);
+        }
+
+        if (aShape.IsNull()) {
           return 0;
+        }
       }
     }
     break;
@@ -127,30 +209,66 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
       if (nbShapes > 0) {
         aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
         aShape = aRefShape->GetValue();
-
+       
         if (!aShape.IsNull()) {
           BRepCheck_Analyzer anAna (aShape, Standard_True);
-
           if (!anAna.IsValid()) {
             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
           }
 
-          for (i = 2; i <= nbShapes; i++) {
-            aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(i));
-            aShape2 = aRefShape->GetValue();
-            anAna.Init(aShape2);
+          BOPAlgo_CheckerSI aCSI;  // checker of self-interferences
 
-            if (!anAna.IsValid()) {
-              StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
+          if (isCheckSelfInte) {
+            aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
+            BOPCol_ListOfShape aList1;
+            aList1.Append(aShape);
+            aCSI.SetArguments(aList1);
+            aCSI.Perform();
+            if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
+              StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
             }
+          }
+
+          // Copy shape
+          TopoDS_Shape aShapeCopy;
+          TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
 
-            aShape = performOperation (aShape, aShape2, aSimpleType);
+          TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
+          aShape = aShapeCopy;
 
-            if (aShape.IsNull()) {
-              return 0;
+          for (i = 2; i <= nbShapes; i++) {
+           aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(i));
+           aShape2 = aRefShape->GetValue();
+           anAna.Init(aShape2);
+           
+           if (!anAna.IsValid()) {
+             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
+           }
+           
+            if (isCheckSelfInte) {
+              BOPCol_ListOfShape aList2;
+              aList2.Append(aShape2);
+              aCSI.SetArguments(aList2);
+              aCSI.Perform();
+              if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
+                StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
+              }
             }
-          }
-        }
+
+            // Copy shape
+            aShapeCopy.Nullify();
+            TNaming_CopyShape::CopyTool(aShape2, aMapTShapes, aShapeCopy);
+           aShape = performOperation (aShape, aShapeCopy, aSimpleType);
+
+            if (isRmExtraEdges) {
+              aShape = RemoveExtraEdges(aShape);
+            }
+           
+           if (aShape.IsNull()) {
+             return 0;
+           }
+         }
+       }
       }
     }
     break;
@@ -168,6 +286,26 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
           StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
         }
 
+       BOPAlgo_CheckerSI aCSI;  // checker of self-interferences
+
+        if (isCheckSelfInte) {
+          aCSI.SetLevelOfCheck(BOP_SELF_INTERSECTIONS_LEVEL);
+          BOPCol_ListOfShape aList1;
+          aList1.Append(aShape);
+          aCSI.SetArguments(aList1);
+          aCSI.Perform();
+          if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
+            StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
+          }
+        }
+
+        // Copy shape
+        TopoDS_Shape aShapeCopy;
+        TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
+
+        TNaming_CopyShape::CopyTool(aShape, aMapTShapes, aShapeCopy);
+        aShape = aShapeCopy;
+       
         Handle(TColStd_HSequenceOfTransient) aTools = aCI.GetShapes();
         const Standard_Integer nbShapes = aTools->Length();
         Standard_Integer i;
@@ -183,7 +321,20 @@ Standard_Integer GEOMImpl_BooleanDriver::Execute (TFunction_Logbook& log) const
             StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is not valid");
           }
 
-          aShape = performOperation (aShape, aTool, BOOLEAN_CUT);
+          if (isCheckSelfInte) {
+            BOPCol_ListOfShape aList2;
+            aList2.Append(aTool);
+            aCSI.SetArguments(aList2);
+            aCSI.Perform();
+            if (aCSI.ErrorStatus() || aCSI.DS().Interferences().Extent() > 0) {
+              StdFail_NotDone::Raise("Boolean operation will not be performed, because argument shape is self-intersected");
+            }
+          }
+
+          // Copy shape
+          aShapeCopy.Nullify();
+          TNaming_CopyShape::CopyTool(aTool, aMapTShapes, aShapeCopy);
+          aShape = performOperation (aShape, aShapeCopy, BOOLEAN_CUT);
 
           if (aShape.IsNull()) {
             return 0;
@@ -510,42 +661,73 @@ TopoDS_Shape GEOMImpl_BooleanDriver::performOperation
   return aShape;
 }
 
-//=======================================================================
-//function :  GEOMImpl_BooleanDriver_Type_
-//purpose  :
-//=======================================================================
-Standard_EXPORT Handle_Standard_Type& GEOMImpl_BooleanDriver_Type_()
-{
-  static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
-  if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
-  static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
-  if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
-  static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
-  if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
-
-  static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
-  static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_BooleanDriver",
-                                                         sizeof(GEOMImpl_BooleanDriver),
-                                                         1,
-                                                         (Standard_Address)_Ancestors,
-                                                         (Standard_Address)NULL);
-
-  return _aType;
-}
+//================================================================================
+/*!
+ * \brief Returns a name of creation operation and names and values of creation parameters
+ */
+//================================================================================
 
-//=======================================================================
-//function : DownCast
-//purpose  :
-//=======================================================================
-const Handle(GEOMImpl_BooleanDriver) Handle(GEOMImpl_BooleanDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
+bool GEOMImpl_BooleanDriver::
+GetCreationInformation(std::string&             theOperationName,
+                       std::vector<GEOM_Param>& theParams)
 {
-  Handle(GEOMImpl_BooleanDriver) _anOtherObject;
+  if (Label().IsNull()) return 0;
+  Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
 
-  if (!AnObject.IsNull()) {
-    if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_BooleanDriver))) {
-      _anOtherObject = Handle(GEOMImpl_BooleanDriver)((Handle(GEOMImpl_BooleanDriver)&)AnObject);
-    }
+  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;
   }
 
-  return _anOtherObject;
+  return true;
 }
+
+IMPLEMENT_STANDARD_HANDLE (GEOMImpl_BooleanDriver,GEOM_BaseDriver);
+
+IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_BooleanDriver,GEOM_BaseDriver);