Salome HOME
Fix compilation errors using gcc-5.X relating to explicit stream::operator bool()
[plugins/blsurfplugin.git] / src / BLSURFPlugin / BLSURFPlugin_BLSURF.cxx
index 7624e36a40ce9beef0f8af6e350d677f08f451e7..fbb7a8f20d418d769f4a4fefdb5e9528bbce3aa1 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2015  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -49,6 +49,7 @@ extern "C"{
 #include <SMESH_MesherHelper.hxx>
 #include <StdMeshers_FaceSide.hxx>
 #include <StdMeshers_ViscousLayers2D.hxx>
+#include <SMESH_File.hxx>
 
 #include <utilities.h>
 
@@ -99,102 +100,103 @@ extern "C"{
  * ===========  PYTHON ==============
  * ==================================*/
 
-typedef struct {
-  PyObject_HEAD
-  int softspace;
-  std::string *out;
-  } PyStdOut;
-
-static void
-PyStdOut_dealloc(PyStdOut *self)
+namespace
 {
-  PyObject_Del(self);
-}
+  typedef struct {
+    PyObject_HEAD
+    int softspace;
+    std::string *out;
+    } PyStdOut;
+
+  static void
+  PyStdOut_dealloc(PyStdOut *self)
+  {
+    PyObject_Del(self);
+  }
 
-static PyObject *
-PyStdOut_write(PyStdOut *self, PyObject *args)
-{
-  char *c;
-  int l;
-  if (!PyArg_ParseTuple(args, "t#:write",&c, &l))
-    return NULL;
+  static PyObject *
+  PyStdOut_write(PyStdOut *self, PyObject *args)
+  {
+    char *c;
+    int l;
+    if (!PyArg_ParseTuple(args, "t#:write",&c, &l))
+      return NULL;
 
-  //std::cerr << c ;
-  *(self->out)=*(self->out)+c;
+    *(self->out)=*(self->out)+c;
 
-  Py_INCREF(Py_None);
-  return Py_None;
-}
+    Py_INCREF(Py_None);
+    return Py_None;
+  }
 
-static PyMethodDef PyStdOut_methods[] = {
-  {"write",  (PyCFunction)PyStdOut_write,  METH_VARARGS,
-    PyDoc_STR("write(string) -> None")},
-  {NULL,    NULL}   /* sentinel */
-};
-
-static PyMemberDef PyStdOut_memberlist[] = {
-  {(char*)"softspace", T_INT,  offsetof(PyStdOut, softspace), 0,
-   (char*)"flag indicating that a space needs to be printed; used by print"},
-  {NULL} /* Sentinel */
-};
-
-static PyTypeObject PyStdOut_Type = {
-  /* The ob_type field must be initialized in the module init function
-   * to be portable to Windows without using C++. */
-  PyObject_HEAD_INIT(NULL)
-  0,                            /*ob_size*/
-  "PyOut",                      /*tp_name*/
-  sizeof(PyStdOut),             /*tp_basicsize*/
-  0,                            /*tp_itemsize*/
-  /* methods */
-  (destructor)PyStdOut_dealloc, /*tp_dealloc*/
-  0,                            /*tp_print*/
-  0,                            /*tp_getattr*/
-  0,                            /*tp_setattr*/
-  0,                            /*tp_compare*/
-  0,                            /*tp_repr*/
-  0,                            /*tp_as_number*/
-  0,                            /*tp_as_sequence*/
-  0,                            /*tp_as_mapping*/
-  0,                            /*tp_hash*/
-  0,                            /*tp_call*/
-  0,                            /*tp_str*/
-  PyObject_GenericGetAttr,      /*tp_getattro*/
-  /* softspace is writable:  we must supply tp_setattro */
-  PyObject_GenericSetAttr,      /* tp_setattro */
-  0,                            /*tp_as_buffer*/
-  Py_TPFLAGS_DEFAULT,           /*tp_flags*/
-  0,                            /*tp_doc*/
-  0,                            /*tp_traverse*/
-  0,                            /*tp_clear*/
-  0,                            /*tp_richcompare*/
-  0,                            /*tp_weaklistoffset*/
-  0,                            /*tp_iter*/
-  0,                            /*tp_iternext*/
-  PyStdOut_methods,             /*tp_methods*/
-  PyStdOut_memberlist,          /*tp_members*/
-  0,                            /*tp_getset*/
-  0,                            /*tp_base*/
-  0,                            /*tp_dict*/
-  0,                            /*tp_descr_get*/
-  0,                            /*tp_descr_set*/
-  0,                            /*tp_dictoffset*/
-  0,                            /*tp_init*/
-  0,                            /*tp_alloc*/
-  0,                            /*tp_new*/
-  0,                            /*tp_free*/
-  0,                            /*tp_is_gc*/
-};
-
-PyObject * newPyStdOut( std::string& out )
-{
-  PyStdOut *self;
-  self = PyObject_New(PyStdOut, &PyStdOut_Type);
-  if (self == NULL)
-    return NULL;
-  self->softspace = 0;
-  self->out=&out;
-  return (PyObject*)self;
+  static PyMethodDef PyStdOut_methods[] = {
+    {"write",  (PyCFunction)PyStdOut_write,  METH_VARARGS,
+      PyDoc_STR("write(string) -> None")},
+    {NULL,    NULL}   /* sentinel */
+  };
+
+  static PyMemberDef PyStdOut_memberlist[] = {
+    {(char*)"softspace", T_INT,  offsetof(PyStdOut, softspace), 0,
+     (char*)"flag indicating that a space needs to be printed; used by print"},
+    {NULL} /* Sentinel */
+  };
+
+  static PyTypeObject PyStdOut_Type = {
+    /* The ob_type field must be initialized in the module init function
+     * to be portable to Windows without using C++. */
+    PyObject_HEAD_INIT(NULL)
+    0,                            /*ob_size*/
+    "PyOut",                      /*tp_name*/
+    sizeof(PyStdOut),             /*tp_basicsize*/
+    0,                            /*tp_itemsize*/
+    /* methods */
+    (destructor)PyStdOut_dealloc, /*tp_dealloc*/
+    0,                            /*tp_print*/
+    0,                            /*tp_getattr*/
+    0,                            /*tp_setattr*/
+    0,                            /*tp_compare*/
+    0,                            /*tp_repr*/
+    0,                            /*tp_as_number*/
+    0,                            /*tp_as_sequence*/
+    0,                            /*tp_as_mapping*/
+    0,                            /*tp_hash*/
+    0,                            /*tp_call*/
+    0,                            /*tp_str*/
+    PyObject_GenericGetAttr,      /*tp_getattro*/
+    /* softspace is writable:  we must supply tp_setattro */
+    PyObject_GenericSetAttr,      /* tp_setattro */
+    0,                            /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT,           /*tp_flags*/
+    0,                            /*tp_doc*/
+    0,                            /*tp_traverse*/
+    0,                            /*tp_clear*/
+    0,                            /*tp_richcompare*/
+    0,                            /*tp_weaklistoffset*/
+    0,                            /*tp_iter*/
+    0,                            /*tp_iternext*/
+    PyStdOut_methods,             /*tp_methods*/
+    PyStdOut_memberlist,          /*tp_members*/
+    0,                            /*tp_getset*/
+    0,                            /*tp_base*/
+    0,                            /*tp_dict*/
+    0,                            /*tp_descr_get*/
+    0,                            /*tp_descr_set*/
+    0,                            /*tp_dictoffset*/
+    0,                            /*tp_init*/
+    0,                            /*tp_alloc*/
+    0,                            /*tp_new*/
+    0,                            /*tp_free*/
+    0,                            /*tp_is_gc*/
+  };
+
+  PyObject * newPyStdOut( std::string& out )
+  {
+    PyStdOut* self = PyObject_New(PyStdOut, &PyStdOut_Type);
+    if (self) {
+      self->softspace = 0;
+      self->out=&out;
+    }
+    return (PyObject*)self;
+  }
 }
 
 
@@ -241,7 +243,7 @@ BLSURFPlugin_BLSURF::BLSURFPlugin_BLSURF(int hypId, int studyId,
 {
   MESSAGE("BLSURFPlugin_BLSURF::BLSURFPlugin_BLSURF");
 
-  _name = "MG_CADSurf";//"BLSURF";
+  _name = "MG-CADSurf";//"BLSURF";
   _shapeType = (1 << TopAbs_FACE); // 1 bit /shape type
   _compatibleHypothesis.push_back(BLSURFPlugin_Hypothesis::GetHypType());
   _compatibleHypothesis.push_back(StdMeshers_ViscousLayers2D::GetHypType());
@@ -363,18 +365,18 @@ bool BLSURFPlugin_BLSURF::CheckHypothesis
 
 //=============================================================================
 /*!
- * Pass parameters to BLSURF
+ * Pass parameters to MG-CADSurf
  */
 //=============================================================================
 
-inline std::string to_string(double d)
+inline std::string val_to_string(double d)
 {
    std::ostringstream o;
    o << d;
    return o.str();
 }
 
-inline std::string to_string_rel(double d)
+inline std::string val_to_string_rel(double d)
 {
    std::ostringstream o;
    o << d;
@@ -382,14 +384,14 @@ inline std::string to_string_rel(double d)
    return o.str();
 }
 
-inline std::string to_string(int i)
+inline std::string val_to_string(int i)
 {
    std::ostringstream o;
    o << i;
    return o.str();
 }
 
-inline std::string to_string_rel(int i)
+inline std::string val_to_string_rel(int i)
 {
    std::ostringstream o;
    o << i;
@@ -398,12 +400,6 @@ inline std::string to_string_rel(int i)
 }
 
 double _smp_phy_size;
-// #if BLSURF_VERSION_LONG >= "3.1.1"
-// //   sizemap_t *geo_sizemap_e, *geo_sizemap_f;
-//   sizemap_t *iso_sizemap_p, *iso_sizemap_e, *iso_sizemap_f;
-// //   sizemap_t *clean_geo_sizemap_e, *clean_geo_sizemap_f;
-//   sizemap_t *clean_iso_sizemap_p, *clean_iso_sizemap_e, *clean_iso_sizemap_f;
-// #endif
 status_t size_on_surface(integer face_id, real *uv, real *size, void *user_data);
 status_t size_on_edge(integer edge_id, real t, real *size, void *user_data);
 status_t size_on_vertex(integer vertex_id, real *size, void *user_data);
@@ -721,6 +717,23 @@ BLSURFPlugin_BLSURF::TListOfIDs _getSubShapeIDsInMainShape(TopoDS_Shape theMainS
   return face_ids;
 }
 
+BLSURFPlugin_BLSURF::TListOfIDs _getSubShapeIDsInMainShape(SMESH_Mesh*      theMesh,
+                                                           TopoDS_Shape     theSubShape,
+                                                           TopAbs_ShapeEnum theShapeType)
+{
+  BLSURFPlugin_BLSURF::TListOfIDs face_ids;
+
+  for (TopExp_Explorer face_iter(theSubShape,theShapeType);face_iter.More();face_iter.Next())
+  {
+    int face_id = theMesh->GetMeshDS()->ShapeToIndex(face_iter.Current());
+    if (face_id == 0)
+      throw SALOME_Exception ( SMESH_Comment("Sub_shape not found in main_shape"));
+    face_ids.push_back(face_id);
+  }
+
+  return face_ids;
+}
+
 void BLSURFPlugin_BLSURF::addCoordsFromVertices(const std::vector<std::string> &theVerticesEntries, std::vector<double> &theVerticesCoords)
 {
   for (std::vector<std::string>::const_iterator it = theVerticesEntries.begin(); it != theVerticesEntries.end(); it++)
@@ -885,7 +898,7 @@ void BLSURFPlugin_BLSURF::createEdgesPeriodicity(TopoDS_Shape theGeomShape, BLSU
 
 /////////////////////////////////////////////////////////
 void BLSURFPlugin_BLSURF::createVerticesPeriodicity(TopoDS_Shape theGeomShape, BLSURFPlugin_Hypothesis::TEntry theEdge1, BLSURFPlugin_Hypothesis::TEntry theVertex1,
-    BLSURFPlugin_Hypothesis::TEntry theEdge2, BLSURFPlugin_Hypothesis::TEntry theVertex2)
+                                                    BLSURFPlugin_Hypothesis::TEntry theEdge2, BLSURFPlugin_Hypothesis::TEntry theVertex2)
 {
   MESSAGE("BLSURFPlugin_BLSURF::createVerticesPeriodicity");
 
@@ -914,50 +927,46 @@ void BLSURFPlugin_BLSURF::createVerticesPeriodicity(TopoDS_Shape theGeomShape, B
 
 /////////////////////////////////////////////////////////
 
-void BLSURFPlugin_BLSURF::SetParameters(
-// #if BLSURF_VERSION_LONG >= "3.1.1"
-//                                         cad_t *                          c,
-// #endif
-                                        const BLSURFPlugin_Hypothesis* hyp,
+void BLSURFPlugin_BLSURF::SetParameters(const BLSURFPlugin_Hypothesis* hyp,
                                         cadsurf_session_t *            css,
                                         precad_session_t *             pcs,
                                         const TopoDS_Shape&            theGeomShape,
-                                        bool *                  use_precad
-                                       )
+                                        bool *                         use_precad
+                                        )
 {
   // rnc : Bug 1457
   // Clear map so that it is not stored in the algorithm with old enforced vertices in it
   EnfVertexCoords2EnfVertexList.clear();
-  
-   double diagonal               = SMESH_Mesh::GetShapeDiagonalSize( theGeomShape );
-   double bbSegmentation         = _gen->GetBoundaryBoxSegmentation();
-   int    _physicalMesh          = BLSURFPlugin_Hypothesis::GetDefaultPhysicalMesh();
-   int    _geometricMesh         = BLSURFPlugin_Hypothesis::GetDefaultGeometricMesh();
-   double _phySize               = BLSURFPlugin_Hypothesis::GetDefaultPhySize(diagonal, bbSegmentation);
-   bool   _phySizeRel            = BLSURFPlugin_Hypothesis::GetDefaultPhySizeRel();
-   double _minSize               = BLSURFPlugin_Hypothesis::GetDefaultMinSize(diagonal);
-   bool   _minSizeRel            = BLSURFPlugin_Hypothesis::GetDefaultMinSizeRel();
-   double _maxSize               = BLSURFPlugin_Hypothesis::GetDefaultMaxSize(diagonal);
-   bool   _maxSizeRel            = BLSURFPlugin_Hypothesis::GetDefaultMaxSizeRel();
-   double _gradation             = BLSURFPlugin_Hypothesis::GetDefaultGradation();
-   bool   _quadAllowed           = BLSURFPlugin_Hypothesis::GetDefaultQuadAllowed();
-   double _angleMesh             = BLSURFPlugin_Hypothesis::GetDefaultAngleMesh();
-   double _chordalError          = BLSURFPlugin_Hypothesis::GetDefaultChordalError(diagonal);
-   bool   _anisotropic           = BLSURFPlugin_Hypothesis::GetDefaultAnisotropic();
-   double _anisotropicRatio      = BLSURFPlugin_Hypothesis::GetDefaultAnisotropicRatio();
-   bool   _removeTinyEdges       = BLSURFPlugin_Hypothesis::GetDefaultRemoveTinyEdges();
-   double _tinyEdgeLength        = BLSURFPlugin_Hypothesis::GetDefaultTinyEdgeLength(diagonal);
-   bool   _badElementRemoval     = BLSURFPlugin_Hypothesis::GetDefaultBadElementRemoval();
-   double _badElementAspectRatio = BLSURFPlugin_Hypothesis::GetDefaultBadElementAspectRatio();
-   bool   _optimizeMesh          = BLSURFPlugin_Hypothesis::GetDefaultOptimizeMesh();
-   bool   _quadraticMesh         = BLSURFPlugin_Hypothesis::GetDefaultQuadraticMesh();
-   int    _verb                  = BLSURFPlugin_Hypothesis::GetDefaultVerbosity();
-   int    _topology              = BLSURFPlugin_Hypothesis::GetDefaultTopology();
+
+  double diagonal               = SMESH_Mesh::GetShapeDiagonalSize( theGeomShape );
+  double bbSegmentation         = _gen->GetBoundaryBoxSegmentation();
+  int    _physicalMesh          = BLSURFPlugin_Hypothesis::GetDefaultPhysicalMesh();
+  int    _geometricMesh         = BLSURFPlugin_Hypothesis::GetDefaultGeometricMesh();
+  double _phySize               = BLSURFPlugin_Hypothesis::GetDefaultPhySize(diagonal, bbSegmentation);
+  bool   _phySizeRel            = BLSURFPlugin_Hypothesis::GetDefaultPhySizeRel();
+  double _minSize               = BLSURFPlugin_Hypothesis::GetDefaultMinSize(diagonal);
+  bool   _minSizeRel            = BLSURFPlugin_Hypothesis::GetDefaultMinSizeRel();
+  double _maxSize               = BLSURFPlugin_Hypothesis::GetDefaultMaxSize(diagonal);
+  bool   _maxSizeRel            = BLSURFPlugin_Hypothesis::GetDefaultMaxSizeRel();
+  double _gradation             = BLSURFPlugin_Hypothesis::GetDefaultGradation();
+  bool   _quadAllowed           = BLSURFPlugin_Hypothesis::GetDefaultQuadAllowed();
+  double _angleMesh             = BLSURFPlugin_Hypothesis::GetDefaultAngleMesh();
+  double _chordalError          = BLSURFPlugin_Hypothesis::GetDefaultChordalError(diagonal);
+  bool   _anisotropic           = BLSURFPlugin_Hypothesis::GetDefaultAnisotropic();
+  double _anisotropicRatio      = BLSURFPlugin_Hypothesis::GetDefaultAnisotropicRatio();
+  bool   _removeTinyEdges       = BLSURFPlugin_Hypothesis::GetDefaultRemoveTinyEdges();
+  double _tinyEdgeLength        = BLSURFPlugin_Hypothesis::GetDefaultTinyEdgeLength(diagonal);
+  bool   _badElementRemoval     = BLSURFPlugin_Hypothesis::GetDefaultBadElementRemoval();
+  double _badElementAspectRatio = BLSURFPlugin_Hypothesis::GetDefaultBadElementAspectRatio();
+  bool   _optimizeMesh          = BLSURFPlugin_Hypothesis::GetDefaultOptimizeMesh();
+  bool   _quadraticMesh         = BLSURFPlugin_Hypothesis::GetDefaultQuadraticMesh();
+  int    _verb                  = BLSURFPlugin_Hypothesis::GetDefaultVerbosity();
+  int    _topology              = BLSURFPlugin_Hypothesis::GetDefaultTopology();
 
   // PreCAD
-   int _precadMergeEdges         = BLSURFPlugin_Hypothesis::GetDefaultPreCADMergeEdges();
-   int _precadProcess3DTopology  = BLSURFPlugin_Hypothesis::GetDefaultPreCADProcess3DTopology();
-   int _precadDiscardInput       = BLSURFPlugin_Hypothesis::GetDefaultPreCADDiscardInput();
+  int _precadMergeEdges         = BLSURFPlugin_Hypothesis::GetDefaultPreCADMergeEdges();
+  int _precadProcess3DTopology  = BLSURFPlugin_Hypothesis::GetDefaultPreCADProcess3DTopology();
+  int _precadDiscardInput       = BLSURFPlugin_Hypothesis::GetDefaultPreCADDiscardInput();
 
 
   if (hyp) {
@@ -981,44 +990,59 @@ void BLSURFPlugin_BLSURF::SetParameters(
     }
     if (hyp->GetGradation() > 0)
       _gradation     = hyp->GetGradation();
-    _quadAllowed   = hyp->GetQuadAllowed();
-     if (hyp->GetAngleMesh() > 0)
-     _angleMesh     = hyp->GetAngleMesh();
-     if (hyp->GetChordalError() > 0)
-       _chordalError           = hyp->GetChordalError();
-     _anisotropic            = hyp->GetAnisotropic();
-     if (hyp->GetAnisotropicRatio() >= 0)
-       _anisotropicRatio       = hyp->GetAnisotropicRatio();
-     _removeTinyEdges        = hyp->GetRemoveTinyEdges();
-     if (hyp->GetTinyEdgeLength() > 0)
-       _tinyEdgeLength         = hyp->GetTinyEdgeLength();
-     _badElementRemoval      = hyp->GetBadElementRemoval();
-     if (hyp->GetBadElementAspectRatio() >= 0)
-       _badElementAspectRatio  = hyp->GetBadElementAspectRatio();
-     _optimizeMesh  = hyp->GetOptimizeMesh();
-     _quadraticMesh = hyp->GetQuadraticMesh();
+    _quadAllowed     = hyp->GetQuadAllowed();
+    if (hyp->GetAngleMesh() > 0)
+      _angleMesh     = hyp->GetAngleMesh();
+    if (hyp->GetChordalError() > 0)
+      _chordalError          = hyp->GetChordalError();
+    _anisotropic             = hyp->GetAnisotropic();
+    if (hyp->GetAnisotropicRatio() >= 0)
+      _anisotropicRatio      = hyp->GetAnisotropicRatio();
+    _removeTinyEdges         = hyp->GetRemoveTinyEdges();
+    if (hyp->GetTinyEdgeLength() > 0)
+      _tinyEdgeLength        = hyp->GetTinyEdgeLength();
+    _badElementRemoval       = hyp->GetBadElementRemoval();
+    if (hyp->GetBadElementAspectRatio() >= 0)
+      _badElementAspectRatio = hyp->GetBadElementAspectRatio();
+    _optimizeMesh  = hyp->GetOptimizeMesh();
+    _quadraticMesh = hyp->GetQuadraticMesh();
     _verb          = hyp->GetVerbosity();
-     _topology      = (int) hyp->GetTopology();
-     // PreCAD
-     _precadMergeEdges = hyp->GetPreCADMergeEdges();
-     _precadProcess3DTopology = hyp->GetPreCADProcess3DTopology();
-     _precadDiscardInput = hyp->GetPreCADDiscardInput();
+    _topology      = (int) hyp->GetTopology();
+    // PreCAD
+    _precadMergeEdges        = hyp->GetPreCADMergeEdges();
+    _precadProcess3DTopology = hyp->GetPreCADProcess3DTopology();
+    _precadDiscardInput      = hyp->GetPreCADDiscardInput();
 
-    const BLSURFPlugin_Hypothesis::TOptionValues & opts = hyp->GetOptionValues();
+    const BLSURFPlugin_Hypothesis::TOptionValues& opts = hyp->GetOptionValues();
     BLSURFPlugin_Hypothesis::TOptionValues::const_iterator opIt;
     for ( opIt = opts.begin(); opIt != opts.end(); ++opIt )
       if ( !opIt->second.empty() ) {
         MESSAGE("cadsurf_set_param(): " << opIt->first << " = " << opIt->second);
         set_param(css, opIt->first.c_str(), opIt->second.c_str());
       }
-      
-    const BLSURFPlugin_Hypothesis::TOptionValues & preCADopts = hyp->GetPreCADOptionValues();
+
+    const BLSURFPlugin_Hypothesis::TOptionValues& custom_opts = hyp->GetCustomOptionValues();
+    for ( opIt = custom_opts.begin(); opIt != custom_opts.end(); ++opIt )
+      if ( !opIt->second.empty() ) {
+        MESSAGE("cadsurf_set_param(): " << opIt->first << " = " << opIt->second);
+        set_param(css, opIt->first.c_str(), opIt->second.c_str());
+     }
+
+    const BLSURFPlugin_Hypothesis::TOptionValues& preCADopts = hyp->GetPreCADOptionValues();
     for ( opIt = preCADopts.begin(); opIt != preCADopts.end(); ++opIt )
       if ( !opIt->second.empty() ) {
         *use_precad = true;
         MESSAGE("precad_set_param(): " << opIt->first << " = " << opIt->second);
         precad_set_param(pcs, opIt->first.c_str(), opIt->second.c_str());
       }
+
+    const BLSURFPlugin_Hypothesis::TOptionValues& custom_preCADopts = hyp->GetCustomPreCADOptionValues();
+    for ( opIt = custom_preCADopts.begin(); opIt != custom_preCADopts.end(); ++opIt )
+      if ( !opIt->second.empty() ) {
+        *use_precad = true;
+        MESSAGE("precad_set_param(): " << opIt->first << " = " << opIt->second);
+        precad_set_param(pcs, opIt->first.c_str(), opIt->second.c_str());
+      }
   }
 //   else {
 //     //0020968: EDF1545 SMESH: Problem in the creation of a mesh group on geometry
@@ -1035,7 +1059,7 @@ void BLSURFPlugin_BLSURF::SetParameters(
   // PreCAD
   if (_topology == BLSURFPlugin_Hypothesis::PreCAD) {
     *use_precad = true;
-    precad_set_param(pcs, "verbose",                to_string(_verb).c_str());
+    precad_set_param(pcs, "verbose",                val_to_string(_verb).c_str());
     precad_set_param(pcs, "merge_edges",            _precadMergeEdges ? "1" : "0");
     precad_set_param(pcs, "process_3d_topology",    _precadProcess3DTopology ? "1" : "0");
     precad_set_param(pcs, "discard_input_topology", _precadDiscardInput ? "1" : "0");
@@ -1048,11 +1072,11 @@ void BLSURFPlugin_BLSURF::SetParameters(
    {
      case BLSURFPlugin_Hypothesis::PhysicalGlobalSize:
        set_param(css, "physical_size_mode", "global");
-       set_param(css, "global_physical_size", _phySizeRel ? to_string_rel(_phySize).c_str() : to_string(_phySize).c_str());
+       set_param(css, "global_physical_size", _phySizeRel ? val_to_string_rel(_phySize).c_str() : val_to_string(_phySize).c_str());
        break;
      case BLSURFPlugin_Hypothesis::PhysicalLocalSize:
        set_param(css, "physical_size_mode", "local");
-       set_param(css, "global_physical_size", _phySizeRel ? to_string_rel(_phySize).c_str() : to_string(_phySize).c_str());
+       set_param(css, "global_physical_size", _phySizeRel ? val_to_string_rel(_phySize).c_str() : val_to_string(_phySize).c_str());
        useGradation = true;
        break;
      default:
@@ -1063,14 +1087,14 @@ void BLSURFPlugin_BLSURF::SetParameters(
    {
      case BLSURFPlugin_Hypothesis::GeometricalGlobalSize:
        set_param(css, "geometric_size_mode", "global");
-       set_param(css, "geometric_approximation", to_string(_angleMesh).c_str());
-       set_param(css, "chordal_error", to_string(_chordalError).c_str());
+       set_param(css, "geometric_approximation", val_to_string(_angleMesh).c_str());
+       set_param(css, "chordal_error", val_to_string(_chordalError).c_str());
        useGradation = true;
        break;
      case BLSURFPlugin_Hypothesis::GeometricalLocalSize:
        set_param(css, "geometric_size_mode", "local");
-       set_param(css, "geometric_approximation", to_string(_angleMesh).c_str());
-       set_param(css, "chordal_error", to_string(_chordalError).c_str());
+       set_param(css, "geometric_approximation", val_to_string(_angleMesh).c_str());
+       set_param(css, "chordal_error", val_to_string(_chordalError).c_str());
        useGradation = true;
        break;
      default:
@@ -1084,43 +1108,44 @@ void BLSURFPlugin_BLSURF::SetParameters(
      double mins = _minSizeRel ? _minSize * diagonal : _minSize;
      // - min size should not be greater than user size
      if ( _phySize < mins )
-       set_param(css, "min_size", _phySizeRel ? to_string_rel(_phySize).c_str() : to_string(_phySize).c_str());
+       set_param(css, "min_size", _phySizeRel ? val_to_string_rel(_phySize).c_str() : val_to_string(_phySize).c_str());
      else
-       set_param(css, "min_size", _minSizeRel ? to_string_rel(_minSize).c_str() : to_string(_minSize).c_str());
+       set_param(css, "min_size", _minSizeRel ? val_to_string_rel(_minSize).c_str() : val_to_string(_minSize).c_str());
      // - compute absolute max size
      double maxs = _maxSizeRel ? _maxSize * diagonal : _maxSize;
      // - max size should not be less than user size
      if ( _phySize > maxs )
-       set_param(css, "max_size", _phySizeRel ? to_string_rel(_phySize).c_str() : to_string(_phySize).c_str());
+       set_param(css, "max_size", _phySizeRel ? val_to_string_rel(_phySize).c_str() : val_to_string(_phySize).c_str());
      else
-       set_param(css, "max_size", _maxSizeRel ? to_string_rel(_maxSize).c_str() : to_string(_maxSize).c_str());
+       set_param(css, "max_size", _maxSizeRel ? val_to_string_rel(_maxSize).c_str() : val_to_string(_maxSize).c_str());
    }
    else {
      // user size is not explicitly specified
      // - if minsize is not explicitly specified, we pass default value computed automatically, in this case "relative" flag is ignored
-     set_param(css, "min_size", _minSizeRel ? to_string_rel(_minSize).c_str() : to_string(_minSize).c_str());
+     set_param(css, "min_size", _minSizeRel ? val_to_string_rel(_minSize).c_str() : val_to_string(_minSize).c_str());
      // - if maxsize is not explicitly specified, we pass default value computed automatically, in this case "relative" flag is ignored
-     set_param(css, "max_size", _maxSizeRel ? to_string_rel(_maxSize).c_str() : to_string(_maxSize).c_str());
+     set_param(css, "max_size", _maxSizeRel ? val_to_string_rel(_maxSize).c_str() : val_to_string(_maxSize).c_str());
    }
-   if ( _quadAllowed )
-     useGradation = false; // limitation of V1.2
-  if ( useGradation )
-     set_param(css, "gradation",                         to_string(_gradation).c_str());
+   // anisotropic and quadrangle mesh requires disabling gradation
+   if ( _anisotropic && _quadAllowed )
+     useGradation = false; // limitation of V1.3
+   if ( useGradation )
+     set_param(css, "gradation",                         val_to_string(_gradation).c_str());
    set_param(css, "element_generation",                _quadAllowed ? "quad_dominant" : "triangle");
 
 
    set_param(css, "metric",                            _anisotropic ? "anisotropic" : "isotropic");
    if ( _anisotropic )
-     set_param(css, "anisotropic_ratio",                 to_string(_anisotropicRatio).c_str());
+     set_param(css, "anisotropic_ratio",                 val_to_string(_anisotropicRatio).c_str());
    set_param(css, "remove_tiny_edges",                 _removeTinyEdges ? "1" : "0");
    if ( _removeTinyEdges )
-     set_param(css, "tiny_edge_length",                  to_string(_tinyEdgeLength).c_str());
+     set_param(css, "tiny_edge_length",                  val_to_string(_tinyEdgeLength).c_str());
    set_param(css, "force_bad_surface_element_removal", _badElementRemoval ? "1" : "0");
    if ( _badElementRemoval )
-     set_param(css, "bad_surface_element_aspect_ratio",  to_string(_badElementAspectRatio).c_str());
+     set_param(css, "bad_surface_element_aspect_ratio",  val_to_string(_badElementAspectRatio).c_str());
    set_param(css, "optimisation",                      _optimizeMesh ? "yes" : "no");
    set_param(css, "element_order",                     _quadraticMesh ? "quadratic" : "linear");
-   set_param(css, "verbose",                           to_string(_verb).c_str());
+   set_param(css, "verbose",                           val_to_string(_verb).c_str());
 
    _smp_phy_size = _phySizeRel ? _phySize*diagonal : _phySize;
    if ( _verb > 0 )
@@ -1454,7 +1479,7 @@ void BLSURFPlugin_BLSURF::SetParameters(
     {
       MESSAGE("USING PRECAD FOR PERIODICITY")
       *use_precad = true;
-      precad_set_param(pcs, "verbose",                to_string(_verb).c_str());
+      precad_set_param(pcs, "verbose",                val_to_string(_verb).c_str());
     }
 
   MESSAGE("SetParameters facesPeriodicityVector");
@@ -1521,7 +1546,7 @@ namespace
 {
   // --------------------------------------------------------------------------
   /*!
-   * \brief Class correctly terminating usage of BLSURF library at destruction
+   * \brief Class correctly terminating usage of MG-CADSurf library at destruction
    */
   class BLSURF_Cleaner
   {
@@ -1672,7 +1697,7 @@ namespace
             }
           }
           // make nodes created on the boundary of viscous layer replace nodes created
-          // by BLSURF as their SMDS_Position is more correct
+          // by MG-CADSurf as their SMDS_Position is more correct
           nodes.sort( ShapeTypeCompare() );
           nodeGroupsToMerge.push_back( nodes );
         }
@@ -1771,7 +1796,7 @@ namespace
       //_proxyFace = TopoDS::Face( fExp.Current() );
 
 
-      // Make input mesh for BLSURF: segments on EDGE's of newFace
+      // Make input mesh for MG-CADSurf: segments on EDGE's of newFace
 
       // make nodes and fill in _tmp2origNN
       //
@@ -1803,7 +1828,7 @@ namespace
 
     //--------------------------------------------------------------------------------
     /*!
-     * \brief Fill in the origMesh with faces computed by BLSURF in this tmp mesh
+     * \brief Fill in the origMesh with faces computed by MG-CADSurf in this tmp mesh
      */
     //--------------------------------------------------------------------------------
 
@@ -1903,7 +1928,7 @@ bool BLSURFPlugin_BLSURF::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
       if ( !viscousMesh )
         return false; // error in StdMeshers_ViscousLayers2D::Compute()
 
-      // Compute BLSURF mesh on viscous layers
+      // Compute MG-CADSurf mesh on viscous layers
 
       if ( viscousMesh->NbProxySubMeshes() > 0 )
       {
@@ -1915,7 +1940,7 @@ bool BLSURFPlugin_BLSURF::Compute(SMESH_Mesh& aMesh, const TopoDS_Shape& aShape)
       }
     }
 
-    // Re-compute BLSURF mesh on the rest faces if the mesh was cleared
+    // Re-compute MG-CADSurf mesh on the rest faces if the mesh was cleared
 
     for (TopExp_Explorer face_iter(aShape,TopAbs_FACE);face_iter.More();face_iter.Next())
     {
@@ -1944,6 +1969,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
   /* create a distene context (generic object) */
   status_t status = STATUS_ERROR;
 
+  myMesh = &aMesh;
   SMESHDS_Mesh* meshDS = aMesh.GetMeshDS();
   SMESH_MesherHelper helper( aMesh );
   // do not call helper.IsQuadraticSubMesh() because sub-meshes
@@ -1955,9 +1981,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
   TSubMeshSet edgeSubmeshes;
   TSubMeshSet& mergeSubmeshes = edgeSubmeshes;
 
-  TopTools_IndexedMapOfShape fmap;
-  TopTools_IndexedMapOfShape emap;
-  TopTools_IndexedMapOfShape pmap;
+  TopTools_IndexedMapOfShape pmap, emap, fmap;
 
   // Issue 0019864. On DebianSarge, FE signals do not obey to OSD::SetSignal(false)
 #ifndef WIN32
@@ -2013,11 +2037,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
 
   MESSAGE("BEGIN SetParameters");
   bool use_precad = false;
-  SetParameters(
-                // #if BLSURF_VERSION_LONG >= "3.1.1"
-                //     c,
-                // #endif
-                _hypothesis, css, pcs, aShape, &use_precad);
+  SetParameters(_hypothesis, css, pcs, aShape, &use_precad);
   MESSAGE("END SetParameters");
 
   MESSAGE("_preCadFacesIDsPeriodicityVector.size() = " << _preCadFacesIDsPeriodicityVector.size());
@@ -2039,7 +2059,6 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
   vector<Handle(Geom2d_Curve)> curves;
   vector<Handle(Geom_Surface)> surfaces;
 
-  fmap.Clear();
   emap.Clear();
   pmap.Clear();
   FaceId2PythonSmp.clear();
@@ -2072,11 +2091,9 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
     if (f.Orientation() != TopAbs_FORWARD && f.Orientation() != TopAbs_REVERSED )
       f.Orientation(TopAbs_FORWARD);
 
-    if (fmap.FindIndex(f) > 0)
-      continue;
     iface = fmap.Add(f);
 //    std::string aFileName = "fmap_face_";
-//    aFileName.append(to_string(iface));
+//    aFileName.append(val_to_string(iface));
 //    aFileName.append(".brep");
 //    BRepTools::Write(f,aFileName.c_str());
 
@@ -2091,8 +2108,9 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
     cad_face_t *fce = cad_face_new(c, iface, surf_fun, surfaces.back());
 
     /* by default a face has no tag (color).
-       The following call sets it to the same value as the face_id : */
-    cad_face_set_tag(fce, iface);
+       The following call sets it to the same value as the Geom module ID : */
+    const int faceTag = meshDS->ShapeToIndex(f);
+    cad_face_set_tag(fce, faceTag);
 
     /* Set face orientation (optional if you want a well oriented output mesh)*/
     if(f.Orientation() != TopAbs_FORWARD)
@@ -2100,7 +2118,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
     else
       cad_face_set_orientation(fce, CAD_ORIENTATION_FORWARD);
 
-    if (HasSizeMapOnFace && !use_precad)
+    if (HasSizeMapOnFace /*&& !use_precad*/) //22903: use_precad seems not to interfere
     {
       // -----------------
       // Classic size map
@@ -2109,8 +2127,8 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
 
 
       if (FaceId2SizeMap.find(faceKey)!=FaceId2SizeMap.end()) {
-        MESSAGE("A size map is defined on face :"<<faceKey)
-          theSizeMapStr = FaceId2SizeMap[faceKey];
+        MESSAGE("A size map is defined on face :"<<faceKey);
+        theSizeMapStr = FaceId2SizeMap[faceKey];
         // check if function ends with "return"
         if (theSizeMapStr.find(bad_end) == (theSizeMapStr.size()-bad_end.size()-1))
           continue;
@@ -2216,21 +2234,22 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
           MESSAGE("Point is out of face: node is not created");
           if (EnfVertexCoords2ProjVertex.find(xyzCoords) != EnfVertexCoords2ProjVertex.end()) {
             EnfVertexCoords2ProjVertex.erase(xyzCoords);
-            EnfVertexCoords2EnfVertexList.erase(xyzCoords);
+            // isssue 22783. Do not erase as this point can be IN other face of a group
+            //EnfVertexCoords2EnfVertexList.erase(xyzCoords);
           }
         }
         if ( result == TopAbs_UNKNOWN ) {
           MESSAGE("Point position on face is unknown: node is not created");
           if (EnfVertexCoords2ProjVertex.find(xyzCoords) != EnfVertexCoords2ProjVertex.end()) {
             EnfVertexCoords2ProjVertex.erase(xyzCoords);
-            EnfVertexCoords2EnfVertexList.erase(xyzCoords);
+            //EnfVertexCoords2EnfVertexList.erase(xyzCoords);
           }
         }
         if ( result == TopAbs_ON ) {
           MESSAGE("Point is on border of face: node is not created");
           if (EnfVertexCoords2ProjVertex.find(xyzCoords) != EnfVertexCoords2ProjVertex.end()) {
             EnfVertexCoords2ProjVertex.erase(xyzCoords);
-            EnfVertexCoords2EnfVertexList.erase(xyzCoords);
+            //EnfVertexCoords2EnfVertexList.erase(xyzCoords);
           }
         }
         if ( result == TopAbs_IN )
@@ -2279,7 +2298,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
         ic = emap.Add(e);
 
 //      std::string aFileName = "fmap_edge_";
-//      aFileName.append(to_string(ic));
+//      aFileName.append(val_to_string(ic));
 //      aFileName.append(".brep");
 //      BRepTools::Write(e,aFileName.c_str());
 
@@ -2356,7 +2375,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
       if (e.Orientation() == TopAbs_INTERNAL)
         cad_edge_set_property(edg, EDGE_PROPERTY_INTERNAL);
 
-      // pass existing nodes of sub-meshes to BLSURF
+      // pass existing nodes of sub-meshes to MG-CADSurf
       if ( nodeData )
       {
         const std::vector<UVPtStruct>& nodeDataVec = nodeData->GetUVPtStruct();
@@ -2413,7 +2432,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
         }
 
 //        std::string aFileName = "fmap_vertex_";
-//        aFileName.append(to_string(*ip));
+//        aFileName.append(val_to_string(*ip));
 //        aFileName.append(".brep");
 //        BRepTools::Write(v,aFileName.c_str());
 
@@ -2499,7 +2518,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
 
   MESSAGE("BEFORE PERIODICITY");
   MESSAGE("_preCadFacesIDsPeriodicityVector.size() = " << _preCadFacesIDsPeriodicityVector.size());
-  if (! _preCadFacesIDsPeriodicityVector.empty()){
+  if (! _preCadFacesIDsPeriodicityVector.empty()) {
     MESSAGE("INTO PRECAD FACES PERIODICITY");
     for (std::size_t i=0; i < _preCadFacesIDsPeriodicityVector.size(); i++){
       std::vector<int> theFace1_ids = _preCadFacesIDsPeriodicityVector[i].shape1IDs;
@@ -2549,7 +2568,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
   }
 
   MESSAGE("_preCadEdgesIDsPeriodicityVector.size() = " << _preCadEdgesIDsPeriodicityVector.size());
-  if (! _preCadEdgesIDsPeriodicityVector.empty()){
+  if (! _preCadEdgesIDsPeriodicityVector.empty()) {
     MESSAGE("INTO PRECAD EDGES PERIODICITY");
     for (std::size_t i=0; i < _preCadEdgesIDsPeriodicityVector.size(); i++){
       std::vector<int> theEdge1_ids = _preCadEdgesIDsPeriodicityVector[i].shape1IDs;
@@ -2719,7 +2738,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
   }
 
   if (cleandc) {
-    cout << "Give the pre-processed dcad object to the current BLSurf session \n";
+    cout << "Give the pre-processed dcad object to the current MG-CADSurf session \n";
     cadsurf_data_set_dcad(css, cleandc);
   }
   else {
@@ -2728,8 +2747,8 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
   }
 
   if (cleanc) {
-    // Give the pre-processed CAD object to the current BLSurf session
-    cout << "Give the pre-processed CAD object to the current BLSurf session \n";
+    // Give the pre-processed CAD object to the current MG-CADSurf session
+    cout << "Give the pre-processed CAD object to the current MG-CADSurf session \n";
     cadsurf_data_set_cad(css, cleanc);
   }
   else {
@@ -2778,8 +2797,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
   if (_hypothesis)
     GMFFileName = _hypothesis->GetGMFFile();
   if (GMFFileName != "") {
-    //     bool GMFFileMode = _hypothesis->GetGMFFileMode();
-    bool asciiFound = (GMFFileName.find(".mesh",GMFFileName.length()-5) != std::string::npos);
+    bool asciiFound  = (GMFFileName.find(".mesh", GMFFileName.length()-5) != std::string::npos);
     bool binaryFound = (GMFFileName.find(".meshb",GMFFileName.length()-6) != std::string::npos);
     if (!asciiFound && !binaryFound)
       GMFFileName.append(".mesh");
@@ -2937,29 +2955,29 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
     mesh_get_triangle_extra_vertices(msh, it, &type, evtri);
     mesh_get_triangle_tag(msh, it, &tag);
     if (tags[vtx[0]]) {
-      meshDS->SetNodeOnFace(nodes[vtx[0]], TopoDS::Face(fmap(tag)));
+      meshDS->SetNodeOnFace(nodes[vtx[0]], tag);
       tags[vtx[0]] = false;
     };
     if (tags[vtx[1]]) {
-      meshDS->SetNodeOnFace(nodes[vtx[1]], TopoDS::Face(fmap(tag)));
+      meshDS->SetNodeOnFace(nodes[vtx[1]], tag);
       tags[vtx[1]] = false;
     };
     if (tags[vtx[2]]) {
-      meshDS->SetNodeOnFace(nodes[vtx[2]], TopoDS::Face(fmap(tag)));
+      meshDS->SetNodeOnFace(nodes[vtx[2]], tag);
       tags[vtx[2]] = false;
     };
     if (type == MESHGEMS_MESH_ELEMENT_TYPE_TRIA6) {
       // QUADRATIC TRIANGLE
       if (tags[evtri[0]]) {
-        meshDS->SetNodeOnFace(nodes[evtri[0]], TopoDS::Face(fmap(tag)));
+        meshDS->SetNodeOnFace(nodes[evtri[0]], tag);
         tags[evtri[0]] = false;
       }
       if (tags[evtri[1]]) {
-        meshDS->SetNodeOnFace(nodes[evtri[1]], TopoDS::Face(fmap(tag)));
+        meshDS->SetNodeOnFace(nodes[evtri[1]], tag);
         tags[evtri[1]] = false;
       }
       if (tags[evtri[2]]) {
-        meshDS->SetNodeOnFace(nodes[evtri[2]], TopoDS::Face(fmap(tag)));
+        meshDS->SetNodeOnFace(nodes[evtri[2]], tag);
         tags[evtri[2]] = false;
       }
       tri = meshDS->AddFace(nodes[vtx[0]], nodes[vtx[1]], nodes[vtx[2]],
@@ -2968,7 +2986,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
     else {
       tri = helper.AddFace(nodes[vtx[0]], nodes[vtx[1]], nodes[vtx[2]]);
     }
-    meshDS->SetMeshElementOnShape(tri, TopoDS::Face(fmap(tag)));
+    meshDS->SetMeshElementOnShape(tri, tag);
   }
 
   /* enumerate quadrangles */
@@ -2978,42 +2996,42 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
     mesh_get_quadrangle_extra_vertices(msh, it, &type, evquad);
     mesh_get_quadrangle_tag(msh, it, &tag);
     if (tags[vtx[0]]) {
-      meshDS->SetNodeOnFace(nodes[vtx[0]], TopoDS::Face(fmap(tag)));
+      meshDS->SetNodeOnFace(nodes[vtx[0]], tag);
       tags[vtx[0]] = false;
     };
     if (tags[vtx[1]]) {
-      meshDS->SetNodeOnFace(nodes[vtx[1]], TopoDS::Face(fmap(tag)));
+      meshDS->SetNodeOnFace(nodes[vtx[1]], tag);
       tags[vtx[1]] = false;
     };
     if (tags[vtx[2]]) {
-      meshDS->SetNodeOnFace(nodes[vtx[2]], TopoDS::Face(fmap(tag)));
+      meshDS->SetNodeOnFace(nodes[vtx[2]], tag);
       tags[vtx[2]] = false;
     };
     if (tags[vtx[3]]) {
-      meshDS->SetNodeOnFace(nodes[vtx[3]], TopoDS::Face(fmap(tag)));
+      meshDS->SetNodeOnFace(nodes[vtx[3]], tag);
       tags[vtx[3]] = false;
     };
     if (type == MESHGEMS_MESH_ELEMENT_TYPE_QUAD9) {
       // QUADRATIC QUADRANGLE
       std::cout << "This is a quadratic quadrangle" << std::endl;
       if (tags[evquad[0]]) {
-        meshDS->SetNodeOnFace(nodes[evquad[0]], TopoDS::Face(fmap(tag)));
+        meshDS->SetNodeOnFace(nodes[evquad[0]], tag);
         tags[evquad[0]] = false;
       }
       if (tags[evquad[1]]) {
-        meshDS->SetNodeOnFace(nodes[evquad[1]], TopoDS::Face(fmap(tag)));
+        meshDS->SetNodeOnFace(nodes[evquad[1]], tag);
         tags[evquad[1]] = false;
       }
       if (tags[evquad[2]]) {
-        meshDS->SetNodeOnFace(nodes[evquad[2]], TopoDS::Face(fmap(tag)));
+        meshDS->SetNodeOnFace(nodes[evquad[2]], tag);
         tags[evquad[2]] = false;
       }
       if (tags[evquad[3]]) {
-        meshDS->SetNodeOnFace(nodes[evquad[3]], TopoDS::Face(fmap(tag)));
+        meshDS->SetNodeOnFace(nodes[evquad[3]], tag);
         tags[evquad[3]] = false;
       }
       if (tags[evquad[4]]) {
-        meshDS->SetNodeOnFace(nodes[evquad[4]], TopoDS::Face(fmap(tag)));
+        meshDS->SetNodeOnFace(nodes[evquad[4]], tag);
         tags[evquad[4]] = false;
       }
       quad = meshDS->AddFace(nodes[vtx[0]], nodes[vtx[1]], nodes[vtx[2]], nodes[vtx[3]],
@@ -3023,13 +3041,13 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
     else {
       quad = helper.AddFace(nodes[vtx[0]], nodes[vtx[1]], nodes[vtx[2]], nodes[vtx[3]]);
     }
-    meshDS->SetMeshElementOnShape(quad, TopoDS::Face(fmap(tag)));
+    meshDS->SetMeshElementOnShape(quad, tag);
   }
 
   /* release the mesh object, the rest is released by cleaner */
   cadsurf_data_regain_mesh(css, msh);
 
-  if ( needMerge ) // sew mesh computed by BLSURF with pre-existing mesh
+  if ( needMerge ) // sew mesh computed by MG-CADSurf with pre-existing mesh
   {
     SMESH_MeshEditor editor( &aMesh );
     SMESH_MeshEditor::TListOfListOfNodes nodeGroupsToMerge;
@@ -3037,7 +3055,7 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
     TSubMeshSet::iterator smIt;
     SMESHDS_SubMesh* smDS;
 
-    // merge nodes on EDGE's with ones computed by BLSURF
+    // merge nodes on EDGE's with ones computed by MG-CADSurf
     for ( smIt = mergeSubmeshes.begin(); smIt != mergeSubmeshes.end(); ++smIt )
     {
       if (! (smDS = *smIt) ) continue;
@@ -3091,9 +3109,10 @@ bool BLSURFPlugin_BLSURF::compute(SMESH_Mesh&         aMesh,
     _comment = "No mesh elements assigned to a face";
   }
   bool badFaceFound = false;
-  for ( int i = 1; i <= fmap.Extent(); ++i )
+  for (TopExp_Explorer face_iter(aShape,TopAbs_FACE);face_iter.More();face_iter.Next())
   {
-    SMESH_subMesh* sm = aMesh.GetSubMesh( fmap(i) );
+    TopoDS_Face f = TopoDS::Face(face_iter.Current());
+    SMESH_subMesh* sm = aMesh.GetSubMesh( f );
     if ( !sm->GetSubMeshDS() || sm->GetSubMeshDS()->NbElements() == 0 )
     {
       sm->GetComputeError().reset( new SMESH_ComputeError( err, _comment, this ));
@@ -3194,14 +3213,14 @@ void BLSURFPlugin_BLSURF::Set_NodeOnEdge(SMESHDS_Mesh* meshDS, const SMDS_MeshNo
 status_t curv_fun(real t, real *uv, real *dt, real *dtt, void *user_data)
 {
   /* t is given. It contains the t (time) 1D parametric coordintaes
-     of the point PreCAD/BLSurf is querying on the curve */
+     of the point PreCAD/MG-CADSurf is querying on the curve */
 
-  /* user_data identifies the edge PreCAD/BLSurf is querying
+  /* user_data identifies the edge PreCAD/MG-CADSurf is querying
    * (see cad_edge_new later in this example) */
   const Geom2d_Curve*pargeo = (const Geom2d_Curve*) user_data;
 
   if (uv){
-   /* BLSurf is querying the function evaluation */
+   /* MG-CADSurf is querying the function evaluation */
     gp_Pnt2d P;
     P=pargeo->Value(t);
     uv[0]=P.X(); uv[1]=P.Y();
@@ -3235,9 +3254,9 @@ status_t surf_fun(real *uv, real *xyz, real*du, real *dv,
                   real *duu, real *duv, real *dvv, void *user_data)
 {
   /* uv[2] is given. It contains the u,v coordinates of the point
-   * PreCAD/BLSurf is querying on the surface */
+   * PreCAD/MG-CADSurf is querying on the surface */
 
-  /* user_data identifies the face PreCAD/BLSurf is querying (see
+  /* user_data identifies the face PreCAD/MG-CADSurf is querying (see
    * cad_face_new later in this example)*/
   const Geom_Surface* geometry = (const Geom_Surface*) user_data;
 
@@ -3274,16 +3293,12 @@ status_t surf_fun(real *uv, real *xyz, real*du, real *dv,
 
 status_t size_on_surface(integer face_id, real *uv, real *size, void *user_data)
 {
-  //MESSAGE("size_on_surface")
   TId2ClsAttractorVec::iterator f2attVec;
   if (FaceId2PythonSmp.count(face_id) != 0){
-    //MESSAGE("A size map is used to calculate size on face : "<<face_id)
-    PyObject * pyresult = NULL;
-    PyObject* new_stderr = NULL;
     assert(Py_IsInitialized());
     PyGILState_STATE gstate;
     gstate = PyGILState_Ensure();
-    pyresult = PyObject_CallFunction(FaceId2PythonSmp[face_id],(char*)"(f,f)",uv[0],uv[1]);
+    PyObject* pyresult = PyObject_CallFunction(FaceId2PythonSmp[face_id],(char*)"(f,f)",uv[0],uv[1]);
     real result;
     if ( pyresult != NULL) {
       result = PyFloat_AsDouble(pyresult);
@@ -3293,10 +3308,12 @@ status_t size_on_surface(integer face_id, real *uv, real *size, void *user_data)
     else{
       fflush(stderr);
       string err_description="";
-      new_stderr = newPyStdOut(err_description);
+      PyObject* new_stderr = newPyStdOut(err_description);
+      PyObject* old_stderr = PySys_GetObject((char*)"stderr");
+      Py_INCREF(old_stderr);
       PySys_SetObject((char*)"stderr", new_stderr);
       PyErr_Print();
-      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      PySys_SetObject((char*)"stderr", old_stderr);
       Py_DECREF(new_stderr);
       MESSAGE("Can't evaluate f(" << uv[0] << "," << uv[1] << ")" << " error is " << err_description);
       result = *((real*)user_data);
@@ -3330,12 +3347,10 @@ status_t size_on_surface(integer face_id, real *uv, real *size, void *user_data)
 status_t size_on_edge(integer edge_id, real t, real *size, void *user_data)
 {
   if (EdgeId2PythonSmp.count(edge_id) != 0){
-    PyObject * pyresult = NULL;
-    PyObject* new_stderr = NULL;
     assert(Py_IsInitialized());
     PyGILState_STATE gstate;
     gstate = PyGILState_Ensure();
-    pyresult = PyObject_CallFunction(EdgeId2PythonSmp[edge_id],(char*)"(f)",t);
+    PyObject* pyresult = PyObject_CallFunction(EdgeId2PythonSmp[edge_id],(char*)"(f)",t);
     real result;
     if ( pyresult != NULL) {
       result = PyFloat_AsDouble(pyresult);
@@ -3345,10 +3360,12 @@ status_t size_on_edge(integer edge_id, real t, real *size, void *user_data)
     else{
       fflush(stderr);
       string err_description="";
-      new_stderr = newPyStdOut(err_description);
+      PyObject* new_stderr = newPyStdOut(err_description);
+      PyObject* old_stderr = PySys_GetObject((char*)"stderr");
+      Py_INCREF(old_stderr);
       PySys_SetObject((char*)"stderr", new_stderr);
       PyErr_Print();
-      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      PySys_SetObject((char*)"stderr", old_stderr);
       Py_DECREF(new_stderr);
       MESSAGE("Can't evaluate f(" << t << ")" << " error is " << err_description);
       result = *((real*)user_data);
@@ -3365,12 +3382,10 @@ status_t size_on_edge(integer edge_id, real t, real *size, void *user_data)
 status_t size_on_vertex(integer point_id, real *size, void *user_data)
 {
   if (VertexId2PythonSmp.count(point_id) != 0){
-    PyObject * pyresult = NULL;
-    PyObject* new_stderr = NULL;
     assert(Py_IsInitialized());
     PyGILState_STATE gstate;
     gstate = PyGILState_Ensure();
-    pyresult = PyObject_CallFunction(VertexId2PythonSmp[point_id],(char*)"");
+    PyObject* pyresult = PyObject_CallFunction(VertexId2PythonSmp[point_id],(char*)"");
     real result;
     if ( pyresult != NULL) {
       result = PyFloat_AsDouble(pyresult);
@@ -3380,10 +3395,12 @@ status_t size_on_vertex(integer point_id, real *size, void *user_data)
     else {
       fflush(stderr);
       string err_description="";
-      new_stderr = newPyStdOut(err_description);
+      PyObject* new_stderr = newPyStdOut(err_description);
+      PyObject* old_stderr = PySys_GetObject((char*)"stderr");
+      Py_INCREF(old_stderr);
       PySys_SetObject((char*)"stderr", new_stderr);
       PyErr_Print();
-      PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+      PySys_SetObject((char*)"stderr", old_stderr);
       Py_DECREF(new_stderr);
       MESSAGE("Can't evaluate f()" << " error is " << err_description);
       result = *((real*)user_data);
@@ -3398,7 +3415,7 @@ status_t size_on_vertex(integer point_id, real *size, void *user_data)
 }
 
 /*
- * The following function will be called for PreCAD/BLSurf message
+ * The following function will be called for PreCAD/MG-CADSurf message
  * printing.  See context_set_message_callback (later in this
  * template) for how to set user_data.
  */
@@ -3430,7 +3447,7 @@ status_t message_cb(message_t *msg, void *user_data)
   return STATUS_OK;
 }
 
-/* This is the interrupt callback. PreCAD/BLSurf will call this
+/* This is the interrupt callback. PreCAD/MG-CADSurf will call this
  * function regularily. See the file meshgems/interrupt.h
  */
 status_t interrupt_cb(integer *interrupt_status, void *user_data)
@@ -3444,7 +3461,7 @@ status_t interrupt_cb(integer *interrupt_status, void *user_data)
     *interrupt_status = INTERRUPT_CONTINUE;
     return STATUS_OK;
   }
-  else /* you want to stop BLSurf */
+  else /* you want to stop MG-CADSurf */
   {
     *interrupt_status = INTERRUPT_STOP;
     return STATUS_ERROR;