Salome HOME
Merge branch occ/shape_reparation_2
[modules/geom.git] / src / GEOMUtils / GEOMUtils.cxx
index 5b5cc2574a167a061354e90e033e212f285b127f..75fa4b94b3784e8d888f739f2a41bfdb7872fe06 100644 (file)
@@ -26,7 +26,6 @@
 
 #include <Basics_OCCTVersion.hxx>
 
-#include <utilities.h>
 #include <OpUtil.hxx>
 #include <Utils_ExceptHandlers.hxx>
 
 #include <BRepClass3d_SolidClassifier.hxx>
 
 #include <BRepBuilderAPI_MakeFace.hxx>
+#include <BRepBuilderAPI_Sewing.hxx>
+
+#include <BRepCheck_Analyzer.hxx>
 
 #include <Bnd_Box.hxx>
 
+#include <BOPTools_AlgoTools.hxx>
+
 #include <TopAbs.hxx>
 #include <TopExp.hxx>
 #include <TopoDS.hxx>
 
 #include <ShapeAnalysis.hxx>
 #include <ShapeFix_Shape.hxx>
+#include <ShapeFix_ShapeTolerance.hxx>
 
 #include <ProjLib.hxx>
 #include <ElSLib.hxx>
 
 #include <vector>
 #include <sstream>
+#include <algorithm>
 
 #include <Standard_Failure.hxx>
 #include <Standard_NullObject.hxx>
 
 #define STD_SORT_ALGO 1
 
