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
30 #include "utilities.h"
31 #include "SALOMEDS_Study_i.hxx"
32 //#include "SALOMEDS_StudyBuilder_i.hxx"
33 #include "SALOMEDS_SObject_i.hxx"
34 #include "SALOMEDS_SComponent_i.hxx"
36 #include "SALOMEDS_IORAttribute.hxx"
37 #include "SALOMEDS_PersRefAttribute.hxx"
38 #include "SALOMEDS_TargetAttribute.hxx"
39 #include "SALOMEDS_StudyPropertiesAttribute.hxx"
40 #include "SALOMEDS_PythonObjectAttribute.hxx"
41 #include "SALOMEDS_ExternalFileDef.hxx"
42 #include "SALOMEDS_FileType.hxx"
43 #include <TDF_ChildIterator.hxx>
44 #include <TDF_Label.hxx>
45 #include <TDataStd_Name.hxx>
46 #include <TDataStd_Comment.hxx>
47 #include <TDataStd_UAttribute.hxx>
48 #include <TDataStd_Real.hxx>
49 #include <TDF_Tool.hxx>
50 #include <TDF_Reference.hxx>
51 #include <TDF_Data.hxx>
52 #include <TDataStd_ChildNodeIterator.hxx>
53 #include <TDF_ListIteratorOfAttributeList.hxx>
54 #include "SALOMEDS_AttributePersistentRef_i.hxx"
55 #include "SALOMEDS_AttributeIOR_i.hxx"
56 #include "SALOMEDS_AttributeExternalFileDef_i.hxx"
57 #include "SALOMEDS_AttributeFileType_i.hxx"
58 #include "SALOMEDS_AttributeComment_i.hxx"
59 #include "SALOMEDS_AttributeName_i.hxx"
60 #include "SALOMEDS_AttributeSequenceOfInteger_i.hxx"
61 #include "SALOMEDS_AttributeSequenceOfReal_i.hxx"
62 #include "SALOMEDS_AttributeTableOfInteger_i.hxx"
63 #include "SALOMEDS_AttributeTableOfReal_i.hxx"
64 #include "SALOMEDS_AttributeTableOfString_i.hxx"
65 #include "SALOMEDS_AttributeInteger_i.hxx"
66 #include "SALOMEDS_AttributeReal_i.hxx"
67 #include "SALOMEDS_AttributeDrawable_i.hxx"
68 #include "SALOMEDS_AttributeSelectable_i.hxx"
69 #include "SALOMEDS_AttributeExpandable_i.hxx"
70 #include "SALOMEDS_AttributeOpened_i.hxx"
71 #include "SALOMEDS_AttributeTextColor_i.hxx"
72 #include "SALOMEDS_AttributeTextHighlightColor_i.hxx"
73 #include "SALOMEDS_AttributePixMap_i.hxx"
74 #include "SALOMEDS_AttributeTreeNode_i.hxx"
75 #include "SALOMEDS_AttributeTarget_i.hxx"
76 #include "SALOMEDS_AttributeLocalID_i.hxx"
77 #include "SALOMEDS_AttributeUserID_i.hxx"
78 #include "SALOMEDS_AttributeStudyProperties_i.hxx"
79 #include "SALOMEDS_AttributePythonObject_i.hxx"
80 #include "SALOMEDS_Tool.hxx"
81 #include "Utils_CorbaException.hxx"
82 #include "Utils_ExceptHandlers.hxx"
87 #define USE_CASE_LABEL_TAG 2
88 #define DIRECTORYID 16661
89 #define FILELOCALID 26662
91 UNEXPECT_CATCH(SBSalomeException, SALOME::SALOME_Exception);
92 UNEXPECT_CATCH(SBLockProtection, SALOMEDS::StudyBuilder::LockProtection);
93 //============================================================================
94 /*! Function : constructor
97 //============================================================================
98 SALOMEDS_StudyBuilder_i::SALOMEDS_StudyBuilder_i(const Handle(TDocStd_Document) doc,
99 CORBA::ORB_ptr orb) : _doc(doc)
101 _orb = CORBA::ORB::_duplicate(orb);
104 //============================================================================
105 /*! Function : destructor
108 //============================================================================
109 SALOMEDS_StudyBuilder_i::~SALOMEDS_StudyBuilder_i()
113 //============================================================================
114 /*! Function : NewComponent
115 * Purpose : Create a new component (Scomponent)
117 //============================================================================
118 SALOMEDS::SComponent_ptr
119 SALOMEDS_StudyBuilder_i::NewComponent(const char* DataType)
122 //Always create component under main label.
123 TDF_Label L = _doc->Main();
125 // YFR DEBUG : 13/02/2002 TDF_Label NL = L.NewChild();
127 Standard_Integer imax = 0;
128 for (TDF_ChildIterator it(L); it.More(); it.Next()) {
129 if (it.Value().Tag() > imax)
130 imax = it.Value().Tag();
133 TDF_Label NL = L.FindChild(imax);
135 TDataStd_Comment::Set(NL,Standard_CString(DataType));
136 // TDataStd_Comment::Set(NL,Standard_CString(CORBA::string_dup(DataType)));
138 SALOMEDS_SComponent_i * so_servant = new SALOMEDS_SComponent_i (NL,_orb);
139 SALOMEDS::SComponent_var so;
140 so= SALOMEDS::SComponent::_narrow(so_servant->SComponent::_this());
142 if(!CORBA::is_nil(_callbackOnAdd)) _callbackOnAdd->OnAddSObject(so);
147 //============================================================================
148 /*! Function : DefineComponentInstance
149 * Purpose : Add IOR attribute of a Scomponent
151 //============================================================================
152 void SALOMEDS_StudyBuilder_i::DefineComponentInstance(SALOMEDS::SComponent_ptr aComponent,
153 CORBA::Object_ptr IOR)
158 ASSERT(!CORBA::is_nil(aComponent));
159 CORBA::String_var compid = aComponent->GetID();
160 TDF_Tool::Label(_doc->GetData(),compid,Lab);
163 ASSERT(!CORBA::is_nil(IOR));
164 CORBA::String_var iorstr = _orb->object_to_string(IOR);
165 SALOMEDS_IORAttribute::Set(Lab,(char*)iorstr,_orb);
169 //============================================================================
170 /*! Function : RemoveComponent
171 * Purpose : Delete a Scomponent
173 //============================================================================
175 SALOMEDS_StudyBuilder_i::RemoveComponent(SALOMEDS::SComponent_ptr aComponent)
178 ASSERT(!CORBA::is_nil(aComponent));
179 RemoveObject(aComponent);
182 //============================================================================
183 /*! Function : NewObject
184 * Purpose : Create a new SObject
186 //============================================================================
187 SALOMEDS::SObject_ptr
188 SALOMEDS_StudyBuilder_i::NewObject(SALOMEDS::SObject_ptr theFatherObject)
191 TCollection_AsciiString anEntry;
193 //Find label of father
196 ASSERT(!CORBA::is_nil(theFatherObject));
197 CORBA::String_var fatherid = theFatherObject->GetID();
198 TDF_Tool::Label(_doc->GetData(),fatherid,Lab);
201 //YFR DEBUG : 13/02/2002 TDF_Label NewLab = Lab.NewChild();
202 Standard_Integer imax = 0;
203 for (TDF_ChildIterator it(Lab); it.More(); it.Next()) {
204 if (it.Value().Tag() > imax)
205 imax = it.Value().Tag();
208 TDF_Label NewLab = Lab.FindChild(imax);
210 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (NewLab,_orb);
211 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
213 if(!CORBA::is_nil(_callbackOnAdd)) _callbackOnAdd->OnAddSObject(so);
218 //============================================================================
219 /*! Function : NewObjectToTag
222 //============================================================================
223 SALOMEDS::SObject_ptr
224 SALOMEDS_StudyBuilder_i::NewObjectToTag(SALOMEDS::SObject_ptr theFatherObject,
228 //Find label of father
231 ASSERT(!CORBA::is_nil(theFatherObject));
232 CORBA::String_var fatherid = theFatherObject->GetID();
233 TDF_Tool::Label(_doc->GetData(),fatherid,Lab);
234 //Create or find label
235 TDF_Label NewLab = Lab.FindChild(atag,1);
237 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (NewLab,_orb);
238 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
240 if(!CORBA::is_nil(_callbackOnAdd)) _callbackOnAdd->OnAddSObject(so);
245 //============================================================================
246 /*! Function : RemoveObject
249 //============================================================================
250 void SALOMEDS_StudyBuilder_i::RemoveObject(SALOMEDS::SObject_ptr anObject)
253 if(!CORBA::is_nil(_callbackOnRemove)) _callbackOnRemove->OnRemoveSObject(anObject);
256 ASSERT(!CORBA::is_nil(anObject));
257 TDF_Tool::Label(_doc->GetData(),anObject->GetID(),Lab);
259 Handle(TDF_Reference) aReference;
260 if (Lab.FindAttribute(TDF_Reference::GetID(), aReference)) {
261 Handle(SALOMEDS_TargetAttribute) aTarget;
262 if (aReference->Get().FindAttribute(SALOMEDS_TargetAttribute::GetID(),aTarget))
263 aTarget->Remove(Lab);
266 Handle(SALOMEDS_IORAttribute) anAttr; // postponed removing of CORBA objects
267 if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
268 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(TCollection_AsciiString(anAttr->Get()).ToCString());
270 Lab.ForgetAllAttributes();
273 //============================================================================
274 /*! Function : RemoveObjectWithChildren
277 //============================================================================
278 void SALOMEDS_StudyBuilder_i::RemoveObjectWithChildren(SALOMEDS::SObject_ptr anObject)
281 if(!CORBA::is_nil(_callbackOnRemove)) _callbackOnRemove->OnRemoveSObject(anObject);
284 ASSERT(!CORBA::is_nil(anObject));
285 TDF_Tool::Label(_doc->GetData(),anObject->GetID(),Lab);
287 Handle(TDF_Reference) aReference;
288 if (Lab.FindAttribute(TDF_Reference::GetID(), aReference)) {
289 Handle(SALOMEDS_TargetAttribute) aTarget;
290 if (aReference->Get().FindAttribute(SALOMEDS_TargetAttribute::GetID(),aTarget))
291 aTarget->Remove(Lab);
293 Handle(SALOMEDS_IORAttribute) anAttr; // postponed removing of CORBA objects
294 if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
295 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(TCollection_AsciiString(anAttr->Get()).ToCString());
297 TDF_ChildIterator it(Lab, Standard_True);
298 for(;it.More();it.Next()) {
299 TDF_Label aLabel = it.Value();
300 if (aLabel.FindAttribute(TDF_Reference::GetID(), aReference)) {
301 Handle(SALOMEDS_TargetAttribute) aTarget;
302 if (aReference->Get().FindAttribute(SALOMEDS_TargetAttribute::GetID(),aTarget))
303 aTarget->Remove(aLabel);
305 Handle(SALOMEDS_IORAttribute) anAttr; // postponed removing of CORBA objects
306 if (aLabel.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
307 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(TCollection_AsciiString(anAttr->Get()).ToCString());
310 Lab.ForgetAllAttributes(Standard_True);
313 //============================================================================
314 /*! Function : Translate_persistentID_to_IOR
317 //============================================================================
318 static void Translate_persistentID_to_IOR(TDF_Label Lab,
319 SALOMEDS::Driver_ptr driver,
321 CORBA::Boolean isMultiFile,
322 CORBA::Boolean isASCII)
324 TDF_ChildIterator itchild (Lab);
326 for (; itchild.More(); itchild.Next()) {
327 TDF_Label current = itchild.Value();
328 Handle(TDF_Attribute) Att;
329 if (current.FindAttribute(SALOMEDS_PersRefAttribute::GetID(),Att)) {
331 Handle(SALOMEDS_LocalIDAttribute) anID;
332 if (current.FindAttribute(SALOMEDS_LocalIDAttribute::GetID(), anID))
333 if (anID->Get() == FILELOCALID) continue; //SRN: This attribute store a file name, skip it
335 TCollection_ExtendedString res;
336 res = Handle(TDataStd_Comment)::DownCast(Att)->Get();
337 TCollection_AsciiString ch(res);
339 CORBA::String_var persistent_string = CORBA::string_dup(ch.ToCString());
340 ASSERT(! CORBA::is_nil(driver));
341 SALOMEDS_SObject_i * so_servant = new SALOMEDS_SObject_i (current,orb);
342 SALOMEDS::SObject_var so = SALOMEDS::SObject::_narrow(so_servant->_this());
344 CORBA::String_var ior_string = driver->LocalPersistentIDToIOR(so, persistent_string, isMultiFile, isASCII);
346 TCollection_ExtendedString value(ior_string);
347 SALOMEDS_IORAttribute::Set (current,value,orb);
349 //TCollection_AsciiString anEntry;TDF_Tool::Entry (current,anEntry); //SRN: No use here
350 //delete persistent_string;
353 Translate_persistentID_to_IOR (current,driver,orb,isMultiFile, isASCII);
357 //============================================================================
358 /*! Function : LoadWith
361 //============================================================================
363 //============================================================================
364 void SALOMEDS_StudyBuilder_i::LoadWith(SALOMEDS::SComponent_ptr anSCO,
365 SALOMEDS::Driver_ptr aDriver) throw(SALOME::SALOME_Exception)
367 Unexpect aCatch(SBSalomeException);
369 ASSERT(!CORBA::is_nil(anSCO));
370 CORBA::String_var scoid = anSCO->GetID();
371 TDF_Tool::Label(_doc->GetData(),scoid,Lab);
372 Handle(TDF_Attribute) Att;
374 //Find the current Url of the study
375 if (_doc->Main().FindAttribute(SALOMEDS_PersRefAttribute::GetID(),Att)) {
376 int aLocked = anSCO->GetStudy()->GetProperties()->IsLocked();
377 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(false);
379 TCollection_ExtendedString Res = Handle(TDataStd_Comment)::DownCast(Att)->Get();
381 Handle(TDataStd_Comment) type;
382 TCollection_ExtendedString DataType;
383 if (Lab.FindAttribute(TDataStd_Comment::GetID(),type))
384 DataType = type->Get();
386 MESSAGE("No Data Type");
388 // associate the driver to the SComponent
389 ASSERT(!CORBA::is_nil(aDriver));
390 // mpv 06.03.2003: SAL1927 - if component data if already loaded, it is not necessary to do it again
391 if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), Att)) {
392 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
395 DefineComponentInstance (anSCO, aDriver);
397 TCollection_AsciiString aHDFPath(Res);
400 bool isASCII = false;
401 if (HDFascii::isASCII(aHDFPath.ToCString())) {
403 char* aResultPath = HDFascii::ConvertFromASCIIToHDF(aHDFPath.ToCString());
404 aHDFUrl = new char[strlen(aResultPath) + 19];
405 sprintf(aHDFUrl, "%shdf_from_ascii.hdf", aResultPath);
408 aHDFUrl = CORBA::string_dup(aHDFPath.ToCString());
411 //Open the Study HDF file
412 HDFfile *hdf_file = new HDFfile(aHDFUrl);
414 char aMultifileState[2];
415 char ASCIIfileState[2];
417 CORBA::String_var scoid = anSCO->GetID();
418 hdf_file->OpenOnDisk(HDF_RDONLY);
419 HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
420 hdf_group->OpenOnDisk();
421 HDFgroup *hdf_sco_group = new HDFgroup(scoid, hdf_group);
422 hdf_sco_group->OpenOnDisk();
424 SALOMEDS::TMPFile_var aStreamFile;
425 if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
426 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
427 hdf_dataset->OpenOnDisk();
428 int aStreamSize = hdf_dataset->GetSize();
429 unsigned char* aBuffer = new unsigned char[aStreamSize];
430 if(aBuffer == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
431 hdf_dataset->ReadFromDisk(aBuffer);
432 aStreamFile = new SALOMEDS::TMPFile(aStreamSize, aStreamSize, aBuffer, 1);
433 hdf_dataset->CloseOnDisk();
435 } else aStreamFile = new SALOMEDS::TMPFile(0);
437 HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
438 multifile_hdf_dataset->OpenOnDisk();
439 multifile_hdf_dataset->ReadFromDisk(aMultifileState);
441 HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
442 ascii_hdf_dataset->OpenOnDisk();
443 ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
445 // set path without file name from URL
446 int aFileNameSize = Res.Length();
447 char* aDir = new char[aFileNameSize];
448 memcpy(aDir, TCollection_AsciiString(Res).ToCString(), aFileNameSize);
449 for(int aCounter = aFileNameSize-1; aCounter>=0; aCounter--)
450 if (aDir[aCounter] == '/') {
451 aDir[aCounter+1] = 0;
455 CORBA::Boolean aResult = (ASCIIfileState[0]=='A')?
456 aDriver->LoadASCII(anSCO, aStreamFile.in(), aDir, aMultifileState[0]=='M'):
457 aDriver->Load(anSCO, aStreamFile.in(), aDir, aMultifileState[0]=='M');
459 RemoveAttribute( anSCO, "AttributeIOR" );
461 SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
462 aFilesToRemove->length(1);
463 aFilesToRemove[0] = CORBA::string_dup(&(aHDFUrl[strlen(SALOMEDS_Tool::GetDirFromPath(aHDFUrl))]));
464 SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
467 MESSAGE("Can't load component");
468 THROW_SALOME_CORBA_EXCEPTION("Unable to load component data",SALOME::BAD_PARAM);
469 // throw HDFexception("Unable to load component");
474 multifile_hdf_dataset->CloseOnDisk();
475 multifile_hdf_dataset = 0;
476 ascii_hdf_dataset->CloseOnDisk();
477 ascii_hdf_dataset = 0;
478 hdf_sco_group->CloseOnDisk();
480 hdf_group->CloseOnDisk();
482 hdf_file->CloseOnDisk();
486 SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
487 aFilesToRemove->length(1);
488 aFilesToRemove[0] = CORBA::string_dup(&(aHDFUrl[strlen(SALOMEDS_Tool::GetDirFromPath(aHDFUrl))]));
489 SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
493 catch (HDFexception) {
494 INFOS("No persistent file Name");
497 SALOMEDS::ListOfFileNames_var aFilesToRemove = new SALOMEDS::ListOfFileNames;
498 aFilesToRemove->length(1);
499 aFilesToRemove[0] = CORBA::string_dup(&(aHDFUrl[strlen(SALOMEDS_Tool::GetDirFromPath(aHDFUrl))]));
500 SALOMEDS_Tool::RemoveTemporaryFiles(SALOMEDS_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
503 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
508 Translate_persistentID_to_IOR (Lab,aDriver,_orb, aMultifileState[0]=='M', ASCIIfileState[0] == 'A');
509 } catch (SALOME::SALOME_Exception) {
510 INFOS("Can't translate PersRef to IOR");
511 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
512 THROW_SALOME_CORBA_EXCEPTION("Unable to convert component persistent data to the transient",SALOME::BAD_PARAM);
513 // throw HDFexception("Unable to load component data");
515 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
517 MESSAGE("No persistent file Name");
521 //============================================================================
525 //============================================================================
526 void SALOMEDS_StudyBuilder_i::Load(SALOMEDS::SObject_ptr sco)
528 MESSAGE ( "This function is not yet implemented");
531 //============================================================================
532 /*! Function : FindOrCreateAttribute
533 * Purpose : Add attribute of given type to SObject, if there is attribute of such type, returns
536 //============================================================================
537 SALOMEDS::GenericAttribute_ptr SALOMEDS_StudyBuilder_i::FindOrCreateAttribute(SALOMEDS::SObject_ptr anObject,
538 const char* aTypeOfAttribute)
541 ASSERT(!CORBA::is_nil(anObject));
542 CORBA::String_var anobid = anObject->GetID();
543 TDF_Tool::Label(_doc->GetData(),anobid,Lab);
545 __FindOrCreateAttributeLocked(TDataStd_Real, AttributeReal)
546 __FindOrCreateAttributeLocked(TDataStd_Integer, AttributeInteger)
547 __FindOrCreateAttributeLocked(SALOMEDS_SequenceOfRealAttribute, AttributeSequenceOfReal)
548 __FindOrCreateAttributeLocked(SALOMEDS_SequenceOfIntegerAttribute, AttributeSequenceOfInteger)
549 __FindOrCreateAttributeLocked(TDataStd_Name, AttributeName)
550 __FindOrCreateAttributeLocked(TDataStd_Comment, AttributeComment)
551 __FindOrCreateAttributeLocked(SALOMEDS_IORAttribute, AttributeIOR)
552 __FindOrCreateAttributeLocked(SALOMEDS_PixMapAttribute, AttributePixMap)
553 __FindOrCreateAttributeLocked(SALOMEDS_LocalIDAttribute, AttributeLocalID)
554 __FindOrCreateAttributeLocked(SALOMEDS_TableOfIntegerAttribute, AttributeTableOfInteger)
555 __FindOrCreateAttributeLocked(SALOMEDS_TableOfRealAttribute, AttributeTableOfReal)
556 __FindOrCreateAttributeLocked(SALOMEDS_TableOfStringAttribute, AttributeTableOfString)
557 __FindOrCreateAttributeLocked(SALOMEDS_PythonObjectAttribute, AttributePythonObject)
559 __FindOrCreateAttribute(SALOMEDS_PersRefAttribute, AttributePersistentRef)
560 __FindOrCreateAttribute(SALOMEDS_DrawableAttribute, AttributeDrawable)
561 __FindOrCreateAttribute(SALOMEDS_SelectableAttribute, AttributeSelectable)
562 __FindOrCreateAttribute(SALOMEDS_ExpandableAttribute, AttributeExpandable)
563 __FindOrCreateAttribute(SALOMEDS_OpenedAttribute, AttributeOpened)
564 __FindOrCreateAttribute(SALOMEDS_TextColorAttribute, AttributeTextColor)
565 __FindOrCreateAttribute(SALOMEDS_TextHighlightColorAttribute, AttributeTextHighlightColor)
566 __FindOrCreateAttribute(SALOMEDS_TargetAttribute, AttributeTarget)
567 __FindOrCreateAttribute(SALOMEDS_StudyPropertiesAttribute, AttributeStudyProperties)
568 __FindOrCreateAttribute(SALOMEDS_ExternalFileDef, AttributeExternalFileDef)
569 __FindOrCreateAttribute(SALOMEDS_FileType, AttributeFileType)
571 if (strncmp(aTypeOfAttribute, "AttributeTreeNode",17) == 0 ) {
572 Standard_GUID aTreeNodeGUID;
573 if (strcmp(aTypeOfAttribute, "AttributeTreeNode") == 0) {
574 aTreeNodeGUID = TDataStd_TreeNode::GetDefaultTreeID();
576 char* aGUIDString = new char[40];
577 sprintf(aGUIDString, &(aTypeOfAttribute[21]));
578 aTreeNodeGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
581 Handle(TDataStd_TreeNode) anAttr;
582 if (!Lab.FindAttribute(aTreeNodeGUID, anAttr)) {
584 anAttr = TDataStd_TreeNode::Set(Lab, aTreeNodeGUID);
586 SALOMEDS_AttributeTreeNode_i* aTreeNodeAttr = new SALOMEDS_AttributeTreeNode_i(anAttr, _orb);
587 return aTreeNodeAttr->AttributeTreeNode::_this();
590 if (strncmp(aTypeOfAttribute, "AttributeUserID",15) == 0 ) {
591 Handle(TDataStd_UAttribute) anAttr;
592 if (!Lab.FindAttribute(SALOMEDS_AttributeUserID_i::DefaultID(), anAttr)) {
594 anAttr = TDataStd_UAttribute::Set(Lab, SALOMEDS_AttributeUserID_i::DefaultID());
596 SALOMEDS_AttributeUserID_i* aUAttr = new SALOMEDS_AttributeUserID_i(anAttr, _orb);
597 return aUAttr->AttributeUserID::_this();
599 return SALOMEDS::GenericAttribute::_nil();
602 //============================================================================
603 /*! Function : FindAttribute
604 * Purpose : Find attribute of given type assigned SObject, returns Standard_True if it is found
606 //============================================================================
608 CORBA::Boolean SALOMEDS_StudyBuilder_i::FindAttribute(SALOMEDS::SObject_ptr anObject,
609 SALOMEDS::GenericAttribute_out anAttribute,
610 const char* aTypeOfAttribute)
613 ASSERT(!CORBA::is_nil(anObject));
614 CORBA::String_var anobid = anObject->GetID();
615 TDF_Tool::Label(_doc->GetData(),anobid,Lab);
616 Handle(TDF_Attribute) anAttr;
617 if (Lab.FindAttribute(SALOMEDS_GenericAttribute_i::GetGUID(aTypeOfAttribute), anAttr)) {
618 anAttribute = SALOMEDS::GenericAttribute::_duplicate(SALOMEDS_GenericAttribute_i::CreateAttribute(_orb, anAttr));
619 return Standard_True;
621 return Standard_False;
624 //============================================================================
625 /*! Function : RemoveAttribute
626 * Purpose : Remove attribute of given type assigned SObject
628 //============================================================================
630 void SALOMEDS_StudyBuilder_i::RemoveAttribute(SALOMEDS::SObject_ptr anObject,
631 const char* aTypeOfAttribute)
635 ASSERT(!CORBA::is_nil(anObject));
636 CORBA::String_var anobid = anObject->GetID();
637 TDF_Tool::Label(_doc->GetData(),anobid,Lab);
639 if (strcmp(aTypeOfAttribute, "AttributeIOR") == 0) { // postponed removing of CORBA objects
640 Handle(SALOMEDS_IORAttribute) anAttr;
641 if (Lab.FindAttribute(SALOMEDS_IORAttribute::GetID(), anAttr))
642 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->AddPostponed(TCollection_AsciiString(anAttr->Get()).ToCString());
646 Lab.ForgetAttribute (SALOMEDS_GenericAttribute_i::GetGUID(aTypeOfAttribute));
649 //============================================================================
650 /*! Function : Addreference
653 //============================================================================
655 SALOMEDS_StudyBuilder_i::Addreference(SALOMEDS::SObject_ptr me,
656 SALOMEDS::SObject_ptr theReferencedObject)
660 ASSERT(!CORBA::is_nil(me));
661 CORBA::String_var meid = me->GetID();
662 TDF_Tool::Label(_doc->GetData(),meid,Lab);
664 ASSERT(!CORBA::is_nil(theReferencedObject));
665 CORBA::String_var roid = theReferencedObject->GetID();
666 TDF_Tool::Label(_doc->GetData(),roid,RefLab);
667 TDF_Reference::Set(Lab,RefLab);
669 SALOMEDS_TargetAttribute::Set(RefLab)->Append(Lab);
671 if(!CORBA::is_nil(_callbackOnRemove) && Lab.IsDescendant(_doc->Main())) _callbackOnRemove->OnRemoveSObject(me);
674 //============================================================================
675 /*! Function : RemoveReference
678 //============================================================================
679 void SALOMEDS_StudyBuilder_i::RemoveReference(SALOMEDS::SObject_ptr me)
681 SALOMEDS::SObject_var theReferencedObject;
682 if(!me->ReferencedObject(theReferencedObject)) return; //No reference is found
686 ASSERT(!CORBA::is_nil(me));
687 CORBA::String_var meid = me->GetID();
688 TDF_Tool::Label(_doc->GetData(),meid,Lab);
690 Lab.ForgetAttribute(TDF_Reference::GetID());
693 ASSERT(!CORBA::is_nil(theReferencedObject));
694 CORBA::String_var roid = theReferencedObject->GetID();
695 TDF_Tool::Label(_doc->GetData(),roid,RefLab);
697 RemoveAttribute(theReferencedObject, "AttributeTarget");
698 //if(!CORBA::is_nil(_callbackOnRemove) && Lab.IsDescendant(_doc->Main())) _callbackOnRemove->OnRemoveSObject(me);
703 //============================================================================
704 /*! Function : AddDirectory
705 * Purpose : adds a new directory with a path = thePath
707 //============================================================================
708 void SALOMEDS_StudyBuilder_i::AddDirectory(const char* thePath)
711 if(thePath == NULL || strlen(thePath) == 0) throw SALOMEDS::Study::StudyInvalidDirectory();
713 TCollection_AsciiString aPath(CORBA::string_dup(thePath)), aContext(""), aFatherPath;
714 TDF_ChildIterator anIterator(_doc->Main());
715 Handle(TDataStd_Name) aName;
717 SALOMEDS_SObject_i* so_servant = new SALOMEDS_SObject_i (_doc->Main(), _orb);
718 SALOMEDS::SObject_var anObject = SALOMEDS::SObject::_narrow(so_servant->_this());
719 SALOMEDS::Study_var aStudy = anObject->GetStudy();
722 anObject = aStudy->FindObjectByPath(thePath); //Check if the directory already exists
726 if(!anObject->_is_nil()) throw SALOMEDS::Study::StudyNameAlreadyUsed();
728 if(aPath.Value(1) != '/') { //Relative path
730 aPath = TCollection_AsciiString(aStudy->GetContext()) + aPath;
733 TCollection_AsciiString aToken = aPath.Token("/", 1);
734 if(aToken.Length() == 0) aFatherPath = "/";
737 while(aToken.Length() != 0) {
738 if(aPath.Token("/", i+1).Length() > 0) {
740 aFatherPath += aToken;
742 aToken = aPath.Token("/", ++i);
745 anObject = SALOMEDS::SObject::_nil();
747 anObject = aStudy->FindObjectByPath(aFatherPath.ToCString()); //Check if the father directory exists
750 if(anObject->_is_nil()) throw SALOMEDS::Study::StudyInvalidDirectory();
752 SALOMEDS::SObject_var aNewObject = NewObject(anObject);
753 TDF_Tool::Label(_doc->GetData(), aNewObject->GetID(), aLabel);
754 if(aLabel.IsNull()) {
755 MESSAGE("### NULL label");
756 throw SALOMEDS::Study::StudyInvalidComponent();
759 TDataStd_Name::Set(aLabel, aPath.Token("/", i-1));
761 //Set LocalID attribute to identify the directory object
762 SALOMEDS::GenericAttribute_var anAttr = FindOrCreateAttribute(aNewObject, "AttributeLocalID");
763 SALOMEDS::AttributeLocalID_var aPersRef = SALOMEDS::AttributeLocalID::_narrow(anAttr);
764 if(aPersRef->_is_nil()) throw SALOMEDS::Study::StudyInvalidDirectory();
766 aPersRef->SetValue(DIRECTORYID);
770 //============================================================================
771 /*! Function : SetGUID
774 //============================================================================
775 void SALOMEDS_StudyBuilder_i::SetGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
779 ASSERT(!CORBA::is_nil(anObject));
780 CORBA::String_var anEntry = anObject->GetID();
781 TDF_Tool::Label(_doc->GetData(), anEntry, aLabel);
782 TDataStd_UAttribute::Set(aLabel, (char*)theGUID);
785 //============================================================================
786 /*! Function : IsGUID
789 //============================================================================
790 bool SALOMEDS_StudyBuilder_i::IsGUID(SALOMEDS::SObject_ptr anObject, const char* theGUID)
793 ASSERT(!CORBA::is_nil(anObject));
794 CORBA::String_var anEntry = anObject->GetID();
795 TDF_Tool::Label(_doc->GetData(), anEntry, aLabel);
796 return aLabel.IsAttribute((char*)theGUID);
800 //============================================================================
801 /*! Function : NewCommand
804 //============================================================================
805 void SALOMEDS_StudyBuilder_i::NewCommand()
807 // mpv: for SAL2114 - unset "lock changed" flag at the operation start
808 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
809 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
810 anAttr = new SALOMEDS_StudyPropertiesAttribute;
811 _doc->Main().AddAttribute(anAttr);
813 anAttr->IsLockChanged(true);
818 //============================================================================
819 /*! Function : CommitCommand
822 //============================================================================
823 void SALOMEDS_StudyBuilder_i::CommitCommand() throw (SALOMEDS::StudyBuilder::LockProtection)
825 Unexpect aCatch(SBLockProtection);
826 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
827 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
828 anAttr = new SALOMEDS_StudyPropertiesAttribute;
829 _doc->Main().AddAttribute(anAttr);
831 if (anAttr->IsLocked() && !anAttr->IsLockChanged(true)) {
832 MESSAGE("Locked document modification !!!");
834 throw SALOMEDS::StudyBuilder::LockProtection();
836 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->RemovePostponed(_doc->GetUndoLimit());
838 int aModif = anAttr->GetModified();
839 if (aModif < 0) aModif = 1000; // if user make undo and then - new transaction "modify" will never be zero
840 anAttr->SetModified(aModif+1);
841 _doc->CommitCommand();
845 //============================================================================
846 /*! Function : HasOpenCommand
849 //============================================================================
850 CORBA::Boolean SALOMEDS_StudyBuilder_i::HasOpenCommand()
852 _doc->HasOpenCommand();
855 //============================================================================
856 /*! Function : AbortCommand
859 //============================================================================
860 void SALOMEDS_StudyBuilder_i::AbortCommand()
862 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->UndoPostponed(0);
864 _doc->AbortCommand();
867 //============================================================================
871 //============================================================================
872 void SALOMEDS_StudyBuilder_i::Undo() throw (SALOMEDS::StudyBuilder::LockProtection)
874 Unexpect aCatch(SBLockProtection);
875 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
876 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
877 anAttr = new SALOMEDS_StudyPropertiesAttribute;
878 _doc->Main().AddAttribute(anAttr);
880 if (anAttr->IsLocked()) {
881 MESSAGE("Locked document modification !!!");
882 throw SALOMEDS::StudyBuilder::LockProtection();
884 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->UndoPostponed(1);
886 anAttr->SetModified(anAttr->GetModified()-1);
890 //============================================================================
894 //============================================================================
895 void SALOMEDS_StudyBuilder_i::Redo() throw (SALOMEDS::StudyBuilder::LockProtection)
897 Unexpect aCatch(SBLockProtection);
898 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
899 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
900 anAttr = new SALOMEDS_StudyPropertiesAttribute;
901 _doc->Main().AddAttribute(anAttr);
904 if (anAttr->IsLocked()) {
905 MESSAGE("Locked document modification !!!");
906 throw SALOMEDS::StudyBuilder::LockProtection();
909 SALOMEDS_Study_i::GetStudy(_doc->Main(), _orb)->UndoPostponed(-1);
910 anAttr->SetModified(anAttr->GetModified()+1);
914 //============================================================================
915 /*! Function : GetAvailableUndos
918 //============================================================================
919 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableUndos()
921 return _doc->GetAvailableUndos();
924 //============================================================================
925 /*! Function : GetAvailableRedos
928 //============================================================================
929 CORBA::Boolean SALOMEDS_StudyBuilder_i::GetAvailableRedos()
931 return _doc->GetAvailableRedos();
934 //============================================================================
935 /*! Function : UndoLimit
938 //============================================================================
939 CORBA::Long SALOMEDS_StudyBuilder_i::UndoLimit()
941 return _doc->GetUndoLimit();
944 //============================================================================
945 /*! Function : UndoLimit
948 //============================================================================
949 void SALOMEDS_StudyBuilder_i::UndoLimit(CORBA::Long n)
952 _doc->SetUndoLimit (n);
955 //============================================================================
956 /*! Function : SetOnAddSObject
959 //============================================================================
960 SALOMEDS::Callback_ptr SALOMEDS_StudyBuilder_i::SetOnAddSObject(SALOMEDS::Callback_ptr theCallback)
962 SALOMEDS::Callback_ptr aRet = (CORBA::is_nil(_callbackOnAdd))?NULL:_callbackOnAdd._retn();
963 _callbackOnAdd = SALOMEDS::Callback::_duplicate(theCallback);
967 //============================================================================
968 /*! Function : SetOnNewSObject
971 //============================================================================
972 SALOMEDS::Callback_ptr SALOMEDS_StudyBuilder_i::SetOnRemoveSObject(SALOMEDS::Callback_ptr theCallback)
974 SALOMEDS::Callback_ptr aRet = (CORBA::is_nil(_callbackOnRemove))?NULL:_callbackOnRemove._retn();
975 _callbackOnRemove = SALOMEDS::Callback::_duplicate(theCallback);
979 //============================================================================
980 /*! Function : CheckLocked
983 //============================================================================
984 void SALOMEDS_StudyBuilder_i::CheckLocked() throw (SALOMEDS::StudyBuilder::LockProtection) {
985 Unexpect aCatch(SBLockProtection);
986 if (_doc->HasOpenCommand()) return;
987 Handle(SALOMEDS_StudyPropertiesAttribute) anAttr;
988 if (!_doc->Main().FindAttribute(SALOMEDS_StudyPropertiesAttribute::GetID(), anAttr)) {
989 anAttr = new SALOMEDS_StudyPropertiesAttribute;
990 _doc->Main().AddAttribute(anAttr);
992 if (anAttr->IsLocked()) throw SALOMEDS::StudyBuilder::LockProtection();
995 //============================================================================
996 /*! Function : SetName
999 //============================================================================
1000 void SALOMEDS_StudyBuilder_i::SetName(SALOMEDS::SObject_ptr theSO, const char* theValue)
1001 throw(SALOMEDS::StudyBuilder::LockProtection)
1003 Unexpect aCatch(SBLockProtection);
1007 ASSERT(!CORBA::is_nil(theSO));
1008 CORBA::String_var aSOID = theSO->GetID();
1009 TDF_Tool::Label(_doc->GetData(), aSOID, aLabel);
1010 TDataStd_Name::Set(aLabel, (char*)theValue);
1013 //============================================================================
1014 /*! Function : SetComment
1017 //============================================================================
1018 void SALOMEDS_StudyBuilder_i::SetComment(SALOMEDS::SObject_ptr theSO, const char* theValue)
1019 throw(SALOMEDS::StudyBuilder::LockProtection)
1021 Unexpect aCatch(SBLockProtection);
1025 ASSERT(!CORBA::is_nil(theSO));
1026 CORBA::String_var aSOID = theSO->GetID();
1027 TDF_Tool::Label(_doc->GetData(), aSOID, aLabel);
1028 TDataStd_Comment::Set(aLabel, (char*)theValue);
1031 //============================================================================
1032 /*! Function : SetIOR
1035 //============================================================================
1036 void SALOMEDS_StudyBuilder_i::SetIOR(SALOMEDS::SObject_ptr theSO, const char* theValue)
1037 throw(SALOMEDS::StudyBuilder::LockProtection)
1039 Unexpect aCatch(SBLockProtection);
1043 ASSERT(!CORBA::is_nil(theSO));
1044 CORBA::String_var aSOID = theSO->GetID();
1045 TDF_Tool::Label(_doc->GetData(), aSOID, aLabel);
1046 SALOMEDS_IORAttribute::Set(aLabel, TCollection_ExtendedString((char*)theValue), _orb);