Salome HOME
Make COMPONENT module compilable (due to MED modifications)
[samples/component.git] / src / CalculatorComponent / CalculatorEngine.cxx
1 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19 //
20 //
21 //
22 //  File   : CalculatorEngine.cxx
23 //  Author : Laurent DADA, CEA
24 //  Module : CalculatorComponent
25 //  $Header$
26
27 using namespace std;
28 #include <unistd.h>
29 #include "utilities.h"
30 #include "CalculatorEngine.hxx"
31 #include <iostream>
32 #include <sstream>
33 #include "MEDMEM_Support_i.hxx"
34
35 #include "MEDMEM_define.hxx"
36 #include "MEDMEM_STRING.hxx"
37 #include "MEDMEM_Exception.hxx"
38 #include "MEDMEM_Unit.hxx"
39
40 using namespace MEDMEM;
41
42 //================================================================================
43 // static PrintFieldValues - shows field contents
44 //================================================================================
45
46 static void PrintFieldValues (FIELD<double> * field, int until_index) 
47 {
48   const double * values = field -> getValue(MED_FULL_INTERLACE);
49   int nb_comp     = field -> getNumberOfComponents();
50   MESSAGE( "Field                    : " << field -> getName() );
51   MESSAGE( "    Description          : " << field -> getDescription() );
52   MESSAGE( "    Number of components : " << nb_comp );
53
54   for (int i=0 ; i<nb_comp ; i++) {
55     MESSAGE ( "       Component " << field -> getComponentName(i+1) << endl \
56              << "                   Unit        : " << field -> getMEDComponentUnit(i+1) << endl \
57              << "                   Description : " << field -> getComponentDescription(i+1) );
58   }
59   MESSAGE( "    First " << until_index << " values   : " );
60   
61   for (int j=0 ; j < until_index ; j++) {
62     for (int i=0;i<nb_comp;i++) MESSAGE( values[i+j*nb_comp] << " " );
63   }
64 }
65
66
67 //================================================================================
68 // Constructors
69 //================================================================================
70
71 CalculatorEngine::CalculatorEngine(CORBA::ORB_ptr orb,
72                                    PortableServer::POA_ptr poa,
73                                    PortableServer::ObjectId * contId, 
74                                    const char *instanceName,
75                                    const char *interfaceName) :
76                                    Engines_Component_i(orb, poa, contId, instanceName, interfaceName,true)
77 {
78   MESSAGE("CalculatorEngine::CalculatorEngine activate object instanceName("
79           << instanceName << ") interfaceName(" << interfaceName << ")" )
80     _thisObj = this ;
81   _id = _poa->activate_object(_thisObj);
82   SALOME_NamingService *NS = new SALOME_NamingService(orb);
83   _NS=NS;
84 }
85
86 CalculatorEngine::CalculatorEngine()
87 {
88 }
89
90 //================================================================================
91 // Destructors
92 //================================================================================
93
94 CalculatorEngine::~CalculatorEngine()
95 {
96 }
97
98 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::PutToStudy(SALOME_MED::FIELDDOUBLE_ptr theField1,
99                                                          long int theStudyId) {
100   CORBA::Object_var anObj = _NS->Resolve("/myStudyManager");
101   SALOMEDS::StudyManager_var aManager  = SALOMEDS::StudyManager::_narrow(anObj);
102 //   SALOMEDS::ListOfOpenStudies_var aList = aManager->GetOpenStudies();
103 //   SALOMEDS::Study_var aStudy = aManager->GetStudyByName(aList[0]);
104   SALOMEDS::Study_var aStudy = aManager->GetStudyByID(theStudyId);
105   SALOMEDS::StudyBuilder_var aBuilder = aStudy->NewBuilder();
106
107   SALOMEDS::SComponent_var aComponent = aStudy->FindComponent("Calculator");
108   if (aComponent->_is_nil()) aComponent = aBuilder->NewComponent("Calculator");
109   SALOMEDS::GenericAttribute_var anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
110   SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
111   aName->SetValue("Calculator");
112   
113   SALOMEDS::SObject_var aSO = aBuilder->NewObject(aComponent);
114   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeName");
115   aName = SALOMEDS::AttributeName::_narrow(anAttr);
116   char aFieldName[10];
117   sprintf(aFieldName, "Field_%d", (int)aSO->Tag());
118   aName->SetValue(strdup(aFieldName));
119   
120   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance() ;
121   ASSERT(SINGLETON_<ORB_INIT>::IsAlreadyExisting()) ;
122   CORBA::ORB_var &orb = init(0,0);
123
124   string iorStr = orb->object_to_string(theField1);
125   anAttr = aBuilder->FindOrCreateAttribute(aSO, "AttributeIOR");
126   SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
127   anIOR->SetValue(iorStr.c_str());
128
129   return SALOME_MED::FIELDDOUBLE::_duplicate(theField1);
130 }
131
132
133 //=======================================================================================
134 //  Addition of 2 FieldNodeDouble (based on Med library)
135 //
136 //  We assume that :
137 //     - support
138 //     - number of components
139 //     - name, type, unit and description of components
140 //  are the same for the 2 fields.
141 //
142 //  Otherwise,
143 //  the result will be a copy of the first field.
144 //
145 //=======================================================================================
146
147 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::Add(SALOME_MED::FIELDDOUBLE_ptr FirstField,
148                                                   SALOME_MED::FIELDDOUBLE_ptr SecondField)
149 {
150   ostringstream tmp;
151   bool          same_support = true;
152   
153   beginService("CalculatorEngine::Add");
154   
155   sendMessage(NOTIF_TRACE, "CalculatorEngine::Add is Computing");
156   
157   BEGIN_OF("CalculatorEngine::Add(SALOME_MED::FIELDDOUBLE_ptr FirstField,SALOME_MED::FIELDDOUBLE_ptr SecondField)");
158
159   SCRUTE(FirstField);
160   SCRUTE(SecondField);
161
162   string message = string("\n             CalculatorEngine::Add : ") +
163                    string("\n                               first field name  --> ") + FirstField -> getName() +
164                    string("\n                               second field name --> ") + SecondField -> getName();
165
166   sendMessage(NOTIF_TRACE, message.c_str());
167
168   // Number of components
169   int nb_comp1  = FirstField -> getNumberOfComponents();
170   int nb_comp2  = SecondField -> getNumberOfComponents();
171   if (nb_comp1 != nb_comp2) same_support = false;
172    MESSAGE( "  nb_comp1 --> " << nb_comp1 <<endl );
173    MESSAGE( "  nb_comp2 --> " << nb_comp2 <<endl );
174
175
176   /* Name Description and Unit of each component of the field */
177
178   string * component_name = new string[nb_comp1];
179   string * component_unit = new string[nb_comp1];
180
181   for (int i=0 ; i<nb_comp1 ; i++) {
182     component_name[i].assign(FirstField -> getComponentName(i+1));
183     component_unit[i].assign(FirstField -> getComponentUnit(i+1));
184
185     if (same_support) {
186       if (
187           (component_name[i] != SecondField -> getComponentName(i+1)) ||
188           (component_unit[i] != SecondField -> getComponentUnit(i+1)))
189         same_support = false;
190     }
191   }
192
193   MESSAGE("CalculatorEngine::Add Number of entities in the Support of each fields");
194  
195   // Number of entities in the Support of the field
196
197   int len_value1 = 0;
198   int len_value2 = 0;
199   SALOME_MED::SUPPORT_ptr support1 = FirstField -> getSupport();
200   SALOME_MED::SUPPORT_ptr support2 = SecondField -> getSupport();
201
202   SCRUTE(support1);
203
204   SCRUTE(support2);
205
206   SALOME_MED::MESH_ptr mesh1 = support1 -> getMesh();
207   SALOME_MED::MESH_ptr mesh2 = support2 -> getMesh();
208
209   SCRUTE(mesh1);
210   SCRUTE(mesh2);
211
212   if (same_support && support1 != support2) same_support = false;
213
214   if (support1 -> isOnAllElements())
215     len_value1 = support1 -> getMesh() -> getNumberOfElements(support1 -> getEntity(),SALOME_MED::MED_ALL_ELEMENTS);
216   else
217     len_value1 = support1 -> getNumberOfElements(SALOME_MED::MED_ALL_ELEMENTS);
218
219   SCRUTE(len_value1);
220
221   if (support2 -> isOnAllElements())
222     len_value2 = support2 -> getMesh() -> getNumberOfElements(support2 -> getEntity(),SALOME_MED::MED_ALL_ELEMENTS);
223   else
224     len_value2 = support2 -> getNumberOfElements(SALOME_MED::MED_ALL_ELEMENTS);
225
226   if (same_support && len_value1 != len_value2) same_support = false;
227
228   SCRUTE(len_value2);
229
230   // Values of new field
231   SALOME_MED::double_array * first_value = FirstField -> getValue(SALOME_MED::MED_FULL_INTERLACE);
232   SALOME_MED::double_array * second_value;
233   if (same_support) {
234     second_value = SecondField -> getValue(SALOME_MED::MED_FULL_INTERLACE);
235     tmp << "\n             CalculatorEngine::Add : " ;
236     tmp << "\n                               Number of entities in the Support of first field  = " << len_value1 ;
237     tmp << "\n                               Number of entities in the Support of second field = " << len_value2 ;
238     message = tmp.str();
239
240     sendMessage(NOTIF_TRACE, message.c_str());
241
242   }
243
244   int totalLength = nb_comp1*len_value1;
245   double * new_value = new double[totalLength];
246   if (same_support) 
247     {
248       sendMessage(NOTIF_TRACE, "CalculatorEngine::Add - field1 and field2 have the same support");
249
250       for(int i=0 ; i<totalLength ; i++) 
251         new_value[i] = ((*first_value)[i]) + ((*second_value)[i]);
252     }
253   else {
254     for(int i=0 ; i<totalLength ; i++) 
255       new_value[i] = ((*first_value)[i]) ;
256   }
257   
258   // Creation of a Local Field
259   
260   MESSAGE("CalculatorEngine::Add Creation of the local field "<< totalLength);
261   
262   FIELD<double> * fieldloc =  new FIELD<double>();
263   fieldloc -> allocValue(nb_comp1,len_value1);
264   fieldloc  -> setValue(MED_FULL_INTERLACE,new_value);
265   fieldloc  -> setValueType(MED_EN::MED_REEL64);
266   fieldloc  -> setName("-new_Add-");
267   fieldloc  -> setDescription( FirstField -> getDescription() );
268   fieldloc -> setComponentsNames(component_name);
269   fieldloc -> setMEDComponentsUnits(component_unit);
270
271   // Control
272   int until_index = ( 20 > len_value1)? len_value1 : 20;
273   PrintFieldValues (fieldloc,until_index);
274
275   sendMessage(NOTIF_TRACE, "CalculatorEngine::Add - new field created");
276
277   // Export result
278   // Creation of the new CORBA field
279
280   MESSAGE("CalculatorEngine::Add Creation of the CORBA field");
281
282   // Corrected by APV - November 23, 2004
283   // FIELDDOUBLE_i * NewField = new FIELDDOUBLE_i(support1,fieldloc) ;
284   FIELDDOUBLE_i * NewField = new FIELDDOUBLE_i(fieldloc) ;
285
286   POA_SALOME_MED::FIELDDOUBLE_tie<FIELDDOUBLE_i> * myFieldTie = new POA_SALOME_MED::FIELDDOUBLE_tie<FIELDDOUBLE_i>(NewField) ;
287   SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = myFieldTie->_this() ;
288
289   sleep(4);
290
291   END_OF("CalculatorEngine::Add(SALOME_MED::FIELDDOUBLE_ptr FirstField,SALOME_MED::FIELDDOUBLE_ptr SecondField)");
292
293   endService("CalculatorEngine::Add");
294   return myFieldIOR;
295
296 }
297
298
299 //===============================================================================
300 //  Multiplication of a FieldNodeDouble by a double value (based on Med library)
301 //===============================================================================
302
303 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::Mul(SALOME_MED::FIELDDOUBLE_ptr OldField,
304                                                   CORBA::Double x1)
305 {
306   beginService("CalculatorEngine::Mul");
307
308   BEGIN_OF("CalculatorEngine::Mul(SALOME_MED::FIELDDOUBLE_ptr OldField,CORBA::Double x1)");
309
310   SCRUTE(OldField);
311   SCRUTE(x1);
312
313   // Name and description of field
314   string field_name        = OldField -> getName();
315   string field_description = OldField -> getDescription();
316
317   // Number of components
318   int nb_comp  = OldField -> getNumberOfComponents();
319   string * component_name = new string[nb_comp];
320   string * component_unit = new string[nb_comp];
321
322   for (int i=0 ; i<nb_comp ; i++) {
323     component_name[i] = OldField -> getComponentName(i+1);
324     component_unit[i] = OldField -> getComponentUnit(i+1);
325   }
326
327   MESSAGE("CalculatorEngine::Mul Number of entities in the Support of the field");
328
329   // Number of entities in the Support of the field
330   int len_value = 0;
331
332   SALOME_MED::SUPPORT_ptr support = OldField -> getSupport();
333
334   SCRUTE(support);
335
336   SALOME_MED::MESH_ptr mesh = support -> getMesh();
337
338   SCRUTE(mesh);
339
340   if (support -> isOnAllElements())
341     len_value = support -> getMesh() -> getNumberOfElements(support -> getEntity(),SALOME_MED::MED_ALL_ELEMENTS);
342   else
343     len_value = support -> getNumberOfElements(SALOME_MED::MED_ALL_ELEMENTS);
344
345   SCRUTE(len_value);
346
347   // Values of new field
348   SALOME_MED::double_array * old_value = OldField -> getValue(SALOME_MED::MED_FULL_INTERLACE);
349
350   int totalLength = nb_comp*len_value;
351   double * new_value = new double[totalLength];
352   for(int i=0 ; i<totalLength ; i++) {
353     new_value[i] = x1 * ((*old_value)[i]);
354   }
355
356   // Creation of a Local Field
357
358   MESSAGE("CalculatorEngine::Mul Creation of the local field "<< totalLength);
359
360   FIELD<double> * fieldloc =  new FIELD<double>();
361   fieldloc -> allocValue(nb_comp,len_value);
362   fieldloc  -> setValue(MED_FULL_INTERLACE,new_value);
363   fieldloc  -> setValueType(MED_EN::MED_REEL64);
364   fieldloc  -> setName("-new_Mul-");
365   fieldloc  -> setDescription(field_description);
366   fieldloc -> setComponentsNames(component_name);
367   fieldloc -> setMEDComponentsUnits(component_unit);
368
369
370   // Creation of the new CORBA field
371
372   MESSAGE("CalculatorEngine::Mul Creation of the CORBA field");
373
374   // Corrected by APV - November 23, 2004
375   // FIELDDOUBLE_i * NewField = new FIELDDOUBLE_i(support,fieldloc) ;
376   FIELDDOUBLE_i * NewField = new FIELDDOUBLE_i(fieldloc) ;
377
378   POA_SALOME_MED::FIELDDOUBLE_tie<FIELDDOUBLE_i> * myFieldTie = new POA_SALOME_MED::FIELDDOUBLE_tie<FIELDDOUBLE_i>(NewField) ;
379   SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = myFieldTie->_this() ;
380
381   sleep(4);
382
383   END_OF("CalculatorEngine::Mul(SALOME_MED::FIELDDOUBLE_ptr OldField,CORBA::Double x1)");
384
385   endService("CalculatorEngine::Mul");
386   return myFieldIOR;
387 }
388
389
390 //================================================================================
391 //  Build a constant field of double based on first field support (Med library)
392 //================================================================================
393
394 SALOME_MED::FIELDDOUBLE_ptr CalculatorEngine::Constant(SALOME_MED::FIELDDOUBLE_ptr FirstField,
395                                                        CORBA::Double x1)
396 {
397
398   beginService("CalculatorEngine::Const");
399
400   BEGIN_OF("CalculatorEngine::Constant(SALOME_MED::FIELDDOUBLE_ptr FirstField,CORBA::Double x1)");
401
402   // Name and description of field
403   string field_name        = FirstField -> getName();
404   string field_description = FirstField -> getDescription();
405
406   SALOME_MED::SUPPORT_ptr FirstSupport = FirstField -> getSupport();
407
408   // Number of components
409   int nb_comp  = FirstField -> getNumberOfComponents();
410
411   // Type, name, unit and description of components
412   string * component_name = new string[nb_comp];
413   string * component_unit = new string[nb_comp];
414
415   for (int i = 0 ; i<nb_comp ; i++) {
416     component_name[i] = FirstField -> getComponentName(i+1);
417     component_unit[i] = FirstField -> getComponentUnit(i+1);
418   }
419
420   MESSAGE("CalculatorEngine::Constant Number of entities in the Support of the field");
421
422   // Number of entities in the Support of the field
423   int len_value = 0;
424
425   SCRUTE(FirstSupport);
426
427   SALOME_MED::MESH_ptr mesh = FirstSupport -> getMesh();
428
429   SCRUTE(mesh);
430
431   if ( FirstSupport -> isOnAllElements() )
432     len_value = FirstSupport -> getMesh() -> getNumberOfElements(FirstSupport -> getEntity(),SALOME_MED::MED_ALL_ELEMENTS);
433   else
434     len_value = FirstSupport -> getNumberOfElements(SALOME_MED::MED_ALL_ELEMENTS);
435
436   // Values of new field
437   SALOME_MED::double_array * old_value = FirstField -> getValue(SALOME_MED::MED_FULL_INTERLACE);
438
439   int totalLength = nb_comp*len_value;
440   double * new_value = new double[totalLength];
441
442   for(int i=0 ; i<totalLength ; i++) 
443     new_value[i] = x1 ;
444  
445
446   // Creation of a Local Field
447
448   MESSAGE("CalculatorEngine::Constant Creation of the local field "<< totalLength);
449
450   FIELD<double> * fieldloc =  new FIELD<double>();
451   fieldloc -> allocValue(nb_comp,len_value);
452   fieldloc  -> setValue(MED_FULL_INTERLACE,new_value);
453   fieldloc  -> setValueType(MED_EN::MED_REEL64);
454   fieldloc  -> setName("-new_Const_Field-");
455   fieldloc  -> setDescription(field_description);
456   fieldloc -> setComponentsNames(component_name);
457   fieldloc -> setMEDComponentsUnits(component_unit);
458
459   // Control
460   int until_index = ( 20 > len_value)? len_value : 20 ;
461   PrintFieldValues (fieldloc,until_index);
462
463   // Creation of the new CORBA field
464
465   MESSAGE("CalculatorEngine::Constant Creation of the CORBA field");
466
467   // Corrected by APV - November 23, 2004
468   // FIELDDOUBLE_i * NewField = new FIELDDOUBLE_i(FirstSupport,fieldloc) ;
469   FIELDDOUBLE_i * NewField = new FIELDDOUBLE_i(fieldloc) ;
470
471   POA_SALOME_MED::FIELDDOUBLE_tie<FIELDDOUBLE_i> * myFieldTie = new POA_SALOME_MED::FIELDDOUBLE_tie<FIELDDOUBLE_i>(NewField) ;
472   SALOME_MED::FIELDDOUBLE_ptr myFieldIOR = myFieldTie->_this() ;
473   
474   sleep(4);
475
476   endService("CalculatorEngine::Const");
477   
478   END_OF("CalculatorEngine::Constant(SALOME_MED::FIELDDOUBLE_ptr FirstField,CORBA::Double x1)");
479   
480   return myFieldIOR;
481   
482 }
483
484 //================================================================================
485 //  write a field in a MED file
486 //================================================================================
487
488 void CalculatorEngine::writeMEDfile(SALOME_MED::FIELDDOUBLE_ptr field, const char *filename)
489 {
490   beginService("CalculatorEngine::writeMEDfile");
491   MESSAGE("fichier d'impression du champ resultat:"<<filename);
492
493   // get support of the field
494
495   SALOME_MED::SUPPORT_ptr support = field -> getSupport();
496   
497   // get mesh from this support
498   
499   SALOME_MED::MESH_ptr mesh = support -> getMesh();
500   
501   // write the mesh
502   
503   int index_m = mesh->addDriver(SALOME_MED::MED_DRIVER,filename,mesh->getName());
504
505   mesh -> write(index_m,"");
506
507   //write the field
508   
509   const char * LOC = "CalculatorEngine::writeMEDfile ";
510   
511   MESSAGE("fichier :"<<filename);
512
513   MED_FR::med_idt _medIdt=MED_FR::MEDouvrir(const_cast <char *> (filename) ,(MED_FR::med_mode_acces) MED_LECTURE_ECRITURE);
514   SCRUTE(_medIdt);
515
516   if (_medIdt<0) return;
517   
518   int err ;
519
520   int component_count= field->getNumberOfComponents();
521   string   component_name(component_count*MED_TAILLE_PNOM,' ') ;
522   string   component_unit(component_count*MED_TAILLE_PNOM,' ') ;
523   
524   for (int i=0; i < component_count ; i++) {
525     component_name.replace(i*MED_TAILLE_PNOM,MED_TAILLE_PNOM,(field -> getComponentName(i+1)),0,MED_TAILLE_PNOM);
526     component_unit.replace(i*MED_TAILLE_PNOM,MED_TAILLE_PNOM,(field -> getComponentUnit(i+1)),0,MED_TAILLE_PNOM);
527   }
528   
529   MESSAGE("component_name=|"<<component_name<<"|");
530   MESSAGE("component_unit=|"<<component_unit<<"|");
531   
532   // already existing ?
533   
534   char * champName = new char[MED_TAILLE_NOM+1] ;
535   char * compName,  * compUnit  ;
536   bool Find = false ;
537
538   int n = MED_FR::MEDnChamp(_medIdt,0);
539   int nbComp ;
540
541   MED_FR::med_type_champ type ;
542
543   for (int i = 1;  i <= n;  i++) 
544     {
545       nbComp = MED_FR::MEDnChamp(_medIdt,i);
546       compName = new char[MED_TAILLE_PNOM*nbComp+1];
547       compUnit = new char[MED_TAILLE_PNOM*nbComp+1];
548
549       err = MED_FR::MEDchampInfo(_medIdt,i,champName,&type,compName,compUnit,nbComp);
550       if (err == 0)
551         if (strcmp(champName, field->getName())==0) { // Found !
552           Find = true ;
553           break ;
554         }
555
556       delete[] compName ;
557       delete[] compUnit ;
558
559     }
560   if (Find) {
561     // the same ?
562     if (nbComp != component_count)
563       throw MEDEXCEPTION ( LOCALIZED (STRING(LOC)
564                                      <<": Field exist in file, but number of component are different : " \
565                                      <<nbComp<<" in file and "<<component_count<<" in CORBA object."
566                                      )
567                          );
568     // component name and unit
569     MESSAGE(LOC<<" Component name in file : "<<compName);
570     MESSAGE(LOC<<" Component name in CORBA object : "<<component_name);
571     MESSAGE(LOC<<" Component unit in file : "<<compUnit);
572     MESSAGE(LOC<<" Component unit in CORBA object : "<<component_unit);
573     delete[] compName ;
574     delete[] compUnit ;
575     
576   } else { 
577     // Verify the field doesn't exist
578     
579     string dataGroupName =  "/CHA/"+ string(field->getName());
580
581     MESSAGE(LOC << "|" << dataGroupName << "|" );
582
583     med_idt gid =  H5Gopen(_medIdt, dataGroupName.c_str() );
584     
585     if ( gid < 0 ) {
586       // create field :
587       err=MED_FR::MEDchampCr(_medIdt, 
588                              const_cast <char*> (field->getName()),
589                              MED_FR::MED_FLOAT64,
590                              const_cast <char*> ( component_name.c_str() ),
591                              const_cast <char*> ( component_unit.c_str() ),
592                              component_count);
593       if ( err < 0 )
594         throw MEDEXCEPTION( LOCALIZED (STRING(LOC) 
595                                        << ": Error MEDchampCr : "<<err
596                                        )
597                            );
598     }
599         else H5Gclose(gid);
600   }
601   
602   SALOME_MED::SUPPORT_ptr mySupport = field -> getSupport();
603   SCRUTE(mySupport);
604   
605   if (! mySupport->isOnAllElements())
606     throw MEDEXCEPTION ( LOCALIZED (STRING(LOC) 
607                                    <<": Field must be on all entity"
608                                     )
609                         );
610   
611   SALOME_MED::MESH_ptr myMesh = mySupport -> getMesh();
612   SCRUTE(mesh);
613
614   SALOME_MED::medGeometryElement_array* Types = mySupport->getTypes() ;
615
616   int NumberOfType = Types->length() ;
617
618   for ( int i = 0; i < NumberOfType; i++ ) 
619     {
620       int NumberOfElements   = mySupport->getNumberOfElements ( (*Types)[i] ) ;
621       int NumberOfGaussPoint = mySupport->getNumberOfGaussPoint ( (*Types)[i] ) ;
622     
623       MESSAGE( " " << field->getName() );
624       MESSAGE( " " << NumberOfElements );
625       MESSAGE( " " << NumberOfGaussPoint );
626       MESSAGE( " " << (int) (convertIdlEntToMedEnt(mySupport->getEntity())) );
627       MESSAGE( " " << (int)(convertIdlEltToMedElt((*Types)[i])) );
628       MESSAGE( " " << field->getIterationNumber() );
629       MESSAGE( " " << field->getTime() );
630       MESSAGE( " " << field->getOrderNumber() );
631       MESSAGE( "MEDchampEcr :" << myMesh->getName() );
632     
633       SALOME_MED::double_array * value = field->getValue( SALOME_MED::MED_FULL_INTERLACE ) ;
634       double *locvalue = new double[NumberOfElements];
635       for (int k = 0; k < NumberOfElements; k++) 
636         locvalue[k] = (*value) [k];
637
638       err=MED_FR::MEDchampEcr(_medIdt, 
639                               const_cast <char*> (myMesh->getName()) , 
640                               const_cast <char*> (field->getName()),
641                               (unsigned char*)locvalue, 
642                               MED_FR::MED_FULL_INTERLACE,
643                               NumberOfElements,
644                               MED_NOGAUSS,
645                               MED_ALL,
646                               MED_NOPFL,
647                               MED_FR::MED_NO_PFLMOD,
648                               (MED_FR::med_entite_maillage)convertIdlEntToMedEnt(mySupport->getEntity()),
649                               (MED_FR::med_geometrie_element)(convertIdlEltToMedElt((*Types)[i])),
650                               field->getIterationNumber(),
651                               "        ",
652                               field->getTime(),
653                               field->getOrderNumber()
654                               );
655       delete locvalue;
656       
657       if (err < MED_VALID )
658         throw MEDEXCEPTION(LOCALIZED( STRING(LOC)
659                                      <<": Error in writing Field "<< field->getName() <<", type "<<(*Types)[i]
660                                      )
661                            );
662     }
663   END_OF( LOC );
664   
665   SCRUTE( err );
666   if (err < 0 ) return ;
667   
668   MED_FR::MEDfermer(_medIdt) ;
669   
670   endService("CalculatorEngine::writeMEDfile");
671   return ;
672   
673 }
674
675 extern "C"
676 {
677   //===========================================================================
678   // CalculatorEngine_factory
679   //===========================================================================
680   PortableServer::ObjectId * CalculatorEngine_factory
681      (CORBA::ORB_ptr orb,
682       PortableServer::POA_ptr poa, 
683       PortableServer::ObjectId * contId,
684       const char *instanceName,
685       const char *interfaceName)
686   {
687     MESSAGE("CalculatorEngine_factory CalculatorEngine ("
688             << instanceName << "," << interfaceName << ")");
689     CalculatorEngine * myCalculator 
690       = new CalculatorEngine(orb, poa, contId, instanceName, interfaceName);
691     return myCalculator->getId() ;
692   }
693 }
694