+// When the following macro is defined, ShapeFix_ShapeTolerance function is used to set max tolerance of curve
+// in GEOMUtils::FixShapeCurves function; otherwise less restrictive BRep_Builder::UpdateEdge/UpdateVertex
+// approach is used
+// VSR (29/12/2014): macro disabled
+//#define USE_LIMIT_TOLERANCE
+
 namespace
 {
   /**
@@ -112,7 +124,6 @@ namespace
                                TopoDS_Shape  &theModifiedShape,
                                Standard_Real &theAddDist)
   {
-    Standard_Boolean isModified = Standard_False;
     TopExp_Explorer anExp;
     int nbf = 0;
 
@@ -136,80 +147,74 @@ namespace
         const Standard_Boolean isShell =
           (sh.ShapeType()==TopAbs_SHELL || sh.ShapeType()==TopAbs_FACE);
 
-        if( isShell || S->IsUPeriodic() ) {
-          // non solid case or any periodic surface (Mantis 22454).
-          double U1,U2,V1,V2;
-          // changes for 0020677: EDF 1219 GEOM: MinDistance gives 0 instead of 20.88
-          //S->Bounds(U1,U2,V1,V2); changed by
-          ShapeAnalysis::GetFaceUVBounds(TopoDS::Face(theModifiedShape),U1,U2,V1,V2);
-          // end of changes for 020677 (dmv)
-          Handle(Geom_RectangularTrimmedSurface) TrS1 =
-            new Geom_RectangularTrimmedSurface(S,U1,(U1+U2)/2.,V1,V2);
-          Handle(Geom_RectangularTrimmedSurface) TrS2 =
-            new Geom_RectangularTrimmedSurface(S,(U1+U2)/2.,U2,V1,V2);
+        if ( !isShell && S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ) {
+          Handle(Geom_SphericalSurface) SS = Handle(Geom_SphericalSurface)::DownCast(S);
+          gp_Pnt PC = SS->Location();
           BRep_Builder B;
-          TopoDS_Face F1,F2;
-          TopoDS_Shape aMShape;
-
-          if (isShell) {
-            B.MakeCompound(TopoDS::Compound(aMShape));
-          } else {
-            B.MakeShell(TopoDS::Shell(aMShape));
-          }
-
-          B.MakeFace(F1,TrS1,1.e-7);
-          B.Add(aMShape,F1);
-          B.MakeFace(F2,TrS2,1.e-7);
-          B.Add(aMShape,F2);
-          Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
-
-          if (!isShell) {
-            // The original shape is a solid.
-            TopoDS_Solid aSolid;
-
-            B.MakeSolid(aSolid);
-            B.Add(aSolid, aMShape);
-            aMShape = aSolid;
-          }
-
-          sfs->Init(aMShape);
-          sfs->SetPrecision(1.e-6);
-          sfs->SetMaxTolerance(1.0);
-          sfs->Perform();
-          theModifiedShape = sfs->Shape();
-          isModified = Standard_True;
+          TopoDS_Vertex V;
+          B.MakeVertex(V,PC,1.e-7);
+          theModifiedShape = V;
+          theAddDist = SS->Radius();
+          return Standard_True;
         }
-        else {
-          if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ) {
-            Handle(Geom_SphericalSurface) SS = Handle(Geom_SphericalSurface)::DownCast(S);
-            gp_Pnt PC = SS->Location();
-            BRep_Builder B;
-            TopoDS_Vertex V;
-            B.MakeVertex(V,PC,1.e-7);
-            theModifiedShape = V;
-            theAddDist = SS->Radius();
-            isModified = Standard_True;
-          }
-          else {
-            Handle(Geom_ToroidalSurface) TS = Handle(Geom_ToroidalSurface)::DownCast(S);
-            gp_Ax3 ax3 = TS->Position();
-            Handle(Geom_Circle) C = new Geom_Circle(ax3.Ax2(),TS->MajorRadius());
-            BRep_Builder B;
-            TopoDS_Edge E;
-            B.MakeEdge(E,C,1.e-7);
-            theModifiedShape = E;
-            theAddDist = TS->MinorRadius();
-            isModified = Standard_True;
-          }
+        if ( !isShell && S->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ) {
+          Handle(Geom_ToroidalSurface) TS = Handle(Geom_ToroidalSurface)::DownCast(S);
+          gp_Ax3 ax3 = TS->Position();
+          Handle(Geom_Circle) C = new Geom_Circle(ax3.Ax2(),TS->MajorRadius());
+          BRep_Builder B;
+          TopoDS_Edge E;
+          B.MakeEdge(E,C,1.e-7);
+          theModifiedShape = E;
+          theAddDist = TS->MinorRadius();
+          return Standard_True;
         }
-      } else {
-        theModifiedShape = theShape;
+
+        // non solid case or any periodic surface (Mantis 22454).
+        double U1,U2,V1,V2;
+        // changes for 0020677: EDF 1219 GEOM: MinDistance gives 0 instead of 20.88
+        //S->Bounds(U1,U2,V1,V2); changed by
+        ShapeAnalysis::GetFaceUVBounds(TopoDS::Face(theModifiedShape),U1,U2,V1,V2);
+        // end of changes for 020677 (dmv)
+        Handle(Geom_RectangularTrimmedSurface) TrS1 =
+          new Geom_RectangularTrimmedSurface(S,U1,(U1+U2)/2.,V1,V2);
+        Handle(Geom_RectangularTrimmedSurface) TrS2 =
+          new Geom_RectangularTrimmedSurface(S,(U1+U2)/2.,U2,V1,V2);
+        TopoDS_Shape aMShape;
+        
+        TopoDS_Face F1 = BRepBuilderAPI_MakeFace(TrS1, Precision::Confusion());
+        TopoDS_Face F2 = BRepBuilderAPI_MakeFace(TrS2, Precision::Confusion());
+        
+        if (isShell) {
+          BRep_Builder B;
+          B.MakeCompound(TopoDS::Compound(aMShape));
+          B.Add(aMShape, F1);
+          B.Add(aMShape, F2);
+        } else {
+          // The original shape is a solid.
+          BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
+          aSewing.Add(F1);
+          aSewing.Add(F2);
+          aSewing.Perform();
+          aMShape = aSewing.SewedShape();
+          BRep_Builder B;
+          TopoDS_Solid aSolid;
+          B.MakeSolid(aSolid);
+          B.Add(aSolid, aMShape);
+          aMShape = aSolid;
+        }
+        
+        Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
+        sfs->Init(aMShape);
+        sfs->SetPrecision(1.e-6);
+        sfs->SetMaxTolerance(1.0);
+        sfs->Perform();
+        theModifiedShape = sfs->Shape();
+        return Standard_True;
       }
     }
-    else
-      theModifiedShape = theShape;
-
-    return isModified;
+    
+    theModifiedShape = theShape;
+    return Standard_False;
   }
 
   void parseWard( const GEOMUtils::LevelsList &theLevelList, std::string &treeStr )
@@ -1039,19 +1044,19 @@ gp_Pnt GEOMUtils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
 // function : ConvertTreeToString()
 // purpose  : Returns the string representation of dependency tree
 //=======================================================================
-void GEOMUtils::ConvertTreeToString( const TreeModel &tree,
-                                     std::string &treeStr )
+void GEOMUtils::ConvertTreeToString( const TreeModeltree,
+                                     std::string& dependencyStr )
 {
   TreeModel::const_iterator i;
   for ( i = tree.begin(); i != tree.end(); ++i ) {
-    treeStr.append( i->first );
-    treeStr.append( "-" );
+    dependencyStr.append( i->first );
+    dependencyStr.append( "-" );
     std::vector<LevelInfo> upLevelList = i->second.first;
-    treeStr.append( "upward" );
-    parseWard( upLevelList, treeStr );
+    dependencyStr.append( "upward" );
+    parseWard( upLevelList, dependencyStr );
     std::vector<LevelInfo> downLevelList = i->second.second;
-    treeStr.append( "downward" );
-    parseWard( downLevelList, treeStr );
+    dependencyStr.append( "downward" );
+    parseWard( downLevelList, dependencyStr );
   }
 }
 
@@ -1059,23 +1064,113 @@ void GEOMUtils::ConvertTreeToString( const TreeModel &tree,
 // function : ConvertStringToTree()
 // purpose  : Returns the dependency tree
 //=======================================================================
-void GEOMUtils::ConvertStringToTree( const std::string &theData,
-                                     TreeModel &tree )
+void GEOMUtils::ConvertStringToTree( const std::string& dependencyStr,
+                                     TreeModeltree )
 {
   std::size_t cursor = 0;
 
-  while( theData.find('-',cursor) != std::string::npos ) //find next selected object
+  while( dependencyStr.find('-',cursor) != std::string::npos ) //find next selected object
   {
-    std::size_t objectIndex = theData.find( '-', cursor );
-    std::string objectEntry = theData.substr( cursor, objectIndex - cursor );
+    std::size_t objectIndex = dependencyStr.find( '-', cursor );
+    std::string objectEntry = dependencyStr.substr( cursor, objectIndex - cursor );
     cursor = objectIndex;
 
-    std::size_t upwardIndexBegin = theData.find("{",cursor) + 1;
-    std::size_t upwardIndexFinish = theData.find("}",upwardIndexBegin);
-    LevelsList upwardList = parseWard( theData, cursor );
+    std::size_t upwardIndexBegin = dependencyStr.find("{",cursor) + 1;
+    std::size_t upwardIndexFinish = dependencyStr.find("}",upwardIndexBegin);
+    LevelsList upwardList = parseWard( dependencyStr, cursor );
 
-    LevelsList downwardList = parseWard( theData, cursor );
+    LevelsList downwardList = parseWard( dependencyStr, cursor );
 
     tree[objectEntry] = std::pair<LevelsList,LevelsList>( upwardList, downwardList );
   }
 }
+
+bool GEOMUtils::CheckShape( TopoDS_Shape& shape,
+                            bool checkGeometry )
+{
+  BRepCheck_Analyzer analyzer( shape, checkGeometry );
+  return analyzer.IsValid();
+}
+
+bool GEOMUtils::FixShapeTolerance( TopoDS_Shape& shape,
+                                   TopAbs_ShapeEnum type,
+                                   Standard_Real tolerance,
+                                   bool checkGeometry )
+{
+  ShapeFix_ShapeTolerance aSft;
+  aSft.LimitTolerance( shape, tolerance, tolerance, type );
+  Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape( shape );
+  aSfs->Perform();
+  shape = aSfs->Shape();
+  return CheckShape( shape, checkGeometry );
+}
+
+bool GEOMUtils::FixShapeTolerance( TopoDS_Shape& shape,
+                                   Standard_Real tolerance,
+                                   bool checkGeometry )
+{
+  return FixShapeTolerance( shape, TopAbs_SHAPE, tolerance, checkGeometry );
+}
+
+bool GEOMUtils::FixShapeTolerance( TopoDS_Shape& shape,
+                                   bool checkGeometry )
+{
+  return FixShapeTolerance( shape, Precision::Confusion(), checkGeometry );
+}
+
+bool GEOMUtils::FixShapeCurves( TopoDS_Shape& shape )
+{
+  Standard_Real aT, aTolE, aD, aDMax;
+  TopExp_Explorer aExpF, aExpE;
+  NCollection_DataMap<TopoDS_Edge, Standard_Real, TopTools_ShapeMapHasher> aDMETol;
+  aExpF.Init(shape, 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();
+      try {
+        if (!BOPTools_AlgoTools::ComputeTolerance(aF, aE, aDMax, aT)) {
+          continue;
+        }
+      }
+      catch(...) {
+        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);
+      }
+    }
+  }
+  NCollection_DataMap<TopoDS_Edge, Standard_Real, TopTools_ShapeMapHasher>::Iterator aDMETolIt(aDMETol);
+#ifdef USE_LIMIT_TOLERANCE
+  ShapeFix_ShapeTolerance sat;
+#else
+  BRep_Builder b;
+#endif
+  for (; aDMETolIt.More(); aDMETolIt.Next()) {
+#ifdef USE_LIMIT_TOLERANCE
+    sat.LimitTolerance(aDMETolIt.Key(), aDMETolIt.Value()*1.001);
+#else
+    TopoDS_Iterator itv(aDMETolIt.Key());
+    for (; itv.More(); itv.Next())
+      b.UpdateVertex(TopoDS::Vertex(itv.Value()), aDMETolIt.Value()*1.001);
+    b.UpdateEdge(aDMETolIt.Key(), aDMETolIt.Value()*1.001);
+#endif
+  }
+  return CheckShape( shape );
+}
+
+bool GEOMUtils::Write( const TopoDS_Shape& shape, const char* fileName )
+{
+  return BRepTools::Write( shape, fileName );
+}