Salome HOME
NPAL16716: Compound. To create the groups of the initial meshes.
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i.cxx
index 6461586bdacbc7898c41b6f7ee499a19f067f030..357b8d77fad43a1f269f43d36cd3629885dfb27b 100644 (file)
@@ -109,6 +109,7 @@ static int MYDEBUG = 0;
 #endif
 
 // Static variables definition
+GEOM::GEOM_Gen_var      SMESH_Gen_i::myGeomGen = GEOM::GEOM_Gen::_nil();
 CORBA::ORB_var          SMESH_Gen_i::myOrb;
 PortableServer::POA_var SMESH_Gen_i::myPoa;
 SALOME_NamingService*   SMESH_Gen_i::myNS  = NULL;
@@ -206,9 +207,15 @@ SALOME_LifeCycleCORBA*  SMESH_Gen_i::GetLCC() {
  */
 //=============================================================================     
 GEOM::GEOM_Gen_ptr SMESH_Gen_i::GetGeomEngine() {
-  GEOM::GEOM_Gen_var aGeomEngine =
-    GEOM::GEOM_Gen::_narrow( GetLCC()->FindOrLoad_Component("FactoryServer","GEOM") );
-  return aGeomEngine._retn();
+  //CCRT GEOM::GEOM_Gen_var aGeomEngine =
+  //CCRT   GEOM::GEOM_Gen::_narrow( GetLCC()->FindOrLoad_Component("FactoryServer","GEOM") );
+  //CCRT return aGeomEngine._retn();
+  if(CORBA::is_nil(myGeomGen))
+  {
+    Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component("FactoryServer","GEOM");
+    myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
+  }
+  return myGeomGen;
 }
 
 //=============================================================================
@@ -414,6 +421,20 @@ GEOM_Client* SMESH_Gen_i::GetShapeReader()
   return myShapeReader;
 }
 
