Salome HOME
Copyright update 2022
[plugins/hybridplugin.git] / src / HYBRIDPlugin / HYBRIDPlugin_HYBRID.cxx
index 769bea33fd0cd14412fb060d06093fd987e33c48..862a704bcb8d67037ebee8e3ff212334a2295d6c 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2004-2013  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2022  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
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
-//=============================================================================
-// File      : HYBRIDPlugin_HYBRID.cxx
-// Created   : 
-// Author    : Edward AGAPOV, modified by Lioka RAZAFINDRAZAKA (CEA) 09/02/2007
-// Project   : SALOME
-//=============================================================================
+// ---
+// File   : HYBRIDPlugin_HYBRID.cxx
+// Author : Christian VAN WAMBEKE (CEA) (from GHS3D plugin V730)
+// ---
 //
 #include "HYBRIDPlugin_HYBRID.hxx"
 #include "HYBRIDPlugin_Hypothesis.hxx"
+#include "MG_HYBRID_API.hxx"
 
 #include <SMDS_FaceOfNodes.hxx>
-#include <SMDS_MeshElement.hxx>
-#include <SMDS_MeshNode.hxx>
+#include <SMDS_LinearEdge.hxx>
 #include <SMDS_VolumeOfNodes.hxx>
 #include <SMESHDS_Group.hxx>
+#include <SMESHDS_Mesh.hxx>
 #include <SMESH_Comment.hxx>
+#include <SMESH_File.hxx>
 #include <SMESH_Group.hxx>
 #include <SMESH_HypoFilter.hxx>
 #include <SMESH_Mesh.hxx>
 #include <SMESH_MeshAlgos.hxx>
 #include <SMESH_MeshEditor.hxx>
 #include <SMESH_MesherHelper.hxx>
-#include <SMESH_OctreeNode.hxx>
+#include <SMESH_ProxyMesh.hxx>
 #include <SMESH_subMeshEventListener.hxx>
-#include <StdMeshers_QuadToTriaAdaptor.hxx>
-#include <StdMeshers_ViscousLayers.hxx>
 
 #include <BRepAdaptor_Surface.hxx>
 #include <BRepBndLib.hxx>
@@ -56,7 +54,6 @@
 #include <Bnd_Box.hxx>
 #include <GProp_GProps.hxx>
 #include <GeomAPI_ProjectPointOnSurf.hxx>
-#include <OSD_File.hxx>
 #include <Precision.hxx>
 #include <Standard_ErrorHandler.hxx>
 #include <Standard_Failure.hxx>
 #include <Basics_Utils.hxx>
 #include <utilities.h>
 
-#ifdef WIN32
-#include <io.h>
-#else
-#include <sys/sysinfo.h>
-#endif
+#include <fstream>
 #include <algorithm>
 
 #define castToNode(n) static_cast<const SMDS_MeshNode *>( n );
 
-extern "C"
-{
-#ifndef WIN32
-#include <unistd.h>
-#include <sys/mman.h>
+#ifndef GMFVERSION
+#define GMFVERSION GmfDouble
 #endif
-#include <sys/stat.h>
-#include <fcntl.h>
-}
+#define GMFDIMENSION 3
 
 #define HOLE_ID -1
 
-typedef const list<const SMDS_MeshFace*> TTriaList;
+typedef const std::list<const SMDS_MeshFace*> TTriaList;
 
 static const char theDomainGroupNamePrefix[] = "Domain_";
 
-static void removeFile( const TCollection_AsciiString& fileName )
+static void removeFile( const std::string& fileName )
 {
   try {
-    OSD_File( fileName ).Remove();
+    SMESH_File( fileName ).remove();
   }
-  catch ( Standard_ProgramError ) {
-    MESSAGE("Can't remove file: " << fileName.ToCString() << " ; file does not exist or permission denied");
+  catch ( ... ) {
+    MESSAGE("Can't remove file: " << fileName << " ; file does not exist or permission denied");
   }
 }
 
@@ -114,31 +102,17 @@ static void removeFile( const TCollection_AsciiString& fileName )
  */
 //=============================================================================
 
-HYBRIDPlugin_HYBRID::HYBRIDPlugin_HYBRID(int hypId, int studyId, SMESH_Gen* gen)
-  : SMESH_3D_Algo(hypId, studyId, gen)
+HYBRIDPlugin_HYBRID::HYBRIDPlugin_HYBRID(int hypId, SMESH_Gen* gen)
+  : SMESH_3D_Algo(hypId, gen)
 {
-  MESSAGE("HYBRIDPlugin_HYBRID::HYBRIDPlugin_HYBRID");
   _name = Name();
   _shapeType = (1 << TopAbs_SHELL) | (1 << TopAbs_SOLID);// 1 bit /shape type
-  _onlyUnaryInput = false; // Compute() will be called on a compound of solids
+  _onlyUnaryInput = true; // Compute() will be called on each solid
   _iShape=0;
   _nbShape=0;
   _compatibleHypothesis.push_back( HYBRIDPlugin_Hypothesis::GetHypType());
-  _compatibleHypothesis.push_back( StdMeshers_ViscousLayers::GetHypType() );
-  _requireShape = false; // can work without shape_studyId
-
-  smeshGen_i = SMESH_Gen_i::GetSMESHGen();
-  CORBA::Object_var anObject = smeshGen_i->GetNS()->Resolve("/myStudyManager");
-  SALOMEDS::StudyManager_var aStudyMgr = SALOMEDS::StudyManager::_narrow(anObject);
-
-  MESSAGE("studyid = " << _studyId);
-
-  myStudy = NULL;
-  myStudy = aStudyMgr->GetStudyByID(_studyId);
-  if (myStudy)
-    MESSAGE("myStudy->StudyId() = " << myStudy->StudyId());
-  
-  _compute_canceled = false;
+  _requireShape = false; // can work without shape
+  _computeCanceled = false;
 }
 
 //=============================================================================
@@ -149,7 +123,6 @@ HYBRIDPlugin_HYBRID::HYBRIDPlugin_HYBRID(int hypId, int studyId, SMESH_Gen* gen)
 
 HYBRIDPlugin_HYBRID::~HYBRIDPlugin_HYBRID()
 {
-  MESSAGE("HYBRIDPlugin_HYBRID::~HYBRIDPlugin_HYBRID");
 }
 
 //=============================================================================
@@ -165,20 +138,17 @@ bool HYBRIDPlugin_HYBRID::CheckHypothesis ( SMESH_Mesh&         aMesh,
   aStatus = SMESH_Hypothesis::HYP_OK;
 
   _hyp = 0;
-  _viscousLayersHyp = 0;
   _keepFiles = false;
   _removeLogOnSuccess = true;
   _logInStandardOutput = false;
 
-  const list <const SMESHDS_Hypothesis * >& hyps =
+  const std::list <const SMESHDS_Hypothesis * >& hyps =
     GetUsedHypothesis(aMesh, aShape, /*ignoreAuxiliary=*/false);
-  list <const SMESHDS_Hypothesis* >::const_iterator h = hyps.begin();
+  std::list <const SMESHDS_Hypothesis* >::const_iterator h = hyps.begin();
   for ( ; h != hyps.end(); ++h )
   {
     if ( !_hyp )
       _hyp = dynamic_cast< const HYBRIDPlugin_Hypothesis*> ( *h );
-    if ( !_viscousLayersHyp )
-      _viscousLayersHyp = dynamic_cast< const StdMeshers_ViscousLayers*> ( *h );
   }
   if ( _hyp )
   {
@@ -198,274 +168,21 @@ bool HYBRIDPlugin_HYBRID::CheckHypothesis ( SMESH_Mesh&         aMesh,
 
 TopoDS_Shape HYBRIDPlugin_HYBRID::entryToShape(std::string entry)
 {
-  MESSAGE("HYBRIDPlugin_HYBRID::entryToShape "<<entry );
+  if ( SMESH_Gen_i::GetSMESHGen()->getStudyServant()->_is_nil() )
+    throw SALOME_Exception("MG-HYBRID plugin can't work w/o publishing in the study");
   GEOM::GEOM_Object_var aGeomObj;
   TopoDS_Shape S = TopoDS_Shape();
-  SALOMEDS::SObject_var aSObj = myStudy->FindObjectID( entry.c_str() );
+  SALOMEDS::SObject_var aSObj = SMESH_Gen_i::GetSMESHGen()->getStudyServant()->FindObjectID( entry.c_str() );
   if (!aSObj->_is_nil() ) {
     CORBA::Object_var obj = aSObj->GetObject();
     aGeomObj = GEOM::GEOM_Object::_narrow(obj);
     aSObj->UnRegister();
   }
   if ( !aGeomObj->_is_nil() )
-    S = smeshGen_i->GeomObjectToShape( aGeomObj.in() );
+    S = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( aGeomObj.in() );
   return S;
 }
 
-//=======================================================================
-//function : findShape
-//purpose  : 
-//=======================================================================
-
-static TopoDS_Shape findShape(const SMDS_MeshNode *aNode[],
-                              TopoDS_Shape        aShape,
-                              const TopoDS_Shape  shape[],
-                              double**            box,
-                              const int           nShape,
-                              TopAbs_State *      state = 0)
-{
-  gp_XYZ aPnt(0,0,0);
-  int j, iShape, nbNode = 4;
-
-  for ( j=0; j<nbNode; j++ ) {
-    gp_XYZ p ( aNode[j]->X(), aNode[j]->Y(), aNode[j]->Z() );
-    if ( aNode[j]->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE ) {
-      aPnt = p;
-      break;
-    }
-    aPnt += p / nbNode;
-  }
-
-  BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
-  if (state) *state = SC.State();
-  if ( SC.State() != TopAbs_IN || aShape.IsNull() || aShape.ShapeType() != TopAbs_SOLID) {
-    for (iShape = 0; iShape < nShape; iShape++) {
-      aShape = shape[iShape];
-      if ( !( aPnt.X() < box[iShape][0] || box[iShape][1] < aPnt.X() ||
-              aPnt.Y() < box[iShape][2] || box[iShape][3] < aPnt.Y() ||
-              aPnt.Z() < box[iShape][4] || box[iShape][5] < aPnt.Z()) ) {
-        BRepClass3d_SolidClassifier SC (aShape, aPnt, Precision::Confusion());
-        if (state) *state = SC.State();
-        if (SC.State() == TopAbs_IN)
-          break;
-      }
-    }
-  }
-  return aShape;
-}
-
-//=======================================================================
-//function : readMapIntLine
-//purpose  : 
-//=======================================================================
-
-static char* readMapIntLine(char* ptr, int tab[]) {
-  long int intVal;
-  std::cout << std::endl;
-
-  for ( int i=0; i<17; i++ ) {
-    intVal = strtol(ptr, &ptr, 10);
-    if ( i < 3 )
-      tab[i] = intVal;
-  }
-  return ptr;
-}
-
-//================================================================================
-/*!
- * \brief returns true if a triangle defined by the nodes is a temporary face on a
- * side facet of pyramid and defines sub-domain inside the pyramid
- */
-//================================================================================
-
-static bool isTmpFace(const SMDS_MeshNode* node1,
-                      const SMDS_MeshNode* node2,
-                      const SMDS_MeshNode* node3)
-{
-  // find a pyramid sharing the 3 nodes
-  //const SMDS_MeshElement* pyram = 0;
-  SMDS_ElemIteratorPtr vIt1 = node1->GetInverseElementIterator(SMDSAbs_Volume);
-  while ( vIt1->more() )
-  {
-    const SMDS_MeshElement* pyram = vIt1->next();
-    if ( pyram->NbCornerNodes() != 5 ) continue;
-    int i2, i3;
-    if ( (i2 = pyram->GetNodeIndex( node2 )) >= 0 &&
-         (i3 = pyram->GetNodeIndex( node3 )) >= 0 )
-    {
-      // Triangle defines sub-domian inside the pyramid if it's
-      // normal points out of the pyram
-
-      // make i2 and i3 hold indices of base nodes of the pyram while
-      // keeping the nodes order in the triangle
-      const int iApex = 4;
-      if ( i2 == iApex )
-        i2 = i3, i3 = pyram->GetNodeIndex( node1 );
-      else if ( i3 == iApex )
-        i3 = i2, i2 = pyram->GetNodeIndex( node1 );
-
-      int i3base = (i2+1) % 4; // next index after i2 within the pyramid base
-      return ( i3base != i3 );
-    }
-  }
-  return false;
-}
-
-//=======================================================================
-//function : findShapeID
-//purpose  : find the solid corresponding to HYBRID sub-domain following
-//           the technique proposed in GHS3D manual (available within
-//           ghs3d installation) in chapter "B.4 Subdomain (sub-region) assignment".
-//           In brief: normal of the triangle defined by the given nodes
-//           points out of the domain it is associated to
-//=======================================================================
-
-static int findShapeID(SMESH_Mesh&          mesh,
-                       const SMDS_MeshNode* node1,
-                       const SMDS_MeshNode* node2,
-                       const SMDS_MeshNode* node3,
-                       const bool           toMeshHoles)
-{
-  const int invalidID = 0;
-  SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
-
-  // face the nodes belong to
-  vector<const SMDS_MeshNode *> nodes(3);
-  nodes[0] = node1;
-  nodes[1] = node2;
-  nodes[2] = node3;
-  const SMDS_MeshElement * face = meshDS->FindElement( nodes, SMDSAbs_Face, /*noMedium=*/true);
-  if ( !face )
-    return isTmpFace(node1, node2, node3) ? HOLE_ID : invalidID;
-#ifdef _DEBUG_
-  std::cout << "bnd face " << face->GetID() << " - ";
-#endif
-  // geom face the face assigned to
-  SMESH_MeshEditor editor(&mesh);
-  int geomFaceID = editor.FindShape( face );
-  if ( !geomFaceID )
-    return isTmpFace(node1, node2, node3) ? HOLE_ID : invalidID;
-  TopoDS_Shape shape = meshDS->IndexToShape( geomFaceID );
-  if ( shape.IsNull() || shape.ShapeType() != TopAbs_FACE )
-    return invalidID;
-  TopoDS_Face geomFace = TopoDS::Face( shape );
-
-  // solids bounded by geom face
-  TopTools_IndexedMapOfShape solids, shells;
-  TopTools_ListIteratorOfListOfShape ansIt = mesh.GetAncestors(geomFace);
-  for ( ; ansIt.More(); ansIt.Next() ) {
-    switch ( ansIt.Value().ShapeType() ) {
-    case TopAbs_SOLID:
-      solids.Add( ansIt.Value() ); break;
-    case TopAbs_SHELL:
-      shells.Add( ansIt.Value() ); break;
-    default:;
-    }
-  }
-  // analyse found solids
-  if ( solids.Extent() == 0 || shells.Extent() == 0)
-    return invalidID;
-
-  const TopoDS_Solid& solid1 = TopoDS::Solid( solids(1) );
-  if ( solids.Extent() == 1 )
-  {
-    if ( toMeshHoles )
-      return meshDS->ShapeToIndex( solid1 );
-
-    // - Are we at a hole boundary face?
-    if ( shells(1).IsSame( BRepClass3d::OuterShell( solid1 )) )
-    { // - No, but maybe a hole is bound by two shapes? Does shells(1) touches another shell?
-      bool touch = false;
-      TopExp_Explorer eExp( shells(1), TopAbs_EDGE );
-      // check if any edge of shells(1) belongs to another shell
-      for ( ; eExp.More() && !touch; eExp.Next() ) {
-        ansIt = mesh.GetAncestors( eExp.Current() );
-        for ( ; ansIt.More() && !touch; ansIt.Next() ) {
-          if ( ansIt.Value().ShapeType() == TopAbs_SHELL )
-            touch = ( !ansIt.Value().IsSame( shells(1) ));
-        }
-      }
-      if (!touch)
-        return meshDS->ShapeToIndex( solid1 );
-    }
-  }
-  // find orientation of geom face within the first solid
-  TopExp_Explorer fExp( solid1, TopAbs_FACE );
-  for ( ; fExp.More(); fExp.Next() )
-    if ( geomFace.IsSame( fExp.Current() )) {
-      geomFace = TopoDS::Face( fExp.Current() );
-      break;
-    }
-  if ( !fExp.More() )
-    return invalidID; // face not found
-
-  // normale to triangle
-  gp_Pnt node1Pnt ( node1->X(), node1->Y(), node1->Z() );
-  gp_Pnt node2Pnt ( node2->X(), node2->Y(), node2->Z() );
-  gp_Pnt node3Pnt ( node3->X(), node3->Y(), node3->Z() );
-  gp_Vec vec12( node1Pnt, node2Pnt );
-  gp_Vec vec13( node1Pnt, node3Pnt );
-  gp_Vec meshNormal = vec12 ^ vec13;
-  if ( meshNormal.SquareMagnitude() < DBL_MIN )
-    return invalidID;
-
-  // get normale to geomFace at any node
-  bool geomNormalOK = false;
-  gp_Vec geomNormal;
-  SMESH_MesherHelper helper( mesh ); helper.SetSubShape( geomFace );
-  for ( int i = 0; !geomNormalOK && i < 3; ++i )
-  {
-    // find UV of i-th node on geomFace
-    const SMDS_MeshNode* nNotOnSeamEdge = 0;
-    if ( helper.IsSeamShape( nodes[i]->getshapeId() )) {
-      if ( helper.IsSeamShape( nodes[(i+1)%3]->getshapeId() ))
-        nNotOnSeamEdge = nodes[(i+2)%3];
-      else
-        nNotOnSeamEdge = nodes[(i+1)%3];
-    }
-    bool uvOK;
-    gp_XY uv = helper.GetNodeUV( geomFace, nodes[i], nNotOnSeamEdge, &uvOK );
-    // check that uv is correct
-    if (uvOK) {
-      double tol = 1e-6;
-      TopoDS_Shape nodeShape = helper.GetSubShapeByNode( nodes[i], meshDS );
-      if ( !nodeShape.IsNull() )
-        switch ( nodeShape.ShapeType() )
-        {
-        case TopAbs_FACE:   tol = BRep_Tool::Tolerance( TopoDS::Face( nodeShape )); break;
-        case TopAbs_EDGE:   tol = BRep_Tool::Tolerance( TopoDS::Edge( nodeShape )); break;
-        case TopAbs_VERTEX: tol = BRep_Tool::Tolerance( TopoDS::Vertex( nodeShape )); break;
-        default:;
-        }
-      gp_Pnt nodePnt ( nodes[i]->X(), nodes[i]->Y(), nodes[i]->Z() );
-      BRepAdaptor_Surface surface( geomFace );
-      uvOK = ( nodePnt.Distance( surface.Value( uv.X(), uv.Y() )) < 2 * tol );
-      if ( uvOK ) {
-        // normale to geomFace at UV
-        gp_Vec du, dv;
-        surface.D1( uv.X(), uv.Y(), nodePnt, du, dv );
-        geomNormal = du ^ dv;
-        if ( geomFace.Orientation() == TopAbs_REVERSED )
-          geomNormal.Reverse();
-        geomNormalOK = ( geomNormal.SquareMagnitude() > DBL_MIN * 1e3 );
-      }
-    }
-  }
-  if ( !geomNormalOK)
-    return invalidID;
-
-  // compare normals
-  bool isReverse = ( meshNormal * geomNormal ) < 0;
-  if ( !isReverse )
-    return meshDS->ShapeToIndex( solid1 );
-
-  if ( solids.Extent() == 1 )
-    return HOLE_ID; // we are inside a hole
-  else
-    return meshDS->ShapeToIndex( solids(2) );
-}
-
-
 //=======================================================================
 //function : addElemInMeshGroup
 //purpose  : Update or create groups in mesh
@@ -474,7 +191,7 @@ static int findShapeID(SMESH_Mesh&          mesh,
 static void addElemInMeshGroup(SMESH_Mesh*             theMesh,
                                const SMDS_MeshElement* anElem,
                                std::string&            groupName,
-                               std::set<std::string>&  groupsToRemove)
+                               std::set<std::string>&  /*groupsToRemove*/)
 {
   if ( !anElem ) return; // issue 0021776
 
@@ -489,19 +206,16 @@ static void addElemInMeshGroup(SMESH_Mesh*             theMesh,
       SMESHDS_Group* aGroupDS = static_cast<SMESHDS_Group*>( groupDS );
       aGroupDS->SMDSGroup().Add(anElem);
       groupDone = true;
-//       MESSAGE("Successfully added enforced element to existing group " << groupName);
       break;
     }
   }
   
   if (!groupDone)
   {
-    int groupId;
-    SMESH_Group* aGroup = theMesh->AddGroup(anElem->GetType(), groupName.c_str(), groupId);
+    SMESH_Group* aGroup = theMesh->AddGroup(anElem->GetType(), groupName.c_str());
     aGroup->SetName( groupName.c_str() );
     SMESHDS_Group* aGroupDS = static_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
     aGroupDS->SMDSGroup().Add(anElem);
-//     MESSAGE("Successfully created enforced vertex group " << groupName);
     groupDone = true;
   }
   if (!groupDone)
@@ -522,10 +236,9 @@ static void updateMeshGroups(SMESH_Mesh* theMesh, std::set<std::string> groupsTo
     if ( !group ) continue;
     SMESHDS_GroupBase* groupDS = group->GetGroupDS();
     if ( !groupDS ) continue;
-    std::string currentGroupName = (string)group->GetName();
+    std::string currentGroupName = (std::string)group->GetName();
     if (groupDS->IsEmpty() && groupsToRemove.find(currentGroupName) != groupsToRemove.end()) {
       // Previous group created by enforced elements
-      MESSAGE("Delete previous group created by removed enforced elements: " << group->GetName())
       theMesh->RemoveGroup(groupDS->GetID());
     }
   }
@@ -572,11 +285,6 @@ static void removeEmptyGroupsOfDomains(SMESH_Mesh* mesh,
 static void makeDomainGroups( std::vector< std::vector< const SMDS_MeshElement* > >& elemsOfDomain,
                               SMESH_MesherHelper*                                    theHelper)
 {
-  // int nbDomains = 0;
-  // for ( size_t i = 0; i < elemsOfDomain.size(); ++i )
-  //   nbDomains += ( elemsOfDomain[i].size() > 0 );
-
-  // if ( nbDomains > 1 )
   for ( size_t iDomain = 0; iDomain < elemsOfDomain.size(); ++iDomain )
   {
     std::vector< const SMDS_MeshElement* > & elems = elemsOfDomain[ iDomain ];
@@ -595,13 +303,12 @@ static void makeDomainGroups( std::vector< std::vector< const SMDS_MeshElement*
     }
     // create and fill the groups
     size_t iElem = 0;
-    int groupID;
     do
     {
       SMESH_Group* group = groupOfType[ elems[ iElem ]->GetType() ];
       if ( !group )
         group = theHelper->GetMesh()->AddGroup( elems[ iElem ]->GetType(),
-                                                domainName.c_str(), groupID );
+                                                domainName.c_str() );
       SMDS_MeshGroup& groupDS =
         static_cast< SMESHDS_Group* >( group->GetGroupDS() )->SMDSGroup();
 
@@ -617,44 +324,82 @@ static void makeDomainGroups( std::vector< std::vector< const SMDS_MeshElement*
 //purpose  : read GMF file w/o geometry associated to mesh
 //=======================================================================
 
-static bool readGMFFile(const char*                     theFile,
-                        HYBRIDPlugin_HYBRID*              theAlgo,
-                        SMESH_MesherHelper*             theHelper,
+static bool readGMFFile(MG_HYBRID_API*                          MGOutput,
+                        const char*                             theFile,
+                        HYBRIDPlugin_HYBRID*                    theAlgo,
+                        SMESH_MesherHelper*                     theHelper,
                         std::vector <const SMDS_MeshNode*> &    theNodeByHybridId,
                         std::vector <const SMDS_MeshElement*> & theFaceByHybridId,
-                        map<const SMDS_MeshNode*,int> & theNodeToHybridIdMap,
-                        std::vector<std::string> &      aNodeGroupByHybridId,
-                        std::vector<std::string> &      anEdgeGroupByHybridId,
-                        std::vector<std::string> &      aFaceGroupByHybridId,
-                        std::set<std::string> &         groupsToRemove,
-                        bool                            toMakeGroupsOfDomains=false,
-                        bool                            toMeshHoles=true)
+                        std::map<const SMDS_MeshNode*,int> &    /*theNodeToHybridIdMap*/,
+                        std::vector<std::string> &              aNodeGroupByHybridId,
+                        std::vector<std::string> &              anEdgeGroupByHybridId,
+                        std::vector<std::string> &              aFaceGroupByHybridId,
+                        std::set<std::string> &                 groupsToRemove,
+                        bool                                    toMakeGroupsOfDomains=false,
+                        bool                                    /*toMeshHoles*/=true)
 {
   std::string tmpStr;
   SMESHDS_Mesh* theMeshDS = theHelper->GetMeshDS();
   const bool hasGeom = ( theHelper->GetMesh()->HasShapeToMesh() );
 
+  // if imprinting, the original mesh faces are modified
+  // => we clear all the faces to retrieve them from Hybrid output mesh.
+  std::vector<int> facesWithImprinting;
+  if (theAlgo->getHyp())
+    facesWithImprinting = theAlgo->getHyp()->GetFacesWithImprinting();
+
+  if ( ! facesWithImprinting.empty() ) {
+#ifdef _DEBUG_
+    std::cout << "Imprinting => Clear original mesh" << std::endl;
+#endif
+    SMESH_subMesh* smOfSolid =
+      theHelper->GetMesh()->GetSubMesh( theHelper->GetSubShape() );
+    SMESH_subMeshIteratorPtr smIt =
+      smOfSolid->getDependsOnIterator(/*includeSelf=*/false, /*complexShapeFirst=*/true);
+    while ( smIt->more() )
+    {
+      SMESH_subMesh* sm = smIt->next();
+      if ( SMESHDS_SubMesh * smDS = sm->GetSubMeshDS() )
+      {
+        SMDS_ElemIteratorPtr eIt = smDS->GetElements();
+        while( eIt->more() )
+        {
+          theMeshDS->RemoveFreeElement( eIt->next(), smDS );
+        }
+        SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
+        while ( nIt->more() )
+        {
+          const SMDS_MeshNode* n = nIt->next();
+          if ( n->NbInverseElements() == 0 )
+            theMeshDS->RemoveFreeNode( n, smDS );
+        }
+      }
+    }
+    theNodeByHybridId.clear();
+    theFaceByHybridId.clear();
+  }
+
   int nbInitialNodes = theNodeByHybridId.size();
+
+#ifdef _DEBUG_
   int nbMeshNodes = theMeshDS->NbNodes();
-  
-  const bool isQuadMesh = 
+  const bool isQuadMesh =
     theHelper->GetMesh()->NbEdges( ORDER_QUADRATIC ) ||
     theHelper->GetMesh()->NbFaces( ORDER_QUADRATIC ) ||
     theHelper->GetMesh()->NbVolumes( ORDER_QUADRATIC );
-    
-#ifdef _DEBUG_
+
   std::cout << "theNodeByHybridId.size(): " << nbInitialNodes << std::endl;
   std::cout << "theHelper->GetMesh()->NbNodes(): " << nbMeshNodes << std::endl;
   std::cout << "isQuadMesh: " << isQuadMesh << std::endl;
 #endif
-  
+
   // ---------------------------------
   // Read generated elements and nodes
   // ---------------------------------
 
   int nbElem = 0, nbRef = 0;
   int aGMFNodeID = 0;
-  const SMDS_MeshNode** GMFNode;
+  std::vector< const SMDS_MeshNode* > GMFNode;
 #ifdef _DEBUG_
   std::map<int, std::set<int> > subdomainId2tetraId;
 #endif
@@ -669,60 +414,25 @@ static bool readGMFFile(const char*                     theFile,
   tabRef[GmfTriangles]      = 3; // for enforced faces
   tabRef[GmfQuadrilaterals] = 4;
   tabRef[GmfTetrahedra]     = 4; // for new tetras
+  tabRef[GmfPyramids]       = 5; // for new pyramids
   tabRef[GmfPrisms]         = 6; // for new prisms
   tabRef[GmfHexahedra]      = 8;
 
   int ver, dim;
-  MESSAGE("Read " << theFile << " file");
-  int InpMsh = GmfOpenMesh(theFile, GmfRead, &ver, &dim);
+  int InpMsh = MGOutput->GmfOpenMesh(theFile, GmfRead, &ver, &dim);
   if (!InpMsh)
     return false;
-  MESSAGE("Done ");
 
-  // Read ids of domains
-  vector< int > solidIDByDomain;
+  // Hybrid is not multi-domain => We can't (and don't need to) read ids of domains in ouput file like in GHS3DPlugin
+  // We just need to get the id of the one and only solid
+  int solidID = 1;
   if ( hasGeom )
   {
-    int solid1; // id used in case of 1 domain or some reading failure
     if ( theHelper->GetSubShape().ShapeType() == TopAbs_SOLID )
-      solid1 = theHelper->GetSubShapeID();
+      solidID = theHelper->GetSubShapeID();
     else
-      solid1 = theMeshDS->ShapeToIndex
+      solidID = theMeshDS->ShapeToIndex
         ( TopExp_Explorer( theHelper->GetSubShape(), TopAbs_SOLID ).Current() );
-
-    int nbDomains = GmfStatKwd( InpMsh, GmfSubDomainFromGeom );
-    if ( nbDomains > 1 )
-    {
-      solidIDByDomain.resize( nbDomains+1, theHelper->GetSubShapeID() );
-      int faceNbNodes, faceIndex, orientation, domainNb;
-      GmfGotoKwd( InpMsh, GmfSubDomainFromGeom );
-      for ( int i = 0; i < nbDomains; ++i )
-      {
-        faceIndex = 0;
-        GmfGetLin( InpMsh, GmfSubDomainFromGeom,
-                   &faceNbNodes, &faceIndex, &orientation, &domainNb);
-        solidIDByDomain[ domainNb ] = 1;
-        if ( 0 < faceIndex && faceIndex-1 < theFaceByHybridId.size() )
-        {
-          const SMDS_MeshElement* face = theFaceByHybridId[ faceIndex-1 ];
-          const SMDS_MeshNode* nn[3] = { face->GetNode(0),
-                                         face->GetNode(1),
-                                         face->GetNode(2) };
-          if ( orientation < 0 )
-            std::swap( nn[1], nn[2] );
-          solidIDByDomain[ domainNb ] =
-            findShapeID( *theHelper->GetMesh(), nn[0], nn[1], nn[2], toMeshHoles );
-          if ( solidIDByDomain[ domainNb ] > 0 )
-          {
-            const TopoDS_Shape& foundShape = theMeshDS->IndexToShape( solidIDByDomain[ domainNb ] );
-            if ( ! theHelper->IsSubShape( foundShape, theHelper->GetSubShape() ))
-              solidIDByDomain[ domainNb ] = HOLE_ID;
-          }
-        }
-      }
-    }
-    if ( solidIDByDomain.size() < 2 )
-      solidIDByDomain.resize( 2, solid1 );
   }
 
   // Issue 0020682. Avoid creating nodes and tetras at place where
@@ -731,29 +441,30 @@ static bool readGMFFile(const char*                     theFile,
   std::vector< const SMDS_MeshElement* > foundVolumes;
   if ( !hasGeom && theHelper->GetMesh()->NbVolumes() > 0 )
     elemSearcher = SMESH_MeshAlgos::GetElementSearcher( *theMeshDS );
-  auto_ptr< SMESH_ElementSearcher > elemSearcherDeleter( elemSearcher );
+  SMESHUtils::Deleter< SMESH_ElementSearcher > elemSearcherDeleter( elemSearcher );
 
   // IMP 0022172: [CEA 790] create the groups corresponding to domains
   std::vector< std::vector< const SMDS_MeshElement* > > elemsOfDomain;
 
-  int nbVertices = GmfStatKwd(InpMsh, GmfVertices) - nbInitialNodes;
-  GMFNode = new const SMDS_MeshNode*[ nbVertices + 1 ];
+  int nbVertices = MGOutput->GmfStatKwd(InpMsh, GmfVertices) - nbInitialNodes;
+  if ( nbVertices < 0 )
+    return false;
+  GMFNode.resize( nbVertices + 1 );
 
   std::map <GmfKwdCod,int>::const_iterator it = tabRef.begin();
   for ( ; it != tabRef.end() ; ++it)
   {
     if(theAlgo->computeCanceled()) {
-      GmfCloseMesh(InpMsh);
-      delete [] GMFNode;
+      MGOutput->GmfCloseMesh(InpMsh);
       return false;
     }
-    int dummy, solidID;
+    int dummy;
     GmfKwdCod token = it->first;
     nbRef           = it->second;
 
-    nbElem = GmfStatKwd(InpMsh, token);
+    nbElem = MGOutput->GmfStatKwd(InpMsh, token);
     if (nbElem > 0) {
-      GmfGotoKwd(InpMsh, token);
+      MGOutput->GmfGotoKwd(InpMsh, token);
       std::cout << "Read " << nbElem;
     }
     else
@@ -764,41 +475,25 @@ static bool readGMFFile(const char*                     theFile,
 
     if (token == GmfVertices) {
       (nbElem <= 1) ? tmpStr = " vertex" : tmpStr = " vertices";
-//       std::cout << nbInitialNodes << " from input mesh " << std::endl;
-
-      // Remove orphan nodes from previous enforced mesh which was cleared
-//       if ( nbElem < nbMeshNodes ) {
-//         const SMDS_MeshNode* node;
-//         SMDS_NodeIteratorPtr nodeIt = theMeshDS->nodesIterator();
-//         while ( nodeIt->more() )
-//         {
-//           node = nodeIt->next();
-//           if (theNodeToHybridIdMap.find(node) != theNodeToHybridIdMap.end())
-//             theMeshDS->RemoveNode(node);
-//         }
-//       }
-
       
       int aGMFID;
-
       float VerTab_f[3];
       double x, y, z;
       const SMDS_MeshNode * aGMFNode;
 
       for ( int iElem = 0; iElem < nbElem; iElem++ ) {
         if(theAlgo->computeCanceled()) {
-          GmfCloseMesh(InpMsh);
-          delete [] GMFNode;
+          MGOutput->GmfCloseMesh(InpMsh);
           return false;
         }
         if (ver == GmfFloat) {
-          GmfGetLin(InpMsh, token, &VerTab_f[0], &VerTab_f[1], &VerTab_f[2], &dummy);
+          MGOutput->GmfGetLin(InpMsh, token, &VerTab_f[0], &VerTab_f[1], &VerTab_f[2], &dummy);
           x = VerTab_f[0];
           y = VerTab_f[1];
           z = VerTab_f[2];
         }
         else {
-          GmfGetLin(InpMsh, token, &x, &y, &z, &dummy);
+          MGOutput->GmfGetLin(InpMsh, token, &x, &y, &z, &dummy);
         }
         if (iElem >= nbInitialNodes) {
           if ( elemSearcher &&
@@ -809,7 +504,7 @@ static bool readGMFFile(const char*                     theFile,
           
           aGMFID = iElem -nbInitialNodes +1;
           GMFNode[ aGMFID ] = aGMFNode;
-          if (aGMFID-1 < aNodeGroupByHybridId.size() && !aNodeGroupByHybridId.at(aGMFID-1).empty())
+          if (aGMFID-1 < (int)aNodeGroupByHybridId.size() && !aNodeGroupByHybridId.at(aGMFID-1).empty())
             addElemInMeshGroup(theHelper->GetMesh(), aGMFNode, aNodeGroupByHybridId.at(aGMFID-1), groupsToRemove);
         }
       }
@@ -817,52 +512,57 @@ static bool readGMFFile(const char*                     theFile,
     else if (token == GmfCorners && nbElem > 0) {
       (nbElem <= 1) ? tmpStr = " corner" : tmpStr = " corners";
       for ( int iElem = 0; iElem < nbElem; iElem++ )
-        GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]]);
+        MGOutput->GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]]);
     }
     else if (token == GmfRidges && nbElem > 0) {
       (nbElem <= 1) ? tmpStr = " ridge" : tmpStr = " ridges";
       for ( int iElem = 0; iElem < nbElem; iElem++ )
-        GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]]);
+        MGOutput->GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]]);
     }
     else if (token == GmfEdges && nbElem > 0) {
       (nbElem <= 1) ? tmpStr = " edge" : tmpStr = " edges";
       for ( int iElem = 0; iElem < nbElem; iElem++ )
-        GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &domainID[iElem]);
+        MGOutput->GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &domainID[iElem]);
     }
     else if (token == GmfTriangles && nbElem > 0) {
       (nbElem <= 1) ? tmpStr = " triangle" : tmpStr = " triangles";
       for ( int iElem = 0; iElem < nbElem; iElem++ )
-        GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &domainID[iElem]);
+        MGOutput->GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &domainID[iElem]);
     }
     else if (token == GmfQuadrilaterals && nbElem > 0) {
       (nbElem <= 1) ? tmpStr = " Quadrilateral" : tmpStr = " Quadrilaterals";
       for ( int iElem = 0; iElem < nbElem; iElem++ )
-        GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &id[iElem*tabRef[token]+3], &domainID[iElem]);
+        MGOutput->GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &id[iElem*tabRef[token]+3], &domainID[iElem]);
     }
     else if (token == GmfTetrahedra && nbElem > 0) {
       (nbElem <= 1) ? tmpStr = " Tetrahedron" : tmpStr = " Tetrahedra";
       for ( int iElem = 0; iElem < nbElem; iElem++ ) {
-        GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &id[iElem*tabRef[token]+3], &domainID[iElem]);
+        MGOutput->GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &id[iElem*tabRef[token]+3], &domainID[iElem]);
 #ifdef _DEBUG_
         subdomainId2tetraId[dummy].insert(iElem+1);
