1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SALOMEDSImpl_Study.cxx
24 // Author : Sergey RUIN
27 #include "SALOMEDSImpl_Study.hxx"
30 #include <KERNEL_version.h>
31 #include <Basics_Utils.hxx>
32 #include <Basics_DirUtils.hxx>
34 #include "HDFexplorer.hxx"
36 #include "DF_Application.hxx"
37 #include "DF_ChildIterator.hxx"
39 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
40 #include "SALOMEDSImpl_Attributes.hxx"
41 #include "SALOMEDSImpl_UseCaseIterator.hxx"
42 #include "SALOMEDSImpl_AttributeReference.hxx"
43 #include "SALOMEDSImpl_StudyHandle.hxx"
44 #include "SALOMEDSImpl_Tool.hxx"
45 #include "SALOMEDSImpl_IParameters.hxx"
46 #include "SALOMEDSImpl_ScalarVariable.hxx"
47 #include "SALOMEDSImpl_SComponent.hxx"
54 // comment out the following define to enable \t symbols in in the python dump files
57 #define DIRECTORYID 16661
58 #define FILELOCALID 26662
59 #define FILEID "FILE: "
60 #define VARIABLE_SEPARATOR ':'
61 #define OPERATION_SEPARATOR '|'
62 #define USE_CASE_LABEL_ID "0:2"
64 static void SaveAttributes(const SALOMEDSImpl_SObject& SO, HDFgroup *hdf_group_sobject);
65 static void ReadAttributes(SALOMEDSImpl_Study*, const SALOMEDSImpl_SObject&, HDFdataset* );
66 static void BuildTree (SALOMEDSImpl_Study*, HDFgroup*);
67 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject&,
68 SALOMEDSImpl_Driver*, bool isMultiFile, bool isASCII);
69 static void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup);
75 StudyUnlocker( SALOMEDSImpl_Study* study ): myStudy( study ), myLocked( false )
77 myPrevLocked = myStudy->GetProperties()->IsLocked();
87 myStudy->GetProperties()->SetLocked(true);
88 myPrevLocked = myLocked;
95 myStudy->GetProperties()->SetLocked(false);
96 myLocked = myPrevLocked;
101 SALOMEDSImpl_Study* myStudy;
107 //============================================================================
108 /*! Function : SALOMEDSImpl_Study
109 * Purpose : SALOMEDSImpl_Study constructor
111 //============================================================================
112 SALOMEDSImpl_Study::SALOMEDSImpl_Study()
114 _appli = new DF_Application();
115 _clipboard = _appli->NewDocument("SALOME_STUDY");
120 //============================================================================
121 /*! Function : ~SALOMEDSImpl_Study
122 * Purpose : SALOMEDSImpl_Study destructor
124 //============================================================================
125 SALOMEDSImpl_Study::~SALOMEDSImpl_Study()
128 _appli->Close(_clipboard);
129 // Destroy application
133 //============================================================================
135 * Purpose : Initialize study components
137 //============================================================================
138 void SALOMEDSImpl_Study::Init()
141 std::stringstream sstrm;
143 _name = "Study" + std::string(sstrm.str());
144 _doc = _appli->NewDocument("SALOME_STUDY");
149 _useCaseBuilder = new SALOMEDSImpl_UseCaseBuilder(_doc);
150 _builder = new SALOMEDSImpl_StudyBuilder(this);
151 _cb = new SALOMEDSImpl_Callback(_useCaseBuilder);
153 //Put on the root label a StudyHandle attribute to store the address of this object
154 //It will be used to retrieve the study object by DF_Label that belongs to the study
155 SALOMEDSImpl_StudyHandle::Set(_doc->Main().Root(), this);
157 // set Study properties
158 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
160 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
161 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
162 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
163 mn, hh, day, month, year);
164 aProp->SetCreationMode(1); //"from scratch"
167 //============================================================================
169 * Purpose : Clear study components
171 //============================================================================
172 void SALOMEDSImpl_Study::Clear()
176 delete _useCaseBuilder;
183 //============================================================================
185 * Purpose : Open a Study from it's persistent reference
187 //============================================================================
188 bool SALOMEDSImpl_Study::Open(const std::string& aUrl)
192 // Set "C" locale temporarily to avoid possible localization problems
193 Kernel_Utils::Localizer loc;
198 HDFfile *hdf_file =0;
199 HDFgroup *hdf_group_study_structure =0;
200 HDFgroup *hdf_notebook_vars = 0;
204 bool isASCII = false;
205 if (HDFascii::isASCII(aUrl.c_str())) {
207 char* aResultPath = HDFascii::ConvertFromASCIIToHDF(aUrl.c_str());
210 aC_HDFUrl = new char[strlen(aResultPath) + 19];
211 sprintf(aC_HDFUrl, "%shdf_from_ascii.hdf", aResultPath);
212 delete [] (aResultPath);
220 hdf_file = new HDFfile((char*)aHDFUrl.c_str());
222 hdf_file->OpenOnDisk(HDF_RDONLY);// mpv: was RDWR, but opened file can be write-protected too
227 eStr = new char[strlen(aUrl.c_str())+17];
228 sprintf(eStr,"Can't open file %s",aUrl.c_str());
230 _errorCode = std::string(eStr);
234 // Assign the value of the URL in the study object
237 SALOMEDSImpl_AttributePersistentRef::Set(_doc->Main(), aUrl);
239 if (!hdf_file->ExistInternalObject("STUDY_STRUCTURE")) {
240 _errorCode = "Study is empty";
244 //Create the Structure of the Document
245 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
248 BuildTree (this, hdf_group_study_structure);
252 char *eStr = new char [strlen(aUrl.c_str())+17];
253 sprintf(eStr,"Can't open file %s", aUrl.c_str());
254 _errorCode = std::string(eStr);
258 //Read and create notebook variables
259 if(hdf_file->ExistInternalObject("NOTEBOOK_VARIABLES")) {
260 hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
261 ReadNoteBookVariables(this, hdf_notebook_vars);
262 hdf_notebook_vars =0; //will be deleted by hdf_sco_group destructor
265 hdf_file->CloseOnDisk();
266 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
269 std::vector<std::string> aFilesToRemove;
270 aFilesToRemove.push_back("hdf_from_ascii.hdf");
271 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
274 delete hdf_file; // all related hdf objects will be deleted
276 // unlock study if it is locked, to set components versions
277 StudyUnlocker unlock(this);
279 //For old studies we have to add "unknown" version tag for all stored components
280 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
281 for (; itcomponent.More(); itcomponent.Next())
283 SALOMEDSImpl_SComponent sco = itcomponent.Value();
284 std::string aCompType = sco.GetComment();
285 if ( aCompType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() ) continue;
286 if ( GetProperties()->GetComponentVersions( aCompType ).empty() )
287 GetProperties()->SetComponentVersion( aCompType, "" ); // empty version means "unknown"
293 //============================================================================
295 * Purpose : Save a Study to it's persistent reference
297 //============================================================================
298 bool SALOMEDSImpl_Study::Save(SALOMEDSImpl_DriverFactory* aFactory,
303 std::string url = URL();
305 _errorCode = "No path specified to save the study. Nothing done";
309 return Impl_SaveAs(url, aFactory, theMultiFile, false);
315 bool SALOMEDSImpl_Study::SaveASCII(SALOMEDSImpl_DriverFactory* aFactory,
320 std::string url = URL();
322 _errorCode = "No path specified to save the study. Nothing done";
326 return Impl_SaveAs(url, aFactory, theMultiFile, true);
332 //=============================================================================
333 /*! Function : SaveAs
334 * Purpose : Save a study to the persistent reference aUrl
336 //============================================================================
337 bool SALOMEDSImpl_Study::SaveAs(const std::string& aUrl,
338 SALOMEDSImpl_DriverFactory* aFactory,
342 return Impl_SaveAs(aUrl, aFactory, theMultiFile, false);
345 bool SALOMEDSImpl_Study::SaveAsASCII(const std::string& aUrl,
346 SALOMEDSImpl_DriverFactory* aFactory,
350 return Impl_SaveAs(aUrl, aFactory, theMultiFile, true);
353 //=============================================================================
354 /*! Function : _SaveProperties
355 * Purpose : save the study properties in HDF file
357 //============================================================================
358 bool SALOMEDSImpl_Study::Impl_SaveProperties(HDFgroup *hdf_group)
362 HDFdataset *hdf_dataset = 0;
366 // add modifications list (user and date of save)
367 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
369 // unlock study if it is locked, to set modification date
370 StudyUnlocker unlock(this);
372 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
373 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
374 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
375 mn, hh, day, month, year);
377 // lock study back if it was locked initially, to write correct value of Locked flag
380 std::vector<std::string> aNames;
381 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
383 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
385 std::string units = aProp->GetUnits();
386 std::string comment = aProp->GetComment();
388 std::map< std::string, std::vector<std::string> > allVersions = aProp->GetComponentsVersions();
389 std::map<std::string, std::string> versions;
391 int aLength = 0, aLength1 = 0, anIndex, i, unitsSize = 0, commentSize = 0;
393 for(i=1; i<=aNames.size(); i++)
394 aLength += aNames[i-1].size() + 1;
396 std::map< std::string, std::vector<std::string> >::const_iterator it;
397 for (it = allVersions.begin(); it != allVersions.end(); ++it ) {
398 std::string vlist = "";
399 std::vector<std::string> vl = it->second;
400 std::vector<std::string>::const_iterator vlit;
401 for ( vlit = vl.begin(); vlit != vl.end(); ++vlit ) {
402 if ( vlist != "" ) vlist += ";";
405 versions[ it->first ] = vlist;
406 aLength1 += it->first.size() + vlist.size() + 2;
409 unitsSize = units.size();
410 commentSize = comment.size();
413 //locked flag, modified flag,
414 //minutes, hours, day, months, year, user name, char(1),
415 //minutes, hours, day, months, year, user name, char(1),
416 //.....................................................,
417 //.....................................................,
418 //.....................................................,
419 //minutes, hours, day, months, year, user name, char(1), char(30) <- !!!! used to define end of section with modifications !!!!
420 //units, char(1), comment, char(30) <- !!!! used to define start of section with components' versions !!!!
421 //component=versions, char(1),
422 //component=versions, char(1),
423 //...........................,
424 //component=versions, char(1), char(0)
426 //string length: 1 byte = locked flag, 1 byte = modified flag, (12 + name length + 1) for each name and date, 1 byte (char(30) section delimeter)
427 // unit length + 1, comment length, "zero" byte
429 char* aProperty = new char[3 + aLength + 12 * aNames.size() + 1 + unitsSize + 1 + commentSize + 1 + aLength1 ];
431 sprintf(aProperty,"%c%c", (char)aProp->GetCreationMode(), (aProp->IsLocked())?'l':'u');
433 aLength = aNames.size();
435 for(anIndex = 0; anIndex<aLength; anIndex++) {
436 sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
437 (int)(aMinutes[anIndex]),
438 (int)(aHours[anIndex]),
439 (int)(aDays[anIndex]),
440 (int)(aMonths[anIndex]),
441 (int)(aYears[anIndex]),
442 aNames[anIndex].c_str());
443 a = strlen(aProperty);
447 //Write delimeter of the section to define end of the modifications section
450 //Write units if need
451 if(units.size() > 0) {
452 sprintf(&(aProperty[a]),"%s",units.c_str());
453 a = strlen(aProperty);
458 //Write comments if need
459 if(comment.size() > 0) {
460 sprintf(&(aProperty[a]),"%s",comment.c_str());
461 a = strlen(aProperty);
464 aProperty[a++] = 30; //delimeter of the component versions
466 std::map<std::string, std::string>::const_iterator versionsIt;
467 for ( versionsIt = versions.begin(); versionsIt != versions.end(); ++versionsIt ) {
468 sprintf(&(aProperty[a]),"%s=%s",
469 (char*)(versionsIt->first.c_str()),
470 (char*)(versionsIt->second.c_str()));
471 a = a + versionsIt->first.size() + versionsIt->second.size() + 1;
477 name_len = (hdf_int32) a;
478 size[0] = name_len + 1 ;
479 hdf_dataset = new HDFdataset("AttributeStudyProperties",hdf_group,HDF_STRING,size,1);
480 hdf_dataset->CreateOnDisk();
481 hdf_dataset->WriteOnDisk(aProperty);
482 hdf_dataset->CloseOnDisk();
483 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
486 aProp->SetModified(0);
490 //=============================================================================
491 /*! Function : _SaveAs
492 * Purpose : save the study in HDF file
494 //============================================================================
495 bool SALOMEDSImpl_Study::Impl_SaveAs(const std::string& aStudyUrl,
496 SALOMEDSImpl_DriverFactory* aFactory,
500 // Set "C" locale temporarily to avoid possible localization problems
501 Kernel_Utils::Localizer loc;
503 // HDF File will be composed of differents part :
504 // * For each ComponentDataType, all data created by the component
505 // Informations in data group hdf_group_datacomponent
506 // * Study Structure -> Exactly what is contained in Document
507 // Informations in data group hdf_group_study_structure
512 HDFgroup *hdf_group_study_structure =0;
513 HDFgroup *hdf_sco_group =0;
514 HDFgroup *hdf_sco_group2 =0;
515 HDFgroup *hdf_notebook_vars =0;
516 HDFgroup *hdf_notebook_var = 0;
518 HDFgroup *hdf_group_datacomponent =0;
519 HDFdataset *hdf_dataset =0;
521 hdf_int32 name_len = 0;
522 std::string component_name;
524 // Store previous URL
525 std::string anOldName = Name();
527 // Map to store components' versions
528 std::map<std::string, std::string> componentVersions;
530 //Create a temporary url to which the study is saved
531 std::string aUrl = SALOMEDSImpl_Tool::GetTmpDir() + SALOMEDSImpl_Tool::GetNameFromPath(aStudyUrl);
533 // unlock study if it is locked, as some attributes need to be modified
534 StudyUnlocker unlock(this);
536 SALOMEDSImpl_StudyBuilder* SB= NewBuilder();
537 std::map<std::string, SALOMEDSImpl_Driver*> aMapTypeDriver;
540 // mpv 15.12.2003: for saving components we have to load all data from all modules
541 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
542 for (; itcomponent.More(); itcomponent.Next()) {
543 SALOMEDSImpl_SComponent sco = itcomponent.Value();
544 // if there is an associated Engine call its method for saving
545 std::string IOREngine;
547 SALOMEDSImpl_Driver* aDriver = NULL;
548 std::string aCompType = sco.GetComment();
549 if (!sco.ComponentIOR(IOREngine)) {
550 if (!aCompType.empty()) {
551 aDriver = aFactory->GetDriverByType(aCompType);
552 if (aDriver != NULL) {
553 if (!SB->LoadWith(sco, aDriver)) {
554 _errorCode = SB->GetErrorCode();
561 aDriver = aFactory->GetDriverByIOR(IOREngine);
563 aMapTypeDriver[aCompType] = aDriver;
566 _errorCode = "Can not restore information to resave it";
571 // VSR: set URL to new file name
572 // VSR: remember to set previous name if save operation fails
575 // To change for Save
576 // Do not have to do a new file but just a Open??? Rewrite all informations after erasing evrything??
577 hdf_file = new HDFfile((char*)aUrl.c_str());
578 hdf_file->CreateOnDisk();
580 //-----------------------------------------------------------------------
581 // 1 - Create a groupe for each SComponent and Update the PersistanceRef
582 //-----------------------------------------------------------------------
583 hdf_group_datacomponent = new HDFgroup("DATACOMPONENT",hdf_file);
584 hdf_group_datacomponent->CreateOnDisk();
586 for (itcomponent.Init(); itcomponent.More(); itcomponent.Next()) {
587 SALOMEDSImpl_SComponent sco = itcomponent.Value();
589 std::string scoid = sco.GetID();
590 hdf_sco_group = new HDFgroup((char*)scoid.c_str(), hdf_group_datacomponent);
591 hdf_sco_group->CreateOnDisk();
593 std::string componentDataType = sco.ComponentDataType();
594 std::string IOREngine;
595 if (sco.ComponentIOR(IOREngine)) {
596 // Engine should be already in the map as it was to added before
597 SALOMEDSImpl_Driver* Engine = aMapTypeDriver[componentDataType];
598 if (Engine != NULL) {
599 SALOMEDSImpl_TMPFile* aStream = NULL;
602 componentVersions[ componentDataType ] = Engine->Version();
604 if (theASCII) aStream = Engine->SaveASCII(sco,
605 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
608 else aStream = Engine->Save(sco,
609 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
612 HDFdataset *hdf_dataset;
613 hdf_size aHDFSize[1];
614 if (length > 0) { //The component saved some auxiliary files, then put them into HDF file
615 aHDFSize[0] = length;
617 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group, HDF_STRING, aHDFSize, 1);
618 hdf_dataset->CreateOnDisk();
619 hdf_dataset->WriteOnDisk(aStream->Data()); //Save the stream in the HDF file
620 hdf_dataset->CloseOnDisk();
623 if (aStream) delete aStream;
625 // store multifile state
627 hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
628 hdf_dataset->CreateOnDisk();
629 hdf_dataset->WriteOnDisk((void*)(theMultiFile?"M":"S")); // save: multi or single
630 hdf_dataset->CloseOnDisk();
631 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
634 hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
635 hdf_dataset->CreateOnDisk();
636 hdf_dataset->WriteOnDisk((void*)(theASCII?"A":"B")); // save: ASCII or BINARY
637 hdf_dataset->CloseOnDisk();
638 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
639 // Creation of the persistance reference attribute
640 Translate_IOR_to_persistentID (sco, Engine, theMultiFile, theASCII);
643 hdf_sco_group->CloseOnDisk();
644 hdf_sco_group=0; // will be deleted by hdf_group_datacomponent destructor
646 hdf_group_datacomponent->CloseOnDisk();
647 hdf_group_datacomponent =0; // will be deleted by hdf_file destructor
649 //-----------------------------------------------------------------------
650 //3 - Write the Study Structure
651 //-----------------------------------------------------------------------
652 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
653 hdf_group_study_structure->CreateOnDisk();
654 // save component attributes
655 for (itcomponent.Init(); itcomponent.More(); itcomponent.Next()) {
656 SALOMEDSImpl_SComponent SC = itcomponent.Value();
657 std::string scid = SC.GetID();
658 hdf_sco_group2 = new HDFgroup((char*)scid.c_str(), hdf_group_study_structure);
659 hdf_sco_group2->CreateOnDisk();
660 SaveAttributes(SC, hdf_sco_group2);
661 // ComponentDataType treatment
662 component_name = SC.ComponentDataType();
663 name_len = (hdf_int32)component_name.length();
664 size[0] = name_len +1 ;
665 hdf_dataset = new HDFdataset("COMPONENTDATATYPE",hdf_sco_group2,HDF_STRING,size,1);
666 hdf_dataset->CreateOnDisk();
667 hdf_dataset->WriteOnDisk((char*)component_name.c_str());
668 hdf_dataset->CloseOnDisk();
669 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
670 Impl_SaveObject(SC, hdf_sco_group2);
671 hdf_sco_group2->CloseOnDisk();
672 hdf_sco_group2=0; // will be deleted by hdf_group_study_structure destructor
674 //-----------------------------------------------------------------------
675 //4 - Write the Study UseCases Structure
676 //-----------------------------------------------------------------------
677 SALOMEDSImpl_SObject aSO = FindObjectID(USE_CASE_LABEL_ID);
679 HDFgroup *hdf_soo_group = new HDFgroup(USE_CASE_LABEL_ID,hdf_group_study_structure);
680 hdf_soo_group->CreateOnDisk();
681 SaveAttributes(aSO, hdf_soo_group);
682 Impl_SaveObject(aSO, hdf_soo_group);
683 hdf_soo_group->CloseOnDisk();
684 hdf_soo_group=0; // will be deleted by hdf_group_study_structure destructor
686 //-----------------------------------------------------------------------
687 //5 - Write the NoteBook Variables
688 //-----------------------------------------------------------------------
690 //5.1 Create group to store all note book variables
691 hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
692 hdf_notebook_vars->CreateOnDisk();
694 std::string varValue;
696 std::string varIndex;
698 for (int i=0 ;i < myNoteBookVars.size(); i++ ) {
699 // For each variable create HDF group
700 hdf_notebook_var = new HDFgroup((char*)myNoteBookVars[i]->Name().c_str(),hdf_notebook_vars);
701 hdf_notebook_var->CreateOnDisk();
703 // Save Variable type
704 varType = myNoteBookVars[i]->SaveType();
705 name_len = (hdf_int32) varType.length();
706 size[0] = name_len +1 ;
707 hdf_dataset = new HDFdataset("VARIABLE_TYPE",hdf_notebook_var,HDF_STRING,size,1);
708 hdf_dataset->CreateOnDisk();
709 hdf_dataset->WriteOnDisk((char*)varType.c_str());
710 hdf_dataset->CloseOnDisk();
711 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
714 sprintf(buffer,"%d",i);
715 varIndex= std::string(buffer);
716 name_len = (hdf_int32) varIndex.length();
717 size[0] = name_len +1 ;
718 hdf_dataset = new HDFdataset("VARIABLE_INDEX",hdf_notebook_var,HDF_STRING,size,1);
719 hdf_dataset->CreateOnDisk();
720 hdf_dataset->WriteOnDisk((char*)varIndex.c_str());
721 hdf_dataset->CloseOnDisk();
722 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
724 // Save Variable value
725 varValue = myNoteBookVars[i]->Save();
726 name_len = (hdf_int32) varValue.length();
727 size[0] = name_len +1 ;
728 hdf_dataset = new HDFdataset("VARIABLE_VALUE",hdf_notebook_var,HDF_STRING,size,1);
729 hdf_dataset->CreateOnDisk();
730 hdf_dataset->WriteOnDisk((char*)varValue.c_str());
731 hdf_dataset->CloseOnDisk();
732 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
733 hdf_notebook_var->CloseOnDisk();
734 hdf_notebook_var = 0; //will be deleted by hdf_sco_group destructor
736 hdf_notebook_vars->CloseOnDisk();
737 hdf_notebook_vars = 0; //will be deleted by hdf_sco_group destructor
739 // record component versions
740 std::map<std::string, std::string>::const_iterator itVersions;
741 for ( itVersions = componentVersions.begin(); itVersions != componentVersions.end(); ++itVersions )
742 GetProperties()->SetComponentVersion( itVersions->first, itVersions->second );
744 // lock study back if it was locked initially, to write correct value of Locked flag
747 //-----------------------------------------------------------------------
748 //6 - Write the Study Properties
749 //-----------------------------------------------------------------------
750 std::string study_name = Name();
751 name_len = (hdf_int32) study_name.size();
752 size[0] = name_len +1 ;
753 hdf_dataset = new HDFdataset("STUDY_NAME",hdf_group_study_structure,HDF_STRING,size,1);
754 hdf_dataset->CreateOnDisk();
755 hdf_dataset->WriteOnDisk((char*)study_name.c_str());
756 hdf_dataset->CloseOnDisk();
757 hdf_dataset=0; // will be deleted by hdf_group_study_structure destructor
759 Impl_SaveProperties(hdf_group_study_structure);
760 hdf_group_study_structure->CloseOnDisk();
761 hdf_file->CloseOnDisk();
763 hdf_group_study_structure =0; // will be deleted by hdf_file destructor
764 delete hdf_file; // recursively deletes all hdf objects...
766 catch (HDFexception) {
767 _errorCode = "HDFexception ! ";
768 URL( anOldName ); // VSR: restore previous url if operation is failed
771 catch (std::exception& exc) {
772 _errorCode = const_cast<char*>(exc.what());
773 URL( anOldName ); // VSR: restore previous url if operation is failed
777 _errorCode = "Unknown exception ! ";
778 URL( anOldName ); // VSR: restore previous url if operation is failed
781 if (theASCII) { // save file in ASCII format
782 HDFascii::ConvertFromHDFToASCII(aUrl.c_str(), true);
785 // Now it's necessary to copy files from the temporary directory to the user defined directory.
786 // The easiest way to get a list of file in the temporary directory
788 std::string aCmd, aTmpFileDir = SALOMEDSImpl_Tool::GetTmpDir();
789 std::string aTmpFile = aTmpFileDir +"files";
790 std::string aStudyTmpDir = SALOMEDSImpl_Tool::GetDirFromPath(aUrl);
793 aCmd = "dir /B \"" + aStudyTmpDir +"\" > " + aTmpFile;
795 aCmd ="ls -1 \"" + aStudyTmpDir +"\" > " + aTmpFile;
797 system(aCmd.c_str());
799 // Iterate and move files in the temporary directory
800 FILE* fp = fopen(aTmpFile.c_str(), "rb");
802 URL( anOldName ); // VSR: restore previous url if operation is failed
805 char* buffer = new char[2047];
807 while (!feof(fp) && !errors) {
808 if ((fgets(buffer, 2046, fp)) == NULL) break;
809 size_t aLen = strlen(buffer);
810 if (buffer[aLen-1] == '\n') buffer[aLen-1] = char(0);
812 aCmd = "move /Y \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl) +"\"";
814 aCmd = "mv -f \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl)+"\"";
816 errors = system(aCmd.c_str());
824 DeleteFileA(aTmpFile.c_str());
826 unlink(aTmpFile.c_str());
830 RemoveDirectoryA(aTmpFileDir.c_str());
831 RemoveDirectoryA(aStudyTmpDir.c_str());
833 rmdir(aTmpFileDir.c_str());
834 rmdir(aStudyTmpDir.c_str());
838 // VSR: finally, if all is done without errors, mark study as Saved
845 //============================================================================
846 /*! Function : Impl_SaveObject
849 //============================================================================
850 bool SALOMEDSImpl_Study::Impl_SaveObject(const SALOMEDSImpl_SObject& SC,
851 HDFgroup *hdf_group_datatype)
855 // Write in group hdf_group_datatype all informations of SObject SC
856 // Iterative function to parse all SObjects under a SComponent
858 HDFgroup *hdf_group_sobject = 0;
860 DF_ChildIterator itchild(SC.GetLabel());
861 for (; itchild.More(); itchild.Next()) {
862 // mpv: don't save empty labels
863 std::vector<DF_Attribute*> attr = itchild.Value().GetAttributes();
864 if (attr.size() == 0) { //No attributes on the label
865 DF_ChildIterator subchild(itchild.Value());
866 if (!subchild.More()) {
869 subchild.Init(itchild.Value(), true);
871 for (; subchild.More() && anEmpty; subchild.Next()) {
872 std::vector<DF_Attribute*> attr2 = subchild.Value().GetAttributes();
874 anEmpty = false; //There are attributes on the child label
878 if (anEmpty) continue;
881 SALOMEDSImpl_SObject SO = SALOMEDSImpl_Study::SObject(itchild.Value());
883 std::string scoid = SO.GetID();
884 hdf_group_sobject = new HDFgroup(scoid.c_str(), hdf_group_datatype);
885 hdf_group_sobject->CreateOnDisk();
886 SaveAttributes(SO, hdf_group_sobject);
887 Impl_SaveObject(SO, hdf_group_sobject);
888 hdf_group_sobject->CloseOnDisk();
889 hdf_group_sobject =0; // will be deleted by father hdf object destructor
894 //============================================================================
895 /*! Function : CanCopy
898 //============================================================================
899 bool SALOMEDSImpl_Study::CanCopy(const SALOMEDSImpl_SObject& theObject,
900 SALOMEDSImpl_Driver* theEngine)
903 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
904 if (!aComponent) return false;
905 if (aComponent.GetLabel() == theObject.GetLabel()) return false;
906 std::string IOREngine;
907 if (!aComponent.ComponentIOR(IOREngine)) return false;
908 if (theEngine == NULL) return false;
909 return theEngine->CanCopy(theObject);
912 //============================================================================
913 /*! Function : CopyLabel
916 //============================================================================
917 bool SALOMEDSImpl_Study::CopyLabel(SALOMEDSImpl_Driver* theEngine,
918 const int theSourceStartDepth,
919 const DF_Label& theSource,
920 const DF_Label& theDestinationMain)
925 DF_Label aTargetLabel = theDestinationMain;
926 DF_Label aAuxTargetLabel = theDestinationMain.Father().FindChild(2);
927 for(a = theSource.Depth() - theSourceStartDepth; a > 0 ; a--) {
928 DF_Label aSourceLabel = theSource;
929 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
930 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
931 aAuxTargetLabel = aAuxTargetLabel.FindChild(aSourceLabel.Tag());
933 // iterate attributes
934 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
935 for(int i = 0, len = attrList.size(); i<len; i++) {
936 DF_Attribute* anAttr = attrList[i];
937 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
938 if (type.substr(0, 17) == std::string("AttributeTreeNode")) continue; // never copy tree node attribute
939 if (type == std::string("AttributeTarget")) continue; // and target attribute
941 if (type == std::string("AttributeReference")) { // reference copied as Comment in aux tree
942 DF_Label aReferenced = dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Get();
943 std::string anEntry = aReferenced.Entry();
944 // store the value of name attribute of referenced label
945 SALOMEDSImpl_AttributeName* aNameAttribute;
946 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aReferenced.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
948 anEntry += aNameAttribute->Value();
950 SALOMEDSImpl_AttributeComment::Set(aAuxTargetLabel, anEntry);
954 if (type == std::string("AttributeIOR")) { // IOR => ID and TMPFile of Engine
955 std::string anEntry = theSource.Entry();
956 SALOMEDSImpl_SObject aSO = FindObjectID(anEntry);
959 SALOMEDSImpl_TMPFile* aStream = theEngine->CopyFrom(aSO, anObjID, aLen);
960 std::string aResStr("");
961 for(a = 0; a < aLen; a++) {
962 aResStr += (char)(aStream->Get(a));
965 if(aStream) delete aStream;
967 SALOMEDSImpl_AttributeInteger::Set(aAuxTargetLabel, anObjID);
968 SALOMEDSImpl_AttributeName::Set(aAuxTargetLabel, aResStr);
971 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
972 aTargetLabel.AddAttribute(aNewAttribute);
973 anAttr->Paste(aNewAttribute);
979 //============================================================================
983 //============================================================================
984 bool SALOMEDSImpl_Study::Copy(const SALOMEDSImpl_SObject& theObject,
985 SALOMEDSImpl_Driver* theEngine)
989 // adoptation for alliances datamodel copy: without IOR attributes !!!
990 bool aStructureOnly; // copy only SObjects and attributes without component help
991 aStructureOnly = !theObject.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID());
993 // get component-engine
994 SALOMEDSImpl_Study* aStudy = theObject.GetStudy();
997 _errorCode = "Document is null";
1001 //Clear the clipboard
1002 _clipboard->Main().Root().ForgetAllAttributes(true);
1003 _appli->Close(_clipboard);
1004 _clipboard = _appli->NewDocument("SALOME_STUDY");
1006 // set component data type to the name attribute of root label
1007 if (!aStructureOnly) {
1008 SALOMEDSImpl_AttributeComment::Set(_clipboard->Main().Root(),
1009 theEngine->ComponentDataType());
1011 // iterate all theObject's label children
1012 DF_Label aStartLabel = theObject.GetLabel();
1013 int aSourceStartDepth = aStartLabel.Depth();
1015 // copy main source label
1016 CopyLabel(theEngine, aSourceStartDepth, aStartLabel, _clipboard->Main());
1018 // copy all subchildren of the main source label (all levels)
1019 DF_ChildIterator anIterator(aStartLabel, true);
1020 for(; anIterator.More(); anIterator.Next()) {
1021 CopyLabel(theEngine, aSourceStartDepth, anIterator.Value(), _clipboard->Main());
1027 //============================================================================
1028 /*! Function : CanPaste
1031 //============================================================================
1032 bool SALOMEDSImpl_Study::CanPaste(const SALOMEDSImpl_SObject& theObject,
1033 SALOMEDSImpl_Driver* theEngine)
1038 _errorCode = "Clipboard is null";
1042 SALOMEDSImpl_AttributeComment* aCompName = NULL;
1043 if (!(aCompName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1044 _errorCode = "Clipboard has no component type";
1047 SALOMEDSImpl_AttributeInteger* anObjID;
1048 if (!(anObjID=(SALOMEDSImpl_AttributeInteger*)_clipboard->Main().Father().FindChild(2).FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
1049 _errorCode = "Clipboard has no object id";
1052 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1054 _errorCode = "Object doesn't belong to component";
1058 std::string IOREngine;
1059 if (!aComponent.ComponentIOR(IOREngine)) {
1060 _errorCode = "component has no IOR";
1063 return theEngine->CanPaste(aCompName->Value(), anObjID->Value());
1066 //============================================================================
1067 /*! Function : PasteLabel
1070 //============================================================================
1071 DF_Label SALOMEDSImpl_Study::PasteLabel(SALOMEDSImpl_Driver* theEngine,
1072 const DF_Label& theSource,
1073 const DF_Label& theDestinationStart,
1074 const bool isFirstElement)
1078 // get corresponding source, target and auxiliary labels
1079 DF_Label aTargetLabel = theDestinationStart;
1081 DF_Label aAuxSourceLabel = theSource.Root().FindChild(2);
1083 if (!isFirstElement) {
1084 for(a = theSource.Depth() - 1; a > 0 ; a--) {
1085 DF_Label aSourceLabel = theSource;
1086 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
1087 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
1088 aAuxSourceLabel = aAuxSourceLabel.FindChild(aSourceLabel.Tag());
1090 SALOMEDSImpl_SObject so = GetSObject(aTargetLabel);
1091 addSO_Notification(so);
1094 // check auxiliary label for TMPFile => IOR
1095 SALOMEDSImpl_AttributeName* aNameAttribute = NULL;
1096 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1097 SALOMEDSImpl_AttributeInteger* anObjID = (SALOMEDSImpl_AttributeInteger*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeInteger::GetID());
1098 SALOMEDSImpl_AttributeComment* aComponentName = (SALOMEDSImpl_AttributeComment*)theSource.Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID());
1099 std::string aCompName = aComponentName->Value();
1101 if (theEngine->CanPaste(aCompName, anObjID->Value())) {
1102 std::string aTMPStr = aNameAttribute->Value();
1103 int aLen = aTMPStr.size();
1104 unsigned char* aStream = NULL;
1106 aStream = new unsigned char[aLen+10];
1107 for(a = 0; a < aLen; a++) {
1108 aStream[a] = aTMPStr[a];
1112 std::string anEntry = aTargetLabel.Entry();
1113 SALOMEDSImpl_SObject aPastedSO = FindObjectID(anEntry);
1115 if (isFirstElement) {
1116 std::string aDestEntry = theEngine->PasteInto(aStream,
1119 aPastedSO.GetFatherComponent());
1120 aTargetLabel = DF_Label::Label(theDestinationStart, aDestEntry);
1122 theEngine->PasteInto(aStream, aLen, anObjID->Value(), aPastedSO);
1124 if(aStream != NULL) delete []aStream;
1128 // iterate attributes
1129 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
1130 for(int i = 0, len = attrList.size(); i<len; i++) {
1131 DF_Attribute* anAttr = attrList[i];
1132 if (aTargetLabel.FindAttribute(anAttr->ID())) {
1133 aTargetLabel.ForgetAttribute(anAttr->ID());
1135 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
1136 aTargetLabel.AddAttribute(aNewAttribute);
1137 anAttr->Paste(aNewAttribute);
1140 // check auxiliary label for Comment => reference or name attribute of the referenced object
1141 SALOMEDSImpl_AttributeComment* aCommentAttribute = NULL;
1142 if ((aCommentAttribute=(SALOMEDSImpl_AttributeComment*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1143 char * anEntry = new char[aCommentAttribute->Value().size() + 1];
1144 strcpy(anEntry, std::string(aCommentAttribute->Value()).c_str());
1145 char* aNameStart = strchr(anEntry, ' ');
1150 // copy to the same study, reanimate reference
1151 DF_Label aRefLabel = DF_Label::Label(aTargetLabel, anEntry);
1152 SALOMEDSImpl_AttributeReference::Set(aTargetLabel, aRefLabel);
1153 // target attributes structure support
1154 SALOMEDSImpl_AttributeTarget::Set(aRefLabel)->Add(SALOMEDSImpl_Study::SObject(aTargetLabel));
1159 return aTargetLabel;
1162 //============================================================================
1163 /*! Function : Paste
1166 //============================================================================
1167 SALOMEDSImpl_SObject SALOMEDSImpl_Study::Paste(const SALOMEDSImpl_SObject& theObject,
1168 SALOMEDSImpl_Driver* theEngine)
1172 SALOMEDSImpl_SObject so;
1174 // if study is locked, then paste can't be done
1175 if (GetProperties()->IsLocked()) {
1176 _errorCode = "LockProtection";
1177 throw LockProtection("LockProtection");
1180 // if there is no component name, then paste only SObjects and attributes: without component help
1181 SALOMEDSImpl_AttributeComment* aComponentName = NULL;
1182 bool aStructureOnly = !(aComponentName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()));
1184 // CAF document of current study usage
1186 _errorCode = "Document is null";
1190 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1192 // fill root inserted SObject
1193 DF_Label aStartLabel;
1194 if (aStructureOnly) {
1195 DF_Label anObjectLabel = DF_Label::Label(_doc->Main(), theObject.GetID());
1196 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), anObjectLabel, false);
1198 DF_Label aComponentLabel = DF_Label::Label(_doc->Main(), aComponent.GetID());
1199 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), aComponentLabel, true);
1202 // paste all sublebels
1203 DF_ChildIterator anIterator(_clipboard->Main(), true);
1204 for(; anIterator.More(); anIterator.Next()) {
1205 PasteLabel(theEngine, anIterator.Value(), aStartLabel, false);
1208 return SALOMEDSImpl_Study::SObject(aStartLabel);
1211 //============================================================================
1212 /*! Function : GetPersistentReference
1213 * Purpose : Get persistent reference of study (idem URL())
1215 //============================================================================
1216 std::string SALOMEDSImpl_Study::GetPersistentReference()
1221 //============================================================================
1222 /*! Function : GetTransientReference
1223 * Purpose : Get IOR of the Study (registred in Document in doc->Root)
1225 //============================================================================
1226 std::string SALOMEDSImpl_Study::GetTransientReference()
1229 std::string IOR = "";
1231 SALOMEDSImpl_AttributeIOR* Att;
1232 DF_Label _lab = _doc->Root();
1233 if ((Att=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
1237 _errorCode = "IOR is empty";
1243 void SALOMEDSImpl_Study::SetTransientReference(const std::string& theIOR)
1247 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1248 int aLocked = aProp->IsLocked();
1249 if (aLocked) aProp->SetLocked(false);
1251 // Assign the value of the IOR in the study->root
1252 SALOMEDSImpl_AttributeIOR::Set(_doc->Main().Root(), theIOR);
1254 if (aLocked) aProp->SetLocked(true);
1257 //============================================================================
1258 /*! Function : IsEmpty
1259 * Purpose : Detect if study is empty
1261 //============================================================================
1262 bool SALOMEDSImpl_Study::IsEmpty()
1265 if (!_doc) return true;
1266 return _doc->IsEmpty();
1269 //============================================================================
1270 /*! Function : FindComponent
1271 * Purpose : Find a Component with ComponentDataType = aComponentName
1273 //============================================================================
1274 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const std::string& aComponentName)
1279 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1280 SALOMEDSImpl_SComponent compo;
1282 for (; itcomp.More(); itcomp.Next()) {
1283 SALOMEDSImpl_SComponent SC = itcomp.Value();
1284 name = SC.ComponentDataType();
1285 if(aComponentName == name) {
1293 _errorCode = "No component was found";
1299 //============================================================================
1300 /*! Function : FindComponentID
1301 * Purpose : Find a Component from it's ID
1303 //============================================================================
1304 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const std::string& aComponentID)
1308 // Iterate on each components defined in the study
1309 // Get the component ID and compare with aComponentID
1312 SALOMEDSImpl_SComponent compo;
1314 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1315 for (; itcomp.More(); itcomp.Next()) {
1316 SALOMEDSImpl_SComponent SC = itcomp.Value();
1318 if(aComponentID == ID)
1320 // ComponentID found
1327 _errorCode = "No component was found";
1334 //============================================================================
1335 /*! Function : FindObject
1336 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1338 //============================================================================
1339 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const std::string& anObjectName)
1343 // Iterate to all components defined in the study
1344 // After testing the component name, iterate in all objects defined under
1345 // components (function _FindObject)
1347 SALOMEDSImpl_SObject RefSO;
1349 SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
1350 for (; it.More();it.Next()){
1353 SALOMEDSImpl_SComponent SC = it.Value();
1354 if (SC.GetName() == anObjectName)
1360 if (!_find) RefSO = _FindObject(SC, anObjectName, _find);
1363 if(!RefSO) _errorCode = "No object was found";
1367 //============================================================================
1368 /*! Function : FindObjectID
1369 * Purpose : Find an Object with ID = anObjectID
1371 //============================================================================
1372 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const std::string& anObjectID)
1375 SALOMEDSImpl_SObject so;
1377 // Convert aSO->GetID in DF_Label.
1378 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
1381 _errorCode = "No label was found by ID";
1384 return GetSObject(Lab);
1388 //============================================================================
1389 /*! Function : CreateObjectID
1390 * Purpose : Creates an Object with ID = anObjectID
1392 //============================================================================
1393 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const std::string& anObjectID)
1396 SALOMEDSImpl_SObject so;
1398 // Convert aSO->GetID in DF_Label.
1399 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
1402 _errorCode = "Can not create a label";
1405 return GetSObject(Lab);
1409 //============================================================================
1410 /*! Function : FindObjectByName
1411 * Purpose : Find Objects with SALOMEDSImpl_Name = anObjectName in a Component
1412 * : with ComponentDataType = aComponentName
1414 //============================================================================
1415 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const std::string& anObjectName,
1416 const std::string& aComponentName)
1420 std::vector<SALOMEDSImpl_SObject> listSO;
1422 SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
1424 _errorCode = "Can not find the component";
1428 // Iterate on each object and subobject of the component
1429 // If objectName is found add it to the list of SObjects
1430 std::string childName ;
1432 std::string compoId = compo.GetID();
1433 SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
1434 for ( ; it.More(); it.Next() ) {
1436 SALOMEDSImpl_SObject CSO = it.Value();
1437 if ( CSO.GetName() == anObjectName ) {
1439 listSO.push_back(CSO) ;
1442 /* looks also for eventual children */
1443 bool found = false ;
1444 CSO = _FindObject( CSO, anObjectName, found ) ;
1446 listSO.push_back(CSO) ;
1455 //============================================================================
1456 /*! Function : FindObjectIOR
1457 * Purpose : Find an Object with IOR = anObjectIOR
1459 //============================================================================
1460 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const std::string& anObjectIOR)
1464 SALOMEDSImpl_SObject aResult ;
1466 // searching in the datamap for optimization
1467 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anObjectIOR);
1468 if (it != myIORLabels.end()) {
1469 aResult = GetSObject(it->second);
1470 // 11 oct 2002: forbidden attributes must be checked here
1471 if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
1472 myIORLabels.erase(anObjectIOR);
1473 aResult = SALOMEDSImpl_SObject();
1477 if(!aResult) _errorCode = "No object was found";
1481 //============================================================================
1482 /*! Function : FindObjectByPath
1483 * Purpose : Find an Object by its path = thePath
1485 //============================================================================
1486 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const std::string& thePath)
1490 std::string aPath(thePath), aToken;
1491 SALOMEDSImpl_SObject aSO;
1492 int aLength = aPath.size();
1493 bool isRelative = false;
1495 if(aLength == 0) { //Empty path - return the current context
1496 return GetSObject(_current);
1499 if(aPath[0] != '/') //Relative path
1502 DF_ChildIterator anIterator;
1504 SALOMEDSImpl_AttributeName* anAttr;
1507 if(_current.IsNull()) return aSO;
1508 anIterator.Init(_current, false);
1511 if(aPath.size() == 1 && aPath[0] == '/') { //Root
1512 return GetSObject(_doc->Main());
1514 anIterator.Init(_doc->Main(), false);
1517 std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
1518 for(int i = 0, len = vs.size(); i<len; i++) {
1521 if(aToken.size() == 0) break;
1523 for ( ; anIterator.More(); anIterator.Next() ) {
1524 aLabel = anIterator.Value();
1525 if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1526 if(anAttr->Value() == aToken) {
1527 if(i == (len-1)) { //The searched label is found (no part of the path is left)
1528 return GetSObject(aLabel);
1531 anIterator.Init(aLabel, false);
1539 if(!aSO) _errorCode = "No object was found";
1543 //============================================================================
1544 /*! Function : GetObjectPath
1547 //============================================================================
1548 std::string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
1552 std::string aPath("");
1554 _errorCode = "Null object";
1558 std::string aName = theObject.GetName();
1559 if(!aName.empty() && aName != "" ) {
1560 std::string aValue("/");
1564 SALOMEDSImpl_SObject aFather = theObject.GetFather();
1566 aName = aFather.GetName();
1567 if(!aName.empty() && aName != "") {
1568 aValue = GetObjectPath(aFather);
1569 aPath = aValue + aPath;
1578 //============================================================================
1579 /*! Function : GetObjectPathByIOR
1582 //============================================================================
1583 std::string SALOMEDSImpl_Study::GetObjectPathByIOR(const std::string& theIOR)
1588 SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
1590 _errorCode = "No SObject was found by IOR";
1594 return GetObjectPath(so);
1598 //============================================================================
1599 /*! Function : SetContext
1600 * Purpose : Sets the current context
1602 //============================================================================
1603 bool SALOMEDSImpl_Study::SetContext(const std::string& thePath)
1606 if(thePath.empty()) {
1607 _errorCode = "InvalidPath";
1611 std::string aPath(thePath), aContext("");
1612 bool isInvalid = false;
1613 SALOMEDSImpl_SObject aSO;
1615 if(aPath[0] != '/') { //Relative path
1616 aContext = GetContext();
1624 aSO = FindObjectByPath(aContext);
1630 if(isInvalid || !aSO) {
1631 _errorCode = "InvalidContext";
1635 DF_Label aLabel = aSO.GetLabel();
1636 if(aLabel.IsNull()) {
1637 _errorCode = "InvalidContext";
1641 _current = aLabel; //Set the current context
1646 //============================================================================
1647 /*! Function : GetContext
1648 * Purpose : Gets the current context
1650 //============================================================================
1651 std::string SALOMEDSImpl_Study::GetContext()
1655 if(_current.IsNull()) {
1656 _errorCode = "InvaidContext";
1659 SALOMEDSImpl_SObject so = GetSObject(_current);
1660 return GetObjectPath(so);
1663 //============================================================================
1664 /*! Function : GetObjectNames
1665 * Purpose : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
1667 //============================================================================
1668 std::vector<std::string> SALOMEDSImpl_Study::GetObjectNames(const std::string& theContext)
1672 std::vector<std::string> aResultSeq;
1674 if (theContext.empty()) {
1677 DF_Label aTmp = _current;
1678 SetContext(theContext);
1682 if (aLabel.IsNull()) {
1683 _errorCode = "InvalidContext";
1687 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
1688 for (; anIter.More(); anIter.Next()) {
1689 DF_Label aLabel = anIter.Value();
1690 SALOMEDSImpl_AttributeName* aName;
1691 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1692 aResultSeq.push_back(aName->Value());
1698 //============================================================================
1699 /*! Function : GetDirectoryNames
1700 * Purpose : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
1702 //============================================================================
1703 std::vector<std::string> SALOMEDSImpl_Study::GetDirectoryNames(const std::string& theContext)
1707 std::vector<std::string> aResultSeq;
1709 if (theContext.empty()) {
1712 DF_Label aTmp = _current;
1713 SetContext(theContext);
1717 if (aLabel.IsNull()) {
1718 _errorCode = "InvalidContext";
1722 DF_ChildIterator anIter (aLabel, true); // iterate first-level children at all sublevels
1723 for (; anIter.More(); anIter.Next()) {
1724 DF_Label aLabel = anIter.Value();
1725 SALOMEDSImpl_AttributeLocalID* anID;
1726 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
1727 if (anID->Value() == DIRECTORYID) {
1728 SALOMEDSImpl_AttributeName* aName;
1729 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1730 aResultSeq.push_back(aName->Value());
1739 //============================================================================
1740 /*! Function : GetFileNames
1741 * Purpose : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
1743 //============================================================================
1744 std::vector<std::string> SALOMEDSImpl_Study::GetFileNames(const std::string& theContext)
1748 std::vector<std::string> aResultSeq;
1750 if (theContext.empty()) {
1753 DF_Label aTmp = _current;
1754 SetContext(theContext);
1758 if (aLabel.IsNull()) {
1759 _errorCode = "InvalidContext";
1763 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
1764 for (; anIter.More(); anIter.Next()) {
1765 DF_Label aLabel = anIter.Value();
1766 SALOMEDSImpl_AttributeLocalID* anID;
1767 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
1768 if (anID->Value() == FILELOCALID) {
1769 SALOMEDSImpl_AttributePersistentRef* aName;
1770 if ((aName=(SALOMEDSImpl_AttributePersistentRef*)aLabel.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
1771 std::string aFileName = aName->Value();
1772 if (aFileName.size() > 0)
1773 aResultSeq.push_back(aFileName.substr(strlen(FILEID), aFileName.size()));
1782 //============================================================================
1783 /*! Function : GetComponentNames
1784 * Purpose : method to get all components names
1786 //============================================================================
1787 std::vector<std::string> SALOMEDSImpl_Study::GetComponentNames(const std::string& theContext)
1791 std::vector<std::string> aResultSeq;
1792 DF_ChildIterator anIter(_doc->Main(), false); // iterate all subchildren at first level
1793 for(; anIter.More(); anIter.Next()) {
1794 DF_Label aLabel = anIter.Value();
1795 SALOMEDSImpl_AttributeName* aName;
1796 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1797 aResultSeq.push_back(aName->Value());
1803 //============================================================================
1804 /*! Function : NewChildIterator
1805 * Purpose : Create a ChildIterator from an SObject
1807 //============================================================================
1808 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
1811 return SALOMEDSImpl_ChildIterator(aSO);
1815 //============================================================================
1816 /*! Function : NewComponentIterator
1817 * Purpose : Create a SComponentIterator
1819 //============================================================================
1820 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
1823 return SALOMEDSImpl_SComponentIterator(_doc);
1827 //============================================================================
1828 /*! Function : NewBuilder
1829 * Purpose : Create a StudyBuilder
1831 //============================================================================
1832 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
1836 _builder->SetOnAddSObject(_cb);
1837 _builder->SetOnRemoveSObject(_cb);
1843 //============================================================================
1845 * Purpose : get study name
1847 //============================================================================
1848 std::string SALOMEDSImpl_Study::Name()
1851 return Kernel_Utils::GetBaseName( _name, false );
1854 //============================================================================
1855 /*! Function : IsSaved
1856 * Purpose : get if study has been saved
1858 //============================================================================
1859 bool SALOMEDSImpl_Study::IsSaved()
1865 //============================================================================
1866 /*! Function : IsSaved
1867 * Purpose : set if study has been saved
1869 //============================================================================
1870 void SALOMEDSImpl_Study::IsSaved(bool save)
1874 if(save) _doc->SetModified(false);
1877 //============================================================================
1878 /*! Function : IsModified
1879 * Purpose : Detect if a Study has been modified since it has been saved
1881 //============================================================================
1882 bool SALOMEDSImpl_Study::IsModified()
1886 // True if is modified
1887 if (_doc && _doc->IsModified()) return true;
1892 //============================================================================
1894 * Purpose : get URL of the study (persistent reference of the study)
1896 //============================================================================
1897 std::string SALOMEDSImpl_Study::URL()
1901 return Kernel_Utils::AddExtension( _name );
1905 //============================================================================
1907 * Purpose : set URL of the study (persistent reference of the study)
1909 //============================================================================
1910 void SALOMEDSImpl_Study::URL(const std::string& url)
1916 /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
1917 std::string tmp(_URL);
1919 char *aName = (char*)tmp.ToCString();
1920 char *adr = strtok(aName, "/");
1924 adr = strtok(NULL, "/");
1930 //============================================================================
1931 /*! Function : _FindObject
1932 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1934 //============================================================================
1935 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
1936 const std::string& theObjectName,
1939 SALOMEDSImpl_SObject RefSO;
1940 if(!SO) return RefSO;
1942 // Iterate on each objects and subobjects of the component
1943 // If objectName find, stop the loop and get the object reference
1944 SALOMEDSImpl_AttributeName* anAttr;
1946 std::string soid = SO.GetID();
1947 DF_ChildIterator it(SO.GetLabel());
1948 for (; it.More(); it.Next()){
1951 if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1953 std::string Val(anAttr->Value());
1954 if (Val == theObjectName)
1956 RefSO = GetSObject(it.Value());
1960 if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
1966 //============================================================================
1967 /*! Function : _FindObjectIOR
1968 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
1970 //============================================================================
1971 SALOMEDSImpl_SObject
1972 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
1973 const std::string& theObjectIOR,
1976 SALOMEDSImpl_SObject RefSO, aSO;
1977 if(!SO) return RefSO;
1979 // Iterate on each objects and subobjects of the component
1980 // If objectName find, stop the loop and get the object reference
1981 SALOMEDSImpl_AttributeIOR* anAttr;
1983 DF_ChildIterator it(SO.GetLabel());
1984 for (; it.More();it.Next()){
1987 if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
1989 std::string Val(anAttr->Value());
1990 if (Val == theObjectIOR)
1992 RefSO = GetSObject(it.Value());
1996 aSO = GetSObject(it.Value());
1997 if (!_find) RefSO = _FindObjectIOR(aSO, theObjectIOR, _find);
2003 //============================================================================
2004 /*! Function : _GetNoteBookAccessor
2005 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
2007 //============================================================================
2008 std::string SALOMEDSImpl_Study::_GetNoteBookAccessor(){
2009 return std::string("notebook");
2012 //============================================================================
2013 /*! Function : _GetStudyVariablesScript
2016 //============================================================================
2017 std::string SALOMEDSImpl_Study::_GetStudyVariablesScript()
2019 std::string dump("");
2021 if(myNoteBookVars.empty())
2024 Kernel_Utils::Localizer loc;
2026 dump += "####################################################\n";
2027 dump += "## Begin of NoteBook variables section ##\n";
2028 dump += "####################################################\n";
2030 std::string set_method = _GetNoteBookAccessor()+".set(";
2031 std::string varName;
2032 std::string varValue;
2033 for(int i = 0 ; i < myNoteBookVars.size();i++ ) {
2034 varName = myNoteBookVars[i]->Name();
2035 varValue = myNoteBookVars[i]->SaveToScript();
2036 dump+=set_method+"\""+varName+"\", "+varValue+")\n";
2039 dump += "####################################################\n";
2040 dump += "## End of NoteBook variables section ##\n";
2041 dump += "####################################################\n";
2046 //============================================================================
2047 /*! Function : _GetNoteBookAccess
2050 //============================================================================
2051 std::string SALOMEDSImpl_Study::_GetNoteBookAccess(const std::string& theStudyVar)
2053 std::string notebook = "import salome_notebook\n";
2054 notebook += _GetNoteBookAccessor() + " = salome_notebook.NoteBook(" + theStudyVar + ")" ;
2058 bool SALOMEDSImpl_Study::IsLocked()
2061 return GetProperties()->IsLocked();
2064 void SALOMEDSImpl_Study::UpdateIORLabelMap(const std::string& anIOR,const std::string& anEntry)
2067 DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
2068 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
2069 if (it != myIORLabels.end()) myIORLabels.erase(it);
2070 myIORLabels[anIOR] = aLabel;
2073 void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR)
2075 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
2076 if (it != myIORLabels.end())
2078 //remove the ior entry and decref the genericobj (if it's one)
2079 myIORLabels.erase(it);
2083 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel)
2085 SALOMEDSImpl_StudyHandle* Att;
2086 if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
2092 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
2094 return GetStudy(theLabel)->GetSObject(theLabel);
2097 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
2099 return GetStudy(theLabel)->GetSComponent(theLabel);
2103 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
2105 std::string aString = theAttribute->Label().Entry();
2106 GetStudy(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
2109 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
2112 std::vector<SALOMEDSImpl_SObject> aSeq;
2114 SALOMEDSImpl_AttributeTarget* aTarget;
2115 if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
2116 return aTarget->Get();
2123 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
2126 return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
2129 std::string SALOMEDSImpl_Study::GetLastModificationDate()
2132 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
2134 std::vector<std::string> aNames;
2135 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
2136 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
2138 int aLastIndex = aNames.size()-1;
2140 sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
2141 (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
2142 (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
2143 std::string aResStr (aResult);
2147 std::vector<std::string> SALOMEDSImpl_Study::GetModificationsDate()
2150 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
2152 std::vector<std::string> aNames;
2153 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
2154 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
2156 int anIndex, aLength = aNames.size();
2157 std::vector<std::string> aDates;
2159 for (anIndex = 1; anIndex < aLength; anIndex++) {
2161 sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
2162 (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
2163 (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
2164 aDates.push_back(aDate);
2171 //============================================================================
2172 /*! Function : GetUseCaseBuilder
2173 * Purpose : Returns a UseCase builder
2175 //============================================================================
2176 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
2179 return _useCaseBuilder;
2182 //============================================================================
2183 /*! Function : GetSComponent
2186 //============================================================================
2187 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const std::string& theEntry)
2189 SALOMEDSImpl_SComponent aSCO;
2190 if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
2191 aSCO = _mapOfSCO[theEntry];
2193 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
2194 aSCO = SALOMEDSImpl_SComponent(aLabel);
2195 _mapOfSCO[theEntry] = aSCO;
2201 //============================================================================
2202 /*! Function : GetSComponent
2205 //============================================================================
2206 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
2208 return SALOMEDSImpl_SComponent(theLabel);
2211 //============================================================================
2212 /*! Function : GetSObject
2215 //============================================================================
2216 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const std::string& theEntry)
2218 SALOMEDSImpl_SObject aSO;
2219 std::map<std::string, SALOMEDSImpl_SObject>::iterator it=_mapOfSO.find(theEntry);
2220 if(it != _mapOfSO.end())
2223 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
2224 aSO = SALOMEDSImpl_SObject(aLabel);
2225 _mapOfSO[theEntry] = aSO;
2231 //============================================================================
2232 /*! Function : GetSObject
2235 //============================================================================
2236 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
2238 return SALOMEDSImpl_SObject(theLabel);
2241 //============================================================================
2242 /*! Function : GetAttribute
2245 //============================================================================
2246 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const std::string& theEntry,
2247 const std::string& theType)
2249 SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
2250 DF_Attribute* anAttr;
2251 aSO.FindAttribute(anAttr, theType);
2255 //! number of spaces for indentation in Python dump files (to replace \t symbols)
2256 static const int indent_size = 2;
2258 static std::string replace_tabs( const std::string& in )
2260 std::string out = in;
2262 size_t pos = out.find( '\t' );
2263 while ( pos != std::string::npos ) {
2264 out.replace( pos, 1, indent_size, ' ' );
2265 pos = out.find( '\t' );
2271 static std::string GetComponentHeader(const char* theComponentName)
2273 std::stringstream txt;
2274 txt << "###" << std::endl;
2275 txt << "### " << theComponentName << " component" << std::endl;
2276 txt << "###" << std::endl;
2280 //============================================================================
2281 /*! Function : DumpStudy
2284 //============================================================================
2285 bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
2286 const std::string& theBaseName,
2289 SALOMEDSImpl_DriverFactory* theFactory)
2293 if(theFactory == NULL) {
2294 _errorCode = "Null factory for creation of Engines";
2298 std::vector<std::string> aSeq;
2299 std::string aCompType, aFactoryType;
2301 //Build a list of all components in the Study
2302 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
2304 for (; itcomponent.More(); itcomponent.Next()) {
2305 SALOMEDSImpl_SComponent sco = itcomponent.Value();
2306 aCompType = sco.ComponentDataType();
2307 if (aCompType == "GEOM")
2308 aSeq.insert(aSeq.begin(), aCompType);
2310 aSeq.push_back(aCompType);
2312 // re-arrange modules in the sequence, if specific order is given via SALOME_MODULES_ORDER environment variable.
2313 if ( getenv("SALOME_MODULES_ORDER") != 0 ) {
2314 std::string order = getenv("SALOME_MODULES_ORDER");
2315 std::vector<std::string> mlist;
2316 while ( !order.empty() ) {
2317 size_t idx = order.find( "," );
2318 std::string m = order.substr(0, idx);
2319 order = order.substr( ( idx == std::string::npos ) ? order.size() : idx+1 );
2320 if ( m.empty() || std::find( mlist.begin(), mlist.end(), m ) != mlist.end() ) continue;
2321 mlist.push_back( m );
2324 for ( std::vector<std::string>::reverse_iterator mit = mlist.rbegin(); mit != mlist.rend(); ++mit ) {
2325 std::vector<std::string>::iterator it = std::find( aSeq.begin(), aSeq.end(), *mit );
2326 if ( it != aSeq.end() ) {
2328 aSeq.insert( aSeq.begin(), *mit );
2334 std::string aFileName =
2335 thePath + std::string("\\") + theBaseName + std::string(".py");
2337 std::string aFileName =
2338 thePath + std::string("/") + theBaseName + std::string(".py");
2341 //Create a file that will contain a main Study script
2343 fp.open(aFileName.c_str(), std::ios::out);
2346 bool isOpened = fp.is_open();
2348 bool isOpened = fp.rdbuf()->is_open();
2352 _errorCode = std::string("Can't create a file ")+aFileName;
2356 std::stringstream sfp;
2358 std::string aBatchModeScript = "salome";
2360 //Output to the main Study script required Python modules import,
2361 //set sys.path and add a creation of the study.
2364 sfp << GetDumpStudyComment() << std::endl;
2367 sfp << "import sys" << std::endl;
2368 sfp << "import " << aBatchModeScript << std::endl << std::endl;
2370 std::string aStudyVar = "salome.myStudy";
2371 // initialization function
2372 sfp << aBatchModeScript << ".salome_init()" << std::endl;
2373 if ( !isMultiFile ) {
2374 sfp << "theStudy = " << aStudyVar << std::endl << std::endl;
2375 aStudyVar = "theStudy";
2377 // notebook initialization
2378 sfp << _GetNoteBookAccess(aStudyVar) << std::endl;
2380 // extend sys.path with the directory where the script is being dumped to
2381 sfp << "sys.path.insert( 0, r\'" << thePath << "\')" << std::endl << std::endl;
2383 // dump NoteBook variables
2384 sfp << _GetStudyVariablesScript();
2386 // dump visual parameters if necessary
2387 bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
2388 int lastSavePoint = -1;
2390 lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
2391 if(lastSavePoint > 0) {
2392 sfp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << std::endl << std::endl;
2396 std::vector<std::string> aSeqOfFileNames;
2398 // dump all components and create the components specific scripts
2400 int aLength = aSeq.size();
2401 for(int i = 1; i <= aLength; i++) {
2403 aCompType = aSeq[i-1];
2404 SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
2405 SALOMEDSImpl_Driver* aDriver = NULL;
2406 // if there is an associated Engine call its method for saving
2407 std::string IOREngine;
2409 if (!sco.ComponentIOR(IOREngine)) {
2410 if (!aCompType.empty()) {
2412 aDriver = theFactory->GetDriverByType(aCompType);
2414 if (aDriver != NULL) {
2415 SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
2416 if(!SB->LoadWith(sco, aDriver)) {
2417 _errorCode = SB->GetErrorCode();
2425 aDriver = theFactory->GetDriverByIOR(IOREngine);
2428 _errorCode = "Can not restore information to dump it";
2432 if(aDriver == NULL) continue;
2435 long aStreamLength = 0;
2436 SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(isPublished, isMultiFile, isValidScript, aStreamLength);
2437 if ( !isValidScript )
2440 std::stringstream sfp2;
2442 //Output the Python script generated by the component in the newly created file.
2444 sfp2 << GetDumpStudyComment( aCompType.c_str() ) << std::endl;
2446 sfp2 << GetComponentHeader( aCompType.c_str() ) << std::endl;
2447 sfp2 << aStream->Data();
2449 if ( isMultiFile ) {
2450 //Create a file that will contain the component specific script
2453 aFileName=thePath+std::string("\\");
2455 aFileName=thePath+std::string("/");
2457 std::string aScriptName;
2458 aScriptName += theBaseName;
2460 aScriptName += aCompType;
2462 aFileName += aScriptName+ std::string(".py");
2463 aSeqOfFileNames.push_back(aFileName);
2465 fp2.open(aFileName.c_str(), std::ios::out);
2468 isOpened = fp2.is_open();
2470 isOpened = fp2.rdbuf()->is_open();
2474 _errorCode = std::string("Can't create a file ")+aFileName;
2475 SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
2479 // replace '\t' symbols
2480 fp2 << replace_tabs( sfp2.str() );
2484 //Add to the main script a call to RebuildData of the generated by the component the Python script
2485 sfp << "import " << aScriptName << std::endl;
2486 sfp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << std::endl;
2491 if(aStream) delete aStream;
2495 sfp << "if salome.sg.hasDesktop():" << std::endl;
2496 sfp << "\tsalome.sg.updateObjBrowser()" << std::endl;
2498 if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
2499 sfp << "\tiparameters.getSession().restoreVisualState(1)" << std::endl;
2502 // replace '\t' symbols
2503 fp << replace_tabs( sfp.str() );
2510 //=======================================================================
2511 //function : GetDumpStudyComment
2512 //purpose : return a header comment for a DumpStudy script
2513 //=======================================================================
2515 std::string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
2517 std::stringstream txt;
2518 txt << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
2519 txt << "###" << std::endl;
2520 txt << "### This file is generated automatically by SALOME v"
2521 << KERNEL_VERSION_STR
2522 << " with dump python functionality";
2523 if ( theComponentName )
2524 txt << " (" << theComponentName << " component)";
2526 txt << "###" << std::endl;
2530 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2532 const std::string& Tab,
2533 SALOMEDSImpl_Study* theStudy);
2535 //============================================================================
2539 //============================================================================
2540 void SALOMEDSImpl_Study::dump(const std::string& theFileName)
2542 //Create a file that will contain a main Study script
2544 fp.open(theFileName.c_str(), std::ios::out);
2547 bool isOpened = fp.is_open();
2549 bool isOpened = fp.rdbuf()->is_open();
2553 _errorCode = std::string("Can't create a file ")+theFileName;
2554 std::cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << std::endl;
2558 SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
2559 fp << "0:1" << std::endl;
2560 SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
2561 std::string aTab(" ");
2562 for(; Itr.More(); Itr.Next()) {
2563 dumpSO(Itr.Value(), fp, aTab, this);
2570 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2572 const std::string& Tab,
2573 SALOMEDSImpl_Study* theStudy)
2575 std::string aTab(Tab), anID(theSO.GetID());
2576 fp << aTab << anID << std::endl;
2577 std::vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
2578 for(int i = 0; i<attribs.size(); i++) {
2579 SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
2585 std::string aType = anAttr->GetClassType();
2586 fp << Tab << " -- " << aType;
2588 if(aType == std::string("AttributeReal")) {
2589 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
2591 else if(aType == std::string("AttributeInteger")) {
2592 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
2594 else if(aType == std::string("AttributeName")) {
2595 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
2597 else if(aType == std::string("AttributeComment")) {
2598 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
2600 else if(aType == std::string("AttributeReference")) {
2601 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
2606 SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
2607 std::string aNewTab(" ");
2609 for(; Itr.More(); Itr.Next()) {
2610 dumpSO(Itr.Value(), fp, aNewTab, theStudy);
2616 void SALOMEDSImpl_Study::Modify()
2619 _doc->SetModified(true);
2622 //============================================================================
2626 //============================================================================
2627 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
2629 if (theSavePoint < -1) return NULL;
2630 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2631 SALOMEDSImpl_SObject so = FindComponent((char*)theID);
2632 if (!so) so = builder->NewComponent((char*)theID);
2633 SALOMEDSImpl_AttributeParameter* attParam = NULL;
2635 if (theSavePoint == -1) {
2637 DF_Label savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2638 DF_Label prevPointLabel;
2639 while ( !savePointLabel.IsNull() ) {
2641 prevPointLabel = savePointLabel;
2642 savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2644 if ( !prevPointLabel.IsNull() )
2645 so = GetSObject( prevPointLabel );
2647 if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
2648 DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
2649 if ( !savePointLabel.IsNull() )
2650 so = GetSObject( savePointLabel );
2651 else // ... if it does not exist - create a new one
2652 so = builder->NewObjectToTag( so, theSavePoint );
2657 builder->FindAttribute(so, A, "AttributeParameter");
2658 if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
2659 A = builder->FindOrCreateAttribute(so, "AttributeParameter");
2661 attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
2666 //============================================================================
2670 //============================================================================
2671 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID,
2672 const char* theModuleName,
2675 if(theSavePoint < -1) return NULL;
2676 SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
2677 SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
2678 SALOMEDSImpl_AttributeParameter* par = NULL;
2680 SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
2681 std::string moduleName(theModuleName);
2682 for(; it.More(); it.Next()) {
2683 SALOMEDSImpl_SObject so(it.Value());
2684 if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
2685 if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
2686 if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
2690 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2691 SALOMEDSImpl_SObject so = builder->NewObject(main_so);
2692 par = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
2693 par->SetString("AP_MODULE_NAME", moduleName);
2697 //============================================================================
2698 /*! Function : SetStudyLock
2701 //============================================================================
2702 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
2704 _lockers.push_back(theLockerID);
2707 //============================================================================
2708 /*! Function : IsStudyLocked
2711 //============================================================================
2712 bool SALOMEDSImpl_Study::IsStudyLocked()
2714 return (_lockers.size() > 0);
2717 //============================================================================
2718 /*! Function : UnLockStudy
2721 //============================================================================
2722 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
2724 std::vector<std::string>::iterator vsI = _lockers.begin();
2725 int length = _lockers.size();
2726 bool isFound = false;
2727 std::string id(theLockerID);
2728 for(int i = 0; i<length; i++, vsI++) {
2729 if(id == _lockers[i]) {
2734 if(isFound) _lockers.erase(vsI);
2737 //============================================================================
2738 /*! Function : GetLockerID
2741 //============================================================================
2742 std::vector<std::string> SALOMEDSImpl_Study::GetLockerID()
2747 //============================================================================
2748 /*! Function : SetVariable
2751 //============================================================================
2752 void SALOMEDSImpl_Study::SetVariable(const std::string& theVarName,
2753 const double theValue,
2754 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2756 bool modified = false;
2757 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2759 if( aGVar == NULL ) {
2761 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2763 aSVar->setValue(theValue);
2764 myNoteBookVars.push_back(aSVar);
2768 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2769 modified = aSVar->setValue(theValue) || modified;
2770 modified = aSVar->setType(theType) || modified;
2777 //============================================================================
2778 /*! Function : SetStringVariable
2781 //============================================================================
2782 void SALOMEDSImpl_Study::SetStringVariable(const std::string& theVarName,
2783 const std::string& theValue,
2784 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2786 bool modified = false;
2787 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2789 if( aGVar == NULL ) {
2791 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2793 aSVar->setStringValue(theValue);
2794 myNoteBookVars.push_back(aSVar);
2798 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2799 modified = aSVar->setStringValue(theValue) || modified;
2800 modified = aSVar->setType(theType) || modified;
2807 //============================================================================
2808 /*! Function : SetStringVariableAsDouble
2811 //============================================================================
2812 void SALOMEDSImpl_Study::SetStringVariableAsDouble(const std::string& theVarName,
2813 const double theValue,
2814 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2816 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2817 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2818 aSVar->setValue(theValue);
2821 //============================================================================
2822 /*! Function : GetReal
2825 //============================================================================
2826 double SALOMEDSImpl_Study::GetVariableValue(const std::string& theVarName)
2828 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2831 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2832 return aSVar->getValue();
2837 //============================================================================
2838 /*! Function : GetString
2841 //============================================================================
2842 std::string SALOMEDSImpl_Study::GetStringVariableValue(const std::string& theVarName)
2844 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2847 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2848 return aSVar->getStringValue();
2853 //============================================================================
2854 /*! Function : IsTypeOf
2857 //============================================================================
2858 bool SALOMEDSImpl_Study::IsTypeOf(const std::string& theVarName,
2859 SALOMEDSImpl_GenericVariable::
2860 VariableTypes theType) const
2862 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2865 return aGVar->Type() == theType;
2870 //============================================================================
2871 /*! Function : IsVariable
2874 //============================================================================
2875 bool SALOMEDSImpl_Study::IsVariable(const std::string& theVarName) const
2877 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2878 return (aGVar != NULL);
2881 //============================================================================
2882 /*! Function : GetVariableNames
2885 //============================================================================
2886 std::vector<std::string> SALOMEDSImpl_Study::GetVariableNames() const
2888 std::vector<std::string> aResult;
2890 for(int i = 0; i < myNoteBookVars.size(); i++)
2891 aResult.push_back(myNoteBookVars[i]->Name());
2896 //============================================================================
2897 /*! Function : AddVariable
2900 //============================================================================
2901 void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
2903 myNoteBookVars.push_back(theVariable);
2906 //============================================================================
2907 /*! Function : AddVariable
2910 //============================================================================
2911 SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
2913 SALOMEDSImpl_GenericVariable* aResult = NULL;
2914 for(int i = 0; i < myNoteBookVars.size();i++) {
2915 if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
2916 aResult = myNoteBookVars[i];
2923 //============================================================================
2924 /*! Function : RemoveVariable
2927 //============================================================================
2928 bool SALOMEDSImpl_Study::RemoveVariable(const std::string& theVarName)
2930 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2934 std::string aValue = aVariable->SaveToScript();
2935 ReplaceVariableAttribute( theVarName, aValue );
2937 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2938 for( ; it != itEnd; it++ )
2940 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2941 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2943 myNoteBookVars.erase( it );
2952 //============================================================================
2953 /*! Function : RenameVariable
2956 //============================================================================
2957 bool SALOMEDSImpl_Study::RenameVariable(const std::string& theVarName, const std::string& theNewVarName)
2959 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2963 ReplaceVariableAttribute( theVarName, theNewVarName );
2965 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2966 for( ; it != itEnd; it++ )
2968 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2969 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2971 aVariableRef->setName( theNewVarName );
2980 //============================================================================
2981 /*! Function : IsVariableUsed
2984 //============================================================================
2985 bool SALOMEDSImpl_Study::IsVariableUsed(const std::string& theVarName)
2987 return FindVariableAttribute( theVarName );
2990 //============================================================================
2991 /*! Function : FindVariableAttribute
2994 //============================================================================
2995 bool SALOMEDSImpl_Study::FindVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
2996 SALOMEDSImpl_SObject theSObject,
2997 const std::string& theName)
2999 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
3000 for( ; anIter.More(); anIter.Next() )
3001 if( FindVariableAttribute( theStudyBuilder, anIter.Value(), theName ) )
3004 DF_Attribute* anAttr;
3005 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
3007 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
3009 std::string aString = aStringAttr->Value();
3011 std::vector< std::vector<std::string> > aSections = ParseVariables( aString );
3012 for( int i = 0, n = aSections.size(); i < n; i++ )
3014 std::vector<std::string> aVector = aSections[i];
3015 for( int j = 0, m = aVector.size(); j < m; j++ )
3017 std::string aStr = aVector[j];
3018 if( aStr.compare( theName ) == 0 )
3027 //============================================================================
3028 /*! Function : FindVariableAttribute
3031 //============================================================================
3032 bool SALOMEDSImpl_Study::FindVariableAttribute(const std::string& theName)
3034 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
3035 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
3036 for( ; aCompIter.More(); aCompIter.Next() )
3038 SALOMEDSImpl_SObject aComp = aCompIter.Value();
3039 if( FindVariableAttribute( aStudyBuilder, aComp, theName ) )
3045 //============================================================================
3046 /*! Function : ReplaceVariableAttribute
3049 //============================================================================
3050 void SALOMEDSImpl_Study::ReplaceVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
3051 SALOMEDSImpl_SObject theSObject,
3052 const std::string& theSource,
3053 const std::string& theDest)
3055 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
3056 for( ; anIter.More(); anIter.Next() )
3057 ReplaceVariableAttribute( theStudyBuilder, anIter.Value(), theSource, theDest );
3059 DF_Attribute* anAttr;
3060 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
3062 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
3064 bool isChanged = false;
3065 std::string aNewString, aCurrentString = aStringAttr->Value();
3067 std::vector< std::vector<std::string> > aSections = ParseVariables( aCurrentString );
3068 for( int i = 0, n = aSections.size(); i < n; i++ )
3070 std::vector<std::string> aVector = aSections[i];
3071 for( int j = 0, m = aVector.size(); j < m; j++ )
3073 std::string aStr = aVector[j];
3074 if( aStr.compare( theSource ) == 0 )
3080 aNewString.append( aStr );
3082 aNewString.append( ":" );
3085 aNewString.append( "|" );
3089 aStringAttr->SetValue( aNewString );
3094 //============================================================================
3095 /*! Function : ReplaceVariableAttribute
3098 //============================================================================
3099 void SALOMEDSImpl_Study::ReplaceVariableAttribute(const std::string& theSource, const std::string& theDest)
3101 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
3102 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
3103 for( ; aCompIter.More(); aCompIter.Next() )
3105 SALOMEDSImpl_SObject aComp = aCompIter.Value();
3106 ReplaceVariableAttribute( aStudyBuilder, aComp, theSource, theDest );
3110 //============================================================================
3111 /*! Function : ParseVariables
3114 //============================================================================
3115 std::vector< std::vector< std::string > > SALOMEDSImpl_Study::ParseVariables(const std::string& theVariables) const
3117 return SALOMEDSImpl_Tool::splitStringWithEmpty( theVariables, OPERATION_SEPARATOR, VARIABLE_SEPARATOR );
3120 //============================================================================
3121 /*! Function : EnableUseCaseAutoFilling
3124 //============================================================================
3125 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
3127 _errorCode = ""; _autoFill = isEnabled;
3129 _builder->SetOnAddSObject(_cb);
3130 _builder->SetOnRemoveSObject(_cb);
3133 _builder->SetOnAddSObject(NULL);
3134 _builder->SetOnRemoveSObject(NULL);
3138 //============================================================================
3139 /*! Function : GetIORs
3142 //============================================================================
3143 std::vector<std::string> SALOMEDSImpl_Study::GetIORs()
3145 std::vector<std::string> anIORs;
3146 std::map<std::string, DF_Label>::const_iterator MI;
3147 for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
3148 anIORs.push_back(MI->first);
3153 //============================================================================
3154 /*! Function : addSO_Notification
3155 * Purpose : This function tells all the observers that a SO has been added
3157 //============================================================================
3158 bool SALOMEDSImpl_Study::addSO_Notification (const SALOMEDSImpl_SObject& theSObject)
3161 return _notifier->addSO_Notification(theSObject);
3166 //============================================================================
3167 /*! Function : removeSO_Notification
3168 * Purpose : This function tells all the observers that a SO has been removed
3170 //============================================================================
3171 bool SALOMEDSImpl_Study::removeSO_Notification (const SALOMEDSImpl_SObject& theSObject)
3174 return _notifier->removeSO_Notification(theSObject);
3179 //============================================================================
3180 /*! Function : modifySO_Notification
3181 * Purpose : This function tells all the observers that a SO has been modified and
3182 pass the mofification reason
3184 //============================================================================
3185 bool SALOMEDSImpl_Study::modifySO_Notification (const SALOMEDSImpl_SObject& theSObject, int reason)
3188 return _notifier->modifySO_Notification(theSObject, reason);
3193 //============================================================================
3194 /*! Function : setNotifier
3195 * Purpose : register a notifier
3197 //============================================================================
3198 void SALOMEDSImpl_Study::setNotifier(SALOMEDSImpl_AbstractCallback* notifier)
3203 //#######################################################################################################
3204 //# STATIC PRIVATE FUNCTIONS
3205 //#######################################################################################################
3207 //============================================================================
3208 /*! Function : SaveAttributes
3209 * Purpose : Save attributes for object
3211 //============================================================================
3212 static void SaveAttributes(const SALOMEDSImpl_SObject& aSO, HDFgroup *hdf_group_sobject)
3215 std::vector<DF_Attribute*> attrList = aSO.GetLabel().GetAttributes();
3216 DF_Attribute* anAttr = NULL;
3217 for(int i = 0, len = attrList.size(); i<len; i++) {
3218 anAttr = attrList[i];
3219 //The following attributes are not supposed to be written to the file
3220 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
3221 if(type == std::string("AttributeIOR")) continue; //IOR attribute is not saved
3222 std::string aSaveStr =anAttr->Save();
3223 //cout << "Saving: " << aSO.GetID() << " type: "<< type<<"|" << endl;
3224 size[0] = (hdf_int32) strlen(aSaveStr.c_str()) + 1;
3225 HDFdataset *hdf_dataset = new HDFdataset((char*)type.c_str(), hdf_group_sobject, HDF_STRING,size, 1);
3226 hdf_dataset->CreateOnDisk();
3227 hdf_dataset->WriteOnDisk((char*)aSaveStr.c_str());
3228 hdf_dataset->CloseOnDisk();
3229 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
3233 //===========================================================================
3234 //Function : ReadAttributes
3235 //===========================================================================
3236 static void ReadAttributes(SALOMEDSImpl_Study* theStudy,
3237 const SALOMEDSImpl_SObject& aSO,
3238 HDFdataset* hdf_dataset)
3240 hdf_dataset->OpenOnDisk();
3242 DF_Attribute* anAttr = NULL;
3243 char* current_string = new char[hdf_dataset->GetSize()+1];
3244 hdf_dataset->ReadFromDisk(current_string);
3245 //cout << "Reading attr type = " << hdf_dataset->GetName() << " SO = " << aSO.GetID() << endl;
3246 if (!strcmp(hdf_dataset->GetName(),"COMPONENTDATATYPE")) {
3247 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, "AttributeComment");
3249 else if (!strcmp(hdf_dataset->GetName(),"AttributeReference") ||
3250 !strcmp(hdf_dataset->GetName(),"Reference")) { // Old format maintainance
3251 theStudy->NewBuilder()->Addreference(aSO, theStudy->CreateObjectID(current_string));
3252 delete [] (current_string);
3253 hdf_dataset->CloseOnDisk();
3257 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, hdf_dataset->GetName());
3261 anAttr->Load(current_string);
3264 delete [] (current_string);
3265 hdf_dataset->CloseOnDisk();
3268 //============================================================================
3269 //Function : BuildlTree
3270 //============================================================================
3271 static void BuildTree (SALOMEDSImpl_Study* theStudy, HDFgroup* hdf_current_group)
3273 hdf_current_group->OpenOnDisk();
3274 SALOMEDSImpl_SObject aSO;
3275 char* Entry = hdf_current_group->GetName();
3276 if (strcmp(Entry,"STUDY_STRUCTURE") == 0) {
3277 aSO = theStudy->CreateObjectID("0:1");
3280 aSO = theStudy->CreateObjectID(Entry);
3283 char name[HDF_NAME_MAX_LEN+1];
3284 int nbsons = hdf_current_group->nInternalObjects();
3285 for (int i=0; i<nbsons; i++) {
3286 hdf_current_group->InternalObjectIndentify(i,name);
3287 if (strncmp(name, "INTERNAL_COMPLEX",16) == 0) continue;
3288 hdf_object_type type = hdf_current_group->InternalObjectType(name);
3290 if (type == HDF_DATASET) {
3291 HDFdataset* new_dataset = new HDFdataset(name,hdf_current_group);
3292 ReadAttributes(theStudy,aSO,new_dataset);
3293 new_dataset = 0; // will be deleted by father destructor
3295 else if (type == HDF_GROUP) {
3296 HDFgroup* new_group = new HDFgroup(name,hdf_current_group);
3297 BuildTree (theStudy, new_group);
3298 new_group = 0; // will be deleted by father destructor
3301 hdf_current_group->CloseOnDisk();
3305 //============================================================================
3306 //Function : Translate_IOR_to_persistentID
3307 //============================================================================
3308 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject& so,
3309 SALOMEDSImpl_Driver* engine,
3313 DF_ChildIterator itchild(so.GetLabel());
3314 std::string ior_string, persistent_string, curid;
3316 for (; itchild.More(); itchild.Next()) {
3317 SALOMEDSImpl_SObject current = SALOMEDSImpl_Study::SObject(itchild.Value());
3318 SALOMEDSImpl_AttributeIOR* IOR = NULL;
3319 if ((IOR=(SALOMEDSImpl_AttributeIOR*)current.GetLabel().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
3320 ior_string = IOR->Value();
3322 persistent_string = engine->IORToLocalPersistentID (current, ior_string, isMultiFile, isASCII);
3323 SALOMEDSImpl_AttributePersistentRef::Set(current.GetLabel(), persistent_string);
3325 Translate_IOR_to_persistentID (current, engine, isMultiFile, isASCII);
3329 void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup)
3334 HDFgroup* new_group =0;
3335 HDFdataset* new_dataset =0;
3337 char aVarName[HDF_NAME_MAX_LEN+1];
3338 char *currentVarType = 0;
3339 char *currentVarValue = 0;
3340 char *currentVarIndex = 0;
3342 //Open HDF group with notebook variables
3343 theGroup->OpenOnDisk();
3345 //Get Nb of variables
3346 int aNbVars = theGroup->nInternalObjects();
3348 std::map<int,SALOMEDSImpl_GenericVariable*> aVarsMap;
3350 for( int iVar=0;iVar < aNbVars;iVar++ ) {
3351 theGroup->InternalObjectIndentify(iVar,aVarName);
3352 hdf_object_type type = theGroup->InternalObjectType(aVarName);
3353 if(type == HDF_GROUP) {
3356 new_group = new HDFgroup(aVarName,theGroup);
3357 new_group->OpenOnDisk();
3360 new_dataset = new HDFdataset("VARIABLE_TYPE",new_group);
3361 new_dataset->OpenOnDisk();
3362 currentVarType = new char[new_dataset->GetSize()+1];
3363 new_dataset->ReadFromDisk(currentVarType);
3364 new_dataset->CloseOnDisk();
3365 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3368 if(new_group->ExistInternalObject("VARIABLE_INDEX")) {
3369 new_dataset = new HDFdataset("VARIABLE_INDEX",new_group);
3370 new_dataset->OpenOnDisk();
3371 currentVarIndex = new char[new_dataset->GetSize()+1];
3372 new_dataset->ReadFromDisk(currentVarIndex);
3373 new_dataset->CloseOnDisk();
3374 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3375 order = atoi(currentVarIndex);
3376 delete [] currentVarIndex;
3382 new_dataset = new HDFdataset("VARIABLE_VALUE",new_group);
3383 new_dataset->OpenOnDisk();
3384 currentVarValue = new char[new_dataset->GetSize()+1];
3385 new_dataset->ReadFromDisk(currentVarValue);
3386 new_dataset->CloseOnDisk();
3387 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3389 new_group->CloseOnDisk();
3390 new_group = 0; //will be deleted by hdf_sco_group destructor
3392 SALOMEDSImpl_GenericVariable::VariableTypes aVarType =
3393 SALOMEDSImpl_GenericVariable::String2VariableType(std::string(currentVarType));
3394 delete [] currentVarType;
3396 //Create variable and add it in the study
3397 SALOMEDSImpl_GenericVariable* aVariable =
3398 new SALOMEDSImpl_ScalarVariable(aVarType,std::string(aVarName));
3399 aVariable->Load(std::string(currentVarValue));
3400 aVarsMap.insert(std::make_pair(order,aVariable));
3401 delete [] currentVarValue;
3405 std::map<int,SALOMEDSImpl_GenericVariable*>::const_iterator it= aVarsMap.begin();
3406 for(;it!=aVarsMap.end();it++)
3407 theStudy->AddVariable((*it).second);
3409 theGroup->CloseOnDisk();