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