Salome HOME
Color Number (Color Group) parameter is returned for compatibility
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i.cxx
index 31ad919b62b3db1ebe743aff629f06a2c5e8ecbb..7d57ff91627cee4cfd92c22b0a7a1f9d54ce3a66 100644 (file)
@@ -93,6 +93,7 @@
 #include "SMDS_FacePosition.hxx"
 #include "SMDS_VertexPosition.hxx"
 #include "SMDS_SpacePosition.hxx"
+#include "SMDS_PolyhedralVolumeOfNodes.hxx"
 
 #include CORBA_SERVER_HEADER(SMESH_Group)
 #include CORBA_SERVER_HEADER(SMESH_Filter)
@@ -125,6 +126,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;
@@ -222,9 +224,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;
 }
 
 //=============================================================================
@@ -255,7 +263,7 @@ SMESH_Gen_i::SMESH_Gen_i( CORBA::ORB_ptr            orb,
                           const char*               interfaceName )
      : Engines_Component_i( orb, poa, contId, instanceName, interfaceName )
 {
-  INFOS( "SMESH_Gen_i::SMESH_Gen_i : standard constructor" );
+  MESSAGE( "SMESH_Gen_i::SMESH_Gen_i : standard constructor" );
 
   myOrb = CORBA::ORB::_duplicate(orb);
   myPoa = PortableServer::POA::_duplicate(poa);
@@ -478,6 +486,22 @@ 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 )
+void SMESH_Gen_i::SetGeomEngine( GEOM::GEOM_Gen_ptr geomcompo )
+{
+  //Engines::Component_ptr temp=GetLCC()->FindOrLoad_Component(containerLoc,"GEOM");
+  //myGeomGen=GEOM::GEOM_Gen::_narrow(temp);
+  myGeomGen=GEOM::GEOM_Gen::_duplicate(geomcompo);
+  //return myGeomGen;
+}
+
 //=============================================================================
 /*!
  *  SMESH_Gen_i::SetEmbeddedMode
@@ -956,6 +980,110 @@ 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 ) && theMesh->HasShapeToMesh())
+    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;
+      if(theMesh->HasShapeToMesh())
+        shape = GeomObjectToShape( theSubObject );
+      else
+        shape = SMESH_Mesh::PseudoShape();
+      
+      ::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
@@ -972,7 +1100,7 @@ SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMes
   Unexpect aCatch(SALOME_SalomeException);
   if(MYDEBUG) MESSAGE( "SMESH_Gen_i::GetAlgoState()" );
 
-  if ( CORBA::is_nil( theSubObject ) )
+  if ( CORBA::is_nil( theSubObject ) && theMesh->HasShapeToMesh())
     THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", SALOME::BAD_PARAM );
 
   if ( CORBA::is_nil( theMesh ) )
@@ -983,7 +1111,12 @@ SMESH::algo_error_array* SMESH_Gen_i::GetAlgoState( SMESH::SMESH_Mesh_ptr theMes
     SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
     ASSERT( meshServant );
     if ( meshServant ) {
-      TopoDS_Shape myLocShape = GeomObjectToShape( theSubObject );
+      TopoDS_Shape myLocShape;
+      if(theMesh->HasShapeToMesh())
+        myLocShape = GeomObjectToShape( theSubObject );
+      else
+        myLocShape = SMESH_Mesh::PseudoShape();
+      
       ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
       list< ::SMESH_Gen::TAlgoStateError > error_list;
       list< ::SMESH_Gen::TAlgoStateError >::iterator error;
@@ -993,55 +1126,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 = CORBA::string_dup( 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();
       }
     }
   }
@@ -1141,7 +1234,7 @@ CORBA::Boolean SMESH_Gen_i::Compute( SMESH::SMESH_Mesh_ptr theMesh,
   Unexpect aCatch(SALOME_SalomeException);
   if(MYDEBUG) MESSAGE( "SMESH_Gen_i::Compute" );
 
-  if ( CORBA::is_nil( theShapeObject ) )
+  if ( CORBA::is_nil( theShapeObject ) && theMesh->HasShapeToMesh())
     THROW_SALOME_CORBA_EXCEPTION( "bad shape object reference", 
                                   SALOME::BAD_PARAM );
 
@@ -1152,23 +1245,27 @@ 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 );
+      TopoDS_Shape myLocShape;
+      if(theMesh->HasShapeToMesh())
+        myLocShape = GeomObjectToShape( theShapeObject );
+      else
+        myLocShape = SMESH_Mesh::PseudoShape();
       // call implementation compute
       ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
       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() );
@@ -1200,14 +1297,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();
@@ -1215,7 +1335,6 @@ SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
   return GEOM::GEOM_Object::_nil();
 }
 
-
 //================================================================================
 /*!
  * \brief Return geometrical object the given element is built on.
@@ -1227,7 +1346,7 @@ SMESH_Gen_i::GetGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
 
 GEOM::GEOM_Object_ptr
 SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
-                                   CORBA::Long            theElementID)
+                                        CORBA::Long            theElementID)
   throw ( SALOME::SALOME_Exception )
 {
   Unexpect aCatch(SALOME_SalomeException);
@@ -1235,7 +1354,7 @@ SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
     THROW_SALOME_CORBA_EXCEPTION( "bad Mesh reference", SALOME::BAD_PARAM );
 
   GEOM::GEOM_Object_var mainShape = theMesh->GetShapeToMesh();
-  GEOM::GEOM_Gen_var    geomGen   = GetGeomEngine();
+  GEOM::GEOM_Gen_ptr    geomGen   = GetGeomEngine();
 
   // get a core mesh DS
   SMESH_Mesh_i* meshServant = SMESH::DownCast<SMESH_Mesh_i*>( theMesh );
@@ -1244,24 +1363,236 @@ SMESH_Gen_i::FindGeometryByMeshElement( SMESH::SMESH_Mesh_ptr  theMesh,
     ::SMESH_Mesh & mesh = meshServant->GetImpl();
     SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
     // find the element in mesh
-    if ( const SMDS_MeshElement * elem = meshDS->FindElement( theElementID ) )
+    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() )
+        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
+ *
+ *  Concatenate the given meshes into one mesh
+ */
+//================================================================================
+
+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 )
+{
+  typedef map<int, int> TIDsMap;
+  typedef list<SMESH::SMESH_Group_var> TListOfNewGroups;
+  typedef map< pair<string, SMESH::ElementType>, TListOfNewGroups > TGroupsMap;
+  typedef std::set<SMESHDS_GroupBase*> TGroups;
+
+  TPythonDump aPythonDump; // prevent dump of called methods
+
+  // create mesh
+  SMESH::SMESH_Mesh_var aNewMesh = CreateEmptyMesh();
+  
+  if ( !aNewMesh->_is_nil() ) {
+    SMESH_Mesh_i* aNewImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( aNewMesh ).in() );
+    if ( aNewImpl ) {
+      ::SMESH_Mesh& aLocMesh = aNewImpl->GetImpl();
+      SMESHDS_Mesh* aNewMeshDS = aLocMesh.GetMeshDS();
+
+      TGroupsMap aGroupsMap;
+      TListOfNewGroups aListOfNewGroups;
+      SMESH_MeshEditor aNewEditor = ::SMESH_MeshEditor(&aLocMesh);
+      SMESH::ListOfGroups_var aListOfGroups = new SMESH::ListOfGroups();
+
+      // loop on meshes
+      for ( int i = 0; i < theMeshesArray.length(); i++) {
+       SMESH::SMESH_Mesh_var anInitMesh = theMeshesArray[i];
+       if ( !anInitMesh->_is_nil() ) {
+         SMESH_Mesh_i* anInitImpl = dynamic_cast<SMESH_Mesh_i*>( GetServant( anInitMesh ).in() );
+         if ( anInitImpl ) {
+           ::SMESH_Mesh& aInitLocMesh = anInitImpl->GetImpl();
+           SMESHDS_Mesh* anInitMeshDS = aInitLocMesh.GetMeshDS();
+
+           TIDsMap nodesMap;
+           TIDsMap elemsMap;
+
+           // loop on elements of mesh
+           SMDS_ElemIteratorPtr itElems = anInitMeshDS->elementsIterator();
+           const SMDS_MeshElement* anElem = 0;
+           const SMDS_MeshElement* aNewElem = 0;
+           int anElemNbNodes = 0;
+
+           for ( int j = 0; itElems->more(); j++) {
+             anElem = itElems->next();
+             SMDSAbs_ElementType anElemType = anElem->GetType();
+             anElemNbNodes = anElem->NbNodes();
+             std::vector<const SMDS_MeshNode*> aNodesArray (anElemNbNodes);
+
+             // loop on nodes of element
+             const SMDS_MeshNode* aNode = 0;
+             const SMDS_MeshNode* aNewNode = 0;
+             SMDS_ElemIteratorPtr itNodes = anElem->nodesIterator();
+
+             for ( int k = 0; itNodes->more(); k++) {
+               aNode = static_cast<const SMDS_MeshNode*>(itNodes->next());
+               if ( nodesMap.find(aNode->GetID()) == nodesMap.end() ) {
+                 aNewNode = aNewMeshDS->AddNode(aNode->X(), aNode->Y(), aNode->Z());
+                 nodesMap.insert( make_pair(aNode->GetID(), aNewNode->GetID()) );
+               }
+               else
+                 aNewNode = aNewMeshDS->FindNode( nodesMap.find(aNode->GetID())->second );
+               aNodesArray[k] = aNewNode;
+             }//nodes loop
+
+             // creates a corresponding element on existent nodes in new mesh
+             if ( anElem->IsPoly() && anElemType == SMDSAbs_Volume )
+               {
+                 const SMDS_PolyhedralVolumeOfNodes* aVolume =
+                   dynamic_cast<const SMDS_PolyhedralVolumeOfNodes*> (anElem);
+                 if ( aVolume ) {
+                   aNewElem = aNewMeshDS->AddPolyhedralVolume(aNodesArray, 
+                                                              aVolume->GetQuanities());
+                   elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
+                 }
+               }
+             else {
+               
+               aNewElem = aNewEditor.AddElement(aNodesArray,
+                                                anElemType,
+                                                anElem->IsPoly());
+               elemsMap.insert(make_pair(anElem->GetID(), aNewElem->GetID()));
+             } 
+           }//elems loop
+           
+           aListOfGroups = anInitImpl->GetGroups();
+           SMESH::SMESH_GroupBase_ptr aGroup;
+
+           // loop on groups of mesh
+           SMESH::long_array_var anInitIDs = new SMESH::long_array();
+           SMESH::long_array_var anNewIDs = new SMESH::long_array();
+           SMESH::SMESH_Group_var aNewGroup;
+           for (int i = 0; i < aListOfGroups->length(); i++) {
+             aGroup = aListOfGroups[i];
+             aListOfNewGroups.clear();
+             SMESH::ElementType aGroupType = aGroup->GetType();
+             CORBA::String_var aGroupName = aGroup->GetName();
+             
+             TGroupsMap::iterator anIter = aGroupsMap.find(make_pair(aGroupName, aGroupType));
+
+             // convert a list of IDs
+             anInitIDs = aGroup->GetListOfID();
+             anNewIDs->length(anInitIDs->length());
+             if ( aGroupType == SMESH::NODE )
+               for (int j = 0; j < anInitIDs->length(); j++) {
+                 anNewIDs[j] = nodesMap.find(anInitIDs[j])->second;
+               }
+             else
+               for (int j = 0; j < anInitIDs->length(); j++) {
+                 anNewIDs[j] = elemsMap.find(anInitIDs[j])->second;
+               }
+             
+             // check that current group name and type don't have identical ones in union mesh
+             if ( anIter == aGroupsMap.end() ) {
+               // add a new group in the mesh
+               aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
+               // add elements into new group
+               aNewGroup->Add( anNewIDs );
+               
+               aListOfNewGroups.push_back(aNewGroup);
+               aGroupsMap.insert(make_pair( make_pair(aGroupName, aGroupType), aListOfNewGroups ));
+             }
+
+             else if ( theUniteIdenticalGroups ) {
+               // unite identical groups
+               TListOfNewGroups& aNewGroups = anIter->second;
+               aNewGroups.front()->Add( anNewIDs );
+             }
+
+             else {
+               // rename identical groups
+               aNewGroup = aNewImpl->CreateGroup(aGroupType, aGroupName);
+               aNewGroup->Add( anNewIDs );
+               
+               TListOfNewGroups& aNewGroups = anIter->second;
+               string aNewGroupName;
+               if (aNewGroups.size() == 1) {
+                 aNewGroupName = string(aGroupName) + "_1";
+                 aNewGroups.front()->SetName(aNewGroupName.c_str());
+               }
+               char aGroupNum[128];
+               sprintf(aGroupNum, "%u", aNewGroups.size()+1);
+               aNewGroupName = string(aGroupName) + "_" + string(aGroupNum);
+               aNewGroup->SetName(aNewGroupName.c_str());
+               aNewGroups.push_back(aNewGroup);
+             }
+           }//groups loop
+         }
+       }
+      }//meshes loop
+
+      if (theMergeNodesAndElements) {
+       // merge nodes
+       set<const SMDS_MeshNode*> aMeshNodes; // no input nodes
+       SMESH_MeshEditor::TListOfListOfNodes aGroupsOfNodes;
+       aNewEditor.FindCoincidentNodes( aMeshNodes, theMergeTolerance, aGroupsOfNodes );
+       aNewEditor.MergeNodes( aGroupsOfNodes );
+       // merge elements
+       aNewEditor.MergeEqualElements();
+      }
+    }
+  }
+  
+  // Update Python script
+  aPythonDump << aNewMesh << " = " << this << ".Concatenate(";
+  aPythonDump << "[";
+  for ( int i = 0; i < theMeshesArray.length(); i++) {
+    if (i > 0) aPythonDump << ", ";
+    aPythonDump << theMeshesArray[i];
+  }
+  aPythonDump << "], ";
+  aPythonDump << theUniteIdenticalGroups << ", "
+              << theMergeNodesAndElements << ", "
+              << theMergeTolerance << ")";
+
+  return aNewMesh._retn();
+}
+
 //=============================================================================
 /*!
  *  SMESH_Gen_i::Save
@@ -1490,6 +1821,7 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
             int id = myStudyContext->findId( string( objStr.in() ) );
             ::SMESH_Mesh& myLocMesh = myImpl->GetImpl();
             SMESHDS_Mesh* mySMESHDSMesh = myLocMesh.GetMeshDS();
+            bool hasShape = myLocMesh.HasShapeToMesh();
 
             // for each mesh open the HDF group basing on its id
             char meshGrpName[ 30 ];
@@ -1512,6 +1844,18 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
             aDataset->WriteOnDisk( ( char* )( strHasData.c_str() ) );
             aDataset->CloseOnDisk();
 
+           // ouv : NPAL12872
+            // for each mesh open the HDF group basing on its auto color parameter
+           char meshAutoColorName[ 30 ];
+           sprintf( meshAutoColorName, "AutoColorMesh %d", id );
+           int anAutoColor[1];
+           anAutoColor[0] = myImpl->GetAutoColor();
+           aSize[ 0 ] = 1;
+           aDataset = new HDFdataset( meshAutoColorName, aTopGroup, HDF_INT32, aSize, 1 );
+           aDataset->CreateOnDisk();
+           aDataset->WriteOnDisk( anAutoColor );
+           aDataset->CloseOnDisk();
+
             // write reference on a shape if exists
             SALOMEDS::SObject_var myRef;
             bool shapeRefFound = false;
@@ -1535,10 +1879,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 && !shapeRefFound ) { // remove applied hyps
+            if ( found && !shapeRefFound && hasShape) { // remove applied hyps
               myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myHypBranch );
             }
-            if ( found && shapeRefFound ) {
+            if ( found && (shapeRefFound || !hasShape) ) {
               aGroup = new HDFgroup( "Applied Hypotheses", aTopGroup );
               aGroup->CreateOnDisk();
 
@@ -1578,10 +1922,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 && !shapeRefFound ) { // remove applied algos
+            if ( found && !shapeRefFound && hasShape) { // remove applied algos
               myCurrentStudy->NewBuilder()->RemoveObjectWithChildren( myAlgoBranch );
             }
-            if ( found && shapeRefFound ) {
+            if ( found && (shapeRefFound || !hasShape)) {
               aGroup = new HDFgroup( "Applied Algorithms", aTopGroup );
               aGroup->CreateOnDisk();
 
@@ -1837,6 +2181,22 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
                    aDataset->WriteOnDisk( aUserName );
                    aDataset->CloseOnDisk();
 
+                   // ouv : NPAL12872
+                   // For each group, create a dataset named "Group <group_persistent_id> Color"
+                   // and store the group's color into it
+                   char grpColorName[ 30 ];
+                   sprintf( grpColorName, "ColorGroup %d", anId );
+                   SALOMEDS::Color aColor = myGroupImpl->GetColor();
+                   double anRGB[3];
+                   anRGB[ 0 ] = aColor.R;
+                   anRGB[ 1 ] = aColor.G;
+                   anRGB[ 2 ] = aColor.B;
+                   aSize[ 0 ] = 3;
+                   aDataset = new HDFdataset( grpColorName, aGroup, HDF_FLOAT64, aSize, 1 );
+                   aDataset->CreateOnDisk();
+                   aDataset->WriteOnDisk( anRGB );
+                   aDataset->CloseOnDisk();
+
                    // Store the group contents into MED file
                    if ( myLocMesh.GetGroup( myGroupImpl->GetLocalID() ) ) {
                      
@@ -1890,7 +2250,7 @@ SALOMEDS::TMPFile* SMESH_Gen_i::Save( SALOMEDS::SComponent_ptr theComponent,
              myWriter.Perform();
              
              // maybe a shape was deleted in the study
-             if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() ) {
+             if ( !shapeRefFound && !mySMESHDSMesh->ShapeToMesh().IsNull() && hasShape) {
                TopoDS_Shape nullShape;
                myLocMesh.ShapeToMesh( nullShape ); // remove shape referring data
              }
@@ -2132,6 +2492,7 @@ SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII( SALOMEDS::SComponent_ptr theComponent
                                           bool                     isMultiFile ) {
   if(MYDEBUG) MESSAGE( "SMESH_Gen_i::SaveASCII" );
   SALOMEDS::TMPFile_var aStreamFile = Save( theComponent, theURL, isMultiFile );
+  return aStreamFile._retn();
 
   //after usual saving needs to encipher binary to text string
   //Any binary symbol will be represent as "|xx" () hexadecimal format number
@@ -2224,6 +2585,11 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
   // Get temporary files location
   TCollection_AsciiString tmpDir =
     isMultiFile ? TCollection_AsciiString( ( char* )theURL ) : ( char* )SALOMEDS_Tool::GetTmpDir().c_str();
+    
+    INFOS( "THE URL++++++++++++++" )
+    INFOS( theURL );
+    INFOS( "THE TMP PATH+++++++++" );
+    INFOS( tmpDir );
 
   // Convert the stream into sequence of files to process
   SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles( theStream,
@@ -2371,6 +2737,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
@@ -2470,6 +2837,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
@@ -2503,6 +2871,21 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
          int newId = myStudyContext->findId( iorString );
          myStudyContext->mapOldToNew( id, newId );
 
+         // ouv : NPAL12872
+         // try to read and set auto color flag
+         char aMeshAutoColorName[ 30 ];
+         sprintf( aMeshAutoColorName, "AutoColorMesh %d", id);
+         if( aTopGroup->ExistInternalObject( aMeshAutoColorName ) )
+         {
+           aDataset = new HDFdataset( aMeshAutoColorName, aTopGroup );
+           aDataset->OpenOnDisk();
+           size = aDataset->GetSize();
+           int* anAutoColor = new int[ size ];
+           aDataset->ReadFromDisk( anAutoColor );
+           aDataset->CloseOnDisk();
+           myNewMeshImpl->SetAutoColor( (bool)anAutoColor[0] );
+         }
+
           // try to read and set reference to shape
           GEOM::GEOM_Object_var aShapeObject;
           if ( aTopGroup->ExistInternalObject( "Ref on shape" ) ) {
@@ -2608,7 +2991,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() && !aShapeObject->_is_nil() )
+                if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
+                                           || !myNewMeshImpl->HasShapeToMesh()) )
                   myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
               }
             }
@@ -2645,7 +3029,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() && !aShapeObject->_is_nil() )
+                if ( !anHyp->_is_nil() && (!aShapeObject->_is_nil()
+                                           || !myNewMeshImpl->HasShapeToMesh()) )
                   myNewMeshImpl->addHypothesis( aShapeObject, anHyp );
               }
             }
@@ -2862,7 +3247,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
               aDataset->CloseOnDisk();
 
               // get elements sorted by ID
-              ::SMESH_MeshEditor::TIDSortedElemSet elemSet;
+              TIDSortedElemSet elemSet;
               if ( isNode )
                 while ( nIt->more() ) elemSet.insert( nIt->next() );
               else
@@ -2870,7 +3255,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
               ASSERT( elemSet.size() == nbElems );
 
               // add elements to submeshes
-              ::SMESH_MeshEditor::TIDSortedElemSet::iterator iE = elemSet.begin();
+              TIDSortedElemSet::iterator iE = elemSet.begin();
               for ( int i = 0; i < nbElems; ++i, ++iE )
               {
                 int smID = smIDs[ i ];
@@ -2894,7 +3279,7 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
                   sm->AddElement( elem );
                 }
               }
-              delete smIDs;
+              delete [] smIDs;
             }
           }
         } // end reading submeshes
@@ -3011,9 +3396,14 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
       } // if ( hasData )
 
       // Recompute State (as computed sub-meshes are restored from MED)
-      if ( !aShapeObject->_is_nil() ) {
+      if ( !aShapeObject->_is_nil() || !myNewMeshImpl->HasShapeToMesh()) {
         MESSAGE("Compute State Engine ...");
-        TopoDS_Shape myLocShape = GeomObjectToShape( aShapeObject );
+        TopoDS_Shape myLocShape;
+        if(myNewMeshImpl->HasShapeToMesh())
+          myLocShape = GeomObjectToShape( aShapeObject );
+        else
+          myLocShape = SMESH_Mesh::PseudoShape();
+        
         myNewMeshImpl->GetImpl().GetSubMesh(myLocShape)->ComputeStateEngine
           (SMESH_subMesh::SUBMESH_RESTORED);
         MESSAGE("Compute State Engine finished");
@@ -3100,6 +3490,22 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
               SMESHDS_GroupBase* aGroupBaseDS = aLocalGroup->GetGroupDS();
               aGroupBaseDS->SetStoreName( name_dataset );
 
+             // ouv : NPAL12872
+             // Read color of the group
+              char aGroupColorName[ 30 ];
+              sprintf( aGroupColorName, "ColorGroup %d", subid);
+              if ( aGroup->ExistInternalObject( aGroupColorName ) )
+             {
+               aDataset = new HDFdataset( aGroupColorName, aGroup );
+               aDataset->OpenOnDisk();
+               size = aDataset->GetSize();
+               double* anRGB = new double[ size ];
+               aDataset->ReadFromDisk( anRGB );
+               aDataset->CloseOnDisk();
+               Quantity_Color aColor( anRGB[0], anRGB[1], anRGB[2], Quantity_TOC_RGB );
+               aGroupBaseDS->SetColor( aColor );
+             }
+
               // Fill group with contents from MED file
               SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( aGroupBaseDS );
               if ( aGrp )
@@ -3111,7 +3517,8 @@ bool SMESH_Gen_i::Load( SALOMEDS::SComponent_ptr theComponent,
       }
     }
     // close mesh group
-    aTopGroup->CloseOnDisk();   
+    if(aTopGroup)
+      aTopGroup->CloseOnDisk();   
   }
   // close HDF file
   aFile->CloseOnDisk();
@@ -3138,6 +3545,7 @@ bool SMESH_Gen_i::LoadASCII( SALOMEDS::SComponent_ptr theComponent,
                             const char*              theURL,
                             bool                     isMultiFile ) {
   if(MYDEBUG) MESSAGE( "SMESH_Gen_i::LoadASCII" );
+  return Load( theComponent, theStream, theURL, isMultiFile );
 
   //before call main ::Load method it's need for decipher text format to
   //binary ( "|xx" => x' )
@@ -3181,6 +3589,19 @@ void SMESH_Gen_i::Close( SALOMEDS::SComponent_ptr theComponent )
     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;
 }