Salome HOME
Porting to DEV version of OCCT
[modules/geom.git] / src / GEOMUtils / GEOMUtils.cxx
index 89b13d96b7a6e85f536be92c25498eada9b37e80..27d89edda864307aa7c91b3f95ba1f30ea131d42 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  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
@@ -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 <BOPAlgo_ArgumentAnalyzer.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>
+
+#if OCC_VERSION_LARGE >= 0x07010000
+#include <V3d_Coordinate.hxx>
+#endif
 
 #include <Standard_Failure.hxx>
 #include <Standard_NullObject.hxx>
 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
 
+#define MAX2(X, Y)    (Abs(X) > Abs(Y) ? Abs(X) : Abs(Y))
+#define MAX3(X, Y, Z) (MAX2(MAX2(X,Y), Z))
+
 #define STD_SORT_ALGO 1
 
-namespace GEOMUtils {
+#define DEFAULT_TOLERANCE_TOLERANCE     1.e-02
+#define DEFAULT_MAX_TOLERANCE_TOLERANCE 1.e-06
+
+// 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
+{
+  /**
+   * This function constructs and returns modified shape from the original one
+   * for singular cases. It is used for the method GetMinDistanceSingular.
+   *
+   * \param theShape the original shape
+   * \param theModifiedShape output parameter. The modified shape.
+   * \param theAddDist output parameter. The added distance for modified shape.
+   * \retval true if the shape is modified; false otherwise.
+   *
+   * \internal
+   */
+  Standard_Boolean ModifyShape(const TopoDS_Shape  &theShape,
+                               TopoDS_Shape  &theModifiedShape,
+                               Standard_Real &theAddDist)
+  {
+    TopExp_Explorer anExp;
+    int nbf = 0;
+
+    theAddDist = 0.;
+    theModifiedShape.Nullify();
+
+    for ( anExp.Init( theShape, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
+      nbf++;
+      theModifiedShape = anExp.Current();
+    }
+    if(nbf==1) {
+      TopoDS_Shape sh = theShape;
+      while(sh.ShapeType()==TopAbs_COMPOUND) {
+        TopoDS_Iterator it(sh);
+        sh = it.Value();
+      }
+      Handle(Geom_Surface) S = BRep_Tool::Surface(TopoDS::Face(theModifiedShape));
+      if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
+          S->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ||
+          S->IsUPeriodic()) {
+        const Standard_Boolean isShell =
+          (sh.ShapeType()==TopAbs_SHELL || sh.ShapeType()==TopAbs_FACE);
+
+        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_Vertex V;
+          B.MakeVertex(V,PC,1.e-7);
+          theModifiedShape = V;
+          theAddDist = SS->Radius();
+          return 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;
+        }
+
+        // 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;
+      }
+    }
+    
+    theModifiedShape = theShape;
+    return Standard_False;
+  }
+
+  void parseWard( const GEOMUtils::LevelsList &theLevelList, std::string &treeStr )
+  {
+    treeStr.append( "{" );
+    for( GEOMUtils::LevelsList::const_iterator j = theLevelList.begin(); 
+         j != theLevelList.end(); ++j ) {
+      if ( j != theLevelList.begin() ) {
+        treeStr.append( ";" );
+      }
+      GEOMUtils::LevelInfo level = (*j);
+      GEOMUtils::LevelInfo::iterator upIter;
+      for ( upIter = level.begin(); upIter != level.end(); ++upIter ) {
+        if ( upIter != level.begin() ) {
+          treeStr.append( "," );
+        }
+        treeStr.append( upIter->first );
+        for ( std::vector<std::string>::iterator k = upIter->second.begin(); k != upIter->second.end(); ++k ) {
+          treeStr.append( "_" );
+          treeStr.append( *k );
+        }
+      }
+    }
+    treeStr.append( "}" );
+  }
+
+  GEOMUtils::LevelsList parseWard( const std::string& theData, std::size_t& theCursor )
+  {
+    std::size_t indexStart = theData.find( "{", theCursor ) + 1;
+    std::size_t indexEnd = theData.find( "}", indexStart );
+
+    std::string ward = theData.substr( indexStart, indexEnd - indexStart );
+    std::stringstream ss(ward);
+    std::string substr;
+    std::vector<std::string> levelsListStr;
+    while ( std::getline( ss, substr, ';' ) ) {
+      if ( !substr.empty() )
+        levelsListStr.push_back( substr );
+    }
+    GEOMUtils::LevelsList levelsListData;
+    for( size_t level = 0; level < levelsListStr.size(); level++ ) {
+      std::vector<std::string> namesListStr;
+      std::stringstream ss1( levelsListStr[level] );
+      while ( std::getline( ss1, substr, ',' ) ) {
+        if ( !substr.empty() )
+          namesListStr.push_back( substr );
+      }
+      GEOMUtils::LevelInfo levelInfoData;
+      for( size_t node = 0; node < namesListStr.size(); node++ ) {
+        std::vector<std::string> linksListStr;
+        std::stringstream ss2( namesListStr[node] );
+        while ( std::getline( ss2, substr, '_' ) ) {
+          if ( !substr.empty() )
+            linksListStr.push_back( substr );
+        }
+        std::string nodeItem = linksListStr[0];
+        if( !nodeItem.empty() ) {
+          GEOMUtils::NodeLinks linksListData;
+          for( size_t link = 1; link < linksListStr.size(); link++ ) {
+            std::string linkItem = linksListStr[link];
+            linksListData.push_back( linkItem );
+          }// Links
+          levelInfoData[nodeItem] = linksListData;
+        }
+      }// Level's objects
+      levelsListData.push_back(levelInfoData);
+    }// Levels
+
+    theCursor = indexEnd + 1;
+    return levelsListData;
+  }
+
+}
+
+//=======================================================================
+//function : ShapeToDouble
+//purpose  : used by CompareShapes::operator()
+//=======================================================================
+std::pair<double, double> GEOMUtils::ShapeToDouble (const TopoDS_Shape& S, bool isOldSorting)
+{
+  // Computing of CentreOfMass
+  gp_Pnt GPoint;
+  double Len;
+
+  if (S.ShapeType() == TopAbs_VERTEX) {
+    GPoint = BRep_Tool::Pnt(TopoDS::Vertex(S));
+    Len = (double)S.Orientation();
+  }
+  else {
+    GProp_GProps GPr;
+    // BEGIN: fix for Mantis issue 0020842
+    if (isOldSorting) {
+      BRepGProp::LinearProperties(S, GPr);
+    }
+    else {
+      if (S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE) {
+        BRepGProp::LinearProperties(S, GPr);
+      }
+      else if (S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL) {
+        BRepGProp::SurfaceProperties(S, GPr);
+      }
+      else {
+        BRepGProp::VolumeProperties(S, GPr);
+      }
+    }
+    // END: fix for Mantis issue 0020842
+    GPoint = GPr.CentreOfMass();
+    Len = GPr.Mass();
+  }
+
+  double dMidXYZ = GPoint.X() * 999.0 + GPoint.Y() * 99.0 + GPoint.Z() * 0.9;
+  return std::make_pair(dMidXYZ, Len);
+}
 
 //=======================================================================
 //function : GetPosition
 //purpose  :
 //=======================================================================
