Salome HOME
0023128: [CEA 1555] FuseCollinearEdgesWithinWire fails on the outline of a square...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_HealingDriver.cxx
index 51154eb638b116c43c4ae70a04c6a7b3169ade0f..fab57241a61731153ec96b7e589dea27593da6ec 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  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
@@ -20,8 +20,6 @@
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
-#include <Standard_Stream.hxx>
-
 #include <GEOMImpl_HealingDriver.hxx>
 #include <GEOMImpl_Types.hxx>
 #include <GEOMImpl_IHealing.hxx>
 
 #include <TNaming_CopyShape.hxx>
 
-#include <ShapeFix_ShapeTolerance.hxx>
-#include <ShapeFix_Shape.hxx>
-
 #include <BRep_Builder.hxx>
 #include <BRepAdaptor_Curve.hxx>
-#include <BRepCheck_Analyzer.hxx>
 #include <BRepTools_WireExplorer.hxx>
 
 #include <TopExp.hxx>
@@ -456,26 +450,19 @@ Standard_Boolean GEOMImpl_HealingDriver::Sew (GEOMImpl_IHealing*  theHI,
 {
   Standard_Real aTol = theHI->GetTolerance();
 
-  TopoDS_Compound faceCompound;
+  TopoDS_Compound aCompound;
   BRep_Builder builder;
-  builder.MakeCompound( faceCompound );
+  builder.MakeCompound( aCompound );
 
-  TopExp_Explorer faceExp( theOriginalShape, TopAbs_FACE );
-  for ( ; faceExp.More(); faceExp.Next() )
-    builder.Add( faceCompound, faceExp.Current() );
-  
+  builder.Add( aCompound, theOriginalShape );
   Handle(TColStd_HSequenceOfTransient) otherObjs = theHI->GetShapes();
   for ( int ind = 1; ind <= otherObjs->Length(); ind++)
   {
     Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(otherObjs->Value(ind));
-    TopoDS_Shape aShape = aRefShape->GetValue();
-    if (aShape.IsNull())
-      Standard_NullObject::Raise("Null object given");
-    for ( faceExp.Init( aShape, TopAbs_FACE ); faceExp.More(); faceExp.Next() )
-      builder.Add( faceCompound, faceExp.Current() );
+    builder.Add( aCompound, aRefShape->GetValue() );
   }
 
-  ShHealOper_Sewing aHealer (faceCompound, aTol);
+  ShHealOper_Sewing aHealer (aCompound, aTol);
 
   // Set non-manifold mode.
   aHealer.SetNonManifoldMode(isAllowNonManifold);
@@ -650,6 +637,8 @@ void GEOMImpl_HealingDriver::LimitTolerance (GEOMImpl_IHealing* theHI,
                                              TopoDS_Shape& theOutShape) const
 {
   Standard_Real aTol = theHI->GetTolerance();
+  TopAbs_ShapeEnum aType = theHI->GetType();
+
   if (aTol < Precision::Confusion())
     aTol = Precision::Confusion();
 
@@ -659,19 +648,14 @@ void GEOMImpl_HealingDriver::LimitTolerance (GEOMImpl_IHealing* theHI,
     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())
+  // 2. Limit tolerance.
+  if (!GEOMUtils::FixShapeTolerance(aShapeCopy, aType, aTol))
     StdFail_NotDone::Raise("Non valid shape result");
 
+  // 3. Set the result
+  theOutShape = aShapeCopy;
+
   // 4. Collect statistics
   {
     ShHealOper_Tool tool;
@@ -820,25 +804,73 @@ void GEOMImpl_HealingDriver::FuseCollinearEdges (const TopoDS_Shape& theOriginal
   theWire = TopoDS::Wire(Fixer->Shape());
   */
 
-  TopoDS_Edge prevEdge;
-  TopTools_ListOfShape finalList, currChain;
+  // Get the ordered list of edges.
+  TopTools_ListOfShape   anEdges;
+  TopTools_ListOfShape   aCurVertices;
+  BRepTools_WireExplorer aWExp (theWire);
 
-  BRepTools_WireExplorer wexp (theWire);
-  if (wexp.More()) {
-    prevEdge = wexp.Current();
-    currChain.Append(prevEdge);
-    wexp.Next();
+  for (; aWExp.More(); aWExp.Next()) {
+    anEdges.Append(aWExp.Current());
+    aCurVertices.Append(aWExp.CurrentVertex());
   }
-  else {
+
+  if (anEdges.IsEmpty()) {
     Standard_NullObject::Raise("Empty wire given");
   }
 
-  for (; wexp.More(); wexp.Next()) {
-    TopoDS_Edge anEdge = wexp.Current();
-    TopoDS_Vertex CurVertex = wexp.CurrentVertex();
+  // Treat the case if the wire is closed and first and last edges are C1.
+  Standard_Boolean isShift = Standard_False;
+
+  if (BRep_Tool::IsClosed(theWire)) {
+    // Wire is closed. Check if there are more than 2 edges in the wire.
+    if (!anEdges.First().IsSame(anEdges.Last())) {
+      isShift = Standard_True;
+    }
+  }
+
+  if (isShift) {
+    // Put first edge to the end of the list while the chain break is reached.
+    TopoDS_Shape aFirstEdge = anEdges.First();
+
+    while (isShift) {
+      isShift = Standard_False;
+
+      // Check if the first vertex should be kept
+      if (aMapToRemove.Contains(aCurVertices.First()) || removeAll) {
+        // Check if first and last edges are C1.
+        TopoDS_Edge anEdge1 = TopoDS::Edge(anEdges.Last());
+        TopoDS_Edge anEdge2 = TopoDS::Edge(anEdges.First());
+
+        if (AreEdgesC1(anEdge1, anEdge2)) {
+          // Make the first edge last.
+          anEdges.Append(anEdge2);
+          anEdges.RemoveFirst();
+          aCurVertices.Append(aCurVertices.First());
+          aCurVertices.RemoveFirst();
+
+          // Check if we reached the first edge again.
+          // Break the loop in this case.
+          isShift = !aFirstEdge.IsSame(anEdges.First());
+        }
+      }
+    }
+  }
+
+  TopTools_ListOfShape finalList, currChain;
+  TopTools_ListIteratorOfListOfShape anEIter(anEdges);
+  TopTools_ListIteratorOfListOfShape aVIter(aCurVertices);
+  TopoDS_Edge                        prevEdge = TopoDS::Edge(anEIter.Value());
+
+  currChain.Append(prevEdge);
+  anEIter.Next();
+  aVIter.Next();
+
+  for (; anEIter.More(); anEIter.Next(), aVIter.Next()) {
+    TopoDS_Edge         anEdge     = TopoDS::Edge(anEIter.Value());
+    const TopoDS_Shape &aCurVertex = aVIter.Value();
 
     bool continueChain = false;
-    if (aMapToRemove.Contains(CurVertex) || removeAll) {
+    if (aMapToRemove.Contains(aCurVertex) || removeAll) {
       // if C1 -> continue chain
       if (AreEdgesC1(prevEdge, anEdge)) {
         continueChain = true;
@@ -907,8 +939,7 @@ void GEOMImpl_HealingDriver::FuseCollinearEdges (const TopoDS_Shape& theOriginal
   }
   theOutShape = aFinalWire;
 
-  BRepCheck_Analyzer ana (theOutShape, Standard_True);
-  if (!ana.IsValid())
+  if (!GEOMUtils::CheckShape(theOutShape, true))
     StdFail_NotDone::Raise("Non valid shape result");
 }
 
@@ -1082,6 +1113,7 @@ GetCreationInformation(std::string&             theOperationName,
     theOperationName = "LIMIT_TOLERANCE";
     AddParam( theParams, "Selected shape", aCI.GetOriginal() );
     AddParam( theParams, "Tolerance", aCI.GetTolerance() );
+    AddParam( theParams, "Type", aCI.GetType() );
     break;
   case FUSE_COLLINEAR_EDGES:
     theOperationName = "FUSE_EDGES";