+//=============================================================================
+/*!
+ *  SMESH_Gen_i::SetGeomEngine
+ *
+ *  Set GEOM::GEOM_Gen reference
+ */
+//=============================================================================
+GEOM::GEOM_Gen_ptr SMESH_Gen_i::SetGeomEngine( const char* containerLoc )
+{
+  Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component(containerLoc,"GEOM");
+  myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
+  return myGeomGen;
+}
+
 //=============================================================================
 /*!
  *  SMESH_Gen_i::SetEmbeddedMode
@@ -892,6 +913,105 @@ CORBA::Boolean SMESH_Gen_i::IsReadyToCompute( SMESH::SMESH_Mesh_ptr theMesh,
   return false;
 }
 
+//================================================================================
+/*!
+ * \brief  Find SObject for an algo
+ */
+//================================================================================
+
+SALOMEDS::SObject_ptr SMESH_Gen_i::GetAlgoSO(const ::SMESH_Algo* algo)
+{
+  if ( algo ) {
+    if ( !myCurrentStudy->_is_nil() ) {
+      // find algo in the study
+      SALOMEDS::SComponent_var father = SALOMEDS::SComponent::_narrow
+        ( myCurrentStudy->FindComponent( ComponentDataType() ) );
+      if ( !father->_is_nil() ) {
+        SALOMEDS::ChildIterator_var itBig = myCurrentStudy->NewChildIterator( father );
+        for ( ; itBig->More(); itBig->Next() ) {
+          SALOMEDS::SObject_var gotBranch = itBig->Value();
+          if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
+            SALOMEDS::ChildIterator_var algoIt = myCurrentStudy->NewChildIterator( gotBranch );
+            for ( ; algoIt->More(); algoIt->Next() ) {
+              SALOMEDS::SObject_var algoSO = algoIt->Value();
+              CORBA::Object_var     algoIOR = SObjectToObject( algoSO );
+              if ( !CORBA::is_nil( algoIOR )) {
+                SMESH_Hypothesis_i* impl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
+                if ( impl && impl->GetImpl() == algo )
+                  return algoSO._retn();
+              }
+            } // loop on algo SO's
+            break;
+          } // if algo tag
+        } // SMESH component iterator
+      }
+    }
+  }
+  return SALOMEDS::SObject::_nil();
+}
+
+//================================================================================
+/*!
+ * \brief Return errors of mesh computation
+ */
+//================================================================================
+
+SMESH::compute_error_array* SMESH_Gen_i::GetComputeErrors( SMESH::SMESH_Mesh_ptr theMesh, 
+                                                           GEOM::GEOM_Object_ptr theSubObject )
+  throw ( SALOME::SALOME_Exception )
+{
+  Unexpect aCatch(SALOME_SalomeException);
+  if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetComputeErrors()" );
+
+  if ( CORBA::is_nil( theSubObject ) )
+    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 );
+
+  SMESH::compute_error_array_var error_array = new SMESH::compute_error_array;
+  try {
+    if ( SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh ))
+    {
+      TopoDS_Shape shape = GeomObjectToShape( theSubObject );
+      ::SMESH_Mesh& mesh = meshServant->GetImpl();
+
+      error_array->length( mesh.GetMeshDS()->MaxShapeIndex() );
+      int nbErr = 0;
+
+      SMESH_subMesh *sm = mesh.GetSubMesh(shape);
+      const bool includeSelf = true, complexShapeFirst = true;
+      SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(includeSelf,
+                                                               complexShapeFirst);
+      while ( smIt->more() )
+      {
+        sm = smIt->next();
+        if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
+          break;
+        SMESH_ComputeErrorPtr error = sm->GetComputeError();
+        if ( error && !error->IsOK() && error->myAlgo )
+        {
+          SMESH::ComputeError & errStruct = error_array[ nbErr++ ];
+          errStruct.code       = -( error->myName < 0 ? error->myName + 1: error->myName ); // -1 -> 0
+          errStruct.comment    = error->myComment.c_str();
+          errStruct.subShapeID = sm->GetId();
+          SALOMEDS::SObject_var algoSO = GetAlgoSO( error->myAlgo );
+          if ( !algoSO->_is_nil() )
+            errStruct.algoName   = algoSO->GetName();
+          else
+            errStruct.algoName   = error->myAlgo->GetName();
+        }
+      }
+      error_array->length( nbErr );
+    }
+  }
+  catch ( SALOME_Exception& S_ex ) {
+    INFOS( "catch exception "<< S_ex.what() );
+  }
+
+  return error_array._retn();
+}
+
 //================================================================================
 /*!
  * \brief Returns errors of hypotheses definintion
@@ -929,55 +1049,15 @@ SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMes
       int i = 0;
       for ( error = error_list.begin(); error != error_list.end(); ++error )
       {
-        // error name
-        SMESH::AlgoStateErrorName errName;
-        switch ( error->_name ) {
-        case ::SMESH_Gen::MISSING_ALGO:     errName = SMESH::MISSING_ALGO; break;
-        case ::SMESH_Gen::MISSING_HYPO:     errName = SMESH::MISSING_HYPO; break;
-        case ::SMESH_Gen::NOT_CONFORM_MESH: errName = SMESH::NOT_CONFORM_MESH; break;
-        case ::SMESH_Gen::BAD_PARAM_VALUE:  errName = SMESH::BAD_PARAM_VALUE; break;
-        default:
-          THROW_SALOME_CORBA_EXCEPTION( "bad error name",SALOME::BAD_PARAM );
-        }
-        // algo name
-        CORBA::String_var algoName = "";
-        if ( error->_algo ) {
-          if ( !myCurrentStudy->_is_nil() ) {
-            // find algo in the study
-            SALOMEDS::SComponent_var father = SALOMEDS::SComponent::_narrow
-              ( myCurrentStudy->FindComponent( ComponentDataType() ) );
-            if ( !father->_is_nil() ) {
-              SALOMEDS::ChildIterator_var itBig = myCurrentStudy->NewChildIterator( father );
-              for ( ; itBig->More(); itBig->Next() ) {
-                SALOMEDS::SObject_var gotBranch = itBig->Value();
-                if ( gotBranch->Tag() == GetAlgorithmsRootTag() ) {
-                  SALOMEDS::ChildIterator_var algoIt = myCurrentStudy->NewChildIterator( gotBranch );
-                  for ( ; algoIt->More(); algoIt->Next() ) {
-                    SALOMEDS::SObject_var algoSO = algoIt->Value();
-                    CORBA::Object_var    algoIOR = SObjectToObject( algoSO );
-                    if ( !CORBA::is_nil( algoIOR )) {
-                      SMESH_Hypothesis_i* myImpl = SMESH::DownCast<SMESH_Hypothesis_i*>( algoIOR );
-                      if ( myImpl && myImpl->GetImpl() == error->_algo ) {
-                        algoName = algoSO->GetName();
-                        break;
-                      }
-                    }
-                  } // loop on algo SO's
-                  break;
-                } // if algo tag
-              } // SMESH component iterator
-            }
-          }
-          if ( algoName.in() == 0 )
-            // use algo type name
-            algoName = error->_algo->GetName();
-        }
         // fill AlgoStateError structure
         SMESH::AlgoStateError & errStruct = error_array[ i++ ];
-        errStruct.name         = errName;
-        errStruct.algoName     = algoName;
+        errStruct.state        = SMESH_Mesh_i::ConvertHypothesisStatus( error->_name );
         errStruct.algoDim      = error->_algoDim;
         errStruct.isGlobalAlgo = error->_isGlobalAlgo;
+        errStruct.algoName     = "";
+        SALOMEDS::SObject_var algoSO = GetAlgoSO( error->_algo );
+        if ( !algoSO->_is_nil() )
+          errStruct.algoName   = algoSO->GetName();
       }
     }
   }
@@ -1088,13 +1168,14 @@ CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
   // Update Python script
   TPythonDump() << "isDone = " << this << ".Compute( "
                 << theMesh << ", " << theShapeObject << ")";
-  TPythonDump() << "if not isDone: print 'Mesh', " << theMesh << ", ': computation failed'";
 
   try {
     // get mesh servant
     SMESH_Mesh_i* meshServant = dynamic_cast<SMESH_Mesh_i*>( GetServant( theMesh ).in() );
     ASSERT( meshServant );
     if ( meshServant ) {
+      // NPAL16168: "geometrical group edition from a submesh don't modifiy mesh computation"
+      meshServant->CheckGeomGroupModif();
       // get local TopoDS_Shape
       TopoDS_Shape myLocShape = GeomObjectToShape( theShapeObject );
       // call implementation compute
@@ -1102,9 +1183,8 @@ CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
       return myGen.Compute( myLocMesh, myLocShape);
     }
   }
-  catch ( std::bad_alloc& exc ) {
-    THROW_SALOME_CORBA_EXCEPTION( "Memory allocation problem",
-                                  SALOME::INTERNAL_ERROR );
+  catch ( std::bad_alloc ) {
+    INFOS( "Compute(): lack of memory" );
   }
   catch ( SALOME_Exception& S_ex ) {
     INFOS( "Compute(): catch exception "<< S_ex.what() );
@@ -1136,14 +1216,37 @@ SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
   GEOM::GEOM_Object_var geom = FindGeometryByMeshElement(theMesh, theElementID);
   if ( !geom->_is_nil() ) {
     GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
-    GEOM::GEOM_Gen_var    geomGen   = GetGeomEngine();
+    GEOM::GEOM_Gen_ptr    geomGen   = GetGeomEngine();
 
     // try to find the corresponding SObject
-    GeomObjectToShape( geom ); // geom client remembers the found shape
     SALOMEDS::SObject_var SObj = ObjectToSObject( myCurrentStudy, geom.in() );
-    if ( SObj->_is_nil() )
-      // publish a new subshape
+    if ( SObj->_is_nil() ) // submesh can be not found even if published
+    {
+      // try to find published submesh
+      GEOM::ListOfLong_var list = geom->GetSubShapeIndices();
+      if ( !geom->IsMainShape() && list->length() == 1 ) {
+        SALOMEDS::SObject_var mainSO = ObjectToSObject( myCurrentStudy, mainShape );
+        SALOMEDS::ChildIterator_var it;
+        if ( !mainSO->_is_nil() )
+          it = myCurrentStudy->NewChildIterator( mainSO );
+        if ( !it->_is_nil() ) {
+          for ( it->InitEx(true); SObj->_is_nil() && it->More(); it->Next() ) {
+            GEOM::GEOM_Object_var subGeom =
+              GEOM::GEOM_Object::_narrow( SObjectToObject( it->Value() ));
+            if ( !subGeom->_is_nil() ) {
+              GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
+              if ( subList->length() == 1 && list[0] == subList[0] ) {
+                SObj = it->Value();
+                geom = subGeom;
+              }
+            }
+          }
+        }
+      }
+    }
+    if ( SObj->_is_nil() ) // publish a new subshape
       SObj = geomGen->AddInStudy( myCurrentStudy, geom, theGeomName, mainShape );
+
     // return only published geometry
     if ( !SObj->_is_nil() )
       return geom._retn();
@@ -1151,6 +1254,76 @@ SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
   return GEOM::GEOM_Object::_nil();
 }
 
+//================================================================================
+/*!
+ * \brief Return geometrical object the given element is built on.
+ *  \param theMesh - the mesh the element is in
+ *  \param theElementID - the element ID
+ *  \retval GEOM::GEOM_Object_ptr - the found geom object
+ */
+//================================================================================
+
+GEOM::GEOM_Object_ptr
+SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
+                                        CORBA::Long            theElementID)
+  throw ( SALOME::SALOME_Exception )
+{
+  Unexpect aCatch(SALOME_SalomeException);
+  if ( CORBA::is_nil( theMesh ) )
+    THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
+
+  GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
+  GEOM::GEOM_Gen_ptr    geomGen   = GetGeomEngine();
+
+  // get a core mesh DS
+  SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
+  if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
+  {
+    ::SMESH_Mesh & mesh = meshServant->GetImpl();
+    SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
+    // find the element in mesh
+    if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) ) {
+      // find a shape id by the element
+      if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
+        // get a geom object by the shape id
+        GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
+        if ( geom->_is_nil() ) {
+          // try to find a published sub-shape
+          SALOMEDS::SObject_var mainSO = ObjectToSObject( myCurrentStudy, mainShape );
+          SALOMEDS::ChildIterator_var it;
+          if ( !mainSO->_is_nil() )
+            it = myCurrentStudy->NewChildIterator( mainSO );
+          if ( !it->_is_nil() ) {
+            for ( it->InitEx(true); it->More(); it->Next() ) {
+              GEOM::GEOM_Object_var subGeom =
+                GEOM::GEOM_Object::_narrow( SObjectToObject( it->Value() ));
+              if ( !subGeom->_is_nil() ) {
+                GEOM::ListOfLong_var subList = subGeom->GetSubShapeIndices();
+                if ( subList->length() == 1 && shapeID == subList[0] ) {
+                  geom = subGeom;
+                  break;
+                }
+              }
+            }
+          }
+        }
+        if ( geom->_is_nil() ) {
+          // explode
+          GEOM::GEOM_IShapesOperations_var op =
+            geomGen->GetIShapesOperations( GetCurrentStudyID() );
+          if ( !op->_is_nil() )
+            geom = op->GetSubShape( mainShape, shapeID );
+        }
+        if ( !geom->_is_nil() ) {
+          GeomObjectToShape( geom ); // let geom client remember the found shape
+         return geom._retn();
+        }
+      }
+    }
+  }
+  return GEOM::GEOM_Object::_nil();
+}
+
 //================================================================================
 /*!
  *  SMESH_Gen_i::Concatenate
@@ -1159,11 +1332,57 @@ SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
  */
 //================================================================================
 
-SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshesArray, 
+SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshesArray,
                                               CORBA::Boolean           theUniteIdenticalGroups, 
                                               CORBA::Boolean           theMergeNodesAndElements, 
                                               CORBA::Double            theMergeTolerance)
   throw ( SALOME::SALOME_Exception )
+{
+  return ConcatenateCommon(theMeshesArray,
+                          theUniteIdenticalGroups,
+                          theMergeNodesAndElements,
+                          theMergeTolerance,
+                          false);
+}
+
+//================================================================================
+/*!
+ *  SMESH_Gen_i::ConcatenateWithGroups
+ *
+ *  Concatenate the given meshes into one mesh
+ *  Create the groups of all elements from initial meshes
+ */
+//================================================================================
+
+SMESH::SMESH_Mesh_ptr
+SMESH_Gen_i::ConcatenateWithGroups(const SMESH::mesh_array& theMeshesArray,
+                                  CORBA::Boolean           theUniteIdenticalGroups, 
+                                   CORBA::Boolean           theMergeNodesAndElements, 
+                                   CORBA::Double            theMergeTolerance)
+  throw ( SALOME::SALOME_Exception )
+{
+  return ConcatenateCommon(theMeshesArray,
+                           theUniteIdenticalGroups,
+                           theMergeNodesAndElements,
+                           theMergeTolerance,
+                          true);
+}
+
+//================================================================================
+/*!
+ *  SMESH_Gen_i::ConcatenateCommon
+ *
+ *  Concatenate the given meshes into one mesh
+ */
+//================================================================================
+
+SMESH::SMESH_Mesh_ptr
+SMESH_Gen_i::ConcatenateCommon(const SMESH::mesh_array& theMeshesArray,
+                              CORBA::Boolean           theUniteIdenticalGroups, 
+                               CORBA::Boolean           theMergeNodesAndElements, 
+                               CORBA::Double            theMergeTolerance,
+                              CORBA::Boolean           theCommonGroups)
+  throw ( SALOME::SALOME_Exception )
 {
   typedef map<int, int> TIDsMap;
   typedef list<SMESH::SMESH_Group_var> TListOfNewGroups;
@@ -1204,6 +1423,23 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshe
            const SMDS_MeshElement* aNewElem = 0;
            int anElemNbNodes = 0;
 
+           int anNbNodes   = 0;
+           int anNbEdges   = 0;
+           int anNbFaces   = 0;
+           int anNbVolumes = 0;
+
+           SMESH::long_array_var anIDsNodes   = new SMESH::long_array();
+           SMESH::long_array_var anIDsEdges   = new SMESH::long_array();
+           SMESH::long_array_var anIDsFaces   = new SMESH::long_array();
+           SMESH::long_array_var anIDsVolumes = new SMESH::long_array();
+
+           if( theCommonGroups ) {
+             anIDsNodes->length(   anInitMeshDS->NbNodes()   );
+             anIDsEdges->length(   anInitMeshDS->NbEdges()   );
+             anIDsFaces->length(   anInitMeshDS->NbFaces()   );
+             anIDsVolumes->length( anInitMeshDS->NbVolumes() );
+           }
+
            for ( int j = 0; itElems->more(); j++) {
              anElem = itElems->next();
              SMDSAbs_ElementType anElemType = anElem->GetType();
@@ -1220,6 +1456,8 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshe
                if ( nodesMap.find(aNode->GetID()) == nodesMap.end() ) {
                  aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
                  nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
+                 if( theCommonGroups )
+                   anIDsNodes[anNbNodes++] = aNewNode->GetID();
                }
                else
                  aNewNode = aNewMeshDS->FindNode( nodesMap.find(aNode->GetID())->second );
@@ -1235,6 +1473,8 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshe
                    aNewElem = aNewMeshDS->AddPolyhedralVolume(aNodesArray, 
                                                               aVolume->GetQuanities());
                    elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
+                   if( theCommonGroups )
+                     anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
                  }
                }
              else {
@@ -1243,6 +1483,14 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshe
                                                 anElemType,
                                                 anElem->IsPoly());
                elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
