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