Salome HOME
PAL10195: SALOME V2 study with a mesh leads to SIGSEGV of SALOME V3
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i.cxx
index fb6ed194996773cc282b73cbca102d68415e2e94..77abfc9e5285878013b6f0bc0b1e9a81464ebfd2 100644 (file)
@@ -26,7 +26,6 @@
 //  Module : SMESH
 //  $Header$
 
-using namespace std;
 #include <TopExp.hxx>
 #include <TopExp_Explorer.hxx>
 #include <TopoDS.hxx>
@@ -63,19 +62,17 @@ using namespace std;
 
 #include "SMESHDS_Document.hxx"
 #include "SMESHDS_Group.hxx"
-#include "SMESH_topo.hxx"
+#include "SMESHDS_GroupOnGeom.hxx"
 #include "SMESH_Group.hxx"
 
+#include "SMDS_EdgePosition.hxx"
+#include "SMDS_FacePosition.hxx"
+
 #include CORBA_SERVER_HEADER(SMESH_Group)
 #include CORBA_SERVER_HEADER(SMESH_Filter)
-#include "SMESH_Filter_i.hxx"
 
-#include "Document_Reader.h"
 #include "DriverMED_W_SMESHDS_Mesh.h"
 #include "DriverMED_R_SMESHDS_Mesh.h"
-#include "DriverMED_R_SMESHDS_Document.h"
-#include "DriverUNV_R_SMESHDS_Document.h"
-#include "DriverDAT_R_SMESHDS_Document.h"
 
 #include "SALOMEDS_Tool.hxx"
 #include "SALOME_NamingService.hxx"
@@ -89,137 +86,24 @@ using namespace std;
 #include "Utils_ExceptHandlers.hxx"
 
 #include <map>
+#include <boost/filesystem/path.hpp>
+
+using namespace std;
 
 #define NUM_TMP_FILES 2
 
-// Tags definition ===========================================================
-// Top level
-long Tag_HypothesisRoot         = 1; // hypotheses root
-long Tag_AlgorithmsRoot         = 2; // algorithms root
-// Mesh/Submesh
-long Tag_RefOnShape             = 1; // references to shape
-long Tag_RefOnAppliedHypothesis = 2; // applied hypotheses root
-long Tag_RefOnAppliedAlgorithms = 3; // applied algorithms root
-// Mesh only
-long Tag_SubMeshOnVertex        = 4; // sub-meshes roots by type
-long Tag_SubMeshOnEdge          = 5; // ...
-long Tag_SubMeshOnFace          = 6; // ...
-long Tag_SubMeshOnSolid         = 7; // ...
-long Tag_SubMeshOnCompound      = 8; // ...
-long Tag_NodeGroups             = 9; // Group roots by type
-long Tag_EdgeGroups             = 10; // ...
-long Tag_FaceGroups             = 11; // ...
-long Tag_VolumeGroups           = 12; // ...
-// ===========================================================================
+#ifdef _DEBUG_
+static int MYDEBUG = 0;
+#else
+static int MYDEBUG = 0;
+#endif
 
 // Static variables definition
 CORBA::ORB_var          SMESH_Gen_i::myOrb;
 PortableServer::POA_var SMESH_Gen_i::myPoa;
 SALOME_NamingService*   SMESH_Gen_i::myNS  = NULL;
 SALOME_LifeCycleCORBA*  SMESH_Gen_i::myLCC = NULL;
-
-//=============================================================================
-/*!
- *  FindMaxChildTag [ static internal ]
- *
- *  Finds maximum child tag for the given object
- */
-//=============================================================================
-
-static long FindMaxChildTag( SALOMEDS::SObject_ptr theSObject )
-{
-  long aTag = 0;
-  if ( !theSObject->_is_nil() ) {
-    SALOMEDS::Study_var aStudy = theSObject->GetStudy();
-    if ( !aStudy->_is_nil() ) {
-      SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( theSObject );
-      for ( ; anIter->More(); anIter->Next() ) {
-       long nTag = anIter->Value()->Tag();
-       if ( nTag > aTag )
-         aTag = nTag;
-      }
-    }
-  }
-  return aTag;
-}
-
-//=============================================================================
-/*!
- *  Get...Tag [ static ]
- *
- *  Methods which determine SMESH data model structure
- */
-//=============================================================================
-
-long SMESH_Gen_i::GetHypothesisRootTag()
-{
-  return Tag_HypothesisRoot;
-}
-
-long SMESH_Gen_i::GetAlgorithmsRootTag()
-{
-  return Tag_AlgorithmsRoot;
-}
-
-long SMESH_Gen_i::GetRefOnShapeTag()
-{
-  return Tag_RefOnShape;
-}
-
-long SMESH_Gen_i::GetRefOnAppliedHypothesisTag()
-{
-  return Tag_RefOnAppliedHypothesis;
-}
-
-long SMESH_Gen_i::GetRefOnAppliedAlgorithmsTag()
-{
-  return Tag_RefOnAppliedAlgorithms;
-}
-
-long SMESH_Gen_i::GetSubMeshOnVertexTag()
-{
-  return Tag_SubMeshOnVertex;
-}
-
-long SMESH_Gen_i::GetSubMeshOnEdgeTag()
-{
-  return Tag_SubMeshOnEdge;
-}
-
-long SMESH_Gen_i::GetSubMeshOnFaceTag()
-{
-  return Tag_SubMeshOnFace;
-}
-
-long SMESH_Gen_i::GetSubMeshOnSolidTag()
-{
-  return Tag_SubMeshOnSolid;
-}
-
-long SMESH_Gen_i::GetSubMeshOnCompoundTag()
-{
-  return Tag_SubMeshOnCompound;
-}
-
-long SMESH_Gen_i::GetNodeGroupsTag()
-{
-  return Tag_NodeGroups;
-}
-
-long SMESH_Gen_i::GetEdgeGroupsTag()
-{
-  return Tag_EdgeGroups;
-}
-
-long SMESH_Gen_i::GetFaceGroupsTag()
-{
-  return Tag_FaceGroups;
-}
-
-long SMESH_Gen_i::GetVolumeGroupsTag()
-{
-  return Tag_VolumeGroups;
-}
+SMESH_Gen_i*            SMESH_Gen_i::mySMESHGen = NULL;
 
 //=============================================================================
 /*!
@@ -238,7 +122,7 @@ PortableServer::ServantBase_var SMESH_Gen_i::GetServant( CORBA::Object_ptr theOb
     return aServant;
   } 
   catch (...) {
-    MESSAGE( "GetServant - Unknown exception was caught!!!" ); 
+    INFOS( "GetServant - Unknown exception was caught!!!" ); 
     return NULL;
   }
 }
@@ -265,7 +149,7 @@ CORBA::Object_var SMESH_Gen_i::SObjectToObject( SALOMEDS::SObject_ptr theSObject
        }
     }
     catch( ... ) {
-      MESSAGE( "SObjectToObject - Unknown exception was caught!!!" );
+      INFOS( "SObjectToObject - Unknown exception was caught!!!" );
     }
   }
   return anObj;
@@ -327,7 +211,7 @@ GEOM::GEOM_Gen_ptr SMESH_Gen_i::GetGeomEngine() {
 
 SMESH_Gen_i::SMESH_Gen_i()
 {
-  MESSAGE( "SMESH_Gen_i::SMESH_Gen_i : default constructor" );
+  INFOS( "SMESH_Gen_i::SMESH_Gen_i : default constructor" );
 }
 
 //=============================================================================
@@ -345,7 +229,7 @@ SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr            orb,
                           const char*               interfaceName )
      : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
 {
-  MESSAGE( "SMESH_Gen_i::SMESH_Gen_i : standard constructor" );
+  INFOS( "SMESH_Gen_i::SMESH_Gen_i : standard constructor" );
 
   myOrb = CORBA::ORB::_duplicate(orb);
   myPoa = PortableServer::POA::_duplicate(poa);
@@ -354,6 +238,7 @@ SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr            orb,
   _id = myPoa->activate_object( _thisObj );
   
   myShapeReader = NULL;  // shape reader
+  mySMESHGen = this;
 }
 
 //=============================================================================
@@ -366,7 +251,7 @@ SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr            orb,
 
 SMESH_Gen_i::~SMESH_Gen_i()
 {
-  MESSAGE( "SMESH_Gen_i::~SMESH_Gen_i" );
+  INFOS( "SMESH_Gen_i::~SMESH_Gen_i" );
 
   // delete hypothesis creators
   map<string, GenericHypothesisCreator_i*>::iterator itHyp;
@@ -387,21 +272,6 @@ SMESH_Gen_i::~SMESH_Gen_i()
     delete myShapeReader;
 }
   
-//=============================================================================
-/*!
- *  SMESH_Gen_i::CreateFilterManager
- *
- *  Create filter manager
- */
-//=============================================================================
-
-SMESH::FilterManager_ptr SMESH_Gen_i::CreateFilterManager()
-{
-  SMESH::FilterManager_i* aFilter = new SMESH::FilterManager_i();
-  SMESH::FilterManager_var anObj = aFilter->_this();
-  return anObj._retn();
-}
-
 //=============================================================================
 /*!
  *  SMESH_Gen_i::createHypothesis
@@ -414,11 +284,8 @@ SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName
      throw (SALOME::SALOME_Exception)
 {
   Unexpect aCatch(SALOME_SalomeException);
-  MESSAGE( "Create Hypothesis <" << theHypName << "> from " << theLibName);
+  if(MYDEBUG) MESSAGE( "Create Hypothesis <" << theHypName << "> from " << theLibName);
 
-  // get study context
-  StudyContext* myStudyContext = GetCurrentStudyContext();
-  
   // create a new hypothesis object servant
   SMESH_Hypothesis_i* myHypothesis_i = 0;
   SMESH::SMESH_Hypothesis_var hypothesis_i;
@@ -429,7 +296,7 @@ SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName
     if (myHypCreatorMap.find(string(theHypName)) == myHypCreatorMap.end())
     {
       // load plugin library
-      MESSAGE("Loading server meshers plugin library ...");
+      if(MYDEBUG) MESSAGE("Loading server meshers plugin library ...");
       void* libHandle = dlopen (theLibName, RTLD_LAZY);
       if (!libHandle)
       {
@@ -439,7 +306,7 @@ SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName
       }
 
       // get method, returning hypothesis creator
-      MESSAGE("Find GetHypothesisCreator() method ...");
+      if(MYDEBUG) MESSAGE("Find GetHypothesisCreator() method ...");
       typedef GenericHypothesisCreator_i* (*GetHypothesisCreator)(const char* theHypName);
       GetHypothesisCreator procHandle =
         (GetHypothesisCreator)dlsym( libHandle, "GetHypothesisCreator" );
@@ -450,7 +317,7 @@ SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName
       }
 
       // get hypothesis creator
-      MESSAGE("Get Hypothesis Creator for " << theHypName);
+      if(MYDEBUG) MESSAGE("Get Hypothesis Creator for " << theHypName);
       GenericHypothesisCreator_i* aCreator = procHandle(theHypName);
       if (!aCreator)
       {
@@ -462,10 +329,9 @@ SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName
     }
 
     // create a new hypothesis object, store its ref. in studyContext
-    MESSAGE("Create Hypothesis " << theHypName);
+    if(MYDEBUG) MESSAGE("Create Hypothesis " << theHypName);
     myHypothesis_i =
-      myHypCreatorMap[string(theHypName)]->Create
-        (myPoa, myCurrentStudy->StudyId(), &myGen);
+      myHypCreatorMap[string(theHypName)]->Create (myPoa, GetCurrentStudyID(), &myGen);
     // _CS_gbo Explicit activation (no longer made in the constructor).
     myHypothesis_i->Activate();
     myHypothesis_i->SetLibName(theLibName); // for persistency assurance
@@ -480,9 +346,8 @@ SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::createHypothesis(const char* theHypName
 
   // activate the CORBA servant of hypothesis
   hypothesis_i = SMESH::SMESH_Hypothesis::_narrow( myHypothesis_i->_this() );
-  string iorString = GetORB()->object_to_string( hypothesis_i );
-  int nextId = myStudyContext->addObject( iorString );
-  MESSAGE( "Add hypo to map with id = "<< nextId << " and IOR = " << iorString.c_str() );
+  int nextId = RegisterObject( hypothesis_i );
+  if(MYDEBUG) MESSAGE( "Add hypo to map with id = "<< nextId );
 
   return hypothesis_i._retn();
 }
@@ -498,25 +363,19 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::createMesh()
      throw ( SALOME::SALOME_Exception )
 {
   Unexpect aCatch(SALOME_SalomeException);
-  MESSAGE( "SMESH_Gen_i::createMesh" );
-
-  // get current study
-  StudyContext* myStudyContext = GetCurrentStudyContext();
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::createMesh" );
 
   // Get or create the GEOM_Client instance
   try {
     // create a new mesh object servant, store it in a map in study context
-    SMESH_Mesh_i* meshServant = new SMESH_Mesh_i( GetPOA(),
-                                                 this,
-                                                 myCurrentStudy->StudyId() );
+    SMESH_Mesh_i* meshServant = new SMESH_Mesh_i( GetPOA(), this, GetCurrentStudyID() );
     // create a new mesh object
-    meshServant->SetImpl( myGen.CreateMesh( myCurrentStudy->StudyId() ) );
+    meshServant->SetImpl( myGen.CreateMesh( GetCurrentStudyID() ));
 
     // activate the CORBA servant of Mesh
-    SMESH::SMESH_Mesh_var mesh = meshServant->_this();
-    string iorString = GetORB()->object_to_string( mesh );
-    int nextId = myStudyContext->addObject( iorString );
-    MESSAGE( "Add mesh to map with id = "<< nextId << " and IOR = " << iorString.c_str() );
+    SMESH::SMESH_Mesh_var mesh = SMESH::SMESH_Mesh::_narrow( meshServant->_this() );
+    int nextId = RegisterObject( mesh );
+    if(MYDEBUG) MESSAGE( "Add mesh to map with id = "<< nextId);
     return mesh._retn();
   }
   catch (SALOME_Exception& S_ex) {
@@ -551,17 +410,19 @@ GEOM_Client* SMESH_Gen_i::GetShapeReader()
 
 void SMESH_Gen_i::SetCurrentStudy( SALOMEDS::Study_ptr theStudy )
 {
-  MESSAGE( "SMESH_Gen_i::SetCurrentStudy" );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SetCurrentStudy" );
   myCurrentStudy = SALOMEDS::Study::_duplicate( theStudy );
   // create study context, if it doesn't exist and set current study
-  int studyId = myCurrentStudy->StudyId();
-  MESSAGE( "SMESH_Gen_i::SetCurrentStudy: study Id = " << studyId );
+  int studyId = GetCurrentStudyID();
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SetCurrentStudy: study Id = " << studyId );
   if ( myStudyContextMap.find( studyId ) == myStudyContextMap.end() ) {
     myStudyContextMap[ studyId ] = new StudyContext;      
   }
   // set current study for geom engine
+  /*
   if ( !CORBA::is_nil( GetGeomEngine() ) )
     GetGeomEngine()->GetCurrentStudy( myCurrentStudy->StudyId() );
+  */
 }
 
 //=============================================================================
