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