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