Back to index

salome-med  6.5.0
MEDMEM_Med_i.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //=============================================================================
00024 // File      : MEDMEM_Med_i.cxx
00025 // Project   : SALOME
00026 // Author    : EDF
00027 // $Header: /export/home/PAL/MED_SRC/src/MEDMEM_I/MEDMEM_i.cxx
00028 //=============================================================================
00029 //
00030 #include <deque>
00031 
00032 //#include "MEDMEM_Field.hxx"
00033 #include "MEDMEM_Med_i.hxx"
00034 #include "MEDMEM_Mesh_i.hxx"
00035 #include "MEDMEM_FieldTemplate_i.hxx"
00036 #include "MEDMEM_Support_i.hxx"
00037 #include "MEDMEM_Family_i.hxx"
00038 #include "MEDMEM_Group_i.hxx"
00039 #include "MEDMEM_convert.hxx"
00040 
00041 #include "MEDMEM_Family.hxx"
00042 #include "MEDMEM_Group.hxx"
00043 #include "MEDMEM_Mesh.hxx"
00044 #include "MEDMEM_Grid.hxx"
00045 
00046 #include "MEDMEM_DriversDef.hxx"
00047 #include "utilities.h"
00048 #include "Utils_CorbaException.hxx"
00049 
00050 using namespace std;
00051 using namespace MEDMEM;
00052 
00053 //=============================================================================
00057 //=============================================================================
00058 MED_i::MED_i()
00059 {
00060 }
00061 
00062 //=======================================================================
00063 //function : init
00064 //purpose  : * methods
00065 //=======================================================================
00066 
00067 void MED_i::init(SALOMEDS::Study_ptr myStudy,driverTypes /*driverType*/, const string & fileName)
00068 {
00069 
00070   // we create all IOR from _med
00071   _med.readFileStruct(fileName);
00072 
00073   // MESHES :
00074   vector<string> meshesNames = _med.getMeshNames();
00075   for (int i=0; i<(int)meshesNames.size(); i++) 
00076   {
00077     const MESH * myMesh = 0;
00078     if ( _med.isStructuredMesh( meshesNames[i] ))
00079       myMesh = GRID( MED_DRIVER, fileName, meshesNames[i] ).convertInMESH();
00080     else
00081       myMesh = new MESH( MED_DRIVER, fileName, meshesNames[i] );
00082     MESH_i * myMeshI = new MESH_i(const_cast<MESH*>( myMesh ));
00083     SALOME_MED::MESH_ptr myMeshIOR = myMeshI->_this();
00084     _meshes[meshesNames[i]] = myMeshIOR;
00085     _medmem_meshes[meshesNames[i]] = const_cast<MESH*>( myMesh );
00086   }
00087 
00088   // FAMILIES :
00089   // we add all families 
00090   vector<FAMILY*> familyVector;
00091   vector<FAMILY*>::iterator familyVectorIt;
00092   // GROUPS :
00093   // we add all groups
00094   vector<GROUP*> groupVector;
00095   vector<GROUP*>::iterator groupVectorIt;
00096   
00097   MED_EN::MESH_ENTITIES::const_iterator currentEntity; 
00098 
00099   // FIELDS :
00100   vector<string> fieldsNames = _med.getFieldNames();
00101   for (int i=0; i<(int)fieldsNames.size(); i++) 
00102   {
00103     string meshName = _med.getMeshName( fieldsNames[i] );
00104     map<string, GMESH* >::const_iterator name_mesh = _medmem_meshes.find( meshName );
00105     GMESH* mesh = name_mesh == _medmem_meshes.end() ? (MESH*) 0 : name_mesh->second;
00106     med_type_champ type = _med.getFieldType(fieldsNames[i]);
00107     VEC_DT_IT_ myIteration = _med.getFieldIteration (fieldsNames[i]);
00108     for (int j=0; j<(int)myIteration.size(); j++) 
00109     {
00110       SALOME_MED::FIELD_ptr myFieldIOR;
00111       switch (type) 
00112       {
00113       case MED_EN::MED_INT32 : 
00114         {
00115           FIELD<int>* myField = new FIELD<int>( MED_DRIVER, fileName, fieldsNames[i],
00116                                                 myIteration[j].dt, myIteration[j].it,
00117                                                 mesh);
00118           FIELDTEMPLATE_I<int> *myFieldIntI = new FIELDTEMPLATE_I<int>(myField);
00119           myFieldIOR = myFieldIntI->_this();
00120           break;
00121         }
00122 
00123       case MED_EN::MED_REEL64: 
00124         {
00125           FIELD<double>* myField = new FIELD<double>( MED_DRIVER, fileName, fieldsNames[i],
00126                                                       myIteration[j].dt, myIteration[j].it,
00127                                                       mesh);
00128           FIELDTEMPLATE_I<double> *myFieldDoubleI = new FIELDTEMPLATE_I<double>(myField);
00129           myFieldIOR = myFieldDoubleI->_this();
00130           break;
00131         }
00132       default:
00133         {
00134           ostringstream stream;
00135           stream << "MED_i::init(driverTypes, const string &)"
00136                  <<"   * Iteration "<<myIteration[j].dt
00137                  <<", order number "<<myIteration[j].it
00138                  <<" has wrong type : "<<type;
00139           throw MED_EXCEPTION(stream.str().c_str());
00140           break;
00141         }
00142       }
00143 
00144       DT_IT_ dtIt;
00145       dtIt.dt  = myIteration[j].dt;
00146       dtIt.it  = myIteration[j].it;
00147       _fields[fieldsNames[i]][dtIt] = myFieldIOR;
00148     }
00149   }
00150 }
00151 
00152 //=============================================================================
00156 //=============================================================================
00157 void MED_i::initWithFieldType(SALOMEDS::Study_ptr myStudy,driverTypes /*driverType*/, const string & fileName, bool persistence)
00158 {
00159   // if (persistence):
00160   //    some objects can be not published
00161 
00162   // we create all IOR from _med
00163   _med.readFileStruct(fileName);
00164 
00165   // MESHES :
00166   vector<string> meshesNames = _med.getMeshNames();
00167   for (int i=0; i<(int)meshesNames.size(); i++) 
00168   {
00169     const MESH * myMesh = 0;
00170     if ( _med.isStructuredMesh( meshesNames[i] ))
00171       myMesh = GRID( MED_DRIVER, fileName, meshesNames[i] ).convertInMESH();
00172     else
00173       myMesh = new MESH( MED_DRIVER, fileName, meshesNames[i] );
00174     MESH_i * myMeshI = new MESH_i(const_cast<MESH*>( myMesh ));
00175     SALOME_MED::MESH_ptr myMeshIOR = myMeshI->_this();
00176     _meshes[meshesNames[i]] = myMeshIOR;
00177     _medmem_meshes[meshesNames[i]] = const_cast<MESH*>( myMesh );
00178     if ( !persistence )
00179       myMeshI->addInStudy(myStudy,myMeshIOR);
00180   }
00181 
00182   if ( !persistence )
00183   {
00184     // FAMILIES :
00185     // we add all families in the study
00186     vector<FAMILY*> familyVector;
00187     vector<FAMILY*>::iterator familyVectorIt;
00188     // GROUPS :
00189     // we add all groups in the study
00190     vector<GROUP*> groupVector;
00191     vector<GROUP*>::iterator groupVectorIt;
00192 
00193     MED_EN::MESH_ENTITIES::const_iterator currentEntity; 
00194     for (int i=0; i<(int)meshesNames.size(); i++) 
00195     {
00196       ::GMESH * ptrMesh = _medmem_meshes[meshesNames[i]];
00197 
00198       SCRUTE(ptrMesh);
00199 
00200       for (currentEntity = MED_EN::meshEntities.begin();
00201            currentEntity != MED_EN::meshEntities.end(); 
00202            currentEntity++)
00203       {
00204         MESSAGE("initWithFieldType: for entity " << (*currentEntity).first);
00205         // family :
00206         familyVector = ptrMesh->getFamilies((MED_EN::medEntityMesh)(*currentEntity).first);
00207         int nb = familyVector.size();
00208         MESSAGE("initWithFieldType: there is(are) " << nb << " family(ies)");
00209         for (familyVectorIt = familyVector.begin();
00210              familyVectorIt != familyVector.end();
00211              familyVectorIt++)
00212         {
00213           FAMILY_i * myFamilyI = new FAMILY_i(*familyVectorIt);
00214           SALOME_MED::FAMILY_ptr myFamilyIOR = myFamilyI->_this();
00215           myFamilyI->addInStudy(myStudy,myFamilyIOR);
00216         }
00217 
00218         // group :
00219         groupVector = ptrMesh->getGroups((MED_EN::medEntityMesh)(*currentEntity).first);
00220         nb = groupVector.size();
00221         MESSAGE("initWithFieldType: there is(are) " << nb << " group(s)");
00222         for (groupVectorIt = groupVector.begin();
00223              groupVectorIt != groupVector.end();
00224              groupVectorIt++)
00225         {
00226           GROUP_i * myGroupI = new GROUP_i(*groupVectorIt);
00227           SALOME_MED::GROUP_ptr myGroupIOR = myGroupI->_this();
00228           myGroupI->addInStudy(myStudy,myGroupIOR);
00229         }
00230       }
00231     }
00232   }
00233 
00234   // SUPPORTS on All:
00235 
00236   map<string, set<string> > publishedSupportsByMesh;
00237 
00238   map<string, ::MEDMEM::GMESH* >::const_iterator name2mesh = _medmem_meshes.begin();
00239   for ( ; name2mesh != _medmem_meshes.end(); ++name2mesh )
00240     {
00241       ::MEDMEM::GMESH* mesh = name2mesh->second;
00242       for ( medEntityMesh entity = 0; entity < MED_ALL_ENTITIES; ++entity )
00243         {
00244           if ( mesh->getNumberOfElements( entity, MED_ALL_ELEMENTS ) > 0 )
00245             {
00246               const ::MEDMEM::SUPPORT* sup = mesh->getSupportOnAll( entity );
00247               SUPPORT_i * mySupportI = new SUPPORT_i( sup );
00248               SALOME_MED::SUPPORT_ptr mySupportIOR = mySupportI->_this();
00249               try // mySupportI->addInStudy() may throw
00250               {
00251                 mySupportI->addInStudy(myStudy,mySupportIOR);
00252                 publishedSupportsByMesh[ mesh->getName() ].insert(sup->getName());
00253               }
00254               catch (...)
00255               {
00256                 mySupportIOR->UnRegister();
00257               }
00258             }
00259         }
00260     }
00261 
00262   // FIELDS :
00263 
00264   vector<string> fieldsNames = _med.getFieldNames();
00265   for (int i=0; i<(int)fieldsNames.size(); i++) 
00266   {
00267     string meshName = _med.getMeshName( fieldsNames[i] );
00268     map<string, GMESH* >::const_iterator name_mesh = _medmem_meshes.find( meshName );
00269     GMESH* mesh = name_mesh == _medmem_meshes.end() ? (GMESH*) 0 : name_mesh->second;
00270 
00271     med_type_champ type = _med.getFieldType(fieldsNames[i]);
00272     VEC_DT_IT_ myIteration = _med.getFieldIteration (fieldsNames[i]);
00273 
00274     for (int j=0; j<(int)myIteration.size(); j++) 
00275     {
00276       SALOME_MED::FIELD_ptr myFieldIOR;
00277       ::FIELD_ * myField = 0;
00278       switch (type) 
00279       {
00280       case MED_EN::MED_INT32 : 
00281         {
00282           FIELD<int>* myIntField = new FIELD<int>( MED_DRIVER, fileName, fieldsNames[i],
00283                                                    myIteration[j].dt, myIteration[j].it,
00284                                                    mesh);
00285           FIELDTEMPLATE_I<int> *myFieldIntI = new FIELDTEMPLATE_I<int>(myIntField);
00286           myFieldIOR = myFieldIntI->_this();
00287           myField = myIntField;
00288           break;
00289         }
00290 
00291       case MED_EN::MED_REEL64: 
00292         {
00293           FIELD<double>* myDblField = new FIELD<double>(MED_DRIVER, fileName, fieldsNames[i],
00294                                                         myIteration[j].dt, myIteration[j].it,
00295                                                         mesh);
00296           FIELDTEMPLATE_I<double> *myFieldDoubleI = new FIELDTEMPLATE_I<double>(myDblField);
00297           myFieldIOR = myFieldDoubleI->_this();
00298           myField = myDblField;
00299           break;
00300         }
00301       default:
00302         {
00303           ostringstream stream;
00304           stream << "MED_i::init(driverTypes, const string &)"
00305                  <<"   * Iteration "<<myIteration[j].dt
00306                  <<", order number "<<myIteration[j].it
00307                  <<" has wrong type : "<<type;
00308           throw MED_EXCEPTION(stream.str().c_str());
00309           break;
00310         }
00311       }
00312 
00313       // check if corresponding support exists in the study
00314       if ( !persistence )
00315       {
00316         string supportName = myField->getSupport()->getName();
00317         bool supportExists = !publishedSupportsByMesh[ meshName ].insert(supportName).second;
00318         if ( !supportExists )
00319         {
00320           SUPPORT_i * mySupportI = new SUPPORT_i( myField->getSupport() );
00321           SALOME_MED::SUPPORT_ptr mySupportIOR = mySupportI->_this();
00322           mySupportI->addInStudy(myStudy,mySupportIOR);
00323         }
00324       }
00325       DT_IT_ dtIt;
00326       dtIt.dt  = myIteration[j].dt;
00327       dtIt.it  = myIteration[j].it;
00328       _fields[fieldsNames[i]][dtIt] = myFieldIOR;
00329       if ( !persistence )
00330         myFieldIOR->addInStudy(myStudy,myFieldIOR);
00331     }
00332   }
00333 }
00334 
00335 //=======================================================================
00336 //function : getSupport
00337 //purpose  : * \brief Return support
00338 //           * \param meshName - name of the mesh the suppotr belongs to
00339 //           * \param entity - support entity
00340 //           * \retval SALOME_MED::SUPPORT_ptr - found support
00341 //           * Raises if support not found
00342 //=======================================================================
00343 
00344 SALOME_MED::SUPPORT_ptr MED_i::getSupport(string                meshName,
00345                                           MED_EN::medEntityMesh entity,
00346                                           const string          name)
00347   throw (SALOME::SALOME_Exception)
00348 {
00349   // try to find a support of field by a name of support
00350 
00351   const ::MEDMEM::SUPPORT* support = 0;
00352   if ( !name.empty() )
00353   {
00354     for ( int i = 0; !support && i < (int)_medmem_fields.size(); ++i )
00355       if ( name     == _medmem_fields[i]->getSupport()->getName() &&
00356            meshName == _medmem_fields[i]->getSupport()->getMeshName())
00357         support = _medmem_fields[i]->getSupport();
00358     if ( !support )
00359       throw MED_EXCEPTION(STRING("MED_i::getSupport(meshName, entity, supportName) : ")
00360                           << "There is no support named |"<< name<<"|"
00361                           << " on mesh named |" << meshName << "|");
00362   }
00363 
00364   if ( !support )
00365   {
00366     // get support on all elements of given entity
00367 
00368     map<string, ::MEDMEM::GMESH* >::iterator name_mesh = _medmem_meshes.find( meshName );
00369     if ( name_mesh == _medmem_meshes.end() )
00370       throw MED_EXCEPTION(STRING("MED_i::getSupport(meshName, entity, supportName) : ")
00371                           << "There is no mesh named |" << meshName << "|");
00372     support = name_mesh->second->getSupportOnAll(entity);
00373   }
00374   if ( ! support )
00375     throw MED_EXCEPTION(STRING("MED_i::getSupport(meshName, entity, supportName) : ")
00376                         << "There is no support on entity " << entity
00377                         << " in mesh named |"<< meshName << "|");
00378 
00379   SUPPORT_i * mySupportI = new SUPPORT_i( support );
00380   SALOME_MED::SUPPORT_ptr mySupportIOR = mySupportI->_this();
00381   return mySupportIOR;
00382 }
00383 
00384 //=======================================================================
00385 //function : ~MED_i
00386 //purpose  : * Destructor
00387 //=======================================================================
00388 
00389 MED_i::~MED_i()
00390 {
00391   map<string, ::MEDMEM::GMESH* >::iterator n_m =  _medmem_meshes.begin();
00392   for ( ; n_m != _medmem_meshes.end(); ++ n_m )
00393     n_m->second->removeReference();
00394   _medmem_meshes.clear();
00395 
00396   for ( int i = 0; i < (int)_medmem_fields.size(); ++i )
00397     _medmem_fields[i]->removeReference();
00398   _medmem_fields.clear();
00399 }
00400 
00401 //=======================================================================
00402 //function : getNumberOfMeshes
00403 //purpose  : * CORBA: Accessor for Number of meshes
00404 //=======================================================================
00405 
00406 CORBA::Long MED_i::getNumberOfMeshes() 
00407 throw (SALOME::SALOME_Exception)
00408 {
00409         try
00410         {
00411                 return _med.getNumberOfMeshes();
00412         }
00413         catch (MEDEXCEPTION &ex)
00414         {
00415                 MESSAGE("Unable to get the number of meshes in Med Object");
00416                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
00417         }
00418 }
00419 
00420 //=======================================================================
00421 //function : getNumberOfFields
00422 //purpose  : * CORBA: Accessor for Number of fields
00423 //=======================================================================
00424 
00425 CORBA::Long MED_i::getNumberOfFields() 
00426 throw (SALOME::SALOME_Exception)
00427 {
00428         try
00429         {
00430                 return _med.getNumberOfFields();
00431         }
00432         catch (MEDEXCEPTION &ex)
00433         {
00434                 MESSAGE("Unable to get the number of fields in Med Object");
00435                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
00436         }
00437 }
00438 //=============================================================================
00442 //=============================================================================
00443 SALOME_TYPES::ListOfString * MED_i::getMeshNames()      
00444 throw (SALOME::SALOME_Exception)
00445 {
00446         SALOME_TYPES::ListOfString_var myseq = new SALOME_TYPES::ListOfString;
00447         try
00448         {
00449                 int nbMeshes=_med.getNumberOfMeshes();
00450                 myseq->length(nbMeshes);
00451                 string * nameMeshes = new string[nbMeshes];
00452                 _med.getMeshNames(nameMeshes);
00453                 for (int i=0;i<nbMeshes;i++)
00454                 {
00455                         myseq[i]=CORBA::string_dup(nameMeshes[i].c_str());
00456                 }
00457                 delete [] nameMeshes;
00458                 nameMeshes=NULL;
00459         }
00460         catch (MEDEXCEPTION &ex)
00461         {
00462                 MESSAGE("Unable to get the names of the meshes in Med Object");
00463                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
00464         }
00465         return myseq._retn();
00466 }
00467 //=============================================================================
00471 //=============================================================================
00472 SALOME_TYPES::ListOfString * MED_i::getFieldNames()     
00473   throw (SALOME::SALOME_Exception)
00474 {
00475   SALOME_TYPES::ListOfString_var myseq = new SALOME_TYPES::ListOfString;
00476   try
00477     {
00478       vector<string> nameFields = _med.getFieldNames();
00479       int nbFields = nameFields.size();
00480       myseq->length(nbFields);
00481 
00482       for (int i=0;i<nbFields;i++)
00483         {
00484           myseq[i]=CORBA::string_dup(nameFields[i].c_str());
00485         }
00486     }
00487   catch (MEDEXCEPTION &ex)
00488     {
00489       MESSAGE("Unable to get the names of the fields in Med Object");
00490       THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
00491     }
00492   return myseq._retn();
00493 }
00494 
00495 //=======================================================================
00496 //function : getMeshByName
00497 //purpose  : * CORBA: Accessor for a specific mesh
00498 //=======================================================================
00499 
00500 SALOME_MED::GMESH_ptr MED_i::getMeshByName(const char* meshName) 
00501 throw (SALOME::SALOME_Exception)
00502 {
00503         try
00504         {
00505           if (_meshes.count( meshName ))
00506             return SALOME_MED::GMESH::_duplicate(_meshes[meshName] );
00507 
00508           GMESH * myMesh = 0;
00509           if ( _med.isStructuredMesh( meshName ))
00510             myMesh = new GRID( MED_DRIVER, _med.getFileName(), meshName );
00511           else
00512             myMesh = new MESH( MED_DRIVER, _med.getFileName(), meshName );
00513           GMESH_i * m1 = new GMESH_i(myMesh);
00514           return m1->_this();
00515         }
00516         catch (MEDEXCEPTION &ex)
00517         {
00518                 MESSAGE("Unable to get the specified mesh");
00519                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
00520         }
00521 
00522 }
00523 
00524 //=======================================================================
00525 //function : getMesh
00526 //purpose  : * CORBA: Accessor for a specific mesh
00527 //=======================================================================
00528 
00529 SALOME_MED::GMESH_ptr MED_i::getMesh(SALOME_MED::FIELD_ptr fieldPtr) 
00530 throw (SALOME::SALOME_Exception)
00531 {
00532         int ind=fieldPtr->getCorbaIndex();
00533         SCRUTE(ind);
00534 
00535         SALOME_MED::FIELDINT_var fieldint =SALOME_MED::FIELDINT::_narrow(fieldPtr);
00536         if (CORBA::is_nil(fieldint))
00537         {
00538                 MESSAGE("Double");
00539                 SALOME_MED::FIELDDOUBLE_var fielddouble = 
00540                             SALOME_MED::FIELDDOUBLE::_narrow(fieldPtr);
00541                 ASSERT(!CORBA::is_nil(fielddouble));
00542 
00543                 ASSERT(FIELD_i::fieldMap.find(ind)!=FIELD_i::fieldMap.end());
00544 
00545         }
00546         else
00547         {
00548                 MESSAGE("Integer");
00549                 ASSERT(FIELD_i::fieldMap.find(ind)!=FIELD_i::fieldMap.end());
00550         }
00551         FIELD_* field = FIELD_i::fieldMap[ind];
00552         string meshName;
00553         if ( field->getSupport() )
00554         {
00555           if ( field->getSupport()->getMesh() )
00556             meshName = field->getSupport()->getMesh()->getName();
00557           else
00558             field->getSupport()->getMeshName();
00559         }
00560         SALOME_MED::GMESH_var meshIOR;
00561         if ( _meshes.count( meshName ))
00562           meshIOR = SALOME_MED::GMESH::_duplicate(_meshes[meshName] );
00563         return meshIOR._retn();
00564 }
00565 
00566 //=======================================================================
00567 //function : getFieldNumberOfIteration
00568 //purpose  : * CORBA: Accessor for Number of iteration of a given field by its name.
00569 //=======================================================================
00570 
00571 CORBA::Long MED_i::getFieldNumberOfIteration(const char* fieldName) 
00572   throw (SALOME::SALOME_Exception)
00573 {
00574   try
00575     {
00576       vector<DT_IT_> fieldIteration = _med.getFieldIteration(string(fieldName));
00577       return fieldIteration.size();
00578     }
00579   catch (MEDEXCEPTION &ex)
00580     {
00581       MESSAGE("Unable to get the number of iteration of the field " << fieldName << " in Med Object");
00582       THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
00583     }
00584 }
00585 //=============================================================================
00590 //=============================================================================
00591 SALOME_TYPES::ListOfLong * MED_i::getFieldIteration(const char* fieldName,CORBA::Long i) 
00592   throw (SALOME::SALOME_Exception)
00593 {
00594   SALOME_TYPES::ListOfLong_var myseq = new SALOME_TYPES::ListOfLong;
00595   try
00596     {
00597       vector<DT_IT_> fieldIteration = _med.getFieldIteration(string(fieldName));
00598       int numberOfIteration = fieldIteration.size();
00599 
00600       if (i < 0 || i >=numberOfIteration)
00601         THROW_SALOME_CORBA_EXCEPTION("The integer i should be geater or equal to 0 and lesser then numberOfIteration", \
00602                                      SALOME::INTERNAL_ERROR);
00603 
00604       myseq->length(2);
00605       myseq[0] = fieldIteration[i].dt;
00606       myseq[1] = fieldIteration[i].it;
00607       return myseq._retn();
00608     }
00609   catch (MEDEXCEPTION &ex)
00610     {
00611       MESSAGE("Unable to get the sequence of DT_IT of the given field");
00612       THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
00613     }
00614 }
00615 //=============================================================================
00620 //=============================================================================
00621 SALOME_TYPES::ListOfLong * MED_i::getFieldIterations(const char* fieldName) 
00622   throw (SALOME::SALOME_Exception)
00623 {
00624   SALOME_TYPES::ListOfLong_var myseq = new SALOME_TYPES::ListOfLong;
00625   try
00626     {
00627       vector<DT_IT_> fieldIteration = _med.getFieldIteration(string(fieldName));
00628       int numberOfIteration = fieldIteration.size();
00629       int size = 2*numberOfIteration;
00630 
00631       myseq->length(size);
00632 
00633       for (int i=0; i<numberOfIteration; i++)
00634         {
00635           int index = 2*i;
00636           myseq[index] = fieldIteration[i].dt;
00637           index = index+1;
00638           myseq[index] = fieldIteration[i].it;
00639         }
00640 
00641       return myseq._retn();
00642     }
00643   catch (MEDEXCEPTION &ex)
00644     {
00645       MESSAGE("Unable to get the sequence of DT_IT of the given field");
00646       THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
00647     }
00648 }
00649 
00650 //=======================================================================
00651 //function : getField
00652 //purpose  : * CORBA: Accessor for a specific field
00653 //=======================================================================
00654 
00655 SALOME_MED::FIELD_ptr MED_i::getField(const char* fieldName, 
00656                                       CORBA::Long pasTemps, 
00657                                       CORBA::Long numOrdre ) 
00658 throw (SALOME::SALOME_Exception)
00659 {
00660 
00661         DT_IT_ dtIt;
00662 
00663         dtIt.dt= (int)pasTemps;
00664         dtIt.it= (int)numOrdre;
00665 
00666         map<string,MAP_IOR_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
00667 
00668         if ( itFields == _fields.end() ) 
00669                 THROW_SALOME_CORBA_EXCEPTION("Field not found !", SALOME::INTERNAL_ERROR);
00670   
00671         const MAP_IOR_DT_IT_ & map_dtIt = (*itFields).second;
00672         MAP_IOR_DT_IT_::const_iterator itMap_dtIt =  map_dtIt.find(dtIt);
00673   
00674         if ( itMap_dtIt == map_dtIt.end() )
00675                 THROW_SALOME_CORBA_EXCEPTION("Iteration not found !", SALOME::INTERNAL_ERROR);
00676   
00677         return (*itMap_dtIt).second;
00678 
00679 }
00680 
00681 //=======================================================================
00682 //function : addDriver
00683 //purpose  : * CORBA: Accessor for a specific field
00684 //=======================================================================
00685 
00686 CORBA::Long MED_i::addDriver(SALOME_MED::medDriverTypes driverType, const char* fileName) 
00687 throw (SALOME::SALOME_Exception)
00688 {
00689   return 0;
00690 }
00691 
00692 //=======================================================================
00693 //function : rmDriver
00694 //purpose  : * CORBA: relase the driver (mandatory)
00695 //=======================================================================
00696 
00697 void MED_i::rmDriver(CORBA::Long i) 
00698 throw (SALOME::SALOME_Exception)
00699 {
00700 }
00701 
00702 //=======================================================================
00703 //function : readFileStruct
00704 //purpose  : * CORBA: read med objects relations
00705 //=======================================================================
00706 
00707 void MED_i::readFileStruct(CORBA::Long i) 
00708 throw (SALOME::SALOME_Exception)
00709 {
00710 }
00711 
00712 //=======================================================================
00713 //function : writeFrom
00714 //purpose  : * CORBA: write specifics objects f
00715 //=======================================================================
00716 
00717 void MED_i::writeFrom(CORBA::Long i) 
00718 throw (SALOME::SALOME_Exception)
00719 {
00720 }
00721 
00722 //=======================================================================
00723 //function : write
00724 //purpose  : * CORBA: write objects
00725 //=======================================================================
00726 
00727 void MED_i::write(CORBA::Long i) 
00728 throw (SALOME::SALOME_Exception)
00729 {
00730 }
00731 
00732 //=======================================================================
00733 //function : addMesh
00734 //purpose  : * CORBA: Add Mesh in Med objects
00735 //=======================================================================
00736 
00737 void MED_i::addMesh(SALOME_MED::GMESH_ptr ptrMesh) 
00738 throw (SALOME::SALOME_Exception)
00739 {
00740   THROW_SALOME_CORBA_EXCEPTION("MED_i::addMesh() is obsolete method",SALOME::BAD_PARAM);
00741 }
00742 
00743 //=======================================================================
00744 //function : addField
00745 //purpose  : * CORBA: Add field in Med objects
00746 //=======================================================================
00747 
00748 void MED_i::addField(SALOME_MED::FIELD_ptr ptrField) 
00749 throw (SALOME::SALOME_Exception)
00750 {
00751   THROW_SALOME_CORBA_EXCEPTION("MED_i::addField() is obsolete method",SALOME::BAD_PARAM);
00752 }
00753 
00754 //=======================================================================
00755 //function : addInStudy
00756 //purpose  : * CORBA: Add Med object in Study 
00757 //=======================================================================
00758 
00759 void MED_i::addInStudy(SALOMEDS::Study_ptr myStudy, SALOME_MED::MED_ptr myIor) 
00760 throw (SALOME::SALOME_Exception,SALOMEDS::StudyBuilder::LockProtection)
00761 {
00762         if ( _medId != "" )
00763         {
00764                 MESSAGE("Med already in Study");
00765                     THROW_SALOME_CORBA_EXCEPTION("Med already in Study", \
00766                                  SALOME::BAD_PARAM);
00767         };
00768 
00769         SALOMEDS::StudyBuilder_var     myBuilder = myStudy->NewBuilder();
00770         myBuilder->NewCommand();
00771         SALOMEDS::GenericAttribute_var anAttr;
00772         SALOMEDS::AttributeName_var    aName;
00773         SALOMEDS::AttributeIOR_var     aIOR;
00774 
00775         // Create SComponent labelled 'MED' if it doesn't already exit
00776         SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
00777         if ( CORBA::is_nil(medfather) )
00778         {
00779           THROW_SALOME_CORBA_EXCEPTION("Component Med not found",
00780                                        SALOME::BAD_PARAM);
00781         };
00782 
00783         MESSAGE("Add a MED Object under Med");
00784         SALOMEDS::SObject_var newObj = myBuilder->NewObject(medfather);
00785 
00786         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
00787         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
00788         CORBA::ORB_var &orb = init(0,0);
00789         CORBA::String_var iorStr = orb->object_to_string(myIor);
00790         anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
00791         aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
00792         aIOR->SetValue(iorStr.in());
00793         anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeName");
00794         aName = SALOMEDS::AttributeName::_narrow(anAttr);
00795         aName->SetValue("Objet MED");
00796         _medId = newObj->GetID();
00797         myBuilder->CommitCommand();
00798 
00799         // register the Corba pointer: increase the referrence count
00800         MESSAGE("Registering of the Corba Med pointer");
00801         Register();
00802 
00803 }
00804 
00805 //=============================================================================
00812 //=============================================================================
00813 void MED_i::addInStudy (SALOMEDS::Study_ptr myStudy,
00814                         SALOME_MED::MED_ptr myIor,
00815                         SALOMEDS::SComponent_ptr medfather,
00816                         const char * fileName)
00817   throw (SALOME::SALOME_Exception,SALOMEDS::StudyBuilder::LockProtection)
00818 {
00819         if ( _medId != "" ) {
00820           MESSAGE("Med already in Study");
00821           THROW_SALOME_CORBA_EXCEPTION("Med already in Study", SALOME::BAD_PARAM);
00822         };
00823 
00824         SALOMEDS::StudyBuilder_var     myBuilder = myStudy->NewBuilder();
00825         myBuilder->NewCommand();
00826         SALOMEDS::GenericAttribute_var anAttr;
00827         SALOMEDS::AttributeName_var    aName;
00828         SALOMEDS::AttributeIOR_var     aIOR;
00829 
00830         // Create SComponent labelled 'MED' if it doesn't already exit
00831         //SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
00832         if ( CORBA::is_nil(medfather) )
00833         {
00834           THROW_SALOME_CORBA_EXCEPTION("Component Med not found",
00835                                        SALOME::BAD_PARAM);
00836         };
00837 
00838         SALOMEDS::SObject_var medObj;
00839         if ( ! fileName )
00840         {
00841           //  FOR PERSISTANCE: if fileName is a NULL string,
00842           //  looks for a SObject with a suitable name and
00843           //  bad IOR and update the IOR
00844           SALOMEDS::ChildIterator_var anIter = myStudy->NewChildIterator( medfather );
00845           for ( ; anIter->More(); anIter->Next()) {
00846             SALOMEDS::SObject_var obj = anIter->Value();
00847             // check name
00848             CORBA::String_var name = obj->GetName();
00849             const char* suitableName1 = "MED_OBJECT_FROM_FILE_";
00850             const char* suitableName2 = "Objet MED";
00851             if ( strncmp( name, suitableName1, strlen(suitableName1)) == 0 || 
00852                  strcmp( name, suitableName2 ) == 0 ) {
00853               // check IOR
00854               CORBA::String_var ior = obj->GetIOR();
00855               if ( strlen( ior ) == 0 ) {
00856                 medObj = obj;
00857                 break;
00858               }
00859             }
00860           }
00861           if ( medObj->_is_nil() )
00862             return; // nothing looking like MED found in the study
00863         }
00864         else
00865         {
00866           MESSAGE("Add a MED Object under the medfather");
00867 
00868           char * medObjName;
00869           string::size_type pos1=string(fileName).rfind('/');
00870           string::size_type lenFileName = strlen(fileName);
00871           string fileNameShort = string(fileName,pos1+1,lenFileName-pos1-1);
00872 
00873           SCRUTE(fileNameShort);
00874 
00875           int lenName = 21 + strlen(fileNameShort.c_str()) + 1;
00876           medObjName = new char[lenName];
00877           medObjName = strcpy(medObjName,"MED_OBJECT_FROM_FILE_");
00878           medObjName = strcat(medObjName,fileNameShort.c_str());
00879 
00880           string path ("/");
00881           path += string(medfather->GetName());
00882           path += string("/");
00883           path += medObjName;
00884           //string path = string("/Med/") + medObjName;
00885           medObj = myStudy->FindObjectByPath(path.c_str());
00886           if ( medObj->_is_nil() )
00887             medObj = myBuilder->NewObject(medfather);
00888 
00889           anAttr = myBuilder->FindOrCreateAttribute(medObj, "AttributeName");
00890           aName = SALOMEDS::AttributeName::_narrow(anAttr);
00891           aName->SetValue(medObjName);
00892 
00893           delete [] medObjName;
00894         }
00895         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
00896         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
00897         CORBA::ORB_var &orb = init(0,0);
00898         string iorStr = orb->object_to_string(myIor);
00899         anAttr = myBuilder->FindOrCreateAttribute(medObj, "AttributeIOR");
00900         aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
00901         aIOR->SetValue(iorStr.c_str());
00902 
00903         _medId = medObj->GetID();
00904         myBuilder->CommitCommand();
00905 
00906         // register the Corba pointer: increase the referrence count
00907         MESSAGE("Registering of the Corba Med pointer");
00908         Register();
00909 }