Salome HOME
This commit was generated by cvs2git to create tag 'V1_4_0b1'.
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_StudyBuilder_i.cxx
1 //  SALOME SALOMEDS : data structure of SALOME and sources of Salome data server 
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOMEDS_StudyBuilder_i.cxx
25 //  Author : Yves FRICAUD
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "utilities.h"
31 #include "SALOMEDS_Study_i.hxx"
32 //#include "SALOMEDS_StudyBuilder_i.hxx"
33 #include "SALOMEDS_SObject_i.hxx"
34 #include "SALOMEDS_SComponent_i.hxx"
35
36 #include "SALOMEDS_IORAttribute.hxx"
37 #include "SALOMEDS_PersRefAttribute.hxx"
38 #include "SALOMEDS_TargetAttribute.hxx"
39 #include "SALOMEDS_StudyPropertiesAttribute.hxx"
40 #include "SALOMEDS_PythonObjectAttribute.hxx"
41 #include <TDF_ChildIterator.hxx>
42 #include <TDF_Label.hxx>
43 #include <TDataStd_Name.hxx>
44 #include <TDataStd_Comment.hxx>
45 #include <TDataStd_UAttribute.hxx>
46 #include <TDF_Tool.hxx>
47 #include <TDF_Reference.hxx>
48 #include <TDF_Data.hxx>
49 #include <TDataStd_ChildNodeIterator.hxx>
50 #include <TDF_ListIteratorOfAttributeList.hxx>
51 #include "SALOMEDS_AttributePersistentRef_i.hxx"
52 #include "SALOMEDS_AttributeIOR_i.hxx"
53 #include "SALOMEDS_AttributeComment_i.hxx"
54 #include "SALOMEDS_AttributeExternalFileDef_i.hxx"
55 #include "SALOMEDS_AttributeFileType_i.hxx"
56 #include "SALOMEDS_AttributeName_i.hxx"
57 #include "SALOMEDS_AttributeSequenceOfInteger_i.hxx"
58 #include "SALOMEDS_AttributeSequenceOfReal_i.hxx"
59 #include "SALOMEDS_AttributeTableOfInteger_i.hxx"
60 #include "SALOMEDS_AttributeTableOfReal_i.hxx"
61 #include "SALOMEDS_AttributeTableOfString_i.hxx"
62 #include "SALOMEDS_AttributeInteger_i.hxx"
63 #include "SALOMEDS_AttributeReal_i.hxx"
64 #include "SALOMEDS_AttributeDrawable_i.hxx"
65 #include "SALOMEDS_AttributeSelectable_i.hxx"
66 #include "SALOMEDS_AttributeExpandable_i.hxx"
67 #include "SALOMEDS_AttributeOpened_i.hxx"
68 #include "SALOMEDS_AttributeTextColor_i.hxx"
69 #include "SALOMEDS_AttributeTextHighlightColor_i.hxx"
70 #include "SALOMEDS_AttributePixMap_i.hxx"
71 #include "SALOMEDS_AttributeTreeNode_i.hxx"
72 #include "SALOMEDS_AttributeTarget_i.hxx"
73 #include "SALOMEDS_AttributeLocalID_i.hxx"
74 #include "SALOMEDS_AttributeUserID_i.hxx"
75 #include "SALOMEDS_AttributeStudyProperties_i.hxx"
76 #include "SALOMEDS_AttributePythonObject_i.hxx"
77 #include "SALOMEDS_Tool.hxx"
78 #include "Utils_CorbaException.hxx"
79
80 #include <HDFOI.hxx>
81 #include <stdlib.h> 
82
83 #define USE_CASE_LABEL_TAG            2
84 #define DIRECTORYID 16661
85 #define FILELOCALID 26662 
86
87 //============================================================================
88 /*! Function : constructor
89  *  Purpose  :
90  */
91 //============================================================================
92 SALOMEDS_StudyBuilder_i::SALOMEDS_StudyBuilder_i(const Handle(TDocStd_Document) doc, 
93                                                  CORBA::ORB_ptr orb) : _doc(doc)
94 {
95   _orb = CORBA::ORB::_duplicate(orb);
96 }
97
98 //============================================================================
99 /*! Function : destructor
100  *  Purpose  :
101  */
102 //============================================================================
103 SALOMEDS_StudyBuilder_i::~SALOMEDS_StudyBuilder_i()
104 {
105 }
106
107 //============================================================================
108 /*! Function : NewComponent
109  *  Purpose  : Create a new component (Scomponent)
110  */
111 //============================================================================
112 SALOMEDS::SComponent_ptr 
113 SALOMEDS_StudyBuilder_i::NewComponent(const char* DataType)
114 {
115   CheckLocked();
116   //Always create component under main label.
117   TDF_Label L  = _doc->Main();
118
119   // YFR DEBUG : 13/02/2002 TDF_Label NL = L.NewChild();
120   
121   Standard_Integer imax = 0;
122   for (TDF_ChildIterator it(L); it.More(); it.Next()) {
123     if (it.Value().Tag() > imax)
124       imax = it.Value().Tag();
125   }
126   imax++;
127   TDF_Label NL = L.FindChild(imax);
128
129 //   TDataStd_Comment::Set(NL,Standard_CString(DataType));
130   TDataStd_Comment::Set(NL,Standard_CString(strdup(DataType)));
131
132   SALOMEDS_SComponent_i *  so_servant = new SALOMEDS_SComponent_i (NL,_orb);
133   SALOMEDS::SComponent_var so;
134   so= SALOMEDS::SComponent::_narrow(so_servant->SComponent::_this()); 
135
136   if(!CORBA::is_nil(_callbackOnAdd)) _callbackOnAdd->OnAddSObject(so);
137
138   return so;
139 }
140
141 //============================================================================
142 /*! Function : DefineComponentInstance
143  *  Purpose  : Add IOR attribute of a Scomponent
144  */
145 //============================================================================
146 void SALOMEDS_StudyBuilder_i::DefineComponentInstance(SALOMEDS::SComponent_ptr aComponent,
147                                                  CORBA::Object_ptr IOR)
148 {
149   CheckLocked();
150   //Find label
151   TDF_Label Lab;
152   ASSERT(!CORBA::is_nil(aComponent));
153   CORBA::String_var compid = aComponent->GetID();
154   TDF_Tool::Label(_doc->GetData(),strdup(compid),Lab);
155
156   //add IOR definition 
157   ASSERT(!CORBA::is_nil(IOR));
158   CORBA::String_var iorstr = _orb->object_to_string(IOR);
159   SALOMEDS_IORAttribute::Set(Lab,strdup(iorstr),_orb);
160   
161 }
162
163 //============================================================================
164 /*! Function : RemoveComponent
165  *  Purpose  : Delete a Scomponent
166  */
167 //============================================================================
168 void 
169 SALOMEDS_StudyBuilder_i::RemoveComponent(SALOMEDS::SComponent_ptr aComponent)
170 {
171   CheckLocked();
172   ASSERT(!CORBA::is_nil(aComponent));
173   RemoveObject(aComponent);
174 }
175
176 //============================================================================
177 /*! Function : NewObject
178  *  Purpose  : Create a new SObject
179  */
180 //============================================================================
181 SALOMEDS::SObject_ptr 
182 SALOMEDS_StudyBuilder_i::NewObject(SALOMEDS::SObject_ptr theFatherObject)
183 {
184   CheckLocked();
185   TCollection_AsciiString anEntry;
186  
187   //Find label of father
188   TDF_Label Lab;
189   
190   ASSERT(!CORBA::is_nil(theFatherObject));
191   CORBA::String_var fatherid = theFatherObject->GetID();
192   TDF_Tool::Label(_doc->GetData(),strdup(fatherid),Lab);
193
194   //Create a new label
195   //YFR DEBUG : 13/02/2002  TDF_Label NewLab = Lab.NewChild();
196   Standard_Integer imax = 0;
197   for (TDF_ChildIterator it(Lab); it.More(); it.Next()) {
198     if (it.Value().Tag() > imax)
199       imax = it.Value().Tag();
200   }
201   imax++;
202   TDF_Label NewLab = Lab.FindChild(imax);
203   
204   SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (NewLab,_orb);
205   SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this()); 
206
207   if(!CORBA::is_nil(_callbackOnAdd)) _callbackOnAdd->OnAddSObject(so);
208
209   return so;
210 }
211
212 //============================================================================
213 /*! Function : NewObjectToTag
214  *  Purpose  :
215  */
216 //============================================================================
217 SALOMEDS::SObject_ptr 
218 SALOMEDS_StudyBuilder_i::NewObjectToTag(SALOMEDS::SObject_ptr theFatherObject,
219                                         CORBA::Long atag)
220 {
221   CheckLocked();
222   //Find label of father
223   TDF_Label Lab;
224
225   ASSERT(!CORBA::is_nil(theFatherObject));
226   CORBA::String_var fatherid = theFatherObject->GetID();
227   TDF_Tool::Label(_doc->GetData(),strdup(fatherid),Lab);
228   //Create or find label
229   TDF_Label NewLab = Lab.FindChild(atag,1);
230
231   SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (NewLab,_orb);
232   SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this()); 
233
234   if(!CORBA::is_nil(_callbackOnAdd)) _callbackOnAdd->OnAddSObject(so);
235
236   return so;
237 }
238
239 //============================================================================
240 /*! Function : RemoveObject
241  *  Purpose  :
242  */
243 //============================================================================
244 void SALOMEDS_StudyBuilder_i::RemoveObject(SALOMEDS::SObject_ptr anObject)
245 {
246   CheckLocked();
247   if(!CORBA::is_nil(_callbackOnRemove)) _callbackOnRemove->OnRemoveSObject(anObject);
248
249   TDF_Label Lab;
250   ASSERT(!CORBA::is_nil(anObject));
251   TDF_Tool::Label(_doc->GetData(),anObject->GetID(),Lab);
252
253   Handle(TDF_Reference) aReference;
254   if (Lab.FindAttribute(TDF_Reference::GetID(), aReference)) {
255     Handle(SALOMEDS_TargetAttribute) aTarget;
256     if (aReference->Get().FindAttribute(SALOMEDS_TargetAttribute::GetID(),aTarget))
257       aTarget->Remove(Lab);
258   }
259
260   Handle(SALOMEDS_IORAttribute) anAttr; // postponed removing of CORBA objects
261   if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
262     SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(strdup(TCollection_AsciiString(anAttr->Get()).ToCString()));
263
264   Lab.ForgetAllAttributes();
265 }
266
267 //============================================================================
268 /*! Function : RemoveObjectWithChildren
269  *  Purpose  :
270  */
271 //============================================================================
272 void SALOMEDS_StudyBuilder_i::RemoveObjectWithChildren(SALOMEDS::SObject_ptr anObject)
273 {
274   CheckLocked();
275   if(!CORBA::is_nil(_callbackOnRemove)) _callbackOnRemove->OnRemoveSObject(anObject);
276
277   TDF_Label Lab;
278   ASSERT(!CORBA::is_nil(anObject));
279   TDF_Tool::Label(_doc->GetData(),anObject->GetID(),Lab);
280
281   Handle(TDF_Reference) aReference;
282   if (Lab.FindAttribute(TDF_Reference::GetID(), aReference)) {
283     Handle(SALOMEDS_TargetAttribute) aTarget;
284     if (aReference->Get().FindAttribute(SALOMEDS_TargetAttribute::GetID(),aTarget))
285       aTarget->Remove(Lab);
286   }
287   Handle(SALOMEDS_IORAttribute) anAttr; // postponed removing of CORBA objects
288   if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
289     SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(strdup(TCollection_AsciiString(anAttr->Get()).ToCString()));
290
291   TDF_ChildIterator it(Lab);
292   for(;it.More();it.Next()) {
293     TDF_Label aLabel = it.Value();
294     if (aLabel.FindAttribute(TDF_Reference::GetID(), aReference)) {
295       Handle(SALOMEDS_TargetAttribute) aTarget;
296       if (aReference->Get().FindAttribute(SALOMEDS_TargetAttribute::GetID(),aTarget))
297         aTarget->Remove(aLabel);
298     }
299     Handle(SALOMEDS_IORAttribute) anAttr; // postponed removing of CORBA objects
300     if (aLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
301       SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(strdup(TCollection_AsciiString(anAttr->Get()).ToCString()));
302   }
303
304   Lab.ForgetAllAttributes(Standard_True);
305 }
306
307 //============================================================================
308 /*! Function : Translate_persistentID_to_IOR
309  *  Purpose  :
310  */
311 //============================================================================
312 static void  Translate_persistentID_to_IOR(TDF_Label                  Lab,
313                                            SALOMEDS::Driver_ptr       driver,
314                                            CORBA::ORB_ptr             orb,
315                                            CORBA::Boolean             isMultiFile,
316                                            CORBA::Boolean             isASCII)
317 {
318   TDF_ChildIterator  itchild (Lab);
319   
320   for (; itchild.More(); itchild.Next()) {
321     TDF_Label current = itchild.Value();
322     Handle(TDF_Attribute) Att;
323     if (current.FindAttribute(SALOMEDS_PersRefAttribute::GetID(),Att)) {  
324
325       Handle(SALOMEDS_LocalIDAttribute) anID;
326       if (current.FindAttribute(SALOMEDS_LocalIDAttribute::GetID(), anID)) 
327         if (anID->Get() == FILELOCALID) continue;        //SRN: This attribute store a file name, skip it 
328
329       TCollection_ExtendedString res;   
330       res = Handle(TDataStd_Comment)::DownCast(Att)->Get();
331       TCollection_AsciiString ch(res);
332       
333       CORBA::String_var persistent_string = CORBA::string_dup(ch.ToCString());
334       ASSERT(! CORBA::is_nil(driver));
335       SALOMEDS_SObject_i *  so_servant = new SALOMEDS_SObject_i (current,orb);
336       SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this()); 
337
338       CORBA::String_var ior_string = driver->LocalPersistentIDToIOR(so, persistent_string, isMultiFile, isASCII);
339
340       TCollection_ExtendedString value(strdup(ior_string )); 
341       SALOMEDS_IORAttribute::Set (current,value,orb); 
342       
343       //TCollection_AsciiString anEntry;TDF_Tool::Entry (current,anEntry); //SRN: No use here
344       //delete persistent_string;
345       //delete ior_string;
346     }
347     Translate_persistentID_to_IOR (current,driver,orb,isMultiFile, isASCII);
348   }
349 }
350
351 //============================================================================
352 /*! Function : LoadWith
353  *  Purpose  : 
354  */
355 //============================================================================
356
357 //============================================================================
358 void SALOMEDS_StudyBuilder_i::LoadWith(SALOMEDS::SComponent_ptr anSCO, 
359                                        SALOMEDS::Driver_ptr aDriver) throw(SALOME::SALOME_Exception)
360 {
361   TDF_Label Lab;
362   ASSERT(!CORBA::is_nil(anSCO));
363   CORBA::String_var scoid = anSCO->GetID();
364   TDF_Tool::Label(_doc->GetData(),strdup(scoid),Lab);
365   Handle(TDF_Attribute) Att;
366
367   //Find the current Url of the study  
368   if (_doc->Main().FindAttribute(SALOMEDS_PersRefAttribute::GetID(),Att)) {
369     int aLocked = anSCO->GetStudy()->GetProperties()->IsLocked();
370     if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(false);
371
372     TCollection_ExtendedString Res = Handle(TDataStd_Comment)::DownCast(Att)->Get();
373     
374     Handle(TDataStd_Comment) type;
375     TCollection_ExtendedString DataType;
376     if (Lab.FindAttribute(TDataStd_Comment::GetID(),type))
377       DataType = type->Get();
378     else 
379       MESSAGE("No Data Type");
380
381     // associate the driver to the SComponent
382     ASSERT(!CORBA::is_nil(aDriver));
383     // mpv 06.03.2003: SAL1927 - if component data if already loaded, it is not necessary to do it again
384     if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), Att)) {
385       if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
386       return;
387     }
388     DefineComponentInstance (anSCO, aDriver);
389
390     TCollection_AsciiString aHDFPath(Res);
391
392     char* aHDFUrl;
393     bool isASCII = false;
394     if (HDFascii::isASCII(aHDFPath.ToCString())) {
395       isASCII = true;
396       char* aResultPath = HDFascii::ConvertFromASCIIToHDF(aHDFPath.ToCString());
397       aHDFUrl = new char[strlen(aResultPath) + 19];
398       sprintf(aHDFUrl, "%shdf_from_ascii.hdf", aResultPath);
399       delete(aResultPath);
400     } else {
401       aHDFUrl = strdup(aHDFPath.ToCString());
402     }
403
404     //Open the Study HDF file 
405     HDFfile *hdf_file = new HDFfile(aHDFUrl); 
406
407     char aMultifileState[2];
408     char ASCIIfileState[2];
409     try {
410       CORBA::String_var scoid = anSCO->GetID();
411       hdf_file->OpenOnDisk(HDF_RDONLY);
412       HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
413       hdf_group->OpenOnDisk();
414       HDFgroup *hdf_sco_group = new HDFgroup(scoid, hdf_group);
415       hdf_sco_group->OpenOnDisk();
416
417       SALOMEDS::TMPFile_var aStreamFile;
418       if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
419         HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
420         hdf_dataset->OpenOnDisk();
421         int aStreamSize = hdf_dataset->GetSize();
422         unsigned char* aBuffer = new unsigned char[aStreamSize];
423         if(aBuffer == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
424         hdf_dataset->ReadFromDisk(aBuffer);
425         aStreamFile = new SALOMEDS::TMPFile(aStreamSize, aStreamSize, aBuffer, 1);
426         hdf_dataset->CloseOnDisk();
427         hdf_dataset = 0;
428       } else aStreamFile = new SALOMEDS::TMPFile(0);
429       
430       HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
431       multifile_hdf_dataset->OpenOnDisk();
432       multifile_hdf_dataset->ReadFromDisk(aMultifileState);
433
434       HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
435       ascii_hdf_dataset->OpenOnDisk();
436       ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
437
438       // set path without file name from URL 
439       int aFileNameSize = Res.Length();
440       char* aDir = new char[aFileNameSize];
441       memcpy(aDir, TCollection_AsciiString(Res).ToCString(), aFileNameSize);
442       for(int aCounter = aFileNameSize-1; aCounter>=0; aCounter--)
443         if (aDir[aCounter] == '/') {
444           aDir[aCounter+1] = 0;
445           break;
446         }
447
448       CORBA::Boolean aResult = (ASCIIfileState[0]=='A')?
449         aDriver->LoadASCII(anSCO, aStreamFile.in(), aDir, aMultifileState[0]=='M'):
450         aDriver->Load(anSCO, aStreamFile.in(), aDir, aMultifileState[0]=='M');
451       if(!aResult) {
452         RemoveAttribute( anSCO, "AttributeIOR" );
453         if (isASCII) {
454           SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
455           aFilesToRemove->length(1);
456           aFilesToRemove[0] = strdup(&(aHDFUrl[strlen(SALOMEDS_Tool::GetDirFromPath(aHDFUrl))]));
457           SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
458         }
459         delete aHDFUrl;
460         MESSAGE("Can't load component");
461         THROW_SALOME_CORBA_EXCEPTION("Unable to load component data",SALOME::BAD_PARAM);
462 //      throw HDFexception("Unable to load component");
463       }
464
465       delete(aDir);
466
467       multifile_hdf_dataset->CloseOnDisk();
468       multifile_hdf_dataset = 0;
469       ascii_hdf_dataset->CloseOnDisk();
470       ascii_hdf_dataset = 0;
471       hdf_sco_group->CloseOnDisk();
472       hdf_sco_group = 0;
473       hdf_group->CloseOnDisk();
474       hdf_group = 0;
475       hdf_file->CloseOnDisk();
476       delete hdf_file;
477
478       if (isASCII) {
479         SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
480         aFilesToRemove->length(1);
481         aFilesToRemove[0] = strdup(&(aHDFUrl[strlen(SALOMEDS_Tool::GetDirFromPath(aHDFUrl))]));
482         SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
483       }
484       delete aHDFUrl;
485     }
486     catch (HDFexception) {
487       MESSAGE("No persistent file Name");
488       delete hdf_file;
489       if (isASCII) {
490         SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
491         aFilesToRemove->length(1);
492         aFilesToRemove[0] = strdup(&(aHDFUrl[strlen(SALOMEDS_Tool::GetDirFromPath(aHDFUrl))]));
493         SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
494       }
495       delete aHDFUrl;
496       if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
497       return;
498     }
499     
500     try {
501       Translate_persistentID_to_IOR (Lab,aDriver,_orb, aMultifileState[0]=='M', ASCIIfileState[0] == 'A');
502     } catch (SALOME::SALOME_Exception) {
503       MESSAGE("Can't translate PersRef to IOR");
504       if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
505       THROW_SALOME_CORBA_EXCEPTION("Unable to convert component persistent data to the transient",SALOME::BAD_PARAM);
506 //        throw HDFexception("Unable to load component data");
507     }
508     if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
509   } else
510     MESSAGE("No persistent file Name");
511 }
512
513
514 //============================================================================
515 /*! Function : Load
516  *  Purpose  : 
517  */
518 //============================================================================
519 void SALOMEDS_StudyBuilder_i::Load(SALOMEDS::SObject_ptr sco)
520 {
521   MESSAGE ( "This function is not yet implemented");
522 }
523
524 //============================================================================
525 /*! Function : AddAttribute
526  *  Purpose  : 
527  */
528 //============================================================================
529 /*
530 void SALOMEDS_StudyBuilder_i::AddAttribute(SALOMEDS::SObject_ptr anObject, 
531                                            SALOMEDS::AttributeType aType, 
532                                            const char * AttributeValue )
533 {
534   TDF_Label Lab;
535   ASSERT(!CORBA::is_nil(anObject));
536   CORBA::String_var anobid = anObject->GetID();
537   TDF_Tool::Label(_doc->GetData(),strdup(anobid),Lab);
538   
539   TCollection_ExtendedString Value(strdup(AttributeValue)); 
540
541   Standard_GUID ID = SALOMEDS_SObject_i::TypeToAttributeID(aType);
542
543   if      (ID == TDataStd_Name::GetID())             TDataStd_Name::Set            (Lab,Value); 
544   else if (ID == TDataStd_Comment::GetID())          TDataStd_Comment::Set         (Lab,Value); 
545   else if (ID == SALOMEDS_IORAttribute::GetID())     SALOMEDS_IORAttribute::Set    (Lab,Value); 
546   else if (ID == SALOMEDS_PersRefAttribute::GetID()) SALOMEDS_PersRefAttribute::Set(Lab,Value); 
547 }
548 */
549 //============================================================================
550 /*! Function : FindOrCreateAttribute
551  *  Purpose  : Add attribute of given type to SObject, if there is attribute of such type, returns
552  *  existing one
553  */
554 //============================================================================
555
556 SALOMEDS::GenericAttribute_ptr SALOMEDS_StudyBuilder_i::FindOrCreateAttribute(SALOMEDS::SObject_ptr anObject, 
557                                                                               const char* aTypeOfAttribute)
558 {
559   TDF_Label Lab;
560   ASSERT(!CORBA::is_nil(anObject));
561   CORBA::String_var anobid = anObject->GetID();
562   TDF_Tool::Label(_doc->GetData(),strdup(anobid),Lab);
563
564   if (strcmp(aTypeOfAttribute, "AttributeReal") == 0 ) {
565     Handle(TDataStd_Real) anAttr;
566     if (!Lab.FindAttribute(TDataStd_Real::GetID(), anAttr)) {
567       CheckLocked();
568       anAttr = new TDataStd_Real;
569       Lab.AddAttribute(anAttr); 
570     }
571     SALOMEDS_AttributeReal_i* aRealAttr = new SALOMEDS_AttributeReal_i(anAttr, _orb);
572     SALOMEDS::AttributeReal_var aRA = aRealAttr->AttributeReal::_this();
573     return  aRA._retn();
574   }
575   else if (strcmp(aTypeOfAttribute, "AttributeInteger") == 0 ) {
576     Handle(TDataStd_Integer) anAttr;
577     if (!Lab.FindAttribute(TDataStd_Integer::GetID(), anAttr)) {
578       CheckLocked();
579       anAttr = new TDataStd_Integer;
580       Lab.AddAttribute(anAttr); 
581     }
582     SALOMEDS_AttributeInteger_i* aIntAttr = new SALOMEDS_AttributeInteger_i(anAttr, _orb);
583     SALOMEDS::AttributeInteger_var aIA = aIntAttr->AttributeInteger::_this();
584     return  aIA._retn();
585   }
586   else if (strcmp(aTypeOfAttribute, "AttributeSequenceOfReal") == 0 ) {
587     Handle(SALOMEDS_SequenceOfRealAttribute) anAttr;
588     if (!Lab.FindAttribute(SALOMEDS_SequenceOfRealAttribute::GetID(), anAttr)) {
589       CheckLocked();
590       anAttr = new SALOMEDS_SequenceOfRealAttribute;
591       Lab.AddAttribute(anAttr); 
592     }
593     SALOMEDS_AttributeSequenceOfReal_i* aSeqRealAttr = new SALOMEDS_AttributeSequenceOfReal_i(anAttr, _orb);
594     SALOMEDS::AttributeSequenceOfReal_var aSRA = aSeqRealAttr->AttributeSequenceOfReal::_this();
595     return  aSRA._retn();
596   }
597   else if (strcmp(aTypeOfAttribute, "AttributeSequenceOfInteger") == 0 ) {
598     Handle(SALOMEDS_SequenceOfIntegerAttribute) anAttr;
599     if (!Lab.FindAttribute(SALOMEDS_SequenceOfIntegerAttribute::GetID(), anAttr)) {
600       CheckLocked();
601       anAttr = new SALOMEDS_SequenceOfIntegerAttribute;
602       Lab.AddAttribute(anAttr); 
603     }
604     SALOMEDS_AttributeSequenceOfInteger_i* aSeqIntegerAttr = new SALOMEDS_AttributeSequenceOfInteger_i(anAttr, _orb);
605     SALOMEDS::AttributeSequenceOfInteger_var aSIA = aSeqIntegerAttr->AttributeSequenceOfInteger::_this();
606     return  aSIA._retn();
607   }
608   else if (strcmp(aTypeOfAttribute, "AttributeName") == 0 ) {
609     Handle(TDataStd_Name) anAttr;
610     if (!Lab.FindAttribute(TDataStd_Name::GetID(), anAttr)) {
611       CheckLocked();
612       anAttr = new TDataStd_Name;
613       Lab.AddAttribute(anAttr); 
614     }
615     SALOMEDS_AttributeName_i* aNameAttr = new SALOMEDS_AttributeName_i(anAttr, _orb);
616     SALOMEDS::AttributeName_var aSNA = aNameAttr->AttributeName::_this();
617     return  aSNA._retn();
618   }
619   else if (strcmp(aTypeOfAttribute, "AttributeComment") == 0 ) {
620     Handle(TDataStd_Comment) anAttr;
621     if (!Lab.FindAttribute(TDataStd_Comment::GetID(), anAttr)) {
622       CheckLocked();
623       anAttr = new TDataStd_Comment;
624       Lab.AddAttribute(anAttr); 
625     }
626     SALOMEDS_AttributeComment_i* aCommentAttr = new SALOMEDS_AttributeComment_i(anAttr, _orb);
627     SALOMEDS::AttributeComment_var aCA = aCommentAttr->AttributeComment::_this();
628     return  aCA._retn();
629   }
630   else if (strcmp(aTypeOfAttribute, "AttributeExternalFileDef") == 0 ) {
631     Handle(SALOMEDS_ExternalFileDef) anAttr;
632     if (!Lab.FindAttribute(SALOMEDS_ExternalFileDef::GetID(), anAttr)) {
633       anAttr = new SALOMEDS_ExternalFileDef;
634       Lab.AddAttribute(anAttr); 
635     }
636     SALOMEDS_AttributeExternalFileDef_i* anExternalFileDefAttr = new SALOMEDS_AttributeExternalFileDef_i(anAttr, _orb);
637     SALOMEDS::AttributeExternalFileDef_var aCA = anExternalFileDefAttr->AttributeExternalFileDef::_this();
638     return  aCA._retn();
639   }
640   else if (strcmp(aTypeOfAttribute, "AttributeFileType") == 0 ) {
641     Handle(SALOMEDS_FileType) anAttr;
642     if (!Lab.FindAttribute(SALOMEDS_FileType::GetID(), anAttr)) {
643       anAttr = new SALOMEDS_FileType;
644       Lab.AddAttribute(anAttr); 
645     }
646     SALOMEDS_AttributeFileType_i* anFileTypeAttr = new SALOMEDS_AttributeFileType_i(anAttr, _orb);
647     SALOMEDS::AttributeFileType_var aCA = anFileTypeAttr->AttributeFileType::_this();
648     return  aCA._retn();
649   }
650   else if (strcmp(aTypeOfAttribute, "AttributeIOR") == 0 ) {
651     Handle(SALOMEDS_IORAttribute) anAttr;
652     if (!Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr)) {
653       CheckLocked();
654       anAttr = new SALOMEDS_IORAttribute;
655       Lab.AddAttribute(anAttr); 
656     }
657     SALOMEDS_AttributeIOR_i* aIORAttr = new SALOMEDS_AttributeIOR_i(anAttr, _orb);
658     SALOMEDS::AttributeIOR_var aIA = aIORAttr->AttributeIOR::_this();
659     return  aIA._retn();
660   }
661   else if (strcmp(aTypeOfAttribute, "AttributePersistentRef") == 0 ) {
662     Handle(SALOMEDS_PersRefAttribute) anAttr;
663     if (!Lab.FindAttribute(SALOMEDS_PersRefAttribute::GetID(), anAttr)) {
664       anAttr = new SALOMEDS_PersRefAttribute;
665       Lab.AddAttribute(anAttr); 
666     }
667     SALOMEDS_AttributePersistentRef_i* aPerRefAttr = new SALOMEDS_AttributePersistentRef_i(anAttr, _orb);
668     SALOMEDS::AttributePersistentRef_var aPRA = aPerRefAttr->AttributePersistentRef::_this();
669     return  aPRA._retn();
670   }
671   else if (strcmp(aTypeOfAttribute, "AttributeDrawable") == 0 ) {
672     Handle(SALOMEDS_DrawableAttribute) anAttr;
673     if (!Lab.FindAttribute(SALOMEDS_DrawableAttribute::GetID(), anAttr)) {
674       anAttr = new SALOMEDS_DrawableAttribute;
675       Lab.AddAttribute(anAttr); 
676     }
677     SALOMEDS_AttributeDrawable_i* aDrawableAttr = new SALOMEDS_AttributeDrawable_i(anAttr, _orb);
678     SALOMEDS::AttributeDrawable_var aDrawA = aDrawableAttr->AttributeDrawable::_this();
679     return  aDrawA._retn();
680   }
681   else if (strcmp(aTypeOfAttribute, "AttributeSelectable") == 0 ) {
682     Handle(SALOMEDS_SelectableAttribute) anAttr;
683     if (!Lab.FindAttribute(SALOMEDS_SelectableAttribute::GetID(), anAttr)) {
684       anAttr = new SALOMEDS_SelectableAttribute;
685       Lab.AddAttribute(anAttr); 
686     }
687     SALOMEDS_AttributeSelectable_i* aSelectableAttr = new SALOMEDS_AttributeSelectable_i(anAttr, _orb);
688     SALOMEDS::AttributeSelectable_var aSelA = aSelectableAttr->AttributeSelectable::_this();
689     return  aSelA._retn();
690   }
691   else if (strcmp(aTypeOfAttribute, "AttributeExpandable") == 0 ) {
692     Handle(SALOMEDS_ExpandableAttribute) anAttr;
693     if (!Lab.FindAttribute(SALOMEDS_ExpandableAttribute::GetID(), anAttr)) {
694       anAttr = new SALOMEDS_ExpandableAttribute;
695       Lab.AddAttribute(anAttr); 
696     }
697     SALOMEDS_AttributeExpandable_i* aExpandableAttr = new SALOMEDS_AttributeExpandable_i(anAttr, _orb);
698     SALOMEDS::AttributeExpandable_var aExpA = aExpandableAttr->AttributeExpandable::_this();
699     return  aExpA._retn();
700   }
701   else if (strcmp(aTypeOfAttribute, "AttributeOpened") == 0 ) {
702     Handle(SALOMEDS_OpenedAttribute) anAttr;
703     if (!Lab.FindAttribute(SALOMEDS_OpenedAttribute::GetID(), anAttr)) {
704       anAttr = new SALOMEDS_OpenedAttribute;
705       Lab.AddAttribute(anAttr); 
706     }
707     SALOMEDS_AttributeOpened_i* aOpenedAttr = new SALOMEDS_AttributeOpened_i(anAttr, _orb);
708     SALOMEDS::AttributeOpened_var aOpenA = aOpenedAttr->AttributeOpened::_this();
709     return  aOpenA._retn();
710   }
711   else if (strcmp(aTypeOfAttribute, "AttributeTextColor") == 0 ) {
712     Handle(SALOMEDS_TextColorAttribute) anAttr;
713     if (!Lab.FindAttribute(SALOMEDS_TextColorAttribute::GetID(), anAttr)) {
714       CheckLocked();
715       anAttr = new SALOMEDS_TextColorAttribute;
716       Lab.AddAttribute(anAttr); 
717     }
718     SALOMEDS_AttributeTextColor_i* aTextColorAttr = new SALOMEDS_AttributeTextColor_i(anAttr, _orb);
719     SALOMEDS::AttributeTextColor_var aTCA = aTextColorAttr->AttributeTextColor::_this();
720     return  aTCA._retn();
721   }
722   else if (strcmp(aTypeOfAttribute, "AttributeTextHighlightColor") == 0 ) {
723     Handle(SALOMEDS_TextHighlightColorAttribute) anAttr;
724     if (!Lab.FindAttribute(SALOMEDS_TextHighlightColorAttribute::GetID(), anAttr)) {
725       CheckLocked();
726       anAttr = new SALOMEDS_TextHighlightColorAttribute;
727       Lab.AddAttribute(anAttr); 
728     }
729     SALOMEDS_AttributeTextHighlightColor_i* aTextHighlightColorAttr = new SALOMEDS_AttributeTextHighlightColor_i(anAttr, _orb);
730     SALOMEDS::AttributeTextHighlightColor_var aTHCA = aTextHighlightColorAttr->AttributeTextHighlightColor::_this();
731     return  aTHCA._retn();
732   }
733   else if (strcmp(aTypeOfAttribute, "AttributePixMap") == 0 ) {
734     Handle(SALOMEDS_PixMapAttribute) anAttr;
735     if (!Lab.FindAttribute(SALOMEDS_PixMapAttribute::GetID(), anAttr)) {
736       CheckLocked();
737       anAttr = new SALOMEDS_PixMapAttribute;
738       Lab.AddAttribute(anAttr); 
739     }
740     SALOMEDS_AttributePixMap_i* aPixMapAttr = new SALOMEDS_AttributePixMap_i(anAttr, _orb);
741     SALOMEDS::AttributePixMap_var aPMA = aPixMapAttr->AttributePixMap::_this();
742     return  aPMA._retn();
743   }
744   else if (strncmp(aTypeOfAttribute, "AttributeTreeNode",17) == 0 ) {
745     Standard_GUID aTreeNodeGUID;
746     if (strcmp(aTypeOfAttribute, "AttributeTreeNode") == 0) 
747       aTreeNodeGUID = TDataStd_TreeNode::GetDefaultTreeID();
748     else {
749       char* aGUIDString = new char[40];
750       sprintf(aGUIDString, &(aTypeOfAttribute[21]));
751       Standard_GUID aGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
752       delete(aGUIDString);
753     }
754     Handle(TDataStd_TreeNode) anAttr;
755     if (!Lab.FindAttribute(aTreeNodeGUID, anAttr)) {
756       CheckLocked();
757       anAttr = TDataStd_TreeNode::Set(Lab, aTreeNodeGUID);
758     }
759     SALOMEDS_AttributeTreeNode_i* aTreeNodeAttr = new SALOMEDS_AttributeTreeNode_i(anAttr, _orb);
760     SALOMEDS::AttributeTreeNode_var aTNA = aTreeNodeAttr->AttributeTreeNode::_this();
761     return  aTNA._retn();
762   }
763   else if (strncmp(aTypeOfAttribute, "AttributeUserID",15) == 0 ) {
764     Handle(TDataStd_UAttribute) anAttr;
765     if (!Lab.FindAttribute(SALOMEDS_AttributeUserID_i::DefaultID(), anAttr)) {
766       CheckLocked();
767       anAttr = TDataStd_UAttribute::Set(Lab, SALOMEDS_AttributeUserID_i::DefaultID());
768     }
769     SALOMEDS_AttributeUserID_i* aUAttr = new SALOMEDS_AttributeUserID_i(anAttr, _orb);
770     SALOMEDS::AttributeUserID_var aUA = aUAttr->AttributeUserID::_this();
771     return  aUA._retn();
772   }
773   else if (strcmp(aTypeOfAttribute, "AttributeLocalID") == 0 ) {
774     Handle(SALOMEDS_LocalIDAttribute) anAttr;
775     if (!Lab.FindAttribute(SALOMEDS_LocalIDAttribute::GetID(), anAttr)) {
776       CheckLocked();
777       anAttr = new SALOMEDS_LocalIDAttribute;
778       Lab.AddAttribute(anAttr); 
779     }
780     SALOMEDS_AttributeLocalID_i* aLIDAttr = new SALOMEDS_AttributeLocalID_i(anAttr, _orb);
781     SALOMEDS::AttributeLocalID_var aLIDA = aLIDAttr->AttributeLocalID::_this();
782     return  aLIDA._retn();
783   }
784   else if (strcmp(aTypeOfAttribute, "AttributeTarget") == 0 ) {
785     Handle(SALOMEDS_TargetAttribute) anAttr;
786     if (!Lab.FindAttribute(SALOMEDS_TargetAttribute::GetID(), anAttr)) {
787       anAttr = new SALOMEDS_TargetAttribute;
788       Lab.AddAttribute(anAttr); 
789     }
790     SALOMEDS_AttributeTarget_i* aLIDAttr = new SALOMEDS_AttributeTarget_i(anAttr, _orb);
791     SALOMEDS::AttributeTarget_var aLIDA = aLIDAttr->AttributeTarget::_this();
792     return  aLIDA._retn();
793   }
794   else if (strcmp(aTypeOfAttribute, "AttributeTableOfInteger") == 0 ) {
795     Handle(SALOMEDS_TableOfIntegerAttribute) anAttr;
796     if (!Lab.FindAttribute(SALOMEDS_TableOfIntegerAttribute::GetID(), anAttr)) {
797       CheckLocked();
798       anAttr = new SALOMEDS_TableOfIntegerAttribute;
799       Lab.AddAttribute(anAttr); 
800     }
801     SALOMEDS_AttributeTableOfInteger_i* aTabIntegerAttr = new SALOMEDS_AttributeTableOfInteger_i(anAttr, _orb);
802     SALOMEDS::AttributeTableOfInteger_var aTIA = aTabIntegerAttr->AttributeTableOfInteger::_this();
803     return  aTIA._retn();
804   }
805   else if (strcmp(aTypeOfAttribute, "AttributeTableOfReal") == 0 ) {
806     Handle(SALOMEDS_TableOfRealAttribute) anAttr;
807     if (!Lab.FindAttribute(SALOMEDS_TableOfRealAttribute::GetID(), anAttr)) {
808       CheckLocked();
809       anAttr = new SALOMEDS_TableOfRealAttribute;
810       Lab.AddAttribute(anAttr); 
811     }
812     SALOMEDS_AttributeTableOfReal_i* aTabRealAttr = new SALOMEDS_AttributeTableOfReal_i(anAttr, _orb);
813     SALOMEDS::AttributeTableOfReal_var aTRA = aTabRealAttr->AttributeTableOfReal::_this();
814     return  aTRA._retn();
815   }
816   else if (strcmp(aTypeOfAttribute, "AttributeTableOfString") == 0 ) {
817     Handle(SALOMEDS_TableOfStringAttribute) anAttr;
818     if (!Lab.FindAttribute(SALOMEDS_TableOfStringAttribute::GetID(), anAttr)) {
819       CheckLocked();
820       anAttr = new SALOMEDS_TableOfStringAttribute;
821       Lab.AddAttribute(anAttr); 
822     }
823     SALOMEDS_AttributeTableOfString_i* aTabStringAttr = new SALOMEDS_AttributeTableOfString_i(anAttr, _orb);
824     SALOMEDS::AttributeTableOfString_var aTRA = aTabStringAttr->AttributeTableOfString::_this();
825     return  aTRA._retn();
826   }
827   else if (strcmp(aTypeOfAttribute, "AttributeStudyProperties") == 0 ) {
828     Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
829     if (!Lab.FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
830       CheckLocked();
831       if (!Lab.FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
832         anAttr = new SALOMEDS_StudyPropertiesAttribute;
833         Lab.AddAttribute(anAttr); 
834         anAttr->SetModified(0);
835       }
836     }
837     SALOMEDS_AttributeStudyProperties_i* aStPropAttr = new SALOMEDS_AttributeStudyProperties_i(anAttr, _orb);
838     SALOMEDS::AttributeStudyProperties_var aSPA = aStPropAttr->AttributeStudyProperties::_this();
839     return  aSPA._retn();
840   }
841   else if (strcmp(aTypeOfAttribute, "AttributePythonObject") == 0 ) {
842     Handle(SALOMEDS_PythonObjectAttribute) anAttr;
843     if (!Lab.FindAttribute(SALOMEDS_PythonObjectAttribute::GetID(), anAttr)) {
844       CheckLocked();
845       anAttr = new SALOMEDS_PythonObjectAttribute;
846       Lab.AddAttribute(anAttr); 
847     }
848     SALOMEDS_AttributePythonObject_i* aPObjAttr = new SALOMEDS_AttributePythonObject_i(anAttr, _orb);
849     SALOMEDS::AttributePythonObject_var aPOA = aPObjAttr->AttributePythonObject::_this();
850     return  aPOA._retn();
851   }
852 }
853
854 //============================================================================
855 /*! Function : FindAttribute
856  *  Purpose  : Find attribute of given type assigned SObject, returns Standard_True if it is found
857  */
858 //============================================================================
859
860 CORBA::Boolean SALOMEDS_StudyBuilder_i::FindAttribute(SALOMEDS::SObject_ptr anObject, 
861                                                              SALOMEDS::GenericAttribute_out anAttribute, 
862                                                              const char* aTypeOfAttribute)
863 {
864   TDF_Label Lab;
865   ASSERT(!CORBA::is_nil(anObject));
866   CORBA::String_var anobid = anObject->GetID();
867   TDF_Tool::Label(_doc->GetData(),strdup(anobid),Lab);
868   Handle(TDF_Attribute) anAttr;
869   Standard_Boolean found = Lab.FindAttribute(SALOMEDS_SObject_i::ReturnGUIDForAttribute(aTypeOfAttribute), anAttr);
870   if (found) {
871     if (strcmp(aTypeOfAttribute, "AttributeReal") == 0 )  {
872       SALOMEDS_AttributeReal_i* Attr= new SALOMEDS_AttributeReal_i(Handle(TDataStd_Real)::DownCast(anAttr), _orb);
873       anAttribute = Attr->AttributeReal::_this();
874       return Standard_True;
875     }
876     else if (strcmp(aTypeOfAttribute, "AttributeInteger") == 0 )  {
877       SALOMEDS_AttributeInteger_i* Attr= new SALOMEDS_AttributeInteger_i(Handle(TDataStd_Integer)::DownCast(anAttr), _orb);
878       anAttribute = Attr->AttributeInteger::_this();
879       return Standard_True;
880     }
881     else if (strcmp(aTypeOfAttribute, "AttributeSequenceOfReal") == 0 )  {
882       SALOMEDS_AttributeSequenceOfReal_i* Attr= new SALOMEDS_AttributeSequenceOfReal_i(Handle(SALOMEDS_SequenceOfRealAttribute)::DownCast(anAttr), _orb);
883       anAttribute = Attr->AttributeSequenceOfReal::_this();
884       return Standard_True;
885     }
886     else if (strcmp(aTypeOfAttribute, "AttributeSequenceOfInteger") == 0 )  {
887       SALOMEDS_AttributeSequenceOfInteger_i* Attr= new SALOMEDS_AttributeSequenceOfInteger_i(Handle(SALOMEDS_SequenceOfIntegerAttribute)::DownCast(anAttr), _orb);
888       anAttribute = Attr->AttributeSequenceOfInteger::_this();
889       return Standard_True;
890     }
891     else if (strcmp(aTypeOfAttribute, "AttributeName") == 0 ) {
892       SALOMEDS_AttributeName_i* Attr= new SALOMEDS_AttributeName_i(Handle(TDataStd_Name)::DownCast(anAttr), _orb);
893       anAttribute = Attr->AttributeName::_this();
894       return Standard_True;
895     }
896     else if (strcmp(aTypeOfAttribute, "AttributeComment") == 0 ) {
897       SALOMEDS_AttributeComment_i* Attr= new SALOMEDS_AttributeComment_i(Handle(TDataStd_Comment)::DownCast(anAttr), _orb);
898       anAttribute = Attr->AttributeComment::_this();
899       return Standard_True;
900     }
901     else if (strcmp(aTypeOfAttribute, "AttributeExternalFileDef") == 0 ) {
902       SALOMEDS_AttributeExternalFileDef_i* Attr= new SALOMEDS_AttributeExternalFileDef_i(Handle(SALOMEDS_ExternalFileDef)::DownCast(anAttr), _orb);
903       anAttribute = Attr->AttributeExternalFileDef::_this();
904       return Standard_True;
905     }
906     else if (strcmp(aTypeOfAttribute, "AttributeFileType") == 0 ) {
907       SALOMEDS_AttributeFileType_i* Attr= new SALOMEDS_AttributeFileType_i(Handle(SALOMEDS_FileType)::DownCast(anAttr), _orb);
908       anAttribute = Attr->AttributeFileType::_this();
909       return Standard_True;
910     }
911     else if (strcmp(aTypeOfAttribute, "AttributeIOR") == 0 ) {
912       SALOMEDS_AttributeIOR_i* Attr= new SALOMEDS_AttributeIOR_i(Handle(SALOMEDS_IORAttribute)::DownCast(anAttr), _orb);
913       anAttribute = Attr->AttributeIOR::_this();
914       return Standard_True;
915     }
916     else if (strcmp(aTypeOfAttribute, "AttributePersistentRef") == 0 )  {
917       SALOMEDS_AttributePersistentRef_i* Attr= new SALOMEDS_AttributePersistentRef_i(Handle(SALOMEDS_PersRefAttribute)::DownCast(anAttr), _orb);
918       anAttribute = Attr->AttributePersistentRef::_this();
919       return Standard_True;
920     }
921     else if (strcmp(aTypeOfAttribute, "AttributeDrawable") == 0 )  {
922       SALOMEDS_AttributeDrawable_i* Attr= new SALOMEDS_AttributeDrawable_i(Handle(SALOMEDS_DrawableAttribute)::DownCast(anAttr), _orb);
923       anAttribute = Attr->AttributeDrawable::_this();
924       return Standard_True;
925     }
926     else if (strcmp(aTypeOfAttribute, "AttributeSelectable") == 0 )  {
927       SALOMEDS_AttributeSelectable_i* Attr= new SALOMEDS_AttributeSelectable_i(Handle(SALOMEDS_SelectableAttribute)::DownCast(anAttr), _orb);
928       anAttribute = Attr->AttributeSelectable::_this();
929       return Standard_True;
930     }
931     else if (strcmp(aTypeOfAttribute, "AttributeExpandable") == 0 )  {
932       SALOMEDS_AttributeExpandable_i* Attr= new SALOMEDS_AttributeExpandable_i(Handle(SALOMEDS_ExpandableAttribute)::DownCast(anAttr), _orb);
933       anAttribute = Attr->AttributeExpandable::_this();
934       return Standard_True;
935     }
936     else if (strcmp(aTypeOfAttribute, "AttributeOpened") == 0 )  {
937       SALOMEDS_AttributeOpened_i* Attr= new SALOMEDS_AttributeOpened_i(Handle(SALOMEDS_OpenedAttribute)::DownCast(anAttr), _orb);
938       anAttribute = Attr->AttributeOpened::_this();
939       return Standard_True;
940     }
941     else if (strcmp(aTypeOfAttribute, "AttributeTextColor") == 0 )  {
942       SALOMEDS_AttributeTextColor_i* Attr= new SALOMEDS_AttributeTextColor_i(Handle(SALOMEDS_TextColorAttribute)::DownCast(anAttr), _orb);
943       anAttribute = Attr->AttributeTextColor::_this();
944       return Standard_True;
945     }
946     else if (strcmp(aTypeOfAttribute, "AttributeTextHighlightColor") == 0 )  {
947       SALOMEDS_AttributeTextHighlightColor_i* Attr= new SALOMEDS_AttributeTextHighlightColor_i(Handle(SALOMEDS_TextHighlightColorAttribute)::DownCast(anAttr), _orb);
948       anAttribute = Attr->AttributeTextHighlightColor::_this();
949       return Standard_True;
950     }
951     else if (strcmp(aTypeOfAttribute, "AttributePixMap") == 0 )  {
952       SALOMEDS_AttributePixMap_i* Attr= new SALOMEDS_AttributePixMap_i(Handle(SALOMEDS_PixMapAttribute)::DownCast(anAttr), _orb);
953       anAttribute = Attr->AttributePixMap::_this();
954       return Standard_True;
955     }
956     else if (strncmp(aTypeOfAttribute, "AttributeTreeNode",17) == 0 )  {
957       SALOMEDS_AttributeTreeNode_i* Attr= new SALOMEDS_AttributeTreeNode_i(Handle(TDataStd_TreeNode)::DownCast(anAttr), _orb);
958       anAttribute = Attr->AttributeTreeNode::_this();
959       return Standard_True;
960     }
961     else if (strncmp(aTypeOfAttribute, "AttributeUserID",15) == 0 )  {
962       SALOMEDS_AttributeUserID_i* Attr= new SALOMEDS_AttributeUserID_i(Handle(TDataStd_UAttribute)::DownCast(anAttr), _orb);
963       anAttribute = Attr->AttributeUserID::_this();
964       return Standard_True;
965     }
966     else if (strcmp(aTypeOfAttribute, "AttributeLocalID") == 0 )  {
967       SALOMEDS_AttributeLocalID_i* Attr= new SALOMEDS_AttributeLocalID_i(Handle(SALOMEDS_LocalIDAttribute)::DownCast(anAttr), _orb);
968       anAttribute = Attr->AttributeLocalID::_this();
969       return Standard_True;
970     }
971     else if (strcmp(aTypeOfAttribute, "AttributeTarget") == 0 )  {
972       SALOMEDS_AttributeTarget_i* Attr= new SALOMEDS_AttributeTarget_i(Handle(SALOMEDS_TargetAttribute)::DownCast(anAttr), _orb);
973       anAttribute = Attr->AttributeTarget::_this();
974       return Standard_True;
975     }
976     else if (strcmp(aTypeOfAttribute, "AttributeTableOfInteger") == 0 )  {
977       SALOMEDS_AttributeTableOfInteger_i* Attr= new SALOMEDS_AttributeTableOfInteger_i(Handle(SALOMEDS_TableOfIntegerAttribute)::DownCast(anAttr), _orb);
978       anAttribute = Attr->AttributeTableOfInteger::_this();
979       return Standard_True;
980     }
981     else if (strcmp(aTypeOfAttribute, "AttributeTableOfReal") == 0 )  {
982       SALOMEDS_AttributeTableOfReal_i* Attr= new SALOMEDS_AttributeTableOfReal_i(Handle(SALOMEDS_TableOfRealAttribute)::DownCast(anAttr), _orb);
983       anAttribute = Attr->AttributeTableOfReal::_this();
984       return Standard_True;
985     }
986     else if (strcmp(aTypeOfAttribute, "AttributeTableOfString") == 0 )  {
987       SALOMEDS_AttributeTableOfString_i* Attr= new SALOMEDS_AttributeTableOfString_i(Handle(SALOMEDS_TableOfStringAttribute)::DownCast(anAttr), _orb);
988       anAttribute = Attr->AttributeTableOfString::_this();
989       return Standard_True;
990     }
991     else if (strcmp(aTypeOfAttribute, "AttributeStudyProperties") == 0 )  {
992       SALOMEDS_AttributeStudyProperties_i* Attr= new SALOMEDS_AttributeStudyProperties_i(Handle(SALOMEDS_StudyPropertiesAttribute)::DownCast(anAttr), _orb);
993       anAttribute = Attr->AttributeStudyProperties::_this();
994       return Standard_True;
995     }
996     else if (strcmp(aTypeOfAttribute, "AttributePythonObject") == 0 )  {
997       SALOMEDS_AttributePythonObject_i* Attr= new SALOMEDS_AttributePythonObject_i(Handle(SALOMEDS_PythonObjectAttribute)::DownCast(anAttr), _orb);
998       anAttribute = Attr->AttributePythonObject::_this();
999       return Standard_True;
1000     }
1001   } else return Standard_False;
1002 }
1003
1004 //============================================================================
1005 /*! Function : RemoveAttribute
1006  *  Purpose  : Remove attribute of given type assigned SObject
1007  */
1008 //============================================================================
1009
1010 void SALOMEDS_StudyBuilder_i::RemoveAttribute(SALOMEDS::SObject_ptr anObject, 
1011                                               const char* aTypeOfAttribute)
1012 {
1013   CheckLocked();
1014   TDF_Label Lab;
1015   ASSERT(!CORBA::is_nil(anObject));
1016   CORBA::String_var anobid = anObject->GetID();
1017   TDF_Tool::Label(_doc->GetData(),strdup(anobid),Lab);
1018   
1019   if (strcmp(aTypeOfAttribute, "AttributeIOR") == 0) { // postponed removing of CORBA objects
1020     Handle(SALOMEDS_IORAttribute) anAttr;
1021     if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
1022       SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(strdup(TCollection_AsciiString(anAttr->Get()).ToCString()));
1023     else return;
1024   }
1025
1026   Lab.ForgetAttribute (SALOMEDS_SObject_i::ReturnGUIDForAttribute(aTypeOfAttribute));
1027 }
1028
1029 //============================================================================
1030 /*! Function : Addreference
1031  *  Purpose  : 
1032  */
1033 //============================================================================
1034 void 
1035 SALOMEDS_StudyBuilder_i::Addreference(SALOMEDS::SObject_ptr me, 
1036                                       SALOMEDS::SObject_ptr theReferencedObject)
1037 {
1038   CheckLocked();
1039   TDF_Label Lab;
1040   ASSERT(!CORBA::is_nil(me));
1041   CORBA::String_var meid = me->GetID();
1042   TDF_Tool::Label(_doc->GetData(),strdup(meid),Lab);  
1043   TDF_Label RefLab;
1044   ASSERT(!CORBA::is_nil(theReferencedObject));
1045   CORBA::String_var roid = theReferencedObject->GetID();
1046   TDF_Tool::Label(_doc->GetData(),strdup(roid),RefLab);
1047   TDF_Reference::Set(Lab,RefLab);
1048
1049   SALOMEDS_TargetAttribute::Set(RefLab)->Append(Lab);
1050
1051   if(!CORBA::is_nil(_callbackOnRemove)) _callbackOnRemove->OnRemoveSObject(me);
1052 }
1053
1054
1055 //============================================================================
1056 /*! Function : AddDirectory
1057  *  Purpose  : adds a new directory with a path = thePath
1058  */
1059 //============================================================================
1060 void SALOMEDS_StudyBuilder_i::AddDirectory(const char* thePath) 
1061 {
1062   CheckLocked();
1063   if(thePath == NULL || strlen(thePath) == 0) throw SALOMEDS::Study::StudyInvalidDirectory();
1064
1065   TCollection_AsciiString aPath(CORBA::string_dup(thePath)), aContext(""), aFatherPath;
1066   TDF_ChildIterator anIterator(_doc->Main());
1067   Handle(TDataStd_Name) aName;
1068   TDF_Label aLabel;
1069   SALOMEDS_SObject_i* so_servant = new SALOMEDS_SObject_i (_doc->Main(), _orb);
1070   SALOMEDS::SObject_var anObject = SALOMEDS::SObject::_narrow(so_servant->_this()); 
1071   SALOMEDS::Study_var aStudy = anObject->GetStudy();
1072
1073   try { 
1074     anObject = aStudy->FindObjectByPath(thePath); //Check if the directory already exists
1075   }
1076   catch(...) { }
1077
1078   if(!anObject->_is_nil()) throw SALOMEDS::Study::StudyNameAlreadyUsed(); 
1079
1080   if(aPath.Value(1) != '/') { //Relative path 
1081     aPath.Prepend('/');
1082     aPath = TCollection_AsciiString(aStudy->GetContext()) + aPath;
1083   }
1084
1085   TCollection_AsciiString aToken = aPath.Token("/", 1);
1086   if(aToken.Length() == 0) aFatherPath = "/";
1087
1088   int i = 1;  
1089   while(aToken.Length() != 0) {
1090     if(aPath.Token("/", i+1).Length() > 0) {
1091       aFatherPath += "/";
1092       aFatherPath += aToken;
1093     }
1094     aToken = aPath.Token("/", ++i);
1095   }
1096
1097   anObject = SALOMEDS::SObject::_nil();
1098   try { 
1099     anObject = aStudy->FindObjectByPath(aFatherPath.ToCString()); //Check if the father directory exists
1100   }
1101   catch(...) { ; }
1102   if(anObject->_is_nil()) throw SALOMEDS::Study::StudyInvalidDirectory(); 
1103
1104   SALOMEDS::SObject_var aNewObject = NewObject(anObject);
1105   TDF_Tool::Label(_doc->GetData(), aNewObject->GetID(), aLabel);
1106   if(aLabel.IsNull()) {
1107     MESSAGE("### NULL label");
1108     throw SALOMEDS::Study::StudyInvalidComponent();      
1109   }
1110
1111   TDataStd_Name::Set(aLabel, aPath.Token("/", i-1));
1112
1113   //Set LocalID attribute to identify the directory object
1114   SALOMEDS::GenericAttribute_var anAttr = FindOrCreateAttribute(aNewObject, "AttributeLocalID");
1115   SALOMEDS::AttributeLocalID_var aPersRef = SALOMEDS::AttributeLocalID::_narrow(anAttr);
1116   if(aPersRef->_is_nil()) throw SALOMEDS::Study::StudyInvalidDirectory();
1117
1118   aPersRef->SetValue(DIRECTORYID);
1119 }
1120
1121
1122 //============================================================================
1123 /*! Function : SetGUID
1124  *  Purpose  : 
1125  */
1126 //============================================================================
1127 void SALOMEDS_StudyBuilder_i::SetGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
1128 {
1129   CheckLocked();
1130   TDF_Label aLabel;
1131   ASSERT(!CORBA::is_nil(anObject));
1132   CORBA::String_var anEntry = anObject->GetID();
1133   TDF_Tool::Label(_doc->GetData(), strdup(anEntry), aLabel);
1134   TDataStd_UAttribute::Set(aLabel, (char*)theGUID);
1135 }
1136
1137 //============================================================================
1138 /*! Function : IsGUID
1139  *  Purpose  : 
1140  */
1141 //============================================================================
1142 bool SALOMEDS_StudyBuilder_i::IsGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
1143 {
1144   TDF_Label aLabel;
1145   ASSERT(!CORBA::is_nil(anObject));
1146   CORBA::String_var anEntry = anObject->GetID();
1147   TDF_Tool::Label(_doc->GetData(), strdup(anEntry), aLabel);
1148   return aLabel.IsAttribute((char*)theGUID);
1149 }
1150
1151
1152 //============================================================================
1153 /*! Function : NewCommand
1154  *  Purpose  : 
1155  */
1156 //============================================================================
1157 void SALOMEDS_StudyBuilder_i::NewCommand()
1158 {
1159   // mpv: for SAL2114 - unset "lock changed" flag at the operation start
1160   Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
1161   if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
1162     anAttr = new SALOMEDS_StudyPropertiesAttribute;
1163     _doc->Main().AddAttribute(anAttr);
1164   }
1165   anAttr->IsLockChanged(true);
1166   
1167   _doc->NewCommand();
1168 }
1169
1170 //============================================================================
1171 /*! Function : CommitCommand
1172  *  Purpose  : 
1173  */
1174 //============================================================================
1175 void SALOMEDS_StudyBuilder_i::CommitCommand() throw (SALOMEDS::StudyBuilder::LockProtection)
1176 {
1177   Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
1178   if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
1179     anAttr = new SALOMEDS_StudyPropertiesAttribute;
1180     _doc->Main().AddAttribute(anAttr);
1181   }
1182   if (anAttr->IsLocked() && !anAttr->IsLockChanged(true)) {
1183     INFOS("Locked document modification !!!");
1184     AbortCommand();
1185     throw SALOMEDS::StudyBuilder::LockProtection();
1186   } else {
1187     SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->RemovePostponed(_doc->GetUndoLimit());
1188
1189     int aModif = anAttr->GetModified();
1190     if (aModif < 0) aModif = 1000; // if user make undo and then - new transaction "modify" will never be zero
1191     anAttr->SetModified(aModif+1);
1192     _doc->CommitCommand();
1193   }
1194 }
1195
1196 //============================================================================
1197 /*! Function : HasOpenCommand
1198  *  Purpose  : 
1199  */
1200 //============================================================================
1201 CORBA::Boolean SALOMEDS_StudyBuilder_i::HasOpenCommand()
1202 {
1203   _doc->HasOpenCommand();
1204 }
1205
1206 //============================================================================
1207 /*! Function : AbortCommand
1208  *  Purpose  : 
1209  */
1210 //============================================================================
1211 void SALOMEDS_StudyBuilder_i::AbortCommand()
1212 {
1213   SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->UndoPostponed(0);
1214   
1215   _doc->AbortCommand();
1216 }
1217
1218 //============================================================================
1219 /*! Function : Undo
1220  *  Purpose  : 
1221  */
1222 //============================================================================
1223 void SALOMEDS_StudyBuilder_i::Undo() throw (SALOMEDS::StudyBuilder::LockProtection)
1224 {
1225   Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
1226   if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
1227     anAttr = new SALOMEDS_StudyPropertiesAttribute;
1228     _doc->Main().AddAttribute(anAttr);
1229     }
1230   if (anAttr->IsLocked()) {
1231     INFOS("Locked document modification !!!");
1232     throw SALOMEDS::StudyBuilder::LockProtection();
1233   } else {
1234     SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->UndoPostponed(1);
1235     _doc->Undo();
1236     anAttr->SetModified(anAttr->GetModified()-1);
1237   }
1238 }
1239
1240 //============================================================================
1241 /*! Function : Redo
1242  *  Purpose  : 
1243  */
1244 //============================================================================
1245 void SALOMEDS_StudyBuilder_i::Redo() throw (SALOMEDS::StudyBuilder::LockProtection)
1246 {
1247    Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
1248     if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
1249       anAttr = new SALOMEDS_StudyPropertiesAttribute;
1250       _doc->Main().AddAttribute(anAttr);
1251     }
1252
1253   if (anAttr->IsLocked()) {
1254     INFOS("Locked document modification !!!");
1255     throw SALOMEDS::StudyBuilder::LockProtection();
1256   } else {
1257     _doc->Redo();
1258     SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->UndoPostponed(-1);
1259     anAttr->SetModified(anAttr->GetModified()+1);
1260   }
1261 }
1262
1263 //============================================================================
1264 /*! Function : GetAvailableUndos
1265  *  Purpose  : 
1266  */
1267 //============================================================================
1268 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableUndos()
1269 {
1270   return _doc->GetAvailableUndos();
1271 }
1272
1273 //============================================================================
1274 /*! Function : GetAvailableRedos
1275  *  Purpose  : 
1276  */
1277 //============================================================================
1278 CORBA::Boolean  SALOMEDS_StudyBuilder_i::GetAvailableRedos()
1279 {
1280   return _doc->GetAvailableRedos();
1281 }
1282
1283 //============================================================================
1284 /*! Function : UndoLimit
1285  *  Purpose  : 
1286  */
1287 //============================================================================
1288 CORBA::Long  SALOMEDS_StudyBuilder_i::UndoLimit()
1289 {
1290   return _doc->GetUndoLimit();
1291 }
1292
1293 //============================================================================
1294 /*! Function : UndoLimit
1295  *  Purpose  : 
1296  */
1297 //============================================================================
1298 void  SALOMEDS_StudyBuilder_i::UndoLimit(CORBA::Long n)
1299 {
1300   CheckLocked();
1301   _doc->SetUndoLimit (n);
1302 }
1303
1304 //============================================================================
1305 /*! Function : SetOnAddSObject
1306  *  Purpose  : 
1307  */
1308 //============================================================================
1309 SALOMEDS::Callback_ptr SALOMEDS_StudyBuilder_i::SetOnAddSObject(SALOMEDS::Callback_ptr theCallback)
1310 {
1311   SALOMEDS::Callback_ptr aRet = (CORBA::is_nil(_callbackOnAdd))?NULL:_callbackOnAdd._retn();
1312   _callbackOnAdd = SALOMEDS::Callback::_duplicate(theCallback);
1313   return aRet;
1314 }
1315
1316 //============================================================================
1317 /*! Function : SetOnNewSObject
1318  *  Purpose  : 
1319  */
1320 //============================================================================
1321 SALOMEDS::Callback_ptr SALOMEDS_StudyBuilder_i::SetOnRemoveSObject(SALOMEDS::Callback_ptr theCallback)
1322 {
1323   SALOMEDS::Callback_ptr aRet = (CORBA::is_nil(_callbackOnRemove))?NULL:_callbackOnRemove._retn();
1324   _callbackOnRemove = SALOMEDS::Callback::_duplicate(theCallback);
1325   return aRet;
1326 }
1327
1328 void SALOMEDS_StudyBuilder_i::CheckLocked() throw (SALOMEDS::StudyBuilder::LockProtection) {
1329   if (_doc->HasOpenCommand()) return;
1330   Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
1331   if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
1332     anAttr = new SALOMEDS_StudyPropertiesAttribute;
1333     _doc->Main().AddAttribute(anAttr);
1334   }
1335   if (anAttr->IsLocked()) throw SALOMEDS::StudyBuilder::LockProtection();
1336 }