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 _name = "Study" + std::to_string(++_id);
142 _doc = _appli->NewDocument("SALOME_STUDY");
147 _useCaseBuilder = new SALOMEDSImpl_UseCaseBuilder(_doc);
148 _builder = new SALOMEDSImpl_StudyBuilder(this);
149 _cb = new SALOMEDSImpl_Callback(_useCaseBuilder);
151 //Put on the root label a StudyHandle attribute to store the address of this object
152 //It will be used to retrieve the study object by DF_Label that belongs to the study
153 SALOMEDSImpl_StudyHandle::Set(_doc->Main().Root(), this);
155 // set Study properties
156 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
158 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
159 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
160 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
161 mn, hh, day, month, year);
162 aProp->SetCreationMode(1); //"from scratch"
165 //============================================================================
167 * Purpose : Clear study components
169 //============================================================================
170 void SALOMEDSImpl_Study::Clear()
174 delete _useCaseBuilder;
181 //============================================================================
183 * Purpose : Open a Study from it's persistent reference
185 //============================================================================
186 bool SALOMEDSImpl_Study::Open(const std::string& aUrl)
190 // Set "C" locale temporarily to avoid possible localization problems
191 Kernel_Utils::Localizer loc;
196 HDFfile *hdf_file =0;
197 HDFgroup *hdf_group_study_structure =0;
198 HDFgroup *hdf_notebook_vars = 0;
202 bool isASCII = false;
203 if (HDFascii::isASCII(aUrl.c_str())) {
205 char* aResultPath = HDFascii::ConvertFromASCIIToHDF(aUrl.c_str());
208 aC_HDFUrl = new char[strlen(aResultPath) + 19];
209 sprintf(aC_HDFUrl, "%shdf_from_ascii.hdf", aResultPath);
210 delete [] (aResultPath);
218 hdf_file = new HDFfile((char*)aHDFUrl.c_str());
220 hdf_file->OpenOnDisk(HDF_RDONLY);// mpv: was RDWR, but opened file can be write-protected too
225 eStr = new char[strlen(aUrl.c_str())+17];
226 sprintf(eStr,"Can't open file %s",aUrl.c_str());
228 _errorCode = std::string(eStr);
232 // Assign the value of the URL in the study object
235 SALOMEDSImpl_AttributePersistentRef::Set(_doc->Main(), aUrl);
237 if (!hdf_file->ExistInternalObject("STUDY_STRUCTURE")) {
238 _errorCode = "Study is empty";
242 //Create the Structure of the Document
243 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
246 BuildTree (this, hdf_group_study_structure);
250 char *eStr = new char [strlen(aUrl.c_str())+17];
251 sprintf(eStr,"Can't open file %s", aUrl.c_str());
252 _errorCode = std::string(eStr);
256 //Read and create notebook variables
257 if(hdf_file->ExistInternalObject("NOTEBOOK_VARIABLES")) {
258 hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
259 ReadNoteBookVariables(this, hdf_notebook_vars);
260 hdf_notebook_vars =0; //will be deleted by hdf_sco_group destructor
263 hdf_file->CloseOnDisk();
264 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
267 std::vector<std::string> aFilesToRemove;
268 aFilesToRemove.push_back("hdf_from_ascii.hdf");
269 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
272 delete hdf_file; // all related hdf objects will be deleted
274 // unlock study if it is locked, to set components versions
275 StudyUnlocker unlock(this);
277 //For old studies we have to add "unknown" version tag for all stored components
278 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
279 for (; itcomponent.More(); itcomponent.Next())
281 SALOMEDSImpl_SComponent sco = itcomponent.Value();
282 std::string aCompType = sco.GetComment();
283 if ( aCompType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() ) continue;
284 if ( GetProperties()->GetComponentVersions( aCompType ).empty() )
285 GetProperties()->SetComponentVersion( aCompType, "" ); // empty version means "unknown"
291 //============================================================================
293 * Purpose : Save a Study to it's persistent reference
295 //============================================================================
296 bool SALOMEDSImpl_Study::Save(SALOMEDSImpl_DriverFactory* aFactory,
301 std::string url = URL();
303 _errorCode = "No path specified to save the study. Nothing done";
307 return Impl_SaveAs(url, aFactory, theMultiFile, false);
313 bool SALOMEDSImpl_Study::SaveASCII(SALOMEDSImpl_DriverFactory* aFactory,
318 std::string url = URL();
320 _errorCode = "No path specified to save the study. Nothing done";
324 return Impl_SaveAs(url, aFactory, theMultiFile, true);
330 //=============================================================================
331 /*! Function : SaveAs
332 * Purpose : Save a study to the persistent reference aUrl
334 //============================================================================
335 bool SALOMEDSImpl_Study::SaveAs(const std::string& aUrl,
336 SALOMEDSImpl_DriverFactory* aFactory,
340 return Impl_SaveAs(aUrl, aFactory, theMultiFile, false);
343 bool SALOMEDSImpl_Study::SaveAsASCII(const std::string& aUrl,
344 SALOMEDSImpl_DriverFactory* aFactory,
348 return Impl_SaveAs(aUrl, aFactory, theMultiFile, true);
351 //=============================================================================
352 /*! Function : _SaveProperties
353 * Purpose : save the study properties in HDF file
355 //============================================================================
356 bool SALOMEDSImpl_Study::Impl_SaveProperties(HDFgroup *hdf_group)
360 HDFdataset *hdf_dataset = 0;
364 // add modifications list (user and date of save)
365 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
367 // unlock study if it is locked, to set modification date
368 StudyUnlocker unlock(this);
370 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
371 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
372 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
373 mn, hh, day, month, year);
375 // lock study back if it was locked initially, to write correct value of Locked flag
378 std::vector<std::string> aNames;
379 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
381 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
383 std::string units = aProp->GetUnits();
384 std::string comment = aProp->GetComment();
386 std::map< std::string, std::vector<std::string> > allVersions = aProp->GetComponentsVersions();
387 std::map<std::string, std::string> versions;
389 int aLength = 0, aLength1 = 0, anIndex, i, unitsSize = 0, commentSize = 0;
391 for(i=1; i<=aNames.size(); i++)
392 aLength += aNames[i-1].size() + 1;
394 std::map< std::string, std::vector<std::string> >::const_iterator it;
395 for (it = allVersions.begin(); it != allVersions.end(); ++it ) {
396 std::string vlist = "";
397 std::vector<std::string> vl = it->second;
398 std::vector<std::string>::const_iterator vlit;
399 for ( vlit = vl.begin(); vlit != vl.end(); ++vlit ) {
400 if ( vlist != "" ) vlist += ";";
403 versions[ it->first ] = vlist;
404 aLength1 += it->first.size() + vlist.size() + 2;
407 unitsSize = units.size();
408 commentSize = comment.size();
411 //locked flag, modified flag,
412 //minutes, hours, day, months, year, user name, char(1),
413 //minutes, hours, day, months, year, user name, char(1),
414 //.....................................................,
415 //.....................................................,
416 //.....................................................,
417 //minutes, hours, day, months, year, user name, char(1), char(30) <- !!!! used to define end of section with modifications !!!!
418 //units, char(1), comment, char(30) <- !!!! used to define start of section with components' versions !!!!
419 //component=versions, char(1),
420 //component=versions, char(1),
421 //...........................,
422 //component=versions, char(1), char(0)
424 //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)
425 // unit length + 1, comment length, "zero" byte
427 char* aProperty = new char[3 + aLength + 12 * aNames.size() + 1 + unitsSize + 1 + commentSize + 1 + aLength1 ];
429 sprintf(aProperty,"%c%c", (char)aProp->GetCreationMode(), (aProp->IsLocked())?'l':'u');
431 aLength = aNames.size();
433 for(anIndex = 0; anIndex<aLength; anIndex++) {
434 sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
435 (int)(aMinutes[anIndex]),
436 (int)(aHours[anIndex]),
437 (int)(aDays[anIndex]),
438 (int)(aMonths[anIndex]),
439 (int)(aYears[anIndex]),
440 aNames[anIndex].c_str());
441 a = strlen(aProperty);
445 //Write delimeter of the section to define end of the modifications section
448 //Write units if need
449 if(units.size() > 0) {
450 sprintf(&(aProperty[a]),"%s",units.c_str());
451 a = strlen(aProperty);
456 //Write comments if need
457 if(comment.size() > 0) {
458 sprintf(&(aProperty[a]),"%s",comment.c_str());
459 a = strlen(aProperty);
462 aProperty[a++] = 30; //delimeter of the component versions
464 std::map<std::string, std::string>::const_iterator versionsIt;
465 for ( versionsIt = versions.begin(); versionsIt != versions.end(); ++versionsIt ) {
466 sprintf(&(aProperty[a]),"%s=%s",
467 (char*)(versionsIt->first.c_str()),
468 (char*)(versionsIt->second.c_str()));
469 a = a + versionsIt->first.size() + versionsIt->second.size() + 1;
475 name_len = (hdf_int32) a;
476 size[0] = name_len + 1 ;
477 hdf_dataset = new HDFdataset("AttributeStudyProperties",hdf_group,HDF_STRING,size,1);
478 hdf_dataset->CreateOnDisk();
479 hdf_dataset->WriteOnDisk(aProperty);
480 hdf_dataset->CloseOnDisk();
481 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
484 aProp->SetModified(0);
488 //=============================================================================
489 /*! Function : _SaveAs
490 * Purpose : save the study in HDF file
492 //============================================================================
493 bool SALOMEDSImpl_Study::Impl_SaveAs(const std::string& aStudyUrl,
494 SALOMEDSImpl_DriverFactory* aFactory,
498 // Set "C" locale temporarily to avoid possible localization problems
499 Kernel_Utils::Localizer loc;
501 // HDF File will be composed of differents part :
502 // * For each ComponentDataType, all data created by the component
503 // Informations in data group hdf_group_datacomponent
504 // * Study Structure -> Exactly what is contained in Document
505 // Informations in data group hdf_group_study_structure
510 HDFgroup *hdf_group_study_structure =0;
511 HDFgroup *hdf_sco_group =0;
512 HDFgroup *hdf_sco_group2 =0;
513 HDFgroup *hdf_notebook_vars =0;
514 HDFgroup *hdf_notebook_var = 0;
516 HDFgroup *hdf_group_datacomponent =0;
517 HDFdataset *hdf_dataset =0;
519 hdf_int32 name_len = 0;
520 std::string component_name;
522 // Store previous URL
523 std::string anOldName = Name();
525 // Map to store components' versions
526 std::map<std::string, std::string> componentVersions;
528 //Create a temporary url to which the study is saved
529 std::string aUrl = SALOMEDSImpl_Tool::GetTmpDir() + SALOMEDSImpl_Tool::GetNameFromPath(aStudyUrl);
531 // unlock study if it is locked, as some attributes need to be modified
532 StudyUnlocker unlock(this);
534 SALOMEDSImpl_StudyBuilder* SB= NewBuilder();
535 std::map<std::string, SALOMEDSImpl_Driver*> aMapTypeDriver;
538 // mpv 15.12.2003: for saving components we have to load all data from all modules
539 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
540 for (; itcomponent.More(); itcomponent.Next()) {
541 SALOMEDSImpl_SComponent sco = itcomponent.Value();
542 // if there is an associated Engine call its method for saving
543 std::string IOREngine;
545 SALOMEDSImpl_Driver* aDriver = NULL;
546 std::string aCompType = sco.GetComment();
547 if (!sco.ComponentIOR(IOREngine)) {
548 if (!aCompType.empty()) {
549 aDriver = aFactory->GetDriverByType(aCompType);
550 if (aDriver != NULL) {
551 if (!SB->LoadWith(sco, aDriver)) {
552 _errorCode = SB->GetErrorCode();
559 aDriver = aFactory->GetDriverByIOR(IOREngine);
561 aMapTypeDriver[aCompType] = aDriver;
564 _errorCode = "Can not restore information to resave it";
569 // VSR: set URL to new file name
570 // VSR: remember to set previous name if save operation fails
573 // To change for Save
574 // Do not have to do a new file but just a Open??? Rewrite all informations after erasing evrything??
575 hdf_file = new HDFfile((char*)aUrl.c_str());
576 hdf_file->CreateOnDisk();
578 //-----------------------------------------------------------------------
579 // 1 - Create a groupe for each SComponent and Update the PersistanceRef
580 //-----------------------------------------------------------------------
581 hdf_group_datacomponent = new HDFgroup("DATACOMPONENT",hdf_file);
582 hdf_group_datacomponent->CreateOnDisk();
584 for (itcomponent.Init(); itcomponent.More(); itcomponent.Next()) {
585 SALOMEDSImpl_SComponent sco = itcomponent.Value();
587 std::string scoid = sco.GetID();
588 hdf_sco_group = new HDFgroup((char*)scoid.c_str(), hdf_group_datacomponent);
589 hdf_sco_group->CreateOnDisk();
591 std::string componentDataType = sco.ComponentDataType();
592 std::string IOREngine;
593 if (sco.ComponentIOR(IOREngine)) {
594 // Engine should be already in the map as it was to added before
595 SALOMEDSImpl_Driver* Engine = aMapTypeDriver[componentDataType];
596 if (Engine != NULL) {
597 SALOMEDSImpl_TMPFile* aStream = NULL;
600 componentVersions[ componentDataType ] = Engine->Version();
602 if (theASCII) aStream = Engine->SaveASCII(sco,
603 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
606 else aStream = Engine->Save(sco,
607 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
610 HDFdataset *hdf_dataset;
611 hdf_size aHDFSize[1];
612 if (length > 0) { //The component saved some auxiliary files, then put them into HDF file
613 aHDFSize[0] = length;
615 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group, HDF_STRING, aHDFSize, 1);
616 hdf_dataset->CreateOnDisk();
617 hdf_dataset->WriteOnDisk(aStream->Data()); //Save the stream in the HDF file
618 hdf_dataset->CloseOnDisk();
621 if (aStream) delete aStream;
623 // store multifile state
625 hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
626 hdf_dataset->CreateOnDisk();
627 hdf_dataset->WriteOnDisk((void*)(theMultiFile?"M":"S")); // save: multi or single
628 hdf_dataset->CloseOnDisk();
629 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
632 hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
633 hdf_dataset->CreateOnDisk();
634 hdf_dataset->WriteOnDisk((void*)(theASCII?"A":"B")); // save: ASCII or BINARY
635 hdf_dataset->CloseOnDisk();
636 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
637 // Creation of the persistance reference attribute
638 Translate_IOR_to_persistentID (sco, Engine, theMultiFile, theASCII);
641 hdf_sco_group->CloseOnDisk();
642 hdf_sco_group=0; // will be deleted by hdf_group_datacomponent destructor
644 hdf_group_datacomponent->CloseOnDisk();
645 hdf_group_datacomponent =0; // will be deleted by hdf_file destructor
647 //-----------------------------------------------------------------------
648 //3 - Write the Study Structure
649 //-----------------------------------------------------------------------
650 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
651 hdf_group_study_structure->CreateOnDisk();
652 // save component attributes
653 for (itcomponent.Init(); itcomponent.More(); itcomponent.Next()) {
654 SALOMEDSImpl_SComponent SC = itcomponent.Value();
655 std::string scid = SC.GetID();
656 hdf_sco_group2 = new HDFgroup((char*)scid.c_str(), hdf_group_study_structure);
657 hdf_sco_group2->CreateOnDisk();
658 SaveAttributes(SC, hdf_sco_group2);
659 // ComponentDataType treatment
660 component_name = SC.ComponentDataType();
661 name_len = (hdf_int32)component_name.length();
662 size[0] = name_len +1 ;
663 hdf_dataset = new HDFdataset("COMPONENTDATATYPE",hdf_sco_group2,HDF_STRING,size,1);
664 hdf_dataset->CreateOnDisk();
665 hdf_dataset->WriteOnDisk((char*)component_name.c_str());
666 hdf_dataset->CloseOnDisk();
667 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
668 Impl_SaveObject(SC, hdf_sco_group2);
669 hdf_sco_group2->CloseOnDisk();
670 hdf_sco_group2=0; // will be deleted by hdf_group_study_structure destructor
672 //-----------------------------------------------------------------------
673 //4 - Write the Study UseCases Structure
674 //-----------------------------------------------------------------------
675 SALOMEDSImpl_SObject aSO = FindObjectID(USE_CASE_LABEL_ID);
677 HDFgroup *hdf_soo_group = new HDFgroup(USE_CASE_LABEL_ID,hdf_group_study_structure);
678 hdf_soo_group->CreateOnDisk();
679 SaveAttributes(aSO, hdf_soo_group);
680 Impl_SaveObject(aSO, hdf_soo_group);
681 hdf_soo_group->CloseOnDisk();
682 hdf_soo_group=0; // will be deleted by hdf_group_study_structure destructor
684 //-----------------------------------------------------------------------
685 //5 - Write the NoteBook Variables
686 //-----------------------------------------------------------------------
688 //5.1 Create group to store all note book variables
689 hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
690 hdf_notebook_vars->CreateOnDisk();
692 std::string varValue;
694 std::string varIndex;
696 for (int i=0 ;i < myNoteBookVars.size(); i++ ) {
697 // For each variable create HDF group
698 hdf_notebook_var = new HDFgroup((char*)myNoteBookVars[i]->Name().c_str(),hdf_notebook_vars);
699 hdf_notebook_var->CreateOnDisk();
701 // Save Variable type
702 varType = myNoteBookVars[i]->SaveType();
703 name_len = (hdf_int32) varType.length();
704 size[0] = name_len +1 ;
705 hdf_dataset = new HDFdataset("VARIABLE_TYPE",hdf_notebook_var,HDF_STRING,size,1);
706 hdf_dataset->CreateOnDisk();
707 hdf_dataset->WriteOnDisk((char*)varType.c_str());
708 hdf_dataset->CloseOnDisk();
709 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
712 sprintf(buffer,"%d",i);
713 varIndex= std::string(buffer);
714 name_len = (hdf_int32) varIndex.length();
715 size[0] = name_len +1 ;
716 hdf_dataset = new HDFdataset("VARIABLE_INDEX",hdf_notebook_var,HDF_STRING,size,1);
717 hdf_dataset->CreateOnDisk();
718 hdf_dataset->WriteOnDisk((char*)varIndex.c_str());
719 hdf_dataset->CloseOnDisk();
720 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
722 // Save Variable value
723 varValue = myNoteBookVars[i]->Save();
724 name_len = (hdf_int32) varValue.length();
725 size[0] = name_len +1 ;
726 hdf_dataset = new HDFdataset("VARIABLE_VALUE",hdf_notebook_var,HDF_STRING,size,1);
727 hdf_dataset->CreateOnDisk();
728 hdf_dataset->WriteOnDisk((char*)varValue.c_str());
729 hdf_dataset->CloseOnDisk();
730 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
731 hdf_notebook_var->CloseOnDisk();
732 hdf_notebook_var = 0; //will be deleted by hdf_sco_group destructor
734 hdf_notebook_vars->CloseOnDisk();
735 hdf_notebook_vars = 0; //will be deleted by hdf_sco_group destructor
737 // record component versions
738 std::map<std::string, std::string>::const_iterator itVersions;
739 for ( itVersions = componentVersions.begin(); itVersions != componentVersions.end(); ++itVersions )
740 GetProperties()->SetComponentVersion( itVersions->first, itVersions->second );
742 // lock study back if it was locked initially, to write correct value of Locked flag
745 //-----------------------------------------------------------------------
746 //6 - Write the Study Properties
747 //-----------------------------------------------------------------------
748 std::string study_name = Name();
749 name_len = (hdf_int32) study_name.size();
750 size[0] = name_len +1 ;
751 hdf_dataset = new HDFdataset("STUDY_NAME",hdf_group_study_structure,HDF_STRING,size,1);
752 hdf_dataset->CreateOnDisk();
753 hdf_dataset->WriteOnDisk((char*)study_name.c_str());
754 hdf_dataset->CloseOnDisk();
755 hdf_dataset=0; // will be deleted by hdf_group_study_structure destructor
757 Impl_SaveProperties(hdf_group_study_structure);
758 hdf_group_study_structure->CloseOnDisk();
759 hdf_file->CloseOnDisk();
761 hdf_group_study_structure =0; // will be deleted by hdf_file destructor
762 delete hdf_file; // recursively deletes all hdf objects...
764 catch (HDFexception) {
765 _errorCode = "HDFexception ! ";
766 URL( anOldName ); // VSR: restore previous url if operation is failed
769 catch (std::exception& exc) {
770 _errorCode = const_cast<char*>(exc.what());
771 URL( anOldName ); // VSR: restore previous url if operation is failed
775 _errorCode = "Unknown exception ! ";
776 URL( anOldName ); // VSR: restore previous url if operation is failed
779 if (theASCII) { // save file in ASCII format
780 HDFascii::ConvertFromHDFToASCII(aUrl.c_str(), true);
783 // Now it's necessary to copy files from the temporary directory to the user defined directory.
784 // The easiest way to get a list of file in the temporary directory
786 std::string aCmd, aTmpFileDir = SALOMEDSImpl_Tool::GetTmpDir();
787 std::string aTmpFile = aTmpFileDir +"files";
788 std::string aStudyTmpDir = SALOMEDSImpl_Tool::GetDirFromPath(aUrl);
791 aCmd = "dir /B \"" + aStudyTmpDir +"\" > " + aTmpFile;
793 aCmd ="ls -1 \"" + aStudyTmpDir +"\" > " + aTmpFile;
795 system(aCmd.c_str());
797 // Iterate and move files in the temporary directory
798 FILE* fp = fopen(aTmpFile.c_str(), "rb");
800 URL( anOldName ); // VSR: restore previous url if operation is failed
803 char* buffer = new char[2047];
805 while (!feof(fp) && !errors) {
806 if ((fgets(buffer, 2046, fp)) == NULL) break;
807 size_t aLen = strlen(buffer);
808 if (buffer[aLen-1] == '\n') buffer[aLen-1] = char(0);
810 aCmd = "move /Y \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl) +"\"";
812 aCmd = "mv -f \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl)+"\"";
814 errors = system(aCmd.c_str());
822 DeleteFileA(aTmpFile.c_str());
824 unlink(aTmpFile.c_str());
828 RemoveDirectoryA(aTmpFileDir.c_str());
829 RemoveDirectoryA(aStudyTmpDir.c_str());
831 rmdir(aTmpFileDir.c_str());
832 rmdir(aStudyTmpDir.c_str());
836 // VSR: finally, if all is done without errors, mark study as Saved
843 //============================================================================
844 /*! Function : Impl_SaveObject
847 //============================================================================
848 bool SALOMEDSImpl_Study::Impl_SaveObject(const SALOMEDSImpl_SObject& SC,
849 HDFgroup *hdf_group_datatype)
853 // Write in group hdf_group_datatype all informations of SObject SC
854 // Iterative function to parse all SObjects under a SComponent
856 HDFgroup *hdf_group_sobject = 0;
858 DF_ChildIterator itchild(SC.GetLabel());
859 for (; itchild.More(); itchild.Next()) {
860 // mpv: don't save empty labels
861 std::vector<DF_Attribute*> attr = itchild.Value().GetAttributes();
862 if (attr.size() == 0) { //No attributes on the label
863 DF_ChildIterator subchild(itchild.Value());
864 if (!subchild.More()) {
867 subchild.Init(itchild.Value(), true);
869 for (; subchild.More() && anEmpty; subchild.Next()) {
870 std::vector<DF_Attribute*> attr2 = subchild.Value().GetAttributes();
872 anEmpty = false; //There are attributes on the child label
876 if (anEmpty) continue;
879 SALOMEDSImpl_SObject SO = SALOMEDSImpl_Study::SObject(itchild.Value());
881 std::string scoid = SO.GetID();
882 hdf_group_sobject = new HDFgroup(scoid.c_str(), hdf_group_datatype);
883 hdf_group_sobject->CreateOnDisk();
884 SaveAttributes(SO, hdf_group_sobject);
885 Impl_SaveObject(SO, hdf_group_sobject);
886 hdf_group_sobject->CloseOnDisk();
887 hdf_group_sobject =0; // will be deleted by father hdf object destructor
892 //============================================================================
893 /*! Function : CanCopy
896 //============================================================================
897 bool SALOMEDSImpl_Study::CanCopy(const SALOMEDSImpl_SObject& theObject,
898 SALOMEDSImpl_Driver* theEngine)
901 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
902 if (!aComponent) return false;
903 if (aComponent.GetLabel() == theObject.GetLabel()) return false;
904 std::string IOREngine;
905 if (!aComponent.ComponentIOR(IOREngine)) return false;
906 if (theEngine == NULL) return false;
907 return theEngine->CanCopy(theObject);
910 //============================================================================
911 /*! Function : CopyLabel
914 //============================================================================
915 bool SALOMEDSImpl_Study::CopyLabel(SALOMEDSImpl_Driver* theEngine,
916 const int theSourceStartDepth,
917 const DF_Label& theSource,
918 const DF_Label& theDestinationMain)
923 DF_Label aTargetLabel = theDestinationMain;
924 DF_Label aAuxTargetLabel = theDestinationMain.Father().FindChild(2);
925 for(a = theSource.Depth() - theSourceStartDepth; a > 0 ; a--) {
926 DF_Label aSourceLabel = theSource;
927 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
928 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
929 aAuxTargetLabel = aAuxTargetLabel.FindChild(aSourceLabel.Tag());
931 // iterate attributes
932 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
933 for(int i = 0, len = attrList.size(); i<len; i++) {
934 DF_Attribute* anAttr = attrList[i];
935 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
936 if (type.substr(0, 17) == std::string("AttributeTreeNode")) continue; // never copy tree node attribute
937 if (type == std::string("AttributeTarget")) continue; // and target attribute
939 if (type == std::string("AttributeReference")) { // reference copied as Comment in aux tree
940 DF_Label aReferenced = dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Get();
941 std::string anEntry = aReferenced.Entry();
942 // store the value of name attribute of referenced label
943 SALOMEDSImpl_AttributeName* aNameAttribute;
944 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aReferenced.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
946 anEntry += aNameAttribute->Value();
948 SALOMEDSImpl_AttributeComment::Set(aAuxTargetLabel, anEntry);
952 if (type == std::string("AttributeIOR")) { // IOR => ID and TMPFile of Engine
953 std::string anEntry = theSource.Entry();
954 SALOMEDSImpl_SObject aSO = FindObjectID(anEntry);
957 SALOMEDSImpl_TMPFile* aStream = theEngine->CopyFrom(aSO, anObjID, aLen);
958 std::string aResStr("");
959 for(a = 0; a < aLen; a++) {
960 aResStr += (char)(aStream->Get(a));
963 if(aStream) delete aStream;
965 SALOMEDSImpl_AttributeInteger::Set(aAuxTargetLabel, anObjID);
966 SALOMEDSImpl_AttributeName::Set(aAuxTargetLabel, aResStr);
969 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
970 aTargetLabel.AddAttribute(aNewAttribute);
971 anAttr->Paste(aNewAttribute);
977 //============================================================================
981 //============================================================================
982 bool SALOMEDSImpl_Study::Copy(const SALOMEDSImpl_SObject& theObject,
983 SALOMEDSImpl_Driver* theEngine)
987 // adoptation for alliances datamodel copy: without IOR attributes !!!
988 bool aStructureOnly; // copy only SObjects and attributes without component help
989 aStructureOnly = !theObject.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID());
991 // get component-engine
992 SALOMEDSImpl_Study* aStudy = theObject.GetStudy();
995 _errorCode = "Document is null";
999 //Clear the clipboard
1000 _clipboard->Main().Root().ForgetAllAttributes(true);
1001 _appli->Close(_clipboard);
1002 _clipboard = _appli->NewDocument("SALOME_STUDY");
1004 // set component data type to the name attribute of root label
1005 if (!aStructureOnly) {
1006 SALOMEDSImpl_AttributeComment::Set(_clipboard->Main().Root(),
1007 theEngine->ComponentDataType());
1009 // iterate all theObject's label children
1010 DF_Label aStartLabel = theObject.GetLabel();
1011 int aSourceStartDepth = aStartLabel.Depth();
1013 // copy main source label
1014 CopyLabel(theEngine, aSourceStartDepth, aStartLabel, _clipboard->Main());
1016 // copy all subchildren of the main source label (all levels)
1017 DF_ChildIterator anIterator(aStartLabel, true);
1018 for(; anIterator.More(); anIterator.Next()) {
1019 CopyLabel(theEngine, aSourceStartDepth, anIterator.Value(), _clipboard->Main());
1025 //============================================================================
1026 /*! Function : CanPaste
1029 //============================================================================
1030 bool SALOMEDSImpl_Study::CanPaste(const SALOMEDSImpl_SObject& theObject,
1031 SALOMEDSImpl_Driver* theEngine)
1036 _errorCode = "Clipboard is null";
1040 SALOMEDSImpl_AttributeComment* aCompName = NULL;
1041 if (!(aCompName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1042 _errorCode = "Clipboard has no component type";
1045 SALOMEDSImpl_AttributeInteger* anObjID;
1046 if (!(anObjID=(SALOMEDSImpl_AttributeInteger*)_clipboard->Main().Father().FindChild(2).FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
1047 _errorCode = "Clipboard has no object id";
1050 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1052 _errorCode = "Object doesn't belong to component";
1056 std::string IOREngine;
1057 if (!aComponent.ComponentIOR(IOREngine)) {
1058 _errorCode = "component has no IOR";
1061 return theEngine->CanPaste(aCompName->Value(), anObjID->Value());
1064 //============================================================================
1065 /*! Function : PasteLabel
1068 //============================================================================
1069 DF_Label SALOMEDSImpl_Study::PasteLabel(SALOMEDSImpl_Driver* theEngine,
1070 const DF_Label& theSource,
1071 const DF_Label& theDestinationStart,
1072 const bool isFirstElement)
1076 // get corresponding source, target and auxiliary labels
1077 DF_Label aTargetLabel = theDestinationStart;
1079 DF_Label aAuxSourceLabel = theSource.Root().FindChild(2);
1081 if (!isFirstElement) {
1082 for(a = theSource.Depth() - 1; a > 0 ; a--) {
1083 DF_Label aSourceLabel = theSource;
1084 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
1085 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
1086 aAuxSourceLabel = aAuxSourceLabel.FindChild(aSourceLabel.Tag());
1088 SALOMEDSImpl_SObject so = GetSObject(aTargetLabel);
1089 addSO_Notification(so);
1092 // check auxiliary label for TMPFile => IOR
1093 SALOMEDSImpl_AttributeName* aNameAttribute = NULL;
1094 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1095 SALOMEDSImpl_AttributeInteger* anObjID = (SALOMEDSImpl_AttributeInteger*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeInteger::GetID());
1096 SALOMEDSImpl_AttributeComment* aComponentName = (SALOMEDSImpl_AttributeComment*)theSource.Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID());
1097 std::string aCompName = aComponentName->Value();
1099 if (theEngine->CanPaste(aCompName, anObjID->Value())) {
1100 std::string aTMPStr = aNameAttribute->Value();
1101 int aLen = aTMPStr.size();
1102 unsigned char* aStream = NULL;
1104 aStream = new unsigned char[aLen+10];
1105 for(a = 0; a < aLen; a++) {
1106 aStream[a] = aTMPStr[a];
1110 std::string anEntry = aTargetLabel.Entry();
1111 SALOMEDSImpl_SObject aPastedSO = FindObjectID(anEntry);
1113 if (isFirstElement) {
1114 std::string aDestEntry = theEngine->PasteInto(aStream,
1117 aPastedSO.GetFatherComponent());
1118 aTargetLabel = DF_Label::Label(theDestinationStart, aDestEntry);
1120 theEngine->PasteInto(aStream, aLen, anObjID->Value(), aPastedSO);
1122 if(aStream != NULL) delete []aStream;
1126 // iterate attributes
1127 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
1128 for(int i = 0, len = attrList.size(); i<len; i++) {
1129 DF_Attribute* anAttr = attrList[i];
1130 if (aTargetLabel.FindAttribute(anAttr->ID())) {
1131 aTargetLabel.ForgetAttribute(anAttr->ID());
1133 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
1134 aTargetLabel.AddAttribute(aNewAttribute);
1135 anAttr->Paste(aNewAttribute);
1138 // check auxiliary label for Comment => reference or name attribute of the referenced object
1139 SALOMEDSImpl_AttributeComment* aCommentAttribute = NULL;
1140 if ((aCommentAttribute=(SALOMEDSImpl_AttributeComment*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1141 char * anEntry = new char[aCommentAttribute->Value().size() + 1];
1142 strcpy(anEntry, std::string(aCommentAttribute->Value()).c_str());
1143 char* aNameStart = strchr(anEntry, ' ');
1148 // copy to the same study, reanimate reference
1149 DF_Label aRefLabel = DF_Label::Label(aTargetLabel, anEntry);
1150 SALOMEDSImpl_AttributeReference::Set(aTargetLabel, aRefLabel);
1151 // target attributes structure support
1152 SALOMEDSImpl_AttributeTarget::Set(aRefLabel)->Add(SALOMEDSImpl_Study::SObject(aTargetLabel));
1157 return aTargetLabel;
1160 //============================================================================
1161 /*! Function : Paste
1164 //============================================================================
1165 SALOMEDSImpl_SObject SALOMEDSImpl_Study::Paste(const SALOMEDSImpl_SObject& theObject,
1166 SALOMEDSImpl_Driver* theEngine)
1170 SALOMEDSImpl_SObject so;
1172 // if study is locked, then paste can't be done
1173 if (GetProperties()->IsLocked()) {
1174 _errorCode = "LockProtection";
1175 throw LockProtection("LockProtection");
1178 // if there is no component name, then paste only SObjects and attributes: without component help
1179 SALOMEDSImpl_AttributeComment* aComponentName = NULL;
1180 bool aStructureOnly = !(aComponentName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()));
1182 // CAF document of current study usage
1184 _errorCode = "Document is null";
1188 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1190 // fill root inserted SObject
1191 DF_Label aStartLabel;
1192 if (aStructureOnly) {
1193 DF_Label anObjectLabel = DF_Label::Label(_doc->Main(), theObject.GetID());
1194 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), anObjectLabel, false);
1196 DF_Label aComponentLabel = DF_Label::Label(_doc->Main(), aComponent.GetID());
1197 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), aComponentLabel, true);
1200 // paste all sublebels
1201 DF_ChildIterator anIterator(_clipboard->Main(), true);
1202 for(; anIterator.More(); anIterator.Next()) {
1203 PasteLabel(theEngine, anIterator.Value(), aStartLabel, false);
1206 return SALOMEDSImpl_Study::SObject(aStartLabel);
1209 //============================================================================
1210 /*! Function : GetPersistentReference
1211 * Purpose : Get persistent reference of study (idem URL())
1213 //============================================================================
1214 std::string SALOMEDSImpl_Study::GetPersistentReference()
1219 //============================================================================
1220 /*! Function : GetTransientReference
1221 * Purpose : Get IOR of the Study (registred in Document in doc->Root)
1223 //============================================================================
1224 std::string SALOMEDSImpl_Study::GetTransientReference()
1227 std::string IOR = "";
1229 SALOMEDSImpl_AttributeIOR* Att;
1230 DF_Label _lab = _doc->Root();
1231 if ((Att=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
1235 _errorCode = "IOR is empty";
1241 void SALOMEDSImpl_Study::SetTransientReference(const std::string& theIOR)
1245 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1246 int aLocked = aProp->IsLocked();
1247 if (aLocked) aProp->SetLocked(false);
1249 // Assign the value of the IOR in the study->root
1250 SALOMEDSImpl_AttributeIOR::Set(_doc->Main().Root(), theIOR);
1252 if (aLocked) aProp->SetLocked(true);
1255 //============================================================================
1256 /*! Function : IsEmpty
1257 * Purpose : Detect if study is empty
1259 //============================================================================
1260 bool SALOMEDSImpl_Study::IsEmpty()
1263 if (!_doc) return true;
1264 return _doc->IsEmpty();
1267 //============================================================================
1268 /*! Function : FindComponent
1269 * Purpose : Find a Component with ComponentDataType = aComponentName
1271 //============================================================================
1272 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const std::string& aComponentName)
1277 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1278 SALOMEDSImpl_SComponent compo;
1280 for (; itcomp.More(); itcomp.Next()) {
1281 SALOMEDSImpl_SComponent SC = itcomp.Value();
1282 name = SC.ComponentDataType();
1283 if(aComponentName == name) {
1291 _errorCode = "No component was found";
1297 //============================================================================
1298 /*! Function : FindComponentID
1299 * Purpose : Find a Component from it's ID
1301 //============================================================================
1302 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const std::string& aComponentID)
1306 // Iterate on each components defined in the study
1307 // Get the component ID and compare with aComponentID
1310 SALOMEDSImpl_SComponent compo;
1312 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1313 for (; itcomp.More(); itcomp.Next()) {
1314 SALOMEDSImpl_SComponent SC = itcomp.Value();
1316 if(aComponentID == ID)
1318 // ComponentID found
1325 _errorCode = "No component was found";
1332 //============================================================================
1333 /*! Function : FindObject
1334 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1336 //============================================================================
1337 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const std::string& anObjectName)
1341 // Iterate to all components defined in the study
1342 // After testing the component name, iterate in all objects defined under
1343 // components (function _FindObject)
1345 SALOMEDSImpl_SObject RefSO;
1347 SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
1348 for (; it.More();it.Next()){
1351 SALOMEDSImpl_SComponent SC = it.Value();
1352 if (SC.GetName() == anObjectName)
1358 if (!_find) RefSO = _FindObject(SC, anObjectName, _find);
1361 if(!RefSO) _errorCode = "No object was found";
1365 //============================================================================
1366 /*! Function : FindObjectID
1367 * Purpose : Find an Object with ID = anObjectID
1369 //============================================================================
1370 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const std::string& anObjectID)
1373 SALOMEDSImpl_SObject so;
1375 // Convert aSO->GetID in DF_Label.
1376 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
1379 _errorCode = "No label was found by ID";
1382 return GetSObject(Lab);
1386 //============================================================================
1387 /*! Function : CreateObjectID
1388 * Purpose : Creates an Object with ID = anObjectID
1390 //============================================================================
1391 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const std::string& anObjectID)
1394 SALOMEDSImpl_SObject so;
1396 // Convert aSO->GetID in DF_Label.
1397 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
1400 _errorCode = "Can not create a label";
1403 return GetSObject(Lab);
1407 //============================================================================
1408 /*! Function : FindObjectByName
1409 * Purpose : Find Objects with SALOMEDSImpl_Name = anObjectName in a Component
1410 * : with ComponentDataType = aComponentName
1412 //============================================================================
1413 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const std::string& anObjectName,
1414 const std::string& aComponentName)
1418 std::vector<SALOMEDSImpl_SObject> listSO;
1420 SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
1422 _errorCode = "Can not find the component";
1426 // Iterate on each object and subobject of the component
1427 // If objectName is found add it to the list of SObjects
1428 std::string childName ;
1430 std::string compoId = compo.GetID();
1431 SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
1432 for ( ; it.More(); it.Next() ) {
1434 SALOMEDSImpl_SObject CSO = it.Value();
1435 if ( CSO.GetName() == anObjectName ) {
1437 listSO.push_back(CSO) ;
1440 /* looks also for eventual children */
1441 bool found = false ;
1442 CSO = _FindObject( CSO, anObjectName, found ) ;
1444 listSO.push_back(CSO) ;
1453 //============================================================================
1454 /*! Function : FindObjectIOR
1455 * Purpose : Find an Object with IOR = anObjectIOR
1457 //============================================================================
1458 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const std::string& anObjectIOR)
1462 SALOMEDSImpl_SObject aResult ;
1464 // searching in the datamap for optimization
1465 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anObjectIOR);
1466 if (it != myIORLabels.end()) {
1467 aResult = GetSObject(it->second);
1468 // 11 oct 2002: forbidden attributes must be checked here
1469 if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
1470 myIORLabels.erase(anObjectIOR);
1471 aResult = SALOMEDSImpl_SObject();
1475 if(!aResult) _errorCode = "No object was found";
1479 //============================================================================
1480 /*! Function : FindObjectByPath
1481 * Purpose : Find an Object by its path = thePath
1483 //============================================================================
1484 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const std::string& thePath)
1488 std::string aPath(thePath), aToken;
1489 SALOMEDSImpl_SObject aSO;
1490 int aLength = aPath.size();
1491 bool isRelative = false;
1493 if(aLength == 0) { //Empty path - return the current context
1494 return GetSObject(_current);
1497 if(aPath[0] != '/') //Relative path
1500 DF_ChildIterator anIterator;
1502 SALOMEDSImpl_AttributeName* anAttr;
1505 if(_current.IsNull()) return aSO;
1506 anIterator.Init(_current, false);
1509 if(aPath.size() == 1 && aPath[0] == '/') { //Root
1510 return GetSObject(_doc->Main());
1512 anIterator.Init(_doc->Main(), false);
1515 std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
1516 for(int i = 0, len = vs.size(); i<len; i++) {
1519 if(aToken.size() == 0) break;
1521 for ( ; anIterator.More(); anIterator.Next() ) {
1522 aLabel = anIterator.Value();
1523 if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1524 if(anAttr->Value() == aToken) {
1525 if(i == (len-1)) { //The searched label is found (no part of the path is left)
1526 return GetSObject(aLabel);
1529 anIterator.Init(aLabel, false);
1537 if(!aSO) _errorCode = "No object was found";
1541 //============================================================================
1542 /*! Function : GetObjectPath
1545 //============================================================================
1546 std::string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
1550 std::string aPath("");
1552 _errorCode = "Null object";
1556 std::string aName = theObject.GetName();
1557 if(!aName.empty() && aName != "" ) {
1558 std::string aValue("/");
1562 SALOMEDSImpl_SObject aFather = theObject.GetFather();
1564 aName = aFather.GetName();
1565 if(!aName.empty() && aName != "") {
1566 aValue = GetObjectPath(aFather);
1567 aPath = aValue + aPath;
1576 //============================================================================
1577 /*! Function : GetObjectPathByIOR
1580 //============================================================================
1581 std::string SALOMEDSImpl_Study::GetObjectPathByIOR(const std::string& theIOR)
1586 SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
1588 _errorCode = "No SObject was found by IOR";
1592 return GetObjectPath(so);
1596 //============================================================================
1597 /*! Function : SetContext
1598 * Purpose : Sets the current context
1600 //============================================================================
1601 bool SALOMEDSImpl_Study::SetContext(const std::string& thePath)
1604 if(thePath.empty()) {
1605 _errorCode = "InvalidPath";
1609 std::string aPath(thePath), aContext("");
1610 bool isInvalid = false;
1611 SALOMEDSImpl_SObject aSO;
1613 if(aPath[0] != '/') { //Relative path
1614 aContext = GetContext();
1622 aSO = FindObjectByPath(aContext);
1628 if(isInvalid || !aSO) {
1629 _errorCode = "InvalidContext";
1633 DF_Label aLabel = aSO.GetLabel();
1634 if(aLabel.IsNull()) {
1635 _errorCode = "InvalidContext";
1639 _current = aLabel; //Set the current context
1644 //============================================================================
1645 /*! Function : GetContext
1646 * Purpose : Gets the current context
1648 //============================================================================
1649 std::string SALOMEDSImpl_Study::GetContext()
1653 if(_current.IsNull()) {
1654 _errorCode = "InvaidContext";
1657 SALOMEDSImpl_SObject so = GetSObject(_current);
1658 return GetObjectPath(so);
1661 //============================================================================
1662 /*! Function : GetObjectNames
1663 * Purpose : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
1665 //============================================================================
1666 std::vector<std::string> SALOMEDSImpl_Study::GetObjectNames(const std::string& theContext)
1670 std::vector<std::string> aResultSeq;
1672 if (theContext.empty()) {
1675 DF_Label aTmp = _current;
1676 SetContext(theContext);
1680 if (aLabel.IsNull()) {
1681 _errorCode = "InvalidContext";
1685 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
1686 for (; anIter.More(); anIter.Next()) {
1687 DF_Label aLabel = anIter.Value();
1688 SALOMEDSImpl_AttributeName* aName;
1689 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1690 aResultSeq.push_back(aName->Value());
1696 //============================================================================
1697 /*! Function : GetDirectoryNames
1698 * Purpose : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
1700 //============================================================================
1701 std::vector<std::string> SALOMEDSImpl_Study::GetDirectoryNames(const std::string& theContext)
1705 std::vector<std::string> aResultSeq;
1707 if (theContext.empty()) {
1710 DF_Label aTmp = _current;
1711 SetContext(theContext);
1715 if (aLabel.IsNull()) {
1716 _errorCode = "InvalidContext";
1720 DF_ChildIterator anIter (aLabel, true); // iterate first-level children at all sublevels
1721 for (; anIter.More(); anIter.Next()) {
1722 DF_Label aLabel = anIter.Value();
1723 SALOMEDSImpl_AttributeLocalID* anID;
1724 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
1725 if (anID->Value() == DIRECTORYID) {
1726 SALOMEDSImpl_AttributeName* aName;
1727 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1728 aResultSeq.push_back(aName->Value());
1737 //============================================================================
1738 /*! Function : GetFileNames
1739 * Purpose : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
1741 //============================================================================
1742 std::vector<std::string> SALOMEDSImpl_Study::GetFileNames(const std::string& theContext)
1746 std::vector<std::string> aResultSeq;
1748 if (theContext.empty()) {
1751 DF_Label aTmp = _current;
1752 SetContext(theContext);
1756 if (aLabel.IsNull()) {
1757 _errorCode = "InvalidContext";
1761 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
1762 for (; anIter.More(); anIter.Next()) {
1763 DF_Label aLabel = anIter.Value();
1764 SALOMEDSImpl_AttributeLocalID* anID;
1765 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
1766 if (anID->Value() == FILELOCALID) {
1767 SALOMEDSImpl_AttributePersistentRef* aName;
1768 if ((aName=(SALOMEDSImpl_AttributePersistentRef*)aLabel.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
1769 std::string aFileName = aName->Value();
1770 if (aFileName.size() > 0)
1771 aResultSeq.push_back(aFileName.substr(strlen(FILEID), aFileName.size()));
1780 //============================================================================
1781 /*! Function : GetComponentNames
1782 * Purpose : method to get all components names
1784 //============================================================================
1785 std::vector<std::string> SALOMEDSImpl_Study::GetComponentNames(const std::string& theContext)
1789 std::vector<std::string> aResultSeq;
1790 DF_ChildIterator anIter(_doc->Main(), false); // iterate all subchildren at first level
1791 for(; anIter.More(); anIter.Next()) {
1792 DF_Label aLabel = anIter.Value();
1793 SALOMEDSImpl_AttributeName* aName;
1794 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1795 aResultSeq.push_back(aName->Value());
1801 //============================================================================
1802 /*! Function : NewChildIterator
1803 * Purpose : Create a ChildIterator from an SObject
1805 //============================================================================
1806 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
1809 return SALOMEDSImpl_ChildIterator(aSO);
1813 //============================================================================
1814 /*! Function : NewComponentIterator
1815 * Purpose : Create a SComponentIterator
1817 //============================================================================
1818 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
1821 return SALOMEDSImpl_SComponentIterator(_doc);
1825 //============================================================================
1826 /*! Function : NewBuilder
1827 * Purpose : Create a StudyBuilder
1829 //============================================================================
1830 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
1834 _builder->SetOnAddSObject(_cb);
1835 _builder->SetOnRemoveSObject(_cb);
1841 //============================================================================
1843 * Purpose : get study name
1845 //============================================================================
1846 std::string SALOMEDSImpl_Study::Name()
1849 return Kernel_Utils::GetBaseName( _name, false );
1852 //============================================================================
1853 /*! Function : IsSaved
1854 * Purpose : get if study has been saved
1856 //============================================================================
1857 bool SALOMEDSImpl_Study::IsSaved()
1863 //============================================================================
1864 /*! Function : IsSaved
1865 * Purpose : set if study has been saved
1867 //============================================================================
1868 void SALOMEDSImpl_Study::IsSaved(bool save)
1872 if(save) _doc->SetModified(false);
1875 //============================================================================
1876 /*! Function : IsModified
1877 * Purpose : Detect if a Study has been modified since it has been saved
1879 //============================================================================
1880 bool SALOMEDSImpl_Study::IsModified()
1884 // True if is modified
1885 if (_doc && _doc->IsModified()) return true;
1890 //============================================================================
1892 * Purpose : get URL of the study (persistent reference of the study)
1894 //============================================================================
1895 std::string SALOMEDSImpl_Study::URL()
1899 return Kernel_Utils::AddExtension( _name );
1903 //============================================================================
1905 * Purpose : set URL of the study (persistent reference of the study)
1907 //============================================================================
1908 void SALOMEDSImpl_Study::URL(const std::string& url)
1914 /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
1915 std::string tmp(_URL);
1917 char *aName = (char*)tmp.ToCString();
1918 char *adr = strtok(aName, "/");
1922 adr = strtok(NULL, "/");
1928 //============================================================================
1929 /*! Function : _FindObject
1930 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1932 //============================================================================
1933 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
1934 const std::string& theObjectName,
1937 SALOMEDSImpl_SObject RefSO;
1938 if(!SO) return RefSO;
1940 // Iterate on each objects and subobjects of the component
1941 // If objectName find, stop the loop and get the object reference
1942 SALOMEDSImpl_AttributeName* anAttr;
1944 std::string soid = SO.GetID();
1945 DF_ChildIterator it(SO.GetLabel());
1946 for (; it.More(); it.Next()){
1949 if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1951 std::string Val(anAttr->Value());
1952 if (Val == theObjectName)
1954 RefSO = GetSObject(it.Value());
1958 if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
1964 //============================================================================
1965 /*! Function : _FindObjectIOR
1966 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
1968 //============================================================================
1969 SALOMEDSImpl_SObject
1970 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
1971 const std::string& theObjectIOR,
1974 SALOMEDSImpl_SObject RefSO, aSO;
1975 if(!SO) return RefSO;
1977 // Iterate on each objects and subobjects of the component
1978 // If objectName find, stop the loop and get the object reference
1979 SALOMEDSImpl_AttributeIOR* anAttr;
1981 DF_ChildIterator it(SO.GetLabel());
1982 for (; it.More();it.Next()){
1985 if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
1987 std::string Val(anAttr->Value());
1988 if (Val == theObjectIOR)
1990 RefSO = GetSObject(it.Value());
1994 aSO = GetSObject(it.Value());
1995 if (!_find) RefSO = _FindObjectIOR(aSO, theObjectIOR, _find);
2001 //============================================================================
2002 /*! Function : _GetNoteBookAccessor
2003 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
2005 //============================================================================
2006 std::string SALOMEDSImpl_Study::_GetNoteBookAccessor(){
2007 return std::string("notebook");
2010 //============================================================================
2011 /*! Function : _GetStudyVariablesScript
2014 //============================================================================
2015 std::string SALOMEDSImpl_Study::_GetStudyVariablesScript()
2017 std::string dump("");
2019 if(myNoteBookVars.empty())
2022 Kernel_Utils::Localizer loc;
2024 dump += "####################################################\n";
2025 dump += "## Begin of NoteBook variables section ##\n";
2026 dump += "####################################################\n";
2028 std::string set_method = _GetNoteBookAccessor()+".set(";
2029 std::string varName;
2030 std::string varValue;
2031 for(int i = 0 ; i < myNoteBookVars.size();i++ ) {
2032 varName = myNoteBookVars[i]->Name();
2033 varValue = myNoteBookVars[i]->SaveToScript();
2034 dump+=set_method+"\""+varName+"\", "+varValue+")\n";
2037 dump += "####################################################\n";
2038 dump += "## End of NoteBook variables section ##\n";
2039 dump += "####################################################\n";
2044 //============================================================================
2045 /*! Function : _GetNoteBookAccess
2048 //============================================================================
2049 std::string SALOMEDSImpl_Study::_GetNoteBookAccess(const std::string& theStudyVar)
2051 std::string notebook = "import salome_notebook\n";
2052 notebook += _GetNoteBookAccessor() + " = salome_notebook.NoteBook(" + theStudyVar + ")" ;
2056 bool SALOMEDSImpl_Study::IsLocked()
2059 return GetProperties()->IsLocked();
2062 void SALOMEDSImpl_Study::UpdateIORLabelMap(const std::string& anIOR,const std::string& anEntry)
2065 DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
2066 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
2067 if (it != myIORLabels.end()) myIORLabels.erase(it);
2068 myIORLabels[anIOR] = aLabel;
2071 void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR)
2073 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
2074 if (it != myIORLabels.end())
2076 //remove the ior entry and decref the genericobj (if it's one)
2077 myIORLabels.erase(it);
2081 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel)
2083 SALOMEDSImpl_StudyHandle* Att;
2084 if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
2090 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
2092 return GetStudy(theLabel)->GetSObject(theLabel);
2095 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
2097 return GetStudy(theLabel)->GetSComponent(theLabel);
2101 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
2103 std::string aString = theAttribute->Label().Entry();
2104 GetStudy(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
2107 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
2110 std::vector<SALOMEDSImpl_SObject> aSeq;
2112 SALOMEDSImpl_AttributeTarget* aTarget;
2113 if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
2114 return aTarget->Get();
2121 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
2124 return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
2127 std::string SALOMEDSImpl_Study::GetLastModificationDate()
2130 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
2132 std::vector<std::string> aNames;
2133 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
2134 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
2136 int aLastIndex = aNames.size()-1;
2138 sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
2139 (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
2140 (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
2141 std::string aResStr (aResult);
2145 std::vector<std::string> SALOMEDSImpl_Study::GetModificationsDate()
2148 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
2150 std::vector<std::string> aNames;
2151 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
2152 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
2154 int anIndex, aLength = aNames.size();
2155 std::vector<std::string> aDates;
2157 for (anIndex = 1; anIndex < aLength; anIndex++) {
2159 sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
2160 (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
2161 (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
2162 aDates.push_back(aDate);
2169 //============================================================================
2170 /*! Function : GetUseCaseBuilder
2171 * Purpose : Returns a UseCase builder
2173 //============================================================================
2174 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
2177 return _useCaseBuilder;
2180 //============================================================================
2181 /*! Function : GetSComponent
2184 //============================================================================
2185 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const std::string& theEntry)
2187 SALOMEDSImpl_SComponent aSCO;
2188 if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
2189 aSCO = _mapOfSCO[theEntry];
2191 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
2192 aSCO = SALOMEDSImpl_SComponent(aLabel);
2193 _mapOfSCO[theEntry] = aSCO;
2199 //============================================================================
2200 /*! Function : GetSComponent
2203 //============================================================================
2204 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
2206 return SALOMEDSImpl_SComponent(theLabel);
2209 //============================================================================
2210 /*! Function : GetSObject
2213 //============================================================================
2214 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const std::string& theEntry)
2216 SALOMEDSImpl_SObject aSO;
2217 std::map<std::string, SALOMEDSImpl_SObject>::iterator it=_mapOfSO.find(theEntry);
2218 if(it != _mapOfSO.end())
2221 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
2222 aSO = SALOMEDSImpl_SObject(aLabel);
2223 _mapOfSO[theEntry] = aSO;
2229 //============================================================================
2230 /*! Function : GetSObject
2233 //============================================================================
2234 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
2236 return SALOMEDSImpl_SObject(theLabel);
2239 //============================================================================
2240 /*! Function : GetAttribute
2243 //============================================================================
2244 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const std::string& theEntry,
2245 const std::string& theType)
2247 SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
2248 DF_Attribute* anAttr;
2249 aSO.FindAttribute(anAttr, theType);
2253 //! number of spaces for indentation in Python dump files (to replace \t symbols)
2254 static const int indent_size = 2;
2256 static std::string replace_tabs( const std::string& in )
2258 std::string out = in;
2260 size_t pos = out.find( '\t' );
2261 while ( pos != std::string::npos ) {
2262 out.replace( pos, 1, indent_size, ' ' );
2263 pos = out.find( '\t' );
2269 static std::string GetComponentHeader(const char* theComponentName)
2271 std::stringstream txt;
2272 txt << "###" << std::endl;
2273 txt << "### " << theComponentName << " component" << std::endl;
2274 txt << "###" << std::endl;
2278 //============================================================================
2279 /*! Function : DumpStudy
2282 //============================================================================
2283 bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
2284 const std::string& theBaseName,
2287 SALOMEDSImpl_DriverFactory* theFactory)
2291 if(theFactory == NULL) {
2292 _errorCode = "Null factory for creation of Engines";
2296 std::vector<std::string> aSeq;
2297 std::string aCompType, aFactoryType;
2299 //Build a list of all components in the Study
2300 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
2302 for (; itcomponent.More(); itcomponent.Next()) {
2303 SALOMEDSImpl_SComponent sco = itcomponent.Value();
2304 aCompType = sco.ComponentDataType();
2305 if (aCompType == "GEOM")
2306 aSeq.insert(aSeq.begin(), aCompType);
2308 aSeq.push_back(aCompType);
2310 // re-arrange modules in the sequence, if specific order is given via SALOME_MODULES_ORDER environment variable.
2311 if ( getenv("SALOME_MODULES_ORDER") != 0 ) {
2312 std::string order = getenv("SALOME_MODULES_ORDER");
2313 std::vector<std::string> mlist;
2314 while ( !order.empty() ) {
2315 size_t idx = order.find( "," );
2316 std::string m = order.substr(0, idx);
2317 order = order.substr( ( idx == std::string::npos ) ? order.size() : idx+1 );
2318 if ( m.empty() || std::find( mlist.begin(), mlist.end(), m ) != mlist.end() ) continue;
2319 mlist.push_back( m );
2322 for ( std::vector<std::string>::reverse_iterator mit = mlist.rbegin(); mit != mlist.rend(); ++mit ) {
2323 std::vector<std::string>::iterator it = std::find( aSeq.begin(), aSeq.end(), *mit );
2324 if ( it != aSeq.end() ) {
2326 aSeq.insert( aSeq.begin(), *mit );
2332 std::string aFileName =
2333 thePath + std::string("\\") + theBaseName + std::string(".py");
2335 std::string aFileName =
2336 thePath + std::string("/") + theBaseName + std::string(".py");
2339 //Create a file that will contain a main Study script
2341 fp.open(aFileName.c_str(), std::ios::out);
2344 bool isOpened = fp.is_open();
2346 bool isOpened = fp.rdbuf()->is_open();
2350 _errorCode = std::string("Can't create a file ")+aFileName;
2354 std::stringstream sfp;
2356 std::string aBatchModeScript = "salome";
2358 //Output to the main Study script required Python modules import,
2359 //set sys.path and add a creation of the study.
2362 sfp << GetDumpStudyComment() << std::endl;
2365 sfp << "import sys" << std::endl;
2366 sfp << "import " << aBatchModeScript << std::endl << std::endl;
2368 std::string aStudyVar = "salome.myStudy";
2369 // initialization function
2370 sfp << aBatchModeScript << ".salome_init()" << std::endl;
2371 if ( !isMultiFile ) {
2372 sfp << "theStudy = " << aStudyVar << std::endl << std::endl;
2373 aStudyVar = "theStudy";
2375 // notebook initialization
2376 sfp << _GetNoteBookAccess(aStudyVar) << std::endl;
2378 // extend sys.path with the directory where the script is being dumped to
2379 sfp << "sys.path.insert( 0, r\'" << thePath << "\')" << std::endl << std::endl;
2381 // dump NoteBook variables
2382 sfp << _GetStudyVariablesScript();
2384 // dump visual parameters if necessary
2385 bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
2386 int lastSavePoint = -1;
2388 lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
2389 if(lastSavePoint > 0) {
2390 sfp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << std::endl << std::endl;
2394 std::vector<std::string> aSeqOfFileNames;
2396 // dump all components and create the components specific scripts
2398 int aLength = aSeq.size();
2399 for(int i = 1; i <= aLength; i++) {
2401 aCompType = aSeq[i-1];
2402 SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
2403 SALOMEDSImpl_Driver* aDriver = NULL;
2404 // if there is an associated Engine call its method for saving
2405 std::string IOREngine;
2407 if (!sco.ComponentIOR(IOREngine)) {
2408 if (!aCompType.empty()) {
2410 aDriver = theFactory->GetDriverByType(aCompType);
2412 if (aDriver != NULL) {
2413 SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
2414 if(!SB->LoadWith(sco, aDriver)) {
2415 _errorCode = SB->GetErrorCode();
2423 aDriver = theFactory->GetDriverByIOR(IOREngine);
2426 _errorCode = "Can not restore information to dump it";
2430 if(aDriver == NULL) continue;
2433 long aStreamLength = 0;
2434 SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(isPublished, isMultiFile, isValidScript, aStreamLength);
2435 if ( !isValidScript )
2438 std::stringstream sfp2;
2440 //Output the Python script generated by the component in the newly created file.
2442 sfp2 << GetDumpStudyComment( aCompType.c_str() ) << std::endl;
2444 sfp2 << GetComponentHeader( aCompType.c_str() ) << std::endl;
2445 sfp2 << aStream->Data();
2447 if ( isMultiFile ) {
2448 //Create a file that will contain the component specific script
2451 aFileName=thePath+std::string("\\");
2453 aFileName=thePath+std::string("/");
2455 std::string aScriptName;
2456 aScriptName += theBaseName;
2458 aScriptName += aCompType;
2460 aFileName += aScriptName+ std::string(".py");
2461 aSeqOfFileNames.push_back(aFileName);
2463 fp2.open(aFileName.c_str(), std::ios::out);
2466 isOpened = fp2.is_open();
2468 isOpened = fp2.rdbuf()->is_open();
2472 _errorCode = std::string("Can't create a file ")+aFileName;
2473 SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
2477 // replace '\t' symbols
2478 fp2 << replace_tabs( sfp2.str() );
2482 //Add to the main script a call to RebuildData of the generated by the component the Python script
2483 sfp << "import " << aScriptName << std::endl;
2484 sfp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << std::endl;
2489 if(aStream) delete aStream;
2493 sfp << "if salome.sg.hasDesktop():" << std::endl;
2494 sfp << "\tsalome.sg.updateObjBrowser(True)" << std::endl;
2496 if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
2497 sfp << "\tiparameters.getSession().restoreVisualState(1)" << std::endl;
2500 // replace '\t' symbols
2501 fp << replace_tabs( sfp.str() );
2508 //=======================================================================
2509 //function : GetDumpStudyComment
2510 //purpose : return a header comment for a DumpStudy script
2511 //=======================================================================
2513 std::string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
2515 std::stringstream txt;
2516 txt << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
2517 txt << "###" << std::endl;
2518 txt << "### This file is generated automatically by SALOME v"
2519 << KERNEL_VERSION_STR
2520 << " with dump python functionality";
2521 if ( theComponentName )
2522 txt << " (" << theComponentName << " component)";
2524 txt << "###" << std::endl;
2528 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2530 const std::string& Tab,
2531 SALOMEDSImpl_Study* theStudy);
2533 //============================================================================
2537 //============================================================================
2538 void SALOMEDSImpl_Study::dump(const std::string& theFileName)
2540 //Create a file that will contain a main Study script
2542 fp.open(theFileName.c_str(), std::ios::out);
2545 bool isOpened = fp.is_open();
2547 bool isOpened = fp.rdbuf()->is_open();
2551 _errorCode = std::string("Can't create a file ")+theFileName;
2552 std::cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << std::endl;
2556 SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
2557 fp << "0:1" << std::endl;
2558 SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
2559 std::string aTab(" ");
2560 for(; Itr.More(); Itr.Next()) {
2561 dumpSO(Itr.Value(), fp, aTab, this);
2568 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2570 const std::string& Tab,
2571 SALOMEDSImpl_Study* theStudy)
2573 std::string aTab(Tab), anID(theSO.GetID());
2574 fp << aTab << anID << std::endl;
2575 std::vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
2576 for(int i = 0; i<attribs.size(); i++) {
2577 SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
2583 std::string aType = anAttr->GetClassType();
2584 fp << Tab << " -- " << aType;
2586 if(aType == std::string("AttributeReal")) {
2587 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
2589 else if(aType == std::string("AttributeInteger")) {
2590 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
2592 else if(aType == std::string("AttributeName")) {
2593 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
2595 else if(aType == std::string("AttributeComment")) {
2596 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
2598 else if(aType == std::string("AttributeReference")) {
2599 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
2604 SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
2605 std::string aNewTab(" ");
2607 for(; Itr.More(); Itr.Next()) {
2608 dumpSO(Itr.Value(), fp, aNewTab, theStudy);
2614 void SALOMEDSImpl_Study::Modify()
2617 _doc->SetModified(true);
2620 //============================================================================
2624 //============================================================================
2625 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
2627 if (theSavePoint < -1) return NULL;
2628 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2629 SALOMEDSImpl_SObject so = FindComponent((char*)theID);
2630 if (!so) so = builder->NewComponent((char*)theID);
2631 SALOMEDSImpl_AttributeParameter* attParam = NULL;
2633 if (theSavePoint == -1) {
2635 DF_Label savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2636 DF_Label prevPointLabel;
2637 while ( !savePointLabel.IsNull() ) {
2639 prevPointLabel = savePointLabel;
2640 savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2642 if ( !prevPointLabel.IsNull() )
2643 so = GetSObject( prevPointLabel );
2645 if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
2646 DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
2647 if ( !savePointLabel.IsNull() )
2648 so = GetSObject( savePointLabel );
2649 else // ... if it does not exist - create a new one
2650 so = builder->NewObjectToTag( so, theSavePoint );
2655 builder->FindAttribute(so, A, "AttributeParameter");
2656 if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
2657 A = builder->FindOrCreateAttribute(so, "AttributeParameter");
2659 attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
2664 //============================================================================
2668 //============================================================================
2669 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID,
2670 const char* theModuleName,
2673 if(theSavePoint < -1) return NULL;
2674 SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
2675 SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
2676 SALOMEDSImpl_AttributeParameter* par = NULL;
2678 SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
2679 std::string moduleName(theModuleName);
2680 for(; it.More(); it.Next()) {
2681 SALOMEDSImpl_SObject so(it.Value());
2682 if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
2683 if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
2684 if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
2688 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2689 SALOMEDSImpl_SObject so = builder->NewObject(main_so);
2690 par = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
2691 par->SetString("AP_MODULE_NAME", moduleName);
2695 //============================================================================
2696 /*! Function : SetStudyLock
2699 //============================================================================
2700 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
2702 _lockers.push_back(theLockerID);
2705 //============================================================================
2706 /*! Function : IsStudyLocked
2709 //============================================================================
2710 bool SALOMEDSImpl_Study::IsStudyLocked()
2712 return (_lockers.size() > 0);
2715 //============================================================================
2716 /*! Function : UnLockStudy
2719 //============================================================================
2720 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
2722 std::vector<std::string>::iterator vsI = _lockers.begin();
2723 int length = _lockers.size();
2724 bool isFound = false;
2725 std::string id(theLockerID);
2726 for(int i = 0; i<length; i++, vsI++) {
2727 if(id == _lockers[i]) {
2732 if(isFound) _lockers.erase(vsI);
2735 //============================================================================
2736 /*! Function : GetLockerID
2739 //============================================================================
2740 std::vector<std::string> SALOMEDSImpl_Study::GetLockerID()
2745 //============================================================================
2746 /*! Function : SetVariable
2749 //============================================================================
2750 void SALOMEDSImpl_Study::SetVariable(const std::string& theVarName,
2751 const double theValue,
2752 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2754 bool modified = false;
2755 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2757 if( aGVar == NULL ) {
2759 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2761 aSVar->setValue(theValue);
2762 myNoteBookVars.push_back(aSVar);
2766 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2767 modified = aSVar->setValue(theValue) || modified;
2768 modified = aSVar->setType(theType) || modified;
2775 //============================================================================
2776 /*! Function : SetStringVariable
2779 //============================================================================
2780 void SALOMEDSImpl_Study::SetStringVariable(const std::string& theVarName,
2781 const std::string& theValue,
2782 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2784 bool modified = false;
2785 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2787 if( aGVar == NULL ) {
2789 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2791 aSVar->setStringValue(theValue);
2792 myNoteBookVars.push_back(aSVar);
2796 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2797 modified = aSVar->setStringValue(theValue) || modified;
2798 modified = aSVar->setType(theType) || modified;
2805 //============================================================================
2806 /*! Function : SetStringVariableAsDouble
2809 //============================================================================
2810 void SALOMEDSImpl_Study::SetStringVariableAsDouble(const std::string& theVarName,
2811 const double theValue,
2812 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2814 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2815 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2816 aSVar->setValue(theValue);
2819 //============================================================================
2820 /*! Function : GetReal
2823 //============================================================================
2824 double SALOMEDSImpl_Study::GetVariableValue(const std::string& theVarName)
2826 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2829 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2830 return aSVar->getValue();
2835 //============================================================================
2836 /*! Function : GetString
2839 //============================================================================
2840 std::string SALOMEDSImpl_Study::GetStringVariableValue(const std::string& theVarName)
2842 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2845 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2846 return aSVar->getStringValue();
2851 //============================================================================
2852 /*! Function : IsTypeOf
2855 //============================================================================
2856 bool SALOMEDSImpl_Study::IsTypeOf(const std::string& theVarName,
2857 SALOMEDSImpl_GenericVariable::
2858 VariableTypes theType) const
2860 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2863 return aGVar->Type() == theType;
2868 //============================================================================
2869 /*! Function : IsVariable
2872 //============================================================================
2873 bool SALOMEDSImpl_Study::IsVariable(const std::string& theVarName) const
2875 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2876 return (aGVar != NULL);
2879 //============================================================================
2880 /*! Function : GetVariableNames
2883 //============================================================================
2884 std::vector<std::string> SALOMEDSImpl_Study::GetVariableNames() const
2886 std::vector<std::string> aResult;
2888 for(int i = 0; i < myNoteBookVars.size(); i++)
2889 aResult.push_back(myNoteBookVars[i]->Name());
2894 //============================================================================
2895 /*! Function : AddVariable
2898 //============================================================================
2899 void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
2901 myNoteBookVars.push_back(theVariable);
2904 //============================================================================
2905 /*! Function : AddVariable
2908 //============================================================================
2909 SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
2911 SALOMEDSImpl_GenericVariable* aResult = NULL;
2912 for(int i = 0; i < myNoteBookVars.size();i++) {
2913 if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
2914 aResult = myNoteBookVars[i];
2921 //============================================================================
2922 /*! Function : RemoveVariable
2925 //============================================================================
2926 bool SALOMEDSImpl_Study::RemoveVariable(const std::string& theVarName)
2928 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2932 std::string aValue = aVariable->SaveToScript();
2933 ReplaceVariableAttribute( theVarName, aValue );
2935 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2936 for( ; it != itEnd; it++ )
2938 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2939 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2941 myNoteBookVars.erase( it );
2950 //============================================================================
2951 /*! Function : RenameVariable
2954 //============================================================================
2955 bool SALOMEDSImpl_Study::RenameVariable(const std::string& theVarName, const std::string& theNewVarName)
2957 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2961 ReplaceVariableAttribute( theVarName, theNewVarName );
2963 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2964 for( ; it != itEnd; it++ )
2966 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2967 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2969 aVariableRef->setName( theNewVarName );
2978 //============================================================================
2979 /*! Function : IsVariableUsed
2982 //============================================================================
2983 bool SALOMEDSImpl_Study::IsVariableUsed(const std::string& theVarName)
2985 return FindVariableAttribute( theVarName );
2988 //============================================================================
2989 /*! Function : FindVariableAttribute
2992 //============================================================================
2993 bool SALOMEDSImpl_Study::FindVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
2994 SALOMEDSImpl_SObject theSObject,
2995 const std::string& theName)
2997 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
2998 for( ; anIter.More(); anIter.Next() )
2999 if( FindVariableAttribute( theStudyBuilder, anIter.Value(), theName ) )
3002 DF_Attribute* anAttr;
3003 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
3005 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
3007 std::string aString = aStringAttr->Value();
3009 std::vector< std::vector<std::string> > aSections = ParseVariables( aString );
3010 for( int i = 0, n = aSections.size(); i < n; i++ )
3012 std::vector<std::string> aVector = aSections[i];
3013 for( int j = 0, m = aVector.size(); j < m; j++ )
3015 std::string aStr = aVector[j];
3016 if( aStr.compare( theName ) == 0 )
3025 //============================================================================
3026 /*! Function : FindVariableAttribute
3029 //============================================================================
3030 bool SALOMEDSImpl_Study::FindVariableAttribute(const std::string& theName)
3032 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
3033 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
3034 for( ; aCompIter.More(); aCompIter.Next() )
3036 SALOMEDSImpl_SObject aComp = aCompIter.Value();
3037 if( FindVariableAttribute( aStudyBuilder, aComp, theName ) )
3043 //============================================================================
3044 /*! Function : ReplaceVariableAttribute
3047 //============================================================================
3048 void SALOMEDSImpl_Study::ReplaceVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
3049 SALOMEDSImpl_SObject theSObject,
3050 const std::string& theSource,
3051 const std::string& theDest)
3053 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
3054 for( ; anIter.More(); anIter.Next() )
3055 ReplaceVariableAttribute( theStudyBuilder, anIter.Value(), theSource, theDest );
3057 DF_Attribute* anAttr;
3058 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
3060 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
3062 bool isChanged = false;
3063 std::string aNewString, aCurrentString = aStringAttr->Value();
3065 std::vector< std::vector<std::string> > aSections = ParseVariables( aCurrentString );
3066 for( int i = 0, n = aSections.size(); i < n; i++ )
3068 std::vector<std::string> aVector = aSections[i];
3069 for( int j = 0, m = aVector.size(); j < m; j++ )
3071 std::string aStr = aVector[j];
3072 if( aStr.compare( theSource ) == 0 )
3078 aNewString.append( aStr );
3080 aNewString.append( ":" );
3083 aNewString.append( "|" );
3087 aStringAttr->SetValue( aNewString );
3092 //============================================================================
3093 /*! Function : ReplaceVariableAttribute
3096 //============================================================================
3097 void SALOMEDSImpl_Study::ReplaceVariableAttribute(const std::string& theSource, const std::string& theDest)
3099 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
3100 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
3101 for( ; aCompIter.More(); aCompIter.Next() )
3103 SALOMEDSImpl_SObject aComp = aCompIter.Value();
3104 ReplaceVariableAttribute( aStudyBuilder, aComp, theSource, theDest );
3108 //============================================================================
3109 /*! Function : ParseVariables
3112 //============================================================================
3113 std::vector< std::vector< std::string > > SALOMEDSImpl_Study::ParseVariables(const std::string& theVariables) const
3115 return SALOMEDSImpl_Tool::splitStringWithEmpty( theVariables, OPERATION_SEPARATOR, VARIABLE_SEPARATOR );
3118 //============================================================================
3119 /*! Function : EnableUseCaseAutoFilling
3122 //============================================================================
3123 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
3125 _errorCode = ""; _autoFill = isEnabled;
3127 _builder->SetOnAddSObject(_cb);
3128 _builder->SetOnRemoveSObject(_cb);
3131 _builder->SetOnAddSObject(NULL);
3132 _builder->SetOnRemoveSObject(NULL);
3136 //============================================================================
3137 /*! Function : GetIORs
3140 //============================================================================
3141 std::vector<std::string> SALOMEDSImpl_Study::GetIORs()
3143 std::vector<std::string> anIORs;
3144 std::map<std::string, DF_Label>::const_iterator MI;
3145 for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
3146 anIORs.push_back(MI->first);
3151 //============================================================================
3152 /*! Function : addSO_Notification
3153 * Purpose : This function tells all the observers that a SO has been added
3155 //============================================================================
3156 bool SALOMEDSImpl_Study::addSO_Notification (const SALOMEDSImpl_SObject& theSObject)
3159 return _notifier->addSO_Notification(theSObject);
3164 //============================================================================
3165 /*! Function : removeSO_Notification
3166 * Purpose : This function tells all the observers that a SO has been removed
3168 //============================================================================
3169 bool SALOMEDSImpl_Study::removeSO_Notification (const SALOMEDSImpl_SObject& theSObject)
3172 return _notifier->removeSO_Notification(theSObject);
3177 //============================================================================
3178 /*! Function : modifySO_Notification
3179 * Purpose : This function tells all the observers that a SO has been modified and
3180 pass the mofification reason
3182 //============================================================================
3183 bool SALOMEDSImpl_Study::modifySO_Notification (const SALOMEDSImpl_SObject& theSObject, int reason)
3186 return _notifier->modifySO_Notification(theSObject, reason);
3191 //============================================================================
3192 /*! Function : setNotifier
3193 * Purpose : register a notifier
3195 //============================================================================
3196 void SALOMEDSImpl_Study::setNotifier(SALOMEDSImpl_AbstractCallback* notifier)
3201 //#######################################################################################################
3202 //# STATIC PRIVATE FUNCTIONS
3203 //#######################################################################################################
3205 //============================================================================
3206 /*! Function : SaveAttributes
3207 * Purpose : Save attributes for object
3209 //============================================================================
3210 static void SaveAttributes(const SALOMEDSImpl_SObject& aSO, HDFgroup *hdf_group_sobject)
3213 std::vector<DF_Attribute*> attrList = aSO.GetLabel().GetAttributes();
3214 DF_Attribute* anAttr = NULL;
3215 for(int i = 0, len = attrList.size(); i<len; i++) {
3216 anAttr = attrList[i];
3217 //The following attributes are not supposed to be written to the file
3218 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
3219 if(type == std::string("AttributeIOR")) continue; //IOR attribute is not saved
3220 std::string aSaveStr =anAttr->Save();
3221 //cout << "Saving: " << aSO.GetID() << " type: "<< type<<"|" << endl;
3222 size[0] = (hdf_int32) strlen(aSaveStr.c_str()) + 1;
3223 HDFdataset *hdf_dataset = new HDFdataset((char*)type.c_str(), hdf_group_sobject, HDF_STRING,size, 1);
3224 hdf_dataset->CreateOnDisk();
3225 hdf_dataset->WriteOnDisk((char*)aSaveStr.c_str());
3226 hdf_dataset->CloseOnDisk();
3227 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
3231 //===========================================================================
3232 //Function : ReadAttributes
3233 //===========================================================================
3234 static void ReadAttributes(SALOMEDSImpl_Study* theStudy,
3235 const SALOMEDSImpl_SObject& aSO,
3236 HDFdataset* hdf_dataset)
3238 hdf_dataset->OpenOnDisk();
3240 DF_Attribute* anAttr = NULL;
3241 char* current_string = new char[hdf_dataset->GetSize()+1];
3242 hdf_dataset->ReadFromDisk(current_string);
3243 //cout << "Reading attr type = " << hdf_dataset->GetName() << " SO = " << aSO.GetID() << endl;
3244 if (!strcmp(hdf_dataset->GetName(),"COMPONENTDATATYPE")) {
3245 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, "AttributeComment");
3247 else if (!strcmp(hdf_dataset->GetName(),"AttributeReference") ||
3248 !strcmp(hdf_dataset->GetName(),"Reference")) { // Old format maintainance
3249 theStudy->NewBuilder()->Addreference(aSO, theStudy->CreateObjectID(current_string));
3250 delete [] (current_string);
3251 hdf_dataset->CloseOnDisk();
3255 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, hdf_dataset->GetName());
3259 anAttr->Load(current_string);
3262 delete [] (current_string);
3263 hdf_dataset->CloseOnDisk();
3266 //============================================================================
3267 //Function : BuildlTree
3268 //============================================================================
3269 static void BuildTree (SALOMEDSImpl_Study* theStudy, HDFgroup* hdf_current_group)
3271 hdf_current_group->OpenOnDisk();
3272 SALOMEDSImpl_SObject aSO;
3273 char* Entry = hdf_current_group->GetName();
3274 if (strcmp(Entry,"STUDY_STRUCTURE") == 0) {
3275 aSO = theStudy->CreateObjectID("0:1");
3278 aSO = theStudy->CreateObjectID(Entry);
3281 char name[HDF_NAME_MAX_LEN+1];
3282 int nbsons = hdf_current_group->nInternalObjects();
3283 for (int i=0; i<nbsons; i++) {
3284 hdf_current_group->InternalObjectIndentify(i,name);
3285 if (strncmp(name, "INTERNAL_COMPLEX",16) == 0) continue;
3286 hdf_object_type type = hdf_current_group->InternalObjectType(name);
3288 if (type == HDF_DATASET) {
3289 HDFdataset* new_dataset = new HDFdataset(name,hdf_current_group);
3290 ReadAttributes(theStudy,aSO,new_dataset);
3291 new_dataset = 0; // will be deleted by father destructor
3293 else if (type == HDF_GROUP) {
3294 HDFgroup* new_group = new HDFgroup(name,hdf_current_group);
3295 BuildTree (theStudy, new_group);
3296 new_group = 0; // will be deleted by father destructor
3299 hdf_current_group->CloseOnDisk();
3303 //============================================================================
3304 //Function : Translate_IOR_to_persistentID
3305 //============================================================================
3306 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject& so,
3307 SALOMEDSImpl_Driver* engine,
3311 DF_ChildIterator itchild(so.GetLabel());
3312 std::string ior_string, persistent_string, curid;
3314 for (; itchild.More(); itchild.Next()) {
3315 SALOMEDSImpl_SObject current = SALOMEDSImpl_Study::SObject(itchild.Value());
3316 SALOMEDSImpl_AttributeIOR* IOR = NULL;
3317 if ((IOR=(SALOMEDSImpl_AttributeIOR*)current.GetLabel().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
3318 ior_string = IOR->Value();
3320 persistent_string = engine->IORToLocalPersistentID (current, ior_string, isMultiFile, isASCII);
3321 SALOMEDSImpl_AttributePersistentRef::Set(current.GetLabel(), persistent_string);
3323 Translate_IOR_to_persistentID (current, engine, isMultiFile, isASCII);
3327 void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup)
3332 HDFgroup* new_group =0;
3333 HDFdataset* new_dataset =0;
3335 char aVarName[HDF_NAME_MAX_LEN+1];
3336 char *currentVarType = 0;
3337 char *currentVarValue = 0;
3338 char *currentVarIndex = 0;
3340 //Open HDF group with notebook variables
3341 theGroup->OpenOnDisk();
3343 //Get Nb of variables
3344 int aNbVars = theGroup->nInternalObjects();
3346 std::map<int,SALOMEDSImpl_GenericVariable*> aVarsMap;
3348 for( int iVar=0;iVar < aNbVars;iVar++ ) {
3349 theGroup->InternalObjectIndentify(iVar,aVarName);
3350 hdf_object_type type = theGroup->InternalObjectType(aVarName);
3351 if(type == HDF_GROUP) {
3354 new_group = new HDFgroup(aVarName,theGroup);
3355 new_group->OpenOnDisk();
3358 new_dataset = new HDFdataset("VARIABLE_TYPE",new_group);
3359 new_dataset->OpenOnDisk();
3360 currentVarType = new char[new_dataset->GetSize()+1];
3361 new_dataset->ReadFromDisk(currentVarType);
3362 new_dataset->CloseOnDisk();
3363 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3366 if(new_group->ExistInternalObject("VARIABLE_INDEX")) {
3367 new_dataset = new HDFdataset("VARIABLE_INDEX",new_group);
3368 new_dataset->OpenOnDisk();
3369 currentVarIndex = new char[new_dataset->GetSize()+1];
3370 new_dataset->ReadFromDisk(currentVarIndex);
3371 new_dataset->CloseOnDisk();
3372 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3373 order = atoi(currentVarIndex);
3374 delete [] currentVarIndex;
3380 new_dataset = new HDFdataset("VARIABLE_VALUE",new_group);
3381 new_dataset->OpenOnDisk();
3382 currentVarValue = new char[new_dataset->GetSize()+1];
3383 new_dataset->ReadFromDisk(currentVarValue);
3384 new_dataset->CloseOnDisk();
3385 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3387 new_group->CloseOnDisk();
3388 new_group = 0; //will be deleted by hdf_sco_group destructor
3390 SALOMEDSImpl_GenericVariable::VariableTypes aVarType =
3391 SALOMEDSImpl_GenericVariable::String2VariableType(std::string(currentVarType));
3392 delete [] currentVarType;
3394 //Create variable and add it in the study
3395 SALOMEDSImpl_GenericVariable* aVariable =
3396 new SALOMEDSImpl_ScalarVariable(aVarType,std::string(aVarName));
3397 aVariable->Load(std::string(currentVarValue));
3398 aVarsMap.insert(std::make_pair(order,aVariable));
3399 delete [] currentVarValue;
3403 std::map<int,SALOMEDSImpl_GenericVariable*>::const_iterator it= aVarsMap.begin();
3404 for(;it!=aVarsMap.end();it++)
3405 theStudy->AddVariable((*it).second);
3407 theGroup->CloseOnDisk();