Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEM_I / MEDMEM_Field_i.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //=============================================================================
24 // File      : MEDMEM_Field_i.cxx
25 // Created   : mer fév 20 15:47:57 CET 2002
26 // Author    : EDF
27 // Project   : SALOME
28 //=============================================================================
29
30 #include "MEDMEM_Field_i.hxx"
31
32 #include "SALOME_NamingService.hxx"
33 #include "SALOME_LifeCycleCORBA.hxx"
34
35 #include CORBA_SERVER_HEADER(SALOME_ModuleCatalog)
36 #include CORBA_CLIENT_HEADER(MED_Gen)
37
38 using namespace MEDMEM;
39 using namespace MED_EN;
40
41 map < int, ::FIELD_ * > FIELD_i::fieldMap ;
42 int  FIELD_i::fieldIndex = 0;
43
44 //=============================================================================
45 /*!
46  * Default constructor
47  */
48 //=============================================================================
49
50 //FIELD_i::FIELD_i():_fieldTptr(FIELD_i::constructConstField())
51 FIELD_i::FIELD_i():_fieldTptr(constructConstField()),_corbaIndex(FIELD_i::fieldIndex++)
52 {
53 }
54 //=============================================================================
55 /*!
56  * Destructor
57  */
58 //=============================================================================
59 FIELD_i::~FIELD_i()
60 {
61   if ( _fieldTptr )
62     _fieldTptr->removeReference();
63   //if (_ownCppPtr) delete _fieldTptr;
64 }
65 //=============================================================================
66 /*!
67  * Constructor
68  */
69 //=============================================================================
70 FIELD_i::FIELD_i(::FIELD_ * const field, bool ownCppPtr):
71   _ownCppPtr(ownCppPtr),
72   _fieldTptr(field),
73   _corbaIndex(FIELD_i::fieldIndex++),
74   _FieldId("")
75 {
76   if ( _fieldTptr ) _fieldTptr->addReference();
77   FIELD_i::fieldMap[_corbaIndex]=_fieldTptr;
78
79   MESSAGE("FIELD_i::FIELD_i  Checking of pointeurs !!!");
80
81   SCRUTE(_fieldTptr);
82 }
83
84 //=============================================================================
85 /*!
86  * Constructor par recopie
87  */
88 //=============================================================================
89 FIELD_i::FIELD_i( FIELD_i & f):_ownCppPtr(false), _fieldTptr(f._fieldTptr),
90                                _corbaIndex(FIELD_i::fieldIndex++),
91                                _FieldId("")
92 {
93   if ( _fieldTptr ) _fieldTptr->addReference();
94   FIELD_i::fieldMap[_corbaIndex]=_fieldTptr;
95 }
96 //=============================================================================
97 /*!
98  * Constructor d un pointeur constant 
99  */
100 //=============================================================================
101  ::FIELD_ * FIELD_i::constructConstField() const
102 {
103         ::FIELD_ * const ptrField =new ::FIELD_();
104         return ptrField;
105 }
106
107 //=============================================================================
108 /*!
109  * CORBA: Accessor for Fields's Name
110  */
111 //=============================================================================
112 char * FIELD_i::getName()
113 throw (SALOME::SALOME_Exception)
114 {
115         if (_fieldTptr==NULL)
116                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
117                                              SALOME::INTERNAL_ERROR);
118         try
119         {
120                 return CORBA::string_dup(_fieldTptr->getName().c_str());
121         }
122         catch (MEDEXCEPTION &ex)
123         {
124                 MESSAGE("Exception en accedant au nom");
125                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
126         }
127 }
128
129 void FIELD_i::setName(const char* theName)
130   throw (SALOME::SALOME_Exception)
131 {
132   if (_fieldTptr == NULL)
133     THROW_SALOME_CORBA_EXCEPTION("No associated Field", SALOME::INTERNAL_ERROR);
134
135   try {
136     _fieldTptr->setName(theName);
137   }
138   catch (MEDEXCEPTION &ex) {
139     MESSAGE("Exception en accedant au nom");
140     THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
141   }
142 }
143
144 //=============================================================================
145 /*!
146  * CORBA: Accessor for Fields's Description
147  */
148 //=============================================================================
149 char * FIELD_i::getDescription()
150 throw (SALOME::SALOME_Exception)
151 {
152         if (_fieldTptr==NULL)
153                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
154                                              SALOME::INTERNAL_ERROR);
155         try
156         {
157                 return CORBA::string_dup(_fieldTptr->getDescription().c_str());
158         }
159         catch (MEDEXCEPTION &ex)
160         {
161                 MESSAGE("Exception en accedant a la description");
162                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
163         }
164 }
165 //=============================================================================
166 /*!
167  * CORBA: Accessor for Fields's Support
168  */
169 //=============================================================================
170 SALOME_MED::SUPPORT_ptr FIELD_i::getSupport()
171   throw (SALOME::SALOME_Exception)
172 {
173   if (_fieldTptr==NULL)
174     THROW_SALOME_CORBA_EXCEPTION("No associated Field",
175                                  SALOME::INTERNAL_ERROR);
176   
177   SUPPORT_i* servant = new SUPPORT_i(_fieldTptr->getSupport());
178
179   SALOME_MED::SUPPORT_ptr support=servant->_this();
180
181   SCRUTE(_fieldTptr);
182
183   return support ;
184 }
185 //=============================================================================
186 /*!
187  * CORBA: Accessor for Fields's Number of components
188  */
189 //=============================================================================
190 CORBA::Long FIELD_i::getNumberOfComponents()
191 throw (SALOME::SALOME_Exception)
192 {
193         if (_fieldTptr==NULL)
194                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
195                                              SALOME::INTERNAL_ERROR);
196         try
197         {
198                 return _fieldTptr->getNumberOfComponents();
199         }
200         catch (MEDEXCEPTION &ex)
201         {
202                 MESSAGE("Exception en accedant au support");
203                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
204         }
205 }
206 //=============================================================================
207 /*!
208  * CORBA: Accessor for names of component I
209  */
210 //=============================================================================
211 char * FIELD_i::getComponentName(CORBA::Long i)
212 throw (SALOME::SALOME_Exception)
213 {
214         if (_fieldTptr==NULL)
215                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
216                                              SALOME::INTERNAL_ERROR);
217         try
218         {
219                 return CORBA::string_dup(_fieldTptr->getComponentName(i).c_str());
220         }
221         catch (MEDEXCEPTION &ex)
222         {
223                 MESSAGE("Exception en accedant au nom d un component");
224                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
225         }
226 }
227 //=============================================================================
228 /*!
229  * CORBA: Accessor for unit of component I
230  */
231 //=============================================================================
232 char * FIELD_i::getComponentUnit(CORBA::Long i)
233 throw (SALOME::SALOME_Exception)
234 {
235         if (_fieldTptr==NULL)
236                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
237                                              SALOME::INTERNAL_ERROR);
238         try
239         {
240                 return CORBA::string_dup(_fieldTptr->getMEDComponentUnit(i).c_str());
241         }
242         catch (MEDEXCEPTION &ex)
243         {
244                 MESSAGE("Exception en accedant au nom d un component");
245                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
246         }
247 }
248 //=============================================================================
249 /*!
250  * CORBA: Accessor for description of component I
251  */
252 //=============================================================================
253 char * FIELD_i::getComponentDescription(CORBA::Long i)
254 throw (SALOME::SALOME_Exception)
255 {
256         if (_fieldTptr==NULL)
257                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
258                                              SALOME::INTERNAL_ERROR);
259         try
260         {
261                 return CORBA::string_dup(_fieldTptr->getComponentDescription(i).c_str());
262         }
263         catch (MEDEXCEPTION &ex)
264         {
265                 MESSAGE("Exception en accedant a la description d un component");
266                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
267         }
268 }
269 //=============================================================================
270 /*!
271  * CORBA: Accessor for iteration number 
272  */
273 //=============================================================================
274 CORBA::Long FIELD_i::getIterationNumber()
275 throw (SALOME::SALOME_Exception)
276 {
277         if (_fieldTptr==NULL)
278                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
279                                              SALOME::INTERNAL_ERROR);
280         try
281         {
282                 return _fieldTptr->getIterationNumber();
283         }
284         catch (MEDEXCEPTION &ex)
285         {
286                 MESSAGE("Exception en accedant au champ");
287                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
288         }
289 }
290 //=============================================================================
291 /*!
292  * CORBA: Accessor for Corba Number 
293  */
294 //=============================================================================
295 CORBA::Long FIELD_i::getCorbaIndex()
296 throw (SALOME::SALOME_Exception)
297 {
298         if (_fieldTptr==NULL)
299                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
300                                              SALOME::INTERNAL_ERROR);
301         return _corbaIndex;
302         
303 }
304 //=============================================================================
305 /*!
306  * CORBA: Accessor for iteration number 
307  */
308 //=============================================================================
309 CORBA::Long FIELD_i::getOrderNumber()
310 throw (SALOME::SALOME_Exception)
311 {
312         if (_fieldTptr==NULL)
313                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
314                                              SALOME::INTERNAL_ERROR);
315         try
316         {
317                 return _fieldTptr->getOrderNumber();
318         }
319         catch (MEDEXCEPTION &ex)
320         {
321                 MESSAGE("Exception en accedant au champ");
322                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
323         }
324 }
325 //=============================================================================
326 /*!
327  * CORBA: Accessor for gauss numbers presence.
328  */
329 //=============================================================================
330 CORBA::Boolean FIELD_i::getGaussPresence()
331 {
332        if (_fieldTptr==NULL)
333                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
334                                              SALOME::INTERNAL_ERROR);
335         try
336         {
337                 return _fieldTptr->getGaussPresence();
338         }
339         catch (MEDEXCEPTION &ex)
340         {
341                 MESSAGE("Exception en accedant au champ");
342                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
343         }
344 }
345 //=============================================================================
346 /*!
347  * CORBA: Accessor  
348  */
349 //=============================================================================
350 CORBA::Double FIELD_i::getTime()
351 throw (SALOME::SALOME_Exception)
352 {
353         if (_fieldTptr==NULL)
354                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
355                                              SALOME::INTERNAL_ERROR);
356         try
357         {
358                 return _fieldTptr->getTime();
359         }
360         catch (MEDEXCEPTION &ex)
361         {
362                 MESSAGE("Exception en accedant au champ");
363                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
364         }
365 }
366 //=============================================================================
367 /*!
368  * CORBA: Accessor for Fields's Components names
369  */
370 //=============================================================================
371 SALOME_TYPES::ListOfString * FIELD_i::getComponentsNames()
372 throw (SALOME::SALOME_Exception)    
373 {
374         if (_fieldTptr==NULL)
375                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
376                                              SALOME::INTERNAL_ERROR);
377         SALOME_TYPES::ListOfString_var myseq = new SALOME_TYPES::ListOfString;
378         try
379         {
380                 int nbcom = _fieldTptr->getNumberOfComponents();
381                 myseq->length(nbcom);
382                 const string * namecom=_fieldTptr->getComponentsNames();
383                 for (int i=0;i<nbcom;i++)
384                 {
385                         myseq[i]=CORBA::string_dup(namecom[i].c_str());
386                 }
387         }
388         catch (MEDEXCEPTION &ex)
389         {
390                 MESSAGE("Exception en accedant au champ");
391                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
392         }
393         return myseq._retn();
394 }
395 //=============================================================================
396 /*!
397  * CORBA: Accessor for Fields's Components units
398  */
399 //=============================================================================
400 SALOME_TYPES::ListOfString * FIELD_i::getComponentsUnits()
401 throw (SALOME::SALOME_Exception)
402 {
403         if (_fieldTptr==NULL)
404                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
405                                              SALOME::INTERNAL_ERROR);
406         SALOME_TYPES::ListOfString_var myseq = new SALOME_TYPES::ListOfString;
407         try
408         {
409                 int nbcom = _fieldTptr->getNumberOfComponents();
410                 myseq->length(nbcom);
411                 const string * unitcom=_fieldTptr->getMEDComponentsUnits();
412                 for (int i=0;i<nbcom;i++)
413                 {
414                         myseq[i]=CORBA::string_dup(unitcom[i].c_str());
415                 }
416         }
417         catch (MEDEXCEPTION &ex)
418         {
419                 MESSAGE("Exception en accedant au champ");
420                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
421         }
422         return myseq._retn();
423 }
424 //=============================================================================
425 /*!
426  * CORBA: Accessor for Fields's Components descriptions
427  */
428 //=============================================================================
429 SALOME_TYPES::ListOfString * FIELD_i::getComponentsDescriptions()
430 throw (SALOME::SALOME_Exception)    
431 {
432         if (_fieldTptr==NULL)
433                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
434                                              SALOME::INTERNAL_ERROR);
435         SALOME_TYPES::ListOfString_var myseq = new SALOME_TYPES::ListOfString;
436         try
437         {
438                 int nbcom = _fieldTptr->getNumberOfComponents();
439                 myseq->length(nbcom);
440                 const string * namecom=_fieldTptr->getComponentsDescriptions();
441                 for (int i=0;i<nbcom;i++)
442                 {
443                         myseq[i]=CORBA::string_dup(namecom[i].c_str());
444                 }
445         }
446         catch (MEDEXCEPTION &ex)
447         {
448                 MESSAGE("Exception en accedant au champ");
449                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
450         }
451         return myseq._retn();
452 }
453
454 //=============================================================================
455 /*!
456  * CORBA: Add in Study
457  */
458 //=============================================================================
459 void FIELD_i::addInStudy (SALOMEDS::Study_ptr   myStudy, 
460                           SALOME_MED::FIELD_ptr myIor)
461   throw (SALOME::SALOME_Exception, SALOMEDS::StudyBuilder::LockProtection)
462 {
463   SALOMEDS::SComponent_var aComponent = PublishMedComponent(myStudy);
464   if (CORBA::is_nil(aComponent))
465     THROW_SALOME_CORBA_EXCEPTION("SComponent labelled 'Med' not Found", SALOME::INTERNAL_ERROR);
466   addInStudyToComponent(aComponent, myIor);
467 }
468
469 static SALOMEDS::SObject_ptr FindChildByName (SALOMEDS::SObject_ptr theFather,
470                                               const string          theName)
471 {
472   SALOMEDS::SObject_var aChild;
473   if (CORBA::is_nil(theFather))
474     return aChild._retn();
475
476   SALOMEDS::SObject_var aCurChild;
477   SALOMEDS::ChildIterator_ptr anIter = theFather->GetStudy()->NewChildIterator(theFather);
478   for (; anIter->More() && aChild->_is_nil(); anIter->Next()) {
479     aCurChild = anIter->Value();
480     string aCurName = aCurChild->GetName();
481     if (aCurName == theName)
482       aChild = aCurChild;
483   }
484   return aChild._retn();
485 }
486
487 void FIELD_i::addInStudyToComponent (SALOMEDS::SComponent_ptr myComponent,
488                                      SALOME_MED::FIELD_ptr    myIor)
489   throw (SALOME::SALOME_Exception, SALOMEDS::StudyBuilder::LockProtection)
490 {
491         if (CORBA::is_nil(myComponent) || CORBA::is_nil(myIor))
492           THROW_SALOME_CORBA_EXCEPTION("Null parameter", SALOME::BAD_PARAM);
493
494         if (_fieldTptr == NULL)
495           THROW_SALOME_CORBA_EXCEPTION("No associated Field", SALOME::INTERNAL_ERROR);
496
497         if (_FieldId != "") {
498           MESSAGE("Field already in Study");
499           THROW_SALOME_CORBA_EXCEPTION("Field already in Study", SALOME::BAD_PARAM);
500         }
501
502         SALOMEDS::Study_var myStudy = myComponent->GetStudy();
503         SALOMEDS::StudyBuilder_var aBuilder = myStudy->NewBuilder();
504
505         SALOMEDS::GenericAttribute_var anAttr;
506         SALOMEDS::AttributeName_var    aName;
507         SALOMEDS::AttributeIOR_var     aIOR;
508
509         // Create SObject labelled 'MEDFIELD' if it doesn't already exist
510         SALOMEDS::Study::ListOfSObject_var aMEDFIELDs =
511           myStudy->FindObjectByName("MEDFIELD", myComponent->ComponentDataType());
512         int aLength = aMEDFIELDs->length();
513         SALOMEDS::SObject_var medfieldfather;
514         if (aLength > 0) {
515           medfieldfather = aMEDFIELDs[0];
516         }
517         else {
518           MESSAGE("Add Object 'MEDFIELD'");
519           medfieldfather = aBuilder->NewObject(myComponent);
520           anAttr = aBuilder->FindOrCreateAttribute(medfieldfather, "AttributeName");
521           aName = SALOMEDS::AttributeName::_narrow(anAttr);
522           aName->SetValue("MEDFIELD");
523         }
524
525         // Create SObject labelled 'FIELDNAME' if it doesn't already exit
526         string fieldName = _fieldTptr->getName();
527         SALOMEDS::SObject_var medfieldnamefather = FindChildByName(medfieldfather, fieldName);
528         if ( CORBA::is_nil(medfieldnamefather) ) 
529         {
530           MESSAGE("Add Object "<<fieldName);
531           medfieldnamefather = aBuilder->NewObject(medfieldfather);
532           anAttr = aBuilder->FindOrCreateAttribute(medfieldnamefather, "AttributeName");
533           aName = SALOMEDS::AttributeName::_narrow(anAttr);
534           aName->SetValue(fieldName.c_str());
535         }
536
537         string fieldEntryPath = "/";
538         //fieldEntryPath += "Med/";
539         string componentName = myComponent->GetName();
540         fieldEntryPath += componentName + "/MEDFIELD/" + fieldName + "/";
541
542         int iterationNumber = _fieldTptr->getIterationNumber();
543         SCRUTE(iterationNumber);
544
545         int orderNumber = _fieldTptr->getOrderNumber();
546         SCRUTE(orderNumber);
547
548         ostringstream iterationName;
549         iterationName<<"(" << iterationNumber << "," << orderNumber << ")";
550         string supportName = (_fieldTptr->getSupport())->getName();
551         string meshName = ((_fieldTptr->getSupport())->getMesh())->getName();
552
553         SCRUTE(meshName);
554         for (string::size_type pos=0; pos<meshName.size(); ++pos) {
555           if (isspace(meshName[pos])) meshName[pos] = '_';
556         }
557
558         char * fieldEntryName;
559         int lenName = strlen(iterationName.str().c_str()) + 4 +
560           strlen(supportName.c_str()) + 4 + strlen(meshName.c_str()) + 1;
561
562         fieldEntryName = new char[lenName];
563         fieldEntryName = strcpy(fieldEntryName,iterationName.str().c_str());
564         fieldEntryName = strcat(fieldEntryName,"_ON_");
565         fieldEntryName = strcat(fieldEntryName,supportName.c_str());
566         fieldEntryName = strcat(fieldEntryName,"_OF_");
567         fieldEntryName = strcat(fieldEntryName,meshName.c_str());
568
569         SCRUTE(fieldEntryName);
570         fieldEntryPath += fieldEntryName;
571
572         // Create object labelled according to Field's Name
573
574         SALOMEDS::SObject_var fieldSO = myStudy->FindObjectByPath(fieldEntryPath.c_str());
575         bool alreadyPublished = ! CORBA::is_nil(fieldSO);
576         aBuilder->NewCommand();
577         if ( !alreadyPublished )
578         {
579           MESSAGE("Add a Field Object under "<<fieldName);
580           fieldSO = aBuilder->NewObject(medfieldnamefather);
581           // check that this method and getEntryPath() build the same path,
582           // though this is true only for MED component
583           MESSAGE("fieldEntryPath: "<< fieldEntryPath);
584           MESSAGE("getEntryPath(): "<< getEntryPath());
585           if (componentName == "Med")
586             ASSERT( getEntryPath() == fieldEntryPath );
587         }
588         anAttr = aBuilder->FindOrCreateAttribute(fieldSO, "AttributeName");
589         aName = SALOMEDS::AttributeName::_narrow(anAttr);
590         aName->SetValue(fieldEntryName);
591
592         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
593         ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
594         CORBA::ORB_var &orb = init(0,0);
595         string iorStr = orb->object_to_string(myIor);
596         anAttr = aBuilder->FindOrCreateAttribute(fieldSO, "AttributeIOR");
597         aIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
598         aIOR->SetValue(iorStr.c_str());
599         _FieldId = fieldSO->GetID();
600
601         if ( !alreadyPublished )
602         {
603           MESSAGE("Computing path to Support");
604
605           string supportEntryPath = SUPPORT_i::getEntryPath( _fieldTptr->getSupport() );
606           SCRUTE(supportEntryPath);
607
608           SALOMEDS::SObject_var supportObject =
609             myStudy->FindObjectByPath(supportEntryPath.c_str());
610
611           SCRUTE(supportObject);
612
613           if ( CORBA::is_nil(supportObject) ) 
614           {
615             MESSAGE("supportObject is a nil corba object");
616             MESSAGE("FIELD_i::addInStudy : SUPPORT not found") ;
617           } 
618           else 
619           {
620             MESSAGE("supportObject is OK and is now going to be referenced !");
621             SALOMEDS::SObject_var newObjSupport = aBuilder->NewObject(fieldSO);
622             aBuilder->Addreference(newObjSupport,supportObject);
623             MESSAGE(" OUF !!!");
624           }
625         }
626
627         aBuilder->CommitCommand();
628
629         delete [] fieldEntryName;
630
631         // register the Corba pointer: increase the referrence count
632         MESSAGE("Registering of the Corba Field pointer");
633         Register();
634
635         MESSAGE("FIELD_i::addInStudy");
636
637         //END_OF();
638 }
639
640 //=============================================================================
641 /*!
642  * CORBA: write
643  */
644 //=============================================================================
645 void FIELD_i::write (CORBA::Long i, const char* driverFieldName)
646 throw (SALOME::SALOME_Exception)
647 {
648         if (_fieldTptr==NULL)
649                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
650                                              SALOME::INTERNAL_ERROR);
651         try
652         {
653                 _fieldTptr->write(i);
654         }
655         catch (MEDEXCEPTION &)
656         {
657                 MESSAGE("Exception en accedant au champ");
658                 THROW_SALOME_CORBA_EXCEPTION("Unable to acces Field C++ Object"\
659                                                 ,SALOME::INTERNAL_ERROR);
660         }
661 }
662 //=============================================================================
663 /*!
664  * CORBA: read
665  */
666 //=============================================================================
667 void FIELD_i::read (CORBA::Long i)
668 throw (SALOME::SALOME_Exception)
669 {
670         if (_fieldTptr==NULL)
671                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
672                                              SALOME::INTERNAL_ERROR);
673         try
674         {
675                 _fieldTptr->read(i);
676         }
677         catch (MEDEXCEPTION &ex)
678         {
679                 MESSAGE("Exception en accedant au champ");
680                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
681         }
682 }
683 //=============================================================================
684 /*!
685  * CORBA: rmDriver
686  */
687 //=============================================================================
688 void FIELD_i::rmDriver (CORBA::Long i)
689 throw (SALOME::SALOME_Exception)
690 {
691         if (_fieldTptr==NULL)
692                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
693                                              SALOME::INTERNAL_ERROR);
694         try
695         {
696                 _fieldTptr->rmDriver(i);
697         }
698         catch (MEDEXCEPTION &ex)
699         {
700                 MESSAGE("Exception en accedant au champ");
701                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
702         }
703 }
704 //=============================================================================
705 /*!
706  * CORBA: addDriver
707  */
708 //=============================================================================
709 CORBA::Long FIELD_i::addDriver (SALOME_MED::medDriverTypes driverType, 
710                         const char* fileName, const char* fieldName) throw (SALOME::SALOME_Exception)
711 {
712         if (_fieldTptr==NULL)
713                 THROW_SALOME_CORBA_EXCEPTION("No associated Field", \
714                                              SALOME::INTERNAL_ERROR);
715         try
716         {
717                 int drivernum=_fieldTptr->addDriver(
718                                         convertIdlDriverToMedDriver(driverType),
719                                         fileName,
720                                         fieldName);
721                 return drivernum;
722         }
723         catch (MEDEXCEPTION &ex)
724         {
725                 MESSAGE("Exception en accedant au champ");
726                 THROW_SALOME_CORBA_EXCEPTION(ex.what(), SALOME::INTERNAL_ERROR);
727         }
728 }
729
730 //=============================================================================
731 /*!
732  * internal method: publish MED component
733  */
734 //=============================================================================
735
736 SALOMEDS::SComponent_ptr FIELD_i::PublishMedComponent(SALOMEDS::Study_ptr theStudy)
737 {
738   if ( CORBA::is_nil(theStudy) )
739     return SALOMEDS::SComponent::_nil();
740
741   SALOMEDS::SComponent_var medfather = theStudy->FindComponent("MED");
742   if ( !CORBA::is_nil(medfather) )
743     return medfather._retn();
744
745   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
746   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
747   CORBA::ORB_var &orb = init(0,0);
748
749   SALOME_NamingService* ns = SINGLETON_<SALOME_NamingService>::Instance();
750   ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
751   ns->init_orb( orb );
752
753   SALOME_LifeCycleCORBA* lcc = new SALOME_LifeCycleCORBA( ns );
754
755   SALOME_ModuleCatalog::ModuleCatalog_var aCatalog  = 
756     SALOME_ModuleCatalog::ModuleCatalog::_narrow(ns->Resolve("/Kernel/ModulCatalog"));
757   if ( CORBA::is_nil( aCatalog ) )
758     return medfather._retn();
759   SALOME_ModuleCatalog::Acomponent_var aComp = aCatalog->GetComponent( "MED" );
760   if ( CORBA::is_nil( aComp ) )
761     return medfather._retn();
762   
763   SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
764   aBuilder->NewCommand();
765   bool aLocked = theStudy->GetProperties()->IsLocked();
766   if (aLocked) theStudy->GetProperties()->SetLocked(false);
767   
768   medfather = aBuilder->NewComponent("MED");
769   SALOMEDS::GenericAttribute_var anAttr = aBuilder->FindOrCreateAttribute(medfather, "AttributeName");
770   SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
771   aName->SetValue( aComp->componentusername() );
772   
773   Engines::EngineComponent_var aMedComponent = lcc->FindOrLoad_Component("FactoryServer", "MED");
774   SALOME_MED::MED_Gen_var aMedEngine = SALOME_MED::MED_Gen::_narrow( aMedComponent );
775   aBuilder->DefineComponentInstance(medfather, aMedEngine);
776   
777   if (aLocked) theStudy->GetProperties()->SetLocked(true);
778   aBuilder->CommitCommand();
779   
780   return medfather._retn();
781 }
782
783 //================================================================================
784 /*!
785  * \brief Return a default path to publish this field
786   * \retval string - the path
787  */
788 //================================================================================
789
790 string FIELD_i::getEntryPath ()
791 {
792   string path;
793   if ( _fieldTptr &&
794        _fieldTptr->getSupport() &&
795        _fieldTptr->getSupport()->getMesh() )
796   {
797     string meshName = _fieldTptr->getSupport()->getMesh()->getName();
798     for (string::size_type pos=0; pos<meshName.size(); ++pos)
799     {
800       if (isspace(meshName[pos])) meshName[pos] = '_';
801     }
802     ostringstream os ;
803
804     os << "/Med/MEDFIELD/" << _fieldTptr->getName() << "/" 
805        << "(" << _fieldTptr->getIterationNumber()
806        << "," << _fieldTptr->getOrderNumber()
807        << ")_ON_" << _fieldTptr->getSupport()->getName()
808        << "_OF_" << meshName;
809
810     path = os.str();
811   }
812   return path;
813 }