-//         MESSAGE("subdomainId2tetraId["<<dummy<<"].insert("<<iElem+1<<")");
 #endif
       }
     }
+    else if (token == GmfPyramids && nbElem > 0) {
+      (nbElem <= 1) ? tmpStr = " Pyramid" : tmpStr = " Pyramids";
+      for ( int iElem = 0; iElem < nbElem; iElem++ )
+        MGOutput->GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &id[iElem*tabRef[token]+3],
+                  &id[iElem*tabRef[token]+4], &domainID[iElem]);
+    }
     else if (token == GmfPrisms && nbElem > 0) {
       (nbElem <= 1) ? tmpStr = " Prism" : tmpStr = " Prisms";
       for ( int iElem = 0; iElem < nbElem; iElem++ )
-        GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &id[iElem*tabRef[token]+3],
+        MGOutput->GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &id[iElem*tabRef[token]+3],
                   &id[iElem*tabRef[token]+4], &id[iElem*tabRef[token]+5], &domainID[iElem]);
     }
     else if (token == GmfHexahedra && nbElem > 0) {
       (nbElem <= 1) ? tmpStr = " Hexahedron" : tmpStr = " Hexahedra";
       for ( int iElem = 0; iElem < nbElem; iElem++ )
-        GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &id[iElem*tabRef[token]+3],
+        MGOutput->GmfGetLin(InpMsh, token, &id[iElem*tabRef[token]], &id[iElem*tabRef[token]+1], &id[iElem*tabRef[token]+2], &id[iElem*tabRef[token]+3],
                   &id[iElem*tabRef[token]+4], &id[iElem*tabRef[token]+5], &id[iElem*tabRef[token]+6], &id[iElem*tabRef[token]+7], &domainID[iElem]);
     }
     std::cout << tmpStr << std::endl;