@@ -574,7 +435,7 @@ void SMESH_Gen_i::SetCurrentStudy( SALOMEDS::Study_ptr theStudy )
 
 SALOMEDS::Study_ptr SMESH_Gen_i::GetCurrentStudy()
 {
-  MESSAGE( "SMESH_Gen_i::GetCurrentStudy: study Id = " << myCurrentStudy->StudyId() );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetCurrentStudy: study Id = " << GetCurrentStudyID() );
   return SALOMEDS::Study::_duplicate( myCurrentStudy );
 }
 
@@ -587,9 +448,11 @@ SALOMEDS::Study_ptr SMESH_Gen_i::GetCurrentStudy()
 //=============================================================================
 StudyContext* SMESH_Gen_i::GetCurrentStudyContext()
 {
-  ASSERT( !CORBA::is_nil( myCurrentStudy ) )
-  ASSERT( myStudyContextMap.find( myCurrentStudy->StudyId() ) != myStudyContextMap.end() );
-  return myStudyContextMap[ myCurrentStudy->StudyId() ];
+  if ( !CORBA::is_nil( myCurrentStudy ) &&
+      myStudyContextMap.find( GetCurrentStudyID() ) != myStudyContextMap.end() )
+    return myStudyContextMap[ myCurrentStudy->StudyId() ];
+  else
+    return 0;
 }
 
 //=============================================================================
@@ -605,17 +468,28 @@ SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypNam
      throw ( SALOME::SALOME_Exception )
 {
   Unexpect aCatch(SALOME_SalomeException);
-  ASSERT( !CORBA::is_nil( myCurrentStudy ) );
   // Create hypothesis/algorithm
   SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
 
   // Publish hypothesis/algorithm in the study
-  if ( this->CanPublishInStudy( hyp ) ) {
-    this->PublishInStudy( myCurrentStudy, SALOMEDS::SObject::_nil(), hyp, "" );
+  if ( CanPublishInStudy( hyp ) ) {
+    SALOMEDS::SObject_var aSO = PublishHypothesis( myCurrentStudy, hyp );
+    if ( !aSO->_is_nil() ) {
+      // Update Python script
+      TCollection_AsciiString aStr (aSO->GetID());
+      aStr += " = smesh.CreateHypothesis(\"";
+      aStr += Standard_CString(theHypName);
+      aStr += "\", \"";
+      aStr += Standard_CString(theLibName);
+      aStr += "\")";
+
+      AddToCurrentPyScript(aStr);
+    }
   }
+
   return hyp._retn();
 }
-  
+
 //=============================================================================
 /*!
  *  SMESH_Gen_i::CreateMesh
@@ -624,25 +498,76 @@ SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypNam
  */
 //=============================================================================
 
-SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Shape_ptr theShape )
+SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObject )
      throw ( SALOME::SALOME_Exception )
 {
   Unexpect aCatch(SALOME_SalomeException);
-  MESSAGE( "SMESH_Gen_i::CreateMesh" );
-  ASSERT( !CORBA::is_nil( myCurrentStudy ) );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMesh" );
   // create mesh
   SMESH::SMESH_Mesh_var mesh = this->createMesh();
-  // publish mesh in the study
-  if ( this->CanPublishInStudy( mesh ) ) {
-    this->PublishInStudy( myCurrentStudy, SALOMEDS::SObject::_nil(), mesh.in(), "" );
-  }
   // set shape
   SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
   ASSERT( meshServant );
-  meshServant->SetShape( theShape );
+  meshServant->SetShape( theShapeObject );
+
+  // publish mesh in the study
+  if ( CanPublishInStudy( mesh ) ) {
+    SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
+    aStudyBuilder->NewCommand();  // There is a transaction
+    SALOMEDS::SObject_var aSO = PublishMesh( myCurrentStudy, mesh.in() );
+    aStudyBuilder->CommitCommand();
+    if ( !aSO->_is_nil() ) {
+      // Update Python script
+      TCollection_AsciiString aStr (aSO->GetID());
+      aStr += " = smesh.CreateMesh(";
+      SMESH_Gen_i::AddObject(aStr, theShapeObject) += ")";
+      
+      AddToCurrentPyScript(aStr);
+    }
+  }
+
   return mesh._retn();
 }
-  
+
+//=============================================================================
+/*!
+ *  SMESH_Gen_i::CreateMeshFromUNV
+ *
+ *  Create mesh and import data from UNV file
+ */
+//=============================================================================
+
+SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName )
+  throw ( SALOME::SALOME_Exception )
+{
+  Unexpect aCatch(SALOME_SalomeException);
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshesFromUNV" );
+
+  SMESH::SMESH_Mesh_var aMesh = createMesh();
+  string aFileName; // = boost::filesystem::path(theFileName).leaf();
+  // publish mesh in the study
+  if ( CanPublishInStudy( aMesh ) ) {
+    SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
+    aStudyBuilder->NewCommand();  // There is a transaction
+    SALOMEDS::SObject_var aSO = PublishMesh( myCurrentStudy, aMesh.in(), aFileName.c_str() );
+    aStudyBuilder->CommitCommand();
+    if ( !aSO->_is_nil() ) {
+      // Update Python script
+      TCollection_AsciiString aStr (aSO->GetID());
+      aStr += " = smesh.CreateMeshesFromUNV(\"";
+      aStr += Standard_CString(theFileName);
+      aStr += "\")";
+
+      AddToCurrentPyScript(aStr);
+    }
+  }
+
+  SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
+  ASSERT( aServant );
+  aServant->ImportUNVFile( theFileName );
+  return aMesh._retn();
+}
+
 //=============================================================================
 /*!
  *  SMESH_Gen_i::CreateMeshFromMED
@@ -656,44 +581,109 @@ SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
      throw ( SALOME::SALOME_Exception )
 {
   Unexpect aCatch(SALOME_SalomeException);
-  MESSAGE( "SMESH_Gen_i::CreateMeshFromMED" );
-  ASSERT( !CORBA::is_nil( myCurrentStudy ) );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshFromMED" );
+
+  // Python Dump
+  TCollection_AsciiString aStr ("([");
 
   // Retrieve mesh names from the file
   DriverMED_R_SMESHDS_Mesh myReader;
   myReader.SetFile( theFileName );
   myReader.SetMeshId( -1 );
-  list<string> aNames = myReader.GetMeshNames();
-
+  Driver_Mesh::Status aStatus;
+  list<string> aNames = myReader.GetMeshNames(aStatus);
   SMESH::mesh_array_var aResult = new SMESH::mesh_array();
-  aResult->length( aNames.size() );
-  int i = 0;
-
-  // Iterate through all meshes and create mesh objects
-  theStatus = SMESH::DRS_OK;
-  for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ ) {
-    // create mesh
-    SMESH::SMESH_Mesh_var mesh = createMesh();
+  theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
+  if (theStatus == SMESH::DRS_OK) {
+    SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
+    aStudyBuilder->NewCommand();  // There is a transaction
+    aResult->length( aNames.size() );
+    int i = 0;
     
-    // publish mesh in the study
-    if ( CanPublishInStudy( mesh ) ) {
-      PublishInStudy( myCurrentStudy, SALOMEDS::SObject::_nil(), mesh.in(), (*it).c_str() );
-    }
+    // Iterate through all meshes and create mesh objects
+    for ( list<string>::iterator it = aNames.begin(); it != aNames.end(); it++ ) {
+      // Python Dump
+      if (i > 0) aStr += ", ";
 
-    // Read mesh data (groups are published automatically by ImportMEDFile())
-    SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
-    ASSERT( meshServant );
-    SMESH::DriverMED_ReadStatus status1 =
-      meshServant->ImportMEDFile( theFileName, (*it).c_str() );
-    if (status1 > theStatus)
-      theStatus = status1;
+      // create mesh
+      SMESH::SMESH_Mesh_var mesh = createMesh();
+      
+      // publish mesh in the study
+      SALOMEDS::SObject_var aSO;
+      if ( CanPublishInStudy( mesh ) )
+        aSO = PublishMesh( myCurrentStudy, mesh.in(), (*it).c_str() );
+      if ( !aSO->_is_nil() ) {
+        // Python Dump
+        aStr += aSO->GetID();
+      } else {
+        // Python Dump
+        aStr += "mesh_";
+        aStr += TCollection_AsciiString(i);
+      }
+
+      // Read mesh data (groups are published automatically by ImportMEDFile())
+      SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
+      ASSERT( meshServant );
+      SMESH::DriverMED_ReadStatus status1 =
+       meshServant->ImportMEDFile( theFileName, (*it).c_str() );
+      if (status1 > theStatus)
+       theStatus = status1;
 
-    aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
+      aResult[i++] = SMESH::SMESH_Mesh::_duplicate( mesh );
+    }
+    aStudyBuilder->CommitCommand();
   }
 
+  // Update Python script
+  aStr += "], status) = smesh.CreateMeshesFromMED(\"";
+  aStr += Standard_CString(theFileName);
+  aStr += "\")";
+
+  AddToCurrentPyScript(aStr);
+
   return aResult._retn();
 }
 
+//=============================================================================
+/*!
+ *  SMESH_Gen_i::CreateMeshFromSTL
+ *
+ *  Create mesh and import data from STL file
+ */
+//=============================================================================
+
+SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName )
+  throw ( SALOME::SALOME_Exception )
+{
+  Unexpect aCatch(SALOME_SalomeException);
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::CreateMeshesFromSTL" );
+
+  SMESH::SMESH_Mesh_var aMesh = createMesh();
+  string aFileName; // = boost::filesystem::path(theFileName).leaf();
+  // publish mesh in the study
+  if ( CanPublishInStudy( aMesh ) ) {
+    SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
+    aStudyBuilder->NewCommand();  // There is a transaction
+    SALOMEDS::SObject_var aSO = PublishInStudy
+      ( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
+    aStudyBuilder->CommitCommand();
+    if ( !aSO->_is_nil() ) {
+    // Update Python script
+      TCollection_AsciiString aStr (aSO->GetID());
+      aStr += " = smesh.CreateMeshesFromSTL(\"";
+      aStr += Standard_CString(theFileName);
+      aStr += "\")";
+
+      AddToCurrentPyScript(aStr);
+    }
+  }
+
+  SMESH_Mesh_i* aServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( aMesh ).in() );
+  ASSERT( aServant );
+  aServant->ImportSTLFile( theFileName );
+  return aMesh._retn();
+}
+
 //=============================================================================
 /*!
  *  SMESH_Gen_i::IsReadyToCompute
@@ -703,14 +693,14 @@ SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
 //=============================================================================
 
 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
-                                              GEOM::GEOM_Shape_ptr  theShape )
+                                              GEOM::GEOM_Object_ptr theShapeObject )
   throw ( SALOME::SALOME_Exception )
 {
   Unexpect aCatch(SALOME_SalomeException);
-  MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IsReadyToCompute" );
 
-  if ( CORBA::is_nil( theShape ) )
-    THROW_SALOME_CORBA_EXCEPTION( "bad shape reference", 
+  if ( CORBA::is_nil( theShapeObject ) )
+    THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", 
                                   SALOME::BAD_PARAM );
 
   if ( CORBA::is_nil( theMesh ) )
@@ -723,14 +713,14 @@ CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
     ASSERT( meshServant );
     if ( meshServant ) {
       // get local TopoDS_Shape
-      TopoDS_Shape myLocShape = GetShapeReader()->GetShape( GetGeomEngine(), theShape );
+      TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
       // call implementation
       ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
       return myGen.CheckAlgoState( myLocMesh, myLocShape );
     }
   }
   catch ( SALOME_Exception& S_ex ) {
-    MESSAGE( "catch exception "<< S_ex.what() );
+    INFOS( "catch exception "<< S_ex.what() );
   }
   return false;
 }
@@ -743,53 +733,52 @@ CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
  */
 //=============================================================================
 
