1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SALOMEDSImpl_Study.cxx
24 // Author : Sergey RUIN
27 #include "SALOMEDSImpl_Study.hxx"
30 #include <KERNEL_version.h>
31 #include <Basics_Utils.hxx>
32 #include <Basics_DirUtils.hxx>
34 #include "HDFexplorer.hxx"
36 #include "DF_Application.hxx"
37 #include "DF_ChildIterator.hxx"
39 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
40 #include "SALOMEDSImpl_Attributes.hxx"
41 #include "SALOMEDSImpl_UseCaseIterator.hxx"
42 #include "SALOMEDSImpl_AttributeReference.hxx"
43 #include "SALOMEDSImpl_StudyHandle.hxx"
44 #include "SALOMEDSImpl_Tool.hxx"
45 #include "SALOMEDSImpl_IParameters.hxx"
46 #include "SALOMEDSImpl_ScalarVariable.hxx"
47 #include "SALOMEDSImpl_SComponent.hxx"
54 // comment out the following define to enable \t symbols in in the python dump files
57 #define DIRECTORYID 16661
58 #define FILELOCALID 26662
59 #define FILEID "FILE: "
60 #define VARIABLE_SEPARATOR ':'
61 #define OPERATION_SEPARATOR '|'
62 #define USE_CASE_LABEL_ID "0:2"
64 static void SaveAttributes(const SALOMEDSImpl_SObject& SO, HDFgroup *hdf_group_sobject);
65 static void ReadAttributes(SALOMEDSImpl_Study*, const SALOMEDSImpl_SObject&, HDFdataset* );
66 static void BuildTree (SALOMEDSImpl_Study*, HDFgroup*);
67 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject&,
68 SALOMEDSImpl_Driver*, bool isMultiFile, bool isASCII);
69 static void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup);
75 StudyUnlocker( SALOMEDSImpl_Study* study ): myStudy( study ), myLocked( false )
77 myPrevLocked = myStudy->GetProperties()->IsLocked();
87 myStudy->GetProperties()->SetLocked(true);
88 myPrevLocked = myLocked;
95 myStudy->GetProperties()->SetLocked(false);
96 myLocked = myPrevLocked;
101 SALOMEDSImpl_Study* myStudy;
107 //============================================================================
108 /*! Function : SALOMEDSImpl_Study
109 * Purpose : SALOMEDSImpl_Study constructor
111 //============================================================================
112 SALOMEDSImpl_Study::SALOMEDSImpl_Study()
114 _appli = new DF_Application();
115 _clipboard = _appli->NewDocument("SALOME_STUDY");
120 //============================================================================
121 /*! Function : ~SALOMEDSImpl_Study
122 * Purpose : SALOMEDSImpl_Study destructor
124 //============================================================================
125 SALOMEDSImpl_Study::~SALOMEDSImpl_Study()
128 _appli->Close(_clipboard);
129 // Destroy application
133 //============================================================================
135 * Purpose : Initialize study components
137 //============================================================================
138 void SALOMEDSImpl_Study::Init()
141 std::stringstream sstrm;
143 _name = "Study" + std::string(sstrm.str());
144 _doc = _appli->NewDocument("SALOME_STUDY");
149 _useCaseBuilder = new SALOMEDSImpl_UseCaseBuilder(_doc);
150 _builder = new SALOMEDSImpl_StudyBuilder(this);
151 _cb = new SALOMEDSImpl_Callback(_useCaseBuilder);
154 //Put on the root label a StudyHandle attribute to store the address of this object
155 //It will be used to retrieve the study object by DF_Label that belongs to the study
156 SALOMEDSImpl_StudyHandle::Set(_doc->Main().Root(), this);
158 // set Study properties
159 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
161 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
162 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
163 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
164 mn, hh, day, month, year);
165 aProp->SetCreationMode(1); //"from scratch"
168 //============================================================================
170 * Purpose : Clear study components
172 //============================================================================
173 void SALOMEDSImpl_Study::Clear()
177 delete _useCaseBuilder;
184 //============================================================================
186 * Purpose : Open a Study from it's persistent reference
188 //============================================================================
189 bool SALOMEDSImpl_Study::Open(const std::string& aUrl)
193 // Set "C" locale temporarily to avoid possible localization problems
194 Kernel_Utils::Localizer loc;
199 HDFfile *hdf_file =0;
200 HDFgroup *hdf_group_study_structure =0;
201 HDFgroup *hdf_notebook_vars = 0;
205 bool isASCII = false;
206 if (HDFascii::isASCII(aUrl.c_str())) {
208 char* aResultPath = HDFascii::ConvertFromASCIIToHDF(aUrl.c_str());
211 aC_HDFUrl = new char[strlen(aResultPath) + 19];
212 sprintf(aC_HDFUrl, "%shdf_from_ascii.hdf", aResultPath);
213 delete [] (aResultPath);
221 hdf_file = new HDFfile((char*)aHDFUrl.c_str());
223 hdf_file->OpenOnDisk(HDF_RDONLY);// mpv: was RDWR, but opened file can be write-protected too
228 eStr = new char[strlen(aUrl.c_str())+17];
229 sprintf(eStr,"Can't open file %s",aUrl.c_str());
231 _errorCode = std::string(eStr);
235 // Assign the value of the URL in the study object
238 SALOMEDSImpl_AttributePersistentRef::Set(_doc->Main(), aUrl);
240 if (!hdf_file->ExistInternalObject("STUDY_STRUCTURE")) {
241 _errorCode = "Study is empty";
245 //Create the Structure of the Document
246 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
249 BuildTree (this, hdf_group_study_structure);
253 char *eStr = new char [strlen(aUrl.c_str())+17];
254 sprintf(eStr,"Can't open file %s", aUrl.c_str());
255 _errorCode = std::string(eStr);
259 //Read and create notebook variables
260 if(hdf_file->ExistInternalObject("NOTEBOOK_VARIABLES")) {
261 hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
262 ReadNoteBookVariables(this, hdf_notebook_vars);
263 hdf_notebook_vars =0; //will be deleted by hdf_sco_group destructor
266 hdf_file->CloseOnDisk();
267 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
270 std::vector<std::string> aFilesToRemove;
271 aFilesToRemove.push_back("hdf_from_ascii.hdf");
272 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
275 delete hdf_file; // all related hdf objects will be deleted
277 // unlock study if it is locked, to set components versions
278 StudyUnlocker unlock(this);
280 //For old studies we have to add "unknown" version tag for all stored components
281 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
282 for (; itcomponent.More(); itcomponent.Next())
284 SALOMEDSImpl_SComponent sco = itcomponent.Value();
285 std::string aCompType = sco.GetComment();
286 if ( aCompType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() ) continue;
287 if ( GetProperties()->GetComponentVersions( aCompType ).empty() )
288 GetProperties()->SetComponentVersion( aCompType, "" ); // empty version means "unknown"
294 //============================================================================
296 * Purpose : Save a Study to it's persistent reference
298 //============================================================================
299 bool SALOMEDSImpl_Study::Save(SALOMEDSImpl_DriverFactory* aFactory,
304 std::string url = URL();
306 _errorCode = "No path specified to save the study. Nothing done";
310 return Impl_SaveAs(url, aFactory, theMultiFile, false);
316 bool SALOMEDSImpl_Study::SaveASCII(SALOMEDSImpl_DriverFactory* aFactory,
321 std::string url = URL();
323 _errorCode = "No path specified to save the study. Nothing done";
327 return Impl_SaveAs(url, aFactory, theMultiFile, true);
333 //=============================================================================
334 /*! Function : SaveAs
335 * Purpose : Save a study to the persistent reference aUrl
337 //============================================================================
338 bool SALOMEDSImpl_Study::SaveAs(const std::string& aUrl,
339 SALOMEDSImpl_DriverFactory* aFactory,
343 return Impl_SaveAs(aUrl, aFactory, theMultiFile, false);
346 bool SALOMEDSImpl_Study::SaveAsASCII(const std::string& aUrl,
347 SALOMEDSImpl_DriverFactory* aFactory,
351 return Impl_SaveAs(aUrl, aFactory, theMultiFile, true);
354 //=============================================================================
355 /*! Function : _SaveProperties
356 * Purpose : save the study properties in HDF file
358 //============================================================================
359 bool SALOMEDSImpl_Study::Impl_SaveProperties(HDFgroup *hdf_group)
363 HDFdataset *hdf_dataset = 0;
367 // add modifications list (user and date of save)
368 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
370 // unlock study if it is locked, to set modification date
371 StudyUnlocker unlock(this);
373 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
374 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
375 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
376 mn, hh, day, month, year);
378 // lock study back if it was locked initially, to write correct value of Locked flag
381 std::vector<std::string> aNames;
382 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
384 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
386 std::string units = aProp->GetUnits();
387 std::string comment = aProp->GetComment();
389 std::map< std::string, std::vector<std::string> > allVersions = aProp->GetComponentsVersions();
390 std::map<std::string, std::string> versions;
392 int aLength = 0, aLength1 = 0, anIndex, i, unitsSize = 0, commentSize = 0;
394 for(i=1; i<=aNames.size(); i++)
395 aLength += aNames[i-1].size() + 1;
397 std::map< std::string, std::vector<std::string> >::const_iterator it;
398 for (it = allVersions.begin(); it != allVersions.end(); ++it ) {
399 std::string vlist = "";
400 std::vector<std::string> vl = it->second;
401 std::vector<std::string>::const_iterator vlit;
402 for ( vlit = vl.begin(); vlit != vl.end(); ++vlit ) {
403 if ( vlist != "" ) vlist += ";";
406 versions[ it->first ] = vlist;
407 aLength1 += it->first.size() + vlist.size() + 2;
410 unitsSize = units.size();
411 commentSize = comment.size();
414 //locked flag, modified flag,
415 //minutes, hours, day, months, year, user name, char(1),
416 //minutes, hours, day, months, year, user name, char(1),
417 //.....................................................,
418 //.....................................................,
419 //.....................................................,
420 //minutes, hours, day, months, year, user name, char(1), char(30) <- !!!! used to define end of section with modifications !!!!
421 //units, char(1), comment, char(30) <- !!!! used to define start of section with components' versions !!!!
422 //component=versions, char(1),
423 //component=versions, char(1),
424 //...........................,
425 //component=versions, char(1), char(0)
427 //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)
428 // unit length + 1, comment length, "zero" byte
430 char* aProperty = new char[3 + aLength + 12 * aNames.size() + 1 + unitsSize + 1 + commentSize + 1 + aLength1 ];
432 sprintf(aProperty,"%c%c", (char)aProp->GetCreationMode(), (aProp->IsLocked())?'l':'u');
434 aLength = aNames.size();
436 for(anIndex = 0; anIndex<aLength; anIndex++) {
437 sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
438 (int)(aMinutes[anIndex]),
439 (int)(aHours[anIndex]),
440 (int)(aDays[anIndex]),
441 (int)(aMonths[anIndex]),
442 (int)(aYears[anIndex]),
443 aNames[anIndex].c_str());
444 a = strlen(aProperty);
448 //Write delimeter of the section to define end of the modifications section
451 //Write units if need
452 if(units.size() > 0) {
453 sprintf(&(aProperty[a]),"%s",units.c_str());
454 a = strlen(aProperty);
459 //Write comments if need
460 if(comment.size() > 0) {
461 sprintf(&(aProperty[a]),"%s",comment.c_str());
462 a = strlen(aProperty);
465 aProperty[a++] = 30; //delimeter of the component versions
467 std::map<std::string, std::string>::const_iterator versionsIt;
468 for ( versionsIt = versions.begin(); versionsIt != versions.end(); ++versionsIt ) {
469 sprintf(&(aProperty[a]),"%s=%s",
470 (char*)(versionsIt->first.c_str()),
471 (char*)(versionsIt->second.c_str()));
472 a = a + versionsIt->first.size() + versionsIt->second.size() + 1;
478 name_len = (hdf_int32) a;
479 size[0] = name_len + 1 ;
480 hdf_dataset = new HDFdataset("AttributeStudyProperties",hdf_group,HDF_STRING,size,1);
481 hdf_dataset->CreateOnDisk();
482 hdf_dataset->WriteOnDisk(aProperty);
483 hdf_dataset->CloseOnDisk();
484 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
487 aProp->SetModified(0);
491 //=============================================================================
492 /*! Function : _SaveAs
493 * Purpose : save the study in HDF file
495 //============================================================================
496 bool SALOMEDSImpl_Study::Impl_SaveAs(const std::string& aStudyUrl,
497 SALOMEDSImpl_DriverFactory* aFactory,
501 // Set "C" locale temporarily to avoid possible localization problems
502 Kernel_Utils::Localizer loc;
504 // HDF File will be composed of differents part :
505 // * For each ComponentDataType, all data created by the component
506 // Informations in data group hdf_group_datacomponent
507 // * Study Structure -> Exactly what is contained in Document
508 // Informations in data group hdf_group_study_structure
513 HDFgroup *hdf_group_study_structure =0;
514 HDFgroup *hdf_sco_group =0;
515 HDFgroup *hdf_sco_group2 =0;
516 HDFgroup *hdf_notebook_vars =0;
517 HDFgroup *hdf_notebook_var = 0;
519 HDFgroup *hdf_group_datacomponent =0;
520 HDFdataset *hdf_dataset =0;
522 hdf_int32 name_len = 0;
523 std::string component_name;
525 // Store previous URL
526 std::string anOldName = Name();
528 // Map to store components' versions
529 std::map<std::string, std::string> componentVersions;
531 //Create a temporary url to which the study is saved
532 std::string aUrl = SALOMEDSImpl_Tool::GetTmpDir() + SALOMEDSImpl_Tool::GetNameFromPath(aStudyUrl);
534 // unlock study if it is locked, as some attributes need to be modified
535 StudyUnlocker unlock(this);
537 SALOMEDSImpl_StudyBuilder* SB= NewBuilder();
538 std::map<std::string, SALOMEDSImpl_Driver*> aMapTypeDriver;
541 // mpv 15.12.2003: for saving components we have to load all data from all modules
542 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
543 for (; itcomponent.More(); itcomponent.Next()) {
544 SALOMEDSImpl_SComponent sco = itcomponent.Value();
545 // if there is an associated Engine call its method for saving
546 std::string IOREngine;
548 SALOMEDSImpl_Driver* aDriver = NULL;
549 std::string aCompType = sco.GetComment();
550 if (!sco.ComponentIOR(IOREngine)) {
551 if (!aCompType.empty()) {
552 aDriver = aFactory->GetDriverByType(aCompType);
553 if (aDriver != NULL) {
554 if (!SB->LoadWith(sco, aDriver)) {
555 _errorCode = SB->GetErrorCode();
562 aDriver = aFactory->GetDriverByIOR(IOREngine);
564 aMapTypeDriver[aCompType] = aDriver;
567 _errorCode = "Can not restore information to resave it";
572 // VSR: set URL to new file name
573 // VSR: remember to set previous name if save operation fails
576 // To change for Save
577 // Do not have to do a new file but just a Open??? Rewrite all informations after erasing evrything??
578 hdf_file = new HDFfile((char*)aUrl.c_str());
579 hdf_file->CreateOnDisk();
581 //-----------------------------------------------------------------------
582 // 1 - Create a groupe for each SComponent and Update the PersistanceRef
583 //-----------------------------------------------------------------------
584 hdf_group_datacomponent = new HDFgroup("DATACOMPONENT",hdf_file);
585 hdf_group_datacomponent->CreateOnDisk();
587 for (itcomponent.Init(); itcomponent.More(); itcomponent.Next()) {
588 SALOMEDSImpl_SComponent sco = itcomponent.Value();
590 std::string scoid = sco.GetID();
591 hdf_sco_group = new HDFgroup((char*)scoid.c_str(), hdf_group_datacomponent);
592 hdf_sco_group->CreateOnDisk();
594 std::string componentDataType = sco.ComponentDataType();
595 std::string IOREngine;
596 if (sco.ComponentIOR(IOREngine)) {
597 // Engine should be already in the map as it was to added before
598 SALOMEDSImpl_Driver* Engine = aMapTypeDriver[componentDataType];
599 if (Engine != NULL) {
600 SALOMEDSImpl_TMPFile* aStream = NULL;
603 componentVersions[ componentDataType ] = Engine->Version();
605 if (theASCII) aStream = Engine->SaveASCII(sco,
606 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
609 else aStream = Engine->Save(sco,
610 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
613 HDFdataset *hdf_dataset;
614 hdf_size aHDFSize[1];
615 if (length > 0) { //The component saved some auxiliary files, then put them into HDF file
616 aHDFSize[0] = length;
618 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group, HDF_STRING, aHDFSize, 1);
619 hdf_dataset->CreateOnDisk();
620 hdf_dataset->WriteOnDisk(aStream->Data()); //Save the stream in the HDF file
621 hdf_dataset->CloseOnDisk();
624 if (aStream) delete aStream;
626 // store multifile state
628 hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
629 hdf_dataset->CreateOnDisk();
630 hdf_dataset->WriteOnDisk((void*)(theMultiFile?"M":"S")); // save: multi or single
631 hdf_dataset->CloseOnDisk();
632 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
635 hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
636 hdf_dataset->CreateOnDisk();
637 hdf_dataset->WriteOnDisk((void*)(theASCII?"A":"B")); // save: ASCII or BINARY
638 hdf_dataset->CloseOnDisk();
639 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
640 // Creation of the persistance reference attribute
641 Translate_IOR_to_persistentID (sco, Engine, theMultiFile, theASCII);
644 hdf_sco_group->CloseOnDisk();
645 hdf_sco_group=0; // will be deleted by hdf_group_datacomponent destructor
647 hdf_group_datacomponent->CloseOnDisk();
648 hdf_group_datacomponent =0; // will be deleted by hdf_file destructor
650 //-----------------------------------------------------------------------
651 //3 - Write the Study Structure
652 //-----------------------------------------------------------------------
653 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
654 hdf_group_study_structure->CreateOnDisk();
655 // save component attributes
656 for (itcomponent.Init(); itcomponent.More(); itcomponent.Next()) {
657 SALOMEDSImpl_SComponent SC = itcomponent.Value();
658 std::string scid = SC.GetID();
659 hdf_sco_group2 = new HDFgroup((char*)scid.c_str(), hdf_group_study_structure);
660 hdf_sco_group2->CreateOnDisk();
661 SaveAttributes(SC, hdf_sco_group2);
662 // ComponentDataType treatment
663 component_name = SC.ComponentDataType();
664 name_len = (hdf_int32)component_name.length();
665 size[0] = name_len +1 ;
666 hdf_dataset = new HDFdataset("COMPONENTDATATYPE",hdf_sco_group2,HDF_STRING,size,1);
667 hdf_dataset->CreateOnDisk();
668 hdf_dataset->WriteOnDisk((char*)component_name.c_str());
669 hdf_dataset->CloseOnDisk();
670 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
671 Impl_SaveObject(SC, hdf_sco_group2);
672 hdf_sco_group2->CloseOnDisk();
673 hdf_sco_group2=0; // will be deleted by hdf_group_study_structure destructor
675 //-----------------------------------------------------------------------
676 //4 - Write the Study UseCases Structure
677 //-----------------------------------------------------------------------
678 SALOMEDSImpl_SObject aSO = FindObjectID(USE_CASE_LABEL_ID);
680 HDFgroup *hdf_soo_group = new HDFgroup(USE_CASE_LABEL_ID,hdf_group_study_structure);
681 hdf_soo_group->CreateOnDisk();
682 SaveAttributes(aSO, hdf_soo_group);
683 Impl_SaveObject(aSO, hdf_soo_group);
684 hdf_soo_group->CloseOnDisk();
685 hdf_soo_group=0; // will be deleted by hdf_group_study_structure destructor
687 //-----------------------------------------------------------------------
688 //5 - Write the NoteBook Variables
689 //-----------------------------------------------------------------------
691 //5.1 Create group to store all note book variables
692 hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
693 hdf_notebook_vars->CreateOnDisk();
695 std::string varValue;
697 std::string varIndex;
699 for (int i=0 ;i < myNoteBookVars.size(); i++ ) {
700 // For each variable create HDF group
701 hdf_notebook_var = new HDFgroup((char*)myNoteBookVars[i]->Name().c_str(),hdf_notebook_vars);
702 hdf_notebook_var->CreateOnDisk();
704 // Save Variable type
705 varType = myNoteBookVars[i]->SaveType();
706 name_len = (hdf_int32) varType.length();
707 size[0] = name_len +1 ;
708 hdf_dataset = new HDFdataset("VARIABLE_TYPE",hdf_notebook_var,HDF_STRING,size,1);
709 hdf_dataset->CreateOnDisk();
710 hdf_dataset->WriteOnDisk((char*)varType.c_str());
711 hdf_dataset->CloseOnDisk();
712 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
715 sprintf(buffer,"%d",i);
716 varIndex= std::string(buffer);
717 name_len = (hdf_int32) varIndex.length();
718 size[0] = name_len +1 ;
719 hdf_dataset = new HDFdataset("VARIABLE_INDEX",hdf_notebook_var,HDF_STRING,size,1);
720 hdf_dataset->CreateOnDisk();
721 hdf_dataset->WriteOnDisk((char*)varIndex.c_str());
722 hdf_dataset->CloseOnDisk();
723 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
725 // Save Variable value
726 varValue = myNoteBookVars[i]->Save();
727 name_len = (hdf_int32) varValue.length();
728 size[0] = name_len +1 ;
729 hdf_dataset = new HDFdataset("VARIABLE_VALUE",hdf_notebook_var,HDF_STRING,size,1);
730 hdf_dataset->CreateOnDisk();
731 hdf_dataset->WriteOnDisk((char*)varValue.c_str());
732 hdf_dataset->CloseOnDisk();
733 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
734 hdf_notebook_var->CloseOnDisk();
735 hdf_notebook_var = 0; //will be deleted by hdf_sco_group destructor
737 hdf_notebook_vars->CloseOnDisk();
738 hdf_notebook_vars = 0; //will be deleted by hdf_sco_group destructor
740 // record component versions
741 std::map<std::string, std::string>::const_iterator itVersions;
742 for ( itVersions = componentVersions.begin(); itVersions != componentVersions.end(); ++itVersions )
743 GetProperties()->SetComponentVersion( itVersions->first, itVersions->second );
745 // lock study back if it was locked initially, to write correct value of Locked flag
748 //-----------------------------------------------------------------------
749 //6 - Write the Study Properties
750 //-----------------------------------------------------------------------
751 std::string study_name = Name();
752 name_len = (hdf_int32) study_name.size();
753 size[0] = name_len +1 ;
754 hdf_dataset = new HDFdataset("STUDY_NAME",hdf_group_study_structure,HDF_STRING,size,1);
755 hdf_dataset->CreateOnDisk();
756 hdf_dataset->WriteOnDisk((char*)study_name.c_str());
757 hdf_dataset->CloseOnDisk();
758 hdf_dataset=0; // will be deleted by hdf_group_study_structure destructor
760 Impl_SaveProperties(hdf_group_study_structure);
761 hdf_group_study_structure->CloseOnDisk();
762 hdf_file->CloseOnDisk();
764 hdf_group_study_structure =0; // will be deleted by hdf_file destructor
765 delete hdf_file; // recursively deletes all hdf objects...
767 catch (HDFexception) {
768 _errorCode = "HDFexception ! ";
769 URL( anOldName ); // VSR: restore previous url if operation is failed
772 catch (std::exception& exc) {
773 _errorCode = const_cast<char*>(exc.what());
774 URL( anOldName ); // VSR: restore previous url if operation is failed
778 _errorCode = "Unknown exception ! ";
779 URL( anOldName ); // VSR: restore previous url if operation is failed
782 if (theASCII) { // save file in ASCII format
783 HDFascii::ConvertFromHDFToASCII(aUrl.c_str(), true);
786 // Now it's necessary to copy files from the temporary directory to the user defined directory.
787 // The easiest way to get a list of file in the temporary directory
789 std::string aCmd, aTmpFileDir = SALOMEDSImpl_Tool::GetTmpDir();
790 std::string aTmpFile = aTmpFileDir +"files";
791 std::string aStudyTmpDir = SALOMEDSImpl_Tool::GetDirFromPath(aUrl);
794 aCmd = "dir /B \"" + aStudyTmpDir +"\" > " + aTmpFile;
796 aCmd ="ls -1 \"" + aStudyTmpDir +"\" > " + aTmpFile;
798 system(aCmd.c_str());
800 // Iterate and move files in the temporary directory
801 FILE* fp = fopen(aTmpFile.c_str(), "rb");
803 URL( anOldName ); // VSR: restore previous url if operation is failed
806 char* buffer = new char[2047];
808 while (!feof(fp) && !errors) {
809 if ((fgets(buffer, 2046, fp)) == NULL) break;
810 size_t aLen = strlen(buffer);
811 if (buffer[aLen-1] == '\n') buffer[aLen-1] = char(0);
813 aCmd = "move /Y \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl) +"\"";
815 aCmd = "mv -f \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl)+"\"";
817 errors = system(aCmd.c_str());
825 DeleteFileA(aTmpFile.c_str());
827 unlink(aTmpFile.c_str());
831 RemoveDirectoryA(aTmpFileDir.c_str());
832 RemoveDirectoryA(aStudyTmpDir.c_str());
834 rmdir(aTmpFileDir.c_str());
835 rmdir(aStudyTmpDir.c_str());
839 // VSR: finally, if all is done without errors, mark study as Saved
846 //============================================================================
847 /*! Function : Impl_SaveObject
850 //============================================================================
851 bool SALOMEDSImpl_Study::Impl_SaveObject(const SALOMEDSImpl_SObject& SC,
852 HDFgroup *hdf_group_datatype)
856 // Write in group hdf_group_datatype all informations of SObject SC
857 // Iterative function to parse all SObjects under a SComponent
859 HDFgroup *hdf_group_sobject = 0;
861 DF_ChildIterator itchild(SC.GetLabel());
862 for (; itchild.More(); itchild.Next()) {
863 // mpv: don't save empty labels
864 std::vector<DF_Attribute*> attr = itchild.Value().GetAttributes();
865 if (attr.size() == 0) { //No attributes on the label
866 DF_ChildIterator subchild(itchild.Value());
867 if (!subchild.More()) {
870 subchild.Init(itchild.Value(), true);
872 for (; subchild.More() && anEmpty; subchild.Next()) {
873 std::vector<DF_Attribute*> attr2 = subchild.Value().GetAttributes();
875 anEmpty = false; //There are attributes on the child label
879 if (anEmpty) continue;
882 SALOMEDSImpl_SObject SO = SALOMEDSImpl_Study::SObject(itchild.Value());
884 std::string scoid = SO.GetID();
885 hdf_group_sobject = new HDFgroup(scoid.c_str(), hdf_group_datatype);
886 hdf_group_sobject->CreateOnDisk();
887 SaveAttributes(SO, hdf_group_sobject);
888 Impl_SaveObject(SO, hdf_group_sobject);
889 hdf_group_sobject->CloseOnDisk();
890 hdf_group_sobject =0; // will be deleted by father hdf object destructor
895 //============================================================================
896 /*! Function : CanCopy
899 //============================================================================
900 bool SALOMEDSImpl_Study::CanCopy(const SALOMEDSImpl_SObject& theObject,
901 SALOMEDSImpl_Driver* theEngine)
904 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
905 if (!aComponent) return false;
906 if (aComponent.GetLabel() == theObject.GetLabel()) return false;
907 std::string IOREngine;
908 if (!aComponent.ComponentIOR(IOREngine)) return false;
909 if (theEngine == NULL) return false;
910 return theEngine->CanCopy(theObject);
913 //============================================================================
914 /*! Function : CopyLabel
917 //============================================================================
918 bool SALOMEDSImpl_Study::CopyLabel(SALOMEDSImpl_Driver* theEngine,
919 const int theSourceStartDepth,
920 const DF_Label& theSource,
921 const DF_Label& theDestinationMain)
926 DF_Label aTargetLabel = theDestinationMain;
927 DF_Label aAuxTargetLabel = theDestinationMain.Father().FindChild(2);
928 for(a = theSource.Depth() - theSourceStartDepth; a > 0 ; a--) {
929 DF_Label aSourceLabel = theSource;
930 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
931 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
932 aAuxTargetLabel = aAuxTargetLabel.FindChild(aSourceLabel.Tag());
934 // iterate attributes
935 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
936 for(int i = 0, len = attrList.size(); i<len; i++) {
937 DF_Attribute* anAttr = attrList[i];
938 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
939 if (type.substr(0, 17) == std::string("AttributeTreeNode")) continue; // never copy tree node attribute
940 if (type == std::string("AttributeTarget")) continue; // and target attribute
942 if (type == std::string("AttributeReference")) { // reference copied as Comment in aux tree
943 DF_Label aReferenced = dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Get();
944 std::string anEntry = aReferenced.Entry();
945 // store the value of name attribute of referenced label
946 SALOMEDSImpl_AttributeName* aNameAttribute;
947 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aReferenced.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
949 anEntry += aNameAttribute->Value();
951 SALOMEDSImpl_AttributeComment::Set(aAuxTargetLabel, anEntry);
955 if (type == std::string("AttributeIOR")) { // IOR => ID and TMPFile of Engine
956 std::string anEntry = theSource.Entry();
957 SALOMEDSImpl_SObject aSO = FindObjectID(anEntry);
960 SALOMEDSImpl_TMPFile* aStream = theEngine->CopyFrom(aSO, anObjID, aLen);
961 std::string aResStr("");
962 for(a = 0; a < aLen; a++) {
963 aResStr += (char)(aStream->Get(a));
966 if(aStream) delete aStream;
968 SALOMEDSImpl_AttributeInteger::Set(aAuxTargetLabel, anObjID);
969 SALOMEDSImpl_AttributeName::Set(aAuxTargetLabel, aResStr);
972 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
973 aTargetLabel.AddAttribute(aNewAttribute);
974 anAttr->Paste(aNewAttribute);
980 //============================================================================
984 //============================================================================
985 bool SALOMEDSImpl_Study::Copy(const SALOMEDSImpl_SObject& theObject,
986 SALOMEDSImpl_Driver* theEngine)
990 // adoptation for alliances datamodel copy: without IOR attributes !!!
991 bool aStructureOnly; // copy only SObjects and attributes without component help
992 aStructureOnly = !theObject.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID());
994 // get component-engine
995 SALOMEDSImpl_Study* aStudy = theObject.GetStudy();
998 _errorCode = "Document is null";
1002 //Clear the clipboard
1003 _clipboard->Main().Root().ForgetAllAttributes(true);
1004 _appli->Close(_clipboard);
1005 _clipboard = _appli->NewDocument("SALOME_STUDY");
1007 // set component data type to the name attribute of root label
1008 if (!aStructureOnly) {
1009 SALOMEDSImpl_AttributeComment::Set(_clipboard->Main().Root(),
1010 theEngine->ComponentDataType());
1012 // iterate all theObject's label children
1013 DF_Label aStartLabel = theObject.GetLabel();
1014 int aSourceStartDepth = aStartLabel.Depth();
1016 // copy main source label
1017 CopyLabel(theEngine, aSourceStartDepth, aStartLabel, _clipboard->Main());
1019 // copy all subchildren of the main source label (all levels)
1020 DF_ChildIterator anIterator(aStartLabel, true);
1021 for(; anIterator.More(); anIterator.Next()) {
1022 CopyLabel(theEngine, aSourceStartDepth, anIterator.Value(), _clipboard->Main());
1028 //============================================================================
1029 /*! Function : CanPaste
1032 //============================================================================
1033 bool SALOMEDSImpl_Study::CanPaste(const SALOMEDSImpl_SObject& theObject,
1034 SALOMEDSImpl_Driver* theEngine)
1039 _errorCode = "Clipboard is null";
1043 SALOMEDSImpl_AttributeComment* aCompName = NULL;
1044 if (!(aCompName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1045 _errorCode = "Clipboard has no component type";
1048 SALOMEDSImpl_AttributeInteger* anObjID;
1049 if (!(anObjID=(SALOMEDSImpl_AttributeInteger*)_clipboard->Main().Father().FindChild(2).FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
1050 _errorCode = "Clipboard has no object id";
1053 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1055 _errorCode = "Object doesn't belong to component";
1059 std::string IOREngine;
1060 if (!aComponent.ComponentIOR(IOREngine)) {
1061 _errorCode = "component has no IOR";
1064 return theEngine->CanPaste(aCompName->Value(), anObjID->Value());
1067 //============================================================================
1068 /*! Function : PasteLabel
1071 //============================================================================
1072 DF_Label SALOMEDSImpl_Study::PasteLabel(SALOMEDSImpl_Driver* theEngine,
1073 const DF_Label& theSource,
1074 const DF_Label& theDestinationStart,
1075 const bool isFirstElement)
1079 // get corresponding source, target and auxiliary labels
1080 DF_Label aTargetLabel = theDestinationStart;
1082 DF_Label aAuxSourceLabel = theSource.Root().FindChild(2);
1084 if (!isFirstElement) {
1085 for(a = theSource.Depth() - 1; a > 0 ; a--) {
1086 DF_Label aSourceLabel = theSource;
1087 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
1088 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
1089 aAuxSourceLabel = aAuxSourceLabel.FindChild(aSourceLabel.Tag());
1091 SALOMEDSImpl_SObject so = GetSObject(aTargetLabel);
1092 addSO_Notification(so);
1095 // check auxiliary label for TMPFile => IOR
1096 SALOMEDSImpl_AttributeName* aNameAttribute = NULL;
1097 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1098 SALOMEDSImpl_AttributeInteger* anObjID = (SALOMEDSImpl_AttributeInteger*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeInteger::GetID());
1099 SALOMEDSImpl_AttributeComment* aComponentName = (SALOMEDSImpl_AttributeComment*)theSource.Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID());
1100 std::string aCompName = aComponentName->Value();
1102 if (theEngine->CanPaste(aCompName, anObjID->Value())) {
1103 std::string aTMPStr = aNameAttribute->Value();
1104 int aLen = aTMPStr.size();
1105 unsigned char* aStream = NULL;
1107 aStream = new unsigned char[aLen+10];
1108 for(a = 0; a < aLen; a++) {
1109 aStream[a] = aTMPStr[a];
1113 std::string anEntry = aTargetLabel.Entry();
1114 SALOMEDSImpl_SObject aPastedSO = FindObjectID(anEntry);
1116 if (isFirstElement) {
1117 std::string aDestEntry = theEngine->PasteInto(aStream,
1120 aPastedSO.GetFatherComponent());
1121 aTargetLabel = DF_Label::Label(theDestinationStart, aDestEntry);
1123 theEngine->PasteInto(aStream, aLen, anObjID->Value(), aPastedSO);
1125 if(aStream != NULL) delete []aStream;
1129 // iterate attributes
1130 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
1131 for(int i = 0, len = attrList.size(); i<len; i++) {
1132 DF_Attribute* anAttr = attrList[i];
1133 if (aTargetLabel.FindAttribute(anAttr->ID())) {
1134 aTargetLabel.ForgetAttribute(anAttr->ID());
1136 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
1137 aTargetLabel.AddAttribute(aNewAttribute);
1138 anAttr->Paste(aNewAttribute);
1141 // check auxiliary label for Comment => reference or name attribute of the referenced object
1142 SALOMEDSImpl_AttributeComment* aCommentAttribute = NULL;
1143 if ((aCommentAttribute=(SALOMEDSImpl_AttributeComment*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1144 char * anEntry = new char[aCommentAttribute->Value().size() + 1];
1145 strcpy(anEntry, std::string(aCommentAttribute->Value()).c_str());
1146 char* aNameStart = strchr(anEntry, ' ');
1151 // copy to the same study, reanimate reference
1152 DF_Label aRefLabel = DF_Label::Label(aTargetLabel, anEntry);
1153 SALOMEDSImpl_AttributeReference::Set(aTargetLabel, aRefLabel);
1154 // target attributes structure support
1155 SALOMEDSImpl_AttributeTarget::Set(aRefLabel)->Add(SALOMEDSImpl_Study::SObject(aTargetLabel));
1160 return aTargetLabel;
1163 //============================================================================
1164 /*! Function : Paste
1167 //============================================================================
1168 SALOMEDSImpl_SObject SALOMEDSImpl_Study::Paste(const SALOMEDSImpl_SObject& theObject,
1169 SALOMEDSImpl_Driver* theEngine)
1173 SALOMEDSImpl_SObject so;
1175 // if study is locked, then paste can't be done
1176 if (GetProperties()->IsLocked()) {
1177 _errorCode = "LockProtection";
1178 throw LockProtection("LockProtection");
1181 // if there is no component name, then paste only SObjects and attributes: without component help
1182 SALOMEDSImpl_AttributeComment* aComponentName = NULL;
1183 bool aStructureOnly = !(aComponentName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()));
1185 // CAF document of current study usage
1187 _errorCode = "Document is null";
1191 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1193 // fill root inserted SObject
1194 DF_Label aStartLabel;
1195 if (aStructureOnly) {
1196 DF_Label anObjectLabel = DF_Label::Label(_doc->Main(), theObject.GetID());
1197 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), anObjectLabel, false);
1199 DF_Label aComponentLabel = DF_Label::Label(_doc->Main(), aComponent.GetID());
1200 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), aComponentLabel, true);
1203 // paste all sublebels
1204 DF_ChildIterator anIterator(_clipboard->Main(), true);
1205 for(; anIterator.More(); anIterator.Next()) {
1206 PasteLabel(theEngine, anIterator.Value(), aStartLabel, false);
1209 return SALOMEDSImpl_Study::SObject(aStartLabel);
1212 //============================================================================
1213 /*! Function : GetPersistentReference
1214 * Purpose : Get persistent reference of study (idem URL())
1216 //============================================================================
1217 std::string SALOMEDSImpl_Study::GetPersistentReference()
1222 //============================================================================
1223 /*! Function : GetTransientReference
1224 * Purpose : Get IOR of the Study (registred in Document in doc->Root)
1226 //============================================================================
1227 std::string SALOMEDSImpl_Study::GetTransientReference()
1230 std::string IOR = "";
1232 SALOMEDSImpl_AttributeIOR* Att;
1233 DF_Label _lab = _doc->Root();
1234 if ((Att=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
1238 _errorCode = "IOR is empty";
1244 void SALOMEDSImpl_Study::SetTransientReference(const std::string& theIOR)
1248 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1249 int aLocked = aProp->IsLocked();
1250 if (aLocked) aProp->SetLocked(false);
1252 // Assign the value of the IOR in the study->root
1253 SALOMEDSImpl_AttributeIOR::Set(_doc->Main().Root(), theIOR);
1255 if (aLocked) aProp->SetLocked(true);
1258 //============================================================================
1259 /*! Function : IsEmpty
1260 * Purpose : Detect if study is empty
1262 //============================================================================
1263 bool SALOMEDSImpl_Study::IsEmpty()
1266 if (!_doc) return true;
1267 return _doc->IsEmpty();
1270 //============================================================================
1271 /*! Function : FindComponent
1272 * Purpose : Find a Component with ComponentDataType = aComponentName
1274 //============================================================================
1275 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const std::string& aComponentName)
1280 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1281 SALOMEDSImpl_SComponent compo;
1283 for (; itcomp.More(); itcomp.Next()) {
1284 SALOMEDSImpl_SComponent SC = itcomp.Value();
1285 name = SC.ComponentDataType();
1286 if(aComponentName == name) {
1294 _errorCode = "No component was found";
1300 //============================================================================
1301 /*! Function : FindComponentID
1302 * Purpose : Find a Component from it's ID
1304 //============================================================================
1305 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const std::string& aComponentID)
1309 // Iterate on each components defined in the study
1310 // Get the component ID and compare with aComponentID
1313 SALOMEDSImpl_SComponent compo;
1315 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1316 for (; itcomp.More(); itcomp.Next()) {
1317 SALOMEDSImpl_SComponent SC = itcomp.Value();
1319 if(aComponentID == ID)
1321 // ComponentID found
1328 _errorCode = "No component was found";
1335 //============================================================================
1336 /*! Function : FindObject
1337 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1339 //============================================================================
1340 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const std::string& anObjectName)
1344 // Iterate to all components defined in the study
1345 // After testing the component name, iterate in all objects defined under
1346 // components (function _FindObject)
1348 SALOMEDSImpl_SObject RefSO;
1350 SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
1351 for (; it.More();it.Next()){
1354 SALOMEDSImpl_SComponent SC = it.Value();
1355 if (SC.GetName() == anObjectName)
1361 if (!_find) RefSO = _FindObject(SC, anObjectName, _find);
1364 if(!RefSO) _errorCode = "No object was found";
1368 //============================================================================
1369 /*! Function : FindObjectID
1370 * Purpose : Find an Object with ID = anObjectID
1372 //============================================================================
1373 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const std::string& anObjectID)
1376 SALOMEDSImpl_SObject so;
1378 // Convert aSO->GetID in DF_Label.
1379 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
1382 _errorCode = "No label was found by ID";
1385 return GetSObject(Lab);
1389 //============================================================================
1390 /*! Function : CreateObjectID
1391 * Purpose : Creates an Object with ID = anObjectID
1393 //============================================================================
1394 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const std::string& anObjectID)
1397 SALOMEDSImpl_SObject so;
1399 // Convert aSO->GetID in DF_Label.
1400 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
1403 _errorCode = "Can not create a label";
1406 return GetSObject(Lab);
1410 //============================================================================
1411 /*! Function : FindObjectByName
1412 * Purpose : Find Objects with SALOMEDSImpl_Name = anObjectName in a Component
1413 * : with ComponentDataType = aComponentName
1415 //============================================================================
1416 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const std::string& anObjectName,
1417 const std::string& aComponentName)
1421 std::vector<SALOMEDSImpl_SObject> listSO;
1423 SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
1425 _errorCode = "Can not find the component";
1429 // Iterate on each object and subobject of the component
1430 // If objectName is found add it to the list of SObjects
1431 std::string childName ;
1433 std::string compoId = compo.GetID();
1434 SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
1435 for ( ; it.More(); it.Next() ) {
1437 SALOMEDSImpl_SObject CSO = it.Value();
1438 if ( CSO.GetName() == anObjectName ) {
1440 listSO.push_back(CSO) ;
1443 /* looks also for eventual children */
1444 bool found = false ;
1445 CSO = _FindObject( CSO, anObjectName, found ) ;
1447 listSO.push_back(CSO) ;
1456 //============================================================================
1457 /*! Function : FindObjectIOR
1458 * Purpose : Find an Object with IOR = anObjectIOR
1460 //============================================================================
1461 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const std::string& anObjectIOR)
1465 SALOMEDSImpl_SObject aResult ;
1467 // searching in the datamap for optimization
1468 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anObjectIOR);
1469 if (it != myIORLabels.end()) {
1470 aResult = GetSObject(it->second);
1471 // 11 oct 2002: forbidden attributes must be checked here
1472 if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
1473 myIORLabels.erase(anObjectIOR);
1474 aResult = SALOMEDSImpl_SObject();
1478 if(!aResult) _errorCode = "No object was found";
1482 //============================================================================
1483 /*! Function : FindObjectByPath
1484 * Purpose : Find an Object by its path = thePath
1486 //============================================================================
1487 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const std::string& thePath)
1491 std::string aPath(thePath), aToken;
1492 SALOMEDSImpl_SObject aSO;
1493 int aLength = aPath.size();
1494 bool isRelative = false;
1496 if(aLength == 0) { //Empty path - return the current context
1497 return GetSObject(_current);
1500 if(aPath[0] != '/') //Relative path
1503 DF_ChildIterator anIterator;
1505 SALOMEDSImpl_AttributeName* anAttr;
1508 if(_current.IsNull()) return aSO;
1509 anIterator.Init(_current, false);
1512 if(aPath.size() == 1 && aPath[0] == '/') { //Root
1513 return GetSObject(_doc->Main());
1515 anIterator.Init(_doc->Main(), false);
1518 std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
1519 for(int i = 0, len = vs.size(); i<len; i++) {
1522 if(aToken.size() == 0) break;
1524 for ( ; anIterator.More(); anIterator.Next() ) {
1525 aLabel = anIterator.Value();
1526 if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1527 if(anAttr->Value() == aToken) {
1528 if(i == (len-1)) { //The searched label is found (no part of the path is left)
1529 return GetSObject(aLabel);
1532 anIterator.Init(aLabel, false);
1540 if(!aSO) _errorCode = "No object was found";
1544 //============================================================================
1545 /*! Function : GetObjectPath
1548 //============================================================================
1549 std::string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
1553 std::string aPath("");
1555 _errorCode = "Null object";
1559 std::string aName = theObject.GetName();
1560 if(!aName.empty() && aName != "" ) {
1561 std::string aValue("/");
1565 SALOMEDSImpl_SObject aFather = theObject.GetFather();
1567 aName = aFather.GetName();
1568 if(!aName.empty() && aName != "") {
1569 aValue = GetObjectPath(aFather);
1570 aPath = aValue + aPath;
1579 //============================================================================
1580 /*! Function : GetObjectPathByIOR
1583 //============================================================================
1584 std::string SALOMEDSImpl_Study::GetObjectPathByIOR(const std::string& theIOR)
1589 SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
1591 _errorCode = "No SObject was found by IOR";
1595 return GetObjectPath(so);
1599 //============================================================================
1600 /*! Function : SetContext
1601 * Purpose : Sets the current context
1603 //============================================================================
1604 bool SALOMEDSImpl_Study::SetContext(const std::string& thePath)
1607 if(thePath.empty()) {
1608 _errorCode = "InvalidPath";
1612 std::string aPath(thePath), aContext("");
1613 bool isInvalid = false;
1614 SALOMEDSImpl_SObject aSO;
1616 if(aPath[0] != '/') { //Relative path
1617 aContext = GetContext();
1625 aSO = FindObjectByPath(aContext);
1631 if(isInvalid || !aSO) {
1632 _errorCode = "InvalidContext";
1636 DF_Label aLabel = aSO.GetLabel();
1637 if(aLabel.IsNull()) {
1638 _errorCode = "InvalidContext";
1642 _current = aLabel; //Set the current context
1647 //============================================================================
1648 /*! Function : GetContext
1649 * Purpose : Gets the current context
1651 //============================================================================
1652 std::string SALOMEDSImpl_Study::GetContext()
1656 if(_current.IsNull()) {
1657 _errorCode = "InvaidContext";
1660 SALOMEDSImpl_SObject so = GetSObject(_current);
1661 return GetObjectPath(so);
1664 //============================================================================
1665 /*! Function : GetObjectNames
1666 * Purpose : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
1668 //============================================================================
1669 std::vector<std::string> SALOMEDSImpl_Study::GetObjectNames(const std::string& theContext)
1673 std::vector<std::string> aResultSeq;
1675 if (theContext.empty()) {
1678 DF_Label aTmp = _current;
1679 SetContext(theContext);
1683 if (aLabel.IsNull()) {
1684 _errorCode = "InvalidContext";
1688 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
1689 for (; anIter.More(); anIter.Next()) {
1690 DF_Label aLabel = anIter.Value();
1691 SALOMEDSImpl_AttributeName* aName;
1692 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1693 aResultSeq.push_back(aName->Value());
1699 //============================================================================
1700 /*! Function : GetDirectoryNames
1701 * Purpose : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
1703 //============================================================================
1704 std::vector<std::string> SALOMEDSImpl_Study::GetDirectoryNames(const std::string& theContext)
1708 std::vector<std::string> aResultSeq;
1710 if (theContext.empty()) {
1713 DF_Label aTmp = _current;
1714 SetContext(theContext);
1718 if (aLabel.IsNull()) {
1719 _errorCode = "InvalidContext";
1723 DF_ChildIterator anIter (aLabel, true); // iterate first-level children at all sublevels
1724 for (; anIter.More(); anIter.Next()) {
1725 DF_Label aLabel = anIter.Value();
1726 SALOMEDSImpl_AttributeLocalID* anID;
1727 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
1728 if (anID->Value() == DIRECTORYID) {
1729 SALOMEDSImpl_AttributeName* aName;
1730 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1731 aResultSeq.push_back(aName->Value());
1740 //============================================================================
1741 /*! Function : GetFileNames
1742 * Purpose : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
1744 //============================================================================
1745 std::vector<std::string> SALOMEDSImpl_Study::GetFileNames(const std::string& theContext)
1749 std::vector<std::string> aResultSeq;
1751 if (theContext.empty()) {
1754 DF_Label aTmp = _current;
1755 SetContext(theContext);
1759 if (aLabel.IsNull()) {
1760 _errorCode = "InvalidContext";
1764 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
1765 for (; anIter.More(); anIter.Next()) {
1766 DF_Label aLabel = anIter.Value();
1767 SALOMEDSImpl_AttributeLocalID* anID;
1768 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
1769 if (anID->Value() == FILELOCALID) {
1770 SALOMEDSImpl_AttributePersistentRef* aName;
1771 if ((aName=(SALOMEDSImpl_AttributePersistentRef*)aLabel.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
1772 std::string aFileName = aName->Value();
1773 if (aFileName.size() > 0)
1774 aResultSeq.push_back(aFileName.substr(strlen(FILEID), aFileName.size()));
1783 //============================================================================
1784 /*! Function : GetComponentNames
1785 * Purpose : method to get all components names
1787 //============================================================================
1788 std::vector<std::string> SALOMEDSImpl_Study::GetComponentNames(const std::string& theContext)
1792 std::vector<std::string> aResultSeq;
1793 DF_ChildIterator anIter(_doc->Main(), false); // iterate all subchildren at first level
1794 for(; anIter.More(); anIter.Next()) {
1795 DF_Label aLabel = anIter.Value();
1796 SALOMEDSImpl_AttributeName* aName;
1797 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1798 aResultSeq.push_back(aName->Value());
1804 //============================================================================
1805 /*! Function : NewChildIterator
1806 * Purpose : Create a ChildIterator from an SObject
1808 //============================================================================
1809 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
1812 return SALOMEDSImpl_ChildIterator(aSO);
1816 //============================================================================
1817 /*! Function : NewComponentIterator
1818 * Purpose : Create a SComponentIterator
1820 //============================================================================
1821 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
1824 return SALOMEDSImpl_SComponentIterator(_doc);
1828 //============================================================================
1829 /*! Function : NewBuilder
1830 * Purpose : Create a StudyBuilder
1832 //============================================================================
1833 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
1837 _builder->SetOnAddSObject(_cb);
1838 _builder->SetOnRemoveSObject(_cb);
1844 //============================================================================
1846 * Purpose : get study name
1848 //============================================================================
1849 std::string SALOMEDSImpl_Study::Name()
1852 return Kernel_Utils::GetBaseName( _name, false );
1855 //============================================================================
1856 /*! Function : IsSaved
1857 * Purpose : get if study has been saved
1859 //============================================================================
1860 bool SALOMEDSImpl_Study::IsSaved()
1866 //============================================================================
1867 /*! Function : IsSaved
1868 * Purpose : set if study has been saved
1870 //============================================================================
1871 void SALOMEDSImpl_Study::IsSaved(bool save)
1875 if(save) _doc->SetModified(false);
1878 //============================================================================
1879 /*! Function : IsModified
1880 * Purpose : Detect if a Study has been modified since it has been saved
1882 //============================================================================
1883 bool SALOMEDSImpl_Study::IsModified()
1887 // True if is modified
1888 if (_doc && _doc->IsModified()) return true;
1893 //============================================================================
1895 * Purpose : get URL of the study (persistent reference of the study)
1897 //============================================================================
1898 std::string SALOMEDSImpl_Study::URL()
1902 return Kernel_Utils::AddExtension( _name );
1906 //============================================================================
1908 * Purpose : set URL of the study (persistent reference of the study)
1910 //============================================================================
1911 void SALOMEDSImpl_Study::URL(const std::string& url)
1917 /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
1918 std::string tmp(_URL);
1920 char *aName = (char*)tmp.ToCString();
1921 char *adr = strtok(aName, "/");
1925 adr = strtok(NULL, "/");
1931 //============================================================================
1932 /*! Function : _FindObject
1933 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1935 //============================================================================
1936 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
1937 const std::string& theObjectName,
1940 SALOMEDSImpl_SObject RefSO;
1941 if(!SO) return RefSO;
1943 // Iterate on each objects and subobjects of the component
1944 // If objectName find, stop the loop and get the object reference
1945 SALOMEDSImpl_AttributeName* anAttr;
1947 std::string soid = SO.GetID();
1948 DF_ChildIterator it(SO.GetLabel());
1949 for (; it.More(); it.Next()){
1952 if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1954 std::string Val(anAttr->Value());
1955 if (Val == theObjectName)
1957 RefSO = GetSObject(it.Value());
1961 if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
1967 //============================================================================
1968 /*! Function : _FindObjectIOR
1969 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
1971 //============================================================================
1972 SALOMEDSImpl_SObject
1973 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
1974 const std::string& theObjectIOR,
1977 SALOMEDSImpl_SObject RefSO, aSO;
1978 if(!SO) return RefSO;
1980 // Iterate on each objects and subobjects of the component
1981 // If objectName find, stop the loop and get the object reference
1982 SALOMEDSImpl_AttributeIOR* anAttr;
1984 DF_ChildIterator it(SO.GetLabel());
1985 for (; it.More();it.Next()){
1988 if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
1990 std::string Val(anAttr->Value());
1991 if (Val == theObjectIOR)
1993 RefSO = GetSObject(it.Value());
1997 aSO = GetSObject(it.Value());
1998 if (!_find) RefSO = _FindObjectIOR(aSO, theObjectIOR, _find);
2004 //============================================================================
2005 /*! Function : _GetNoteBookAccessor
2006 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
2008 //============================================================================
2009 std::string SALOMEDSImpl_Study::_GetNoteBookAccessor(){
2010 return std::string("notebook");
2013 //============================================================================
2014 /*! Function : _GetStudyVariablesScript
2017 //============================================================================
2018 std::string SALOMEDSImpl_Study::_GetStudyVariablesScript()
2020 std::string dump("");
2022 if(myNoteBookVars.empty())
2025 Kernel_Utils::Localizer loc;
2027 dump += "####################################################\n";
2028 dump += "## Begin of NoteBook variables section ##\n";
2029 dump += "####################################################\n";
2031 std::string set_method = _GetNoteBookAccessor()+".set(";
2032 std::string varName;
2033 std::string varValue;
2034 for(int i = 0 ; i < myNoteBookVars.size();i++ ) {
2035 varName = myNoteBookVars[i]->Name();
2036 varValue = myNoteBookVars[i]->SaveToScript();
2037 dump+=set_method+"\""+varName+"\", "+varValue+")\n";
2040 dump += "####################################################\n";
2041 dump += "## End of NoteBook variables section ##\n";
2042 dump += "####################################################\n";
2047 //============================================================================
2048 /*! Function : _GetNoteBookAccess
2051 //============================================================================
2052 std::string SALOMEDSImpl_Study::_GetNoteBookAccess(const std::string& theStudyVar)
2054 std::string notebook = "import salome_notebook\n";
2055 notebook += _GetNoteBookAccessor() + " = salome_notebook.NoteBook(" + theStudyVar + ")" ;
2059 bool SALOMEDSImpl_Study::IsLocked()
2062 return GetProperties()->IsLocked();
2065 void SALOMEDSImpl_Study::UpdateIORLabelMap(const std::string& anIOR,const std::string& anEntry)
2068 DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
2069 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
2070 if (it != myIORLabels.end()) myIORLabels.erase(it);
2071 myIORLabels[anIOR] = aLabel;
2074 void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR)
2076 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
2077 if (it != myIORLabels.end())
2079 //remove the ior entry and decref the genericobj (if it's one)
2080 myIORLabels.erase(it);
2084 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel)
2086 SALOMEDSImpl_StudyHandle* Att;
2087 if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
2093 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
2095 return GetStudy(theLabel)->GetSObject(theLabel);
2098 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
2100 return GetStudy(theLabel)->GetSComponent(theLabel);
2104 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
2106 std::string aString = theAttribute->Label().Entry();
2107 GetStudy(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
2110 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
2113 std::vector<SALOMEDSImpl_SObject> aSeq;
2115 SALOMEDSImpl_AttributeTarget* aTarget;
2116 if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
2117 return aTarget->Get();
2124 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
2127 return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
2130 std::string SALOMEDSImpl_Study::GetLastModificationDate()
2133 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
2135 std::vector<std::string> aNames;
2136 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
2137 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
2139 int aLastIndex = aNames.size()-1;
2141 sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
2142 (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
2143 (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
2144 std::string aResStr (aResult);
2148 std::vector<std::string> SALOMEDSImpl_Study::GetModificationsDate()
2151 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
2153 std::vector<std::string> aNames;
2154 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
2155 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
2157 int anIndex, aLength = aNames.size();
2158 std::vector<std::string> aDates;
2160 for (anIndex = 1; anIndex < aLength; anIndex++) {
2162 sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
2163 (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
2164 (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
2165 aDates.push_back(aDate);
2172 //============================================================================
2173 /*! Function : GetUseCaseBuilder
2174 * Purpose : Returns a UseCase builder
2176 //============================================================================
2177 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
2180 return _useCaseBuilder;
2183 //============================================================================
2184 /*! Function : GetSComponent
2187 //============================================================================
2188 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const std::string& theEntry)
2190 SALOMEDSImpl_SComponent aSCO;
2191 if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
2192 aSCO = _mapOfSCO[theEntry];
2194 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
2195 aSCO = SALOMEDSImpl_SComponent(aLabel);
2196 _mapOfSCO[theEntry] = aSCO;
2202 //============================================================================
2203 /*! Function : GetSComponent
2206 //============================================================================
2207 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
2209 return SALOMEDSImpl_SComponent(theLabel);
2212 //============================================================================
2213 /*! Function : GetSObject
2216 //============================================================================
2217 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const std::string& theEntry)
2219 SALOMEDSImpl_SObject aSO;
2220 std::map<std::string, SALOMEDSImpl_SObject>::iterator it=_mapOfSO.find(theEntry);
2221 if(it != _mapOfSO.end())
2224 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
2225 aSO = SALOMEDSImpl_SObject(aLabel);
2226 _mapOfSO[theEntry] = aSO;
2232 //============================================================================
2233 /*! Function : GetSObject
2236 //============================================================================
2237 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
2239 return SALOMEDSImpl_SObject(theLabel);
2242 //============================================================================
2243 /*! Function : GetAttribute
2246 //============================================================================
2247 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const std::string& theEntry,
2248 const std::string& theType)
2250 SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
2251 DF_Attribute* anAttr;
2252 aSO.FindAttribute(anAttr, theType);
2256 //! number of spaces for indentation in Python dump files (to replace \t symbols)
2257 static const int indent_size = 2;
2259 static std::string replace_tabs( const std::string& in )
2261 std::string out = in;
2263 size_t pos = out.find( '\t' );
2264 while ( pos != std::string::npos ) {
2265 out.replace( pos, 1, indent_size, ' ' );
2266 pos = out.find( '\t' );
2272 static std::string GetComponentHeader(const char* theComponentName)
2274 std::stringstream txt;
2275 txt << "###" << std::endl;
2276 txt << "### " << theComponentName << " component" << std::endl;
2277 txt << "###" << std::endl;
2281 //============================================================================
2282 /*! Function : DumpStudy
2285 //============================================================================
2286 bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
2287 const std::string& theBaseName,
2290 SALOMEDSImpl_DriverFactory* theFactory)
2294 if(theFactory == NULL) {
2295 _errorCode = "Null factory for creation of Engines";
2299 std::vector<std::string> aSeq;
2300 std::string aCompType, aFactoryType;
2302 //Build a list of all components in the Study
2303 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
2305 for (; itcomponent.More(); itcomponent.Next()) {
2306 SALOMEDSImpl_SComponent sco = itcomponent.Value();
2307 aCompType = sco.ComponentDataType();
2308 if (aCompType == "GEOM")
2309 aSeq.insert(aSeq.begin(), aCompType);
2311 aSeq.push_back(aCompType);
2313 // re-arrange modules in the sequence, if specific order is given via SALOME_MODULES_ORDER environment variable.
2314 if ( getenv("SALOME_MODULES_ORDER") != 0 ) {
2315 std::string order = getenv("SALOME_MODULES_ORDER");
2316 std::vector<std::string> mlist;
2317 while ( !order.empty() ) {
2318 size_t idx = order.find( "," );
2319 std::string m = order.substr(0, idx);
2320 order = order.substr( ( idx == std::string::npos ) ? order.size() : idx+1 );
2321 if ( m.empty() || std::find( mlist.begin(), mlist.end(), m ) != mlist.end() ) continue;
2322 mlist.push_back( m );
2325 for ( std::vector<std::string>::reverse_iterator mit = mlist.rbegin(); mit != mlist.rend(); ++mit ) {
2326 std::vector<std::string>::iterator it = std::find( aSeq.begin(), aSeq.end(), *mit );
2327 if ( it != aSeq.end() ) {
2329 aSeq.insert( aSeq.begin(), *mit );
2335 std::string aFileName =
2336 thePath + std::string("\\") + theBaseName + std::string(".py");
2338 std::string aFileName =
2339 thePath + std::string("/") + theBaseName + std::string(".py");
2342 //Create a file that will contain a main Study script
2344 fp.open(aFileName.c_str(), std::ios::out);
2347 bool isOpened = fp.is_open();
2349 bool isOpened = fp.rdbuf()->is_open();
2353 _errorCode = std::string("Can't create a file ")+aFileName;
2357 std::stringstream sfp;
2359 std::string aBatchModeScript = "salome";
2361 //Output to the main Study script required Python modules import,
2362 //set sys.path and add a creation of the study.
2365 sfp << GetDumpStudyComment() << std::endl;
2368 sfp << "import sys" << std::endl;
2369 sfp << "import " << aBatchModeScript << std::endl << std::endl;
2371 std::string aStudyVar = "salome.myStudy";
2372 // initialization function
2373 sfp << aBatchModeScript << ".salome_init()" << std::endl;
2374 if ( !isMultiFile ) {
2375 sfp << "theStudy = " << aStudyVar << std::endl << std::endl;
2376 aStudyVar = "theStudy";
2378 // notebook initialization
2379 sfp << _GetNoteBookAccess(aStudyVar) << std::endl;
2381 // extend sys.path with the directory where the script is being dumped to
2382 sfp << "sys.path.insert( 0, r\'" << thePath << "\')" << std::endl << std::endl;
2384 // dump NoteBook variables
2385 sfp << _GetStudyVariablesScript();
2387 // dump visual parameters if necessary
2388 bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
2389 int lastSavePoint = -1;
2391 lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
2392 if(lastSavePoint > 0) {
2393 sfp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << std::endl << std::endl;
2397 std::vector<std::string> aSeqOfFileNames;
2399 // dump all components and create the components specific scripts
2401 int aLength = aSeq.size();
2402 for(int i = 1; i <= aLength; i++) {
2404 aCompType = aSeq[i-1];
2405 SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
2406 SALOMEDSImpl_Driver* aDriver = NULL;
2407 // if there is an associated Engine call its method for saving
2408 std::string IOREngine;
2410 if (!sco.ComponentIOR(IOREngine)) {
2411 if (!aCompType.empty()) {
2413 aDriver = theFactory->GetDriverByType(aCompType);
2415 if (aDriver != NULL) {
2416 SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
2417 if(!SB->LoadWith(sco, aDriver)) {
2418 _errorCode = SB->GetErrorCode();
2426 aDriver = theFactory->GetDriverByIOR(IOREngine);
2429 _errorCode = "Can not restore information to dump it";
2433 if(aDriver == NULL) continue;
2436 long aStreamLength = 0;
2437 SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(isPublished, isMultiFile, isValidScript, aStreamLength);
2438 if ( !isValidScript )
2441 std::stringstream sfp2;
2443 //Output the Python script generated by the component in the newly created file.
2445 sfp2 << GetDumpStudyComment( aCompType.c_str() ) << std::endl;
2447 sfp2 << GetComponentHeader( aCompType.c_str() ) << std::endl;
2448 sfp2 << aStream->Data();
2450 if ( isMultiFile ) {
2451 //Create a file that will contain the component specific script
2454 aFileName=thePath+std::string("\\");
2456 aFileName=thePath+std::string("/");
2458 std::string aScriptName;
2459 aScriptName += theBaseName;
2461 aScriptName += aCompType;
2463 aFileName += aScriptName+ std::string(".py");
2464 aSeqOfFileNames.push_back(aFileName);
2466 fp2.open(aFileName.c_str(), std::ios::out);
2469 isOpened = fp2.is_open();
2471 isOpened = fp2.rdbuf()->is_open();
2475 _errorCode = std::string("Can't create a file ")+aFileName;
2476 SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
2480 // replace '\t' symbols
2481 fp2 << replace_tabs( sfp2.str() );
2485 //Add to the main script a call to RebuildData of the generated by the component the Python script
2486 sfp << "import " << aScriptName << std::endl;
2487 sfp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << std::endl;
2492 if(aStream) delete aStream;
2496 sfp << "if salome.sg.hasDesktop():" << std::endl;
2497 sfp << "\tsalome.sg.updateObjBrowser()" << std::endl;
2499 if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
2500 sfp << "\tiparameters.getSession().restoreVisualState(1)" << std::endl;
2503 // replace '\t' symbols
2504 fp << replace_tabs( sfp.str() );
2511 //=======================================================================
2512 //function : GetDumpStudyComment
2513 //purpose : return a header comment for a DumpStudy script
2514 //=======================================================================
2516 std::string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
2518 std::stringstream txt;
2519 txt << "# -*- coding: utf-8 -*-" << std::endl << std::endl;
2520 txt << "###" << std::endl;
2521 txt << "### This file is generated automatically by SALOME v"
2522 << KERNEL_VERSION_STR
2523 << " with dump python functionality";
2524 if ( theComponentName )
2525 txt << " (" << theComponentName << " component)";
2527 txt << "###" << std::endl;
2531 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2533 const std::string& Tab,
2534 SALOMEDSImpl_Study* theStudy);
2536 //============================================================================
2540 //============================================================================
2541 void SALOMEDSImpl_Study::dump(const std::string& theFileName)
2543 //Create a file that will contain a main Study script
2545 fp.open(theFileName.c_str(), std::ios::out);
2548 bool isOpened = fp.is_open();
2550 bool isOpened = fp.rdbuf()->is_open();
2554 _errorCode = std::string("Can't create a file ")+theFileName;
2555 std::cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << std::endl;
2559 SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
2560 fp << "0:1" << std::endl;
2561 SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
2562 std::string aTab(" ");
2563 for(; Itr.More(); Itr.Next()) {
2564 dumpSO(Itr.Value(), fp, aTab, this);
2571 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2573 const std::string& Tab,
2574 SALOMEDSImpl_Study* theStudy)
2576 std::string aTab(Tab), anID(theSO.GetID());
2577 fp << aTab << anID << std::endl;
2578 std::vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
2579 for(int i = 0; i<attribs.size(); i++) {
2580 SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
2586 std::string aType = anAttr->GetClassType();
2587 fp << Tab << " -- " << aType;
2589 if(aType == std::string("AttributeReal")) {
2590 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
2592 else if(aType == std::string("AttributeInteger")) {
2593 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
2595 else if(aType == std::string("AttributeName")) {
2596 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
2598 else if(aType == std::string("AttributeComment")) {
2599 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
2601 else if(aType == std::string("AttributeReference")) {
2602 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
2607 SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
2608 std::string aNewTab(" ");
2610 for(; Itr.More(); Itr.Next()) {
2611 dumpSO(Itr.Value(), fp, aNewTab, theStudy);
2617 void SALOMEDSImpl_Study::Modify()
2620 _doc->SetModified(true);
2623 //============================================================================
2627 //============================================================================
2628 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
2630 if (theSavePoint < -1) return NULL;
2631 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2632 SALOMEDSImpl_SObject so = FindComponent((char*)theID);
2633 if (!so) so = builder->NewComponent((char*)theID);
2634 SALOMEDSImpl_AttributeParameter* attParam = NULL;
2636 if (theSavePoint == -1) {
2638 DF_Label savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2639 DF_Label prevPointLabel;
2640 while ( !savePointLabel.IsNull() ) {
2642 prevPointLabel = savePointLabel;
2643 savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2645 if ( !prevPointLabel.IsNull() )
2646 so = GetSObject( prevPointLabel );
2648 if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
2649 DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
2650 if ( !savePointLabel.IsNull() )
2651 so = GetSObject( savePointLabel );
2652 else // ... if it does not exist - create a new one
2653 so = builder->NewObjectToTag( so, theSavePoint );
2658 builder->FindAttribute(so, A, "AttributeParameter");
2659 if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
2660 A = builder->FindOrCreateAttribute(so, "AttributeParameter");
2662 attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
2667 //============================================================================
2671 //============================================================================
2672 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID,
2673 const char* theModuleName,
2676 if(theSavePoint < -1) return NULL;
2677 SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
2678 SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
2679 SALOMEDSImpl_AttributeParameter* par = NULL;
2681 SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
2682 std::string moduleName(theModuleName);
2683 for(; it.More(); it.Next()) {
2684 SALOMEDSImpl_SObject so(it.Value());
2685 if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
2686 if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
2687 if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
2691 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2692 SALOMEDSImpl_SObject so = builder->NewObject(main_so);
2693 par = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
2694 par->SetString("AP_MODULE_NAME", moduleName);
2698 //============================================================================
2699 /*! Function : SetStudyLock
2702 //============================================================================
2703 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
2705 _lockers.push_back(theLockerID);
2708 //============================================================================
2709 /*! Function : IsStudyLocked
2712 //============================================================================
2713 bool SALOMEDSImpl_Study::IsStudyLocked()
2715 return (_lockers.size() > 0);
2718 //============================================================================
2719 /*! Function : UnLockStudy
2722 //============================================================================
2723 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
2725 std::vector<std::string>::iterator vsI = _lockers.begin();
2726 int length = _lockers.size();
2727 bool isFound = false;
2728 std::string id(theLockerID);
2729 for(int i = 0; i<length; i++, vsI++) {
2730 if(id == _lockers[i]) {
2735 if(isFound) _lockers.erase(vsI);
2738 //============================================================================
2739 /*! Function : GetLockerID
2742 //============================================================================
2743 std::vector<std::string> SALOMEDSImpl_Study::GetLockerID()
2748 //============================================================================
2749 /*! Function : SetVariable
2752 //============================================================================
2753 void SALOMEDSImpl_Study::SetVariable(const std::string& theVarName,
2754 const double theValue,
2755 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2757 bool modified = false;
2758 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2760 if( aGVar == NULL ) {
2762 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2764 aSVar->setValue(theValue);
2765 myNoteBookVars.push_back(aSVar);
2769 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2770 modified = aSVar->setValue(theValue) || modified;
2771 modified = aSVar->setType(theType) || modified;
2778 //============================================================================
2779 /*! Function : SetStringVariable
2782 //============================================================================
2783 void SALOMEDSImpl_Study::SetStringVariable(const std::string& theVarName,
2784 const std::string& theValue,
2785 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2787 bool modified = false;
2788 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2790 if( aGVar == NULL ) {
2792 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2794 aSVar->setStringValue(theValue);
2795 myNoteBookVars.push_back(aSVar);
2799 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2800 modified = aSVar->setStringValue(theValue) || modified;
2801 modified = aSVar->setType(theType) || modified;
2808 //============================================================================
2809 /*! Function : SetStringVariableAsDouble
2812 //============================================================================
2813 void SALOMEDSImpl_Study::SetStringVariableAsDouble(const std::string& theVarName,
2814 const double theValue,
2815 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2817 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2818 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2819 aSVar->setValue(theValue);
2822 //============================================================================
2823 /*! Function : GetReal
2826 //============================================================================
2827 double SALOMEDSImpl_Study::GetVariableValue(const std::string& theVarName)
2829 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2832 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2833 return aSVar->getValue();
2838 //============================================================================
2839 /*! Function : GetString
2842 //============================================================================
2843 std::string SALOMEDSImpl_Study::GetStringVariableValue(const std::string& theVarName)
2845 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2848 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2849 return aSVar->getStringValue();
2854 //============================================================================
2855 /*! Function : IsTypeOf
2858 //============================================================================
2859 bool SALOMEDSImpl_Study::IsTypeOf(const std::string& theVarName,
2860 SALOMEDSImpl_GenericVariable::
2861 VariableTypes theType) const
2863 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2866 return aGVar->Type() == theType;
2871 //============================================================================
2872 /*! Function : IsVariable
2875 //============================================================================
2876 bool SALOMEDSImpl_Study::IsVariable(const std::string& theVarName) const
2878 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2879 return (aGVar != NULL);
2882 //============================================================================
2883 /*! Function : GetVariableNames
2886 //============================================================================
2887 std::vector<std::string> SALOMEDSImpl_Study::GetVariableNames() const
2889 std::vector<std::string> aResult;
2891 for(int i = 0; i < myNoteBookVars.size(); i++)
2892 aResult.push_back(myNoteBookVars[i]->Name());
2897 //============================================================================
2898 /*! Function : AddVariable
2901 //============================================================================
2902 void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
2904 myNoteBookVars.push_back(theVariable);
2907 //============================================================================
2908 /*! Function : AddVariable
2911 //============================================================================
2912 SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
2914 SALOMEDSImpl_GenericVariable* aResult = NULL;
2915 for(int i = 0; i < myNoteBookVars.size();i++) {
2916 if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
2917 aResult = myNoteBookVars[i];
2924 //============================================================================
2925 /*! Function : RemoveVariable
2928 //============================================================================
2929 bool SALOMEDSImpl_Study::RemoveVariable(const std::string& theVarName)
2931 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2935 std::string aValue = aVariable->SaveToScript();
2936 ReplaceVariableAttribute( theVarName, aValue );
2938 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2939 for( ; it != itEnd; it++ )
2941 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2942 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2944 myNoteBookVars.erase( it );
2953 //============================================================================
2954 /*! Function : RenameVariable
2957 //============================================================================
2958 bool SALOMEDSImpl_Study::RenameVariable(const std::string& theVarName, const std::string& theNewVarName)
2960 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2964 ReplaceVariableAttribute( theVarName, theNewVarName );
2966 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2967 for( ; it != itEnd; it++ )
2969 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2970 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2972 aVariableRef->setName( theNewVarName );
2981 //============================================================================
2982 /*! Function : IsVariableUsed
2985 //============================================================================
2986 bool SALOMEDSImpl_Study::IsVariableUsed(const std::string& theVarName)
2988 return FindVariableAttribute( theVarName );
2991 //============================================================================
2992 /*! Function : FindVariableAttribute
2995 //============================================================================
2996 bool SALOMEDSImpl_Study::FindVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
2997 SALOMEDSImpl_SObject theSObject,
2998 const std::string& theName)
3000 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
3001 for( ; anIter.More(); anIter.Next() )
3002 if( FindVariableAttribute( theStudyBuilder, anIter.Value(), theName ) )
3005 DF_Attribute* anAttr;
3006 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
3008 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
3010 std::string aString = aStringAttr->Value();
3012 std::vector< std::vector<std::string> > aSections = ParseVariables( aString );
3013 for( int i = 0, n = aSections.size(); i < n; i++ )
3015 std::vector<std::string> aVector = aSections[i];
3016 for( int j = 0, m = aVector.size(); j < m; j++ )
3018 std::string aStr = aVector[j];
3019 if( aStr.compare( theName ) == 0 )
3028 //============================================================================
3029 /*! Function : FindVariableAttribute
3032 //============================================================================
3033 bool SALOMEDSImpl_Study::FindVariableAttribute(const std::string& theName)
3035 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
3036 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
3037 for( ; aCompIter.More(); aCompIter.Next() )
3039 SALOMEDSImpl_SObject aComp = aCompIter.Value();
3040 if( FindVariableAttribute( aStudyBuilder, aComp, theName ) )
3046 //============================================================================
3047 /*! Function : ReplaceVariableAttribute
3050 //============================================================================
3051 void SALOMEDSImpl_Study::ReplaceVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
3052 SALOMEDSImpl_SObject theSObject,
3053 const std::string& theSource,
3054 const std::string& theDest)
3056 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
3057 for( ; anIter.More(); anIter.Next() )
3058 ReplaceVariableAttribute( theStudyBuilder, anIter.Value(), theSource, theDest );
3060 DF_Attribute* anAttr;
3061 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
3063 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
3065 bool isChanged = false;
3066 std::string aNewString, aCurrentString = aStringAttr->Value();
3068 std::vector< std::vector<std::string> > aSections = ParseVariables( aCurrentString );
3069 for( int i = 0, n = aSections.size(); i < n; i++ )
3071 std::vector<std::string> aVector = aSections[i];
3072 for( int j = 0, m = aVector.size(); j < m; j++ )
3074 std::string aStr = aVector[j];
3075 if( aStr.compare( theSource ) == 0 )
3081 aNewString.append( aStr );
3083 aNewString.append( ":" );
3086 aNewString.append( "|" );
3090 aStringAttr->SetValue( aNewString );
3095 //============================================================================
3096 /*! Function : ReplaceVariableAttribute
3099 //============================================================================
3100 void SALOMEDSImpl_Study::ReplaceVariableAttribute(const std::string& theSource, const std::string& theDest)
3102 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
3103 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
3104 for( ; aCompIter.More(); aCompIter.Next() )
3106 SALOMEDSImpl_SObject aComp = aCompIter.Value();
3107 ReplaceVariableAttribute( aStudyBuilder, aComp, theSource, theDest );
3111 //============================================================================
3112 /*! Function : ParseVariables
3115 //============================================================================
3116 std::vector< std::vector< std::string > > SALOMEDSImpl_Study::ParseVariables(const std::string& theVariables) const
3118 return SALOMEDSImpl_Tool::splitStringWithEmpty( theVariables, OPERATION_SEPARATOR, VARIABLE_SEPARATOR );
3121 //============================================================================
3122 /*! Function : EnableUseCaseAutoFilling
3125 //============================================================================
3126 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
3128 _errorCode = ""; _autoFill = isEnabled;
3130 _builder->SetOnAddSObject(_cb);
3131 _builder->SetOnRemoveSObject(_cb);
3134 _builder->SetOnAddSObject(NULL);
3135 _builder->SetOnRemoveSObject(NULL);
3139 //============================================================================
3140 /*! Function : GetIORs
3143 //============================================================================
3144 std::vector<std::string> SALOMEDSImpl_Study::GetIORs()
3146 std::vector<std::string> anIORs;
3147 std::map<std::string, DF_Label>::const_iterator MI;
3148 for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
3149 anIORs.push_back(MI->first);
3154 //============================================================================
3155 /*! Function : addSO_Notification
3156 * Purpose : This function tells all the observers that a SO has been added
3158 //============================================================================
3159 bool SALOMEDSImpl_Study::addSO_Notification (const SALOMEDSImpl_SObject& theSObject)
3162 return _notifier->addSO_Notification(theSObject);
3167 //============================================================================
3168 /*! Function : removeSO_Notification
3169 * Purpose : This function tells all the observers that a SO has been removed
3171 //============================================================================
3172 bool SALOMEDSImpl_Study::removeSO_Notification (const SALOMEDSImpl_SObject& theSObject)
3175 return _notifier->removeSO_Notification(theSObject);
3180 //============================================================================
3181 /*! Function : modifySO_Notification
3182 * Purpose : This function tells all the observers that a SO has been modified and
3183 pass the mofification reason
3185 //============================================================================
3186 bool SALOMEDSImpl_Study::modifySO_Notification (const SALOMEDSImpl_SObject& theSObject, int reason)
3189 return _notifier->modifySO_Notification(theSObject, reason);
3194 //============================================================================
3195 /*! Function : setNotifier
3196 * Purpose : register a notifier
3198 //============================================================================
3199 void SALOMEDSImpl_Study::setNotifier(SALOMEDSImpl_AbstractCallback* notifier)
3204 static SALOMEDSImpl_AbstractCallback* & getGenObjRegister( DF_Document* doc )
3206 static std::vector< SALOMEDSImpl_AbstractCallback* > _genObjRegVec;
3207 if ( doc->GetDocumentID() >= (int)_genObjRegVec.size() )
3208 _genObjRegVec.resize( doc->GetDocumentID() + 1, 0 );
3209 return _genObjRegVec[ doc->GetDocumentID() ];
3212 //================================================================================
3214 * \brief Stores theRegister
3216 //================================================================================
3218 void SALOMEDSImpl_Study::setGenObjRegister(SALOMEDSImpl_AbstractCallback* theRegister)
3220 getGenObjRegister( _doc ) = theRegister;
3223 //================================================================================
3225 * \brief Indirectly invokes GenericObj_i::Register()
3227 //================================================================================
3229 void SALOMEDSImpl_Study::RegisterGenObj (const std::string& theIOR, DF_Label label)
3231 if ( SALOMEDSImpl_AbstractCallback* goRegister = getGenObjRegister( label.GetDocument() ))
3232 goRegister->RegisterGenObj( theIOR );
3235 //================================================================================
3237 * \brief Indirectly invokes GenericObj_i::UnRegister()
3239 //================================================================================
3241 void SALOMEDSImpl_Study::UnRegisterGenObj(const std::string& theIOR, DF_Label label)
3243 if ( SALOMEDSImpl_AbstractCallback* goRegister = getGenObjRegister( label.GetDocument() ))
3244 goRegister->UnRegisterGenObj( theIOR );
3247 //#######################################################################################################
3248 //# STATIC PRIVATE FUNCTIONS
3249 //#######################################################################################################
3251 //============================================================================
3252 /*! Function : SaveAttributes
3253 * Purpose : Save attributes for object
3255 //============================================================================
3256 static void SaveAttributes(const SALOMEDSImpl_SObject& aSO, HDFgroup *hdf_group_sobject)
3259 std::vector<DF_Attribute*> attrList = aSO.GetLabel().GetAttributes();
3260 DF_Attribute* anAttr = NULL;
3261 for(int i = 0, len = attrList.size(); i<len; i++) {
3262 anAttr = attrList[i];
3263 //The following attributes are not supposed to be written to the file
3264 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
3265 if(type == std::string("AttributeIOR")) continue; //IOR attribute is not saved
3266 std::string aSaveStr =anAttr->Save();
3267 //cout << "Saving: " << aSO.GetID() << " type: "<< type<<"|" << endl;
3268 size[0] = (hdf_int32) strlen(aSaveStr.c_str()) + 1;
3269 HDFdataset *hdf_dataset = new HDFdataset((char*)type.c_str(), hdf_group_sobject, HDF_STRING,size, 1);
3270 hdf_dataset->CreateOnDisk();
3271 hdf_dataset->WriteOnDisk((char*)aSaveStr.c_str());
3272 hdf_dataset->CloseOnDisk();
3273 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
3277 //===========================================================================
3278 //Function : ReadAttributes
3279 //===========================================================================
3280 static void ReadAttributes(SALOMEDSImpl_Study* theStudy,
3281 const SALOMEDSImpl_SObject& aSO,
3282 HDFdataset* hdf_dataset)
3284 hdf_dataset->OpenOnDisk();
3286 DF_Attribute* anAttr = NULL;
3287 char* current_string = new char[hdf_dataset->GetSize()+1];
3288 hdf_dataset->ReadFromDisk(current_string);
3289 //cout << "Reading attr type = " << hdf_dataset->GetName() << " SO = " << aSO.GetID() << endl;
3290 if (!strcmp(hdf_dataset->GetName(),"COMPONENTDATATYPE")) {
3291 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, "AttributeComment");
3293 else if (!strcmp(hdf_dataset->GetName(),"AttributeReference") ||
3294 !strcmp(hdf_dataset->GetName(),"Reference")) { // Old format maintainance
3295 theStudy->NewBuilder()->Addreference(aSO, theStudy->CreateObjectID(current_string));
3296 delete [] (current_string);
3297 hdf_dataset->CloseOnDisk();
3301 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, hdf_dataset->GetName());
3305 anAttr->Load(current_string);
3308 delete [] (current_string);
3309 hdf_dataset->CloseOnDisk();
3312 //============================================================================
3313 //Function : BuildlTree
3314 //============================================================================
3315 static void BuildTree (SALOMEDSImpl_Study* theStudy, HDFgroup* hdf_current_group)
3317 hdf_current_group->OpenOnDisk();
3318 SALOMEDSImpl_SObject aSO;
3319 char* Entry = hdf_current_group->GetName();
3320 if (strcmp(Entry,"STUDY_STRUCTURE") == 0) {
3321 aSO = theStudy->CreateObjectID("0:1");
3324 aSO = theStudy->CreateObjectID(Entry);
3327 char name[HDF_NAME_MAX_LEN+1];
3328 int nbsons = hdf_current_group->nInternalObjects();
3329 for (int i=0; i<nbsons; i++) {
3330 hdf_current_group->InternalObjectIndentify(i,name);
3331 if (strncmp(name, "INTERNAL_COMPLEX",16) == 0) continue;
3332 hdf_object_type type = hdf_current_group->InternalObjectType(name);
3334 if (type == HDF_DATASET) {
3335 HDFdataset* new_dataset = new HDFdataset(name,hdf_current_group);
3336 ReadAttributes(theStudy,aSO,new_dataset);
3337 new_dataset = 0; // will be deleted by father destructor
3339 else if (type == HDF_GROUP) {
3340 HDFgroup* new_group = new HDFgroup(name,hdf_current_group);
3341 BuildTree (theStudy, new_group);
3342 new_group = 0; // will be deleted by father destructor
3345 hdf_current_group->CloseOnDisk();
3349 //============================================================================
3350 //Function : Translate_IOR_to_persistentID
3351 //============================================================================
3352 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject& so,
3353 SALOMEDSImpl_Driver* engine,
3357 DF_ChildIterator itchild(so.GetLabel());
3358 std::string ior_string, persistent_string, curid;
3360 for (; itchild.More(); itchild.Next()) {
3361 SALOMEDSImpl_SObject current = SALOMEDSImpl_Study::SObject(itchild.Value());
3362 SALOMEDSImpl_AttributeIOR* IOR = NULL;
3363 if ((IOR=(SALOMEDSImpl_AttributeIOR*)current.GetLabel().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
3364 ior_string = IOR->Value();
3366 persistent_string = engine->IORToLocalPersistentID (current, ior_string, isMultiFile, isASCII);
3367 SALOMEDSImpl_AttributePersistentRef::Set(current.GetLabel(), persistent_string);
3369 Translate_IOR_to_persistentID (current, engine, isMultiFile, isASCII);
3373 void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup)
3378 HDFgroup* new_group =0;
3379 HDFdataset* new_dataset =0;
3381 char aVarName[HDF_NAME_MAX_LEN+1];
3382 char *currentVarType = 0;
3383 char *currentVarValue = 0;
3384 char *currentVarIndex = 0;
3386 //Open HDF group with notebook variables
3387 theGroup->OpenOnDisk();
3389 //Get Nb of variables
3390 int aNbVars = theGroup->nInternalObjects();
3392 std::map<int,SALOMEDSImpl_GenericVariable*> aVarsMap;
3394 for( int iVar=0;iVar < aNbVars;iVar++ ) {
3395 theGroup->InternalObjectIndentify(iVar,aVarName);
3396 hdf_object_type type = theGroup->InternalObjectType(aVarName);
3397 if(type == HDF_GROUP) {
3400 new_group = new HDFgroup(aVarName,theGroup);
3401 new_group->OpenOnDisk();
3404 new_dataset = new HDFdataset("VARIABLE_TYPE",new_group);
3405 new_dataset->OpenOnDisk();
3406 currentVarType = new char[new_dataset->GetSize()+1];
3407 new_dataset->ReadFromDisk(currentVarType);
3408 new_dataset->CloseOnDisk();
3409 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3412 if(new_group->ExistInternalObject("VARIABLE_INDEX")) {
3413 new_dataset = new HDFdataset("VARIABLE_INDEX",new_group);
3414 new_dataset->OpenOnDisk();
3415 currentVarIndex = new char[new_dataset->GetSize()+1];
3416 new_dataset->ReadFromDisk(currentVarIndex);
3417 new_dataset->CloseOnDisk();
3418 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3419 order = atoi(currentVarIndex);
3420 delete [] currentVarIndex;
3426 new_dataset = new HDFdataset("VARIABLE_VALUE",new_group);
3427 new_dataset->OpenOnDisk();
3428 currentVarValue = new char[new_dataset->GetSize()+1];
3429 new_dataset->ReadFromDisk(currentVarValue);
3430 new_dataset->CloseOnDisk();
3431 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3433 new_group->CloseOnDisk();
3434 new_group = 0; //will be deleted by hdf_sco_group destructor
3436 SALOMEDSImpl_GenericVariable::VariableTypes aVarType =
3437 SALOMEDSImpl_GenericVariable::String2VariableType(std::string(currentVarType));
3438 delete [] currentVarType;
3440 //Create variable and add it in the study
3441 SALOMEDSImpl_GenericVariable* aVariable =
3442 new SALOMEDSImpl_ScalarVariable(aVarType,std::string(aVarName));
3443 aVariable->Load(std::string(currentVarValue));
3444 aVarsMap.insert(std::make_pair(order,aVariable));
3445 delete [] currentVarValue;
3449 std::map<int,SALOMEDSImpl_GenericVariable*>::const_iterator it= aVarsMap.begin();
3450 for(;it!=aVarsMap.end();it++)
3451 theStudy->AddVariable((*it).second);
3453 theGroup->CloseOnDisk();