-gp_Ax3 GetPosition (const TopoDS_Shape& theShape)
+gp_Ax3 GEOMUtils::GetPosition (const TopoDS_Shape& theShape)
 {
   gp_Ax3 aResult;
 
@@ -161,7 +404,7 @@ gp_Ax3 GetPosition (const TopoDS_Shape& theShape)
 //function : GetVector
 //purpose  :
 //=======================================================================
-gp_Vec GetVector (const TopoDS_Shape& theShape,
+gp_Vec GEOMUtils::GetVector (const TopoDS_Shape& theShape,
                              Standard_Boolean doConsiderOrientation)
 {
   if (theShape.IsNull())
@@ -186,51 +429,11 @@ gp_Vec GetVector (const TopoDS_Shape& theShape,
   return aV;
 }
 
-//=======================================================================
-//function : ShapeToDouble
-//purpose  : used by CompareShapes::operator()
-//=======================================================================
-std::pair<double, double> ShapeToDouble (const TopoDS_Shape& S, bool isOldSorting)
-{
-  // Computing of CentreOfMass
-  gp_Pnt GPoint;
-  double Len;
-
-  if (S.ShapeType() == TopAbs_VERTEX) {
-    GPoint = BRep_Tool::Pnt(TopoDS::Vertex(S));
-    Len = (double)S.Orientation();
-  }
-  else {
-    GProp_GProps GPr;
-    // BEGIN: fix for Mantis issue 0020842
-    if (isOldSorting) {
-      BRepGProp::LinearProperties(S, GPr);
-    }
-    else {
-      if (S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE) {
-        BRepGProp::LinearProperties(S, GPr);
-      }
-      else if (S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SHELL) {
-        BRepGProp::SurfaceProperties(S, GPr);
-      }
-      else {
-        BRepGProp::VolumeProperties(S, GPr);
-      }
-    }
-    // END: fix for Mantis issue 0020842
-    GPoint = GPr.CentreOfMass();
-    Len = GPr.Mass();
-  }
-
-  double dMidXYZ = GPoint.X() * 999.0 + GPoint.Y() * 99.0 + GPoint.Z() * 0.9;
-  return std::make_pair(dMidXYZ, Len);
-}
-
 //=======================================================================
 //function : CompareShapes::operator()
 //purpose  : used by std::sort(), called from SortShapes()
 //=======================================================================
-bool CompareShapes::operator() (const TopoDS_Shape& theShape1,
+bool GEOMUtils::CompareShapes::operator() (const TopoDS_Shape& theShape1,
                                            const TopoDS_Shape& theShape2)
 {
   if (!myMap.IsBound(theShape1)) {
@@ -290,7 +493,7 @@ bool CompareShapes::operator() (const TopoDS_Shape& theShape1,
 //function : SortShapes
 //purpose  :
 //=======================================================================
-void SortShapes (TopTools_ListOfShape& SL,
+void GEOMUtils::SortShapes (TopTools_ListOfShape& SL,
                             const Standard_Boolean isOldSorting)
 {
 #ifdef STD_SORT_ALGO
@@ -428,7 +631,7 @@ void SortShapes (TopTools_ListOfShape& SL,
 //function : CompsolidToCompound
 //purpose  :
 //=======================================================================
-TopoDS_Shape CompsolidToCompound (const TopoDS_Shape& theCompsolid)
+TopoDS_Shape GEOMUtils::CompsolidToCompound (const TopoDS_Shape& theCompsolid)
 {
   if (theCompsolid.ShapeType() != TopAbs_COMPSOLID) {
     return theCompsolid;
@@ -455,7 +658,7 @@ TopoDS_Shape CompsolidToCompound (const TopoDS_Shape& theCompsolid)
 //function : AddSimpleShapes
 //purpose  :
 //=======================================================================
-void AddSimpleShapes (const TopoDS_Shape& theShape, TopTools_ListOfShape& theList)
+void GEOMUtils::AddSimpleShapes (const TopoDS_Shape& theShape, TopTools_ListOfShape& theList)
 {
   if (theShape.ShapeType() != TopAbs_COMPOUND &&
       theShape.ShapeType() != TopAbs_COMPSOLID) {
@@ -483,7 +686,7 @@ void AddSimpleShapes (const TopoDS_Shape& theShape, TopTools_ListOfShape& theLis
 //function : CheckTriangulation
 //purpose  :
 //=======================================================================
-bool CheckTriangulation (const TopoDS_Shape& aShape)
+bool GEOMUtils::CheckTriangulation (const TopoDS_Shape& aShape)
 {
   bool isTriangulation = true;
 
@@ -533,7 +736,7 @@ bool CheckTriangulation (const TopoDS_Shape& aShape)
 //function : GetTypeOfSimplePart
 //purpose  :
 //=======================================================================
-TopAbs_ShapeEnum GetTypeOfSimplePart (const TopoDS_Shape& theShape)
+TopAbs_ShapeEnum GEOMUtils::GetTypeOfSimplePart (const TopoDS_Shape& theShape)
 {
   TopAbs_ShapeEnum aType = theShape.ShapeType();
   if      (aType == TopAbs_VERTEX)                             return TopAbs_VERTEX;
@@ -554,7 +757,7 @@ TopAbs_ShapeEnum GetTypeOfSimplePart (const TopoDS_Shape& theShape)
 //function : GetEdgeNearPoint
 //purpose  :
 //=======================================================================
-TopoDS_Shape GetEdgeNearPoint (const TopoDS_Shape& theShape,
+TopoDS_Shape GEOMUtils::GetEdgeNearPoint (const TopoDS_Shape& theShape,
                                           const TopoDS_Vertex& thePoint)
 {
   TopoDS_Shape aResult;
@@ -622,11 +825,13 @@ TopoDS_Shape GetEdgeNearPoint (const TopoDS_Shape& theShape,
 //function : PreciseBoundingBox
 //purpose  : 
 //=======================================================================
-Standard_Boolean PreciseBoundingBox
+Standard_Boolean GEOMUtils::PreciseBoundingBox
                           (const TopoDS_Shape &theShape, Bnd_Box &theBox)
 {
-  Standard_Real aBound[6];
+  if ( theBox.IsVoid() ) BRepBndLib::Add( theShape, theBox );
+  if ( theBox.IsVoid() ) return Standard_False;
 
+  Standard_Real aBound[6];
   theBox.Get(aBound[0], aBound[2], aBound[4], aBound[1], aBound[3], aBound[5]);
 
   Standard_Integer i;
@@ -658,7 +863,7 @@ Standard_Boolean PreciseBoundingBox
     const Standard_Integer iHalf = i/2;
     const gp_Pln aPln(aPnt[i], aDir[iHalf]);
     BRepBuilderAPI_MakeFace aMkFace(aPln, -aPlnSize[iHalf], aPlnSize[iHalf],
-                                          -aPlnSize[iHalf], aPlnSize[iHalf]);
+                                    -aPlnSize[iHalf], aPlnSize[iHalf]);
 
     if (!aMkFace.IsDone()) {
       return Standard_False;
@@ -668,7 +873,7 @@ Standard_Boolean PreciseBoundingBox
 
     // Get minimal distance between planar face and shape.
     Standard_Real aMinDist =
-      GetMinDistance(aFace, theShape, aPMin[0], aPMin[1]);
+      GEOMUtils::GetMinDistance(aFace, theShape, aPMin[0], aPMin[1]);
 
     if (aMinDist < 0.) {
       return Standard_False;
@@ -684,128 +889,11 @@ Standard_Boolean PreciseBoundingBox
   return Standard_True;
 }
 
-//=======================================================================
-// function : ModifyShape
-// purpose  : This function constructs and returns modified shape 
-//            from the original one for singular cases. 
-//            It is used for the method GetMinDistanceSingular.
-//   
-//   \param theShape the original shape
-//   \param theModifiedShape output parameter. The modified shape.
-//   \param theAddDist output parameter. The added distance for modified shape.
-//   \retval true if the shape is modified; false otherwise.
-//   
-
-//=======================================================================
-Standard_Boolean ModifyShape(const TopoDS_Shape  &theShape,
-                                              TopoDS_Shape  &theModifiedShape,
-                                              Standard_Real &theAddDist)
-{
-  Standard_Boolean isModified = Standard_False;
-  TopExp_Explorer anExp;
-  int nbf = 0;
-
-  theAddDist = 0.;
-  theModifiedShape.Nullify();
-
-  for ( anExp.Init( theShape, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
-    nbf++;
-    theModifiedShape = anExp.Current();
-  }
-  if(nbf==1) {
-    TopoDS_Shape sh = theShape;
-    while(sh.ShapeType()==TopAbs_COMPOUND) {
-      TopoDS_Iterator it(sh);
-      sh = it.Value();
-    }
-    Handle(Geom_Surface) S = BRep_Tool::Surface(TopoDS::Face(theModifiedShape));
-    if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
-        S->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ||
-        S->IsUPeriodic()) {
-      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);
-        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;
-      }
-      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;
-        }
-      }
-    } else {
-      theModifiedShape = theShape;
-    }
-  }
-  else
-    theModifiedShape = theShape;
-
-  return isModified;
-}
-
 //=======================================================================
 //function : GetMinDistanceSingular
 //purpose  : 
 //=======================================================================
-double GetMinDistanceSingular(const TopoDS_Shape& aSh1,
+double GEOMUtils::GetMinDistanceSingular(const TopoDS_Shape& aSh1,
                                          const TopoDS_Shape& aSh2,
                                          gp_Pnt& Ptmp1, gp_Pnt& Ptmp2)
 {
@@ -874,7 +962,7 @@ double GetMinDistanceSingular(const TopoDS_Shape& aSh1,
 //function : GetMinDistance
 //purpose  : 
 //=======================================================================
-Standard_Real GetMinDistance
+Standard_Real GEOMUtils::GetMinDistance
                                (const TopoDS_Shape& theShape1,
                                 const TopoDS_Shape& theShape2,
                                 gp_Pnt& thePnt1, gp_Pnt& thePnt2)
@@ -910,7 +998,7 @@ Standard_Real GetMinDistance
 
   // skl 30.06.2008
   // additional workaround for bugs 19899, 19908 and 19910 from Mantis
-  double dist = GetMinDistanceSingular
+  double dist = GEOMUtils::GetMinDistanceSingular
       (theShape1, theShape2, thePnt1, thePnt2);
 
   if (dist > -1.0) {
@@ -941,7 +1029,7 @@ Standard_Real GetMinDistance
 // function : ConvertClickToPoint()
 // purpose  : Returns the point clicked in 3D view
 //=======================================================================
-gp_Pnt ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
+gp_Pnt GEOMUtils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
 {
   V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
   aView->Eye( XEye, YEye, ZEye );
@@ -963,119 +1051,322 @@ gp_Pnt ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
   return ResultPoint;
 }
 
-void parseWard( const LevelsList &theLevelList, std::string &treeStr )
-{
-  treeStr.append( "{" );
-  for( LevelsList::const_iterator j = theLevelList.begin(); 
-       j != theLevelList.end(); ++j ) {
-    if ( j != theLevelList.begin() ) {
-      treeStr.append( ";" );
-    }
-    LevelInfo level = (*j);
-    LevelInfo::iterator upIter;
-    for ( upIter = level.begin(); upIter != level.end(); ++upIter ) {
-      if ( upIter != level.begin() ) {
-       treeStr.append( "," );
-      }
-      treeStr.append( upIter->first );
-      for ( std::vector<std::string>::iterator k = upIter->second.begin(); k != upIter->second.end(); ++k ) {
-       treeStr.append( "_" );
-       treeStr.append( *k );
-      }
-    }
-  }
-  treeStr.append( "}" );
-}
-
 //=======================================================================
 // function : ConvertTreeToString()
 // purpose  : Returns the string representation of dependency tree
 //=======================================================================
-void ConvertTreeToString( const TreeModel &tree,
-                         std::string &treeStr )
+void GEOMUtils::ConvertTreeToString( const TreeModel& tree,
+                                     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 );
   }
 }
 
-LevelsList parseWard( const std::string& theData, std::size_t& theCursor )
+//=======================================================================
+// function : ConvertStringToTree()
+// purpose  : Returns the dependency tree
+//=======================================================================
+void GEOMUtils::ConvertStringToTree( const std::string& dependencyStr,
+                                     TreeModel& tree )
 {
-  std::size_t indexStart = theData.find( "{", theCursor ) + 1;
-  std::size_t indexEnd = theData.find( "}", indexStart );
-
-  std::string ward = theData.substr( indexStart, indexEnd - indexStart );
-  std::stringstream ss(ward);
-  std::string substr;
-  std::vector<std::string> levelsListStr;
-  while ( std::getline( ss, substr, ';' ) ) {
-    if ( !substr.empty() )
-      levelsListStr.push_back( substr );
+  std::size_t cursor = 0;
+
+  while( dependencyStr.find('-',cursor) != std::string::npos ) //find next selected object
+  {
+    std::size_t objectIndex = dependencyStr.find( '-', cursor );
+    std::string objectEntry = dependencyStr.substr( cursor, objectIndex - cursor );
+    cursor = objectIndex;
+
+    std::size_t upwardIndexBegin = dependencyStr.find("{",cursor) + 1;
+    //std::size_t upwardIndexFinish = dependencyStr.find("}",upwardIndexBegin);
+    LevelsList upwardList = parseWard( dependencyStr, cursor );
+
+    LevelsList downwardList = parseWard( dependencyStr, cursor );
+
+    tree[objectEntry] = std::pair<LevelsList,LevelsList>( upwardList, downwardList );
   }
-  LevelsList levelsListData;
-  for( int level = 0; level < levelsListStr.size(); level++ ) {
-    std::vector<std::string> namesListStr;
-    std::stringstream ss1( levelsListStr[level] );
-    while ( std::getline( ss1, substr, ',' ) ) {
-      if ( !substr.empty() )
-       namesListStr.push_back( substr );
-    }
-    LevelInfo levelInfoData;
-    for( int node = 0; node < namesListStr.size(); node++ ) {
-      std::vector<std::string> linksListStr;
-      std::stringstream ss2( namesListStr[node] );
-      while ( std::getline( ss2, substr, '_' ) ) {
-       if ( !substr.empty() )
-         linksListStr.push_back( substr );
+}
+
+bool GEOMUtils::CheckShape( TopoDS_Shape& shape,
+                            bool checkGeometry )
+{
+  BRepCheck_Analyzer analyzer( shape, checkGeometry );
+  return analyzer.IsValid();
+}
+
+bool GEOMUtils::CheckBOPArguments(const TopoDS_Shape &theShape)
+{
+  BOPAlgo_ArgumentAnalyzer aChecker;
+
+  aChecker.SetShape1(theShape);
+
+  // Set default options
+  aChecker.ArgumentTypeMode()   = Standard_True;
+  aChecker.SelfInterMode()      = Standard_True;
+  aChecker.SmallEdgeMode()      = Standard_True;
+  aChecker.RebuildFaceMode()    = Standard_True;
+  aChecker.ContinuityMode()     = Standard_True;
+  aChecker.CurveOnSurfaceMode() = Standard_True;
+
+  aChecker.StopOnFirstFaulty() = Standard_True;
+  aChecker.Perform();
+
+  // process result of checking
+  const bool isValid = !aChecker.HasFaulty();
+
+  return 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;
+        }
       }
-      std::string nodeItem = linksListStr[0];
-      if( !nodeItem.empty() ) {
-        NodeLinks linksListData;
-        for( int link = 1; link < linksListStr.size(); link++ ) {
-          std::string linkItem = linksListStr[link];
-          linksListData.push_back( linkItem );
-        }// Links
-        levelInfoData[nodeItem] = linksListData;
+      catch(...) {
+        continue;
       }
-    }// Level's objects
-    levelsListData.push_back(levelInfoData);
-  }// Levels
+      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 );
+}
+
+TopoDS_Shape GEOMUtils::ReduceCompound( const TopoDS_Shape& shape )
+{
+  TopoDS_Shape result = shape;
+
+  if ( shape.ShapeType() == TopAbs_COMPOUND ||
+       shape.ShapeType() == TopAbs_COMPSOLID ) {
+
+    TopTools_ListOfShape l;
+    
+    TopoDS_Iterator it ( shape );
+    for ( ; it.More(); it.Next() )
+      l.Append( it.Value() );
+    if ( l.Extent() == 1 && l.First() != shape )
+      result = ReduceCompound( l.First() );
+  }
+  
+  return result;
+}
 
-  theCursor = indexEnd + 1;
-  return levelsListData;
+void GEOMUtils::MeshShape( const TopoDS_Shape shape,
+                           double deflection, bool theForced )
+{
+  Standard_Real aDeflection = ( deflection <= 0 ) ? DefaultDeflection() : deflection;
+  
+  // Is shape triangulated?
+  Standard_Boolean alreadyMeshed = true;
+  TopExp_Explorer ex;
+  TopLoc_Location aLoc;
+  for ( ex.Init( shape, TopAbs_FACE ); ex.More() && alreadyMeshed; ex.Next() ) {
+    const TopoDS_Face& aFace = TopoDS::Face( ex.Current() );
+    Handle(Poly_Triangulation) aPoly = BRep_Tool::Triangulation( aFace, aLoc );
+    alreadyMeshed = !aPoly.IsNull(); 
+  }
+  
+  if ( !alreadyMeshed || theForced ) {
+    // Compute bounding box
+    Bnd_Box B;
+    BRepBndLib::Add( shape, B );
+    if ( B.IsVoid() )
+      return; // NPAL15983 (Bug when displaying empty groups) 
+    Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
+    B.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
+    
+    // This magic line comes from Prs3d_ShadedShape.gxx in OCCT
+    aDeflection = MAX3(aXmax-aXmin, aYmax-aYmin, aZmax-aZmin) * aDeflection * 4;
+    
+    // Clean triangulation before compute incremental mesh
+    BRepTools::Clean( shape );
+    
+    // Compute triangulation
+    BRepMesh_IncrementalMesh mesh( shape, aDeflection ); 
+  }
+}
+
+double GEOMUtils::DefaultDeflection()
+{
+  return 0.001;
 }
 
 //=======================================================================
-// function : ConvertStringToTree()
-// purpose  : Returns the dependency tree
+//function : IsOpenPath
+//purpose  : 
 //=======================================================================
-void ConvertStringToTree( const std::string &theData,
-                         TreeModel &tree )
+bool GEOMUtils::IsOpenPath(const TopoDS_Shape &theShape)
 {
-  std::size_t cursor = 0;
+  bool isOpen = true;
 
-  while( theData.find('-',cursor) != std::string::npos ) //find next selected object
-  {
-    std::size_t objectIndex = theData.find( '-', cursor );
-    std::string objectEntry = theData.substr( cursor, objectIndex - cursor );
-    cursor = objectIndex;
+  if (theShape.IsNull() == Standard_False) {
+    if (theShape.Closed()) {
+      // The shape is closed
+      isOpen = false;
+    } else {
+      const TopAbs_ShapeEnum aType = theShape.ShapeType();
 
-    std::size_t upwardIndexBegin = theData.find("{",cursor) + 1;
-    std::size_t upwardIndexFinish = theData.find("}",upwardIndexBegin);
-    LevelsList upwardList = parseWard( theData, cursor );
+      if (aType == TopAbs_EDGE || aType == TopAbs_WIRE) {
+        // Check if path ends are coinsident.
+        TopoDS_Vertex aV[2];
 
-    LevelsList downwardList = parseWard( theData, cursor );
+        if (aType == TopAbs_EDGE) {
+          // Edge
+          TopExp::Vertices(TopoDS::Edge(theShape), aV[0], aV[1]);
+        } else {
+          // Wire
+          TopExp::Vertices(TopoDS::Wire(theShape), aV[0], aV[1]);
+        }
 
-    tree[objectEntry] = std::pair<LevelsList,LevelsList>( upwardList, downwardList );
+        if (aV[0].IsNull() == Standard_False &&
+            aV[1].IsNull() == Standard_False) {
+          if (aV[0].IsSame(aV[1])) {
+            // The shape is closed
+            isOpen = false;
+          } else {
+            const Standard_Real aTol1 = BRep_Tool::Tolerance(aV[0]);
+            const Standard_Real aTol2 = BRep_Tool::Tolerance(aV[1]);
+            const gp_Pnt        aPnt1 = BRep_Tool::Pnt(aV[0]);
+            const gp_Pnt        aPnt2 = BRep_Tool::Pnt(aV[1]);
+
+            if (aPnt1.Distance(aPnt2) <= aTol1 + aTol2) {
+              // The shape is closed
+              isOpen = false;
+            }
+          }
+        }
+      }
+    }
   }
+
+  return isOpen;
 }
 
-} //namespace GEOMUtils
+//=======================================================================
+//function : CompareToleranceValues
+//purpose  : 
+//=======================================================================
+int GEOMUtils::CompareToleranceValues(const double theTolShape,
+                                      const double theTolRef)
+{
+  const double aTolTol = Min(DEFAULT_MAX_TOLERANCE_TOLERANCE,
+                             theTolRef*DEFAULT_TOLERANCE_TOLERANCE);
+
+  int aResult = 0;
+
+  if (theTolShape < theTolRef - aTolTol) {
+    aResult = -1;
+  } else if (theTolShape > theTolRef + aTolTol) {
+    aResult = 1;
+  }
+
+  return aResult;
+}
+
+//=======================================================================
+//function : IsFitCondition
+//purpose  : 
+//=======================================================================
+bool GEOMUtils::IsFitCondition(const ComparisonCondition theCondition,
+                               const double              theTolShape,
+                               const double              theTolRef)
+{
+  const int aCompValue = CompareToleranceValues(theTolShape, theTolRef);
+  bool      isFit      = false;
+
+  switch (theCondition) {
+    case CC_GT:
+      isFit = aCompValue == 1;
+      break;
+    case GEOMUtils::CC_GE:
+      isFit = aCompValue != -1;
+      break;
+    case GEOMUtils::CC_LT:
+      isFit = aCompValue == -1;
+      break;
+    case GEOMUtils::CC_LE:
+      isFit = aCompValue != 1;
+      break;
+    default:
+      break;
+  }
+
+  return isFit;
+}