-SMESH::long_array* SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Shape_ptr      theMainShape,
-                                               const SMESH::shape_array& theListOfSubShape )
+SMESH::long_array* SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Object_ptr theMainShapeObject,
+                                           const SMESH::object_array& theListOfSubShapeObject )
      throw ( SALOME::SALOME_Exception )
 {
   Unexpect aCatch(SALOME_SalomeException);
-  MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetSubShapesId" );
 
   SMESH::long_array_var shapesId = new SMESH::long_array;
   set<int> setId;
 
-  if ( CORBA::is_nil( theMainShape ) )
-    THROW_SALOME_CORBA_EXCEPTION( "bad shape reference",
+  if ( CORBA::is_nil( theMainShapeObject ) )
+    THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference",
                                   SALOME::BAD_PARAM );
 
   try
     {
-      if (! myShapeReader) myShapeReader = new GEOM_Client(GetContainerRef());
-      ASSERT(myShapeReader);
-      TopoDS_Shape myMainShape  = GetShapeReader()->GetShape(GetGeomEngine(),theMainShape);
+      TopoDS_Shape myMainShape = GeomObjectToShape(theMainShapeObject);
       TopTools_IndexedMapOfShape myIndexToShape;      
       TopExp::MapShapes(myMainShape,myIndexToShape);
 
-      for (int i=0; i<theListOfSubShape.length(); i++)
+      for ( int i = 0; i < theListOfSubShapeObject.length(); i++ )
        {
-         GEOM::GEOM_Shape_var aShape
-           = GEOM::GEOM_Shape::_narrow(theListOfSubShape[i]);
-         if (CORBA::is_nil(aShape))
-           THROW_SALOME_CORBA_EXCEPTION("bad shape reference", \
-                                        SALOME::BAD_PARAM);
-         TopoDS_Shape locShape  = GetShapeReader()->GetShape(GetGeomEngine(),aShape);
+         GEOM::GEOM_Object_var aShapeObject
+           = GEOM::GEOM_Object::_narrow(theListOfSubShapeObject[i]);
+         if ( CORBA::is_nil( aShapeObject ) )
+           THROW_SALOME_CORBA_EXCEPTION ("bad shape object reference", \
+                                       SALOME::BAD_PARAM );
+
+         TopoDS_Shape locShape  = GeomObjectToShape(aShapeObject);
          for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
            {
              const TopoDS_Face& F = TopoDS::Face(exp.Current());
              setId.insert(myIndexToShape.FindIndex(F));
-             SCRUTE(myIndexToShape.FindIndex(F));
+             if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(F));
            }
          for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
            {
              const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
              setId.insert(myIndexToShape.FindIndex(E));
-             SCRUTE(myIndexToShape.FindIndex(E));
+             if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(E));
            }
          for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
            {
              const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
              setId.insert(myIndexToShape.FindIndex(V));
-             SCRUTE(myIndexToShape.FindIndex(V));
+             if(MYDEBUG) SCRUTE(myIndexToShape.FindIndex(V));
            }
        }
       shapesId->length(setId.size());
@@ -797,9 +786,9 @@ SMESH::long_array* SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Shape_ptr      theMai
       int i=0;
       for (iind = setId.begin(); iind != setId.end(); iind++)
        {
-         SCRUTE((*iind));
+         if(MYDEBUG) SCRUTE((*iind));
          shapesId[i] = (*iind);
-         SCRUTE(shapesId[i]);
+         if(MYDEBUG) SCRUTE(shapesId[i]);
          i++;
        }
     }
@@ -820,37 +809,48 @@ SMESH::long_array* SMESH_Gen_i::GetSubShapesId( GEOM::GEOM_Shape_ptr      theMai
 //=============================================================================
 
 CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
-                                     GEOM::GEOM_Shape_ptr  theShape )
+                                     GEOM::GEOM_Object_ptr theShapeObject )
      throw ( SALOME::SALOME_Exception )
 {
   Unexpect aCatch(SALOME_SalomeException);
-  MESSAGE( "SMESH_Gen_i::Compute" );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
 
-  if ( CORBA::is_nil( theShape ) )
-    THROW_SALOME_CORBA_EXCEPTION( "bad shape reference", 
+  if ( CORBA::is_nil( theShapeObject ) )
+    THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", 
                                   SALOME::BAD_PARAM );
 
   if ( CORBA::is_nil( theMesh ) )
     THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference",
                                   SALOME::BAD_PARAM );
 
+  // Update Python script
+  TCollection_AsciiString aStr ("isDone = smesh.Compute(");
+  SMESH_Gen_i::AddObject(aStr, theMesh) += ", ";
+  SMESH_Gen_i::AddObject(aStr, theShapeObject) += ")";
+
+  AddToCurrentPyScript(aStr);
+
+  aStr = "if isDone == 0: print \"Mesh ";
+  SMESH_Gen_i::AddObject(aStr, theMesh) += " computation failed\"";
+  AddToCurrentPyScript(aStr);
+
   try {
     // get mesh servant
     SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
     ASSERT( meshServant );
     if ( meshServant ) {
       // get local TopoDS_Shape
-      TopoDS_Shape myLocShape = GetShapeReader()->GetShape( GetGeomEngine(), theShape );
-      // call implementarion compute
+      TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
+      // call implementation compute
       ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
       return myGen.Compute( myLocMesh, myLocShape);
     }
   }
   catch ( SALOME_Exception& S_ex ) {
-    MESSAGE( "Compute(): catch exception "<< S_ex.what() );
+    INFOS( "Compute(): catch exception "<< S_ex.what() );
   }
   catch ( ... ) {
-    MESSAGE( "Compute(): unknown exception " );
+    INFOS( "Compute(): unknown exception " );
   }
   return false;
 }
@@ -868,7 +868,16 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
 {
   INFOS( "SMESH_Gen_i::Save" );
 
-  ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
+//  ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
+  // san -- in case <myCurrentStudy> differs from theComponent's study,
+  // use that of the component
+  if ( myCurrentStudy->_is_nil() || 
+       theComponent->GetStudy()->StudyId() != myCurrentStudy->StudyId() )
+    SetCurrentStudy( theComponent->GetStudy() );
+
+  // Store study contents as a set of python commands
+  SavePython(myCurrentStudy);
+
   StudyContext* myStudyContext = GetCurrentStudyContext();
   
   // Declare a byte stream
@@ -876,7 +885,7 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
   
   // Obtain a temporary dir
   TCollection_AsciiString tmpDir =
-    ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : SALOMEDS_Tool::GetTmpDir();
+    ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
 
   // Create a sequence of files processed
   SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
@@ -884,7 +893,7 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
 
   TCollection_AsciiString aStudyName( "" );
   if ( isMultiFile ) 
-    aStudyName = ( SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ) );
+    aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
 
   // Set names of temporary files
   TCollection_AsciiString filename =
@@ -1059,13 +1068,15 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
            
            // write reference on a shape if exists
            SALOMEDS::SObject_var myRef;
