]> SALOME platform Git repositories - modules/geom.git/blobdiff - src/GEOMImpl/GEOMImpl_Block6Explorer.cxx
Salome HOME
Fix pb in non-regression scripts, caused by previous integration
[modules/geom.git] / src / GEOMImpl / GEOMImpl_Block6Explorer.cxx
index a73609e60eedf1e5c3bd82159a815907a1456537..0df1b0047487eb927ae120249bf0d436e22fe56e 100644 (file)
@@ -1,30 +1,35 @@
-// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// 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
-// 
+//
 // 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 
+// License as published by the Free Software Foundation; either
+// 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
+// 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/
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-#include <Standard_Stream.hxx>
 
-#include <BRepOffsetAPI_MakeFilling.hxx>
+#include <Standard_Stream.hxx>
 
 #include <GEOMImpl_Block6Explorer.hxx>
 
+#include <ShHealOper_ShapeProcess.hxx>
+
 #include "utilities.h"
 
+#include <Basics_OCCTVersion.hxx>
+
 #include <BRep_Tool.hxx>
 #include <BRep_TFace.hxx>
 #include <BRep_Builder.hxx>
@@ -33,6 +38,8 @@
 #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 <Geom_TrimmedCurve.hxx>
 #include <GeomFill_Generator.hxx>
 
+#include <gce_MakePln.hxx>
+
 #include <Precision.hxx>
 #include <gp_Pnt.hxx>
+#include <gp_Pln.hxx>
 #include <TColgp_Array1OfPnt.hxx>
 
 #include <StdFail_NotDone.hxx>
 #define NBEDGES 12
 #define NBVERTS 8
 
+#define PLANAR_FACE_MAX_TOLERANCE 1e-06
+
+// The following macro, when enabled, causes pcurves upgrade after MakeFilling algorithm
+// in MakeAnyFace function;
+// WARNING: it may lead to extra vertices generation by partition algorithm
+// in some cases, for example when fillet is made on a PipeTShape - 
+// see issues 0021568 and 0021550
+// VSR (15/05/2012): macro commented out (disabled) to avoid extra vertices!
+//#define MAKE_FACE_UPGRADE_PCURVES
+
+// The following macro, when enabled, causes fixing tolerance for pcurves
+// after BRepBuilderAPI_MakeFace + ShHealOper_ShapeProcess in MakeAnyFace function;
+// This sometimes allows to fix problems of extra vertices generation
+// see issue 0022706
+// VSR (17/11/2014): macro enabled
+#define MAKE_FACE_PCURVES_FIX_TOLERANCE
+
+#ifdef MAKE_FACE_PCURVES_FIX_TOLERANCE
+#include <BOPTools_AlgoTools.hxx>
+#include <NCollection_DataMap.hxx>
+#include <ShapeFix_ShapeTolerance.hxx>
+#endif
+
 static Standard_Integer mod4 (Standard_Integer nb)
 {
   if (nb <= 0) return nb + 4;
@@ -979,6 +1012,19 @@ void GEOMImpl_Block6Explorer::InitByTwoFaces (const TopoDS_Shape& theFace1,
     myEdges(edge_id(2, i)) = anEdges2(nb);
   }
 
+  // check the wires closure
+  TopoDS_Wire wire1 = TopoDS::Wire(aWire1);
+  TopoDS_Wire wire2 = TopoDS::Wire(aWire2);
+  TopoDS_Vertex aV1, aV2;
+
+  TopExp::Vertices(wire1, aV1, aV2);
+  if (!aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2))
+    aWire1.Closed(true);
+
+  TopExp::Vertices(wire2, aV1, aV2);
+  if (!aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2))
+    aWire2.Closed(true);
+
   // 4. Generate side surface
   if (!aWire1.Closed() || !aWire2.Closed()) {
     // BRepOffsetAPI_ThruSections is not applicable on not closed wires
@@ -1176,112 +1222,269 @@ Standard_Integer GEOMImpl_Block6Explorer::FindFace
 //function : MakeFace
 //purpose  :
 //=======================================================================
-void GEOMImpl_Block6Explorer::MakeFace (const TopoDS_Wire&     theWire,
-                                        const Standard_Boolean isPlanarWanted,
-                                        TopoDS_Shape&          theResult)
+TCollection_AsciiString GEOMImpl_Block6Explorer::MakeFace (const TopoDS_Wire&     theWire,
+                                                           const Standard_Boolean isPlanarWanted,
+                                                           TopoDS_Shape&          theResult)
 {
-  // try to build face on plane or on any surface under the edges of the wire
-  BRepBuilderAPI_MakeFace MK (theWire, isPlanarWanted);
-  if (MK.IsDone()) {
-    theResult = MK.Shape();
-    return;
-  }
+  if (!isPlanarWanted)
+    return MakeAnyFace(theWire, theResult);
 
-  if (!isPlanarWanted) {
-    // try to construct filling surface
-    BRepOffsetAPI_MakeFilling MF;
+  // Try to build a planar face.
 
-    Standard_Integer nbEdges = 0;
-    BRepTools_WireExplorer aWE (theWire);
-    for (; aWE.More(); aWE.Next(), nbEdges++) {
-      MF.Add(TopoDS::Edge(aWE.Current()), GeomAbs_C0);
-    }
+  // If required tolerance increase will be
+  // higher than PLANAR_FACE_MAX_TOLERANCE,
+  // we will try to build a non-planar face.
 
-    MF.Build();
-    if (MF.IsDone()) {
-      // Result of filling
-      TopoDS_Shape aFace = MF.Shape();
+  TCollection_AsciiString aWarning;
 
-      // Update tolerance
-      Standard_Real aTol = MF.G0Error();
+  // Workaround for Mantis issue 0020956
 
-      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()));
-      }
+  // Count the number of points in the wire.
+  // Collect the first three points.
+  gp_Pnt p1, p2, p3;
+  bool is3Pnts (false);
+  bool p1set(false), p2set(false), p3set(false);
+  BRepTools_WireExplorer wexpl (theWire);
+  for (; wexpl.More(); wexpl.Next()) {
+    if (!p1set) {
+      p1set = true;
+      p1 = BRep_Tool::Pnt(wexpl.CurrentVertex());
+    }
+    else if (!p2set) {
+      p2set = true;
+      p2 = BRep_Tool::Pnt(wexpl.CurrentVertex());
+    }
+    else if (!p3set) {
+      p3set = true;
+      is3Pnts = true;
+      p3 = BRep_Tool::Pnt(wexpl.CurrentVertex());
+    }
+    else {
+      is3Pnts = false;
+      break;
+    }
+  }
 
-      // 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);
-        }
-      }
+  // Construct a plane for the case of three points in the wire.
+  gp_Pln plane;
+  if (is3Pnts) {
+    gce_MakePln mkPln (p1, p2, p3);
+    if (mkPln.IsDone()) {
+      plane = mkPln.Value();
+    }
+    else {
+      is3Pnts = false;
+    }
+  }
 
