1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // MED MED : implemetation of MED idl descriptions
24 // File : Med_Gen_i.cxx
25 // Author : Paul RASCLE, EDF
28 #include "Med_Gen_i.hxx"
29 #include "MED_version.h"
31 #include "MEDMEM_Mesh.hxx"
32 #include "MEDMEM_Med_i.hxx"
33 #include "MEDMEM_FieldTemplate_i.hxx"
34 #include "MEDMEM_Support_i.hxx"
36 #include "MEDMEM_Mesh_i.hxx"
37 #include "MEDMEM_Field.hxx"
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"
47 #include "Utils_SINGLETON.hxx"
49 #include "Utils_CorbaException.hxx"
50 #include "utilities.h"
52 #include "SALOMEDS_Tool.hxx"
58 #include <HDFascii.hxx>
60 using namespace MEDMEM;
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;
67 //=============================================================================
69 * default constructor: not for use
71 //=============================================================================
72 //Med_Gen_i::Med_Gen_i()
74 // MESSAGE("Med_Gen_i::Med_Gen_i");
77 //=============================================================================
79 * standard constructor
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),
90 MESSAGE("activate object");
92 _id = _poa->activate_object(_thisObj);
95 // get an NamingService interface
96 //_NS = SINGLETON_<SALOME_NamingService>::Instance() ;
97 //ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
98 //_NS->init_orb( _orb ) ;
104 //=============================================================================
108 //=============================================================================
109 Med_Gen_i::~Med_Gen_i()
111 MESSAGE("Med_Gen_i::~Med_Gen_i");
114 //=============================================================================
116 * GetServant [ static ]
118 * Get servant of the CORBA object
120 //=============================================================================
121 PortableServer::ServantBase_var Med_Gen_i::GetServant( CORBA::Object_ptr theObject )
123 if( CORBA::is_nil( theObject ) || !_MEDGen || CORBA::is_nil( _MEDGen->_poa ) )
126 PortableServer::Servant aServant = _MEDGen->_poa->reference_to_servant( theObject );
130 INFOS( "GetServant - Unknown exception was caught!!!" );
135 //=============================================================================
137 * private method : change a study name in SALOMEDS::Study_var
139 //=============================================================================
140 SALOMEDS::Study_var Med_Gen_i::studyName2Study(const char* studyName)
141 throw(SALOME::SALOME_Exception)
143 string myStudyName(studyName);
145 if (myStudyName.size() == 0)
146 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
149 // Get StudyManager Reference, current study,
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", \
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", \
164 return SALOMEDS::Study::_duplicate(myStudy) ;
167 //=============================================================================
169 * private method : add Med component in Study (Not MedGen ???)
171 //=============================================================================
172 void Med_Gen_i::addInStudy(SALOMEDS::Study_var myStudy)
173 throw(SALOME::SALOME_Exception)
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) )
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);
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");
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() );
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);
203 if (aLocked) myStudy->GetProperties()->SetLocked(true);
204 myBuilder->CommitCommand();
208 //=============================================================================
210 * Lit tous les objets contenus dans un fichier med et les lit en memoire
212 //=============================================================================
213 SALOME_MED::MED_ptr Med_Gen_i::readStructFile (const char* fileName,
214 const char* studyName)
215 throw(SALOME::SALOME_Exception)
217 beginService("Med_Gen_i::readStructFile");
220 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
221 //if (!_duringLoad) addInStudy(myStudy) ;
223 SALOME_MED::MED_ptr myMedIOR ;
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();
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) ;
240 catch (const SALOMEDS::StudyBuilder::LockProtection & ) {}
243 MESSAGE("Erreur a la lecture du fichier");
244 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
248 endService("Med_Gen_i::readStructFile");
252 //=============================================================================
254 * Prepare un Maillage sans le lire en memoire avec stokage dans l'etude
255 * des champs avec leur type
257 //=============================================================================
258 void Med_Gen_i::readStructFileWithFieldType (const char* fileName,
259 const char* studyName)
260 throw (SALOME::SALOME_Exception)
262 beginService("Med_Gen_i::readStructFileWithFieldType");
264 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
265 if (!_duringLoad) addInStudy(myStudy) ;
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();
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);
282 catch (const SALOMEDS::StudyBuilder::LockProtection & ) {}
285 MESSAGE("Erreur a la lecture du fichier");
286 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
290 endService("Med_Gen_i::readStructFileWithFieldType");
293 //=============================================================================
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)
303 beginService("Med_Gen_i::readMeshInFile");
305 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
307 // if (!_duringLoad) addInStudy(myStudy) ;
309 // Creation du maillage
314 // check mesh or grid:
316 if ( MEDFILEBROWSER( fileName ).isStructuredMesh( meshName ) )
319 myMesh = new MESH() ;
321 catch (const std::exception & ex) {
322 MESSAGE("Exception Interceptee : ");
324 myMesh = new MESH() ;
327 myMesh->setName(meshName);
328 MED_MESH_RDONLY_DRIVER myMeshDriver(fileName,myMesh);
331 myMeshDriver.setMeshName(meshName);
334 #if defined(_DEBUG_) || defined(_DEBUG)
335 catch (const std::exception & ex)
337 catch (const std::exception &)
340 MESSAGE("Exception Interceptee : ");
342 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
347 MESSAGE("apres read");
348 myMeshDriver.close();
350 #if defined(_DEBUG_) || defined(_DEBUG)
351 catch (const std::exception & ex)
353 catch (const std::exception &)
356 MESSAGE("Exception Interceptee : ");
358 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
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();
366 // add the mesh object in study
367 // if (!_duringLoad) meshi->addInStudy(myStudy,mesh);
369 catch (const SALOMEDS::StudyBuilder::LockProtection & ) {}
371 endService("Med_Gen_i::readMeshInFile");
375 //=============================================================================
379 //=============================================================================
380 SALOME_MED::FIELD_ptr Med_Gen_i::readFieldInFile(const char* fileName,
381 const char* studyName,
382 const char* fieldName,
385 throw (SALOME::SALOME_Exception)
387 beginService("Med_Gen_i::readFieldInFile");
389 string myStudyName(studyName);
391 if (myStudyName.size() == 0)
392 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
395 // Get StudyManager Reference, current study,
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", \
407 SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
408 SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
409 if (CORBA::is_nil(medfather))
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);
416 medfather = myBuilder->NewComponent("MED");
417 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(
418 myBuilder->FindOrCreateAttribute(medfather, "AttributeName"));
419 //NRI aName->SetValue("Med");
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() );
428 CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
429 myBuilder->DefineComponentInstance(medfather,myO);
431 if (aLocked) myStudy->GetProperties()->SetLocked(true);
432 myBuilder->CommitCommand();
436 MESSAGE("MED dejà dans l étude");
438 MESSAGE("Lecture du fichier ")
444 MEDFILEBROWSER mymed;
447 mymed.readFileStruct(fileName) ;
449 catch (const std::exception & ex)
451 MESSAGE("Exception Interceptee : ");
453 THROW_SALOME_CORBA_EXCEPTION("Unable to find this file ",SALOME::BAD_PARAM);
458 vector<string> fieldsNames = mymed.getFieldNames() ;
459 int numberOfFields = fieldsNames.size();
461 for (i=0; i<numberOfFields; i++)
463 if (fieldsNames[i]== fieldName) break;
465 if (i == numberOfFields)
467 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field ",SALOME::BAD_PARAM);
471 switch( mymed.getFieldType( fieldName ))
473 case MED_REEL64: myField = new FIELD<double>; break;
475 case MED_INT64: myField = new FIELD<int>; break;
477 THROW_SALOME_CORBA_EXCEPTION("Invalid field type",SALOME::BAD_PARAM);
479 myField->setIterationNumber( iter );
480 myField->setOrderNumber( ordre );
481 myField->addDriver( MED_DRIVER, fileName, fieldName, MED_EN::RDONLY);
484 #if defined(_DEBUG_) || defined(_DEBUG)
485 catch (const std::exception & ex)
487 catch (const std::exception &)
490 MESSAGE("Exception Interceptee : ");
492 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field in this file",SALOME::BAD_PARAM);
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 );
501 SCRUTE(myMesh->getName());
503 #if defined(_DEBUG_) || defined(_DEBUG)
504 catch (const std::exception & ex)
506 catch (const std::exception &)
509 MESSAGE("Exception Interceptee : ");
511 THROW_SALOME_CORBA_EXCEPTION("Unable to find associated mesh",SALOME::BAD_PARAM);
514 med_type_champ type = myField->getValueType() ;
517 case MED_EN::MED_INT32:
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");
529 catch (const SALOMEDS::StudyBuilder::LockProtection &) {}
530 #if defined(_DEBUG_) || defined(_DEBUG)
531 catch (const std::exception & ex)
533 catch (const std::exception &)
536 MESSAGE("Exception Interceptee : ");
538 THROW_SALOME_CORBA_EXCEPTION("Unable to read int field",SALOME::BAD_PARAM);
542 case MED_EN::MED_REEL64:
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");
554 catch (const SALOMEDS::StudyBuilder::LockProtection &) {}
556 #if defined(_DEBUG_) || defined(_DEBUG)
557 catch (const std::exception & ex)
559 catch (const std::exception &)
562 MESSAGE("Exception Interceptee : ");
564 THROW_SALOME_CORBA_EXCEPTION("Unable to read double field",SALOME::BAD_PARAM);
569 THROW_SALOME_CORBA_EXCEPTION("Not recognized type of field !",SALOME::BAD_PARAM);
572 return SALOME_MED::FIELD::_nil();
576 //=============================================================================
580 //=============================================================================
581 Engines::EngineComponent_ptr Med_Gen_i::GetComponentInstance()
583 return MED_Gen::_this();
586 char* Med_Gen_i::ComponentDataType()
588 MESSAGE("Med_Gen_i::ComponentDataType");
589 return CORBA::string_dup("MED") ; /* What is this type ? */
592 // Version information
593 char* Med_Gen_i::getVersion()
595 #if SALOMEMED_DEVELOPMENT
596 return CORBA::string_dup(SALOMEMED_VERSION_STR"dev");
598 return CORBA::string_dup(SALOMEMED_VERSION_STR);
604 //=============================================================================
606 * C factory, accessible with dlsym, after dlopen
608 //=============================================================================
610 extern "C" MED_EXPORT
611 PortableServer::ObjectId * MEDEngine_factory(
613 PortableServer::POA_ptr poa,
614 PortableServer::ObjectId * contId,
615 const char *instanceName,
616 const char *interfaceName)
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() ;