+            bool shapeRefFound = false;
            bool found = gotBranch->FindSubObject( GetRefOnShapeTag(), myRef );
            if ( found ) {
              SALOMEDS::SObject_var myShape;
              bool ok = myRef->ReferencedObject( myShape );
              if ( ok ) {
+                shapeRefFound = (! CORBA::is_nil( myShape->GetObject() ));
                string myRefOnObject = myShape->GetID();
-               if ( myRefOnObject.length() > 0 ) {
+               if ( shapeRefFound && myRefOnObject.length() > 0 ) {
                  aSize[ 0 ] = myRefOnObject.length() + 1;
                  aDataset = new HDFdataset( "Ref on shape", aTopGroup, HDF_STRING, aSize, 1 );
                  aDataset->CreateOnDisk();
@@ -1078,7 +1089,10 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
            // write applied hypotheses if exist
            SALOMEDS::SObject_var myHypBranch;
            found = gotBranch->FindSubObject( GetRefOnAppliedHypothesisTag(), myHypBranch );
-           if ( found ) {
+           if ( found && !shapeRefFound ) { // remove applied hyps
+              myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
+            }
+           if ( found && shapeRefFound ) {
              aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
              aGroup->CreateOnDisk();
 
@@ -1117,7 +1131,10 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
            // write applied algorithms if exist
            SALOMEDS::SObject_var myAlgoBranch;
            found = gotBranch->FindSubObject( GetRefOnAppliedAlgorithmsTag(), myAlgoBranch );
-           if ( found ) {
+           if ( found && !shapeRefFound ) { // remove applied algos
+              myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
+            }
+           if ( found && shapeRefFound ) {
              aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
              aGroup->CreateOnDisk();
 
@@ -1154,17 +1171,60 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
            }
 
            // --> submesh objects sub-branches
+
            for ( int i = GetSubMeshOnVertexTag(); i <= GetSubMeshOnCompoundTag(); i++ ) {
              SALOMEDS::SObject_var mySubmeshBranch;
              found = gotBranch->FindSubObject( i, mySubmeshBranch );
+
+              if ( found ) // check if there is shape reference in submeshes
+              {
+                bool hasShapeRef = false;
+               SALOMEDS::ChildIterator_var itSM =
+                  myCurrentStudy->NewChildIterator( mySubmeshBranch );
+               for ( ; itSM->More(); itSM->Next() ) {
+                 SALOMEDS::SObject_var mySubRef, myShape, mySObject = itSM->Value();
+                  if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ))
+                    mySubRef->ReferencedObject( myShape );
+                  if ( !CORBA::is_nil( myShape ) && !CORBA::is_nil( myShape->GetObject() ))
+                    hasShapeRef = true;
+                  else
+                  { // remove one submesh
+                    if ( shapeRefFound )
+                    { // unassign hypothesis
+                      SMESH::SMESH_subMesh_var mySubMesh =
+                        SMESH::SMESH_subMesh::_narrow( SObjectToObject( mySObject ));
+                      if ( !mySubMesh->_is_nil() ) {
+                        int shapeID = mySubMesh->GetId();
+                        TopoDS_Shape S = mySMESHDSMesh->IndexToShape( shapeID );
+                        const list<const SMESHDS_Hypothesis*>& hypList =
+                          mySMESHDSMesh->GetHypothesis( S );
+                        list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
+                        while ( hyp != hypList.end() ) {
+                          int hypID = (*hyp++)->GetID(); // goto next hyp here because
+                          myLocMesh.RemoveHypothesis( S, hypID ); // hypList changes here
+                        }
+                      }
+                    }
+                    myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySObject );
+                  }
+                } // loop on submeshes of a type
+                if ( !shapeRefFound || !hasShapeRef ) { // remove the whole submeshes branch
+                  myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( mySubmeshBranch );
+                  found = false;
+                }
+              }  // end check if there is shape reference in submeshes
              if ( found ) {
                char name_meshgroup[ 30 ];
                if ( i == GetSubMeshOnVertexTag() )
                  strcpy( name_meshgroup, "SubMeshes On Vertex" );
                else if ( i == GetSubMeshOnEdgeTag() )
                  strcpy( name_meshgroup, "SubMeshes On Edge" );
+               else if ( i == GetSubMeshOnWireTag() )
+                 strcpy( name_meshgroup, "SubMeshes On Wire" );
                else if ( i == GetSubMeshOnFaceTag() )
                  strcpy( name_meshgroup, "SubMeshes On Face" );
+               else if ( i == GetSubMeshOnShellTag() )
+                 strcpy( name_meshgroup, "SubMeshes On Shell" );
                else if ( i == GetSubMeshOnSolidTag() )
                  strcpy( name_meshgroup, "SubMeshes On Solid" );
                else if ( i == GetSubMeshOnCompoundTag() )
@@ -1179,7 +1239,8 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
                for ( ; itSM->More(); itSM->Next() ) {
                  SALOMEDS::SObject_var mySObject = itSM->Value();
                  CORBA::Object_var anSubObject = SObjectToObject( mySObject );
-                 if ( !CORBA::is_nil( anSubObject ) ) {
+                 if ( !CORBA::is_nil( anSubObject ))
+                  {
                    SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow( anSubObject ) ;
                    int subid = myStudyContext->findId( string( GetORB()->object_to_string( anSubObject ) ) );
                      
@@ -1189,31 +1250,18 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
                    aSubGroup = new HDFgroup( submeshGrpName, aGroup );
                    aSubGroup->CreateOnDisk();
 
-//                 // Put submesh data to MED convertor
-//                 if ( myImpl->_mapSubMesh.find( mySubMesh->GetId() ) != myImpl->_mapSubMesh.end() ) {
-//                   MESSAGE( "VSR - SMESH_Gen_i::Save(): saving submesh with ID = "
-//                              << mySubMesh->GetId() << " to MED file" );
-//                   ::SMESH_subMesh* aLocalSubmesh = myImpl->_mapSubMesh[mySubMesh->GetId()];
-//                   myWriter.AddSubMesh( aLocalSubmesh->GetSubMeshDS(), subid );
-//                 }
-                   
-                   // write reference on a shape if exists
-                   SALOMEDS::SObject_var mySubRef;
-                   found = mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef );
-                   if ( found ) {
-                     SALOMEDS::SObject_var myShape;
-                     bool ok = mySubRef->ReferencedObject( myShape );
-                     if ( ok ) {
-                       string myRefOnObject = myShape->GetID();
-                       if ( myRefOnObject.length() > 0 ) {
-                         aSize[ 0 ] = myRefOnObject.length() + 1;
-                         aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
-                         aDataset->CreateOnDisk();
-                         aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
-                         aDataset->CloseOnDisk();
-                       }
-                     }
-                   }
+                   // write reference on a shape, already checked if it exists
+                    SALOMEDS::SObject_var mySubRef, myShape;
+                    if ( mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ))
+                      mySubRef->ReferencedObject( myShape );
+                    string myRefOnObject = myShape->GetID();
+                    if ( myRefOnObject.length() > 0 ) {
+                      aSize[ 0 ] = myRefOnObject.length() + 1;
+                      aDataset = new HDFdataset( "Ref on shape", aSubGroup, HDF_STRING, aSize, 1 );
+                      aDataset->CreateOnDisk();
+                      aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
+                      aDataset->CloseOnDisk();
+                    }
 
                    // write applied hypotheses if exist
                    SALOMEDS::SObject_var mySubHypBranch;
@@ -1291,7 +1339,8 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
              }
            }
             // All sub-meshes will be stored in MED file
-            myWriter.AddAllSubMeshes();
+            if ( shapeRefFound )
+              myWriter.AddAllSubMeshes();
 
            // groups root sub-branch
            SALOMEDS::SObject_var myGroupsBranch;
@@ -1312,12 +1361,12 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
                aGroup->CreateOnDisk();
 
                SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator( myGroupsBranch );
-               int grpNb = 0;
                for ( ; it->More(); it->Next() ) {
                  SALOMEDS::SObject_var mySObject = it->Value();
                  CORBA::Object_var aSubObject = SObjectToObject( mySObject );
                  if ( !CORBA::is_nil( aSubObject ) ) {
-                   SMESH_Group_i* myGroupImpl = dynamic_cast<SMESH_Group_i*>( GetServant( aSubObject ).in() );
+                   SMESH_GroupBase_i* myGroupImpl =
+                      dynamic_cast<SMESH_GroupBase_i*>( GetServant( aSubObject ).in() );
                    if ( !myGroupImpl )
                      continue;
 
@@ -1337,23 +1386,197 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
 
                    // Store the group contents into MED file
                    if ( myLocMesh.GetGroup( myGroupImpl->GetLocalID() ) ) {
-                     MESSAGE( "VSR - SMESH_Gen_i::Save(): saving group with StoreName = "
+
+                     if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen_i::Save(): saving group with StoreName = "
                               << grpName << " to MED file" );
-                     SMESHDS_Group* aGrpDS = myLocMesh.GetGroup( myGroupImpl->GetLocalID() )->GetGroupDS();
-                     aGrpDS->SetStoreName( grpName );
+                     SMESHDS_GroupBase* aGrpBaseDS =
+                        myLocMesh.GetGroup( myGroupImpl->GetLocalID() )->GetGroupDS();
+                     aGrpBaseDS->SetStoreName( grpName );
 
                      // Pass SMESHDS_Group to MED writer 
-                     myWriter.AddGroup( aGrpDS );
+                     SMESHDS_Group* aGrpDS = dynamic_cast<SMESHDS_Group*>( aGrpBaseDS );
+                      if ( aGrpDS )
+                        myWriter.AddGroup( aGrpDS );
+
+                      // write reference on a shape if exists
+                      SMESHDS_GroupOnGeom* aGeomGrp =
+                        dynamic_cast<SMESHDS_GroupOnGeom*>( aGrpBaseDS );
+                      if ( aGeomGrp ) {
+                        SALOMEDS::SObject_var mySubRef, myShape;
+                        if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ) &&
+                            mySubRef->ReferencedObject( myShape ) &&
+                            !CORBA::is_nil( myShape->GetObject() ))
+                        {
+                          string myRefOnObject = myShape->GetID();
+                          if ( myRefOnObject.length() > 0 ) {
+                            char aRefName[ 30 ];
+                            sprintf( aRefName, "Ref on shape %d", anId);
+                            aSize[ 0 ] = myRefOnObject.length() + 1;
+                            aDataset = new HDFdataset(aRefName, aGroup, HDF_STRING, aSize, 1);
+                            aDataset->CreateOnDisk();
+                            aDataset->WriteOnDisk( ( char* )( myRefOnObject.c_str() ) );
+                            aDataset->CloseOnDisk();
+                          }
+                        }
+                        else // shape ref is invalid:
+                        {
+                          // save a group on geometry as ordinary group
+                          myWriter.AddGroup( aGeomGrp );
+                        }
+                      }
                    }
                  }
                }
                aGroup->CloseOnDisk();
              }
-           }
-
-           // Flush current mesh information into MED file
-           if ( strcmp( strHasData.c_str(), "1" ) == 0 )             
-             myWriter.Add();
+           } // loop on groups 
+
+           if ( strcmp( strHasData.c_str(), "1" ) == 0 )
+            {
+              // Flush current mesh information into MED file
+             myWriter.Perform();
+
+              // maybe a shape was deleted in the study
+              if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() ) {
+                TopoDS_Shape nullShape;
+                myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
+              }
+
+              // Store node positions on sub-shapes (SMDS_Position):
+
+              if ( !mySMESHDSMesh->SubMeshes().empty() )
+              {
+                aGroup = new HDFgroup( "Node Positions", aTopGroup );
+                aGroup->CreateOnDisk();
+
+                // in aGroup, create 5 datasets to contain:
+                // "Nodes on Edges" - ID of node on edge
+                // "Edge positions" - U parameter on node on edge
+                // "Nodes on Faces" - ID of node on face
+                // "Face U positions" - U parameter of node on face
+                // "Face V positions" - V parameter of node on face
+
+                // Find out nb of nodes on edges and faces
+                // Collect corresponing sub-meshes
+                int nbEdgeNodes = 0, nbFaceNodes = 0;
+                list<SMESHDS_SubMesh*> aEdgeSM, aFaceSM;
+                // loop on SMESHDS_SubMesh'es
+                const map<int,SMESHDS_SubMesh*>& aSubMeshes = mySMESHDSMesh->SubMeshes();
+                map<int,SMESHDS_SubMesh*>::const_iterator itSubM ( aSubMeshes.begin() );
+                for ( ; itSubM != aSubMeshes.end() ; itSubM++ )
+                {
+                  SMESHDS_SubMesh* aSubMesh = (*itSubM).second;
+                  if ( aSubMesh->IsComplexSubmesh() )
+                    continue; // submesh containing other submeshs
+                  int nbNodes = aSubMesh->NbNodes();
+                  if ( nbNodes == 0 ) continue;
+
+                  int aShapeID = (*itSubM).first;
+                  int aShapeType = mySMESHDSMesh->IndexToShape( aShapeID ).ShapeType();
+                  // write only SMDS_FacePosition and SMDS_EdgePosition
+                  switch ( aShapeType ) {
+                  case TopAbs_FACE:
+                    nbFaceNodes += nbNodes;
+                    aFaceSM.push_back( aSubMesh );
+                    break;
+                  case TopAbs_EDGE:
+                    nbEdgeNodes += nbNodes;
+                    aEdgeSM.push_back( aSubMesh );
+                    break;
+                  default:
+                    continue;
+                  }
+                }
+                // Treat positions on edges or faces
+                for ( int onFace = 0; onFace < 2; onFace++ )
+                {
+                  // Create arrays to store in datasets
+                  int iNode = 0, nbNodes = ( onFace ? nbFaceNodes : nbEdgeNodes );
+                  if (!nbNodes) continue;
+                  int* aNodeIDs = new int [ nbNodes ];
+                  double* aUPos = new double [ nbNodes ];
+                  double* aVPos = ( onFace ? new double[ nbNodes ] : 0 );
+
+                  // Fill arrays
+                  // loop on sub-meshes
+                  list<SMESHDS_SubMesh*> * pListSM = ( onFace ? &aFaceSM : &aEdgeSM );
+                  list<SMESHDS_SubMesh*>::iterator itSM = pListSM->begin();
+                  for ( ; itSM != pListSM->end(); itSM++ )
+                  {
+                    SMESHDS_SubMesh* aSubMesh = (*itSM);
+                    if ( aSubMesh->IsComplexSubmesh() )
+                      continue; // submesh containing other submeshs
+
+                    SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
+                    // loop on nodes in aSubMesh
+                    while ( itNode->more() )
+                    {
+                      //node ID
+                      const SMDS_MeshNode* node = itNode->next();
+                      aNodeIDs [ iNode ] = node->GetID();
+
+                      // Position
+                      const SMDS_PositionPtr pos = node->GetPosition();
+                      if ( onFace ) { // on FACE
+                        const SMDS_FacePosition* fPos =
+                          dynamic_cast<const SMDS_FacePosition*>( pos.get() );
+                        if ( fPos ) {
+                          aUPos[ iNode ] = fPos->GetUParameter();
+                          aVPos[ iNode ] = fPos->GetVParameter();
+                          iNode++;
+                        }
+                        else
+                          nbNodes--;
+                      }
+                      else { // on EDGE
+                        const SMDS_EdgePosition* ePos =
+                          dynamic_cast<const SMDS_EdgePosition*>( pos.get() );
+                        if ( ePos ) {
+                          aUPos[ iNode ] = ePos->GetUParameter();
+                          iNode++;
+                        }
+                        else
+                          nbNodes--;
+                      }
+                    } // loop on nodes in aSubMesh
+                  } // loop on sub-meshes
+
+                  // Write datasets
+                  if ( nbNodes )
+                  {
+                    aSize[ 0 ] = nbNodes;
+                    // IDS
+                    string aDSName( onFace ? "Nodes on Faces" : "Nodes on Edges");
+                    aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_INT32, aSize, 1 );
+                    aDataset->CreateOnDisk();
+                    aDataset->WriteOnDisk( aNodeIDs );
+                    aDataset->CloseOnDisk();
+
+                    // U Positions
+                    aDSName = ( onFace ? "Face U positions" : "Edge positions");
+                    aDataset = new HDFdataset( (char*)aDSName.c_str(), aGroup, HDF_FLOAT64, aSize, 1);
+                    aDataset->CreateOnDisk();
+                    aDataset->WriteOnDisk( aUPos );
+                    aDataset->CloseOnDisk();
+                    // V Positions
+                    if ( onFace ) {
+                      aDataset = new HDFdataset( "Face V positions", aGroup, HDF_FLOAT64, aSize, 1);
+                      aDataset->CreateOnDisk();
+                      aDataset->WriteOnDisk( aVPos );
+                      aDataset->CloseOnDisk();
+                    }
+                  }
+                  delete [] aNodeIDs;
+                  delete [] aUPos;
+                  if ( aVPos ) delete [] aVPos;
+
+                } // treat positions on edges or faces
+
+                // close "Node Positions" group
+                aGroup->CloseOnDisk(); 
+
+              } // if ( there are submeshes in SMESHDS_Mesh )
+            } // if ( hasData )
 
            // close mesh HDF group
            aTopGroup->CloseOnDisk();
