1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDSImpl_StudyBuilder.cxx
21 // Author : Sergey RUIN
26 #include "SALOMEDSImpl_Attributes.hxx"
30 #include "SALOMEDSImpl_Study.hxx"
31 #include "SALOMEDSImpl_StudyBuilder.hxx"
32 #include "SALOMEDSImpl_SObject.hxx"
33 #include "SALOMEDSImpl_SComponent.hxx"
34 #include "SALOMEDSImpl_Tool.hxx"
36 #include <SALOMEDSImpl_ChildNodeIterator.hxx>
38 #include <TDF_ChildIterator.hxx>
39 #include <TDF_Label.hxx>
40 #include <TDF_Tool.hxx>
41 #include <TDF_Data.hxx>
42 #include <TDF_ListIteratorOfAttributeList.hxx>
44 #include <OSD_Path.hxx>
49 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_StudyBuilder, MMgt_TShared )
50 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_StudyBuilder, MMgt_TShared )
52 #define USE_CASE_LABEL_TAG 2
53 #define DIRECTORYID 16661
54 #define FILELOCALID 26662
56 static void Translate_persistentID_to_IOR(TDF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII);
58 //============================================================================
59 /*! Function : constructor
62 //============================================================================
63 SALOMEDSImpl_StudyBuilder::SALOMEDSImpl_StudyBuilder(const Handle(Standard_Transient)& theOwner)
67 _doc = Handle(SALOMEDSImpl_Study)::DownCast(theOwner)->GetDocument();
70 //============================================================================
71 /*! Function : destructor
74 //============================================================================
75 SALOMEDSImpl_StudyBuilder::~SALOMEDSImpl_StudyBuilder()
78 //============================================================================
79 /*! Function : NewComponent
80 * Purpose : Create a new component (Scomponent)
82 //============================================================================
83 Handle(SALOMEDSImpl_SComponent) SALOMEDSImpl_StudyBuilder::NewComponent(const TCollection_AsciiString& DataType)
88 if(DataType.Length() == 0) return 0;
90 //Always create component under main label.
91 TDF_Label L = _doc->Main();
94 for (TDF_ChildIterator it(L); it.More(); it.Next()) {
95 if (it.Value().Tag() > imax)
96 imax = it.Value().Tag();
99 TDF_Label NL = L.FindChild(imax);
101 SALOMEDSImpl_AttributeComment::Set(NL, DataType);
103 Handle(SALOMEDSImpl_SComponent) so = Handle(SALOMEDSImpl_Study)::DownCast(_study)->GetSComponent (NL);
105 if(!_callbackOnAdd.IsNull()) _callbackOnAdd->OnAddSObject(so);
112 //============================================================================
113 /*! Function : DefineComponentInstance
114 * Purpose : Add IOR attribute of a Scomponent
116 //============================================================================
117 bool SALOMEDSImpl_StudyBuilder::DefineComponentInstance(const Handle(SALOMEDSImpl_SComponent)& aComponent,
118 const TCollection_AsciiString& IOR)
123 if(aComponent.IsNull() || IOR.IsEmpty()) {
124 _errorCode = "Invalid arguments";
128 SALOMEDSImpl_AttributeIOR::Set(aComponent->GetLabel(), IOR);
135 //============================================================================
136 /*! Function : RemoveComponent
137 * Purpose : Delete a Scomponent
139 //============================================================================
140 bool SALOMEDSImpl_StudyBuilder::RemoveComponent(const Handle(SALOMEDSImpl_SComponent)& aComponent)
144 return RemoveObject(aComponent);
147 //============================================================================
148 /*! Function : NewObject
149 * Purpose : Create a new SObject
151 //============================================================================
152 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_StudyBuilder::NewObject(const Handle(SALOMEDSImpl_SObject)& theFatherObject)
157 //Find label of father
158 TDF_Label Lab = theFatherObject->GetLabel();
162 for (TDF_ChildIterator it(Lab); it.More(); it.Next()) {
163 if (it.Value().Tag() > imax)
164 imax = it.Value().Tag();
167 TDF_Label NewLab = Lab.FindChild(imax);
169 Handle(SALOMEDSImpl_SObject) so = Handle(SALOMEDSImpl_Study)::DownCast(_study)->GetSObject(NewLab);
170 if(!_callbackOnAdd.IsNull()) _callbackOnAdd->OnAddSObject(so);
176 //============================================================================
177 /*! Function : NewObjectToTag
180 //============================================================================
181 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_StudyBuilder::NewObjectToTag(const Handle(SALOMEDSImpl_SObject)& theFatherObject,
186 //Find label of father
187 TDF_Label Lab = theFatherObject->GetLabel();
189 //Create or find label
190 TDF_Label NewLab = Lab.FindChild(theTag, 1);
192 Handle(SALOMEDSImpl_SObject) so = Handle(SALOMEDSImpl_Study)::DownCast(_study)->GetSObject(NewLab);
194 if(!_callbackOnAdd.IsNull()) _callbackOnAdd->OnAddSObject(so);
200 //============================================================================
201 /*! Function : RemoveObject
204 //============================================================================
205 bool SALOMEDSImpl_StudyBuilder::RemoveObject(const Handle(SALOMEDSImpl_SObject)& anObject)
209 if(anObject.IsNull()) {
210 _errorCode = "Null object";
214 if(!_callbackOnRemove.IsNull()) _callbackOnRemove->OnRemoveSObject(anObject);
216 TDF_Label Lab = anObject->GetLabel();
218 Handle(SALOMEDSImpl_AttributeReference) aReference;
219 if (Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aReference)) {
220 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
221 if (aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(),aTarget))
222 aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
225 Handle(SALOMEDSImpl_AttributeIOR) anAttr; // postponed removing of CORBA objects
226 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
227 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(TCollection_AsciiString(anAttr->Value()).ToCString());
229 Lab.ForgetAllAttributes();
236 //============================================================================
237 /*! Function : RemoveObjectWithChildren
240 //============================================================================
241 bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const Handle(SALOMEDSImpl_SObject)& anObject)
245 if(anObject.IsNull()) {
246 _errorCode = "Null object";
250 if(!_callbackOnRemove.IsNull()) _callbackOnRemove->OnRemoveSObject(anObject);
252 TDF_Label Lab = anObject->GetLabel();
254 Handle(SALOMEDSImpl_AttributeReference) aReference;
255 if (Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aReference)) {
256 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
257 if (aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(),aTarget))
258 aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
260 Handle(SALOMEDSImpl_AttributeIOR) anAttr; // postponed removing of CORBA objects
261 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
262 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(TCollection_AsciiString(anAttr->Value()).ToCString());
264 TDF_ChildIterator it(Lab, Standard_True);
265 for(;it.More();it.Next()) {
266 TDF_Label aLabel = it.Value();
267 if (aLabel.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aReference)) {
268 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
269 if (aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(),aTarget))
270 aTarget->Remove(SALOMEDSImpl_Study::SObject(aLabel));
272 Handle(SALOMEDSImpl_AttributeIOR) anAttr; // postponed removing of CORBA objects
273 if (aLabel.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
274 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(TCollection_AsciiString(anAttr->Value()).ToCString());
277 Lab.ForgetAllAttributes(Standard_True);
284 //============================================================================
285 /*! Function : LoadWith
288 //============================================================================
289 bool SALOMEDSImpl_StudyBuilder::LoadWith(const Handle(SALOMEDSImpl_SComponent)& anSCO,
290 SALOMEDSImpl_Driver* aDriver)
294 TDF_Label Lab = anSCO->GetLabel();
295 Handle(SALOMEDSImpl_AttributePersistentRef) Att;
297 //Find the current Url of the study
298 if (_doc->Main().FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID(),Att)) {
299 int aLocked = anSCO->GetStudy()->GetProperties()->IsLocked();
300 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(false);
302 TCollection_ExtendedString Res(Att->Value());
303 TCollection_AsciiString aHDFPath(Res);
305 Handle(SALOMEDSImpl_AttributeComment) type;
306 TCollection_ExtendedString DataType;
307 if (Lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID(),type))
308 DataType = type->Value();
310 // associate the driver to the SComponent
311 if(aDriver == NULL) {
312 _errorCode = "Driver is null";
316 // mpv 06.03.2003: SAL1927 - if component data if already loaded, it is not necessary to do it again
317 Handle(SALOMEDSImpl_AttributeIOR) attrIOR;
318 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), attrIOR)) {
319 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
323 DefineComponentInstance (anSCO, aDriver->GetIOR());
325 TCollection_AsciiString aHDFUrl;
326 bool isASCII = false;
327 if (HDFascii::isASCII(aHDFPath.ToCString())) {
329 aHDFUrl = HDFascii::ConvertFromASCIIToHDF(aHDFPath.ToCString());
330 aHDFUrl += "hdf_from_ascii.hdf";
335 //Open the Study HDF file
336 HDFfile *hdf_file = new HDFfile(aHDFUrl.ToCString());
338 char aMultifileState[2];
339 char ASCIIfileState[2];
341 TCollection_AsciiString scoid = anSCO->GetID();
342 hdf_file->OpenOnDisk(HDF_RDONLY);
343 HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
344 hdf_group->OpenOnDisk();
345 HDFgroup *hdf_sco_group = new HDFgroup(scoid.ToCString(), hdf_group);
346 hdf_sco_group->OpenOnDisk();
348 unsigned char* aStreamFile = NULL;
351 if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
352 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
353 hdf_dataset->OpenOnDisk();
354 aStreamSize = hdf_dataset->GetSize();
355 aStreamFile = new unsigned char[aStreamSize];
356 if(aStreamFile == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
357 hdf_dataset->ReadFromDisk(aStreamFile);
358 hdf_dataset->CloseOnDisk();
363 HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
364 multifile_hdf_dataset->OpenOnDisk();
365 multifile_hdf_dataset->ReadFromDisk(aMultifileState);
367 HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
368 ascii_hdf_dataset->OpenOnDisk();
369 ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
371 // set path without file name from URL
372 //int aFileNameSize = Res.Length();
373 //char* aDir = new char[aFileNameSize];
374 //memcpy(aDir, TCollection_AsciiString(Res).ToCString(), aFileNameSize);
375 //for(int aCounter = aFileNameSize-1; aCounter>=0; aCounter--)
376 //if (aDir[aCounter] == '/') {
377 // aDir[aCounter+1] = 0;
380 // Above code was working wrong for paths without '/' inside.
381 TCollection_AsciiString aDir = SALOMEDSImpl_Tool::GetDirFromPath(Res);
383 bool aResult = (ASCIIfileState[0]=='A')?
384 aDriver->LoadASCII(anSCO, aStreamFile, aStreamSize, aDir.ToCString(), aMultifileState[0]=='M'):
385 aDriver->Load(anSCO, aStreamFile, aStreamSize, aDir.ToCString(), aMultifileState[0]=='M');
387 if(aStreamFile != NULL) delete []aStreamFile;
390 RemoveAttribute( anSCO, "AttributeIOR" );
392 _errorCode = "Can't load component";
393 throw HDFexception("Unable to load component");
396 //if(aDir != NULL) delete []aDir;
398 multifile_hdf_dataset->CloseOnDisk();
399 multifile_hdf_dataset = 0;
400 ascii_hdf_dataset->CloseOnDisk();
401 ascii_hdf_dataset = 0;
403 hdf_sco_group->CloseOnDisk();
405 hdf_group->CloseOnDisk();
407 hdf_file->CloseOnDisk();
411 Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
412 aFilesToRemove->Append("hdf_from_ascii.hdf");
413 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl),
414 aFilesToRemove, true);
417 catch (HDFexception) {
421 Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
422 aFilesToRemove->Append(aHDFUrl);
423 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
426 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
427 _errorCode = "No persistent file";
432 Translate_persistentID_to_IOR (Lab, aDriver, aMultifileState[0]=='M', ASCIIfileState[0] == 'A');
434 _errorCode = "Can not convert persistent IDs to IORs";
438 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
440 _errorCode = "No persistent file";
447 //============================================================================
451 //============================================================================
452 bool SALOMEDSImpl_StudyBuilder::Load(const Handle(SALOMEDSImpl_SObject)& sco)
454 _errorCode = "Not implemented";
458 //============================================================================
459 /*! Function : FindOrCreateAttribute
460 * Purpose : Add attribute of given type to SObject, if there is attribute of such type, returns
463 //============================================================================
464 Handle(TDF_Attribute) SALOMEDSImpl_StudyBuilder::FindOrCreateAttribute(const Handle(SALOMEDSImpl_SObject)& anObject,
465 const TCollection_AsciiString& aTypeOfAttribute)
468 if(anObject.IsNull()) {
469 _errorCode = "Invalid arguments";
473 TDF_Label Lab = anObject->GetLabel();
475 _errorCode = "Null label";
481 //The macro adds all necessary checks for standardly behaiving attributes
482 __FindOrCreateAttributeForBuilder
485 //Add checks for TreeNode and UserID attributes
486 if (strncmp(aTypeOfAttribute.ToCString(), "AttributeTreeNode",17) == 0 ) {
488 Standard_GUID aTreeNodeGUID;
489 if (strcmp(aTypeOfAttribute.ToCString(), "AttributeTreeNode") == 0) {
490 aTreeNodeGUID = SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID();
492 char* aGUIDString = new char[41];
493 char* aType = (char*)aTypeOfAttribute.ToCString();
494 sprintf(aGUIDString, &(aType[17]));
495 if(!Standard_GUID::CheckGUIDFormat(aGUIDString)) {
499 aTreeNodeGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
502 Handle(SALOMEDSImpl_AttributeTreeNode) anAttr;
503 if (!Lab.FindAttribute(aTreeNodeGUID, anAttr)) {
505 anAttr = SALOMEDSImpl_AttributeTreeNode::Set(Lab, aTreeNodeGUID);
510 if (strncmp(aTypeOfAttribute.ToCString(), "AttributeUserID",15) == 0 ) {
511 Standard_GUID aUserGUID;
512 if (strcmp(aTypeOfAttribute.ToCString(), "AttributeUserID") == 0) {
513 aUserGUID = SALOMEDSImpl_AttributeUserID::DefaultID();
515 char* aGUIDString = new char[41];
516 char* aType = (char*)aTypeOfAttribute.ToCString();
517 sprintf(aGUIDString, &(aType[15]));
518 if(!Standard_GUID::CheckGUIDFormat(aGUIDString)) {
522 aUserGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
525 Handle(SALOMEDSImpl_AttributeUserID) anAttr;
526 if (!Lab.FindAttribute(SALOMEDSImpl_AttributeUserID::DefaultID(), anAttr)) {
528 anAttr = SALOMEDSImpl_AttributeUserID::Set(Lab, aUserGUID);
532 _errorCode = "Can not create an attribute";
537 //============================================================================
538 /*! Function : FindAttribute
539 * Purpose : Find attribute of given type assigned SObject, returns Standard_True if it is found
541 //============================================================================
543 bool SALOMEDSImpl_StudyBuilder::FindAttribute(const Handle(SALOMEDSImpl_SObject)& anObject,
544 Handle(TDF_Attribute)& anAttribute,
545 const TCollection_AsciiString& aTypeOfAttribute)
548 if(anObject.IsNull()) {
549 _errorCode = "Invalid arguments";
552 TDF_Label Lab = anObject->GetLabel();
553 if (Lab.FindAttribute(SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute), anAttribute)) {
554 // commented out because NO MODIFICATION is done to attributes when calling FindAttribute()
556 return Standard_True;
558 return Standard_False;
561 //============================================================================
562 /*! Function : RemoveAttribute
563 * Purpose : Remove attribute of given type assigned SObject
565 //============================================================================
567 bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const Handle(SALOMEDSImpl_SObject)& anObject,
568 const TCollection_AsciiString& aTypeOfAttribute)
572 if(anObject.IsNull()) {
573 _errorCode = "Invalid arguments";
576 TDF_Label Lab = anObject->GetLabel();
578 if (aTypeOfAttribute == "AttributeIOR") { // postponed removing of CORBA objects
579 Handle(SALOMEDSImpl_AttributeIOR) anAttr;
580 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
581 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(anAttr->Value());
584 Lab.ForgetAttribute (SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute));
591 //============================================================================
592 /*! Function : Addreference
595 //============================================================================
596 bool SALOMEDSImpl_StudyBuilder::Addreference(const Handle(SALOMEDSImpl_SObject)& me,
597 const Handle(SALOMEDSImpl_SObject)& theReferencedObject)
600 if(me.IsNull() || theReferencedObject.IsNull()) {
601 _errorCode = "Invalid arguments";
605 TDF_Label Lab = me->GetLabel();
606 TDF_Label RefLab = theReferencedObject->GetLabel();
607 SALOMEDSImpl_AttributeReference::Set(Lab,RefLab);
609 SALOMEDSImpl_AttributeTarget::Set(RefLab)->Add(SALOMEDSImpl_Study::SObject(Lab));
611 if(!_callbackOnRemove.IsNull() && Lab.IsDescendant(_doc->Main())) _callbackOnRemove->OnRemoveSObject(me);
616 //============================================================================
617 /*! Function : RemoveReference
620 //============================================================================
621 bool SALOMEDSImpl_StudyBuilder::RemoveReference(const Handle(SALOMEDSImpl_SObject)& me)
624 Handle(SALOMEDSImpl_SObject) theReferencedObject;
625 if(!me->ReferencedObject(theReferencedObject)) return false; //No reference is found
628 TDF_Label Lab = me->GetLabel();
630 Lab.ForgetAttribute(SALOMEDSImpl_AttributeReference::GetID());
632 //SRN: 30 Aug, 2004 : fix from Ecole l'ete version
634 TDF_Label RefLab = theReferencedObject->GetLabel();
636 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
637 if(RefLab.FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(), aTarget))
638 aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
647 //============================================================================
648 /*! Function : AddDirectory
649 * Purpose : adds a new directory with a path = thePath
651 //============================================================================
652 bool SALOMEDSImpl_StudyBuilder::AddDirectory(const TCollection_AsciiString& thePath)
656 if(thePath.IsEmpty() || thePath == "") {
657 _errorCode = "Invalid path";
661 TCollection_AsciiString aPath(thePath), aContext(""), aFatherPath;
663 Handle(SALOMEDSImpl_Study) aStudy = SALOMEDSImpl_Study::GetStudy(_doc->Main());
664 Handle(SALOMEDSImpl_SObject) anObject;
667 anObject = aStudy->FindObjectByPath(thePath); //Check if the directory already exists
671 if(!anObject.IsNull()) {
672 _errorCode = "StudyNameAlreadyUsed";
676 if(aPath.Value(1) != '/') { //Relative path
678 aPath = aStudy->GetContext() + aPath;
681 TCollection_AsciiString aToken = aPath.Token("/", 1);
682 if(aToken.Length() == 0) aFatherPath = "/";
685 while(aToken.Length() != 0) {
686 if(aPath.Token("/", i+1).Length() > 0) {
688 aFatherPath += aToken;
690 aToken = aPath.Token("/", ++i);
695 anObject = aStudy->FindObjectByPath(aFatherPath); //Check if the father directory exists
698 if(anObject.IsNull()) {
699 _errorCode = "StudyInvalidDirectory";
703 Handle(SALOMEDSImpl_SObject) aNewObject = NewObject(anObject);
704 aLabel = aNewObject->GetLabel();
705 if(aLabel.IsNull()) {
706 _errorCode = "StudyInvalidComponent";
710 SALOMEDSImpl_AttributeName::Set(aLabel, aPath.Token("/", i-1));
712 //Set LocalID attribute to identify the directory object
713 Handle(SALOMEDSImpl_AttributeLocalID) aLocalID = SALOMEDSImpl_AttributeLocalID::Set(aLabel, DIRECTORYID);
721 //============================================================================
722 /*! Function : SetGUID
725 //============================================================================
726 bool SALOMEDSImpl_StudyBuilder::SetGUID(const Handle(SALOMEDSImpl_SObject)& anObject,
727 const TCollection_AsciiString& theGUID)
731 if(anObject.IsNull()) {
732 _errorCode = "Invalid arguments";
736 TDF_Label aLabel = anObject->GetLabel();
737 SALOMEDSImpl_AttributeUserID::Set(aLabel, theGUID.ToCString());
744 //============================================================================
745 /*! Function : IsGUID
748 //============================================================================
749 bool SALOMEDSImpl_StudyBuilder::IsGUID(const Handle(SALOMEDSImpl_SObject)& anObject,
750 const TCollection_AsciiString& theGUID)
753 if(anObject.IsNull()) {
754 _errorCode = "Invalid arguments";
757 TDF_Label aLabel = anObject->GetLabel();
758 return aLabel.IsAttribute(theGUID.ToCString());
762 //============================================================================
763 /*! Function : NewCommand
766 //============================================================================
767 void SALOMEDSImpl_StudyBuilder::NewCommand()
770 // mpv: for SAL2114 - unset "lock changed" flag at the operation start
771 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
772 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
773 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
774 _doc->Main().AddAttribute(anAttr);
776 anAttr->IsLockChanged(true);
781 //============================================================================
782 /*! Function : CommitCommand
785 //============================================================================
786 void SALOMEDSImpl_StudyBuilder::CommitCommand()
789 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
790 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
791 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
792 _doc->Main().AddAttribute(anAttr);
794 if (anAttr->IsLocked() && !anAttr->IsLockChanged(true)) {
796 _errorCode = "LockProtection";
797 throw LockProtection("LockProtection");
799 SALOMEDSImpl_Study::GetStudy(_doc->Main())->RemovePostponed(_doc->GetUndoLimit());
801 int aModif = anAttr->GetModified();
802 if (aModif < 0) aModif = 1000; // if user make undo and then - new transaction "modify" will never be zero
803 anAttr->SetModified(aModif+1);
804 _doc->CommitCommand();
810 //============================================================================
811 /*! Function : HasOpenCommand
814 //============================================================================
815 bool SALOMEDSImpl_StudyBuilder::HasOpenCommand()
818 return _doc->HasOpenCommand();
821 //============================================================================
822 /*! Function : AbortCommand
825 //============================================================================
826 void SALOMEDSImpl_StudyBuilder::AbortCommand()
829 SALOMEDSImpl_Study::GetStudy(_doc->Main())->UndoPostponed(0);
831 _doc->AbortCommand();
834 //============================================================================
838 //============================================================================
839 void SALOMEDSImpl_StudyBuilder::Undo()
842 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
843 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
844 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
845 _doc->Main().AddAttribute(anAttr);
847 if (anAttr->IsLocked()) {
848 _errorCode = "LockProtection";
849 throw LockProtection("LockProtection");
851 SALOMEDSImpl_Study::GetStudy(_doc->Main())->UndoPostponed(1);
853 anAttr->SetModified(anAttr->GetModified()-1);
859 //============================================================================
863 //============================================================================
864 void SALOMEDSImpl_StudyBuilder::Redo()
867 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
868 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
869 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
870 _doc->Main().AddAttribute(anAttr);
873 if (anAttr->IsLocked()) {
874 _errorCode = "LockProtection";
875 throw LockProtection("LockProtection");
878 SALOMEDSImpl_Study::GetStudy(_doc->Main())->UndoPostponed(-1);
879 anAttr->SetModified(anAttr->GetModified()+1);
885 //============================================================================
886 /*! Function : GetAvailableUndos
889 //============================================================================
890 bool SALOMEDSImpl_StudyBuilder::GetAvailableUndos()
893 return _doc->GetAvailableUndos();
896 //============================================================================
897 /*! Function : GetAvailableRedos
900 //============================================================================
901 bool SALOMEDSImpl_StudyBuilder::GetAvailableRedos()
904 return _doc->GetAvailableRedos();
907 //============================================================================
908 /*! Function : UndoLimit
911 //============================================================================
912 int SALOMEDSImpl_StudyBuilder::UndoLimit()
915 return _doc->GetUndoLimit();
918 //============================================================================
919 /*! Function : UndoLimit
922 //============================================================================
923 void SALOMEDSImpl_StudyBuilder::UndoLimit(int n)
927 _doc->SetUndoLimit (n);
930 //============================================================================
931 /*! Function : SetOnAddSObject
934 //============================================================================
935 Handle(SALOMEDSImpl_Callback)
936 SALOMEDSImpl_StudyBuilder::SetOnAddSObject(const Handle(SALOMEDSImpl_Callback)& theCallback)
939 Handle(SALOMEDSImpl_Callback) aRet = _callbackOnAdd;
940 _callbackOnAdd = theCallback;
944 //============================================================================
945 /*! Function : SetOnNewSObject
948 //============================================================================
949 Handle(SALOMEDSImpl_Callback)
950 SALOMEDSImpl_StudyBuilder::SetOnRemoveSObject(const Handle(SALOMEDSImpl_Callback)& theCallback)
953 Handle(SALOMEDSImpl_Callback) aRet = _callbackOnRemove;
954 _callbackOnRemove = theCallback;
958 //============================================================================
959 /*! Function : CheckLocked
962 //============================================================================
963 void SALOMEDSImpl_StudyBuilder::CheckLocked()
966 if (_doc->HasOpenCommand()) return;
967 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
968 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
969 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
970 _doc->Main().AddAttribute(anAttr);
972 if (anAttr->IsLocked()) {
973 _errorCode = "LockProtection";
974 throw LockProtection("LockProtection");
978 //============================================================================
979 /*! Function : SetName
982 //============================================================================
983 bool SALOMEDSImpl_StudyBuilder::SetName(const Handle(SALOMEDSImpl_SObject)& theSO,
984 const TCollection_AsciiString& theValue)
989 _errorCode = "Invalid arguments";
992 SALOMEDSImpl_AttributeName::Set(theSO->GetLabel(), theValue);
999 //============================================================================
1000 /*! Function : SetComment
1003 //============================================================================
1004 bool SALOMEDSImpl_StudyBuilder::SetComment(const Handle(SALOMEDSImpl_SObject)& theSO,
1005 const TCollection_AsciiString& theValue)
1009 if(theSO.IsNull()) {
1010 _errorCode = "Invalid arguments";
1013 SALOMEDSImpl_AttributeComment::Set(theSO->GetLabel(), theValue);
1020 //============================================================================
1021 /*! Function : SetIOR
1024 //============================================================================
1025 bool SALOMEDSImpl_StudyBuilder::SetIOR(const Handle(SALOMEDSImpl_SObject)& theSO,
1026 const TCollection_AsciiString& theValue)
1030 if(theSO.IsNull()) {
1031 _errorCode = "Invalid arguments";
1034 SALOMEDSImpl_AttributeIOR::Set(theSO->GetLabel(), theValue);
1042 //============================================================================
1043 /*! Function : Translate_persistentID_to_IOR
1046 //============================================================================
1047 static void Translate_persistentID_to_IOR(TDF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII)
1049 if(driver == NULL) return;
1050 TDF_ChildIterator itchild (Lab);
1052 for (; itchild.More(); itchild.Next()) {
1053 TDF_Label current = itchild.Value();
1054 Handle(SALOMEDSImpl_AttributePersistentRef) Att;
1055 if (current.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID(),Att)) {
1057 Handle(SALOMEDSImpl_AttributeLocalID) anID;
1058 if (current.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID(), anID))
1059 if (anID->Value() == FILELOCALID) continue; //SRN: This attribute store a file name, skip it
1061 TCollection_AsciiString persist_ref(Att->Value());
1062 Handle(SALOMEDSImpl_SObject) so = SALOMEDSImpl_Study::SObject(current);
1063 TCollection_AsciiString ior_string = driver->LocalPersistentIDToIOR(so,
1067 SALOMEDSImpl_AttributeIOR::Set (current, ior_string);
1070 Translate_persistentID_to_IOR (current, driver, isMultiFile, isASCII);