1 // Copyright (C) 2007-2019 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() : _doc(NULL)
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 return; // noop: already initialized
144 std::stringstream sstrm;
146 _name = "Study" + std::string(sstrm.str());
147 _doc = _appli->NewDocument("SALOME_STUDY");
152 _useCaseBuilder = new SALOMEDSImpl_UseCaseBuilder(_doc);
153 _builder = new SALOMEDSImpl_StudyBuilder(this);
154 _cb = new SALOMEDSImpl_Callback(_useCaseBuilder);
157 //Put on the root label a StudyHandle attribute to store the address of this object
158 //It will be used to retrieve the study object by DF_Label that belongs to the study
159 SALOMEDSImpl_StudyHandle::Set(_doc->Main().Root(), this);
161 // set Study properties
162 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
164 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
165 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
166 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
167 mn, hh, day, month, year);
168 aProp->SetCreationMode(1); //"from scratch"
171 //============================================================================
173 * Purpose : Clear study components
175 //============================================================================
176 void SALOMEDSImpl_Study::Clear()
178 if (_builder) delete _builder;
182 if (_useCaseBuilder) delete _useCaseBuilder;
183 _useCaseBuilder = NULL;
191 //============================================================================
193 * Purpose : Open a Study from it's persistent reference
195 //============================================================================
196 bool SALOMEDSImpl_Study::Open(const std::string& aUrl)
198 // Set "C" locale temporarily to avoid possible localization problems
199 Kernel_Utils::Localizer loc;
204 HDFfile *hdf_file =0;
205 HDFgroup *hdf_group_study_structure =0;
206 HDFgroup *hdf_notebook_vars = 0;
210 bool isASCII = false;
211 if (HDFascii::isASCII(aUrl.c_str())) {
213 char* aResultPath = HDFascii::ConvertFromASCIIToHDF(aUrl.c_str());
216 aC_HDFUrl = new char[strlen(aResultPath) + 19];
217 sprintf(aC_HDFUrl, "%shdf_from_ascii.hdf", aResultPath);
218 delete [] (aResultPath);
226 hdf_file = new HDFfile((char*)aHDFUrl.c_str());
228 hdf_file->OpenOnDisk(HDF_RDONLY);// mpv: was RDWR, but opened file can be write-protected too
233 eStr = new char[strlen(aUrl.c_str())+17];
234 sprintf(eStr,"Can't open file %s",aUrl.c_str());
236 _errorCode = std::string(eStr);
240 // Assign the value of the URL in the study object
243 SALOMEDSImpl_AttributePersistentRef::Set(_doc->Main(), aUrl);
245 if (!hdf_file->ExistInternalObject("STUDY_STRUCTURE")) {
246 _errorCode = "Study is empty";
250 //Create the Structure of the Document
251 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
254 BuildTree (this, hdf_group_study_structure);
258 char *eStr = new char [strlen(aUrl.c_str())+17];
259 sprintf(eStr,"Can't open file %s", aUrl.c_str());
260 _errorCode = std::string(eStr);
264 //Read and create notebook variables
265 if(hdf_file->ExistInternalObject("NOTEBOOK_VARIABLES")) {
266 hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
267 ReadNoteBookVariables(this, hdf_notebook_vars);
268 hdf_notebook_vars =0; //will be deleted by hdf_sco_group destructor
271 hdf_file->CloseOnDisk();
272 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
275 std::vector<std::string> aFilesToRemove;
276 aFilesToRemove.push_back("hdf_from_ascii.hdf");
277 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
280 delete hdf_file; // all related hdf objects will be deleted
282 // unlock study if it is locked, to set components versions
283 StudyUnlocker unlock(this);
285 //For old studies we have to add "unknown" version tag for all stored components
286 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
287 for (; itcomponent.More(); itcomponent.Next())
289 SALOMEDSImpl_SComponent sco = itcomponent.Value();
290 std::string aCompType = sco.GetComment();
291 if ( aCompType == SALOMEDSImpl_IParameters::getDefaultVisualComponent() ) continue;
292 if ( GetProperties()->GetComponentVersions( aCompType ).empty() )
293 GetProperties()->SetComponentVersion( aCompType, "" ); // empty version means "unknown"
299 //============================================================================
301 * Purpose : Save a Study to it's persistent reference
303 //============================================================================
304 bool SALOMEDSImpl_Study::Save(SALOMEDSImpl_DriverFactory* aFactory,
310 std::string url = URL();
312 _errorCode = "No path specified to save the study. Nothing done";
316 return Impl_SaveAs(url, aFactory, theMultiFile, theASCII);
322 //=============================================================================
323 /*! Function : SaveAs
324 * Purpose : Save a study to the persistent reference aUrl
326 //============================================================================
327 bool SALOMEDSImpl_Study::SaveAs(const std::string& aUrl,
328 SALOMEDSImpl_DriverFactory* aFactory,
333 return Impl_SaveAs(aUrl, aFactory, theMultiFile, theASCII);
336 //=============================================================================
337 /*! Function : _SaveProperties
338 * Purpose : save the study properties in HDF file
340 //============================================================================
341 bool SALOMEDSImpl_Study::Impl_SaveProperties(HDFgroup *hdf_group)
345 HDFdataset *hdf_dataset = 0;
349 // add modifications list (user and date of save)
350 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
352 // unlock study if it is locked, to set modification date
353 StudyUnlocker unlock(this);
355 int month=0,day=0,year=0,hh=0,mn=0,ss=0;
356 SALOMEDSImpl_Tool::GetSystemDate(year, month, day, hh, mn, ss);
357 aProp->SetModification(SALOMEDSImpl_Tool::GetUserName(),
358 mn, hh, day, month, year);
360 // lock study back if it was locked initially, to write correct value of Locked flag
363 std::vector<std::string> aNames;
364 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
366 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
368 std::string units = aProp->GetUnits();
369 std::string comment = aProp->GetComment();
371 std::map< std::string, std::vector<std::string> > allVersions = aProp->GetComponentsVersions();
372 std::map<std::string, std::string> versions;
374 int aLength = 0, aLength1 = 0, anIndex, i, unitsSize = 0, commentSize = 0;
376 for(i=1; i<=aNames.size(); i++)
377 aLength += aNames[i-1].size() + 1;
379 std::map< std::string, std::vector<std::string> >::const_iterator it;
380 for (it = allVersions.begin(); it != allVersions.end(); ++it ) {
381 std::string vlist = "";
382 std::vector<std::string> vl = it->second;
383 std::vector<std::string>::const_iterator vlit;
384 for ( vlit = vl.begin(); vlit != vl.end(); ++vlit ) {
385 if ( vlist != "" ) vlist += ";";
388 versions[ it->first ] = vlist;
389 aLength1 += it->first.size() + vlist.size() + 2;
392 unitsSize = units.size();
393 commentSize = comment.size();
396 //locked flag, modified flag,
397 //minutes, hours, day, months, year, user name, char(1),
398 //minutes, hours, day, months, year, user name, char(1),
399 //.....................................................,
400 //.....................................................,
401 //.....................................................,
402 //minutes, hours, day, months, year, user name, char(1), char(30) <- !!!! used to define end of section with modifications !!!!
403 //units, char(1), comment, char(30) <- !!!! used to define start of section with components' versions !!!!
404 //component=versions, char(1),
405 //component=versions, char(1),
406 //...........................,
407 //component=versions, char(1), char(0)
409 //string length: 1 byte = locked flag, 1 byte = modified flag, (12 + name length + 1) for each name and date, 1 byte (char(30) section delimiter)
410 // unit length + 1, comment length, "zero" byte
412 char* aProperty = new char[3 + aLength + 12 * aNames.size() + 1 + unitsSize + 1 + commentSize + 1 + aLength1 ];
414 sprintf(aProperty,"%c%c", (char)aProp->GetCreationMode(), (aProp->IsLocked())?'l':'u');
416 aLength = aNames.size();
418 for(anIndex = 0; anIndex<aLength; anIndex++) {
419 sprintf(&(aProperty[a]),"%2d%2d%2d%2d%4d%s",
420 (int)(aMinutes[anIndex]),
421 (int)(aHours[anIndex]),
422 (int)(aDays[anIndex]),
423 (int)(aMonths[anIndex]),
424 (int)(aYears[anIndex]),
425 aNames[anIndex].c_str());
426 a = strlen(aProperty);
430 //Write delimiter of the section to define end of the modifications section
433 //Write units if need
434 if(units.size() > 0) {
435 sprintf(&(aProperty[a]),"%s",units.c_str());
436 a = strlen(aProperty);
441 //Write comments if need
442 if(comment.size() > 0) {
443 sprintf(&(aProperty[a]),"%s",comment.c_str());
444 a = strlen(aProperty);
447 aProperty[a++] = 30; //delimiter of the component versions
449 std::map<std::string, std::string>::const_iterator versionsIt;
450 for ( versionsIt = versions.begin(); versionsIt != versions.end(); ++versionsIt ) {
451 sprintf(&(aProperty[a]),"%s=%s",
452 (char*)(versionsIt->first.c_str()),
453 (char*)(versionsIt->second.c_str()));
454 a = a + versionsIt->first.size() + versionsIt->second.size() + 1;
460 name_len = (hdf_int32) a;
461 size[0] = name_len + 1 ;
462 hdf_dataset = new HDFdataset("AttributeStudyProperties",hdf_group,HDF_STRING,size,1);
463 hdf_dataset->CreateOnDisk();
464 hdf_dataset->WriteOnDisk(aProperty);
465 hdf_dataset->CloseOnDisk();
466 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
469 aProp->SetModified(0);
473 //=============================================================================
474 /*! Function : _SaveAs
475 * Purpose : save the study in HDF file
477 //============================================================================
478 bool SALOMEDSImpl_Study::Impl_SaveAs(const std::string& aStudyUrl,
479 SALOMEDSImpl_DriverFactory* aFactory,
483 // Set "C" locale temporarily to avoid possible localization problems
484 Kernel_Utils::Localizer loc;
486 // HDF File will be composed of different part :
487 // * For each ComponentDataType, all data created by the component
488 // Information in data group hdf_group_datacomponent
489 // * Study Structure -> Exactly what is contained in Document
490 // Information in data group hdf_group_study_structure
495 HDFgroup *hdf_group_study_structure =0;
496 HDFgroup *hdf_sco_group =0;
497 HDFgroup *hdf_sco_group2 =0;
498 HDFgroup *hdf_notebook_vars =0;
499 HDFgroup *hdf_notebook_var = 0;
501 HDFgroup *hdf_group_datacomponent =0;
502 HDFdataset *hdf_dataset =0;
504 hdf_int32 name_len = 0;
505 std::string component_name;
507 // Store previous URL
508 std::string anOldName = URL();
510 // Map to store components' versions
511 std::map<std::string, std::string> componentVersions;
513 //Create a temporary url to which the study is saved
514 std::string aUrl = SALOMEDSImpl_Tool::GetTmpDir() + SALOMEDSImpl_Tool::GetNameFromPath(aStudyUrl);
516 // unlock study if it is locked, as some attributes need to be modified
517 StudyUnlocker unlock(this);
519 SALOMEDSImpl_StudyBuilder* SB= NewBuilder();
520 std::map<std::string, SALOMEDSImpl_Driver*> aMapTypeDriver;
523 // mpv 15.12.2003: for saving components we have to load all data from all modules
524 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
525 for (; itcomponent.More(); itcomponent.Next()) {
526 SALOMEDSImpl_SComponent sco = itcomponent.Value();
527 // if there is an associated Engine call its method for saving
528 std::string IOREngine;
530 SALOMEDSImpl_Driver* aDriver = NULL;
531 std::string aCompType = sco.GetComment();
532 if (!sco.ComponentIOR(IOREngine)) {
533 if (!aCompType.empty()) {
534 aDriver = aFactory->GetDriverByType(aCompType);
535 if (aDriver != NULL) {
536 if (!SB->LoadWith(sco, aDriver)) {
537 _errorCode = SB->GetErrorCode();
544 aDriver = aFactory->GetDriverByIOR(IOREngine);
546 aMapTypeDriver[aCompType] = aDriver;
549 _errorCode = "Can not restore information to resave it";
554 // VSR: set URL to new file name
555 // VSR: remember to set previous name if save operation fails
558 // To change for Save
559 // Do not have to do a new file but just a Open??? Rewrite all information after erasing everything??
560 hdf_file = new HDFfile((char*)aUrl.c_str());
561 hdf_file->CreateOnDisk();
563 //-----------------------------------------------------------------------
564 // 1 - Create a groupe for each SComponent and Update the PersistanceRef
565 //-----------------------------------------------------------------------
566 hdf_group_datacomponent = new HDFgroup("DATACOMPONENT",hdf_file);
567 hdf_group_datacomponent->CreateOnDisk();
569 for (itcomponent.Init(); itcomponent.More(); itcomponent.Next()) {
570 SALOMEDSImpl_SComponent sco = itcomponent.Value();
572 std::string scoid = sco.GetID();
573 hdf_sco_group = new HDFgroup((char*)scoid.c_str(), hdf_group_datacomponent);
574 hdf_sco_group->CreateOnDisk();
576 std::string componentDataType = sco.ComponentDataType();
577 std::string IOREngine;
578 if (sco.ComponentIOR(IOREngine)) {
579 // Engine should be already in the map as it was to added before
580 SALOMEDSImpl_Driver* Engine = aMapTypeDriver[componentDataType];
581 if (Engine != NULL) {
582 SALOMEDSImpl_TMPFile* aStream = NULL;
585 componentVersions[ componentDataType ] = Engine->Version();
587 if (theASCII) aStream = Engine->SaveASCII(sco,
588 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
591 else aStream = Engine->Save(sco,
592 SALOMEDSImpl_Tool::GetDirFromPath(aUrl),
595 HDFdataset *hdf_dataset;
596 hdf_size aHDFSize[1];
597 if (length > 0) { //The component saved some auxiliary files, then put them into HDF file
598 aHDFSize[0] = length;
600 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group, HDF_STRING, aHDFSize, 1);
601 hdf_dataset->CreateOnDisk();
602 hdf_dataset->WriteOnDisk(aStream->Data()); //Save the stream in the HDF file
603 hdf_dataset->CloseOnDisk();
606 if (aStream) delete aStream;
608 // store multifile state
610 hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
611 hdf_dataset->CreateOnDisk();
612 hdf_dataset->WriteOnDisk((void*)(theMultiFile?"M":"S")); // save: multi or single
613 hdf_dataset->CloseOnDisk();
614 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
617 hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group, HDF_STRING, aHDFSize, 1);
618 hdf_dataset->CreateOnDisk();
619 hdf_dataset->WriteOnDisk((void*)(theASCII?"A":"B")); // save: ASCII or BINARY
620 hdf_dataset->CloseOnDisk();
621 hdf_dataset=0; //will be deleted by hdf_sco_AuxFiles destructor
622 // Creation of the persistence reference attribute
623 Translate_IOR_to_persistentID (sco, Engine, theMultiFile, theASCII);
626 hdf_sco_group->CloseOnDisk();
627 hdf_sco_group=0; // will be deleted by hdf_group_datacomponent destructor
629 hdf_group_datacomponent->CloseOnDisk();
630 hdf_group_datacomponent =0; // will be deleted by hdf_file destructor
632 //-----------------------------------------------------------------------
633 //3 - Write the Study Structure
634 //-----------------------------------------------------------------------
635 hdf_group_study_structure = new HDFgroup("STUDY_STRUCTURE",hdf_file);
636 hdf_group_study_structure->CreateOnDisk();
637 // save component attributes
638 for (itcomponent.Init(); itcomponent.More(); itcomponent.Next()) {
639 SALOMEDSImpl_SComponent SC = itcomponent.Value();
640 std::string scid = SC.GetID();
641 hdf_sco_group2 = new HDFgroup((char*)scid.c_str(), hdf_group_study_structure);
642 hdf_sco_group2->CreateOnDisk();
643 SaveAttributes(SC, hdf_sco_group2);
644 // ComponentDataType treatment
645 component_name = SC.ComponentDataType();
646 name_len = (hdf_int32)component_name.length();
647 size[0] = name_len +1 ;
648 hdf_dataset = new HDFdataset("COMPONENTDATATYPE",hdf_sco_group2,HDF_STRING,size,1);
649 hdf_dataset->CreateOnDisk();
650 hdf_dataset->WriteOnDisk((char*)component_name.c_str());
651 hdf_dataset->CloseOnDisk();
652 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
653 Impl_SaveObject(SC, hdf_sco_group2);
654 hdf_sco_group2->CloseOnDisk();
655 hdf_sco_group2=0; // will be deleted by hdf_group_study_structure destructor
657 //-----------------------------------------------------------------------
658 //4 - Write the Study UseCases Structure
659 //-----------------------------------------------------------------------
660 SALOMEDSImpl_SObject aSO = FindObjectID(USE_CASE_LABEL_ID);
662 HDFgroup *hdf_soo_group = new HDFgroup(USE_CASE_LABEL_ID,hdf_group_study_structure);
663 hdf_soo_group->CreateOnDisk();
664 SaveAttributes(aSO, hdf_soo_group);
665 Impl_SaveObject(aSO, hdf_soo_group);
666 hdf_soo_group->CloseOnDisk();
667 hdf_soo_group=0; // will be deleted by hdf_group_study_structure destructor
669 //-----------------------------------------------------------------------
670 //5 - Write the NoteBook Variables
671 //-----------------------------------------------------------------------
673 //5.1 Create group to store all note book variables
674 hdf_notebook_vars = new HDFgroup("NOTEBOOK_VARIABLES",hdf_file);
675 hdf_notebook_vars->CreateOnDisk();
677 std::string varValue;
679 std::string varIndex;
681 for (int i=0 ;i < myNoteBookVars.size(); i++ ) {
682 // For each variable create HDF group
683 hdf_notebook_var = new HDFgroup((char*)myNoteBookVars[i]->Name().c_str(),hdf_notebook_vars);
684 hdf_notebook_var->CreateOnDisk();
686 // Save Variable type
687 varType = myNoteBookVars[i]->SaveType();
688 name_len = (hdf_int32) varType.length();
689 size[0] = name_len +1 ;
690 hdf_dataset = new HDFdataset("VARIABLE_TYPE",hdf_notebook_var,HDF_STRING,size,1);
691 hdf_dataset->CreateOnDisk();
692 hdf_dataset->WriteOnDisk((char*)varType.c_str());
693 hdf_dataset->CloseOnDisk();
694 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
697 sprintf(buffer,"%d",i);
698 varIndex= std::string(buffer);
699 name_len = (hdf_int32) varIndex.length();
700 size[0] = name_len +1 ;
701 hdf_dataset = new HDFdataset("VARIABLE_INDEX",hdf_notebook_var,HDF_STRING,size,1);
702 hdf_dataset->CreateOnDisk();
703 hdf_dataset->WriteOnDisk((char*)varIndex.c_str());
704 hdf_dataset->CloseOnDisk();
705 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
707 // Save Variable value
708 varValue = myNoteBookVars[i]->Save();
709 name_len = (hdf_int32) varValue.length();
710 size[0] = name_len +1 ;
711 hdf_dataset = new HDFdataset("VARIABLE_VALUE",hdf_notebook_var,HDF_STRING,size,1);
712 hdf_dataset->CreateOnDisk();
713 hdf_dataset->WriteOnDisk((char*)varValue.c_str());
714 hdf_dataset->CloseOnDisk();
715 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
716 hdf_notebook_var->CloseOnDisk();
717 hdf_notebook_var = 0; //will be deleted by hdf_sco_group destructor
719 hdf_notebook_vars->CloseOnDisk();
720 hdf_notebook_vars = 0; //will be deleted by hdf_sco_group destructor
722 // record component versions
723 std::map<std::string, std::string>::const_iterator itVersions;
724 for ( itVersions = componentVersions.begin(); itVersions != componentVersions.end(); ++itVersions )
725 GetProperties()->SetComponentVersion( itVersions->first, itVersions->second );
727 // lock study back if it was locked initially, to write correct value of Locked flag
730 //-----------------------------------------------------------------------
731 //6 - Write the Study Properties
732 //-----------------------------------------------------------------------
733 std::string study_name = Name();
734 name_len = (hdf_int32) study_name.size();
735 size[0] = name_len +1 ;
736 hdf_dataset = new HDFdataset("STUDY_NAME",hdf_group_study_structure,HDF_STRING,size,1);
737 hdf_dataset->CreateOnDisk();
738 hdf_dataset->WriteOnDisk((char*)study_name.c_str());
739 hdf_dataset->CloseOnDisk();
740 hdf_dataset=0; // will be deleted by hdf_group_study_structure destructor
742 Impl_SaveProperties(hdf_group_study_structure);
743 hdf_group_study_structure->CloseOnDisk();
744 hdf_file->CloseOnDisk();
746 hdf_group_study_structure =0; // will be deleted by hdf_file destructor
747 delete hdf_file; // recursively deletes all hdf objects...
749 catch (HDFexception) {
750 _errorCode = "HDFexception ! ";
751 URL( anOldName ); // VSR: restore previous url if operation is failed
754 catch (std::exception& exc) {
755 _errorCode = const_cast<char*>(exc.what());
756 URL( anOldName ); // VSR: restore previous url if operation is failed
760 _errorCode = "Unknown exception ! ";
761 URL( anOldName ); // VSR: restore previous url if operation is failed
764 if (theASCII) { // save file in ASCII format
765 HDFascii::ConvertFromHDFToASCII(aUrl.c_str(), true);
768 // Now it's necessary to copy files from the temporary directory to the user defined directory.
769 // The easiest way to get a list of file in the temporary directory
771 std::string aCmd, aTmpFileDir = SALOMEDSImpl_Tool::GetTmpDir();
772 std::string aTmpFile = aTmpFileDir +"files";
773 std::string aStudyTmpDir = SALOMEDSImpl_Tool::GetDirFromPath(aUrl);
776 aCmd = "dir /B \"" + aStudyTmpDir +"\" > \"" + aTmpFile + "\"";
778 aCmd ="ls -1 \"" + aStudyTmpDir +"\" > " + aTmpFile;
780 #if defined(WIN32) && defined(UNICODE)
781 std::wstring awCmd = Kernel_Utils::utf8_decode_s(aCmd);
782 _wsystem( awCmd.c_str() );
784 system(aCmd.c_str());
787 // Iterate and move files in the temporary directory
788 #if defined(WIN32) && defined(UNICODE)
789 std::wstring awTmpFile = Kernel_Utils::utf8_decode_s(aTmpFile);
790 FILE* fp = _wfopen(awTmpFile.c_str(), L"rb");
792 FILE* fp = fopen(aTmpFile.c_str(), "rb");
795 URL( anOldName ); // VSR: restore previous url if operation is failed
798 char* buffer = new char[2047];
800 while (!feof(fp) && !errors) {
801 if ((fgets(buffer, 2046, fp)) == NULL) break;
802 size_t aLen = strlen(buffer);
803 if (buffer[aLen-1] == '\n') buffer[aLen-1] = char(0);
805 aCmd = "move /Y \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl) +"\" 2>NUL";
807 aCmd = "mv -f \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl)+"\"";
809 #if defined(WIN32) && defined(UNICODE)
810 std::wstring awCmd = Kernel_Utils::utf8_decode_s(aCmd);
811 errors = _wsystem(awCmd.c_str());
813 errors = system(aCmd.c_str());
823 std::wstring aTmpFileToDelete = Kernel_Utils::utf8_decode_s(aTmpFile);
826 std::string aTmpFileToDelete = aTmpFile;
828 DeleteFile(aTmpFileToDelete.c_str());
830 unlink(aTmpFile.c_str());
835 std::wstring aTmpFileDirToDelete = Kernel_Utils::utf8_decode_s( aTmpFileDir );
836 std::wstring aStudyTmpDirToDelete = Kernel_Utils::utf8_decode_s( aStudyTmpDir );
838 std::string aTmpFileDirToDelete = aTmpFileDir;
839 std::string aStudyTmpDirToDelete = aStudyTmpDir;
841 RemoveDirectory( aTmpFileDirToDelete.c_str() );
842 RemoveDirectory( aStudyTmpDirToDelete.c_str() );
844 rmdir(aTmpFileDir.c_str());
845 rmdir(aStudyTmpDir.c_str());
849 // VSR: finally, if all is done without errors, mark study as Saved
853 std::map<std::string, SALOMEDSImpl_Driver*>::iterator n2dr = aMapTypeDriver.begin();
854 for ( ; n2dr != aMapTypeDriver.end(); ++n2dr )
860 //============================================================================
861 /*! Function : Impl_SaveObject
864 //============================================================================
865 bool SALOMEDSImpl_Study::Impl_SaveObject(const SALOMEDSImpl_SObject& SC,
866 HDFgroup *hdf_group_datatype)
870 // Write in group hdf_group_datatype all information of SObject SC
871 // Iterative function to parse all SObjects under a SComponent
873 HDFgroup *hdf_group_sobject = 0;
875 DF_ChildIterator itchild(SC.GetLabel());
876 for (; itchild.More(); itchild.Next()) {
877 // mpv: don't save empty labels
878 std::vector<DF_Attribute*> attr = itchild.Value().GetAttributes();
879 if (attr.size() == 0) { //No attributes on the label
880 DF_ChildIterator subchild(itchild.Value());
881 if (!subchild.More()) {
884 subchild.Init(itchild.Value(), true);
886 for (; subchild.More() && anEmpty; subchild.Next()) {
887 std::vector<DF_Attribute*> attr2 = subchild.Value().GetAttributes();
889 anEmpty = false; //There are attributes on the child label
893 if (anEmpty) continue;
896 SALOMEDSImpl_SObject SO = SALOMEDSImpl_Study::SObject(itchild.Value());
898 std::string scoid = SO.GetID();
899 hdf_group_sobject = new HDFgroup(scoid.c_str(), hdf_group_datatype);
900 hdf_group_sobject->CreateOnDisk();
901 SaveAttributes(SO, hdf_group_sobject);
902 Impl_SaveObject(SO, hdf_group_sobject);
903 hdf_group_sobject->CloseOnDisk();
904 hdf_group_sobject =0; // will be deleted by father hdf object destructor
909 //============================================================================
910 /*! Function : CanCopy
913 //============================================================================
914 bool SALOMEDSImpl_Study::CanCopy(const SALOMEDSImpl_SObject& theObject,
915 SALOMEDSImpl_Driver* theEngine)
918 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
919 if (!aComponent) return false;
920 if (aComponent.GetLabel() == theObject.GetLabel()) return false;
921 std::string IOREngine;
922 if (!aComponent.ComponentIOR(IOREngine)) return false;
923 if (theEngine == NULL) return false;
924 return theEngine->CanCopy(theObject);
927 //============================================================================
928 /*! Function : CopyLabel
931 //============================================================================
932 bool SALOMEDSImpl_Study::CopyLabel(SALOMEDSImpl_Driver* theEngine,
933 const int theSourceStartDepth,
934 const DF_Label& theSource,
935 const DF_Label& theDestinationMain)
940 DF_Label aTargetLabel = theDestinationMain;
941 DF_Label aAuxTargetLabel = theDestinationMain.Father().FindChild(2);
942 for(a = theSource.Depth() - theSourceStartDepth; a > 0 ; a--) {
943 DF_Label aSourceLabel = theSource;
944 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
945 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
946 aAuxTargetLabel = aAuxTargetLabel.FindChild(aSourceLabel.Tag());
948 // iterate attributes
949 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
950 for(int i = 0, len = attrList.size(); i<len; i++) {
951 DF_Attribute* anAttr = attrList[i];
952 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
953 if (type.substr(0, 17) == std::string("AttributeTreeNode")) continue; // never copy tree node attribute
954 if (type == std::string("AttributeTarget")) continue; // and target attribute
956 if (type == std::string("AttributeReference")) { // reference copied as Comment in aux tree
957 DF_Label aReferenced = dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Get();
958 std::string anEntry = aReferenced.Entry();
959 // store the value of name attribute of referenced label
960 SALOMEDSImpl_AttributeName* aNameAttribute;
961 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aReferenced.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
963 anEntry += aNameAttribute->Value();
965 SALOMEDSImpl_AttributeComment::Set(aAuxTargetLabel, anEntry);
969 if (type == std::string("AttributeIOR")) { // IOR => ID and TMPFile of Engine
970 std::string anEntry = theSource.Entry();
971 SALOMEDSImpl_SObject aSO = FindObjectID(anEntry);
974 SALOMEDSImpl_TMPFile* aStream = theEngine->CopyFrom(aSO, anObjID, aLen);
975 std::string aResStr("");
976 for(a = 0; a < aLen; a++) {
977 aResStr += (char)(aStream->Get(a));
980 if(aStream) delete aStream;
982 SALOMEDSImpl_AttributeInteger::Set(aAuxTargetLabel, anObjID);
983 SALOMEDSImpl_AttributeName::Set(aAuxTargetLabel, aResStr);
986 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
987 aTargetLabel.AddAttribute(aNewAttribute);
988 anAttr->Paste(aNewAttribute);
994 //============================================================================
998 //============================================================================
999 bool SALOMEDSImpl_Study::Copy(const SALOMEDSImpl_SObject& theObject,
1000 SALOMEDSImpl_Driver* theEngine)
1004 // adoptation for alliances datamodel copy: without IOR attributes !!!
1005 bool aStructureOnly; // copy only SObjects and attributes without component help
1006 aStructureOnly = !theObject.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID());
1009 _errorCode = "Document is null";
1013 //Clear the clipboard
1014 _clipboard->Main().Root().ForgetAllAttributes(true);
1015 _appli->Close(_clipboard);
1016 _clipboard = _appli->NewDocument("SALOME_STUDY");
1018 // set component data type to the name attribute of root label
1019 if (!aStructureOnly) {
1020 SALOMEDSImpl_AttributeComment::Set(_clipboard->Main().Root(),
1021 theEngine->ComponentDataType());
1023 // iterate all theObject's label children
1024 DF_Label aStartLabel = theObject.GetLabel();
1025 int aSourceStartDepth = aStartLabel.Depth();
1027 // copy main source label
1028 CopyLabel(theEngine, aSourceStartDepth, aStartLabel, _clipboard->Main());
1030 // copy all subchildren of the main source label (all levels)
1031 DF_ChildIterator anIterator(aStartLabel, true);
1032 for(; anIterator.More(); anIterator.Next()) {
1033 CopyLabel(theEngine, aSourceStartDepth, anIterator.Value(), _clipboard->Main());
1039 //============================================================================
1040 /*! Function : CanPaste
1043 //============================================================================
1044 bool SALOMEDSImpl_Study::CanPaste(const SALOMEDSImpl_SObject& theObject,
1045 SALOMEDSImpl_Driver* theEngine)
1050 _errorCode = "Clipboard is null";
1054 SALOMEDSImpl_AttributeComment* aCompName = NULL;
1055 if (!(aCompName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1056 _errorCode = "Clipboard has no component type";
1059 SALOMEDSImpl_AttributeInteger* anObjID;
1060 if (!(anObjID=(SALOMEDSImpl_AttributeInteger*)_clipboard->Main().Father().FindChild(2).FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
1061 _errorCode = "Clipboard has no object id";
1064 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1066 _errorCode = "Object doesn't belong to component";
1070 std::string IOREngine;
1071 if (!aComponent.ComponentIOR(IOREngine)) {
1072 _errorCode = "component has no IOR";
1075 return theEngine->CanPaste(aCompName->Value(), anObjID->Value());
1078 //============================================================================
1079 /*! Function : PasteLabel
1082 //============================================================================
1083 DF_Label SALOMEDSImpl_Study::PasteLabel(SALOMEDSImpl_Driver* theEngine,
1084 const DF_Label& theSource,
1085 const DF_Label& theDestinationStart,
1086 const bool isFirstElement)
1090 // get corresponding source, target and auxiliary labels
1091 DF_Label aTargetLabel = theDestinationStart;
1093 DF_Label aAuxSourceLabel = theSource.Root().FindChild(2);
1095 if (!isFirstElement) {
1096 for(a = theSource.Depth() - 1; a > 0 ; a--) {
1097 DF_Label aSourceLabel = theSource;
1098 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
1099 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
1100 aAuxSourceLabel = aAuxSourceLabel.FindChild(aSourceLabel.Tag());
1102 SALOMEDSImpl_SObject so = GetSObject(aTargetLabel);
1103 addSO_Notification(so);
1106 // check auxiliary label for TMPFile => IOR
1107 SALOMEDSImpl_AttributeName* aNameAttribute = NULL;
1108 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1109 SALOMEDSImpl_AttributeInteger* anObjID = (SALOMEDSImpl_AttributeInteger*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeInteger::GetID());
1110 SALOMEDSImpl_AttributeComment* aComponentName = (SALOMEDSImpl_AttributeComment*)theSource.Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID());
1111 std::string aCompName = aComponentName->Value();
1113 if (theEngine->CanPaste(aCompName, anObjID->Value())) {
1114 std::string aTMPStr = aNameAttribute->Value();
1115 int aLen = aTMPStr.size();
1116 unsigned char* aStream = NULL;
1118 aStream = new unsigned char[aLen+10];
1119 for(a = 0; a < aLen; a++) {
1120 aStream[a] = aTMPStr[a];
1124 std::string anEntry = aTargetLabel.Entry();
1125 SALOMEDSImpl_SObject aPastedSO = FindObjectID(anEntry);
1127 if (isFirstElement) {
1128 std::string aDestEntry = theEngine->PasteInto(aStream,
1131 aPastedSO.GetFatherComponent());
1132 aTargetLabel = DF_Label::Label(theDestinationStart, aDestEntry);
1134 theEngine->PasteInto(aStream, aLen, anObjID->Value(), aPastedSO);
1136 if(aStream != NULL) delete []aStream;
1140 // iterate attributes
1141 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
1142 for(int i = 0, len = attrList.size(); i<len; i++) {
1143 DF_Attribute* anAttr = attrList[i];
1144 if (aTargetLabel.FindAttribute(anAttr->ID())) {
1145 aTargetLabel.ForgetAttribute(anAttr->ID());
1147 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
1148 aTargetLabel.AddAttribute(aNewAttribute);
1149 anAttr->Paste(aNewAttribute);
1152 // check auxiliary label for Comment => reference or name attribute of the referenced object
1153 SALOMEDSImpl_AttributeComment* aCommentAttribute = NULL;
1154 if ((aCommentAttribute=(SALOMEDSImpl_AttributeComment*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1155 char * anEntry = new char[aCommentAttribute->Value().size() + 1];
1156 strcpy(anEntry, std::string(aCommentAttribute->Value()).c_str());
1157 char* aNameStart = strchr(anEntry, ' ');
1162 // copy to the same study, reanimate reference
1163 DF_Label aRefLabel = DF_Label::Label(aTargetLabel, anEntry);
1164 SALOMEDSImpl_AttributeReference::Set(aTargetLabel, aRefLabel);
1165 // target attributes structure support
1166 SALOMEDSImpl_AttributeTarget::Set(aRefLabel)->Add(SALOMEDSImpl_Study::SObject(aTargetLabel));
1171 return aTargetLabel;
1174 //============================================================================
1175 /*! Function : Paste
1178 //============================================================================
1179 SALOMEDSImpl_SObject SALOMEDSImpl_Study::Paste(const SALOMEDSImpl_SObject& theObject,
1180 SALOMEDSImpl_Driver* theEngine)
1184 SALOMEDSImpl_SObject so;
1186 // if study is locked, then paste can't be done
1187 if (GetProperties()->IsLocked()) {
1188 _errorCode = "LockProtection";
1189 throw LockProtection("LockProtection");
1192 // if there is no component name, then paste only SObjects and attributes: without component help
1193 SALOMEDSImpl_AttributeComment* aComponentName = NULL;
1194 bool aStructureOnly = !(aComponentName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()));
1196 // CAF document of current study usage
1198 _errorCode = "Document is null";
1202 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1204 // fill root inserted SObject
1205 DF_Label aStartLabel;
1206 if (aStructureOnly) {
1207 DF_Label anObjectLabel = DF_Label::Label(_doc->Main(), theObject.GetID());
1208 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), anObjectLabel, false);
1210 DF_Label aComponentLabel = DF_Label::Label(_doc->Main(), aComponent.GetID());
1211 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), aComponentLabel, true);
1214 // paste all sublebels
1215 DF_ChildIterator anIterator(_clipboard->Main(), true);
1216 for(; anIterator.More(); anIterator.Next()) {
1217 PasteLabel(theEngine, anIterator.Value(), aStartLabel, false);
1220 return SALOMEDSImpl_Study::SObject(aStartLabel);
1223 //============================================================================
1224 /*! Function : GetPersistentReference
1225 * Purpose : Get persistent reference of study (idem URL())
1227 //============================================================================
1228 std::string SALOMEDSImpl_Study::GetPersistentReference()
1234 //============================================================================
1235 /*! Function : IsEmpty
1236 * Purpose : Detect if study is empty
1238 //============================================================================
1239 bool SALOMEDSImpl_Study::IsEmpty()
1242 if (!_doc) return true;
1243 return _doc->IsEmpty();
1246 //============================================================================
1247 /*! Function : FindComponent
1248 * Purpose : Find a Component with ComponentDataType = aComponentName
1250 //============================================================================
1251 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const std::string& aComponentName)
1256 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1257 SALOMEDSImpl_SComponent compo;
1259 for (; itcomp.More(); itcomp.Next()) {
1260 SALOMEDSImpl_SComponent SC = itcomp.Value();
1261 name = SC.ComponentDataType();
1262 if(aComponentName == name) {
1270 _errorCode = "No component was found";
1276 //============================================================================
1277 /*! Function : FindComponentID
1278 * Purpose : Find a Component from it's ID
1280 //============================================================================
1281 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const std::string& aComponentID)
1285 // Iterate on each components defined in the study
1286 // Get the component ID and compare with aComponentID
1289 SALOMEDSImpl_SComponent compo;
1291 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1292 for (; itcomp.More(); itcomp.Next()) {
1293 SALOMEDSImpl_SComponent SC = itcomp.Value();
1295 if(aComponentID == ID)
1297 // ComponentID found
1304 _errorCode = "No component was found";
1311 //============================================================================
1312 /*! Function : FindObject
1313 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1315 //============================================================================
1316 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const std::string& anObjectName)
1320 // Iterate to all components defined in the study
1321 // After testing the component name, iterate in all objects defined under
1322 // components (function _FindObject)
1324 SALOMEDSImpl_SObject RefSO;
1326 SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
1327 for (; it.More();it.Next()){
1330 SALOMEDSImpl_SComponent SC = it.Value();
1331 if (SC.GetName() == anObjectName)
1337 if (!_find) RefSO = _FindObject(SC, anObjectName, _find);
1340 if(!RefSO) _errorCode = "No object was found";
1344 //============================================================================
1345 /*! Function : FindObjectID
1346 * Purpose : Find an Object with ID = anObjectID
1348 //============================================================================
1349 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const std::string& anObjectID)
1352 SALOMEDSImpl_SObject so;
1354 // Convert aSO->GetID in DF_Label.
1355 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
1358 _errorCode = "No label was found by ID";
1361 return GetSObject(Lab);
1365 //============================================================================
1366 /*! Function : CreateObjectID
1367 * Purpose : Creates an Object with ID = anObjectID
1369 //============================================================================
1370 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const std::string& anObjectID)
1373 SALOMEDSImpl_SObject so;
1375 // Convert aSO->GetID in DF_Label.
1376 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
1379 _errorCode = "Can not create a label";
1382 return GetSObject(Lab);
1386 //============================================================================
1387 /*! Function : FindObjectByName
1388 * Purpose : Find Objects with SALOMEDSImpl_Name = anObjectName in a Component
1389 * : with ComponentDataType = aComponentName
1391 //============================================================================
1392 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const std::string& anObjectName,
1393 const std::string& aComponentName)
1397 std::vector<SALOMEDSImpl_SObject> listSO;
1399 SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
1401 _errorCode = "Can not find the component";
1405 // Iterate on each object and subobject of the component
1406 // If objectName is found add it to the list of SObjects
1407 std::string childName ;
1409 std::string compoId = compo.GetID();
1410 SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
1411 for ( ; it.More(); it.Next() ) {
1413 SALOMEDSImpl_SObject CSO = it.Value();
1414 if ( CSO.GetName() == anObjectName ) {
1416 listSO.push_back(CSO) ;
1419 /* looks also for eventual children */
1420 bool found = false ;
1421 CSO = _FindObject( CSO, anObjectName, found ) ;
1423 listSO.push_back(CSO) ;
1432 //============================================================================
1433 /*! Function : FindObjectIOR
1434 * Purpose : Find an Object with IOR = anObjectIOR
1436 //============================================================================
1437 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const std::string& anObjectIOR)
1441 SALOMEDSImpl_SObject aResult ;
1443 // searching in the datamap for optimization
1444 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anObjectIOR);
1445 if (it != myIORLabels.end()) {
1446 aResult = GetSObject(it->second);
1447 // 11 oct 2002: forbidden attributes must be checked here
1448 if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
1449 myIORLabels.erase(anObjectIOR);
1450 aResult = SALOMEDSImpl_SObject();
1454 if(!aResult) _errorCode = "No object was found";
1458 //============================================================================
1459 /*! Function : FindObjectByPath
1460 * Purpose : Find an Object by its path = thePath
1462 //============================================================================
1463 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const std::string& thePath)
1467 std::string aPath(thePath), aToken;
1468 SALOMEDSImpl_SObject aSO;
1469 int aLength = aPath.size();
1470 bool isRelative = false;
1472 if(aLength == 0) { //Empty path - return the current context
1476 if(aPath[0] != '/') //Relative path
1479 DF_ChildIterator anIterator;
1481 SALOMEDSImpl_AttributeName* anAttr;
1487 if(aPath.size() == 1 && aPath[0] == '/') { //Root
1488 return GetSObject(_doc->Main());
1490 anIterator.Init(_doc->Main(), false);
1493 std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
1494 for(int i = 0, len = vs.size(); i<len; i++) {
1497 if(aToken.size() == 0) break;
1499 for ( ; anIterator.More(); anIterator.Next() ) {
1500 aLabel = anIterator.Value();
1501 if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1502 if(anAttr->Value() == aToken) {
1503 if(i == (len-1)) { //The searched label is found (no part of the path is left)
1504 return GetSObject(aLabel);
1507 anIterator.Init(aLabel, false);
1515 if(!aSO) _errorCode = "No object was found";
1519 //============================================================================
1520 /*! Function : GetObjectPath
1523 //============================================================================
1524 std::string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
1528 std::string aPath("");
1530 _errorCode = "Null object";
1534 std::string aName = theObject.GetName();
1535 if(!aName.empty() && aName != "" ) {
1536 std::string aValue("/");
1540 SALOMEDSImpl_SObject aFather = theObject.GetFather();
1542 aName = aFather.GetName();
1543 if(!aName.empty() && aName != "") {
1544 aValue = GetObjectPath(aFather);
1545 aPath = aValue + aPath;
1554 //============================================================================
1555 /*! Function : GetObjectPathByIOR
1558 //============================================================================
1559 std::string SALOMEDSImpl_Study::GetObjectPathByIOR(const std::string& theIOR)
1564 SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
1566 _errorCode = "No SObject was found by IOR";
1570 return GetObjectPath(so);
1573 //============================================================================
1574 /*! Function : NewChildIterator
1575 * Purpose : Create a ChildIterator from an SObject
1577 //============================================================================
1578 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
1581 return SALOMEDSImpl_ChildIterator(aSO);
1585 //============================================================================
1586 /*! Function : NewComponentIterator
1587 * Purpose : Create a SComponentIterator
1589 //============================================================================
1590 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
1593 return SALOMEDSImpl_SComponentIterator(_doc);
1597 //============================================================================
1598 /*! Function : NewBuilder
1599 * Purpose : Create a StudyBuilder
1601 //============================================================================
1602 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
1606 _builder->SetOnAddSObject(_cb);
1607 _builder->SetOnRemoveSObject(_cb);
1613 //============================================================================
1615 * Purpose : get study name
1617 //============================================================================
1618 std::string SALOMEDSImpl_Study::Name()
1621 return Kernel_Utils::GetBaseName( _name, false );
1624 //============================================================================
1626 * Purpose : set study name
1628 //============================================================================
1629 void SALOMEDSImpl_Study::Name(const std::string& name)
1635 //============================================================================
1636 /*! Function : IsSaved
1637 * Purpose : get if study has been saved
1639 //============================================================================
1640 bool SALOMEDSImpl_Study::IsSaved()
1646 //============================================================================
1647 /*! Function : IsSaved
1648 * Purpose : set if study has been saved
1650 //============================================================================
1651 void SALOMEDSImpl_Study::IsSaved(bool save)
1655 if(save) _doc->SetModified(false);
1658 //============================================================================
1659 /*! Function : IsModified
1660 * Purpose : Detect if a Study has been modified since it has been saved
1662 //============================================================================
1663 bool SALOMEDSImpl_Study::IsModified()
1667 // True if is modified
1668 if (_doc && _doc->IsModified()) return true;
1673 //============================================================================
1675 * Purpose : get URL of the study (persistent reference of the study)
1677 //============================================================================
1678 std::string SALOMEDSImpl_Study::URL()
1684 //============================================================================
1686 * Purpose : set URL of the study (persistent reference of the study)
1688 //============================================================================
1689 void SALOMEDSImpl_Study::URL(const std::string& url)
1695 /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
1696 std::string tmp(_URL);
1698 char *aName = (char*)tmp.ToCString();
1699 char *adr = strtok(aName, "/");
1703 adr = strtok(NULL, "/");
1709 //============================================================================
1710 /*! Function : _FindObject
1711 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1713 //============================================================================
1714 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
1715 const std::string& theObjectName,
1718 SALOMEDSImpl_SObject RefSO;
1719 if(!SO) return RefSO;
1721 // Iterate on each objects and subobjects of the component
1722 // If objectName find, stop the loop and get the object reference
1723 SALOMEDSImpl_AttributeName* anAttr;
1725 std::string soid = SO.GetID();
1726 DF_ChildIterator it(SO.GetLabel());
1727 for (; it.More(); it.Next()){
1730 if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1732 std::string Val(anAttr->Value());
1733 if (Val == theObjectName)
1735 RefSO = GetSObject(it.Value());
1739 if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
1745 //============================================================================
1746 /*! Function : _FindObjectIOR
1747 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
1749 //============================================================================
1750 SALOMEDSImpl_SObject
1751 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
1752 const std::string& theObjectIOR,
1755 SALOMEDSImpl_SObject RefSO, aSO;
1756 if(!SO) return RefSO;
1758 // Iterate on each objects and subobjects of the component
1759 // If objectName find, stop the loop and get the object reference
1760 SALOMEDSImpl_AttributeIOR* anAttr;
1762 DF_ChildIterator it(SO.GetLabel());
1763 for (; it.More();it.Next()){
1766 if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
1768 std::string Val(anAttr->Value());
1769 if (Val == theObjectIOR)
1771 RefSO = GetSObject(it.Value());
1775 aSO = GetSObject(it.Value());
1776 if (!_find) RefSO = _FindObjectIOR(aSO, theObjectIOR, _find);
1782 //============================================================================
1783 /*! Function : _GetNoteBookAccessor
1784 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
1786 //============================================================================
1787 std::string SALOMEDSImpl_Study::_GetNoteBookAccessor(){
1788 return std::string("notebook");
1791 //============================================================================
1792 /*! Function : _GetStudyVariablesScript
1795 //============================================================================
1796 std::string SALOMEDSImpl_Study::_GetStudyVariablesScript()
1798 std::string dump("");
1800 if(myNoteBookVars.empty())
1803 Kernel_Utils::Localizer loc;
1805 dump += "####################################################\n";
1806 dump += "## Begin of NoteBook variables section ##\n";
1807 dump += "####################################################\n";
1809 std::string set_method = _GetNoteBookAccessor()+".set(";
1810 std::string varName;
1811 std::string varValue;
1812 for(int i = 0 ; i < myNoteBookVars.size();i++ ) {
1813 varName = myNoteBookVars[i]->Name();
1814 varValue = myNoteBookVars[i]->SaveToScript();
1815 dump+=set_method+"\""+varName+"\", "+varValue+")\n";
1818 dump += "####################################################\n";
1819 dump += "## End of NoteBook variables section ##\n";
1820 dump += "####################################################\n";
1825 //============================================================================
1826 /*! Function : _GetNoteBookAccess
1829 //============================================================================
1830 std::string SALOMEDSImpl_Study::_GetNoteBookAccess()
1832 std::string notebook = "import salome_notebook\n";
1833 notebook += _GetNoteBookAccessor() + " = salome_notebook.NoteBook()" ;
1837 bool SALOMEDSImpl_Study::IsLocked()
1840 return GetProperties()->IsLocked();
1843 void SALOMEDSImpl_Study::UpdateIORLabelMap(const std::string& anIOR,const std::string& anEntry)
1846 DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
1847 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
1848 if (it != myIORLabels.end()) myIORLabels.erase(it);
1849 myIORLabels[anIOR] = aLabel;
1852 void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR)
1854 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
1855 if (it != myIORLabels.end())
1857 //remove the ior entry and decref the genericobj (if it's one)
1858 myIORLabels.erase(it);
1862 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudyImpl(const DF_Label& theLabel)
1864 SALOMEDSImpl_StudyHandle* Att;
1865 if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
1871 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
1873 return GetStudyImpl(theLabel)->GetSObject(theLabel);
1876 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
1878 return GetStudyImpl(theLabel)->GetSComponent(theLabel);
1882 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
1884 std::string aString = theAttribute->Label().Entry();
1885 GetStudyImpl(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
1888 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
1891 std::vector<SALOMEDSImpl_SObject> aSeq;
1893 SALOMEDSImpl_AttributeTarget* aTarget;
1894 if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
1895 return aTarget->Get();
1902 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
1905 return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
1908 std::string SALOMEDSImpl_Study::GetLastModificationDate()
1911 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1913 std::vector<std::string> aNames;
1914 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1915 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1917 int aLastIndex = aNames.size()-1;
1919 sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1920 (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
1921 (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
1922 std::string aResStr (aResult);
1926 std::vector<std::string> SALOMEDSImpl_Study::GetModificationsDate()
1929 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1931 std::vector<std::string> aNames;
1932 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1933 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1935 int anIndex, aLength = aNames.size();
1936 std::vector<std::string> aDates;
1938 for (anIndex = 1; anIndex < aLength; anIndex++) {
1940 sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1941 (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
1942 (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
1943 aDates.push_back(aDate);
1950 //============================================================================
1951 /*! Function : GetUseCaseBuilder
1952 * Purpose : Returns a UseCase builder
1954 //============================================================================
1955 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
1958 return _useCaseBuilder;
1961 //============================================================================
1962 /*! Function : GetSComponent
1965 //============================================================================
1966 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const std::string& theEntry)
1968 SALOMEDSImpl_SComponent aSCO;
1969 if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
1970 aSCO = _mapOfSCO[theEntry];
1972 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1973 aSCO = SALOMEDSImpl_SComponent(aLabel);
1974 _mapOfSCO[theEntry] = aSCO;
1980 //============================================================================
1981 /*! Function : GetSComponent
1984 //============================================================================
1985 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
1987 return SALOMEDSImpl_SComponent(theLabel);
1990 //============================================================================
1991 /*! Function : GetSObject
1994 //============================================================================
1995 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const std::string& theEntry)
1997 SALOMEDSImpl_SObject aSO;
1998 std::map<std::string, SALOMEDSImpl_SObject>::iterator it=_mapOfSO.find(theEntry);
1999 if(it != _mapOfSO.end())
2002 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
2003 aSO = SALOMEDSImpl_SObject(aLabel);
2004 _mapOfSO[theEntry] = aSO;
2010 //============================================================================
2011 /*! Function : GetSObject
2014 //============================================================================
2015 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
2017 return SALOMEDSImpl_SObject(theLabel);
2020 //============================================================================
2021 /*! Function : GetAttribute
2024 //============================================================================
2025 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const std::string& theEntry,
2026 const std::string& theType)
2028 SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
2029 DF_Attribute* anAttr;
2030 aSO.FindAttribute(anAttr, theType);
2034 //! number of spaces for indentation in Python dump files (to replace \t symbols)
2035 static const int indent_size = 2;
2037 static std::string replace_tabs( const std::string& in )
2039 std::string out = in;
2041 size_t pos = out.find( '\t' );
2042 while ( pos != std::string::npos ) {
2043 out.replace( pos, 1, indent_size, ' ' );
2044 pos = out.find( '\t' );
2050 static std::string GetComponentHeader(const char* theComponentName)
2052 std::stringstream txt;
2053 txt << "###" << std::endl;
2054 txt << "### " << theComponentName << " component" << std::endl;
2055 txt << "###" << std::endl;
2059 //============================================================================
2060 /*! Function : DumpStudy
2063 //============================================================================
2064 bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
2065 const std::string& theBaseName,
2068 SALOMEDSImpl_DriverFactory* theFactory)
2072 if(theFactory == NULL) {
2073 _errorCode = "Null factory for creation of Engines";
2077 std::vector<std::string> aSeq;
2078 std::string aFactoryType;
2080 //Build a list of all components in the Study
2081 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
2083 for (; itcomponent.More(); itcomponent.Next())
2084 aSeq.push_back(itcomponent.Value().ComponentDataType());
2086 std::vector<std::string>::iterator it;
2087 if ( (it = std::find( aSeq.begin(), aSeq.end(), "GEOM" )) != aSeq.end() ) {
2089 aSeq.insert(aSeq.begin(), "GEOM" );
2091 if ( (it = std::find( aSeq.begin(), aSeq.end(), "SHAPER" )) != aSeq.end() ) {
2093 aSeq.insert(aSeq.begin(), "SHAPER" );
2096 // re-arrange modules in the sequence, if specific order is given via SALOME_MODULES_ORDER environment variable.
2097 if ( getenv("SALOME_MODULES_ORDER") != 0 ) {
2098 std::string order = getenv("SALOME_MODULES_ORDER");
2099 std::vector<std::string> mlist;
2100 while ( !order.empty() ) {
2101 size_t idx = order.find( ":" );
2102 std::string m = order.substr(0, idx);
2103 order = order.substr( ( idx == std::string::npos ) ? order.size() : idx+1 );
2104 if ( m.empty() || std::find( mlist.begin(), mlist.end(), m ) != mlist.end() ) continue;
2105 mlist.push_back( m );
2108 for ( std::vector<std::string>::reverse_iterator mit = mlist.rbegin(); mit != mlist.rend(); ++mit ) {
2109 std::vector<std::string>::iterator it = std::find( aSeq.begin(), aSeq.end(), *mit );
2110 if ( it != aSeq.end() ) {
2112 aSeq.insert( aSeq.begin(), *mit );
2118 std::string aFileName =
2119 thePath + std::string("\\") + theBaseName + std::string(".py");
2121 std::string aFileName =
2122 thePath + std::string("/") + theBaseName + std::string(".py");
2125 //Create a file that will contain a main Study script
2127 #if defined(WIN32) && defined(UNICODE)
2128 std::wstring aConverterFN = Kernel_Utils::utf8_decode_s(aFileName);
2129 fp.open(aConverterFN.c_str(), std::ios::out);
2131 fp.open(aFileName.c_str(), std::ios::out);
2135 bool isOpened = fp.is_open();
2137 bool isOpened = fp.rdbuf()->is_open();
2141 _errorCode = std::string("Can't create a file ")+aFileName;
2145 std::stringstream sfp;
2147 std::string aBatchModeScript = "salome";
2149 //Output to the main Study script required Python modules import,
2150 //set sys.path and add a creation of the study.
2153 sfp << GetDumpStudyComment() << std::endl;
2156 sfp << "import sys" << std::endl;
2157 sfp << "import " << aBatchModeScript << std::endl << std::endl;
2159 // initialization function
2160 sfp << aBatchModeScript << ".salome_init()" << std::endl;
2162 // notebook initialization
2163 sfp << _GetNoteBookAccess() << std::endl;
2165 // extend sys.path with the directory where the script is being dumped to
2166 sfp << "sys.path.insert(0, r\'" << thePath << "\')" << std::endl << std::endl;
2168 // dump NoteBook variables
2169 sfp << _GetStudyVariablesScript();
2171 // dump visual parameters if necessary
2172 bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
2173 int lastSavePoint = -1;
2175 lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
2176 if(lastSavePoint > 0) {
2177 sfp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << std::endl << std::endl;
2181 std::vector<std::string> aSeqOfFileNames;
2183 // dump all components and create the components specific scripts
2185 int aLength = aSeq.size();
2186 for(int i = 1; i <= aLength; i++) {
2188 std::string aCompType = aSeq[i-1];
2189 SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
2190 SALOMEDSImpl_Driver* aDriver = NULL;
2191 // if there is an associated Engine call its method for saving
2192 std::string IOREngine;
2194 if (!sco.ComponentIOR(IOREngine)) {
2195 if (!aCompType.empty()) {
2197 aDriver = theFactory->GetDriverByType(aCompType);
2199 if (aDriver != NULL) {
2200 SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
2201 if(!SB->LoadWith(sco, aDriver)) {
2202 _errorCode = SB->GetErrorCode();
2210 aDriver = theFactory->GetDriverByIOR(IOREngine);
2213 _errorCode = "Can not restore information to dump it";
2217 if(aDriver == NULL) continue;
2220 long aStreamLength = 0;
2221 SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(isPublished, isMultiFile, isValidScript, aStreamLength);
2222 if ( !isValidScript )
2225 std::stringstream sfp2;
2227 //Output the Python script generated by the component in the newly created file.
2229 sfp2 << GetDumpStudyComment( aCompType.c_str() ) << std::endl;
2231 sfp2 << GetComponentHeader( aCompType.c_str() ) << std::endl;
2232 sfp2 << aStream->Data();
2234 if ( isMultiFile ) {
2235 //Create a file that will contain the component specific script
2238 aFileName=thePath+std::string("\\");
2240 aFileName=thePath+std::string("/");
2242 std::string aScriptName;
2243 aScriptName += theBaseName;
2245 aScriptName += aCompType;
2247 aFileName += aScriptName+ std::string(".py");
2248 aSeqOfFileNames.push_back(aFileName);
2250 #if defined(WIN32) && defined(UNICODE)
2251 std::wstring aConverterFN2 = Kernel_Utils::utf8_decode_s(aFileName);
2252 fp2.open(aConverterFN2.c_str(), std::ios::out);
2254 fp2.open(aFileName.c_str(), std::ios::out);
2258 isOpened = fp2.is_open();
2260 isOpened = fp2.rdbuf()->is_open();
2264 _errorCode = std::string("Can't create a file ")+aFileName;
2265 SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
2269 // replace '\t' symbols
2270 fp2 << replace_tabs( sfp2.str() );
2274 //Add to the main script a call to RebuildData of the generated by the component the Python script
2275 sfp << "import " << aScriptName << std::endl;
2276 sfp << aScriptName << ".RebuildData()" << std::endl;
2281 if(aStream) delete aStream;
2285 sfp << "if salome.sg.hasDesktop():" << std::endl;
2286 sfp << "\tsalome.sg.updateObjBrowser()" << std::endl;
2288 if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
2289 sfp << "\tiparameters.getSession().restoreVisualState(1)" << std::endl;
2292 // replace '\t' symbols
2293 fp << replace_tabs( sfp.str() );
2300 //=======================================================================
2301 //function : GetDumpStudyComment
2302 //purpose : return a header comment for a DumpStudy script
2303 //=======================================================================
2305 std::string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
2307 std::stringstream txt;
2308 txt << "#!/usr/bin/env python" << std::endl << std::endl;
2309 txt << "###" << std::endl;
2310 txt << "### This file is generated automatically by SALOME v"
2311 << KERNEL_VERSION_STR
2312 << " with dump python functionality";
2313 if ( theComponentName )
2314 txt << " (" << theComponentName << " component)";
2316 txt << "###" << std::endl;
2320 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2322 const std::string& Tab,
2323 SALOMEDSImpl_Study* theStudy);
2325 //============================================================================
2329 //============================================================================
2330 void SALOMEDSImpl_Study::dump(const std::string& theFileName)
2332 //Create a file that will contain a main Study script
2334 fp.open(theFileName.c_str(), std::ios::out);
2337 bool isOpened = fp.is_open();
2339 bool isOpened = fp.rdbuf()->is_open();
2343 _errorCode = std::string("Can't create a file ")+theFileName;
2344 std::cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << std::endl;
2348 SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
2349 fp << "0:1" << std::endl;
2350 SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
2351 std::string aTab(" ");
2352 for(; Itr.More(); Itr.Next()) {
2353 dumpSO(Itr.Value(), fp, aTab, this);
2360 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2362 const std::string& Tab,
2363 SALOMEDSImpl_Study* theStudy)
2365 std::string aTab(Tab), anID(theSO.GetID());
2366 fp << aTab << anID << std::endl;
2367 std::vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
2368 for(int i = 0; i<attribs.size(); i++) {
2369 SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
2375 std::string aType = anAttr->GetClassType();
2376 fp << Tab << " -- " << aType;
2378 if(aType == std::string("AttributeReal")) {
2379 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
2381 else if(aType == std::string("AttributeInteger")) {
2382 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
2384 else if(aType == std::string("AttributeName")) {
2385 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
2387 else if(aType == std::string("AttributeComment")) {
2388 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
2390 else if(aType == std::string("AttributeReference")) {
2391 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
2396 SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
2397 std::string aNewTab(" ");
2399 for(; Itr.More(); Itr.Next()) {
2400 dumpSO(Itr.Value(), fp, aNewTab, theStudy);
2406 void SALOMEDSImpl_Study::Modify()
2409 _doc->SetModified(true);
2412 //============================================================================
2416 //============================================================================
2417 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
2419 if (theSavePoint < -1) return NULL;
2420 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2421 SALOMEDSImpl_SObject so = FindComponent((char*)theID);
2422 if (!so) so = builder->NewComponent((char*)theID);
2423 SALOMEDSImpl_AttributeParameter* attParam = NULL;
2425 if (theSavePoint == -1) {
2427 DF_Label savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2428 DF_Label prevPointLabel;
2429 while ( !savePointLabel.IsNull() ) {
2431 prevPointLabel = savePointLabel;
2432 savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2434 if ( !prevPointLabel.IsNull() )
2435 so = GetSObject( prevPointLabel );
2437 if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
2438 DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
2439 if ( !savePointLabel.IsNull() )
2440 so = GetSObject( savePointLabel );
2441 else // ... if it does not exist - create a new one
2442 so = builder->NewObjectToTag( so, theSavePoint );
2447 builder->FindAttribute(so, A, "AttributeParameter");
2448 if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
2449 A = builder->FindOrCreateAttribute(so, "AttributeParameter");
2451 attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
2456 //============================================================================
2460 //============================================================================
2461 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID,
2462 const char* theModuleName,
2465 if(theSavePoint < -1) return NULL;
2466 SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
2467 SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
2468 SALOMEDSImpl_AttributeParameter* par = NULL;
2470 SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
2471 std::string moduleName(theModuleName);
2472 for(; it.More(); it.Next()) {
2473 SALOMEDSImpl_SObject so(it.Value());
2474 if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
2475 if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
2476 if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
2480 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2481 SALOMEDSImpl_SObject so = builder->NewObject(main_so);
2482 par = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
2483 par->SetString("AP_MODULE_NAME", moduleName);
2487 //============================================================================
2488 /*! Function : SetStudyLock
2491 //============================================================================
2492 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
2494 _lockers.push_back(theLockerID);
2497 //============================================================================
2498 /*! Function : IsStudyLocked
2501 //============================================================================
2502 bool SALOMEDSImpl_Study::IsStudyLocked()
2504 return (_lockers.size() > 0);
2507 //============================================================================
2508 /*! Function : UnLockStudy
2511 //============================================================================
2512 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
2514 std::vector<std::string>::iterator vsI = _lockers.begin();
2515 int length = _lockers.size();
2516 bool isFound = false;
2517 std::string id(theLockerID);
2518 for(int i = 0; i<length; i++, vsI++) {
2519 if(id == _lockers[i]) {
2524 if(isFound) _lockers.erase(vsI);
2527 //============================================================================
2528 /*! Function : GetLockerID
2531 //============================================================================
2532 std::vector<std::string> SALOMEDSImpl_Study::GetLockerID()
2537 //============================================================================
2538 /*! Function : SetVariable
2541 //============================================================================
2542 void SALOMEDSImpl_Study::SetVariable(const std::string& theVarName,
2543 const double theValue,
2544 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2546 bool modified = false;
2547 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2549 if( aGVar == NULL ) {
2551 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2553 aSVar->setValue(theValue);
2554 myNoteBookVars.push_back(aSVar);
2558 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2559 modified = aSVar->setValue(theValue) || modified;
2560 modified = aSVar->setType(theType) || modified;
2567 //============================================================================
2568 /*! Function : SetStringVariable
2571 //============================================================================
2572 void SALOMEDSImpl_Study::SetStringVariable(const std::string& theVarName,
2573 const std::string& theValue,
2574 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2576 bool modified = false;
2577 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2579 if( aGVar == NULL ) {
2581 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2583 aSVar->setStringValue(theValue);
2584 myNoteBookVars.push_back(aSVar);
2588 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2589 modified = aSVar->setStringValue(theValue) || modified;
2590 modified = aSVar->setType(theType) || modified;
2597 //============================================================================
2598 /*! Function : SetStringVariableAsDouble
2601 //============================================================================
2602 void SALOMEDSImpl_Study::SetStringVariableAsDouble(const std::string& theVarName,
2603 const double theValue,
2604 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2606 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2607 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2608 aSVar->setValue(theValue);
2611 //============================================================================
2612 /*! Function : GetReal
2615 //============================================================================
2616 double SALOMEDSImpl_Study::GetVariableValue(const std::string& theVarName)
2618 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2621 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2622 return aSVar->getValue();
2627 //============================================================================
2628 /*! Function : GetString
2631 //============================================================================
2632 std::string SALOMEDSImpl_Study::GetStringVariableValue(const std::string& theVarName)
2634 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2637 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2638 return aSVar->getStringValue();
2643 //============================================================================
2644 /*! Function : IsTypeOf
2647 //============================================================================
2648 bool SALOMEDSImpl_Study::IsTypeOf(const std::string& theVarName,
2649 SALOMEDSImpl_GenericVariable::
2650 VariableTypes theType) const
2652 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2655 return aGVar->Type() == theType;
2660 //============================================================================
2661 /*! Function : IsVariable
2664 //============================================================================
2665 bool SALOMEDSImpl_Study::IsVariable(const std::string& theVarName) const
2667 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2668 return (aGVar != NULL);
2671 //============================================================================
2672 /*! Function : GetVariableNames
2675 //============================================================================
2676 std::vector<std::string> SALOMEDSImpl_Study::GetVariableNames() const
2678 std::vector<std::string> aResult;
2680 for(int i = 0; i < myNoteBookVars.size(); i++)
2681 aResult.push_back(myNoteBookVars[i]->Name());
2686 //============================================================================
2687 /*! Function : AddVariable
2690 //============================================================================
2691 void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
2693 myNoteBookVars.push_back(theVariable);
2696 //============================================================================
2697 /*! Function : AddVariable
2700 //============================================================================
2701 SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
2703 SALOMEDSImpl_GenericVariable* aResult = NULL;
2704 for(int i = 0; i < myNoteBookVars.size();i++) {
2705 if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
2706 aResult = myNoteBookVars[i];
2713 //============================================================================
2714 /*! Function : RemoveVariable
2717 //============================================================================
2718 bool SALOMEDSImpl_Study::RemoveVariable(const std::string& theVarName)
2720 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2724 std::string aValue = aVariable->SaveToScript();
2725 ReplaceVariableAttribute( theVarName, aValue );
2727 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2728 for( ; it != itEnd; it++ )
2730 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2731 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2733 myNoteBookVars.erase( it );
2742 //============================================================================
2743 /*! Function : RenameVariable
2746 //============================================================================
2747 bool SALOMEDSImpl_Study::RenameVariable(const std::string& theVarName, const std::string& theNewVarName)
2749 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2753 ReplaceVariableAttribute( theVarName, theNewVarName );
2755 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2756 for( ; it != itEnd; it++ )
2758 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2759 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2761 aVariableRef->setName( theNewVarName );
2770 //============================================================================
2771 /*! Function : IsVariableUsed
2774 //============================================================================
2775 bool SALOMEDSImpl_Study::IsVariableUsed(const std::string& theVarName)
2777 return FindVariableAttribute( theVarName );
2780 //============================================================================
2781 /*! Function : FindVariableAttribute
2784 //============================================================================
2785 bool SALOMEDSImpl_Study::FindVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
2786 SALOMEDSImpl_SObject theSObject,
2787 const std::string& theName)
2789 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
2790 for( ; anIter.More(); anIter.Next() )
2791 if( FindVariableAttribute( theStudyBuilder, anIter.Value(), theName ) )
2794 DF_Attribute* anAttr;
2795 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
2797 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
2799 std::string aString = aStringAttr->Value();
2801 std::vector< std::vector<std::string> > aSections = ParseVariables( aString );
2802 for( int i = 0, n = aSections.size(); i < n; i++ )
2804 std::vector<std::string> aVector = aSections[i];
2805 for( int j = 0, m = aVector.size(); j < m; j++ )
2807 std::string aStr = aVector[j];
2808 if( aStr.compare( theName ) == 0 )
2817 //============================================================================
2818 /*! Function : FindVariableAttribute
2821 //============================================================================
2822 bool SALOMEDSImpl_Study::FindVariableAttribute(const std::string& theName)
2824 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
2825 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
2826 for( ; aCompIter.More(); aCompIter.Next() )
2828 SALOMEDSImpl_SObject aComp = aCompIter.Value();
2829 if( FindVariableAttribute( aStudyBuilder, aComp, theName ) )
2835 //============================================================================
2836 /*! Function : ReplaceVariableAttribute
2839 //============================================================================
2840 void SALOMEDSImpl_Study::ReplaceVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
2841 SALOMEDSImpl_SObject theSObject,
2842 const std::string& theSource,
2843 const std::string& theDest)
2845 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
2846 for( ; anIter.More(); anIter.Next() )
2847 ReplaceVariableAttribute( theStudyBuilder, anIter.Value(), theSource, theDest );
2849 DF_Attribute* anAttr;
2850 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
2852 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
2854 bool isChanged = false;
2855 std::string aNewString, aCurrentString = aStringAttr->Value();
2857 std::vector< std::vector<std::string> > aSections = ParseVariables( aCurrentString );
2858 for( int i = 0, n = aSections.size(); i < n; i++ )
2860 std::vector<std::string> aVector = aSections[i];
2861 for( int j = 0, m = aVector.size(); j < m; j++ )
2863 std::string aStr = aVector[j];
2864 if( aStr.compare( theSource ) == 0 )
2870 aNewString.append( aStr );
2872 aNewString.append( ":" );
2875 aNewString.append( "|" );
2879 aStringAttr->SetValue( aNewString );
2884 //============================================================================
2885 /*! Function : ReplaceVariableAttribute
2888 //============================================================================
2889 void SALOMEDSImpl_Study::ReplaceVariableAttribute(const std::string& theSource, const std::string& theDest)
2891 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
2892 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
2893 for( ; aCompIter.More(); aCompIter.Next() )
2895 SALOMEDSImpl_SObject aComp = aCompIter.Value();
2896 ReplaceVariableAttribute( aStudyBuilder, aComp, theSource, theDest );
2900 //============================================================================
2901 /*! Function : ParseVariables
2904 //============================================================================
2905 std::vector< std::vector< std::string > > SALOMEDSImpl_Study::ParseVariables(const std::string& theVariables) const
2907 return SALOMEDSImpl_Tool::splitStringWithEmpty( theVariables, OPERATION_SEPARATOR, VARIABLE_SEPARATOR );
2910 //============================================================================
2911 /*! Function : EnableUseCaseAutoFilling
2914 //============================================================================
2915 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
2917 _errorCode = ""; _autoFill = isEnabled;
2919 _builder->SetOnAddSObject(_cb);
2920 _builder->SetOnRemoveSObject(_cb);
2923 _builder->SetOnAddSObject(NULL);
2924 _builder->SetOnRemoveSObject(NULL);
2928 //============================================================================
2929 /*! Function : GetIORs
2932 //============================================================================
2933 std::vector<std::string> SALOMEDSImpl_Study::GetIORs()
2935 std::vector<std::string> anIORs;
2936 std::map<std::string, DF_Label>::const_iterator MI;
2937 for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
2938 anIORs.push_back(MI->first);
2943 //============================================================================
2944 /*! Function : addSO_Notification
2945 * Purpose : This function tells all the observers that a SO has been added
2947 //============================================================================
2948 bool SALOMEDSImpl_Study::addSO_Notification (const SALOMEDSImpl_SObject& theSObject)
2951 return _notifier->addSO_Notification(theSObject);
2956 //============================================================================
2957 /*! Function : removeSO_Notification
2958 * Purpose : This function tells all the observers that a SO has been removed
2960 //============================================================================
2961 bool SALOMEDSImpl_Study::removeSO_Notification (const SALOMEDSImpl_SObject& theSObject)
2964 return _notifier->removeSO_Notification(theSObject);
2969 //============================================================================
2970 /*! Function : modifySO_Notification
2971 * Purpose : This function tells all the observers that a SO has been modified and
2972 pass the mofification reason
2974 //============================================================================
2975 bool SALOMEDSImpl_Study::modifySO_Notification (const SALOMEDSImpl_SObject& theSObject, int reason)
2978 return _notifier->modifySO_Notification(theSObject, reason);
2983 //============================================================================
2984 /*! Function : setNotifier
2985 * Purpose : register a notifier
2987 //============================================================================
2988 void SALOMEDSImpl_Study::setNotifier(SALOMEDSImpl_AbstractCallback* notifier)
2993 static SALOMEDSImpl_AbstractCallback* & getGenObjRegister( DF_Document* doc )
2995 static std::vector< SALOMEDSImpl_AbstractCallback* > _genObjRegVec;
2996 if ( doc->GetDocumentID() >= (int)_genObjRegVec.size() )
2997 _genObjRegVec.resize( doc->GetDocumentID() + 1, 0 );
2998 return _genObjRegVec[ doc->GetDocumentID() ];
3001 //================================================================================
3003 * \brief Stores theRegister
3005 //================================================================================
3007 void SALOMEDSImpl_Study::setGenObjRegister(SALOMEDSImpl_AbstractCallback* theRegister)
3009 getGenObjRegister( _doc ) = theRegister;
3012 //================================================================================
3014 * \brief Indirectly invokes GenericObj_i::Register()
3016 //================================================================================
3018 void SALOMEDSImpl_Study::RegisterGenObj (const std::string& theIOR, DF_Label label)
3020 if ( SALOMEDSImpl_AbstractCallback* goRegister = getGenObjRegister( label.GetDocument() ))
3021 goRegister->RegisterGenObj( theIOR );
3024 //================================================================================
3026 * \brief Indirectly invokes GenericObj_i::UnRegister()
3028 //================================================================================
3030 void SALOMEDSImpl_Study::UnRegisterGenObj(const std::string& theIOR, DF_Label label)
3032 if ( SALOMEDSImpl_AbstractCallback* goRegister = getGenObjRegister( label.GetDocument() ))
3033 goRegister->UnRegisterGenObj( theIOR );
3036 //#######################################################################################################
3037 //# STATIC PRIVATE FUNCTIONS
3038 //#######################################################################################################
3040 //============================================================================
3041 /*! Function : SaveAttributes
3042 * Purpose : Save attributes for object
3044 //============================================================================
3045 static void SaveAttributes(const SALOMEDSImpl_SObject& aSO, HDFgroup *hdf_group_sobject)
3048 std::vector<DF_Attribute*> attrList = aSO.GetLabel().GetAttributes();
3049 DF_Attribute* anAttr = NULL;
3050 for(int i = 0, len = attrList.size(); i<len; i++) {
3051 anAttr = attrList[i];
3052 //The following attributes are not supposed to be written to the file
3053 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
3054 if(type == std::string("AttributeIOR")) continue; //IOR attribute is not saved
3055 std::string aSaveStr =anAttr->Save();
3056 //cout << "Saving: " << aSO.GetID() << " type: "<< type<<"|" << endl;
3057 size[0] = (hdf_int32) strlen(aSaveStr.c_str()) + 1;
3058 HDFdataset *hdf_dataset = new HDFdataset((char*)type.c_str(), hdf_group_sobject, HDF_STRING,size, 1);
3059 hdf_dataset->CreateOnDisk();
3060 hdf_dataset->WriteOnDisk((char*)aSaveStr.c_str());
3061 hdf_dataset->CloseOnDisk();
3062 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
3066 //===========================================================================
3067 //Function : ReadAttributes
3068 //===========================================================================
3069 static void ReadAttributes(SALOMEDSImpl_Study* theStudy,
3070 const SALOMEDSImpl_SObject& aSO,
3071 HDFdataset* hdf_dataset)
3073 hdf_dataset->OpenOnDisk();
3075 DF_Attribute* anAttr = NULL;
3076 char* current_string = new char[hdf_dataset->GetSize()+1];
3077 hdf_dataset->ReadFromDisk(current_string);
3078 //cout << "Reading attr type = " << hdf_dataset->GetName() << " SO = " << aSO.GetID() << endl;
3079 if (!strcmp(hdf_dataset->GetName(),"COMPONENTDATATYPE")) {
3080 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, "AttributeComment");
3082 else if (!strcmp(hdf_dataset->GetName(),"AttributeReference") ||
3083 !strcmp(hdf_dataset->GetName(),"Reference")) { // Old format maintenance
3084 theStudy->NewBuilder()->Addreference(aSO, theStudy->CreateObjectID(current_string));
3085 delete [] (current_string);
3086 hdf_dataset->CloseOnDisk();
3090 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, hdf_dataset->GetName());
3094 anAttr->Load(current_string);
3097 delete [] (current_string);
3098 hdf_dataset->CloseOnDisk();
3101 //============================================================================
3102 //Function : BuildlTree
3103 //============================================================================
3104 static void BuildTree (SALOMEDSImpl_Study* theStudy, HDFgroup* hdf_current_group)
3106 hdf_current_group->OpenOnDisk();
3107 SALOMEDSImpl_SObject aSO;
3108 char* Entry = hdf_current_group->GetName();
3109 if (strcmp(Entry,"STUDY_STRUCTURE") == 0) {
3110 aSO = theStudy->CreateObjectID("0:1");
3113 aSO = theStudy->CreateObjectID(Entry);
3116 char name[HDF_NAME_MAX_LEN+1];
3117 int nbsons = hdf_current_group->nInternalObjects();
3118 for (int i=0; i<nbsons; i++) {
3119 hdf_current_group->InternalObjectIndentify(i,name);
3120 if (strncmp(name, "INTERNAL_COMPLEX",16) == 0) continue;
3121 hdf_object_type type = hdf_current_group->InternalObjectType(name);
3123 if (type == HDF_DATASET) {
3124 HDFdataset* new_dataset = new HDFdataset(name,hdf_current_group);
3125 ReadAttributes(theStudy,aSO,new_dataset);
3126 new_dataset = 0; // will be deleted by father destructor
3128 else if (type == HDF_GROUP) {
3129 HDFgroup* new_group = new HDFgroup(name,hdf_current_group);
3130 BuildTree (theStudy, new_group);
3131 new_group = 0; // will be deleted by father destructor
3134 hdf_current_group->CloseOnDisk();
3138 //============================================================================
3139 //Function : Translate_IOR_to_persistentID
3140 //============================================================================
3141 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject& so,
3142 SALOMEDSImpl_Driver* engine,
3146 DF_ChildIterator itchild(so.GetLabel());
3147 std::string ior_string, persistent_string, curid;
3149 for (; itchild.More(); itchild.Next()) {
3150 SALOMEDSImpl_SObject current = SALOMEDSImpl_Study::SObject(itchild.Value());
3151 SALOMEDSImpl_AttributeIOR* IOR = NULL;
3152 if ((IOR=(SALOMEDSImpl_AttributeIOR*)current.GetLabel().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
3153 ior_string = IOR->Value();
3155 persistent_string = engine->IORToLocalPersistentID (current, ior_string, isMultiFile, isASCII);
3156 SALOMEDSImpl_AttributePersistentRef::Set(current.GetLabel(), persistent_string);
3158 Translate_IOR_to_persistentID (current, engine, isMultiFile, isASCII);
3162 void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup)
3167 HDFgroup* new_group =0;
3168 HDFdataset* new_dataset =0;
3170 char aVarName[HDF_NAME_MAX_LEN+1];
3171 char *currentVarType = 0;
3172 char *currentVarValue = 0;
3173 char *currentVarIndex = 0;
3175 //Open HDF group with notebook variables
3176 theGroup->OpenOnDisk();
3178 //Get Nb of variables
3179 int aNbVars = theGroup->nInternalObjects();
3181 std::map<int,SALOMEDSImpl_GenericVariable*> aVarsMap;
3183 for( int iVar=0;iVar < aNbVars;iVar++ ) {
3184 theGroup->InternalObjectIndentify(iVar,aVarName);
3185 hdf_object_type type = theGroup->InternalObjectType(aVarName);
3186 if(type == HDF_GROUP) {
3189 new_group = new HDFgroup(aVarName,theGroup);
3190 new_group->OpenOnDisk();
3193 new_dataset = new HDFdataset("VARIABLE_TYPE",new_group);
3194 new_dataset->OpenOnDisk();
3195 currentVarType = new char[new_dataset->GetSize()+1];
3196 new_dataset->ReadFromDisk(currentVarType);
3197 new_dataset->CloseOnDisk();
3198 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3201 if(new_group->ExistInternalObject("VARIABLE_INDEX")) {
3202 new_dataset = new HDFdataset("VARIABLE_INDEX",new_group);
3203 new_dataset->OpenOnDisk();
3204 currentVarIndex = new char[new_dataset->GetSize()+1];
3205 new_dataset->ReadFromDisk(currentVarIndex);
3206 new_dataset->CloseOnDisk();
3207 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3208 order = atoi(currentVarIndex);
3209 delete [] currentVarIndex;
3215 new_dataset = new HDFdataset("VARIABLE_VALUE",new_group);
3216 new_dataset->OpenOnDisk();
3217 currentVarValue = new char[new_dataset->GetSize()+1];
3218 new_dataset->ReadFromDisk(currentVarValue);
3219 new_dataset->CloseOnDisk();
3220 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3222 new_group->CloseOnDisk();
3223 new_group = 0; //will be deleted by hdf_sco_group destructor
3225 SALOMEDSImpl_GenericVariable::VariableTypes aVarType =
3226 SALOMEDSImpl_GenericVariable::String2VariableType(std::string(currentVarType));
3227 delete [] currentVarType;
3229 //Create variable and add it in the study
3230 SALOMEDSImpl_GenericVariable* aVariable =
3231 new SALOMEDSImpl_ScalarVariable(aVarType,std::string(aVarName));
3232 aVariable->Load(std::string(currentVarValue));
3233 aVarsMap.insert(std::make_pair(order,aVariable));
3234 delete [] currentVarValue;
3238 std::map<int,SALOMEDSImpl_GenericVariable*>::const_iterator it= aVarsMap.begin();
3239 for(;it!=aVarsMap.end();it++)
3240 theStudy->AddVariable((*it).second);
3242 theGroup->CloseOnDisk();