]> SALOME platform Git repositories - modules/med.git/blob - src/MED/Med_Gen_i.cxx
Salome HOME
Initialisation module MED_SRC de la base MED
[modules/med.git] / src / MED / Med_Gen_i.cxx
1 //=============================================================================
2 // File      : Med_Gen_i.cxx
3 // Created   : mer fév 20 15:47:57 CET 2002
4 // Author    : Paul RASCLE, EDF
5 // Project   : SALOME
6 // Copyright : EDF 2001
7 // $Header$
8 //=============================================================================
9 using namespace std;
10
11 #include "Med_Gen_i.hxx"
12
13 #include "Mesh_i.hxx"
14 #include "Med_i.hxx"
15 #include "FieldInt_i.hxx"
16 #include "FieldDouble_i.hxx"
17 #include "Support_i.hxx"
18
19
20 #include "MEDMEM_Mesh.hxx"
21 #include "MEDMEM_Field.hxx"
22 #include "MEDMEM_Med.hxx"
23 #include "MEDMEM_MedMedDriver.hxx"
24 #include "MEDMEM_MedMeshDriver.hxx"
25 #include "MEDMEM_MedFieldDriver.hxx"
26 #include "MEDMEM_define.hxx"
27 #include "MEDMEM_DriversDef.hxx"
28
29
30 #include "Utils_SINGLETON.hxx"
31 #include "OpUtil.hxx"
32 #include "Utils_CorbaException.hxx"
33 #include "utilities.h"
34
35 #include <string>
36 #include <deque>
37 #include <map>
38
39 #include <TCollection_AsciiString.hxx>
40 #include <TColStd_SequenceOfAsciiString.hxx>
41 #include "SALOMEDS_Tool.hxx"
42
43 // Initialisation des variables statiques
44  map <string, string> Med_Gen_i::_MedCorbaObj;
45  string Med_Gen_i::_myFileName="";
46  string Med_Gen_i::_saveFileName="";
47
48 //=============================================================================
49 /*!
50  *  default constructor: not for use
51  */
52 //=============================================================================
53
54 Med_Gen_i::Med_Gen_i()
55 {
56   MESSAGE("Med_Gen_i::Med_Gen_i");
57 }
58
59 //=============================================================================
60 /*!
61  *  standard constructor
62  */
63 //=============================================================================
64
65 Med_Gen_i:: Med_Gen_i(CORBA::ORB_ptr orb,
66                                 PortableServer::POA_ptr poa,
67                                 PortableServer::ObjectId * contId, 
68                                 const char *instanceName, 
69                                 const char *interfaceName) :
70   Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
71 {
72   MESSAGE("activate object");
73   _thisObj = this ;
74   _id = _poa->activate_object(_thisObj);
75
76   _duringLoad=false;
77   // get an NamingService interface
78   _NS = SINGLETON_<SALOME_NamingService>::Instance() ;
79   ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
80   _NS->init_orb( _orb ) ;
81 }
82 //=============================================================================
83 /*!
84  *  private method : change a study name in SALOMEDS::Study_var
85  */
86 //=============================================================================
87
88 SALOMEDS::Study_var Med_Gen_i::studyName2Study(const char* studyName)
89   throw(SALOME::SALOME_Exception)
90 {
91   string myStudyName(studyName);
92
93   if (myStudyName.size() == 0)
94     THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
95                                  SALOME::BAD_PARAM);
96
97   // Get StudyManager Reference, current study,
98   
99   CORBA::Object_var obj = _NS->Resolve("/myStudyManager");
100   SALOMEDS::StudyManager_var myStudyManager =
101     SALOMEDS::StudyManager::_narrow(obj);
102   if(CORBA::is_nil(myStudyManager))
103     THROW_SALOME_CORBA_EXCEPTION("No StudyManager Found in NameService", \
104                                  SALOME::BAD_PARAM);
105   
106   SALOMEDS::Study_var myStudy =
107     myStudyManager->GetStudyByName(myStudyName.c_str());
108   if (CORBA::is_nil(myStudy))
109     THROW_SALOME_CORBA_EXCEPTION("Wrong Study Name", \
110                                  SALOME::BAD_PARAM);
111
112   return SALOMEDS::Study::_duplicate(myStudy) ;
113 }
114
115 //=============================================================================
116 /*!
117  *  private method : add Med component in Study (Not MedGen ???)
118  */
119 //=============================================================================
120
121 void Med_Gen_i::addInStudy(SALOMEDS::Study_var myStudy)
122   throw(SALOME::SALOME_Exception)
123 {
124   SALOMEDS::StudyBuilder_var  myBuilder = myStudy->NewBuilder();
125   
126   myBuilder->NewCommand();
127   
128   // Create SComponent labelled 'Med' if it doesn't already exit
129   SALOMEDS::SComponent_var medfather = myStudy->FindComponent("Med");
130   if ( CORBA::is_nil(medfather) )
131     {
132       // mpv: component label must be created in spite of "Locked" study flag state
133       bool aLocked = myStudy->GetProperties()->IsLocked();
134       if (aLocked) myStudy->GetProperties()->SetLocked(false);
135       
136       MESSAGE("Add Component Med");
137       medfather = myBuilder->NewComponent("Med");
138       SALOMEDS::GenericAttribute_var anAttr = myBuilder->FindOrCreateAttribute(medfather, "AttributeName");
139       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
140       aName->SetValue("Med");
141       //            Utilisation de this  deconseillee par Paul ??
142       //            myBuilder->DefineComponentInstance(medfather,POA_Engines::Med_Gen::_this());
143       CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
144       myBuilder->DefineComponentInstance(medfather,myO);
145       
146       if (aLocked) myStudy->GetProperties()->SetLocked(true);
147     } 
148   
149   myBuilder->CommitCommand();
150    
151 }
152
153 //=============================================================================
154 /*!
155  * Lit tous les objets contenus dans un fichier med et les lit en memoire
156  */
157 //=============================================================================
158 SALOME_MED::MED_ptr Med_Gen_i::readStructFile (const char* fileName,
159                                 const char* studyName)
160   throw(SALOME::SALOME_Exception)
161 {
162         SCRUTE(fileName);
163         SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
164         if (!_duringLoad) addInStudy(myStudy) ;
165
166         SALOME_MED::MED_ptr myMedIOR ;
167         try 
168         {
169           // we create a new MED_i and add in study
170           MED_i * myMedI = new MED_i();
171           myMedIOR = myMedI->_this() ;
172           if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR) ;        
173           // create ::MED object, read all and add in study !
174           myMedI->init(myStudy,MED_DRIVER,fileName) ;
175         }
176         catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
177         catch(...)
178         {
179                 MESSAGE("Erreur a la lecture du fichier");
180                 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
181                                                 ,SALOME::BAD_PARAM);
182         }
183         return SALOME_MED::MED::_duplicate(myMedIOR) ;
184 }
185
186 //=============================================================================
187 /*!
188  *  Prepare un Maillage sans le lire en memoire avec stokage dans l'etude
189  *  des champs avec leur type
190  */
191 //=============================================================================
192 void Med_Gen_i::readStructFileWithFieldType (const char* fileName,
193                                              const char* studyName)
194 throw (SALOME::SALOME_Exception)
195 {
196   BEGIN_OF("Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)");
197
198         SCRUTE(fileName);
199         SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
200
201         if (!_duringLoad) addInStudy(myStudy) ;
202
203         try 
204         {
205           // we create a new MED_i and add in study
206           MED_i * myMedI = new MED_i();
207           SALOME_MED::MED_ptr myMedIOR = myMedI->_this() ;
208           if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR) ;        
209           // create ::MED object, read all and add in study !
210           myMedI->initWithFieldType(myStudy,MED_DRIVER,fileName) ;
211         }
212         catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
213         catch(...)
214         {
215                 MESSAGE("Erreur a la lecture du fichier");
216                 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
217                                                 ,SALOME::BAD_PARAM);
218         }
219
220         END_OF("Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)");
221
222
223 }
224
225 //=============================================================================
226 /*!
227  *  Sert un Maillage
228  */
229 //=============================================================================
230 SALOME_MED::MESH_ptr Med_Gen_i::readMeshInFile(const char* fileName,
231                                                const char* studyName,
232                                                const char* meshName)
233 throw (SALOME::SALOME_Exception)
234 {
235         SCRUTE(fileName);
236         SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
237
238         if (!_duringLoad) addInStudy(myStudy) ;
239
240 // Creation du maillage
241
242         MESH * myMesh= new MESH() ;
243         myMesh->setName(meshName);
244         MED_MESH_RDONLY_DRIVER myMeshDriver(fileName,myMesh);
245         try
246         {
247                 myMeshDriver.setMeshName(meshName);
248                 myMeshDriver.open();
249         }
250         catch (const exception & ex)
251         {
252                 MESSAGE("Exception Interceptee : ");
253                 SCRUTE(ex.what());
254                 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
255         };
256         try
257         {
258                 myMeshDriver.read();
259 ("apres read");
260                 myMeshDriver.close();
261         }
262         catch (const exception & ex)
263         {
264                 MESSAGE("Exception Interceptee : ");
265                 SCRUTE(ex.what());
266                 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
267         };
268
269         MESH_i * meshi = new MESH_i(myMesh);
270         //SALOME_MED::MESH_var mesh = SALOME_MED::MESH::_narrow(meshi->_this());
271         SALOME_MED::MESH_ptr mesh = meshi->_this();
272         try
273         {
274         // add the mesh object in study
275                 if (!_duringLoad) meshi->addInStudy(myStudy,mesh);
276         }
277         catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
278         return SALOME_MED::MESH::_duplicate(mesh);
279 }
280 //=============================================================================
281 /*!
282  *  Sert un Champ
283  */
284 //=============================================================================
285 SALOME_MED::FIELD_ptr Med_Gen_i::readFieldInFile(const char* fileName,
286                                                const char* studyName,
287                                                const char* fieldName,
288                                                CORBA::Long ordre,
289                                                CORBA::Long iter)
290 throw (SALOME::SALOME_Exception)
291 {
292         SCRUTE(fileName);
293         string myStudyName(studyName);
294
295         if (myStudyName.size() == 0)
296                 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
297                                  SALOME::BAD_PARAM);
298
299         // Get StudyManager Reference, current study,
300         
301         CORBA::Object_var obj = _NS->Resolve("/myStudyManager");
302         SALOMEDS::StudyManager_var myStudyManager =
303                         SALOMEDS::StudyManager::_narrow(obj);
304         ASSERT(! CORBA::is_nil(myStudyManager));
305         SALOMEDS::Study_var myStudy =
306                 myStudyManager->GetStudyByName(myStudyName.c_str());
307         if (CORBA::is_nil(myStudy))
308         THROW_SALOME_CORBA_EXCEPTION("Wrong Study Name", \
309                                  SALOME::BAD_PARAM);
310
311         SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
312         SALOMEDS::SComponent_var medfather = myStudy->FindComponent("Med");
313         if (CORBA::is_nil(medfather))
314         {
315                 myBuilder->NewCommand();
316                 // mpv: component label must be created in spite of "Locked" study flag state
317                 bool aLocked = myStudy->GetProperties()->IsLocked();
318                 if (aLocked) myStudy->GetProperties()->SetLocked(false);
319
320                 medfather = myBuilder->NewComponent("Med");
321                 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(
322                       myBuilder->FindOrCreateAttribute(medfather, "AttributeName"));
323                 aName->SetValue("Med");
324                 CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
325                 myBuilder->DefineComponentInstance(medfather,myO);
326
327                 if (aLocked) myStudy->GetProperties()->SetLocked(true);
328                 myBuilder->CommitCommand();
329
330         }
331         else
332                         MESSAGE("MED dejà dans l étude");
333
334         MESSAGE("Lecture du fichier ")
335         SCRUTE(fileName);
336
337 // Creation du champ
338
339         FIELD_ * myField= new FIELD_() ;
340         MED * mymed = new MED(MED_DRIVER,fileName) ;
341         try
342         {
343                 deque<string> fieldsNames = mymed->getFieldNames() ;
344                 int numberOfFields = fieldsNames.size();
345                 int i;
346                 for (i=0; i<numberOfFields; i++)
347                 {
348                         if (fieldsNames[i]== fieldName) break;
349                 }
350                 if (i == numberOfFields)
351                 {
352                         THROW_SALOME_CORBA_EXCEPTION("Unable to find this field ",SALOME::BAD_PARAM);
353                 }
354                 MESSAGE("trouve");
355 /*
356                 deque<DT_IT_> myIteration = mymed->getFieldIteration (fieldName);
357                 if (myIteration.size() != 1)
358                 {
359                         MESSAGE("WARNING : My iteration size is ")
360                         SCRUTE(myIteration.size());
361                 }
362 */
363                 myField = mymed->getField(fieldName,iter,ordre);
364         }
365         catch (const exception & ex)
366         {
367                 MESSAGE("Exception Interceptee : ");
368                 SCRUTE(ex.what());
369                 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field in this file",SALOME::BAD_PARAM);
370         };
371                         
372         SUPPORT * fieldSupport;
373         SALOME_MED::SUPPORT_ptr mySupportIOR;
374         try
375         {
376                 fieldSupport=(SUPPORT *)myField->getSupport() ;
377                 ASSERT(fieldSupport != NULL);
378                 MESH * myMesh=(MESH *)fieldSupport->getMesh();
379                 ASSERT(myMesh != NULL);
380                 myMesh->read();
381                 SCRUTE(myMesh->getName());
382                 fieldSupport->update();
383                 SUPPORT_i * mySupportI = new SUPPORT_i(fieldSupport);
384                 mySupportIOR=mySupportI->_this();
385         }
386         catch (const exception & ex)
387         {
388                 MESSAGE("Exception Interceptee : ");
389                 SCRUTE(ex.what());
390                 THROW_SALOME_CORBA_EXCEPTION("Unable to find associated support",SALOME::BAD_PARAM);
391         };
392
393         med_type_champ type = myField->getValueType() ;
394         switch (type) 
395         {
396          case MED_FR::MED_INT32:        
397          {
398                 try 
399                 {
400                         ((FIELD<int>*)myField)->read() ;
401                         FIELDINT_i * myFieldIntI = new FIELDINT_i(mySupportIOR,(FIELD<int>*)myField);
402                         POA_SALOME_MED::FIELD_tie<FIELD_i> * myFieldTie = new POA_SALOME_MED::FIELD_tie<FIELD_i>(myFieldIntI);
403                         SALOME_MED::FIELD_ptr myFieldIOR = myFieldTie->_this() ;
404                         if (!_duringLoad) myFieldIntI->addInStudy(myStudy,myFieldIOR) ;
405                         return SALOME_MED::FIELD::_duplicate(myFieldIOR);
406                 }
407                 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
408                 catch (const exception & ex)
409                 {
410                         MESSAGE("Exception Interceptee : ");
411                         SCRUTE(ex.what());
412                         THROW_SALOME_CORBA_EXCEPTION("Unable to read int field",SALOME::BAD_PARAM);
413                 };
414                 break;
415          }
416          case MED_FR::MED_REEL64: 
417          {
418                 try 
419                 {
420                         ((FIELD<double>*)myField)->read() ;
421                         FIELDDOUBLE_i * myFieldDoubleI = new FIELDDOUBLE_i(mySupportIOR,(FIELD<double>*)myField);
422                         POA_SALOME_MED::FIELD_tie<FIELD_i> * myFieldTie = new POA_SALOME_MED::FIELD_tie<FIELD_i>(myFieldDoubleI);
423                         SALOME_MED::FIELD_ptr myFieldIOR = myFieldTie->_this() ;
424                         if (!_duringLoad) myFieldDoubleI->addInStudy(myStudy,myFieldIOR) ;
425                         return SALOME_MED::FIELD::_duplicate(myFieldIOR);
426                 }
427                 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
428                 catch (const exception & ex)
429                 {
430                         MESSAGE("Exception Interceptee : ");
431                         SCRUTE(ex.what());
432                         THROW_SALOME_CORBA_EXCEPTION("Unable to read double field",SALOME::BAD_PARAM);
433                 };
434                 break;
435          }
436         }
437
438         
439 }
440
441
442 //=============================================================================
443 /*!
444  *  Destructor
445  */
446 //=============================================================================
447
448 Med_Gen_i::~Med_Gen_i()
449 {
450   MESSAGE("Med_Gen_i::~Med_Gen_i");
451 }
452
453 //=============================================================================
454 /*!
455  *  CORBA: Save Mesh objects (called when a study is saved)
456  */
457 //=============================================================================
458 SALOMEDS::TMPFile* Med_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent) {
459   const char* LOC = "Med_Gen_i::Save";
460   BEGIN_OF(LOC);
461
462   SALOMEDS::TMPFile_var aStreamFile;
463   // Get a temporary directory to store a file
464   TCollection_AsciiString aTmpDir = SALOMEDS_Tool::GetTmpDir();
465   // Create a list to store names of created files
466   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
467   TColStd_SequenceOfAsciiString aFileNames;
468
469   SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
470   if (!CORBA::is_nil(aMedMeshFather)) {
471     SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
472     for(; anIter->More(); anIter->Next()) {
473       SALOMEDS::SObject_var aSO = anIter->Value();
474       SALOMEDS::GenericAttribute_var anAttr;
475       if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
476         CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
477         SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
478         if (! CORBA::is_nil(myMesh)) {
479           TCollection_AsciiString aName;
480           aName = "MED_tmp_";
481           aName += myMesh->getName();
482           aName += ".med";
483 //        MESSAGE("Save mesh with name "<<aName.ToCString());
484           long driverId = myMesh->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myMesh->getName());
485           myMesh->write(driverId,"");
486           aFileNames.Append(aName);
487         }
488       }
489     }
490   }
491
492   SALOMEDS::SObject_var aMedFieldFather = theComponent->GetStudy()->FindObject("MEDFIELD");
493   if (!CORBA::is_nil(aMedFieldFather)) {
494     SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedFieldFather);
495     for(; anIter->More(); anIter->Next()) {
496       SALOMEDS::SObject_var aSO = anIter->Value();
497       SALOMEDS::GenericAttribute_var anAttr;
498       if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
499         CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
500         SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
501         if (! CORBA::is_nil(myField)) {
502           TCollection_AsciiString aName;
503           aName = "MED_tmp_";
504           aName += myField->getName();
505           aName += ".med";
506 //        MESSAGE("Save mesh with name "<<aName.ToCString());
507           long driverId = myField->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myField->getName());
508           myField->write(driverId,"");
509           aFileNames.Append(aName);
510         }
511       }
512     }
513   }
514
515   int i;
516   aSeq->length(aFileNames.Length());
517   for(i = aFileNames.Length(); i > 0; i--) aSeq[i-1] = CORBA::string_dup(aFileNames.Value(i).ToCString());
518   // Conver a file to the byte stream
519   aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq);
520   // Remove the created file and tmp directory
521   SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq, true);
522   // Return the created byte stream
523   return aStreamFile._retn();
524   
525   END_OF(LOC);
526 }
527
528 //=============================================================================
529 /*!
530  *  CORBA: Load Mesh objects (called when an existing study is opened)
531  */
532 //=============================================================================
533
534 CORBA::Boolean Med_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent, const SALOMEDS::TMPFile& theStream) {
535   const char* LOC = "Med_Gen_i::Load";
536   BEGIN_OF(LOC);
537
538   // Get a temporary directory for a file
539   TCollection_AsciiString aTmpDir = SALOMEDS_Tool::GetTmpDir();
540   _saveFileName = CORBA::string_dup(aTmpDir.ToCString());
541   SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString());
542   return true;
543
544   END_OF(LOC);
545 }
546
547 //=============================================================================
548 /*!
549  *  CORBA: 
550  */
551 //=============================================================================
552
553 void Med_Gen_i::Close(const char *IORSComponent)
554 {
555   MESSAGE("Med_Gen_i::Close");
556 }
557
558 //=============================================================================
559 /*!
560  *  CORBA:
561  */
562 //=============================================================================
563
564 char* Med_Gen_i::ComponentDataType()
565 {
566   MESSAGE("Med_Gen_i::ComponentDataType");
567   return strdup("Med") ; /* What is this type ? */
568 }
569     
570 //=============================================================================
571 /*!
572  *  CORBA: give a persistent reference of a transient object (for study save) 
573  */
574 //=============================================================================
575
576 char* Med_Gen_i::IORToLocalPersistentID(const char* IORString,
577                                         CORBA::Boolean& IsAFile)
578 {
579   const char * LOC = "Med_Gen_i::IORToLocalPersistentID" ;
580   BEGIN_OF(LOC) ;
581
582   SCRUTE(IORString);
583   if (string(IORString).size()==0) return strdup("");
584   // Well, we know where put object (_saveFilename) and we know object (IORString)
585   // cast object :
586   CORBA::Object_var myIOR = _orb->string_to_object(IORString);
587
588   // MED
589   SALOME_MED::MED_var myMed = SALOME_MED::MED::_narrow(myIOR);
590   if (! CORBA::is_nil(myMed)) 
591   {
592         // nothing to save : Support will be saved inside the mesh
593         string str_MedName="Objet Med + /OBJ_MED/";
594         return strdup(str_MedName.c_str()) ; 
595   }
596  
597   // MESH
598   SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
599   if (! CORBA::is_nil(myMesh)) 
600   {
601         string str_MeshName;
602         try
603         {
604                 long driverId = myMesh->addDriver(SALOME_MED::MED_DRIVER,_saveFileName.c_str(),myMesh->getName()) ;
605                 myMesh->write(driverId,"");
606                 str_MeshName=string("/ENS_MAA/")+string(myMesh->getName());
607         }
608         catch(...)
609         {
610                 MESSAGE("Unable to save the mesh");
611                 THROW_SALOME_CORBA_EXCEPTION("Unable to save Mesh in Med"\
612                                               ,SALOME::INTERNAL_ERROR);
613         }
614         return strdup(str_MeshName.c_str()) ;
615   }
616     
617   // SUPPORT
618   SALOME_MED::SUPPORT_var mySupport = SALOME_MED::SUPPORT::_narrow(myIOR);
619   if (! CORBA::is_nil(mySupport)) 
620   {
621         // nothing to save : Support will be saved inside the mesh
622         string str_SupportName;
623         try
624         {
625                 str_SupportName=string("/FAS/")+string(mySupport->getName());
626                 str_SupportName+=string("/ENS_MAA/")+string(mySupport->getMesh()->getName());
627                 SCRUTE(str_SupportName);
628         }
629         catch(...)
630         {
631                 MESSAGE("Unable to save the support");
632                 THROW_SALOME_CORBA_EXCEPTION("Unable to save Field in Med"\
633                                               ,SALOME::INTERNAL_ERROR);
634         }
635     return strdup(str_SupportName.c_str());
636   }
637     
638   SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
639   if (! CORBA::is_nil(myField)) 
640   {
641         string str_FieldName;
642         try
643         {
644                 long driverId = myField->addDriver(SALOME_MED::MED_DRIVER,_saveFileName.c_str(),myField->getName()) ;
645                 myField->write(driverId,"");
646                 str_FieldName=string("/CHA/")+string(myField->getName());
647                 ostringstream a,b;
648                 a<< myField->getOrderNumber();
649                 b<< myField->getIterationNumber();
650                 str_FieldName+=string("/ORDRE/")+a.str()+string("/ITER/")+b.str();
651         }
652         catch(...)
653         {
654                 MESSAGE("Unable to save the mesh");
655                 THROW_SALOME_CORBA_EXCEPTION("Unable to save Field in Med"\
656                                               ,SALOME::INTERNAL_ERROR);
657         }
658         return strdup(str_FieldName.c_str());
659   }
660
661   //THROW_SALOME_CORBA_EXCEPTION("Unable to save IOR",SALOME::BAD_PARAM);
662   return strdup("");
663
664   END_OF(LOC) ;
665 }
666
667 //=============================================================================
668 /*!
669  *  CORBA: give a transient reference (when loading an object, opening study)
670  */
671 //=============================================================================
672
673 char* Med_Gen_i::LocalPersistentIDToIOR(const char* aLocalPersistentID)
674 {
675   const char * LOC = "Med_Gen_i::LocalPersistentIDToIOR" ;
676   BEGIN_OF(LOC) ;
677
678   TCollection_AsciiString aTmpDir(CORBA::string_dup(_saveFileName.c_str()));
679   TCollection_AsciiString aFileName("MED_tmp_");
680   aFileName += CORBA::string_dup(aLocalPersistentID);
681   aFileName += ".med";
682
683   MESH * myMesh= new MESH() ;
684   myMesh->setName(aLocalPersistentID);
685   MED_MESH_RDONLY_DRIVER myMeshDriver((aTmpDir+aFileName).ToCString(),myMesh);
686   try
687     {
688       myMeshDriver.setMeshName(aLocalPersistentID);
689       myMeshDriver.open();
690     }
691   catch (const exception & ex)
692     {
693       MESSAGE("Exception Interceptee : ");
694       SCRUTE(ex.what());
695       THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
696     };
697   try
698     {
699       myMeshDriver.read();
700       MESSAGE("apres read");
701       myMeshDriver.close();
702     }
703   catch (const exception & ex)
704     {
705       MESSAGE("Exception Interceptee : ");
706       SCRUTE(ex.what());
707       THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
708     };
709   
710   MESH_i * meshi = new MESH_i(myMesh);
711   //SALOME_MED::MESH_var mesh = SALOME_MED::MESH::_narrow(meshi->_this());
712   SALOME_MED::MESH_ptr mesh = meshi->_this();
713
714   SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
715   aSeq->length(1);
716   aSeq[0]=CORBA::string_dup(aFileName.ToCString());
717   SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq, true);
718
719   return(CORBA::string_dup(_orb->object_to_string(mesh)));
720
721   END_OF(LOC) ;
722 }
723
724 //=============================================================================
725 /*!
726  *  returns true, if can publish object
727  */
728 //=============================================================================
729 bool Med_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
730   SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(theIOR);
731   return !(aMesh->_is_nil());
732 }
733
734 //=============================================================================
735 /*!
736  *  publish the given object
737  */
738 //=============================================================================
739 SALOMEDS::SObject_ptr Med_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
740                                                 SALOMEDS::SObject_ptr theSObject,
741                                                 CORBA::Object_ptr theObject,
742                                                 const char* theName) throw (SALOME::SALOME_Exception)
743 {
744   SALOMEDS::SObject_var aResultSO;
745
746   if(CORBA::is_nil(theObject)) return aResultSO;
747   if(theStudy->_is_nil()) return aResultSO;
748
749   SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
750   SALOMEDS::GenericAttribute_var anAttr;
751   SALOMEDS::AttributeName_var    aName;
752   SALOMEDS::AttributeIOR_var     anIOR;
753   SALOMEDS::SComponent_var aFather = theStudy->FindComponent("Med");
754
755   if (aFather->_is_nil()) {
756     aFather = aBuilder->NewComponent("Med");
757     if (aFather->_is_nil()) return aResultSO;
758     anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributeName");
759     aName = SALOMEDS::AttributeName::_narrow(anAttr);
760     aName->SetValue("MED");
761     aBuilder->DefineComponentInstance(aFather, Med_Gen::_this());
762   }
763
764   if (CORBA::is_nil(theSObject)) {
765     SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(theObject);
766     aMesh->addInStudy(theStudy, aMesh);
767     SALOMEDS::SObject_var aResultSO = theStudy->FindObjectIOR(_orb->object_to_string(theObject));
768   } else {
769     if (!theSObject->ReferencedObject(aResultSO)) 
770       THROW_SALOME_CORBA_EXCEPTION("Publish in study MED object error",SALOME::BAD_PARAM);
771   }
772 //    aBuilder->Addreference(theObject, aResultSO);
773   return aResultSO._retn();
774 }
775
776 //=============================================================================
777 /*! 
778  * C factory, accessible with dlsym, after dlopen  
779  */
780 //=============================================================================
781
782 extern "C"
783 {
784   PortableServer::ObjectId * MedEngine_factory(
785                                CORBA::ORB_ptr orb,
786                                PortableServer::POA_ptr poa, 
787                                PortableServer::ObjectId * contId,
788                                const char *instanceName, 
789                                const char *interfaceName)
790   {
791     MESSAGE("PortableServer::ObjectId * MedEngine_factory()");
792     SCRUTE(interfaceName);
793     Med_Gen_i * myMed_Gen 
794       = new Med_Gen_i(orb, poa, contId, instanceName, interfaceName);
795     return myMed_Gen->getId() ;
796   }
797 }