+               if( theCommonGroups ) {
+                 if( anElemType == SMDSAbs_Edge )
+                   anIDsEdges[anNbEdges++] = aNewElem->GetID();
+                 else if( anElemType == SMDSAbs_Face )
+                   anIDsFaces[anNbFaces++] = aNewElem->GetID();
+                 else if( anElemType == SMDSAbs_Volume )
+                   anIDsVolumes[anNbVolumes++] = aNewElem->GetID();
+               }
              } 
            }//elems loop
            
@@ -1253,12 +1501,73 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshe
            SMESH::long_array_var anInitIDs = new SMESH::long_array();
            SMESH::long_array_var anNewIDs = new SMESH::long_array();
            SMESH::SMESH_Group_var aNewGroup;
+
+           SMESH::ElementType aGroupType;
+           CORBA::String_var aGroupName;
+           if ( theCommonGroups ) {
+             for(aGroupType=SMESH::NODE;aGroupType<=SMESH::VOLUME;aGroupType=(SMESH::ElementType)(aGroupType+1)) {
+               string str = "Gr";
+               SALOMEDS::SObject_var aMeshSObj = ObjectToSObject( myCurrentStudy, anInitMesh );
+               if(aMeshSObj)
+                 str += aMeshSObj->GetName();
+               str += "_";
+
+               switch(aGroupType) {
+               case SMESH::NODE:
+                   str += "Nodes";
+                   anIDsNodes->length(anNbNodes++);
+                   break;
+               case SMESH::EDGE:
+                   str += "Edges";
+                   anIDsEdges->length(anNbEdges++);
+                   break;
+               case SMESH::FACE:
+                   str += "Faces";
+                   anIDsFaces->length(anNbFaces++);
+                   break;
+               case SMESH::VOLUME:
+                   str += "Volumes";
+                   anIDsVolumes->length(anNbVolumes++);
+                   break;
+               default:
+                   break;
+               }
+               
+               aGroupName = str.c_str();
+
+               // add a new group in the mesh
+               aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
+
+               switch(aGroupType) {
+               case SMESH::NODE:
+                   aNewGroup->Add( anIDsNodes );
+                   break;
+               case SMESH::EDGE:
+                   aNewGroup->Add( anIDsEdges );
+                   break;
+               case SMESH::FACE:
+                   aNewGroup->Add( anIDsFaces );
+                   break;
+               case SMESH::VOLUME:
+                   aNewGroup->Add( anIDsVolumes );
+                   break;
+               default:
+                   break;
+               }
+               
+               aListOfNewGroups.clear();
+               aListOfNewGroups.push_back(aNewGroup);
+               aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
+             }
+           }
+
+           // check that current group name and type don't have identical ones in union mesh
            for (int i = 0; i < aListOfGroups->length(); i++) {
              aGroup = aListOfGroups[i];
              aListOfNewGroups.clear();
-             SMESH::ElementType aGroupType = aGroup->GetType();
-             CORBA::String_var aGroupName = aGroup->GetName();
-             
+             aGroupType = aGroup->GetType();
+             aGroupName = aGroup->GetName();
+
              TGroupsMap::iterator anIter = aGroupsMap.find(make_pair(aGroupName, aGroupType));
 
              // convert a list of IDs
@@ -1325,7 +1634,11 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshe
   }
   
   // Update Python script