-    std::cout << std::endl;
+    //std::cout << std::endl;
 
     switch (token) {
     case GmfCorners:
@@ -871,6 +571,7 @@ static bool readGMFFile(const char*                     theFile,
     case GmfTriangles:
     case GmfQuadrilaterals:
     case GmfTetrahedra:
+    case GmfPyramids:
     case GmfPrisms:
     case GmfHexahedra:
     {
@@ -882,8 +583,7 @@ static bool readGMFFile(const char*                     theFile,
       for ( int iElem = 0; iElem < nbElem; iElem++ )
       {
         if(theAlgo->computeCanceled()) {
-          GmfCloseMesh(InpMsh);
-          delete [] GMFNode;
+          MGOutput->GmfCloseMesh(InpMsh);
           return false;
         }
         // Check if elem is already in input mesh. If yes => skip
@@ -910,26 +610,43 @@ static bool readGMFFile(const char*                     theFile,
         case GmfEdges:
           if (fullyCreatedElement) {
             aCreatedElem = theHelper->AddEdge( node[0], node[1], noID, force3d );
-            if (anEdgeGroupByHybridId.size() && !anEdgeGroupByHybridId[iElem].empty())
+            if ( !anEdgeGroupByHybridId.empty() && !anEdgeGroupByHybridId[iElem].empty())
               addElemInMeshGroup(theHelper->GetMesh(), aCreatedElem, anEdgeGroupByHybridId[iElem], groupsToRemove);
           }
           break;
         case GmfTriangles:
           if (fullyCreatedElement) {
             aCreatedElem = theHelper->AddFace( node[0], node[1], node[2], noID, force3d );
-            if (aFaceGroupByHybridId.size() && !aFaceGroupByHybridId[iElem].empty())
+            // add iElem < aFaceGroupByHybridId.size() to avoid crash if imprinting with hexa core with MeshGems <= 2.4-5
+            if ( iElem < (int)aFaceGroupByHybridId.size() && !aFaceGroupByHybridId[iElem].empty() ) {
               addElemInMeshGroup(theHelper->GetMesh(), aCreatedElem, aFaceGroupByHybridId[iElem], groupsToRemove);
+            }
+            // add element in shape for groups on geom to work
+            if ( domainID[iElem] > 0 )
+            {
+              theMeshDS->SetMeshElementOnShape( aCreatedElem, domainID[iElem] );
+              for ( int iN = 0; iN < 3; ++iN )
+                if ( node[iN]->getshapeId() < 1 )
+                  theMeshDS->SetNodeOnFace( node[iN], domainID[iElem] );
+            }
           }
           break;
         case GmfQuadrilaterals:
           if (fullyCreatedElement) {
             aCreatedElem = theHelper->AddFace( node[0], node[1], node[2], node[3], noID, force3d );
+            // add element in shape for groups on geom to work
+            if ( domainID[iElem] > 0 )
+            {
+              theMeshDS->SetMeshElementOnShape( aCreatedElem, domainID[iElem] );
+              for ( int iN = 0; iN < 3; ++iN )
+                if ( node[iN]->getshapeId() < 1 )
+                  theMeshDS->SetNodeOnFace( node[iN], domainID[iElem] );
+            }
           }
           break;
         case GmfTetrahedra:
           if ( hasGeom )
           {
-            solidID = solidIDByDomain[ domainID[iElem]];
             if ( solidID != HOLE_ID )
             {
               aCreatedElem = theHelper->AddVolume( node[1], node[0], node[2], node[3],
@@ -958,10 +675,43 @@ static bool readGMFFile(const char*                     theFile,
                                                  noID, force3d );
           }
           break;
+        case GmfPyramids:
+          if ( hasGeom )
+          {
+            if ( solidID != HOLE_ID )
+            {
+              aCreatedElem = theHelper->AddVolume( node[3], node[2], node[1],
+                                                   node[0], node[4],
+                                                   noID, force3d );
+              theMeshDS->SetMeshElementOnShape( aCreatedElem, solidID );
+              for ( int iN = 0; iN < 5; ++iN )
+                if ( node[iN]->getshapeId() < 1 )
+                  theMeshDS->SetNodeInVolume( node[iN], solidID );
+            }
+          }
+          else
+          {
+            if ( elemSearcher ) {
+              // Issue 0020682. Avoid creating nodes and tetras at place where
+              // volumic elements already exist
+              if ( !node[1] || !node[0] || !node[2] || !node[3] || !node[4] || !node[5] )
+                continue;
+              if ( elemSearcher->FindElementsByPoint((SMESH_TNodeXYZ(node[0]) +
+                                                      SMESH_TNodeXYZ(node[1]) +
+                                                      SMESH_TNodeXYZ(node[2]) +
+                                                      SMESH_TNodeXYZ(node[3]) +
+                                                      SMESH_TNodeXYZ(node[4])) / 5.,
+                                                     SMDSAbs_Volume, foundVolumes ))
+                break;
+            }
+            aCreatedElem = theHelper->AddVolume( node[3], node[2], node[1],
+                                                   node[0], node[4],
+                                                   noID, force3d );
+          }
+          break;
         case GmfPrisms:
           if ( hasGeom )
           {
-            solidID = solidIDByDomain[ domainID[iElem]];
             if ( solidID != HOLE_ID )
             {
               aCreatedElem = theHelper->AddVolume( node[0], node[2], node[1],
@@ -997,7 +747,6 @@ static bool readGMFFile(const char*                     theFile,
         case GmfHexahedra:
           if ( hasGeom )
           {
-            solidID = solidIDByDomain[ domainID[iElem]];
             if ( solidID != HOLE_ID )
             {
               aCreatedElem = theHelper->AddVolume( node[0], node[3], node[2], node[1],
@@ -1044,6 +793,8 @@ static bool readGMFFile(const char*                     theFile,
       } // loop on elements of one type
       break;
     } // case ...
+
+    default:;
     } // switch (token)
   } // loop on tabRef
 
@@ -1055,25 +806,22 @@ static bool readGMFFile(const char*                     theFile,
         theMeshDS->RemoveFreeNode( GMFNode[i], /*sm=*/0, /*fromGroups=*/false );
   }
 
-  GmfCloseMesh(InpMsh);
-  delete [] GMFNode;
+  MGOutput->GmfCloseMesh(InpMsh);
 
   // 0022172: [CEA 790] create the groups corresponding to domains
   if ( toMakeGroupsOfDomains )
     makeDomainGroups( elemsOfDomain, theHelper );
 
 #ifdef _DEBUG_
-  MESSAGE("Nb subdomains " << subdomainId2tetraId.size());
   std::map<int, std::set<int> >::const_iterator subdomainIt = subdomainId2tetraId.begin();
-  TCollection_AsciiString aSubdomainFileName = theFile;
+  std::string aSubdomainFileName = theFile;
   aSubdomainFileName = aSubdomainFileName + ".subdomain";
-  ofstream aSubdomainFile  ( aSubdomainFileName.ToCString()  , ios::out);
+  std::ofstream aSubdomainFile  ( aSubdomainFileName  , ios::out);
 
   aSubdomainFile << "Nb subdomains " << subdomainId2tetraId.size() << std::endl;
   for(;subdomainIt != subdomainId2tetraId.end() ; ++subdomainIt) {
     int subdomainId = subdomainIt->first;
     std::set<int> tetraIds = subdomainIt->second;
-    MESSAGE("Subdomain #"<<subdomainId<<": "<<tetraIds.size()<<" tetrahedrons");
     std::set<int>::const_iterator tetraIdsIt = tetraIds.begin();
     aSubdomainFile << subdomainId << std::endl;
     for(;tetraIdsIt != tetraIds.end() ; ++tetraIdsIt) {
@@ -1088,7 +836,8 @@ static bool readGMFFile(const char*                     theFile,
 }
 
 
-static bool writeGMFFile(const char*                                     theMeshFileName,
+static bool writeGMFFile(MG_HYBRID_API*                                  MGInput,
+                         const char*                                     theMeshFileName,
                          const char*                                     theRequiredFileName,
                          const char*                                     theSolFileName,
                          const SMESH_ProxyMesh&                          theProxyMesh,
@@ -1105,34 +854,32 @@ static bool writeGMFFile(const char*                                     theMesh
                          std::map<std::vector<double>, std::string> &    enfVerticesWithGroup,
                          HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertexCoordsValues & theEnforcedVertices)
 {
-  MESSAGE("writeGMFFile w/o geometry");
-  std::cout << "!!!!!!!!!!!writeGMFFile w/o geometry..." << std::endl;
   std::string tmpStr;
   int idx, idxRequired = 0, idxSol = 0;
   //tabg each dummyint
-  const int dummyint = 0;
+  //const int dummyint = 0;
   const int dummyint1 = 1;
   const int dummyint2 = 2;
   const int dummyint3 = 3;
   const int dummyint4 = 4;
-  const int dummyint5 = 5;
-  const int dummyint6 = 6; //are interesting for layers
+  const int enforcedTag = HYBRIDPlugin_Hypothesis::EnforcedTag();
+  //const int dummyint6 = 6; //are interesting for layers
   HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertexCoordsValues::const_iterator vertexIt;
   std::vector<double> enfVertexSizes;
   const SMDS_MeshElement* elem;
-  TIDSortedElemSet anElemSet, theKeptEnforcedEdges, theKeptEnforcedTriangles;
+  TIDSortedElemSet anElemSetTri, anElemSetQuad, theKeptEnforcedEdges, theKeptEnforcedTriangles;
   SMDS_ElemIteratorPtr nodeIt;
   std::vector <const SMDS_MeshNode*> theEnforcedNodeByHybridId;
-  map<const SMDS_MeshNode*,int> anEnforcedNodeToHybridIdMap, anExistingEnforcedNodeToHybridIdMap;
+  std::map<const SMDS_MeshNode*,int> anEnforcedNodeToHybridIdMap, anExistingEnforcedNodeToHybridIdMap;
   std::vector< const SMDS_MeshElement* > foundElems;
-  map<const SMDS_MeshNode*,TopAbs_State> aNodeToTopAbs_StateMap;
+  std::map<const SMDS_MeshNode*,TopAbs_State> aNodeToTopAbs_StateMap;
   int nbFoundElems;
   HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap::iterator elemIt;
   TIDSortedElemSet::iterator elemSetIt;
   bool isOK;
   SMESH_Mesh* theMesh = theHelper.GetMesh();
   const bool hasGeom = theMesh->HasShapeToMesh();
-  auto_ptr< SMESH_ElementSearcher > pntCls
+  SMESHUtils::Deleter< SMESH_ElementSearcher > pntCls
     ( SMESH_MeshAlgos::GetElementSearcher(*theMesh->GetMeshDS()));
   
   int nbEnforcedVertices = theEnforcedVertices.size();
@@ -1149,7 +896,7 @@ static bool writeGMFFile(const char*                                     theMesh
   if ( nbFaces == 0 )
     return false;
   
-  idx = GmfOpenMesh(theMeshFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
+  idx = MGInput->GmfOpenMesh(theMeshFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
   if (!idx)
     return false;
   
@@ -1160,15 +907,23 @@ static bool writeGMFFile(const char*                                     theMesh
   while ( eIt->more() )
   {
     elem = eIt->next();
-    anElemSet.insert(elem);
     nodeIt = elem->nodesIterator();
     nbNodes = elem->NbCornerNodes();
+    if (nbNodes == 3)
+      anElemSetTri.insert(elem);
+    else if (nbNodes == 4)
+      anElemSetQuad.insert(elem);
+    else
+    {
+      std::cout << "Unexpected number of nodes: " << nbNodes << std::endl;
+      throw ("Unexpected number of nodes" );
+    }
     while ( nodeIt->more() && nbNodes--)
     {
       // find HYBRID ID
       const SMDS_MeshNode* node = castToNode( nodeIt->next() );
       int newId = aNodeToHybridIdMap.size() + 1; // hybrid ids count from 1
-      aNodeToHybridIdMap.insert( make_pair( node, newId ));
+      aNodeToHybridIdMap.insert( std::make_pair( node, newId ));
     }
   }
   
@@ -1190,7 +945,7 @@ static bool writeGMFFile(const char*                                     theMesh
         isOK = false;
         break;
       }
-      aNodeToTopAbs_StateMap.insert( make_pair( node, result ));
+      aNodeToTopAbs_StateMap.insert( std::make_pair( node, result ));
     }
     if (isOK) {
       nodeIt = elem->nodesIterator();
@@ -1208,13 +963,13 @@ static bool writeGMFFile(const char*                                     theMesh
         if (nbFoundElems ==0) {
           if ((*aNodeToTopAbs_StateMap.find(node)).second == TopAbs_IN) {
             newId = aNodeToHybridIdMap.size() + anEnforcedNodeToHybridIdMap.size() + 1; // hybrid ids count from 1
-            anEnforcedNodeToHybridIdMap.insert( make_pair( node, newId ));
+            anEnforcedNodeToHybridIdMap.insert( std::make_pair( node, newId ));
           }
         }
         else if (nbFoundElems ==1) {
           const SMDS_MeshNode* existingNode = (SMDS_MeshNode*) foundElems.at(0);
           newId = (*aNodeToHybridIdMap.find(existingNode)).second;
-          anExistingEnforcedNodeToHybridIdMap.insert( make_pair( node, newId ));
+          anExistingEnforcedNodeToHybridIdMap.insert( std::make_pair( node, newId ));
         }
         else
           isOK = false;
@@ -1245,7 +1000,7 @@ static bool writeGMFFile(const char*                                     theMesh
         isOK = false;
         break;
       }
-      aNodeToTopAbs_StateMap.insert( make_pair( node, result ));
+      aNodeToTopAbs_StateMap.insert( std::make_pair( node, result ));
     }
     if (isOK) {
       nodeIt = elem->nodesIterator();
@@ -1262,13 +1017,13 @@ static bool writeGMFFile(const char*                                     theMesh
         if (nbFoundElems ==0) {
           if ((*aNodeToTopAbs_StateMap.find(node)).second == TopAbs_IN) {
             newId = aNodeToHybridIdMap.size() + anEnforcedNodeToHybridIdMap.size() + 1; // hybrid ids count from 1
-            anEnforcedNodeToHybridIdMap.insert( make_pair( node, newId ));
+            anEnforcedNodeToHybridIdMap.insert( std::make_pair( node, newId ));
           }
         }
         else if (nbFoundElems ==1) {
           const SMDS_MeshNode* existingNode = (SMDS_MeshNode*) foundElems.at(0);
           newId = (*aNodeToHybridIdMap.find(existingNode)).second;
-          anExistingEnforcedNodeToHybridIdMap.insert( make_pair( node, newId ));
+          anExistingEnforcedNodeToHybridIdMap.insert( std::make_pair( node, newId ));
         }
         else
           isOK = false;
@@ -1286,7 +1041,7 @@ static bool writeGMFFile(const char*                                     theMesh
   std::cout << "aNodeToHybridIdMap.size(): "<<aNodeToHybridIdMap.size()<<std::endl;
 #endif
   theNodeByHybridId.resize( aNodeToHybridIdMap.size() );
-  map<const SMDS_MeshNode*,int>::const_iterator n2id = aNodeToHybridIdMap.begin();
+  std::map<const SMDS_MeshNode*,int>::const_iterator n2id = aNodeToHybridIdMap.begin();
   for ( ; n2id != aNodeToHybridIdMap.end(); ++ n2id)
   {
 //     std::cout << "n2id->first: "<<n2id->first<<std::endl;
@@ -1301,17 +1056,17 @@ static bool writeGMFFile(const char*                                     theMesh
   n2id = anEnforcedNodeToHybridIdMap.begin();
   for ( ; n2id != anEnforcedNodeToHybridIdMap.end(); ++ n2id)
   {
-    if (n2id->second > aNodeToHybridIdMap.size()) {
+    if (n2id->second > (int)aNodeToHybridIdMap.size()) {
       theEnforcedNodeByHybridId[ n2id->second - aNodeToHybridIdMap.size() - 1 ] = n2id->first; // hybrid ids count from 1
     }
   }
   
   
   //========================== NODES ==========================
-  vector<const SMDS_MeshNode*> theOrderedNodes, theRequiredNodes;
+  std::vector<const SMDS_MeshNode*> theOrderedNodes, theRequiredNodes;
   std::set< std::vector<double> > nodesCoords;
-  vector<const SMDS_MeshNode*>::const_iterator hybridNodeIt = theNodeByHybridId.begin();
-  vector<const SMDS_MeshNode*>::const_iterator after  = theNodeByHybridId.end();
+  std::vector<const SMDS_MeshNode*>::const_iterator hybridNodeIt = theNodeByHybridId.begin();
+  std::vector<const SMDS_MeshNode*>::const_iterator after  = theNodeByHybridId.end();
   
   (theNodeByHybridId.size() <= 1) ? tmpStr = " node" : " nodes";
   std::cout << theNodeByHybridId.size() << tmpStr << " from mesh ..." << std::endl;
@@ -1358,22 +1113,6 @@ static bool writeGMFFile(const char*                                     theMesh
       continue;
     }
     
-//     gp_Pnt myPoint(node->X(),node->Y(),node->Z());
-//     nbFoundElems = pntCls->FindElementsByPoint(myPoint, SMDSAbs_Node, foundElems);
-//     if (nbFoundElems ==0) {
-//       std::cout << " not found" << std::endl;
-//       if ((*aNodeToTopAbs_StateMap.find(node)).second == TopAbs_IN) {
-//         nodesCoords.insert(coords);
-//         theOrderedNodes.push_back(node);
-//       }
-//     }
-//     else {
-//       std::cout << " found in initial mesh" << std::endl;
-//       const SMDS_MeshNode* existingNode = (SMDS_MeshNode*) foundElems.at(0);
-//       nodesCoords.insert(coords);
-//       theOrderedNodes.push_back(existingNode);
-//     }
-    
 #ifdef _DEBUG_
     std::cout << " not found" << std::endl;
 #endif
@@ -1424,29 +1163,6 @@ static bool writeGMFFile(const char*                                     theMesh
       continue;
     }
     
-//     nbFoundElems = pntCls->FindElementsByPoint(myPoint, SMDSAbs_Node, foundElems);
-//     if (nbFoundElems ==0) {
-//       std::cout << " not found" << std::endl;
-//       if (result == TopAbs_IN) {
-//         nodesCoords.insert(coords);
-//         theRequiredNodes.push_back(node);
-//       }
-//     }
-//     else {
-//       std::cout << " found in initial mesh" << std::endl;
-//       const SMDS_MeshNode* existingNode = (SMDS_MeshNode*) foundElems.at(0);
-// //       nodesCoords.insert(coords);
-//       theRequiredNodes.push_back(existingNode);
-//     }
-//     
-//     
-//     
-//     if (pntCls->FindElementsByPoint(myPoint, SMDSAbs_Node, foundElems) == 0)
-//       continue;
-
-//     if ( result != TopAbs_IN )
-//       continue;
-    
 #ifdef _DEBUG_
     std::cout << " not found" << std::endl;
 #endif
@@ -1459,7 +1175,6 @@ static bool writeGMFFile(const char*                                     theMesh
   int solSize = 0;
   std::vector<std::vector<double> > ReqVerTab;
   if (nbEnforcedVertices) {
-//    ReqVerTab.clear();
     (nbEnforcedVertices <= 1) ? tmpStr = " node" : " nodes";
     std::cout << nbEnforcedVertices << tmpStr << " from enforced vertices ..." << std::endl;
     // Iterate over the enforced vertices
@@ -1472,11 +1187,6 @@ static bool writeGMFFile(const char*                                     theMesh
       TopAbs_State result = pntCls->GetPointState( myPoint );
       if ( result == TopAbs_OUT )
         continue;
-      //if (pntCls->FindElementsByPoint(myPoint, SMDSAbs_Node, foundElems) == 0)
-      //continue;
-
-//       if ( result != TopAbs_IN )
-//         continue;
       std::vector<double> coords;
       coords.push_back(x);
       coords.push_back(y);
@@ -1489,39 +1199,37 @@ static bool writeGMFFile(const char*                                     theMesh
   
   
   // GmfVertices
-  std::cout << "Begin writting required nodes in GmfVertices" << std::endl;
+  std::cout << "Begin writing required nodes in GmfVertices" << std::endl;
   std::cout << "Nb vertices: " << theOrderedNodes.size() << std::endl;
-  GmfSetKwd(idx, GmfVertices, theOrderedNodes.size()); //theOrderedNodes.size()+solSize)
+  MGInput->GmfSetKwd(idx, GmfVertices, theOrderedNodes.size());
   for (hybridNodeIt = theOrderedNodes.begin();hybridNodeIt != theOrderedNodes.end();++hybridNodeIt) {
-    GmfSetLin(idx, GmfVertices, (*hybridNodeIt)->X(), (*hybridNodeIt)->Y(), (*hybridNodeIt)->Z(), dummyint1);
+    MGInput->GmfSetLin(idx, GmfVertices, (*hybridNodeIt)->X(), (*hybridNodeIt)->Y(), (*hybridNodeIt)->Z(), dummyint1);
   }
 
-  std::cout << "End writting required nodes in GmfVertices" << std::endl;
+  std::cout << "End writing required nodes in GmfVertices" << std::endl;
 
   if (requiredNodes + solSize) {
-    std::cout << "Begin writting in req and sol file" << std::endl;
+    std::cout << "Begin writing in req and sol file" << std::endl;
     aNodeGroupByHybridId.resize( requiredNodes + solSize );
-    idxRequired = GmfOpenMesh(theRequiredFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
+    idxRequired = MGInput->GmfOpenMesh(theRequiredFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
     if (!idxRequired) {
-      GmfCloseMesh(idx);
+      MGInput->GmfCloseMesh(idx);
       return false;
     }
-    idxSol = GmfOpenMesh(theSolFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
+    idxSol = MGInput->GmfOpenMesh(theSolFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
     if (!idxSol) {
-      GmfCloseMesh(idx);
+      MGInput->GmfCloseMesh(idx);
       if (idxRequired)
-        GmfCloseMesh(idxRequired);
+        MGInput->GmfCloseMesh(idxRequired);
       return false;
     }
     int TypTab[] = {GmfSca};
     double ValTab[] = {0.0};
-    GmfSetKwd(idxRequired, GmfVertices, requiredNodes + solSize);
-    GmfSetKwd(idxSol, GmfSolAtVertices, requiredNodes + solSize, 1, TypTab);
-//     int usedEnforcedNodes = 0;
-//     std::string gn = "";
+    MGInput->GmfSetKwd(idxRequired, GmfVertices, requiredNodes + solSize);
+    MGInput->GmfSetKwd(idxSol, GmfSolAtVertices, requiredNodes + solSize, 1, TypTab);
     for (hybridNodeIt = theRequiredNodes.begin();hybridNodeIt != theRequiredNodes.end();++hybridNodeIt) {
-      GmfSetLin(idxRequired, GmfVertices, (*hybridNodeIt)->X(), (*hybridNodeIt)->Y(), (*hybridNodeIt)->Z(), dummyint2);
-      GmfSetLin(idxSol, GmfSolAtVertices, ValTab);
+      MGInput->GmfSetLin(idxRequired, GmfVertices, (*hybridNodeIt)->X(), (*hybridNodeIt)->Y(), (*hybridNodeIt)->Z(), dummyint2);
+      MGInput->GmfSetLin(idxSol, GmfSolAtVertices, ValTab);
       if (theEnforcedNodes.find((*hybridNodeIt)) != theEnforcedNodes.end())
         gn = theEnforcedNodes.find((*hybridNodeIt))->second;
       aNodeGroupByHybridId[usedEnforcedNodes] = gn;
@@ -1534,28 +1242,24 @@ static bool writeGMFFile(const char*                                     theMesh
       std::cout << "enfVertexSizes.at("<<i<<"): " << enfVertexSizes.at(i) << std::endl;
 #endif
       double solTab[] = {enfVertexSizes.at(i)};
-      GmfSetLin(idxRequired, GmfVertices, ReqVerTab[i][0], ReqVerTab[i][1], ReqVerTab[i][2], dummyint3);
-      GmfSetLin(idxSol, GmfSolAtVertices, solTab);
+      MGInput->GmfSetLin(idxRequired, GmfVertices, ReqVerTab[i][0], ReqVerTab[i][1], ReqVerTab[i][2], dummyint3);
+      MGInput->GmfSetLin(idxSol, GmfSolAtVertices, solTab);
       aNodeGroupByHybridId[usedEnforcedNodes] = enfVerticesWithGroup.find(ReqVerTab[i])->second;
 #ifdef _DEBUG_
       std::cout << "aNodeGroupByHybridId["<<usedEnforcedNodes<<"] = \""<<aNodeGroupByHybridId[usedEnforcedNodes]<<"\""<<std::endl;
 #endif
       usedEnforcedNodes++;
     }
-    std::cout << "End writting in req and sol file" << std::endl;
+    std::cout << "End writing in req and sol file" << std::endl;
   }
 
-  int nedge[2], ntri[3];
+  int nedge[2], ntri[3], nquad[4];
     
   // GmfEdges
   int usedEnforcedEdges = 0;
   if (theKeptEnforcedEdges.size()) {
     anEdgeGroupByHybridId.resize( theKeptEnforcedEdges.size() );
-//    idxRequired = GmfOpenMesh(theRequiredFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
-//    if (!idxRequired)
-//      return false;
-    GmfSetKwd(idx, GmfEdges, theKeptEnforcedEdges.size());
-//    GmfSetKwd(idxRequired, GmfEdges, theKeptEnforcedEdges.size());
+    MGInput->GmfSetKwd(idx, GmfEdges, theKeptEnforcedEdges.size());
     for(elemSetIt = theKeptEnforcedEdges.begin() ; elemSetIt != theKeptEnforcedEdges.end() ; ++elemSetIt) {
       elem = (*elemSetIt);
       nodeIt = elem->nodesIterator();
@@ -1563,7 +1267,7 @@ static bool writeGMFFile(const char*                                     theMesh
       while ( nodeIt->more() ) {
         // find HYBRID ID
         const SMDS_MeshNode* node = castToNode( nodeIt->next() );
-        map< const SMDS_MeshNode*,int >::iterator it = anEnforcedNodeToHybridIdMap.find(node);
+        std::map< const SMDS_MeshNode*,int >::iterator it = anEnforcedNodeToHybridIdMap.find(node);
         if (it == anEnforcedNodeToHybridIdMap.end()) {
           it = anExistingEnforcedNodeToHybridIdMap.find(node);
           if (it == anEnforcedNodeToHybridIdMap.end())
@@ -1572,57 +1276,63 @@ static bool writeGMFFile(const char*                                     theMesh
         nedge[index] = it->second;
         index++;
       }
-      GmfSetLin(idx, GmfEdges, nedge[0], nedge[1], dummyint4);
+      MGInput->GmfSetLin(idx, GmfEdges, nedge[0], nedge[1], dummyint4);
       anEdgeGroupByHybridId[usedEnforcedEdges] = theEnforcedEdges.find(elem)->second;
-//      GmfSetLin(idxRequired, GmfEdges, nedge[0], nedge[1], dummyint);
       usedEnforcedEdges++;
     }
-//    GmfCloseMesh(idxRequired);
   }
 
 
   if (usedEnforcedEdges) {
-    GmfSetKwd(idx, GmfRequiredEdges, usedEnforcedEdges);
+    MGInput->GmfSetKwd(idx, GmfRequiredEdges, usedEnforcedEdges);
     for (int enfID=1;enfID<=usedEnforcedEdges;enfID++) {
-      GmfSetLin(idx, GmfRequiredEdges, enfID);
+      MGInput->GmfSetLin(idx, GmfRequiredEdges, enfID);
     }
   }
 
   // GmfTriangles
   int usedEnforcedTriangles = 0;
-  if (anElemSet.size()+theKeptEnforcedTriangles.size()) {
-    aFaceGroupByHybridId.resize( anElemSet.size()+theKeptEnforcedTriangles.size() );
-    GmfSetKwd(idx, GmfTriangles, anElemSet.size()+theKeptEnforcedTriangles.size());
+  if (anElemSetTri.size()+theKeptEnforcedTriangles.size())
+  {
+    aFaceGroupByHybridId.resize( anElemSetTri.size()+theKeptEnforcedTriangles.size() );
+    MGInput->GmfSetKwd(idx, GmfTriangles, anElemSetTri.size()+theKeptEnforcedTriangles.size());
     int k=0;
-    for(elemSetIt = anElemSet.begin() ; elemSetIt != anElemSet.end() ; ++elemSetIt,++k) {
+    for(elemSetIt = anElemSetTri.begin() ; elemSetIt != anElemSetTri.end() ; ++elemSetIt,++k)
+    {
       elem = (*elemSetIt);
       theFaceByHybridId.push_back( elem );
       nodeIt = elem->nodesIterator();
       int index=0;
-      for ( int j = 0; j < 3; ++j ) {
+      for ( int j = 0; j < 3; ++j )
+      {
         // find HYBRID ID
         const SMDS_MeshNode* node = castToNode( nodeIt->next() );
-        map< const SMDS_MeshNode*,int >::iterator it = aNodeToHybridIdMap.find(node);
+        std::map< const SMDS_MeshNode*,int >::iterator it = aNodeToHybridIdMap.find(node);
         if (it == aNodeToHybridIdMap.end())
           throw "Node not found";
         ntri[index] = it->second;
         index++;
       }
-      GmfSetLin(idx, GmfTriangles, ntri[0], ntri[1], ntri[2], dummyint5);
+      MGInput->GmfSetLin(idx, GmfTriangles, ntri[0], ntri[1], ntri[2], /*tag=*/elem->getshapeId() );
       aFaceGroupByHybridId[k] = "";
     }
-    if ( !theHelper.GetMesh()->HasShapeToMesh() )
-      SMESHUtils::FreeVector( theFaceByHybridId );
-    if (theKeptEnforcedTriangles.size()) {
-      for(elemSetIt = theKeptEnforcedTriangles.begin() ; elemSetIt != theKeptEnforcedTriangles.end() ; ++elemSetIt,++k) {
+    
+    if ( !theHelper.GetMesh()->HasShapeToMesh() ) SMESHUtils::FreeVector( theFaceByHybridId ); 
+    std::cout << "Enforced triangles size " << theKeptEnforcedTriangles.size() << std::endl;
+    if (theKeptEnforcedTriangles.size())
+    {
+      for(elemSetIt = theKeptEnforcedTriangles.begin() ; elemSetIt != theKeptEnforcedTriangles.end() ; ++elemSetIt,++k)
+      {
         elem = (*elemSetIt);
         nodeIt = elem->nodesIterator();
         int index=0;
-        for ( int j = 0; j < 3; ++j ) {
+        for ( int j = 0; j < 3; ++j )
+        {
           // find HYBRID ID
           const SMDS_MeshNode* node = castToNode( nodeIt->next() );
-          map< const SMDS_MeshNode*,int >::iterator it = anEnforcedNodeToHybridIdMap.find(node);
-          if (it == anEnforcedNodeToHybridIdMap.end()) {
+          std::map< const SMDS_MeshNode*,int >::iterator it = anEnforcedNodeToHybridIdMap.find(node);
+          if (it == anEnforcedNodeToHybridIdMap.end())
+          {
             it = anExistingEnforcedNodeToHybridIdMap.find(node);
             if (it == anEnforcedNodeToHybridIdMap.end())
               throw "Node not found";
@@ -1630,7 +1340,7 @@ static bool writeGMFFile(const char*                                     theMesh
           ntri[index] = it->second;
           index++;
         }
-        GmfSetLin(idx, GmfTriangles, ntri[0], ntri[1], ntri[2], dummyint6);
+        MGInput->GmfSetLin(idx, GmfTriangles, ntri[0], ntri[1], ntri[2], enforcedTag);
         aFaceGroupByHybridId[k] = theEnforcedTriangles.find(elem)->second;
         usedEnforcedTriangles++;
       }
@@ -1638,1233 +1348,50 @@ static bool writeGMFFile(const char*                                     theMesh
   }
 
   
-  if (usedEnforcedTriangles) {
-    GmfSetKwd(idx, GmfRequiredTriangles, usedEnforcedTriangles);
-    for (int enfID=1;enfID<=usedEnforcedTriangles;enfID++)
-      GmfSetLin(idx, GmfRequiredTriangles, anElemSet.size()+enfID);
-  }
-
-  GmfCloseMesh(idx);
-  if (idxRequired)
-    GmfCloseMesh(idxRequired);
-  if (idxSol)
-    GmfCloseMesh(idxSol);
-  
-  return true;
-  
-}
-
-// static bool writeGMFFile(const char*                                    theMeshFileName,
-//                         const char*                                     theRequiredFileName,
-//                         const char*                                     theSolFileName,
-//                         SMESH_MesherHelper&                             theHelper,
-//                         const SMESH_ProxyMesh&                          theProxyMesh,
-//                         std::map <int,int> &                            theNodeId2NodeIndexMap,
-//                         std::map <int,int> &                            theSmdsToHybridIdMap,
-//                         std::map <int,const SMDS_MeshNode*> &           theHybridIdToNodeMap,
-//                         TIDSortedNodeSet &                              theEnforcedNodes,
-//                         TIDSortedElemSet &                              theEnforcedEdges,
-//                         TIDSortedElemSet &                              theEnforcedTriangles,
-// //                         TIDSortedElemSet &                              theEnforcedQuadrangles,
-//                         HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertexCoordsValues & theEnforcedVertices)
-// {
-//   MESSAGE("writeGMFFile with geometry");
-//   int idx, idxRequired, idxSol;
-//   int nbv, nbev, nben, aHybridID = 0;
-//   const int dummyint = 0;
-//   HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertexCoordsValues::const_iterator vertexIt;
-//   std::vector<double> enfVertexSizes;
-//   TIDSortedNodeSet::const_iterator enfNodeIt;
-//   const SMDS_MeshNode* node;
-//   SMDS_NodeIteratorPtr nodeIt;
-// 
-//   idx = GmfOpenMesh(theMeshFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
-//   if (!idx)
-//     return false;
-//   
-//   SMESHDS_Mesh * theMeshDS = theHelper.GetMeshDS();
-//   
-//   /* ========================== NODES ========================== */
-//   // NB_NODES
-//   nbv = theMeshDS->NbNodes();
-//   if ( nbv == 0 )
-//     return false;
-//   nbev = theEnforcedVertices.size();
-//   nben = theEnforcedNodes.size();
-//   
-//   // Issue 020674: EDF 870 SMESH: Mesh generated by Netgen not usable by HYBRID
-//   // The problem is in nodes on degenerated edges, we need to skip nodes which are free
-//   // and replace not-free nodes on edges by the node on vertex
-//   TNodeNodeMap n2nDegen; // map a node on degenerated edge to a node on vertex
-//   TNodeNodeMap::iterator n2nDegenIt;
-//   if ( theHelper.HasDegeneratedEdges() )
-//   {
-//     set<int> checkedSM;
-//     for (TopExp_Explorer e(theMeshDS->ShapeToMesh(), TopAbs_EDGE ); e.More(); e.Next())
-//     {
-//       SMESH_subMesh* sm = theHelper.GetMesh()->GetSubMesh( e.Current() );
-//       if ( checkedSM.insert( sm->GetId() ).second && theHelper.IsDegenShape(sm->GetId() ))
-//       {
-//         if ( SMESHDS_SubMesh* smDS = sm->GetSubMeshDS() )
-//         {
-//           TopoDS_Shape vertex = TopoDS_Iterator( e.Current() ).Value();
-//           const SMDS_MeshNode* vNode = SMESH_Algo::VertexNode( TopoDS::Vertex( vertex ), theMeshDS);
-//           {
-//             SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
-//             while ( nIt->more() )
-//               n2nDegen.insert( make_pair( nIt->next(), vNode ));
-//           }
-//         }
-//       }
-//     }
-//   }
-//   
-//   const bool isQuadMesh = 
-//     theHelper.GetMesh()->NbEdges( ORDER_QUADRATIC ) ||
-//     theHelper.GetMesh()->NbFaces( ORDER_QUADRATIC ) ||
-//     theHelper.GetMesh()->NbVolumes( ORDER_QUADRATIC );
-// 
-//   std::vector<std::vector<double> > VerTab;
-//   std::set<std::vector<double> > VerMap;
-//   VerTab.clear();
-//   std::vector<double> aVerTab;
-//   // Loop from 1 to NB_NODES
-// 
-//   nodeIt = theMeshDS->nodesIterator();
-//   
-//   while ( nodeIt->more() )
-//   {
-//     node = nodeIt->next();
-//     if ( isQuadMesh && theHelper.IsMedium( node )) // Issue 0021238
-//       continue;
-//     if ( n2nDegen.count( node ) ) // Issue 0020674
-//       continue;
-// 
-//     std::vector<double> coords;
-//     coords.push_back(node->X());
-//     coords.push_back(node->Y());
-//     coords.push_back(node->Z());
-//     if (VerMap.find(coords) != VerMap.end()) {
-//       aHybridID = theSmdsToHybridIdMap[node->GetID()];
-//       theHybridIdToNodeMap[theSmdsToHybridIdMap[node->GetID()]] = node;
-//       continue;
-//     }
-//     VerTab.push_back(coords);
-//     VerMap.insert(coords);
-//     aHybridID++;
-//     theSmdsToHybridIdMap.insert( make_pair( node->GetID(), aHybridID ));
-//     theHybridIdToNodeMap.insert( make_pair( aHybridID, node ));
-//   }
-//   
-//   
-//   /* ENFORCED NODES ========================== */
-//   if (nben) {
-//     std::cout << "Add " << nben << " enforced nodes to input .mesh file" << std::endl;
-//     for(enfNodeIt = theEnforcedNodes.begin() ; enfNodeIt != theEnforcedNodes.end() ; ++enfNodeIt) {
-//       double x = (*enfNodeIt)->X();
-//       double y = (*enfNodeIt)->Y();
-//       double z = (*enfNodeIt)->Z();
-//       // Test if point is inside shape to mesh
-//       gp_Pnt myPoint(x,y,z);
-//       BRepClass3d_SolidClassifier scl(theMeshDS->ShapeToMesh());
-//       scl.Perform(myPoint, 1e-7);
-//       TopAbs_State result = scl.State();
-//       if ( result != TopAbs_IN )
-//         continue;
-//       std::vector<double> coords;
-//       coords.push_back(x);
-//       coords.push_back(y);
-//       coords.push_back(z);
-//       if (theEnforcedVertices.find(coords) != theEnforcedVertices.end())
-//         continue;
-//       if (VerMap.find(coords) != VerMap.end())
-//         continue;
-//       VerTab.push_back(coords);
-//       VerMap.insert(coords);
-//       aHybridID++;
-//       theNodeId2NodeIndexMap.insert( make_pair( (*enfNodeIt)->GetID(), aHybridID ));
-//     }
-//   }
-//     
-//   
-//   /* ENFORCED VERTICES ========================== */
-//   int solSize = 0;
-//   std::vector<std::vector<double> > ReqVerTab;
-//   ReqVerTab.clear();
-//   if (nbev) {
-//     std::cout << "Add " << nbev << " enforced vertices to input .mesh file" << std::endl;
-//     for(vertexIt = theEnforcedVertices.begin() ; vertexIt != theEnforcedVertices.end() ; ++vertexIt) {
-//       double x = vertexIt->first[0];
-//       double y = vertexIt->first[1];
-//       double z = vertexIt->first[2];
-//       // Test if point is inside shape to mesh
-//       gp_Pnt myPoint(x,y,z);
-//       BRepClass3d_SolidClassifier scl(theMeshDS->ShapeToMesh());
-//       scl.Perform(myPoint, 1e-7);
-//       TopAbs_State result = scl.State();
-//       if ( result != TopAbs_IN )
-//         continue;
-//       enfVertexSizes.push_back(vertexIt->second);
-//       std::vector<double> coords;
-//       coords.push_back(x);
-//       coords.push_back(y);
-//       coords.push_back(z);
-//       if (VerMap.find(coords) != VerMap.end())
-//         continue;
-//       ReqVerTab.push_back(coords);
-//       VerMap.insert(coords);
-//       solSize++;
-//     }
-//   }
-// 
-//   
-//   /* ========================== FACES ========================== */
-//   
-//   int nbTriangles = 0/*, nbQuadrangles = 0*/, aSmdsID;
-//   TopTools_IndexedMapOfShape facesMap, trianglesMap/*, quadranglesMap*/;
-//   TIDSortedElemSet::const_iterator elemIt;
-//   const SMESHDS_SubMesh* theSubMesh;
-//   TopoDS_Shape aShape;
-//   SMDS_ElemIteratorPtr itOnSubMesh, itOnSubFace;
-//   const SMDS_MeshElement* aFace;
-//   map<int,int>::const_iterator itOnMap;
-//   std::vector<std::vector<int> > tt, qt,et;
-//   tt.clear();
-//   qt.clear();
-//   et.clear();
-//   std::vector<int> att, aqt, aet;
-//   
-//   TopExp::MapShapes( theMeshDS->ShapeToMesh(), TopAbs_FACE, facesMap );
-// 
-//   for ( int i = 1; i <= facesMap.Extent(); ++i )
-//     if (( theSubMesh  = theProxyMesh.GetSubMesh( facesMap(i))))
-//     {
-//       SMDS_ElemIteratorPtr it = theSubMesh->GetElements();
-//       while (it->more())
-//       {
-//         const SMDS_MeshElement *elem = it->next();
-//         int nbCornerNodes = elem->NbCornerNodes();
-//         if (nbCornerNodes == 3)
-//         {
-//           trianglesMap.Add(facesMap(i));
-//           nbTriangles ++;
-//         }
-// //         else if (nbCornerNodes == 4)
-// //         {
-// //           quadranglesMap.Add(facesMap(i));
-// //           nbQuadrangles ++;
-// //         }
-//       }
-//     }
-//     
-//   /* TRIANGLES ========================== */
-//   if (nbTriangles) {
-//     for ( int i = 1; i <= trianglesMap.Extent(); i++ )
-//     {
-//       aShape = trianglesMap(i);
-//       theSubMesh = theProxyMesh.GetSubMesh(aShape);
-//       if ( !theSubMesh ) continue;
-//       itOnSubMesh = theSubMesh->GetElements();
-//       while ( itOnSubMesh->more() )
-//       {
-//         aFace = itOnSubMesh->next();
-//         itOnSubFace = aFace->nodesIterator();
-//         att.clear();
-//         for ( int j = 0; j < 3; ++j ) {
-//           // find HYBRID ID
-//           node = castToNode( itOnSubFace->next() );
-//           if (( n2nDegenIt = n2nDegen.find( node )) != n2nDegen.end() )
-//             node = n2nDegenIt->second;
-//           aSmdsID = node->GetID();
-//           itOnMap = theSmdsToHybridIdMap.find( aSmdsID );
-//           ASSERT( itOnMap != theSmdsToHybridIdMap.end() );
-//           att.push_back((*itOnMap).second);
-//         }
-//         tt.push_back(att);
-//       }
-//     }
-//   }
-// 
-//   if (theEnforcedTriangles.size()) {
-//     std::cout << "Add " << theEnforcedTriangles.size() << " enforced triangles to input .mesh file" << std::endl;
-//     // Iterate over the enforced triangles
-//     for(elemIt = theEnforcedTriangles.begin() ; elemIt != theEnforcedTriangles.end() ; ++elemIt) {
-//       aFace = (*elemIt);
-//       itOnSubFace = aFace->nodesIterator();
-//       bool isOK = true;
-//       att.clear();
-//       
-//       for ( int j = 0; j < 3; ++j ) {
-//         node = castToNode( itOnSubFace->next() );
-//         if (( n2nDegenIt = n2nDegen.find( node )) != n2nDegen.end() )
-//           node = n2nDegenIt->second;
-// //         std::cout << node;
-//         double x = node->X();
-//         double y = node->Y();
-//         double z = node->Z();
-//         // Test if point is inside shape to mesh
-//         gp_Pnt myPoint(x,y,z);
-//         BRepClass3d_SolidClassifier scl(theMeshDS->ShapeToMesh());
-//         scl.Perform(myPoint, 1e-7);
-//         TopAbs_State result = scl.State();
-//         if ( result != TopAbs_IN ) {
-//           isOK = false;
-//           theEnforcedTriangles.erase(elemIt);
-//           continue;
-//         }
-//         std::vector<double> coords;
-//         coords.push_back(x);
-//         coords.push_back(y);
-//         coords.push_back(z);
-//         if (VerMap.find(coords) != VerMap.end()) {
-//           att.push_back(theNodeId2NodeIndexMap[node->GetID()]);
-//           continue;
-//         }
-//         VerTab.push_back(coords);
-//         VerMap.insert(coords);
-//         aHybridID++;
-//         theNodeId2NodeIndexMap.insert( make_pair( node->GetID(), aHybridID ));
-//         att.push_back(aHybridID);
-//       }
-//       if (isOK)
-//         tt.push_back(att);
-//     }
-//   }
-// 
-// 
-//   /* ========================== EDGES ========================== */
-// 
-//   if (theEnforcedEdges.size()) {
-//     // Iterate over the enforced edges
-//     std::cout << "Add " << theEnforcedEdges.size() << " enforced edges to input .mesh file" << std::endl;
-//     for(elemIt = theEnforcedEdges.begin() ; elemIt != theEnforcedEdges.end() ; ++elemIt) {
-//       aFace = (*elemIt);
-//       bool isOK = true;
-//       itOnSubFace = aFace->nodesIterator();
-//       aet.clear();
-//       for ( int j = 0; j < 2; ++j ) {
-//         node = castToNode( itOnSubFace->next() );
-//         if (( n2nDegenIt = n2nDegen.find( node )) != n2nDegen.end() )
-//           node = n2nDegenIt->second;
-//         double x = node->X();
-//         double y = node->Y();
-//         double z = node->Z();
-//         // Test if point is inside shape to mesh
-//         gp_Pnt myPoint(x,y,z);
-//         BRepClass3d_SolidClassifier scl(theMeshDS->ShapeToMesh());
-//         scl.Perform(myPoint, 1e-7);
-//         TopAbs_State result = scl.State();
-//         if ( result != TopAbs_IN ) {
-//           isOK = false;
-//           theEnforcedEdges.erase(elemIt);
-//           continue;
-//         }
-//         std::vector<double> coords;
-//         coords.push_back(x);
-//         coords.push_back(y);
-//         coords.push_back(z);
-//         if (VerMap.find(coords) != VerMap.end()) {
-//           aet.push_back(theNodeId2NodeIndexMap[node->GetID()]);
-//           continue;
-//         }
-//         VerTab.push_back(coords);
-//         VerMap.insert(coords);
-//         
-//         aHybridID++;
-//         theNodeId2NodeIndexMap.insert( make_pair( node->GetID(), aHybridID ));
-//         aet.push_back(aHybridID);
-//       }
-//       if (isOK)
-//         et.push_back(aet);
-//     }
-//   }
-// 
-// 
-//   /* Write vertices number */
-//   MESSAGE("Number of vertices: "<<aHybridID);
-//   MESSAGE("Size of vector: "<<VerTab.size());
-//   GmfSetKwd(idx, GmfVertices, aHybridID/*+solSize*/);
-//   for (int i=0;i<aHybridID;i++)
-//     GmfSetLin(idx, GmfVertices, VerTab[i][0], VerTab[i][1], VerTab[i][2], dummyint);
-// //   for (int i=0;i<solSize;i++) {
-// //     std::cout << ReqVerTab[i][0] <<" "<< ReqVerTab[i][1] << " "<< ReqVerTab[i][2] << std::endl;
-// //     GmfSetLin(idx, GmfVertices, ReqVerTab[i][0], ReqVerTab[i][1], ReqVerTab[i][2], dummyint);
-// //   }
-// 
-//   if (solSize) {
-//     idxRequired = GmfOpenMesh(theRequiredFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
-//     if (!idxRequired) {
-//       GmfCloseMesh(idx);
-//       return false;
-//     }
-//     idxSol = GmfOpenMesh(theSolFileName, GmfWrite, GMFVERSION, GMFDIMENSION);
-//     if (!idxSol){
-//       GmfCloseMesh(idx);
-//       if (idxRequired)
-//         GmfCloseMesh(idxRequired);
-//       return false;
-//     }
-//     
-//     int TypTab[] = {GmfSca};
-//     GmfSetKwd(idxRequired, GmfVertices, solSize);
-//     GmfSetKwd(idxSol, GmfSolAtVertices, solSize, 1, TypTab);
-//     
-//     for (int i=0;i<solSize;i++) {
-//       double solTab[] = {enfVertexSizes.at(i)};
-//       GmfSetLin(idxRequired, GmfVertices, ReqVerTab[i][0], ReqVerTab[i][1], ReqVerTab[i][2], dummyint);
-//       GmfSetLin(idxSol, GmfSolAtVertices, solTab);
-//     }
-//     GmfCloseMesh(idxRequired);
-//     GmfCloseMesh(idxSol);
-//   }
-//   
-//   /* Write triangles number */
-//   if (tt.size()) {
-//     GmfSetKwd(idx, GmfTriangles, tt.size());
-//     for (int i=0;i<tt.size();i++)
-//       GmfSetLin(idx, GmfTriangles, tt[i][0], tt[i][1], tt[i][2], dummyint);
-//   }  
-//   
-//   /* Write edges number */
-//   if (et.size()) {
-//     GmfSetKwd(idx, GmfEdges, et.size());
-//     for (int i=0;i<et.size();i++)
-//       GmfSetLin(idx, GmfEdges, et[i][0], et[i][1], dummyint);
-//   }
-// 
-//   /* QUADRANGLES ========================== */
-//   // TODO: add pyramids ?
-// //   if (nbQuadrangles) {
-// //     for ( int i = 1; i <= quadranglesMap.Extent(); i++ )
-// //     {
-// //       aShape = quadranglesMap(i);
-// //       theSubMesh = theProxyMesh.GetSubMesh(aShape);
-// //       if ( !theSubMesh ) continue;
-// //       itOnSubMesh = theSubMesh->GetElements();
-// //       for ( int j = 0; j < 4; ++j )
-// //       {
-// //         aFace = itOnSubMesh->next();
-// //         itOnSubFace = aFace->nodesIterator();
-// //         aqt.clear();
-// //         while ( itOnSubFace->more() ) {
-// //           // find HYBRID ID
-// //           aSmdsID = itOnSubFace->next()->GetID();
-// //           itOnMap = theSmdsToHybridIdMap.find( aSmdsID );
-// //           ASSERT( itOnMap != theSmdsToHybridIdMap.end() );
-// //           aqt.push_back((*itOnMap).second);
-// //         }
-// //         qt.push_back(aqt);
-// //       }
-// //     }
-// //   }
-// // 
-// //   if (theEnforcedQuadrangles.size()) {
-// //     // Iterate over the enforced triangles
-// //     for(elemIt = theEnforcedQuadrangles.begin() ; elemIt != theEnforcedQuadrangles.end() ; ++elemIt) {
-// //       aFace = (*elemIt);
-// //       bool isOK = true;
-// //       itOnSubFace = aFace->nodesIterator();
-// //       aqt.clear();
-// //       for ( int j = 0; j < 4; ++j ) {
-// //         int aNodeID = itOnSubFace->next()->GetID();
-// //         itOnMap = theNodeId2NodeIndexMap.find(aNodeID);
-// //         if (itOnMap != theNodeId2NodeIndexMap.end())
-// //           aqt.push_back((*itOnMap).second);
-// //         else {
-// //           isOK = false;
-// //           theEnforcedQuadrangles.erase(elemIt);
-// //           break;
-// //         }
-// //       }
-// //       if (isOK)
-// //         qt.push_back(aqt);
-// //     }
-// //   }
-// //  
-//   
-// //   /* Write quadrilaterals number */
-// //   if (qt.size()) {
-// //     GmfSetKwd(idx, GmfQuadrilaterals, qt.size());
-// //     for (int i=0;i<qt.size();i++)
-// //       GmfSetLin(idx, GmfQuadrilaterals, qt[i][0], qt[i][1], qt[i][2], qt[i][3], dummyint);
-// //   }
-// 
-//   GmfCloseMesh(idx);
-//   return true;
-// }
-
-
-//=======================================================================
-//function : writeFaces
-//purpose  : 
-//=======================================================================
-
-static bool writeFaces (ofstream &              theFile,
-                        const SMESH_ProxyMesh&  theMesh,
-                        const TopoDS_Shape&     theShape,
-                        const map <int,int> &   theSmdsToHybridIdMap,
-                        const map <int,int> &   theEnforcedNodeIdToHybridIdMap,
-                        HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap & theEnforcedEdges,
-                        HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap & theEnforcedTriangles)
-{
-  // record structure:
-  //
-  // NB_ELEMS DUMMY_INT
-  // Loop from 1 to NB_ELEMS
-  // NB_NODES NODE_NB_1 NODE_NB_2 ... (NB_NODES + 1) times: DUMMY_INT
-
-  TopoDS_Shape aShape;
-  const SMESHDS_SubMesh* theSubMesh;
-  const SMDS_MeshElement* aFace;
-  const char* space    = "  ";
-  const int   dummyint = 0;
-  map<int,int>::const_iterator itOnMap;
-  SMDS_ElemIteratorPtr itOnSubMesh, itOnSubFace;
-  int nbNodes, aSmdsID;
-
-  TIDSortedElemSet::const_iterator elemIt;
-  int nbEnforcedEdges       = theEnforcedEdges.size();
-  int nbEnforcedTriangles   = theEnforcedTriangles.size();
-
-  // count triangles bound to geometry
-  int nbTriangles = 0;
-
-  TopTools_IndexedMapOfShape facesMap, trianglesMap;
-  TopExp::MapShapes( theShape, TopAbs_FACE, facesMap );
-  
-  int nbFaces = facesMap.Extent();
-
-  for ( int i = 1; i <= nbFaces; ++i )
-    if (( theSubMesh  = theMesh.GetSubMesh( facesMap(i))))
-      nbTriangles += theSubMesh->NbElements();
-  std::string tmpStr;
-  (nbFaces == 0 || nbFaces == 1) ? tmpStr = " shape " : tmpStr = " shapes " ;
-  std::cout << "    " << nbFaces << tmpStr << "of 2D dimension";
-  int nbEnforcedElements = nbEnforcedEdges+nbEnforcedTriangles;
-  if (nbEnforcedElements > 0) {
-    (nbEnforcedElements == 1) ? tmpStr = "shape:" : tmpStr = "shapes:";
-    std::cout << " and" << std::endl;
-    std::cout << "    " << nbEnforcedElements 
-                        << " enforced " << tmpStr << std::endl;
-  }
-  else
-    std::cout << std::endl;
-  if (nbEnforcedEdges) {
-    (nbEnforcedEdges == 1) ? tmpStr = "edge" : tmpStr = "edges";
-    std::cout << "      " << nbEnforcedEdges << " enforced " << tmpStr << std::endl;
-  }
-  if (nbEnforcedTriangles) {
-    (nbEnforcedTriangles == 1) ? tmpStr = "triangle" : tmpStr = "triangles";
-    std::cout << "      " << nbEnforcedTriangles << " enforced " << tmpStr << std::endl;
-  }
-  std::cout << std::endl;
-
-//   theFile << space << nbTriangles << space << dummyint << std::endl;
-  std::ostringstream globalStream, localStream, aStream;
-
-  for ( int i = 1; i <= facesMap.Extent(); i++ )
+  if (usedEnforcedTriangles)
   {
-    aShape = facesMap(i);
-    theSubMesh = theMesh.GetSubMesh(aShape);
-    if ( !theSubMesh ) continue;
-    itOnSubMesh = theSubMesh->GetElements();
-    while ( itOnSubMesh->more() )
-    {
-      aFace = itOnSubMesh->next();
-      nbNodes = aFace->NbCornerNodes();
-
-      localStream << nbNodes << space;
-
-      itOnSubFace = aFace->nodesIterator();
-      for ( int j = 0; j < 3; ++j ) {
-        // find HYBRID ID
-        aSmdsID = itOnSubFace->next()->GetID();
-        itOnMap = theSmdsToHybridIdMap.find( aSmdsID );
-        // if ( itOnMap == theSmdsToHybridIdMap.end() ) {
-        //   cout << "not found node: " << aSmdsID << endl;
-        //   return false;
-        // }
-        ASSERT( itOnMap != theSmdsToHybridIdMap.end() );
-
-        localStream << (*itOnMap).second << space ;
-      }
-
-      // (NB_NODES + 1) times: DUMMY_INT
-      for ( int j=0; j<=nbNodes; j++)
-        localStream << dummyint << space ;
-
-      localStream << std::endl;
-    }
+    MGInput->GmfSetKwd(idx, GmfRequiredTriangles, usedEnforcedTriangles);
+    for (int enfID=1;enfID<=usedEnforcedTriangles;enfID++)
+      MGInput->GmfSetLin(idx, GmfRequiredTriangles, anElemSetTri.size()+enfID);
   }
   
-  globalStream << localStream.str();
-  localStream.str("");
-
-  //
-  //        FACES : END
-  //
-
-//   //
-//   //        ENFORCED EDGES : BEGIN
-//   //
-//   
-//   // Iterate over the enforced edges
-//   int usedEnforcedEdges = 0;
-//   bool isOK;
-//   for(elemIt = theEnforcedEdges.begin() ; elemIt != theEnforcedEdges.end() ; ++elemIt) {
-//     aFace = (*elemIt);
-//     isOK = true;
-//     itOnSubFace = aFace->nodesIterator();
-//     aStream.str("");
-//     aStream << "2" << space ;
-//     for ( int j = 0; j < 2; ++j ) {
-//       aSmdsID = itOnSubFace->next()->GetID();
-//       itOnMap = theEnforcedNodeIdToHybridIdMap.find(aSmdsID);
-//       if (itOnMap != theEnforcedNodeIdToHybridIdMap.end())
-//         aStream << (*itOnMap).second << space;
-//       else {
-//         isOK = false;
-//         break;
-//       }
-//     }
-//     if (isOK) {
-//       for ( int j=0; j<=2; j++)
-//         aStream << dummyint << space ;
-// //       aStream << dummyint << space << dummyint;
-//       localStream << aStream.str() << std::endl;
-//       usedEnforcedEdges++;
-//     }
-//   }
-//   
-//   if (usedEnforcedEdges) {
-//     globalStream << localStream.str();
-//     localStream.str("");
-//   }
-// 
-//   //
-//   //        ENFORCED EDGES : END
-//   //
-//   //
-// 
-//   //
-//   //        ENFORCED TRIANGLES : BEGIN
-//   //
-//     // Iterate over the enforced triangles
-//   int usedEnforcedTriangles = 0;
-//   for(elemIt = theEnforcedTriangles.begin() ; elemIt != theEnforcedTriangles.end() ; ++elemIt) {
-//     aFace = (*elemIt);
-//     nbNodes = aFace->NbCornerNodes();
-//     isOK = true;
-//     itOnSubFace = aFace->nodesIterator();
-//     aStream.str("");
-//     aStream << nbNodes << space ;
-//     for ( int j = 0; j < 3; ++j ) {
-//       aSmdsID = itOnSubFace->next()->GetID();
-//       itOnMap = theEnforcedNodeIdToHybridIdMap.find(aSmdsID);
-//       if (itOnMap != theEnforcedNodeIdToHybridIdMap.end())
-//         aStream << (*itOnMap).second << space;
-//       else {
-//         isOK = false;
-//         break;
-//       }
-//     }
-//     if (isOK) {
-//       for ( int j=0; j<=3; j++)
-//         aStream << dummyint << space ;
-//       localStream << aStream.str() << std::endl;
-//       usedEnforcedTriangles++;
-//     }
-//   }
-//   
-//   if (usedEnforcedTriangles) {
-//     globalStream << localStream.str();
-//     localStream.str("");
-//   }
-// 
-//   //
-//   //        ENFORCED TRIANGLES : END
-//   //
-  
-  theFile
-  << nbTriangles/*+usedEnforcedTriangles+usedEnforcedEdges*/
-  << " 0" << std::endl
-  << globalStream.str();
-
-  return true;
-}
-
-//=======================================================================
-//function : writePoints
-//purpose  : 
-//=======================================================================
-
-static bool writePoints (ofstream &                       theFile,
-                         SMESH_MesherHelper&              theHelper,
-                         map <int,int> &                  theSmdsToHybridIdMap,
-                         map <int,int> &                  theEnforcedNodeIdToHybridIdMap,
-                         map <int,const SMDS_MeshNode*> & theHybridIdToNodeMap,
-                         HYBRIDPlugin_Hypothesis::TID2SizeMap & theNodeIDToSizeMap,
-                         HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertexCoordsValues & theEnforcedVertices,
-                         HYBRIDPlugin_Hypothesis::TIDSortedNodeGroupMap & theEnforcedNodes,
-                         HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap & theEnforcedEdges,
-                         HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap & theEnforcedTriangles)
-{
-  // record structure:
-  //
-  // NB_NODES
-  // Loop from 1 to NB_NODES
-  //   X Y Z DUMMY_INT
-
-  SMESHDS_Mesh * theMeshDS = theHelper.GetMeshDS();
-  int nbNodes = theMeshDS->NbNodes();
-  if ( nbNodes == 0 )
-    return false;
-  
-  int nbEnforcedVertices = theEnforcedVertices.size();
-  int nbEnforcedNodes    = theEnforcedNodes.size();
-  
-  const TopoDS_Shape shapeToMesh = theMeshDS->ShapeToMesh();
-  
-  int aHybridID = 1;
-  SMDS_NodeIteratorPtr nodeIt = theMeshDS->nodesIterator();
-  const SMDS_MeshNode* node;
-
-  // Issue 020674: EDF 870 SMESH: Mesh generated by Netgen not usable by HYBRID
-  // The problem is in nodes on degenerated edges, we need to skip nodes which are free
-  // and replace not-free nodes on degenerated edges by the node on vertex
-  TNodeNodeMap n2nDegen; // map a node on degenerated edge to a node on vertex
-  TNodeNodeMap::iterator n2nDegenIt;
-  if ( theHelper.HasDegeneratedEdges() )
+  if (anElemSetQuad.size())
   {
-    set<int> checkedSM;
-    for (TopExp_Explorer e(theMeshDS->ShapeToMesh(), TopAbs_EDGE ); e.More(); e.Next())
+    MGInput->GmfSetKwd(idx, GmfQuadrilaterals, anElemSetQuad.size());
+    int k=0;
+    for(elemSetIt = anElemSetQuad.begin() ; elemSetIt != anElemSetQuad.end() ; ++elemSetIt,++k)
     {
-      SMESH_subMesh* sm = theHelper.GetMesh()->GetSubMesh( e.Current() );
-      if ( checkedSM.insert( sm->GetId() ).second && theHelper.IsDegenShape(sm->GetId() ))
+      elem = (*elemSetIt);
+      theFaceByHybridId.push_back( elem );
+      nodeIt = elem->nodesIterator();
+      int index=0;
+      for ( int j = 0; j < 4; ++j )
       {
-        if ( SMESHDS_SubMesh* smDS = sm->GetSubMeshDS() )
-        {
-          TopoDS_Shape vertex = TopoDS_Iterator( e.Current() ).Value();
-          const SMDS_MeshNode* vNode = SMESH_Algo::VertexNode( TopoDS::Vertex( vertex ), theMeshDS);
-          {
-            SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
-            while ( nIt->more() )
-              n2nDegen.insert( make_pair( nIt->next(), vNode ));
-          }
-        }
+        // find HYBRID ID
+        const SMDS_MeshNode* node = castToNode( nodeIt->next() );
+        std::map< const SMDS_MeshNode*,int >::iterator it = aNodeToHybridIdMap.find(node);
+        if (it == aNodeToHybridIdMap.end())
+          throw "Node not found";
+        nquad[index] = it->second;
+        index++;
       }
+      MGInput->GmfSetLin(idx, GmfQuadrilaterals, nquad[0], nquad[1], nquad[2], nquad[3],
+                         /*tag=*/elem->getshapeId() );
+      // _CEA_cbo what is it for???
+      //aFaceGroupByHybridId[k] = "";
     }
-    nbNodes -= n2nDegen.size();
-  }
-
-  const bool isQuadMesh = 
-    theHelper.GetMesh()->NbEdges( ORDER_QUADRATIC ) ||
-    theHelper.GetMesh()->NbFaces( ORDER_QUADRATIC ) ||
-    theHelper.GetMesh()->NbVolumes( ORDER_QUADRATIC );
-  if ( isQuadMesh )
-  {
-    // descrease nbNodes by nb of medium nodes
-    while ( nodeIt->more() )
-    {
-      node = nodeIt->next();
-      if ( !theHelper.IsDegenShape( node->getshapeId() ))
-        nbNodes -= int( theHelper.IsMedium( node ));
-    }
-    nodeIt = theMeshDS->nodesIterator();
   }
 
-  const char* space    = "  ";
-  const int   dummyint = 0;
-
-  std::string tmpStr;
-  (nbNodes == 0 || nbNodes == 1) ? tmpStr = " node" : tmpStr = " nodes";
-  // NB_NODES
-  std::cout << std::endl;
-  std::cout << "The initial 2D mesh contains :" << std::endl;
-  std::cout << "    " << nbNodes << tmpStr << std::endl;
-  if (nbEnforcedVertices > 0) {
-    (nbEnforcedVertices == 1) ? tmpStr = "vertex" : tmpStr = "vertices";
-    std::cout << "    " << nbEnforcedVertices << " enforced " << tmpStr << std::endl;
-  }
-  if (nbEnforcedNodes > 0) {
-    (nbEnforcedNodes == 1) ? tmpStr = "node" : tmpStr = "nodes";
-    std::cout << "    " << nbEnforcedNodes << " enforced " << tmpStr << std::endl;
-  }
-  std::cout << std::endl;
-  std::cout << "Start writing in 'points' file ..." << std::endl;
-
-  theFile << nbNodes << std::endl;
-
-  // Loop from 1 to NB_NODES
-
-  while ( nodeIt->more() )
-  {
-    node = nodeIt->next();
-    if ( isQuadMesh && theHelper.IsMedium( node )) // Issue 0021238
-      continue;
-    if ( n2nDegen.count( node ) ) // Issue 0020674
-      continue;
-
-    theSmdsToHybridIdMap.insert( make_pair( node->GetID(), aHybridID ));
-    theHybridIdToNodeMap.insert( make_pair( aHybridID, node ));
-    aHybridID++;
-
-    // X Y Z DUMMY_INT
-    theFile
-    << node->X() << space 
-    << node->Y() << space 
-    << node->Z() << space 
-    << dummyint;
-
-    theFile << std::endl;
-
-  }
-  
-  // Iterate over the enforced nodes
-  std::map<int,double> enfVertexIndexSizeMap;
-  if (nbEnforcedNodes) {
-    HYBRIDPlugin_Hypothesis::TIDSortedNodeGroupMap::const_iterator nodeIt = theEnforcedNodes.begin();
-    for( ; nodeIt != theEnforcedNodes.end() ; ++nodeIt) {
-      double x = nodeIt->first->X();
-      double y = nodeIt->first->Y();
-      double z = nodeIt->first->Z();
-      // Test if point is inside shape to mesh
-      gp_Pnt myPoint(x,y,z);
-      BRepClass3d_SolidClassifier scl(shapeToMesh);
-      scl.Perform(myPoint, 1e-7);
-      TopAbs_State result = scl.State();
-      if ( result != TopAbs_IN )
-        continue;
-      std::vector<double> coords;
-      coords.push_back(x);
-      coords.push_back(y);
-      coords.push_back(z);
-      if (theEnforcedVertices.find(coords) != theEnforcedVertices.end())
-        continue;
-        
-//      double size = theNodeIDToSizeMap.find(nodeIt->first->GetID())->second;
-  //       theHybridIdToNodeMap.insert( make_pair( nbNodes + i, (*nodeIt) ));
-  //       MESSAGE("Adding enforced node (" << x << "," << y <<"," << z << ")");
-      // X Y Z PHY_SIZE DUMMY_INT
-      theFile
-      << x << space 
-      << y << space 
-      << z << space
-      << -1 << space
-      << dummyint << space;
-      theFile << std::endl;
-      theEnforcedNodeIdToHybridIdMap.insert( make_pair( nodeIt->first->GetID(), aHybridID ));
-      enfVertexIndexSizeMap[aHybridID] = -1;
-      aHybridID++;
-  //     else
-  //         MESSAGE("Enforced vertex (" << x << "," << y <<"," << z << ") is not inside the geometry: it was not added ");
-    }
-  }
-  
-  if (nbEnforcedVertices) {
-    // Iterate over the enforced vertices
-    HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertexCoordsValues::const_iterator vertexIt = theEnforcedVertices.begin();
-    for( ; vertexIt != theEnforcedVertices.end() ; ++vertexIt) {
-      double x = vertexIt->first[0];
-      double y = vertexIt->first[1];
-      double z = vertexIt->first[2];
-      // Test if point is inside shape to mesh
-      gp_Pnt myPoint(x,y,z);
-      BRepClass3d_SolidClassifier scl(shapeToMesh);
-      scl.Perform(myPoint, 1e-7);
-      TopAbs_State result = scl.State();
-      if ( result != TopAbs_IN )
-        continue;
-      MESSAGE("Adding enforced vertex (" << x << "," << y <<"," << z << ") = " << vertexIt->second);
-      // X Y Z PHY_SIZE DUMMY_INT
-      theFile
-      << x << space 
-      << y << space 
-      << z << space
-      << vertexIt->second << space 
-      << dummyint << space;
-      theFile << std::endl;
-      enfVertexIndexSizeMap[aHybridID] = vertexIt->second;
-      aHybridID++;
-    }
-  }
-  
+  MGInput->GmfCloseMesh(idx);
+  if (idxRequired)
+    MGInput->GmfCloseMesh(idxRequired);
+  if (idxSol)
+    MGInput->GmfCloseMesh(idxSol);
   
-  std::cout << std::endl;
-  std::cout << "End writing in 'points' file." << std::endl;
-
   return true;
-}
-
-//=======================================================================
-//function : readResultFile
-//purpose  : readResultFile with geometry
-//=======================================================================
-
-static bool readResultFile(const int                       fileOpen,
-#ifdef WIN32
-                           const char*                     fileName,
-#endif
-                           HYBRIDPlugin_HYBRID*            theAlgo,
-                           SMESH_MesherHelper&             theHelper,
-                           TopoDS_Shape                    tabShape[],
-                           double**                        tabBox,
-                           const int                       nbShape,
-                           map <int,const SMDS_MeshNode*>& theHybridIdToNodeMap,
-                           std::map <int,int> &            theNodeId2NodeIndexMap,
-                           bool                            toMeshHoles,
-                           int                             nbEnforcedVertices,
-                           int                             nbEnforcedNodes,
-                           HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap & theEnforcedEdges,
-                           HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap & theEnforcedTriangles,
-                           bool                            toMakeGroupsOfDomains)
-{
-  MESSAGE("HYBRIDPlugin_HYBRID::readResultFile()");
-  Kernel_Utils::Localizer loc;
-  struct stat status;
-  size_t      length;
   
-  std::string tmpStr;
-
-  char *ptr, *mapPtr;
-  char *tetraPtr;
-  char *shapePtr;
-
-  SMESHDS_Mesh* theMeshDS = theHelper.GetMeshDS();
-
-  int nbElems, nbNodes, nbInputNodes;
-  int nbTriangle;
-  int ID, shapeID, hybridShapeID;
-  int IdShapeRef = 1;
-  int compoundID =
-    nbShape ? theMeshDS->ShapeToIndex( tabShape[0] ) : theMeshDS->ShapeToIndex( theMeshDS->ShapeToMesh() );
-
-  int *tab, *tabID, *nodeID, *nodeAssigne;
-  double *coord;
-  const SMDS_MeshNode **node;
-
-  tab    = new int[3];
-  nodeID = new int[4];
-  coord  = new double[3];
-  node   = new const SMDS_MeshNode*[4];
-
-  TopoDS_Shape aSolid;
-  SMDS_MeshNode * aNewNode;
-  map <int,const SMDS_MeshNode*>::iterator itOnNode;
-  SMDS_MeshElement* aTet;
-#ifdef _DEBUG_
-  set<int> shapeIDs;
-#endif
-
-  // Read the file state
-  fstat(fileOpen, &status);
-  length   = status.st_size;
-
-  // Mapping the result file into memory
-#ifdef WIN32
-  HANDLE fd = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ,
-                         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
-  HANDLE hMapObject = CreateFileMapping(fd, NULL, PAGE_READONLY,
-                                        0, (DWORD)length, NULL);
-  ptr = ( char* ) MapViewOfFile(hMapObject, FILE_MAP_READ, 0, 0, 0 );
-#else
-  ptr = (char *) mmap(0,length,PROT_READ,MAP_PRIVATE,fileOpen,0);
-#endif
-  mapPtr = ptr;
-
-  ptr      = readMapIntLine(ptr, tab);
-  tetraPtr = ptr;
-
-  nbElems      = tab[0];
-  nbNodes      = tab[1];
-  nbInputNodes = tab[2];
-
-  nodeAssigne = new int[ nbNodes+1 ];
-
-  if (nbShape > 0)
-    aSolid = tabShape[0];
-
-  // Reading the nodeId
-  for (int i=0; i < 4*nbElems; i++)
-    strtol(ptr, &ptr, 10);
-
-  MESSAGE("nbInputNodes: "<<nbInputNodes);
-  MESSAGE("nbEnforcedVertices: "<<nbEnforcedVertices);
-  MESSAGE("nbEnforcedNodes: "<<nbEnforcedNodes);
-  // Reading the nodeCoor and update the nodeMap
-  for (int iNode=1; iNode <= nbNodes; iNode++) {
-    if(theAlgo->computeCanceled())
-      return false;
-    for (int iCoor=0; iCoor < 3; iCoor++)
-      coord[ iCoor ] = strtod(ptr, &ptr);
-    nodeAssigne[ iNode ] = 1;
-    if ( iNode > (nbInputNodes-(nbEnforcedVertices+nbEnforcedNodes)) ) {
-      // Creating SMESH nodes
-      // - for enforced vertices
-      // - for vertices of forced edges
-      // - for hybrid nodes
-      nodeAssigne[ iNode ] = 0;
-      aNewNode = theMeshDS->AddNode( coord[0],coord[1],coord[2] );
-      theHybridIdToNodeMap.insert(theHybridIdToNodeMap.end(), make_pair( iNode, aNewNode ));
-    }
-  }
-
-  // Reading the number of triangles which corresponds to the number of sub-domains
-  nbTriangle = strtol(ptr, &ptr, 10);
-
-  tabID = new int[nbTriangle];
-  for (int i=0; i < nbTriangle; i++) {
-    if(theAlgo->computeCanceled())
-      return false;
-    tabID[i] = 0;
-    // find the solid corresponding to HYBRID sub-domain following
-    // the technique proposed in HYBRID manual in chapter
-    // "B.4 Subdomain (sub-region) assignment"
-    int nodeId1 = strtol(ptr, &ptr, 10);
-    int nodeId2 = strtol(ptr, &ptr, 10);
-    int nodeId3 = strtol(ptr, &ptr, 10);
-    if ( nbTriangle > 1 ) {
-      const SMDS_MeshNode* n1 = theHybridIdToNodeMap[ nodeId1 ];
-      const SMDS_MeshNode* n2 = theHybridIdToNodeMap[ nodeId2 ];
-      const SMDS_MeshNode* n3 = theHybridIdToNodeMap[ nodeId3 ];
-      if (!n1 || !n2 || !n3) {
-        tabID[i] = HOLE_ID;
-        continue;
-      }
-      try {
-        OCC_CATCH_SIGNALS;
-//         tabID[i] = findShapeID( theHelper, n1, n2, n3, toMeshHoles );
-        tabID[i] = findShapeID( *theHelper.GetMesh(), n1, n2, n3, toMeshHoles );
-        // -- 0020330: Pb with hybrid as a submesh
-        // check that found shape is to be meshed
-        if ( tabID[i] > 0 ) {
-          const TopoDS_Shape& foundShape = theMeshDS->IndexToShape( tabID[i] );
-          bool isToBeMeshed = false;
-          for ( int iS = 0; !isToBeMeshed && iS < nbShape; ++iS )
-            isToBeMeshed = foundShape.IsSame( tabShape[ iS ]);
-          if ( !isToBeMeshed )
-            tabID[i] = HOLE_ID;
-        }
-        // END -- 0020330: Pb with hybrid as a submesh
-#ifdef _DEBUG_
-        std::cout << i+1 << " subdomain: findShapeID() returns " << tabID[i] << std::endl;
-#endif
-      }
-      catch ( Standard_Failure & ex)
-      {
-#ifdef _DEBUG_
-        std::cout << i+1 << " subdomain: Exception caugt: " << ex.GetMessageString() << std::endl;
-#endif
-      }
-      catch (...) {
-#ifdef _DEBUG_
-        std::cout << i+1 << " subdomain: unknown exception caught " << std::endl;
-#endif
-      }
-    }
-  }
-
-  shapePtr = ptr;
-
-  if ( nbTriangle <= nbShape ) // no holes
-    toMeshHoles = true; // not avoid creating tetras in holes
-
-  // IMP 0022172: [CEA 790] create the groups corresponding to domains
-  std::vector< std::vector< const SMDS_MeshElement* > > elemsOfDomain( Max( nbTriangle, nbShape ));
-
-  // Associating the tetrahedrons to the shapes
-  shapeID = compoundID;
-  for (int iElem = 0; iElem < nbElems; iElem++) {
-    if(theAlgo->computeCanceled())
-      return false;
-    for (int iNode = 0; iNode < 4; iNode++) {
-      ID = strtol(tetraPtr, &tetraPtr, 10);
-      itOnNode = theHybridIdToNodeMap.find(ID);
-      node[ iNode ] = itOnNode->second;
-      nodeID[ iNode ] = ID;
-    }
-    // We always run HYBRID with "to mesh holes"==TRUE but we must not create
-    // tetras within holes depending on hypo option,
-    // so we first check if aTet is inside a hole and then create it 
-    //aTet = theMeshDS->AddVolume( node[1], node[0], node[2], node[3] );
-    hybridShapeID = 0; // domain ID
-    if ( nbTriangle > 1 ) {
-      shapeID = HOLE_ID; // negative shapeID means not to create tetras if !toMeshHoles
-      hybridShapeID = strtol(shapePtr, &shapePtr, 10) - IdShapeRef;
-      if ( tabID[ hybridShapeID ] == 0 ) {
-        TopAbs_State state;
-        aSolid = findShape(node, aSolid, tabShape, tabBox, nbShape, &state);
-        if ( toMeshHoles || state == TopAbs_IN )
-          shapeID = theMeshDS->ShapeToIndex( aSolid );
-        tabID[ hybridShapeID ] = shapeID;
-      }
-      else
-        shapeID = tabID[ hybridShapeID ];
-    }
-    else if ( nbShape > 1 ) {
-      // Case where nbTriangle == 1 while nbShape == 2 encountered
-      // with compound of 2 boxes and "To mesh holes"==False,
-      // so there are no subdomains specified for each tetrahedron.
-      // Try to guess a solid by a node already bound to shape
-      shapeID = 0;
-      for ( int i=0; i<4 && shapeID==0; i++ ) {
-        if ( nodeAssigne[ nodeID[i] ] == 1 &&
-             node[i]->GetPosition()->GetTypeOfPosition() == SMDS_TOP_3DSPACE &&
-             node[i]->getshapeId() > 1 )
-        {
-          shapeID = node[i]->getshapeId();
-        }
-      }
-      if ( shapeID==0 ) {
-        aSolid = findShape(node, aSolid, tabShape, tabBox, nbShape);
-        shapeID = theMeshDS->ShapeToIndex( aSolid );
-      }
-    }
-    // set new nodes and tetrahedron onto the shape
-    for ( int i=0; i<4; i++ ) {
-      if ( nodeAssigne[ nodeID[i] ] == 0 ) {
-        if ( shapeID != HOLE_ID )
-          theMeshDS->SetNodeInVolume( node[i], shapeID );
-        nodeAssigne[ nodeID[i] ] = shapeID;
-      }
-    }
-    if ( toMeshHoles || shapeID != HOLE_ID ) {
-      aTet = theHelper.AddVolume( node[1], node[0], node[2], node[3],
-                                  /*id=*/0, /*force3d=*/false);
-      theMeshDS->SetMeshElementOnShape( aTet, shapeID );
-      if ( toMakeGroupsOfDomains )
-      {
-        if ( int( elemsOfDomain.size() ) < hybridShapeID+1 )
-          elemsOfDomain.resize( hybridShapeID+1 );
-        elemsOfDomain[ hybridShapeID ].push_back( aTet );
-      }
-    }
-#ifdef _DEBUG_
-    shapeIDs.insert( shapeID );
-#endif
-  }
-  if ( toMakeGroupsOfDomains )
-    makeDomainGroups( elemsOfDomain, &theHelper );
-  
-  // Add enforced elements
-  HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap::const_iterator elemIt;
-  const SMDS_MeshElement* anElem;
-  SMDS_ElemIteratorPtr itOnEnfElem;
-  map<int,int>::const_iterator itOnMap;
-  shapeID = compoundID;
-  // Enforced edges
-  if (theEnforcedEdges.size()) {
-    (theEnforcedEdges.size() <= 1) ? tmpStr = " enforced edge" : " enforced edges";
-    std::cout << "Add " << theEnforcedEdges.size() << tmpStr << std::endl;
-    std::vector< const SMDS_MeshNode* > node( 2 );
-    // Iterate over the enforced edges
-    for(elemIt = theEnforcedEdges.begin() ; elemIt != theEnforcedEdges.end() ; ++elemIt) {
-      anElem = elemIt->first;
-      bool addElem = true;
-      itOnEnfElem = anElem->nodesIterator();
-      for ( int j = 0; j < 2; ++j ) {
-        int aNodeID = itOnEnfElem->next()->GetID();
-        itOnMap = theNodeId2NodeIndexMap.find(aNodeID);
-        if (itOnMap != theNodeId2NodeIndexMap.end()) {
-          itOnNode = theHybridIdToNodeMap.find((*itOnMap).second);
-          if (itOnNode != theHybridIdToNodeMap.end()) {
-            node.push_back((*itOnNode).second);
-//             shapeID =(*itOnNode).second->getshapeId();
-          }
-          else
-            addElem = false;
-        }
-        else
-          addElem = false;
-      }
-      if (addElem) {
-        aTet = theHelper.AddEdge( node[0], node[1], 0,  false);
-        theMeshDS->SetMeshElementOnShape( aTet, shapeID );
-      }
-    }
-  }
-  // Enforced faces
-  if (theEnforcedTriangles.size()) {
-    (theEnforcedTriangles.size() <= 1) ? tmpStr = " enforced triangle" : " enforced triangles";
-    std::cout << "Add " << theEnforcedTriangles.size() << " enforced triangles" << std::endl;
-    std::vector< const SMDS_MeshNode* > node( 3 );
-    // Iterate over the enforced triangles
-    for(elemIt = theEnforcedTriangles.begin() ; elemIt != theEnforcedTriangles.end() ; ++elemIt) {
-      anElem = elemIt->first;
-      bool addElem = true;
-      itOnEnfElem = anElem->nodesIterator();
-      for ( int j = 0; j < 3; ++j ) {
-        int aNodeID = itOnEnfElem->next()->GetID();
-        itOnMap = theNodeId2NodeIndexMap.find(aNodeID);
-        if (itOnMap != theNodeId2NodeIndexMap.end()) {
-          itOnNode = theHybridIdToNodeMap.find((*itOnMap).second);
-          if (itOnNode != theHybridIdToNodeMap.end()) {
-            node.push_back((*itOnNode).second);
-//             shapeID =(*itOnNode).second->getshapeId();
-          }
-          else
-            addElem = false;
-        }
-        else
-          addElem = false;
-      }
-      if (addElem) {
-        aTet = theHelper.AddFace( node[0], node[1], node[2], 0,  false);
-        theMeshDS->SetMeshElementOnShape( aTet, shapeID );
-      }
-    }
-  }
-
-  // Remove nodes of tetras inside holes if !toMeshHoles
-  if ( !toMeshHoles ) {
-    itOnNode = theHybridIdToNodeMap.find( nbInputNodes );
-    for ( ; itOnNode != theHybridIdToNodeMap.end(); ++itOnNode) {
-      ID = itOnNode->first;
-      if ( nodeAssigne[ ID ] == HOLE_ID )
-        theMeshDS->RemoveFreeNode( itOnNode->second, 0 );
-    }
-  }
-
-  
-  if ( nbElems ) {
-    (nbElems <= 1) ? tmpStr = " tetrahedra" : " tetrahedrons";
-    cout << nbElems << tmpStr << " have been associated to " << nbShape;
-    (nbShape <= 1) ? tmpStr = " shape" : " shapes";
-    cout << tmpStr << endl;
-  }
-#ifdef WIN32
-  UnmapViewOfFile(mapPtr);
-  CloseHandle(hMapObject);
-  CloseHandle(fd);
-#else
-  munmap(mapPtr, length);
-#endif
-  close(fileOpen);
-
-  delete [] tab;
-  delete [] tabID;
-  delete [] nodeID;
-  delete [] coord;
-  delete [] node;
-  delete [] nodeAssigne;
-
-#ifdef _DEBUG_
-  shapeIDs.erase(-1);
-  if ( shapeIDs.size() != nbShape ) {
-    (shapeIDs.size() <= 1) ? tmpStr = " solid" : " solids";
-    std::cout << "Only " << shapeIDs.size() << tmpStr << " of " << nbShape << " found" << std::endl;
-    for (int i=0; i<nbShape; i++) {
-      shapeID = theMeshDS->ShapeToIndex( tabShape[i] );
-      if ( shapeIDs.find( shapeID ) == shapeIDs.end() )
-        std::cout << "  Solid #" << shapeID << " not found" << std::endl;
-    }
-  }
-#endif
-
-  return true;
 }
 
-
 //=============================================================================
 /*!
  *Here we are going to use the HYBRID mesher with geometry
@@ -2874,70 +1401,31 @@ static bool readResultFile(const int                       fileOpen,
 bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
                                   const TopoDS_Shape& theShape)
 {
-  bool Ok(false);
-  //SMESHDS_Mesh* meshDS = theMesh.GetMeshDS();
-
-  // we count the number of shapes
-  // _nbShape = countShape( meshDS, TopAbs_SOLID ); -- 0020330: Pb with hybrid as a submesh
-  // _nbShape = 0;
-  TopExp_Explorer expBox ( theShape, TopAbs_SOLID );
-  // for ( ; expBox.More(); expBox.Next() )
-  //   _nbShape++;
-
-  // create bounding box for every shape inside the compound
-
-  // int iShape = 0;
-  // TopoDS_Shape* tabShape;
-  // double**      tabBox;
-  // tabShape = new TopoDS_Shape[_nbShape];
-  // tabBox   = new double*[_nbShape];
-  // for (int i=0; i<_nbShape; i++)
-  //   tabBox[i] = new double[6];
-  // Standard_Real Xmin, Ymin, Zmin, Xmax, Ymax, Zmax;
-
-  // for (expBox.ReInit(); expBox.More(); expBox.Next()) {
-  //   tabShape[iShape] = expBox.Current();
-  //   Bnd_Box BoundingBox;
-  //   BRepBndLib::Add(expBox.Current(), BoundingBox);
-  //   BoundingBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
-  //   tabBox[iShape][0] = Xmin; tabBox[iShape][1] = Xmax;
-  //   tabBox[iShape][2] = Ymin; tabBox[iShape][3] = Ymax;
-  //   tabBox[iShape][4] = Zmin; tabBox[iShape][5] = Zmax;
-  //   iShape++;
-  // }
+  bool Ok = false;
 
   // a unique working file name
   // to avoid access to the same files by eg different users
   _genericName = HYBRIDPlugin_Hypothesis::GetFileName(_hyp);
-  TCollection_AsciiString aGenericName((char*) _genericName.c_str() );
-  TCollection_AsciiString aGenericNameRequired = aGenericName + "_required";
+  std::string aGenericName         = _genericName;
+  std::string aGenericNameRequired = aGenericName + "_required";
 
-  TCollection_AsciiString aLogFileName    = aGenericName + ".log";    // log
-  TCollection_AsciiString aResultFileName;
+  std::string aLogFileName    = aGenericName + ".log";    // log
+  std::string aResultFileName;
 
-  TCollection_AsciiString aGMFFileName, aRequiredVerticesFileName, aSolFileName, aResSolFileName;
-//#ifdef _DEBUG_
+  std::string aGMFFileName, aRequiredVerticesFileName, aSolFileName, aResSolFileName;
   aGMFFileName              = aGenericName + ".mesh"; // GMF mesh file
   aResultFileName           = aGenericName + "Vol.mesh"; // GMF mesh file
   aResSolFileName           = aGenericName + "Vol.sol"; // GMF mesh file
   aRequiredVerticesFileName = aGenericNameRequired + ".mesh"; // GMF required vertices mesh file
   aSolFileName              = aGenericNameRequired + ".sol"; // GMF solution file
-//#else
-//  aGMFFileName    = aGenericName + ".meshb"; // GMF mesh file
-//  aResultFileName = aGenericName + "Vol.meshb"; // GMF mesh file
-//  aRequiredVerticesFileName    = aGenericNameRequired + ".meshb"; // GMF required vertices mesh file
-//  aSolFileName    = aGenericNameRequired + ".solb"; // GMF solution file
-//#endif
   
   std::map <int,int> aNodeId2NodeIndexMap, aSmdsToHybridIdMap, anEnforcedNodeIdToHybridIdMap;
-  //std::map <int,const SMDS_MeshNode*> aHybridIdToNodeMap;
   std::map <int, int> nodeID2nodeIndexMap;
   std::map<std::vector<double>, std::string> enfVerticesWithGroup;
   HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertexCoordsValues coordsSizeMap = HYBRIDPlugin_Hypothesis::GetEnforcedVerticesCoordsSize(_hyp);
   HYBRIDPlugin_Hypothesis::TIDSortedNodeGroupMap enforcedNodes = HYBRIDPlugin_Hypothesis::GetEnforcedNodes(_hyp);
   HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap enforcedEdges = HYBRIDPlugin_Hypothesis::GetEnforcedEdges(_hyp);
   HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap enforcedTriangles = HYBRIDPlugin_Hypothesis::GetEnforcedTriangles(_hyp);
-//   TIDSortedElemSet enforcedQuadrangles = HYBRIDPlugin_Hypothesis::GetEnforcedQuadrangles(_hyp);
   HYBRIDPlugin_Hypothesis::TID2SizeMap nodeIDToSizeMap = HYBRIDPlugin_Hypothesis::GetNodeIDToSizeMap(_hyp);
 
   HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertexList enfVertices = HYBRIDPlugin_Hypothesis::GetEnforcedVertices(_hyp);
@@ -2947,59 +1435,36 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   for ( ; enfVerIt != enfVertices.end() ; ++enfVerIt)
   {
     HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertex* enfVertex = (*enfVerIt);
-//     if (enfVertex->geomEntry.empty() && enfVertex->coords.size()) {
     if (enfVertex->coords.size()) {
-      coordsSizeMap.insert(make_pair(enfVertex->coords,enfVertex->size));
-      enfVerticesWithGroup.insert(make_pair(enfVertex->coords,enfVertex->groupName));
-//       MESSAGE("enfVerticesWithGroup.insert(make_pair(("<<enfVertex->coords[0]<<","<<enfVertex->coords[1]<<","<<enfVertex->coords[2]<<"),\""<<enfVertex->groupName<<"\"))");
+      coordsSizeMap.insert(std::make_pair(enfVertex->coords,enfVertex->size));
+      enfVerticesWithGroup.insert(std::make_pair(enfVertex->coords,enfVertex->groupName));
     }
     else {
-//     if (!enfVertex->geomEntry.empty()) {
       TopoDS_Shape GeomShape = entryToShape(enfVertex->geomEntry);
-//       GeomType = GeomShape.ShapeType();
-
-//       if (!enfVertex->isCompound) {
-// //       if (GeomType == TopAbs_VERTEX) {
-//         coords.clear();
-//         aPnt = BRep_Tool::Pnt(TopoDS::Vertex(GeomShape));
-//         coords.push_back(aPnt.X());
-//         coords.push_back(aPnt.Y());
-//         coords.push_back(aPnt.Z());
-//         if (coordsSizeMap.find(coords) == coordsSizeMap.end()) {
-//           coordsSizeMap.insert(make_pair(coords,enfVertex->size));
-//           enfVerticesWithGroup.insert(make_pair(coords,enfVertex->groupName));
-//         }
-//       }
-//
-//       // Group Management
-//       else {
-//       if (GeomType == TopAbs_COMPOUND){
-        for (TopoDS_Iterator it (GeomShape); it.More(); it.Next()){
-          coords.clear();
-          if (it.Value().ShapeType() == TopAbs_VERTEX){
-            gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(it.Value()));
-            coords.push_back(aPnt.X());
-            coords.push_back(aPnt.Y());
-            coords.push_back(aPnt.Z());
-            if (coordsSizeMap.find(coords) == coordsSizeMap.end()) {
-              coordsSizeMap.insert(make_pair(coords,enfVertex->size));
-              enfVerticesWithGroup.insert(make_pair(coords,enfVertex->groupName));
-//               MESSAGE("enfVerticesWithGroup.insert(make_pair(("<<coords[0]<<","<<coords[1]<<","<<coords[2]<<"),\""<<enfVertex->groupName<<"\"))");
-            }
+      for (TopoDS_Iterator it (GeomShape); it.More(); it.Next()){
+        coords.clear();
+        if (it.Value().ShapeType() == TopAbs_VERTEX){
+          gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(it.Value()));
+          coords.push_back(aPnt.X());
+          coords.push_back(aPnt.Y());
+          coords.push_back(aPnt.Z());
+          if (coordsSizeMap.find(coords) == coordsSizeMap.end()) {
+            coordsSizeMap.insert(std::make_pair(coords,enfVertex->size));
+            enfVerticesWithGroup.insert(std::make_pair(coords,enfVertex->groupName));
           }
         }
-//       }
+      }
     }
   }
   int nbEnforcedVertices = coordsSizeMap.size();
   int nbEnforcedNodes = enforcedNodes.size();
-  
+
   std::string tmpStr;
   (nbEnforcedNodes <= 1) ? tmpStr = "node" : "nodes";
   std::cout << nbEnforcedNodes << " enforced " << tmpStr << " from hypo" << std::endl;
   (nbEnforcedVertices <= 1) ? tmpStr = "vertex" : "vertices";
   std::cout << nbEnforcedVertices << " enforced " << tmpStr << " from hypo" << std::endl;
-  
+
   SMESH_MesherHelper helper( theMesh );
   helper.SetSubShape( theShape );
 
@@ -3007,55 +1472,25 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   std::vector <const SMDS_MeshElement*> aFaceByHybridId;
   std::map<const SMDS_MeshNode*,int> aNodeToHybridIdMap;
   std::vector<std::string> aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId;
-  {
-    SMESH_ProxyMesh::Ptr proxyMesh( new SMESH_ProxyMesh( theMesh ));
 
-    // make prisms on quadrangles
-    if ( theMesh.NbQuadrangles() > 0 )
-    {
-      vector<SMESH_ProxyMesh::Ptr> components;
-      for (expBox.ReInit(); expBox.More(); expBox.Next())
-      {
-        if ( _viscousLayersHyp )
-        {
-          proxyMesh = _viscousLayersHyp->Compute( theMesh, expBox.Current() );
-          if ( !proxyMesh )
-            return false;
-        }
-        StdMeshers_QuadToTriaAdaptor* q2t = new StdMeshers_QuadToTriaAdaptor;
-        q2t->Compute( theMesh, expBox.Current(), proxyMesh.get() );
-        components.push_back( SMESH_ProxyMesh::Ptr( q2t ));
-      }
-      proxyMesh.reset( new SMESH_ProxyMesh( components ));
-    }
-    // build viscous layers
-    else if ( _viscousLayersHyp )
-    {
-      proxyMesh = _viscousLayersHyp->Compute( theMesh, theShape );
-      if ( !proxyMesh )
-        return false;
-    }
+  SMESH_ProxyMesh::Ptr proxyMesh( new SMESH_ProxyMesh( theMesh ));
 
-    // Ok = (writePoints( aPointsFile, helper, 
-    //                    aSmdsToHybridIdMap, anEnforcedNodeIdToHybridIdMap, aHybridIdToNodeMap, 
-    //                    nodeIDToSizeMap,
-    //                    coordsSizeMap, enforcedNodes, enforcedEdges, enforcedTriangles)
-    //       &&
-    //       writeFaces ( aFacesFile, *proxyMesh, theShape, 
-    //                    aSmdsToHybridIdMap, anEnforcedNodeIdToHybridIdMap,
-    //                    enforcedEdges, enforcedTriangles ));
-    Ok = writeGMFFile(aGMFFileName.ToCString(), aRequiredVerticesFileName.ToCString(), aSolFileName.ToCString(),
-                      *proxyMesh, helper,
-                      aNodeByHybridId, aFaceByHybridId, aNodeToHybridIdMap,
-                      aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId,
-                      enforcedNodes, enforcedEdges, enforcedTriangles, /*enforcedQuadrangles,*/
-                      enfVerticesWithGroup, coordsSizeMap);
-  }
+  MG_HYBRID_API mgHybrid( _computeCanceled, _progress );
+
+  Ok = writeGMFFile(&mgHybrid,
+                    aGMFFileName.c_str(),
+                    aRequiredVerticesFileName.c_str(),
+                    aSolFileName.c_str(),
+                    *proxyMesh, helper,
+                    aNodeByHybridId, aFaceByHybridId, aNodeToHybridIdMap,
+                    aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId,
+                    enforcedNodes, enforcedEdges, enforcedTriangles, /*enforcedQuadrangles,*/
+                    enfVerticesWithGroup, coordsSizeMap);
 
   // Write aSmdsToHybridIdMap to temp file
-  TCollection_AsciiString aSmdsToHybridIdMapFileName;
+  std::string aSmdsToHybridIdMapFileName;
   aSmdsToHybridIdMapFileName = aGenericName + ".ids";  // ids relation
-  ofstream aIdsFile  ( aSmdsToHybridIdMapFileName.ToCString()  , ios::out);
+  std::ofstream aIdsFile  ( aSmdsToHybridIdMapFileName  , ios::out);
   Ok = aIdsFile.rdbuf()->is_open();
   if (!Ok) {
     INFOS( "Can't write into " << aSmdsToHybridIdMapFileName);
@@ -3063,13 +1498,13 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   }
   INFOS( "Writing ids relation into " << aSmdsToHybridIdMapFileName);
   aIdsFile << "Smds Hybrid" << std::endl;
-  map <int,int>::const_iterator myit;
+  std::map <int,int>::const_iterator myit;
   for (myit=aSmdsToHybridIdMap.begin() ; myit != aSmdsToHybridIdMap.end() ; ++myit) {
     aIdsFile << myit->first << " " << myit->second << std::endl;
   }
 
   aIdsFile.close();
-  
+
   if ( ! Ok ) {
     if ( !_keepFiles ) {
       removeFile( aGMFFileName );
@@ -3085,25 +1520,34 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   // run hybrid mesher
   // -----------------
 
-  TCollection_AsciiString cmd( (char*)HYBRIDPlugin_Hypothesis::CommandToRun( _hyp ).c_str() );
-  
-  cmd += TCollection_AsciiString(" --in ") + aGMFFileName;
-  //if ( nbEnforcedVertices + nbEnforcedNodes)
-  //  cmd += TCollection_AsciiString(" --required_vertices ") + aGenericNameRequired;
-  cmd += TCollection_AsciiString(" --out ") + aResultFileName;
-  if ( !_logInStandardOutput )
-    cmd += TCollection_AsciiString(" &>" ) + aLogFileName;  // dump into file
+  std::string cmd = HYBRIDPlugin_Hypothesis::CommandToRun( _hyp );
 
+  if ( mgHybrid.IsExecutable() )
+  {
+    cmd += " --in "  + aGMFFileName;
+    cmd += " --out " + aResultFileName;
+  }
   std::cout << std::endl;
   std::cout << "Hybrid execution with geometry..." << std::endl;
-  std::cout << cmd << std::endl;
+  std::cout << cmd;
+  if ( !_logInStandardOutput )
+  {
+    mgHybrid.SetLogFile( aLogFileName );
+    if ( mgHybrid.IsExecutable() )
+      cmd += " 1>" + aLogFileName;  // dump into file
+    std::cout << " 1> " << aLogFileName;
+  }
+  std::cout << std::endl;
 
-  _compute_canceled = false;
+  _computeCanceled = false;
 
-  system( cmd.ToCString() ); // run
+  std::string errStr;
+  Ok = mgHybrid.Compute( cmd, errStr ); // run
 
-  std::cout << std::endl;
-  std::cout << "End of Hybrid execution !" << std::endl;
+  if ( _logInStandardOutput && mgHybrid.IsLibrary() )
+    std::cout << std::endl << mgHybrid.GetLog() << std::endl;
+  if ( Ok )
+    std::cout << "End of Hybrid execution !" << std::endl;
 
   // --------------
   // read a result
@@ -3111,45 +1555,21 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
 
   // Mapping the result file
 
-  // int fileOpen;
-  // fileOpen = open( aResultFileName.ToCString(), O_RDONLY);
-  // if ( fileOpen < 0 ) {
-  //   std::cout << std::endl;
-  //   std::cout << "Can't open the " << aResultFileName.ToCString() << " HYBRID output file" << std::endl;
-  //   std::cout << "Log: " << aLogFileName << std::endl;
-  //   Ok = false;
-  // }
-  // else {
-    HYBRIDPlugin_Hypothesis::TSetStrings groupsToRemove = HYBRIDPlugin_Hypothesis::GetGroupsToRemove(_hyp);
-    bool toMeshHoles =
-      _hyp ? _hyp->GetToMeshHoles(true) : HYBRIDPlugin_Hypothesis::DefaultMeshHoles();
-    const bool toMakeGroupsOfDomains = HYBRIDPlugin_Hypothesis::GetToMakeGroupsOfDomains( _hyp );
-
-    helper.IsQuadraticSubMesh( theShape );
-    helper.SetElementsOnShape( false );
-
-//     Ok = readResultFile( fileOpen,
-// #ifdef WIN32
-//                          aResultFileName.ToCString(),
-// #endif
-//                          this, //theMesh,
-//                          helper, tabShape, tabBox, _nbShape, 
-//                          aHybridIdToNodeMap, aNodeId2NodeIndexMap,
-//                          toMeshHoles, 
-//                          nbEnforcedVertices, nbEnforcedNodes, 
-//                          enforcedEdges, enforcedTriangles,
-//                          toMakeGroupsOfDomains );
-                         
-    Ok = readGMFFile(aResultFileName.ToCString(),
-                     this,
-                     &helper, aNodeByHybridId, aFaceByHybridId, aNodeToHybridIdMap,
-                     aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId,
-                     groupsToRemove, toMakeGroupsOfDomains, toMeshHoles);
-
-    //removeEmptyGroupsOfDomains( helper.GetMesh(), notEmptyAsWell );
-    removeEmptyGroupsOfDomains( helper.GetMesh(), !toMakeGroupsOfDomains );
-    //}
+  HYBRIDPlugin_Hypothesis::TSetStrings groupsToRemove = HYBRIDPlugin_Hypothesis::GetGroupsToRemove(_hyp);
+  bool toMeshHoles =
+    _hyp ? _hyp->GetToMeshHoles(true) : HYBRIDPlugin_Hypothesis::DefaultMeshHoles();
+  const bool toMakeGroupsOfDomains = HYBRIDPlugin_Hypothesis::GetToMakeGroupsOfDomains( _hyp );
 
+  helper.IsQuadraticSubMesh( theShape );
+  helper.SetElementsOnShape( false );
+
+  Ok = readGMFFile(&mgHybrid, aResultFileName.c_str(),
+                   this,
+                   &helper, aNodeByHybridId, aFaceByHybridId, aNodeToHybridIdMap,
+                   aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId,
+                   groupsToRemove, toMakeGroupsOfDomains, toMeshHoles);
+
+  removeEmptyGroupsOfDomains( helper.GetMesh(), !toMakeGroupsOfDomains );
 
 
 
@@ -3161,26 +1581,24 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   {
     if ( _removeLogOnSuccess )
       removeFile( aLogFileName );
-
-    // if ( _hyp && _hyp->GetToMakeGroupsOfDomains() )
-    //   error( COMPERR_WARNING, "'toMakeGroupsOfDomains' is ignored since the mesh is on shape" );
   }
-  else if ( OSD_File( aLogFileName ).Size() > 0 )
+  else if ( mgHybrid.HasLog() )
   {
     // get problem description from the log file
     _Ghs2smdsConvertor conv( aNodeByHybridId );
-    storeErrorDescription( aLogFileName, conv );
+    storeErrorDescription( _logInStandardOutput ? 0 : aLogFileName.c_str(),
+                           mgHybrid.GetLog(), conv );
   }
-  else
+  else if ( !errStr.empty() )
   {
     // the log file is empty
     removeFile( aLogFileName );
-    INFOS( "HYBRID Error, command '" << cmd.ToCString() << "' failed" );
-    error(COMPERR_ALGO_FAILED, "hybrid: command not found" );
+    INFOS( "HYBRID Error, " << errStr );
+    error(COMPERR_ALGO_FAILED, errStr );
   }
 
   if ( !_keepFiles ) {
-    if (! Ok && _compute_canceled)
+    if (! Ok && _computeCanceled)
       removeFile( aLogFileName );
     removeFile( aGMFFileName );
     removeFile( aRequiredVerticesFileName );
@@ -3189,15 +1607,18 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
     removeFile( aResultFileName );
     removeFile( aSmdsToHybridIdMapFileName );
   }
-  std::cout << "<" << aResultFileName.ToCString() << "> HYBRID output file ";
-  if ( !Ok )
-    std::cout << "not ";
-  std::cout << "treated !" << std::endl;
-  std::cout << std::endl;
-
-  // _nbShape = 0;    // re-initializing _nbShape for the next Compute() method call
-  // delete [] tabShape;
-  // delete [] tabBox;
+  if ( mgHybrid.IsExecutable() )
+  {
+    std::cout << "<" << aResultFileName << "> HYBRID output file ";
+    if ( !Ok )
+      std::cout << "not ";
+    std::cout << "treated !" << std::endl;
+    std::cout << std::endl;
+  }
+  else
+  {
+    std::cout << "MG-HYBRID " << ( Ok ? "succeeded" : "failed") << std::endl;
+  }
 
   return Ok;
 }
@@ -3210,39 +1631,29 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
 bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
                                   SMESH_MesherHelper* theHelper)
 {
-  MESSAGE("HYBRIDPlugin_HYBRID::Compute()");
-
   theHelper->IsQuadraticSubMesh( theHelper->GetSubShape() );
 
   // a unique working file name
   // to avoid access to the same files by eg different users
   _genericName = HYBRIDPlugin_Hypothesis::GetFileName(_hyp);
-  TCollection_AsciiString aGenericName((char*) _genericName.c_str() );
-  TCollection_AsciiString aGenericNameRequired = aGenericName + "_required";
+  std::string aGenericName((char*) _genericName.c_str() );
+  std::string aGenericNameRequired = aGenericName + "_required";
 
-  TCollection_AsciiString aLogFileName    = aGenericName + ".log";    // log
-  TCollection_AsciiString aResultFileName;
+  std::string aLogFileName    = aGenericName + ".log";    // log
+  std::string aResultFileName;
   bool Ok;
 
-  TCollection_AsciiString aGMFFileName, aRequiredVerticesFileName, aSolFileName, aResSolFileName;
-//#ifdef _DEBUG_
+  std::string aGMFFileName, aRequiredVerticesFileName, aSolFileName, aResSolFileName;
   aGMFFileName              = aGenericName + ".mesh"; // GMF mesh file
   aResultFileName           = aGenericName + "Vol.mesh"; // GMF mesh file
   aResSolFileName           = aGenericName + "Vol.sol"; // GMF mesh file
   aRequiredVerticesFileName = aGenericNameRequired + ".mesh"; // GMF required vertices mesh file
   aSolFileName              = aGenericNameRequired + ".sol"; // GMF solution file
-//#else
-//  aGMFFileName    = aGenericName + ".meshb"; // GMF mesh file
-//  aResultFileName = aGenericName + "Vol.meshb"; // GMF mesh file
-//  aRequiredVerticesFileName    = aGenericNameRequired + ".meshb"; // GMF required vertices mesh file
-//  aSolFileName    = aGenericNameRequired + ".solb"; // GMF solution file
-//#endif
 
   std::map <int, int> nodeID2nodeIndexMap;
   std::map<std::vector<double>, std::string> enfVerticesWithGroup;
   HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertexCoordsValues coordsSizeMap;
   TopoDS_Shape GeomShape;
-//   TopAbs_ShapeEnum GeomType;
   std::vector<double> coords;
   gp_Pnt aPnt;
   HYBRIDPlugin_Hypothesis::THYBRIDEnforcedVertex* enfVertex;
@@ -3253,69 +1664,31 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   for ( ; enfVerIt != enfVertices.end() ; ++enfVerIt)
   {
     enfVertex = (*enfVerIt);
-//     if (enfVertex->geomEntry.empty() && enfVertex->coords.size()) {
     if (enfVertex->coords.size()) {
-      coordsSizeMap.insert(make_pair(enfVertex->coords,enfVertex->size));
-      enfVerticesWithGroup.insert(make_pair(enfVertex->coords,enfVertex->groupName));
-//       MESSAGE("enfVerticesWithGroup.insert(make_pair(("<<enfVertex->coords[0]<<","<<enfVertex->coords[1]<<","<<enfVertex->coords[2]<<"),\""<<enfVertex->groupName<<"\"))");
+      coordsSizeMap.insert(std::make_pair(enfVertex->coords,enfVertex->size));
+      enfVerticesWithGroup.insert(std::make_pair(enfVertex->coords,enfVertex->groupName));
     }
     else {
-//     if (!enfVertex->geomEntry.empty()) {
       GeomShape = entryToShape(enfVertex->geomEntry);
-//       GeomType = GeomShape.ShapeType();
-
-//       if (!enfVertex->isCompound) {
-// //       if (GeomType == TopAbs_VERTEX) {
-//         coords.clear();
-//         aPnt = BRep_Tool::Pnt(TopoDS::Vertex(GeomShape));
-//         coords.push_back(aPnt.X());
-//         coords.push_back(aPnt.Y());
-//         coords.push_back(aPnt.Z());
-//         if (coordsSizeMap.find(coords) == coordsSizeMap.end()) {
-//           coordsSizeMap.insert(make_pair(coords,enfVertex->size));
-//           enfVerticesWithGroup.insert(make_pair(coords,enfVertex->groupName));
-//         }
-//       }
-//
-//       // Group Management
-//       else {
-//       if (GeomType == TopAbs_COMPOUND){
-        for (TopoDS_Iterator it (GeomShape); it.More(); it.Next()){
-          coords.clear();
-          if (it.Value().ShapeType() == TopAbs_VERTEX){
-            aPnt = BRep_Tool::Pnt(TopoDS::Vertex(it.Value()));
-            coords.push_back(aPnt.X());
-            coords.push_back(aPnt.Y());
-            coords.push_back(aPnt.Z());
-            if (coordsSizeMap.find(coords) == coordsSizeMap.end()) {
-              coordsSizeMap.insert(make_pair(coords,enfVertex->size));
-              enfVerticesWithGroup.insert(make_pair(coords,enfVertex->groupName));
-//               MESSAGE("enfVerticesWithGroup.insert(make_pair(("<<coords[0]<<","<<coords[1]<<","<<coords[2]<<"),\""<<enfVertex->groupName<<"\"))");
-            }
+      for (TopoDS_Iterator it (GeomShape); it.More(); it.Next()){
+        coords.clear();
+        if (it.Value().ShapeType() == TopAbs_VERTEX){
+          aPnt = BRep_Tool::Pnt(TopoDS::Vertex(it.Value()));
+          coords.push_back(aPnt.X());
+          coords.push_back(aPnt.Y());
+          coords.push_back(aPnt.Z());
+          if (coordsSizeMap.find(coords) == coordsSizeMap.end()) {
+            coordsSizeMap.insert(std::make_pair(coords,enfVertex->size));
+            enfVerticesWithGroup.insert(std::make_pair(coords,enfVertex->groupName));
           }
         }
-//       }
+      }
     }
   }
 
-//   const SMDS_MeshNode* enfNode;
   HYBRIDPlugin_Hypothesis::TIDSortedNodeGroupMap enforcedNodes = HYBRIDPlugin_Hypothesis::GetEnforcedNodes(_hyp);
-//   HYBRIDPlugin_Hypothesis::TIDSortedNodeGroupMap::const_iterator enfNodeIt = enforcedNodes.begin();
-//   for ( ; enfNodeIt != enforcedNodes.end() ; ++enfNodeIt)
-//   {
-//     enfNode = enfNodeIt->first;
-//     coords.clear();
-//     coords.push_back(enfNode->X());
-//     coords.push_back(enfNode->Y());
-//     coords.push_back(enfNode->Z());
-//     if (enfVerticesWithGro
-//       enfVerticesWithGroup.insert(make_pair(coords,enfNodeIt->second));
-//   }
-
-
   HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap enforcedEdges = HYBRIDPlugin_Hypothesis::GetEnforcedEdges(_hyp);
   HYBRIDPlugin_Hypothesis::TIDSortedElemGroupMap enforcedTriangles = HYBRIDPlugin_Hypothesis::GetEnforcedTriangles(_hyp);
-//   TIDSortedElemSet enforcedQuadrangles = HYBRIDPlugin_Hypothesis::GetEnforcedQuadrangles(_hyp);
   HYBRIDPlugin_Hypothesis::TID2SizeMap nodeIDToSizeMap = HYBRIDPlugin_Hypothesis::GetNodeIDToSizeMap(_hyp);
 
   std::string tmpStr;
@@ -3331,46 +1704,49 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   std::vector <const SMDS_MeshElement*> aFaceByHybridId;
   std::map<const SMDS_MeshNode*,int> aNodeToHybridIdMap;
   std::vector<std::string> aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId;
-  {
-    SMESH_ProxyMesh::Ptr proxyMesh( new SMESH_ProxyMesh( theMesh ));
-    if ( theMesh.NbQuadrangles() > 0 )
-    {
-      StdMeshers_QuadToTriaAdaptor* aQuad2Trias = new StdMeshers_QuadToTriaAdaptor;
-      aQuad2Trias->Compute( theMesh );
-      proxyMesh.reset( aQuad2Trias );
-    }
 
-    Ok = writeGMFFile(aGMFFileName.ToCString(), aRequiredVerticesFileName.ToCString(), aSolFileName.ToCString(),
-                      *proxyMesh, *theHelper,
-                      aNodeByHybridId, aFaceByHybridId, aNodeToHybridIdMap,
-                      aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId,
-                      enforcedNodes, enforcedEdges, enforcedTriangles,
-                      enfVerticesWithGroup, coordsSizeMap);
-  }
+  SMESH_ProxyMesh::Ptr proxyMesh( new SMESH_ProxyMesh( theMesh ));
+
+  MG_HYBRID_API mgHybrid( _computeCanceled, _progress );
+
+  Ok = writeGMFFile(&mgHybrid,
+                    aGMFFileName.c_str(),
+                    aRequiredVerticesFileName.c_str(), aSolFileName.c_str(),
+                    *proxyMesh, *theHelper,
+                    aNodeByHybridId, aFaceByHybridId, aNodeToHybridIdMap,
+                    aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId,
+                    enforcedNodes, enforcedEdges, enforcedTriangles,
+                    enfVerticesWithGroup, coordsSizeMap);
 
   // -----------------
   // run hybrid mesher
   // -----------------
 
-  TCollection_AsciiString cmd = TCollection_AsciiString((char*)HYBRIDPlugin_Hypothesis::CommandToRun( _hyp, false ).c_str());
+  std::string cmd = HYBRIDPlugin_Hypothesis::CommandToRun( _hyp );
 
-  cmd += TCollection_AsciiString(" --in ") + aGMFFileName;
-  //if ( nbEnforcedVertices + nbEnforcedNodes)
-  //  cmd += TCollection_AsciiString(" --required_vertices ") + aGenericNameRequired;
-  cmd += TCollection_AsciiString(" --out ") + aResultFileName;
+  if ( mgHybrid.IsExecutable() )
+  {
+    cmd += " --in "  + aGMFFileName;
+    cmd += " --out " + aResultFileName;
+  }
   if ( !_logInStandardOutput )
-    cmd += TCollection_AsciiString(" &>" ) + aLogFileName;  // dump into file
-
+  {
+    cmd += " 1> " + aLogFileName;  // dump into file
+    mgHybrid.SetLogFile( aLogFileName );
+  }
   std::cout << std::endl;
   std::cout << "Hybrid execution w/o geometry..." << std::endl;
   std::cout << cmd << std::endl;
 
-  _compute_canceled = false;
+  _computeCanceled = false;
 
-  system( cmd.ToCString() ); // run
+  std::string errStr;
+  Ok = mgHybrid.Compute( cmd, errStr ); // run
 
-  std::cout << std::endl;
-  std::cout << "End of Hybrid execution !" << std::endl;
+  if ( _logInStandardOutput && mgHybrid.IsLibrary() )
+    std::cout << std::endl << mgHybrid.GetLog() << std::endl;
+  if ( Ok )
+    std::cout << "End of Hybrid execution !" << std::endl;
 
   // --------------
   // read a result
@@ -3378,14 +1754,14 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   HYBRIDPlugin_Hypothesis::TSetStrings groupsToRemove = HYBRIDPlugin_Hypothesis::GetGroupsToRemove(_hyp);
   const bool toMakeGroupsOfDomains = HYBRIDPlugin_Hypothesis::GetToMakeGroupsOfDomains( _hyp );
 
-  Ok = readGMFFile(aResultFileName.ToCString(),
-                   this,
-                   theHelper, aNodeByHybridId, aFaceByHybridId, aNodeToHybridIdMap,
-                   aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId,
-                   groupsToRemove, toMakeGroupsOfDomains);
+  Ok = Ok && readGMFFile(&mgHybrid,
+                         aResultFileName.c_str(),
+                         this,
+                         theHelper, aNodeByHybridId, aFaceByHybridId, aNodeToHybridIdMap,
+                         aNodeGroupByHybridId, anEdgeGroupByHybridId, aFaceGroupByHybridId,
+                         groupsToRemove, toMakeGroupsOfDomains);
 
   updateMeshGroups(theHelper->GetMesh(), groupsToRemove);
-  //removeEmptyGroupsOfDomains( theHelper->GetMesh(), notEmptyAsWell );
   removeEmptyGroupsOfDomains( theHelper->GetMesh(), !toMakeGroupsOfDomains );
 
   if ( Ok ) {
@@ -3401,26 +1777,24 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
   {
     if ( _removeLogOnSuccess )
       removeFile( aLogFileName );
-
-    //if ( !toMakeGroupsOfDomains && _hyp && _hyp->GetToMakeGroupsOfDomains() )
-    //error( COMPERR_WARNING, "'toMakeGroupsOfDomains' is ignored since 'toMeshHoles' is OFF." );
   }
-  else if ( OSD_File( aLogFileName ).Size() > 0 )
+  else if ( mgHybrid.HasLog() )
   {
     // get problem description from the log file
     _Ghs2smdsConvertor conv( aNodeByHybridId );
-    storeErrorDescription( aLogFileName, conv );
+    storeErrorDescription( _logInStandardOutput ? 0 : aLogFileName.c_str(),
+                           mgHybrid.GetLog(), conv );
   }
   else {
     // the log file is empty
     removeFile( aLogFileName );
-    INFOS( "HYBRID Error, command '" << cmd.ToCString() << "' failed" );
+    INFOS( "HYBRID Error, command '" << cmd << "' failed" );
     error(COMPERR_ALGO_FAILED, "hybrid: command not found" );
   }
 
   if ( !_keepFiles )
   {
-    if (! Ok && _compute_canceled)
+    if (! Ok && _computeCanceled)
       removeFile( aLogFileName );
     removeFile( aGMFFileName );
     removeFile( aResultFileName );
@@ -3433,9 +1807,8 @@ bool HYBRIDPlugin_HYBRID::Compute(SMESH_Mesh&         theMesh,
 
 void HYBRIDPlugin_HYBRID::CancelCompute()
 {
-  _compute_canceled = true;
-#ifdef WIN32
-#else
+  _computeCanceled = true;
+#if !defined(WIN32) && !defined(__APPLE__)
   std::string cmd = "ps xo pid,args | grep " + _genericName;
   //cmd += " | grep -e \"^ *[0-9]\\+ \\+" + HYBRIDPlugin_Hypothesis::GetExeName() + "\"";
   cmd += " | awk '{print $1}' | xargs kill -9 > /dev/null 2>&1";
@@ -3466,7 +1839,7 @@ static const char* translateError(const int errNum)
  */
 //================================================================================
 
-static char* getIds( char* ptr, int nbIds, vector<int>& ids )
+static char* getIds( char* ptr, int nbIds, std::vector<int>& ids )
 {
   ids.clear();
   ids.reserve( nbIds );
@@ -3487,30 +1860,15 @@ static char* getIds( char* ptr, int nbIds, vector<int>& ids )
  */
 //================================================================================
 
-bool HYBRIDPlugin_HYBRID::storeErrorDescription(const TCollection_AsciiString& logFile,
-                                                const _Ghs2smdsConvertor &     toSmdsConvertor )
+bool HYBRIDPlugin_HYBRID::storeErrorDescription(const char*                logFile,
+                                                const std::string&         log,
+                                                const _Ghs2smdsConvertor & toSmdsConvertor )
 {
-  if(_compute_canceled)
+  if(_computeCanceled)
     return error(SMESH_Comment("interruption initiated by user"));
-  // open file
-#ifdef WIN32
-  int file = ::_open (logFile.ToCString(), _O_RDONLY|_O_BINARY);
-#else
-  int file = ::open (logFile.ToCString(), O_RDONLY);
-#endif
-  if ( file < 0 )
-    return error( SMESH_Comment("See ") << logFile << " for problem description");
-
-  // get file size
-  off_t length = lseek( file, 0, SEEK_END);
-  lseek( file, 0, SEEK_SET);
 
-  // read file
-  vector< char > buf( length );
-  int nBytesRead = ::read (file, & buf[0], length);
-  ::close (file);
-  char* ptr = & buf[0];
-  char* bufEnd = ptr + nBytesRead;
+  char* ptr = const_cast<char*>( log.c_str() );
+  char* buf = ptr, * bufEnd = ptr + log.size();
 
   SMESH_Comment errDescription;
 
@@ -3537,15 +1895,15 @@ bool HYBRIDPlugin_HYBRID::storeErrorDescription(const TCollection_AsciiString& l
 
   // look for errors "ERR #"
 
-  set<string> foundErrorStr; // to avoid reporting same error several times
-  set<int>    elemErrorNums; // not to report different types of errors with bad elements
+  std::set<std::string> foundErrorStr; // to avoid reporting same error several times
+  std::set<int>         elemErrorNums; // not to report different types of errors with bad elements
   while ( ++ptr < bufEnd )
   {
     if ( strncmp( ptr, "ERR ", 4 ) != 0 )
       continue;
 
-    list<const SMDS_MeshElement*> badElems;
-    vector<int> nodeIds;
+    std::list<const SMDS_MeshElement*> badElems;
+    std::vector<int> nodeIds;
 
     ptr += 4;
     char* errBeg = ptr;
@@ -3565,7 +1923,7 @@ bool HYBRIDPlugin_HYBRID::storeErrorDescription(const TCollection_AsciiString& l
       ptr = getIds(ptr, TRIA, nodeIds);
       badElems.push_back( toSmdsConvertor.getElement(nodeIds));
       {
-        vector<int> edgeNodes( nodeIds.begin(), --nodeIds.end() ); // 01
+        std::vector<int> edgeNodes( nodeIds.begin(), --nodeIds.end() ); // 01
         badElems.push_back( toSmdsConvertor.getElement(edgeNodes));
         edgeNodes[1] = nodeIds[2]; // 02
         badElems.push_back( toSmdsConvertor.getElement(edgeNodes));
@@ -3652,7 +2010,7 @@ bool HYBRIDPlugin_HYBRID::storeErrorDescription(const TCollection_AsciiString& l
       // even if all nodes found, volume it most probably invisible,
       // add its faces to demonstrate it anyhow
       {
-        vector<int> faceNodes( nodeIds.begin(), --nodeIds.end() ); // 012
+        std::vector<int> faceNodes( nodeIds.begin(), --nodeIds.end() ); // 012
         badElems.push_back( toSmdsConvertor.getElement(faceNodes));
         faceNodes[2] = nodeIds[3]; // 013
         badElems.push_back( toSmdsConvertor.getElement(faceNodes));
@@ -3688,7 +2046,7 @@ bool HYBRIDPlugin_HYBRID::storeErrorDescription(const TCollection_AsciiString& l
       badElems.push_back( toSmdsConvertor.getElement(nodeIds));
       // add triangle edges as it most probably has zero area and hence invisible
       {
-        vector<int> edgeNodes(2);
+        std::vector<int> edgeNodes(2);
         edgeNodes[0] = nodeIds[0]; edgeNodes[1] = nodeIds[1]; // 0-1
         badElems.push_back( toSmdsConvertor.getElement(edgeNodes));
         edgeNodes[1] = nodeIds[2]; // 0-2
@@ -3705,7 +2063,7 @@ bool HYBRIDPlugin_HYBRID::storeErrorDescription(const TCollection_AsciiString& l
       break;
     }
 
-    bool isNewError = foundErrorStr.insert( string( errBeg, ptr )).second;
+    bool isNewError = foundErrorStr.insert( std::string( errBeg, ptr )).second;
     if ( !isNewError )
       continue; // not to report same error several times
 
@@ -3720,13 +2078,13 @@ bool HYBRIDPlugin_HYBRID::storeErrorDescription(const TCollection_AsciiString& l
 
     // store bad elements
     //if ( allElemsOk ) {
-      list<const SMDS_MeshElement*>::iterator elem = badElems.begin();
+      std::list<const SMDS_MeshElement*>::iterator elem = badElems.begin();
       for ( ; elem != badElems.end(); ++elem )
         addBadInputElement( *elem );
       //}
 
     // make error text
-    string text = translateError( errNum );
+    std::string text = translateError( errNum );
     if ( errDescription.find( text ) == text.npos ) {
       if ( !errDescription.empty() )
         errDescription << "\n";
@@ -3738,22 +2096,24 @@ bool HYBRIDPlugin_HYBRID::storeErrorDescription(const TCollection_AsciiString& l
   if ( errDescription.empty() ) { // no errors found
     char msgLic1[] = "connection to server failed";
     char msgLic2[] = " Dlim ";
-    if ( search( &buf[0], bufEnd, msgLic1, msgLic1 + strlen(msgLic1)) != bufEnd ||
-         search( &buf[0], bufEnd, msgLic2, msgLic2 + strlen(msgLic2)) != bufEnd )
+    if ( std::search( &buf[0], bufEnd, msgLic1, msgLic1 + strlen(msgLic1)) != bufEnd ||
+         std::search( &buf[0], bufEnd, msgLic2, msgLic2 + strlen(msgLic2)) != bufEnd )
       errDescription << "Licence problems.";
     else
     {
       char msg2[] = "SEGMENTATION FAULT";
-      if ( search( &buf[0], bufEnd, msg2, msg2 + strlen(msg2)) != bufEnd )
+      if ( std::search( &buf[0], bufEnd, msg2, msg2 + strlen(msg2)) != bufEnd )
         errDescription << "hybrid: SEGMENTATION FAULT. ";
     }
   }
 
-  if ( errDescription.empty() )
-    errDescription << "See " << logFile << " for problem description";
-  else
-    errDescription << "\nSee " << logFile << " for more information";
-
+  if ( logFile && logFile[0] )
+  {
+    if ( errDescription.empty() )
+      errDescription << "See " << logFile << " for problem description";
+    else
+      errDescription << "\nSee " << logFile << " for more information";
+  }
   return error( errDescription );
 }
 
@@ -3763,7 +2123,7 @@ bool HYBRIDPlugin_HYBRID::storeErrorDescription(const TCollection_AsciiString& l
  */
 //================================================================================
 
-_Ghs2smdsConvertor::_Ghs2smdsConvertor( const map <int,const SMDS_MeshNode*> & ghs2NodeMap)
+_Ghs2smdsConvertor::_Ghs2smdsConvertor( const std::map <int,const SMDS_MeshNode*> & ghs2NodeMap)
   :_ghs2NodeMap( & ghs2NodeMap ), _nodeByGhsId( 0 )
 {
 }
@@ -3774,7 +2134,7 @@ _Ghs2smdsConvertor::_Ghs2smdsConvertor( const map <int,const SMDS_MeshNode*> & g
  */
 //================================================================================
 
-_Ghs2smdsConvertor::_Ghs2smdsConvertor( const vector <const SMDS_MeshNode*> &  nodeByGhsId)
+_Ghs2smdsConvertor::_Ghs2smdsConvertor( const std::vector <const SMDS_MeshNode*> &  nodeByGhsId)
   : _ghs2NodeMap( 0 ), _nodeByGhsId( &nodeByGhsId )
 {
 }
@@ -3785,20 +2145,20 @@ _Ghs2smdsConvertor::_Ghs2smdsConvertor( const vector <const SMDS_MeshNode*> &  n
  */
 //================================================================================
 
-const SMDS_MeshElement* _Ghs2smdsConvertor::getElement(const vector<int>& ghsNodes) const
+const SMDS_MeshElement* _Ghs2smdsConvertor::getElement(const std::vector<int>& ghsNodes) const
 {
   size_t nbNodes = ghsNodes.size();
-  vector<const SMDS_MeshNode*> nodes( nbNodes, 0 );
+  std::vector<const SMDS_MeshNode*> nodes( nbNodes, 0 );
   for ( size_t i = 0; i < nbNodes; ++i ) {
     int ghsNode = ghsNodes[ i ];
     if ( _ghs2NodeMap ) {
-      map <int,const SMDS_MeshNode*>::const_iterator in = _ghs2NodeMap->find( ghsNode);
+      std::map <int,const SMDS_MeshNode*>::const_iterator in = _ghs2NodeMap->find( ghsNode);
       if ( in == _ghs2NodeMap->end() )
         return 0;
       nodes[ i ] = in->second;
     }
     else {
-      if ( ghsNode < 1 || ghsNode > _nodeByGhsId->size() )
+      if ( ghsNode < 1 || ghsNode > (int)_nodeByGhsId->size() )
         return 0;
       nodes[ i ] = (*_nodeByGhsId)[ ghsNode-1 ];
     }
@@ -3846,9 +2206,9 @@ bool HYBRIDPlugin_HYBRID::Evaluate(SMESH_Mesh& aMesh,
                                             "Submesh can not be evaluated",this));
       return false;
     }
-    std::vector<int> aVec = (*anIt).second;
-    nbtri += Max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
-    nbqua += Max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
+    std::vector<smIdType> aVec = (*anIt).second;
+    nbtri += std::max(aVec[SMDSEntity_Triangle],aVec[SMDSEntity_Quad_Triangle]);
+    nbqua += std::max(aVec[SMDSEntity_Quadrangle],aVec[SMDSEntity_Quad_Quadrangle]);
     GProp_GProps G;
     BRepGProp::SurfaceProperties(F,G);
     double anArea = G.Mass();
@@ -3867,9 +2227,9 @@ bool HYBRIDPlugin_HYBRID::Evaluate(SMESH_Mesh& aMesh,
     tmpMap.Add(E);
     SMESH_subMesh *aSubMesh = aMesh.GetSubMesh(exp.Current());
     MapShapeNbElemsItr anIt = aResMap.find(aSubMesh);
-    std::vector<int> aVec = (*anIt).second;
+    std::vector<smIdType> aVec = (*anIt).second;
     nb0d_e += aVec[SMDSEntity_Node];
-    nb1d_e += Max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
+    nb1d_e += std::max(aVec[SMDSEntity_Edge],aVec[SMDSEntity_Quad_Edge]);
     if(IsFirst) {
       IsQuadratic = (aVec[SMDSEntity_Quad_Edge] > aVec[SMDSEntity_Edge]);
       IsFirst = false;
@@ -3887,7 +2247,7 @@ bool HYBRIDPlugin_HYBRID::Evaluate(SMESH_Mesh& aMesh,
   int nbVols = int(aVolume/tetrVol/CoeffQuality);
   int nb1d_f = (nbtri*3 + nbqua*4 - nb1d_e) / 2;
   int nb1d_in = (int) ( nbVols*6 - nb1d_e - nb1d_f ) / 5;
-  std::vector<int> aVec(SMDSEntity_Last);
+  std::vector<smIdType> aVec(SMDSEntity_Last);
   for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i]=0;
   if( IsQuadratic ) {
     aVec[SMDSEntity_Node] = nb1d_in/6 + 1 + nb1d_in;
@@ -3907,19 +2267,11 @@ bool HYBRIDPlugin_HYBRID::Evaluate(SMESH_Mesh& aMesh,
 
 bool HYBRIDPlugin_HYBRID::importGMFMesh(const char* theGMFFileName, SMESH_Mesh& theMesh)
 {
-  SMESH_MesherHelper* helper  = new SMESH_MesherHelper(theMesh );
-  std::vector <const SMDS_MeshNode*> dummyNodeVector;
-  std::vector <const SMDS_MeshElement*> aFaceByHybridId;
-  std::map<const SMDS_MeshNode*,int> dummyNodeMap;
-  std::map<std::vector<double>, std::string> dummyEnfVertGroup;
-  std::vector<std::string> dummyElemGroup;
-  std::set<std::string> dummyGroupsToRemove;
-
-  bool ok = readGMFFile(theGMFFileName,
-                        this,
-                        helper, dummyNodeVector, aFaceByHybridId, dummyNodeMap, dummyElemGroup, dummyElemGroup, dummyElemGroup, dummyGroupsToRemove);
+  SMESH_ComputeErrorPtr err = theMesh.GMFToMesh( theGMFFileName, /*makeRequiredGroups =*/ true );
+
   theMesh.GetMeshDS()->Modified();
-  return ok;
+
+  return ( !err || err->IsOK());
 }
 
 namespace
@@ -3947,9 +2299,9 @@ namespace
      */
     void ProcessEvent(const int                       event,
                       const int                       eventType,
-                      SMESH_subMesh*                  subMesh,
+                      SMESH_subMesh*                  /*subMesh*/,
                       SMESH_subMeshEventListenerData* data,
-                      const SMESH_Hypothesis*         hyp)
+                      const SMESH_Hypothesis*         /*hyp*/)
     {
       if ( SMESH_subMesh::SUBMESH_LOADED == event &&
            SMESH_subMesh::COMPUTE_EVENT  == eventType &&
@@ -3988,11 +2340,11 @@ namespace
     /*!
      * \brief Treat events of the subMesh
      */
-    void ProcessEvent(const int                       event,
+    void ProcessEvent(const int                       /*event*/,
                       const int                       eventType,
                       SMESH_subMesh*                  subMesh,
-                      SMESH_subMeshEventListenerData* data,
-                      const SMESH_Hypothesis*         hyp)
+                      SMESH_subMeshEventListenerData* /*data*/,
+                      const SMESH_Hypothesis*         /*hyp*/)
     {
       if (SMESH_subMesh::ALGO_EVENT == eventType &&
           !subMesh->GetAlgo() )