1 // Copyright (C) 2007-2008 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
22 // MED MED : implemetation of MED idl descriptions
23 // File : Med_Gen_i.cxx
24 // Author : Paul RASCLE, EDF
28 #include "Med_Gen_i.hxx"
30 #include "MEDMEM_Mesh.hxx"
31 #include "MEDMEM_Med_i.hxx"
32 #include "MEDMEM_FieldTemplate_i.hxx"
33 #include "MEDMEM_Support_i.hxx"
35 #include "MEDMEM_Mesh_i.hxx"
36 #include "MEDMEM_Field.hxx"
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"
47 #include "Utils_SINGLETON.hxx"
49 #include "Utils_CorbaException.hxx"
50 #include "utilities.h"
52 #include "SALOMEDS_Tool.hxx"
58 #include <HDFascii.hxx>
61 using namespace MEDMEM;
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;
68 //=============================================================================
70 * default constructor: not for use
72 //=============================================================================
73 //Med_Gen_i::Med_Gen_i()
75 // MESSAGE("Med_Gen_i::Med_Gen_i");
78 //=============================================================================
80 * standard constructor
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),
91 MESSAGE("activate object");
93 _id = _poa->activate_object(_thisObj);
96 // get an NamingService interface
97 //_NS = SINGLETON_<SALOME_NamingService>::Instance() ;
98 //ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
99 //_NS->init_orb( _orb ) ;
105 //=============================================================================
109 //=============================================================================
110 Med_Gen_i::~Med_Gen_i()
112 MESSAGE("Med_Gen_i::~Med_Gen_i");
115 //=============================================================================
117 * GetServant [ static ]
119 * Get servant of the CORBA object
121 //=============================================================================
122 PortableServer::ServantBase_var Med_Gen_i::GetServant( CORBA::Object_ptr theObject )
124 if( CORBA::is_nil( theObject ) || !_MEDGen || CORBA::is_nil( _MEDGen->_poa ) )
127 PortableServer::Servant aServant = _MEDGen->_poa->reference_to_servant( theObject );
131 INFOS( "GetServant - Unknown exception was caught!!!" );
136 //=============================================================================
138 * private method : change a study name in SALOMEDS::Study_var
140 //=============================================================================
141 SALOMEDS::Study_var Med_Gen_i::studyName2Study(const char* studyName)
142 throw(SALOME::SALOME_Exception)
144 string myStudyName(studyName);
146 if (myStudyName.size() == 0)
147 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
150 // Get StudyManager Reference, current study,
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", \
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", \
165 return SALOMEDS::Study::_duplicate(myStudy) ;
168 //=============================================================================
170 * private method : add Med component in Study (Not MedGen ???)
172 //=============================================================================
173 void Med_Gen_i::addInStudy(SALOMEDS::Study_var myStudy)
174 throw(SALOME::SALOME_Exception)
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) )
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);
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");
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() );
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);
204 if (aLocked) myStudy->GetProperties()->SetLocked(true);
205 myBuilder->CommitCommand();
209 //=============================================================================
211 * Lit tous les objets contenus dans un fichier med et les lit en memoire
213 //=============================================================================
214 SALOME_MED::MED_ptr Med_Gen_i::readStructFile (const char* fileName,
215 const char* studyName)
216 throw(SALOME::SALOME_Exception)
218 beginService("Med_Gen_i::readStructFile");
221 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
222 //if (!_duringLoad) addInStudy(myStudy) ;
224 SALOME_MED::MED_ptr myMedIOR ;
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();
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) ;
241 catch (const SALOMEDS::StudyBuilder::LockProtection & ) {}
244 MESSAGE("Erreur a la lecture du fichier");
245 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
249 endService("Med_Gen_i::readStructFile");
253 //=============================================================================
255 * Prepare un Maillage sans le lire en memoire avec stokage dans l'etude
256 * des champs avec leur type
258 //=============================================================================
259 void Med_Gen_i::readStructFileWithFieldType (const char* fileName,
260 const char* studyName)
261 throw (SALOME::SALOME_Exception)
263 beginService("Med_Gen_i::readStructFileWithFieldType");
265 const char* LOC = "Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)";
269 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
270 if (!_duringLoad) addInStudy(myStudy) ;
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();
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);
287 catch (const SALOMEDS::StudyBuilder::LockProtection & ) {}
290 MESSAGE("Erreur a la lecture du fichier");
291 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
295 endService("Med_Gen_i::readStructFileWithFieldType");
299 //=============================================================================
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)
309 beginService("Med_Gen_i::readMeshInFile");
311 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
313 // if (!_duringLoad) addInStudy(myStudy) ;
315 // Creation du maillage
320 // check mesh or grid:
322 MED med(MED_DRIVER,fileName);
323 MESH* tmpMesh = med.getMesh(meshName);
325 if(tmpMesh->getIsAGrid())
328 myMesh = new MESH() ;
331 catch (const std::exception & ex) {
332 MESSAGE("Exception Interceptee : ");
334 myMesh = new MESH() ;
337 myMesh->setName(meshName);
338 MED_MESH_RDONLY_DRIVER myMeshDriver(fileName,myMesh);
341 myMeshDriver.setMeshName(meshName);
344 #if defined(_DEBUG_) || defined(_DEBUG)
345 catch (const std::exception & ex)
347 catch (const std::exception &)
350 MESSAGE("Exception Interceptee : ");
352 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
357 MESSAGE("apres read");
358 myMeshDriver.close();
360 #if defined(_DEBUG_) || defined(_DEBUG)
361 catch (const std::exception & ex)
363 catch (const std::exception &)
366 MESSAGE("Exception Interceptee : ");
368 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
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();
376 // add the mesh object in study
377 // if (!_duringLoad) meshi->addInStudy(myStudy,mesh);
379 catch (const SALOMEDS::StudyBuilder::LockProtection & ) {}
381 endService("Med_Gen_i::readMeshInFile");
385 //=============================================================================
389 //=============================================================================
390 SALOME_MED::FIELD_ptr Med_Gen_i::readFieldInFile(const char* fileName,
391 const char* studyName,
392 const char* fieldName,
395 throw (SALOME::SALOME_Exception)
397 beginService("Med_Gen_i::readFieldInFile");
399 string myStudyName(studyName);
401 if (myStudyName.size() == 0)
402 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
405 // Get StudyManager Reference, current study,
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", \
417 SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
418 SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
419 if (CORBA::is_nil(medfather))
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);
426 medfather = myBuilder->NewComponent("MED");
427 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(
428 myBuilder->FindOrCreateAttribute(medfather, "AttributeName"));
429 //NRI aName->SetValue("Med");
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() );
438 CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
439 myBuilder->DefineComponentInstance(medfather,myO);
441 if (aLocked) myStudy->GetProperties()->SetLocked(true);
442 myBuilder->CommitCommand();
446 MESSAGE("MED dejà dans l étude");
448 MESSAGE("Lecture du fichier ")
457 mymed = new MED(MED_DRIVER,fileName) ;
459 catch (const std::exception & ex)
461 MESSAGE("Exception Interceptee : ");
463 THROW_SALOME_CORBA_EXCEPTION("Unable to find this file ",SALOME::BAD_PARAM);
468 deque<string> fieldsNames = mymed->getFieldNames() ;
469 int numberOfFields = fieldsNames.size();
471 for (i=0; i<numberOfFields; i++)
473 if (fieldsNames[i]== fieldName) break;
475 if (i == numberOfFields)
477 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field ",SALOME::BAD_PARAM);
481 deque<DT_IT_> myIteration = mymed->getFieldIteration (fieldName);
482 if (myIteration.size() != 1)
484 MESSAGE("WARNING : My iteration size is ")
485 SCRUTE(myIteration.size());
488 myField = mymed->getField(fieldName,ordre,iter);
490 #if defined(_DEBUG_) || defined(_DEBUG)
491 catch (const std::exception & ex)
493 catch (const std::exception &)
496 MESSAGE("Exception Interceptee : ");
498 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field in this file",SALOME::BAD_PARAM);
501 SUPPORT * fieldSupport;
504 fieldSupport=(SUPPORT *)myField->getSupport() ;
505 ASSERT(fieldSupport != NULL);
506 MESH * myMesh=(MESH *)fieldSupport->getMesh();
507 ASSERT(myMesh != NULL);
509 SCRUTE(myMesh->getName());
510 fieldSupport->update();
512 #if defined(_DEBUG_) || defined(_DEBUG)
513 catch (const std::exception & ex)
515 catch (const std::exception &)
518 MESSAGE("Exception Interceptee : ");
520 THROW_SALOME_CORBA_EXCEPTION("Unable to find associated support",SALOME::BAD_PARAM);
523 med_type_champ type = myField->getValueType() ;
526 case MED_EN::MED_INT32:
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");
537 catch (const SALOMEDS::StudyBuilder::LockProtection &) {}
538 #if defined(_DEBUG_) || defined(_DEBUG)
539 catch (const std::exception & ex)
541 catch (const std::exception &)
544 MESSAGE("Exception Interceptee : ");
546 THROW_SALOME_CORBA_EXCEPTION("Unable to read int field",SALOME::BAD_PARAM);
550 case MED_EN::MED_REEL64:
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");
561 catch (const SALOMEDS::StudyBuilder::LockProtection &) {}
563 #if defined(_DEBUG_) || defined(_DEBUG)
564 catch (const std::exception & ex)
566 catch (const std::exception &)
569 MESSAGE("Exception Interceptee : ");
571 THROW_SALOME_CORBA_EXCEPTION("Unable to read double field",SALOME::BAD_PARAM);
577 return SALOME_MED::FIELD::_nil();
581 //=============================================================================
585 //=============================================================================
586 Engines::Component_ptr Med_Gen_i::GetComponentInstance()
588 return MED_Gen::_this();
591 char* Med_Gen_i::ComponentDataType()
593 MESSAGE("Med_Gen_i::ComponentDataType");
594 return CORBA::string_dup("MED") ; /* What is this type ? */
599 //=============================================================================
601 * C factory, accessible with dlsym, after dlopen
603 //=============================================================================
605 extern "C" MED_EXPORT
606 PortableServer::ObjectId * MEDEngine_factory(
608 PortableServer::POA_ptr poa,
609 PortableServer::ObjectId * contId,
610 const char *instanceName,
611 const char *interfaceName)
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() ;