Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MED / Med_Gen_i.cxx
1 //  Copyright (C) 2007-2008  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 //  MED MED : implemetation of MED idl descriptions
23 //  File   : Med_Gen_i.cxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : MED
26 //  $Header$
27 //
28 #include "Med_Gen_i.hxx"
29
30 #include "MEDMEM_Mesh.hxx"
31 #include "MEDMEM_Med_i.hxx"
32 #include "MEDMEM_FieldTemplate_i.hxx"
33 #include "MEDMEM_Support_i.hxx"
34
35 #include "MEDMEM_Mesh_i.hxx"
36 #include "MEDMEM_Field.hxx"
37
38 #include "MEDMEM_Med.hxx"
39 #include "MEDMEM_MedMedDriver.hxx"
40 #include "MEDMEM_MedMeshDriver.hxx"
41 #include "MEDMEM_MedFieldDriver.hxx"
42 #include "MEDMEM_define.hxx"
43 #include "MEDMEM_DriversDef.hxx"
44 #include "MEDMEM_Grid.hxx"
45
46
47 #include "Utils_SINGLETON.hxx"
48 #include "OpUtil.hxx"
49 #include "Utils_CorbaException.hxx"
50 #include "utilities.h"
51
52 #include "SALOMEDS_Tool.hxx"
53
54 #include <string>
55 #include <deque>
56 #include <map>
57
58 #include <HDFascii.hxx>
59
60 using namespace std;
61 using namespace MEDMEM;
62
63 // Initialisation des variables statiques
64 //string Med_Gen_i::_myFileName="";
65 //string Med_Gen_i::_saveFileName="";
66  Med_Gen_i* Med_Gen_i::_MEDGen = NULL;
67
68 //=============================================================================
69 /*!
70  *  default constructor: not for use
71  */
72 //=============================================================================
73 //Med_Gen_i::Med_Gen_i()
74 //{
75 //  MESSAGE("Med_Gen_i::Med_Gen_i");
76 //}
77
78 //=============================================================================
79 /*!
80  *  standard constructor
81  */
82 //=============================================================================
83 Med_Gen_i::Med_Gen_i(CORBA::ORB_ptr orb,
84                      PortableServer::POA_ptr poa,
85                      PortableServer::ObjectId * contId,
86                      const char *instanceName,
87                      const char *interfaceName)
88   : Engines_Component_i(orb, poa, contId, instanceName, interfaceName),
89     Med_Gen_Driver_i(orb)
90 {
91   MESSAGE("activate object");
92   _thisObj = this ;
93   _id = _poa->activate_object(_thisObj);
94
95   _duringLoad=false;
96   // get an NamingService interface
97   //_NS = SINGLETON_<SALOME_NamingService>::Instance() ;
98   //ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
99   //_NS->init_orb( _orb ) ;
100
101   //_myMedI = 0;
102   _MEDGen = this;
103 }
104
105 //=============================================================================
106 /*!
107  *  Destructor
108  */
109 //=============================================================================
110 Med_Gen_i::~Med_Gen_i()
111 {
112   MESSAGE("Med_Gen_i::~Med_Gen_i");
113 }
114
115 //=============================================================================
116 /*!
117  *  GetServant [ static ]
118  *
119  *  Get servant of the CORBA object
120  */
121 //=============================================================================
122 PortableServer::ServantBase_var Med_Gen_i::GetServant( CORBA::Object_ptr theObject )
123 {
124   if( CORBA::is_nil( theObject ) || !_MEDGen || CORBA::is_nil( _MEDGen->_poa ) )
125     return NULL;
126   try {
127     PortableServer::Servant aServant = _MEDGen->_poa->reference_to_servant( theObject );
128     return aServant;
129   }
130   catch (...) {
131     INFOS( "GetServant - Unknown exception was caught!!!" );
132     return NULL;
133   }
134 }
135
136 //=============================================================================
137 /*!
138  *  private method : change a study name in SALOMEDS::Study_var
139  */
140 //=============================================================================
141 SALOMEDS::Study_var Med_Gen_i::studyName2Study(const char* studyName)
142   throw(SALOME::SALOME_Exception)
143 {
144   string myStudyName(studyName);
145
146   if (myStudyName.size() == 0)
147     THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
148                                  SALOME::BAD_PARAM);
149
150   // Get StudyManager Reference, current study,
151
152   CORBA::Object_var obj = _NS->Resolve("/myStudyManager");
153   SALOMEDS::StudyManager_var myStudyManager =
154     SALOMEDS::StudyManager::_narrow(obj);
155   if(CORBA::is_nil(myStudyManager))
156     THROW_SALOME_CORBA_EXCEPTION("No StudyManager Found in NameService", \
157                                  SALOME::BAD_PARAM);
158
159   SALOMEDS::Study_var myStudy =
160     myStudyManager->GetStudyByName(myStudyName.c_str());
161   if (CORBA::is_nil(myStudy))
162     THROW_SALOME_CORBA_EXCEPTION("Wrong Study Name", \
163                                  SALOME::BAD_PARAM);
164
165   return SALOMEDS::Study::_duplicate(myStudy) ;
166 }
167
168 //=============================================================================
169 /*!
170  *  private method : add Med component in Study (Not MedGen ???)
171  */
172 //=============================================================================
173 void Med_Gen_i::addInStudy(SALOMEDS::Study_var myStudy)
174   throw(SALOME::SALOME_Exception)
175 {
176   SALOMEDS::StudyBuilder_var  myBuilder = myStudy->NewBuilder();
177   // Create SComponent labelled 'Med' if it doesn't already exit
178   SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
179   if ( CORBA::is_nil(medfather) )
180     {
181       myBuilder->NewCommand();
182       // mpv: component label must be created in spite of "Locked" study flag state
183       bool aLocked = myStudy->GetProperties()->IsLocked();
184       if (aLocked) myStudy->GetProperties()->SetLocked(false);
185
186       MESSAGE("Add Component Med");
187       medfather = myBuilder->NewComponent("MED");
188       SALOMEDS::GenericAttribute_var anAttr = myBuilder->FindOrCreateAttribute(medfather, "AttributeName");
189       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
190       //NRI      aName->SetValue("Med");
191
192       CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
193       SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
194       SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
195       if ( !Comp->_is_nil() ) {
196         aName->SetValue( Comp->componentusername() );
197       }
198
199       //            Utilisation de this  deconseillee par Paul ??
200       //            myBuilder->DefineComponentInstance(medfather,POA_Engines::MED_Gen::_this());
201       CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
202       myBuilder->DefineComponentInstance(medfather,myO);
203
204       if (aLocked) myStudy->GetProperties()->SetLocked(true);
205       myBuilder->CommitCommand();
206     }
207 }
208
209 //=============================================================================
210 /*!
211  * Lit tous les objets contenus dans un fichier med et les lit en memoire
212  */
213 //=============================================================================
214 SALOME_MED::MED_ptr Med_Gen_i::readStructFile (const char* fileName,
215                                                const char* studyName)
216   throw(SALOME::SALOME_Exception)
217 {
218         beginService("Med_Gen_i::readStructFile");
219
220         SCRUTE(fileName);
221         SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
222         //if (!_duringLoad) addInStudy(myStudy) ;
223
224         SALOME_MED::MED_ptr myMedIOR ;
225         try
226         {
227           // we create a new MED_i and add in study
228           //SALOMEDS::SComponent_var theComponent = myStudy->FindComponent("MED");
229           //MED_i * myMedI = 0;
230           //if (CORBA::is_nil(theComponent))
231           //  myMedI = new MED_i();
232           //else
233           //  myMedI = Med_Gen_Driver_i::GetMED(theComponent);
234           MED_i * myMedI = new MED_i();
235           myMedIOR = myMedI->_this() ;
236 //        if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR) ;
237 //        if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR,fileName) ;
238           // create ::MED object, read all and add in study !
239           myMedI->init(myStudy,MED_DRIVER,fileName) ;
240         }
241         catch (const SALOMEDS::StudyBuilder::LockProtection & ) {}
242         catch(...)
243         {
244                 MESSAGE("Erreur a la lecture du fichier");
245                 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
246                                                 ,SALOME::BAD_PARAM);
247         }
248
249         endService("Med_Gen_i::readStructFile");
250         return myMedIOR;
251 }
252
253 //=============================================================================
254 /*!
255  *  Prepare un Maillage sans le lire en memoire avec stokage dans l'etude
256  *  des champs avec leur type
257  */
258 //=============================================================================
259 void Med_Gen_i::readStructFileWithFieldType (const char* fileName,
260                                              const char* studyName)
261   throw (SALOME::SALOME_Exception)
262 {
263         beginService("Med_Gen_i::readStructFileWithFieldType");
264
265   const char* LOC = "Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)";
266   BEGIN_OF(LOC);
267
268         SCRUTE(fileName);
269         SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
270         if (!_duringLoad) addInStudy(myStudy) ;
271
272         try
273         {
274           // we create a new MED_i and add in study
275           SALOMEDS::SComponent_var theComponent = myStudy->FindComponent("MED");
276           //MED_i * myMedI = 0;
277           //if (CORBA::is_nil(theComponent))
278           //  myMedI = new MED_i();
279           //else
280           //  myMedI = Med_Gen_Driver_i::GetMED(theComponent);
281           MED_i * myMedI = new MED_i();
282           SALOME_MED::MED_ptr myMedIOR = myMedI->_this();
283           if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR,theComponent,fileName);
284           // create ::MED object, read all and add in study !
285           myMedI->initWithFieldType(myStudy,MED_DRIVER,fileName);
286         }
287         catch (const SALOMEDS::StudyBuilder::LockProtection & ) {}
288         catch(...)
289         {
290                 MESSAGE("Erreur a la lecture du fichier");
291                 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
292                                                 ,SALOME::BAD_PARAM);
293         }
294
295         endService("Med_Gen_i::readStructFileWithFieldType");
296   END_OF(LOC);
297 }
298
299 //=============================================================================
300 /*!
301  *  Sert un Maillage
302  */
303 //=============================================================================
304 SALOME_MED::MESH_ptr Med_Gen_i::readMeshInFile(const char* fileName,
305                                                const char* studyName,
306                                                const char* meshName)
307 throw (SALOME::SALOME_Exception)
308 {
309         beginService("Med_Gen_i::readMeshInFile");
310         SCRUTE(fileName);
311         SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
312
313 //      if (!_duringLoad) addInStudy(myStudy) ;
314
315 // Creation du maillage
316
317         MESH * myMesh;
318
319         // skl for IPAL14240
320         // check mesh or grid:
321         try {
322           MED med(MED_DRIVER,fileName);
323           MESH* tmpMesh = med.getMesh(meshName);
324           if(tmpMesh) {
325             if(tmpMesh->getIsAGrid())
326               myMesh = new GRID();
327             else
328               myMesh = new MESH() ;
329           }
330         }
331         catch (const std::exception & ex) {
332           MESSAGE("Exception Interceptee : ");
333           SCRUTE(ex.what());
334           myMesh = new MESH() ;
335         };
336
337         myMesh->setName(meshName);
338         MED_MESH_RDONLY_DRIVER myMeshDriver(fileName,myMesh);
339         try
340         {
341                 myMeshDriver.setMeshName(meshName);
342                 myMeshDriver.open();
343         }
344 #if defined(_DEBUG_) || defined(_DEBUG)
345         catch (const std::exception & ex)
346 #else
347         catch (const std::exception &)
348 #endif
349         {
350                 MESSAGE("Exception Interceptee : ");
351                 SCRUTE(ex.what());
352                 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
353         };
354         try
355         {
356                 myMeshDriver.read();
357                 MESSAGE("apres read");
358                 myMeshDriver.close();
359         }
360 #if defined(_DEBUG_) || defined(_DEBUG)
361         catch (const std::exception & ex)
362 #else
363         catch (const std::exception &)
364 #endif
365         {
366                 MESSAGE("Exception Interceptee : ");
367                 SCRUTE(ex.what());
368                 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
369         };
370
371         MESH_i * meshi = new MESH_i(myMesh);
372         //SALOME_MED::MESH_var mesh = SALOME_MED::MESH::_narrow(meshi->_this());
373         SALOME_MED::MESH_ptr mesh = meshi->_this();
374         try
375         {
376           // add the mesh object in study
377 //        if (!_duringLoad) meshi->addInStudy(myStudy,mesh);
378         }
379         catch (const SALOMEDS::StudyBuilder::LockProtection & ) {}
380
381         endService("Med_Gen_i::readMeshInFile");
382         return mesh;
383 }
384
385 //=============================================================================
386 /*!
387  *  Sert un Champ
388  */
389 //=============================================================================
390 SALOME_MED::FIELD_ptr Med_Gen_i::readFieldInFile(const char* fileName,
391                                                  const char* studyName,
392                                                  const char* fieldName,
393                                                  CORBA::Long ordre,
394                                                  CORBA::Long iter)
395 throw (SALOME::SALOME_Exception)
396 {
397         beginService("Med_Gen_i::readFieldInFile");
398         SCRUTE(fileName);
399         string myStudyName(studyName);
400
401         if (myStudyName.size() == 0)
402                 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
403                                  SALOME::BAD_PARAM);
404
405         // Get StudyManager Reference, current study,
406
407         CORBA::Object_var obj = _NS->Resolve("/myStudyManager");
408         SALOMEDS::StudyManager_var myStudyManager =
409                         SALOMEDS::StudyManager::_narrow(obj);
410         ASSERT(! CORBA::is_nil(myStudyManager));
411         SALOMEDS::Study_var myStudy =
412                 myStudyManager->GetStudyByName(myStudyName.c_str());
413         if (CORBA::is_nil(myStudy))
414         THROW_SALOME_CORBA_EXCEPTION("Wrong Study Name", \
415                                  SALOME::BAD_PARAM);
416
417         SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
418         SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
419         if (CORBA::is_nil(medfather))
420         {
421                 myBuilder->NewCommand();
422                 // mpv: component label must be created in spite of "Locked" study flag state
423                 bool aLocked = myStudy->GetProperties()->IsLocked();
424                 if (aLocked) myStudy->GetProperties()->SetLocked(false);
425
426                 medfather = myBuilder->NewComponent("MED");
427                 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(
428                       myBuilder->FindOrCreateAttribute(medfather, "AttributeName"));
429                 //NRI           aName->SetValue("Med");
430
431                 CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
432                 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue  = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
433                 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
434                 if ( !Comp->_is_nil() ) {
435                   aName->SetValue( Comp->componentusername() );
436                 }
437
438                 CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
439                 myBuilder->DefineComponentInstance(medfather,myO);
440
441                 if (aLocked) myStudy->GetProperties()->SetLocked(true);
442                 myBuilder->CommitCommand();
443
444         }
445         else
446                         MESSAGE("MED dejà dans l étude");
447
448         MESSAGE("Lecture du fichier ")
449         SCRUTE(fileName);
450
451 // Creation du champ
452
453         FIELD_ * myField;
454         MED * mymed;
455         try
456         {
457           mymed = new MED(MED_DRIVER,fileName) ;
458         }
459         catch (const std::exception & ex)
460         {
461           MESSAGE("Exception Interceptee : ");
462           SCRUTE(ex.what());
463           THROW_SALOME_CORBA_EXCEPTION("Unable to find this file ",SALOME::BAD_PARAM);
464         }
465
466         try
467         {
468                 deque<string> fieldsNames = mymed->getFieldNames() ;
469                 int numberOfFields = fieldsNames.size();
470                 int i;
471                 for (i=0; i<numberOfFields; i++)
472                 {
473                         if (fieldsNames[i]== fieldName) break;
474                 }
475                 if (i == numberOfFields)
476                 {
477                         THROW_SALOME_CORBA_EXCEPTION("Unable to find this field ",SALOME::BAD_PARAM);
478                 }
479                 MESSAGE("trouve");
480 /*
481                 deque<DT_IT_> myIteration = mymed->getFieldIteration (fieldName);
482                 if (myIteration.size() != 1)
483                 {
484                         MESSAGE("WARNING : My iteration size is ")
485                         SCRUTE(myIteration.size());
486                 }
487 */
488                 myField = mymed->getField(fieldName,ordre,iter);
489         }
490 #if defined(_DEBUG_) || defined(_DEBUG)
491         catch (const std::exception & ex)
492 #else
493         catch (const std::exception &)
494 #endif
495         {
496                 MESSAGE("Exception Interceptee : ");
497                 SCRUTE(ex.what());
498                 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field in this file",SALOME::BAD_PARAM);
499         };
500
501         SUPPORT * fieldSupport;
502         try
503         {
504                 fieldSupport=(SUPPORT *)myField->getSupport() ;
505                 ASSERT(fieldSupport != NULL);
506                 MESH * myMesh=(MESH *)fieldSupport->getMesh();
507                 ASSERT(myMesh != NULL);
508                 myMesh->read();
509                 SCRUTE(myMesh->getName());
510                 fieldSupport->update();
511         }
512 #if defined(_DEBUG_) || defined(_DEBUG)
513         catch (const std::exception & ex)
514 #else
515         catch (const std::exception &)
516 #endif
517         {
518                 MESSAGE("Exception Interceptee : ");
519                 SCRUTE(ex.what());
520                 THROW_SALOME_CORBA_EXCEPTION("Unable to find associated support",SALOME::BAD_PARAM);
521         };
522
523         med_type_champ type = myField->getValueType() ;
524         switch (type)
525         {
526          case MED_EN::MED_INT32:
527          {
528                 try
529                 {
530                         ((FIELD<int>*)myField)->read() ;
531                         FIELDTEMPLATE_I<int,FullInterlace> * myFieldIntI = new FIELDTEMPLATE_I<int,FullInterlace>((FIELD<int,FullInterlace>*)myField);
532                         SALOME_MED::FIELD_ptr myFieldIOR = myFieldIntI->_this();
533 //                      if (!_duringLoad) myFieldIntI->addInStudy(myStudy,myFieldIOR) ;
534                         endService("Med_Gen_i::readFieldInFile");
535                         return myFieldIOR;
536                 }
537                 catch (const SALOMEDS::StudyBuilder::LockProtection &) {}
538 #if defined(_DEBUG_) || defined(_DEBUG)
539                 catch (const std::exception & ex)
540 #else
541                 catch (const std::exception &)
542 #endif
543                 {
544                         MESSAGE("Exception Interceptee : ");
545                         SCRUTE(ex.what());
546                         THROW_SALOME_CORBA_EXCEPTION("Unable to read int field",SALOME::BAD_PARAM);
547                 };
548                 break;
549          }
550          case MED_EN::MED_REEL64:
551          {
552                 try
553                 {
554                         ((FIELD<double>*)myField)->read() ;
555                         FIELDTEMPLATE_I<double,FullInterlace> * myFieldDoubleI = new FIELDTEMPLATE_I<double,FullInterlace>((FIELD<double,FullInterlace>*)myField);
556                         SALOME_MED::FIELD_ptr myFieldIOR = myFieldDoubleI->_this() ;
557 //                      if (!_duringLoad) myFieldDoubleI->addInStudy(myStudy,myFieldIOR) ;
558                         endService("Med_Gen_i::readFieldInFile");
559                         return myFieldIOR;
560                 }
561                 catch (const SALOMEDS::StudyBuilder::LockProtection &) {}
562
563 #if defined(_DEBUG_) || defined(_DEBUG)
564                 catch (const std::exception & ex)
565 #else
566                 catch (const std::exception &)
567 #endif
568                 {
569                         MESSAGE("Exception Interceptee : ");
570                         SCRUTE(ex.what());
571                         THROW_SALOME_CORBA_EXCEPTION("Unable to read double field",SALOME::BAD_PARAM);
572                 };
573                 break;
574          }
575         }
576
577         return SALOME_MED::FIELD::_nil();
578 }
579
580
581 //=============================================================================
582 /*!
583  *  from Driver
584  */
585 //=============================================================================
586 Engines::Component_ptr Med_Gen_i::GetComponentInstance()
587 {
588   return MED_Gen::_this();
589 }
590
591 char* Med_Gen_i::ComponentDataType()
592 {
593   MESSAGE("Med_Gen_i::ComponentDataType");
594   return CORBA::string_dup("MED") ; /* What is this type ? */
595 }
596
597
598
599 //=============================================================================
600 /*!
601  * C factory, accessible with dlsym, after dlopen
602  */
603 //=============================================================================
604
605 extern "C" MED_EXPORT 
606   PortableServer::ObjectId * MEDEngine_factory(
607                                CORBA::ORB_ptr orb,
608                                PortableServer::POA_ptr poa,
609                                PortableServer::ObjectId * contId,
610                                const char *instanceName,
611                                const char *interfaceName)
612 {
613   MESSAGE("PortableServer::ObjectId * MedEngine_factory()");
614   SCRUTE(interfaceName);
615   Med_Gen_i * myMed_Gen
616     = new Med_Gen_i(orb, poa, contId, instanceName, interfaceName);
617   return myMed_Gen->getId() ;
618 }