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