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 <TDF_ChildIterator.hxx>
37 #include <TDF_Label.hxx>
38 #include <TDF_Tool.hxx>
39 #include <TDF_Data.hxx>
40 #include <SALOMEDSImpl_ChildNodeIterator.hxx>
41 #include <TDF_ListIteratorOfAttributeList.hxx>
46 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_StudyBuilder, MMgt_TShared )
47 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_StudyBuilder, MMgt_TShared )
49 #define USE_CASE_LABEL_TAG 2
50 #define DIRECTORYID 16661
51 #define FILELOCALID 26662
53 static void Translate_persistentID_to_IOR(TDF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII);
55 //============================================================================
56 /*! Function : constructor
59 //============================================================================
60 SALOMEDSImpl_StudyBuilder::SALOMEDSImpl_StudyBuilder(const Handle(Standard_Transient)& theOwner)
64 _doc = Handle(SALOMEDSImpl_Study)::DownCast(theOwner)->GetDocument();
67 //============================================================================
68 /*! Function : destructor
71 //============================================================================
72 SALOMEDSImpl_StudyBuilder::~SALOMEDSImpl_StudyBuilder()
75 //============================================================================
76 /*! Function : NewComponent
77 * Purpose : Create a new component (Scomponent)
79 //============================================================================
80 Handle(SALOMEDSImpl_SComponent) SALOMEDSImpl_StudyBuilder::NewComponent(const TCollection_AsciiString& DataType)
85 if(DataType.Length() == 0) return 0;
87 //Always create component under main label.
88 TDF_Label L = _doc->Main();
91 for (TDF_ChildIterator it(L); it.More(); it.Next()) {
92 if (it.Value().Tag() > imax)
93 imax = it.Value().Tag();
96 TDF_Label NL = L.FindChild(imax);
98 SALOMEDSImpl_AttributeComment::Set(NL, DataType);
100 Handle(SALOMEDSImpl_SComponent) so = Handle(SALOMEDSImpl_Study)::DownCast(_study)->GetSComponent (NL);
102 if(!_callbackOnAdd.IsNull()) _callbackOnAdd->OnAddSObject(so);
109 //============================================================================
110 /*! Function : DefineComponentInstance
111 * Purpose : Add IOR attribute of a Scomponent
113 //============================================================================
114 bool SALOMEDSImpl_StudyBuilder::DefineComponentInstance(const Handle(SALOMEDSImpl_SComponent)& aComponent,
115 const TCollection_AsciiString& IOR)
120 if(aComponent.IsNull() || IOR.IsEmpty()) {
121 _errorCode = "Invalid arguments";
125 SALOMEDSImpl_AttributeIOR::Set(aComponent->GetLabel(), IOR);
132 //============================================================================
133 /*! Function : RemoveComponent
134 * Purpose : Delete a Scomponent
136 //============================================================================
137 bool SALOMEDSImpl_StudyBuilder::RemoveComponent(const Handle(SALOMEDSImpl_SComponent)& aComponent)
141 return RemoveObject(aComponent);
144 //============================================================================
145 /*! Function : NewObject
146 * Purpose : Create a new SObject
148 //============================================================================
149 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_StudyBuilder::NewObject(const Handle(SALOMEDSImpl_SObject)& theFatherObject)
154 //Find label of father
155 TDF_Label Lab = theFatherObject->GetLabel();
159 for (TDF_ChildIterator it(Lab); it.More(); it.Next()) {
160 if (it.Value().Tag() > imax)
161 imax = it.Value().Tag();
164 TDF_Label NewLab = Lab.FindChild(imax);
166 Handle(SALOMEDSImpl_SObject) so = Handle(SALOMEDSImpl_Study)::DownCast(_study)->GetSObject(NewLab);
167 if(!_callbackOnAdd.IsNull()) _callbackOnAdd->OnAddSObject(so);
173 //============================================================================
174 /*! Function : NewObjectToTag
177 //============================================================================
178 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_StudyBuilder::NewObjectToTag(const Handle(SALOMEDSImpl_SObject)& theFatherObject,
183 //Find label of father
184 TDF_Label Lab = theFatherObject->GetLabel();
186 //Create or find label
187 TDF_Label NewLab = Lab.FindChild(theTag, 1);
189 Handle(SALOMEDSImpl_SObject) so = Handle(SALOMEDSImpl_Study)::DownCast(_study)->GetSObject(NewLab);
191 if(!_callbackOnAdd.IsNull()) _callbackOnAdd->OnAddSObject(so);
197 //============================================================================
198 /*! Function : RemoveObject
201 //============================================================================
202 bool SALOMEDSImpl_StudyBuilder::RemoveObject(const Handle(SALOMEDSImpl_SObject)& anObject)
206 if(anObject.IsNull()) {
207 _errorCode = "Null object";
211 if(!_callbackOnRemove.IsNull()) _callbackOnRemove->OnRemoveSObject(anObject);
213 TDF_Label Lab = anObject->GetLabel();
215 Handle(SALOMEDSImpl_AttributeReference) aReference;
216 if (Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aReference)) {
217 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
218 if (aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(),aTarget))
219 aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
222 Handle(SALOMEDSImpl_AttributeIOR) anAttr; // postponed removing of CORBA objects
223 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
224 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(TCollection_AsciiString(anAttr->Value()).ToCString());
226 Lab.ForgetAllAttributes();
233 //============================================================================
234 /*! Function : RemoveObjectWithChildren
237 //============================================================================
238 bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const Handle(SALOMEDSImpl_SObject)& anObject)
242 if(anObject.IsNull()) {
243 _errorCode = "Null object";
247 if(!_callbackOnRemove.IsNull()) _callbackOnRemove->OnRemoveSObject(anObject);
249 TDF_Label Lab = anObject->GetLabel();
251 Handle(SALOMEDSImpl_AttributeReference) aReference;
252 if (Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aReference)) {
253 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
254 if (aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(),aTarget))
255 aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
257 Handle(SALOMEDSImpl_AttributeIOR) anAttr; // postponed removing of CORBA objects
258 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
259 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(TCollection_AsciiString(anAttr->Value()).ToCString());
261 TDF_ChildIterator it(Lab, Standard_True);
262 for(;it.More();it.Next()) {
263 TDF_Label aLabel = it.Value();
264 if (aLabel.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aReference)) {
265 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
266 if (aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(),aTarget))
267 aTarget->Remove(SALOMEDSImpl_Study::SObject(aLabel));
269 Handle(SALOMEDSImpl_AttributeIOR) anAttr; // postponed removing of CORBA objects
270 if (aLabel.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
271 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(TCollection_AsciiString(anAttr->Value()).ToCString());
274 Lab.ForgetAllAttributes(Standard_True);
281 //============================================================================
282 /*! Function : LoadWith
285 //============================================================================
286 bool SALOMEDSImpl_StudyBuilder::LoadWith(const Handle(SALOMEDSImpl_SComponent)& anSCO,
287 SALOMEDSImpl_Driver* aDriver)
291 TDF_Label Lab = anSCO->GetLabel();
292 Handle(SALOMEDSImpl_AttributePersistentRef) Att;
294 //Find the current Url of the study
295 if (_doc->Main().FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID(),Att)) {
296 int aLocked = anSCO->GetStudy()->GetProperties()->IsLocked();
297 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(false);
299 TCollection_ExtendedString Res(Att->Value());
301 Handle(SALOMEDSImpl_AttributeComment) type;
302 TCollection_ExtendedString DataType;
303 if (Lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID(),type))
304 DataType = type->Value();
306 // associate the driver to the SComponent
307 if(aDriver == NULL) {
308 _errorCode = "Driver is null";
312 // mpv 06.03.2003: SAL1927 - if component data if already loaded, it is not necessary to do it again
313 Handle(SALOMEDSImpl_AttributeIOR) attrIOR;
314 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), attrIOR)) {
315 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
319 DefineComponentInstance (anSCO, aDriver->GetIOR());
321 TCollection_AsciiString aHDFPath(Res);
323 TCollection_AsciiString aHDFUrl;
324 bool isASCII = false;
325 if (HDFascii::isASCII(aHDFPath.ToCString())) {
327 aHDFUrl = HDFascii::ConvertFromASCIIToHDF(aHDFPath.ToCString());
328 aHDFUrl += "hdf_from_ascii.hdf";
333 //Open the Study HDF file
334 HDFfile *hdf_file = new HDFfile(aHDFUrl.ToCString());
336 char aMultifileState[2];
337 char ASCIIfileState[2];
339 TCollection_AsciiString scoid = anSCO->GetID();
340 hdf_file->OpenOnDisk(HDF_RDONLY);
341 HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
342 hdf_group->OpenOnDisk();
343 HDFgroup *hdf_sco_group = new HDFgroup(scoid.ToCString(), hdf_group);
344 hdf_sco_group->OpenOnDisk();
346 unsigned char* aStreamFile = NULL;
349 if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
350 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
351 hdf_dataset->OpenOnDisk();
352 aStreamSize = hdf_dataset->GetSize();
353 aStreamFile = new unsigned char[aStreamSize];
354 if(aStreamFile == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
355 hdf_dataset->ReadFromDisk(aStreamFile);
356 hdf_dataset->CloseOnDisk();
361 HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
362 multifile_hdf_dataset->OpenOnDisk();
363 multifile_hdf_dataset->ReadFromDisk(aMultifileState);
365 HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
366 ascii_hdf_dataset->OpenOnDisk();
367 ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
369 // set path without file name from URL
370 int aFileNameSize = Res.Length();
371 char* aDir = new char[aFileNameSize];
372 memcpy(aDir, TCollection_AsciiString(Res).ToCString(), aFileNameSize);
373 for(int aCounter = aFileNameSize-1; aCounter>=0; aCounter--)
374 if (aDir[aCounter] == '/') {
375 aDir[aCounter+1] = 0;
379 bool aResult = (ASCIIfileState[0]=='A')?
380 aDriver->LoadASCII(anSCO, aStreamFile, aStreamSize, aDir, aMultifileState[0]=='M'):
381 aDriver->Load(anSCO, aStreamFile, aStreamSize, aDir, aMultifileState[0]=='M');
383 if(aStreamFile != NULL) delete []aStreamFile;
386 RemoveAttribute( anSCO, "AttributeIOR" );
388 _errorCode = "Can't load component";
389 throw HDFexception("Unable to load component");
392 if(aDir != NULL) delete []aDir;
394 multifile_hdf_dataset->CloseOnDisk();
395 multifile_hdf_dataset = 0;
396 ascii_hdf_dataset->CloseOnDisk();
397 ascii_hdf_dataset = 0;
399 hdf_sco_group->CloseOnDisk();
401 hdf_group->CloseOnDisk();
403 hdf_file->CloseOnDisk();
407 Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
408 aFilesToRemove->Append("hdf_from_ascii.hdf");
409 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl),
410 aFilesToRemove, true);
413 catch (HDFexception) {
417 Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
418 aFilesToRemove->Append(aHDFUrl);
419 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
422 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
423 _errorCode = "No persistent file";
428 Translate_persistentID_to_IOR (Lab, aDriver, aMultifileState[0]=='M', ASCIIfileState[0] == 'A');
430 _errorCode = "Can not convert persistent IDs to IORs";
434 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
436 _errorCode = "No persistent file";
443 //============================================================================
447 //============================================================================
448 bool SALOMEDSImpl_StudyBuilder::Load(const Handle(SALOMEDSImpl_SObject)& sco)
450 _errorCode = "Not implemented";
454 //============================================================================
455 /*! Function : FindOrCreateAttribute
456 * Purpose : Add attribute of given type to SObject, if there is attribute of such type, returns
459 //============================================================================
460 Handle(TDF_Attribute) SALOMEDSImpl_StudyBuilder::FindOrCreateAttribute(const Handle(SALOMEDSImpl_SObject)& anObject,
461 const TCollection_AsciiString& aTypeOfAttribute)
464 if(anObject.IsNull()) {
465 _errorCode = "Invalid arguments";
469 TDF_Label Lab = anObject->GetLabel();
471 _errorCode = "Null label";
477 //The macro adds all necessary checks for standardly behaiving attributes
478 __FindOrCreateAttributeForBuilder
481 //Add checks for TreeNode and UserID attributes
482 if (strncmp(aTypeOfAttribute.ToCString(), "AttributeTreeNode",17) == 0 ) {
484 Standard_GUID aTreeNodeGUID;
485 if (strcmp(aTypeOfAttribute.ToCString(), "AttributeTreeNode") == 0) {
486 aTreeNodeGUID = SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID();
488 char* aGUIDString = new char[41];
489 char* aType = (char*)aTypeOfAttribute.ToCString();
490 sprintf(aGUIDString, &(aType[17]));
491 if(!Standard_GUID::CheckGUIDFormat(aGUIDString)) {
495 aTreeNodeGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
498 Handle(SALOMEDSImpl_AttributeTreeNode) anAttr;
499 if (!Lab.FindAttribute(aTreeNodeGUID, anAttr)) {
501 anAttr = SALOMEDSImpl_AttributeTreeNode::Set(Lab, aTreeNodeGUID);
506 if (strncmp(aTypeOfAttribute.ToCString(), "AttributeUserID",15) == 0 ) {
507 Standard_GUID aUserGUID;
508 if (strcmp(aTypeOfAttribute.ToCString(), "AttributeUserID") == 0) {
509 aUserGUID = SALOMEDSImpl_AttributeUserID::DefaultID();
511 char* aGUIDString = new char[41];
512 char* aType = (char*)aTypeOfAttribute.ToCString();
513 sprintf(aGUIDString, &(aType[15]));
514 if(!Standard_GUID::CheckGUIDFormat(aGUIDString)) {
518 aUserGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
521 Handle(SALOMEDSImpl_AttributeUserID) anAttr;
522 if (!Lab.FindAttribute(SALOMEDSImpl_AttributeUserID::DefaultID(), anAttr)) {
524 anAttr = SALOMEDSImpl_AttributeUserID::Set(Lab, aUserGUID);
528 _errorCode = "Can not create an attribute";
533 //============================================================================
534 /*! Function : FindAttribute
535 * Purpose : Find attribute of given type assigned SObject, returns Standard_True if it is found
537 //============================================================================
539 bool SALOMEDSImpl_StudyBuilder::FindAttribute(const Handle(SALOMEDSImpl_SObject)& anObject,
540 Handle(TDF_Attribute)& anAttribute,
541 const TCollection_AsciiString& aTypeOfAttribute)
544 if(anObject.IsNull()) {
545 _errorCode = "Invalid arguments";
548 TDF_Label Lab = anObject->GetLabel();
549 if (Lab.FindAttribute(SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute), anAttribute)) {
550 // commented out because NO MODIFICATION is done to attributes when calling FindAttribute()
552 return Standard_True;
554 return Standard_False;
557 //============================================================================
558 /*! Function : RemoveAttribute
559 * Purpose : Remove attribute of given type assigned SObject
561 //============================================================================
563 bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const Handle(SALOMEDSImpl_SObject)& anObject,
564 const TCollection_AsciiString& aTypeOfAttribute)
568 if(anObject.IsNull()) {
569 _errorCode = "Invalid arguments";
572 TDF_Label Lab = anObject->GetLabel();
574 if (aTypeOfAttribute == "AttributeIOR") { // postponed removing of CORBA objects
575 Handle(SALOMEDSImpl_AttributeIOR) anAttr;
576 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
577 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(anAttr->Value());
580 Lab.ForgetAttribute (SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute));
587 //============================================================================
588 /*! Function : Addreference
591 //============================================================================
592 bool SALOMEDSImpl_StudyBuilder::Addreference(const Handle(SALOMEDSImpl_SObject)& me,
593 const Handle(SALOMEDSImpl_SObject)& theReferencedObject)
596 if(me.IsNull() || theReferencedObject.IsNull()) {
597 _errorCode = "Invalid arguments";
601 TDF_Label Lab = me->GetLabel();
602 TDF_Label RefLab = theReferencedObject->GetLabel();
603 SALOMEDSImpl_AttributeReference::Set(Lab,RefLab);
605 SALOMEDSImpl_AttributeTarget::Set(RefLab)->Add(SALOMEDSImpl_Study::SObject(Lab));
607 if(!_callbackOnRemove.IsNull() && Lab.IsDescendant(_doc->Main())) _callbackOnRemove->OnRemoveSObject(me);
612 //============================================================================
613 /*! Function : RemoveReference
616 //============================================================================
617 bool SALOMEDSImpl_StudyBuilder::RemoveReference(const Handle(SALOMEDSImpl_SObject)& me)
620 Handle(SALOMEDSImpl_SObject) theReferencedObject;
621 if(!me->ReferencedObject(theReferencedObject)) return false; //No reference is found
624 TDF_Label Lab = me->GetLabel();
626 Lab.ForgetAttribute(SALOMEDSImpl_AttributeReference::GetID());
628 //SRN: 30 Aug, 2004 : fix from Ecole l'ete version
630 TDF_Label RefLab = theReferencedObject->GetLabel();
632 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
633 if(RefLab.FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(), aTarget))
634 aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
643 //============================================================================
644 /*! Function : AddDirectory
645 * Purpose : adds a new directory with a path = thePath
647 //============================================================================
648 bool SALOMEDSImpl_StudyBuilder::AddDirectory(const TCollection_AsciiString& thePath)
652 if(thePath.IsEmpty() || thePath == "") {
653 _errorCode = "Invalid path";
657 TCollection_AsciiString aPath(thePath), aContext(""), aFatherPath;
659 Handle(SALOMEDSImpl_Study) aStudy = SALOMEDSImpl_Study::GetStudy(_doc->Main());
660 Handle(SALOMEDSImpl_SObject) anObject;
663 anObject = aStudy->FindObjectByPath(thePath); //Check if the directory already exists
667 if(!anObject.IsNull()) {
668 _errorCode = "StudyNameAlreadyUsed";
672 if(aPath.Value(1) != '/') { //Relative path
674 aPath = aStudy->GetContext() + aPath;
677 TCollection_AsciiString aToken = aPath.Token("/", 1);
678 if(aToken.Length() == 0) aFatherPath = "/";
681 while(aToken.Length() != 0) {
682 if(aPath.Token("/", i+1).Length() > 0) {
684 aFatherPath += aToken;
686 aToken = aPath.Token("/", ++i);
691 anObject = aStudy->FindObjectByPath(aFatherPath); //Check if the father directory exists
694 if(anObject.IsNull()) {
695 _errorCode = "StudyInvalidDirectory";
699 Handle(SALOMEDSImpl_SObject) aNewObject = NewObject(anObject);
700 aLabel = aNewObject->GetLabel();
701 if(aLabel.IsNull()) {
702 _errorCode = "StudyInvalidComponent";
706 SALOMEDSImpl_AttributeName::Set(aLabel, aPath.Token("/", i-1));
708 //Set LocalID attribute to identify the directory object
709 Handle(SALOMEDSImpl_AttributeLocalID) aLocalID = SALOMEDSImpl_AttributeLocalID::Set(aLabel, DIRECTORYID);
717 //============================================================================
718 /*! Function : SetGUID
721 //============================================================================
722 bool SALOMEDSImpl_StudyBuilder::SetGUID(const Handle(SALOMEDSImpl_SObject)& anObject,
723 const TCollection_AsciiString& theGUID)
727 if(anObject.IsNull()) {
728 _errorCode = "Invalid arguments";
732 TDF_Label aLabel = anObject->GetLabel();
733 SALOMEDSImpl_AttributeUserID::Set(aLabel, theGUID.ToCString());
740 //============================================================================
741 /*! Function : IsGUID
744 //============================================================================
745 bool SALOMEDSImpl_StudyBuilder::IsGUID(const Handle(SALOMEDSImpl_SObject)& anObject,
746 const TCollection_AsciiString& theGUID)
749 if(anObject.IsNull()) {
750 _errorCode = "Invalid arguments";
753 TDF_Label aLabel = anObject->GetLabel();
754 return aLabel.IsAttribute(theGUID.ToCString());
758 //============================================================================
759 /*! Function : NewCommand
762 //============================================================================
763 void SALOMEDSImpl_StudyBuilder::NewCommand()
766 // mpv: for SAL2114 - unset "lock changed" flag at the operation start
767 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
768 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
769 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
770 _doc->Main().AddAttribute(anAttr);
772 anAttr->IsLockChanged(true);
777 //============================================================================
778 /*! Function : CommitCommand
781 //============================================================================
782 void SALOMEDSImpl_StudyBuilder::CommitCommand()
785 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
786 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
787 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
788 _doc->Main().AddAttribute(anAttr);
790 if (anAttr->IsLocked() && !anAttr->IsLockChanged(true)) {
792 _errorCode = "LockProtection";
793 throw LockProtection("LockProtection");
795 SALOMEDSImpl_Study::GetStudy(_doc->Main())->RemovePostponed(_doc->GetUndoLimit());
797 int aModif = anAttr->GetModified();
798 if (aModif < 0) aModif = 1000; // if user make undo and then - new transaction "modify" will never be zero
799 anAttr->SetModified(aModif+1);
800 _doc->CommitCommand();
806 //============================================================================
807 /*! Function : HasOpenCommand
810 //============================================================================
811 bool SALOMEDSImpl_StudyBuilder::HasOpenCommand()
814 return _doc->HasOpenCommand();
817 //============================================================================
818 /*! Function : AbortCommand
821 //============================================================================
822 void SALOMEDSImpl_StudyBuilder::AbortCommand()
825 SALOMEDSImpl_Study::GetStudy(_doc->Main())->UndoPostponed(0);
827 _doc->AbortCommand();
830 //============================================================================
834 //============================================================================
835 void SALOMEDSImpl_StudyBuilder::Undo()
838 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
839 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
840 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
841 _doc->Main().AddAttribute(anAttr);
843 if (anAttr->IsLocked()) {
844 _errorCode = "LockProtection";
845 throw LockProtection("LockProtection");
847 SALOMEDSImpl_Study::GetStudy(_doc->Main())->UndoPostponed(1);
849 anAttr->SetModified(anAttr->GetModified()-1);
855 //============================================================================
859 //============================================================================
860 void SALOMEDSImpl_StudyBuilder::Redo()
863 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
864 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
865 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
866 _doc->Main().AddAttribute(anAttr);
869 if (anAttr->IsLocked()) {
870 _errorCode = "LockProtection";
871 throw LockProtection("LockProtection");
874 SALOMEDSImpl_Study::GetStudy(_doc->Main())->UndoPostponed(-1);
875 anAttr->SetModified(anAttr->GetModified()+1);
881 //============================================================================
882 /*! Function : GetAvailableUndos
885 //============================================================================
886 bool SALOMEDSImpl_StudyBuilder::GetAvailableUndos()
889 return _doc->GetAvailableUndos();
892 //============================================================================
893 /*! Function : GetAvailableRedos
896 //============================================================================
897 bool SALOMEDSImpl_StudyBuilder::GetAvailableRedos()
900 return _doc->GetAvailableRedos();
903 //============================================================================
904 /*! Function : UndoLimit
907 //============================================================================
908 int SALOMEDSImpl_StudyBuilder::UndoLimit()
911 return _doc->GetUndoLimit();
914 //============================================================================
915 /*! Function : UndoLimit
918 //============================================================================
919 void SALOMEDSImpl_StudyBuilder::UndoLimit(int n)
923 _doc->SetUndoLimit (n);
926 //============================================================================
927 /*! Function : SetOnAddSObject
930 //============================================================================
931 Handle(SALOMEDSImpl_Callback)
932 SALOMEDSImpl_StudyBuilder::SetOnAddSObject(const Handle(SALOMEDSImpl_Callback)& theCallback)
935 Handle(SALOMEDSImpl_Callback) aRet = _callbackOnAdd;
936 _callbackOnAdd = theCallback;
940 //============================================================================
941 /*! Function : SetOnNewSObject
944 //============================================================================
945 Handle(SALOMEDSImpl_Callback)
946 SALOMEDSImpl_StudyBuilder::SetOnRemoveSObject(const Handle(SALOMEDSImpl_Callback)& theCallback)
949 Handle(SALOMEDSImpl_Callback) aRet = _callbackOnRemove;
950 _callbackOnRemove = theCallback;
954 //============================================================================
955 /*! Function : CheckLocked
958 //============================================================================
959 void SALOMEDSImpl_StudyBuilder::CheckLocked()
962 if (_doc->HasOpenCommand()) return;
963 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
964 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
965 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
966 _doc->Main().AddAttribute(anAttr);
968 if (anAttr->IsLocked()) {
969 _errorCode = "LockProtection";
970 throw LockProtection("LockProtection");
974 //============================================================================
975 /*! Function : SetName
978 //============================================================================
979 bool SALOMEDSImpl_StudyBuilder::SetName(const Handle(SALOMEDSImpl_SObject)& theSO,
980 const TCollection_AsciiString& theValue)
985 _errorCode = "Invalid arguments";
988 SALOMEDSImpl_AttributeName::Set(theSO->GetLabel(), theValue);
995 //============================================================================
996 /*! Function : SetComment
999 //============================================================================
1000 bool SALOMEDSImpl_StudyBuilder::SetComment(const Handle(SALOMEDSImpl_SObject)& theSO,
1001 const TCollection_AsciiString& theValue)
1005 if(theSO.IsNull()) {
1006 _errorCode = "Invalid arguments";
1009 SALOMEDSImpl_AttributeComment::Set(theSO->GetLabel(), theValue);
1016 //============================================================================
1017 /*! Function : SetIOR
1020 //============================================================================
1021 bool SALOMEDSImpl_StudyBuilder::SetIOR(const Handle(SALOMEDSImpl_SObject)& theSO,
1022 const TCollection_AsciiString& theValue)
1026 if(theSO.IsNull()) {
1027 _errorCode = "Invalid arguments";
1030 SALOMEDSImpl_AttributeIOR::Set(theSO->GetLabel(), theValue);
1038 //============================================================================
1039 /*! Function : Translate_persistentID_to_IOR
1042 //============================================================================
1043 static void Translate_persistentID_to_IOR(TDF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII)
1045 if(driver == NULL) return;
1046 TDF_ChildIterator itchild (Lab);
1048 for (; itchild.More(); itchild.Next()) {
1049 TDF_Label current = itchild.Value();
1050 Handle(SALOMEDSImpl_AttributePersistentRef) Att;
1051 if (current.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID(),Att)) {
1053 Handle(SALOMEDSImpl_AttributeLocalID) anID;
1054 if (current.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID(), anID))
1055 if (anID->Value() == FILELOCALID) continue; //SRN: This attribute store a file name, skip it
1057 TCollection_AsciiString persist_ref(Att->Value());
1058 Handle(SALOMEDSImpl_SObject) so = SALOMEDSImpl_Study::SObject(current);
1059 TCollection_AsciiString ior_string = driver->LocalPersistentIDToIOR(so,
1063 SALOMEDSImpl_AttributeIOR::Set (current, ior_string);
1066 Translate_persistentID_to_IOR (current, driver, isMultiFile, isASCII);