Salome HOME
Copyrights update
[modules/med.git] / src / MEDMEM_I / MEDMEM_Med_i.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 //=============================================================================
21 // File      : MEDMEM_Med_i.cxx
22 // Project   : SALOME
23 // Author    : EDF
24 // Copyright : EDF 2002
25 // $Header: /export/home/PAL/MED_SRC/src/MEDMEM_I/MEDMEM_i.cxx
26 //=============================================================================
27
28 #include <deque>
29
30 //#include "MEDMEM_Field.hxx"
31
32 #include "MEDMEM_Med_i.hxx"
33 #include "MEDMEM_Mesh_i.hxx"
34 #include "MEDMEM_FieldDouble_i.hxx"
35 #include "MEDMEM_FieldInt_i.hxx"
36 #include "MEDMEM_Support_i.hxx"
37 #include "MEDMEM_Family_i.hxx"
38 #include "MEDMEM_Group_i.hxx"
39 #include "MEDMEM_convert.hxx"
40 #include "MEDMEM_Family.hxx"
41 #include "MEDMEM_Group.hxx"
42
43 #include "MEDMEM_DriversDef.hxx"
44 #include "utilities.h"
45 #include "Utils_CorbaException.hxx"
46
47 using namespace std;
48 using namespace MEDMEM;
49
50 //=============================================================================
51 /*!
52  * Default constructor
53  */
54 //=============================================================================
55 MED_i::MED_i():_med((::MED*)NULL)
56 {
57         BEGIN_OF("Default Constructor MED_i");
58         _med = new ::MED();
59         END_OF("Default Constructor MED_i");
60 }
61 //=============================================================================
62 /*!
63  * methods
64  */
65 //=============================================================================
66 void MED_i::init(SALOMEDS::Study_ptr myStudy,driverTypes driverType, const string & fileName)
67 {
68         const char * LOC = "MED_i::init(driverTypes, const string &)";
69         BEGIN_OF(LOC);
70
71   // we create all IOR from _med
72         if ( _med )
73           delete _med;
74         _med = new ::MED(driverType,fileName);
75
76   // MESHES :
77         deque<string> meshesNames = _med->getMeshNames();
78         int numberOfMeshes = meshesNames.size();
79         for (int i=0; i<numberOfMeshes; i++) 
80         {
81                 ::MESH * myMesh = _med->getMesh(meshesNames[i]);
82                 myMesh->read();
83                 MESH_i * myMeshI = new MESH_i(myMesh);
84                 SALOME_MED::MESH_ptr myMeshIOR = myMeshI->_this();
85                 _meshes[meshesNames[i]] = myMeshIOR;
86 //              myMeshI->addInStudy(myStudy,myMeshIOR,fileName);
87         }
88
89   // SUPPORTS :
90         _med->updateSupport();
91
92   // FAMILIES :
93   // we add all families 
94         vector<FAMILY*> familyVector;
95         vector<FAMILY*>::iterator familyVectorIt;
96   // GROUPS :
97   // we add all groups
98         vector<GROUP*> groupVector;
99         vector<GROUP*>::iterator groupVectorIt;
100   
101         MED_EN::MESH_ENTITIES::const_iterator currentEntity; 
102         for (int i=0; i<numberOfMeshes; i++) 
103         {
104                 ::MESH * ptrMesh = _med->getMesh(meshesNames[i]);
105                 for (currentEntity = MED_EN::meshEntities.begin();
106                      currentEntity != MED_EN::meshEntities.end(); 
107                      currentEntity++) 
108                 {
109 //                   MESSAGE(LOC << ": for entity " << (*currentEntity).first);
110                 // family :
111                      familyVector = ptrMesh->getFamilies((MED_EN::medEntityMesh)(*currentEntity).first);
112                      int nb = familyVector.size();
113 //                   MESSAGE(LOC << ": there is(are) " << nb << " family(ies)");
114                      for (familyVectorIt = familyVector.begin();
115                           familyVectorIt != familyVector.end();
116                           familyVectorIt++) 
117                      {
118                           FAMILY_i * myFamilyI = new FAMILY_i(*familyVectorIt);
119                           SALOME_MED::FAMILY_ptr myFamilyIOR = myFamilyI->POA_SALOME_MED::FAMILY::_this();
120 //                         myFamilyI->addInStudy(myStudy,myFamilyIOR);
121                      }
122
123                // group :
124                      groupVector = ptrMesh->getGroups((MED_EN::medEntityMesh)(*currentEntity).first);
125                      nb = groupVector.size();
126 //                   MESSAGE(LOC << ": there is(are) " << nb << " group(s)");
127                     for (groupVectorIt = groupVector.begin();
128                          groupVectorIt != groupVector.end();
129                          groupVectorIt++) 
130                     {
131                          GROUP_i * myGroupI = new GROUP_i(*groupVectorIt);
132                          SALOME_MED::GROUP_ptr myGroupIOR = myGroupI->POA_SALOME_MED::GROUP::_this();
133 //                       myGroupI->addInStudy(myStudy,myGroupIOR);
134                     }
135                 }      
136         }
137
138         for (int i=0; i<numberOfMeshes; i++) 
139         {
140               map<MED_EN::medEntityMesh,::SUPPORT*> mySupports = _med->getSupports(meshesNames[i]);
141               map<MED_EN::medEntityMesh,::SUPPORT*>::const_iterator itSupport;
142               map<MED_EN::medEntityMesh, SALOME_MED::SUPPORT_ptr> & mySupportsIOR 
143                                 = _supports[meshesNames[i]];
144               for ( itSupport = mySupports.begin(); itSupport != mySupports.end(); itSupport++ ) 
145               {
146                     SUPPORT_i * mySupportI = new SUPPORT_i((*itSupport).second);
147                     SALOME_MED::SUPPORT_ptr mySupportIOR = mySupportI->_this();
148                     mySupportsIOR[(*itSupport).first] = mySupportIOR;
149 //                  mySupportI->addInStudy(myStudy,mySupportIOR);
150               }
151         }
152
153   // FIELDS :
154         deque<string> fieldsNames = _med->getFieldNames();
155         int numberOfFields = fieldsNames.size();
156         for (int i=0; i<numberOfFields; i++) 
157         {
158              deque<DT_IT_> myIteration = _med->getFieldIteration (fieldsNames[i]);
159              int numberOfIteration = myIteration.size();
160              for (int j=0; j<numberOfIteration; j++) 
161              {
162                  ::FIELD_ * myField = _med->getField(fieldsNames[i], myIteration[j].dt, myIteration[j].it);
163                  string meshName = myField->getSupport()->getMesh()->getName();
164                  medEntityMesh myEntity = myField->getSupport()->getEntity();
165                  map<string, map<MED_EN::medEntityMesh, SALOME_MED::SUPPORT_ptr> >::const_iterator 
166                                                              itSupportOnMesh = _supports.find(meshName);
167                  if ( itSupportOnMesh == _supports.end() )
168                       throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
169                                          << "There is no support on mesh named |" 
170                                          << meshName << "|" ));
171                  const map<MED_EN::medEntityMesh, SALOME_MED::SUPPORT_ptr> & SupportOnMesh 
172                                         = (*itSupportOnMesh).second;
173                  map<MED_EN::medEntityMesh,SALOME_MED::SUPPORT_ptr>::const_iterator itSupport 
174                                         = SupportOnMesh.find(myEntity);
175                  if (itSupport == SupportOnMesh.end())
176                       throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
177                                          << "There is no support on entity "
178                                          << entity << " in mesh named |" 
179                                          << meshName << "|"));
180                  SALOME_MED::SUPPORT_ptr mySupportIOR = (*itSupport).second;
181                  med_type_champ type = myField->getValueType();
182                  SALOME_MED::FIELD_ptr myFieldIOR;
183                  switch (type) 
184                  {
185                          case MED_EN::MED_INT32 : 
186                          {
187                              ((FIELD<int>*)myField)->read();
188                              FIELDINT_i * myFieldIntI = new FIELDINT_i((FIELD<int>*)myField);
189                              myFieldIOR = myFieldIntI->_this();
190 //                           myFieldIntI->addInStudy(myStudy,myFieldIOR);
191                              break;
192                          }
193
194                         case MED_EN::MED_REEL64: 
195                         {
196                              ((FIELD<double>*)myField)->read();
197                              FIELDDOUBLE_i * myFieldDoubleI = new FIELDDOUBLE_i((FIELD<double>*)myField);
198                              myFieldIOR = myFieldDoubleI->_this();
199 //                           myFieldDoubleI->addInStudy(myStudy,myFieldIOR);
200                              break;
201                         }
202                         default: 
203                         {
204                              throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
205                                          <<"   * Iteration "<<FieldIteration[j].dt
206                                          <<", order number "<<FieldIteration[j].it
207                                          <<" has wrong type : "<<type));
208                              break;
209                         }
210                 }
211
212                 DT_IT_ dtIt;
213                 dtIt.dt  = myIteration[j].dt;
214                 dtIt.it  = myIteration[j].it;
215                 _fields[fieldsNames[i]][dtIt] = myFieldIOR;
216              }
217   }
218   
219   END_OF(LOC);
220 }
221
222 //=============================================================================
223 /*!
224  * methods
225  */
226 //=============================================================================
227 void MED_i::initWithFieldType(SALOMEDS::Study_ptr myStudy,driverTypes driverType, const string & fileName)
228 {
229         const char * LOC = "MED_i::initWithFieldType(driverTypes, const string &)";
230         BEGIN_OF(LOC);
231
232   // we create all IOR from _med
233
234         SCRUTE(driverType);
235         SCRUTE(fileName)
236
237         if ( _med )
238           delete _med;
239         _med = new ::MED(driverType,fileName);
240
241         int numberOfMeshes = _med->getNumberOfMeshes();
242         SCRUTE(numberOfMeshes);
243   // MESHES :
244         deque<string> meshesNames = _med->getMeshNames();
245         string meshName0 = meshesNames[0];
246
247         SCRUTE(meshName0);
248
249         for (int i=0; i<numberOfMeshes; i++) 
250         {
251             string meshNamei = meshesNames[i];
252             SCRUTE(meshNamei);
253
254             ::MESH * myMesh = _med->getMesh(meshNamei);
255             myMesh->read();
256             MESH_i * myMeshI = new MESH_i(myMesh);
257             SALOME_MED::MESH_ptr myMeshIOR = myMeshI->_this();
258             _meshes[meshesNames[i]]=myMeshIOR;
259             myMeshI->addInStudy(myStudy,myMeshIOR);
260         }
261
262   // SUPPORTS :
263         _med->updateSupport();
264
265   // FAMILIES :
266   // we add all families 
267         vector<FAMILY*> familyVector;
268         vector<FAMILY*>::iterator familyVectorIt;
269   // GROUPS :
270   // we add all groups
271         vector<GROUP*> groupVector;
272         vector<GROUP*>::iterator groupVectorIt;
273   
274         SCRUTE(numberOfMeshes);
275
276         MED_EN::MESH_ENTITIES::const_iterator currentEntity; 
277         for (int i=0; i<numberOfMeshes; i++) 
278         {
279                 ::MESH * ptrMesh = _med->getMesh(meshesNames[i]);
280
281                 SCRUTE(ptrMesh);
282
283                 for (currentEntity = MED_EN::meshEntities.begin();
284                      currentEntity != MED_EN::meshEntities.end(); 
285                      currentEntity++) 
286                 {
287                      MESSAGE(LOC << ": for entity " << (*currentEntity).first);
288                 // family :
289                      familyVector = ptrMesh->getFamilies((MED_EN::medEntityMesh)(*currentEntity).first);
290                      int nb = familyVector.size();
291                      MESSAGE(LOC << ": there is(are) " << nb << " family(ies)");
292                      for (familyVectorIt = familyVector.begin();
293                           familyVectorIt != familyVector.end();
294                           familyVectorIt++) 
295                      {
296                           FAMILY_i * myFamilyI = new FAMILY_i(*familyVectorIt);
297                           SALOME_MED::FAMILY_ptr myFamilyIOR = myFamilyI->POA_SALOME_MED::FAMILY::_this();
298                            myFamilyI->addInStudy(myStudy,myFamilyIOR);
299                      }
300
301                // group :
302                      groupVector = ptrMesh->getGroups((MED_EN::medEntityMesh)(*currentEntity).first);
303                      nb = groupVector.size();
304                      MESSAGE(LOC << ": there is(are) " << nb << " group(s)");
305                     for (groupVectorIt = groupVector.begin();
306                          groupVectorIt != groupVector.end();
307                          groupVectorIt++) 
308                     {
309                          GROUP_i * myGroupI = new GROUP_i(*groupVectorIt);
310                          SALOME_MED::GROUP_ptr myGroupIOR = myGroupI->POA_SALOME_MED::GROUP::_this();
311                          myGroupI->addInStudy(myStudy,myGroupIOR);
312                     }
313                 }      
314         }
315
316         for (int i=0; i<numberOfMeshes; i++) 
317         {
318             map<MED_EN::medEntityMesh,::SUPPORT*> mySupports = _med->getSupports(meshesNames[i]);
319             map<MED_EN::medEntityMesh,::SUPPORT*>::const_iterator itSupport;
320             map<MED_EN::medEntityMesh,SALOME_MED::SUPPORT_ptr> & 
321                         mySupportsIOR = _supports[meshesNames[i]];
322             for (itSupport=mySupports.begin(); itSupport!=mySupports.end(); itSupport++ ) 
323             {
324                  SUPPORT_i * mySupportI = new SUPPORT_i((*itSupport).second);
325                  SALOME_MED::SUPPORT_ptr mySupportIOR = mySupportI->_this();
326                  mySupportsIOR[(*itSupport).first]= mySupportIOR;
327                  mySupportI->addInStudy(myStudy,mySupportIOR);
328             }
329         }
330
331
332
333
334 //      for (int i=0; i<numberOfMeshes; i++) 
335 //        {
336 //          string meshName = meshesNames[i];
337 //          char * supportEntryPath;
338 //          int lenName;
339 //          string supportName;
340 //          SALOMEDS::SObject_var supportEntry;
341
342 //          supportName = "SupportOnAll_MED_MAILLE";
343 //          lenName = 13 + 15 + strlen(meshName.c_str()) + 1 + strlen(supportName.c_str());
344 //          supportEntryPath = new char[lenName];
345 //          supportEntryPath = strcpy(supportEntryPath,"/Med/MEDMESH/");
346 //          supportEntryPath = strcat(supportEntryPath,"MEDSUPPORTS_OF_");
347 //          supportEntryPath = strcat(supportEntryPath,meshName.c_str());
348 //          supportEntryPath = strcat(supportEntryPath,"/");
349 //          supportEntryPath = strcat(supportEntryPath,supportName.c_str());
350
351 //          SCRUTE(supportEntryPath);
352
353 //          cout << "supportEntryPath in Med " << supportEntryPath << " length " << lenName << endl;
354
355 //          supportEntry = myStudy->FindObjectByPath(supportEntryPath);
356
357 //          if ( CORBA::is_nil(supportEntry) ) 
358 //            cout << "The reuse in Med is OK " << endl;
359 //          else 
360 //            cout << "the reuse in Med is not OK and there was a problem in the storage in the study" << endl;
361 //          delete [] supportEntryPath;
362
363
364
365 //          supportName = "SupportOnAll_MED_FACE";
366 //          lenName = 13 + 15 + strlen(meshName.c_str()) + 1 + strlen(supportName.c_str());
367 //          supportEntryPath = new char[lenName];
368 //          supportEntryPath = strcpy(supportEntryPath,"/Med/MEDMESH/");
369 //          supportEntryPath = strcat(supportEntryPath,"MEDSUPPORTS_OF_");
370 //          supportEntryPath = strcat(supportEntryPath,meshName.c_str());
371 //          supportEntryPath = strcat(supportEntryPath,"/");
372 //          supportEntryPath = strcat(supportEntryPath,supportName.c_str());
373
374 //          SCRUTE(supportEntryPath);
375
376 //          cout << "supportEntryPath in Med " << supportEntryPath << " length " << lenName << endl;
377
378 //          supportEntry = myStudy->FindObjectByPath(supportEntryPath);
379
380 //          if ( CORBA::is_nil(supportEntry) ) 
381 //            cout << "The reuse in Med is OK " << endl;
382 //          else 
383 //            cout << "the reuse in Med is not OK and there was a problem in the storage in the study" << endl;
384 //          delete [] supportEntryPath;
385
386
387
388 //          supportName = "SupportOnAll_MED_ARETE";
389 //          lenName = 13 + 15 + strlen(meshName.c_str()) + 1 + strlen(supportName.c_str());
390 //          supportEntryPath = new char[lenName];
391 //          supportEntryPath = strcpy(supportEntryPath,"/Med/MEDMESH/");
392 //          supportEntryPath = strcat(supportEntryPath,"MEDSUPPORTS_OF_");
393 //          supportEntryPath = strcat(supportEntryPath,meshName.c_str());
394 //          supportEntryPath = strcat(supportEntryPath,"/");
395 //          supportEntryPath = strcat(supportEntryPath,supportName.c_str());
396
397 //          SCRUTE(supportEntryPath);
398
399 //          cout << "supportEntryPath in Med " << supportEntryPath << " length " << lenName << endl;
400
401 //          supportEntry = myStudy->FindObjectByPath(supportEntryPath);
402
403 //          if ( CORBA::is_nil(supportEntry) ) 
404 //            cout << "The reuse in Med is OK " << endl;
405 //          else 
406 //            cout << "the reuse in Med is not OK and there was a problem in the storage in the study" << endl;
407 //          delete [] supportEntryPath;
408
409
410
411
412 //          supportName = "SupportOnAll_MED_NOEUD";
413 //          lenName = 13 + 15 + strlen(meshName.c_str()) + 1 + strlen(supportName.c_str());
414 //          supportEntryPath = new char[lenName];
415 //          supportEntryPath = strcpy(supportEntryPath,"/Med/MEDMESH/");
416 //          supportEntryPath = strcat(supportEntryPath,"MEDSUPPORTS_OF_");
417 //          supportEntryPath = strcat(supportEntryPath,meshName.c_str());
418 //          supportEntryPath = strcat(supportEntryPath,"/");
419 //          supportEntryPath = strcat(supportEntryPath,supportName.c_str());
420
421 //          SCRUTE(supportEntryPath);
422
423 //          cout << "supportEntryPath in Med " << supportEntryPath << " length " << lenName << endl;
424
425 //          supportEntry = myStudy->FindObjectByPath(supportEntryPath);
426
427 //          if ( CORBA::is_nil(supportEntry) ) 
428 //            cout << "The reuse in Med is OK " << endl;
429 //          else 
430 //            cout << "the reuse in Med is not OK and there was a problem in the storage in the study" << endl;
431 //          delete [] supportEntryPath;
432
433
434
435
436 //        }
437
438
439
440
441
442
443
444   // FIELDS :
445         deque<string> fieldsNames = _med->getFieldNames();
446         int numberOfFields = fieldsNames.size();
447
448         SCRUTE(numberOfFields);
449
450         for (int i=0; i<numberOfFields; i++) 
451           {
452             MESSAGE("The name of the " << i << "^th fields is "<< fieldsNames[i]);
453
454             deque<DT_IT_> myIteration = _med->getFieldIteration (fieldsNames[i]);
455             int numberOfIteration = myIteration.size();
456
457             SCRUTE(numberOfIteration);
458
459             for (int j=0; j<numberOfIteration; j++) 
460               {
461                 MESSAGE("its " << i << "^th iteration is dt = " << myIteration[j].dt << " it = " << myIteration[j].it);
462               }
463           }
464
465         for (int i=0; i<numberOfFields; i++) 
466           {
467
468             MESSAGE("dealing with the fields collection " << fieldsNames[i]);
469
470              deque<DT_IT_> myIteration = _med->getFieldIteration (fieldsNames[i]);
471              int numberOfIteration = myIteration.size();
472
473              SCRUTE(numberOfIteration);
474
475              for (int j=0; j<numberOfIteration; j++) 
476              {
477                int dt = myIteration[j].dt;
478                int it = myIteration[j].it;
479
480                MESSAGE("Treatement of " << fieldsNames[i] << " dt = " << dt << " it = " << it);
481
482                   ::FIELD_ * myField = _med->getField(fieldsNames[i],dt,it);
483
484                   string meshName = myField->getSupport()->getMesh()->getName();
485                   medEntityMesh myEntity = myField->getSupport()->getEntity();
486                   map<string, map<MED_EN::medEntityMesh,SALOME_MED::SUPPORT_ptr> >::const_iterator 
487                         itSupportOnMesh = _supports.find(meshName);
488                   if (itSupportOnMesh == _supports.end() )
489                         throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
490                                          << "There is no support on mesh named |" 
491                                          << meshName << "|"));
492                   const map<MED_EN::medEntityMesh,SALOME_MED::SUPPORT_ptr> & SupportOnMesh 
493                                 = (*itSupportOnMesh).second;
494                   map<MED_EN::medEntityMesh,SALOME_MED::SUPPORT_ptr>::const_iterator itSupport
495                                 = SupportOnMesh.find(myEntity);
496                   if (itSupport == SupportOnMesh.end() )
497                         throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
498                                          << "There is no support on entity "
499                                          << entity << " in mesh named |" 
500                                          << meshName << "|"));
501                   SALOME_MED::SUPPORT_ptr mySupportIOR = (*itSupport).second;
502                   med_type_champ type = myField->getValueType();
503
504                   DT_IT_ dtIt;
505                   dtIt.dt  = dt;
506                   dtIt.it  = it;
507
508                   switch (type) 
509                   {
510                      case MED_EN::MED_INT32: 
511                      {
512                         ((FIELD<int>*)myField)->read();
513                         FIELDINT_i * myFieldIntI = new FIELDINT_i((FIELD<int>*)myField);
514                         SALOME_MED::FIELDINT_ptr myFieldIntIOR;
515                         myFieldIntIOR = myFieldIntI->_this();
516
517                         MESSAGE(LOC << " add in study of the field " << fieldsNames[i].c_str() << " dt = " << dtIt.dt << " it = " << dtIt.it);
518
519                         myFieldIntI->addInStudy(myStudy,myFieldIntIOR);
520                         _fields[fieldsNames[i]][dtIt] = myFieldIntIOR;
521                         break;
522                      }
523
524                      case MED_EN::MED_REEL64: 
525                      {
526                         ((FIELD<double>*)myField)->read();
527                         FIELDDOUBLE_i * myFieldDoubleI = new FIELDDOUBLE_i((FIELD<double>*)myField);
528                         SALOME_MED::FIELDDOUBLE_ptr myFieldDoubleIOR;
529                         myFieldDoubleIOR = myFieldDoubleI->_this();
530
531                         MESSAGE(LOC << " add in study of the field " << fieldsNames[i].c_str() << " dt = " << dtIt.dt << " it = " << dtIt.it);
532
533                         myFieldDoubleI->addInStudy(myStudy,myFieldDoubleIOR);
534                         _fields[fieldsNames[i]][dtIt] = myFieldDoubleIOR;
535                         break;
536                      }
537
538                     default: 
539                     {
540                         throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
541                                          <<"   * Iteration "<<FieldIteration[j].dt
542                                          <<", order number "<<FieldIteration[j].it
543                                          <<" has wrong type : "<<type));
544                         break;
545                      }
546                   }
547              }
548
549              MESSAGE("Here we are i="<< i);
550         }
551   
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566 //      for (int i=0; i<numberOfMeshes; i++) 
567 //        {
568 //          string meshName = meshesNames[i];
569 //          char * supportEntryPath;
570 //          int lenName;
571 //          string supportName;
572 //          SALOMEDS::SObject_var supportEntry;
573
574 //          supportName = "SupportOnAll_MED_MAILLE";
575 //          lenName = 13 + 15 + strlen(meshName.c_str()) + 1 + strlen(supportName.c_str());
576 //          supportEntryPath = new char[lenName];
577 //          supportEntryPath = strcpy(supportEntryPath,"/Med/MEDMESH/");
578 //          supportEntryPath = strcat(supportEntryPath,"MEDSUPPORTS_OF_");
579 //          supportEntryPath = strcat(supportEntryPath,meshName.c_str());
580 //          supportEntryPath = strcat(supportEntryPath,"/");
581 //          supportEntryPath = strcat(supportEntryPath,supportName.c_str());
582
583 //          SCRUTE(supportEntryPath);
584
585 //          cout << "supportEntryPath in Med " << supportEntryPath << " length " << lenName << endl;
586
587 //          supportEntry = myStudy->FindObjectByPath(supportEntryPath);
588
589 //          if ( CORBA::is_nil(supportEntry) ) 
590 //            cout << "The reuse in Med is OK " << endl;
591 //          else 
592 //            cout << "the reuse in Med is not OK and there was a problem in the storage in the study" << endl;
593 //          delete [] supportEntryPath;
594
595
596
597 //          supportName = "SupportOnAll_MED_FACE";
598 //          lenName = 13 + 15 + strlen(meshName.c_str()) + 1 + strlen(supportName.c_str());
599 //          supportEntryPath = new char[lenName];
600 //          supportEntryPath = strcpy(supportEntryPath,"/Med/MEDMESH/");
601 //          supportEntryPath = strcat(supportEntryPath,"MEDSUPPORTS_OF_");
602 //          supportEntryPath = strcat(supportEntryPath,meshName.c_str());
603 //          supportEntryPath = strcat(supportEntryPath,"/");
604 //          supportEntryPath = strcat(supportEntryPath,supportName.c_str());
605
606 //          SCRUTE(supportEntryPath);
607
608 //          cout << "supportEntryPath in Med " << supportEntryPath << " length " << lenName << endl;
609
610 //          supportEntry = myStudy->FindObjectByPath(supportEntryPath);
611
612 //          if ( CORBA::is_nil(supportEntry) ) 
613 //            cout << "The reuse in Med is OK " << endl;
614 //          else 
615 //            cout << "the reuse in Med is not OK and there was a problem in the storage in the study" << endl;
616 //          delete [] supportEntryPath;
617
618
619
620 //          supportName = "SupportOnAll_MED_ARETE";
621 //          lenName = 13 + 15 + strlen(meshName.c_str()) + 1 + strlen(supportName.c_str());
622 //          supportEntryPath = new char[lenName];
623 //          supportEntryPath = strcpy(supportEntryPath,"/Med/MEDMESH/");
624 //          supportEntryPath = strcat(supportEntryPath,"MEDSUPPORTS_OF_");
625 //          supportEntryPath = strcat(supportEntryPath,meshName.c_str());
626 //          supportEntryPath = strcat(supportEntryPath,"/");
627 //          supportEntryPath = strcat(supportEntryPath,supportName.c_str());
628
629 //          SCRUTE(supportEntryPath);
630
631 //          cout << "supportEntryPath in Med " << supportEntryPath << " length " << lenName << endl;
632
633 //          supportEntry = myStudy->FindObjectByPath(supportEntryPath);
634
635 //          if ( CORBA::is_nil(supportEntry) ) 
636 //            cout << "The reuse in Med is OK " << endl;
637 //          else 
638 //            cout << "the reuse in Med is not OK and there was a problem in the storage in the study" << endl;
639 //          delete [] supportEntryPath;
640
641
642
643
644 //          supportName = "SupportOnAll_MED_NOEUD";
645 //          lenName = 13 + 15 + strlen(meshName.c_str()) + 1 + strlen(supportName.c_str());
646 //          supportEntryPath = new char[lenName];
647 //          supportEntryPath = strcpy(supportEntryPath,"/Med/MEDMESH/");
648 //          supportEntryPath = strcat(supportEntryPath,"MEDSUPPORTS_OF_");
649 //          supportEntryPath = strcat(supportEntryPath,meshName.c_str());
650 //          supportEntryPath = strcat(supportEntryPath,"/");
651 //          supportEntryPath = strcat(supportEntryPath,supportName.c_str());
652
653 //          SCRUTE(supportEntryPath);
654
655 //          cout << "supportEntryPath in Med " << supportEntryPath << " length " << lenName << endl;
656
657 //          supportEntry = myStudy->FindObjectByPath(supportEntryPath);
658
659 //          if ( CORBA::is_nil(supportEntry) ) 
660 //            cout << "The reuse in Med is OK " << endl;
661 //          else 
662 //            cout << "the reuse in Med is not OK and there was a problem in the storage in the study" << endl;
663 //          delete [] supportEntryPath;
664
665
666
667
668 //        }
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684         END_OF(LOC);
685 }
686
687 //=============================================================================
688 /*!
689  * Destructor
690  */
691 //=============================================================================
692 MED_i::~MED_i()
693 {
694   //delete _med;
695 }
696
697 //=======================================================================
698 //function : updateSupportIORs
699 //purpose  : 
700 //=======================================================================
701
702 void MED_i::updateSupportIORs(SALOMEDS::Study_ptr myStudy, const char* meshName)
703 {
704   vector<FAMILY*> familyVector;
705   vector<FAMILY*>::iterator familyVectorIt;
706   vector<GROUP*> groupVector;
707   vector<GROUP*>::iterator groupVectorIt;
708   string supportEntryPath;
709
710   ::MESH * ptrMesh = _med->getMesh(meshName);
711
712   MED_EN::MESH_ENTITIES::const_iterator currentEntity; 
713
714   for (currentEntity = MED_EN::meshEntities.begin();
715        currentEntity != MED_EN::meshEntities.end(); 
716        currentEntity++) 
717   {
718     // family :
719     familyVector = ptrMesh->getFamilies((MED_EN::medEntityMesh)(*currentEntity).first);
720     int nb = familyVector.size();
721     for (familyVectorIt = familyVector.begin();
722          familyVectorIt != familyVector.end();
723          familyVectorIt++) 
724     {
725       supportEntryPath = SUPPORT_i::getEntryPath( meshName, *familyVectorIt );
726       SALOMEDS::SObject_var familyEntry = myStudy->FindObjectByPath(supportEntryPath.c_str());
727       if ( !familyEntry->_is_nil() || CORBA::is_nil( familyEntry->GetObject() ))
728       {
729         FAMILY_i * myFamilyI = new FAMILY_i(*familyVectorIt);
730         SALOME_MED::FAMILY_ptr myFamilyIOR = myFamilyI->POA_SALOME_MED::FAMILY::_this();
731         myFamilyI->addInStudy(myStudy,myFamilyIOR);
732       }
733     }
734
735     // group :
736     groupVector = ptrMesh->getGroups((MED_EN::medEntityMesh)(*currentEntity).first);
737     nb = groupVector.size();
738     for (groupVectorIt = groupVector.begin();
739          groupVectorIt != groupVector.end();
740          groupVectorIt++) 
741     {
742       supportEntryPath = SUPPORT_i::getEntryPath( meshName, *groupVectorIt );
743       SALOMEDS::SObject_var groupEntry = myStudy->FindObjectByPath(supportEntryPath.c_str());
744       if ( !groupEntry->_is_nil() || CORBA::is_nil( groupEntry->GetObject() ))
745       {
746         GROUP_i * myGroupI = new GROUP_i(*groupVectorIt);
747         SALOME_MED::GROUP_ptr myGroupIOR = myGroupI->POA_SALOME_MED::GROUP::_this();
748         myGroupI->addInStudy(myStudy,myGroupIOR);
749       }
750     }
751   }  
752
753   _med->updateSupport();
754
755   // supports
756   map<MED_EN::medEntityMesh,::SUPPORT*> mySupports = _med->getSupports(meshName);
757   map<MED_EN::medEntityMesh,::SUPPORT*>::const_iterator itSupport;
758   map<MED_EN::medEntityMesh,SALOME_MED::SUPPORT_ptr> & 
759     mySupportsIOR = _supports[meshName];
760   for (itSupport=mySupports.begin(); itSupport!=mySupports.end(); itSupport++ ) 
761   {
762     supportEntryPath = SUPPORT_i::getEntryPath( meshName, itSupport->second);
763     SALOMEDS::SObject_var supportEntry = myStudy->FindObjectByPath(supportEntryPath.c_str());
764     if ( !supportEntry->_is_nil() || CORBA::is_nil( supportEntry->GetObject() ))
765     {
766       SUPPORT_i * mySupportI = new SUPPORT_i((*itSupport).second);
767       SALOME_MED::SUPPORT_ptr mySupportIOR = mySupportI->_this();
768       mySupportsIOR[(*itSupport).first]= mySupportIOR;
769       mySupportI->addInStudy(myStudy,mySupportIOR);
770     }
771   }
772 }
773
774
775 //=============================================================================
776 /*!
777  * CORBA: Accessor for Number of meshes
778  */
779 //=============================================================================
780 CORBA::Long MED_i::getNumberOfMeshes() 
781 throw (SALOME::SALOME_Exception)
782 {
783         if (_med==NULL)
784                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
785                                              SALOME::INTERNAL_ERROR);
786         try
787         {
788                 return _med->getNumberOfMeshes();
789         }
790         catch (MEDEXCEPTION &ex)
791         {
792                 MESSAGE("Unable to get the number of meshes in Med Object");
793                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
794         }
795 }
796 //=============================================================================
797 /*!
798  * CORBA: Accessor for Number of fields
799  */
800 //=============================================================================
801 CORBA::Long MED_i::getNumberOfFields() 
802 throw (SALOME::SALOME_Exception)
803 {
804         if (_med==NULL)
805                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
806                                              SALOME::INTERNAL_ERROR);
807         try
808         {
809                 return _med->getNumberOfFields();
810         }
811         catch (MEDEXCEPTION &ex)
812         {
813                 MESSAGE("Unable to get the number of fields in Med Object");
814                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
815         }
816 }
817 //=============================================================================
818 /*!
819  * CORBA: Accessor for meshes names
820  */
821 //=============================================================================
822 SALOME_MED::string_array * MED_i::getMeshNames()      
823 throw (SALOME::SALOME_Exception)
824 {
825         if (_med==NULL)
826                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
827                                              SALOME::INTERNAL_ERROR);
828         SALOME_MED::string_array_var myseq = new SALOME_MED::string_array;
829         try
830         {
831                 int nbMeshes=_med->getNumberOfMeshes();
832                 myseq->length(nbMeshes);
833                 string * nameMeshes = new string[nbMeshes];
834                 _med->getMeshNames(nameMeshes);
835                 for (int i=0;i<nbMeshes;i++)
836                 {
837                         myseq[i]=CORBA::string_dup(nameMeshes[i].c_str());
838                 }
839                 delete [] nameMeshes;
840                 nameMeshes=NULL;
841         }
842         catch (MEDEXCEPTION &ex)
843         {
844                 MESSAGE("Unable to get the names of the meshes in Med Object");
845                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
846         }
847         return myseq._retn();
848 }
849 //=============================================================================
850 /*!
851  * CORBA: Accessor for fields names
852  */
853 //=============================================================================
854 SALOME_MED::string_array * MED_i::getFieldNames()     
855   throw (SALOME::SALOME_Exception)
856 {
857   if (_med==NULL)
858     THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
859                                              SALOME::INTERNAL_ERROR);
860   SALOME_MED::string_array_var myseq = new SALOME_MED::string_array;
861   try
862     {
863       deque<string> nameFields = _med->getFieldNames();
864       int nbFields = nameFields.size();
865       myseq->length(nbFields);
866
867       for (int i=0;i<nbFields;i++)
868         {
869           myseq[i]=CORBA::string_dup(nameFields[i].c_str());
870         }
871     }
872   catch (MEDEXCEPTION &ex)
873     {
874       MESSAGE("Unable to get the names of the fields in Med Object");
875       THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
876     }
877   return myseq._retn();
878 }
879 //=============================================================================
880 /*!
881  * CORBA: Accessor for a specific mesh
882  */
883 //=============================================================================
884 SALOME_MED::MESH_ptr MED_i::getMeshByName(const char* meshName) 
885 throw (SALOME::SALOME_Exception)
886 {
887         if (_med==NULL)
888                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
889                                              SALOME::INTERNAL_ERROR);
890         SALOME_MED::MESH_ptr meshIOR;
891         try
892         {
893           map<string,SALOME_MED::MESH_ptr>::const_iterator name_meshIOR =
894             _meshes.find( meshName );
895           if ( name_meshIOR == _meshes.end() ) {
896                 MESH * mesh=_med->getMesh(meshName);
897                 MESH_i * m1 = new MESH_i(mesh);
898                 meshIOR = m1->POA_SALOME_MED::MESH::_this();
899                 _meshes[ meshName ] = meshIOR;
900           }
901           else {
902             meshIOR = name_meshIOR->second;
903           }
904         }
905         catch (MEDEXCEPTION &ex)
906         {
907                 MESSAGE("Unable to get the specified mesh");
908                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
909         }
910         return meshIOR;
911 }
912 //=============================================================================
913 /*!
914  * CORBA: Accessor for a specific mesh
915  */
916 //=============================================================================
917 SALOME_MED::MESH_ptr MED_i::getMesh(SALOME_MED::FIELD_ptr fieldPtr) 
918 throw (SALOME::SALOME_Exception)
919 {
920         if (_med==NULL)
921                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
922                                              SALOME::INTERNAL_ERROR);
923         int ind=fieldPtr->getCorbaIndex();
924         SCRUTE(ind);
925
926         MESH * mesh;
927         SALOME_MED::FIELDINT_var fieldint =SALOME_MED::FIELDINT::_narrow(fieldPtr);
928         if (CORBA::is_nil(fieldint))
929         {
930                 MESSAGE("Double");
931                 SALOME_MED::FIELDDOUBLE_var fielddouble = 
932                             SALOME_MED::FIELDDOUBLE::_narrow(fieldPtr);
933                 ASSERT(!CORBA::is_nil(fielddouble));
934
935                 ASSERT(FIELD_i::fieldMap.find(ind)!=FIELD_i::fieldMap.end());
936
937                 ::FIELD<double> * fdouble = (::FIELD<double> *)FIELD_i::fieldMap[ind];
938                 mesh=_med->getMesh(fdouble);
939         }
940         else
941         {
942                 MESSAGE("Integer");
943                 ASSERT(FIELD_i::fieldMap.find(ind)!=FIELD_i::fieldMap.end());
944
945                 ::FIELD<int> * fint = (::FIELD<int> *)FIELD_i::fieldMap[ind];
946                 mesh=_med->getMesh(fint);
947         }
948         MESH_i * meshi = new MESH_i(mesh);
949         return meshi->POA_SALOME_MED::MESH::_this();
950
951 }
952 //=============================================================================
953 /*!
954  * CORBA: Accessor for Number of iteration of a given field by its name.
955  */
956 //=============================================================================
957 CORBA::Long MED_i::getFieldNumberOfIteration(const char* fieldName) 
958   throw (SALOME::SALOME_Exception)
959 {
960   if (_med==NULL)
961     THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
962                                  SALOME::INTERNAL_ERROR);
963
964   try
965     {
966       deque<DT_IT_> fieldIteration = _med->getFieldIteration(string(fieldName));
967       return fieldIteration.size();
968     }
969   catch (MEDEXCEPTION &ex)
970     {
971       MESSAGE("Unable to get the number of iteration of the field " << fieldName << " in Med Object");
972       THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
973     }
974 }
975 //=============================================================================
976 /*!
977  * CORBA: Accessor for the Corba equivalent DT_IT_ (see MEDMEM direcrtory)
978  * of a given field by its name and its number (sequence of two long integers).
979  */
980 //=============================================================================
981 SALOME_MED::long_array * MED_i::getFieldIteration(const char* fieldName,CORBA::Long i) 
982   throw (SALOME::SALOME_Exception)
983 {
984   if (_med==NULL)
985     THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
986                                  SALOME::INTERNAL_ERROR);
987
988   SALOME_MED::long_array_var myseq = new SALOME_MED::long_array;
989   try
990     {
991       deque<DT_IT_> fieldIteration = _med->getFieldIteration(string(fieldName));
992       int numberOfIteration = fieldIteration.size();
993
994       if (i < 0 || i >=numberOfIteration)
995         THROW_SALOME_CORBA_EXCEPTION("The integer i should be geater or equal to 0 and lesser then numberOfIteration", \
996                                      SALOME::INTERNAL_ERROR);
997
998       myseq->length(2);
999       myseq[0] = fieldIteration[i].dt;
1000       myseq[1] = fieldIteration[i].it;
1001       return myseq._retn();
1002     }
1003   catch (MEDEXCEPTION &ex)
1004     {
1005       MESSAGE("Unable to get the sequence of DT_IT of the given field");
1006       THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
1007     }
1008 }
1009 //=============================================================================
1010 /*!
1011  * CORBA: Accessor for the list of Corba equivalent DT_IT_ (see MEDMEM direcrtory)
1012  * of a given field by its name (sequence of two by iteration number long integers).
1013  */
1014 //=============================================================================
1015 SALOME_MED::long_array * MED_i::getFieldIterations(const char* fieldName) 
1016   throw (SALOME::SALOME_Exception)
1017 {
1018   if (_med==NULL)
1019     THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
1020                                  SALOME::INTERNAL_ERROR);
1021
1022   SALOME_MED::long_array_var myseq = new SALOME_MED::long_array;
1023   try
1024     {
1025       deque<DT_IT_> fieldIteration = _med->getFieldIteration(string(fieldName));
1026       int numberOfIteration = fieldIteration.size();
1027       int size = 2*numberOfIteration;
1028
1029       myseq->length(size);
1030
1031       for (int i=0; i<numberOfIteration; i++)
1032         {
1033           int index = 2*i;
1034           myseq[index] = fieldIteration[i].dt;
1035           index = index+1;
1036           myseq[index] = fieldIteration[i].it;
1037         }
1038
1039       return myseq._retn();
1040     }
1041   catch (MEDEXCEPTION &ex)
1042     {
1043       MESSAGE("Unable to get the sequence of DT_IT of the given field");
1044       THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
1045     }
1046 }
1047 //=============================================================================
1048 /*!
1049  * CORBA: Accessor for a specific field
1050  */
1051 //=============================================================================
1052 SALOME_MED::FIELD_ptr MED_i::getField(const char* fieldName, 
1053                                       CORBA::Long pasTemps, 
1054                                       CORBA::Long numOrdre ) 
1055 throw (SALOME::SALOME_Exception)
1056 {
1057         const char * LOC="MED_i::getField(const char*,CORBA::Long,CORBA::Long) ";
1058         BEGIN_OF(LOC);
1059         if (_med==NULL)
1060                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
1061                                              SALOME::INTERNAL_ERROR);
1062
1063         SALOME_MED::FIELD_ptr myFieldIOR = SALOME_MED::FIELD::_nil();
1064
1065         DT_IT_ dtIt;
1066
1067         dtIt.dt= (int)pasTemps;
1068         dtIt.it= (int)numOrdre;
1069
1070         map<string,MAP_IOR_DT_IT_>::const_iterator itFields = _fields.find(fieldName);
1071         if ( itFields != _fields.end() )
1072         {
1073           const MAP_IOR_DT_IT_ & map_dtIt = (*itFields).second;
1074           MAP_IOR_DT_IT_::const_iterator itMap_dtIt =  map_dtIt.find(dtIt);
1075           if ( itMap_dtIt != map_dtIt.end() )
1076             myFieldIOR = (*itMap_dtIt).second;
1077         }
1078
1079         if ( CORBA::is_nil( myFieldIOR ))
1080         {
1081           try
1082           {
1083             ::FIELD_* myField = _med->getField( fieldName, pasTemps, numOrdre);
1084
1085             switch ( myField->getValueType() ) {
1086             case MED_EN::MED_INT32 : {
1087               FIELDINT_i * myFieldIntI = new FIELDINT_i((FIELD<int>*)myField);
1088               myFieldIOR = myFieldIntI->_this();
1089               break;
1090             }
1091
1092             case MED_EN::MED_REEL64: {
1093               FIELDDOUBLE_i * myFieldDoubleI = new FIELDDOUBLE_i((FIELD<double>*)myField);
1094               myFieldIOR = myFieldDoubleI->_this();
1095               break;
1096             }
1097             default: 
1098               THROW_SALOME_CORBA_EXCEPTION ("Wrong field type", SALOME::INTERNAL_ERROR);
1099             }
1100           }
1101           catch (const std::exception & ex)
1102           {
1103             MESSAGE("Exception Interceptee : ");
1104             SCRUTE(ex.what());
1105             THROW_SALOME_CORBA_EXCEPTION("Field not found !", SALOME::INTERNAL_ERROR);
1106           }
1107           _fields[fieldName][dtIt] = myFieldIOR;
1108         }
1109
1110         END_OF(LOC);
1111         return myFieldIOR;
1112 }
1113 //=============================================================================
1114 /*!
1115  * CORBA: Accessor for a specific field
1116  */
1117 //=============================================================================
1118 CORBA::Long MED_i::addDriver(SALOME_MED::medDriverTypes driverType, const char* fileName) 
1119 throw (SALOME::SALOME_Exception)
1120 {
1121         if (_med==NULL)
1122                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
1123                                              SALOME::INTERNAL_ERROR);
1124         try
1125         {
1126                 int driv=_med->addDriver(convertIdlDriverToMedDriver(driverType),
1127                                         fileName);
1128                 return driv;
1129         }
1130         catch (MEDEXCEPTION &ex)
1131         {
1132                 MESSAGE("Unable to add a driver to the med object");
1133                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
1134         }
1135         
1136 }
1137 //=============================================================================
1138 /*!
1139  * CORBA: relase the driver (mandatory)
1140  */
1141 //=============================================================================
1142 void MED_i::rmDriver(CORBA::Long i) 
1143 throw (SALOME::SALOME_Exception)
1144 {
1145         if (_med==NULL)
1146                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
1147                                              SALOME::INTERNAL_ERROR);
1148         try
1149         {
1150                 _med->rmDriver(i);
1151         }
1152         catch (MEDEXCEPTION &ex)
1153         {
1154                 MESSAGE("Unable to unlink the driver from the med object");
1155                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
1156         }
1157 }
1158 //=============================================================================
1159 /*!
1160  * CORBA: read med objects relations
1161  */
1162 //=============================================================================
1163 void MED_i::readFileStruct(CORBA::Long i) 
1164 throw (SALOME::SALOME_Exception)
1165 {
1166         if (_med==NULL)
1167                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
1168                                              SALOME::INTERNAL_ERROR);
1169         try
1170         {
1171                 _med->readFileStruct(i);
1172         }
1173         catch (MEDEXCEPTION &ex)
1174         {
1175                 MESSAGE("Unable to read the structure of this file ");
1176                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
1177         }
1178 }
1179 //=============================================================================
1180 /*!
1181  * CORBA: write specifics objects f
1182  */
1183 //=============================================================================
1184 void MED_i::writeFrom(CORBA::Long i) 
1185 throw (SALOME::SALOME_Exception)
1186 {
1187         if (_med==NULL)
1188                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
1189                                              SALOME::INTERNAL_ERROR);
1190         try
1191         {
1192                 _med->writeFrom(i);
1193         }
1194         catch (MEDEXCEPTION &ex)
1195         {
1196                 MESSAGE("Unable to write this driver ");
1197                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
1198         }
1199 }
1200 //=============================================================================
1201 /*!
1202  * CORBA: write objects
1203  */
1204 //=============================================================================
1205 void MED_i::write(CORBA::Long i) 
1206 throw (SALOME::SALOME_Exception)
1207 {
1208         if (_med==NULL)
1209                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
1210                                              SALOME::INTERNAL_ERROR);
1211         try
1212         {
1213                 _med->write(i);
1214         }
1215         catch (MEDEXCEPTION &ex)
1216         {
1217                 MESSAGE("Unable to write this file ");
1218                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
1219         }
1220 }
1221 //=============================================================================
1222 /*!
1223  * CORBA: Add Mesh in Med objects
1224  */
1225 //=============================================================================
1226 void MED_i::addMesh(SALOME_MED::MESH_ptr ptrMesh) 
1227 throw (SALOME::SALOME_Exception)
1228 {
1229         if (_med==NULL)
1230                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
1231                                              SALOME::INTERNAL_ERROR);
1232         try
1233         {
1234                 int ind=ptrMesh->getCorbaIndex();
1235                 SCRUTE(ind);
1236                 ASSERT(MESH_i::meshMap.find(ind) !=MESH_i::meshMap.end());
1237                 MESH * mesh= MESH_i::meshMap[ind];
1238                 _med->addMesh(mesh);
1239         }
1240         catch (MEDEXCEPTION &ex)
1241         {
1242                 MESSAGE("Unable to link this mesh to this med object");
1243                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
1244         }
1245 }
1246 //=============================================================================
1247 /*!
1248  * CORBA: Add field in Med objects
1249  */
1250 //=============================================================================
1251 void MED_i::addField(SALOME_MED::FIELD_ptr ptrField) 
1252 throw (SALOME::SALOME_Exception)
1253 {
1254         if (_med==NULL)
1255                 THROW_SALOME_CORBA_EXCEPTION("No associated Med object",\
1256                                              SALOME::INTERNAL_ERROR);
1257         int ind=ptrField->getCorbaIndex();
1258         SCRUTE(ind);
1259
1260         SALOME_MED::FIELDINT_var fieldint =SALOME_MED::FIELDINT::_narrow(ptrField);
1261         if (CORBA::is_nil(fieldint))
1262         {
1263                 MESSAGE("Double");
1264                 SALOME_MED::FIELDDOUBLE_var fielddouble = 
1265                             SALOME_MED::FIELDDOUBLE::_narrow(ptrField);
1266                 ASSERT(!CORBA::is_nil(fielddouble));
1267
1268                 ASSERT(FIELD_i::fieldMap.find(ind)!=FIELD_i::fieldMap.end());
1269
1270                 ::FIELD<double> * fdouble = (::FIELD<double> *)FIELD_i::fieldMap[ind];
1271                 // A modifier
1272                 //_med->addField(fdouble);
1273         }
1274         else
1275         {
1276                 MESSAGE("Integer");
1277                 ASSERT(FIELD_i::fieldMap.find(ind)!=FIELD_i::fieldMap.end());
1278                 ::FIELD<int> * fint = (::FIELD<int> *)FIELD_i::fieldMap[ind];
1279                 //_med->addField(fint);
1280         }
1281 }
1282 //=============================================================================
1283 /*!
1284  * CORBA: Add Med object in Study 
1285  */
1286 //=============================================================================
1287 void MED_i::addInStudy(SALOMEDS::Study_ptr myStudy, SALOME_MED::MED_ptr myIor) 
1288 throw (SALOME::SALOME_Exception,SALOMEDS::StudyBuilder::LockProtection)
1289 {
1290         BEGIN_OF("MED_Mesh_i::addInStudy");
1291         if ( _medId != "" )
1292         {
1293                 MESSAGE("Med already in Study");
1294                     THROW_SALOME_CORBA_EXCEPTION("Med already in Study", \
1295                                  SALOME::BAD_PARAM);
1296         };
1297
1298         SALOMEDS::StudyBuilder_var     myBuilder = myStudy->NewBuilder();
1299         myBuilder->NewCommand();
1300         SALOMEDS::GenericAttribute_var anAttr;
1301         SALOMEDS::AttributeName_var    aName;
1302         SALOMEDS::AttributeIOR_var     aIOR;
1303
1304         // Create SComponent labelled 'MED' if it doesn't already exit
1305         SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
1306         if ( CORBA::is_nil(medfather) )
1307         {
1308           THROW_SALOME_CORBA_EXCEPTION("Component Med not found",
1309                                        SALOME::BAD_PARAM);
1310         };
1311
1312         MESSAGE("Add a MED Object under Med");
1313         SALOMEDS::SObject_var newObj = myBuilder->NewObject(medfather);
1314
1315         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
1316         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
1317         CORBA::ORB_var &orb = init(0,0);
1318         string iorStr = orb->object_to_string(myIor);
1319         anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
1320         aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1321         aIOR->SetValue(iorStr.c_str());
1322         anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeName");
1323         aName = SALOMEDS::AttributeName::_narrow(anAttr);
1324         aName->SetValue("Objet MED");
1325         _medId = newObj->GetID();
1326         myBuilder->CommitCommand();
1327
1328         // register the Corba pointer: increase the referrence count
1329         MESSAGE("Registering of the Corba Med pointer");
1330         Register();
1331
1332         END_OF("Med_i::addInStudy(SALOMEDS::Study_ptr myStudy)");
1333 }
1334
1335 //=============================================================================
1336 /*!
1337  * CORBA: Add Med object in Study with a name medObjName
1338  */
1339 //=============================================================================
1340 void MED_i::addInStudy(SALOMEDS::Study_ptr myStudy, SALOME_MED::MED_ptr myIor,
1341                        const char * fileName) 
1342   throw (SALOME::SALOME_Exception,SALOMEDS::StudyBuilder::LockProtection)
1343 {
1344         BEGIN_OF("MED_i::addInStudy(myStudy, myIor, fileName)");
1345         if ( _medId != "" )
1346         {
1347                 MESSAGE("Med already in Study");
1348                     THROW_SALOME_CORBA_EXCEPTION("Med already in Study", \
1349                                  SALOME::BAD_PARAM);
1350         };
1351
1352         SALOMEDS::StudyBuilder_var     myBuilder = myStudy->NewBuilder();
1353         myBuilder->NewCommand();
1354         SALOMEDS::GenericAttribute_var anAttr;
1355         SALOMEDS::AttributeName_var    aName;
1356         SALOMEDS::AttributeIOR_var     aIOR;
1357
1358         // Create SComponent labelled 'MED' if it doesn't already exit
1359         SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
1360         if ( CORBA::is_nil(medfather) )
1361         {
1362           THROW_SALOME_CORBA_EXCEPTION("Component Med not found",
1363                                        SALOME::BAD_PARAM);
1364         };
1365
1366         MESSAGE("Add a MED Object under Med");
1367
1368         char * medObjName;
1369         string::size_type pos1=string(fileName).rfind('/');
1370         string::size_type lenFileName = strlen(fileName);
1371         string fileNameShort = string(fileName,pos1+1,lenFileName-pos1-1);
1372
1373         SCRUTE(fileNameShort);
1374
1375         int lenName = 21 + strlen(fileNameShort.c_str()) + 1;
1376         medObjName = new char[lenName];
1377         medObjName = strcpy(medObjName,"MED_OBJECT_FROM_FILE_");
1378         medObjName = strcat(medObjName,fileNameShort.c_str());
1379
1380         SALOMEDS::SObject_var newObj = myBuilder->NewObject(medfather);
1381
1382         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
1383         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting());
1384         CORBA::ORB_var &orb = init(0,0);
1385         string iorStr = orb->object_to_string(myIor);
1386         anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
1387         aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1388         aIOR->SetValue(iorStr.c_str());
1389         anAttr = myBuilder->FindOrCreateAttribute(newObj, "AttributeName");
1390         aName = SALOMEDS::AttributeName::_narrow(anAttr);
1391         aName->SetValue(medObjName);
1392         _medId = newObj->GetID();
1393         myBuilder->CommitCommand();
1394
1395         delete [] medObjName;
1396
1397 //      char * medObjName1;
1398 //      lenName = 26 + strlen(fileNameShort.c_str());
1399 //      medObjName1 = new char[lenName];
1400 //      medObjName1 = strcpy(medObjName1,"/MED/MED_OBJECT_FROM_FILE_");
1401 //      medObjName1 = strcat(medObjName1,fileNameShort.c_str());
1402
1403 //      SALOMEDS::SObject_var medEntry = myStudy->FindObjectByPath(medObjName1);
1404 //      if (!CORBA::is_nil(medEntry))
1405 //        {
1406 //          MESSAGE("MED_Mesh_i::addInStudy(myStudy, myIor, fileName) : The reuse in Med of Object MED from Study is OK");
1407 //        }
1408 //      else
1409 //        {
1410 //          MESSAGE("MED_Mesh_i::addInStudy(myStudy, myIor, fileName) : the reuse in Med of Object MED is not OK and there was a problem in the storage in the study");
1411 //        }
1412
1413 //      delete [] medObjName1;
1414
1415         // register the Corba pointer: increase the referrence count
1416         MESSAGE("Registering of the Corba Med pointer");
1417         Register();
1418
1419         END_OF("Med_i::addInStudy(myStudy, myIor, medObjName)");
1420 }