]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDSImpl/SALOMEDSImpl_Study.cxx
Salome HOME
Issue 0020472: An error in SALOMEDSImpl_Tool::Exists
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_Study.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOMEDSImpl_Study.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include "SALOMEDSImpl_Study.hxx"
27 #include <string.h>
28
29 using namespace std;
30
31 #include "DF_Application.hxx"
32 #include "DF_ChildIterator.hxx"
33
34 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
35 #include "SALOMEDSImpl_Attributes.hxx"
36 #include "SALOMEDSImpl_UseCaseIterator.hxx"
37 #include "SALOMEDSImpl_AttributeReference.hxx"
38 #include "SALOMEDSImpl_StudyHandle.hxx"
39 #include "SALOMEDSImpl_Tool.hxx"
40 #include "SALOMEDSImpl_IParameters.hxx"
41 #include "SALOMEDSImpl_ScalarVariable.hxx"
42
43 #include <fstream>
44
45 #define DIRECTORYID       16661
46 #define FILELOCALID       26662
47 #define FILEID            "FILE: "
48 #define VARIABLE_SEPARATOR  ':'
49 #define OPERATION_SEPARATOR '|'
50
51
52 //============================================================================
53 /*! Function : SALOMEDSImpl_Study
54  *  Purpose  : SALOMEDSImpl_Study constructor
55  */
56 //============================================================================
57 SALOMEDSImpl_Study::SALOMEDSImpl_Study(const DF_Document* doc,
58                                        const string& study_name)
59 {
60   _name = study_name;
61   _doc = (DF_Document*)doc;
62   _Saved = false ;
63   _URL = "";
64   _StudyId = -1;
65   _autoFill = false;
66   _errorCode = "";
67   _useCaseBuilder = new SALOMEDSImpl_UseCaseBuilder(_doc);
68   _builder = new SALOMEDSImpl_StudyBuilder(this);
69   _cb = new SALOMEDSImpl_Callback(_useCaseBuilder);
70   //Put on the root label a StudyHandle attribute to store the address of this object
71   //It will be used to retrieve the study object by DF_Label that belongs to the study
72   SALOMEDSImpl_StudyHandle::Set(_doc->Main().Root(), this);
73 }
74
75
76 //============================================================================
77 /*! Function : ~SALOMEDSImpl_Study
78  *  Purpose  : SALOMEDSImpl_Study destructor
79  */
80 //============================================================================
81 SALOMEDSImpl_Study::~SALOMEDSImpl_Study()
82 {
83   delete _builder;
84   delete _cb;
85   delete _useCaseBuilder;
86 }
87
88 //============================================================================
89 /*! Function : GetPersistentReference
90  *  Purpose  : Get persistent reference of study (idem URL())
91  */
92 //============================================================================
93 string SALOMEDSImpl_Study::GetPersistentReference()
94 {
95   _errorCode = "";
96   return URL();
97 }
98 //============================================================================
99 /*! Function : GetTransientReference
100  *  Purpose  : Get IOR of the Study (registred in Document in doc->Root)
101  */
102 //============================================================================
103 string SALOMEDSImpl_Study::GetTransientReference()
104 {
105   _errorCode = "";
106   string IOR = "";
107
108   SALOMEDSImpl_AttributeIOR* Att;
109   DF_Label _lab = _doc->Root();
110   if ((Att=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
111     IOR = Att->Value();
112   }
113   else {
114     _errorCode = "IOR is empty";
115   }
116
117   return IOR;
118 }
119
120 void SALOMEDSImpl_Study::SetTransientReference(const string& theIOR)
121 {
122   _errorCode = "";
123
124   SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
125   int aLocked = aProp->IsLocked();
126   if (aLocked) aProp->SetLocked(false);
127
128   // Assign the value of the IOR in the study->root
129   SALOMEDSImpl_AttributeIOR::Set(_doc->Main().Root(), theIOR);
130
131   if (aLocked) aProp->SetLocked(true);
132 }
133
134 //============================================================================
135 /*! Function : IsEmpty
136  *  Purpose  : Detect if study is empty
137  */
138 //============================================================================
139 bool SALOMEDSImpl_Study::IsEmpty()
140 {
141   _errorCode = "";
142   if (!_doc) return true;
143   return _doc->IsEmpty();
144 }
145
146 //============================================================================
147 /*! Function : FindComponent
148  *  Purpose  : Find a Component with ComponentDataType = aComponentName
149  */
150 //============================================================================
151 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const string& aComponentName)
152 {
153   _errorCode = "";
154   bool _find = false;
155   string name;
156   SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
157   SALOMEDSImpl_SComponent compo;
158
159   for (; itcomp.More(); itcomp.Next()) {
160     SALOMEDSImpl_SComponent SC = itcomp.Value();
161     name = SC.ComponentDataType();
162     if(aComponentName == name) {
163       _find = true;
164       return SC;
165     }
166   }
167
168   if(!_find)
169     {
170       _errorCode = "No component was found";
171       return compo;
172     }
173   return compo;
174 }
175
176 //============================================================================
177 /*! Function : FindComponentID
178  *  Purpose  : Find a Component from it's ID
179  */
180 //============================================================================
181 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const string& aComponentID)
182 {
183   _errorCode = "";
184
185   // Iterate on each components defined in the study
186   // Get the component ID and compare with aComponentID
187   bool _find = false;
188   string ID;
189   SALOMEDSImpl_SComponent compo;
190
191   SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
192   for (; itcomp.More(); itcomp.Next()) {
193     SALOMEDSImpl_SComponent SC = itcomp.Value();
194     ID = SC.GetID();
195     if(aComponentID == ID)
196       {
197         // ComponentID found
198         _find = true;
199         compo = SC;
200       }
201   }
202   if(!_find)
203     {
204       _errorCode = "No component was found";
205       compo = compo;
206     }
207
208   return compo;
209 }
210
211 //============================================================================
212 /*! Function : FindObject
213  *  Purpose  : Find an Object with SALOMEDSImpl_Name = anObjectName
214  */
215 //============================================================================
216 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const string& anObjectName)
217 {
218   _errorCode = "";
219
220   // Iterate to all components defined in the study
221   // After testing the component name, iterate in all objects defined under
222   // components (function _FindObject)
223   bool _find = false;
224   SALOMEDSImpl_SObject RefSO;
225
226   SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
227   for (; it.More();it.Next()){
228     if(!_find)
229       {
230         SALOMEDSImpl_SComponent SC = it.Value();
231         if (SC.GetName() == anObjectName)
232         {
233             _find = true;
234             RefSO = SC;
235
236         }
237         if (!_find) RefSO =  _FindObject(SC, anObjectName, _find);
238       }
239   }
240   if(!RefSO) _errorCode = "No object was found";
241   return RefSO;
242 }
243
244 //============================================================================
245 /*! Function : FindObjectID
246  *  Purpose  : Find an Object with ID = anObjectID
247  */
248 //============================================================================
249 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const string& anObjectID)
250 {
251   _errorCode = "";
252   SALOMEDSImpl_SObject so;
253   
254   // Convert aSO->GetID in DF_Label.
255   DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
256
257   if (Lab.IsNull()) {
258     _errorCode = "No label was found by ID";
259     return so;
260   }
261   return GetSObject(Lab);
262
263 }
264
265 //============================================================================
266 /*! Function : CreateObjectID
267  *  Purpose  : Creates an Object with ID = anObjectID
268  */
269 //============================================================================
270 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const string& anObjectID)
271 {
272   _errorCode = "";
273   SALOMEDSImpl_SObject so;
274
275   // Convert aSO->GetID in DF_Label.
276   DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
277
278   if (Lab.IsNull()) {
279     _errorCode = "Can not create a label";
280     return so;
281   }
282   return GetSObject(Lab);
283
284 }
285
286 //============================================================================
287 /*! Function : FindObjectByName
288  *  Purpose  : Find Objects with SALOMEDSImpl_Name = anObjectName in a Component
289  *           : with ComponentDataType = aComponentName
290  */
291 //============================================================================
292 vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const string& anObjectName,
293                                                           const string& aComponentName)
294 {
295   _errorCode = "";
296
297   vector<SALOMEDSImpl_SObject> listSO;
298
299   SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
300   if ( !compo ) {
301     _errorCode = "Can not find the component";
302     return listSO;
303   }
304
305   // Iterate on each object and subobject of the component
306   // If objectName is found add it to the list of SObjects
307   string childName ;
308
309   string compoId = compo.GetID();
310   SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
311   for ( ; it.More(); it.Next() ) {
312
313     SALOMEDSImpl_SObject CSO = it.Value();
314     if ( CSO.GetName() == anObjectName ) {
315         /* add to list */
316         listSO.push_back(CSO) ;
317     }
318
319     /* looks also for eventual children */
320     bool found = false ;
321     CSO = _FindObject( CSO, anObjectName, found ) ;
322     if( found) {
323       listSO.push_back(CSO) ;
324     }
325   }
326
327   return listSO;
328 }
329
330
331
332 //============================================================================
333 /*! Function : FindObjectIOR
334  *  Purpose  : Find an Object with IOR = anObjectIOR
335  */
336 //============================================================================
337 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const string& anObjectIOR)
338 {
339   _errorCode = "";
340   
341   SALOMEDSImpl_SObject aResult ;
342   
343   // searching in the datamap for optimization
344   if (myIORLabels.find(anObjectIOR) != myIORLabels.end()) {
345     aResult = GetSObject(myIORLabels[anObjectIOR]);
346     // 11 oct 2002: forbidden attributes must be checked here
347     if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
348       myIORLabels.erase(anObjectIOR);
349       aResult = SALOMEDSImpl_SObject();  
350     }  
351   }
352   
353   if(!aResult) _errorCode = "No object was found";
354   return aResult;
355 }
356
357 //============================================================================
358 /*! Function : FindObjectByPath
359  *  Purpose  : Find an Object by its path = thePath
360  */
361 //============================================================================
362 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const string& thePath)
363 {
364   _errorCode = "";
365
366   string aPath(thePath), aToken;
367   SALOMEDSImpl_SObject aSO;
368   int aLength = aPath.size();
369   bool isRelative = false;
370
371   if(aLength == 0) {  //Empty path - return the current context
372     return GetSObject(_current);
373   }
374
375   if(aPath[0] != '/')  //Relative path
376     isRelative = true;
377
378   DF_ChildIterator anIterator;
379   DF_Label aLabel;
380   SALOMEDSImpl_AttributeName* anAttr;
381
382   if(isRelative) {
383     if(_current.IsNull()) return aSO;
384     anIterator.Init(_current, false);
385   }
386   else {
387     if(aPath.size() == 1 && aPath[0] == '/') {    //Root
388       return GetSObject(_doc->Main());
389     }
390     anIterator.Init(_doc->Main(), false);
391   }
392
393   vector<string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
394   for(int i = 0, len = vs.size(); i<len; i++) {
395
396     aToken = vs[i];
397     if(aToken.size() == 0) break;
398
399     for ( ; anIterator.More(); anIterator.Next() ) {
400       aLabel = anIterator.Value();
401       if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
402         if(anAttr->Value() == aToken) {
403           if(i == (len-1)) {  //The searched label is found (no part of the path is left)
404               return GetSObject(aLabel);
405           }
406
407           anIterator.Init(aLabel, false);
408           break;
409         }
410       }
411     }
412
413   }
414
415   if(!aSO) _errorCode = "No object was found";
416   return aSO;
417 }
418
419 //============================================================================
420 /*! Function : GetObjectPath
421  *  Purpose  :
422  */
423 //============================================================================
424 string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
425 {
426   _errorCode = "";
427
428   string aPath("");
429   if(!theObject) {
430     _errorCode = "Null object";
431     return aPath;
432   }
433
434   string aName = theObject.GetName();
435   if(!aName.empty() && aName != "" ) {
436     string aValue("/");
437     aValue+=aName;
438     aValue += aPath;
439     aPath = aValue;
440     SALOMEDSImpl_SObject aFather = theObject.GetFather();
441     if(aFather) {
442        aName = aFather.GetName();
443        if(!aName.empty() && aName != "") {
444           aValue = GetObjectPath(aFather);
445           aPath = aValue + aPath;
446        }
447     }
448   }
449
450   return aPath;
451 }
452
453
454 //============================================================================
455 /*! Function : GetObjectPathByIOR
456  *  Purpose  :
457  */
458 //============================================================================
459 string SALOMEDSImpl_Study::GetObjectPathByIOR(const string& theIOR)
460 {
461   _errorCode = "";
462
463   string aPath;
464   SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
465   if(!so) {
466     _errorCode = "No SObject was found by IOR";
467     return aPath;
468   }
469
470   return GetObjectPath(so);
471 }
472
473
474 //============================================================================
475 /*! Function : SetContext
476  *  Purpose  : Sets the current context
477  */
478 //============================================================================
479 bool SALOMEDSImpl_Study::SetContext(const string& thePath)
480 {
481   _errorCode = "";
482   if(thePath.empty()) {
483     _errorCode = "InvalidPath";
484     return false;
485   }
486
487   string aPath(thePath), aContext("");
488   bool isInvalid = false;
489   SALOMEDSImpl_SObject aSO;
490
491   if(aPath[0] != '/') { //Relative path
492     aContext = GetContext();
493     aContext += '/';
494     aContext += aPath;
495   }
496   else
497     aContext = aPath;
498
499   try {
500     aSO = FindObjectByPath(aContext);
501   }
502   catch( ... ) {
503     isInvalid = true;
504   }
505
506   if(isInvalid || !aSO) {
507     _errorCode = "InvalidContext";
508     return false;
509   }
510
511   DF_Label aLabel = aSO.GetLabel();
512   if(aLabel.IsNull()) {
513     _errorCode = "InvalidContext";
514     return false;
515   }
516   else
517     _current = aLabel;  //Set the current context
518
519   return true;
520 }
521
522 //============================================================================
523 /*! Function : GetContext
524  *  Purpose  : Gets the current context
525  */
526 //============================================================================
527 string SALOMEDSImpl_Study::GetContext()
528 {
529   _errorCode = "";
530
531   if(_current.IsNull()) {
532     _errorCode = "InvaidContext";
533     return "";
534   }
535   SALOMEDSImpl_SObject so = GetSObject(_current);
536   return GetObjectPath(so);
537 }
538
539 //============================================================================
540 /*! Function : GetObjectNames
541  *  Purpose  : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
542  */
543 //============================================================================
544 vector<string> SALOMEDSImpl_Study::GetObjectNames(const string& theContext)
545 {
546   _errorCode = "";
547
548   vector<string> aResultSeq;
549   DF_Label aLabel;
550   if (theContext.empty()) {
551     aLabel = _current;
552   } else {
553     DF_Label aTmp = _current;
554     SetContext(theContext);
555     aLabel = _current;
556     _current = aTmp;
557   }
558   if (aLabel.IsNull()) {
559     _errorCode = "InvalidContext";
560     return aResultSeq;
561   }
562
563   DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
564   for (; anIter.More(); anIter.Next()) {
565     DF_Label aLabel = anIter.Value();
566     SALOMEDSImpl_AttributeName* aName;
567     if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) 
568       aResultSeq.push_back(aName->Value());
569   }
570
571   return aResultSeq;
572 }
573
574 //============================================================================
575 /*! Function : GetDirectoryNames
576  *  Purpose  : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
577  */
578 //============================================================================
579 vector<string> SALOMEDSImpl_Study::GetDirectoryNames(const string& theContext)
580 {
581   _errorCode = "";
582
583   vector<string> aResultSeq;
584   DF_Label aLabel;
585   if (theContext.empty()) {
586     aLabel = _current;
587   } else {
588     DF_Label aTmp = _current;
589     SetContext(theContext);
590     aLabel = _current;
591     _current = aTmp;
592   }
593   if (aLabel.IsNull()) {
594     _errorCode = "InvalidContext";
595     return aResultSeq;
596   }
597
598   DF_ChildIterator anIter (aLabel, true); // iterate first-level children at all sublevels
599   for (; anIter.More(); anIter.Next()) {
600     DF_Label aLabel = anIter.Value();
601     SALOMEDSImpl_AttributeLocalID* anID;
602     if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
603       if (anID->Value() == DIRECTORYID) {
604         SALOMEDSImpl_AttributeName* aName;
605         if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
606           aResultSeq.push_back(aName->Value());
607         }
608       }
609     }
610   }
611
612   return aResultSeq;
613 }
614
615 //============================================================================
616 /*! Function : GetFileNames
617  *  Purpose  : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
618  */
619 //============================================================================
620 vector<string> SALOMEDSImpl_Study::GetFileNames(const string& theContext)
621 {
622   _errorCode = "";
623
624   vector<string> aResultSeq;
625   DF_Label aLabel;
626   if (theContext.empty()) {
627     aLabel = _current;
628   } else {
629     DF_Label aTmp = _current;
630     SetContext(theContext);
631     aLabel = _current;
632     _current = aTmp;
633   }
634   if (aLabel.IsNull()) {
635     _errorCode = "InvalidContext";
636     return aResultSeq;
637   }
638
639   DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
640   for (; anIter.More(); anIter.Next()) {
641     DF_Label aLabel = anIter.Value();
642     SALOMEDSImpl_AttributeLocalID* anID;
643     if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
644       if (anID->Value() == FILELOCALID) {
645         SALOMEDSImpl_AttributePersistentRef* aName;
646         if ((aName=(SALOMEDSImpl_AttributePersistentRef*)aLabel.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
647           std::string aFileName = aName->Value();
648           if (aFileName.size() > 0)
649             aResultSeq.push_back(aFileName.substr(strlen(FILEID), aFileName.size()));
650         }
651       }
652     }
653   }
654
655   return aResultSeq;
656 }
657
658 //============================================================================
659 /*! Function : GetComponentNames
660  *  Purpose  : method to get all components names
661  */
662 //============================================================================
663 vector<string> SALOMEDSImpl_Study::GetComponentNames(const string& theContext)
664 {
665   _errorCode = "";
666
667   vector<string> aResultSeq;
668   DF_ChildIterator anIter(_doc->Main(), false); // iterate all subchildren at first level
669   for(; anIter.More(); anIter.Next()) {
670     DF_Label aLabel = anIter.Value();
671     SALOMEDSImpl_AttributeName* aName;
672     if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) 
673       aResultSeq.push_back(aName->Value());
674   }
675
676   return aResultSeq;
677 }
678
679 //============================================================================
680 /*! Function : NewChildIterator
681  *  Purpose  : Create a ChildIterator from an SObject
682  */
683 //============================================================================
684 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
685 {
686   _errorCode = "";
687   return SALOMEDSImpl_ChildIterator(aSO);
688 }
689
690
691 //============================================================================
692 /*! Function : NewComponentIterator
693  *  Purpose  : Create a SComponentIterator
694  */
695 //============================================================================
696 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
697 {
698   _errorCode = "";
699   return SALOMEDSImpl_SComponentIterator(_doc);
700 }
701
702
703 //============================================================================
704 /*! Function : NewBuilder
705  *  Purpose  : Create a StudyBuilder
706  */
707 //============================================================================
708 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
709 {
710   _errorCode = "";
711   if(_autoFill) {
712     _builder->SetOnAddSObject(_cb);
713     _builder->SetOnRemoveSObject(_cb);
714   }
715   return _builder;
716
717 }
718
719 //============================================================================
720 /*! Function : Name
721  *  Purpose  : get study name
722  */
723 //============================================================================
724 string SALOMEDSImpl_Study::Name()
725 {
726   _errorCode = "";
727   return _name;
728 }
729
730 //============================================================================
731 /*! Function : Name
732  *  Purpose  : set study name
733  */
734 //============================================================================
735 void SALOMEDSImpl_Study::Name(const string& name)
736 {
737   _errorCode = "";
738   _name = name;
739 }
740
741 //============================================================================
742 /*! Function : IsSaved
743  *  Purpose  : get if study has been saved
744  */
745 //============================================================================
746 bool SALOMEDSImpl_Study::IsSaved()
747 {
748   _errorCode = "";
749   return _Saved;
750 }
751
752 //============================================================================
753 /*! Function : IsSaved
754  *  Purpose  : set if study has been saved
755  */
756 //============================================================================
757 void SALOMEDSImpl_Study::IsSaved(bool save)
758 {
759   _errorCode = "";
760   _Saved = save;
761   if(save) _doc->SetModified(false);
762 }
763
764 //============================================================================
765 /*! Function : IsModified
766  *  Purpose  : Detect if a Study has been modified since it has been saved
767  */
768 //============================================================================
769 bool SALOMEDSImpl_Study::IsModified()
770 {
771   _errorCode = "";
772
773   // True if is modified
774   if (_doc && _doc->IsModified()) return true;
775
776   return false;
777 }
778
779 //============================================================================
780 /*! Function : URL
781  *  Purpose  : get URL of the study (persistent reference of the study)
782  */
783 //============================================================================
784 string SALOMEDSImpl_Study::URL()
785 {
786   _errorCode = "";
787   return _URL;
788 }
789
790 //============================================================================
791 /*! Function : URL
792  *  Purpose  : set URL of the study (persistent reference of the study)
793  */
794 //============================================================================
795 void SALOMEDSImpl_Study::URL(const string& url)
796 {
797   _errorCode = "";
798   _URL = url;
799
800   /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
801   string tmp(_URL);
802
803   char *aName = (char*)tmp.ToCString();
804   char *adr = strtok(aName, "/");
805   while (adr)
806     {
807       aName = adr;
808       adr = strtok(NULL, "/");
809     }
810   Name(aName);*/
811   Name(url);
812 }
813
814
815 //============================================================================
816 /*! Function : _FindObject
817  *  Purpose  : Find an Object with SALOMEDSImpl_Name = anObjectName
818  */
819 //============================================================================
820 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
821                                                              const string& theObjectName,
822                                                              bool& _find)
823 {
824   SALOMEDSImpl_SObject RefSO;
825   if(!SO) return RefSO;
826
827   // Iterate on each objects and subobjects of the component
828   // If objectName find, stop the loop and get the object reference
829   SALOMEDSImpl_AttributeName* anAttr;
830
831   string soid = SO.GetID();
832   DF_ChildIterator it(SO.GetLabel());
833   for (; it.More(); it.Next()){
834     if(!_find)
835       {
836         if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
837         {
838           string Val(anAttr->Value());
839           if (Val == theObjectName)
840             {
841               RefSO = GetSObject(it.Value());
842               _find = true;
843             }
844         }
845         if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
846       }
847   }
848   return RefSO;
849 }
850
851 //============================================================================
852 /*! Function : _FindObjectIOR
853  *  Purpose  : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
854  */
855 //============================================================================
856 SALOMEDSImpl_SObject
857 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
858                                    const string& theObjectIOR,
859                                    bool& _find)
860 {
861   SALOMEDSImpl_SObject RefSO, aSO;
862   if(!SO) return RefSO;
863
864   // Iterate on each objects and subobjects of the component
865   // If objectName find, stop the loop and get the object reference
866   SALOMEDSImpl_AttributeIOR* anAttr;
867
868   DF_ChildIterator it(SO.GetLabel());
869   for (; it.More();it.Next()){
870     if(!_find)
871       {
872         if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
873         {
874           string Val(anAttr->Value());
875           if (Val == theObjectIOR)
876             {
877               RefSO = GetSObject(it.Value());
878               _find = true;
879             }
880         }
881         aSO = GetSObject(it.Value());
882         if (!_find) RefSO =  _FindObjectIOR(aSO, theObjectIOR, _find);
883       }
884   }
885   return RefSO;
886 }
887
888 //============================================================================
889 /*! Function : _GetNoteBookAccessor
890  *  Purpose  : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
891  */
892 //============================================================================
893 string SALOMEDSImpl_Study::_GetNoteBookAccessor(){
894   return string("notebook");
895 }
896
897 //============================================================================
898 /*! Function : _GetStudyVariablesScript
899  *  Purpose  : 
900  */
901 //============================================================================
902 string SALOMEDSImpl_Study::_GetStudyVariablesScript()
903 {
904   string dump("");
905   
906   if(myNoteBookVars.empty())
907     return dump;
908   
909   dump += "####################################################\n";
910   dump += "##       Begin of NoteBook variables section      ##\n";
911   dump += "####################################################\n";
912
913   string set_method = _GetNoteBookAccessor()+".set(";
914   string varName;
915   string varValue;
916   for(int i = 0 ; i < myNoteBookVars.size();i++ ) {
917     varName = myNoteBookVars[i]->Name();
918     varValue = myNoteBookVars[i]->SaveToScript();
919     dump+=set_method+"\""+varName+"\", "+varValue+")\n";
920   }
921   
922   dump += "####################################################\n";
923   dump += "##        End of NoteBook variables section       ##\n";
924   dump += "####################################################\n";
925
926   return dump;
927 }
928
929 //============================================================================
930 /*! Function : _GetNoteBookAccess
931  *  Purpose  :
932  */
933 //============================================================================
934 string SALOMEDSImpl_Study::_GetNoteBookAccess()
935 {
936   string accessor = _GetNoteBookAccessor();
937   string notebook = "import salome_notebook\n";
938          notebook += accessor+" = salome_notebook."+accessor + "\n";
939   return notebook;       
940 }
941
942 bool SALOMEDSImpl_Study::IsLocked()
943 {
944   _errorCode = "";
945   return GetProperties()->IsLocked();
946 }
947
948 int SALOMEDSImpl_Study::StudyId()
949 {
950   _errorCode = "";
951   return _StudyId;
952 }
953
954 void SALOMEDSImpl_Study::StudyId(int id)
955 {
956   _errorCode = "";
957   _StudyId = id;
958 }
959
960 void SALOMEDSImpl_Study::UpdateIORLabelMap(const string& anIOR,const string& anEntry)
961 {
962   _errorCode = "";
963   DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
964   if (myIORLabels.find(anIOR) != myIORLabels.end()) myIORLabels.erase(anIOR);
965   myIORLabels[anIOR] = aLabel;
966 }
967
968 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel)
969 {
970   SALOMEDSImpl_StudyHandle* Att;
971   if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
972     return Att->Get();
973   }
974   return NULL;
975 }
976
977 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
978 {
979   return GetStudy(theLabel)->GetSObject(theLabel);
980 }
981
982 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
983 {
984   return GetStudy(theLabel)->GetSComponent(theLabel);
985 }
986
987
988 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
989 {
990   string aString = theAttribute->Label().Entry();
991   GetStudy(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
992 }
993
994 vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
995 {
996   _errorCode = "";
997   vector<SALOMEDSImpl_SObject> aSeq;
998
999   SALOMEDSImpl_AttributeTarget* aTarget;
1000   if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
1001     return aTarget->Get();
1002   }
1003
1004   return aSeq;
1005 }
1006
1007
1008 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
1009 {
1010   _errorCode = "";
1011   return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
1012 }
1013
1014 string SALOMEDSImpl_Study::GetLastModificationDate()
1015 {
1016   _errorCode = "";
1017   SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1018
1019   vector<string> aNames;
1020   vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1021   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1022
1023   int aLastIndex = aNames.size()-1;
1024   char aResult[20];
1025   sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1026           (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
1027           (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
1028   string aResStr (aResult);
1029   return aResStr;
1030 }
1031
1032 vector<string> SALOMEDSImpl_Study::GetModificationsDate()
1033 {
1034   _errorCode = "";
1035   SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1036
1037   vector<string> aNames;
1038   vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1039   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1040
1041   int anIndex, aLength = aNames.size();
1042   vector<string> aDates;
1043
1044   for (anIndex = 1; anIndex < aLength; anIndex++) {
1045     char aDate[20];
1046     sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1047             (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
1048             (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
1049     aDates.push_back(aDate);
1050   }
1051   return aDates;
1052 }
1053
1054
1055
1056 //============================================================================
1057 /*! Function : GetUseCaseBuilder
1058  *  Purpose  : Returns a UseCase builder
1059  */
1060 //============================================================================
1061 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
1062 {
1063   _errorCode = "";
1064   return _useCaseBuilder;
1065 }
1066
1067
1068 //============================================================================
1069 /*! Function : Close
1070  *  Purpose  :
1071  */
1072 //============================================================================
1073 void SALOMEDSImpl_Study::Close()
1074 {
1075   _errorCode = "";
1076   _doc->GetApplication()->Close(_doc);
1077   _doc = NULL;
1078   _mapOfSO.clear();
1079   _mapOfSCO.clear();
1080 }
1081
1082
1083 //============================================================================
1084 /*! Function : GetSComponent
1085  *  Purpose  :
1086  */
1087 //============================================================================
1088 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const string& theEntry)
1089 {
1090   SALOMEDSImpl_SComponent aSCO;
1091   if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
1092     aSCO = _mapOfSCO[theEntry];
1093   else {
1094     DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1095     aSCO = SALOMEDSImpl_SComponent(aLabel);
1096     _mapOfSCO[theEntry] = aSCO;
1097   }
1098
1099   return aSCO;
1100 }
1101
1102 //============================================================================
1103 /*! Function : GetSComponent
1104  *  Purpose  :
1105  */
1106 //============================================================================
1107 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
1108 {
1109   return SALOMEDSImpl_SComponent(theLabel);
1110 }
1111
1112 //============================================================================
1113 /*! Function : GetSObject
1114  *  Purpose  :
1115  */
1116 //============================================================================
1117 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const string& theEntry)
1118 {
1119   SALOMEDSImpl_SObject aSO;
1120   if(_mapOfSO.find(theEntry) != _mapOfSO.end())
1121     aSO = _mapOfSO[theEntry];
1122   else {
1123     DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1124     aSO = SALOMEDSImpl_SObject(aLabel);
1125     _mapOfSO[theEntry] = aSO;
1126   }
1127
1128   return aSO;
1129 }
1130
1131 //============================================================================
1132 /*! Function : GetSObject
1133  *  Purpose  :
1134  */
1135 //============================================================================
1136 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
1137 {
1138   return SALOMEDSImpl_SObject(theLabel);
1139 }
1140
1141 //============================================================================
1142 /*! Function : GetAttribute
1143  *  Purpose  :
1144  */
1145 //============================================================================
1146 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const string& theEntry,
1147                                                const string& theType)
1148 {
1149   SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
1150   DF_Attribute* anAttr;
1151   aSO.FindAttribute(anAttr, theType);
1152   return anAttr;
1153 }
1154
1155 //============================================================================
1156 /*! Function : DumpStudy
1157  *  Purpose  :
1158  */
1159 //============================================================================
1160 bool SALOMEDSImpl_Study::DumpStudy(const string& thePath,
1161                                    const string& theBaseName,
1162                                    bool isPublished,
1163                                    SALOMEDSImpl_DriverFactory* theFactory)
1164 {
1165   _errorCode = "";
1166
1167   if(theFactory == NULL) {
1168     _errorCode = "Null factory for creation of Engines";
1169     return false;
1170   }
1171
1172   vector<string> aSeq;
1173   string aCompType, aFactoryType;
1174
1175   //Build a list of all components in the Study
1176   SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
1177
1178   for (; itcomponent.More(); itcomponent.Next()) {
1179     SALOMEDSImpl_SComponent sco = itcomponent.Value();
1180     aCompType = sco.ComponentDataType();
1181     //GEOM and MED are independent components
1182     if (aCompType == "GEOM" || aCompType == "MED")
1183       aSeq.insert(aSeq.begin(), aCompType);
1184     else
1185       aSeq.push_back(aCompType);
1186   }
1187
1188 #ifdef WIN32
1189   string aFileName =
1190     thePath + string("\\") + theBaseName + string(".py");
1191 #else
1192   string aFileName =
1193     thePath + string("/")  + theBaseName + string(".py");
1194 #endif
1195
1196   //Create a file that will contain a main Study script
1197   fstream fp;
1198   fp.open(aFileName.c_str(), ios::out);
1199
1200 #ifdef WIN32
1201   bool isOpened = fp.is_open();
1202 #else
1203   bool isOpened = fp.rdbuf()->is_open();
1204 #endif
1205
1206   if(!isOpened) {
1207     _errorCode = string("Can't create a file ")+aFileName;
1208     return false;
1209   }
1210
1211   string aBatchModeScript = "salome";
1212
1213   //Output to the main Study script required Python modules import,
1214   //set sys.path and add a creation of the study.
1215   fp << GetDumpStudyComment() << endl << endl;
1216   fp << "import sys" << endl;
1217   fp << "import " << aBatchModeScript << endl << endl;
1218
1219   fp << aBatchModeScript << ".salome_init()" << endl << endl;
1220
1221   fp << _GetNoteBookAccess();
1222
1223   fp << "sys.path.insert( 0, r\'" << thePath << "\')" << endl << endl;
1224
1225   //Dump NoteBook Variables
1226   fp << _GetStudyVariablesScript();
1227
1228   //Check if it's necessary to dump visual parameters
1229   bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
1230   int lastSavePoint = -1;
1231   if(isDumpVisuals) {
1232     lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
1233     if(lastSavePoint > 0) {
1234       fp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << endl << endl;
1235     }
1236   }
1237   
1238
1239   vector<string> aSeqOfFileNames;
1240
1241   //Iterate all components and create the componponents specific scripts.
1242   bool isOk = true;
1243   int aLength = aSeq.size();
1244   for(int i = 1; i <= aLength; i++) {
1245
1246     aCompType = aSeq[i-1];
1247     SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
1248     SALOMEDSImpl_Driver* aDriver = NULL;
1249     // if there is an associated Engine call its method for saving
1250     string IOREngine;
1251     try {
1252       if (!sco.ComponentIOR(IOREngine)) {
1253         if (!aCompType.empty()) {
1254
1255           aDriver = theFactory->GetDriverByType(aCompType);
1256
1257           if (aDriver != NULL) {
1258             SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
1259             if(!SB->LoadWith(sco, aDriver)) {
1260               _errorCode = SB->GetErrorCode();
1261               return false;
1262             }
1263           }
1264           else continue;
1265         }
1266       }
1267       else {
1268         aDriver = theFactory->GetDriverByIOR(IOREngine);
1269       }
1270     } catch(...) {
1271       _errorCode = "Can not restore information to dump it";
1272       return false;
1273     }
1274
1275     if(aDriver == NULL) continue;
1276
1277     bool isValidScript;
1278     long aStreamLength  = 0;
1279     SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(this, isPublished, isValidScript, aStreamLength);
1280     if ( !isValidScript )
1281       isOk = false;
1282
1283     //Create a file that will contain the component specific script
1284     fstream fp2;
1285 #ifdef WIN32
1286     aFileName=thePath+string("\\");
1287 #else
1288     aFileName=thePath+string("/");
1289 #endif
1290     string aScriptName;
1291     aScriptName += theBaseName;
1292     aScriptName += "_";
1293     aScriptName += aCompType;
1294
1295     aFileName += aScriptName+ string(".py");
1296     aSeqOfFileNames.push_back(aFileName);
1297
1298     fp2.open(aFileName.c_str(), ios::out);
1299
1300 #ifdef WIN32
1301     isOpened = fp2.is_open();
1302 #else
1303     isOpened = fp2.rdbuf()->is_open();
1304 #endif
1305
1306     if(!isOpened) {
1307       _errorCode = string("Can't create a file ")+aFileName;
1308       SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
1309       return false;
1310     }
1311
1312     //Output the Python script generated by the component in the newly created file.
1313     fp2 << aStream->Data();
1314     fp2.close();
1315
1316     if(aStream) delete aStream;
1317
1318     //Add to the main script a call to RebuildData of the generated by the component the Python script
1319     fp << "import " << aScriptName << endl;
1320     fp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << endl;
1321   }
1322
1323   fp << endl;
1324   fp << "if salome.sg.hasDesktop():" << endl;
1325   fp << "\tsalome.sg.updateObjBrowser(1)" << endl;
1326
1327   if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
1328     fp << "\tiparameters.getSession().restoreVisualState(1)" << endl;
1329   }
1330
1331
1332   fp.close();
1333   return isOk;
1334 }
1335
1336 //=======================================================================
1337 //function : GetDumpStudyComment
1338 //purpose  : return a header comment for a DumpStudy script
1339 //=======================================================================
1340
1341 string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
1342 {
1343   string txt
1344     ("### This file is generated by SALOME automatically by dump python functionality");
1345   if ( theComponentName )
1346     txt += string(" of ") + (char*) theComponentName + " component";
1347   return txt;
1348 }
1349
1350 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1351             fstream& fp,
1352             const string& Tab,
1353             SALOMEDSImpl_Study* theStudy);
1354
1355 //============================================================================
1356 /*! Function : dump
1357  *  Purpose  :
1358  */
1359 //============================================================================
1360 void SALOMEDSImpl_Study::dump(const string& theFileName)
1361 {
1362   //Create a file that will contain a main Study script
1363   fstream fp;
1364   fp.open(theFileName.c_str(), ios::out);
1365
1366 #ifdef WIN32
1367   bool isOpened = fp.is_open();
1368 #else
1369   bool isOpened = fp.rdbuf()->is_open();
1370 #endif
1371
1372   if(!isOpened) {
1373     _errorCode = string("Can't create a file ")+theFileName;
1374     cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << endl;
1375     return;
1376   }
1377
1378   SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
1379   fp << "0:1" << endl;
1380   SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
1381   string aTab("   ");
1382   for(; Itr.More(); Itr.Next()) {
1383     dumpSO(Itr.Value(), fp, aTab, this);
1384   }
1385
1386   fp.close();
1387 }
1388
1389
1390 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1391             fstream& fp,
1392             const string& Tab,
1393             SALOMEDSImpl_Study* theStudy)
1394 {
1395   string aTab(Tab), anID(theSO.GetID());
1396   fp << aTab << anID << endl;
1397   vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
1398   for(int i = 0; i<attribs.size(); i++) {
1399     SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
1400
1401     if(!anAttr) {
1402       continue;
1403     }
1404
1405     string aType = anAttr->GetClassType();
1406     fp << Tab << "  -- " << aType;
1407
1408     if(aType == string("AttributeReal")) {
1409       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
1410     }
1411     else if(aType == string("AttributeInteger")) {
1412       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
1413     }
1414     else if(aType ==  string("AttributeName")) {
1415       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
1416     }
1417     else if(aType == string("AttributeComment")) {
1418       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
1419     }
1420     else if(aType == string("AttributeReference")) {
1421       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
1422     }
1423     fp << endl;
1424   }
1425
1426   SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
1427   string aNewTab("   ");
1428   aNewTab+=aTab;
1429   for(; Itr.More(); Itr.Next()) {
1430     dumpSO(Itr.Value(), fp, aNewTab, theStudy);
1431   }
1432
1433   return;
1434 }
1435
1436 void SALOMEDSImpl_Study::Modify()
1437 {
1438   _errorCode = "";
1439   _doc->SetModified(true);
1440 }
1441
1442 //============================================================================
1443 /*! Function : 
1444  *  Purpose  :
1445  */
1446 //============================================================================
1447 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
1448 {
1449   if (theSavePoint < 0) return NULL;
1450   SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1451   SALOMEDSImpl_SObject so = FindComponent((char*)theID);
1452   if (!so) so = builder->NewComponent((char*)theID);
1453   SALOMEDSImpl_AttributeParameter* attParam = NULL;
1454
1455   if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
1456     DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
1457     if ( !savePointLabel.IsNull() )
1458       so = GetSObject( savePointLabel );
1459     else // ... if it does not exist - create a new one
1460       so = builder->NewObjectToTag( so, theSavePoint );
1461   }
1462
1463   DF_Attribute* A;
1464   if (so) {
1465     builder->FindAttribute(so, A, "AttributeParameter");
1466     if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
1467       A = builder->FindOrCreateAttribute(so, "AttributeParameter"); 
1468     }
1469     attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
1470   }
1471   return attParam;
1472 }
1473
1474 //============================================================================
1475 /*! Function : 
1476  *  Purpose  :
1477  */
1478 //============================================================================
1479 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID, 
1480                                                                          const char* theModuleName,
1481                                                                          int theSavePoint)
1482 {
1483   if(theSavePoint <= 0) return NULL;
1484   SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
1485   SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
1486   SALOMEDSImpl_AttributeParameter* par = NULL;
1487
1488   SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
1489   string moduleName(theModuleName);
1490   for(; it.More(); it.Next()) {
1491     SALOMEDSImpl_SObject so(it.Value());
1492     if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
1493       if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
1494       if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
1495     }
1496   }
1497
1498   SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1499   SALOMEDSImpl_SObject so = builder->NewObject(main_so);
1500   par  = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
1501   par->SetString("AP_MODULE_NAME", moduleName);
1502   return par;
1503 }
1504
1505 //============================================================================
1506 /*! Function : SetStudyLock
1507  *  Purpose  :
1508  */
1509 //============================================================================
1510 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
1511 {
1512   _lockers.push_back(theLockerID);
1513 }
1514
1515 //============================================================================
1516 /*! Function : IsStudyLocked
1517  *  Purpose  :
1518  */
1519 //============================================================================
1520 bool SALOMEDSImpl_Study::IsStudyLocked()
1521 {
1522   return (_lockers.size() > 0);
1523 }
1524
1525 //============================================================================
1526 /*! Function : UnLockStudy
1527  *  Purpose  :
1528  */
1529 //============================================================================
1530 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
1531 {
1532   vector<string>::iterator vsI = _lockers.begin();
1533   int length = _lockers.size();
1534   bool isFound = false;
1535   string id(theLockerID);
1536   for(int i = 0; i<length; i++, vsI++) {
1537     if(id == _lockers[i]) {
1538       isFound = true;;
1539       break;
1540     }
1541   }
1542   if(isFound) _lockers.erase(vsI);
1543 }
1544   
1545 //============================================================================
1546 /*! Function : GetLockerID
1547  *  Purpose  :
1548  */
1549 //============================================================================
1550 vector<string> SALOMEDSImpl_Study::GetLockerID()
1551 {
1552   return _lockers;
1553 }
1554
1555 //============================================================================
1556 /*! Function : SetVariable
1557  *  Purpose  :
1558  */
1559 //============================================================================
1560 void SALOMEDSImpl_Study::SetVariable(const string& theVarName,
1561                                      const double theValue,
1562                                      const SALOMEDSImpl_GenericVariable::VariableTypes theType)
1563 {
1564   bool modified = false;
1565   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1566   
1567   if( aGVar == NULL ) {
1568
1569     SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
1570
1571     aSVar->setValue(theValue);
1572     myNoteBookVars.push_back(aSVar);
1573     modified = true;
1574   }
1575   else {
1576     if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
1577       modified = aSVar->setValue(theValue) || modified;
1578       modified = aSVar->setType(theType) || modified;
1579     }
1580   }
1581   if(modified)
1582     Modify();
1583 }
1584
1585 //============================================================================
1586 /*! Function : GetReal
1587  *  Purpose  :
1588  */
1589 //============================================================================
1590 double SALOMEDSImpl_Study::GetVariableValue(const string& theVarName)
1591 {
1592   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1593   
1594   if(aGVar != NULL )
1595     if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
1596       return aSVar->getValue();
1597
1598   return 0;
1599 }
1600
1601 //============================================================================
1602 /*! Function : IsTypeOf
1603  *  Purpose  :
1604  */
1605 //============================================================================
1606 bool SALOMEDSImpl_Study::IsTypeOf(const string& theVarName,
1607                                   SALOMEDSImpl_GenericVariable::
1608                                   VariableTypes theType) const
1609 {
1610   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1611   
1612   if(aGVar != NULL )
1613     return aGVar->Type() == theType;
1614   
1615   return false;
1616 }
1617
1618 //============================================================================
1619 /*! Function : IsVariable
1620  *  Purpose  :
1621  */
1622 //============================================================================
1623 bool SALOMEDSImpl_Study::IsVariable(const string& theVarName) const
1624 {
1625   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1626   return (aGVar != NULL);
1627 }
1628
1629 //============================================================================
1630 /*! Function : GetVariableNames
1631  *  Purpose  :
1632  */
1633 //============================================================================
1634 vector<string> SALOMEDSImpl_Study::GetVariableNames() const
1635 {
1636   vector<string> aResult;
1637
1638   for(int i = 0; i < myNoteBookVars.size(); i++)
1639     aResult.push_back(myNoteBookVars[i]->Name());
1640   
1641   return aResult;
1642 }
1643
1644 //============================================================================
1645 /*! Function : AddVariable
1646  *  Purpose  :
1647  */
1648 //============================================================================
1649 void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
1650 {
1651   myNoteBookVars.push_back(theVariable);
1652 }
1653
1654 //============================================================================
1655 /*! Function : AddVariable
1656  *  Purpose  :
1657  */
1658 //============================================================================
1659 SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
1660 {
1661   SALOMEDSImpl_GenericVariable* aResult = NULL;
1662   for(int i = 0; i < myNoteBookVars.size();i++) {
1663     if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
1664       aResult = myNoteBookVars[i];
1665       break;
1666     }  
1667   }
1668   return aResult;
1669 }
1670
1671 //============================================================================
1672 /*! Function : RemoveVariable
1673  *  Purpose  :
1674  */
1675 //============================================================================
1676 bool SALOMEDSImpl_Study::RemoveVariable(const string& theVarName)
1677 {
1678   SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
1679   if( !aVariable )
1680     return false;
1681
1682   string aValue = aVariable->SaveToScript();
1683   ReplaceVariableAttribute( theVarName, aValue );
1684
1685   std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
1686   for( ; it != itEnd; it++ )
1687   {
1688     SALOMEDSImpl_GenericVariable* aVariableRef = *it;
1689     if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
1690     {
1691       myNoteBookVars.erase( it );
1692       Modify();
1693       break;
1694     }
1695   }
1696
1697   return true;
1698 }
1699
1700 //============================================================================
1701 /*! Function : RenameVariable
1702  *  Purpose  :
1703  */
1704 //============================================================================
1705 bool SALOMEDSImpl_Study::RenameVariable(const string& theVarName, const string& theNewVarName)
1706 {
1707   SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
1708   if( !aVariable )
1709     return false;
1710
1711   ReplaceVariableAttribute( theVarName, theNewVarName );
1712
1713   std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
1714   for( ; it != itEnd; it++ )
1715   {
1716     SALOMEDSImpl_GenericVariable* aVariableRef = *it;
1717     if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
1718     {
1719       aVariableRef->setName( theNewVarName );
1720       Modify();
1721       break;
1722     }
1723   }
1724
1725   return true;
1726 }
1727
1728 //============================================================================
1729 /*! Function : IsVariableUsed
1730  *  Purpose  :
1731  */
1732 //============================================================================
1733 bool SALOMEDSImpl_Study::IsVariableUsed(const string& theVarName)
1734 {
1735   return FindVariableAttribute( theVarName );
1736 }
1737
1738 //============================================================================
1739 /*! Function : FindVariableAttribute
1740  *  Purpose  :
1741  */
1742 //============================================================================
1743 bool SALOMEDSImpl_Study::FindVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
1744                                                SALOMEDSImpl_SObject theSObject,
1745                                                const std::string& theName)
1746 {
1747   SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
1748   for( ; anIter.More(); anIter.Next() )
1749     if( FindVariableAttribute( theStudyBuilder, anIter.Value(), theName ) )
1750       return true;
1751
1752   DF_Attribute* anAttr;
1753   if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
1754   {
1755     if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
1756     {
1757       string aString = aStringAttr->Value();
1758
1759       vector< vector<string> > aSections = ParseVariables( aString );
1760       for( int i = 0, n = aSections.size(); i < n; i++ )
1761       {
1762         vector<string> aVector = aSections[i];
1763         for( int j = 0, m = aVector.size(); j < m; j++ )
1764         {
1765           string aStr = aVector[j];
1766           if( aStr.compare( theName ) == 0 )
1767             return true;
1768         }
1769       }
1770     }
1771   }
1772   return false;
1773 }
1774
1775 //============================================================================
1776 /*! Function : FindVariableAttribute
1777  *  Purpose  :
1778  */
1779 //============================================================================
1780 bool SALOMEDSImpl_Study::FindVariableAttribute(const std::string& theName)
1781 {
1782   SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
1783   SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
1784   for( ; aCompIter.More(); aCompIter.Next() )
1785   {
1786     SALOMEDSImpl_SObject aComp = aCompIter.Value();
1787     if( FindVariableAttribute( aStudyBuilder, aComp, theName ) )
1788       return true;
1789   }
1790   return false;
1791 }
1792
1793 //============================================================================
1794 /*! Function : ReplaceVariableAttribute
1795  *  Purpose  :
1796  */
1797 //============================================================================
1798 void SALOMEDSImpl_Study::ReplaceVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
1799                                                   SALOMEDSImpl_SObject theSObject,
1800                                                   const std::string& theSource,
1801                                                   const std::string& theDest)
1802 {
1803   SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
1804   for( ; anIter.More(); anIter.Next() )
1805     ReplaceVariableAttribute( theStudyBuilder, anIter.Value(), theSource, theDest );
1806
1807   DF_Attribute* anAttr;
1808   if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
1809   {
1810     if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
1811     {
1812       bool isChanged = false;
1813       string aNewString, aCurrentString = aStringAttr->Value();
1814
1815       vector< vector<string> > aSections = ParseVariables( aCurrentString );
1816       for( int i = 0, n = aSections.size(); i < n; i++ )
1817       {
1818         vector<string> aVector = aSections[i];
1819         for( int j = 0, m = aVector.size(); j < m; j++ )
1820         {
1821           string aStr = aVector[j];
1822           if( aStr.compare( theSource ) == 0 )
1823           {
1824             isChanged = true;
1825             aStr = theDest;
1826           }
1827
1828           aNewString.append( aStr );
1829           if( j != m - 1 )
1830             aNewString.append( ":" );
1831         }
1832         if( i != n - 1 )
1833           aNewString.append( "|" );
1834       }
1835
1836       if( isChanged )
1837         aStringAttr->SetValue( aNewString );
1838     }
1839   }
1840 }
1841
1842 //============================================================================
1843 /*! Function : ReplaceVariableAttribute
1844  *  Purpose  :
1845  */
1846 //============================================================================
1847 void SALOMEDSImpl_Study::ReplaceVariableAttribute(const std::string& theSource, const std::string& theDest)
1848 {
1849   SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
1850   SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
1851   for( ; aCompIter.More(); aCompIter.Next() )
1852   {
1853     SALOMEDSImpl_SObject aComp = aCompIter.Value();
1854     ReplaceVariableAttribute( aStudyBuilder, aComp, theSource, theDest );
1855   }
1856 }
1857
1858 //============================================================================
1859 /*! Function : ParseVariables
1860  *  Purpose  :
1861  */
1862 //============================================================================
1863 vector< vector< string > > SALOMEDSImpl_Study::ParseVariables(const string& theVariables) const
1864 {
1865   return SALOMEDSImpl_Tool::splitStringWithEmpty( theVariables, OPERATION_SEPARATOR, VARIABLE_SEPARATOR );
1866 }
1867
1868 //============================================================================
1869 /*! Function : EnableUseCaseAutoFilling
1870  *  Purpose  :
1871  */
1872 //============================================================================
1873 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
1874
1875   _errorCode = ""; _autoFill = isEnabled; 
1876   if(isEnabled) {
1877     _builder->SetOnAddSObject(_cb);
1878     _builder->SetOnRemoveSObject(_cb);
1879   }
1880   else {
1881     _builder->SetOnAddSObject(NULL);
1882     _builder->SetOnRemoveSObject(NULL);
1883   }
1884 }
1885
1886 //============================================================================
1887 /*! Function : GetIORs
1888  *  Purpose  :
1889  */
1890 //============================================================================
1891 vector<string> SALOMEDSImpl_Study::GetIORs()
1892 {
1893   vector<string> anIORs;
1894   map<string, DF_Label>::const_iterator MI;
1895   for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
1896     anIORs.push_back(MI->first);
1897
1898   return anIORs;
1899 }