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
29 #include "Med_Gen_i.hxx"
31 #include "MEDMEM_Mesh_i.hxx"
32 #include "MEDMEM_Med_i.hxx"
33 #include "MEDMEM_FieldInt_i.hxx"
34 #include "MEDMEM_FieldDouble_i.hxx"
35 #include "MEDMEM_Support_i.hxx"
37 #include "MEDMEM_Mesh.hxx"
38 #include "MEDMEM_Field.hxx"
39 #include "MEDMEM_Med.hxx"
40 #include "MEDMEM_MedMedDriver.hxx"
41 #include "MEDMEM_MedMeshDriver21.hxx"
42 #include "MEDMEM_MedFieldDriver.hxx"
43 #include "MEDMEM_define.hxx"
44 #include "MEDMEM_DriversDef.hxx"
47 #include "Utils_SINGLETON.hxx"
49 #include "Utils_CorbaException.hxx"
50 #include "utilities.h"
56 #include <TCollection_AsciiString.hxx>
57 #include <TColStd_SequenceOfAsciiString.hxx>
58 #include <HDFascii.hxx>
59 #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();
146 // Create SComponent labelled 'Med' if it doesn't already exit
147 SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
148 if ( CORBA::is_nil(medfather) )
150 myBuilder->NewCommand();
151 // mpv: component label must be created in spite of "Locked" study flag state
152 bool aLocked = myStudy->GetProperties()->IsLocked();
153 if (aLocked) myStudy->GetProperties()->SetLocked(false);
155 MESSAGE("Add Component Med");
156 medfather = myBuilder->NewComponent("MED");
157 SALOMEDS::GenericAttribute_var anAttr = myBuilder->FindOrCreateAttribute(medfather, "AttributeName");
158 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
159 //NRI aName->SetValue("Med");
161 CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
162 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
163 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
164 if ( !Comp->_is_nil() ) {
165 aName->SetValue( Comp->componentusername() );
168 // Utilisation de this deconseillee par Paul ??
169 // myBuilder->DefineComponentInstance(medfather,POA_Engines::MED_Gen::_this());
170 CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
171 myBuilder->DefineComponentInstance(medfather,myO);
173 if (aLocked) myStudy->GetProperties()->SetLocked(true);
174 myBuilder->CommitCommand();
180 //=============================================================================
182 * Lit tous les objets contenus dans un fichier med et les lit en memoire
184 //=============================================================================
185 SALOME_MED::MED_ptr Med_Gen_i::readStructFile (const char* fileName,
186 const char* studyName)
187 throw(SALOME::SALOME_Exception)
189 beginService("Med_Gen_i::readStructFile");
192 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
193 // if (!_duringLoad) addInStudy(myStudy) ;
195 SALOME_MED::MED_ptr myMedIOR ;
198 // we create a new MED_i and add in study
199 MED_i * myMedI = new MED_i();
200 myMedIOR = myMedI->_this() ;
201 // if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR) ;
202 // if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR,fileName) ;
203 // create ::MED object, read all and add in study !
204 myMedI->init(myStudy,MED_DRIVER,fileName) ;
206 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
209 MESSAGE("Erreur a la lecture du fichier");
210 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
214 endService("Med_Gen_i::readStructFile");
218 //=============================================================================
220 * Prepare un Maillage sans le lire en memoire avec stokage dans l'etude
221 * des champs avec leur type
223 //=============================================================================
224 void Med_Gen_i::readStructFileWithFieldType (const char* fileName,
225 const char* studyName)
226 throw (SALOME::SALOME_Exception)
228 beginService("Med_Gen_i::readStructFileWithFieldType");
230 BEGIN_OF("Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)");
233 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
234 if (!_duringLoad) addInStudy(myStudy) ;
238 // we create a new MED_i and add in study
239 MED_i * myMedI = new MED_i();
240 SALOME_MED::MED_ptr myMedIOR = myMedI->_this() ;
241 if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR,fileName) ;
242 // create ::MED object, read all and add in study !
243 myMedI->initWithFieldType(myStudy,MED_DRIVER,fileName) ;
245 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
248 MESSAGE("Erreur a la lecture du fichier");
249 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
253 endService("Med_Gen_i::readStructFileWithFieldType");
254 END_OF("Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)");
259 //=============================================================================
263 //=============================================================================
264 SALOME_MED::MESH_ptr Med_Gen_i::readMeshInFile(const char* fileName,
265 const char* studyName,
266 const char* meshName)
267 throw (SALOME::SALOME_Exception)
269 beginService("Med_Gen_i::readMeshInFile");
271 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
273 // if (!_duringLoad) addInStudy(myStudy) ;
275 // Creation du maillage
277 MESH * myMesh= new MESH() ;
278 myMesh->setName(meshName);
279 MED_MESH_RDONLY_DRIVER21 myMeshDriver(fileName,myMesh);
282 myMeshDriver.setMeshName(meshName);
285 catch (const std::exception & ex)
287 MESSAGE("Exception Interceptee : ");
289 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
294 MESSAGE("apres read");
295 myMeshDriver.close();
297 catch (const std::exception & ex)
299 MESSAGE("Exception Interceptee : ");
301 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
304 MESH_i * meshi = new MESH_i(myMesh);
305 //SALOME_MED::MESH_var mesh = SALOME_MED::MESH::_narrow(meshi->_this());
306 SALOME_MED::MESH_ptr mesh = meshi->_this();
309 // add the mesh object in study
310 // if (!_duringLoad) meshi->addInStudy(myStudy,mesh);
312 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
314 endService("Med_Gen_i::readMeshInFile");
317 //=============================================================================
321 //=============================================================================
322 SALOME_MED::FIELD_ptr Med_Gen_i::readFieldInFile(const char* fileName,
323 const char* studyName,
324 const char* fieldName,
327 throw (SALOME::SALOME_Exception)
329 beginService("Med_Gen_i::readFieldInFile");
331 string myStudyName(studyName);
333 if (myStudyName.size() == 0)
334 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
337 // Get StudyManager Reference, current study,
339 CORBA::Object_var obj = _NS->Resolve("/myStudyManager");
340 SALOMEDS::StudyManager_var myStudyManager =
341 SALOMEDS::StudyManager::_narrow(obj);
342 ASSERT(! CORBA::is_nil(myStudyManager));
343 SALOMEDS::Study_var myStudy =
344 myStudyManager->GetStudyByName(myStudyName.c_str());
345 if (CORBA::is_nil(myStudy))
346 THROW_SALOME_CORBA_EXCEPTION("Wrong Study Name", \
349 SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
350 SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
351 if (CORBA::is_nil(medfather))
353 myBuilder->NewCommand();
354 // mpv: component label must be created in spite of "Locked" study flag state
355 bool aLocked = myStudy->GetProperties()->IsLocked();
356 if (aLocked) myStudy->GetProperties()->SetLocked(false);
358 medfather = myBuilder->NewComponent("MED");
359 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(
360 myBuilder->FindOrCreateAttribute(medfather, "AttributeName"));
361 //NRI aName->SetValue("Med");
363 CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
364 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
365 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
366 if ( !Comp->_is_nil() ) {
367 aName->SetValue( Comp->componentusername() );
370 CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
371 myBuilder->DefineComponentInstance(medfather,myO);
373 if (aLocked) myStudy->GetProperties()->SetLocked(true);
374 myBuilder->CommitCommand();
378 MESSAGE("MED dejà dans l étude");
380 MESSAGE("Lecture du fichier ")
386 MED * mymed = new MED(MED_DRIVER,fileName) ;
389 deque<string> fieldsNames = mymed->getFieldNames() ;
390 int numberOfFields = fieldsNames.size();
392 for (i=0; i<numberOfFields; i++)
394 if (fieldsNames[i]== fieldName) break;
396 if (i == numberOfFields)
398 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field ",SALOME::BAD_PARAM);
402 deque<DT_IT_> myIteration = mymed->getFieldIteration (fieldName);
403 if (myIteration.size() != 1)
405 MESSAGE("WARNING : My iteration size is ")
406 SCRUTE(myIteration.size());
409 myField = mymed->getField(fieldName,iter,ordre);
411 catch (const std::exception & ex)
413 MESSAGE("Exception Interceptee : ");
415 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field in this file",SALOME::BAD_PARAM);
418 SUPPORT * fieldSupport;
421 fieldSupport=(SUPPORT *)myField->getSupport() ;
422 ASSERT(fieldSupport != NULL);
423 MESH * myMesh=(MESH *)fieldSupport->getMesh();
424 ASSERT(myMesh != NULL);
426 SCRUTE(myMesh->getName());
427 fieldSupport->update();
429 catch (const std::exception & ex)
431 MESSAGE("Exception Interceptee : ");
433 THROW_SALOME_CORBA_EXCEPTION("Unable to find associated support",SALOME::BAD_PARAM);
436 med_type_champ type = myField->getValueType() ;
439 case MED_EN::MED_INT32:
443 ((FIELD<int>*)myField)->read() ;
444 FIELDINT_i * myFieldIntI = new FIELDINT_i((FIELD<int>*)myField);
445 SALOME_MED::FIELD_ptr myFieldIOR = myFieldIntI->_this();
446 // if (!_duringLoad) myFieldIntI->addInStudy(myStudy,myFieldIOR) ;
447 endService("Med_Gen_i::readFieldInFile");
450 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
451 catch (const std::exception & ex)
453 MESSAGE("Exception Interceptee : ");
455 THROW_SALOME_CORBA_EXCEPTION("Unable to read int field",SALOME::BAD_PARAM);
459 case MED_EN::MED_REEL64:
463 ((FIELD<double>*)myField)->read() ;
464 FIELDDOUBLE_i * myFieldDoubleI = new FIELDDOUBLE_i((FIELD<double>*)myField);
465 SALOME_MED::FIELD_ptr myFieldIOR = myFieldDoubleI->_this() ;
466 // if (!_duringLoad) myFieldDoubleI->addInStudy(myStudy,myFieldIOR) ;
467 endService("Med_Gen_i::readFieldInFile");
470 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
471 catch (const std::exception & ex)
473 MESSAGE("Exception Interceptee : ");
475 THROW_SALOME_CORBA_EXCEPTION("Unable to read double field",SALOME::BAD_PARAM);
485 //=============================================================================
489 //=============================================================================
491 Med_Gen_i::~Med_Gen_i()
493 MESSAGE("Med_Gen_i::~Med_Gen_i");
496 //=============================================================================
498 * CORBA: Save Mesh objects (called when a study is saved)
500 //=============================================================================
501 SALOMEDS::TMPFile* Med_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
504 const char* LOC = "Med_Gen_i::Save";
507 SALOMEDS::TMPFile_var aStreamFile;
508 // Get a temporary directory to store a file
509 TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
510 // Create a list to store names of created files
511 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
512 TColStd_SequenceOfAsciiString aFileNames;
514 CORBA::String_var aSaveStudyName("");
515 if (isMultiFile) aSaveStudyName = CORBA::string_dup(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()).c_str());
517 SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
518 if (!CORBA::is_nil(aMedMeshFather)) {
519 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
521 for(; anIter->More(); anIter->Next()) {
522 SALOMEDS::SObject_var aSO = anIter->Value();
523 SALOMEDS::GenericAttribute_var anAttr;
524 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
525 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
526 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
527 if (! CORBA::is_nil(myMesh)) {
528 TCollection_AsciiString aName(aSaveStudyName);
529 aName += "_MEDMESH_";
530 aName += myMesh->getName();
532 MESSAGE("Save mesh with name "<<aName.ToCString());
533 long driverId = myMesh->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myMesh->getName());
534 myMesh->write(driverId,"");
535 aFileNames.Append(aName);
541 SALOMEDS::SObject_var aMedFieldFather = theComponent->GetStudy()->FindObject("MEDFIELD");
542 if (!CORBA::is_nil(aMedFieldFather)) {
543 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedFieldFather);
545 for(; anIter->More(); anIter->Next()) {
546 SALOMEDS::SObject_var aSO = anIter->Value();
547 SALOMEDS::GenericAttribute_var anAttr;
548 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
549 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
550 SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
551 if (! CORBA::is_nil(myField)) {
553 a<< myField->getOrderNumber();
554 b<< myField->getIterationNumber();
556 TCollection_AsciiString aName(aSaveStudyName);
557 aName += "_MEDFIELD_";
558 aName += myField->getName();
560 aName += (char*)(a.str().c_str());
562 aName += (char*)(b.str().c_str());
564 MESSAGE("Save mesh with name "<<aName.ToCString());
565 long driverId = myField->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myField->getName());
566 myField->write(driverId,"");
567 aFileNames.Append(aName);
574 aSeq->length(aFileNames.Length());
575 for(i = aFileNames.Length(); i > 0; i--) aSeq[i-1] = CORBA::string_dup(aFileNames.Value(i).ToCString());
576 // Conver a file to the byte stream
577 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
578 // Remove the created file and tmp directory
579 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
580 // Return the created byte stream
581 return aStreamFile._retn();
586 SALOMEDS::TMPFile* Med_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
589 const char* LOC = "Med_Gen_i::SaveASCII";
592 SALOMEDS::TMPFile_var aStreamFile;
593 // Get a temporary directory to store a file
594 TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
595 // Create a list to store names of created files
596 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
597 TColStd_SequenceOfAsciiString aFileNames;
599 CORBA::String_var aSaveStudyName("");
600 if (isMultiFile) aSaveStudyName = CORBA::string_dup(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()).c_str());
602 SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
603 if (!CORBA::is_nil(aMedMeshFather)) {
604 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
606 for(; anIter->More(); anIter->Next()) {
607 SALOMEDS::SObject_var aSO = anIter->Value();
608 SALOMEDS::GenericAttribute_var anAttr;
609 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
610 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
611 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
612 if (! CORBA::is_nil(myMesh)) {
613 TCollection_AsciiString aName(aSaveStudyName);
614 aName += "_MEDMESH_";
615 aName += myMesh->getName();
617 MESSAGE("Save mesh with name "<<aName.ToCString());
618 long driverId = myMesh->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myMesh->getName());
619 myMesh->write(driverId,"");
620 HDFascii::ConvertFromHDFToASCII((aTmpDir+aName).ToCString(), true);
621 aFileNames.Append(aName);
627 SALOMEDS::SObject_var aMedFieldFather = theComponent->GetStudy()->FindObject("MEDFIELD");
628 if (!CORBA::is_nil(aMedFieldFather)) {
629 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedFieldFather);
631 for(; anIter->More(); anIter->Next()) {
632 SALOMEDS::SObject_var aSO = anIter->Value();
633 SALOMEDS::GenericAttribute_var anAttr;
634 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
635 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
636 SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
637 if (! CORBA::is_nil(myField)) {
639 a<< myField->getOrderNumber();
640 b<< myField->getIterationNumber();
642 TCollection_AsciiString aName(aSaveStudyName);
643 aName += "_MEDFIELD_";
644 aName += myField->getName();
646 aName += (char*)(a.str().c_str());
648 aName += (char*)(b.str().c_str());
650 MESSAGE("Save mesh with name "<<aName.ToCString());
651 long driverId = myField->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myField->getName());
652 myField->write(driverId,"");
653 HDFascii::ConvertFromHDFToASCII((aTmpDir+aName).ToCString(), true);
654 aFileNames.Append(aName);
661 aSeq->length(aFileNames.Length());
662 for(i = aFileNames.Length(); i > 0; i--) aSeq[i-1] = CORBA::string_dup(aFileNames.Value(i).ToCString());
663 // Conver a file to the byte stream
664 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
665 // Remove the created file and tmp directory
666 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
667 // Return the created byte stream
668 return aStreamFile._retn();
671 //=============================================================================
673 * CORBA: Load Mesh objects (called when an existing study is opened)
675 //=============================================================================
677 CORBA::Boolean Med_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
678 const SALOMEDS::TMPFile& theStream,
681 const char* LOC = "Med_Gen_i::Load";
684 // Get a temporary directory for a file
685 TCollection_AsciiString aTmpDir =
686 (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
687 _saveFileName = CORBA::string_dup(aTmpDir.ToCString());
688 SALOMEDS::ListOfFileNames_var aSeq =
689 SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(), isMultiFile);
693 CORBA::Boolean Med_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
694 const SALOMEDS::TMPFile& theStream,
697 return Load(theComponent, theStream, theURL, isMultiFile);
700 //=============================================================================
704 //=============================================================================
706 void Med_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
708 MESSAGE("Med_Gen_i::Close");
709 SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
710 if (!CORBA::is_nil(aMedMeshFather)) {
711 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
712 for(; anIter->More(); anIter->Next()) {
713 SALOMEDS::SObject_var aSO = anIter->Value();
714 SALOMEDS::GenericAttribute_var anAttr;
715 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
716 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
717 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
718 // here must call method destroy of myMesh, but it not implented yet
724 //=============================================================================
728 //=============================================================================
730 char* Med_Gen_i::ComponentDataType()
732 MESSAGE("Med_Gen_i::ComponentDataType");
733 return CORBA::string_dup("MED") ; /* What is this type ? */
736 //=============================================================================
738 * CORBA: give a persistent reference of a transient object (for study save)
740 //=============================================================================
742 char* Med_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
743 const char* IORString,
744 CORBA::Boolean isMultiFile,
745 CORBA::Boolean isASCII) {
746 const char * LOC = "Med_Gen_i::IORToLocalPersistentID" ;
751 if (string(IORString).size()==0) return CORBA::string_dup("_MED");
752 // Well, we know where put object (_saveFilename) and we know object (IORString)
754 CORBA::Object_var myIOR = _orb->string_to_object(IORString);
757 SALOME_MED::MED_var myMed = SALOME_MED::MED::_narrow(myIOR);
758 if (! CORBA::is_nil(myMed))
760 // nothing to save : Support will be saved inside the mesh
761 string str_MedName="_MED Objet Med + /OBJ_MED/";
762 return CORBA::string_dup(str_MedName.c_str()) ;
766 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
767 if (! CORBA::is_nil(myMesh))
769 CORBA::String_var aName((string("_MEDMESH_")+ myMesh->getName() + ".med").c_str());
770 return aName._retn() ;
774 SALOME_MED::SUPPORT_var mySupport = SALOME_MED::SUPPORT::_narrow(myIOR);
775 if (! CORBA::is_nil(mySupport))
777 // nothing to save : Support will be saved inside the mesh
778 string str_SupportName;
781 str_SupportName=string("/FAS/")+string(mySupport->getName());
782 str_SupportName+=string("/ENS_MAA/")+string(mySupport->getMesh()->getName());
783 SCRUTE(str_SupportName);
787 MESSAGE("Unable to save the support");
788 THROW_SALOME_CORBA_EXCEPTION("Unable to save Field in Med"\
789 ,SALOME::INTERNAL_ERROR);
791 return CORBA::string_dup(("_MED"+str_SupportName).c_str());
794 SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
795 if (! CORBA::is_nil(myField))
797 string str_FieldName;
799 a<< myField->getOrderNumber();
800 b<< myField->getIterationNumber();
801 CORBA::String_var aName((string("_MEDFIELD_")+ myField->getName() +
802 string("_ORDRE_")+a.str()+
803 string("_ITER_")+b.str() +
805 return aName._retn();
808 //THROW_SALOME_CORBA_EXCEPTION("Unable to save IOR",SALOME::BAD_PARAM);
809 return CORBA::string_dup("_MED");
812 //=============================================================================
814 * CORBA: give a transient reference (when loading an object, opening study)
816 //=============================================================================
818 char* Med_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
819 const char* aLocalPersistentID,
820 CORBA::Boolean isMultiFile,
821 CORBA::Boolean isASCII)
822 throw(SALOME::SALOME_Exception)
824 const char * LOC = "Med_Gen_i::LocalPersistentIDToIOR" ;
826 TCollection_AsciiString aTmpDir((char*)(_saveFileName.c_str()));
828 TCollection_AsciiString aSaveStudyName("");
829 if (isMultiFile) aSaveStudyName = (char*)SALOMEDS_Tool::GetNameFromPath(theSObject->GetStudy()->URL()).c_str();
831 if (strcmp(aLocalPersistentID, "Objet Med + /OBJ_MED/") == 0) return CORBA::string_dup(""); // MED
833 if (strncmp(aLocalPersistentID, "_MEDMESH_",9) == 0) {// MESH
834 MESH * myMesh= new MESH() ;
835 int aMeshNameLen = strlen(aLocalPersistentID) - 12;
836 char* aMeshName = new char[aMeshNameLen];
837 strncpy(aMeshName, &(aLocalPersistentID[9]), aMeshNameLen-1);
838 aMeshName[aMeshNameLen-1] = 0;
839 myMesh->setName(aMeshName);
843 char* aResultPath = HDFascii::ConvertFromASCIIToHDF((aTmpDir + aSaveStudyName + (char*)aLocalPersistentID).ToCString());
844 aFileName = new char[strlen(aResultPath) + 19];
845 sprintf(aFileName, "%shdf_from_ascii.hdf", aResultPath);
847 } else aFileName = CORBA::string_dup((aTmpDir + aSaveStudyName + (char*)aLocalPersistentID).ToCString());
848 MED_MESH_RDONLY_DRIVER21 myMeshDriver(aFileName,myMesh);
851 myMeshDriver.setMeshName(aMeshName);
854 catch (const std::exception & ex)
856 MESSAGE("Exception Interceptee : ");
858 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
863 MESSAGE("apres read");
864 myMeshDriver.close();
866 catch (const std::exception & ex)
868 MESSAGE("Exception Interceptee : ");
870 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
872 MESH_i * meshi = new MESH_i(myMesh);
873 //SALOME_MED::MESH_var mesh = SALOME_MED::MESH::_narrow(meshi->_this());
874 SALOME_MED::MESH_ptr mesh = meshi->_this();
875 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
877 aSeq[0]=CORBA::string_dup(aLocalPersistentID);
878 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
880 SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
881 aFilesToRemove->length(1);
882 aFilesToRemove[0] = CORBA::string_dup(&(aFileName[strlen(SALOMEDS_Tool::GetDirFromPath(aFileName).c_str())]));
883 SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aFileName).c_str(), aFilesToRemove, true);
886 return(CORBA::string_dup(_orb->object_to_string(mesh)));
887 } else if (strncmp(aLocalPersistentID, "_MEDFIELD_",14) == 0) { // FIELD
888 return(CORBA::string_dup("")); // not implemented yet
891 return CORBA::string_dup("");
894 //=============================================================================
896 * returns true, if can publish object
898 //=============================================================================
899 bool Med_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
900 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(theIOR);
901 return !(aMesh->_is_nil());
904 //=============================================================================
906 * publish the given object
908 //=============================================================================
909 SALOMEDS::SObject_ptr Med_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
910 SALOMEDS::SObject_ptr theSObject,
911 CORBA::Object_ptr theObject,
912 const char* theName) throw (SALOME::SALOME_Exception)
914 SALOMEDS::SObject_var aResultSO;
916 if(CORBA::is_nil(theObject)) return aResultSO;
917 if(theStudy->_is_nil()) return aResultSO;
919 SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
920 SALOMEDS::GenericAttribute_var anAttr;
921 SALOMEDS::AttributeName_var aName;
922 SALOMEDS::AttributeIOR_var anIOR;
923 SALOMEDS::SComponent_var aFather = theStudy->FindComponent("MED");
925 if (aFather->_is_nil()) {
926 aFather = aBuilder->NewComponent("MED");
927 if (aFather->_is_nil()) return aResultSO;
928 anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributeName");
929 aName = SALOMEDS::AttributeName::_narrow(anAttr);
930 //NRI aName->SetValue("MED");
932 CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
933 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
934 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
935 if ( !Comp->_is_nil() ) {
936 aName->SetValue( Comp->componentusername() );
938 aBuilder->DefineComponentInstance(aFather, MED_Gen::_this());
941 if (CORBA::is_nil(theSObject)) {
942 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(theObject);
943 aMesh->addInStudy(theStudy, aMesh);
944 SALOMEDS::SObject_var aResultSO = theStudy->FindObjectIOR(_orb->object_to_string(theObject));
946 if (!theSObject->ReferencedObject(aResultSO))
947 THROW_SALOME_CORBA_EXCEPTION("Publish in study MED object error",SALOME::BAD_PARAM);
949 // aBuilder->Addreference(theObject, aResultSO);
950 return aResultSO._retn();
953 //=============================================================================
955 * returns true, if can copy the object
957 //=============================================================================
958 CORBA::Boolean Med_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
959 // Try to retrieve known by MED component mesh by given IOR
960 SALOMEDS::GenericAttribute_var anAttr;
961 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
963 CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
964 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(anObj);
965 // If the object is null one it can't be copied: return false
966 if (aMesh->_is_nil()) return false;
973 //=============================================================================
975 * create copy of the object and put it to the stream
977 //=============================================================================
978 SALOMEDS::TMPFile* Med_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
979 // Declare a sequence of the byte to store the copied object
980 SALOMEDS::TMPFile_var aStreamFile;
982 // Try to get GEOM_Shape object by given SObject
983 SALOMEDS::GenericAttribute_var anAttr;
984 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return new SALOMEDS::TMPFile(0);
985 CORBA::String_var anIOR = CORBA::string_dup(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
986 CORBA::Object_var anObj = _orb->string_to_object(anIOR);
987 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(anObj);
988 if (aMesh->_is_nil()) return new SALOMEDS::TMPFile(0);
990 // Get a temporary directory to store a temporary file
991 CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir().c_str();
992 // Create a list to store names of created files
993 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
995 aSeq[0] = CORBA::string_dup(aMesh->getName());
996 char* aFullName = new char[strlen(aTmpDir)+strlen(aSeq[0])+1];
997 strcpy(aFullName, aTmpDir);
998 strcpy(aFullName+strlen(aTmpDir), aSeq[0]);
999 long driverId = aMesh->addDriver(SALOME_MED::MED_DRIVER,aFullName , aMesh->getName());
1000 aMesh->write(driverId,"");
1002 // aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), false);
1003 char* aFullName1 = new char[strlen(aTmpDir)+1];
1004 strcpy(aFullName1, aTmpDir);
1005 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aFullName1, aSeq.in(), false);
1006 // SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
1007 SALOMEDS_Tool::RemoveTemporaryFiles(aFullName1, aSeq.in(), true);
1009 // Assign an ID = 1 the the type SALOME_MED::MESH
1012 return aStreamFile._retn();
1015 //=============================================================================
1017 * returns true, if can copy the object
1019 //=============================================================================
1020 CORBA::Boolean Med_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
1021 // The MED component can paste only objects copied by MED component
1022 // and with the object type = 1 (mesh)
1023 if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
1027 //=============================================================================
1029 * returns true, if can copy the object
1031 //=============================================================================
1032 SALOMEDS::SObject_ptr Med_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
1033 CORBA::Long theObjectID,
1034 SALOMEDS::SObject_ptr theObject) {
1035 SALOMEDS::SObject_var aResultSO = SALOMEDS::SObject::_duplicate(theObject);
1036 if (theStream.length() == 0) return aResultSO._retn();
1038 SALOMEDS::Study_var aStudy = theObject->GetStudy();
1040 CORBA::String_var aTmpDir = CORBA::string_dup(SALOMEDS_Tool::GetTmpDir().c_str());
1041 char* aFullName2 = new char[strlen(aTmpDir)+1];
1042 strcpy(aFullName2,aTmpDir);
1043 // SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
1044 SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aFullName2, false);
1045 CORBA::String_var aMeshName = CORBA::string_dup(aSeq[0]);
1046 char* aFullName = new char[strlen(aTmpDir)+strlen(aMeshName)+1];
1047 strcpy(aFullName, aTmpDir);
1048 strcpy(aFullName+strlen(aTmpDir), aMeshName);
1050 MESH * myMesh= new MESH() ;
1051 // myMesh->setName(aMeshName.c_str());
1052 char* aFullMeshName = new char[strlen(aMeshName)+1];
1053 strcpy(aFullMeshName,aMeshName);
1054 myMesh->setName(aFullMeshName);
1055 MED_MESH_RDONLY_DRIVER21 myMeshDriver(aFullName, myMesh);
1057 myMeshDriver.setMeshName(aFullMeshName);
1058 myMeshDriver.open();
1059 } catch (const std::exception & ex) {
1060 MESSAGE("Exception Interceptee : ");
1062 return aResultSO._retn();
1065 myMeshDriver.read();
1067 myMeshDriver.close();
1068 } catch (const std::exception & ex) {
1069 MESSAGE("Exception Interceptee : ");
1071 return aResultSO._retn();
1073 // set new mesh name, becouse now there are no possibility to operate meshes with the same names
1074 // srand((unsigned int)time(NULL));
1075 int aRND = rand(); //Get a random number to present a name of a copied mesh
1076 char aCopiedMeshName[20];
1077 sprintf(aCopiedMeshName,"MESH_COPY_%d",aRND);
1078 myMesh->setName(aCopiedMeshName);
1079 MESH_i * meshi = new MESH_i(myMesh);
1080 SALOME_MED::MESH_ptr mesh = meshi->_this();
1081 // add the mesh object in study
1082 meshi->addInStudy(aStudy,mesh);
1083 // get the IOR attribute of just added mesh
1084 CORBA::String_var anIORString = _orb->object_to_string(mesh);
1085 aResultSO = aStudy->FindObjectIOR(anIORString);
1087 char * aFullName1 = new char[strlen(aTmpDir)+1];
1088 strcpy(aFullName1,aTmpDir);
1089 // SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
1090 SALOMEDS_Tool::RemoveTemporaryFiles(aFullName1, aSeq.in(), true);
1091 return aResultSO._retn();
1099 //=============================================================================
1101 * C factory, accessible with dlsym, after dlopen
1103 //=============================================================================
1107 PortableServer::ObjectId * MEDEngine_factory(
1109 PortableServer::POA_ptr poa,
1110 PortableServer::ObjectId * contId,
1111 const char *instanceName,
1112 const char *interfaceName)
1114 MESSAGE("PortableServer::ObjectId * MedEngine_factory()");
1115 SCRUTE(interfaceName);
1116 Med_Gen_i * myMed_Gen
1117 = new Med_Gen_i(orb, poa, contId, instanceName, interfaceName);
1118 return myMed_Gen->getId() ;