]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDSImpl/SALOMEDSImpl_StudyBuilder.cxx
Salome HOME
Use the prefix std:: instead of the directive using namespace std;
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_StudyBuilder.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_StudyBuilder.cxx
23 //  Author : Sergey RUIN
24 //  Module : SALOME
25 //
26 #include "SALOMEDSImpl_Attributes.hxx"
27 #include "SALOMEDSImpl_Study.hxx"
28 #include "SALOMEDSImpl_StudyBuilder.hxx"
29 #include "SALOMEDSImpl_SObject.hxx"
30 #include "SALOMEDSImpl_SComponent.hxx"
31 #include "SALOMEDSImpl_Tool.hxx"
32 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
33
34 #include "DF_ChildIterator.hxx"
35 #include "DF_Label.hxx"
36
37 #include <HDFOI.hxx>
38 #include <stdlib.h> 
39 #include <string.h> 
40
41 #define USE_CASE_LABEL_TAG            2
42 #define DIRECTORYID                   16661
43 #define FILELOCALID                   26662 
44
45 static void Translate_persistentID_to_IOR(DF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII);
46
47 //============================================================================
48 /*! Function : constructor
49  *  Purpose  :
50  */
51 //============================================================================
52 SALOMEDSImpl_StudyBuilder::SALOMEDSImpl_StudyBuilder(const SALOMEDSImpl_Study* theOwner)
53 {
54    _errorCode = "";
55   _callbackOnAdd=NULL;
56   _callbackOnRemove = NULL;
57    _study = (SALOMEDSImpl_Study*)theOwner;
58    _doc = _study->GetDocument();
59 }
60
61 //============================================================================
62 /*! Function : destructor
63  *  Purpose  :
64  */
65 //============================================================================
66 SALOMEDSImpl_StudyBuilder::~SALOMEDSImpl_StudyBuilder()
67 {}
68
69 //============================================================================
70 /*! Function : NewComponent
71  *  Purpose  : Create a new component (Scomponent)
72  */
73 //============================================================================
74 SALOMEDSImpl_SComponent SALOMEDSImpl_StudyBuilder::NewComponent(const std::string& DataType)
75 {
76   _errorCode = "";
77   CheckLocked();
78
79   SALOMEDSImpl_SComponent sco;
80
81   if(DataType.size() == 0) return sco;
82
83   //Always create component under main label.
84   DF_Label L  = _doc->Main();
85
86   DF_Label NL = L.NewChild();
87
88   SALOMEDSImpl_AttributeComment::Set(NL, DataType);
89
90   SALOMEDSImpl_SComponent so =  _study->GetSComponent (NL);
91
92   if(_callbackOnAdd) _callbackOnAdd->OnAddSObject(so);
93
94   _doc->SetModified(true);
95
96   return so;
97 }
98
99 //============================================================================
100 /*! Function : DefineComponentInstance
101  *  Purpose  : Add IOR attribute of a Scomponent
102  */
103 //============================================================================
104 bool SALOMEDSImpl_StudyBuilder::DefineComponentInstance(const SALOMEDSImpl_SComponent& aComponent,
105                                                         const std::string& IOR)
106 {
107    _errorCode = "";
108
109   CheckLocked();
110   if(!aComponent || IOR.empty()) {
111     _errorCode = "Invalid arguments";
112     return false;
113   }
114   //add IOR definition 
115   SALOMEDSImpl_AttributeIOR::Set(aComponent.GetLabel(), IOR);  
116
117   return true;
118 }
119
120 //============================================================================
121 /*! Function : RemoveComponent
122  *  Purpose  : Delete a Scomponent
123  */
124 //============================================================================
125 bool SALOMEDSImpl_StudyBuilder::RemoveComponent(const SALOMEDSImpl_SComponent& aComponent)
126 {
127    _errorCode = "";
128   CheckLocked();
129   return RemoveObject(aComponent);
130 }
131
132 //============================================================================
133 /*! Function : NewObject
134  *  Purpose  : Create a new SObject
135  */
136 //============================================================================
137 SALOMEDSImpl_SObject SALOMEDSImpl_StudyBuilder::NewObject(const SALOMEDSImpl_SObject& theFatherObject)
138 {
139   _errorCode = "";
140   CheckLocked();
141
142   //Find label of father
143   DF_Label Lab = theFatherObject.GetLabel();
144   
145   //Create a new label
146   DF_Label NewLab = Lab.NewChild();
147   
148   SALOMEDSImpl_SObject so = _study->GetSObject(NewLab);
149   if(_callbackOnAdd) _callbackOnAdd->OnAddSObject(so);
150
151   _doc->SetModified(true);  
152   return so;
153 }
154
155 //============================================================================
156 /*! Function : NewObjectToTag
157  *  Purpose  :
158  */
159 //============================================================================
160 SALOMEDSImpl_SObject SALOMEDSImpl_StudyBuilder::NewObjectToTag(const SALOMEDSImpl_SObject& theFatherObject,
161                                                         const int theTag)
162 {
163   _errorCode = "";
164   CheckLocked();
165   //Find label of father
166   DF_Label Lab = theFatherObject.GetLabel();
167
168   //Create or find label
169   DF_Label NewLab = Lab.FindChild(theTag, 1);
170
171   SALOMEDSImpl_SObject so = _study->GetSObject(NewLab);
172
173   if(_callbackOnAdd) _callbackOnAdd->OnAddSObject(so);
174
175   _doc->SetModified(true);  
176   return so;
177 }
178
179 //============================================================================
180 /*! Function : RemoveObject
181  *  Purpose  :
182  */
183 //============================================================================
184 bool SALOMEDSImpl_StudyBuilder::RemoveObject(const SALOMEDSImpl_SObject& anObject)
185 {
186    _errorCode = "";
187   CheckLocked();
188   if(!anObject) {
189     _errorCode = "Null object";
190     return false;
191   }
192
193   if(_callbackOnRemove) _callbackOnRemove->OnRemoveSObject(anObject);
194
195   DF_Label Lab = anObject.GetLabel();
196
197   SALOMEDSImpl_AttributeReference* aReference = NULL;
198   if ((aReference=(SALOMEDSImpl_AttributeReference*)Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
199     SALOMEDSImpl_AttributeTarget* aTarget = NULL;
200     if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
201       aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
202   }
203
204   SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
205   if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
206     _study->DeleteIORLabelMapItem(anAttr->Value());
207   }
208
209   Lab.ForgetAllAttributes();
210  
211   _doc->SetModified(true);  
212     
213   return true;
214 }
215
216 //============================================================================
217 /*! Function : RemoveObjectWithChildren
218  *  Purpose  :
219  */
220 //============================================================================
221 bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const SALOMEDSImpl_SObject& anObject)
222 {
223    _errorCode = "";
224   CheckLocked();
225   if(!anObject) {
226     _errorCode = "Null object";
227     return false;
228   }
229
230   if(_callbackOnRemove) _callbackOnRemove->OnRemoveSObject(anObject);
231
232   DF_Label Lab = anObject.GetLabel();
233
234   SALOMEDSImpl_AttributeReference* aReference = NULL;
235   if ((aReference=(SALOMEDSImpl_AttributeReference*)Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
236     SALOMEDSImpl_AttributeTarget* aTarget = NULL;
237     if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
238       aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
239   }
240   SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
241   if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
242     _study->DeleteIORLabelMapItem(anAttr->Value());
243   }
244
245   DF_ChildIterator it(Lab, true);
246   for(;it.More();it.Next()) {
247     DF_Label aLabel = it.Value();
248     if ((aReference=(SALOMEDSImpl_AttributeReference*)aLabel.FindAttribute(SALOMEDSImpl_AttributeReference::GetID()))) {
249       SALOMEDSImpl_AttributeTarget* aTarget = NULL;
250       if ((aTarget=(SALOMEDSImpl_AttributeTarget*)aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID())))
251         aTarget->Remove(SALOMEDSImpl_Study::SObject(aLabel));
252     }
253     SALOMEDSImpl_AttributeIOR* anAttr = NULL; //Remove from IORLabel map
254     if ((anAttr=(SALOMEDSImpl_AttributeIOR*)aLabel.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
255       _study->DeleteIORLabelMapItem(anAttr->Value());
256     }
257   }
258
259   Lab.ForgetAllAttributes(true);
260
261   _doc->SetModified(true);  
262   
263   return true;
264 }
265
266 //============================================================================
267 /*! Function : LoadWith
268  *  Purpose  : 
269  */
270 //============================================================================
271 bool SALOMEDSImpl_StudyBuilder::LoadWith(const SALOMEDSImpl_SComponent& anSCO,
272                                          SALOMEDSImpl_Driver* aDriver) 
273 {
274   _errorCode = "";
275
276   DF_Label Lab = anSCO.GetLabel();
277   SALOMEDSImpl_AttributePersistentRef* Att = NULL;
278
279   //Find the current Url of the study  
280   if ((Att=(SALOMEDSImpl_AttributePersistentRef*)_doc->Main().FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
281     int aLocked = _study->GetProperties()->IsLocked();
282     if (aLocked) _study->GetProperties()->SetLocked(false);
283
284     std::string Res(Att->Value());
285     std::string aHDFPath(Res);
286
287     SALOMEDSImpl_AttributeComment* type = NULL;
288     std::string DataType;
289     if ((type=(SALOMEDSImpl_AttributeComment*)Lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID())))
290       DataType = type->Value();
291
292     // associate the driver to the SComponent
293     if(aDriver == NULL) {
294       _errorCode = "Driver is null";
295       return false;
296     }
297
298     // mpv 06.03.2003: SAL1927 - if component data if already loaded, it is not necessary to do it again
299     SALOMEDSImpl_AttributeIOR* attrIOR = NULL;
300     if ((attrIOR=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
301       if (aLocked) _study->GetProperties()->SetLocked(true);
302       return true;
303     }
304
305     DefineComponentInstance (anSCO, aDriver->GetIOR());
306
307     std::string aHDFUrl;
308     bool isASCII = false;
309     if (HDFascii::isASCII(aHDFPath.c_str())) {
310       isASCII = true;
311       aHDFUrl = HDFascii::ConvertFromASCIIToHDF(aHDFPath.c_str());
312       aHDFUrl += "hdf_from_ascii.hdf";
313     } else {
314       aHDFUrl = aHDFPath;
315     }
316
317     //Open the Study HDF file 
318     HDFfile *hdf_file = new HDFfile((char*)aHDFUrl.c_str()); 
319
320     char aMultifileState[2];
321     char ASCIIfileState[2];
322     try {
323       std::string scoid = anSCO.GetID();
324       hdf_file->OpenOnDisk(HDF_RDONLY);
325       HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
326       hdf_group->OpenOnDisk();
327       HDFgroup *hdf_sco_group = new HDFgroup((char*)scoid.c_str(), hdf_group);
328       hdf_sco_group->OpenOnDisk();
329
330       unsigned char* aStreamFile = NULL;
331       int aStreamSize = 0;
332
333       if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
334         HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
335         hdf_dataset->OpenOnDisk();
336         aStreamSize = hdf_dataset->GetSize();
337         aStreamFile  = new unsigned char[aStreamSize];
338         if(aStreamFile == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
339         hdf_dataset->ReadFromDisk(aStreamFile);
340         hdf_dataset->CloseOnDisk();
341         hdf_dataset = 0;
342       } else
343         aStreamFile = NULL;
344
345       HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
346       multifile_hdf_dataset->OpenOnDisk();
347       multifile_hdf_dataset->ReadFromDisk(aMultifileState);
348
349       HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
350       ascii_hdf_dataset->OpenOnDisk();
351       ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
352
353       std::string aDir = SALOMEDSImpl_Tool::GetDirFromPath(Res);
354
355       bool aResult = (ASCIIfileState[0]=='A')?
356         aDriver->LoadASCII(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M'):
357         aDriver->Load(anSCO, aStreamFile, aStreamSize, aDir.c_str(), aMultifileState[0]=='M');
358
359       if(aStreamFile != NULL) delete []aStreamFile; 
360
361       if(!aResult) {
362         RemoveAttribute( anSCO, "AttributeIOR" );
363
364         _errorCode = "Can't load component";
365         throw HDFexception("Unable to load component");
366       }
367
368       //if(aDir != NULL) delete []aDir;
369
370       multifile_hdf_dataset->CloseOnDisk();
371       multifile_hdf_dataset = 0;
372       ascii_hdf_dataset->CloseOnDisk();
373       ascii_hdf_dataset = 0;
374
375       hdf_sco_group->CloseOnDisk();
376       hdf_sco_group = 0;
377       hdf_group->CloseOnDisk();
378       hdf_group = 0;
379       hdf_file->CloseOnDisk();
380       delete hdf_file;
381
382       if (isASCII) {
383         std::vector<std::string> aFilesToRemove;
384         aFilesToRemove.push_back("hdf_from_ascii.hdf");
385         SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl),
386                                                 aFilesToRemove, true);
387       }      
388     }
389     catch (HDFexception) {
390       delete hdf_file;
391
392       if (isASCII) {
393         std::vector<std::string> aFilesToRemove;
394         aFilesToRemove.push_back(aHDFUrl);
395         SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
396       }
397
398       if (aLocked) _study->GetProperties()->SetLocked(true);
399       _errorCode = "No persistent file";   
400       return false;
401     }
402
403     try {
404       Translate_persistentID_to_IOR (Lab, aDriver, aMultifileState[0]=='M', ASCIIfileState[0] == 'A');
405     } catch(...) {
406       _errorCode = "Can not convert persistent IDs to IORs";
407       return false;
408     }
409
410     if (aLocked) _study->GetProperties()->SetLocked(true);
411   } else {
412     _errorCode = "No persistent file";   
413   }
414
415   return true;
416 }
417
418
419 //============================================================================
420 /*! Function : Load
421  *  Purpose  : 
422  */
423 //============================================================================
424 bool SALOMEDSImpl_StudyBuilder::Load(const SALOMEDSImpl_SObject& sco)
425 {
426   _errorCode = "Not implemented";
427   return false;
428 }
429
430 //============================================================================
431 /*! Function : FindOrCreateAttribute
432  *  Purpose  : Add attribute of given type to SObject, if there is attribute of such type, returns
433  *  existing one
434  */
435 //============================================================================
436 DF_Attribute* SALOMEDSImpl_StudyBuilder::FindOrCreateAttribute(const SALOMEDSImpl_SObject& anObject, 
437                                                                const std::string& aTypeOfAttribute)
438 {
439   _errorCode = "";
440   if(!anObject) {
441     _errorCode = "Invalid arguments";
442     return NULL;
443   }
444
445   DF_Label Lab = anObject.GetLabel();
446   if(Lab.IsNull()) {
447     _errorCode = "Null label";
448     return NULL;
449   }
450
451   _doc->SetModified(true);  
452
453   //The macro adds all necessary checks for standardly behaiving attributes
454   __FindOrCreateAttributeForBuilder
455
456  
457   //Add checks for TreeNode and UserID attributes  
458   if (strncmp(aTypeOfAttribute.c_str(), "AttributeTreeNode",17) == 0 ) {
459     
460     std::string aTreeNodeGUID;
461     if (strcmp(aTypeOfAttribute.c_str(), "AttributeTreeNode") == 0) {
462       aTreeNodeGUID = SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID();
463     } else {
464       aTreeNodeGUID = aTypeOfAttribute.substr(21, aTypeOfAttribute.size()); // create tree node GUID by name
465     }
466     SALOMEDSImpl_AttributeTreeNode* anAttr = NULL;
467     if (!(anAttr=(SALOMEDSImpl_AttributeTreeNode*)Lab.FindAttribute(aTreeNodeGUID))) {
468       CheckLocked();
469       anAttr = SALOMEDSImpl_AttributeTreeNode::Set(Lab, aTreeNodeGUID);
470     }
471     return anAttr;
472   }
473
474   if (strncmp(aTypeOfAttribute.c_str(), "AttributeUserID",15) == 0 ) {
475     std::string aUserGUID;
476     if (strcmp(aTypeOfAttribute.c_str(), "AttributeUserID") == 0) {
477       aUserGUID = SALOMEDSImpl_AttributeUserID::DefaultID();
478     } else {
479       aUserGUID = aTypeOfAttribute.substr(15, aTypeOfAttribute.size()); // create tree node GUID by name
480     }
481     SALOMEDSImpl_AttributeUserID* anAttr = NULL;
482     if (!(anAttr=(SALOMEDSImpl_AttributeUserID*)Lab.FindAttribute(aUserGUID))) {
483       CheckLocked();
484       anAttr = SALOMEDSImpl_AttributeUserID::Set(Lab, aUserGUID);
485     }
486     return anAttr;
487   }
488   _errorCode = "Can not create an attribute";
489
490   return NULL;
491 }
492
493 //============================================================================
494 /*! Function : FindAttribute
495  *  Purpose  : Find attribute of given type assigned SObject, returns Standard_True if it is found
496  */
497 //============================================================================
498
499 bool SALOMEDSImpl_StudyBuilder::FindAttribute(const SALOMEDSImpl_SObject& anObject, 
500                                               DF_Attribute*& anAttribute, 
501                                               const std::string& aTypeOfAttribute)
502 {
503   _errorCode = "";
504   if(!anObject) {
505     _errorCode = "Invalid arguments";
506     return false;
507   }
508   DF_Label Lab = anObject.GetLabel();
509   if ((anAttribute=Lab.FindAttribute(SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute)))) {
510     // commented out because NO MODIFICATION is done to attributes when calling FindAttribute()
511     // _doc->Modify();  
512     return true;
513   }
514   return false;
515 }
516
517 //============================================================================
518 /*! Function : RemoveAttribute
519  *  Purpose  : Remove attribute of given type assigned SObject
520  */
521 //============================================================================
522
523 bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const SALOMEDSImpl_SObject& anObject, 
524                                                 const std::string& aTypeOfAttribute)
525 {
526   _errorCode = "";
527   CheckLocked();
528   if(!anObject) {
529     _errorCode = "Invalid arguments";
530     return false;
531   }
532   DF_Label Lab = anObject.GetLabel();
533   
534   if (aTypeOfAttribute == std::string("AttributeIOR")) { // Remove from IORLabel map
535     SALOMEDSImpl_AttributeIOR* anAttr = NULL;
536     if ((anAttr=(SALOMEDSImpl_AttributeIOR*)Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
537       _study->DeleteIORLabelMapItem(anAttr->Value());
538     }
539   }
540
541   Lab.ForgetAttribute (SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute));
542     
543   _doc->SetModified(true);  
544     
545   return true;
546 }
547
548 //============================================================================
549 /*! Function : Addreference
550  *  Purpose  : 
551  */
552 //============================================================================
553 bool SALOMEDSImpl_StudyBuilder::Addreference(const SALOMEDSImpl_SObject& me, 
554                                              const SALOMEDSImpl_SObject& theReferencedObject)
555 {
556   _errorCode = "";
557   if(!me || !theReferencedObject) {
558    _errorCode = "Invalid arguments";
559    return false;
560   }
561   CheckLocked();
562   DF_Label Lab = me.GetLabel();
563   DF_Label RefLab = theReferencedObject.GetLabel();
564   SALOMEDSImpl_AttributeReference::Set(Lab,RefLab);
565
566   SALOMEDSImpl_AttributeTarget::Set(RefLab)->Add(SALOMEDSImpl_Study::SObject(Lab));
567
568   if(_callbackOnRemove && Lab.IsDescendant(_doc->Main())) _callbackOnRemove->OnRemoveSObject(me);
569   
570   return true;
571 }
572
573 //============================================================================
574 /*! Function : RemoveReference
575  *  Purpose  : 
576  */
577 //============================================================================
578 bool SALOMEDSImpl_StudyBuilder::RemoveReference(const SALOMEDSImpl_SObject& me)
579 {
580   _errorCode = "";
581   SALOMEDSImpl_SObject theReferencedObject;
582   
583   if(!me.ReferencedObject(theReferencedObject)) return false;  //No reference is found
584   
585   CheckLocked();
586   DF_Label Lab = me.GetLabel();
587
588   //SRN: 30 Aug, 2004 : fix from Ecole l'ete version 
589
590   DF_Label RefLab = theReferencedObject.GetLabel();
591        
592   SALOMEDSImpl_AttributeTarget* aTarget = NULL;
593   if((aTarget=(SALOMEDSImpl_AttributeTarget*)RefLab.FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
594     aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
595   }
596   
597   Lab.ForgetAttribute(SALOMEDSImpl_AttributeReference::GetID());  
598   
599   _doc->SetModified(true);  
600   
601   return true;
602 }
603
604
605
606 //============================================================================
607 /*! Function : AddDirectory
608  *  Purpose  : adds a new directory with a path = thePath
609  */
610 //============================================================================
611 bool SALOMEDSImpl_StudyBuilder::AddDirectory(const std::string& thePath) 
612 {
613   _errorCode = "";
614   CheckLocked();
615   if(thePath.empty()) {
616     _errorCode = "Invalid path";
617     return false;
618   }
619
620   std::string aPath(thePath), aContext(""), aFatherPath;
621   DF_Label aLabel;
622   SALOMEDSImpl_SObject anObject;
623
624   try { 
625     anObject = _study->FindObjectByPath(thePath); //Check if the directory already exists
626   }
627   catch(...) { }
628
629   if(anObject) {
630     _errorCode = "StudyNameAlreadyUsed";
631     return false; 
632   }
633
634   if(aPath[0] != '/') { //Relative path 
635     aPath.insert(aPath.begin(), '/');
636     aPath = _study->GetContext() + aPath;
637   }
638
639   std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
640   if(vs.size() == 1) 
641     aFatherPath = "/";
642   else {
643     for(int i = 0, len = vs.size()-1; i<len; i++) { 
644       aFatherPath += "/";
645       aFatherPath += vs[i];
646     }
647   }
648
649   try { 
650     anObject = _study->FindObjectByPath(aFatherPath); //Check if the father directory exists
651   }
652   catch(...) { ; }
653   if(!anObject) {
654     _errorCode = "StudyInvalidDirectory";
655     return false; 
656   }
657
658   SALOMEDSImpl_SObject aNewObject = NewObject(anObject);
659   aLabel = aNewObject.GetLabel();
660   if(aLabel.IsNull()) {
661     _errorCode = "StudyInvalidComponent";
662     return false;
663   }
664
665   SALOMEDSImpl_AttributeName::Set(aLabel, vs.back());
666
667   //Set LocalID attribute to identify the directory object
668   SALOMEDSImpl_AttributeLocalID::Set(aLabel, DIRECTORYID);
669   
670   _doc->SetModified(true); 
671   
672   return true;
673 }
674
675
676 //============================================================================
677 /*! Function : SetGUID
678  *  Purpose  : 
679  */
680 //============================================================================
681 bool SALOMEDSImpl_StudyBuilder::SetGUID(const SALOMEDSImpl_SObject& anObject, 
682                                         const std::string& theGUID)
683 {
684   _errorCode = "";
685   CheckLocked();
686   if(!anObject) {
687     _errorCode = "Invalid arguments";
688     return false;
689   }
690
691   DF_Label aLabel = anObject.GetLabel();
692   SALOMEDSImpl_AttributeUserID::Set(aLabel, theGUID);
693
694   _doc->SetModified(true);  
695
696   return true;
697 }
698
699 //============================================================================
700 /*! Function : IsGUID
701  *  Purpose  : 
702  */
703 //============================================================================
704 bool SALOMEDSImpl_StudyBuilder::IsGUID(const SALOMEDSImpl_SObject& anObject, 
705                                        const std::string& theGUID)
706 {
707   _errorCode = "";
708   if(!anObject) {
709     _errorCode = "Invalid arguments";
710     return false;
711   }
712   DF_Label aLabel = anObject.GetLabel();
713   return aLabel.IsAttribute(theGUID);
714 }
715
716
717 //============================================================================
718 /*! Function : NewCommand
719  *  Purpose  : 
720  */
721 //============================================================================
722 void SALOMEDSImpl_StudyBuilder::NewCommand()
723 {
724   _errorCode = "";
725
726   // mpv: for SAL2114 - unset "lock changed" flag at the operation start
727   _study->GetProperties()->IsLockChanged(true);
728
729   //Not implemented
730 }
731
732 //============================================================================
733 /*! Function : CommitCommand
734  *  Purpose  : 
735  */
736 //============================================================================
737 void SALOMEDSImpl_StudyBuilder::CommitCommand()
738 {
739   _errorCode = "";
740   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
741   if (anAttr->IsLocked() && !anAttr->IsLockChanged(true)) {
742     _errorCode = "LockProtection";
743     throw LockProtection("LockProtection");
744   } else {
745     int aModif = anAttr->GetModified();
746     if (aModif < 0) aModif = 1000; // if user make undo and then - new transaction "modify" will never be zero
747     anAttr->SetModified(aModif+1);
748   }
749   
750
751   //Not implemented
752   _doc->SetModified(true);  
753 }
754
755 //============================================================================
756 /*! Function : HasOpenCommand
757  *  Purpose  : 
758  */
759 //============================================================================
760 bool SALOMEDSImpl_StudyBuilder::HasOpenCommand()
761 {
762   _errorCode = "";
763
764   //Not implememnted
765   return false;
766 }
767
768 //============================================================================
769 /*! Function : AbortCommand
770  *  Purpose  : 
771  */
772 //============================================================================
773 void SALOMEDSImpl_StudyBuilder::AbortCommand()
774 {
775   _errorCode = "";
776   //Not implemented    
777 }
778
779 //============================================================================
780 /*! Function : Undo
781  *  Purpose  : 
782  */
783 //============================================================================
784 void SALOMEDSImpl_StudyBuilder::Undo()
785 {
786   //Not implemented
787   _errorCode = "";
788   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
789   if (anAttr->IsLocked()) {
790     _errorCode = "LockProtection";
791     throw LockProtection("LockProtection");
792   } else {
793     anAttr->SetModified(anAttr->GetModified()-1);
794   }
795
796   _doc->SetModified(true);  
797 }
798
799 //============================================================================
800 /*! Function : Redo
801  *  Purpose  : 
802  */
803 //============================================================================
804 void SALOMEDSImpl_StudyBuilder::Redo() 
805 {
806   _errorCode = "";
807   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
808   if (anAttr->IsLocked()) {
809     _errorCode = "LockProtection";
810     throw LockProtection("LockProtection");
811   } else {
812     anAttr->SetModified(anAttr->GetModified()+1);
813   }
814
815   //Not implemented
816
817   _doc->SetModified(true);  
818 }
819
820 //============================================================================
821 /*! Function : GetAvailableUndos
822  *  Purpose  : 
823  */
824 //============================================================================
825 bool SALOMEDSImpl_StudyBuilder::GetAvailableUndos()
826 {
827   _errorCode = "";
828   return false;
829 }
830
831 //============================================================================
832 /*! Function : GetAvailableRedos
833  *  Purpose  : 
834  */
835 //============================================================================
836 bool  SALOMEDSImpl_StudyBuilder::GetAvailableRedos()
837 {
838   _errorCode = "";
839   return false;
840 }
841
842 //============================================================================
843 /*! Function : UndoLimit
844  *  Purpose  : 
845  */
846 //============================================================================
847 int  SALOMEDSImpl_StudyBuilder::UndoLimit()
848 {
849   _errorCode = "";
850   return 1;
851 }
852
853 //============================================================================
854 /*! Function : UndoLimit
855  *  Purpose  : 
856  */
857 //============================================================================
858 void SALOMEDSImpl_StudyBuilder::UndoLimit(int n)
859 {
860   _errorCode = "";
861   CheckLocked();
862   //Not implemented
863 }
864
865 //============================================================================
866 /*! Function : SetOnAddSObject
867  *  Purpose  : 
868  */
869 //============================================================================
870 SALOMEDSImpl_Callback*
871 SALOMEDSImpl_StudyBuilder::SetOnAddSObject(const SALOMEDSImpl_Callback* theCallback)
872 {
873   _errorCode = "";
874   SALOMEDSImpl_Callback* aRet = _callbackOnAdd;
875   _callbackOnAdd = (SALOMEDSImpl_Callback*)theCallback;
876   return aRet;
877 }
878
879 //============================================================================
880 /*! Function : SetOnNewSObject
881  *  Purpose  : 
882  */
883 //============================================================================
884 SALOMEDSImpl_Callback* 
885 SALOMEDSImpl_StudyBuilder::SetOnRemoveSObject(const SALOMEDSImpl_Callback* theCallback)
886 {
887   _errorCode = "";
888   SALOMEDSImpl_Callback* aRet = _callbackOnRemove;
889   _callbackOnRemove = (SALOMEDSImpl_Callback*)theCallback;
890   return aRet;
891 }
892
893 //============================================================================
894 /*! Function : CheckLocked
895  *  Purpose  : 
896  */
897 //============================================================================
898 void SALOMEDSImpl_StudyBuilder::CheckLocked()
899 {
900   _errorCode = "";
901   if (HasOpenCommand()) return;
902   SALOMEDSImpl_AttributeStudyProperties* anAttr = _study->GetProperties();
903   if (anAttr->IsLocked()) {
904     _errorCode = "LockProtection";
905     throw LockProtection("LockProtection");
906   }
907 }
908
909 //============================================================================
910 /*! Function : SetName
911  *  Purpose  : 
912  */
913 //============================================================================
914 bool SALOMEDSImpl_StudyBuilder::SetName(const SALOMEDSImpl_SObject& theSO, 
915                                         const std::string& theValue)
916 {
917   _errorCode = "";
918   CheckLocked();
919   if(!theSO) {
920     _errorCode = "Invalid arguments";
921     return false;
922   }
923   SALOMEDSImpl_AttributeName::Set(theSO.GetLabel(), theValue);
924
925   _doc->SetModified(true);  
926
927   return true;
928 }
929
930 //============================================================================
931 /*! Function : SetComment
932  *  Purpose  : 
933  */
934 //============================================================================
935 bool SALOMEDSImpl_StudyBuilder::SetComment(const SALOMEDSImpl_SObject& theSO, 
936                                            const std::string& theValue)
937 {
938   _errorCode = "";
939   CheckLocked();
940   if(!theSO) {
941     _errorCode = "Invalid arguments";
942     return false;
943   }
944   SALOMEDSImpl_AttributeComment::Set(theSO.GetLabel(), theValue);
945
946   _doc->SetModified(true);  
947
948   return true;
949 }
950
951 //============================================================================
952 /*! Function : SetIOR
953  *  Purpose  : 
954  */
955 //============================================================================
956 bool SALOMEDSImpl_StudyBuilder::SetIOR(const SALOMEDSImpl_SObject& theSO, 
957                                        const std::string& theValue)
958 {
959   _errorCode = "";
960   CheckLocked();
961   if(!theSO) {
962     _errorCode = "Invalid arguments";
963     return false;
964   }
965   SALOMEDSImpl_AttributeIOR::Set(theSO.GetLabel(), theValue);
966
967   _doc->SetModified(true);  
968
969   return true;
970 }
971
972
973 //============================================================================
974 /*! Function : Translate_persistentID_to_IOR
975  *  Purpose  :
976  */
977 //============================================================================
978 static void Translate_persistentID_to_IOR(DF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII)
979 {
980   if(driver == NULL) return;
981   DF_ChildIterator  itchild (Lab);
982   
983   for (; itchild.More(); itchild.Next()) {
984     DF_Label current = itchild.Value();
985     SALOMEDSImpl_AttributePersistentRef* Att = NULL;
986     if ((Att=(SALOMEDSImpl_AttributePersistentRef*)current.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {  
987
988       SALOMEDSImpl_AttributeLocalID* anID = NULL;
989       if ((anID=(SALOMEDSImpl_AttributeLocalID*)current.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) 
990         if (anID->Value() == FILELOCALID) continue;   //SRN: This attribute store a file name, skip it 
991
992       std::string persist_ref = Att->Value();
993       SALOMEDSImpl_SObject so = SALOMEDSImpl_Study::SObject(current);
994       std::string ior_string = driver->LocalPersistentIDToIOR(so, 
995                                                          persist_ref, 
996                                                          isMultiFile, 
997                                                          isASCII);
998       SALOMEDSImpl_AttributeIOR::Set (current, ior_string); 
999      
1000     }
1001     Translate_persistentID_to_IOR (current, driver, isMultiFile, isASCII);
1002   }
1003 }
1004