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_Study.cxx
21 // Author : Sergey RUIN
25 #include "SALOMEDSImpl_Study.hxx"
29 #include "DF_Application.hxx"
30 #include "DF_ChildIterator.hxx"
32 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
33 #include "SALOMEDSImpl_Attributes.hxx"
34 #include "SALOMEDSImpl_UseCaseIterator.hxx"
35 #include "SALOMEDSImpl_AttributeReference.hxx"
36 #include "SALOMEDSImpl_StudyHandle.hxx"
37 #include "SALOMEDSImpl_Tool.hxx"
38 #include "SALOMEDSImpl_IParameters.hxx"
42 #define DIRECTORYID 16661
43 #define FILELOCALID 26662
44 #define FILEID "FILE: "
47 //============================================================================
48 /*! Function : SALOMEDSImpl_Study
49 * Purpose : SALOMEDSImpl_Study constructor
51 //============================================================================
52 SALOMEDSImpl_Study::SALOMEDSImpl_Study(const DF_Document* doc,
53 const string& study_name)
56 _doc = (DF_Document*)doc;
62 _useCaseBuilder = new SALOMEDSImpl_UseCaseBuilder(_doc);
63 _builder = new SALOMEDSImpl_StudyBuilder(this);
64 _cb = new SALOMEDSImpl_Callback(_useCaseBuilder);
65 //Put on the root label a StudyHandle attribute to store the address of this object
66 //It will be used to retrieve the study object by DF_Label that belongs to the study
67 SALOMEDSImpl_StudyHandle::Set(_doc->Main().Root(), this);
71 //============================================================================
72 /*! Function : ~SALOMEDSImpl_Study
73 * Purpose : SALOMEDSImpl_Study destructor
75 //============================================================================
76 SALOMEDSImpl_Study::~SALOMEDSImpl_Study()
79 //============================================================================
80 /*! Function : GetPersistentReference
81 * Purpose : Get persistent reference of study (idem URL())
83 //============================================================================
84 string SALOMEDSImpl_Study::GetPersistentReference()
89 //============================================================================
90 /*! Function : GetTransientReference
91 * Purpose : Get IOR of the Study (registred in Document in doc->Root)
93 //============================================================================
94 string SALOMEDSImpl_Study::GetTransientReference()
99 SALOMEDSImpl_AttributeIOR* Att;
100 DF_Label _lab = _doc->Root();
101 if ((Att=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
105 _errorCode = "IOR is empty";
111 void SALOMEDSImpl_Study::SetTransientReference(const string& theIOR)
115 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
116 int aLocked = aProp->IsLocked();
117 if (aLocked) aProp->SetLocked(false);
119 // Assign the value of the IOR in the study->root
120 SALOMEDSImpl_AttributeIOR::Set(_doc->Main().Root(), theIOR);
122 if (aLocked) aProp->SetLocked(true);
125 //============================================================================
126 /*! Function : IsEmpty
127 * Purpose : Detect if study is empty
129 //============================================================================
130 bool SALOMEDSImpl_Study::IsEmpty()
133 if (!_doc) return true;
134 return _doc->IsEmpty();
137 //============================================================================
138 /*! Function : FindComponent
139 * Purpose : Find a Component with ComponentDataType = aComponentName
141 //============================================================================
142 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const string& aComponentName)
147 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
148 SALOMEDSImpl_SComponent compo;
150 for (; itcomp.More(); itcomp.Next()) {
151 SALOMEDSImpl_SComponent SC = itcomp.Value();
152 name = SC.ComponentDataType();
153 if(aComponentName == name) {
161 _errorCode = "No component was found";
167 //============================================================================
168 /*! Function : FindComponentID
169 * Purpose : Find a Component from it's ID
171 //============================================================================
172 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const string& aComponentID)
176 // Iterate on each components defined in the study
177 // Get the component ID and compare with aComponentID
180 SALOMEDSImpl_SComponent compo;
182 SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
183 for (; itcomp.More(); itcomp.Next()) {
184 SALOMEDSImpl_SComponent SC = itcomp.Value();
186 if(aComponentID == ID)
195 _errorCode = "No component was found";
202 //============================================================================
203 /*! Function : FindObject
204 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
206 //============================================================================
207 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const string& anObjectName)
211 // Iterate to all components defined in the study
212 // After testing the component name, iterate in all objects defined under
213 // components (function _FindObject)
215 SALOMEDSImpl_SObject RefSO;
217 SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
218 for (; it.More();it.Next()){
221 SALOMEDSImpl_SComponent SC = it.Value();
222 if (SC.GetName() == anObjectName)
228 if (!_find) RefSO = _FindObject(SC, anObjectName, _find);
231 if(!RefSO) _errorCode = "No object was found";
235 //============================================================================
236 /*! Function : FindObjectID
237 * Purpose : Find an Object with ID = anObjectID
239 //============================================================================
240 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const string& anObjectID)
243 SALOMEDSImpl_SObject so;
245 // Convert aSO->GetID in DF_Label.
246 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
249 _errorCode = "No label was found by ID";
252 return GetSObject(Lab);
256 //============================================================================
257 /*! Function : CreateObjectID
258 * Purpose : Creates an Object with ID = anObjectID
260 //============================================================================
261 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const string& anObjectID)
264 SALOMEDSImpl_SObject so;
266 // Convert aSO->GetID in DF_Label.
267 DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
270 _errorCode = "Can not create a label";
273 return GetSObject(Lab);
277 //============================================================================
278 /*! Function : FindObjectByName
279 * Purpose : Find Objects with SALOMEDSImpl_Name = anObjectName in a Component
280 * : with ComponentDataType = aComponentName
282 //============================================================================
283 vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const string& anObjectName,
284 const string& aComponentName)
288 vector<SALOMEDSImpl_SObject> listSO;
290 SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
292 _errorCode = "Can not find the component";
296 // Iterate on each object and subobject of the component
297 // If objectName is found add it to the list of SObjects
300 string compoId = compo.GetID();
301 SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
302 for ( ; it.More(); it.Next() ) {
304 SALOMEDSImpl_SObject CSO = it.Value();
305 if ( CSO.GetName() == anObjectName ) {
307 listSO.push_back(CSO) ;
310 /* looks also for eventual children */
312 CSO = _FindObject( CSO, anObjectName, found ) ;
314 listSO.push_back(CSO) ;
323 //============================================================================
324 /*! Function : FindObjectIOR
325 * Purpose : Find an Object with IOR = anObjectIOR
327 //============================================================================
328 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const string& anObjectIOR)
332 SALOMEDSImpl_SObject aResult ;
334 // searching in the datamap for optimization
335 if (myIORLabels.find(anObjectIOR) != myIORLabels.end()) {
336 aResult = GetSObject(myIORLabels[anObjectIOR]);
337 // 11 oct 2002: forbidden attributes must be checked here
338 if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
339 myIORLabels.erase(anObjectIOR);
340 aResult = SALOMEDSImpl_SObject();
344 if(!aResult) _errorCode = "No object was found";
348 //============================================================================
349 /*! Function : FindObjectByPath
350 * Purpose : Find an Object by its path = thePath
352 //============================================================================
353 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const string& thePath)
357 string aPath(thePath), aToken;
358 SALOMEDSImpl_SObject aSO;
359 int aLength = aPath.size();
360 bool isRelative = false;
362 if(aLength == 0) { //Empty path - return the current context
363 return GetSObject(_current);
366 if(aPath[0] != '/') //Relative path
369 DF_ChildIterator anIterator;
371 SALOMEDSImpl_AttributeName* anAttr;
374 if(_current.IsNull()) return aSO;
375 anIterator.Init(_current, false);
378 if(aPath.size() == 1 && aPath[0] == '/') { //Root
379 return GetSObject(_doc->Main());
381 anIterator.Init(_doc->Main(), false);
384 vector<string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
385 for(int i = 0, len = vs.size(); i<len; i++) {
388 if(aToken.size() == 0) break;
390 for ( ; anIterator.More(); anIterator.Next() ) {
391 aLabel = anIterator.Value();
392 if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
393 if(anAttr->Value() == aToken) {
394 if(i == (len-1)) { //The searched label is found (no part of the path is left)
395 return GetSObject(aLabel);
398 anIterator.Init(aLabel, false);
406 if(!aSO) _errorCode = "No object was found";
410 //============================================================================
411 /*! Function : GetObjectPath
414 //============================================================================
415 string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
421 _errorCode = "Null object";
425 string aName = theObject.GetName();
426 if(!aName.empty() && aName != "" ) {
431 SALOMEDSImpl_SObject aFather = theObject.GetFather();
433 aName = aFather.GetName();
434 if(!aName.empty() && aName != "") {
435 aValue = GetObjectPath(aFather);
436 aPath = aValue + aPath;
445 //============================================================================
446 /*! Function : GetObjectPathByIOR
449 //============================================================================
450 string SALOMEDSImpl_Study::GetObjectPathByIOR(const string& theIOR)
455 SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
457 _errorCode = "No SObject was found by IOR";
461 return GetObjectPath(so);
465 //============================================================================
466 /*! Function : SetContext
467 * Purpose : Sets the current context
469 //============================================================================
470 bool SALOMEDSImpl_Study::SetContext(const string& thePath)
473 if(thePath.empty()) {
474 _errorCode = "InvalidPath";
478 string aPath(thePath), aContext("");
479 bool isInvalid = false;
480 SALOMEDSImpl_SObject aSO;
482 if(aPath[0] != '/') { //Relative path
483 aContext = GetContext();
491 aSO = FindObjectByPath(aContext);
497 if(isInvalid || !aSO) {
498 _errorCode = "InvalidContext";
502 DF_Label aLabel = aSO.GetLabel();
503 if(aLabel.IsNull()) {
504 _errorCode = "InvalidContext";
508 _current = aLabel; //Set the current context
513 //============================================================================
514 /*! Function : GetContext
515 * Purpose : Gets the current context
517 //============================================================================
518 string SALOMEDSImpl_Study::GetContext()
522 if(_current.IsNull()) {
523 _errorCode = "InvaidContext";
526 SALOMEDSImpl_SObject so = GetSObject(_current);
527 return GetObjectPath(so);
530 //============================================================================
531 /*! Function : GetObjectNames
532 * Purpose : method to get all object names in the given context (or in the current context, if 'theContext' is empty)
534 //============================================================================
535 vector<string> SALOMEDSImpl_Study::GetObjectNames(const string& theContext)
539 vector<string> aResultSeq;
541 if (theContext.empty()) {
544 DF_Label aTmp = _current;
545 SetContext(theContext);
549 if (aLabel.IsNull()) {
550 _errorCode = "InvalidContext";
554 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
555 for (; anIter.More(); anIter.Next()) {
556 DF_Label aLabel = anIter.Value();
557 SALOMEDSImpl_AttributeName* aName;
558 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
559 aResultSeq.push_back(aName->Value());
565 //============================================================================
566 /*! Function : GetDirectoryNames
567 * Purpose : method to get all directory names in the given context (or in the current context, if 'theContext' is empty)
569 //============================================================================
570 vector<string> SALOMEDSImpl_Study::GetDirectoryNames(const string& theContext)
574 vector<string> aResultSeq;
576 if (theContext.empty()) {
579 DF_Label aTmp = _current;
580 SetContext(theContext);
584 if (aLabel.IsNull()) {
585 _errorCode = "InvalidContext";
589 DF_ChildIterator anIter (aLabel, true); // iterate first-level children at all sublevels
590 for (; anIter.More(); anIter.Next()) {
591 DF_Label aLabel = anIter.Value();
592 SALOMEDSImpl_AttributeLocalID* anID;
593 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
594 if (anID->Value() == DIRECTORYID) {
595 SALOMEDSImpl_AttributeName* aName;
596 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
597 aResultSeq.push_back(aName->Value());
606 //============================================================================
607 /*! Function : GetFileNames
608 * Purpose : method to get all file names in the given context (or in the current context, if 'theContext' is empty)
610 //============================================================================
611 vector<string> SALOMEDSImpl_Study::GetFileNames(const string& theContext)
615 vector<string> aResultSeq;
617 if (theContext.empty()) {
620 DF_Label aTmp = _current;
621 SetContext(theContext);
625 if (aLabel.IsNull()) {
626 _errorCode = "InvalidContext";
630 DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
631 for (; anIter.More(); anIter.Next()) {
632 DF_Label aLabel = anIter.Value();
633 SALOMEDSImpl_AttributeLocalID* anID;
634 if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
635 if (anID->Value() == FILELOCALID) {
636 SALOMEDSImpl_AttributePersistentRef* aName;
637 if ((aName=(SALOMEDSImpl_AttributePersistentRef*)aLabel.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
638 std::string aFileName = aName->Value();
639 if (aFileName.size() > 0)
640 aResultSeq.push_back(aFileName.substr(strlen(FILEID), aFileName.size()));
649 //============================================================================
650 /*! Function : GetComponentNames
651 * Purpose : method to get all components names
653 //============================================================================
654 vector<string> SALOMEDSImpl_Study::GetComponentNames(const string& theContext)
658 vector<string> aResultSeq;
659 DF_ChildIterator anIter(_doc->Main(), false); // iterate all subchildren at first level
660 for(; anIter.More(); anIter.Next()) {
661 DF_Label aLabel = anIter.Value();
662 SALOMEDSImpl_AttributeName* aName;
663 if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
664 aResultSeq.push_back(aName->Value());
670 //============================================================================
671 /*! Function : NewChildIterator
672 * Purpose : Create a ChildIterator from an SObject
674 //============================================================================
675 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
678 return SALOMEDSImpl_ChildIterator(aSO);
682 //============================================================================
683 /*! Function : NewComponentIterator
684 * Purpose : Create a SComponentIterator
686 //============================================================================
687 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
690 return SALOMEDSImpl_SComponentIterator(_doc);
694 //============================================================================
695 /*! Function : NewBuilder
696 * Purpose : Create a StudyBuilder
698 //============================================================================
699 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
703 _builder->SetOnAddSObject(_cb);
704 _builder->SetOnRemoveSObject(_cb);
710 //============================================================================
712 * Purpose : get study name
714 //============================================================================
715 string SALOMEDSImpl_Study::Name()
721 //============================================================================
723 * Purpose : set study name
725 //============================================================================
726 void SALOMEDSImpl_Study::Name(const string& name)
732 //============================================================================
733 /*! Function : IsSaved
734 * Purpose : get if study has been saved
736 //============================================================================
737 bool SALOMEDSImpl_Study::IsSaved()
743 //============================================================================
744 /*! Function : IsSaved
745 * Purpose : set if study has been saved
747 //============================================================================
748 void SALOMEDSImpl_Study::IsSaved(bool save)
752 if(save) _doc->SetModified(false);
755 //============================================================================
756 /*! Function : IsModified
757 * Purpose : Detect if a Study has been modified since it has been saved
759 //============================================================================
760 bool SALOMEDSImpl_Study::IsModified()
764 // True if is modified
765 if (_doc->IsModified()) return true;
770 //============================================================================
772 * Purpose : get URL of the study (persistent reference of the study)
774 //============================================================================
775 string SALOMEDSImpl_Study::URL()
781 //============================================================================
783 * Purpose : set URL of the study (persistent reference of the study)
785 //============================================================================
786 void SALOMEDSImpl_Study::URL(const string& url)
791 /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
794 char *aName = (char*)tmp.ToCString();
795 char *adr = strtok(aName, "/");
799 adr = strtok(NULL, "/");
806 //============================================================================
807 /*! Function : _FindObject
808 * Purpose : Find an Object with SALOMEDSImpl_Name = anObjectName
810 //============================================================================
811 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
812 const string& theObjectName,
815 SALOMEDSImpl_SObject RefSO;
816 if(!SO) return RefSO;
818 // Iterate on each objects and subobjects of the component
819 // If objectName find, stop the loop and get the object reference
820 SALOMEDSImpl_AttributeName* anAttr;
822 string soid = SO.GetID();
823 DF_ChildIterator it(SO.GetLabel());
824 for (; it.More(); it.Next()){
827 if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
829 string Val(anAttr->Value());
830 if (Val == theObjectName)
832 RefSO = GetSObject(it.Value());
836 if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
842 //============================================================================
843 /*! Function : _FindObjectIOR
844 * Purpose : Find an Object with SALOMEDSImpl_IOR = anObjectIOR
846 //============================================================================
848 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
849 const string& theObjectIOR,
852 SALOMEDSImpl_SObject RefSO, aSO;
853 if(!SO) return RefSO;
855 // Iterate on each objects and subobjects of the component
856 // If objectName find, stop the loop and get the object reference
857 SALOMEDSImpl_AttributeIOR* anAttr;
859 DF_ChildIterator it(SO.GetLabel());
860 for (; it.More();it.Next()){
863 if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
865 string Val(anAttr->Value());
866 if (Val == theObjectIOR)
868 RefSO = GetSObject(it.Value());
872 aSO = GetSObject(it.Value());
873 if (!_find) RefSO = _FindObjectIOR(aSO, theObjectIOR, _find);
879 bool SALOMEDSImpl_Study::IsLocked()
882 return GetProperties()->IsLocked();
885 int SALOMEDSImpl_Study::StudyId()
891 void SALOMEDSImpl_Study::StudyId(int id)
897 void SALOMEDSImpl_Study::UpdateIORLabelMap(const string& anIOR,const string& anEntry)
900 DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
901 if (myIORLabels.find(anIOR) != myIORLabels.end()) myIORLabels.erase(anIOR);
902 myIORLabels[anIOR] = aLabel;
905 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel)
907 SALOMEDSImpl_StudyHandle* Att;
908 if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
914 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
916 return GetStudy(theLabel)->GetSObject(theLabel);
919 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
921 return GetStudy(theLabel)->GetSComponent(theLabel);
925 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
927 string aString = theAttribute->Label().Entry();
928 GetStudy(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
931 vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
934 vector<SALOMEDSImpl_SObject> aSeq;
936 SALOMEDSImpl_AttributeTarget* aTarget;
937 if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
938 return aTarget->Get();
945 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
948 return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
951 string SALOMEDSImpl_Study::GetLastModificationDate()
954 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
956 vector<string> aNames;
957 vector<int> aMinutes, aHours, aDays, aMonths, aYears;
958 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
960 int aLastIndex = aNames.size()-1;
962 sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
963 (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
964 (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
965 string aResStr (aResult);
969 vector<string> SALOMEDSImpl_Study::GetModificationsDate()
972 SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
974 vector<string> aNames;
975 vector<int> aMinutes, aHours, aDays, aMonths, aYears;
976 aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
978 int anIndex, aLength = aNames.size();
979 vector<string> aDates;
981 for (anIndex = 1; anIndex < aLength; anIndex++) {
983 sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
984 (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
985 (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
986 aDates.push_back(aDate);
993 //============================================================================
994 /*! Function : GetUseCaseBuilder
995 * Purpose : Returns a UseCase builder
997 //============================================================================
998 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
1001 return _useCaseBuilder;
1005 //============================================================================
1006 /*! Function : Close
1009 //============================================================================
1010 void SALOMEDSImpl_Study::Close()
1013 _doc->GetApplication()->Close(_doc);
1020 //============================================================================
1021 /*! Function : GetSComponent
1024 //============================================================================
1025 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const string& theEntry)
1027 SALOMEDSImpl_SComponent aSCO;
1028 if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
1029 aSCO = _mapOfSCO[theEntry];
1031 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1032 aSCO = SALOMEDSImpl_SComponent(aLabel);
1033 _mapOfSCO[theEntry] = aSCO;
1039 //============================================================================
1040 /*! Function : GetSComponent
1043 //============================================================================
1044 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
1046 return GetSComponent(theLabel.Entry());
1049 //============================================================================
1050 /*! Function : GetSObject
1053 //============================================================================
1054 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const string& theEntry)
1056 SALOMEDSImpl_SObject aSO;
1057 if(_mapOfSO.find(theEntry) != _mapOfSO.end())
1058 aSO = _mapOfSO[theEntry];
1060 DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
1061 aSO = SALOMEDSImpl_SObject(aLabel);
1062 _mapOfSO[theEntry] = aSO;
1068 //============================================================================
1069 /*! Function : GetSObject
1072 //============================================================================
1073 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
1075 return GetSObject(theLabel.Entry());
1078 //============================================================================
1079 /*! Function : GetAttribute
1082 //============================================================================
1083 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const string& theEntry,
1084 const string& theType)
1086 SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
1087 DF_Attribute* anAttr;
1088 aSO.FindAttribute(anAttr, theType);
1092 //============================================================================
1093 /*! Function : DumpStudy
1096 //============================================================================
1097 bool SALOMEDSImpl_Study::DumpStudy(const string& thePath,
1098 const string& theBaseName,
1100 SALOMEDSImpl_DriverFactory* theFactory)
1104 if(theFactory == NULL) {
1105 _errorCode = "Null factory for creation of Engines";
1109 vector<string> aSeq;
1110 string aCompType, aFactoryType;
1112 //Build a list of all components in the Study
1113 SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
1115 for (; itcomponent.More(); itcomponent.Next()) {
1116 SALOMEDSImpl_SComponent sco = itcomponent.Value();
1117 aCompType = sco.ComponentDataType();
1118 //GEOM and MED are independent components
1119 if (aCompType == "GEOM" || aCompType == "MED")
1120 aSeq.insert(aSeq.begin(), aCompType);
1122 aSeq.push_back(aCompType);
1127 thePath + string("\\") + theBaseName + string(".py");
1130 thePath + string("/") + theBaseName + string(".py");
1133 //Create a file that will contain a main Study script
1135 fp.open(aFileName.c_str(), ios::out);
1138 bool isOpened = fp.is_open();
1140 bool isOpened = fp.rdbuf()->is_open();
1144 _errorCode = string("Can't create a file ")+aFileName;
1148 string aBatchModeScript = "salome";
1150 //Output to the main Study script required Python modules import,
1151 //set sys.path and add a creation of the study.
1152 fp << GetDumpStudyComment() << endl << endl;
1153 fp << "import sys" << endl;
1154 fp << "import " << aBatchModeScript << endl << endl;
1156 fp << aBatchModeScript << ".salome_init()" << endl << endl;
1158 fp << "sys.path.insert( 0, \'" << thePath << "\')" << endl << endl;
1161 //Check if it's necessary to dump visual parameters
1162 bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
1163 int lastSavePoint = -1;
1165 lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
1166 if(lastSavePoint > 0) {
1167 fp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << endl << endl;
1172 vector<string> aSeqOfFileNames;
1174 //Iterate all components and create the componponents specific scripts.
1176 int aLength = aSeq.size();
1177 for(int i = 1; i <= aLength; i++) {
1179 aCompType = aSeq[i-1];
1180 SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
1181 SALOMEDSImpl_Driver* aDriver = NULL;
1182 // if there is an associated Engine call its method for saving
1185 if (!sco.ComponentIOR(IOREngine)) {
1186 if (!aCompType.empty()) {
1188 aDriver = theFactory->GetDriverByType(aCompType);
1190 if (aDriver != NULL) {
1191 SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
1192 if(!SB->LoadWith(sco, aDriver)) {
1193 _errorCode = SB->GetErrorCode();
1201 aDriver = theFactory->GetDriverByIOR(IOREngine);
1204 _errorCode = "Can not restore information to dump it";
1208 if(aDriver == NULL) continue;
1211 long aStreamLength = 0;
1212 SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(this, isPublished, isValidScript, aStreamLength);
1213 if ( !isValidScript )
1216 //Create a file that will contain the component specific script
1219 aFileName=thePath+string("\\");
1221 aFileName=thePath+string("/");
1224 aScriptName += theBaseName;
1226 aScriptName += aCompType;
1228 aFileName += aScriptName+ string(".py");
1229 aSeqOfFileNames.push_back(aFileName);
1231 fp2.open(aFileName.c_str(), ios::out);
1234 isOpened = fp2.is_open();
1236 isOpened = fp2.rdbuf()->is_open();
1240 _errorCode = string("Can't create a file ")+aFileName;
1241 SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
1245 //Output the Python script generated by the component in the newly created file.
1246 fp2 << aStream->Data();
1249 if(aStream) delete aStream;
1251 //Add to the main script a call to RebuildData of the generated by the component the Python script
1252 fp << "import " << aScriptName << endl;
1253 fp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << endl;
1257 fp << "if salome.sg.hasDesktop():" << endl;
1258 fp << "\tsalome.sg.updateObjBrowser(1)" << endl;
1260 if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
1261 fp << "\tiparameters.getSession().restoreVisualState(1)" << endl;
1269 //=======================================================================
1270 //function : GetDumpStudyComment
1271 //purpose : return a header comment for a DumpStudy script
1272 //=======================================================================
1274 string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
1277 ("### This file is generated by SALOME automatically by dump python functionality");
1278 if ( theComponentName )
1279 txt += string(" of ") + (char*) theComponentName + " component";
1283 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1286 SALOMEDSImpl_Study* theStudy);
1288 //============================================================================
1292 //============================================================================
1293 void SALOMEDSImpl_Study::dump(const string& theFileName)
1295 //Create a file that will contain a main Study script
1297 fp.open(theFileName.c_str(), ios::out);
1300 bool isOpened = fp.is_open();
1302 bool isOpened = fp.rdbuf()->is_open();
1306 _errorCode = string("Can't create a file ")+theFileName;
1307 cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << endl;
1311 SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
1312 fp << "0:1" << endl;
1313 SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
1315 for(; Itr.More(); Itr.Next()) {
1316 dumpSO(Itr.Value(), fp, aTab, this);
1323 void dumpSO(const SALOMEDSImpl_SObject& theSO,
1326 SALOMEDSImpl_Study* theStudy)
1328 string aTab(Tab), anID(theSO.GetID());
1329 fp << aTab << anID << endl;
1330 vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
1331 for(int i = 0; i<attribs.size(); i++) {
1332 SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
1338 string aType = anAttr->GetClassType();
1339 fp << Tab << " -- " << aType;
1341 if(aType == string("AttributeReal")) {
1342 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
1344 else if(aType == string("AttributeInteger")) {
1345 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
1347 else if(aType == string("AttributeName")) {
1348 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
1350 else if(aType == string("AttributeComment")) {
1351 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
1353 else if(aType == string("AttributeReference")) {
1354 fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
1359 SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
1360 string aNewTab(" ");
1362 for(; Itr.More(); Itr.Next()) {
1363 dumpSO(Itr.Value(), fp, aNewTab, theStudy);
1369 void SALOMEDSImpl_Study::Modify()
1372 _doc->SetModified(true);
1375 //============================================================================
1379 //============================================================================
1380 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
1382 if (theSavePoint < 0) return NULL;
1383 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1384 SALOMEDSImpl_SObject so = FindComponent((char*)theID);
1385 if (!so) so = builder->NewComponent((char*)theID);
1386 SALOMEDSImpl_AttributeParameter* attParam = NULL;
1388 if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
1389 DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
1390 if ( !savePointLabel.IsNull() )
1391 so = GetSObject( savePointLabel );
1392 else // ... if it does not exist - create a new one
1393 so = builder->NewObjectToTag( so, theSavePoint );
1398 builder->FindAttribute(so, A, "AttributeParameter");
1399 if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
1400 A = builder->FindOrCreateAttribute(so, "AttributeParameter");
1402 attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
1407 //============================================================================
1411 //============================================================================
1412 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID,
1413 const char* theModuleName,
1416 if(theSavePoint <= 0) return NULL;
1417 SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
1418 SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
1419 SALOMEDSImpl_AttributeParameter* par = NULL;
1421 SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
1422 string moduleName(theModuleName);
1423 for(; it.More(); it.Next()) {
1424 SALOMEDSImpl_SObject so(it.Value());
1425 if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
1426 if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
1427 if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
1431 SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
1432 SALOMEDSImpl_SObject so = builder->NewObject(main_so);
1433 par = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
1434 par->SetString("AP_MODULE_NAME", moduleName);
1438 //============================================================================
1439 /*! Function : SetStudyLock
1442 //============================================================================
1443 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
1445 _lockers.push_back(theLockerID);
1448 //============================================================================
1449 /*! Function : IsStudyLocked
1452 //============================================================================
1453 bool SALOMEDSImpl_Study::IsStudyLocked()
1455 return (_lockers.size() > 0);
1458 //============================================================================
1459 /*! Function : UnLockStudy
1462 //============================================================================
1463 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
1465 vector<string>::iterator vsI = _lockers.begin();
1466 int length = _lockers.size();
1467 bool isFound = false;
1468 string id(theLockerID);
1469 for(int i = 0; i<length; i++, vsI++) {
1470 if(id == _lockers[i]) {
1475 if(isFound) _lockers.erase(vsI);
1478 //============================================================================
1479 /*! Function : GetLockerID
1482 //============================================================================
1483 vector<string> SALOMEDSImpl_Study::GetLockerID()
1488 //============================================================================
1489 /*! Function : EnableUseCaseAutoFilling
1492 //============================================================================
1493 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
1495 _errorCode = ""; _autoFill = isEnabled;
1497 _builder->SetOnAddSObject(_cb);
1498 _builder->SetOnRemoveSObject(_cb);
1501 _builder->SetOnAddSObject(NULL);
1502 _builder->SetOnRemoveSObject(NULL);
1506 //============================================================================
1507 /*! Function : GetIORs
1510 //============================================================================
1511 vector<string> SALOMEDSImpl_Study::GetIORs()
1513 vector<string> anIORs;
1514 map<string, DF_Label>::const_iterator MI;
1515 for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
1516 anIORs.push_back(MI->first);