-      if ((*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance() < aTol) {
-        (*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance(aTol);
-      }
-      theResult = aFace;
+  // Construct a face based on the plane (in case of three points in the wire) or
+  // allow MakeFace to build the plane itself (in case of the number of points is greater than 3).
+  if (is3Pnts) {
+    BRepBuilderAPI_MakeFace MK (plane, theWire, isPlanarWanted);
+    if (MK.IsDone()) {
+      theResult = MK.Shape();
+      return aWarning;
     }
-  } else {
-    // try to update wire tolerances to build a planar face
+  }
+  else {
+    BRepBuilderAPI_MakeFace MK (theWire, isPlanarWanted);
+    if (MK.IsDone()) {
+      theResult = MK.Shape();
+      return aWarning;
+    }
+  }
 
-    // With OCCT6.0 or lower
+  // try to update wire tolerances to build a planar face
 
-    // Find a deviation
-    Standard_Real aToleranceReached, aTol;
-    BRepLib_FindSurface aFS;
-    aFS.Init(theWire, -1., isPlanarWanted);
+  // 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 aWarning;
     aToleranceReached = aFS.ToleranceReached();
     aTol = aFS.Tolerance();
+  }
+  aTol = Max(1.2 * aToleranceReached, aTol);
+
+  // Mantis issue 0021432: EDF GEOM: Faces with huge tolerance can be built in GEOM
+  if (aTol > PLANAR_FACE_MAX_TOLERANCE) {
+    aWarning = MakeAnyFace(theWire, theResult);
+    if (aWarning.IsEmpty() && !theResult.IsNull())
+      aWarning = "MAKE_FACE_TOLERANCE_TOO_BIG";
+    return aWarning;
+  }
+
+  // Copy the wire, bacause it can be updated with very-very big tolerance here
+  BRepBuilderAPI_Copy aMC (theWire);
+  if (!aMC.IsDone()) return aWarning;
+  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();
+    // Mantis issue 0021432: EDF GEOM: Faces with huge tolerance can be built in GEOM
+    //if (aTol > PLANAR_FACE_MAX_TOLERANCE)
+    //  aWarning = "MAKE_FACE_TOLERANCE_TOO_BIG";
+  }
+
+  return aWarning;
+}
+
+//=======================================================================
+//function : MakeAnyFace
+//purpose  :
+//=======================================================================
+TCollection_AsciiString GEOMImpl_Block6Explorer::MakeAnyFace (const TopoDS_Wire& theWire,
+                                                              TopoDS_Shape&      theResult)
+{
+  TCollection_AsciiString aWarning;
+
+  // try to build a face on any surface under the edges of the wire
+  BRepBuilderAPI_MakeFace MK (theWire, Standard_False);
+  if (MK.IsDone()) {
+    theResult = MK.Shape();
+    return aWarning;
+  }
+
+  // try to construct filling surface
+  BRepOffsetAPI_MakeFilling MF;
+
+  Standard_Integer nbEdges = 0;
+  BRepTools_WireExplorer aWE (theWire);
+  for (; aWE.More(); aWE.Next(), nbEdges++) {
+    MF.Add(TopoDS::Edge(aWE.Current()), GeomAbs_C0);
+  }
+
+  MF.Build();
+  if (!MF.IsDone()) {
+    aWarning = "BRepOffsetAPI_MakeFilling failed";
+    return aWarning;
+  }
 
-    if (!aFS.Found()) {
-      aFS.Init(theWire, aToleranceReached, isPlanarWanted);
-      if (!aFS.Found()) return;
-      aToleranceReached = aFS.ToleranceReached();
-      aTol = aFS.Tolerance();
+  // Result of filling
+  TopoDS_Shape aFace = MF.Shape();
+
+  // 12.04.2006 for PAL12149 begin
+  Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(aFace));
+
+#ifdef MAKE_FACE_UPGRADE_PCURVES
+  BRep_Builder BB;
+  TopoDS_Iterator itw(theWire);
+  for (; itw.More(); itw.Next())
+  {
+    const TopoDS_Edge& anEdge = TopoDS::Edge(itw.Value());
+    TopoDS_Edge NewEdge = TopoDS::Edge(MF.Generated(anEdge).First());
+    Standard_Real fpar, lpar;
+    Handle(Geom2d_Curve) NewPCurve = BRep_Tool::CurveOnSurface(NewEdge, TopoDS::Face(aFace), fpar, lpar);
+    TopLoc_Location aLoc;
+    Standard_Real NewTol = BRep_Tool::Tolerance(NewEdge);
+    BB.UpdateEdge(anEdge, NewPCurve, aGS, aLoc, NewTol);
+  }
+#endif
+
+  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 result of BRepBuilderAPI_MakeFace + ShHealOper_ShapeProcess
+#if OCC_VERSION_LARGE >= 0x06080000
+#ifdef MAKE_FACE_PCURVES_FIX_TOLERANCE
+    // check and fix pcurves, if necessary
+    Standard_Real aT, aTolE, aD, aDMax;
+    TopExp_Explorer aExpF, aExpE;
+    NCollection_DataMap<TopoDS_Shape, Standard_Real, TopTools_ShapeMapHasher> aDMETol;
+    aExpF.Init(theResult, TopAbs_FACE);
+    for (; aExpF.More(); aExpF.Next()) {
+      const TopoDS_Face& aF = *(TopoDS_Face*)&aExpF.Current();
+      aExpE.Init(aF, TopAbs_EDGE);
+      for (; aExpE.More(); aExpE.Next()) {
+        const TopoDS_Edge& aE = *(TopoDS_Edge*)&aExpE.Current();
+        if (!BOPTools_AlgoTools::ComputeTolerance(aF, aE, aDMax, aT)) continue;
+        aTolE = BRep_Tool::Tolerance(aE);
+        if (aDMax < aTolE) continue;
+        if (aDMETol.IsBound(aE)) {
+          aD = aDMETol.Find(aE);
+          if (aDMax > aD) {
+            aDMETol.UnBind(aE);
+            aDMETol.Bind(aE, aDMax);
+          }
+        }
+        else {
+          aDMETol.Bind(aE, aDMax);
+        }
+      }
     }
-    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);
+    NCollection_DataMap<TopoDS_Shape, Standard_Real, TopTools_ShapeMapHasher>::Iterator aDMETolIt(aDMETol);
+    ShapeFix_ShapeTolerance sat;
+    for (; aDMETolIt.More(); aDMETolIt.Next()) {
+      sat.LimitTolerance(aDMETolIt.Key(), aDMETolIt.Value());
     }
