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 5d1d45ed6addc59484de84454e1c7a216f6ba744..77abfc9e5285878013b6f0bc0b1e9a81464ebfd2 100644 (file)
@@ -472,12 +472,24 @@ SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis( const char* theHypNam
   SMESH::SMESH_Hypothesis_var hyp = this->createHypothesis( theHypName, theLibName );
 
   // Publish hypothesis/algorithm in the study
-  if ( CanPublishInStudy( hyp ) )
-    PublishHypothesis( myCurrentStudy, 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
@@ -497,9 +509,23 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMesh( GEOM::GEOM_Object_ptr theShapeObj
   SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( mesh ).in() );
   ASSERT( meshServant );
   meshServant->SetShape( theShapeObject );
+
   // publish mesh in the study
-  if ( CanPublishInStudy( mesh ) )
-    PublishMesh( myCurrentStudy, mesh.in() );
+  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();
 }
 
@@ -520,8 +546,21 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromUNV( const char* theFileName
   SMESH::SMESH_Mesh_var aMesh = createMesh();
   string aFileName; // = boost::filesystem::path(theFileName).leaf();
   // publish mesh in the study
-  if ( CanPublishInStudy( aMesh ) )
-    PublishMesh( myCurrentStudy, aMesh.in(), aFileName.c_str() );
+  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 );
@@ -544,6 +583,9 @@ SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
   Unexpect aCatch(SALOME_SalomeException);
   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 );
@@ -552,19 +594,33 @@ SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
   list<string> aNames = myReader.GetMeshNames(aStatus);
   SMESH::mesh_array_var aResult = new SMESH::mesh_array();
   theStatus = (SMESH::DriverMED_ReadStatus)aStatus;
-  if(theStatus == SMESH::DRS_OK){
+  if (theStatus == SMESH::DRS_OK) {
+    SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
+    aStudyBuilder->NewCommand();  // There is a transaction
     aResult->length( aNames.size() );
     int i = 0;
     
     // 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 += ", ";
+
       // create mesh
       SMESH::SMESH_Mesh_var mesh = createMesh();
       
       // publish mesh in the study
+      SALOMEDS::SObject_var aSO;
       if ( CanPublishInStudy( mesh ) )
-        PublishMesh( myCurrentStudy, mesh.in(), (*it).c_str() );
-      
+        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 );
@@ -575,7 +631,16 @@ SMESH::mesh_array* SMESH_Gen_i::CreateMeshesFromMED( const char* theFileName,
 
       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();
 }
 
@@ -596,8 +661,22 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateMeshesFromSTL( const char* theFileName
   SMESH::SMESH_Mesh_var aMesh = createMesh();
   string aFileName; // = boost::filesystem::path(theFileName).leaf();
   // publish mesh in the study
-  if ( CanPublishInStudy( aMesh ) )
-    PublishInStudy( myCurrentStudy, SALOMEDS::SObject::_nil(), aMesh.in(), aFileName.c_str() );
+  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 );
@@ -744,6 +823,17 @@ CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr 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() );
@@ -785,6 +875,9 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
        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
@@ -975,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();
@@ -994,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();
 
@@ -1033,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();
 
@@ -1070,9 +1171,48 @@ 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() )
@@ -1099,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 ) ) );
                      
@@ -1109,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() ) {
-//                   if(MYDEBUG) 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;
@@ -1211,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;
@@ -1257,6 +1386,7 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
 
                    // Store the group contents into MED file
                    if ( myLocMesh.GetGroup( myGroupImpl->GetLocalID() ) ) {
+
                      if(MYDEBUG) MESSAGE( "VSR - SMESH_Gen_i::Save(): saving group with StoreName = "
                               << grpName << " to MED file" );
                      SMESHDS_GroupBase* aGrpBaseDS =
@@ -1274,7 +1404,8 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
                       if ( aGeomGrp ) {
                         SALOMEDS::SObject_var mySubRef, myShape;
                         if (mySObject->FindSubObject( GetRefOnShapeTag(), mySubRef ) &&
-                            mySubRef->ReferencedObject( myShape ))
+                            mySubRef->ReferencedObject( myShape ) &&
+                            !CORBA::is_nil( myShape->GetObject() ))
                         {
                           string myRefOnObject = myShape->GetID();
                           if ( myRefOnObject.length() > 0 ) {
@@ -1287,151 +1418,164 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
                             aDataset->CloseOnDisk();
                           }
                         }
+                        else // shape ref is invalid:
+                        {
+                          // save a group on geometry as ordinary group
+                          myWriter.AddGroup( aGeomGrp );
+                        }
                       }
                    }
                  }
                }
                aGroup->CloseOnDisk();
              }
-           }
+           } // 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):
 
