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/
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)
84 //Always create component under main label.
85 TDF_Label L = _doc->Main();
88 for (TDF_ChildIterator it(L); it.More(); it.Next()) {
89 if (it.Value().Tag() > imax)
90 imax = it.Value().Tag();
93 TDF_Label NL = L.FindChild(imax);
95 SALOMEDSImpl_AttributeComment::Set(NL, DataType);
97 Handle(SALOMEDSImpl_SComponent) so = Handle(SALOMEDSImpl_Study)::DownCast(_study)->GetSComponent (NL);
99 if(!_callbackOnAdd.IsNull()) _callbackOnAdd->OnAddSObject(so);
106 //============================================================================
107 /*! Function : DefineComponentInstance
108 * Purpose : Add IOR attribute of a Scomponent
110 //============================================================================
111 bool SALOMEDSImpl_StudyBuilder::DefineComponentInstance(const Handle(SALOMEDSImpl_SComponent)& aComponent,
112 const TCollection_AsciiString& IOR)
117 if(aComponent.IsNull() || IOR.IsEmpty()) {
118 _errorCode = "Invalid arguments";
122 SALOMEDSImpl_AttributeIOR::Set(aComponent->GetLabel(), IOR);
129 //============================================================================
130 /*! Function : RemoveComponent
131 * Purpose : Delete a Scomponent
133 //============================================================================
134 bool SALOMEDSImpl_StudyBuilder::RemoveComponent(const Handle(SALOMEDSImpl_SComponent)& aComponent)
138 return RemoveObject(aComponent);
141 //============================================================================
142 /*! Function : NewObject
143 * Purpose : Create a new SObject
145 //============================================================================
146 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_StudyBuilder::NewObject(const Handle(SALOMEDSImpl_SObject)& theFatherObject)
151 //Find label of father
152 TDF_Label Lab = theFatherObject->GetLabel();
156 for (TDF_ChildIterator it(Lab); it.More(); it.Next()) {
157 if (it.Value().Tag() > imax)
158 imax = it.Value().Tag();
161 TDF_Label NewLab = Lab.FindChild(imax);
163 Handle(SALOMEDSImpl_SObject) so = Handle(SALOMEDSImpl_Study)::DownCast(_study)->GetSObject(NewLab);
164 if(!_callbackOnAdd.IsNull()) _callbackOnAdd->OnAddSObject(so);
170 //============================================================================
171 /*! Function : NewObjectToTag
174 //============================================================================
175 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_StudyBuilder::NewObjectToTag(const Handle(SALOMEDSImpl_SObject)& theFatherObject,
180 //Find label of father
181 TDF_Label Lab = theFatherObject->GetLabel();
183 //Create or find label
184 TDF_Label NewLab = Lab.FindChild(theTag, 1);
186 Handle(SALOMEDSImpl_SObject) so = Handle(SALOMEDSImpl_Study)::DownCast(_study)->GetSObject(NewLab);
188 if(!_callbackOnAdd.IsNull()) _callbackOnAdd->OnAddSObject(so);
194 //============================================================================
195 /*! Function : RemoveObject
198 //============================================================================
199 bool SALOMEDSImpl_StudyBuilder::RemoveObject(const Handle(SALOMEDSImpl_SObject)& anObject)
203 if(anObject.IsNull()) {
204 _errorCode = "Null object";
208 if(!_callbackOnRemove.IsNull()) _callbackOnRemove->OnRemoveSObject(anObject);
210 TDF_Label Lab = anObject->GetLabel();
212 Handle(SALOMEDSImpl_AttributeReference) aReference;
213 if (Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aReference)) {
214 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
215 if (aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(),aTarget))
216 aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
219 Handle(SALOMEDSImpl_AttributeIOR) anAttr; // postponed removing of CORBA objects
220 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
221 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(TCollection_AsciiString(anAttr->Value()).ToCString());
223 Lab.ForgetAllAttributes();
230 //============================================================================
231 /*! Function : RemoveObjectWithChildren
234 //============================================================================
235 bool SALOMEDSImpl_StudyBuilder::RemoveObjectWithChildren(const Handle(SALOMEDSImpl_SObject)& anObject)
239 if(anObject.IsNull()) {
240 _errorCode = "Null object";
244 if(!_callbackOnRemove.IsNull()) _callbackOnRemove->OnRemoveSObject(anObject);
246 TDF_Label Lab = anObject->GetLabel();
248 Handle(SALOMEDSImpl_AttributeReference) aReference;
249 if (Lab.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aReference)) {
250 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
251 if (aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(),aTarget))
252 aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
254 Handle(SALOMEDSImpl_AttributeIOR) anAttr; // postponed removing of CORBA objects
255 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
256 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(TCollection_AsciiString(anAttr->Value()).ToCString());
258 TDF_ChildIterator it(Lab, Standard_True);
259 for(;it.More();it.Next()) {
260 TDF_Label aLabel = it.Value();
261 if (aLabel.FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aReference)) {
262 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
263 if (aReference->Get().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(),aTarget))
264 aTarget->Remove(SALOMEDSImpl_Study::SObject(aLabel));
266 Handle(SALOMEDSImpl_AttributeIOR) anAttr; // postponed removing of CORBA objects
267 if (aLabel.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
268 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(TCollection_AsciiString(anAttr->Value()).ToCString());
271 Lab.ForgetAllAttributes(Standard_True);
278 //============================================================================
279 /*! Function : LoadWith
282 //============================================================================
283 bool SALOMEDSImpl_StudyBuilder::LoadWith(const Handle(SALOMEDSImpl_SComponent)& anSCO,
284 SALOMEDSImpl_Driver* aDriver)
288 TDF_Label Lab = anSCO->GetLabel();
289 Handle(SALOMEDSImpl_AttributePersistentRef) Att;
291 //Find the current Url of the study
292 if (_doc->Main().FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID(),Att)) {
293 int aLocked = anSCO->GetStudy()->GetProperties()->IsLocked();
294 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(false);
296 TCollection_ExtendedString Res(Att->Value());
298 Handle(SALOMEDSImpl_AttributeComment) type;
299 TCollection_ExtendedString DataType;
300 if (Lab.FindAttribute(SALOMEDSImpl_AttributeComment::GetID(),type))
301 DataType = type->Value();
303 // associate the driver to the SComponent
304 if(aDriver == NULL) {
305 _errorCode = "Driver is null";
309 // mpv 06.03.2003: SAL1927 - if component data if already loaded, it is not necessary to do it again
310 Handle(SALOMEDSImpl_AttributeIOR) attrIOR;
311 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), attrIOR)) {
312 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
316 DefineComponentInstance (anSCO, aDriver->GetIOR());
318 TCollection_AsciiString aHDFPath(Res);
320 TCollection_AsciiString aHDFUrl;
321 bool isASCII = false;
322 if (HDFascii::isASCII(aHDFPath.ToCString())) {
324 aHDFUrl = HDFascii::ConvertFromASCIIToHDF(aHDFPath.ToCString());
325 aHDFUrl += "hdf_from_ascii.hdf";
330 //Open the Study HDF file
331 HDFfile *hdf_file = new HDFfile(aHDFUrl.ToCString());
333 char aMultifileState[2];
334 char ASCIIfileState[2];
336 TCollection_AsciiString scoid = anSCO->GetID();
337 hdf_file->OpenOnDisk(HDF_RDONLY);
338 HDFgroup *hdf_group = new HDFgroup("DATACOMPONENT",hdf_file);
339 hdf_group->OpenOnDisk();
340 HDFgroup *hdf_sco_group = new HDFgroup(scoid.ToCString(), hdf_group);
341 hdf_sco_group->OpenOnDisk();
343 unsigned char* aStreamFile = NULL;
346 if (hdf_sco_group->ExistInternalObject("FILE_STREAM")) {
347 HDFdataset *hdf_dataset = new HDFdataset("FILE_STREAM", hdf_sco_group);
348 hdf_dataset->OpenOnDisk();
349 aStreamSize = hdf_dataset->GetSize();
350 aStreamFile = new unsigned char[aStreamSize];
351 if(aStreamFile == NULL) throw HDFexception("Unable to open dataset FILE_STREAM");
352 hdf_dataset->ReadFromDisk(aStreamFile);
353 hdf_dataset->CloseOnDisk();
358 HDFdataset *multifile_hdf_dataset = new HDFdataset("MULTIFILE_STATE", hdf_sco_group);
359 multifile_hdf_dataset->OpenOnDisk();
360 multifile_hdf_dataset->ReadFromDisk(aMultifileState);
362 HDFdataset *ascii_hdf_dataset = new HDFdataset("ASCII_STATE", hdf_sco_group);
363 ascii_hdf_dataset->OpenOnDisk();
364 ascii_hdf_dataset->ReadFromDisk(ASCIIfileState);
366 // set path without file name from URL
367 int aFileNameSize = Res.Length();
368 char* aDir = new char[aFileNameSize];
369 memcpy(aDir, TCollection_AsciiString(Res).ToCString(), aFileNameSize);
370 for(int aCounter = aFileNameSize-1; aCounter>=0; aCounter--)
371 if (aDir[aCounter] == '/') {
372 aDir[aCounter+1] = 0;
376 bool aResult = (ASCIIfileState[0]=='A')?
377 aDriver->LoadASCII(anSCO, aStreamFile, aStreamSize, aDir, aMultifileState[0]=='M'):
378 aDriver->Load(anSCO, aStreamFile, aStreamSize, aDir, aMultifileState[0]=='M');
380 if(aStreamFile != NULL) delete []aStreamFile;
383 RemoveAttribute( anSCO, "AttributeIOR" );
385 _errorCode = "Can't load component";
386 throw HDFexception("Unable to load component");
389 if(aDir != NULL) delete []aDir;
391 multifile_hdf_dataset->CloseOnDisk();
392 multifile_hdf_dataset = 0;
393 ascii_hdf_dataset->CloseOnDisk();
394 ascii_hdf_dataset = 0;
396 hdf_sco_group->CloseOnDisk();
398 hdf_group->CloseOnDisk();
400 hdf_file->CloseOnDisk();
404 Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
405 aFilesToRemove->Append(aHDFUrl);
406 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl),
407 aFilesToRemove, true);
410 catch (HDFexception) {
414 Handle(TColStd_HSequenceOfAsciiString) aFilesToRemove = new TColStd_HSequenceOfAsciiString;
415 aFilesToRemove->Append(aHDFUrl);
416 SALOMEDSImpl_Tool::RemoveTemporaryFiles(SALOMEDSImpl_Tool::GetDirFromPath(aHDFUrl), aFilesToRemove, true);
419 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
420 _errorCode = "No persistent file";
425 Translate_persistentID_to_IOR (Lab, aDriver, aMultifileState[0]=='M', ASCIIfileState[0] == 'A');
427 _errorCode = "Can not convert persistent IDs to IORs";
431 if (aLocked) anSCO->GetStudy()->GetProperties()->SetLocked(true);
433 _errorCode = "No persistent file";
440 //============================================================================
444 //============================================================================
445 bool SALOMEDSImpl_StudyBuilder::Load(const Handle(SALOMEDSImpl_SObject)& sco)
447 _errorCode = "Not implemented";
451 //============================================================================
452 /*! Function : FindOrCreateAttribute
453 * Purpose : Add attribute of given type to SObject, if there is attribute of such type, returns
456 //============================================================================
457 Handle(TDF_Attribute) SALOMEDSImpl_StudyBuilder::FindOrCreateAttribute(const Handle(SALOMEDSImpl_SObject)& anObject,
458 const TCollection_AsciiString& aTypeOfAttribute)
461 if(anObject.IsNull()) {
462 _errorCode = "Invalid arguments";
466 TDF_Label Lab = anObject->GetLabel();
468 _errorCode = "Null label";
474 //The macro adds all necessary checks for standardly behaiving attributes
475 __FindOrCreateAttributeForBuilder
477 //Add checks for TreeNode and UserID attributes
478 if (strncmp(aTypeOfAttribute.ToCString(), "AttributeTreeNode",17) == 0 ) {
479 Standard_GUID aTreeNodeGUID;
480 if (strcmp(aTypeOfAttribute.ToCString(), "AttributeTreeNode") == 0) {
481 aTreeNodeGUID = SALOMEDSImpl_AttributeTreeNode::GetDefaultTreeID();
483 char* aGUIDString = new char[40];
484 char* aType = (char*)aTypeOfAttribute.ToCString();
485 sprintf(aGUIDString, &(aType[21]));
486 aTreeNodeGUID = Standard_GUID(aGUIDString); // create tree node GUID by name
489 Handle(SALOMEDSImpl_AttributeTreeNode) anAttr;
490 if (!Lab.FindAttribute(aTreeNodeGUID, anAttr)) {
492 anAttr = SALOMEDSImpl_AttributeTreeNode::Set(Lab, aTreeNodeGUID);
497 if (strncmp(aTypeOfAttribute.ToCString(), "AttributeUserID",15) == 0 ) {
498 Handle(SALOMEDSImpl_AttributeUserID) anAttr;
499 if (!Lab.FindAttribute(SALOMEDSImpl_AttributeUserID::DefaultID(), anAttr)) {
501 anAttr = SALOMEDSImpl_AttributeUserID::Set(Lab, SALOMEDSImpl_AttributeUserID::DefaultID());
505 _errorCode = "Can not create an attribute";
510 //============================================================================
511 /*! Function : FindAttribute
512 * Purpose : Find attribute of given type assigned SObject, returns Standard_True if it is found
514 //============================================================================
516 bool SALOMEDSImpl_StudyBuilder::FindAttribute(const Handle(SALOMEDSImpl_SObject)& anObject,
517 Handle(TDF_Attribute)& anAttribute,
518 const TCollection_AsciiString& aTypeOfAttribute)
521 if(anObject.IsNull()) {
522 _errorCode = "Invalid arguments";
525 TDF_Label Lab = anObject->GetLabel();
526 if (Lab.FindAttribute(SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute), anAttribute)) {
528 return Standard_True;
530 return Standard_False;
533 //============================================================================
534 /*! Function : RemoveAttribute
535 * Purpose : Remove attribute of given type assigned SObject
537 //============================================================================
539 bool SALOMEDSImpl_StudyBuilder::RemoveAttribute(const Handle(SALOMEDSImpl_SObject)& anObject,
540 const TCollection_AsciiString& aTypeOfAttribute)
544 if(anObject.IsNull()) {
545 _errorCode = "Invalid arguments";
548 TDF_Label Lab = anObject->GetLabel();
550 if (aTypeOfAttribute == "AttributeIOR") { // postponed removing of CORBA objects
551 Handle(SALOMEDSImpl_AttributeIOR) anAttr;
552 if (Lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID(), anAttr))
553 SALOMEDSImpl_Study::GetStudy(_doc->Main())->AddPostponed(anAttr->Value());
556 Lab.ForgetAttribute (SALOMEDSImpl_SObject::GetGUID(aTypeOfAttribute));
563 //============================================================================
564 /*! Function : Addreference
567 //============================================================================
568 bool SALOMEDSImpl_StudyBuilder::Addreference(const Handle(SALOMEDSImpl_SObject)& me,
569 const Handle(SALOMEDSImpl_SObject)& theReferencedObject)
572 if(me.IsNull() || theReferencedObject.IsNull()) {
573 _errorCode = "Invalid arguments";
577 TDF_Label Lab = me->GetLabel();
578 TDF_Label RefLab = theReferencedObject->GetLabel();
579 SALOMEDSImpl_AttributeReference::Set(Lab,RefLab);
581 SALOMEDSImpl_AttributeTarget::Set(RefLab)->Add(SALOMEDSImpl_Study::SObject(Lab));
583 if(!_callbackOnRemove.IsNull() && Lab.IsDescendant(_doc->Main())) _callbackOnRemove->OnRemoveSObject(me);
588 //============================================================================
589 /*! Function : RemoveReference
592 //============================================================================
593 bool SALOMEDSImpl_StudyBuilder::RemoveReference(const Handle(SALOMEDSImpl_SObject)& me)
596 Handle(SALOMEDSImpl_SObject) theReferencedObject;
597 if(!me->ReferencedObject(theReferencedObject)) return false; //No reference is found
600 TDF_Label Lab = me->GetLabel();
602 Lab.ForgetAttribute(SALOMEDSImpl_AttributeReference::GetID());
604 //SRN: 30 Aug, 2004 : fix from Ecole l'ete version
606 TDF_Label RefLab = theReferencedObject->GetLabel();
608 Handle(SALOMEDSImpl_AttributeTarget) aTarget;
609 if(RefLab.FindAttribute(SALOMEDSImpl_AttributeTarget::GetID(), aTarget))
610 aTarget->Remove(SALOMEDSImpl_Study::SObject(Lab));
619 //============================================================================
620 /*! Function : AddDirectory
621 * Purpose : adds a new directory with a path = thePath
623 //============================================================================
624 bool SALOMEDSImpl_StudyBuilder::AddDirectory(const TCollection_AsciiString& thePath)
628 if(thePath.IsEmpty() || thePath == "") {
629 _errorCode = "Invalid path";
633 TCollection_AsciiString aPath(thePath), aContext(""), aFatherPath;
635 Handle(SALOMEDSImpl_Study) aStudy = SALOMEDSImpl_Study::GetStudy(_doc->Main());
636 Handle(SALOMEDSImpl_SObject) anObject;
639 anObject = aStudy->FindObjectByPath(thePath); //Check if the directory already exists
643 if(!anObject.IsNull()) {
644 _errorCode = "StudyNameAlreadyUsed";
648 if(aPath.Value(1) != '/') { //Relative path
650 aPath = aStudy->GetContext() + aPath;
653 TCollection_AsciiString aToken = aPath.Token("/", 1);
654 if(aToken.Length() == 0) aFatherPath = "/";
657 while(aToken.Length() != 0) {
658 if(aPath.Token("/", i+1).Length() > 0) {
660 aFatherPath += aToken;
662 aToken = aPath.Token("/", ++i);
667 anObject = aStudy->FindObjectByPath(aFatherPath); //Check if the father directory exists
670 if(anObject.IsNull()) {
671 _errorCode = "StudyInvalidDirectory";
675 Handle(SALOMEDSImpl_SObject) aNewObject = NewObject(anObject);
676 aLabel = aNewObject->GetLabel();
677 if(aLabel.IsNull()) {
678 _errorCode = "StudyInvalidComponent";
682 SALOMEDSImpl_AttributeName::Set(aLabel, aPath.Token("/", i-1));
684 //Set LocalID attribute to identify the directory object
685 Handle(SALOMEDSImpl_AttributeLocalID) aLocalID = SALOMEDSImpl_AttributeLocalID::Set(aLabel, DIRECTORYID);
693 //============================================================================
694 /*! Function : SetGUID
697 //============================================================================
698 bool SALOMEDSImpl_StudyBuilder::SetGUID(const Handle(SALOMEDSImpl_SObject)& anObject,
699 const TCollection_AsciiString& theGUID)
703 if(anObject.IsNull()) {
704 _errorCode = "Invalid arguments";
708 TDF_Label aLabel = anObject->GetLabel();
709 SALOMEDSImpl_AttributeUserID::Set(aLabel, theGUID.ToCString());
716 //============================================================================
717 /*! Function : IsGUID
720 //============================================================================
721 bool SALOMEDSImpl_StudyBuilder::IsGUID(const Handle(SALOMEDSImpl_SObject)& anObject,
722 const TCollection_AsciiString& theGUID)
725 if(anObject.IsNull()) {
726 _errorCode = "Invalid arguments";
729 TDF_Label aLabel = anObject->GetLabel();
730 return aLabel.IsAttribute(theGUID.ToCString());
734 //============================================================================
735 /*! Function : NewCommand
738 //============================================================================
739 void SALOMEDSImpl_StudyBuilder::NewCommand()
742 // mpv: for SAL2114 - unset "lock changed" flag at the operation start
743 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
744 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
745 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
746 _doc->Main().AddAttribute(anAttr);
748 anAttr->IsLockChanged(true);
753 //============================================================================
754 /*! Function : CommitCommand
757 //============================================================================
758 void SALOMEDSImpl_StudyBuilder::CommitCommand()
761 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
762 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
763 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
764 _doc->Main().AddAttribute(anAttr);
766 if (anAttr->IsLocked() && !anAttr->IsLockChanged(true)) {
768 _errorCode = "LockProtection";
769 throw LockProtection("LockProtection");
771 SALOMEDSImpl_Study::GetStudy(_doc->Main())->RemovePostponed(_doc->GetUndoLimit());
773 int aModif = anAttr->GetModified();
774 if (aModif < 0) aModif = 1000; // if user make undo and then - new transaction "modify" will never be zero
775 anAttr->SetModified(aModif+1);
776 _doc->CommitCommand();
782 //============================================================================
783 /*! Function : HasOpenCommand
786 //============================================================================
787 bool SALOMEDSImpl_StudyBuilder::HasOpenCommand()
790 return _doc->HasOpenCommand();
793 //============================================================================
794 /*! Function : AbortCommand
797 //============================================================================
798 void SALOMEDSImpl_StudyBuilder::AbortCommand()
801 SALOMEDSImpl_Study::GetStudy(_doc->Main())->UndoPostponed(0);
803 _doc->AbortCommand();
806 //============================================================================
810 //============================================================================
811 void SALOMEDSImpl_StudyBuilder::Undo()
814 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
815 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
816 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
817 _doc->Main().AddAttribute(anAttr);
819 if (anAttr->IsLocked()) {
820 _errorCode = "LockProtection";
821 throw LockProtection("LockProtection");
823 SALOMEDSImpl_Study::GetStudy(_doc->Main())->UndoPostponed(1);
825 anAttr->SetModified(anAttr->GetModified()-1);
831 //============================================================================
835 //============================================================================
836 void SALOMEDSImpl_StudyBuilder::Redo()
839 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
840 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
841 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
842 _doc->Main().AddAttribute(anAttr);
845 if (anAttr->IsLocked()) {
846 _errorCode = "LockProtection";
847 throw LockProtection("LockProtection");
850 SALOMEDSImpl_Study::GetStudy(_doc->Main())->UndoPostponed(-1);
851 anAttr->SetModified(anAttr->GetModified()+1);
857 //============================================================================
858 /*! Function : GetAvailableUndos
861 //============================================================================
862 bool SALOMEDSImpl_StudyBuilder::GetAvailableUndos()
865 return _doc->GetAvailableUndos();
868 //============================================================================
869 /*! Function : GetAvailableRedos
872 //============================================================================
873 bool SALOMEDSImpl_StudyBuilder::GetAvailableRedos()
876 return _doc->GetAvailableRedos();
879 //============================================================================
880 /*! Function : UndoLimit
883 //============================================================================
884 int SALOMEDSImpl_StudyBuilder::UndoLimit()
887 return _doc->GetUndoLimit();
890 //============================================================================
891 /*! Function : UndoLimit
894 //============================================================================
895 void SALOMEDSImpl_StudyBuilder::UndoLimit(int n)
899 _doc->SetUndoLimit (n);
902 //============================================================================
903 /*! Function : SetOnAddSObject
906 //============================================================================
907 Handle(SALOMEDSImpl_Callback)
908 SALOMEDSImpl_StudyBuilder::SetOnAddSObject(const Handle(SALOMEDSImpl_Callback)& theCallback)
911 Handle(SALOMEDSImpl_Callback) aRet = _callbackOnAdd;
912 _callbackOnAdd = theCallback;
916 //============================================================================
917 /*! Function : SetOnNewSObject
920 //============================================================================
921 Handle(SALOMEDSImpl_Callback)
922 SALOMEDSImpl_StudyBuilder::SetOnRemoveSObject(const Handle(SALOMEDSImpl_Callback)& theCallback)
925 Handle(SALOMEDSImpl_Callback) aRet = _callbackOnRemove;
926 _callbackOnRemove = theCallback;
930 //============================================================================
931 /*! Function : CheckLocked
934 //============================================================================
935 void SALOMEDSImpl_StudyBuilder::CheckLocked()
938 if (_doc->HasOpenCommand()) return;
939 Handle(SALOMEDSImpl_AttributeStudyProperties) anAttr;
940 if (!_doc->Main().FindAttribute(SALOMEDSImpl_AttributeStudyProperties::GetID(), anAttr)) {
941 anAttr = new SALOMEDSImpl_AttributeStudyProperties;
942 _doc->Main().AddAttribute(anAttr);
944 if (anAttr->IsLocked()) {
945 _errorCode = "LockProtection";
946 throw LockProtection("LockProtection");
950 //============================================================================
951 /*! Function : SetName
954 //============================================================================
955 bool SALOMEDSImpl_StudyBuilder::SetName(const Handle(SALOMEDSImpl_SObject)& theSO,
956 const TCollection_AsciiString& theValue)
961 _errorCode = "Invalid arguments";
964 SALOMEDSImpl_AttributeName::Set(theSO->GetLabel(), theValue);
971 //============================================================================
972 /*! Function : SetComment
975 //============================================================================
976 bool SALOMEDSImpl_StudyBuilder::SetComment(const Handle(SALOMEDSImpl_SObject)& theSO,
977 const TCollection_AsciiString& theValue)
982 _errorCode = "Invalid arguments";
985 SALOMEDSImpl_AttributeComment::Set(theSO->GetLabel(), theValue);
992 //============================================================================
993 /*! Function : SetIOR
996 //============================================================================
997 bool SALOMEDSImpl_StudyBuilder::SetIOR(const Handle(SALOMEDSImpl_SObject)& theSO,
998 const TCollection_AsciiString& theValue)
1002 if(theSO.IsNull()) {
1003 _errorCode = "Invalid arguments";
1006 SALOMEDSImpl_AttributeIOR::Set(theSO->GetLabel(), theValue);
1014 //============================================================================
1015 /*! Function : Translate_persistentID_to_IOR
1018 //============================================================================
1019 static void Translate_persistentID_to_IOR(TDF_Label& Lab, SALOMEDSImpl_Driver* driver, bool isMultiFile, bool isASCII)
1021 if(driver == NULL) return;
1022 TDF_ChildIterator itchild (Lab);
1024 for (; itchild.More(); itchild.Next()) {
1025 TDF_Label current = itchild.Value();
1026 Handle(SALOMEDSImpl_AttributePersistentRef) Att;
1027 if (current.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID(),Att)) {
1029 Handle(SALOMEDSImpl_AttributeLocalID) anID;
1030 if (current.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID(), anID))
1031 if (anID->Value() == FILELOCALID) continue; //SRN: This attribute store a file name, skip it
1033 TCollection_AsciiString persist_ref(Att->Value());
1034 Handle(SALOMEDSImpl_SObject) so = SALOMEDSImpl_Study::SObject(current);
1035 TCollection_AsciiString ior_string = driver->LocalPersistentIDToIOR(so,
1039 SALOMEDSImpl_AttributeIOR::Set (current, ior_string);
1042 Translate_persistentID_to_IOR (current, driver, isMultiFile, isASCII);