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