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);
806 // Force removing readonly attribute from a file under Windows, because of a but in the HDF
807 std::string aReadOlnyRmCmd = "attrib -r \"" + aStudyTmpDir + std::string(buffer)+ "\" > nul 2>&1";
809 std::wstring awReadOlnyRmCmd = Kernel_Utils::utf8_decode_s(aReadOlnyRmCmd);
810 _wsystem(awReadOlnyRmCmd.c_str());
812 system(aReadOlnyRmCmd.c_str());
815 aCmd = "move /Y \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl) +"\" > nul 2>&1";
817 aCmd = "mv -f \"" + aStudyTmpDir + std::string(buffer) + "\" \"" + SALOMEDSImpl_Tool::GetDirFromPath(aStudyUrl)+"\"";
819 #if defined(WIN32) && defined(UNICODE)
820 std::wstring awCmd = Kernel_Utils::utf8_decode_s(aCmd);
821 errors = _wsystem(awCmd.c_str());
823 errors = system(aCmd.c_str());
833 std::wstring aTmpFileToDelete = Kernel_Utils::utf8_decode_s(aTmpFile);
836 std::string aTmpFileToDelete = aTmpFile;
838 DeleteFile(aTmpFileToDelete.c_str());
840 unlink(aTmpFile.c_str());
845 std::wstring aTmpFileDirToDelete = Kernel_Utils::utf8_decode_s( aTmpFileDir );
846 std::wstring aStudyTmpDirToDelete = Kernel_Utils::utf8_decode_s( aStudyTmpDir );
848 std::string aTmpFileDirToDelete = aTmpFileDir;
849 std::string aStudyTmpDirToDelete = aStudyTmpDir;
851 RemoveDirectory( aTmpFileDirToDelete.c_str() );
852 RemoveDirectory( aStudyTmpDirToDelete.c_str() );
854 rmdir(aTmpFileDir.c_str());
855 rmdir(aStudyTmpDir.c_str());
859 // VSR: finally, if all is done without errors, mark study as Saved
863 std::map<std::string, SALOMEDSImpl_Driver*>::iterator n2dr = aMapTypeDriver.begin();
864 for ( ; n2dr != aMapTypeDriver.end(); ++n2dr )
870 //============================================================================
871 /*! Function : Impl_SaveObject
874 //============================================================================
875 bool SALOMEDSImpl_Study::Impl_SaveObject(const SALOMEDSImpl_SObject& SC,
876 HDFgroup *hdf_group_datatype)
880 // Write in group hdf_group_datatype all information of SObject SC
881 // Iterative function to parse all SObjects under a SComponent
883 HDFgroup *hdf_group_sobject = 0;
885 DF_ChildIterator itchild(SC.GetLabel());
886 for (; itchild.More(); itchild.Next()) {
887 // mpv: don't save empty labels
888 std::vector<DF_Attribute*> attr = itchild.Value().GetAttributes();
889 if (attr.size() == 0) { //No attributes on the label
890 DF_ChildIterator subchild(itchild.Value());
891 if (!subchild.More()) {
894 subchild.Init(itchild.Value(), true);
896 for (; subchild.More() && anEmpty; subchild.Next()) {
897 std::vector<DF_Attribute*> attr2 = subchild.Value().GetAttributes();
899 anEmpty = false; //There are attributes on the child label
903 if (anEmpty) continue;
906 SALOMEDSImpl_SObject SO = SALOMEDSImpl_Study::SObject(itchild.Value());
908 std::string scoid = SO.GetID();
909 hdf_group_sobject = new HDFgroup(scoid.c_str(), hdf_group_datatype);
910 hdf_group_sobject->CreateOnDisk();
911 SaveAttributes(SO, hdf_group_sobject);
912 Impl_SaveObject(SO, hdf_group_sobject);
913 hdf_group_sobject->CloseOnDisk();
914 hdf_group_sobject =0; // will be deleted by father hdf object destructor
919 //============================================================================
920 /*! Function : CanCopy
923 //============================================================================
924 bool SALOMEDSImpl_Study::CanCopy(const SALOMEDSImpl_SObject& theObject,
925 SALOMEDSImpl_Driver* theEngine)
928 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
929 if (!aComponent) return false;
930 if (aComponent.GetLabel() == theObject.GetLabel()) return false;
931 std::string IOREngine;
932 if (!aComponent.ComponentIOR(IOREngine)) return false;
933 if (theEngine == NULL) return false;
934 return theEngine->CanCopy(theObject);
937 //============================================================================
938 /*! Function : CopyLabel
941 //============================================================================
942 bool SALOMEDSImpl_Study::CopyLabel(SALOMEDSImpl_Driver* theEngine,
943 const int theSourceStartDepth,
944 const DF_Label& theSource,
945 const DF_Label& theDestinationMain)
950 DF_Label aTargetLabel = theDestinationMain;
951 DF_Label aAuxTargetLabel = theDestinationMain.Father().FindChild(2);
952 for(a = theSource.Depth() - theSourceStartDepth; a > 0 ; a--) {
953 DF_Label aSourceLabel = theSource;
954 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
955 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
956 aAuxTargetLabel = aAuxTargetLabel.FindChild(aSourceLabel.Tag());
958 // iterate attributes
959 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
960 for(int i = 0, len = attrList.size(); i<len; i++) {
961 DF_Attribute* anAttr = attrList[i];
962 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
963 if (type.substr(0, 17) == std::string("AttributeTreeNode")) continue; // never copy tree node attribute
964 if (type == std::string("AttributeTarget")) continue; // and target attribute
966 if (type == std::string("AttributeReference")) { // reference copied as Comment in aux tree
967 DF_Label aReferenced = dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Get();
968 std::string anEntry = aReferenced.Entry();
969 // store the value of name attribute of referenced label
970 SALOMEDSImpl_AttributeName* aNameAttribute;
971 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aReferenced.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
973 anEntry += aNameAttribute->Value();
975 SALOMEDSImpl_AttributeComment::Set(aAuxTargetLabel, anEntry);
979 if (type == std::string("AttributeIOR")) { // IOR => ID and TMPFile of Engine
980 std::string anEntry = theSource.Entry();
981 SALOMEDSImpl_SObject aSO = FindObjectID(anEntry);
984 SALOMEDSImpl_TMPFile* aStream = theEngine->CopyFrom(aSO, anObjID, aLen);
985 std::string aResStr("");
986 for(a = 0; a < aLen; a++) {
987 aResStr += (char)(aStream->Get(a));
990 if(aStream) delete aStream;
992 SALOMEDSImpl_AttributeInteger::Set(aAuxTargetLabel, anObjID);
993 SALOMEDSImpl_AttributeName::Set(aAuxTargetLabel, aResStr);
996 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
997 aTargetLabel.AddAttribute(aNewAttribute);
998 anAttr->Paste(aNewAttribute);
1004 //============================================================================
1008 //============================================================================
1009 bool SALOMEDSImpl_Study::Copy(const SALOMEDSImpl_SObject& theObject,
1010 SALOMEDSImpl_Driver* theEngine)
1014 // adoptation for alliances datamodel copy: without IOR attributes !!!
1015 bool aStructureOnly; // copy only SObjects and attributes without component help
1016 aStructureOnly = !theObject.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID());
1019 _errorCode = "Document is null";
1023 //Clear the clipboard
1024 _clipboard->Main().Root().ForgetAllAttributes(true);
1025 _appli->Close(_clipboard);
1026 _clipboard = _appli->NewDocument("SALOME_STUDY");
1028 // set component data type to the name attribute of root label
1029 if (!aStructureOnly) {
1030 SALOMEDSImpl_AttributeComment::Set(_clipboard->Main().Root(),
1031 theEngine->ComponentDataType());
1033 // iterate all theObject's label children
1034 DF_Label aStartLabel = theObject.GetLabel();
1035 int aSourceStartDepth = aStartLabel.Depth();
1037 // copy main source label
1038 CopyLabel(theEngine, aSourceStartDepth, aStartLabel, _clipboard->Main());
1040 // copy all subchildren of the main source label (all levels)
1041 DF_ChildIterator anIterator(aStartLabel, true);
1042 for(; anIterator.More(); anIterator.Next()) {
1043 CopyLabel(theEngine, aSourceStartDepth, anIterator.Value(), _clipboard->Main());
1049 //============================================================================
1050 /*! Function : CanPaste
1053 //============================================================================
1054 bool SALOMEDSImpl_Study::CanPaste(const SALOMEDSImpl_SObject& theObject,
1055 SALOMEDSImpl_Driver* theEngine)
1060 _errorCode = "Clipboard is null";
1064 SALOMEDSImpl_AttributeComment* aCompName = NULL;
1065 if (!(aCompName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1066 _errorCode = "Clipboard has no component type";
1069 SALOMEDSImpl_AttributeInteger* anObjID;
1070 if (!(anObjID=(SALOMEDSImpl_AttributeInteger*)_clipboard->Main().Father().FindChild(2).FindAttribute(SALOMEDSImpl_AttributeInteger::GetID()))) {
1071 _errorCode = "Clipboard has no object id";
1074 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1076 _errorCode = "Object doesn't belong to component";
1080 std::string IOREngine;
1081 if (!aComponent.ComponentIOR(IOREngine)) {
1082 _errorCode = "component has no IOR";
1085 return theEngine->CanPaste(aCompName->Value(), anObjID->Value());
1088 //============================================================================
1089 /*! Function : PasteLabel
1092 //============================================================================
1093 DF_Label SALOMEDSImpl_Study::PasteLabel(SALOMEDSImpl_Driver* theEngine,
1094 const DF_Label& theSource,
1095 const DF_Label& theDestinationStart,
1096 const bool isFirstElement)
1100 // get corresponding source, target and auxiliary labels
1101 DF_Label aTargetLabel = theDestinationStart;
1103 DF_Label aAuxSourceLabel = theSource.Root().FindChild(2);
1105 if (!isFirstElement) {
1106 for(a = theSource.Depth() - 1; a > 0 ; a--) {
1107 DF_Label aSourceLabel = theSource;
1108 for(int aNbFather = 1; aNbFather < a; aNbFather++) aSourceLabel = aSourceLabel.Father();
1109 aTargetLabel = aTargetLabel.FindChild(aSourceLabel.Tag());
1110 aAuxSourceLabel = aAuxSourceLabel.FindChild(aSourceLabel.Tag());
1112 SALOMEDSImpl_SObject so = GetSObject(aTargetLabel);
1113 addSO_Notification(so);
1116 // check auxiliary label for TMPFile => IOR
1117 SALOMEDSImpl_AttributeName* aNameAttribute = NULL;
1118 if ((aNameAttribute=(SALOMEDSImpl_AttributeName*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1119 SALOMEDSImpl_AttributeInteger* anObjID = (SALOMEDSImpl_AttributeInteger*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeInteger::GetID());
1120 SALOMEDSImpl_AttributeComment* aComponentName = (SALOMEDSImpl_AttributeComment*)theSource.Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID());
1121 std::string aCompName = aComponentName->Value();
1123 if (theEngine->CanPaste(aCompName, anObjID->Value())) {
1124 std::string aTMPStr = aNameAttribute->Value();
1125 int aLen = aTMPStr.size();
1126 unsigned char* aStream = NULL;
1128 aStream = new unsigned char[aLen+10];
1129 for(a = 0; a < aLen; a++) {
1130 aStream[a] = aTMPStr[a];
1134 std::string anEntry = aTargetLabel.Entry();
1135 SALOMEDSImpl_SObject aPastedSO = FindObjectID(anEntry);
1137 if (isFirstElement) {
1138 std::string aDestEntry = theEngine->PasteInto(aStream,
1141 aPastedSO.GetFatherComponent());
1142 aTargetLabel = DF_Label::Label(theDestinationStart, aDestEntry);
1144 theEngine->PasteInto(aStream, aLen, anObjID->Value(), aPastedSO);
1146 if(aStream != NULL) delete []aStream;
1150 // iterate attributes
1151 std::vector<DF_Attribute*> attrList = theSource.GetAttributes();
1152 for(int i = 0, len = attrList.size(); i<len; i++) {
1153 DF_Attribute* anAttr = attrList[i];
1154 if (aTargetLabel.FindAttribute(anAttr->ID())) {
1155 aTargetLabel.ForgetAttribute(anAttr->ID());
1157 DF_Attribute* aNewAttribute = anAttr->NewEmpty();
1158 aTargetLabel.AddAttribute(aNewAttribute);
1159 anAttr->Paste(aNewAttribute);
1162 // check auxiliary label for Comment => reference or name attribute of the referenced object
1163 SALOMEDSImpl_AttributeComment* aCommentAttribute = NULL;
1164 if ((aCommentAttribute=(SALOMEDSImpl_AttributeComment*)aAuxSourceLabel.FindAttribute(SALOMEDSImpl_AttributeComment::GetID()))) {
1165 char * anEntry = new char[aCommentAttribute->Value().size() + 1];
1166 strcpy(anEntry, std::string(aCommentAttribute->Value()).c_str());
1167 char* aNameStart = strchr(anEntry, ' ');
1172 // copy to the same study, reanimate reference
1173 DF_Label aRefLabel = DF_Label::Label(aTargetLabel, anEntry);
1174 SALOMEDSImpl_AttributeReference::Set(aTargetLabel, aRefLabel);
1175 // target attributes structure support
1176 SALOMEDSImpl_AttributeTarget::Set(aRefLabel)->Add(SALOMEDSImpl_Study::SObject(aTargetLabel));
1181 return aTargetLabel;
1184 //============================================================================
1185 /*! Function : Paste
1188 //============================================================================
1189 SALOMEDSImpl_SObject SALOMEDSImpl_Study::Paste(const SALOMEDSImpl_SObject& theObject,
1190 SALOMEDSImpl_Driver* theEngine)
1194 SALOMEDSImpl_SObject so;
1196 // if study is locked, then paste can't be done
1197 if (GetProperties()->IsLocked()) {
1198 _errorCode = "LockProtection";
1199 throw LockProtection("LockProtection");
1202 // if there is no component name, then paste only SObjects and attributes: without component help
1203 SALOMEDSImpl_AttributeComment* aComponentName = NULL;
1204 bool aStructureOnly = !(aComponentName=(SALOMEDSImpl_AttributeComment*)_clipboard->Main().Root().FindAttribute(SALOMEDSImpl_AttributeComment::GetID()));
1206 // CAF document of current study usage
1208 _errorCode = "Document is null";
1212 SALOMEDSImpl_SComponent aComponent = theObject.GetFatherComponent();
1214 // fill root inserted SObject
1215 DF_Label aStartLabel;
1216 if (aStructureOnly) {
1217 DF_Label anObjectLabel = DF_Label::Label(_doc->Main(), theObject.GetID());
1218 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), anObjectLabel, false);
1220 DF_Label aComponentLabel = DF_Label::Label(_doc->Main(), aComponent.GetID());
1221 aStartLabel = PasteLabel(theEngine, _clipboard->Main(), aComponentLabel, true);
1224 // paste all sublebels
1225 DF_ChildIterator anIterator(_clipboard->Main(), true);
1226 for(; anIterator.More(); anIterator.Next()) {
1227 PasteLabel(theEngine, anIterator.Value(), aStartLabel, false);
1230 return SALOMEDSImpl_Study::SObject(aStartLabel);
1233 //============================================================================
1234 /*! Function : GetPersistentReference
1235 * Purpose : Get persistent reference of study (idem URL())
1237 //============================================================================
1238 std::string SALOMEDSImpl_Study::GetPersistentReference()
1244 //============================================================================
1245 /*! Function : IsEmpty
1246 * Purpose : Detect if study is empty
1248 //============================================================================
1249 bool SALOMEDSImpl_Study::IsEmpty()
1252 if (!_doc) return true;
1253 return _doc->IsEmpty();
1256 //============================================================================
1257 /*! Function : FindComponent
1258 * Purpose : Find a Component with ComponentDataType = aComponentName
1260 //============================================================================
1261 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const std::string& aComponentName)
1266 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1267 SALOMEDSImpl_SComponent compo;
1269 for (; itcomp.More(); itcomp.Next()) {
1270 SALOMEDSImpl_SComponent SC = itcomp.Value();
1271 name = SC.ComponentDataType();
1272 if(aComponentName == name) {
1280 _errorCode = "No component was found";
1286 //============================================================================
1287 /*! Function : FindComponentID
1288 * Purpose : Find a Component from it's ID
1290 //============================================================================
1291 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const std::string& aComponentID)
1295 // Iterate on each components defined in the study
1296 // Get the component ID and compare with aComponentID
1299 SALOMEDSImpl_SComponent compo;
1301 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
1302 for (; itcomp.More(); itcomp.Next()) {
1303 SALOMEDSImpl_SComponent SC = itcomp.Value();
1305 if(aComponentID == ID)
1307 // ComponentID found
1314 _errorCode = "No component was found";
1321 //============================================================================
1322 /*! Function : FindObject
1323 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1325 //============================================================================
1326 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const std::string& anObjectName)
1330 // Iterate to all components defined in the study
1331 // After testing the component name, iterate in all objects defined under
1332 // components (function _FindObject)
1334 SALOMEDSImpl_SObject RefSO;
1336 SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
1337 for (; it.More();it.Next()){
1340 SALOMEDSImpl_SComponent SC = it.Value();
1341 if (SC.GetName() == anObjectName)
1347 if (!_find) RefSO = _FindObject(SC, anObjectName, _find);
1350 if(!RefSO) _errorCode = "No object was found";
1354 //============================================================================
1355 /*! Function : FindObjectID
1356 * Purpose : Find an Object with ID = anObjectID
1358 //============================================================================
1359 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const std::string& anObjectID)
1362 SALOMEDSImpl_SObject so;
1364 // Convert aSO->GetID in DF_Label.
1365 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
1368 _errorCode = "No label was found by ID";
1371 return GetSObject(Lab);
1375 //============================================================================
1376 /*! Function : CreateObjectID
1377 * Purpose : Creates an Object with ID = anObjectID
1379 //============================================================================
1380 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const std::string& anObjectID)
1383 SALOMEDSImpl_SObject so;
1385 // Convert aSO->GetID in DF_Label.
1386 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
1389 _errorCode = "Can not create a label";
1392 return GetSObject(Lab);
1396 //============================================================================
1397 /*! Function : FindObjectByName
1398 * Purpose : Find Objects with SALOMEDSImpl_Name = anObjectName in a Component
1399 * : with ComponentDataType = aComponentName
1401 //============================================================================
1402 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const std::string& anObjectName,
1403 const std::string& aComponentName)
1407 std::vector<SALOMEDSImpl_SObject> listSO;
1409 SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
1411 _errorCode = "Can not find the component";
1415 // Iterate on each object and subobject of the component
1416 // If objectName is found add it to the list of SObjects
1417 std::string childName ;
1419 std::string compoId = compo.GetID();
1420 SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
1421 for ( ; it.More(); it.Next() ) {
1423 SALOMEDSImpl_SObject CSO = it.Value();
1424 if ( CSO.GetName() == anObjectName ) {
1426 listSO.push_back(CSO) ;
1429 /* looks also for eventual children */
1430 bool found = false ;
1431 CSO = _FindObject( CSO, anObjectName, found ) ;
1433 listSO.push_back(CSO) ;
1442 //============================================================================
1443 /*! Function : FindObjectIOR
1444 * Purpose : Find an Object with IOR = anObjectIOR
1446 //============================================================================
1447 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const std::string& anObjectIOR)
1451 SALOMEDSImpl_SObject aResult ;
1453 // searching in the datamap for optimization
1454 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anObjectIOR);
1455 if (it != myIORLabels.end()) {
1456 aResult = GetSObject(it->second);
1457 // 11 oct 2002: forbidden attributes must be checked here
1458 if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
1459 myIORLabels.erase(anObjectIOR);
1460 aResult = SALOMEDSImpl_SObject();
1464 if(!aResult) _errorCode = "No object was found";
1468 //============================================================================
1469 /*! Function : FindObjectByPath
1470 * Purpose : Find an Object by its path = thePath
1472 //============================================================================
1473 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const std::string& thePath)
1477 std::string aPath(thePath), aToken;
1478 SALOMEDSImpl_SObject aSO;
1479 int aLength = aPath.size();
1480 bool isRelative = false;
1482 if(aLength == 0) { //Empty path - return the current context
1486 if(aPath[0] != '/') //Relative path
1489 DF_ChildIterator anIterator;
1491 SALOMEDSImpl_AttributeName* anAttr;
1497 if(aPath.size() == 1 && aPath[0] == '/') { //Root
1498 return GetSObject(_doc->Main());
1500 anIterator.Init(_doc->Main(), false);
1503 std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
1504 for(int i = 0, len = vs.size(); i<len; i++) {
1507 if(aToken.size() == 0) break;
1509 for ( ; anIterator.More(); anIterator.Next() ) {
1510 aLabel = anIterator.Value();
1511 if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
1512 if(anAttr->Value() == aToken) {
1513 if(i == (len-1)) { //The searched label is found (no part of the path is left)
1514 return GetSObject(aLabel);
1517 anIterator.Init(aLabel, false);
1525 if(!aSO) _errorCode = "No object was found";
1529 //============================================================================
1530 /*! Function : GetObjectPath
1533 //============================================================================
1534 std::string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
1538 std::string aPath("");
1540 _errorCode = "Null object";
1544 std::string aName = theObject.GetName();
1545 if(!aName.empty() && aName != "" ) {
1546 std::string aValue("/");
1550 SALOMEDSImpl_SObject aFather = theObject.GetFather();
1552 aName = aFather.GetName();
1553 if(!aName.empty() && aName != "") {
1554 aValue = GetObjectPath(aFather);
1555 aPath = aValue + aPath;
1564 //============================================================================
1565 /*! Function : GetObjectPathByIOR
1568 //============================================================================
1569 std::string SALOMEDSImpl_Study::GetObjectPathByIOR(const std::string& theIOR)
1574 SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
1576 _errorCode = "No SObject was found by IOR";
1580 return GetObjectPath(so);
1583 //============================================================================
1584 /*! Function : NewChildIterator
1585 * Purpose : Create a ChildIterator from an SObject
1587 //============================================================================
1588 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
1591 return SALOMEDSImpl_ChildIterator(aSO);
1595 //============================================================================
1596 /*! Function : NewComponentIterator
1597 * Purpose : Create a SComponentIterator
1599 //============================================================================
1600 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
1603 return SALOMEDSImpl_SComponentIterator(_doc);
1607 //============================================================================
1608 /*! Function : NewBuilder
1609 * Purpose : Create a StudyBuilder
1611 //============================================================================
1612 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
1616 _builder->SetOnAddSObject(_cb);
1617 _builder->SetOnRemoveSObject(_cb);
1623 //============================================================================
1625 * Purpose : get study name
1627 //============================================================================
1628 std::string SALOMEDSImpl_Study::Name()
1631 return Kernel_Utils::GetBaseName( _name, false );
1634 //============================================================================
1636 * Purpose : set study name
1638 //============================================================================
1639 void SALOMEDSImpl_Study::Name(const std::string& name)
1645 //============================================================================
1646 /*! Function : IsSaved
1647 * Purpose : get if study has been saved
1649 //============================================================================
1650 bool SALOMEDSImpl_Study::IsSaved()
1656 //============================================================================
1657 /*! Function : IsSaved
1658 * Purpose : set if study has been saved
1660 //============================================================================
1661 void SALOMEDSImpl_Study::IsSaved(bool save)
1665 if(save) _doc->SetModified(false);
1668 //============================================================================
1669 /*! Function : IsModified
1670 * Purpose : Detect if a Study has been modified since it has been saved
1672 //============================================================================
1673 bool SALOMEDSImpl_Study::IsModified()
1677 // True if is modified
1678 if (_doc && _doc->IsModified()) return true;
1683 //============================================================================
1685 * Purpose : get URL of the study (persistent reference of the study)
1687 //============================================================================
1688 std::string SALOMEDSImpl_Study::URL()
1694 //============================================================================
1696 * Purpose : set URL of the study (persistent reference of the study)
1698 //============================================================================
1699 void SALOMEDSImpl_Study::URL(const std::string& url)
1705 /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
1706 std::string tmp(_URL);
1708 char *aName = (char*)tmp.ToCString();
1709 char *adr = strtok(aName, "/");
1713 adr = strtok(NULL, "/");
1719 //============================================================================
1720 /*! Function : _FindObject
1721 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
1723 //============================================================================
1724 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
1725 const std::string& theObjectName,
1728 SALOMEDSImpl_SObject RefSO;
1729 if(!SO) return RefSO;
1731 // Iterate on each objects and subobjects of the component
1732 // If objectName find, stop the loop and get the object reference
1733 SALOMEDSImpl_AttributeName* anAttr;
1735 std::string soid = SO.GetID();
1736 DF_ChildIterator it(SO.GetLabel());
1737 for (; it.More(); it.Next()){
1740 if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
1742 std::string Val(anAttr->Value());
1743 if (Val == theObjectName)
1745 RefSO = GetSObject(it.Value());
1749 if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
1755 //============================================================================
1756 /*! Function : _FindObjectIOR
1757 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
1759 //============================================================================
1760 SALOMEDSImpl_SObject
1761 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
1762 const std::string& theObjectIOR,
1765 SALOMEDSImpl_SObject RefSO, aSO;
1766 if(!SO) return RefSO;
1768 // Iterate on each objects and subobjects of the component
1769 // If objectName find, stop the loop and get the object reference
1770 SALOMEDSImpl_AttributeIOR* anAttr;
1772 DF_ChildIterator it(SO.GetLabel());
1773 for (; it.More();it.Next()){
1776 if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
1778 std::string Val(anAttr->Value());
1779 if (Val == theObjectIOR)
1781 RefSO = GetSObject(it.Value());
1785 aSO = GetSObject(it.Value());
1786 if (!_find) RefSO = _FindObjectIOR(aSO, theObjectIOR, _find);
1792 //============================================================================
1793 /*! Function : _GetNoteBookAccessor
1794 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
1796 //============================================================================
1797 std::string SALOMEDSImpl_Study::_GetNoteBookAccessor(){
1798 return std::string("notebook");
1801 //============================================================================
1802 /*! Function : _GetStudyVariablesScript
1805 //============================================================================
1806 std::string SALOMEDSImpl_Study::_GetStudyVariablesScript()
1808 std::string dump("");
1810 if(myNoteBookVars.empty())
1813 Kernel_Utils::Localizer loc;
1815 dump += "####################################################\n";
1816 dump += "## Begin of NoteBook variables section ##\n";
1817 dump += "####################################################\n";
1819 std::string set_method = _GetNoteBookAccessor()+".set(";
1820 std::string varName;
1821 std::string varValue;
1822 for(int i = 0 ; i < myNoteBookVars.size();i++ ) {
1823 varName = myNoteBookVars[i]->Name();
1824 varValue = myNoteBookVars[i]->SaveToScript();
1825 dump+=set_method+"\""+varName+"\", "+varValue+")\n";
1828 dump += "####################################################\n";
1829 dump += "## End of NoteBook variables section ##\n";
1830 dump += "####################################################\n";
1835 //============================================================================
1836 /*! Function : _GetNoteBookAccess
1839 //============================================================================
1840 std::string SALOMEDSImpl_Study::_GetNoteBookAccess()
1842 std::string notebook = "import salome_notebook\n";
1843 notebook += _GetNoteBookAccessor() + " = salome_notebook.NoteBook()" ;
1847 bool SALOMEDSImpl_Study::IsLocked()
1850 return GetProperties()->IsLocked();
1853 void SALOMEDSImpl_Study::UpdateIORLabelMap(const std::string& anIOR,const std::string& anEntry)
1856 DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
1857 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
1858 if (it != myIORLabels.end()) myIORLabels.erase(it);
1859 myIORLabels[anIOR] = aLabel;
1862 void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR)
1864 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
1865 if (it != myIORLabels.end())
1867 //remove the ior entry and decref the genericobj (if it's one)
1868 myIORLabels.erase(it);
1872 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudyImpl(const DF_Label& theLabel)
1874 SALOMEDSImpl_StudyHandle* Att;
1875 if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
1881 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
1883 return GetStudyImpl(theLabel)->GetSObject(theLabel);
1886 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
1888 return GetStudyImpl(theLabel)->GetSComponent(theLabel);
1892 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
1894 std::string aString = theAttribute->Label().Entry();
1895 GetStudyImpl(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
1898 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
1901 std::vector<SALOMEDSImpl_SObject> aSeq;
1903 SALOMEDSImpl_AttributeTarget* aTarget;
1904 if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
1905 return aTarget->Get();
1912 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
1915 return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
1918 std::string SALOMEDSImpl_Study::GetLastModificationDate()
1921 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1923 std::vector<std::string> aNames;
1924 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1925 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1927 int aLastIndex = aNames.size()-1;
1929 sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1930 (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
1931 (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
1932 std::string aResStr (aResult);
1936 std::vector<std::string> SALOMEDSImpl_Study::GetModificationsDate()
1939 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1941 std::vector<std::string> aNames;
1942 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1943 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1945 int anIndex, aLength = aNames.size();
1946 std::vector<std::string> aDates;
1948 for (anIndex = 1; anIndex < aLength; anIndex++) {
1950 sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1951 (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
1952 (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
1953 aDates.push_back(aDate);
1960 //============================================================================
1961 /*! Function : GetUseCaseBuilder
1962 * Purpose : Returns a UseCase builder
1964 //============================================================================
1965 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
1968 return _useCaseBuilder;
1971 //============================================================================
1972 /*! Function : GetSComponent
1975 //============================================================================
1976 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const std::string& theEntry)
1978 SALOMEDSImpl_SComponent aSCO;
1979 if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
1980 aSCO = _mapOfSCO[theEntry];
1982 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1983 aSCO = SALOMEDSImpl_SComponent(aLabel);
1984 _mapOfSCO[theEntry] = aSCO;
1990 //============================================================================
1991 /*! Function : GetSComponent
1994 //============================================================================
1995 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
1997 return SALOMEDSImpl_SComponent(theLabel);
2000 //============================================================================
2001 /*! Function : GetSObject
2004 //============================================================================
2005 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const std::string& theEntry)
2007 SALOMEDSImpl_SObject aSO;
2008 std::map<std::string, SALOMEDSImpl_SObject>::iterator it=_mapOfSO.find(theEntry);
2009 if(it != _mapOfSO.end())
2012 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
2013 aSO = SALOMEDSImpl_SObject(aLabel);
2014 _mapOfSO[theEntry] = aSO;
2020 //============================================================================
2021 /*! Function : GetSObject
2024 //============================================================================
2025 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
2027 return SALOMEDSImpl_SObject(theLabel);
2030 //============================================================================
2031 /*! Function : GetAttribute
2034 //============================================================================
2035 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const std::string& theEntry,
2036 const std::string& theType)
2038 SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
2039 DF_Attribute* anAttr;
2040 aSO.FindAttribute(anAttr, theType);
2044 //! number of spaces for indentation in Python dump files (to replace \t symbols)
2045 static const int indent_size = 2;
2047 static std::string replace_tabs( const std::string& in )
2049 std::string out = in;
2051 size_t pos = out.find( '\t' );
2052 while ( pos != std::string::npos ) {
2053 out.replace( pos, 1, indent_size, ' ' );
2054 pos = out.find( '\t' );
2060 static std::string GetComponentHeader(const char* theComponentName)
2062 std::stringstream txt;
2063 txt << "###" << std::endl;
2064 txt << "### " << theComponentName << " component" << std::endl;
2065 txt << "###" << std::endl;
2069 //============================================================================
2070 /*! Function : DumpStudy
2073 //============================================================================
2074 bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
2075 const std::string& theBaseName,
2078 SALOMEDSImpl_DriverFactory* theFactory)
2082 if(theFactory == NULL) {
2083 _errorCode = "Null factory for creation of Engines";
2087 std::vector<std::string> aSeq;
2088 std::string aFactoryType;
2090 //Build a list of all components in the Study
2091 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
2093 for (; itcomponent.More(); itcomponent.Next())
2094 aSeq.push_back(itcomponent.Value().ComponentDataType());
2096 std::vector<std::string>::iterator it;
2097 if ( (it = std::find( aSeq.begin(), aSeq.end(), "GEOM" )) != aSeq.end() ) {
2099 aSeq.insert(aSeq.begin(), "GEOM" );
2101 if ( (it = std::find( aSeq.begin(), aSeq.end(), "SHAPER" )) != aSeq.end() ) {
2103 aSeq.insert(aSeq.begin(), "SHAPER" );
2106 // re-arrange modules in the sequence, if specific order is given via SALOME_MODULES_ORDER environment variable.
2107 if ( getenv("SALOME_MODULES_ORDER") != 0 ) {
2108 std::string order = getenv("SALOME_MODULES_ORDER");
2109 std::vector<std::string> mlist;
2110 while ( !order.empty() ) {
2111 size_t idx = order.find( ":" );
2112 std::string m = order.substr(0, idx);
2113 order = order.substr( ( idx == std::string::npos ) ? order.size() : idx+1 );
2114 if ( m.empty() || std::find( mlist.begin(), mlist.end(), m ) != mlist.end() ) continue;
2115 mlist.push_back( m );
2118 for ( std::vector<std::string>::reverse_iterator mit = mlist.rbegin(); mit != mlist.rend(); ++mit ) {
2119 std::vector<std::string>::iterator it = std::find( aSeq.begin(), aSeq.end(), *mit );
2120 if ( it != aSeq.end() ) {
2122 aSeq.insert( aSeq.begin(), *mit );
2128 std::string aFileName =
2129 thePath + std::string("\\") + theBaseName + std::string(".py");
2131 std::string aFileName =
2132 thePath + std::string("/") + theBaseName + std::string(".py");
2135 //Create a file that will contain a main Study script
2137 #if defined(WIN32) && defined(UNICODE)
2138 std::wstring aConverterFN = Kernel_Utils::utf8_decode_s(aFileName);
2139 fp.open(aConverterFN.c_str(), std::ios::out);
2141 fp.open(aFileName.c_str(), std::ios::out);
2145 bool isOpened = fp.is_open();
2147 bool isOpened = fp.rdbuf()->is_open();
2151 _errorCode = std::string("Can't create a file ")+aFileName;
2154 _dumpPath = thePath;
2156 std::stringstream sfp;
2158 std::string aBatchModeScript = "salome";
2160 //Output to the main Study script required Python modules import,
2161 //set sys.path and add a creation of the study.
2164 sfp << GetDumpStudyComment() << std::endl;
2167 sfp << "import sys" << std::endl;
2168 sfp << "import " << aBatchModeScript << std::endl << std::endl;
2170 // initialization function
2171 sfp << aBatchModeScript << ".salome_init()" << std::endl;
2173 // notebook initialization
2174 sfp << _GetNoteBookAccess() << std::endl;
2176 // extend sys.path with the directory where the script is being dumped to
2177 sfp << "sys.path.insert(0, r\'" << thePath << "\')" << std::endl << std::endl;
2179 // dump NoteBook variables
2180 sfp << _GetStudyVariablesScript();
2182 // dump visual parameters if necessary
2183 bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
2184 int lastSavePoint = -1;
2186 lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
2187 if(lastSavePoint > 0) {
2188 sfp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << std::endl << std::endl;
2192 std::vector<std::string> aSeqOfFileNames;
2194 // dump all components and create the components specific scripts
2196 int aLength = aSeq.size();
2197 for(int i = 1; i <= aLength; i++) {
2199 std::string aCompType = aSeq[i-1];
2200 SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
2201 SALOMEDSImpl_Driver* aDriver = NULL;
2202 // if there is an associated Engine call its method for saving
2203 std::string IOREngine;
2205 if (!sco.ComponentIOR(IOREngine)) {
2206 if (!aCompType.empty()) {
2208 aDriver = theFactory->GetDriverByType(aCompType);
2210 if (aDriver != NULL) {
2211 SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
2212 if(!SB->LoadWith(sco, aDriver)) {
2213 _errorCode = SB->GetErrorCode();
2221 aDriver = theFactory->GetDriverByIOR(IOREngine);
2224 _errorCode = "Can not restore information to dump it";
2228 if(aDriver == NULL) continue;
2231 long aStreamLength = 0;
2232 SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(isPublished, isMultiFile, isValidScript, aStreamLength);
2233 if ( !isValidScript )
2236 std::stringstream sfp2;
2238 //Output the Python script generated by the component in the newly created file.
2240 sfp2 << GetDumpStudyComment( aCompType.c_str() ) << std::endl;
2242 sfp2 << GetComponentHeader( aCompType.c_str() ) << std::endl;
2243 sfp2 << aStream->Data();
2245 if ( isMultiFile ) {
2246 //Create a file that will contain the component specific script
2249 aFileName=thePath+std::string("\\");
2251 aFileName=thePath+std::string("/");
2253 std::string aScriptName;
2254 aScriptName += theBaseName;
2256 aScriptName += aCompType;
2258 aFileName += aScriptName+ std::string(".py");
2259 aSeqOfFileNames.push_back(aFileName);
2261 #if defined(WIN32) && defined(UNICODE)
2262 std::wstring aConverterFN2 = Kernel_Utils::utf8_decode_s(aFileName);
2263 fp2.open(aConverterFN2.c_str(), std::ios::out);
2265 fp2.open(aFileName.c_str(), std::ios::out);
2269 isOpened = fp2.is_open();
2271 isOpened = fp2.rdbuf()->is_open();
2275 _errorCode = std::string("Can't create a file ")+aFileName;
2276 SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
2280 // replace '\t' symbols
2281 fp2 << replace_tabs( sfp2.str() );
2285 //Add to the main script a call to RebuildData of the generated by the component the Python script
2286 sfp << "import " << aScriptName << std::endl;
2287 sfp << aScriptName << ".RebuildData()" << std::endl;
2292 if(aStream) delete aStream;
2296 sfp << "if salome.sg.hasDesktop():" << std::endl;
2297 sfp << "\tsalome.sg.updateObjBrowser()" << std::endl;
2299 if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
2300 sfp << "\tiparameters.getSession().restoreVisualState(1)" << std::endl;
2303 // replace '\t' symbols
2304 fp << replace_tabs( sfp.str() );
2313 // Returns the folder of the python script which is currently dumped
2314 std::string SALOMEDSImpl_Study::GetDumpPath()
2320 //=======================================================================
2321 //function : GetDumpStudyComment
2322 //purpose : return a header comment for a DumpStudy script
2323 //=======================================================================
2325 std::string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
2327 std::stringstream txt;
2328 txt << "#!/usr/bin/env python" << std::endl << std::endl;
2329 txt << "###" << std::endl;
2330 txt << "### This file is generated automatically by SALOME v"
2331 << KERNEL_VERSION_STR
2332 << " with dump python functionality";
2333 if ( theComponentName )
2334 txt << " (" << theComponentName << " component)";
2336 txt << "###" << std::endl;
2340 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2342 const std::string& Tab,
2343 SALOMEDSImpl_Study* theStudy);
2345 //============================================================================
2349 //============================================================================
2350 void SALOMEDSImpl_Study::dump(const std::string& theFileName)
2352 //Create a file that will contain a main Study script
2354 fp.open(theFileName.c_str(), std::ios::out);
2357 bool isOpened = fp.is_open();
2359 bool isOpened = fp.rdbuf()->is_open();
2363 _errorCode = std::string("Can't create a file ")+theFileName;
2364 std::cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << std::endl;
2368 SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
2369 fp << "0:1" << std::endl;
2370 SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
2371 std::string aTab(" ");
2372 for(; Itr.More(); Itr.Next()) {
2373 dumpSO(Itr.Value(), fp, aTab, this);
2380 void dumpSO(const SALOMEDSImpl_SObject& theSO,
2382 const std::string& Tab,
2383 SALOMEDSImpl_Study* theStudy)
2385 std::string aTab(Tab), anID(theSO.GetID());
2386 fp << aTab << anID << std::endl;
2387 std::vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
2388 for(int i = 0; i<attribs.size(); i++) {
2389 SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
2395 std::string aType = anAttr->GetClassType();
2396 fp << Tab << " -- " << aType;
2398 if(aType == std::string("AttributeReal")) {
2399 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
2401 else if(aType == std::string("AttributeInteger")) {
2402 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
2404 else if(aType == std::string("AttributeName")) {
2405 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
2407 else if(aType == std::string("AttributeComment")) {
2408 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
2410 else if(aType == std::string("AttributeReference")) {
2411 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
2416 SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
2417 std::string aNewTab(" ");
2419 for(; Itr.More(); Itr.Next()) {
2420 dumpSO(Itr.Value(), fp, aNewTab, theStudy);
2426 void SALOMEDSImpl_Study::Modify()
2429 _doc->SetModified(true);
2432 //============================================================================
2436 //============================================================================
2437 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
2439 if (theSavePoint < -1) return NULL;
2440 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2441 SALOMEDSImpl_SObject so = FindComponent((char*)theID);
2442 if (!so) so = builder->NewComponent((char*)theID);
2443 SALOMEDSImpl_AttributeParameter* attParam = NULL;
2445 if (theSavePoint == -1) {
2447 DF_Label savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2448 DF_Label prevPointLabel;
2449 while ( !savePointLabel.IsNull() ) {
2451 prevPointLabel = savePointLabel;
2452 savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
2454 if ( !prevPointLabel.IsNull() )
2455 so = GetSObject( prevPointLabel );
2457 if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
2458 DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
2459 if ( !savePointLabel.IsNull() )
2460 so = GetSObject( savePointLabel );
2461 else // ... if it does not exist - create a new one
2462 so = builder->NewObjectToTag( so, theSavePoint );
2467 builder->FindAttribute(so, A, "AttributeParameter");
2468 if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
2469 A = builder->FindOrCreateAttribute(so, "AttributeParameter");
2471 attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
2476 //============================================================================
2480 //============================================================================
2481 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID,
2482 const char* theModuleName,
2485 if(theSavePoint < -1) return NULL;
2486 SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
2487 SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
2488 SALOMEDSImpl_AttributeParameter* par = NULL;
2490 SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
2491 std::string moduleName(theModuleName);
2492 for(; it.More(); it.Next()) {
2493 SALOMEDSImpl_SObject so(it.Value());
2494 if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
2495 if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
2496 if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
2500 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
2501 SALOMEDSImpl_SObject so = builder->NewObject(main_so);
2502 par = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
2503 par->SetString("AP_MODULE_NAME", moduleName);
2507 //============================================================================
2508 /*! Function : SetStudyLock
2511 //============================================================================
2512 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
2514 _lockers.push_back(theLockerID);
2517 //============================================================================
2518 /*! Function : IsStudyLocked
2521 //============================================================================
2522 bool SALOMEDSImpl_Study::IsStudyLocked()
2524 return (_lockers.size() > 0);
2527 //============================================================================
2528 /*! Function : UnLockStudy
2531 //============================================================================
2532 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
2534 std::vector<std::string>::iterator vsI = _lockers.begin();
2535 int length = _lockers.size();
2536 bool isFound = false;
2537 std::string id(theLockerID);
2538 for(int i = 0; i<length; i++, vsI++) {
2539 if(id == _lockers[i]) {
2544 if(isFound) _lockers.erase(vsI);
2547 //============================================================================
2548 /*! Function : GetLockerID
2551 //============================================================================
2552 std::vector<std::string> SALOMEDSImpl_Study::GetLockerID()
2557 //============================================================================
2558 /*! Function : SetVariable
2561 //============================================================================
2562 void SALOMEDSImpl_Study::SetVariable(const std::string& theVarName,
2563 const double theValue,
2564 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2566 bool modified = false;
2567 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2569 if( aGVar == NULL ) {
2571 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2573 aSVar->setValue(theValue);
2574 myNoteBookVars.push_back(aSVar);
2578 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2579 modified = aSVar->setValue(theValue) || modified;
2580 modified = aSVar->setType(theType) || modified;
2587 //============================================================================
2588 /*! Function : SetStringVariable
2591 //============================================================================
2592 void SALOMEDSImpl_Study::SetStringVariable(const std::string& theVarName,
2593 const std::string& theValue,
2594 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2596 bool modified = false;
2597 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2599 if( aGVar == NULL ) {
2601 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
2603 aSVar->setStringValue(theValue);
2604 myNoteBookVars.push_back(aSVar);
2608 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
2609 modified = aSVar->setStringValue(theValue) || modified;
2610 modified = aSVar->setType(theType) || modified;
2617 //============================================================================
2618 /*! Function : SetStringVariableAsDouble
2621 //============================================================================
2622 void SALOMEDSImpl_Study::SetStringVariableAsDouble(const std::string& theVarName,
2623 const double theValue,
2624 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
2626 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2627 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2628 aSVar->setValue(theValue);
2631 //============================================================================
2632 /*! Function : GetReal
2635 //============================================================================
2636 double SALOMEDSImpl_Study::GetVariableValue(const std::string& theVarName)
2638 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2641 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2642 return aSVar->getValue();
2647 //============================================================================
2648 /*! Function : GetString
2651 //============================================================================
2652 std::string SALOMEDSImpl_Study::GetStringVariableValue(const std::string& theVarName)
2654 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2657 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
2658 return aSVar->getStringValue();
2663 //============================================================================
2664 /*! Function : IsTypeOf
2667 //============================================================================
2668 bool SALOMEDSImpl_Study::IsTypeOf(const std::string& theVarName,
2669 SALOMEDSImpl_GenericVariable::
2670 VariableTypes theType) const
2672 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2675 return aGVar->Type() == theType;
2680 //============================================================================
2681 /*! Function : IsVariable
2684 //============================================================================
2685 bool SALOMEDSImpl_Study::IsVariable(const std::string& theVarName) const
2687 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
2688 return (aGVar != NULL);
2691 //============================================================================
2692 /*! Function : GetVariableNames
2695 //============================================================================
2696 std::vector<std::string> SALOMEDSImpl_Study::GetVariableNames() const
2698 std::vector<std::string> aResult;
2700 for(int i = 0; i < myNoteBookVars.size(); i++)
2701 aResult.push_back(myNoteBookVars[i]->Name());
2706 //============================================================================
2707 /*! Function : AddVariable
2710 //============================================================================
2711 void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
2713 myNoteBookVars.push_back(theVariable);
2716 //============================================================================
2717 /*! Function : AddVariable
2720 //============================================================================
2721 SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
2723 SALOMEDSImpl_GenericVariable* aResult = NULL;
2724 for(int i = 0; i < myNoteBookVars.size();i++) {
2725 if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
2726 aResult = myNoteBookVars[i];
2733 //============================================================================
2734 /*! Function : RemoveVariable
2737 //============================================================================
2738 bool SALOMEDSImpl_Study::RemoveVariable(const std::string& theVarName)
2740 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2744 std::string aValue = aVariable->SaveToScript();
2745 ReplaceVariableAttribute( theVarName, aValue );
2747 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2748 for( ; it != itEnd; it++ )
2750 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2751 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2753 myNoteBookVars.erase( it );
2762 //============================================================================
2763 /*! Function : RenameVariable
2766 //============================================================================
2767 bool SALOMEDSImpl_Study::RenameVariable(const std::string& theVarName, const std::string& theNewVarName)
2769 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
2773 ReplaceVariableAttribute( theVarName, theNewVarName );
2775 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
2776 for( ; it != itEnd; it++ )
2778 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
2779 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
2781 aVariableRef->setName( theNewVarName );
2790 //============================================================================
2791 /*! Function : IsVariableUsed
2794 //============================================================================
2795 bool SALOMEDSImpl_Study::IsVariableUsed(const std::string& theVarName)
2797 return FindVariableAttribute( theVarName );
2800 //============================================================================
2801 /*! Function : FindVariableAttribute
2804 //============================================================================
2805 bool SALOMEDSImpl_Study::FindVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
2806 SALOMEDSImpl_SObject theSObject,
2807 const std::string& theName)
2809 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
2810 for( ; anIter.More(); anIter.Next() )
2811 if( FindVariableAttribute( theStudyBuilder, anIter.Value(), theName ) )
2814 DF_Attribute* anAttr;
2815 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
2817 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
2819 std::string aString = aStringAttr->Value();
2821 std::vector< std::vector<std::string> > aSections = ParseVariables( aString );
2822 for( int i = 0, n = aSections.size(); i < n; i++ )
2824 std::vector<std::string> aVector = aSections[i];
2825 for( int j = 0, m = aVector.size(); j < m; j++ )
2827 std::string aStr = aVector[j];
2828 if( aStr.compare( theName ) == 0 )
2837 //============================================================================
2838 /*! Function : FindVariableAttribute
2841 //============================================================================
2842 bool SALOMEDSImpl_Study::FindVariableAttribute(const std::string& theName)
2844 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
2845 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
2846 for( ; aCompIter.More(); aCompIter.Next() )
2848 SALOMEDSImpl_SObject aComp = aCompIter.Value();
2849 if( FindVariableAttribute( aStudyBuilder, aComp, theName ) )
2855 //============================================================================
2856 /*! Function : ReplaceVariableAttribute
2859 //============================================================================
2860 void SALOMEDSImpl_Study::ReplaceVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
2861 SALOMEDSImpl_SObject theSObject,
2862 const std::string& theSource,
2863 const std::string& theDest)
2865 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
2866 for( ; anIter.More(); anIter.Next() )
2867 ReplaceVariableAttribute( theStudyBuilder, anIter.Value(), theSource, theDest );
2869 DF_Attribute* anAttr;
2870 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
2872 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
2874 bool isChanged = false;
2875 std::string aNewString, aCurrentString = aStringAttr->Value();
2877 std::vector< std::vector<std::string> > aSections = ParseVariables( aCurrentString );
2878 for( int i = 0, n = aSections.size(); i < n; i++ )
2880 std::vector<std::string> aVector = aSections[i];
2881 for( int j = 0, m = aVector.size(); j < m; j++ )
2883 std::string aStr = aVector[j];
2884 if( aStr.compare( theSource ) == 0 )
2890 aNewString.append( aStr );
2892 aNewString.append( ":" );
2895 aNewString.append( "|" );
2899 aStringAttr->SetValue( aNewString );
2904 //============================================================================
2905 /*! Function : ReplaceVariableAttribute
2908 //============================================================================
2909 void SALOMEDSImpl_Study::ReplaceVariableAttribute(const std::string& theSource, const std::string& theDest)
2911 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
2912 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
2913 for( ; aCompIter.More(); aCompIter.Next() )
2915 SALOMEDSImpl_SObject aComp = aCompIter.Value();
2916 ReplaceVariableAttribute( aStudyBuilder, aComp, theSource, theDest );
2920 //============================================================================
2921 /*! Function : ParseVariables
2924 //============================================================================
2925 std::vector< std::vector< std::string > > SALOMEDSImpl_Study::ParseVariables(const std::string& theVariables) const
2927 return SALOMEDSImpl_Tool::splitStringWithEmpty( theVariables, OPERATION_SEPARATOR, VARIABLE_SEPARATOR );
2930 //============================================================================
2931 /*! Function : EnableUseCaseAutoFilling
2934 //============================================================================
2935 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
2937 _errorCode = ""; _autoFill = isEnabled;
2939 _builder->SetOnAddSObject(_cb);
2940 _builder->SetOnRemoveSObject(_cb);
2943 _builder->SetOnAddSObject(NULL);
2944 _builder->SetOnRemoveSObject(NULL);
2948 //============================================================================
2949 /*! Function : GetIORs
2952 //============================================================================
2953 std::vector<std::string> SALOMEDSImpl_Study::GetIORs()
2955 std::vector<std::string> anIORs;
2956 std::map<std::string, DF_Label>::const_iterator MI;
2957 for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
2958 anIORs.push_back(MI->first);
2963 //============================================================================
2964 /*! Function : addSO_Notification
2965 * Purpose : This function tells all the observers that a SO has been added
2967 //============================================================================
2968 bool SALOMEDSImpl_Study::addSO_Notification (const SALOMEDSImpl_SObject& theSObject)
2971 return _notifier->addSO_Notification(theSObject);
2976 //============================================================================
2977 /*! Function : removeSO_Notification
2978 * Purpose : This function tells all the observers that a SO has been removed
2980 //============================================================================
2981 bool SALOMEDSImpl_Study::removeSO_Notification (const SALOMEDSImpl_SObject& theSObject)
2984 return _notifier->removeSO_Notification(theSObject);
2989 //============================================================================
2990 /*! Function : modifySO_Notification
2991 * Purpose : This function tells all the observers that a SO has been modified and
2992 pass the mofification reason
2994 //============================================================================
2995 bool SALOMEDSImpl_Study::modifySO_Notification (const SALOMEDSImpl_SObject& theSObject, int reason)
2998 return _notifier->modifySO_Notification(theSObject, reason);
3003 //============================================================================
3004 /*! Function : setNotifier
3005 * Purpose : register a notifier
3007 //============================================================================
3008 void SALOMEDSImpl_Study::setNotifier(SALOMEDSImpl_AbstractCallback* notifier)
3013 static SALOMEDSImpl_AbstractCallback* & getGenObjRegister( DF_Document* doc )
3015 static std::vector< SALOMEDSImpl_AbstractCallback* > _genObjRegVec;
3016 if ( doc->GetDocumentID() >= (int)_genObjRegVec.size() )
3017 _genObjRegVec.resize( doc->GetDocumentID() + 1, 0 );
3018 return _genObjRegVec[ doc->GetDocumentID() ];
3021 //================================================================================
3023 * \brief Stores theRegister
3025 //================================================================================
3027 void SALOMEDSImpl_Study::setGenObjRegister(SALOMEDSImpl_AbstractCallback* theRegister)
3029 getGenObjRegister( _doc ) = theRegister;
3032 //================================================================================
3034 * \brief Indirectly invokes GenericObj_i::Register()
3036 //================================================================================
3038 void SALOMEDSImpl_Study::RegisterGenObj (const std::string& theIOR, DF_Label label)
3040 if ( SALOMEDSImpl_AbstractCallback* goRegister = getGenObjRegister( label.GetDocument() ))
3041 goRegister->RegisterGenObj( theIOR );
3044 //================================================================================
3046 * \brief Indirectly invokes GenericObj_i::UnRegister()
3048 //================================================================================
3050 void SALOMEDSImpl_Study::UnRegisterGenObj(const std::string& theIOR, DF_Label label)
3052 if ( SALOMEDSImpl_AbstractCallback* goRegister = getGenObjRegister( label.GetDocument() ))
3053 goRegister->UnRegisterGenObj( theIOR );
3056 //#######################################################################################################
3057 //# STATIC PRIVATE FUNCTIONS
3058 //#######################################################################################################
3060 //============================================================================
3061 /*! Function : SaveAttributes
3062 * Purpose : Save attributes for object
3064 //============================================================================
3065 static void SaveAttributes(const SALOMEDSImpl_SObject& aSO, HDFgroup *hdf_group_sobject)
3068 std::vector<DF_Attribute*> attrList = aSO.GetLabel().GetAttributes();
3069 DF_Attribute* anAttr = NULL;
3070 for(int i = 0, len = attrList.size(); i<len; i++) {
3071 anAttr = attrList[i];
3072 //The following attributes are not supposed to be written to the file
3073 std::string type = SALOMEDSImpl_GenericAttribute::Impl_GetType(anAttr);
3074 if(type == std::string("AttributeIOR")) continue; //IOR attribute is not saved
3075 std::string aSaveStr =anAttr->Save();
3076 //cout << "Saving: " << aSO.GetID() << " type: "<< type<<"|" << endl;
3077 size[0] = (hdf_int32) strlen(aSaveStr.c_str()) + 1;
3078 HDFdataset *hdf_dataset = new HDFdataset((char*)type.c_str(), hdf_group_sobject, HDF_STRING,size, 1);
3079 hdf_dataset->CreateOnDisk();
3080 hdf_dataset->WriteOnDisk((char*)aSaveStr.c_str());
3081 hdf_dataset->CloseOnDisk();
3082 hdf_dataset=0; //will be deleted by hdf_sco_group destructor
3086 //===========================================================================
3087 //Function : ReadAttributes
3088 //===========================================================================
3089 static void ReadAttributes(SALOMEDSImpl_Study* theStudy,
3090 const SALOMEDSImpl_SObject& aSO,
3091 HDFdataset* hdf_dataset)
3093 hdf_dataset->OpenOnDisk();
3095 DF_Attribute* anAttr = NULL;
3096 char* current_string = new char[hdf_dataset->GetSize()+1];
3097 hdf_dataset->ReadFromDisk(current_string);
3098 //cout << "Reading attr type = " << hdf_dataset->GetName() << " SO = " << aSO.GetID() << endl;
3099 if (!strcmp(hdf_dataset->GetName(),"COMPONENTDATATYPE")) {
3100 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, "AttributeComment");
3102 else if (!strcmp(hdf_dataset->GetName(),"AttributeReference") ||
3103 !strcmp(hdf_dataset->GetName(),"Reference")) { // Old format maintenance
3104 theStudy->NewBuilder()->Addreference(aSO, theStudy->CreateObjectID(current_string));
3105 delete [] (current_string);
3106 hdf_dataset->CloseOnDisk();
3110 anAttr = theStudy->NewBuilder()->FindOrCreateAttribute(aSO, hdf_dataset->GetName());
3114 anAttr->Load(current_string);
3117 delete [] (current_string);
3118 hdf_dataset->CloseOnDisk();
3121 //============================================================================
3122 //Function : BuildlTree
3123 //============================================================================
3124 static void BuildTree (SALOMEDSImpl_Study* theStudy, HDFgroup* hdf_current_group)
3126 hdf_current_group->OpenOnDisk();
3127 SALOMEDSImpl_SObject aSO;
3128 char* Entry = hdf_current_group->GetName();
3129 if (strcmp(Entry,"STUDY_STRUCTURE") == 0) {
3130 aSO = theStudy->CreateObjectID("0:1");
3133 aSO = theStudy->CreateObjectID(Entry);
3136 char name[HDF_NAME_MAX_LEN+1];
3137 int nbsons = hdf_current_group->nInternalObjects();
3138 for (int i=0; i<nbsons; i++) {
3139 hdf_current_group->InternalObjectIndentify(i,name);
3140 if (strncmp(name, "INTERNAL_COMPLEX",16) == 0) continue;
3141 hdf_object_type type = hdf_current_group->InternalObjectType(name);
3143 if (type == HDF_DATASET) {
3144 HDFdataset* new_dataset = new HDFdataset(name,hdf_current_group);
3145 ReadAttributes(theStudy,aSO,new_dataset);
3146 new_dataset = 0; // will be deleted by father destructor
3148 else if (type == HDF_GROUP) {
3149 HDFgroup* new_group = new HDFgroup(name,hdf_current_group);
3150 BuildTree (theStudy, new_group);
3151 new_group = 0; // will be deleted by father destructor
3154 hdf_current_group->CloseOnDisk();
3158 //============================================================================
3159 //Function : Translate_IOR_to_persistentID
3160 //============================================================================
3161 static void Translate_IOR_to_persistentID (const SALOMEDSImpl_SObject& so,
3162 SALOMEDSImpl_Driver* engine,
3166 DF_ChildIterator itchild(so.GetLabel());
3167 std::string ior_string, persistent_string, curid;
3169 for (; itchild.More(); itchild.Next()) {
3170 SALOMEDSImpl_SObject current = SALOMEDSImpl_Study::SObject(itchild.Value());
3171 SALOMEDSImpl_AttributeIOR* IOR = NULL;
3172 if ((IOR=(SALOMEDSImpl_AttributeIOR*)current.GetLabel().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
3173 ior_string = IOR->Value();
3175 persistent_string = engine->IORToLocalPersistentID (current, ior_string, isMultiFile, isASCII);
3176 SALOMEDSImpl_AttributePersistentRef::Set(current.GetLabel(), persistent_string);
3178 Translate_IOR_to_persistentID (current, engine, isMultiFile, isASCII);
3182 void ReadNoteBookVariables(SALOMEDSImpl_Study* theStudy, HDFgroup* theGroup)
3187 HDFgroup* new_group =0;
3188 HDFdataset* new_dataset =0;
3190 char aVarName[HDF_NAME_MAX_LEN+1];
3191 char *currentVarType = 0;
3192 char *currentVarValue = 0;
3193 char *currentVarIndex = 0;
3195 //Open HDF group with notebook variables
3196 theGroup->OpenOnDisk();
3198 //Get Nb of variables
3199 int aNbVars = theGroup->nInternalObjects();
3201 std::map<int,SALOMEDSImpl_GenericVariable*> aVarsMap;
3203 for( int iVar=0;iVar < aNbVars;iVar++ ) {
3204 theGroup->InternalObjectIndentify(iVar,aVarName);
3205 hdf_object_type type = theGroup->InternalObjectType(aVarName);
3206 if(type == HDF_GROUP) {
3209 new_group = new HDFgroup(aVarName,theGroup);
3210 new_group->OpenOnDisk();
3213 new_dataset = new HDFdataset("VARIABLE_TYPE",new_group);
3214 new_dataset->OpenOnDisk();
3215 currentVarType = new char[new_dataset->GetSize()+1];
3216 new_dataset->ReadFromDisk(currentVarType);
3217 new_dataset->CloseOnDisk();
3218 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3221 if(new_group->ExistInternalObject("VARIABLE_INDEX")) {
3222 new_dataset = new HDFdataset("VARIABLE_INDEX",new_group);
3223 new_dataset->OpenOnDisk();
3224 currentVarIndex = new char[new_dataset->GetSize()+1];
3225 new_dataset->ReadFromDisk(currentVarIndex);
3226 new_dataset->CloseOnDisk();
3227 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3228 order = atoi(currentVarIndex);
3229 delete [] currentVarIndex;
3235 new_dataset = new HDFdataset("VARIABLE_VALUE",new_group);
3236 new_dataset->OpenOnDisk();
3237 currentVarValue = new char[new_dataset->GetSize()+1];
3238 new_dataset->ReadFromDisk(currentVarValue);
3239 new_dataset->CloseOnDisk();
3240 new_dataset = 0; //will be deleted by hdf_sco_group destructor
3242 new_group->CloseOnDisk();
3243 new_group = 0; //will be deleted by hdf_sco_group destructor
3245 SALOMEDSImpl_GenericVariable::VariableTypes aVarType =
3246 SALOMEDSImpl_GenericVariable::String2VariableType(std::string(currentVarType));
3247 delete [] currentVarType;
3249 //Create variable and add it in the study
3250 SALOMEDSImpl_GenericVariable* aVariable =
3251 new SALOMEDSImpl_ScalarVariable(aVarType,std::string(aVarName));
3252 aVariable->Load(std::string(currentVarValue));
3253 aVarsMap.insert(std::make_pair(order,aVariable));
3254 delete [] currentVarValue;
3258 std::map<int,SALOMEDSImpl_GenericVariable*>::const_iterator it= aVarsMap.begin();
3259 for(;it!=aVarsMap.end();it++)
3260 theStudy->AddVariable((*it).second);
3262 theGroup->CloseOnDisk();