@@ -1389,7 +1612,7 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent,
                                           const char*              theURL,
                                           bool                     isMultiFile ) {
-  MESSAGE( "SMESH_Gen_i::SaveASCII" );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
   SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
   return aStreamFile._retn();
 }
@@ -1430,12 +1653,15 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
 {
   INFOS( "SMESH_Gen_i::Load" );
 
-  ASSERT( theComponent->GetStudy()->StudyId() == myCurrentStudy->StudyId() )
+  if ( myCurrentStudy->_is_nil() || 
+       theComponent->GetStudy()->StudyId() != myCurrentStudy->StudyId() )
+    SetCurrentStudy( theComponent->GetStudy() );
+
   StudyContext* myStudyContext = GetCurrentStudyContext();
   
   // Get temporary files location
   TCollection_AsciiString tmpDir =
-    isMultiFile ? TCollection_AsciiString( ( char* )theURL ) : SALOMEDS_Tool::GetTmpDir();
+    isMultiFile ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
 
   // Convert the stream into sequence of files to process
   SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
@@ -1443,7 +1669,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                                                                            isMultiFile );
   TCollection_AsciiString aStudyName( "" );
   if ( isMultiFile ) 
-    aStudyName = ( SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ) );
+    aStudyName = ( (char*)SALOMEDS_Tool::GetNameFromPath( myCurrentStudy->URL() ).c_str() );
 
   // Set names of temporary files
   TCollection_AsciiString filename = tmpDir + aStudyName + TCollection_AsciiString( "_SMESH.hdf" );
@@ -1464,7 +1690,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
     aFile->OpenOnDisk( HDF_RDONLY );
   }
   catch ( HDFexception ) {
-    MESSAGE( "Load(): " << filename << " not found!" );
+    INFOS( "Load(): " << filename << " not found!" );
     return false;
   }
 
@@ -1512,7 +1738,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              char* hypname_str = new char[ size ];
              aDataset->ReadFromDisk( hypname_str );
              hypname = string( hypname_str );
-             delete hypname_str;
+             delete [] hypname_str;
              aDataset->CloseOnDisk();
            }
            // --> get hypothesis plugin library name
@@ -1522,9 +1748,9 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              size = aDataset->GetSize();
              char* libname_str = new char[ size ];
              aDataset->ReadFromDisk( libname_str );
-             SCRUTE( libname_str );
+             if(MYDEBUG) SCRUTE( libname_str );
              libname = string( libname_str );
-             delete libname_str;
+             delete [] libname_str;
              aDataset->CloseOnDisk();
            }
            // --> get hypothesis data
@@ -1535,7 +1761,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              char* hypdata_str = new char[ size ];
              aDataset->ReadFromDisk( hypdata_str );
              hypdata = string( hypdata_str );
-             delete hypdata_str;
+             delete [] hypdata_str;
              aDataset->CloseOnDisk();
            }
          }
@@ -1544,7 +1770,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
 
          // --> restore hypothesis from data
          if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
-           MESSAGE("VSR - load hypothesis : id = " << id <<
+           if(MYDEBUG) MESSAGE("VSR - load hypothesis : id = " << id <<
                     ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
             SMESH::SMESH_Hypothesis_var myHyp;
            
@@ -1552,7 +1778,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
            }
            catch (...) {
-             MESSAGE( "Exception during hypothesis creation" );
+             INFOS( "Exception during hypothesis creation" );
            }
 
            SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
@@ -1563,7 +1789,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              myStudyContext->mapOldToNew( id, newId );
            }
            else
-             MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
+             if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
           }
         }
       }
@@ -1609,7 +1835,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              char* hypname_str = new char[ size ];
              aDataset->ReadFromDisk( hypname_str );
              hypname = string( hypname_str );
-             delete hypname_str;
+             delete [] hypname_str;
              aDataset->CloseOnDisk();
            }
            // --> get algorithm plugin library name
@@ -1619,9 +1845,9 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              size = aDataset->GetSize();
              char* libname_str = new char[ size ];
              aDataset->ReadFromDisk( libname_str );
-             SCRUTE( libname_str );
+             if(MYDEBUG) SCRUTE( libname_str );
              libname = string( libname_str );
-             delete libname_str;
+             delete [] libname_str;
              aDataset->CloseOnDisk();
            }
            // --> get algorithm data
@@ -1631,9 +1857,9 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              size = aDataset->GetSize();
              char* hypdata_str = new char[ size ];
              aDataset->ReadFromDisk( hypdata_str );
-             SCRUTE( hypdata_str );
+             if(MYDEBUG) SCRUTE( hypdata_str );
              hypdata = string( hypdata_str );
-             delete hypdata_str;
+             delete [] hypdata_str;
              aDataset->CloseOnDisk();
            }
          }
@@ -1642,7 +1868,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
          
          // --> restore algorithm from data
          if ( id > 0 && !hypname.empty()/* && !hypdata.empty()*/ ) { // VSR : persistent data can be empty
-           MESSAGE("VSR - load algo : id = " << id <<
+           if(MYDEBUG) MESSAGE("VSR - load algo : id = " << id <<
                     ", name = " << hypname.c_str() << ", persistent string = " << hypdata.c_str());
             SMESH::SMESH_Hypothesis_var myHyp;
                    
@@ -1650,7 +1876,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              myHyp = this->createHypothesis( hypname.c_str(), libname.c_str() );
            }
            catch (...) {
-             MESSAGE( "Exception during hypothesis creation" );
+             INFOS( "Exception during hypothesis creation" );
            }
            
            SMESH_Hypothesis_i* myImpl = dynamic_cast<SMESH_Hypothesis_i*>( GetServant( myHyp ).in() );
@@ -1661,7 +1887,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              myStudyContext->mapOldToNew( id, newId );
            }
            else
-             MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
+             if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen::Load - can't get servant" );
           }
         }
       }
@@ -1691,10 +1917,10 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
        int aNbObjects = aTopGroup->nInternalObjects(); 
        if ( aNbObjects > 0 ) {
          // create mesh
-         MESSAGE( "VSR - load mesh : id = " << id );
+         if(MYDEBUG) MESSAGE( "VSR - load mesh : id = " << id );
          SMESH::SMESH_Mesh_var myNewMesh = this->createMesh();
          SMESH_Mesh_i* myNewMeshImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( myNewMesh ).in() );
-         if ( !myNewMeshImpl )
+          if ( !myNewMeshImpl )
            continue;
          string iorString = GetORB()->object_to_string( myNewMesh );
          int newId = myStudyContext->findId( iorString );
@@ -1716,13 +1942,13 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              // read mesh data from MED file
              myReader.SetMesh( mySMESHDSMesh );
              myReader.SetMeshId( id );
-             myReader.ReadMySelf();
+             myReader.Perform();
              hasData = true;
            }
          }
 
          // try to read and set reference to shape
-         GEOM::GEOM_Shape_var aShape;
+         GEOM::GEOM_Object_var aShapeObject;
          if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
            // load mesh "Ref on shape" - it's an entry to SObject
            aDataset = new HDFdataset( "Ref on shape", aTopGroup );
@@ -1739,25 +1965,25 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
 
              CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
              if ( !CORBA::is_nil( shapeObject ) ) {
-               aShape = GEOM::GEOM_Shape::_narrow( shapeObject );
-               if ( !aShape->_is_nil() )
-                 myNewMeshImpl->setShape( aShape );
+               aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
+               if ( !aShapeObject->_is_nil() )
+                 myNewMeshImpl->SetShape( aShapeObject );
              }
            }
          }
 
-         // try to get applied hypotheses
-         if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
-           aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
+         // try to get applied algorithms
+         if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
+           aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
            aGroup->OpenOnDisk();
-           // get number of applied hypotheses
+           // get number of applied algorithms
            int aNbSubObjects = aGroup->nInternalObjects(); 
-           MESSAGE( "VSR - number of applied hypotheses = " << aNbSubObjects );
+           if(MYDEBUG) MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
            for ( int j = 0; j < aNbSubObjects; j++ ) {
              char name_dataset[ HDF_NAME_MAX_LEN+1 ];
              aGroup->InternalObjectIndentify( j, name_dataset );
-             // check if it is a hypothesis
-             if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
+             // check if it is an algorithm
+             if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
                aDataset = new HDFdataset( name_dataset, aGroup );
                aDataset->OpenOnDisk();
                size = aDataset->GetSize();
@@ -1765,7 +1991,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                aDataset->ReadFromDisk( refFromFile );
                aDataset->CloseOnDisk();
 
-               // san - it is impossible to recover applied hypotheses using their entries within Load() method
+               // san - it is impossible to recover applied algorithms using their entries within Load() method
                
                //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
                //CORBA::Object_var hypObject = SObjectToObject( hypSO );
@@ -1775,27 +2001,26 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                  CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
                  if ( !CORBA::is_nil( hypObject ) ) {
                    SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
-                   if ( !anHyp->_is_nil() && !aShape->_is_nil() )
-                     myNewMeshImpl->addHypothesis( aShape, anHyp );
+                   if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
+                     myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
                  }
                }
              }
            }
            aGroup->CloseOnDisk();
          }
