1 // SALOME SALOMEDS : data structure of SALOME and sources of Salome data server
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOMEDS_StudyBuilder_i.cxx
25 // Author : Yves FRICAUD
29 #include "utilities.h"
30 #include "SALOMEDS_Study_i.hxx"
31 //#include "SALOMEDS_StudyBuilder_i.hxx"
32 #include "SALOMEDS_SObject_i.hxx"
33 #include "SALOMEDS_SComponent_i.hxx"
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 "SALOMEDS_ExternalFileDef.hxx"
41 #include "SALOMEDS_FileType.hxx"
42 #include <TDF_ChildIterator.hxx>
43 #include <TDF_Label.hxx>
44 #include <TDataStd_Name.hxx>
45 #include <TDataStd_Comment.hxx>
46 #include <TDataStd_UAttribute.hxx>
47 #include <TDataStd_Real.hxx>
48 #include <TDF_Tool.hxx>
49 #include <TDF_Reference.hxx>
50 #include <TDF_Data.hxx>
51 #include <TDataStd_ChildNodeIterator.hxx>
52 #include <TDF_ListIteratorOfAttributeList.hxx>
53 #include "SALOMEDS_AttributePersistentRef_i.hxx"
54 #include "SALOMEDS_AttributeIOR_i.hxx"
55 #include "SALOMEDS_AttributeExternalFileDef_i.hxx"
56 #include "SALOMEDS_AttributeFileType_i.hxx"
57 #include "SALOMEDS_AttributeComment_i.hxx"
58 #include "SALOMEDS_AttributeName_i.hxx"
59 #include "SALOMEDS_AttributeSequenceOfInteger_i.hxx"
60 #include "SALOMEDS_AttributeSequenceOfReal_i.hxx"
61 #include "SALOMEDS_AttributeTableOfInteger_i.hxx"
62 #include "SALOMEDS_AttributeTableOfReal_i.hxx"
63 #include "SALOMEDS_AttributeTableOfString_i.hxx"
64 #include "SALOMEDS_AttributeInteger_i.hxx"
65 #include "SALOMEDS_AttributeReal_i.hxx"
66 #include "SALOMEDS_AttributeDrawable_i.hxx"
67 #include "SALOMEDS_AttributeSelectable_i.hxx"
68 #include "SALOMEDS_AttributeExpandable_i.hxx"
69 #include "SALOMEDS_AttributeOpened_i.hxx"
70 #include "SALOMEDS_AttributeFlags_i.hxx"
71 #include "SALOMEDS_AttributeGraphic_i.hxx"
72 #include "SALOMEDS_AttributeTextColor_i.hxx"
73 #include "SALOMEDS_AttributeTextHighlightColor_i.hxx"
74 #include "SALOMEDS_AttributePixMap_i.hxx"
75 #include "SALOMEDS_AttributeTreeNode_i.hxx"
76 #include "SALOMEDS_AttributeTarget_i.hxx"
77 #include "SALOMEDS_AttributeLocalID_i.hxx"
78 #include "SALOMEDS_AttributeUserID_i.hxx"
79 #include "SALOMEDS_AttributeStudyProperties_i.hxx"
80 #include "SALOMEDS_AttributePythonObject_i.hxx"
81 #include "SALOMEDS_Tool.hxx"
82 #include "Utils_CorbaException.hxx"
83 #include "Utils_ExceptHandlers.hxx"
90 #define USE_CASE_LABEL_TAG 2
91 #define DIRECTORYID 16661
92 #define FILELOCALID 26662
94 UNEXPECT_CATCH(SBSalomeException, SALOME::SALOME_Exception);
95 UNEXPECT_CATCH(SBLockProtection, SALOMEDS::StudyBuilder::LockProtection);
96 //============================================================================
97 /*! Function : constructor
100 //============================================================================
101 SALOMEDS_StudyBuilder_i::SALOMEDS_StudyBuilder_i(const Handle(TDocStd_Document) doc,
102 CORBA::ORB_ptr orb) : _doc(doc)
104 _orb = CORBA::ORB::_duplicate(orb);
107 //============================================================================
108 /*! Function : destructor
111 //============================================================================
112 SALOMEDS_StudyBuilder_i::~SALOMEDS_StudyBuilder_i()
116 //============================================================================
117 /*! Function : NewComponent
118 * Purpose : Create a new component (Scomponent)
120 //============================================================================
121 SALOMEDS::SComponent_ptr
122 SALOMEDS_StudyBuilder_i::NewComponent(const char* DataType)
125 //Always create component under main label.
126 TDF_Label L = _doc->Main();
128 // YFR DEBUG : 13/02/2002 TDF_Label NL = L.NewChild();
130 Standard_Integer imax = 0;
131 for (TDF_ChildIterator it(L); it.More(); it.Next()) {
132 if (it.Value().Tag() > imax)
133 imax = it.Value().Tag();
136 TDF_Label NL = L.FindChild(imax);
138 TDataStd_Comment::Set(NL,Standard_CString(DataType));
139 // TDataStd_Comment::Set(NL,Standard_CString(CORBA::string_dup(DataType)));
141 SALOMEDS_SComponent_i * so_servant = new SALOMEDS_SComponent_i (NL,_orb);
142 SALOMEDS::SComponent_var so;
143 so= SALOMEDS::SComponent::_narrow(so_servant->SComponent::_this());
145 if(!CORBA::is_nil(_callbackOnAdd)) _callbackOnAdd->OnAddSObject(so);
150 //============================================================================
151 /*! Function : DefineComponentInstance
152 * Purpose : Add IOR attribute of a Scomponent
154 //============================================================================
155 void SALOMEDS_StudyBuilder_i::DefineComponentInstance(SALOMEDS::SComponent_ptr aComponent,
156 CORBA::Object_ptr IOR)
161 ASSERT(!CORBA::is_nil(aComponent));
162 CORBA::String_var compid = aComponent->GetID();
163 TDF_Tool::Label(_doc->GetData(),compid,Lab);
166 ASSERT(!CORBA::is_nil(IOR));
167 CORBA::String_var iorstr = _orb->object_to_string(IOR);
168 SALOMEDS_IORAttribute::Set(Lab,(char*)iorstr,_orb);
172 //============================================================================
173 /*! Function : RemoveComponent
174 * Purpose : Delete a Scomponent
176 //============================================================================
178 SALOMEDS_StudyBuilder_i::RemoveComponent(SALOMEDS::SComponent_ptr aComponent)
181 ASSERT(!CORBA::is_nil(aComponent));
182 RemoveObject(aComponent);
185 //============================================================================
186 /*! Function : NewObject
187 * Purpose : Create a new SObject
189 //============================================================================
190 SALOMEDS::SObject_ptr
191 SALOMEDS_StudyBuilder_i::NewObject(SALOMEDS::SObject_ptr theFatherObject)
194 TCollection_AsciiString anEntry;
196 //Find label of father
199 ASSERT(!CORBA::is_nil(theFatherObject));
200 CORBA::String_var fatherid = theFatherObject->GetID();
201 TDF_Tool::Label(_doc->GetData(),fatherid,Lab);
204 //YFR DEBUG : 13/02/2002 TDF_Label NewLab = Lab.NewChild();
205 Standard_Integer imax = 0;
206 for (TDF_ChildIterator it(Lab); it.More(); it.Next()) {
207 if (it.Value().Tag() > imax)
208 imax = it.Value().Tag();
211 TDF_Label NewLab = Lab.FindChild(imax);
213 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (NewLab,_orb);
214 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
216 if(!CORBA::is_nil(_callbackOnAdd)) _callbackOnAdd->OnAddSObject(so);
221 //============================================================================
222 /*! Function : NewObjectToTag
225 //============================================================================
226 SALOMEDS::SObject_ptr
227 SALOMEDS_StudyBuilder_i::NewObjectToTag(SALOMEDS::SObject_ptr theFatherObject,
231 //Find label of father
234 ASSERT(!CORBA::is_nil(theFatherObject));
235 CORBA::String_var fatherid = theFatherObject->GetID();
236 TDF_Tool::Label(_doc->GetData(),fatherid,Lab);
237 //Create or find label
238 TDF_Label NewLab = Lab.FindChild(atag,1);
240 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (NewLab,_orb);
241 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
243 if(!CORBA::is_nil(_callbackOnAdd)) _callbackOnAdd->OnAddSObject(so);
248 //============================================================================
249 /*! Function : RemoveObject
252 //============================================================================
253 void SALOMEDS_StudyBuilder_i::RemoveObject(SALOMEDS::SObject_ptr anObject)
256 if(!CORBA::is_nil(_callbackOnRemove)) _callbackOnRemove->OnRemoveSObject(anObject);
259 ASSERT(!CORBA::is_nil(anObject));
260 TDF_Tool::Label(_doc->GetData(),anObject->GetID(),Lab);
262 Handle(TDF_Reference) aReference;
263 if (Lab.FindAttribute(TDF_Reference::GetID(), aReference)) {
264 Handle(SALOMEDS_TargetAttribute) aTarget;
265 if (aReference->Get().FindAttribute(SALOMEDS_TargetAttribute::GetID(),aTarget))
266 aTarget->Remove(Lab);
269 Handle(SALOMEDS_IORAttribute) anAttr; // postponed removing of CORBA objects
270 if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
271 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(TCollection_AsciiString(anAttr->Get()).ToCString());
273 Lab.ForgetAllAttributes();
276 //============================================================================
277 /*! Function : RemoveObjectWithChildren
280 //============================================================================
281 void SALOMEDS_StudyBuilder_i::RemoveObjectWithChildren(SALOMEDS::SObject_ptr anObject)
284 if(!CORBA::is_nil(_callbackOnRemove)) _callbackOnRemove->OnRemoveSObject(anObject);
287 ASSERT(!CORBA::is_nil(anObject));
288 TDF_Tool::Label(_doc->GetData(),anObject->GetID(),Lab);
290 Handle(TDF_Reference) aReference;
291 if (Lab.FindAttribute(TDF_Reference::GetID(), aReference)) {
292 Handle(SALOMEDS_TargetAttribute) aTarget;
293 if (aReference->Get().FindAttribute(SALOMEDS_TargetAttribute::GetID(),aTarget))
294 aTarget->Remove(Lab);
296 Handle(SALOMEDS_IORAttribute) anAttr; // postponed removing of CORBA objects
297 if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
298 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(TCollection_AsciiString(anAttr->Get()).ToCString());
300 TDF_ChildIterator it(Lab, Standard_True);
301 for(;it.More();it.Next()) {
302 TDF_Label aLabel = it.Value();
303 if (aLabel.FindAttribute(TDF_Reference::GetID(), aReference)) {
304 Handle(SALOMEDS_TargetAttribute) aTarget;
305 if (aReference->Get().FindAttribute(SALOMEDS_TargetAttribute::GetID(),aTarget))
306 aTarget->Remove(aLabel);
308 Handle(SALOMEDS_IORAttribute) anAttr; // postponed removing of CORBA objects
309 if (aLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
310 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(TCollection_AsciiString(anAttr->Get()).ToCString());
313 Lab.ForgetAllAttributes(Standard_True);
316 //============================================================================
317 /*! Function : Translate_persistentID_to_IOR
320 //============================================================================
321 static void Translate_persistentID_to_IOR(TDF_Label Lab,
322 SALOMEDS::Driver_ptr driver,
324 CORBA::Boolean isMultiFile,
325 CORBA::Boolean isASCII)
327 TDF_ChildIterator itchild (Lab);
329 for (; itchild.More(); itchild.Next()) {
330 TDF_Label current = itchild.Value();
331 Handle(TDF_Attribute) Att;
332 if (current.FindAttribute(SALOMEDS_PersRefAttribute::GetID(),Att)) {
334 Handle(SALOMEDS_LocalIDAttribute) anID;
335 if (current.FindAttribute(SALOMEDS_LocalIDAttribute::GetID(), anID))
336 if (anID->Get() == FILELOCALID) continue; //SRN: This attribute store a file name, skip it
338 TCollection_ExtendedString res;
339 res = Handle(TDataStd_Comment)::DownCast(Att)->Get();
340 TCollection_AsciiString ch(res);
342 CORBA::String_var persistent_string = CORBA::string_dup(ch.ToCString());
343 ASSERT(! CORBA::is_nil(driver));
344 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (current,orb);
345 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
347 CORBA::String_var ior_string = driver->LocalPersistentIDToIOR(so, persistent_string, isMultiFile, isASCII);
349 TCollection_ExtendedString value(ior_string);
350 SALOMEDS_IORAttribute::Set (current,value,orb);
352 //TCollection_AsciiString anEntry;TDF_Tool::Entry (current,anEntry); //SRN: No use here
353 //delete persistent_string;
356 Translate_persistentID_to_IOR (current,driver,orb,isMultiFile, isASCII);
360 //============================================================================
361 /*! Function : LoadWith
364 //============================================================================
366 //============================================================================
367 void SALOMEDS_StudyBuilder_i::LoadWith(SALOMEDS::SComponent_ptr anSCO,
368 SALOMEDS::Driver_ptr aDriver) throw(SALOME::SALOME_Exception)
370 Unexpect aCatch(SBSalomeException);
372 ASSERT(!CORBA::is_nil(anSCO));
373 CORBA::String_var scoid = anSCO->GetID();
374 TDF_Tool::Label(_doc->GetData(),scoid,Lab);
375 Handle(TDF_Attribute) Att;
377 //Find the current Url of the study
378 if (_doc->Main().FindAttribute(SALOMEDS_PersRefAttribute::GetID(),Att)) {
379 int aLocked = anSCO->GetStudy()->GetProperties()->IsLocked();
380 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(false);
382 TCollection_ExtendedString Res = Handle(TDataStd_Comment)::DownCast(Att)->Get();
384 Handle(TDataStd_Comment) type;
385 TCollection_ExtendedString DataType;
386 if (Lab.FindAttribute(TDataStd_Comment::GetID(),type))
387 DataType = type->Get();
389 MESSAGE("No Data Type");
391 // associate the driver to the SComponent
392 ASSERT(!CORBA::is_nil(aDriver));
393 // mpv 06.03.2003: SAL1927 - if component data if already loaded, it is not necessary to do it again
394 if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), Att)) {
395 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
398 DefineComponentInstance (anSCO, aDriver);
400 TCollection_AsciiString aHDFPath(Res);
403 bool isASCII = false;
404 if (HDFascii::isASCII(aHDFPath.ToCString())) {
406 char* aResultPath = HDFascii::ConvertFromASCIIToHDF(aHDFPath.ToCString());
407 aHDFUrl = new char[strlen(aResultPath) + 19];
408 sprintf(aHDFUrl, "%shdf_from_ascii.hdf", aResultPath);
411 aHDFUrl = CORBA::string_dup(aHDFPath.ToCString());
414 //Open the Study HDF file
415 HDFfile *hdf_file = new HDFfile(aHDFUrl);
417 char aMultifileState[2];
418 char ASCIIfileState[2];
420 CORBA::String_var scoid = anSCO->GetID();
421 hdf_file->OpenOnDisk(HDF_RDONLY);
422 HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
423 hdf_group->OpenOnDisk();
424 HDFgroup *hdf_sco_group = new HDFgroup(scoid, hdf_group);
425 hdf_sco_group->OpenOnDisk();
427 SALOMEDS::TMPFile_var aStreamFile;
428 if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
429 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
430 hdf_dataset->OpenOnDisk();
431 int aStreamSize = hdf_dataset->GetSize();
432 unsigned char* aBuffer = new unsigned char[aStreamSize];
433 if(aBuffer == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
434 hdf_dataset->ReadFromDisk(aBuffer);
435 aStreamFile = new SALOMEDS::TMPFile(aStreamSize, aStreamSize, aBuffer, 1);
436 hdf_dataset->CloseOnDisk();
438 } else aStreamFile = new SALOMEDS::TMPFile(0);
440 HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
441 multifile_hdf_dataset->OpenOnDisk();
442 multifile_hdf_dataset->ReadFromDisk(aMultifileState);
444 HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
445 ascii_hdf_dataset->OpenOnDisk();
446 ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
448 // set path without file name from URL
449 int aFileNameSize = Res.Length();
450 char* aDir = new char[aFileNameSize];
451 memcpy(aDir, TCollection_AsciiString(Res).ToCString(), aFileNameSize);
452 for(int aCounter = aFileNameSize-1; aCounter>=0; aCounter--)
453 if (aDir[aCounter] == '/') {
454 aDir[aCounter+1] = 0;
458 CORBA::Boolean aResult = (ASCIIfileState[0]=='A')?
459 aDriver->LoadASCII(anSCO, aStreamFile.in(), aDir, aMultifileState[0]=='M'):
460 aDriver->Load(anSCO, aStreamFile.in(), aDir, aMultifileState[0]=='M');
462 RemoveAttribute( anSCO, "AttributeIOR" );
464 MESSAGE("Can't load component");
465 //THROW_SALOME_CORBA_EXCEPTION("Unable to load component data",SALOME::BAD_PARAM);
466 throw HDFexception("Unable to load component");
471 multifile_hdf_dataset->CloseOnDisk();
472 multifile_hdf_dataset = 0;
473 ascii_hdf_dataset->CloseOnDisk();
474 ascii_hdf_dataset = 0;
475 hdf_sco_group->CloseOnDisk();
477 hdf_group->CloseOnDisk();
479 hdf_file->CloseOnDisk();
483 SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
484 aFilesToRemove->length(1);
485 aFilesToRemove[0] = CORBA::string_dup(&(aHDFUrl[strlen(SALOMEDS_Tool::GetDirFromPath(aHDFUrl).c_str())]));
486 SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aHDFUrl).c_str(), aFilesToRemove, true);
490 catch (HDFexception) {
491 INFOS("No persistent file Name");
494 SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
495 aFilesToRemove->length(1);
496 aFilesToRemove[0] = CORBA::string_dup(&(aHDFUrl[strlen(SALOMEDS_Tool::GetDirFromPath(aHDFUrl).c_str())]));
497 SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aHDFUrl).c_str(), aFilesToRemove, true);
500 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
501 THROW_SALOME_CORBA_EXCEPTION("No persistent file Name found",SALOME::BAD_PARAM);
505 Translate_persistentID_to_IOR (Lab,aDriver,_orb, aMultifileState[0]=='M', ASCIIfileState[0] == 'A');
506 } catch (SALOME::SALOME_Exception) {
507 INFOS("Can't translate PersRef to IOR");
508 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
509 THROW_SALOME_CORBA_EXCEPTION("Unable to convert component persistent data to the transient",SALOME::BAD_PARAM);
510 // throw HDFexception("Unable to load component data");
512 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
514 MESSAGE("No persistent file Name");
518 //============================================================================
522 //============================================================================
523 void SALOMEDS_StudyBuilder_i::Load(SALOMEDS::SObject_ptr sco)
525 MESSAGE ( "This function is not yet implemented");
528 //============================================================================
529 /*! Function : FindOrCreateAttribute
530 * Purpose : Add attribute of given type to SObject, if there is attribute of such type, returns
533 //============================================================================
534 SALOMEDS::GenericAttribute_ptr SALOMEDS_StudyBuilder_i::FindOrCreateAttribute(SALOMEDS::SObject_ptr anObject,
535 const char* aTypeOfAttribute)
538 ASSERT(!CORBA::is_nil(anObject));
539 CORBA::String_var anobid = anObject->GetID();
540 TDF_Tool::Label(_doc->GetData(),anobid,Lab);
542 __FindOrCreateAttributeLocked(TDataStd_Real, AttributeReal)
543 __FindOrCreateAttributeLocked(TDataStd_Integer, AttributeInteger)
544 __FindOrCreateAttributeLocked(SALOMEDS_SequenceOfRealAttribute, AttributeSequenceOfReal)
545 __FindOrCreateAttributeLocked(SALOMEDS_SequenceOfIntegerAttribute, AttributeSequenceOfInteger)
546 __FindOrCreateAttributeLocked(TDataStd_Name, AttributeName)
547 __FindOrCreateAttributeLocked(TDataStd_Comment, AttributeComment)
548 __FindOrCreateAttributeLocked(SALOMEDS_IORAttribute, AttributeIOR)
549 __FindOrCreateAttributeLocked(SALOMEDS_PixMapAttribute, AttributePixMap)
550 __FindOrCreateAttributeLocked(SALOMEDS_LocalIDAttribute, AttributeLocalID)
551 __FindOrCreateAttributeLocked(SALOMEDS_TableOfIntegerAttribute, AttributeTableOfInteger)
552 __FindOrCreateAttributeLocked(SALOMEDS_TableOfRealAttribute, AttributeTableOfReal)
553 __FindOrCreateAttributeLocked(SALOMEDS_TableOfStringAttribute, AttributeTableOfString)
554 __FindOrCreateAttributeLocked(SALOMEDS_PythonObjectAttribute, AttributePythonObject)
556 __FindOrCreateAttribute(SALOMEDS_PersRefAttribute, AttributePersistentRef)
557 __FindOrCreateAttribute(SALOMEDS_DrawableAttribute, AttributeDrawable)
558 __FindOrCreateAttribute(SALOMEDS_SelectableAttribute, AttributeSelectable)
559 __FindOrCreateAttribute(SALOMEDS_ExpandableAttribute, AttributeExpandable)
560 __FindOrCreateAttribute(SALOMEDS_OpenedAttribute, AttributeOpened)
561 __FindOrCreateAttribute(SALOMEDS_TextColorAttribute, AttributeTextColor)
562 __FindOrCreateAttribute(SALOMEDS_TextHighlightColorAttribute, AttributeTextHighlightColor)
563 __FindOrCreateAttribute(SALOMEDS_TargetAttribute, AttributeTarget)
564 __FindOrCreateAttribute(SALOMEDS_StudyPropertiesAttribute, AttributeStudyProperties)
565 __FindOrCreateAttribute(SALOMEDS_ExternalFileDef, AttributeExternalFileDef)
566 __FindOrCreateAttribute(SALOMEDS_FileType, AttributeFileType)
567 __FindOrCreateAttribute(SALOMEDS_FlagsAttribute, AttributeFlags)
568 __FindOrCreateAttribute(SALOMEDS_GraphicAttribute, AttributeGraphic)
570 if (strncmp(aTypeOfAttribute, "AttributeTreeNode",17) == 0 ) {
571 Standard_GUID aTreeNodeGUID;
572 if (strcmp(aTypeOfAttribute, "AttributeTreeNode") == 0) {
573 aTreeNodeGUID = TDataStd_TreeNode::GetDefaultTreeID();
575 char* aGUIDString = new char[40];
576 sprintf(aGUIDString, &(aTypeOfAttribute[21]));
577 aTreeNodeGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
580 Handle(TDataStd_TreeNode) anAttr;
581 if (!Lab.FindAttribute(aTreeNodeGUID, anAttr)) {
583 anAttr = TDataStd_TreeNode::Set(Lab, aTreeNodeGUID);
585 SALOMEDS_AttributeTreeNode_i* aTreeNodeAttr = new SALOMEDS_AttributeTreeNode_i(anAttr, _orb);
586 return aTreeNodeAttr->AttributeTreeNode::_this();
589 if (strncmp(aTypeOfAttribute, "AttributeUserID",15) == 0 ) {
590 Handle(TDataStd_UAttribute) anAttr;
591 if (!Lab.FindAttribute(SALOMEDS_AttributeUserID_i::DefaultID(), anAttr)) {
593 anAttr = TDataStd_UAttribute::Set(Lab, SALOMEDS_AttributeUserID_i::DefaultID());
595 SALOMEDS_AttributeUserID_i* aUAttr = new SALOMEDS_AttributeUserID_i(anAttr, _orb);
596 return aUAttr->AttributeUserID::_this();
598 return SALOMEDS::GenericAttribute::_nil();
601 //============================================================================
602 /*! Function : FindAttribute
603 * Purpose : Find attribute of given type assigned SObject, returns Standard_True if it is found
605 //============================================================================
607 CORBA::Boolean SALOMEDS_StudyBuilder_i::FindAttribute(SALOMEDS::SObject_ptr anObject,
608 SALOMEDS::GenericAttribute_out anAttribute,
609 const char* aTypeOfAttribute)
612 ASSERT(!CORBA::is_nil(anObject));
613 CORBA::String_var anobid = anObject->GetID();
614 TDF_Tool::Label(_doc->GetData(),anobid,Lab);
615 Handle(TDF_Attribute) anAttr;
616 if (Lab.FindAttribute(SALOMEDS_GenericAttribute_i::GetGUID(aTypeOfAttribute), anAttr)) {
617 anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(_orb, anAttr));
618 return Standard_True;
620 return Standard_False;
623 //============================================================================
624 /*! Function : RemoveAttribute
625 * Purpose : Remove attribute of given type assigned SObject
627 //============================================================================
629 void SALOMEDS_StudyBuilder_i::RemoveAttribute(SALOMEDS::SObject_ptr anObject,
630 const char* aTypeOfAttribute)
634 ASSERT(!CORBA::is_nil(anObject));
635 CORBA::String_var anobid = anObject->GetID();
636 TDF_Tool::Label(_doc->GetData(),anobid,Lab);
638 if (strcmp(aTypeOfAttribute, "AttributeIOR") == 0) { // postponed removing of CORBA objects
639 Handle(SALOMEDS_IORAttribute) anAttr;
640 if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
641 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(TCollection_AsciiString(anAttr->Get()).ToCString());
645 Lab.ForgetAttribute (SALOMEDS_GenericAttribute_i::GetGUID(aTypeOfAttribute));
648 //============================================================================
649 /*! Function : Addreference
652 //============================================================================
654 SALOMEDS_StudyBuilder_i::Addreference(SALOMEDS::SObject_ptr me,
655 SALOMEDS::SObject_ptr theReferencedObject)
659 ASSERT(!CORBA::is_nil(me));
660 CORBA::String_var meid = me->GetID();
661 TDF_Tool::Label(_doc->GetData(),meid,Lab);
663 ASSERT(!CORBA::is_nil(theReferencedObject));
664 CORBA::String_var roid = theReferencedObject->GetID();
665 TDF_Tool::Label(_doc->GetData(),roid,RefLab);
666 TDF_Reference::Set(Lab,RefLab);
668 SALOMEDS_TargetAttribute::Set(RefLab)->Append(Lab);
670 if(!CORBA::is_nil(_callbackOnRemove) && Lab.IsDescendant(_doc->Main())) _callbackOnRemove->OnRemoveSObject(me);
673 //============================================================================
674 /*! Function : RemoveReference
677 //============================================================================
678 void SALOMEDS_StudyBuilder_i::RemoveReference(SALOMEDS::SObject_ptr me)
680 SALOMEDS::SObject_var theReferencedObject;
681 if(!me->ReferencedObject(theReferencedObject)) return; //No reference is found
685 ASSERT(!CORBA::is_nil(me));
686 CORBA::String_var meid = me->GetID();
687 TDF_Tool::Label(_doc->GetData(),meid,Lab);
689 Lab.ForgetAttribute(TDF_Reference::GetID());
691 //SRN: 30 Aug, 2004 : fix from Ecole l'ete version
694 ASSERT(!CORBA::is_nil(theReferencedObject));
695 CORBA::String_var roid = theReferencedObject->GetID();
696 TDF_Tool::Label(_doc->GetData(),roid,RefLab);
698 Handle(SALOMEDS_TargetAttribute) aTarget;
699 if(RefLab.FindAttribute(SALOMEDS_TargetAttribute::GetID(), aTarget)) aTarget->Remove(Lab);
704 //============================================================================
705 /*! Function : AddDirectory
706 * Purpose : adds a new directory with a path = thePath
708 //============================================================================
709 void SALOMEDS_StudyBuilder_i::AddDirectory(const char* thePath)
712 if(thePath == NULL || strlen(thePath) == 0) throw SALOMEDS::Study::StudyInvalidDirectory();
714 TCollection_AsciiString aPath(CORBA::string_dup(thePath)), aContext(""), aFatherPath;
715 TDF_ChildIterator anIterator(_doc->Main());
716 Handle(TDataStd_Name) aName;
718 SALOMEDS_SObject_i* so_servant = new SALOMEDS_SObject_i (_doc->Main(), _orb);
719 SALOMEDS::SObject_var anObject = SALOMEDS::SObject::_narrow(so_servant->_this());
720 SALOMEDS::Study_var aStudy = anObject->GetStudy();
723 anObject = aStudy->FindObjectByPath(thePath); //Check if the directory already exists
727 if(!anObject->_is_nil()) throw SALOMEDS::Study::StudyNameAlreadyUsed();
729 if(aPath.Value(1) != '/') { //Relative path
731 aPath = TCollection_AsciiString(aStudy->GetContext()) + aPath;
734 TCollection_AsciiString aToken = aPath.Token("/", 1);
735 if(aToken.Length() == 0) aFatherPath = "/";
738 while(aToken.Length() != 0) {
739 if(aPath.Token("/", i+1).Length() > 0) {
741 aFatherPath += aToken;
743 aToken = aPath.Token("/", ++i);
746 anObject = SALOMEDS::SObject::_nil();
748 anObject = aStudy->FindObjectByPath(aFatherPath.ToCString()); //Check if the father directory exists
751 if(anObject->_is_nil()) throw SALOMEDS::Study::StudyInvalidDirectory();
753 SALOMEDS::SObject_var aNewObject = NewObject(anObject);
754 TDF_Tool::Label(_doc->GetData(), aNewObject->GetID(), aLabel);
755 if(aLabel.IsNull()) {
756 MESSAGE("### NULL label");
757 throw SALOMEDS::Study::StudyInvalidComponent();
760 TDataStd_Name::Set(aLabel, aPath.Token("/", i-1));
762 //Set LocalID attribute to identify the directory object
763 SALOMEDS::GenericAttribute_var anAttr = FindOrCreateAttribute(aNewObject, "AttributeLocalID");
764 SALOMEDS::AttributeLocalID_var aPersRef = SALOMEDS::AttributeLocalID::_narrow(anAttr);
765 if(aPersRef->_is_nil()) throw SALOMEDS::Study::StudyInvalidDirectory();
767 aPersRef->SetValue(DIRECTORYID);
771 //============================================================================
772 /*! Function : SetGUID
775 //============================================================================
776 void SALOMEDS_StudyBuilder_i::SetGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
780 ASSERT(!CORBA::is_nil(anObject));
781 CORBA::String_var anEntry = anObject->GetID();
782 TDF_Tool::Label(_doc->GetData(), anEntry, aLabel);
783 TDataStd_UAttribute::Set(aLabel, (char*)theGUID);
786 //============================================================================
787 /*! Function : IsGUID
790 //============================================================================
791 bool SALOMEDS_StudyBuilder_i::IsGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
794 ASSERT(!CORBA::is_nil(anObject));
795 CORBA::String_var anEntry = anObject->GetID();
796 TDF_Tool::Label(_doc->GetData(), anEntry, aLabel);
797 return aLabel.IsAttribute((char*)theGUID);
801 //============================================================================
802 /*! Function : NewCommand
805 //============================================================================
806 void SALOMEDS_StudyBuilder_i::NewCommand()
808 // mpv: for SAL2114 - unset "lock changed" flag at the operation start
809 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
810 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
811 anAttr = new SALOMEDS_StudyPropertiesAttribute;
812 _doc->Main().AddAttribute(anAttr);
814 anAttr->IsLockChanged(true);
819 //============================================================================
820 /*! Function : CommitCommand
823 //============================================================================
824 void SALOMEDS_StudyBuilder_i::CommitCommand() throw (SALOMEDS::StudyBuilder::LockProtection)
826 Unexpect aCatch(SBLockProtection);
827 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
828 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
829 anAttr = new SALOMEDS_StudyPropertiesAttribute;
830 _doc->Main().AddAttribute(anAttr);
832 if (anAttr->IsLocked() && !anAttr->IsLockChanged(true)) {
833 MESSAGE("Locked document modification !!!");
835 throw SALOMEDS::StudyBuilder::LockProtection();
837 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->RemovePostponed(_doc->GetUndoLimit());
839 int aModif = anAttr->GetModified();
840 if (aModif < 0) aModif = 1000; // if user make undo and then - new transaction "modify" will never be zero
841 anAttr->SetModified(aModif+1);
842 _doc->CommitCommand();
846 //============================================================================
847 /*! Function : HasOpenCommand
850 //============================================================================
851 CORBA::Boolean SALOMEDS_StudyBuilder_i::HasOpenCommand()
853 return _doc->HasOpenCommand();
856 //============================================================================
857 /*! Function : AbortCommand
860 //============================================================================
861 void SALOMEDS_StudyBuilder_i::AbortCommand()
863 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->UndoPostponed(0);
865 _doc->AbortCommand();
868 //============================================================================
872 //============================================================================
873 void SALOMEDS_StudyBuilder_i::Undo() throw (SALOMEDS::StudyBuilder::LockProtection)
875 Unexpect aCatch(SBLockProtection);
876 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
877 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
878 anAttr = new SALOMEDS_StudyPropertiesAttribute;
879 _doc->Main().AddAttribute(anAttr);
881 if (anAttr->IsLocked()) {
882 MESSAGE("Locked document modification !!!");
883 throw SALOMEDS::StudyBuilder::LockProtection();
885 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->UndoPostponed(1);
887 anAttr->SetModified(anAttr->GetModified()-1);
891 //============================================================================
895 //============================================================================
896 void SALOMEDS_StudyBuilder_i::Redo() throw (SALOMEDS::StudyBuilder::LockProtection)
898 Unexpect aCatch(SBLockProtection);
899 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
900 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
901 anAttr = new SALOMEDS_StudyPropertiesAttribute;
902 _doc->Main().AddAttribute(anAttr);
905 if (anAttr->IsLocked()) {
906 MESSAGE("Locked document modification !!!");
907 throw SALOMEDS::StudyBuilder::LockProtection();
910 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->UndoPostponed(-1);
911 anAttr->SetModified(anAttr->GetModified()+1);
915 //============================================================================
916 /*! Function : GetAvailableUndos
919 //============================================================================
920 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableUndos()
922 return _doc->GetAvailableUndos();
925 //============================================================================
926 /*! Function : GetAvailableRedos
929 //============================================================================
930 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableRedos()
932 return _doc->GetAvailableRedos();
935 //============================================================================
936 /*! Function : UndoLimit
939 //============================================================================
940 CORBA::Long SALOMEDS_StudyBuilder_i::UndoLimit()
942 return _doc->GetUndoLimit();
945 //============================================================================
946 /*! Function : UndoLimit
949 //============================================================================
950 void SALOMEDS_StudyBuilder_i::UndoLimit(CORBA::Long n)
953 _doc->SetUndoLimit (n);
956 //============================================================================
957 /*! Function : SetOnAddSObject
960 //============================================================================
961 SALOMEDS::Callback_ptr SALOMEDS_StudyBuilder_i::SetOnAddSObject(SALOMEDS::Callback_ptr theCallback)
963 SALOMEDS::Callback_ptr aRet = (CORBA::is_nil(_callbackOnAdd))?NULL:_callbackOnAdd._retn();
964 _callbackOnAdd = SALOMEDS::Callback::_duplicate(theCallback);
968 //============================================================================
969 /*! Function : SetOnNewSObject
972 //============================================================================
973 SALOMEDS::Callback_ptr SALOMEDS_StudyBuilder_i::SetOnRemoveSObject(SALOMEDS::Callback_ptr theCallback)
975 SALOMEDS::Callback_ptr aRet = (CORBA::is_nil(_callbackOnRemove))?NULL:_callbackOnRemove._retn();
976 _callbackOnRemove = SALOMEDS::Callback::_duplicate(theCallback);
980 //============================================================================
981 /*! Function : CheckLocked
984 //============================================================================
985 void SALOMEDS_StudyBuilder_i::CheckLocked() throw (SALOMEDS::StudyBuilder::LockProtection) {
986 Unexpect aCatch(SBLockProtection);
987 if (_doc->HasOpenCommand()) return;
988 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
989 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
990 anAttr = new SALOMEDS_StudyPropertiesAttribute;
991 _doc->Main().AddAttribute(anAttr);
993 if (anAttr->IsLocked()) throw SALOMEDS::StudyBuilder::LockProtection();
996 //============================================================================
997 /*! Function : SetName
1000 //============================================================================
1001 void SALOMEDS_StudyBuilder_i::SetName(SALOMEDS::SObject_ptr theSO, const char* theValue)
1002 throw(SALOMEDS::StudyBuilder::LockProtection)
1004 Unexpect aCatch(SBLockProtection);
1008 ASSERT(!CORBA::is_nil(theSO));
1009 CORBA::String_var aSOID = theSO->GetID();
1010 TDF_Tool::Label(_doc->GetData(), aSOID, aLabel);
1011 TDataStd_Name::Set(aLabel, (char*)theValue);
1014 //============================================================================
1015 /*! Function : SetComment
1018 //============================================================================
1019 void SALOMEDS_StudyBuilder_i::SetComment(SALOMEDS::SObject_ptr theSO, const char* theValue)
1020 throw(SALOMEDS::StudyBuilder::LockProtection)
1022 Unexpect aCatch(SBLockProtection);
1026 ASSERT(!CORBA::is_nil(theSO));
1027 CORBA::String_var aSOID = theSO->GetID();
1028 TDF_Tool::Label(_doc->GetData(), aSOID, aLabel);
1029 TDataStd_Comment::Set(aLabel, (char*)theValue);
1032 //============================================================================
1033 /*! Function : SetIOR
1036 //============================================================================
1037 void SALOMEDS_StudyBuilder_i::SetIOR(SALOMEDS::SObject_ptr theSO, const char* theValue)
1038 throw(SALOMEDS::StudyBuilder::LockProtection)
1040 Unexpect aCatch(SBLockProtection);
1044 ASSERT(!CORBA::is_nil(theSO));
1045 CORBA::String_var aSOID = theSO->GetID();
1046 TDF_Tool::Label(_doc->GetData(), aSOID, aLabel);
1047 SALOMEDS_IORAttribute::Set(aLabel, TCollection_ExtendedString((char*)theValue), _orb);