Salome HOME
Bug 0020228: [CEA 331] the geompy.RemoveExtraEdges crash with the attached brep shape...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_Block6Explorer.cxx
index 71987a990e9ed8aa34fe0496304fd7a2cc41251a..68e3983d911d4151f35fce2f7435814a6fac67cb 100644 (file)
@@ -1,16 +1,43 @@
-using namespace std;
-
-#include <BRepOffsetAPI_MakeFilling.hxx>
-
-#include "GEOMImpl_Block6Explorer.hxx"
+//  Copyright (C) 2007-2008  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
+//  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
+//  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
+//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+//  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+#include <Standard_Stream.hxx>
+
+#include <GEOMImpl_Block6Explorer.hxx>
+
+#include <ShHealOper_ShapeProcess.hxx>
 
 #include "utilities.h"
 
 #include <BRep_Tool.hxx>
 #include <BRep_TFace.hxx>
+#include <BRep_Builder.hxx>
+#include <BRepLib.hxx>
+#include <BRepLib_FindSurface.hxx>
 #include <BRepTools.hxx>
 #include <BRepTools_WireExplorer.hxx>
 #include <BRepOffsetAPI_ThruSections.hxx>
+#include <BRepOffsetAPI_MakeFilling.hxx>
+#include <BRepCheck_Analyzer.hxx>
+#include <BRepBuilderAPI_Copy.hxx>
 #include <BRepBuilderAPI_MakeEdge.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
 #include <BRepBuilderAPI_MakeFace.hxx>