-       
-         // try to get applied algorithms
-         if ( aTopGroup->ExistInternalObject( "Applied Algorithms" ) ) {
-           aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
+
+         // try to get applied hypotheses
+         if ( aTopGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
+           aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
            aGroup->OpenOnDisk();
-           // get number of applied algorithms
+           // get number of applied hypotheses
            int aNbSubObjects = aGroup->nInternalObjects(); 
-           MESSAGE( "VSR - number of applied algos " << aNbSubObjects );
            for ( int j = 0; j < aNbSubObjects; j++ ) {
              char name_dataset[ HDF_NAME_MAX_LEN+1 ];
              aGroup->InternalObjectIndentify( j, name_dataset );
-             // check if it is an algorithm
-             if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
+             // check if it is a hypothesis
+             if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
                aDataset = new HDFdataset( name_dataset, aGroup );
                aDataset->OpenOnDisk();
                size = aDataset->GetSize();
@@ -1803,7 +2028,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                aDataset->ReadFromDisk( refFromFile );
                aDataset->CloseOnDisk();
 
-               // san - it is impossible to recover applied algorithms using their entries within Load() method
+               // san - it is impossible to recover applied hypotheses using their entries within Load() method
                
                //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
                //CORBA::Object_var hypObject = SObjectToObject( hypSO );
@@ -1813,8 +2038,8 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                  CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
                  if ( !CORBA::is_nil( hypObject ) ) {
                    SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
-                   if ( !anHyp->_is_nil() && !aShape->_is_nil() )
-                     myNewMeshImpl->addHypothesis( aShape, anHyp );
+                   if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
+                     myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
                  }
                }
              }
@@ -1829,8 +2054,12 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              strcpy( name_meshgroup, "SubMeshes On Vertex" );
            else if ( j == GetSubMeshOnEdgeTag() )
              strcpy( name_meshgroup, "SubMeshes On Edge" );
+           else if ( j == GetSubMeshOnWireTag() )
+             strcpy( name_meshgroup, "SubMeshes On Wire" );
            else if ( j == GetSubMeshOnFaceTag() )
              strcpy( name_meshgroup, "SubMeshes On Face" );
+           else if ( j == GetSubMeshOnShellTag() )
+             strcpy( name_meshgroup, "SubMeshes On Shell" );
            else if ( j == GetSubMeshOnSolidTag() )
              strcpy( name_meshgroup, "SubMeshes On Solid" );
            else if ( j == GetSubMeshOnCompoundTag() )
@@ -1858,9 +2087,9 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                  aSubGroup->OpenOnDisk();
                  
                  // try to read and set reference to subshape
-                 GEOM::GEOM_Shape_var     aSubShape;
+                 GEOM::GEOM_Object_var aSubShapeObject;
                  SMESH::SMESH_subMesh_var aSubMesh;
-                   
+
                  if ( aSubGroup->ExistInternalObject( "Ref on shape" ) ) {
                    // load submesh "Ref on shape" - it's an entry to SObject
                    aDataset = new HDFdataset( "Ref on shape", aSubGroup );
@@ -1873,10 +2102,10 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                      SALOMEDS::SObject_var subShapeSO = myCurrentStudy->FindObjectID( refFromFile );
                      CORBA::Object_var subShapeObject = SObjectToObject( subShapeSO );
                      if ( !CORBA::is_nil( subShapeObject ) ) {
-                       aSubShape = GEOM::GEOM_Shape::_narrow( subShapeObject );
-                       if ( !aSubShape->_is_nil() )
+                       aSubShapeObject = GEOM::GEOM_Object::_narrow( subShapeObject );
+                       if ( !aSubShapeObject->_is_nil() )
                          aSubMesh = SMESH::SMESH_subMesh::_duplicate
-                            ( myNewMeshImpl->createSubMesh( aSubShape ) );
+                            ( myNewMeshImpl->createSubMesh( aSubShapeObject ) );
                        if ( aSubMesh->_is_nil() )
                          continue;
                        string iorSubString = GetORB()->object_to_string( aSubMesh );
@@ -1892,37 +2121,37 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                  // VSR: Get submesh data from MED convertor
 //               int anInternalSubmeshId = aSubMesh->GetId(); // this is not a persistent ID, it's an internal one computed from sub-shape
 //               if (myNewMeshImpl->_mapSubMesh.find(anInternalSubmeshId) != myNewMeshImpl->_mapSubMesh.end()) {
-//                 MESSAGE("VSR - SMESH_Gen_i::Load(): loading from MED file submesh with ID = " <<
+//                 if(MYDEBUG) MESSAGE("VSR - SMESH_Gen_i::Load(): loading from MED file submesh with ID = " <<
 //                            subid << " for subshape # " << anInternalSubmeshId);
 //                 SMESHDS_SubMesh* aSubMeshDS =
 //                      myNewMeshImpl->_mapSubMesh[anInternalSubmeshId]->CreateSubMeshDS();
 //                 if ( !aSubMeshDS ) {
-//                   MESSAGE("VSR - SMESH_Gen_i::Load(): FAILED to create a submesh for subshape # " <<
+//                   if(MYDEBUG) MESSAGE("VSR - SMESH_Gen_i::Load(): FAILED to create a submesh for subshape # " <<
 //                              anInternalSubmeshId << " in current mesh!");
 //                 }
 //                 else
 //                   myReader.GetSubMesh( aSubMeshDS, subid );
 //               }
                    
-                 // try to get applied hypotheses
-                 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
-                   // open "applied hypotheses" HDF group
-                   aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
+                 // try to get applied algorithms
+                 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
+                   // open "applied algorithms" HDF group
+                   aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
                    aSubSubGroup->OpenOnDisk();
-                   // get number of applied hypotheses
+                   // get number of applied algorithms
                    int aNbSubObjects = aSubSubGroup->nInternalObjects(); 
                    for ( int l = 0; l < aNbSubObjects; l++ ) {
                      char name_dataset[ HDF_NAME_MAX_LEN+1 ];
                      aSubSubGroup->InternalObjectIndentify( l, name_dataset );
-                     // check if it is a hypothesis
-                     if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
+                     // check if it is an algorithm
+                     if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
                        aDataset = new HDFdataset( name_dataset, aSubSubGroup );
                        aDataset->OpenOnDisk();
                        size = aDataset->GetSize();
                        char* refFromFile = new char[ size ];
                        aDataset->ReadFromDisk( refFromFile );
                        aDataset->CloseOnDisk();
-                       
+
                        //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
                        //CORBA::Object_var hypObject = SObjectToObject( hypSO );
                        int id = atoi( refFromFile );
@@ -1931,35 +2160,35 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                          CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
                          if ( !CORBA::is_nil( hypObject ) ) {
                            SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
-                           if ( !anHyp->_is_nil() && !aShape->_is_nil() )
-                             myNewMeshImpl->addHypothesis( aSubShape, anHyp );
+                           if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
+                             myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
                          }
                        }
                      }
                    }
-                   // close "applied hypotheses" HDF group
+                   // close "applied algorithms" HDF group
                    aSubSubGroup->CloseOnDisk();
                  }
-       
-                 // try to get applied algorithms
-                 if ( aSubGroup->ExistInternalObject( "Applied Algorithms" ) ) {
-                   // open "applied algorithms" HDF group
-                   aSubSubGroup = new HDFgroup( "Applied Algorithms", aSubGroup );
+                 
+                 // try to get applied hypotheses
+                 if ( aSubGroup->ExistInternalObject( "Applied Hypotheses" ) ) {
+                   // open "applied hypotheses" HDF group
+                   aSubSubGroup = new HDFgroup( "Applied Hypotheses", aSubGroup );
                    aSubSubGroup->OpenOnDisk();
-                   // get number of applied algorithms
+                   // get number of applied hypotheses
                    int aNbSubObjects = aSubSubGroup->nInternalObjects(); 
                    for ( int l = 0; l < aNbSubObjects; l++ ) {
                      char name_dataset[ HDF_NAME_MAX_LEN+1 ];
                      aSubSubGroup->InternalObjectIndentify( l, name_dataset );
-                     // check if it is an algorithm
-                     if ( string( name_dataset ).substr( 0, 4 ) == string( "Algo" ) ) {
+                     // check if it is a hypothesis
+                     if ( string( name_dataset ).substr( 0, 3 ) == string( "Hyp" ) ) {
                        aDataset = new HDFdataset( name_dataset, aSubSubGroup );
                        aDataset->OpenOnDisk();
                        size = aDataset->GetSize();
                        char* refFromFile = new char[ size ];
                        aDataset->ReadFromDisk( refFromFile );
                        aDataset->CloseOnDisk();
-
+                       
                        //SALOMEDS::SObject_var hypSO = myCurrentStudy->FindObjectID( refFromFile );
                        //CORBA::Object_var hypObject = SObjectToObject( hypSO );
                        int id = atoi( refFromFile );
@@ -1968,16 +2197,16 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                          CORBA::Object_var hypObject = GetORB()->string_to_object( anIOR.c_str() );
                          if ( !CORBA::is_nil( hypObject ) ) {
                            SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow( hypObject );
-                           if ( !anHyp->_is_nil() && !aShape->_is_nil() )
-                             myNewMeshImpl->addHypothesis( aSubShape, anHyp );
+                           if ( !anHyp->_is_nil() && !aShapeObject->_is_nil() )
+                             myNewMeshImpl->addHypothesis( aSubShapeObject, anHyp );
                          }
                        }
                      }
                    }
-                   // close "applied algorithms" HDF group
+                   // close "applied hypotheses" HDF group
                    aSubSubGroup->CloseOnDisk();
                  }
-                 
+
                  // close submesh HDF group
                  aSubGroup->CloseOnDisk();
                }
@@ -1987,24 +2216,129 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
            }
          }
 
-         if(hasData){
+         if(hasData) {
            // Read sub-meshes from MED
-           MESSAGE("JFA - Create all sub-meshes");
+           if(MYDEBUG) MESSAGE("Create all sub-meshes");
            myReader.CreateAllSubMeshes();
-         }
+
+
+            // Read node positions on sub-shapes (SMDS_Position)
+
+            if ( aTopGroup->ExistInternalObject( "Node Positions" ))
+            {
+              // There are 5 datasets to read:
+              // "Nodes on Edges" - ID of node on edge
+              // "Edge positions" - U parameter on node on edge
+              // "Nodes on Faces" - ID of node on face
+              // "Face U positions" - U parameter of node on face
+              // "Face V positions" - V parameter of node on face
+              char* aEid_DSName = "Nodes on Edges";
+              char* aEu_DSName  = "Edge positions";
+              char* aFu_DSName  = "Face U positions";
+              //char* aFid_DSName = "Nodes on Faces";
+              //char* aFv_DSName  = "Face V positions";
+
+              // data to retrieve
+              int nbEids = 0, nbFids = 0;
+              int *aEids = 0, *aFids  = 0;
+              double *aEpos = 0, *aFupos = 0, *aFvpos = 0;
+
+              // open a group
+              aGroup = new HDFgroup( "Node Positions", aTopGroup ); 
+              aGroup->OpenOnDisk();
+
+              // loop on 5 data sets
+              int aNbObjects = aGroup->nInternalObjects();
+              for ( int i = 0; i < aNbObjects; i++ )
+              {
+                // identify dataset
+                char aDSName[ HDF_NAME_MAX_LEN+1 ];
+                aGroup->InternalObjectIndentify( i, aDSName );
+                // read data
+                aDataset = new HDFdataset( aDSName, aGroup );
+                aDataset->OpenOnDisk();
+                if ( aDataset->GetType() == HDF_FLOAT64 ) // Positions
+                {
+                  double* pos = new double [ aDataset->GetSize() ];
+                  aDataset->ReadFromDisk( pos );
+                  // which one?
+                  if ( strncmp( aDSName, aEu_DSName, strlen( aEu_DSName )) == 0 )
+                    aEpos = pos;
+                  else if ( strncmp( aDSName, aFu_DSName, strlen( aFu_DSName )) == 0 )
+                    aFupos = pos;
+                  else
+                    aFvpos = pos;
+                }
+                else // NODE IDS
+                {
+                 int aSize = aDataset->GetSize();
+
+                  // for reading files, created from 18.07.2005 till 10.10.2005
+                  if (aDataset->GetType() == HDF_STRING)
+                    aSize /= sizeof(int);
+
+                 int* ids = new int [aSize];
+                  aDataset->ReadFromDisk( ids );
+                  // on face or nodes?
+                  if ( strncmp( aDSName, aEid_DSName, strlen( aEid_DSName )) == 0 ) {
+                    aEids = ids;
+                    nbEids = aSize;
+                  }
+                  else {
+                    aFids = ids;
+                    nbFids = aSize;
+                  }
+                }
+              } // loop on 5 datasets
+
+              // Set node positions on edges or faces
+              for ( int onFace = 0; onFace < 2; onFace++ )
+              {
+                int nbNodes = ( onFace ? nbFids : nbEids );
+                if ( nbNodes == 0 ) continue;
+                int* aNodeIDs = ( onFace ? aFids : aEids );
+                double* aUPos = ( onFace ? aFupos : aEpos );
+                double* aVPos = ( onFace ? aFvpos : 0 );
+                // loop on node IDs
+                for ( int iNode = 0; iNode < nbNodes; iNode++ )
+                {
+                  const SMDS_MeshNode* node = mySMESHDSMesh->FindNode( aNodeIDs[ iNode ]);
+                  ASSERT( node );
+                  SMDS_PositionPtr aPos = node->GetPosition();
+                  ASSERT( aPos )
+                  if ( onFace ) {
+                    ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_FACE );
+                    SMDS_FacePosition* fPos = const_cast<SMDS_FacePosition*>
+                      ( static_cast<const SMDS_FacePosition*>( aPos.get() ));
+                    fPos->SetUParameter( aUPos[ iNode ]);
+                    fPos->SetVParameter( aVPos[ iNode ]);
+                  }
+                  else {
+                    ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE );
+                    SMDS_EdgePosition* fPos = const_cast<SMDS_EdgePosition*>
+                      ( static_cast<const SMDS_EdgePosition*>( aPos.get() ));
+                    fPos->SetUParameter( aUPos[ iNode ]);
+                  }
+                }
+              }
+              if ( aEids ) delete [] aEids;
+              if ( aFids ) delete [] aFids;
+              if ( aEpos ) delete [] aEpos;
+              if ( aFupos ) delete [] aFupos;
+              if ( aFvpos ) delete [] aFvpos;
+              
+              aGroup->CloseOnDisk();
+
+            } // if ( aTopGroup->ExistInternalObject( "Node Positions" ) )
+         } // if ( hasData )
 
           // Recompute State (as computed sub-meshes are restored from MED)
