1 // MED MED : implemetation of MED idl descriptions
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : Med_Gen_i.cxx
25 // Author : Paul RASCLE, EDF
30 #include "Med_Gen_i.hxx"
32 #include "MEDMEM_Mesh_i.hxx"
33 #include "MEDMEM_Med_i.hxx"
34 #include "MEDMEM_FieldInt_i.hxx"
35 #include "MEDMEM_FieldDouble_i.hxx"
36 #include "MEDMEM_Support_i.hxx"
38 #include "MEDMEM_Mesh.hxx"
39 #include "MEDMEM_Field.hxx"
40 #include "MEDMEM_Med.hxx"
41 #include "MEDMEM_MedMedDriver.hxx"
42 #include "MEDMEM_MedMeshDriver.hxx"
43 #include "MEDMEM_MedFieldDriver.hxx"
44 #include "MEDMEM_define.hxx"
45 #include "MEDMEM_DriversDef.hxx"
48 #include "Utils_SINGLETON.hxx"
50 #include "Utils_CorbaException.hxx"
51 #include "utilities.h"
57 #include <TCollection_AsciiString.hxx>
58 #include <TColStd_SequenceOfAsciiString.hxx>
59 #include <HDFascii.hxx>
60 #include "SALOMEDS_Tool.hxx"
62 using namespace MEDMEM;
64 // Initialisation des variables statiques
65 map <string, string> Med_Gen_i::_MedCorbaObj;
66 string Med_Gen_i::_myFileName="";
67 string Med_Gen_i::_saveFileName="";
69 //=============================================================================
71 * default constructor: not for use
73 //=============================================================================
75 Med_Gen_i::Med_Gen_i()
77 MESSAGE("Med_Gen_i::Med_Gen_i");
80 //=============================================================================
82 * standard constructor
84 //=============================================================================
86 Med_Gen_i:: Med_Gen_i(CORBA::ORB_ptr orb,
87 PortableServer::POA_ptr poa,
88 PortableServer::ObjectId * contId,
89 const char *instanceName,
90 const char *interfaceName) :
91 Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
93 MESSAGE("activate object");
95 _id = _poa->activate_object(_thisObj);
98 // get an NamingService interface
99 _NS = SINGLETON_<SALOME_NamingService>::Instance() ;
100 ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
101 _NS->init_orb( _orb ) ;
103 //=============================================================================
105 * private method : change a study name in SALOMEDS::Study_var
107 //=============================================================================
109 SALOMEDS::Study_var Med_Gen_i::studyName2Study(const char* studyName)
110 throw(SALOME::SALOME_Exception)
112 string myStudyName(studyName);
114 if (myStudyName.size() == 0)
115 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
118 // Get StudyManager Reference, current study,
120 CORBA::Object_var obj = _NS->Resolve("/myStudyManager");
121 SALOMEDS::StudyManager_var myStudyManager =
122 SALOMEDS::StudyManager::_narrow(obj);
123 if(CORBA::is_nil(myStudyManager))
124 THROW_SALOME_CORBA_EXCEPTION("No StudyManager Found in NameService", \
127 SALOMEDS::Study_var myStudy =
128 myStudyManager->GetStudyByName(myStudyName.c_str());
129 if (CORBA::is_nil(myStudy))
130 THROW_SALOME_CORBA_EXCEPTION("Wrong Study Name", \
133 return SALOMEDS::Study::_duplicate(myStudy) ;
136 //=============================================================================
138 * private method : add Med component in Study (Not MedGen ???)
140 //=============================================================================
142 void Med_Gen_i::addInStudy(SALOMEDS::Study_var myStudy)
143 throw(SALOME::SALOME_Exception)
145 SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
147 // Create SComponent labelled 'Med' if it doesn't already exit
148 SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
149 if ( CORBA::is_nil(medfather) )
151 myBuilder->NewCommand();
152 // mpv: component label must be created in spite of "Locked" study flag state
153 bool aLocked = myStudy->GetProperties()->IsLocked();
154 if (aLocked) myStudy->GetProperties()->SetLocked(false);
156 MESSAGE("Add Component Med");
157 medfather = myBuilder->NewComponent("MED");
158 SALOMEDS::GenericAttribute_var anAttr = myBuilder->FindOrCreateAttribute(medfather, "AttributeName");
159 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
160 //NRI aName->SetValue("Med");
162 CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
163 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
164 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
165 if ( !Comp->_is_nil() ) {
166 aName->SetValue( Comp->componentusername() );
169 // Utilisation de this deconseillee par Paul ??
170 // myBuilder->DefineComponentInstance(medfather,POA_Engines::MED_Gen::_this());
171 CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
172 myBuilder->DefineComponentInstance(medfather,myO);
174 if (aLocked) myStudy->GetProperties()->SetLocked(true);
175 myBuilder->CommitCommand();
181 //=============================================================================
183 * Lit tous les objets contenus dans un fichier med et les lit en memoire
185 //=============================================================================
186 SALOME_MED::MED_ptr Med_Gen_i::readStructFile (const char* fileName,
187 const char* studyName)
188 throw(SALOME::SALOME_Exception)
191 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
192 if (!_duringLoad) addInStudy(myStudy) ;
194 SALOME_MED::MED_ptr myMedIOR ;
197 // we create a new MED_i and add in study
198 MED_i * myMedI = new MED_i();
199 myMedIOR = myMedI->_this() ;
200 if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR) ;
201 // create ::MED object, read all and add in study !
202 myMedI->init(myStudy,MED_DRIVER,fileName) ;
204 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
207 MESSAGE("Erreur a la lecture du fichier");
208 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
211 return SALOME_MED::MED::_duplicate(myMedIOR) ;
214 //=============================================================================
216 * Prepare un Maillage sans le lire en memoire avec stokage dans l'etude
217 * des champs avec leur type
219 //=============================================================================
220 void Med_Gen_i::readStructFileWithFieldType (const char* fileName,
221 const char* studyName)
222 throw (SALOME::SALOME_Exception)
224 BEGIN_OF("Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)");
227 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
229 if (!_duringLoad) addInStudy(myStudy) ;
233 // we create a new MED_i and add in study
234 MED_i * myMedI = new MED_i();
235 SALOME_MED::MED_ptr myMedIOR = myMedI->_this() ;
236 if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR) ;
237 // create ::MED object, read all and add in study !
238 myMedI->initWithFieldType(myStudy,MED_DRIVER,fileName) ;
240 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
243 MESSAGE("Erreur a la lecture du fichier");
244 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
248 END_OF("Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)");
253 //=============================================================================
257 //=============================================================================
258 SALOME_MED::MESH_ptr Med_Gen_i::readMeshInFile(const char* fileName,
259 const char* studyName,
260 const char* meshName)
261 throw (SALOME::SALOME_Exception)
264 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
266 if (!_duringLoad) addInStudy(myStudy) ;
268 // Creation du maillage
270 MESH * myMesh= new MESH() ;
271 myMesh->setName(meshName);
272 MED_MESH_RDONLY_DRIVER myMeshDriver(fileName,myMesh);
275 myMeshDriver.setMeshName(meshName);
278 catch (const exception & ex)
280 MESSAGE("Exception Interceptee : ");
282 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
287 MESSAGE("apres read");
288 myMeshDriver.close();
290 catch (const exception & ex)
292 MESSAGE("Exception Interceptee : ");
294 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
297 MESH_i * meshi = new MESH_i(myMesh);
298 //SALOME_MED::MESH_var mesh = SALOME_MED::MESH::_narrow(meshi->_this());
299 SALOME_MED::MESH_ptr mesh = meshi->_this();
302 // add the mesh object in study
303 if (!_duringLoad) meshi->addInStudy(myStudy,mesh);
305 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
306 return SALOME_MED::MESH::_duplicate(mesh);
308 //=============================================================================
312 //=============================================================================
313 SALOME_MED::FIELD_ptr Med_Gen_i::readFieldInFile(const char* fileName,
314 const char* studyName,
315 const char* fieldName,
318 throw (SALOME::SALOME_Exception)
321 string myStudyName(studyName);
323 if (myStudyName.size() == 0)
324 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
327 // Get StudyManager Reference, current study,
329 CORBA::Object_var obj = _NS->Resolve("/myStudyManager");
330 SALOMEDS::StudyManager_var myStudyManager =
331 SALOMEDS::StudyManager::_narrow(obj);
332 ASSERT(! CORBA::is_nil(myStudyManager));
333 SALOMEDS::Study_var myStudy =
334 myStudyManager->GetStudyByName(myStudyName.c_str());
335 if (CORBA::is_nil(myStudy))
336 THROW_SALOME_CORBA_EXCEPTION("Wrong Study Name", \
339 SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
340 SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
341 if (CORBA::is_nil(medfather))
343 myBuilder->NewCommand();
344 // mpv: component label must be created in spite of "Locked" study flag state
345 bool aLocked = myStudy->GetProperties()->IsLocked();
346 if (aLocked) myStudy->GetProperties()->SetLocked(false);
348 medfather = myBuilder->NewComponent("MED");
349 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(
350 myBuilder->FindOrCreateAttribute(medfather, "AttributeName"));
351 //NRI aName->SetValue("Med");
353 CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
354 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
355 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
356 if ( !Comp->_is_nil() ) {
357 aName->SetValue( Comp->componentusername() );
360 CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
361 myBuilder->DefineComponentInstance(medfather,myO);
363 if (aLocked) myStudy->GetProperties()->SetLocked(true);
364 myBuilder->CommitCommand();
368 MESSAGE("MED dejà dans l étude");
370 MESSAGE("Lecture du fichier ")
375 FIELD_ * myField= new FIELD_() ;
376 MED * mymed = new MED(MED_DRIVER,fileName) ;
379 deque<string> fieldsNames = mymed->getFieldNames() ;
380 int numberOfFields = fieldsNames.size();
382 for (i=0; i<numberOfFields; i++)
384 if (fieldsNames[i]== fieldName) break;
386 if (i == numberOfFields)
388 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field ",SALOME::BAD_PARAM);
392 deque<DT_IT_> myIteration = mymed->getFieldIteration (fieldName);
393 if (myIteration.size() != 1)
395 MESSAGE("WARNING : My iteration size is ")
396 SCRUTE(myIteration.size());
399 myField = mymed->getField(fieldName,iter,ordre);
401 catch (const exception & ex)
403 MESSAGE("Exception Interceptee : ");
405 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field in this file",SALOME::BAD_PARAM);
408 SUPPORT * fieldSupport;
409 SALOME_MED::SUPPORT_ptr mySupportIOR;
412 fieldSupport=(SUPPORT *)myField->getSupport() ;
413 ASSERT(fieldSupport != NULL);
414 MESH * myMesh=(MESH *)fieldSupport->getMesh();
415 ASSERT(myMesh != NULL);
417 SCRUTE(myMesh->getName());
418 fieldSupport->update();
419 SUPPORT_i * mySupportI = new SUPPORT_i(fieldSupport);
420 mySupportIOR=mySupportI->_this();
422 catch (const exception & ex)
424 MESSAGE("Exception Interceptee : ");
426 THROW_SALOME_CORBA_EXCEPTION("Unable to find associated support",SALOME::BAD_PARAM);
429 med_type_champ type = myField->getValueType() ;
432 case MED_FR::MED_INT32:
436 ((FIELD<int>*)myField)->read() ;
437 FIELDINT_i * myFieldIntI = new FIELDINT_i(mySupportIOR,(FIELD<int>*)myField);
438 POA_SALOME_MED::FIELD_tie<FIELD_i> * myFieldTie = new POA_SALOME_MED::FIELD_tie<FIELD_i>(myFieldIntI);
439 SALOME_MED::FIELD_ptr myFieldIOR = myFieldTie->_this() ;
440 if (!_duringLoad) myFieldIntI->addInStudy(myStudy,myFieldIOR) ;
441 return SALOME_MED::FIELD::_duplicate(myFieldIOR);
443 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
444 catch (const exception & ex)
446 MESSAGE("Exception Interceptee : ");
448 THROW_SALOME_CORBA_EXCEPTION("Unable to read int field",SALOME::BAD_PARAM);
452 case MED_FR::MED_REEL64:
456 ((FIELD<double>*)myField)->read() ;
457 FIELDDOUBLE_i * myFieldDoubleI = new FIELDDOUBLE_i(mySupportIOR,(FIELD<double>*)myField);
458 POA_SALOME_MED::FIELD_tie<FIELD_i> * myFieldTie = new POA_SALOME_MED::FIELD_tie<FIELD_i>(myFieldDoubleI);
459 SALOME_MED::FIELD_ptr myFieldIOR = myFieldTie->_this() ;
460 if (!_duringLoad) myFieldDoubleI->addInStudy(myStudy,myFieldIOR) ;
461 return SALOME_MED::FIELD::_duplicate(myFieldIOR);
463 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
464 catch (const exception & ex)
466 MESSAGE("Exception Interceptee : ");
468 THROW_SALOME_CORBA_EXCEPTION("Unable to read double field",SALOME::BAD_PARAM);
478 //=============================================================================
482 //=============================================================================
484 Med_Gen_i::~Med_Gen_i()
486 MESSAGE("Med_Gen_i::~Med_Gen_i");
489 //=============================================================================
491 * CORBA: Save Mesh objects (called when a study is saved)
493 //=============================================================================
494 SALOMEDS::TMPFile* Med_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
497 const char* LOC = "Med_Gen_i::Save";
500 SALOMEDS::TMPFile_var aStreamFile;
501 // Get a temporary directory to store a file
502 TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
503 // Create a list to store names of created files
504 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
505 TColStd_SequenceOfAsciiString aFileNames;
507 CORBA::String_var aSaveStudyName("");
508 if (isMultiFile) aSaveStudyName = strdup(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()));
510 SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
511 if (!CORBA::is_nil(aMedMeshFather)) {
512 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
513 for(; anIter->More(); anIter->Next()) {
514 SALOMEDS::SObject_var aSO = anIter->Value();
515 SALOMEDS::GenericAttribute_var anAttr;
516 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
517 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
518 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
519 if (! CORBA::is_nil(myMesh)) {
520 TCollection_AsciiString aName(strdup(aSaveStudyName));
521 aName += "_MEDMESH_";
522 aName += myMesh->getName();
524 MESSAGE("Save mesh with name "<<aName.ToCString());
525 long driverId = myMesh->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myMesh->getName());
526 myMesh->write(driverId,"");
527 aFileNames.Append(aName);
533 SALOMEDS::SObject_var aMedFieldFather = theComponent->GetStudy()->FindObject("MEDFIELD");
534 if (!CORBA::is_nil(aMedFieldFather)) {
535 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedFieldFather);
536 for(; anIter->More(); anIter->Next()) {
537 SALOMEDS::SObject_var aSO = anIter->Value();
538 SALOMEDS::GenericAttribute_var anAttr;
539 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
540 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
541 SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
542 if (! CORBA::is_nil(myField)) {
544 a<< myField->getOrderNumber();
545 b<< myField->getIterationNumber();
547 TCollection_AsciiString aName(strdup(aSaveStudyName));
548 aName += "_MEDFIELD_";
549 aName += myField->getName();
551 aName += strdup(a.str().c_str());
553 aName += strdup(b.str().c_str());
555 MESSAGE("Save mesh with name "<<aName.ToCString());
556 long driverId = myField->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myField->getName());
557 myField->write(driverId,"");
558 aFileNames.Append(aName);
565 aSeq->length(aFileNames.Length());
566 for(i = aFileNames.Length(); i > 0; i--) aSeq[i-1] = CORBA::string_dup(aFileNames.Value(i).ToCString());
567 // Conver a file to the byte stream
568 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
569 // Remove the created file and tmp directory
570 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
571 // Return the created byte stream
572 return aStreamFile._retn();
577 SALOMEDS::TMPFile* Med_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
580 const char* LOC = "Med_Gen_i::SaveASCII";
583 SALOMEDS::TMPFile_var aStreamFile;
584 // Get a temporary directory to store a file
585 TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
586 // Create a list to store names of created files
587 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
588 TColStd_SequenceOfAsciiString aFileNames;
590 CORBA::String_var aSaveStudyName("");
591 if (isMultiFile) aSaveStudyName = strdup(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()));
593 SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
594 if (!CORBA::is_nil(aMedMeshFather)) {
595 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
596 for(; anIter->More(); anIter->Next()) {
597 SALOMEDS::SObject_var aSO = anIter->Value();
598 SALOMEDS::GenericAttribute_var anAttr;
599 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
600 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
601 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
602 if (! CORBA::is_nil(myMesh)) {
603 TCollection_AsciiString aName(strdup(aSaveStudyName));
604 aName += "_MEDMESH_";
605 aName += myMesh->getName();
607 MESSAGE("Save mesh with name "<<aName.ToCString());
608 long driverId = myMesh->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myMesh->getName());
609 myMesh->write(driverId,"");
610 HDFascii::ConvertFromHDFToASCII(strdup((aTmpDir+aName).ToCString()), true);
611 aFileNames.Append(aName);
617 SALOMEDS::SObject_var aMedFieldFather = theComponent->GetStudy()->FindObject("MEDFIELD");
618 if (!CORBA::is_nil(aMedFieldFather)) {
619 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedFieldFather);
620 for(; anIter->More(); anIter->Next()) {
621 SALOMEDS::SObject_var aSO = anIter->Value();
622 SALOMEDS::GenericAttribute_var anAttr;
623 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
624 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
625 SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
626 if (! CORBA::is_nil(myField)) {
628 a<< myField->getOrderNumber();
629 b<< myField->getIterationNumber();
631 TCollection_AsciiString aName(strdup(aSaveStudyName));
632 aName += "_MEDFIELD_";
633 aName += myField->getName();
635 aName += strdup(a.str().c_str());
637 aName += strdup(b.str().c_str());
639 MESSAGE("Save mesh with name "<<aName.ToCString());
640 long driverId = myField->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myField->getName());
641 myField->write(driverId,"");
642 HDFascii::ConvertFromHDFToASCII(strdup((aTmpDir+aName).ToCString()), true);
643 aFileNames.Append(aName);
650 aSeq->length(aFileNames.Length());
651 for(i = aFileNames.Length(); i > 0; i--) aSeq[i-1] = CORBA::string_dup(aFileNames.Value(i).ToCString());
652 // Conver a file to the byte stream
653 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
654 // Remove the created file and tmp directory
655 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
656 // Return the created byte stream
657 return aStreamFile._retn();
662 //=============================================================================
664 * CORBA: Load Mesh objects (called when an existing study is opened)
666 //=============================================================================
668 CORBA::Boolean Med_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
669 const SALOMEDS::TMPFile& theStream,
672 const char* LOC = "Med_Gen_i::Load";
675 // Get a temporary directory for a file
676 TCollection_AsciiString aTmpDir =
677 (isMultiFile)?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
678 _saveFileName = CORBA::string_dup(aTmpDir.ToCString());
679 SALOMEDS::ListOfFileNames_var aSeq =
680 SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(), isMultiFile);
686 CORBA::Boolean Med_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
687 const SALOMEDS::TMPFile& theStream,
690 return Load(theComponent, theStream, theURL, isMultiFile);
693 //=============================================================================
697 //=============================================================================
699 void Med_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
701 MESSAGE("Med_Gen_i::Close");
702 SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
703 if (!CORBA::is_nil(aMedMeshFather)) {
704 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
705 for(; anIter->More(); anIter->Next()) {
706 SALOMEDS::SObject_var aSO = anIter->Value();
707 SALOMEDS::GenericAttribute_var anAttr;
708 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
709 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
710 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
711 // here must call method destroy of myMesh, but it not implented yet
717 //=============================================================================
721 //=============================================================================
723 char* Med_Gen_i::ComponentDataType()
725 MESSAGE("Med_Gen_i::ComponentDataType");
726 return strdup("MED") ; /* What is this type ? */
729 //=============================================================================
731 * CORBA: give a persistent reference of a transient object (for study save)
733 //=============================================================================
735 char* Med_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
736 const char* IORString,
737 CORBA::Boolean isMultiFile,
738 CORBA::Boolean isASCII) {
739 const char * LOC = "Med_Gen_i::IORToLocalPersistentID" ;
744 if (string(IORString).size()==0) return "_MED";
745 // Well, we know where put object (_saveFilename) and we know object (IORString)
747 CORBA::Object_var myIOR = _orb->string_to_object(IORString);
750 SALOME_MED::MED_var myMed = SALOME_MED::MED::_narrow(myIOR);
751 if (! CORBA::is_nil(myMed))
753 // nothing to save : Support will be saved inside the mesh
754 string str_MedName="_MED Objet Med + /OBJ_MED/";
755 return strdup(str_MedName.c_str()) ;
759 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
760 if (! CORBA::is_nil(myMesh))
762 CORBA::String_var aName((string("_MEDMESH_")+ myMesh->getName() + ".med").c_str());
763 return strdup(aName._retn()) ;
767 SALOME_MED::SUPPORT_var mySupport = SALOME_MED::SUPPORT::_narrow(myIOR);
768 if (! CORBA::is_nil(mySupport))
770 // nothing to save : Support will be saved inside the mesh
771 string str_SupportName;
774 str_SupportName=string("/FAS/")+string(mySupport->getName());
775 str_SupportName+=string("/ENS_MAA/")+string(mySupport->getMesh()->getName());
776 SCRUTE(str_SupportName);
780 MESSAGE("Unable to save the support");
781 THROW_SALOME_CORBA_EXCEPTION("Unable to save Field in Med"\
782 ,SALOME::INTERNAL_ERROR);
784 return strdup(("_MED"+str_SupportName).c_str());
787 SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
788 if (! CORBA::is_nil(myField))
790 string str_FieldName;
792 a<< myField->getOrderNumber();
793 b<< myField->getIterationNumber();
794 CORBA::String_var aName((string("_MEDFIELD_")+ myField->getName() +
795 string("_ORDRE_")+a.str()+
796 string("_ITER_")+b.str() +
798 return strdup(aName._retn());
801 //THROW_SALOME_CORBA_EXCEPTION("Unable to save IOR",SALOME::BAD_PARAM);
807 //=============================================================================
809 * CORBA: give a transient reference (when loading an object, opening study)
811 //=============================================================================
813 char* Med_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
814 const char* aLocalPersistentID,
815 CORBA::Boolean isMultiFile,
816 CORBA::Boolean isASCII)
817 throw(SALOME::SALOME_Exception)
819 const char * LOC = "Med_Gen_i::LocalPersistentIDToIOR" ;
821 TCollection_AsciiString aTmpDir(CORBA::string_dup(_saveFileName.c_str()));
823 TCollection_AsciiString aSaveStudyName("");
824 if (isMultiFile) aSaveStudyName = strdup(SALOMEDS_Tool::GetNameFromPath(theSObject->GetStudy()->URL()));
826 if (strcmp(aLocalPersistentID, "Objet Med + /OBJ_MED/") == 0) return strdup(""); // MED
828 if (strncmp(aLocalPersistentID, "_MEDMESH_",9) == 0) {// MESH
829 MESH * myMesh= new MESH() ;
830 int aMeshNameLen = strlen(aLocalPersistentID) - 12;
831 char* aMeshName = new char[aMeshNameLen];
832 strncpy(aMeshName, &(aLocalPersistentID[9]), aMeshNameLen-1);
833 aMeshName[aMeshNameLen-1] = 0;
834 myMesh->setName(aMeshName);
838 char* aResultPath = HDFascii::ConvertFromASCIIToHDF((aTmpDir + strdup(aLocalPersistentID)).ToCString());
839 aFileName = new char[strlen(aResultPath) + 19];
840 sprintf(aFileName, "%shdf_from_ascii.hdf", aResultPath);
842 } else aFileName = strdup((aTmpDir + aSaveStudyName + strdup(aLocalPersistentID)).ToCString());
843 MED_MESH_RDONLY_DRIVER myMeshDriver(aFileName,myMesh);
846 myMeshDriver.setMeshName(aMeshName);
849 catch (const exception & ex)
851 MESSAGE("Exception Interceptee : ");
853 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
858 MESSAGE("apres read");
859 myMeshDriver.close();
861 catch (const exception & ex)
863 MESSAGE("Exception Interceptee : ");
865 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
867 MESH_i * meshi = new MESH_i(myMesh);
868 //SALOME_MED::MESH_var mesh = SALOME_MED::MESH::_narrow(meshi->_this());
869 SALOME_MED::MESH_ptr mesh = meshi->_this();
870 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
872 aSeq[0]=CORBA::string_dup(aLocalPersistentID);
873 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
875 SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
876 aFilesToRemove->length(1);
877 aFilesToRemove[0] = strdup(&(aFileName[strlen(SALOMEDS_Tool::GetDirFromPath(aFileName))]));
878 SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aFileName), aFilesToRemove, true);
881 return(CORBA::string_dup(_orb->object_to_string(mesh)));
882 } else if (strncmp(aLocalPersistentID, "_MEDFIELD_",14) == 0) { // FIELD
883 return(strdup("")); // not implemented yet
891 //=============================================================================
893 * returns true, if can publish object
895 //=============================================================================
896 bool Med_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
897 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(theIOR);
898 return !(aMesh->_is_nil());
901 //=============================================================================
903 * publish the given object
905 //=============================================================================
906 SALOMEDS::SObject_ptr Med_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
907 SALOMEDS::SObject_ptr theSObject,
908 CORBA::Object_ptr theObject,
909 const char* theName) throw (SALOME::SALOME_Exception)
911 SALOMEDS::SObject_var aResultSO;
913 if(CORBA::is_nil(theObject)) return aResultSO;
914 if(theStudy->_is_nil()) return aResultSO;
916 SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
917 SALOMEDS::GenericAttribute_var anAttr;
918 SALOMEDS::AttributeName_var aName;
919 SALOMEDS::AttributeIOR_var anIOR;
920 SALOMEDS::SComponent_var aFather = theStudy->FindComponent("MED");
922 if (aFather->_is_nil()) {
923 aFather = aBuilder->NewComponent("MED");
924 if (aFather->_is_nil()) return aResultSO;
925 anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributeName");
926 aName = SALOMEDS::AttributeName::_narrow(anAttr);
927 //NRI aName->SetValue("MED");
929 CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
930 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
931 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
932 if ( !Comp->_is_nil() ) {
933 aName->SetValue( Comp->componentusername() );
935 aBuilder->DefineComponentInstance(aFather, MED_Gen::_this());
938 if (CORBA::is_nil(theSObject)) {
939 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(theObject);
940 aMesh->addInStudy(theStudy, aMesh);
941 SALOMEDS::SObject_var aResultSO = theStudy->FindObjectIOR(_orb->object_to_string(theObject));
943 if (!theSObject->ReferencedObject(aResultSO))
944 THROW_SALOME_CORBA_EXCEPTION("Publish in study MED object error",SALOME::BAD_PARAM);
946 // aBuilder->Addreference(theObject, aResultSO);
947 return aResultSO._retn();
950 //=============================================================================
952 * returns true, if can copy the object
954 //=============================================================================
955 CORBA::Boolean Med_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
956 // Try to retrieve known by MED component mesh by given IOR
957 SALOMEDS::GenericAttribute_var anAttr;
958 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
960 CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
961 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(anObj);
962 // If the object is null one it can't be copied: return false
963 if (aMesh->_is_nil()) return false;
970 //=============================================================================
972 * create copy of the object and put it to the stream
974 //=============================================================================
975 SALOMEDS::TMPFile* Med_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
976 // Declare a sequence of the byte to store the copied object
977 SALOMEDS::TMPFile_var aStreamFile;
979 // Try to get GEOM_Shape object by given SObject
980 SALOMEDS::GenericAttribute_var anAttr;
981 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return new SALOMEDS::TMPFile(0);
982 CORBA::String_var anIOR = strdup(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
983 CORBA::Object_var anObj = _orb->string_to_object(anIOR);
984 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(anObj);
985 if (aMesh->_is_nil()) return new SALOMEDS::TMPFile(0);
987 // Get a temporary directory to store a temporary file
988 CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir();
989 // Create a list to store names of created files
990 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
992 aSeq[0] = strdup(aMesh->getName());
993 char* aFullName = new char[strlen(aTmpDir)+strlen(aSeq[0])+1];
994 strcpy(aFullName, aTmpDir);
995 strcpy(aFullName+strlen(aTmpDir), aSeq[0]);
996 long driverId = aMesh->addDriver(SALOME_MED::MED_DRIVER,strdup(aFullName) , aMesh->getName());
997 aMesh->write(driverId,"");
1000 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir, aSeq.in(), false);
1001 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeq.in(), true);
1003 // Assign an ID = 1 the the type SALOME_MED::MESH
1006 return aStreamFile._retn();
1009 //=============================================================================
1011 * returns true, if can copy the object
1013 //=============================================================================
1014 CORBA::Boolean Med_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
1015 // The MED component can paste only objects copied by MED component
1016 // and with the object type = 1 (mesh)
1017 if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
1021 //=============================================================================
1023 * returns true, if can copy the object
1025 //=============================================================================
1026 SALOMEDS::SObject_ptr Med_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
1027 CORBA::Long theObjectID,
1028 SALOMEDS::SObject_ptr theObject) {
1029 SALOMEDS::SObject_var aResultSO = SALOMEDS::SObject::_duplicate(theObject);
1030 if (theStream.length() == 0) return aResultSO._retn();
1032 SALOMEDS::Study_var aStudy = theObject->GetStudy();
1034 CORBA::String_var aTmpDir = strdup(SALOMEDS_Tool::GetTmpDir());
1035 SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
1036 CORBA::String_var aMeshName = strdup(aSeq[0]);
1037 char* aFullName = new char[strlen(aTmpDir)+strlen(aMeshName)+1];
1038 strcpy(aFullName, aTmpDir);
1039 strcpy(aFullName+strlen(aTmpDir), aMeshName);
1041 MESH * myMesh= new MESH() ;
1042 myMesh->setName((char*)aMeshName);
1043 MED_MESH_RDONLY_DRIVER myMeshDriver(aFullName, myMesh);
1045 myMeshDriver.setMeshName((char*)aMeshName);
1046 myMeshDriver.open();
1047 } catch (const exception & ex) {
1048 MESSAGE("Exception Interceptee : ");
1051 return aResultSO._retn();
1054 myMeshDriver.read();
1056 myMeshDriver.close();
1057 } catch (const exception & ex) {
1058 MESSAGE("Exception Interceptee : ");
1061 return aResultSO._retn();
1063 // set new mesh name, becouse now there are no possibility to operate meshes with the same names
1064 // srand((unsigned int)time(NULL));
1065 int aRND = rand(); //Get a random number to present a name of a copied mesh
1066 char aCopiedMeshName[20];
1067 sprintf(aCopiedMeshName,"MESH_COPY_%d",aRND);
1068 myMesh->setName(aCopiedMeshName);
1069 MESH_i * meshi = new MESH_i(myMesh);
1070 SALOME_MED::MESH_ptr mesh = meshi->_this();
1071 // add the mesh object in study
1072 meshi->addInStudy(aStudy,mesh);
1073 // get the IOR attribute of just added mesh
1074 CORBA::String_var anIORString = _orb->object_to_string(mesh);
1075 aResultSO = aStudy->FindObjectIOR(anIORString);
1077 SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir, aSeq.in(), true);
1079 return aResultSO._retn();
1087 //=============================================================================
1089 * C factory, accessible with dlsym, after dlopen
1091 //=============================================================================
1095 PortableServer::ObjectId * MEDEngine_factory(
1097 PortableServer::POA_ptr poa,
1098 PortableServer::ObjectId * contId,
1099 const char *instanceName,
1100 const char *interfaceName)
1102 MESSAGE("PortableServer::ObjectId * MedEngine_factory()");
1103 SCRUTE(interfaceName);
1104 Med_Gen_i * myMed_Gen
1105 = new Med_Gen_i(orb, poa, contId, instanceName, interfaceName);
1106 return myMed_Gen->getId() ;