-    for (TopExp_Explorer expV (aWire, TopAbs_VERTEX); expV.More(); expV.Next()) {
-      TopoDS_Vertex aV = TopoDS::Vertex(expV.Current());
-      B.UpdateVertex(aV, aTol);
+#endif
+#endif
+  }
+  else {
+    // try to deal with pure result of BRepOffsetAPI_MakeFilling
+
+    // Update tolerance
+    Standard_Real aTol = MF.G0Error();
+
+    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()));
     }
-    //BRepLib::UpdateTolerances(aWire);
-    // Build face
-    BRepBuilderAPI_MakeFace MK1 (aWire, isPlanarWanted);
-    if (MK1.IsDone()) {
-      theResult = MK1.Shape();
-      return;
+
+    // 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);
+      }
     }
 
-    // After migration on OCCT version higher than 6.0
-    //BRepLib_MakeFace aBMF;
-    //aBMF.Init(theWire, isPlanarWanted, Standard_True);
-    //if (aBMF.Error() == BRepLib_FaceDone) {
-    //  theResult = aBMF.Shape();
-    //  return;
-    //}
+    if ((*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance() < aTol) {
+      (*((Handle(BRep_TFace)*)&aFace.TShape()))->Tolerance(aTol);
+    }
+    theResult = aFace;
   }
+
+  return aWarning;
 }