-         if ( !aShape->_is_nil() ) {
-           MESSAGE("JFA - Compute State Engine ...");
-//          map<int, ::SMESH_subMesh*>::iterator anAllSubMeshes = myNewMeshImpl->_mapSubMesh.begin();
-//          for (; anAllSubMeshes != myNewMeshImpl->_mapSubMesh.end(); anAllSubMeshes++)
-//          {
-//            (*anAllSubMeshes).second->GetSubMeshDS(); // init SMESH_subMesh::_meshDS
-//            (*anAllSubMeshes).second->ComputeStateEngine(SMESH_subMesh::SUBMESH_RESTORED);
-//          }
-           TopoDS_Shape myLocShape = GetShapeReader()->GetShape( GetGeomEngine(), aShape );
-           myNewMeshImpl->GetImpl().GetSubMesh(myLocShape)->ComputeStateEngine(SMESH_subMesh::SUBMESH_RESTORED);
-           MESSAGE("JFA - Compute State Engine finished");
+         if ( !aShapeObject->_is_nil() ) {
+           MESSAGE("Compute State Engine ...");
+           TopoDS_Shape myLocShape = GeomObjectToShape( aShapeObject );
+           myNewMeshImpl->GetImpl().GetSubMesh(myLocShape)->ComputeStateEngine
+              (SMESH_subMesh::SUBMESH_RESTORED);
+           MESSAGE("Compute State Engine finished");
          }
 
          // try to get groups
@@ -2041,11 +2375,33 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                  char* nameFromFile = new char[ size ];
                  aDataset->ReadFromDisk( nameFromFile );
                  aDataset->CloseOnDisk();
-                 
+
+                 // Try to find a shape reference
+                  TopoDS_Shape aShape;
+                  char aRefName[ 30 ];
+                  sprintf( aRefName, "Ref on shape %d", subid);
+                  if ( aGroup->ExistInternalObject( aRefName ) ) {
+                    // load mesh "Ref on shape" - it's an entry to SObject
+                    aDataset = new HDFdataset( aRefName, aGroup );
+                    aDataset->OpenOnDisk();
+                    size = aDataset->GetSize();
+                    char* refFromFile = new char[ size ];
+                    aDataset->ReadFromDisk( refFromFile );
+                    aDataset->CloseOnDisk();
+                    if ( strlen( refFromFile ) > 0 ) {
+                      SALOMEDS::SObject_var shapeSO = myCurrentStudy->FindObjectID( refFromFile );
+                      CORBA::Object_var shapeObject = SObjectToObject( shapeSO );
+                      if ( !CORBA::is_nil( shapeObject ) ) {
+                        aShapeObject = GEOM::GEOM_Object::_narrow( shapeObject );
+                        if ( !aShapeObject->_is_nil() )
+                          aShape = GeomObjectToShape( aShapeObject );
+                      }
+                    }
+                  }
                  // Create group servant
-                 SMESH::SMESH_Group_var aNewGroup = SMESH::SMESH_Group::_duplicate
-                    ( myNewMeshImpl->createGroup( (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1),
-                                                 nameFromFile ) );
+                  SMESH::ElementType type = (SMESH::ElementType)(ii - GetNodeGroupsTag() + 1);
+                 SMESH::SMESH_GroupBase_var aNewGroup = SMESH::SMESH_GroupBase::_duplicate
+                    ( myNewMeshImpl->createGroup( type, nameFromFile, aShape ) );
                  // Obtain a SMESHDS_Group object 
                  if ( aNewGroup->_is_nil() )
                    continue;
@@ -2054,7 +2410,8 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                  int newSubId = myStudyContext->findId( iorSubString );
                  myStudyContext->mapOldToNew( subid, newSubId );
 
-                 SMESH_Group_i* aGroupImpl = dynamic_cast<SMESH_Group_i*>( GetServant( aNewGroup ).in() );
+                 SMESH_GroupBase_i* aGroupImpl =
+                    dynamic_cast<SMESH_GroupBase_i*>( GetServant( aNewGroup ).in() );
                  if ( !aGroupImpl )
                    continue;
 
@@ -2062,11 +2419,13 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                  if ( !aLocalGroup )
                    continue;
 
-                 SMESHDS_Group* aGroupDS = aLocalGroup->GetGroupDS();
-                 aGroupDS->SetStoreName( name_dataset );
+                 SMESHDS_GroupBase* aGroupBaseDS = aLocalGroup->GetGroupDS();
+                 aGroupBaseDS->SetStoreName( name_dataset );
 
                  // Fill group with contents from MED file
-                 myReader.GetGroup( aGroupDS );
+                  SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( aGroupBaseDS );
+                  if ( aGrp )
+                    myReader.GetGroup( aGrp );
                }
              }
              aGroup->CloseOnDisk();
@@ -2102,7 +2461,7 @@ bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
                             const SALOMEDS::TMPFile& theStream,
                             const char*              theURL,
                             bool                     isMultiFile ) {
-  MESSAGE( "SMESH_Gen_i::LoadASCII" );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
   return Load( theComponent, theStream, theURL, isMultiFile );
 }
 
