Salome HOME
Mantis issue 0021465: EDF 2067 GEOM: Extrusion along a path leads to a self-intersect...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_HealingDriver.cxx
index ffe05748ebe074dcf35834a56800ff4f5a13883f..fe08dc9e532320a3cfef697ae169a8ca81196270 100644 (file)
@@ -1,18 +1,20 @@
-// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// Copyright (C) 2007-2011  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
-// 
+//
 // 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 
+// 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 
+//
+// 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 
+// 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
@@ -25,6 +27,8 @@
 #include <GEOMImpl_IHealing.hxx>
 #include <GEOM_Function.hxx>
 
+#include <GEOMImpl_GlueDriver.hxx>
+
 #include <ShHealOper_ShapeProcess.hxx>
 #include <ShHealOper_RemoveFace.hxx>
 #include <ShHealOper_CloseContour.hxx>
 #include <ShHealOper_FillHoles.hxx>
 #include <ShHealOper_Sewing.hxx>
 #include <ShHealOper_EdgeDivide.hxx>
+#include <ShHealOper_ChangeOrientation.hxx>
+
+#include <BRep_Builder.hxx>
 
-#include <TopoDS.hxx>
 #include <TopExp.hxx>
+#include <TopoDS.hxx>
+#include <TopoDS_Iterator.hxx>
 #include <TopTools_IndexedMapOfShape.hxx>
 
+#include <TColStd_IndexedDataMapOfTransientTransient.hxx>
+#include <TNaming_CopyShape.hxx>
+#include <ShapeFix_ShapeTolerance.hxx>
+#include <ShapeFix_Shape.hxx>
+#include <BRepCheck_Analyzer.hxx>
+
+#include <Precision.hxx>
+
 #include <StdFail_NotDone.hxx>
 
 //=======================================================================
@@ -64,12 +80,10 @@ const Standard_GUID& GEOMImpl_HealingDriver::GetID()
   return aHealingDriver;
 }
 
-
 //=======================================================================
 //function : GEOMImpl_HealingDriver
 //purpose  :
 //=======================================================================
-
 GEOMImpl_HealingDriver::GEOMImpl_HealingDriver()
 {
 }
@@ -115,6 +129,12 @@ Standard_Integer GEOMImpl_HealingDriver::Execute(TFunction_Logbook& log) const
   case DIVIDE_EDGE:
     AddPointOnEdge(&HI, anOriginalShape, aShape);
     break;
+  case CHANGE_ORIENTATION:
+    ChangeOrientation(&HI, anOriginalShape, aShape);
+    break;
+  case LIMIT_TOLERANCE:
+    LimitTolerance(&HI, anOriginalShape, aShape);
+    break;
   default:
     return 0;
   }
