1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SALOMEDSImpl_Study.cxx
23 // Author : Sergey RUIN
26 #include "SALOMEDSImpl_Study.hxx"
31 #include <Basics_Utils.hxx>
33 #include "DF_Application.hxx"
34 #include "DF_ChildIterator.hxx"
36 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
37 #include "SALOMEDSImpl_Attributes.hxx"
38 #include "SALOMEDSImpl_UseCaseIterator.hxx"
39 #include "SALOMEDSImpl_AttributeReference.hxx"
40 #include "SALOMEDSImpl_StudyHandle.hxx"
41 #include "SALOMEDSImpl_Tool.hxx"
42 #include "SALOMEDSImpl_IParameters.hxx"
46 #define DIRECTORYID 16661
47 #define FILELOCALID 26662
48 #define FILEID "FILE: "
50 //============================================================================
51 /*! Function : SALOMEDSImpl_Study
52 * Purpose : SALOMEDSImpl_Study constructor
54 //============================================================================
55 SALOMEDSImpl_Study::SALOMEDSImpl_Study(const DF_Document* doc,
56 const string& study_name)
59 _doc = (DF_Document*)doc;
65 _useCaseBuilder = new SALOMEDSImpl_UseCaseBuilder(_doc);
66 _builder = new SALOMEDSImpl_StudyBuilder(this);
67 _cb = new SALOMEDSImpl_Callback(_useCaseBuilder);
68 //Put on the root label a StudyHandle attribute to store the address of this object
69 //It will be used to retrieve the study object by DF_Label that belongs to the study
70 SALOMEDSImpl_StudyHandle::Set(_doc->Main().Root(), this);
74 //============================================================================
75 /*! Function : ~SALOMEDSImpl_Study
76 * Purpose : SALOMEDSImpl_Study destructor
78 //============================================================================
79 SALOMEDSImpl_Study::~SALOMEDSImpl_Study()
83 delete _useCaseBuilder;
86 //============================================================================
87 /*! Function : GetPersistentReference
88 * Purpose : Get persistent reference of study (idem URL())
90 //============================================================================
91 string SALOMEDSImpl_Study::GetPersistentReference()
96 //============================================================================
97 /*! Function : GetTransientReference
98 * Purpose : Get IOR of the Study (registred in Document in doc->Root)
100 //============================================================================
101 string SALOMEDSImpl_Study::GetTransientReference()
106 SALOMEDSImpl_AttributeIOR* Att;
107 DF_Label _lab = _doc->Root();
108 if ((Att=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
112 _errorCode = "IOR is empty";
118 void SALOMEDSImpl_Study::SetTransientReference(const string& theIOR)
122 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
123 int aLocked = aProp->IsLocked();
124 if (aLocked) aProp->SetLocked(false);
126 // Assign the value of the IOR in the study->root
127 SALOMEDSImpl_AttributeIOR::Set(_doc->Main().Root(), theIOR);
129 if (aLocked) aProp->SetLocked(true);
132 //============================================================================
133 /*! Function : IsEmpty
134 * Purpose : Detect if study is empty
136 //============================================================================
137 bool SALOMEDSImpl_Study::IsEmpty()
140 if (!_doc) return true;
141 return _doc->IsEmpty();
144 //============================================================================
145 /*! Function : FindComponent
146 * Purpose : Find a Component with ComponentDataType = aComponentName
148 //============================================================================
149 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const string& aComponentName)
154 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
155 SALOMEDSImpl_SComponent compo;
157 for (; itcomp.More(); itcomp.Next()) {
158 SALOMEDSImpl_SComponent SC = itcomp.Value();
159 name = SC.ComponentDataType();
160 if(aComponentName == name) {
168 _errorCode = "No component was found";
174 //============================================================================
175 /*! Function : FindComponentID
176 * Purpose : Find a Component from it's ID
178 //============================================================================
179 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const string& aComponentID)
183 // Iterate on each components defined in the study
184 // Get the component ID and compare with aComponentID
187 SALOMEDSImpl_SComponent compo;
189 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
190 for (; itcomp.More(); itcomp.Next()) {
191 SALOMEDSImpl_SComponent SC = itcomp.Value();
193 if(aComponentID == ID)
202 _errorCode = "No component was found";
209 //============================================================================
210 /*! Function : FindObject
211 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
213 //============================================================================
214 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const string& anObjectName)
218 // Iterate to all components defined in the study
219 // After testing the component name, iterate in all objects defined under
220 // components (function _FindObject)
222 SALOMEDSImpl_SObject RefSO;
224 SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
225 for (; it.More();it.Next()){
228 SALOMEDSImpl_SComponent SC = it.Value();
229 if (SC.GetName() == anObjectName)
235 if (!_find) RefSO = _FindObject(SC, anObjectName, _find);
238 if(!RefSO) _errorCode = "No object was found";
242 //============================================================================
243 /*! Function : FindObjectID
244 * Purpose : Find an Object with ID = anObjectID
246 //============================================================================
247 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const string& anObjectID)
250 SALOMEDSImpl_SObject so;
252 // Convert aSO->GetID in DF_Label.
253 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
256 _errorCode = "No label was found by ID";
259 return GetSObject(Lab);
263 //============================================================================
264 /*! Function : CreateObjectID
265 * Purpose : Creates an Object with ID = anObjectID
267 //============================================================================
268 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const string& anObjectID)
271 SALOMEDSImpl_SObject so;
273 // Convert aSO->GetID in DF_Label.
274 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
277 _errorCode = "Can not create a label";
280 return GetSObject(Lab);
284 //============================================================================
285 /*! Function : FindObjectByName
286 * Purpose : Find Objects with SALOMEDSImpl_Name = anObjectName in a Component
287 * : with ComponentDataType = aComponentName
289 //============================================================================
290 vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const string& anObjectName,
291 const string& aComponentName)
295 vector<SALOMEDSImpl_SObject> listSO;
297 SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
299 _errorCode = "Can not find the component";
303 // Iterate on each object and subobject of the component
304 // If objectName is found add it to the list of SObjects
307 string compoId = compo.GetID();
308 SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
309 for ( ; it.More(); it.Next() ) {
311 SALOMEDSImpl_SObject CSO = it.Value();
312 if ( CSO.GetName() == anObjectName ) {
314 listSO.push_back(CSO) ;
317 /* looks also for eventual children */
319 CSO = _FindObject( CSO, anObjectName, found ) ;
321 listSO.push_back(CSO) ;
330 //============================================================================
331 /*! Function : FindObjectIOR
332 * Purpose : Find an Object with IOR = anObjectIOR
334 //============================================================================
335 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const string& anObjectIOR)
339 SALOMEDSImpl_SObject aResult ;
341 // searching in the datamap for optimization
342 if (myIORLabels.find(anObjectIOR) != myIORLabels.end()) {
343 aResult = GetSObject(myIORLabels[anObjectIOR]);
344 // 11 oct 2002: forbidden attributes must be checked here
345 if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
346 myIORLabels.erase(anObjectIOR);
347 aResult = SALOMEDSImpl_SObject();
351 if(!aResult) _errorCode = "No object was found";
355 //============================================================================
356 /*! Function : FindObjectByPath
357 * Purpose : Find an Object by its path = thePath
359 //============================================================================
360 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const string& thePath)
364 string aPath(thePath), aToken;
365 SALOMEDSImpl_SObject aSO;
366 int aLength = aPath.size();
367 bool isRelative = false;
369 if(aLength == 0) { //Empty path - return the current context
370 return GetSObject(_current);
373 if(aPath[0] != '/') //Relative path
376 DF_ChildIterator anIterator;
378 SALOMEDSImpl_AttributeName* anAttr;
381 if(_current.IsNull()) return aSO;
382 anIterator.Init(_current, false);
385 if(aPath.size() == 1 && aPath[0] == '/') { //Root
386 return GetSObject(_doc->Main());
388 anIterator.Init(_doc->Main(), false);
391 vector<string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
392 for(int i = 0, len = vs.size(); i<len; i++) {
395 if(aToken.size() == 0) break;
397 for ( ; anIterator.More(); anIterator.Next() ) {
398 aLabel = anIterator.Value();
399 if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
400 if(anAttr->Value() == aToken) {
401 if(i == (len-1)) { //The searched label is found (no part of the path is left)
402 return GetSObject(aLabel);
405 anIterator.Init(aLabel, false);
413 if(!aSO) _errorCode = "No object was found";
417 //============================================================================
418 /*! Function : GetObjectPath
421 //============================================================================
422 string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
428 _errorCode = "Null object";
432 string aName = theObject.GetName();
433 if(!aName.empty() && aName != "" ) {
438 SALOMEDSImpl_SObject aFather = theObject.GetFather();
440 aName = aFather.GetName();
441 if(!aName.empty() && aName != "") {
442 aValue = GetObjectPath(aFather);
443 aPath = aValue + aPath;
452 //============================================================================
453 /*! Function : GetObjectPathByIOR
456 //============================================================================
457 string SALOMEDSImpl_Study::GetObjectPathByIOR(const string& theIOR)
462 SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
464 _errorCode = "No SObject was found by IOR";
468 return GetObjectPath(so);
472 //============================================================================
473 /*! Function : SetContext
474 * Purpose : Sets the current context
476 //============================================================================
477 bool SALOMEDSImpl_Study::SetContext(const string& thePath)
480 if(thePath.empty()) {
481 _errorCode = "InvalidPath";
485 string aPath(thePath), aContext("");
486 bool isInvalid = false;
487 SALOMEDSImpl_SObject aSO;
489 if(aPath[0] != '/') { //Relative path
490 aContext = GetContext();
498 aSO = FindObjectByPath(aContext);
504 if(isInvalid || !aSO) {
505 _errorCode = "InvalidContext";
509 DF_Label aLabel = aSO.GetLabel();
510 if(aLabel.IsNull()) {
511 _errorCode = "InvalidContext";
515 _current = aLabel; //Set the current context
520 //============================================================================
521 /*! Function : GetContext
522 * Purpose : Gets the current context
524 //============================================================================
525 string SALOMEDSImpl_Study::GetContext()
529 if(_current.IsNull()) {
530 _errorCode = "InvaidContext";
533 SALOMEDSImpl_SObject so = GetSObject(_current);
534 return GetObjectPath(so);
537 //============================================================================
538 /*! Function : GetObjectNames
539 * Purpose : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
541 //============================================================================
542 vector<string> SALOMEDSImpl_Study::GetObjectNames(const string& theContext)
546 vector<string> aResultSeq;
548 if (theContext.empty()) {
551 DF_Label aTmp = _current;
552 SetContext(theContext);
556 if (aLabel.IsNull()) {
557 _errorCode = "InvalidContext";
561 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
562 for (; anIter.More(); anIter.Next()) {
563 DF_Label aLabel = anIter.Value();
564 SALOMEDSImpl_AttributeName* aName;
565 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
566 aResultSeq.push_back(aName->Value());
572 //============================================================================
573 /*! Function : GetDirectoryNames
574 * Purpose : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
576 //============================================================================
577 vector<string> SALOMEDSImpl_Study::GetDirectoryNames(const string& theContext)
581 vector<string> aResultSeq;
583 if (theContext.empty()) {
586 DF_Label aTmp = _current;
587 SetContext(theContext);
591 if (aLabel.IsNull()) {
592 _errorCode = "InvalidContext";
596 DF_ChildIterator anIter (aLabel, true); // iterate first-level children at all sublevels
597 for (; anIter.More(); anIter.Next()) {
598 DF_Label aLabel = anIter.Value();
599 SALOMEDSImpl_AttributeLocalID* anID;
600 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
601 if (anID->Value() == DIRECTORYID) {
602 SALOMEDSImpl_AttributeName* aName;
603 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
604 aResultSeq.push_back(aName->Value());
613 //============================================================================
614 /*! Function : GetFileNames
615 * Purpose : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
617 //============================================================================
618 vector<string> SALOMEDSImpl_Study::GetFileNames(const string& theContext)
622 vector<string> aResultSeq;
624 if (theContext.empty()) {
627 DF_Label aTmp = _current;
628 SetContext(theContext);
632 if (aLabel.IsNull()) {
633 _errorCode = "InvalidContext";
637 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
638 for (; anIter.More(); anIter.Next()) {
639 DF_Label aLabel = anIter.Value();
640 SALOMEDSImpl_AttributeLocalID* anID;
641 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
642 if (anID->Value() == FILELOCALID) {
643 SALOMEDSImpl_AttributePersistentRef* aName;
644 if ((aName=(SALOMEDSImpl_AttributePersistentRef*)aLabel.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
645 std::string aFileName = aName->Value();
646 if (aFileName.size() > 0)
647 aResultSeq.push_back(aFileName.substr(strlen(FILEID), aFileName.size()));
656 //============================================================================
657 /*! Function : GetComponentNames
658 * Purpose : method to get all components names
660 //============================================================================
661 vector<string> SALOMEDSImpl_Study::GetComponentNames(const string& theContext)
665 vector<string> aResultSeq;
666 DF_ChildIterator anIter(_doc->Main(), false); // iterate all subchildren at first level
667 for(; anIter.More(); anIter.Next()) {
668 DF_Label aLabel = anIter.Value();
669 SALOMEDSImpl_AttributeName* aName;
670 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
671 aResultSeq.push_back(aName->Value());
677 //============================================================================
678 /*! Function : NewChildIterator
679 * Purpose : Create a ChildIterator from an SObject
681 //============================================================================
682 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
685 return SALOMEDSImpl_ChildIterator(aSO);
689 //============================================================================
690 /*! Function : NewComponentIterator
691 * Purpose : Create a SComponentIterator
693 //============================================================================
694 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
697 return SALOMEDSImpl_SComponentIterator(_doc);
701 //============================================================================
702 /*! Function : NewBuilder
703 * Purpose : Create a StudyBuilder
705 //============================================================================
706 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
710 _builder->SetOnAddSObject(_cb);
711 _builder->SetOnRemoveSObject(_cb);
717 //============================================================================
719 * Purpose : get study name
721 //============================================================================
722 string SALOMEDSImpl_Study::Name()
728 //============================================================================
730 * Purpose : set study name
732 //============================================================================
733 void SALOMEDSImpl_Study::Name(const string& name)
739 //============================================================================
740 /*! Function : IsSaved
741 * Purpose : get if study has been saved
743 //============================================================================
744 bool SALOMEDSImpl_Study::IsSaved()
750 //============================================================================
751 /*! Function : IsSaved
752 * Purpose : set if study has been saved
754 //============================================================================
755 void SALOMEDSImpl_Study::IsSaved(bool save)
759 if(save) _doc->SetModified(false);
762 //============================================================================
763 /*! Function : IsModified
764 * Purpose : Detect if a Study has been modified since it has been saved
766 //============================================================================
767 bool SALOMEDSImpl_Study::IsModified()
771 // True if is modified
772 if (_doc && _doc->IsModified()) return true;
777 //============================================================================
779 * Purpose : get URL of the study (persistent reference of the study)
781 //============================================================================
782 string SALOMEDSImpl_Study::URL()
788 //============================================================================
790 * Purpose : set URL of the study (persistent reference of the study)
792 //============================================================================
793 void SALOMEDSImpl_Study::URL(const string& url)
798 /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
801 char *aName = (char*)tmp.ToCString();
802 char *adr = strtok(aName, "/");
806 adr = strtok(NULL, "/");
813 //============================================================================
814 /*! Function : _FindObject
815 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
817 //============================================================================
818 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
819 const string& theObjectName,
822 SALOMEDSImpl_SObject RefSO;
823 if(!SO) return RefSO;
825 // Iterate on each objects and subobjects of the component
826 // If objectName find, stop the loop and get the object reference
827 SALOMEDSImpl_AttributeName* anAttr;
829 string soid = SO.GetID();
830 DF_ChildIterator it(SO.GetLabel());
831 for (; it.More(); it.Next()){
834 if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
836 string Val(anAttr->Value());
837 if (Val == theObjectName)
839 RefSO = GetSObject(it.Value());
843 if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
849 //============================================================================
850 /*! Function : _FindObjectIOR
851 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
853 //============================================================================
855 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
856 const string& theObjectIOR,
859 SALOMEDSImpl_SObject RefSO, aSO;
860 if(!SO) return RefSO;
862 // Iterate on each objects and subobjects of the component
863 // If objectName find, stop the loop and get the object reference
864 SALOMEDSImpl_AttributeIOR* anAttr;
866 DF_ChildIterator it(SO.GetLabel());
867 for (; it.More();it.Next()){
870 if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
872 string Val(anAttr->Value());
873 if (Val == theObjectIOR)
875 RefSO = GetSObject(it.Value());
879 aSO = GetSObject(it.Value());
880 if (!_find) RefSO = _FindObjectIOR(aSO, theObjectIOR, _find);
886 bool SALOMEDSImpl_Study::IsLocked()
889 return GetProperties()->IsLocked();
892 int SALOMEDSImpl_Study::StudyId()
898 void SALOMEDSImpl_Study::StudyId(int id)
904 void SALOMEDSImpl_Study::UpdateIORLabelMap(const string& anIOR,const string& anEntry)
907 DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
908 if (myIORLabels.find(anIOR) != myIORLabels.end()) myIORLabels.erase(anIOR);
909 myIORLabels[anIOR] = aLabel;
912 void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR)
914 if (myIORLabels.find(anIOR) != myIORLabels.end())
916 //remove the ior entry and decref the genericobj (if it's one)
917 myIORLabels.erase(anIOR);
921 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel)
923 SALOMEDSImpl_StudyHandle* Att;
924 if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
930 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
932 return GetStudy(theLabel)->GetSObject(theLabel);
935 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
937 return GetStudy(theLabel)->GetSComponent(theLabel);
941 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
943 string aString = theAttribute->Label().Entry();
944 GetStudy(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
947 vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
950 vector<SALOMEDSImpl_SObject> aSeq;
952 SALOMEDSImpl_AttributeTarget* aTarget;
953 if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
954 return aTarget->Get();
961 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
964 return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
967 string SALOMEDSImpl_Study::GetLastModificationDate()
970 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
972 vector<string> aNames;
973 vector<int> aMinutes, aHours, aDays, aMonths, aYears;
974 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
976 int aLastIndex = aNames.size()-1;
978 sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
979 (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
980 (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
981 string aResStr (aResult);
985 vector<string> SALOMEDSImpl_Study::GetModificationsDate()
988 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
990 vector<string> aNames;
991 vector<int> aMinutes, aHours, aDays, aMonths, aYears;
992 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
994 int anIndex, aLength = aNames.size();
995 vector<string> aDates;
997 for (anIndex = 1; anIndex < aLength; anIndex++) {
999 sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
1000 (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
1001 (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
1002 aDates.push_back(aDate);
1009 //============================================================================
1010 /*! Function : GetUseCaseBuilder
1011 * Purpose : Returns a UseCase builder
1013 //============================================================================
1014 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
1017 return _useCaseBuilder;
1021 //============================================================================
1022 /*! Function : Close
1025 //============================================================================
1026 void SALOMEDSImpl_Study::Close()
1029 _doc->GetApplication()->Close(_doc);
1036 //============================================================================
1037 /*! Function : GetSComponent
1040 //============================================================================
1041 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const string& theEntry)
1043 SALOMEDSImpl_SComponent aSCO;
1044 if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
1045 aSCO = _mapOfSCO[theEntry];
1047 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1048 aSCO = SALOMEDSImpl_SComponent(aLabel);
1049 _mapOfSCO[theEntry] = aSCO;
1055 //============================================================================
1056 /*! Function : GetSComponent
1059 //============================================================================
1060 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
1062 return SALOMEDSImpl_SComponent(theLabel);
1065 //============================================================================
1066 /*! Function : GetSObject
1069 //============================================================================
1070 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const string& theEntry)
1072 SALOMEDSImpl_SObject aSO;
1073 if(_mapOfSO.find(theEntry) != _mapOfSO.end())
1074 aSO = _mapOfSO[theEntry];
1076 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1077 aSO = SALOMEDSImpl_SObject(aLabel);
1078 _mapOfSO[theEntry] = aSO;
1084 //============================================================================
1085 /*! Function : GetSObject
1088 //============================================================================
1089 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
1091 return SALOMEDSImpl_SObject(theLabel);
1094 //============================================================================
1095 /*! Function : GetAttribute
1098 //============================================================================
1099 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const string& theEntry,
1100 const string& theType)
1102 SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
1103 DF_Attribute* anAttr;
1104 aSO.FindAttribute(anAttr, theType);
1108 //============================================================================
1109 /*! Function : DumpStudy
1112 //============================================================================
1113 bool SALOMEDSImpl_Study::DumpStudy(const string& thePath,
1114 const string& theBaseName,
1116 SALOMEDSImpl_DriverFactory* theFactory)
1120 if(theFactory == NULL) {
1121 _errorCode = "Null factory for creation of Engines";
1125 vector<string> aSeq;
1126 string aCompType, aFactoryType;
1128 //Build a list of all components in the Study
1129 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
1131 for (; itcomponent.More(); itcomponent.Next()) {
1132 SALOMEDSImpl_SComponent sco = itcomponent.Value();
1133 aCompType = sco.ComponentDataType();
1134 //GEOM and MED are independent components
1135 if (aCompType == "GEOM" || aCompType == "MED")
1136 aSeq.insert(aSeq.begin(), aCompType);
1138 aSeq.push_back(aCompType);
1143 thePath + string("\\") + theBaseName + string(".py");
1146 thePath + string("/") + theBaseName + string(".py");
1149 //Create a file that will contain a main Study script
1151 fp.open(aFileName.c_str(), ios::out);
1154 bool isOpened = fp.is_open();
1156 bool isOpened = fp.rdbuf()->is_open();
1160 _errorCode = string("Can't create a file ")+aFileName;
1164 string aBatchModeScript = "salome";
1166 //Output to the main Study script required Python modules import,
1167 //set sys.path and add a creation of the study.
1168 fp << "# -*- coding: iso-8859-1 -*-\n" << endl;
1170 fp << GetDumpStudyComment() << endl << endl;
1171 fp << "import sys" << endl;
1172 fp << "import " << aBatchModeScript << endl << endl;
1174 fp << aBatchModeScript << ".salome_init()" << endl << endl;
1176 //ASL: fp << _GetNoteBookAccess();
1178 fp << "sys.path.insert( 0, r\'" << thePath << "\')" << endl << endl;
1180 //Dump NoteBook Variables
1181 //ASL: fp << _GetStudyVariablesScript();
1183 //Check if it's necessary to dump visual parameters
1184 bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
1185 int lastSavePoint = -1;
1187 lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
1188 if(lastSavePoint > 0) {
1189 fp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << endl << endl;
1193 vector<string> aSeqOfFileNames;
1195 //Iterate all components and create the componponents specific scripts.
1197 int aLength = aSeq.size();
1198 for(int i = 1; i <= aLength; i++) {
1200 aCompType = aSeq[i-1];
1201 SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
1202 SALOMEDSImpl_Driver* aDriver = NULL;
1203 // if there is an associated Engine call its method for saving
1206 if (!sco.ComponentIOR(IOREngine)) {
1207 if (!aCompType.empty()) {
1209 aDriver = theFactory->GetDriverByType(aCompType);
1211 if (aDriver != NULL) {
1212 SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
1213 if(!SB->LoadWith(sco, aDriver)) {
1214 _errorCode = SB->GetErrorCode();
1222 aDriver = theFactory->GetDriverByIOR(IOREngine);
1225 _errorCode = "Can not restore information to dump it";
1229 if(aDriver == NULL) continue;
1232 long aStreamLength = 0;
1233 SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(this, isPublished, isValidScript, aStreamLength);
1234 if ( !isValidScript )
1237 //Create a file that will contain the component specific script
1240 aFileName=thePath+string("\\");
1242 aFileName=thePath+string("/");
1245 aScriptName += theBaseName;
1247 aScriptName += aCompType;
1249 aFileName += aScriptName+ string(".py");
1250 aSeqOfFileNames.push_back(aFileName);
1252 fp2.open(aFileName.c_str(), ios::out);
1255 isOpened = fp2.is_open();
1257 isOpened = fp2.rdbuf()->is_open();
1261 _errorCode = string("Can't create a file ")+aFileName;
1262 SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
1266 //Output the Python script generated by the component in the newly created file.
1267 fp2 << aStream->Data();
1270 if(aStream) delete aStream;
1272 //Add to the main script a call to RebuildData of the generated by the component the Python script
1273 fp << "import " << aScriptName << endl;
1274 fp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << endl;
1278 fp << "if salome.sg.hasDesktop():" << endl;
1279 fp << "\tsalome.sg.updateObjBrowser(1)" << endl;
1281 if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
1282 fp << "\tiparameters.getSession().restoreVisualState(1)" << endl;
1290 //=======================================================================
1291 //function : GetDumpStudyComment
1292 //purpose : return a header comment for a DumpStudy script
1293 //=======================================================================
1295 string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
1298 ("### This file is generated by SALOME automatically by dump python functionality");
1299 if ( theComponentName )
1300 txt += string(" of ") + (char*) theComponentName + " component";
1304 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1307 SALOMEDSImpl_Study* theStudy);
1309 //============================================================================
1313 //============================================================================
1314 void SALOMEDSImpl_Study::dump(const string& theFileName)
1316 //Create a file that will contain a main Study script
1318 fp.open(theFileName.c_str(), ios::out);
1321 bool isOpened = fp.is_open();
1323 bool isOpened = fp.rdbuf()->is_open();
1327 _errorCode = string("Can't create a file ")+theFileName;
1328 cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << endl;
1332 SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
1333 fp << "0:1" << endl;
1334 SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
1336 for(; Itr.More(); Itr.Next()) {
1337 dumpSO(Itr.Value(), fp, aTab, this);
1344 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1347 SALOMEDSImpl_Study* theStudy)
1349 string aTab(Tab), anID(theSO.GetID());
1350 fp << aTab << anID << endl;
1351 vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
1352 for(int i = 0; i<attribs.size(); i++) {
1353 SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
1359 string aType = anAttr->GetClassType();
1360 fp << Tab << " -- " << aType;
1362 if(aType == string("AttributeReal")) {
1363 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
1365 else if(aType == string("AttributeInteger")) {
1366 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
1368 else if(aType == string("AttributeName")) {
1369 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
1371 else if(aType == string("AttributeComment")) {
1372 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
1374 else if(aType == string("AttributeReference")) {
1375 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
1380 SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
1381 string aNewTab(" ");
1383 for(; Itr.More(); Itr.Next()) {
1384 dumpSO(Itr.Value(), fp, aNewTab, theStudy);
1390 void SALOMEDSImpl_Study::Modify()
1393 _doc->SetModified(true);
1396 //============================================================================
1400 //============================================================================
1401 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
1403 if (theSavePoint < 0) return NULL;
1404 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1405 SALOMEDSImpl_SObject so = FindComponent((char*)theID);
1406 if (!so) so = builder->NewComponent((char*)theID);
1407 SALOMEDSImpl_AttributeParameter* attParam = NULL;
1409 if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
1410 DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
1411 if ( !savePointLabel.IsNull() )
1412 so = GetSObject( savePointLabel );
1413 else // ... if it does not exist - create a new one
1414 so = builder->NewObjectToTag( so, theSavePoint );
1419 builder->FindAttribute(so, A, "AttributeParameter");
1420 if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
1421 A = builder->FindOrCreateAttribute(so, "AttributeParameter");
1423 attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
1428 //============================================================================
1432 //============================================================================
1433 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID,
1434 const char* theModuleName,
1437 if(theSavePoint <= 0) return NULL;
1438 SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
1439 SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
1440 SALOMEDSImpl_AttributeParameter* par = NULL;
1442 SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
1443 string moduleName(theModuleName);
1444 for(; it.More(); it.Next()) {
1445 SALOMEDSImpl_SObject so(it.Value());
1446 if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
1447 if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
1448 if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
1452 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1453 SALOMEDSImpl_SObject so = builder->NewObject(main_so);
1454 par = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
1455 par->SetString("AP_MODULE_NAME", moduleName);
1459 //============================================================================
1460 /*! Function : SetStudyLock
1463 //============================================================================
1464 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
1466 _lockers.push_back(theLockerID);
1469 //============================================================================
1470 /*! Function : IsStudyLocked
1473 //============================================================================
1474 bool SALOMEDSImpl_Study::IsStudyLocked()
1476 return (_lockers.size() > 0);
1479 //============================================================================
1480 /*! Function : UnLockStudy
1483 //============================================================================
1484 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
1486 vector<string>::iterator vsI = _lockers.begin();
1487 int length = _lockers.size();
1488 bool isFound = false;
1489 string id(theLockerID);
1490 for(int i = 0; i<length; i++, vsI++) {
1491 if(id == _lockers[i]) {
1496 if(isFound) _lockers.erase(vsI);
1499 //============================================================================
1500 /*! Function : GetLockerID
1503 //============================================================================
1504 vector<string> SALOMEDSImpl_Study::GetLockerID()
1509 //============================================================================
1510 /*! Function : EnableUseCaseAutoFilling
1513 //============================================================================
1514 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
1516 _errorCode = ""; _autoFill = isEnabled;
1518 _builder->SetOnAddSObject(_cb);
1519 _builder->SetOnRemoveSObject(_cb);
1522 _builder->SetOnAddSObject(NULL);
1523 _builder->SetOnRemoveSObject(NULL);
1527 //============================================================================
1528 /*! Function : GetIORs
1531 //============================================================================
1532 vector<string> SALOMEDSImpl_Study::GetIORs()
1534 vector<string> anIORs;
1535 map<string, DF_Label>::const_iterator MI;
1536 for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
1537 anIORs.push_back(MI->first);