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