1 // Copyright (C) 2007-2013 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.
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>
33 #include "DF_Application.hxx"
34 #include "DF_ChildIterator.hxx"
36 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
37 #include "SALOMEDSImpl_Attributes.hxx"
38 #include "SALOMEDSImpl_UseCaseIterator.hxx"
39 #include "SALOMEDSImpl_AttributeReference.hxx"
40 #include "SALOMEDSImpl_StudyHandle.hxx"
41 #include "SALOMEDSImpl_Tool.hxx"
42 #include "SALOMEDSImpl_IParameters.hxx"
43 #include "SALOMEDSImpl_ScalarVariable.hxx"
48 // comment out the following define to enable \t symbols in in the python dump files
51 #define DIRECTORYID 16661
52 #define FILELOCALID 26662
53 #define FILEID "FILE: "
54 #define VARIABLE_SEPARATOR ':'
55 #define OPERATION_SEPARATOR '|'
57 //============================================================================
58 /*! Function : SALOMEDSImpl_Study
59 * Purpose : SALOMEDSImpl_Study constructor
61 //============================================================================
62 SALOMEDSImpl_Study::SALOMEDSImpl_Study(const DF_Document* doc,
63 const std::string& study_name)
66 _doc = (DF_Document*)doc;
72 _useCaseBuilder = new SALOMEDSImpl_UseCaseBuilder(_doc);
73 _builder = new SALOMEDSImpl_StudyBuilder(this);
74 _cb = new SALOMEDSImpl_Callback(_useCaseBuilder);
77 //Put on the root label a StudyHandle attribute to store the address of this object
78 //It will be used to retrieve the study object by DF_Label that belongs to the study
79 SALOMEDSImpl_StudyHandle::Set(_doc->Main().Root(), this);
83 //============================================================================
84 /*! Function : ~SALOMEDSImpl_Study
85 * Purpose : SALOMEDSImpl_Study destructor
87 //============================================================================
88 SALOMEDSImpl_Study::~SALOMEDSImpl_Study()
92 delete _useCaseBuilder;
95 //============================================================================
96 /*! Function : GetPersistentReference
97 * Purpose : Get persistent reference of study (idem URL())
99 //============================================================================
100 std::string SALOMEDSImpl_Study::GetPersistentReference()
105 //============================================================================
106 /*! Function : GetTransientReference
107 * Purpose : Get IOR of the Study (registred in Document in doc->Root)
109 //============================================================================
110 std::string SALOMEDSImpl_Study::GetTransientReference()
113 std::string IOR = "";
115 SALOMEDSImpl_AttributeIOR* Att;
116 DF_Label _lab = _doc->Root();
117 if ((Att=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
121 _errorCode = "IOR is empty";
127 void SALOMEDSImpl_Study::SetTransientReference(const std::string& theIOR)
131 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
132 int aLocked = aProp->IsLocked();
133 if (aLocked) aProp->SetLocked(false);
135 // Assign the value of the IOR in the study->root
136 SALOMEDSImpl_AttributeIOR::Set(_doc->Main().Root(), theIOR);
138 if (aLocked) aProp->SetLocked(true);
141 //============================================================================
142 /*! Function : IsEmpty
143 * Purpose : Detect if study is empty
145 //============================================================================
146 bool SALOMEDSImpl_Study::IsEmpty()
149 if (!_doc) return true;
150 return _doc->IsEmpty();
153 //============================================================================
154 /*! Function : FindComponent
155 * Purpose : Find a Component with ComponentDataType = aComponentName
157 //============================================================================
158 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const std::string& aComponentName)
163 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
164 SALOMEDSImpl_SComponent compo;
166 for (; itcomp.More(); itcomp.Next()) {
167 SALOMEDSImpl_SComponent SC = itcomp.Value();
168 name = SC.ComponentDataType();
169 if(aComponentName == name) {
177 _errorCode = "No component was found";
183 //============================================================================
184 /*! Function : FindComponentID
185 * Purpose : Find a Component from it's ID
187 //============================================================================
188 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const std::string& aComponentID)
192 // Iterate on each components defined in the study
193 // Get the component ID and compare with aComponentID
196 SALOMEDSImpl_SComponent compo;
198 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
199 for (; itcomp.More(); itcomp.Next()) {
200 SALOMEDSImpl_SComponent SC = itcomp.Value();
202 if(aComponentID == ID)
211 _errorCode = "No component was found";
218 //============================================================================
219 /*! Function : FindObject
220 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
222 //============================================================================
223 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const std::string& anObjectName)
227 // Iterate to all components defined in the study
228 // After testing the component name, iterate in all objects defined under
229 // components (function _FindObject)
231 SALOMEDSImpl_SObject RefSO;
233 SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
234 for (; it.More();it.Next()){
237 SALOMEDSImpl_SComponent SC = it.Value();
238 if (SC.GetName() == anObjectName)
244 if (!_find) RefSO = _FindObject(SC, anObjectName, _find);
247 if(!RefSO) _errorCode = "No object was found";
251 //============================================================================
252 /*! Function : FindObjectID
253 * Purpose : Find an Object with ID = anObjectID
255 //============================================================================
256 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const std::string& anObjectID)
259 SALOMEDSImpl_SObject so;
261 // Convert aSO->GetID in DF_Label.
262 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
265 _errorCode = "No label was found by ID";
268 return GetSObject(Lab);
272 //============================================================================
273 /*! Function : CreateObjectID
274 * Purpose : Creates an Object with ID = anObjectID
276 //============================================================================
277 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const std::string& anObjectID)
280 SALOMEDSImpl_SObject so;
282 // Convert aSO->GetID in DF_Label.
283 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
286 _errorCode = "Can not create a label";
289 return GetSObject(Lab);
293 //============================================================================
294 /*! Function : FindObjectByName
295 * Purpose : Find Objects with SALOMEDSImpl_Name = anObjectName in a Component
296 * : with ComponentDataType = aComponentName
298 //============================================================================
299 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const std::string& anObjectName,
300 const std::string& aComponentName)
304 std::vector<SALOMEDSImpl_SObject> listSO;
306 SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
308 _errorCode = "Can not find the component";
312 // Iterate on each object and subobject of the component
313 // If objectName is found add it to the list of SObjects
314 std::string childName ;
316 std::string compoId = compo.GetID();
317 SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
318 for ( ; it.More(); it.Next() ) {
320 SALOMEDSImpl_SObject CSO = it.Value();
321 if ( CSO.GetName() == anObjectName ) {
323 listSO.push_back(CSO) ;
326 /* looks also for eventual children */
328 CSO = _FindObject( CSO, anObjectName, found ) ;
330 listSO.push_back(CSO) ;
339 //============================================================================
340 /*! Function : FindObjectIOR
341 * Purpose : Find an Object with IOR = anObjectIOR
343 //============================================================================
344 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const std::string& anObjectIOR)
348 SALOMEDSImpl_SObject aResult ;
350 // searching in the datamap for optimization
351 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anObjectIOR);
352 if (it != myIORLabels.end()) {
353 aResult = GetSObject(it->second);
354 // 11 oct 2002: forbidden attributes must be checked here
355 if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
356 myIORLabels.erase(anObjectIOR);
357 aResult = SALOMEDSImpl_SObject();
361 if(!aResult) _errorCode = "No object was found";
365 //============================================================================
366 /*! Function : FindObjectByPath
367 * Purpose : Find an Object by its path = thePath
369 //============================================================================
370 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const std::string& thePath)
374 std::string aPath(thePath), aToken;
375 SALOMEDSImpl_SObject aSO;
376 int aLength = aPath.size();
377 bool isRelative = false;
379 if(aLength == 0) { //Empty path - return the current context
380 return GetSObject(_current);
383 if(aPath[0] != '/') //Relative path
386 DF_ChildIterator anIterator;
388 SALOMEDSImpl_AttributeName* anAttr;
391 if(_current.IsNull()) return aSO;
392 anIterator.Init(_current, false);
395 if(aPath.size() == 1 && aPath[0] == '/') { //Root
396 return GetSObject(_doc->Main());
398 anIterator.Init(_doc->Main(), false);
401 std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
402 for(int i = 0, len = vs.size(); i<len; i++) {
405 if(aToken.size() == 0) break;
407 for ( ; anIterator.More(); anIterator.Next() ) {
408 aLabel = anIterator.Value();
409 if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
410 if(anAttr->Value() == aToken) {
411 if(i == (len-1)) { //The searched label is found (no part of the path is left)
412 return GetSObject(aLabel);
415 anIterator.Init(aLabel, false);
423 if(!aSO) _errorCode = "No object was found";
427 //============================================================================
428 /*! Function : GetObjectPath
431 //============================================================================
432 std::string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
436 std::string aPath("");
438 _errorCode = "Null object";
442 std::string aName = theObject.GetName();
443 if(!aName.empty() && aName != "" ) {
444 std::string aValue("/");
448 SALOMEDSImpl_SObject aFather = theObject.GetFather();
450 aName = aFather.GetName();
451 if(!aName.empty() && aName != "") {
452 aValue = GetObjectPath(aFather);
453 aPath = aValue + aPath;
462 //============================================================================
463 /*! Function : GetObjectPathByIOR
466 //============================================================================
467 std::string SALOMEDSImpl_Study::GetObjectPathByIOR(const std::string& theIOR)
472 SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
474 _errorCode = "No SObject was found by IOR";
478 return GetObjectPath(so);
482 //============================================================================
483 /*! Function : SetContext
484 * Purpose : Sets the current context
486 //============================================================================
487 bool SALOMEDSImpl_Study::SetContext(const std::string& thePath)
490 if(thePath.empty()) {
491 _errorCode = "InvalidPath";
495 std::string aPath(thePath), aContext("");
496 bool isInvalid = false;
497 SALOMEDSImpl_SObject aSO;
499 if(aPath[0] != '/') { //Relative path
500 aContext = GetContext();
508 aSO = FindObjectByPath(aContext);
514 if(isInvalid || !aSO) {
515 _errorCode = "InvalidContext";
519 DF_Label aLabel = aSO.GetLabel();
520 if(aLabel.IsNull()) {
521 _errorCode = "InvalidContext";
525 _current = aLabel; //Set the current context
530 //============================================================================
531 /*! Function : GetContext
532 * Purpose : Gets the current context
534 //============================================================================
535 std::string SALOMEDSImpl_Study::GetContext()
539 if(_current.IsNull()) {
540 _errorCode = "InvaidContext";
543 SALOMEDSImpl_SObject so = GetSObject(_current);
544 return GetObjectPath(so);
547 //============================================================================
548 /*! Function : GetObjectNames
549 * Purpose : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
551 //============================================================================
552 std::vector<std::string> SALOMEDSImpl_Study::GetObjectNames(const std::string& theContext)
556 std::vector<std::string> aResultSeq;
558 if (theContext.empty()) {
561 DF_Label aTmp = _current;
562 SetContext(theContext);
566 if (aLabel.IsNull()) {
567 _errorCode = "InvalidContext";
571 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
572 for (; anIter.More(); anIter.Next()) {
573 DF_Label aLabel = anIter.Value();
574 SALOMEDSImpl_AttributeName* aName;
575 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
576 aResultSeq.push_back(aName->Value());
582 //============================================================================
583 /*! Function : GetDirectoryNames
584 * Purpose : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
586 //============================================================================
587 std::vector<std::string> SALOMEDSImpl_Study::GetDirectoryNames(const std::string& theContext)
591 std::vector<std::string> aResultSeq;
593 if (theContext.empty()) {
596 DF_Label aTmp = _current;
597 SetContext(theContext);
601 if (aLabel.IsNull()) {
602 _errorCode = "InvalidContext";
606 DF_ChildIterator anIter (aLabel, true); // iterate first-level children at all sublevels
607 for (; anIter.More(); anIter.Next()) {
608 DF_Label aLabel = anIter.Value();
609 SALOMEDSImpl_AttributeLocalID* anID;
610 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
611 if (anID->Value() == DIRECTORYID) {
612 SALOMEDSImpl_AttributeName* aName;
613 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
614 aResultSeq.push_back(aName->Value());
623 //============================================================================
624 /*! Function : GetFileNames
625 * Purpose : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
627 //============================================================================
628 std::vector<std::string> SALOMEDSImpl_Study::GetFileNames(const std::string& theContext)
632 std::vector<std::string> aResultSeq;
634 if (theContext.empty()) {
637 DF_Label aTmp = _current;
638 SetContext(theContext);
642 if (aLabel.IsNull()) {
643 _errorCode = "InvalidContext";
647 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
648 for (; anIter.More(); anIter.Next()) {
649 DF_Label aLabel = anIter.Value();
650 SALOMEDSImpl_AttributeLocalID* anID;
651 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
652 if (anID->Value() == FILELOCALID) {
653 SALOMEDSImpl_AttributePersistentRef* aName;
654 if ((aName=(SALOMEDSImpl_AttributePersistentRef*)aLabel.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
655 std::string aFileName = aName->Value();
656 if (aFileName.size() > 0)
657 aResultSeq.push_back(aFileName.substr(strlen(FILEID), aFileName.size()));
666 //============================================================================
667 /*! Function : GetComponentNames
668 * Purpose : method to get all components names
670 //============================================================================
671 std::vector<std::string> SALOMEDSImpl_Study::GetComponentNames(const std::string& theContext)
675 std::vector<std::string> aResultSeq;
676 DF_ChildIterator anIter(_doc->Main(), false); // iterate all subchildren at first level
677 for(; anIter.More(); anIter.Next()) {
678 DF_Label aLabel = anIter.Value();
679 SALOMEDSImpl_AttributeName* aName;
680 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
681 aResultSeq.push_back(aName->Value());
687 //============================================================================
688 /*! Function : NewChildIterator
689 * Purpose : Create a ChildIterator from an SObject
691 //============================================================================
692 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
695 return SALOMEDSImpl_ChildIterator(aSO);
699 //============================================================================
700 /*! Function : NewComponentIterator
701 * Purpose : Create a SComponentIterator
703 //============================================================================
704 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
707 return SALOMEDSImpl_SComponentIterator(_doc);
711 //============================================================================
712 /*! Function : NewBuilder
713 * Purpose : Create a StudyBuilder
715 //============================================================================
716 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
720 _builder->SetOnAddSObject(_cb);
721 _builder->SetOnRemoveSObject(_cb);
727 //============================================================================
729 * Purpose : get study name
731 //============================================================================
732 std::string SALOMEDSImpl_Study::Name()
738 //============================================================================
740 * Purpose : set study name
742 //============================================================================
743 void SALOMEDSImpl_Study::Name(const std::string& name)
749 //============================================================================
750 /*! Function : IsSaved
751 * Purpose : get if study has been saved
753 //============================================================================
754 bool SALOMEDSImpl_Study::IsSaved()
760 //============================================================================
761 /*! Function : IsSaved
762 * Purpose : set if study has been saved
764 //============================================================================
765 void SALOMEDSImpl_Study::IsSaved(bool save)
769 if(save) _doc->SetModified(false);
772 //============================================================================
773 /*! Function : IsModified
774 * Purpose : Detect if a Study has been modified since it has been saved
776 //============================================================================
777 bool SALOMEDSImpl_Study::IsModified()
781 // True if is modified
782 if (_doc && _doc->IsModified()) return true;
787 //============================================================================
789 * Purpose : get URL of the study (persistent reference of the study)
791 //============================================================================
792 std::string SALOMEDSImpl_Study::URL()
798 //============================================================================
800 * Purpose : set URL of the study (persistent reference of the study)
802 //============================================================================
803 void SALOMEDSImpl_Study::URL(const std::string& url)
808 /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
809 std::string tmp(_URL);
811 char *aName = (char*)tmp.ToCString();
812 char *adr = strtok(aName, "/");
816 adr = strtok(NULL, "/");
823 //============================================================================
824 /*! Function : _FindObject
825 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
827 //============================================================================
828 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
829 const std::string& theObjectName,
832 SALOMEDSImpl_SObject RefSO;
833 if(!SO) return RefSO;
835 // Iterate on each objects and subobjects of the component
836 // If objectName find, stop the loop and get the object reference
837 SALOMEDSImpl_AttributeName* anAttr;
839 std::string soid = SO.GetID();
840 DF_ChildIterator it(SO.GetLabel());
841 for (; it.More(); it.Next()){
844 if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
846 std::string Val(anAttr->Value());
847 if (Val == theObjectName)
849 RefSO = GetSObject(it.Value());
853 if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
859 //============================================================================
860 /*! Function : _FindObjectIOR
861 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
863 //============================================================================
865 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
866 const std::string& theObjectIOR,
869 SALOMEDSImpl_SObject RefSO, aSO;
870 if(!SO) return RefSO;
872 // Iterate on each objects and subobjects of the component
873 // If objectName find, stop the loop and get the object reference
874 SALOMEDSImpl_AttributeIOR* anAttr;
876 DF_ChildIterator it(SO.GetLabel());
877 for (; it.More();it.Next()){
880 if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
882 std::string Val(anAttr->Value());
883 if (Val == theObjectIOR)
885 RefSO = GetSObject(it.Value());
889 aSO = GetSObject(it.Value());
890 if (!_find) RefSO = _FindObjectIOR(aSO, theObjectIOR, _find);
896 //============================================================================
897 /*! Function : _GetNoteBookAccessor
898 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
900 //============================================================================
901 std::string SALOMEDSImpl_Study::_GetNoteBookAccessor(){
902 return std::string("notebook");
905 //============================================================================
906 /*! Function : _GetStudyVariablesScript
909 //============================================================================
910 std::string SALOMEDSImpl_Study::_GetStudyVariablesScript()
912 std::string dump("");
914 if(myNoteBookVars.empty())
917 Kernel_Utils::Localizer loc;
919 dump += "####################################################\n";
920 dump += "## Begin of NoteBook variables section ##\n";
921 dump += "####################################################\n";
923 std::string set_method = _GetNoteBookAccessor()+".set(";
925 std::string varValue;
926 for(int i = 0 ; i < myNoteBookVars.size();i++ ) {
927 varName = myNoteBookVars[i]->Name();
928 varValue = myNoteBookVars[i]->SaveToScript();
929 dump+=set_method+"\""+varName+"\", "+varValue+")\n";
932 dump += "####################################################\n";
933 dump += "## End of NoteBook variables section ##\n";
934 dump += "####################################################\n";
939 //============================================================================
940 /*! Function : _GetNoteBookAccess
943 //============================================================================
944 std::string SALOMEDSImpl_Study::_GetNoteBookAccess()
946 std::string accessor = _GetNoteBookAccessor();
947 std::string notebook = "import salome_notebook\n";
948 notebook += accessor+" = salome_notebook."+accessor + "\n";
952 bool SALOMEDSImpl_Study::IsLocked()
955 return GetProperties()->IsLocked();
958 int SALOMEDSImpl_Study::StudyId()
964 void SALOMEDSImpl_Study::StudyId(int id)
970 void SALOMEDSImpl_Study::UpdateIORLabelMap(const std::string& anIOR,const std::string& anEntry)
973 DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
974 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
975 if (it != myIORLabels.end()) myIORLabels.erase(it);
976 myIORLabels[anIOR] = aLabel;
979 void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR)
981 std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
982 if (it != myIORLabels.end())
984 //remove the ior entry and decref the genericobj (if it's one)
985 myIORLabels.erase(it);
989 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel)
991 SALOMEDSImpl_StudyHandle* Att;
992 if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
998 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
1000 return GetStudy(theLabel)->GetSObject(theLabel);
1003 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
1005 return GetStudy(theLabel)->GetSComponent(theLabel);
1009 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
1011 std::string aString = theAttribute->Label().Entry();
1012 GetStudy(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
1015 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
1018 std::vector<SALOMEDSImpl_SObject> aSeq;
1020 SALOMEDSImpl_AttributeTarget* aTarget;
1021 if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
1022 return aTarget->Get();
1029 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
1032 return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
1035 std::string SALOMEDSImpl_Study::GetLastModificationDate()
1038 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1040 std::vector<std::string> aNames;
1041 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1042 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1044 int aLastIndex = aNames.size()-1;
1046 sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1047 (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
1048 (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
1049 std::string aResStr (aResult);
1053 std::vector<std::string> SALOMEDSImpl_Study::GetModificationsDate()
1056 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1058 std::vector<std::string> aNames;
1059 std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1060 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1062 int anIndex, aLength = aNames.size();
1063 std::vector<std::string> aDates;
1065 for (anIndex = 1; anIndex < aLength; anIndex++) {
1067 sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1068 (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
1069 (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
1070 aDates.push_back(aDate);
1077 //============================================================================
1078 /*! Function : GetUseCaseBuilder
1079 * Purpose : Returns a UseCase builder
1081 //============================================================================
1082 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
1085 return _useCaseBuilder;
1089 //============================================================================
1090 /*! Function : Close
1093 //============================================================================
1094 void SALOMEDSImpl_Study::Close()
1098 _doc->GetApplication()->Close(_doc);
1105 //============================================================================
1106 /*! Function : GetSComponent
1109 //============================================================================
1110 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const std::string& theEntry)
1112 SALOMEDSImpl_SComponent aSCO;
1113 if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
1114 aSCO = _mapOfSCO[theEntry];
1116 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1117 aSCO = SALOMEDSImpl_SComponent(aLabel);
1118 _mapOfSCO[theEntry] = aSCO;
1124 //============================================================================
1125 /*! Function : GetSComponent
1128 //============================================================================
1129 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
1131 return SALOMEDSImpl_SComponent(theLabel);
1134 //============================================================================
1135 /*! Function : GetSObject
1138 //============================================================================
1139 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const std::string& theEntry)
1141 SALOMEDSImpl_SObject aSO;
1142 std::map<std::string, SALOMEDSImpl_SObject>::iterator it=_mapOfSO.find(theEntry);
1143 if(it != _mapOfSO.end())
1146 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1147 aSO = SALOMEDSImpl_SObject(aLabel);
1148 _mapOfSO[theEntry] = aSO;
1154 //============================================================================
1155 /*! Function : GetSObject
1158 //============================================================================
1159 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
1161 return SALOMEDSImpl_SObject(theLabel);
1164 //============================================================================
1165 /*! Function : GetAttribute
1168 //============================================================================
1169 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const std::string& theEntry,
1170 const std::string& theType)
1172 SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
1173 DF_Attribute* anAttr;
1174 aSO.FindAttribute(anAttr, theType);
1178 //! number of spaces for indentation in Python dump files (to replace \t symbols)
1179 static const int indent_size = 2;
1181 static std::string replace_tabs( const std::string& in )
1183 std::string out = in;
1185 size_t pos = out.find( '\t' );
1186 while ( pos != std::string::npos ) {
1187 out.replace( pos, 1, indent_size, ' ' );
1188 pos = out.find( '\t' );
1194 static std::string GetComponentHeader(const char* theComponentName)
1196 std::stringstream txt;
1197 txt << "###" << std::endl;
1198 txt << "### " << theComponentName << " component" << std::endl;
1199 txt << "###" << std::endl;
1203 //============================================================================
1204 /*! Function : DumpStudy
1207 //============================================================================
1208 bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
1209 const std::string& theBaseName,
1212 SALOMEDSImpl_DriverFactory* theFactory)
1216 if(theFactory == NULL) {
1217 _errorCode = "Null factory for creation of Engines";
1221 std::vector<std::string> aSeq;
1222 std::string aCompType, aFactoryType;
1224 //Build a list of all components in the Study
1225 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
1227 for (; itcomponent.More(); itcomponent.Next()) {
1228 SALOMEDSImpl_SComponent sco = itcomponent.Value();
1229 aCompType = sco.ComponentDataType();
1230 //GEOM and MED are independent components
1231 if (aCompType == "GEOM" || aCompType == "MED")
1232 aSeq.insert(aSeq.begin(), aCompType);
1234 aSeq.push_back(aCompType);
1238 std::string aFileName =
1239 thePath + std::string("\\") + theBaseName + std::string(".py");
1241 std::string aFileName =
1242 thePath + std::string("/") + theBaseName + std::string(".py");
1245 //Create a file that will contain a main Study script
1247 fp.open(aFileName.c_str(), std::ios::out);
1250 bool isOpened = fp.is_open();
1252 bool isOpened = fp.rdbuf()->is_open();
1256 _errorCode = std::string("Can't create a file ")+aFileName;
1260 std::stringstream sfp;
1262 std::string aBatchModeScript = "salome";
1264 //Output to the main Study script required Python modules import,
1265 //set sys.path and add a creation of the study.
1268 sfp << GetDumpStudyComment() << std::endl;
1271 sfp << "import sys" << std::endl;
1272 sfp << "import " << aBatchModeScript << std::endl << std::endl;
1274 // initialization function
1275 sfp << aBatchModeScript << ".salome_init()" << std::endl;
1277 sfp << "theStudy = salome.myStudy" <<std::endl << std::endl;
1279 // notebook initialization
1280 sfp << _GetNoteBookAccess();
1282 // extend sys.path with the directory where the script is being dumped to
1283 sfp << "sys.path.insert( 0, r\'" << thePath << "\')" << std::endl << std::endl;
1285 // dump NoteBook variables
1286 sfp << _GetStudyVariablesScript();
1288 // dump visual parameters if necessary
1289 bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
1290 int lastSavePoint = -1;
1292 lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
1293 if(lastSavePoint > 0) {
1294 sfp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << std::endl << std::endl;
1298 std::vector<std::string> aSeqOfFileNames;
1300 // dump all components and create the components specific scripts
1302 int aLength = aSeq.size();
1303 for(int i = 1; i <= aLength; i++) {
1305 aCompType = aSeq[i-1];
1306 SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
1307 SALOMEDSImpl_Driver* aDriver = NULL;
1308 // if there is an associated Engine call its method for saving
1309 std::string IOREngine;
1311 if (!sco.ComponentIOR(IOREngine)) {
1312 if (!aCompType.empty()) {
1314 aDriver = theFactory->GetDriverByType(aCompType);
1316 if (aDriver != NULL) {
1317 SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
1318 if(!SB->LoadWith(sco, aDriver)) {
1319 _errorCode = SB->GetErrorCode();
1327 aDriver = theFactory->GetDriverByIOR(IOREngine);
1330 _errorCode = "Can not restore information to dump it";
1334 if(aDriver == NULL) continue;
1337 long aStreamLength = 0;
1338 SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(this, isPublished, isMultiFile, isValidScript, aStreamLength);
1339 if ( !isValidScript )
1342 std::stringstream sfp2;
1344 //Output the Python script generated by the component in the newly created file.
1346 sfp2 << GetDumpStudyComment( aCompType.c_str() ) << std::endl;
1348 sfp2 << GetComponentHeader( aCompType.c_str() ) << std::endl;
1349 sfp2 << aStream->Data();
1351 if ( isMultiFile ) {
1352 //Create a file that will contain the component specific script
1355 aFileName=thePath+std::string("\\");
1357 aFileName=thePath+std::string("/");
1359 std::string aScriptName;
1360 aScriptName += theBaseName;
1362 aScriptName += aCompType;
1364 aFileName += aScriptName+ std::string(".py");
1365 aSeqOfFileNames.push_back(aFileName);
1367 fp2.open(aFileName.c_str(), std::ios::out);
1370 isOpened = fp2.is_open();
1372 isOpened = fp2.rdbuf()->is_open();
1376 _errorCode = std::string("Can't create a file ")+aFileName;
1377 SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
1381 // replace '\t' symbols
1382 fp2 << replace_tabs( sfp2.str() );
1386 //Add to the main script a call to RebuildData of the generated by the component the Python script
1387 sfp << "import " << aScriptName << std::endl;
1388 sfp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << std::endl;
1393 if(aStream) delete aStream;
1397 sfp << "if salome.sg.hasDesktop():" << std::endl;
1398 sfp << "\tsalome.sg.updateObjBrowser(1)" << std::endl;
1400 if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
1401 sfp << "\tiparameters.getSession().restoreVisualState(1)" << std::endl;
1404 // replace '\t' symbols
1405 fp << replace_tabs( sfp.str() );
1412 //=======================================================================
1413 //function : GetDumpStudyComment
1414 //purpose : return a header comment for a DumpStudy script
1415 //=======================================================================
1417 std::string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
1419 std::stringstream txt;
1420 txt << "# -*- coding: iso-8859-1 -*-" << std::endl << std::endl;
1421 txt << "###" << std::endl;
1422 txt << "### This file is generated automatically by SALOME v"
1423 << KERNEL_VERSION_STR
1424 << " with dump python functionality";
1425 if ( theComponentName )
1426 txt << " (" << theComponentName << " component)";
1428 txt << "###" << std::endl;
1432 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1434 const std::string& Tab,
1435 SALOMEDSImpl_Study* theStudy);
1437 //============================================================================
1441 //============================================================================
1442 void SALOMEDSImpl_Study::dump(const std::string& theFileName)
1444 //Create a file that will contain a main Study script
1446 fp.open(theFileName.c_str(), std::ios::out);
1449 bool isOpened = fp.is_open();
1451 bool isOpened = fp.rdbuf()->is_open();
1455 _errorCode = std::string("Can't create a file ")+theFileName;
1456 std::cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << std::endl;
1460 SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
1461 fp << "0:1" << std::endl;
1462 SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
1463 std::string aTab(" ");
1464 for(; Itr.More(); Itr.Next()) {
1465 dumpSO(Itr.Value(), fp, aTab, this);
1472 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1474 const std::string& Tab,
1475 SALOMEDSImpl_Study* theStudy)
1477 std::string aTab(Tab), anID(theSO.GetID());
1478 fp << aTab << anID << std::endl;
1479 std::vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
1480 for(int i = 0; i<attribs.size(); i++) {
1481 SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
1487 std::string aType = anAttr->GetClassType();
1488 fp << Tab << " -- " << aType;
1490 if(aType == std::string("AttributeReal")) {
1491 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
1493 else if(aType == std::string("AttributeInteger")) {
1494 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
1496 else if(aType == std::string("AttributeName")) {
1497 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
1499 else if(aType == std::string("AttributeComment")) {
1500 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
1502 else if(aType == std::string("AttributeReference")) {
1503 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
1508 SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
1509 std::string aNewTab(" ");
1511 for(; Itr.More(); Itr.Next()) {
1512 dumpSO(Itr.Value(), fp, aNewTab, theStudy);
1518 void SALOMEDSImpl_Study::Modify()
1521 _doc->SetModified(true);
1524 //============================================================================
1528 //============================================================================
1529 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
1531 if (theSavePoint < -1) return NULL;
1532 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1533 SALOMEDSImpl_SObject so = FindComponent((char*)theID);
1534 if (!so) so = builder->NewComponent((char*)theID);
1535 SALOMEDSImpl_AttributeParameter* attParam = NULL;
1537 if (theSavePoint == -1) {
1539 DF_Label savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
1540 DF_Label prevPointLabel;
1541 while ( !savePointLabel.IsNull() ) {
1543 prevPointLabel = savePointLabel;
1544 savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
1546 if ( !prevPointLabel.IsNull() )
1547 so = GetSObject( prevPointLabel );
1549 if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
1550 DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
1551 if ( !savePointLabel.IsNull() )
1552 so = GetSObject( savePointLabel );
1553 else // ... if it does not exist - create a new one
1554 so = builder->NewObjectToTag( so, theSavePoint );
1559 builder->FindAttribute(so, A, "AttributeParameter");
1560 if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
1561 A = builder->FindOrCreateAttribute(so, "AttributeParameter");
1563 attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
1568 //============================================================================
1572 //============================================================================
1573 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID,
1574 const char* theModuleName,
1577 if(theSavePoint < -1) return NULL;
1578 SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
1579 SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
1580 SALOMEDSImpl_AttributeParameter* par = NULL;
1582 SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
1583 std::string moduleName(theModuleName);
1584 for(; it.More(); it.Next()) {
1585 SALOMEDSImpl_SObject so(it.Value());
1586 if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
1587 if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
1588 if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
1592 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1593 SALOMEDSImpl_SObject so = builder->NewObject(main_so);
1594 par = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
1595 par->SetString("AP_MODULE_NAME", moduleName);
1599 //============================================================================
1600 /*! Function : SetStudyLock
1603 //============================================================================
1604 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
1606 _lockers.push_back(theLockerID);
1609 //============================================================================
1610 /*! Function : IsStudyLocked
1613 //============================================================================
1614 bool SALOMEDSImpl_Study::IsStudyLocked()
1616 return (_lockers.size() > 0);
1619 //============================================================================
1620 /*! Function : UnLockStudy
1623 //============================================================================
1624 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
1626 std::vector<std::string>::iterator vsI = _lockers.begin();
1627 int length = _lockers.size();
1628 bool isFound = false;
1629 std::string id(theLockerID);
1630 for(int i = 0; i<length; i++, vsI++) {
1631 if(id == _lockers[i]) {
1636 if(isFound) _lockers.erase(vsI);
1639 //============================================================================
1640 /*! Function : GetLockerID
1643 //============================================================================
1644 std::vector<std::string> SALOMEDSImpl_Study::GetLockerID()
1649 //============================================================================
1650 /*! Function : SetVariable
1653 //============================================================================
1654 void SALOMEDSImpl_Study::SetVariable(const std::string& theVarName,
1655 const double theValue,
1656 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
1658 bool modified = false;
1659 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1661 if( aGVar == NULL ) {
1663 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
1665 aSVar->setValue(theValue);
1666 myNoteBookVars.push_back(aSVar);
1670 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
1671 modified = aSVar->setValue(theValue) || modified;
1672 modified = aSVar->setType(theType) || modified;
1679 //============================================================================
1680 /*! Function : SetStringVariable
1683 //============================================================================
1684 void SALOMEDSImpl_Study::SetStringVariable(const std::string& theVarName,
1685 const std::string& theValue,
1686 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
1688 bool modified = false;
1689 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1691 if( aGVar == NULL ) {
1693 SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
1695 aSVar->setStringValue(theValue);
1696 myNoteBookVars.push_back(aSVar);
1700 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
1701 modified = aSVar->setStringValue(theValue) || modified;
1702 modified = aSVar->setType(theType) || modified;
1709 //============================================================================
1710 /*! Function : SetStringVariableAsDouble
1713 //============================================================================
1714 void SALOMEDSImpl_Study::SetStringVariableAsDouble(const std::string& theVarName,
1715 const double theValue,
1716 const SALOMEDSImpl_GenericVariable::VariableTypes theType)
1718 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1719 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
1720 aSVar->setValue(theValue);
1723 //============================================================================
1724 /*! Function : GetReal
1727 //============================================================================
1728 double SALOMEDSImpl_Study::GetVariableValue(const std::string& theVarName)
1730 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1733 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
1734 return aSVar->getValue();
1739 //============================================================================
1740 /*! Function : GetString
1743 //============================================================================
1744 std::string SALOMEDSImpl_Study::GetStringVariableValue(const std::string& theVarName)
1746 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1749 if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
1750 return aSVar->getStringValue();
1755 //============================================================================
1756 /*! Function : IsTypeOf
1759 //============================================================================
1760 bool SALOMEDSImpl_Study::IsTypeOf(const std::string& theVarName,
1761 SALOMEDSImpl_GenericVariable::
1762 VariableTypes theType) const
1764 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1767 return aGVar->Type() == theType;
1772 //============================================================================
1773 /*! Function : IsVariable
1776 //============================================================================
1777 bool SALOMEDSImpl_Study::IsVariable(const std::string& theVarName) const
1779 SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1780 return (aGVar != NULL);
1783 //============================================================================
1784 /*! Function : GetVariableNames
1787 //============================================================================
1788 std::vector<std::string> SALOMEDSImpl_Study::GetVariableNames() const
1790 std::vector<std::string> aResult;
1792 for(int i = 0; i < myNoteBookVars.size(); i++)
1793 aResult.push_back(myNoteBookVars[i]->Name());
1798 //============================================================================
1799 /*! Function : AddVariable
1802 //============================================================================
1803 void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
1805 myNoteBookVars.push_back(theVariable);
1808 //============================================================================
1809 /*! Function : AddVariable
1812 //============================================================================
1813 SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
1815 SALOMEDSImpl_GenericVariable* aResult = NULL;
1816 for(int i = 0; i < myNoteBookVars.size();i++) {
1817 if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
1818 aResult = myNoteBookVars[i];
1825 //============================================================================
1826 /*! Function : RemoveVariable
1829 //============================================================================
1830 bool SALOMEDSImpl_Study::RemoveVariable(const std::string& theVarName)
1832 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
1836 std::string aValue = aVariable->SaveToScript();
1837 ReplaceVariableAttribute( theVarName, aValue );
1839 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
1840 for( ; it != itEnd; it++ )
1842 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
1843 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
1845 myNoteBookVars.erase( it );
1854 //============================================================================
1855 /*! Function : RenameVariable
1858 //============================================================================
1859 bool SALOMEDSImpl_Study::RenameVariable(const std::string& theVarName, const std::string& theNewVarName)
1861 SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
1865 ReplaceVariableAttribute( theVarName, theNewVarName );
1867 std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
1868 for( ; it != itEnd; it++ )
1870 SALOMEDSImpl_GenericVariable* aVariableRef = *it;
1871 if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
1873 aVariableRef->setName( theNewVarName );
1882 //============================================================================
1883 /*! Function : IsVariableUsed
1886 //============================================================================
1887 bool SALOMEDSImpl_Study::IsVariableUsed(const std::string& theVarName)
1889 return FindVariableAttribute( theVarName );
1892 //============================================================================
1893 /*! Function : FindVariableAttribute
1896 //============================================================================
1897 bool SALOMEDSImpl_Study::FindVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
1898 SALOMEDSImpl_SObject theSObject,
1899 const std::string& theName)
1901 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
1902 for( ; anIter.More(); anIter.Next() )
1903 if( FindVariableAttribute( theStudyBuilder, anIter.Value(), theName ) )
1906 DF_Attribute* anAttr;
1907 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
1909 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
1911 std::string aString = aStringAttr->Value();
1913 std::vector< std::vector<std::string> > aSections = ParseVariables( aString );
1914 for( int i = 0, n = aSections.size(); i < n; i++ )
1916 std::vector<std::string> aVector = aSections[i];
1917 for( int j = 0, m = aVector.size(); j < m; j++ )
1919 std::string aStr = aVector[j];
1920 if( aStr.compare( theName ) == 0 )
1929 //============================================================================
1930 /*! Function : FindVariableAttribute
1933 //============================================================================
1934 bool SALOMEDSImpl_Study::FindVariableAttribute(const std::string& theName)
1936 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
1937 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
1938 for( ; aCompIter.More(); aCompIter.Next() )
1940 SALOMEDSImpl_SObject aComp = aCompIter.Value();
1941 if( FindVariableAttribute( aStudyBuilder, aComp, theName ) )
1947 //============================================================================
1948 /*! Function : ReplaceVariableAttribute
1951 //============================================================================
1952 void SALOMEDSImpl_Study::ReplaceVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
1953 SALOMEDSImpl_SObject theSObject,
1954 const std::string& theSource,
1955 const std::string& theDest)
1957 SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
1958 for( ; anIter.More(); anIter.Next() )
1959 ReplaceVariableAttribute( theStudyBuilder, anIter.Value(), theSource, theDest );
1961 DF_Attribute* anAttr;
1962 if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
1964 if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
1966 bool isChanged = false;
1967 std::string aNewString, aCurrentString = aStringAttr->Value();
1969 std::vector< std::vector<std::string> > aSections = ParseVariables( aCurrentString );
1970 for( int i = 0, n = aSections.size(); i < n; i++ )
1972 std::vector<std::string> aVector = aSections[i];
1973 for( int j = 0, m = aVector.size(); j < m; j++ )
1975 std::string aStr = aVector[j];
1976 if( aStr.compare( theSource ) == 0 )
1982 aNewString.append( aStr );
1984 aNewString.append( ":" );
1987 aNewString.append( "|" );
1991 aStringAttr->SetValue( aNewString );
1996 //============================================================================
1997 /*! Function : ReplaceVariableAttribute
2000 //============================================================================
2001 void SALOMEDSImpl_Study::ReplaceVariableAttribute(const std::string& theSource, const std::string& theDest)
2003 SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
2004 SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
2005 for( ; aCompIter.More(); aCompIter.Next() )
2007 SALOMEDSImpl_SObject aComp = aCompIter.Value();
2008 ReplaceVariableAttribute( aStudyBuilder, aComp, theSource, theDest );
2012 //============================================================================
2013 /*! Function : ParseVariables
2016 //============================================================================
2017 std::vector< std::vector< std::string > > SALOMEDSImpl_Study::ParseVariables(const std::string& theVariables) const
2019 return SALOMEDSImpl_Tool::splitStringWithEmpty( theVariables, OPERATION_SEPARATOR, VARIABLE_SEPARATOR );
2022 //============================================================================
2023 /*! Function : EnableUseCaseAutoFilling
2026 //============================================================================
2027 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
2029 _errorCode = ""; _autoFill = isEnabled;
2031 _builder->SetOnAddSObject(_cb);
2032 _builder->SetOnRemoveSObject(_cb);
2035 _builder->SetOnAddSObject(NULL);
2036 _builder->SetOnRemoveSObject(NULL);
2040 //============================================================================
2041 /*! Function : GetIORs
2044 //============================================================================
2045 std::vector<std::string> SALOMEDSImpl_Study::GetIORs()
2047 std::vector<std::string> anIORs;
2048 std::map<std::string, DF_Label>::const_iterator MI;
2049 for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
2050 anIORs.push_back(MI->first);
2055 //============================================================================
2056 /*! Function : addSO_Notification
2057 * Purpose : This function tells all the observers that a SO has been added
2059 //============================================================================
2060 bool SALOMEDSImpl_Study::addSO_Notification (const SALOMEDSImpl_SObject& theSObject)
2063 return _notifier->addSO_Notification(theSObject);
2068 //============================================================================
2069 /*! Function : removeSO_Notification
2070 * Purpose : This function tells all the observers that a SO has been removed
2072 //============================================================================
2073 bool SALOMEDSImpl_Study::removeSO_Notification (const SALOMEDSImpl_SObject& theSObject)
2076 return _notifier->removeSO_Notification(theSObject);
2081 //============================================================================
2082 /*! Function : modifySO_Notification
2083 * Purpose : This function tells all the observers that a SO has been modified and
2084 pass the mofification reason
2086 //============================================================================
2087 bool SALOMEDSImpl_Study::modifySO_Notification (const SALOMEDSImpl_SObject& theSObject, int reason)
2090 return _notifier->modifySO_Notification(theSObject, reason);
2095 //============================================================================
2096 /*! Function : setNotifier
2097 * Purpose : register a notifier
2099 //============================================================================
2100 void SALOMEDSImpl_Study::setNotifier(SALOMEDSImpl_AbstractCallback* notifier)
2105 static SALOMEDSImpl_AbstractCallback* & getGenObjRegister( DF_Document* doc )
2107 static std::vector< SALOMEDSImpl_AbstractCallback* > _genObjRegVec;
2108 if ( doc->GetDocumentID() >= (int)_genObjRegVec.size() )
2109 _genObjRegVec.resize( doc->GetDocumentID() + 1, 0 );
2110 return _genObjRegVec[ doc->GetDocumentID() ];
2113 //================================================================================
2115 * \brief Stores theRegister
2117 //================================================================================
2119 void SALOMEDSImpl_Study::setGenObjRegister(SALOMEDSImpl_AbstractCallback* theRegister)
2121 getGenObjRegister( _doc ) = theRegister;
2124 //================================================================================
2126 * \brief Indirectly invokes GenericObj_i::Register()
2128 //================================================================================
2130 void SALOMEDSImpl_Study::RegisterGenObj (const std::string& theIOR, DF_Label label)
2132 if ( SALOMEDSImpl_AbstractCallback* goRegister = getGenObjRegister( label.GetDocument() ))
2133 goRegister->RegisterGenObj( theIOR );
2136 //================================================================================
2138 * \brief Indirectly invokes GenericObj_i::UnRegister()
2140 //================================================================================
2142 void SALOMEDSImpl_Study::UnRegisterGenObj(const std::string& theIOR, DF_Label label)
2144 if ( SALOMEDSImpl_AbstractCallback* goRegister = getGenObjRegister( label.GetDocument() ))
2145 goRegister->UnRegisterGenObj( theIOR );