-              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++ )
+              if ( !mySMESHDSMesh->SubMeshes().empty() )
               {
-                // 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++ )
+                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 = (*itSM);
+                  SMESHDS_SubMesh* aSubMesh = (*itSubM).second;
                   if ( aSubMesh->IsComplexSubmesh() )
                     continue; // submesh containing other submeshs
-
-                  SMDS_NodeIteratorPtr itNode = aSubMesh->GetNodes();
-                  // loop on nodes in aSubMesh
-                  while ( itNode->more() )
+                  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++ )
                   {
-                    //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++;
+                    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
-                        nbNodes--;
-                    }
-                    else { // on EDGE
-                      const SMDS_EdgePosition* ePos =
-                        dynamic_cast<const SMDS_EdgePosition*>( pos.get() );
-                      if ( ePos ) {
-                        aUPos[ iNode ] = ePos->GetUParameter();
-                        iNode++;
+                      else { // on EDGE
+                        const SMDS_EdgePosition* ePos =
+                          dynamic_cast<const SMDS_EdgePosition*>( pos.get() );
+                        if ( ePos ) {
+                          aUPos[ iNode ] = ePos->GetUParameter();
+                          iNode++;
+                        }
+                        else
+                          nbNodes--;
                       }
-                      else
-                        nbNodes--;
-                    }
-                  } // loop on nodes in aSubMesh
-                } // loop on sub-meshes
+                    } // 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);
+                  // 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( aVPos );
+                    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;
+                  delete [] aNodeIDs;
+                  delete [] aUPos;
+                  if ( aVPos ) delete [] aVPos;
 
-              } // treat positions on edges or faces
+                } // treat positions on edges or faces
 
-              // close "Node Positions" group
-              aGroup->CloseOnDisk(); 
+                // close "Node Positions" group
+                aGroup->CloseOnDisk(); 
 
+              } // if ( there are submeshes in SMESHDS_Mesh )
             } // if ( hasData )
 
            // close mesh HDF group
@@ -1594,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
@@ -1606,7 +1750,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              aDataset->ReadFromDisk( libname_str );
              if(MYDEBUG) SCRUTE( libname_str );
              libname = string( libname_str );
-             delete libname_str;
+             delete [] libname_str;
              aDataset->CloseOnDisk();
            }
            // --> get hypothesis data
@@ -1617,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();
            }
          }
@@ -1691,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
@@ -1703,7 +1847,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              aDataset->ReadFromDisk( libname_str );
              if(MYDEBUG) SCRUTE( libname_str );
              libname = string( libname_str );
-             delete libname_str;
+             delete [] libname_str;
              aDataset->CloseOnDisk();
            }
            // --> get algorithm data
@@ -1715,7 +1859,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
              aDataset->ReadFromDisk( hypdata_str );
              if(MYDEBUG) SCRUTE( hypdata_str );
              hypdata = string( hypdata_str );
-             delete hypdata_str;
+             delete [] hypdata_str;
              aDataset->CloseOnDisk();
            }
          }
@@ -1828,17 +1972,18 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
            }
          }
 
-         // 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(); 
+           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();
@@ -1846,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 );
@@ -1865,18 +2010,17 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
            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(); 
-           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 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();
@@ -1884,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 );
@@ -1989,25 +2133,25 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
 //                   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 );
@@ -2022,29 +2166,29 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                        }
                      }
                    }
-                   // 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 );
@@ -2059,10 +2203,10 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                        }
                      }
                    }
-                   // close "applied algorithms" HDF group
+                   // close "applied hypotheses" HDF group
                    aSubSubGroup->CloseOnDisk();
                  }
-                 
+
                  // close submesh HDF group
                  aSubGroup->CloseOnDisk();
                }
@@ -2074,7 +2218,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
 
          if(hasData) {
            // Read sub-meshes from MED
-           if(MYDEBUG) MESSAGE("JFA - Create all sub-meshes");
+           if(MYDEBUG) MESSAGE("Create all sub-meshes");
            myReader.CreateAllSubMeshes();
 
 
@@ -2127,16 +2271,22 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                 }
                 else // NODE IDS
                 {
-                  int* ids = new int [ aDataset->GetSize() ];
+                 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 = aDataset->GetSize();
+                    nbEids = aSize;
                   }
                   else {
                     aFids = ids;
-                    nbFids = aDataset->GetSize();
+                    nbFids = aSize;
                   }
                 }
               } // loop on 5 datasets
@@ -2184,11 +2334,11 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
 
           // Recompute State (as computed sub-meshes are restored from MED)
          if ( !aShapeObject->_is_nil() ) {
-           MESSAGE("JFA - Compute State Engine ...");
+           MESSAGE("Compute State Engine ...");
            TopoDS_Shape myLocShape = GeomObjectToShape( aShapeObject );
            myNewMeshImpl->GetImpl().GetSubMesh(myLocShape)->ComputeStateEngine
               (SMESH_subMesh::SUBMESH_RESTORED);
-           MESSAGE("JFA - Compute State Engine finished");
+           MESSAGE("Compute State Engine finished");
          }
 
          // try to get groups