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();
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)
190 beginService("Med_Gen_i::readStructFile");
193 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
194 // if (!_duringLoad) addInStudy(myStudy) ;
196 SALOME_MED::MED_ptr myMedIOR ;
199 // we create a new MED_i and add in study
200 MED_i * myMedI = new MED_i();
201 myMedIOR = myMedI->_this() ;
202 // if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR) ;
203 // if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR,fileName) ;
204 // create ::MED object, read all and add in study !
205 myMedI->init(myStudy,MED_DRIVER,fileName) ;
207 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
210 MESSAGE("Erreur a la lecture du fichier");
211 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
215 endService("Med_Gen_i::readStructFile");
219 //=============================================================================
221 * Prepare un Maillage sans le lire en memoire avec stokage dans l'etude
222 * des champs avec leur type
224 //=============================================================================
225 void Med_Gen_i::readStructFileWithFieldType (const char* fileName,
226 const char* studyName)
227 throw (SALOME::SALOME_Exception)
229 beginService("Med_Gen_i::readStructFileWithFieldType");
231 BEGIN_OF("Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)");
234 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
236 if (!_duringLoad) addInStudy(myStudy) ;
240 // we create a new MED_i and add in study
241 MED_i * myMedI = new MED_i();
242 SALOME_MED::MED_ptr myMedIOR = myMedI->_this() ;
243 if (!_duringLoad) myMedI->addInStudy(myStudy,myMedIOR,fileName) ;
244 // create ::MED object, read all and add in study !
245 myMedI->initWithFieldType(myStudy,MED_DRIVER,fileName) ;
247 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
250 MESSAGE("Erreur a la lecture du fichier");
251 THROW_SALOME_CORBA_EXCEPTION("Unable to open File "\
255 endService("Med_Gen_i::readStructFileWithFieldType");
256 END_OF("Med_Gen_i::readStructFileWithFieldType (const char* fileName,const char* studyName)");
261 //=============================================================================
265 //=============================================================================
266 SALOME_MED::MESH_ptr Med_Gen_i::readMeshInFile(const char* fileName,
267 const char* studyName,
268 const char* meshName)
269 throw (SALOME::SALOME_Exception)
271 beginService("Med_Gen_i::readMeshInFile");
273 SALOMEDS::Study_var myStudy = studyName2Study(studyName) ;
275 // if (!_duringLoad) addInStudy(myStudy) ;
277 // Creation du maillage
279 MESH * myMesh= new MESH() ;
280 myMesh->setName(meshName);
281 MED_MESH_RDONLY_DRIVER21 myMeshDriver(fileName,myMesh);
284 myMeshDriver.setMeshName(meshName);
287 catch (const std::exception & ex)
289 MESSAGE("Exception Interceptee : ");
291 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
296 MESSAGE("apres read");
297 myMeshDriver.close();
299 catch (const std::exception & ex)
301 MESSAGE("Exception Interceptee : ");
303 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
306 MESH_i * meshi = new MESH_i(myMesh);
307 //SALOME_MED::MESH_var mesh = SALOME_MED::MESH::_narrow(meshi->_this());
308 SALOME_MED::MESH_ptr mesh = meshi->_this();
311 // add the mesh object in study
312 // if (!_duringLoad) meshi->addInStudy(myStudy,mesh);
314 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
316 endService("Med_Gen_i::readMeshInFile");
319 //=============================================================================
323 //=============================================================================
324 SALOME_MED::FIELD_ptr Med_Gen_i::readFieldInFile(const char* fileName,
325 const char* studyName,
326 const char* fieldName,
329 throw (SALOME::SALOME_Exception)
331 beginService("Med_Gen_i::readFieldInFile");
333 string myStudyName(studyName);
335 if (myStudyName.size() == 0)
336 THROW_SALOME_CORBA_EXCEPTION("No Study Name given", \
339 // Get StudyManager Reference, current study,
341 CORBA::Object_var obj = _NS->Resolve("/myStudyManager");
342 SALOMEDS::StudyManager_var myStudyManager =
343 SALOMEDS::StudyManager::_narrow(obj);
344 ASSERT(! CORBA::is_nil(myStudyManager));
345 SALOMEDS::Study_var myStudy =
346 myStudyManager->GetStudyByName(myStudyName.c_str());
347 if (CORBA::is_nil(myStudy))
348 THROW_SALOME_CORBA_EXCEPTION("Wrong Study Name", \
351 SALOMEDS::StudyBuilder_var myBuilder = myStudy->NewBuilder();
352 SALOMEDS::SComponent_var medfather = myStudy->FindComponent("MED");
353 if (CORBA::is_nil(medfather))
355 myBuilder->NewCommand();
356 // mpv: component label must be created in spite of "Locked" study flag state
357 bool aLocked = myStudy->GetProperties()->IsLocked();
358 if (aLocked) myStudy->GetProperties()->SetLocked(false);
360 medfather = myBuilder->NewComponent("MED");
361 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(
362 myBuilder->FindOrCreateAttribute(medfather, "AttributeName"));
363 //NRI aName->SetValue("Med");
365 CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
366 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
367 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
368 if ( !Comp->_is_nil() ) {
369 aName->SetValue( Comp->componentusername() );
372 CORBA::Object_var myO = _poa->id_to_reference(*_id); // this ior...
373 myBuilder->DefineComponentInstance(medfather,myO);
375 if (aLocked) myStudy->GetProperties()->SetLocked(true);
376 myBuilder->CommitCommand();
380 MESSAGE("MED dejà dans l étude");
382 MESSAGE("Lecture du fichier ")
388 MED * mymed = new MED(MED_DRIVER,fileName) ;
391 deque<string> fieldsNames = mymed->getFieldNames() ;
392 int numberOfFields = fieldsNames.size();
394 for (i=0; i<numberOfFields; i++)
396 if (fieldsNames[i]== fieldName) break;
398 if (i == numberOfFields)
400 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field ",SALOME::BAD_PARAM);
404 deque<DT_IT_> myIteration = mymed->getFieldIteration (fieldName);
405 if (myIteration.size() != 1)
407 MESSAGE("WARNING : My iteration size is ")
408 SCRUTE(myIteration.size());
411 myField = mymed->getField(fieldName,iter,ordre);
413 catch (const std::exception & ex)
415 MESSAGE("Exception Interceptee : ");
417 THROW_SALOME_CORBA_EXCEPTION("Unable to find this field in this file",SALOME::BAD_PARAM);
420 SUPPORT * fieldSupport;
423 fieldSupport=(SUPPORT *)myField->getSupport() ;
424 ASSERT(fieldSupport != NULL);
425 MESH * myMesh=(MESH *)fieldSupport->getMesh();
426 ASSERT(myMesh != NULL);
428 SCRUTE(myMesh->getName());
429 fieldSupport->update();
431 catch (const std::exception & ex)
433 MESSAGE("Exception Interceptee : ");
435 THROW_SALOME_CORBA_EXCEPTION("Unable to find associated support",SALOME::BAD_PARAM);
438 med_type_champ type = myField->getValueType() ;
441 case MED_EN::MED_INT32:
445 ((FIELD<int>*)myField)->read() ;
446 FIELDINT_i * myFieldIntI = new FIELDINT_i((FIELD<int>*)myField);
447 SALOME_MED::FIELD_ptr myFieldIOR = myFieldIntI->_this();
448 // if (!_duringLoad) myFieldIntI->addInStudy(myStudy,myFieldIOR) ;
449 endService("Med_Gen_i::readFieldInFile");
452 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
453 catch (const std::exception & ex)
455 MESSAGE("Exception Interceptee : ");
457 THROW_SALOME_CORBA_EXCEPTION("Unable to read int field",SALOME::BAD_PARAM);
461 case MED_EN::MED_REEL64:
465 ((FIELD<double>*)myField)->read() ;
466 FIELDDOUBLE_i * myFieldDoubleI = new FIELDDOUBLE_i((FIELD<double>*)myField);
467 SALOME_MED::FIELD_ptr myFieldIOR = myFieldDoubleI->_this() ;
468 // if (!_duringLoad) myFieldDoubleI->addInStudy(myStudy,myFieldIOR) ;
469 endService("Med_Gen_i::readFieldInFile");
472 catch (const SALOMEDS::StudyBuilder::LockProtection & lp) {}
473 catch (const std::exception & ex)
475 MESSAGE("Exception Interceptee : ");
477 THROW_SALOME_CORBA_EXCEPTION("Unable to read double field",SALOME::BAD_PARAM);
487 //=============================================================================
491 //=============================================================================
493 Med_Gen_i::~Med_Gen_i()
495 MESSAGE("Med_Gen_i::~Med_Gen_i");
498 //=============================================================================
500 * CORBA: Save Mesh objects (called when a study is saved)
502 //=============================================================================
503 SALOMEDS::TMPFile* Med_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
506 const char* LOC = "Med_Gen_i::Save";
509 SALOMEDS::TMPFile_var aStreamFile;
510 // Get a temporary directory to store a file
511 TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
512 // Create a list to store names of created files
513 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
514 TColStd_SequenceOfAsciiString aFileNames;
516 CORBA::String_var aSaveStudyName("");
517 if (isMultiFile) aSaveStudyName = CORBA::string_dup(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()).c_str());
519 SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
520 if (!CORBA::is_nil(aMedMeshFather)) {
521 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
523 for(; anIter->More(); anIter->Next()) {
524 SALOMEDS::SObject_var aSO = anIter->Value();
525 SALOMEDS::GenericAttribute_var anAttr;
526 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
527 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
528 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
529 if (! CORBA::is_nil(myMesh)) {
530 TCollection_AsciiString aName(aSaveStudyName);
531 aName += "_MEDMESH_";
532 aName += myMesh->getName();
534 MESSAGE("Save mesh with name "<<aName.ToCString());
535 long driverId = myMesh->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myMesh->getName());
536 myMesh->write(driverId,"");
537 aFileNames.Append(aName);
543 SALOMEDS::SObject_var aMedFieldFather = theComponent->GetStudy()->FindObject("MEDFIELD");
544 if (!CORBA::is_nil(aMedFieldFather)) {
545 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedFieldFather);
547 for(; anIter->More(); anIter->Next()) {
548 SALOMEDS::SObject_var aSO = anIter->Value();
549 SALOMEDS::GenericAttribute_var anAttr;
550 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
551 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
552 SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
553 if (! CORBA::is_nil(myField)) {
555 a<< myField->getOrderNumber();
556 b<< myField->getIterationNumber();
558 TCollection_AsciiString aName(aSaveStudyName);
559 aName += "_MEDFIELD_";
560 aName += myField->getName();
562 aName += (char*)(a.str().c_str());
564 aName += (char*)(b.str().c_str());
566 MESSAGE("Save mesh with name "<<aName.ToCString());
567 long driverId = myField->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myField->getName());
568 myField->write(driverId,"");
569 aFileNames.Append(aName);
576 aSeq->length(aFileNames.Length());
577 for(i = aFileNames.Length(); i > 0; i--) aSeq[i-1] = CORBA::string_dup(aFileNames.Value(i).ToCString());
578 // Conver a file to the byte stream
579 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
580 // Remove the created file and tmp directory
581 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
582 // Return the created byte stream
583 return aStreamFile._retn();
588 SALOMEDS::TMPFile* Med_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
591 const char* LOC = "Med_Gen_i::SaveASCII";
594 SALOMEDS::TMPFile_var aStreamFile;
595 // Get a temporary directory to store a file
596 TCollection_AsciiString aTmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
597 // Create a list to store names of created files
598 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
599 TColStd_SequenceOfAsciiString aFileNames;
601 CORBA::String_var aSaveStudyName("");
602 if (isMultiFile) aSaveStudyName = CORBA::string_dup(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()).c_str());
604 SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
605 if (!CORBA::is_nil(aMedMeshFather)) {
606 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
608 for(; anIter->More(); anIter->Next()) {
609 SALOMEDS::SObject_var aSO = anIter->Value();
610 SALOMEDS::GenericAttribute_var anAttr;
611 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
612 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
613 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
614 if (! CORBA::is_nil(myMesh)) {
615 TCollection_AsciiString aName(aSaveStudyName);
616 aName += "_MEDMESH_";
617 aName += myMesh->getName();
619 MESSAGE("Save mesh with name "<<aName.ToCString());
620 long driverId = myMesh->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myMesh->getName());
621 myMesh->write(driverId,"");
622 HDFascii::ConvertFromHDFToASCII((aTmpDir+aName).ToCString(), true);
623 aFileNames.Append(aName);
629 SALOMEDS::SObject_var aMedFieldFather = theComponent->GetStudy()->FindObject("MEDFIELD");
630 if (!CORBA::is_nil(aMedFieldFather)) {
631 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedFieldFather);
633 for(; anIter->More(); anIter->Next()) {
634 SALOMEDS::SObject_var aSO = anIter->Value();
635 SALOMEDS::GenericAttribute_var anAttr;
636 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
637 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
638 SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
639 if (! CORBA::is_nil(myField)) {
641 a<< myField->getOrderNumber();
642 b<< myField->getIterationNumber();
644 TCollection_AsciiString aName(aSaveStudyName);
645 aName += "_MEDFIELD_";
646 aName += myField->getName();
648 aName += (char*)(a.str().c_str());
650 aName += (char*)(b.str().c_str());
652 MESSAGE("Save mesh with name "<<aName.ToCString());
653 long driverId = myField->addDriver(SALOME_MED::MED_DRIVER,(aTmpDir+aName).ToCString(),myField->getName());
654 myField->write(driverId,"");
655 HDFascii::ConvertFromHDFToASCII((aTmpDir+aName).ToCString(), true);
656 aFileNames.Append(aName);
663 aSeq->length(aFileNames.Length());
664 for(i = aFileNames.Length(); i > 0; i--) aSeq[i-1] = CORBA::string_dup(aFileNames.Value(i).ToCString());
665 // Conver a file to the byte stream
666 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.ToCString(), aSeq.in(), isMultiFile);
667 // Remove the created file and tmp directory
668 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
669 // Return the created byte stream
670 return aStreamFile._retn();
673 //=============================================================================
675 * CORBA: Load Mesh objects (called when an existing study is opened)
677 //=============================================================================
679 CORBA::Boolean Med_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
680 const SALOMEDS::TMPFile& theStream,
683 const char* LOC = "Med_Gen_i::Load";
686 // Get a temporary directory for a file
687 TCollection_AsciiString aTmpDir =
688 (isMultiFile)?TCollection_AsciiString((char*)theURL):(char*)SALOMEDS_Tool::GetTmpDir().c_str();
689 _saveFileName = CORBA::string_dup(aTmpDir.ToCString());
690 SALOMEDS::ListOfFileNames_var aSeq =
691 SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir.ToCString(), isMultiFile);
695 CORBA::Boolean Med_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
696 const SALOMEDS::TMPFile& theStream,
699 return Load(theComponent, theStream, theURL, isMultiFile);
702 //=============================================================================
706 //=============================================================================
708 void Med_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
710 MESSAGE("Med_Gen_i::Close");
711 SALOMEDS::SObject_var aMedMeshFather = theComponent->GetStudy()->FindObject("MEDMESH");
712 if (!CORBA::is_nil(aMedMeshFather)) {
713 SALOMEDS::ChildIterator_var anIter = theComponent->GetStudy()->NewChildIterator(aMedMeshFather);
714 for(; anIter->More(); anIter->Next()) {
715 SALOMEDS::SObject_var aSO = anIter->Value();
716 SALOMEDS::GenericAttribute_var anAttr;
717 if (aSO->FindAttribute(anAttr,"AttributeIOR")) {
718 CORBA::Object_var myIOR = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
719 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
720 // here must call method destroy of myMesh, but it not implented yet
726 //=============================================================================
730 //=============================================================================
732 char* Med_Gen_i::ComponentDataType()
734 MESSAGE("Med_Gen_i::ComponentDataType");
735 return CORBA::string_dup("MED") ; /* What is this type ? */
738 //=============================================================================
740 * CORBA: give a persistent reference of a transient object (for study save)
742 //=============================================================================
744 char* Med_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
745 const char* IORString,
746 CORBA::Boolean isMultiFile,
747 CORBA::Boolean isASCII) {
748 const char * LOC = "Med_Gen_i::IORToLocalPersistentID" ;
753 if (string(IORString).size()==0) return CORBA::string_dup("_MED");
754 // Well, we know where put object (_saveFilename) and we know object (IORString)
756 CORBA::Object_var myIOR = _orb->string_to_object(IORString);
759 SALOME_MED::MED_var myMed = SALOME_MED::MED::_narrow(myIOR);
760 if (! CORBA::is_nil(myMed))
762 // nothing to save : Support will be saved inside the mesh
763 string str_MedName="_MED Objet Med + /OBJ_MED/";
764 return CORBA::string_dup(str_MedName.c_str()) ;
768 SALOME_MED::MESH_var myMesh = SALOME_MED::MESH::_narrow(myIOR);
769 if (! CORBA::is_nil(myMesh))
771 CORBA::String_var aName((string("_MEDMESH_")+ myMesh->getName() + ".med").c_str());
772 return aName._retn() ;
776 SALOME_MED::SUPPORT_var mySupport = SALOME_MED::SUPPORT::_narrow(myIOR);
777 if (! CORBA::is_nil(mySupport))
779 // nothing to save : Support will be saved inside the mesh
780 string str_SupportName;
783 str_SupportName=string("/FAS/")+string(mySupport->getName());
784 str_SupportName+=string("/ENS_MAA/")+string(mySupport->getMesh()->getName());
785 SCRUTE(str_SupportName);
789 MESSAGE("Unable to save the support");
790 THROW_SALOME_CORBA_EXCEPTION("Unable to save Field in Med"\
791 ,SALOME::INTERNAL_ERROR);
793 return CORBA::string_dup(("_MED"+str_SupportName).c_str());
796 SALOME_MED::FIELD_var myField = SALOME_MED::FIELD::_narrow(myIOR);
797 if (! CORBA::is_nil(myField))
799 string str_FieldName;
801 a<< myField->getOrderNumber();
802 b<< myField->getIterationNumber();
803 CORBA::String_var aName((string("_MEDFIELD_")+ myField->getName() +
804 string("_ORDRE_")+a.str()+
805 string("_ITER_")+b.str() +
807 return aName._retn();
810 //THROW_SALOME_CORBA_EXCEPTION("Unable to save IOR",SALOME::BAD_PARAM);
811 return CORBA::string_dup("_MED");
814 //=============================================================================
816 * CORBA: give a transient reference (when loading an object, opening study)
818 //=============================================================================
820 char* Med_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
821 const char* aLocalPersistentID,
822 CORBA::Boolean isMultiFile,
823 CORBA::Boolean isASCII)
824 throw(SALOME::SALOME_Exception)
826 const char * LOC = "Med_Gen_i::LocalPersistentIDToIOR" ;
828 TCollection_AsciiString aTmpDir((char*)(_saveFileName.c_str()));
830 TCollection_AsciiString aSaveStudyName("");
831 if (isMultiFile) aSaveStudyName = (char*)SALOMEDS_Tool::GetNameFromPath(theSObject->GetStudy()->URL()).c_str();
833 if (strcmp(aLocalPersistentID, "Objet Med + /OBJ_MED/") == 0) return CORBA::string_dup(""); // MED
835 if (strncmp(aLocalPersistentID, "_MEDMESH_",9) == 0) {// MESH
836 MESH * myMesh= new MESH() ;
837 int aMeshNameLen = strlen(aLocalPersistentID) - 12;
838 char* aMeshName = new char[aMeshNameLen];
839 strncpy(aMeshName, &(aLocalPersistentID[9]), aMeshNameLen-1);
840 aMeshName[aMeshNameLen-1] = 0;
841 myMesh->setName(aMeshName);
845 char* aResultPath = HDFascii::ConvertFromASCIIToHDF((aTmpDir + aSaveStudyName + (char*)aLocalPersistentID).ToCString());
846 aFileName = new char[strlen(aResultPath) + 19];
847 sprintf(aFileName, "%shdf_from_ascii.hdf", aResultPath);
849 } else aFileName = CORBA::string_dup((aTmpDir + aSaveStudyName + (char*)aLocalPersistentID).ToCString());
850 MED_MESH_RDONLY_DRIVER21 myMeshDriver(aFileName,myMesh);
853 myMeshDriver.setMeshName(aMeshName);
856 catch (const std::exception & ex)
858 MESSAGE("Exception Interceptee : ");
860 THROW_SALOME_CORBA_EXCEPTION("Unable to find this mesh in this file",SALOME::BAD_PARAM);
865 MESSAGE("apres read");
866 myMeshDriver.close();
868 catch (const std::exception & ex)
870 MESSAGE("Exception Interceptee : ");
872 THROW_SALOME_CORBA_EXCEPTION("Unable to read this mesh in this file",SALOME::BAD_PARAM);
874 MESH_i * meshi = new MESH_i(myMesh);
875 //SALOME_MED::MESH_var mesh = SALOME_MED::MESH::_narrow(meshi->_this());
876 SALOME_MED::MESH_ptr mesh = meshi->_this();
877 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
879 aSeq[0]=CORBA::string_dup(aLocalPersistentID);
880 if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.ToCString(), aSeq.in(), true);
882 SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
883 aFilesToRemove->length(1);
884 aFilesToRemove[0] = CORBA::string_dup(&(aFileName[strlen(SALOMEDS_Tool::GetDirFromPath(aFileName).c_str())]));
885 SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aFileName).c_str(), aFilesToRemove, true);
888 return(CORBA::string_dup(_orb->object_to_string(mesh)));
889 } else if (strncmp(aLocalPersistentID, "_MEDFIELD_",14) == 0) { // FIELD
890 return(CORBA::string_dup("")); // not implemented yet
893 return CORBA::string_dup("");
896 //=============================================================================
898 * returns true, if can publish object
900 //=============================================================================
901 bool Med_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR) {
902 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(theIOR);
903 return !(aMesh->_is_nil());
906 //=============================================================================
908 * publish the given object
910 //=============================================================================
911 SALOMEDS::SObject_ptr Med_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
912 SALOMEDS::SObject_ptr theSObject,
913 CORBA::Object_ptr theObject,
914 const char* theName) throw (SALOME::SALOME_Exception)
916 SALOMEDS::SObject_var aResultSO;
918 if(CORBA::is_nil(theObject)) return aResultSO;
919 if(theStudy->_is_nil()) return aResultSO;
921 SALOMEDS::StudyBuilder_var aBuilder = theStudy->NewBuilder();
922 SALOMEDS::GenericAttribute_var anAttr;
923 SALOMEDS::AttributeName_var aName;
924 SALOMEDS::AttributeIOR_var anIOR;
925 SALOMEDS::SComponent_var aFather = theStudy->FindComponent("MED");
927 if (aFather->_is_nil()) {
928 aFather = aBuilder->NewComponent("MED");
929 if (aFather->_is_nil()) return aResultSO;
930 anAttr = aBuilder->FindOrCreateAttribute(aFather, "AttributeName");
931 aName = SALOMEDS::AttributeName::_narrow(anAttr);
932 //NRI aName->SetValue("MED");
934 CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
935 SALOME_ModuleCatalog::ModuleCatalog_var Catalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
936 SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent( "MED" );
937 if ( !Comp->_is_nil() ) {
938 aName->SetValue( Comp->componentusername() );
940 aBuilder->DefineComponentInstance(aFather, MED_Gen::_this());
943 if (CORBA::is_nil(theSObject)) {
944 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(theObject);
945 aMesh->addInStudy(theStudy, aMesh);
946 SALOMEDS::SObject_var aResultSO = theStudy->FindObjectIOR(_orb->object_to_string(theObject));
948 if (!theSObject->ReferencedObject(aResultSO))
949 THROW_SALOME_CORBA_EXCEPTION("Publish in study MED object error",SALOME::BAD_PARAM);
951 // aBuilder->Addreference(theObject, aResultSO);
952 return aResultSO._retn();
955 //=============================================================================
957 * returns true, if can copy the object
959 //=============================================================================
960 CORBA::Boolean Med_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject) {
961 // Try to retrieve known by MED component mesh by given IOR
962 SALOMEDS::GenericAttribute_var anAttr;
963 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return false;
965 CORBA::Object_var anObj = _orb->string_to_object(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
966 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(anObj);
967 // If the object is null one it can't be copied: return false
968 if (aMesh->_is_nil()) return false;
975 //=============================================================================
977 * create copy of the object and put it to the stream
979 //=============================================================================
980 SALOMEDS::TMPFile* Med_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject, CORBA::Long& theObjectID) {
981 // Declare a sequence of the byte to store the copied object
982 SALOMEDS::TMPFile_var aStreamFile;
984 // Try to get GEOM_Shape object by given SObject
985 SALOMEDS::GenericAttribute_var anAttr;
986 if (!theObject->FindAttribute(anAttr, "AttributeIOR")) return new SALOMEDS::TMPFile(0);
987 CORBA::String_var anIOR = CORBA::string_dup(SALOMEDS::AttributeIOR::_narrow(anAttr)->Value());
988 CORBA::Object_var anObj = _orb->string_to_object(anIOR);
989 SALOME_MED::MESH_var aMesh = SALOME_MED::MESH::_narrow(anObj);
990 if (aMesh->_is_nil()) return new SALOMEDS::TMPFile(0);
992 // Get a temporary directory to store a temporary file
993 CORBA::String_var aTmpDir = SALOMEDS_Tool::GetTmpDir().c_str();
994 // Create a list to store names of created files
995 SALOMEDS::ListOfFileNames_var aSeq = new SALOMEDS::ListOfFileNames;
997 aSeq[0] = CORBA::string_dup(aMesh->getName());
998 char* aFullName = new char[strlen(aTmpDir)+strlen(aSeq[0])+1];
999 strcpy(aFullName, aTmpDir);
1000 strcpy(aFullName+strlen(aTmpDir), aSeq[0]);
1001 long driverId = aMesh->addDriver(SALOME_MED::MED_DRIVER,aFullName , aMesh->getName());
1002 aMesh->write(driverId,"");
1004 // aStreamFile = SALOMEDS_Tool::PutFilesToStream(aTmpDir.c_str(), aSeq.in(), false);
1005 char* aFullName1 = new char[strlen(aTmpDir)+1];
1006 strcpy(aFullName1, aTmpDir);
1007 aStreamFile = SALOMEDS_Tool::PutFilesToStream(aFullName1, aSeq.in(), false);
1008 // SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
1009 SALOMEDS_Tool::RemoveTemporaryFiles(aFullName1, aSeq.in(), true);
1011 // Assign an ID = 1 the the type SALOME_MED::MESH
1014 return aStreamFile._retn();
1017 //=============================================================================
1019 * returns true, if can copy the object
1021 //=============================================================================
1022 CORBA::Boolean Med_Gen_i::CanPaste(const char* theComponentName, CORBA::Long theObjectID) {
1023 // The MED component can paste only objects copied by MED component
1024 // and with the object type = 1 (mesh)
1025 if (strcmp(theComponentName, ComponentDataType()) != 0 || theObjectID != 1) return false;
1029 //=============================================================================
1031 * returns true, if can copy the object
1033 //=============================================================================
1034 SALOMEDS::SObject_ptr Med_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
1035 CORBA::Long theObjectID,
1036 SALOMEDS::SObject_ptr theObject) {
1037 SALOMEDS::SObject_var aResultSO = SALOMEDS::SObject::_duplicate(theObject);
1038 if (theStream.length() == 0) return aResultSO._retn();
1040 SALOMEDS::Study_var aStudy = theObject->GetStudy();
1042 CORBA::String_var aTmpDir = CORBA::string_dup(SALOMEDS_Tool::GetTmpDir().c_str());
1043 char* aFullName2 = new char[strlen(aTmpDir)+1];
1044 strcpy(aFullName2,aTmpDir);
1045 // SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aTmpDir, false);
1046 SALOMEDS::ListOfFileNames_var aSeq = SALOMEDS_Tool::PutStreamToFiles(theStream, aFullName2, false);
1047 CORBA::String_var aMeshName = CORBA::string_dup(aSeq[0]);
1048 char* aFullName = new char[strlen(aTmpDir)+strlen(aMeshName)+1];
1049 strcpy(aFullName, aTmpDir);
1050 strcpy(aFullName+strlen(aTmpDir), aMeshName);
1052 MESH * myMesh= new MESH() ;
1053 // myMesh->setName(aMeshName.c_str());
1054 char* aFullMeshName = new char[strlen(aMeshName)+1];
1055 strcpy(aFullMeshName,aMeshName);
1056 myMesh->setName(aFullMeshName);
1057 MED_MESH_RDONLY_DRIVER21 myMeshDriver(aFullName, myMesh);
1059 myMeshDriver.setMeshName(aFullMeshName);
1060 myMeshDriver.open();
1061 } catch (const std::exception & ex) {
1062 MESSAGE("Exception Interceptee : ");
1064 return aResultSO._retn();
1067 myMeshDriver.read();
1069 myMeshDriver.close();
1070 } catch (const std::exception & ex) {
1071 MESSAGE("Exception Interceptee : ");
1073 return aResultSO._retn();
1075 // set new mesh name, becouse now there are no possibility to operate meshes with the same names
1076 // srand((unsigned int)time(NULL));
1077 int aRND = rand(); //Get a random number to present a name of a copied mesh
1078 char aCopiedMeshName[20];
1079 sprintf(aCopiedMeshName,"MESH_COPY_%d",aRND);
1080 myMesh->setName(aCopiedMeshName);
1081 MESH_i * meshi = new MESH_i(myMesh);
1082 SALOME_MED::MESH_ptr mesh = meshi->_this();
1083 // add the mesh object in study
1084 meshi->addInStudy(aStudy,mesh);
1085 // get the IOR attribute of just added mesh
1086 CORBA::String_var anIORString = _orb->object_to_string(mesh);
1087 aResultSO = aStudy->FindObjectIOR(anIORString);
1089 char * aFullName1 = new char[strlen(aTmpDir)+1];
1090 strcpy(aFullName1,aTmpDir);
1091 // SALOMEDS_Tool::RemoveTemporaryFiles(aTmpDir.c_str(), aSeq.in(), true);
1092 SALOMEDS_Tool::RemoveTemporaryFiles(aFullName1, aSeq.in(), true);
1093 return aResultSO._retn();
1101 //=============================================================================
1103 * C factory, accessible with dlsym, after dlopen
1105 //=============================================================================
1109 PortableServer::ObjectId * MEDEngine_factory(
1111 PortableServer::POA_ptr poa,
1112 PortableServer::ObjectId * contId,
1113 const char *instanceName,
1114 const char *interfaceName)
1116 MESSAGE("PortableServer::ObjectId * MedEngine_factory()");
1117 SCRUTE(interfaceName);
1118 Med_Gen_i * myMed_Gen
1119 = new Med_Gen_i(orb, poa, contId, instanceName, interfaceName);
1120 return myMed_Gen->getId() ;