@@ -178,19 +198,75 @@ Standard_Boolean GEOMImpl_HealingDriver::ShapeProcess (GEOMImpl_IHealing* theHI,
 //function :  SupressFaces
 //purpose  :
 //=======================================================================
+void SuppressFacesRec (const TopTools_SequenceOfShape& theShapesFaces,
+                       const TopoDS_Shape&             theOriginalShape,
+                       TopoDS_Shape&                   theOutShape)
+{
+  if ((theOriginalShape.ShapeType() != TopAbs_COMPOUND &&
+       theOriginalShape.ShapeType() != TopAbs_COMPSOLID))
+  {
+    ShHealOper_RemoveFace aHealer (theOriginalShape);
+    Standard_Boolean aResult = aHealer.Perform(theShapesFaces);
+
+    if (aResult)
+      theOutShape = aHealer.GetResultShape();
+    else
+      raiseNotDoneExeption(aHealer.GetErrorStatus());
+  }
+  else
+  {
+    BRep_Builder BB;
+    TopoDS_Compound CC;
+    BB.MakeCompound(CC);
+
+    TopTools_MapOfShape mapShape;
+    TopoDS_Iterator It (theOriginalShape, Standard_True, Standard_True);
+
+    for (; It.More(); It.Next()) {
+      TopoDS_Shape aShape_i = It.Value();
+      if (mapShape.Add(aShape_i)) {
+        // check, if current shape contains at least one of faces to be removed
+        bool isFound = false;
+        TopTools_IndexedMapOfShape aShapes_i;
+        TopExp::MapShapes(aShape_i, aShapes_i);
+        for (int i = 1; i <= theShapesFaces.Length() && !isFound; i++) {
+          const TopoDS_Shape& aFace_i = theShapesFaces.Value(i);
+          if (aShapes_i.Contains(aFace_i)) isFound = true;
+        }
+        if (isFound) {
+          TopoDS_Shape anOutSh_i;
+          SuppressFacesRec(theShapesFaces, aShape_i, anOutSh_i);
+          if ( !anOutSh_i.IsNull() )
+            BB.Add(CC, anOutSh_i);
+        }
+        else {
+          // nothing to do
+          BB.Add(CC, aShape_i);
+        }
+      }
+    }
+    theOutShape = CC;
+  }
+}
+
 Standard_Boolean GEOMImpl_HealingDriver::SuppressFaces (GEOMImpl_IHealing* theHI,
                                                         const TopoDS_Shape& theOriginalShape,
                                                         TopoDS_Shape& theOutShape) const
 {
   Handle(TColStd_HArray1OfInteger) aFaces = theHI->GetFaces();
 
-  ShHealOper_RemoveFace aHealer (theOriginalShape);
-
   Standard_Boolean aResult = Standard_False;
-  if (aFaces.IsNull()) // remove all faces
-  {
+
+  if (aFaces.IsNull()) {
+    ShHealOper_RemoveFace aHealer (theOriginalShape);
     aResult = aHealer.Perform();
-  } else {
+
+    if (aResult)
+      theOutShape = aHealer.GetResultShape();
+    else
+      raiseNotDoneExeption(aHealer.GetErrorStatus());
+  }
+  else {
     TopTools_SequenceOfShape aShapesFaces;
     TopTools_IndexedMapOfShape aShapes;
     TopExp::MapShapes(theOriginalShape, aShapes);
@@ -199,16 +275,15 @@ Standard_Boolean GEOMImpl_HealingDriver::SuppressFaces (GEOMImpl_IHealing* theHI
       TopoDS_Shape aFace = aShapes.FindKey(indexOfFace);
       aShapesFaces.Append(aFace);
     }
-
-    aResult = aHealer.Perform(aShapesFaces);
+    SuppressFacesRec(aShapesFaces, theOriginalShape, theOutShape);
+    if ((theOriginalShape.ShapeType() == TopAbs_COMPOUND ||
+         theOriginalShape.ShapeType() == TopAbs_COMPSOLID)) {
+      TopoDS_Shape aSh = theOutShape;
+      theOutShape = GEOMImpl_GlueDriver::GlueFaces(aSh, Precision::Confusion(), Standard_True);
+    }
   }
 
-  if ( aResult )
-    theOutShape = aHealer.GetResultShape();
-  else
-    raiseNotDoneExeption( aHealer.GetErrorStatus() );
-
-  return aResult;
+  return Standard_True;
 }
 
 //=======================================================================
@@ -379,6 +454,57 @@ Standard_Boolean GEOMImpl_HealingDriver::AddPointOnEdge (GEOMImpl_IHealing* theH
 }
 
 
+//=======================================================================
+//function :  ChangeOrientation
+//purpose  :
+//=======================================================================
+Standard_Boolean GEOMImpl_HealingDriver::ChangeOrientation (GEOMImpl_IHealing* theHI,
+                                                            const TopoDS_Shape& theOriginalShape,
+                                                            TopoDS_Shape& theOutShape) const
+{
+  ShHealOper_ChangeOrientation aHealer (theOriginalShape);
+
+  Standard_Boolean aResult = aHealer.Perform();
+
+  if (aResult)
+    theOutShape = aHealer.GetResultShape();
+  else
+    raiseNotDoneExeption( aHealer.GetErrorStatus() );
+
+  return aResult;
+}
+
+//=======================================================================
+//function : LimitTolerance
+//purpose  :
+//=======================================================================
+void GEOMImpl_HealingDriver::LimitTolerance (GEOMImpl_IHealing* theHI,
+                                             const TopoDS_Shape& theOriginalShape,
+                                             TopoDS_Shape& theOutShape) const
+{
+  Standard_Real aTol = theHI->GetTolerance();
+  if (aTol < Precision::Confusion())
+    aTol = Precision::Confusion();
+
+  // 1. Make a copy to prevent the original shape changes.
+  TopoDS_Shape aShapeCopy;
+  TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
+  TNaming_CopyShape::CopyTool(theOriginalShape, aMapTShapes, aShapeCopy);
+
+  // 2. Limit tolerance.
+  ShapeFix_ShapeTolerance aSFT;
+  aSFT.LimitTolerance(aShapeCopy, aTol, aTol, TopAbs_SHAPE);
+
+  // 3. Fix obtained shape.
+  Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShapeCopy);
+  aSfs->Perform();
+  theOutShape = aSfs->Shape();
+
+  BRepCheck_Analyzer ana (theOutShape, Standard_True);
+  if (!ana.IsValid())
+    StdFail_NotDone::Raise("Non valid shape result");
+}
+
 //=======================================================================
 //function :  GEOMImpl_HealingDriver_Type_
 //purpose  :
@@ -396,10 +522,10 @@ Standard_EXPORT Handle_Standard_Type& GEOMImpl_HealingDriver_Type_()
 
   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_HealingDriver",
-                                                        sizeof(GEOMImpl_HealingDriver),
-                                                        1,
-                                                        (Standard_Address)_Ancestors,
-                                                        (Standard_Address)NULL);
+                                                         sizeof(GEOMImpl_HealingDriver),
+                                                         1,
+                                                         (Standard_Address)_Ancestors,
+                                                         (Standard_Address)NULL);
 
   return _aType;
 }
@@ -419,7 +545,5 @@ const Handle(GEOMImpl_HealingDriver) Handle(GEOMImpl_HealingDriver)::DownCast(co
      }
   }
 
-  return _anOtherObject ;
+  return _anOtherObject;
 }
-
-