@@ -720,7 +747,8 @@ void GEOMImpl_Block6Explorer::InitByBlockAndVertices (const TopoDS_Shape& theBlo
   TopTools_IndexedMapOfShape aFaceEdges;
   TopExp::MapShapes(myFaces(1), TopAbs_EDGE, aFaceEdges);
 
-  for (Standard_Integer i = 1; i <= 4; i++) {
+  Standard_Integer i = 1;
+  for (; i <= 4; i++) {
     // Get i-th vertex of the face 1
     TopoDS_Shape Vi = myVertices(vertex_id(1, i));
     if (!MB.Contains(Vi)) {
@@ -757,7 +785,7 @@ void GEOMImpl_Block6Explorer::InitByBlockAndVertices (const TopoDS_Shape& theBlo
   }
 
   // 3. Store edges of the second (opposite to the first) face
-  for (Standard_Integer i = 1; i <= 4; i++) {
+  for (i = 1; i <= 4; i++) {
     // Get i-th and (i+1)-th vertices of the face 2
     TopoDS_Shape Vi = myVertices(vertex_id(2, i));
     TopoDS_Shape Vj = myVertices(vertex_id(2, mod4(i + 1)));
@@ -792,7 +820,7 @@ void GEOMImpl_Block6Explorer::InitByBlockAndVertices (const TopoDS_Shape& theBlo
     Standard_TypeMismatch::Raise("Block has wrong number of edges");
   }
 
-  for (Standard_Integer i = 2; i <= NBFACES; i++) {
+  for (i = 2; i <= NBFACES; i++) {
     TopoDS_Shape Ei1 = myEdges(edge_id(i, 1));
     TopoDS_Shape Ei2 = myEdges(edge_id(i, 2));
     const TopTools_ListOfShape& aFacesOfEi1 = MBE.FindFromKey(Ei1);
@@ -915,7 +943,8 @@ void GEOMImpl_Block6Explorer::InitByTwoFaces (const TopoDS_Shape& theFace1,
 
   Standard_Real Dist_min = RealLast();
   // try all possible locations to find the best (with minimum sum distance)
-  for (Standard_Integer i = 1; i <= 4; i++) {
+  Standard_Integer i = 1;
+  for (; i <= 4; i++) {
     // try direct orientation
     Standard_Real Dist_plus = aPnts1(1).Distance(aPnts2(i)) +
                               aPnts1(2).Distance(aPnts2(mod4(i + 1))) +
@@ -941,7 +970,7 @@ void GEOMImpl_Block6Explorer::InitByTwoFaces (const TopoDS_Shape& theFace1,
 
   // 3. Put vertices and edges of the second face to they
   //    permanent location in <myVertices> and <myEdges>
-  for (Standard_Integer i = 1; i <= 4; i++) {
+  for (i = 1; i <= 4; i++) {
     Standard_Integer nb = mod4(i_min + s_min*(i - 1));
 
     if (aPnts1(i).Distance(aPnts2(nb)) < Precision::Confusion()) {
@@ -1162,8 +1191,8 @@ void GEOMImpl_Block6Explorer::MakeFace (const TopoDS_Wire&     theWire,
     return;
   }
 
-  // try to construct filling surface
   if (!isPlanarWanted) {
+    // try to construct filling surface
     BRepOffsetAPI_MakeFilling MF;
 
     Standard_Integer nbEdges = 0;
@@ -1177,37 +1206,105 @@ void GEOMImpl_Block6Explorer::MakeFace (const TopoDS_Wire&     theWire,
       // Result of filling
       TopoDS_Shape aFace = MF.Shape();
 
-      // Update tolerance
-      Standard_Real aTol = MF.G0Error();
-
-      TColgp_Array1OfPnt aPnts (1,nbEdges); // points of the given wire
-      aWE = BRepTools_WireExplorer(theWire);
-      Standard_Integer vi = 1;
-      for (; aWE.More() && vi <= nbEdges; aWE.Next(), vi++) {
-        aPnts(vi) = BRep_Tool::Pnt(TopoDS::Vertex(aWE.CurrentVertex()));
+      // 12.04.2006 for PAL12149 begin
+      Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aFace));
+      BRepBuilderAPI_MakeFace MK1 (aGS, theWire);
+      if (MK1.IsDone()) {
+        TopoDS_Shape aFace1 = MK1.Shape();
+
+        BRepCheck_Analyzer ana (aFace1, false);
+        if (!ana.IsValid()) {
+          TopoDS_Shape aFace2;
+          ShHealOper_ShapeProcess aHealer;
+          aHealer.Perform(aFace1, aFace2);
+          if (aHealer.isDone())
+            theResult = aFace2;
+        }
       }
+      // 12.04.2006 for PAL12149 end
+
+      if (theResult.IsNull()) { // try to deal with pure result of filling
+        // Update tolerance
+        Standard_Real aTol = MF.G0Error();
 
-      // Find maximum deviation in vertices
-      TopExp_Explorer exp (aFace, TopAbs_VERTEX);
-      TopTools_MapOfShape mapShape;
-      for (; exp.More(); exp.Next()) {
-        if (mapShape.Add(exp.Current())) {
-          TopoDS_Vertex aV = TopoDS::Vertex(exp.Current());
-          Standard_Real aTolV = BRep_Tool::Tolerance(aV);
-          gp_Pnt aP = BRep_Tool::Pnt(aV);
-          Standard_Real min_dist = aP.Distance(aPnts(1));
-          for (vi = 2; vi <= nbEdges; vi++) {
-            min_dist = Min(min_dist, aP.Distance(aPnts(vi)));
+        TColgp_Array1OfPnt aPnts (1,nbEdges); // points of the given wire
+        BRepTools_WireExplorer aWE1 (theWire);
+        Standard_Integer vi = 1;
+        for (; aWE1.More() && vi <= nbEdges; aWE1.Next(), vi++) {
+          aPnts(vi) = BRep_Tool::Pnt(TopoDS::Vertex(aWE1.CurrentVertex()));
+        }
+
+        // Find maximum deviation in vertices
+        TopExp_Explorer exp (aFace, TopAbs_VERTEX);
+        TopTools_MapOfShape mapShape;
+        for (; exp.More(); exp.Next()) {
+          if (mapShape.Add(exp.Current())) {
+            TopoDS_Vertex aV = TopoDS::Vertex(exp.Current());
+            Standard_Real aTolV = BRep_Tool::Tolerance(aV);
+            gp_Pnt aP = BRep_Tool::Pnt(aV);
+            Standard_Real min_dist = aP.Distance(aPnts(1));
+            for (vi = 2; vi <= nbEdges; vi++) {
+              min_dist = Min(min_dist, aP.Distance(aPnts(vi)));
+            }
+            aTol = Max(aTol, aTolV);
+            aTol = Max(aTol, min_dist);
           }
-          aTol = Max(aTol, aTolV);
-          aTol = Max(aTol, min_dist);
         }
-      }
 
-      if ((*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance() < aTol) {
-        (*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance(aTol);
+        if ((*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance() < aTol) {
+          (*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance(aTol);
+        }
+        theResult = aFace;
       }
-      theResult = aFace;
     }
+  } else {
+    // try to update wire tolerances to build a planar face
+
+#if 1 //(OCC_VERSION_MAJOR < 6) || (OCC_VERSION_MAJOR == 6 && OCC_VERSION_MINOR <= 1)
+    // Find a deviation
+    Standard_Real aToleranceReached, aTol;
+    BRepLib_FindSurface aFS;
+    aFS.Init(theWire, -1., isPlanarWanted);
+    aToleranceReached = aFS.ToleranceReached();
+    aTol = aFS.Tolerance();
+
+    if (!aFS.Found()) {
+      aFS.Init(theWire, aToleranceReached, isPlanarWanted);
+      if (!aFS.Found()) return;
+      aToleranceReached = aFS.ToleranceReached();
+      aTol = aFS.Tolerance();
+    }
+    aTol = Max(1.2 * aToleranceReached, aTol);
+
+    // Copy the wire, bacause it can be updated with very-very big tolerance here
+    BRepBuilderAPI_Copy aMC (theWire);
+    if (!aMC.IsDone()) return;
+    TopoDS_Wire aWire = TopoDS::Wire(aMC.Shape());
+    // Update tolerances to <aTol>
+    BRep_Builder B;
+    for (TopExp_Explorer expE (aWire, TopAbs_EDGE); expE.More(); expE.Next()) {
+      TopoDS_Edge anE = TopoDS::Edge(expE.Current());
+      B.UpdateEdge(anE, aTol);
+    }
+    for (TopExp_Explorer expV (aWire, TopAbs_VERTEX); expV.More(); expV.Next()) {
+      TopoDS_Vertex aV = TopoDS::Vertex(expV.Current());
+      B.UpdateVertex(aV, aTol);
+    }
+    //BRepLib::UpdateTolerances(aWire);
+    // Build face
+    BRepBuilderAPI_MakeFace MK1 (aWire, isPlanarWanted);
+    if (MK1.IsDone()) {
+      theResult = MK1.Shape();
+      return;
+    }
+
+#else // After migration on OCCT version, containing PKV's fix. See bug 8293
+    BRepLib_MakeFace aBMF;
+    aBMF.Init(theWire, isPlanarWanted, Standard_True);
+    if (aBMF.Error() == BRepLib_FaceDone) {
+      theResult = aBMF.Shape();
+      return;
+    }
+#endif
   }
 }