Salome HOME
CCAR: remove more memory leaks in SALOMEDS
[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 void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR)
969 {
970   if (myIORLabels.find(anIOR) != myIORLabels.end()) 
971     {
972       //remove the ior entry
973       myIORLabels.erase(anIOR);
974     }
975 }
976
977 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel)
978 {
979   SALOMEDSImpl_StudyHandle* Att;
980   if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
981     return Att->Get();
982   }
983   return NULL;
984 }
985
986 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
987 {
988   return GetStudy(theLabel)->GetSObject(theLabel);
989 }
990
991 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
992 {
993   return GetStudy(theLabel)->GetSComponent(theLabel);
994 }
995
996
997 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
998 {
999   string aString = theAttribute->Label().Entry();
1000   GetStudy(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
1001 }
1002
1003 vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
1004 {
1005   _errorCode = "";
1006   vector<SALOMEDSImpl_SObject> aSeq;
1007
1008   SALOMEDSImpl_AttributeTarget* aTarget;
1009   if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
1010     return aTarget->Get();
1011   }
1012
1013   return aSeq;
1014 }
1015
1016
1017 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
1018 {
1019   _errorCode = "";
1020   return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
1021 }
1022
1023 string SALOMEDSImpl_Study::GetLastModificationDate()
1024 {
1025   _errorCode = "";
1026   SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1027
1028   vector<string> aNames;
1029   vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1030   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1031
1032   int aLastIndex = aNames.size()-1;
1033   char aResult[20];
1034   sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1035           (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
1036           (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
1037   string aResStr (aResult);
1038   return aResStr;
1039 }
1040
1041 vector<string> SALOMEDSImpl_Study::GetModificationsDate()
1042 {
1043   _errorCode = "";
1044   SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
1045
1046   vector<string> aNames;
1047   vector<int> aMinutes, aHours, aDays, aMonths, aYears;
1048   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
1049
1050   int anIndex, aLength = aNames.size();
1051   vector<string> aDates;
1052
1053   for (anIndex = 1; anIndex < aLength; anIndex++) {
1054     char aDate[20];
1055     sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1056             (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
1057             (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
1058     aDates.push_back(aDate);
1059   }
1060   return aDates;
1061 }
1062
1063
1064
1065 //============================================================================
1066 /*! Function : GetUseCaseBuilder
1067  *  Purpose  : Returns a UseCase builder
1068  */
1069 //============================================================================
1070 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
1071 {
1072   _errorCode = "";
1073   return _useCaseBuilder;
1074 }
1075
1076
1077 //============================================================================
1078 /*! Function : Close
1079  *  Purpose  :
1080  */
1081 //============================================================================
1082 void SALOMEDSImpl_Study::Close()
1083 {
1084   _errorCode = "";
1085   _doc->GetApplication()->Close(_doc);
1086   _doc = NULL;
1087   _mapOfSO.clear();
1088   _mapOfSCO.clear();
1089 }
1090
1091
1092 //============================================================================
1093 /*! Function : GetSComponent
1094  *  Purpose  :
1095  */
1096 //============================================================================
1097 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const string& theEntry)
1098 {
1099   SALOMEDSImpl_SComponent aSCO;
1100   if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
1101     aSCO = _mapOfSCO[theEntry];
1102   else {
1103     DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1104     aSCO = SALOMEDSImpl_SComponent(aLabel);
1105     _mapOfSCO[theEntry] = aSCO;
1106   }
1107
1108   return aSCO;
1109 }
1110
1111 //============================================================================
1112 /*! Function : GetSComponent
1113  *  Purpose  :
1114  */
1115 //============================================================================
1116 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
1117 {
1118   return SALOMEDSImpl_SComponent(theLabel);
1119 }
1120
1121 //============================================================================
1122 /*! Function : GetSObject
1123  *  Purpose  :
1124  */
1125 //============================================================================
1126 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const string& theEntry)
1127 {
1128   SALOMEDSImpl_SObject aSO;
1129   if(_mapOfSO.find(theEntry) != _mapOfSO.end())
1130     aSO = _mapOfSO[theEntry];
1131   else {
1132     DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1133     aSO = SALOMEDSImpl_SObject(aLabel);
1134     _mapOfSO[theEntry] = aSO;
1135   }
1136
1137   return aSO;
1138 }
1139
1140 //============================================================================
1141 /*! Function : GetSObject
1142  *  Purpose  :
1143  */
1144 //============================================================================
1145 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
1146 {
1147   return SALOMEDSImpl_SObject(theLabel);
1148 }
1149
1150 //============================================================================
1151 /*! Function : GetAttribute
1152  *  Purpose  :
1153  */
1154 //============================================================================
1155 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const string& theEntry,
1156                                                const string& theType)
1157 {
1158   SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
1159   DF_Attribute* anAttr;
1160   aSO.FindAttribute(anAttr, theType);
1161   return anAttr;
1162 }
1163
1164 //============================================================================
1165 /*! Function : DumpStudy
1166  *  Purpose  :
1167  */
1168 //============================================================================
1169 bool SALOMEDSImpl_Study::DumpStudy(const string& thePath,
1170                                    const string& theBaseName,
1171                                    bool isPublished,
1172                                    SALOMEDSImpl_DriverFactory* theFactory)
1173 {
1174   _errorCode = "";
1175
1176   if(theFactory == NULL) {
1177     _errorCode = "Null factory for creation of Engines";
1178     return false;
1179   }
1180
1181   vector<string> aSeq;
1182   string aCompType, aFactoryType;
1183
1184   //Build a list of all components in the Study
1185   SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
1186
1187   for (; itcomponent.More(); itcomponent.Next()) {
1188     SALOMEDSImpl_SComponent sco = itcomponent.Value();
1189     aCompType = sco.ComponentDataType();
1190     //GEOM and MED are independent components
1191     if (aCompType == "GEOM" || aCompType == "MED")
1192       aSeq.insert(aSeq.begin(), aCompType);
1193     else
1194       aSeq.push_back(aCompType);
1195   }
1196
1197 #ifdef WIN32
1198   string aFileName =
1199     thePath + string("\\") + theBaseName + string(".py");
1200 #else
1201   string aFileName =
1202     thePath + string("/")  + theBaseName + string(".py");
1203 #endif
1204
1205   //Create a file that will contain a main Study script
1206   fstream fp;
1207   fp.open(aFileName.c_str(), ios::out);
1208
1209 #ifdef WIN32
1210   bool isOpened = fp.is_open();
1211 #else
1212   bool isOpened = fp.rdbuf()->is_open();
1213 #endif
1214
1215   if(!isOpened) {
1216     _errorCode = string("Can't create a file ")+aFileName;
1217     return false;
1218   }
1219
1220   string aBatchModeScript = "salome";
1221
1222   //Output to the main Study script required Python modules import,
1223   //set sys.path and add a creation of the study.
1224   fp << GetDumpStudyComment() << endl << endl;
1225   fp << "import sys" << endl;
1226   fp << "import " << aBatchModeScript << endl << endl;
1227
1228   fp << aBatchModeScript << ".salome_init()" << endl << endl;
1229
1230   fp << _GetNoteBookAccess();
1231
1232   fp << "sys.path.insert( 0, r\'" << thePath << "\')" << endl << endl;
1233
1234   //Dump NoteBook Variables
1235   fp << _GetStudyVariablesScript();
1236
1237   //Check if it's necessary to dump visual parameters
1238   bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
1239   int lastSavePoint = -1;
1240   if(isDumpVisuals) {
1241     lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
1242     if(lastSavePoint > 0) {
1243       fp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << endl << endl;
1244     }
1245   }
1246   
1247
1248   vector<string> aSeqOfFileNames;
1249
1250   //Iterate all components and create the componponents specific scripts.
1251   bool isOk = true;
1252   int aLength = aSeq.size();
1253   for(int i = 1; i <= aLength; i++) {
1254
1255     aCompType = aSeq[i-1];
1256     SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
1257     SALOMEDSImpl_Driver* aDriver = NULL;
1258     // if there is an associated Engine call its method for saving
1259     string IOREngine;
1260     try {
1261       if (!sco.ComponentIOR(IOREngine)) {
1262         if (!aCompType.empty()) {
1263
1264           aDriver = theFactory->GetDriverByType(aCompType);
1265
1266           if (aDriver != NULL) {
1267             SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
1268             if(!SB->LoadWith(sco, aDriver)) {
1269               _errorCode = SB->GetErrorCode();
1270               return false;
1271             }
1272           }
1273           else continue;
1274         }
1275       }
1276       else {
1277         aDriver = theFactory->GetDriverByIOR(IOREngine);
1278       }
1279     } catch(...) {
1280       _errorCode = "Can not restore information to dump it";
1281       return false;
1282     }
1283
1284     if(aDriver == NULL) continue;
1285
1286     bool isValidScript;
1287     long aStreamLength  = 0;
1288     SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(this, isPublished, isValidScript, aStreamLength);
1289     if ( !isValidScript )
1290       isOk = false;
1291
1292     //Create a file that will contain the component specific script
1293     fstream fp2;
1294 #ifdef WIN32
1295     aFileName=thePath+string("\\");
1296 #else
1297     aFileName=thePath+string("/");
1298 #endif
1299     string aScriptName;
1300     aScriptName += theBaseName;
1301     aScriptName += "_";
1302     aScriptName += aCompType;
1303
1304     aFileName += aScriptName+ string(".py");
1305     aSeqOfFileNames.push_back(aFileName);
1306
1307     fp2.open(aFileName.c_str(), ios::out);
1308
1309 #ifdef WIN32
1310     isOpened = fp2.is_open();
1311 #else
1312     isOpened = fp2.rdbuf()->is_open();
1313 #endif
1314
1315     if(!isOpened) {
1316       _errorCode = string("Can't create a file ")+aFileName;
1317       SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
1318       return false;
1319     }
1320
1321     //Output the Python script generated by the component in the newly created file.
1322     fp2 << aStream->Data();
1323     fp2.close();
1324
1325     if(aStream) delete aStream;
1326
1327     //Add to the main script a call to RebuildData of the generated by the component the Python script
1328     fp << "import " << aScriptName << endl;
1329     fp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << endl;
1330   }
1331
1332   fp << endl;
1333   fp << "if salome.sg.hasDesktop():" << endl;
1334   fp << "\tsalome.sg.updateObjBrowser(1)" << endl;
1335
1336   if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
1337     fp << "\tiparameters.getSession().restoreVisualState(1)" << endl;
1338   }
1339
1340
1341   fp.close();
1342   return isOk;
1343 }
1344
1345 //=======================================================================
1346 //function : GetDumpStudyComment
1347 //purpose  : return a header comment for a DumpStudy script
1348 //=======================================================================
1349
1350 string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
1351 {
1352   string txt
1353     ("### This file is generated by SALOME automatically by dump python functionality");
1354   if ( theComponentName )
1355     txt += string(" of ") + (char*) theComponentName + " component";
1356   return txt;
1357 }
1358
1359 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1360             fstream& fp,
1361             const string& Tab,
1362             SALOMEDSImpl_Study* theStudy);
1363
1364 //============================================================================
1365 /*! Function : dump
1366  *  Purpose  :
1367  */
1368 //============================================================================
1369 void SALOMEDSImpl_Study::dump(const string& theFileName)
1370 {
1371   //Create a file that will contain a main Study script
1372   fstream fp;
1373   fp.open(theFileName.c_str(), ios::out);
1374
1375 #ifdef WIN32
1376   bool isOpened = fp.is_open();
1377 #else
1378   bool isOpened = fp.rdbuf()->is_open();
1379 #endif
1380
1381   if(!isOpened) {
1382     _errorCode = string("Can't create a file ")+theFileName;
1383     cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << endl;
1384     return;
1385   }
1386
1387   SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
1388   fp << "0:1" << endl;
1389   SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
1390   string aTab("   ");
1391   for(; Itr.More(); Itr.Next()) {
1392     dumpSO(Itr.Value(), fp, aTab, this);
1393   }
1394
1395   fp.close();
1396 }
1397
1398
1399 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1400             fstream& fp,
1401             const string& Tab,
1402             SALOMEDSImpl_Study* theStudy)
1403 {
1404   string aTab(Tab), anID(theSO.GetID());
1405   fp << aTab << anID << endl;
1406   vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
1407   for(int i = 0; i<attribs.size(); i++) {
1408     SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
1409
1410     if(!anAttr) {
1411       continue;
1412     }
1413
1414     string aType = anAttr->GetClassType();
1415     fp << Tab << "  -- " << aType;
1416
1417     if(aType == string("AttributeReal")) {
1418       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
1419     }
1420     else if(aType == string("AttributeInteger")) {
1421       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
1422     }
1423     else if(aType ==  string("AttributeName")) {
1424       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
1425     }
1426     else if(aType == string("AttributeComment")) {
1427       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
1428     }
1429     else if(aType == string("AttributeReference")) {
1430       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
1431     }
1432     fp << endl;
1433   }
1434
1435   SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
1436   string aNewTab("   ");
1437   aNewTab+=aTab;
1438   for(; Itr.More(); Itr.Next()) {
1439     dumpSO(Itr.Value(), fp, aNewTab, theStudy);
1440   }
1441
1442   return;
1443 }
1444
1445 void SALOMEDSImpl_Study::Modify()
1446 {
1447   _errorCode = "";
1448   _doc->SetModified(true);
1449 }
1450
1451 //============================================================================
1452 /*! Function : 
1453  *  Purpose  :
1454  */
1455 //============================================================================
1456 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
1457 {
1458   if (theSavePoint < 0) return NULL;
1459   SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1460   SALOMEDSImpl_SObject so = FindComponent((char*)theID);
1461   if (!so) so = builder->NewComponent((char*)theID);
1462   SALOMEDSImpl_AttributeParameter* attParam = NULL;
1463
1464   if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
1465     DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
1466     if ( !savePointLabel.IsNull() )
1467       so = GetSObject( savePointLabel );
1468     else // ... if it does not exist - create a new one
1469       so = builder->NewObjectToTag( so, theSavePoint );
1470   }
1471
1472   DF_Attribute* A;
1473   if (so) {
1474     builder->FindAttribute(so, A, "AttributeParameter");
1475     if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
1476       A = builder->FindOrCreateAttribute(so, "AttributeParameter"); 
1477     }
1478     attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
1479   }
1480   return attParam;
1481 }
1482
1483 //============================================================================
1484 /*! Function : 
1485  *  Purpose  :
1486  */
1487 //============================================================================
1488 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID, 
1489                                                                          const char* theModuleName,
1490                                                                          int theSavePoint)
1491 {
1492   if(theSavePoint <= 0) return NULL;
1493   SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
1494   SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
1495   SALOMEDSImpl_AttributeParameter* par = NULL;
1496
1497   SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
1498   string moduleName(theModuleName);
1499   for(; it.More(); it.Next()) {
1500     SALOMEDSImpl_SObject so(it.Value());
1501     if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
1502       if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
1503       if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
1504     }
1505   }
1506
1507   SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1508   SALOMEDSImpl_SObject so = builder->NewObject(main_so);
1509   par  = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
1510   par->SetString("AP_MODULE_NAME", moduleName);
1511   return par;
1512 }
1513
1514 //============================================================================
1515 /*! Function : SetStudyLock
1516  *  Purpose  :
1517  */
1518 //============================================================================
1519 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
1520 {
1521   _lockers.push_back(theLockerID);
1522 }
1523
1524 //============================================================================
1525 /*! Function : IsStudyLocked
1526  *  Purpose  :
1527  */
1528 //============================================================================
1529 bool SALOMEDSImpl_Study::IsStudyLocked()
1530 {
1531   return (_lockers.size() > 0);
1532 }
1533
1534 //============================================================================
1535 /*! Function : UnLockStudy
1536  *  Purpose  :
1537  */
1538 //============================================================================
1539 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
1540 {
1541   vector<string>::iterator vsI = _lockers.begin();
1542   int length = _lockers.size();
1543   bool isFound = false;
1544   string id(theLockerID);
1545   for(int i = 0; i<length; i++, vsI++) {
1546     if(id == _lockers[i]) {
1547       isFound = true;;
1548       break;
1549     }
1550   }
1551   if(isFound) _lockers.erase(vsI);
1552 }
1553   
1554 //============================================================================
1555 /*! Function : GetLockerID
1556  *  Purpose  :
1557  */
1558 //============================================================================
1559 vector<string> SALOMEDSImpl_Study::GetLockerID()
1560 {
1561   return _lockers;
1562 }
1563
1564 //============================================================================
1565 /*! Function : SetVariable
1566  *  Purpose  :
1567  */
1568 //============================================================================
1569 void SALOMEDSImpl_Study::SetVariable(const string& theVarName,
1570                                      const double theValue,
1571                                      const SALOMEDSImpl_GenericVariable::VariableTypes theType)
1572 {
1573   bool modified = false;
1574   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1575   
1576   if( aGVar == NULL ) {
1577
1578     SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
1579
1580     aSVar->setValue(theValue);
1581     myNoteBookVars.push_back(aSVar);
1582     modified = true;
1583   }
1584   else {
1585     if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
1586       modified = aSVar->setValue(theValue) || modified;
1587       modified = aSVar->setType(theType) || modified;
1588     }
1589   }
1590   if(modified)
1591     Modify();
1592 }
1593
1594 //============================================================================
1595 /*! Function : GetReal
1596  *  Purpose  :
1597  */
1598 //============================================================================
1599 double SALOMEDSImpl_Study::GetVariableValue(const string& theVarName)
1600 {
1601   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1602   
1603   if(aGVar != NULL )
1604     if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
1605       return aSVar->getValue();
1606
1607   return 0;
1608 }
1609
1610 //============================================================================
1611 /*! Function : IsTypeOf
1612  *  Purpose  :
1613  */
1614 //============================================================================
1615 bool SALOMEDSImpl_Study::IsTypeOf(const string& theVarName,
1616                                   SALOMEDSImpl_GenericVariable::
1617                                   VariableTypes theType) const
1618 {
1619   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1620   
1621   if(aGVar != NULL )
1622     return aGVar->Type() == theType;
1623   
1624   return false;
1625 }
1626
1627 //============================================================================
1628 /*! Function : IsVariable
1629  *  Purpose  :
1630  */
1631 //============================================================================
1632 bool SALOMEDSImpl_Study::IsVariable(const string& theVarName) const
1633 {
1634   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
1635   return (aGVar != NULL);
1636 }
1637
1638 //============================================================================
1639 /*! Function : GetVariableNames
1640  *  Purpose  :
1641  */
1642 //============================================================================
1643 vector<string> SALOMEDSImpl_Study::GetVariableNames() const
1644 {
1645   vector<string> aResult;
1646
1647   for(int i = 0; i < myNoteBookVars.size(); i++)
1648     aResult.push_back(myNoteBookVars[i]->Name());
1649   
1650   return aResult;
1651 }
1652
1653 //============================================================================
1654 /*! Function : AddVariable
1655  *  Purpose  :
1656  */
1657 //============================================================================
1658 void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
1659 {
1660   myNoteBookVars.push_back(theVariable);
1661 }
1662
1663 //============================================================================
1664 /*! Function : AddVariable
1665  *  Purpose  :
1666  */
1667 //============================================================================
1668 SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
1669 {
1670   SALOMEDSImpl_GenericVariable* aResult = NULL;
1671   for(int i = 0; i < myNoteBookVars.size();i++) {
1672     if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
1673       aResult = myNoteBookVars[i];
1674       break;
1675     }  
1676   }
1677   return aResult;
1678 }
1679
1680 //============================================================================
1681 /*! Function : RemoveVariable
1682  *  Purpose  :
1683  */
1684 //============================================================================
1685 bool SALOMEDSImpl_Study::RemoveVariable(const string& theVarName)
1686 {
1687   SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
1688   if( !aVariable )
1689     return false;
1690
1691   string aValue = aVariable->SaveToScript();
1692   ReplaceVariableAttribute( theVarName, aValue );
1693
1694   std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
1695   for( ; it != itEnd; it++ )
1696   {
1697     SALOMEDSImpl_GenericVariable* aVariableRef = *it;
1698     if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
1699     {
1700       myNoteBookVars.erase( it );
1701       Modify();
1702       break;
1703     }
1704   }
1705
1706   return true;
1707 }
1708
1709 //============================================================================
1710 /*! Function : RenameVariable
1711  *  Purpose  :
1712  */
1713 //============================================================================
1714 bool SALOMEDSImpl_Study::RenameVariable(const string& theVarName, const string& theNewVarName)
1715 {
1716   SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
1717   if( !aVariable )
1718     return false;
1719
1720   ReplaceVariableAttribute( theVarName, theNewVarName );
1721
1722   std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
1723   for( ; it != itEnd; it++ )
1724   {
1725     SALOMEDSImpl_GenericVariable* aVariableRef = *it;
1726     if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
1727     {
1728       aVariableRef->setName( theNewVarName );
1729       Modify();
1730       break;
1731     }
1732   }
1733
1734   return true;
1735 }
1736
1737 //============================================================================
1738 /*! Function : IsVariableUsed
1739  *  Purpose  :
1740  */
1741 //============================================================================
1742 bool SALOMEDSImpl_Study::IsVariableUsed(const string& theVarName)
1743 {
1744   return FindVariableAttribute( theVarName );
1745 }
1746
1747 //============================================================================
1748 /*! Function : FindVariableAttribute
1749  *  Purpose  :
1750  */
1751 //============================================================================
1752 bool SALOMEDSImpl_Study::FindVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
1753                                                SALOMEDSImpl_SObject theSObject,
1754                                                const std::string& theName)
1755 {
1756   SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
1757   for( ; anIter.More(); anIter.Next() )
1758     if( FindVariableAttribute( theStudyBuilder, anIter.Value(), theName ) )
1759       return true;
1760
1761   DF_Attribute* anAttr;
1762   if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
1763   {
1764     if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
1765     {
1766       string aString = aStringAttr->Value();
1767
1768       vector< vector<string> > aSections = ParseVariables( aString );
1769       for( int i = 0, n = aSections.size(); i < n; i++ )
1770       {
1771         vector<string> aVector = aSections[i];
1772         for( int j = 0, m = aVector.size(); j < m; j++ )
1773         {
1774           string aStr = aVector[j];
1775           if( aStr.compare( theName ) == 0 )
1776             return true;
1777         }
1778       }
1779     }
1780   }
1781   return false;
1782 }
1783
1784 //============================================================================
1785 /*! Function : FindVariableAttribute
1786  *  Purpose  :
1787  */
1788 //============================================================================
1789 bool SALOMEDSImpl_Study::FindVariableAttribute(const std::string& theName)
1790 {
1791   SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
1792   SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
1793   for( ; aCompIter.More(); aCompIter.Next() )
1794   {
1795     SALOMEDSImpl_SObject aComp = aCompIter.Value();
1796     if( FindVariableAttribute( aStudyBuilder, aComp, theName ) )
1797       return true;
1798   }
1799   return false;
1800 }
1801
1802 //============================================================================
1803 /*! Function : ReplaceVariableAttribute
1804  *  Purpose  :
1805  */
1806 //============================================================================
1807 void SALOMEDSImpl_Study::ReplaceVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
1808                                                   SALOMEDSImpl_SObject theSObject,
1809                                                   const std::string& theSource,
1810                                                   const std::string& theDest)
1811 {
1812   SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
1813   for( ; anIter.More(); anIter.Next() )
1814     ReplaceVariableAttribute( theStudyBuilder, anIter.Value(), theSource, theDest );
1815
1816   DF_Attribute* anAttr;
1817   if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
1818   {
1819     if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
1820     {
1821       bool isChanged = false;
1822       string aNewString, aCurrentString = aStringAttr->Value();
1823
1824       vector< vector<string> > aSections = ParseVariables( aCurrentString );
1825       for( int i = 0, n = aSections.size(); i < n; i++ )
1826       {
1827         vector<string> aVector = aSections[i];
1828         for( int j = 0, m = aVector.size(); j < m; j++ )
1829         {
1830           string aStr = aVector[j];
1831           if( aStr.compare( theSource ) == 0 )
1832           {
1833             isChanged = true;
1834             aStr = theDest;
1835           }
1836
1837           aNewString.append( aStr );
1838           if( j != m - 1 )
1839             aNewString.append( ":" );
1840         }
1841         if( i != n - 1 )
1842           aNewString.append( "|" );
1843       }
1844
1845       if( isChanged )
1846         aStringAttr->SetValue( aNewString );
1847     }
1848   }
1849 }
1850
1851 //============================================================================
1852 /*! Function : ReplaceVariableAttribute
1853  *  Purpose  :
1854  */
1855 //============================================================================
1856 void SALOMEDSImpl_Study::ReplaceVariableAttribute(const std::string& theSource, const std::string& theDest)
1857 {
1858   SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
1859   SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
1860   for( ; aCompIter.More(); aCompIter.Next() )
1861   {
1862     SALOMEDSImpl_SObject aComp = aCompIter.Value();
1863     ReplaceVariableAttribute( aStudyBuilder, aComp, theSource, theDest );
1864   }
1865 }
1866
1867 //============================================================================
1868 /*! Function : ParseVariables
1869  *  Purpose  :
1870  */
1871 //============================================================================
1872 vector< vector< string > > SALOMEDSImpl_Study::ParseVariables(const string& theVariables) const
1873 {
1874   return SALOMEDSImpl_Tool::splitStringWithEmpty( theVariables, OPERATION_SEPARATOR, VARIABLE_SEPARATOR );
1875 }
1876
1877 //============================================================================
1878 /*! Function : EnableUseCaseAutoFilling
1879  *  Purpose  :
1880  */
1881 //============================================================================
1882 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
1883
1884   _errorCode = ""; _autoFill = isEnabled; 
1885   if(isEnabled) {
1886     _builder->SetOnAddSObject(_cb);
1887     _builder->SetOnRemoveSObject(_cb);
1888   }
1889   else {
1890     _builder->SetOnAddSObject(NULL);
1891     _builder->SetOnRemoveSObject(NULL);
1892   }
1893 }
1894
1895 //============================================================================
1896 /*! Function : GetIORs
1897  *  Purpose  :
1898  */
1899 //============================================================================
1900 vector<string> SALOMEDSImpl_Study::GetIORs()
1901 {
1902   vector<string> anIORs;
1903   map<string, DF_Label>::const_iterator MI;
1904   for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
1905     anIORs.push_back(MI->first);
1906
1907   return anIORs;
1908 }