-  aPythonDump << aNewMesh << " = " << this << ".Concatenate(";
+  aPythonDump << aNewMesh << " = " << this;
+  if( !theCommonGroups )
+    aPythonDump << ".Concatenate(";
+  else
+    aPythonDump << ".ConcatenateWithGroups(";
   aPythonDump << "[";
   for ( int i = 0; i < theMeshesArray.length(); i++) {
     if (i > 0) aPythonDump << ", ";
@@ -1333,58 +1646,12 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Concatenate(const SMESH::mesh_array& theMeshe
   }
   aPythonDump << "], ";
   aPythonDump << theUniteIdenticalGroups << ", "
-              << theMergeNodesAndElements << ", "
+             << theMergeNodesAndElements << ", "
               << theMergeTolerance << ")";
 
   return aNewMesh._retn();
 }
 
-//================================================================================
-/*!
- * \brief Return geometrical object the given element is built on.
- *  \param theMesh - the mesh the element is in
- *  \param theElementID - the element ID
- *  \retval GEOM::GEOM_Object_ptr - the found geom object
- */
-//================================================================================
-
-GEOM::GEOM_Object_ptr
-SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
-                                   CORBA::Long            theElementID)
-  throw ( SALOME::SALOME_Exception )
-{
-  Unexpect aCatch(SALOME_SalomeException);
-  if ( CORBA::is_nil( theMesh ) )
-    THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
-
-  GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
-  GEOM::GEOM_Gen_var    geomGen   = GetGeomEngine();
-
-  // get a core mesh DS
-  SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
-  if ( meshServant && !geomGen->_is_nil() && !mainShape->_is_nil() )
-  {
-    ::SMESH_Mesh & mesh = meshServant->GetImpl();
-    SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
-    // find the element in mesh
-    if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) )
-      // find a shape id by the element
-      if ( int shapeID = ::SMESH_MeshEditor( &mesh ).FindShape( elem )) {
-        // get a geom object by the shape id
-        GEOM::GEOM_Object_var geom = ShapeToGeomObject( meshDS->IndexToShape( shapeID ));
-        if ( geom->_is_nil() ) {
-          GEOM::GEOM_IShapesOperations_var op =
-            geomGen->GetIShapesOperations( GetCurrentStudyID() );
-          if ( !op->_is_nil() )
-            geom = op->GetSubShape( mainShape, shapeID );
-        }
-        if ( !geom->_is_nil() )
-         return geom._retn();
-      }
-  }
-  return GEOM::GEOM_Object::_nil();
-}
-
 //=============================================================================
 /*!
  *  SMESH_Gen_i::Save
@@ -1409,10 +1676,10 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
   SavePython(myCurrentStudy);
 
   StudyContext* myStudyContext = GetCurrentStudyContext();
-  
+
   // Declare a byte stream
   SALOMEDS::TMPFile_var aStreamFile;
-  
+
   // Obtain a temporary dir
   TCollection_AsciiString tmpDir =
     ( isMultiFile ) ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
@@ -2446,6 +2713,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
       }
       // close hypotheses root HDF group
       aTopGroup->CloseOnDisk();
+      aTopGroup = 0;
     }
 
     // --> then we should read&create algorithms
@@ -2545,6 +2813,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
       }
       // close algorithms root HDF group
       aTopGroup->CloseOnDisk();
+      aTopGroup = 0;
     }
 
     // --> the rest groups should be meshes
@@ -3186,7 +3455,8 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
       }
     }
     // close mesh group
-    aTopGroup->CloseOnDisk();   
+    if(aTopGroup)
+      aTopGroup->CloseOnDisk();   
   }
   // close HDF file
   aFile->CloseOnDisk();
@@ -3228,12 +3498,29 @@ void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
 {
   if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Close" );
 
+  // set correct current study
+  if (theComponent->GetStudy()->StudyId() != GetCurrentStudyID())
+    SetCurrentStudy(theComponent->GetStudy());
+
   // Clear study contexts data
   int studyId = GetCurrentStudyID();
   if ( myStudyContextMap.find( studyId ) != myStudyContextMap.end() ) {
     delete myStudyContextMap[ studyId ];
     myStudyContextMap.erase( studyId );
   }
+
+  // delete SMESH_Mesh's
+  StudyContextStruct* context = myGen.GetStudyContext( studyId );
+  map< int, SMESH_Mesh* >::iterator i_mesh = context->mapMesh.begin();
+  for ( ; i_mesh != context->mapMesh.end(); ++i_mesh )
+    delete i_mesh->second;
+  // delete SMESHDS_Mesh's
+  // it's too long on big meshes
+//   if ( context->myDocument ) {
+//     delete context->myDocument;
+//     context->myDocument = 0;
+//   }
+  
   return;
 }