@@ -2116,10 +2475,10 @@ bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
 
 void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
 {
-  MESSAGE( "SMESH_Gen_i::Close" );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
 
   // Clear study contexts data
-  int studyId = myCurrentStudy->StudyId();
+  int studyId = GetCurrentStudyID();
   if ( myStudyContextMap.find( studyId ) != myStudyContextMap.end() ) {
     delete myStudyContextMap[ studyId ];
     myStudyContextMap.erase( studyId );
@@ -2137,8 +2496,8 @@ void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
 
 char* SMESH_Gen_i::ComponentDataType()
 {
-  MESSAGE( "SMESH_Gen_i::ComponentDataType" );
-  return strdup( "SMESH" );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::ComponentDataType" );
+  return CORBA::string_dup( "SMESH" );
 }
 
     
@@ -2150,24 +2509,24 @@ char* SMESH_Gen_i::ComponentDataType()
  */
 //=============================================================================
 
-char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr theSObject,
+char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr /*theSObject*/,
                                           const char*           IORString,
-                                          CORBA::Boolean        isMultiFile,
-                                          CORBA::Boolean        isASCII )
+                                          CORBA::Boolean        /*isMultiFile*/,
+                                          CORBA::Boolean        /*isASCII*/ )
 {
-  MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::IORToLocalPersistentID" );
   StudyContext* myStudyContext = GetCurrentStudyContext();
   
-  if ( strcmp( IORString, "" ) != 0 ) {
+  if ( myStudyContext && strcmp( IORString, "" ) != 0 ) {
     int anId = myStudyContext->findId( IORString );
     if ( anId ) {
-      MESSAGE( "VSR " << anId )
+      if(MYDEBUG) MESSAGE( "VSR " << anId )
       char strId[ 20 ];
       sprintf( strId, "%d", anId );
       return  CORBA::string_dup( strId );
     }
   }
-  return strdup( "" );
+  return CORBA::string_dup( "" );
 }
 
 //=============================================================================
@@ -2178,309 +2537,34 @@ char* SMESH_Gen_i::IORToLocalPersistentID( SALOMEDS::SObject_ptr theSObject,
  */
 //=============================================================================
 
-char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr theSObject,
+char* SMESH_Gen_i::LocalPersistentIDToIOR( SALOMEDS::SObject_ptr /*theSObject*/,
                                           const char*           aLocalPersistentID,
-                                          CORBA::Boolean        isMultiFile,
-                                          CORBA::Boolean        isASCII )
+                                          CORBA::Boolean        /*isMultiFile*/,
+                                          CORBA::Boolean        /*isASCII*/ )
 {
-  MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LocalPersistentIDToIOR(): id = " << aLocalPersistentID );
   StudyContext* myStudyContext = GetCurrentStudyContext();
 
-  if ( strcmp( aLocalPersistentID, "" ) != 0 ) {
+  if ( myStudyContext && strcmp( aLocalPersistentID, "" ) != 0 ) {
     int anId = atoi( aLocalPersistentID );
     return CORBA::string_dup( myStudyContext->getIORbyOldId( anId ).c_str() );
   }
-  return strdup( "" );
+  return CORBA::string_dup( "" );
 }
 
-//=============================================================================
-/*!
- *  SMESH_Gen_i::CanPublishInStudy
- *
- *  Returns true if object can be published in the study
- */
-//=============================================================================
+//=======================================================================
+//function : RegisterObject
+//purpose  : 
+//=======================================================================
 
-bool SMESH_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
+int SMESH_Gen_i::RegisterObject(CORBA::Object_ptr theObject)
 {
-  SMESH::SMESH_Mesh_var aMesh       = SMESH::SMESH_Mesh::_narrow(theIOR);
-  if( !aMesh->_is_nil() )
-    return true;
-
-  SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow(theIOR);
-  if( !aSubMesh->_is_nil() )
-    return true;
-
-  SMESH::SMESH_Hypothesis_var aHyp  = SMESH::SMESH_Hypothesis::_narrow(theIOR);
-  if( !aHyp->_is_nil() )
-    return true;
-
-  SMESH::SMESH_Group_var aGroup     = SMESH::SMESH_Group::_narrow(theIOR);
-  if( !aGroup->_is_nil() )
-    return true;
-
-  return false;
-}
-
-//=============================================================================
-/*!
- *  SMESH_Gen_i::PublishInStudy
- *
- *  Publish object in the study
- */
-//=============================================================================
-
-SALOMEDS::SObject_ptr SMESH_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
-                                                 SALOMEDS::SObject_ptr theSObject,
-                                                 CORBA::Object_ptr theIOR,
-                                                 const char* theName) 
-throw (SALOME::SALOME_Exception)
-{
-  Unexpect aCatch(SALOME_SalomeException);
-  MESSAGE( "********** SMESH_Gen_i::PublishInStudy()" );
-  SALOMEDS::SObject_var aSO;
-
-  // san - first try to find SObject corresponding to SMESH component in theStudy
-  // It is dangerous to use FindComponent("MESH") for this, as some other component
-  // of type "MESH" might be present in theStudy.
-  // So component's user name obtained from ModuleCatalog is passed to FindObject()...
-  SALOME_ModuleCatalog::ModuleCatalog_var aCat =
-    SALOME_ModuleCatalog::ModuleCatalog::_narrow( GetNS()->Resolve("/Kernel/ModulCatalog") );
-  if ( CORBA::is_nil( aCat ) )
-    return aSO._retn();
-
-  SALOME_ModuleCatalog::Acomponent_var   aComp = aCat->GetComponent( "SMESH" );
-  if ( CORBA::is_nil( aComp ) )
-    return aSO._retn();
-
-  SALOMEDS::SComponent_var father =
-    SALOMEDS::SComponent::_narrow( theStudy->FindObject( strdup( aComp->componentusername() ) ) );
-  SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder(); 
-
-  SALOMEDS::GenericAttribute_var anAttr;
-  SALOMEDS::AttributeName_var    aName;
-  SALOMEDS::AttributePixMap_var  aPixmap;
-  
-  if ( father->_is_nil() ) {
-    father  = aStudyBuilder->NewComponent( "MESH" );
-    anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributeName" );
-    aName   = SALOMEDS::AttributeName::_narrow( anAttr );
-    aName   ->SetValue( strdup( aComp->componentusername() ) );
-    anAttr  = aStudyBuilder->FindOrCreateAttribute( father, "AttributePixMap" );
-    aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
-    aPixmap ->SetPixMap( "ICON_OBJBROWSER_SMESH" );
-    aStudyBuilder->DefineComponentInstance( father, SMESH_Gen::_this() );
-  }
-
-  if ( father->_is_nil() )  
-    return aSO._retn();
-
-  SALOMEDS::AttributeIOR_var        anIOR;
-  SALOMEDS::AttributeSelectable_var aSelAttr;
-  TCollection_AsciiString anObjName("obj");
-
-  // Publishing a mesh
-  SMESH::SMESH_Mesh_var aMesh = SMESH::SMESH_Mesh::_narrow( theIOR );
-  if( !aMesh->_is_nil() ) {
-    // Find correct free tag
-    long aTag = FindMaxChildTag( father.in() );
-    if ( aTag <= GetAlgorithmsRootTag() )
-      aTag = GetAlgorithmsRootTag() + 1;
-    else
-      aTag++;
-    // Add New Mesh
-    SALOMEDS::SObject_var newMesh = aStudyBuilder->NewObjectToTag( father, aTag );
-    anAttr  = aStudyBuilder->FindOrCreateAttribute( newMesh, "AttributePixMap" );
-    aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
-    aPixmap ->SetPixMap( "ICON_SMESH_TREE_MESH" );
-    anAttr  = aStudyBuilder->FindOrCreateAttribute( newMesh, "AttributeIOR" );
-    anIOR   = SALOMEDS::AttributeIOR::_narrow(anAttr);
-    anIOR   ->SetValue( GetORB()->object_to_string( aMesh ) );
-    aSO     = SALOMEDS::SObject::_narrow( newMesh );
-    anObjName = TCollection_AsciiString( "Mesh" );
-  }
-
-  // Publishing a sub-mesh
-  SMESH::SMESH_subMesh_var aSubMesh = SMESH::SMESH_subMesh::_narrow( theIOR );
-  if( aSO->_is_nil() && !aSubMesh->_is_nil() ) {
-    // try to obtain a parent mesh's SObject
-    MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): publishing submesh..." );
-    SALOMEDS::SObject_var aParentSO;
-    SMESH::SMESH_Mesh_var aParentMesh;
-    SMESH_subMesh_i* aServant = dynamic_cast<SMESH_subMesh_i*>( GetServant( aSubMesh ).in() );
-    if ( aServant != NULL ) {
-      aParentMesh = aServant->_mesh_i->_this();
-      if ( !aParentMesh->_is_nil() ) {
-       aParentSO = theStudy->FindObjectIOR( GetORB()->object_to_string( aParentMesh ) );
-      }
-    }
-
-    // Find submesh sub-tree tag
-    if ( !aParentSO->_is_nil() ) {
-      long aRootTag = GetSubMeshOnVertexTag();
-      char* aRootName = "";
-
-      SMESH_Mesh_i* aMeshServant = aServant->_mesh_i;
-      if ( aMeshServant->_mapSubMesh.find( aServant->GetId() ) != aMeshServant->_mapSubMesh.end() ) {
-       MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): local submesh found" )
-       SMESH_subMesh* aLocalSubMesh = aMeshServant->_mapSubMesh[aServant->GetId()];
-       switch ( aLocalSubMesh->GetSubShape().ShapeType() ) {
-       case TopAbs_VERTEX:
-         aRootTag  = GetSubMeshOnVertexTag();
-         aRootName = "SubMeshes on Vertex";
-         break;
-       case TopAbs_EDGE:
-         aRootTag  = GetSubMeshOnEdgeTag();
-         aRootName = "SubMeshes on Edge";        
-         break;
-       case TopAbs_FACE:
-         aRootTag  = GetSubMeshOnFaceTag();
-         aRootName = "SubMeshes on Face";        
-         break;
-       case TopAbs_SOLID:
-         aRootTag  = GetSubMeshOnSolidTag();
-         aRootName = "SubMeshes on Solid";
-         break;
-       default:
-         aRootTag  = GetSubMeshOnCompoundTag();
-         aRootName = "SubMeshes on Compound";
-         break;
-       }
-      }
-
-      // Find or create submesh root
-      SALOMEDS::SObject_var aRootSO;
-      if ( !aParentSO->FindSubObject ( aRootTag, aRootSO ) ) {
-           MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): creating submesh root..." )
-       aRootSO  = aStudyBuilder->NewObjectToTag( aParentSO, aRootTag );
-       anAttr   = aStudyBuilder->FindOrCreateAttribute( aRootSO, "AttributeName" );
-       aName    = SALOMEDS::AttributeName::_narrow( anAttr );
-       aName    ->SetValue( aRootName );
-       anAttr   = aStudyBuilder->FindOrCreateAttribute( aRootSO, "AttributeSelectable" );
-       aSelAttr = SALOMEDS::AttributeSelectable::_narrow( anAttr );
-       aSelAttr ->SetSelectable( false );
-      }
-
-      // Add new submesh to corresponding sub-tree
-      MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): adding submesh to study..." )
-      SALOMEDS::SObject_var newMesh = aStudyBuilder->NewObject( aRootSO );
-      anAttr  = aStudyBuilder->FindOrCreateAttribute( newMesh, "AttributePixMap" );
-      aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
-      aPixmap ->SetPixMap( "ICON_SMESH_TREE_MESH" );
-      anAttr  = aStudyBuilder->FindOrCreateAttribute( newMesh, "AttributeIOR" );
-      anIOR   = SALOMEDS::AttributeIOR::_narrow(anAttr);
-      anIOR   ->SetValue( GetORB()->object_to_string( aSubMesh ) );
-      aSO     = SALOMEDS::SObject::_narrow( newMesh );
-      anObjName = TCollection_AsciiString( "SubMesh" );
-    }
-  }
-
-  // Publishing a hypothesis or algorithm
-  SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow( theIOR );
-  if( aSO->_is_nil() && !aHyp->_is_nil() ) {
-    //Find or Create Hypothesis root
-    SALOMEDS::SObject_var  HypothesisRoot;
-    Standard_Integer aRootTag =
-      SMESH::SMESH_Algo::_narrow( theIOR )->_is_nil() ? GetHypothesisRootTag() : GetAlgorithmsRootTag();
-
-    if ( !father->FindSubObject ( aRootTag, HypothesisRoot ) ) {
-      HypothesisRoot = aStudyBuilder->NewObjectToTag( father, aRootTag );
-      anAttr   = aStudyBuilder->FindOrCreateAttribute( HypothesisRoot, "AttributeName" );
-      aName    = SALOMEDS::AttributeName::_narrow( anAttr );
-      aName    ->SetValue( aRootTag ==  GetHypothesisRootTag()  ? "Hypotheses" : "Algorithms" );
-      anAttr   = aStudyBuilder->FindOrCreateAttribute( HypothesisRoot, "AttributeSelectable" );
-      aSelAttr = SALOMEDS::AttributeSelectable::_narrow( anAttr );
-      aSelAttr ->SetSelectable( false );
-      anAttr   = aStudyBuilder->FindOrCreateAttribute( HypothesisRoot, "AttributePixMap" );
-      aPixmap  = SALOMEDS::AttributePixMap::_narrow( anAttr );
-      aPixmap  ->SetPixMap( aRootTag == GetHypothesisRootTag()  ? "ICON_SMESH_TREE_HYPO" : "ICON_SMESH_TREE_ALGO" );
-    }
-
-    // Add New Hypothesis
-    string aPmName;
-    SALOMEDS::SObject_var newHypo = aStudyBuilder->NewObject( HypothesisRoot );
-    anAttr  = aStudyBuilder->FindOrCreateAttribute( newHypo, "AttributePixMap" );
-    aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
-    aPmName = ( aRootTag == GetHypothesisRootTag()  ? "ICON_SMESH_TREE_HYPO_" : "ICON_SMESH_TREE_ALGO_" );
-    aPmName += aHyp->GetName();
-    aPixmap ->SetPixMap( aPmName.c_str() );
-    anAttr  = aStudyBuilder->FindOrCreateAttribute( newHypo, "AttributeIOR" );
-    anIOR   = SALOMEDS::AttributeIOR::_narrow(anAttr);
-    anIOR   ->SetValue( GetORB()->object_to_string( aHyp ) );
-    aSO     = SALOMEDS::SObject::_narrow( newHypo );
-    anObjName = TCollection_AsciiString( aHyp->GetName() );
-  }
-
-  // Publishing a group
-  SMESH::SMESH_Group_var aGroup = SMESH::SMESH_Group::_narrow(theIOR);
-  if( aSO->_is_nil() && !aGroup->_is_nil() ) {
-    // try to obtain a parent mesh's SObject
-    MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): publishing group..." );
-    SALOMEDS::SObject_var aParentSO;
-    SMESH::SMESH_Mesh_var aParentMesh;
-    SMESH_Group_i* aServant = dynamic_cast<SMESH_Group_i*>( GetServant( aGroup ).in() );
-    if ( aServant != NULL ) {
-      aParentMesh = SMESH::SMESH_Mesh::_narrow( GetPOA()->servant_to_reference( aServant->GetMeshServant() ) );
-      if ( !aParentMesh->_is_nil() ) {
-       MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): publishing group: refernce to mesh is OK" );
-       string anIOR = GetORB()->object_to_string( aParentMesh );
-       MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): publishing group: mesh IOR = "<<anIOR.c_str() );
-       aParentSO = theStudy->FindObjectIOR( anIOR.c_str() );
-      }
-    }
-
-    // Find proper group sub-tree tag
-    if ( !aParentSO->_is_nil() ) {
-      MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): publishing group: parent mesh found" );
-      int aType = (int)aGroup->GetType();
-      const char* aRootNames[] = { "Compound Groups", "Groups of Nodes", "Groups of Edges", "Groups of Faces", "Groups of Volumes" };
-
-      // Currently, groups with heterogenous content are not supported
-      if ( aType != SMESH::ALL ) {
-       MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): publishing group: group type OK" );
-       long aRootTag = GetNodeGroupsTag() + aType - 1;
-
-       // Find or create groups root
-       SALOMEDS::SObject_var aRootSO;
-       if ( !aParentSO->FindSubObject ( aRootTag, aRootSO ) ) {
-         MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): creating groups root..." )
-         aRootSO  = aStudyBuilder->NewObjectToTag( aParentSO, aRootTag );
-         anAttr   = aStudyBuilder->FindOrCreateAttribute( aRootSO, "AttributeName" );
-         aName    = SALOMEDS::AttributeName::_narrow( anAttr );
-         aName    ->SetValue( aRootNames[aType] );
-         anAttr   = aStudyBuilder->FindOrCreateAttribute( aRootSO, "AttributeSelectable" );
-         aSelAttr = SALOMEDS::AttributeSelectable::_narrow( anAttr );
-         aSelAttr ->SetSelectable( false );
-       }
-
-       // Add new group to corresponding sub-tree
-       MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): adding group to study..." )
-       SALOMEDS::SObject_var aGroupSO = aStudyBuilder->NewObject( aRootSO );
-       anAttr  = aStudyBuilder->FindOrCreateAttribute( aGroupSO, "AttributePixMap" );
-       aPixmap = SALOMEDS::AttributePixMap::_narrow( anAttr );
-       aPixmap ->SetPixMap( "ICON_SMESH_TREE_GROUP" );
-       anAttr  = aStudyBuilder->FindOrCreateAttribute( aGroupSO, "AttributeIOR" );
-       anIOR   = SALOMEDS::AttributeIOR::_narrow( anAttr );
-       anIOR   ->SetValue( GetORB()->object_to_string( aGroup ) );
-       aSO     = SALOMEDS::SObject::_narrow( aGroupSO );
-       anObjName = TCollection_AsciiString( "Group" );
-      }
-    }
-  }
-
-  // Setting SObject's name
-  if ( !aSO->_is_nil() ) {
-    if ( strlen( theName ) == 0 ) 
-      anObjName += TCollection_AsciiString( "_" ) + TCollection_AsciiString( aSO->Tag() );
-    else 
-      anObjName = TCollection_AsciiString( strdup( theName ) );
-    anAttr  = aStudyBuilder->FindOrCreateAttribute( aSO, "AttributeName" );
-    aName   = SALOMEDS::AttributeName::_narrow( anAttr );
-    aName   ->SetValue( anObjName.ToCString() );    
+  StudyContext* myStudyContext = GetCurrentStudyContext();
+  if ( myStudyContext && !CORBA::is_nil( theObject )) {
+    string iorString = GetORB()->object_to_string( theObject );
+    return myStudyContext->addObject( iorString );
   }
-
-  MESSAGE( "********** SMESH_Gen_i::PublishInStudy(): COMPLETED" )
-  return aSO._retn();
+  return 0;
 }
       
 //=============================================================================
@@ -2499,10 +2583,9 @@ extern "C"
                                                 const char*               instanceName, 
                                                 const char*               interfaceName )
   {
-    MESSAGE( "PortableServer::ObjectId* SMESHEngine_factory()" );
-    SCRUTE(interfaceName);
-    SMESH_Gen_i * mySMESH_Gen 
-      = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
-    return mySMESH_Gen->getId() ;
+    if(MYDEBUG) MESSAGE( "PortableServer::ObjectId* SMESHEngine_factory()" );
+    if(MYDEBUG) SCRUTE(interfaceName);
+    SMESH_Gen_i* aSMESHGen = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
+    return aSMESHGen->getId() ;
   }
 }