Back to index

salome-kernel  6.5.0
SALOMEDSImpl_Study.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : SALOMEDSImpl_Study.cxx
00024 //  Author : Sergey RUIN
00025 //  Module : SALOME
00026 //
00027 #include "SALOMEDSImpl_Study.hxx"
00028 #include <string.h>
00029 
00030 #include <KERNEL_version.h>
00031 #include <Basics_Utils.hxx>
00032 
00033 #include "DF_Application.hxx"
00034 #include "DF_ChildIterator.hxx"
00035 
00036 #include "SALOMEDSImpl_ChildNodeIterator.hxx"
00037 #include "SALOMEDSImpl_Attributes.hxx"
00038 #include "SALOMEDSImpl_UseCaseIterator.hxx"
00039 #include "SALOMEDSImpl_AttributeReference.hxx"
00040 #include "SALOMEDSImpl_StudyHandle.hxx"
00041 #include "SALOMEDSImpl_Tool.hxx"
00042 #include "SALOMEDSImpl_IParameters.hxx"
00043 #include "SALOMEDSImpl_ScalarVariable.hxx"
00044 
00045 #include <fstream>
00046 #include <sstream>
00047 
00048 // comment out the following define to enable \t symbols in in the python dump files
00049 #define WITHOUT_TABS
00050 
00051 #define DIRECTORYID       16661
00052 #define FILELOCALID       26662
00053 #define FILEID            "FILE: "
00054 #define VARIABLE_SEPARATOR  ':'
00055 #define OPERATION_SEPARATOR '|'
00056 
00057 //============================================================================
00061 //============================================================================
00062 SALOMEDSImpl_Study::SALOMEDSImpl_Study(const DF_Document* doc,
00063                                        const std::string& study_name)
00064 {
00065   _name = study_name;
00066   _doc = (DF_Document*)doc;
00067   _Saved = false ;
00068   _URL = "";
00069   _StudyId = -1;
00070   _autoFill = false;
00071   _errorCode = "";
00072   _useCaseBuilder = new SALOMEDSImpl_UseCaseBuilder(_doc);
00073   _builder = new SALOMEDSImpl_StudyBuilder(this);
00074   _cb = new SALOMEDSImpl_Callback(_useCaseBuilder);
00075   _notifier=0;
00076   //Put on the root label a StudyHandle attribute to store the address of this object
00077   //It will be used to retrieve the study object by DF_Label that belongs to the study
00078   SALOMEDSImpl_StudyHandle::Set(_doc->Main().Root(), this);
00079 }
00080 
00081 
00082 //============================================================================
00086 //============================================================================
00087 SALOMEDSImpl_Study::~SALOMEDSImpl_Study()
00088 {
00089   delete _builder;
00090   delete _cb;
00091   delete _useCaseBuilder;
00092 }
00093 
00094 //============================================================================
00098 //============================================================================
00099 std::string SALOMEDSImpl_Study::GetPersistentReference()
00100 {
00101   _errorCode = "";
00102   return URL();
00103 }
00104 //============================================================================
00108 //============================================================================
00109 std::string SALOMEDSImpl_Study::GetTransientReference()
00110 {
00111   _errorCode = "";
00112   std::string IOR = "";
00113 
00114   SALOMEDSImpl_AttributeIOR* Att;
00115   DF_Label _lab = _doc->Root();
00116   if ((Att=(SALOMEDSImpl_AttributeIOR*)_lab.FindAttribute(SALOMEDSImpl_AttributeIOR::GetID()))) {
00117     IOR = Att->Value();
00118   }
00119   else {
00120     _errorCode = "IOR is empty";
00121   }
00122 
00123   return IOR;
00124 }
00125 
00126 void SALOMEDSImpl_Study::SetTransientReference(const std::string& theIOR)
00127 {
00128   _errorCode = "";
00129 
00130   SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
00131   int aLocked = aProp->IsLocked();
00132   if (aLocked) aProp->SetLocked(false);
00133 
00134   // Assign the value of the IOR in the study->root
00135   SALOMEDSImpl_AttributeIOR::Set(_doc->Main().Root(), theIOR);
00136 
00137   if (aLocked) aProp->SetLocked(true);
00138 }
00139 
00140 //============================================================================
00144 //============================================================================
00145 bool SALOMEDSImpl_Study::IsEmpty()
00146 {
00147   _errorCode = "";
00148   if (!_doc) return true;
00149   return _doc->IsEmpty();
00150 }
00151 
00152 //============================================================================
00156 //============================================================================
00157 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponent (const std::string& aComponentName)
00158 {
00159   _errorCode = "";
00160   bool _find = false;
00161   std::string name;
00162   SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
00163   SALOMEDSImpl_SComponent compo;
00164 
00165   for (; itcomp.More(); itcomp.Next()) {
00166     SALOMEDSImpl_SComponent SC = itcomp.Value();
00167     name = SC.ComponentDataType();
00168     if(aComponentName == name) {
00169       _find = true;
00170       return SC;
00171     }
00172   }
00173 
00174   if(!_find)
00175     {
00176       _errorCode = "No component was found";
00177       return compo;
00178     }
00179   return compo;
00180 }
00181 
00182 //============================================================================
00186 //============================================================================
00187 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::FindComponentID(const std::string& aComponentID)
00188 {
00189   _errorCode = "";
00190 
00191   // Iterate on each components defined in the study
00192   // Get the component ID and compare with aComponentID
00193   bool _find = false;
00194   std::string ID;
00195   SALOMEDSImpl_SComponent compo;
00196 
00197   SALOMEDSImpl_SComponentIterator itcomp = NewComponentIterator();
00198   for (; itcomp.More(); itcomp.Next()) {
00199     SALOMEDSImpl_SComponent SC = itcomp.Value();
00200     ID = SC.GetID();
00201     if(aComponentID == ID)
00202       {
00203         // ComponentID found
00204         _find = true;
00205         compo = SC;
00206       }
00207   }
00208   if(!_find)
00209     {
00210       _errorCode = "No component was found";
00211       compo = compo;
00212     }
00213 
00214   return compo;
00215 }
00216 
00217 //============================================================================
00221 //============================================================================
00222 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObject(const std::string& anObjectName)
00223 {
00224   _errorCode = "";
00225 
00226   // Iterate to all components defined in the study
00227   // After testing the component name, iterate in all objects defined under
00228   // components (function _FindObject)
00229   bool _find = false;
00230   SALOMEDSImpl_SObject RefSO;
00231 
00232   SALOMEDSImpl_SComponentIterator it = NewComponentIterator();
00233   for (; it.More();it.Next()){
00234     if(!_find)
00235       {
00236         SALOMEDSImpl_SComponent SC = it.Value();
00237         if (SC.GetName() == anObjectName)
00238         {
00239             _find = true;
00240             RefSO = SC;
00241 
00242         }
00243         if (!_find) RefSO =  _FindObject(SC, anObjectName, _find);
00244       }
00245   }
00246   if(!RefSO) _errorCode = "No object was found";
00247   return RefSO;
00248 }
00249 
00250 //============================================================================
00254 //============================================================================
00255 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectID(const std::string& anObjectID)
00256 {
00257   _errorCode = "";
00258   SALOMEDSImpl_SObject so;
00259 
00260   // Convert aSO->GetID in DF_Label.
00261   DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, false);
00262 
00263   if (Lab.IsNull()) {
00264     _errorCode = "No label was found by ID";
00265     return so;
00266   }
00267   return GetSObject(Lab);
00268 
00269 }
00270 
00271 //============================================================================
00275 //============================================================================
00276 SALOMEDSImpl_SObject SALOMEDSImpl_Study::CreateObjectID(const std::string& anObjectID)
00277 {
00278   _errorCode = "";
00279   SALOMEDSImpl_SObject so;
00280 
00281   // Convert aSO->GetID in DF_Label.
00282   DF_Label Lab = DF_Label::Label(_doc->Main(), anObjectID, true);
00283 
00284   if (Lab.IsNull()) {
00285     _errorCode = "Can not create a label";
00286     return so;
00287   }
00288   return GetSObject(Lab);
00289 
00290 }
00291 
00292 //============================================================================
00297 //============================================================================
00298 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindObjectByName(const std::string& anObjectName,
00299                                                                const std::string& aComponentName)
00300 {
00301   _errorCode = "";
00302 
00303   std::vector<SALOMEDSImpl_SObject> listSO;
00304 
00305   SALOMEDSImpl_SComponent compo = FindComponent(aComponentName) ;
00306   if ( !compo ) {
00307     _errorCode = "Can not find the component";
00308     return listSO;
00309   }
00310 
00311   // Iterate on each object and subobject of the component
00312   // If objectName is found add it to the list of SObjects
00313   std::string childName ;
00314 
00315   std::string compoId = compo.GetID();
00316   SALOMEDSImpl_ChildIterator it = NewChildIterator(compo);
00317   for ( ; it.More(); it.Next() ) {
00318 
00319     SALOMEDSImpl_SObject CSO = it.Value();
00320     if ( CSO.GetName() == anObjectName ) {
00321         /* add to list */
00322         listSO.push_back(CSO) ;
00323     }
00324 
00325     /* looks also for eventual children */
00326     bool found = false ;
00327     CSO = _FindObject( CSO, anObjectName, found ) ;
00328     if( found) {
00329       listSO.push_back(CSO) ;
00330     }
00331   }
00332 
00333   return listSO;
00334 }
00335 
00336 
00337 
00338 //============================================================================
00342 //============================================================================
00343 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectIOR(const std::string& anObjectIOR)
00344 {
00345   _errorCode = "";
00346 
00347   SALOMEDSImpl_SObject aResult ;
00348 
00349   // searching in the datamap for optimization
00350   std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anObjectIOR);
00351   if (it != myIORLabels.end()) {
00352     aResult = GetSObject(it->second);
00353     // 11 oct 2002: forbidden attributes must be checked here
00354     if (!aResult.GetLabel().IsAttribute(SALOMEDSImpl_AttributeIOR::GetID())) {
00355       myIORLabels.erase(anObjectIOR);
00356       aResult = SALOMEDSImpl_SObject();
00357     }
00358   }
00359 
00360   if(!aResult) _errorCode = "No object was found";
00361   return aResult;
00362 }
00363 
00364 //============================================================================
00368 //============================================================================
00369 SALOMEDSImpl_SObject SALOMEDSImpl_Study::FindObjectByPath(const std::string& thePath)
00370 {
00371   _errorCode = "";
00372 
00373   std::string aPath(thePath), aToken;
00374   SALOMEDSImpl_SObject aSO;
00375   int aLength = aPath.size();
00376   bool isRelative = false;
00377 
00378   if(aLength == 0) {  //Empty path - return the current context
00379     return GetSObject(_current);
00380   }
00381 
00382   if(aPath[0] != '/')  //Relative path
00383     isRelative = true;
00384 
00385   DF_ChildIterator anIterator;
00386   DF_Label aLabel;
00387   SALOMEDSImpl_AttributeName* anAttr;
00388 
00389   if(isRelative) {
00390     if(_current.IsNull()) return aSO;
00391     anIterator.Init(_current, false);
00392   }
00393   else {
00394     if(aPath.size() == 1 && aPath[0] == '/') {    //Root
00395       return GetSObject(_doc->Main());
00396     }
00397     anIterator.Init(_doc->Main(), false);
00398   }
00399 
00400   std::vector<std::string> vs = SALOMEDSImpl_Tool::splitString(aPath, '/');
00401   for(int i = 0, len = vs.size(); i<len; i++) {
00402 
00403     aToken = vs[i];
00404     if(aToken.size() == 0) break;
00405 
00406     for ( ; anIterator.More(); anIterator.Next() ) {
00407       aLabel = anIterator.Value();
00408       if((anAttr=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
00409         if(anAttr->Value() == aToken) {
00410           if(i == (len-1)) {  //The searched label is found (no part of the path is left)
00411               return GetSObject(aLabel);
00412           }
00413 
00414           anIterator.Init(aLabel, false);
00415           break;
00416         }
00417       }
00418     }
00419 
00420   }
00421 
00422   if(!aSO) _errorCode = "No object was found";
00423   return aSO;
00424 }
00425 
00426 //============================================================================
00430 //============================================================================
00431 std::string SALOMEDSImpl_Study::GetObjectPath(const SALOMEDSImpl_SObject& theObject)
00432 {
00433   _errorCode = "";
00434 
00435   std::string aPath("");
00436   if(!theObject) {
00437     _errorCode = "Null object";
00438     return aPath;
00439   }
00440 
00441   std::string aName = theObject.GetName();
00442   if(!aName.empty() && aName != "" ) {
00443     std::string aValue("/");
00444     aValue+=aName;
00445     aValue += aPath;
00446     aPath = aValue;
00447     SALOMEDSImpl_SObject aFather = theObject.GetFather();
00448     if(aFather) {
00449        aName = aFather.GetName();
00450        if(!aName.empty() && aName != "") {
00451            aValue = GetObjectPath(aFather);
00452           aPath = aValue + aPath;
00453        }
00454     }
00455   }
00456 
00457   return aPath;
00458 }
00459 
00460 
00461 //============================================================================
00465 //============================================================================
00466 std::string SALOMEDSImpl_Study::GetObjectPathByIOR(const std::string& theIOR)
00467 {
00468   _errorCode = "";
00469 
00470   std::string aPath;
00471   SALOMEDSImpl_SObject so = FindObjectIOR(theIOR);
00472   if(!so) {
00473     _errorCode = "No SObject was found by IOR";
00474     return aPath;
00475   }
00476 
00477   return GetObjectPath(so);
00478 }
00479 
00480 
00481 //============================================================================
00485 //============================================================================
00486 bool SALOMEDSImpl_Study::SetContext(const std::string& thePath)
00487 {
00488   _errorCode = "";
00489   if(thePath.empty()) {
00490     _errorCode = "InvalidPath";
00491     return false;
00492   }
00493 
00494   std::string aPath(thePath), aContext("");
00495   bool isInvalid = false;
00496   SALOMEDSImpl_SObject aSO;
00497 
00498   if(aPath[0] != '/') { //Relative path
00499     aContext = GetContext();
00500     aContext += '/';
00501     aContext += aPath;
00502   }
00503   else
00504     aContext = aPath;
00505 
00506   try {
00507     aSO = FindObjectByPath(aContext);
00508   }
00509   catch( ... ) {
00510     isInvalid = true;
00511   }
00512 
00513   if(isInvalid || !aSO) {
00514     _errorCode = "InvalidContext";
00515     return false;
00516   }
00517 
00518   DF_Label aLabel = aSO.GetLabel();
00519   if(aLabel.IsNull()) {
00520     _errorCode = "InvalidContext";
00521     return false;
00522   }
00523   else
00524     _current = aLabel;  //Set the current context
00525 
00526   return true;
00527 }
00528 
00529 //============================================================================
00533 //============================================================================
00534 std::string SALOMEDSImpl_Study::GetContext()
00535 {
00536   _errorCode = "";
00537 
00538   if(_current.IsNull()) {
00539     _errorCode = "InvaidContext";
00540     return "";
00541   }
00542   SALOMEDSImpl_SObject so = GetSObject(_current);
00543   return GetObjectPath(so);
00544 }
00545 
00546 //============================================================================
00550 //============================================================================
00551 std::vector<std::string> SALOMEDSImpl_Study::GetObjectNames(const std::string& theContext)
00552 {
00553   _errorCode = "";
00554 
00555   std::vector<std::string> aResultSeq;
00556   DF_Label aLabel;
00557   if (theContext.empty()) {
00558     aLabel = _current;
00559   } else {
00560     DF_Label aTmp = _current;
00561     SetContext(theContext);
00562     aLabel = _current;
00563     _current = aTmp;
00564   }
00565   if (aLabel.IsNull()) {
00566     _errorCode = "InvalidContext";
00567     return aResultSeq;
00568   }
00569 
00570   DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
00571   for (; anIter.More(); anIter.Next()) {
00572     DF_Label aLabel = anIter.Value();
00573     SALOMEDSImpl_AttributeName* aName;
00574     if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
00575       aResultSeq.push_back(aName->Value());
00576   }
00577 
00578   return aResultSeq;
00579 }
00580 
00581 //============================================================================
00585 //============================================================================
00586 std::vector<std::string> SALOMEDSImpl_Study::GetDirectoryNames(const std::string& theContext)
00587 {
00588   _errorCode = "";
00589 
00590   std::vector<std::string> aResultSeq;
00591   DF_Label aLabel;
00592   if (theContext.empty()) {
00593     aLabel = _current;
00594   } else {
00595     DF_Label aTmp = _current;
00596     SetContext(theContext);
00597     aLabel = _current;
00598     _current = aTmp;
00599   }
00600   if (aLabel.IsNull()) {
00601     _errorCode = "InvalidContext";
00602     return aResultSeq;
00603   }
00604 
00605   DF_ChildIterator anIter (aLabel, true); // iterate first-level children at all sublevels
00606   for (; anIter.More(); anIter.Next()) {
00607     DF_Label aLabel = anIter.Value();
00608     SALOMEDSImpl_AttributeLocalID* anID;
00609     if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
00610       if (anID->Value() == DIRECTORYID) {
00611         SALOMEDSImpl_AttributeName* aName;
00612         if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID()))) {
00613           aResultSeq.push_back(aName->Value());
00614         }
00615       }
00616     }
00617   }
00618 
00619   return aResultSeq;
00620 }
00621 
00622 //============================================================================
00626 //============================================================================
00627 std::vector<std::string> SALOMEDSImpl_Study::GetFileNames(const std::string& theContext)
00628 {
00629   _errorCode = "";
00630 
00631   std::vector<std::string> aResultSeq;
00632   DF_Label aLabel;
00633   if (theContext.empty()) {
00634     aLabel = _current;
00635   } else {
00636     DF_Label aTmp = _current;
00637     SetContext(theContext);
00638     aLabel = _current;
00639     _current = aTmp;
00640   }
00641   if (aLabel.IsNull()) {
00642     _errorCode = "InvalidContext";
00643     return aResultSeq;
00644   }
00645 
00646   DF_ChildIterator anIter (aLabel, true); // iterate all subchildren at all sublevels
00647   for (; anIter.More(); anIter.Next()) {
00648     DF_Label aLabel = anIter.Value();
00649     SALOMEDSImpl_AttributeLocalID* anID;
00650     if ((anID=(SALOMEDSImpl_AttributeLocalID*)aLabel.FindAttribute(SALOMEDSImpl_AttributeLocalID::GetID()))) {
00651       if (anID->Value() == FILELOCALID) {
00652         SALOMEDSImpl_AttributePersistentRef* aName;
00653         if ((aName=(SALOMEDSImpl_AttributePersistentRef*)aLabel.FindAttribute(SALOMEDSImpl_AttributePersistentRef::GetID()))) {
00654           std::string aFileName = aName->Value();
00655           if (aFileName.size() > 0)
00656             aResultSeq.push_back(aFileName.substr(strlen(FILEID), aFileName.size()));
00657         }
00658       }
00659     }
00660   }
00661 
00662   return aResultSeq;
00663 }
00664 
00665 //============================================================================
00669 //============================================================================
00670 std::vector<std::string> SALOMEDSImpl_Study::GetComponentNames(const std::string& theContext)
00671 {
00672   _errorCode = "";
00673 
00674   std::vector<std::string> aResultSeq;
00675   DF_ChildIterator anIter(_doc->Main(), false); // iterate all subchildren at first level
00676   for(; anIter.More(); anIter.Next()) {
00677     DF_Label aLabel = anIter.Value();
00678     SALOMEDSImpl_AttributeName* aName;
00679     if ((aName=(SALOMEDSImpl_AttributeName*)aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
00680       aResultSeq.push_back(aName->Value());
00681   }
00682 
00683   return aResultSeq;
00684 }
00685 
00686 //============================================================================
00690 //============================================================================
00691 SALOMEDSImpl_ChildIterator SALOMEDSImpl_Study::NewChildIterator(const SALOMEDSImpl_SObject& aSO)
00692 {
00693   _errorCode = "";
00694   return SALOMEDSImpl_ChildIterator(aSO);
00695 }
00696 
00697 
00698 //============================================================================
00702 //============================================================================
00703 SALOMEDSImpl_SComponentIterator SALOMEDSImpl_Study::NewComponentIterator()
00704 {
00705   _errorCode = "";
00706   return SALOMEDSImpl_SComponentIterator(_doc);
00707 }
00708 
00709 
00710 //============================================================================
00714 //============================================================================
00715 SALOMEDSImpl_StudyBuilder* SALOMEDSImpl_Study::NewBuilder()
00716 {
00717   _errorCode = "";
00718   if(_autoFill) {
00719     _builder->SetOnAddSObject(_cb);
00720     _builder->SetOnRemoveSObject(_cb);
00721   }
00722   return _builder;
00723 
00724 }
00725 
00726 //============================================================================
00730 //============================================================================
00731 std::string SALOMEDSImpl_Study::Name()
00732 {
00733   _errorCode = "";
00734   return _name;
00735 }
00736 
00737 //============================================================================
00741 //============================================================================
00742 void SALOMEDSImpl_Study::Name(const std::string& name)
00743 {
00744   _errorCode = "";
00745   _name = name;
00746 }
00747 
00748 //============================================================================
00752 //============================================================================
00753 bool SALOMEDSImpl_Study::IsSaved()
00754 {
00755   _errorCode = "";
00756   return _Saved;
00757 }
00758 
00759 //============================================================================
00763 //============================================================================
00764 void SALOMEDSImpl_Study::IsSaved(bool save)
00765 {
00766   _errorCode = "";
00767   _Saved = save;
00768   if(save) _doc->SetModified(false);
00769 }
00770 
00771 //============================================================================
00775 //============================================================================
00776 bool SALOMEDSImpl_Study::IsModified()
00777 {
00778   _errorCode = "";
00779 
00780   // True if is modified
00781   if (_doc && _doc->IsModified()) return true;
00782 
00783   return false;
00784 }
00785 
00786 //============================================================================
00790 //============================================================================
00791 std::string SALOMEDSImpl_Study::URL()
00792 {
00793   _errorCode = "";
00794   return _URL;
00795 }
00796 
00797 //============================================================================
00801 //============================================================================
00802 void SALOMEDSImpl_Study::URL(const std::string& url)
00803 {
00804   _errorCode = "";
00805   _URL = url;
00806 
00807   /*jfa: Now name of SALOMEDS study will correspond to name of SalomeApp study
00808   std::string tmp(_URL);
00809 
00810   char *aName = (char*)tmp.ToCString();
00811   char *adr = strtok(aName, "/");
00812   while (adr)
00813     {
00814       aName = adr;
00815       adr = strtok(NULL, "/");
00816     }
00817   Name(aName);*/
00818   Name(url);
00819 }
00820 
00821 
00822 //============================================================================
00826 //============================================================================
00827 SALOMEDSImpl_SObject SALOMEDSImpl_Study::_FindObject(const SALOMEDSImpl_SObject& SO,
00828                                                const std::string& theObjectName,
00829                                                bool& _find)
00830 {
00831   SALOMEDSImpl_SObject RefSO;
00832   if(!SO) return RefSO;
00833 
00834   // Iterate on each objects and subobjects of the component
00835   // If objectName find, stop the loop and get the object reference
00836   SALOMEDSImpl_AttributeName* anAttr;
00837 
00838   std::string soid = SO.GetID();
00839   DF_ChildIterator it(SO.GetLabel());
00840   for (; it.More(); it.Next()){
00841     if(!_find)
00842       {
00843         if ((anAttr=(SALOMEDSImpl_AttributeName*)it.Value().FindAttribute(SALOMEDSImpl_AttributeName::GetID())))
00844         {
00845           std::string Val(anAttr->Value());
00846           if (Val == theObjectName)
00847             {
00848               RefSO = GetSObject(it.Value());
00849               _find = true;
00850             }
00851         }
00852         if (!_find) RefSO = _FindObject(GetSObject(it.Value()), theObjectName, _find);
00853       }
00854   }
00855   return RefSO;
00856 }
00857 
00858 //============================================================================
00862 //============================================================================
00863 SALOMEDSImpl_SObject
00864 SALOMEDSImpl_Study::_FindObjectIOR(const SALOMEDSImpl_SObject& SO,
00865                                    const std::string& theObjectIOR,
00866                                    bool& _find)
00867 {
00868   SALOMEDSImpl_SObject RefSO, aSO;
00869   if(!SO) return RefSO;
00870 
00871   // Iterate on each objects and subobjects of the component
00872   // If objectName find, stop the loop and get the object reference
00873   SALOMEDSImpl_AttributeIOR* anAttr;
00874 
00875   DF_ChildIterator it(SO.GetLabel());
00876   for (; it.More();it.Next()){
00877     if(!_find)
00878       {
00879         if ((anAttr=(SALOMEDSImpl_AttributeIOR*)it.Value().FindAttribute(SALOMEDSImpl_AttributeIOR::GetID())))
00880         {
00881           std::string Val(anAttr->Value());
00882           if (Val == theObjectIOR)
00883             {
00884               RefSO = GetSObject(it.Value());
00885               _find = true;
00886             }
00887         }
00888         aSO = GetSObject(it.Value());
00889         if (!_find) RefSO =  _FindObjectIOR(aSO, theObjectIOR, _find);
00890       }
00891   }
00892   return RefSO;
00893 }
00894 
00895 //============================================================================
00899 //============================================================================
00900 std::string SALOMEDSImpl_Study::_GetNoteBookAccessor(){
00901   return std::string("notebook");
00902 }
00903 
00904 //============================================================================
00908 //============================================================================
00909 std::string SALOMEDSImpl_Study::_GetStudyVariablesScript()
00910 {
00911   std::string dump("");
00912 
00913   if(myNoteBookVars.empty())
00914     return dump;
00915 
00916   Kernel_Utils::Localizer loc;
00917 
00918   dump += "####################################################\n";
00919   dump += "##       Begin of NoteBook variables section      ##\n";
00920   dump += "####################################################\n";
00921 
00922   std::string set_method = _GetNoteBookAccessor()+".set(";
00923   std::string varName;
00924   std::string varValue;
00925   for(int i = 0 ; i < myNoteBookVars.size();i++ ) {
00926     varName = myNoteBookVars[i]->Name();
00927     varValue = myNoteBookVars[i]->SaveToScript();
00928     dump+=set_method+"\""+varName+"\", "+varValue+")\n";
00929   }
00930 
00931   dump += "####################################################\n";
00932   dump += "##        End of NoteBook variables section       ##\n";
00933   dump += "####################################################\n";
00934 
00935   return dump;
00936 }
00937 
00938 //============================================================================
00942 //============================================================================
00943 std::string SALOMEDSImpl_Study::_GetNoteBookAccess()
00944 {
00945   std::string accessor = _GetNoteBookAccessor();
00946   std::string notebook = "import salome_notebook\n";
00947   notebook += accessor+" = salome_notebook."+accessor + "\n";
00948   return notebook;
00949 }
00950 
00951 bool SALOMEDSImpl_Study::IsLocked()
00952 {
00953   _errorCode = "";
00954   return GetProperties()->IsLocked();
00955 }
00956 
00957 int SALOMEDSImpl_Study::StudyId()
00958 {
00959   _errorCode = "";
00960   return _StudyId;
00961 }
00962 
00963 void SALOMEDSImpl_Study::StudyId(int id)
00964 {
00965   _errorCode = "";
00966   _StudyId = id;
00967 }
00968 
00969 void SALOMEDSImpl_Study::UpdateIORLabelMap(const std::string& anIOR,const std::string& anEntry)
00970 {
00971   _errorCode = "";
00972   DF_Label aLabel = DF_Label::Label(_doc->Main(), anEntry, true);
00973   std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
00974   if (it != myIORLabels.end()) myIORLabels.erase(it);
00975   myIORLabels[anIOR] = aLabel;
00976 }
00977 
00978 void SALOMEDSImpl_Study::DeleteIORLabelMapItem(const std::string& anIOR)
00979 {
00980   std::map<std::string, DF_Label>::iterator it=myIORLabels.find(anIOR);
00981   if (it != myIORLabels.end())
00982     {
00983       //remove the ior entry and decref the genericobj (if it's one)
00984       myIORLabels.erase(it);
00985     }
00986 }
00987 
00988 SALOMEDSImpl_Study* SALOMEDSImpl_Study::GetStudy(const DF_Label& theLabel)
00989 {
00990   SALOMEDSImpl_StudyHandle* Att;
00991   if ((Att=(SALOMEDSImpl_StudyHandle*)theLabel.Root().FindAttribute(SALOMEDSImpl_StudyHandle::GetID()))) {
00992     return Att->Get();
00993   }
00994   return NULL;
00995 }
00996 
00997 SALOMEDSImpl_SObject SALOMEDSImpl_Study::SObject(const DF_Label& theLabel)
00998 {
00999   return GetStudy(theLabel)->GetSObject(theLabel);
01000 }
01001 
01002 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::SComponent(const DF_Label& theLabel)
01003 {
01004   return GetStudy(theLabel)->GetSComponent(theLabel);
01005 }
01006 
01007 
01008 void SALOMEDSImpl_Study::IORUpdated(const SALOMEDSImpl_AttributeIOR* theAttribute)
01009 {
01010   std::string aString = theAttribute->Label().Entry();
01011   GetStudy(theAttribute->Label())->UpdateIORLabelMap(theAttribute->Value(), aString);
01012 }
01013 
01014 std::vector<SALOMEDSImpl_SObject> SALOMEDSImpl_Study::FindDependances(const SALOMEDSImpl_SObject& anObject)
01015 {
01016   _errorCode = "";
01017   std::vector<SALOMEDSImpl_SObject> aSeq;
01018 
01019   SALOMEDSImpl_AttributeTarget* aTarget;
01020   if ((aTarget=(SALOMEDSImpl_AttributeTarget*)anObject.GetLabel().FindAttribute(SALOMEDSImpl_AttributeTarget::GetID()))) {
01021     return aTarget->Get();
01022   }
01023 
01024   return aSeq;
01025 }
01026 
01027 
01028 SALOMEDSImpl_AttributeStudyProperties* SALOMEDSImpl_Study::GetProperties()
01029 {
01030   _errorCode = "";
01031   return SALOMEDSImpl_AttributeStudyProperties::Set(_doc->Main());
01032 }
01033 
01034 std::string SALOMEDSImpl_Study::GetLastModificationDate()
01035 {
01036   _errorCode = "";
01037   SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
01038 
01039   std::vector<std::string> aNames;
01040   std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
01041   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
01042 
01043   int aLastIndex = aNames.size()-1;
01044   char aResult[20];
01045   sprintf(aResult, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
01046           (int)(aDays[aLastIndex]),(int)(aMonths[aLastIndex]), (int)(aYears[aLastIndex]),
01047           (int)(aHours[aLastIndex]), (int)(aMinutes[aLastIndex]));
01048   std::string aResStr (aResult);
01049   return aResStr;
01050 }
01051 
01052 std::vector<std::string> SALOMEDSImpl_Study::GetModificationsDate()
01053 {
01054   _errorCode = "";
01055   SALOMEDSImpl_AttributeStudyProperties* aProp = GetProperties();
01056 
01057   std::vector<std::string> aNames;
01058   std::vector<int> aMinutes, aHours, aDays, aMonths, aYears;
01059   aProp->GetModifications(aNames, aMinutes, aHours, aDays, aMonths, aYears);
01060 
01061   int anIndex, aLength = aNames.size();
01062   std::vector<std::string> aDates;
01063 
01064   for (anIndex = 1; anIndex < aLength; anIndex++) {
01065     char aDate[20];
01066     sprintf(aDate, "%2.2d/%2.2d/%4.4d %2.2d:%2.2d",
01067             (int)(aDays[anIndex]), (int)(aMonths[anIndex]), (int)(aYears[anIndex]),
01068             (int)(aHours[anIndex]), (int)(aMinutes[anIndex]));
01069     aDates.push_back(aDate);
01070   }
01071   return aDates;
01072 }
01073 
01074 
01075 
01076 //============================================================================
01080 //============================================================================
01081 SALOMEDSImpl_UseCaseBuilder* SALOMEDSImpl_Study::GetUseCaseBuilder()
01082 {
01083   _errorCode = "";
01084   return _useCaseBuilder;
01085 }
01086 
01087 
01088 //============================================================================
01092 //============================================================================
01093 void SALOMEDSImpl_Study::Close()
01094 {
01095   _errorCode = "";
01096   _doc->GetApplication()->Close(_doc);
01097   _doc = NULL;
01098   _mapOfSO.clear();
01099   _mapOfSCO.clear();
01100 }
01101 
01102 
01103 //============================================================================
01107 //============================================================================
01108 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const std::string& theEntry)
01109 {
01110   SALOMEDSImpl_SComponent aSCO;
01111   if(_mapOfSCO.find(theEntry) != _mapOfSCO.end())
01112     aSCO = _mapOfSCO[theEntry];
01113   else {
01114     DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
01115     aSCO = SALOMEDSImpl_SComponent(aLabel);
01116     _mapOfSCO[theEntry] = aSCO;
01117   }
01118 
01119   return aSCO;
01120 }
01121 
01122 //============================================================================
01126 //============================================================================
01127 SALOMEDSImpl_SComponent SALOMEDSImpl_Study::GetSComponent(const DF_Label& theLabel)
01128 {
01129   return SALOMEDSImpl_SComponent(theLabel);
01130 }
01131 
01132 //============================================================================
01136 //============================================================================
01137 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const std::string& theEntry)
01138 {
01139   SALOMEDSImpl_SObject aSO;
01140   std::map<std::string, SALOMEDSImpl_SObject>::iterator it=_mapOfSO.find(theEntry);
01141   if(it != _mapOfSO.end())
01142     aSO = it->second;
01143   else {
01144     DF_Label aLabel = DF_Label::Label(_doc->Main(), theEntry);
01145     aSO = SALOMEDSImpl_SObject(aLabel);
01146     _mapOfSO[theEntry] = aSO;
01147   }
01148 
01149   return aSO;
01150 }
01151 
01152 //============================================================================
01156 //============================================================================
01157 SALOMEDSImpl_SObject SALOMEDSImpl_Study::GetSObject(const DF_Label& theLabel)
01158 {
01159   return SALOMEDSImpl_SObject(theLabel);
01160 }
01161 
01162 //============================================================================
01166 //============================================================================
01167 DF_Attribute* SALOMEDSImpl_Study::GetAttribute(const std::string& theEntry,
01168                                                const std::string& theType)
01169 {
01170   SALOMEDSImpl_SObject aSO = GetSObject(theEntry);
01171   DF_Attribute* anAttr;
01172   aSO.FindAttribute(anAttr, theType);
01173   return anAttr;
01174 }
01175 
01177 static const int indent_size = 2;
01178 
01179 static std::string replace_tabs( const std::string& in )
01180 {
01181   std::string out = in;
01182 #ifdef WITHOUT_TABS
01183   size_t pos = out.find( '\t' );
01184   while ( pos != std::string::npos ) {
01185     out.replace( pos, 1, indent_size, ' ' );
01186     pos = out.find( '\t' );
01187   }
01188 #endif
01189   return out;
01190 }
01191 
01192 static std::string GetComponentHeader(const char* theComponentName)
01193 {
01194   std::stringstream txt;
01195   txt << "###" << std::endl;
01196   txt << "### " << theComponentName << " component" << std::endl;
01197   txt << "###" << std::endl;
01198   return txt.str();
01199 }
01200 
01201 //============================================================================
01205 //============================================================================
01206 bool SALOMEDSImpl_Study::DumpStudy(const std::string& thePath,
01207                                    const std::string& theBaseName,
01208                                    bool isPublished,
01209                                    bool isMultiFile,
01210                                    SALOMEDSImpl_DriverFactory* theFactory)
01211 {
01212   _errorCode = "";
01213 
01214   if(theFactory == NULL) {
01215     _errorCode = "Null factory for creation of Engines";
01216     return false;
01217   }
01218 
01219   std::vector<std::string> aSeq;
01220   std::string aCompType, aFactoryType;
01221 
01222   //Build a list of all components in the Study
01223   SALOMEDSImpl_SComponentIterator itcomponent = NewComponentIterator();
01224 
01225   for (; itcomponent.More(); itcomponent.Next()) {
01226     SALOMEDSImpl_SComponent sco = itcomponent.Value();
01227     aCompType = sco.ComponentDataType();
01228     //GEOM and MED are independent components
01229     if (aCompType == "GEOM" || aCompType == "MED")
01230       aSeq.insert(aSeq.begin(), aCompType);
01231     else
01232       aSeq.push_back(aCompType);
01233   }
01234 
01235 #ifdef WIN32
01236   std::string aFileName =
01237     thePath + std::string("\\") + theBaseName + std::string(".py");
01238 #else
01239   std::string aFileName =
01240     thePath + std::string("/")  + theBaseName + std::string(".py");
01241 #endif
01242 
01243   //Create a file that will contain a main Study script
01244   std::fstream fp;
01245   fp.open(aFileName.c_str(), std::ios::out);
01246 
01247 #ifdef WIN32
01248   bool isOpened = fp.is_open();
01249 #else
01250   bool isOpened = fp.rdbuf()->is_open();
01251 #endif
01252 
01253   if(!isOpened) {
01254     _errorCode = std::string("Can't create a file ")+aFileName;
01255     return false;
01256   }
01257 
01258   std::stringstream sfp;
01259 
01260   std::string aBatchModeScript = "salome";
01261 
01262   //Output to the main Study script required Python modules import,
01263   //set sys.path and add a creation of the study.
01264 
01265   // dump header
01266   sfp << GetDumpStudyComment() << std::endl;
01267 
01268   // global imports
01269   sfp << "import sys" << std::endl;
01270   sfp << "import " << aBatchModeScript << std::endl << std::endl;
01271 
01272   // initialization function
01273   sfp << aBatchModeScript << ".salome_init()" << std::endl;
01274   if ( !isMultiFile )
01275     sfp << "theStudy = salome.myStudy" <<std::endl << std::endl;
01276 
01277   // notebook initialization
01278   sfp << _GetNoteBookAccess();
01279 
01280   // extend sys.path with the directory where the script is being dumped to
01281   sfp << "sys.path.insert( 0, r\'" << thePath << "\')" << std::endl << std::endl;
01282 
01283   // dump NoteBook variables
01284   sfp << _GetStudyVariablesScript();
01285 
01286   // dump visual parameters if necessary
01287   bool isDumpVisuals = SALOMEDSImpl_IParameters::isDumpPython(this);
01288   int lastSavePoint = -1;
01289   if(isDumpVisuals) {
01290     lastSavePoint = SALOMEDSImpl_IParameters::getLastSavePoint(this);
01291     if(lastSavePoint > 0) {
01292       sfp << SALOMEDSImpl_IParameters::getStudyScript(this, lastSavePoint) << std::endl << std::endl;
01293     }
01294   }
01295 
01296   std::vector<std::string> aSeqOfFileNames;
01297 
01298   // dump all components and create the components specific scripts
01299   bool isOk = true;
01300   int aLength = aSeq.size();
01301   for(int i = 1; i <= aLength; i++) {
01302 
01303     aCompType = aSeq[i-1];
01304     SALOMEDSImpl_SComponent sco = FindComponent(aCompType);
01305     SALOMEDSImpl_Driver* aDriver = NULL;
01306     // if there is an associated Engine call its method for saving
01307     std::string IOREngine;
01308     try {
01309       if (!sco.ComponentIOR(IOREngine)) {
01310         if (!aCompType.empty()) {
01311 
01312           aDriver = theFactory->GetDriverByType(aCompType);
01313 
01314           if (aDriver != NULL) {
01315             SALOMEDSImpl_StudyBuilder* SB = NewBuilder();
01316             if(!SB->LoadWith(sco, aDriver)) {
01317               _errorCode = SB->GetErrorCode();
01318               return false;
01319             }
01320           }
01321           else continue;
01322         }
01323       }
01324       else {
01325         aDriver = theFactory->GetDriverByIOR(IOREngine);
01326       }
01327     } catch(...) {
01328       _errorCode = "Can not restore information to dump it";
01329       return false;
01330     }
01331 
01332     if(aDriver == NULL) continue;
01333 
01334     bool isValidScript;
01335     long aStreamLength  = 0;
01336     SALOMEDSImpl_TMPFile* aStream = aDriver->DumpPython(this, isPublished, isMultiFile, isValidScript, aStreamLength);
01337     if ( !isValidScript )
01338       isOk = false;
01339 
01340     std::stringstream sfp2;
01341     
01342     //Output the Python script generated by the component in the newly created file.
01343     if ( isMultiFile )
01344       sfp2 << GetDumpStudyComment( aCompType.c_str() ) << std::endl;
01345     else
01346       sfp2 << GetComponentHeader( aCompType.c_str() ) << std::endl;
01347     sfp2 << aStream->Data();
01348     
01349     if ( isMultiFile ) {
01350       //Create a file that will contain the component specific script
01351       std::fstream fp2;
01352 #ifdef WIN32
01353       aFileName=thePath+std::string("\\");
01354 #else
01355       aFileName=thePath+std::string("/");
01356 #endif
01357       std::string aScriptName;
01358       aScriptName += theBaseName;
01359       aScriptName += "_";
01360       aScriptName += aCompType;
01361       
01362       aFileName += aScriptName+ std::string(".py");
01363       aSeqOfFileNames.push_back(aFileName);
01364       
01365       fp2.open(aFileName.c_str(), std::ios::out);
01366       
01367 #ifdef WIN32
01368       isOpened = fp2.is_open();
01369 #else
01370       isOpened = fp2.rdbuf()->is_open();
01371 #endif
01372       
01373       if(!isOpened) {
01374        _errorCode = std::string("Can't create a file ")+aFileName;
01375        SALOMEDSImpl_Tool::RemoveTemporaryFiles(thePath, aSeqOfFileNames, false);
01376        return false;
01377       }
01378      
01379       // replace '\t' symbols
01380       fp2 << replace_tabs( sfp2.str() );
01381 
01382       fp2.close();
01383 
01384       //Add to the main script a call to RebuildData of the generated by the component the Python script
01385       sfp << "import " << aScriptName << std::endl;
01386       sfp << aScriptName << ".RebuildData(" << aBatchModeScript << ".myStudy)" << std::endl;
01387     }
01388     else
01389       sfp << sfp2.str();
01390 
01391     if(aStream) delete aStream;
01392   }
01393 
01394   sfp << std::endl;
01395   sfp << "if salome.sg.hasDesktop():" << std::endl;
01396   sfp << "\tsalome.sg.updateObjBrowser(1)" << std::endl;
01397 
01398   if(isDumpVisuals) { //Output the call to Session's method restoreVisualState
01399     sfp << "\tiparameters.getSession().restoreVisualState(1)" << std::endl;
01400   }
01401 
01402   // replace '\t' symbols
01403   fp << replace_tabs( sfp.str() );
01404   
01405   fp.close();
01406 
01407   return isOk;
01408 }
01409 
01410 //=======================================================================
01411 //function : GetDumpStudyComment
01412 //purpose  : return a header comment for a DumpStudy script
01413 //=======================================================================
01414 
01415 std::string SALOMEDSImpl_Study::GetDumpStudyComment(const char* theComponentName)
01416 {
01417   std::stringstream txt;
01418   txt << "# -*- coding: iso-8859-1 -*-" << std::endl << std::endl;
01419   txt << "###" << std::endl;
01420   txt << "### This file is generated automatically by SALOME v"
01421       << KERNEL_VERSION_STR
01422       << " with dump python functionality";
01423   if ( theComponentName )
01424     txt << " (" << theComponentName << " component)";
01425   txt << std::endl;
01426   txt << "###" << std::endl;
01427   return txt.str();
01428 }
01429 
01430 void dumpSO(const SALOMEDSImpl_SObject& theSO,
01431             std::fstream& fp,
01432             const std::string& Tab,
01433             SALOMEDSImpl_Study* theStudy);
01434 
01435 //============================================================================
01439 //============================================================================
01440 void SALOMEDSImpl_Study::dump(const std::string& theFileName)
01441 {
01442   //Create a file that will contain a main Study script
01443   std::fstream fp;
01444   fp.open(theFileName.c_str(), std::ios::out);
01445 
01446 #ifdef WIN32
01447   bool isOpened = fp.is_open();
01448 #else
01449   bool isOpened = fp.rdbuf()->is_open();
01450 #endif
01451 
01452   if(!isOpened) {
01453     _errorCode = std::string("Can't create a file ")+theFileName;
01454     std::cout << "### SALOMEDSImpl_Study::dump Error: " << _errorCode << std::endl;
01455     return;
01456   }
01457 
01458   SALOMEDSImpl_SObject aSO = FindObjectID("0:1");
01459   fp << "0:1" << std::endl;
01460   SALOMEDSImpl_ChildIterator Itr = NewChildIterator(aSO);
01461   std::string aTab("   ");
01462   for(; Itr.More(); Itr.Next()) {
01463     dumpSO(Itr.Value(), fp, aTab, this);
01464   }
01465 
01466   fp.close();
01467 }
01468 
01469 
01470 void dumpSO(const SALOMEDSImpl_SObject& theSO,
01471             std::fstream& fp,
01472             const std::string& Tab,
01473             SALOMEDSImpl_Study* theStudy)
01474 {
01475   std::string aTab(Tab), anID(theSO.GetID());
01476   fp << aTab << anID << std::endl;
01477   std::vector<DF_Attribute*> attribs = theSO.GetLabel().GetAttributes();
01478   for(int i = 0; i<attribs.size(); i++) {
01479     SALOMEDSImpl_GenericAttribute* anAttr = dynamic_cast<SALOMEDSImpl_GenericAttribute*>(attribs[i]);
01480 
01481     if(!anAttr) {
01482       continue;
01483     }
01484 
01485     std::string aType = anAttr->GetClassType();
01486     fp << Tab << "  -- " << aType;
01487 
01488     if(aType == std::string("AttributeReal")) {
01489       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReal*>(anAttr)->Value();
01490     }
01491     else if(aType == std::string("AttributeInteger")) {
01492       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeInteger*>(anAttr)->Value();
01493     }
01494     else if(aType ==  std::string("AttributeName")) {
01495       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeName*>(anAttr)->Value();
01496     }
01497     else if(aType == std::string("AttributeComment")) {
01498       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeComment*>(anAttr)->Value();
01499     }
01500     else if(aType == std::string("AttributeReference")) {
01501       fp << " : " << dynamic_cast<SALOMEDSImpl_AttributeReference*>(anAttr)->Save();
01502     }
01503     fp << std::endl;
01504   }
01505 
01506   SALOMEDSImpl_ChildIterator Itr = theStudy->NewChildIterator(theSO);
01507   std::string aNewTab("   ");
01508   aNewTab+=aTab;
01509   for(; Itr.More(); Itr.Next()) {
01510     dumpSO(Itr.Value(), fp, aNewTab, theStudy);
01511   }
01512 
01513   return;
01514 }
01515 
01516 void SALOMEDSImpl_Study::Modify()
01517 {
01518   _errorCode = "";
01519   _doc->SetModified(true);
01520 }
01521 
01522 //============================================================================
01526 //============================================================================
01527 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetCommonParameters(const char* theID, int theSavePoint)
01528 {
01529   if (theSavePoint < -1) return NULL;
01530   SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
01531   SALOMEDSImpl_SObject so = FindComponent((char*)theID);
01532   if (!so) so = builder->NewComponent((char*)theID);
01533   SALOMEDSImpl_AttributeParameter* attParam = NULL;
01534 
01535   if (theSavePoint == -1) {
01536     int ctag = 1;
01537     DF_Label savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
01538     DF_Label prevPointLabel;
01539     while ( !savePointLabel.IsNull() ) {
01540       ctag++;
01541       prevPointLabel = savePointLabel;
01542       savePointLabel = so.GetLabel().FindChild( ctag, /*create=*/0 );
01543     }
01544     if ( !prevPointLabel.IsNull() )
01545       so = GetSObject( prevPointLabel );
01546   }
01547   if (theSavePoint > 0) { // Try to find SObject that contains attribute parameter ...
01548     DF_Label savePointLabel = so.GetLabel().FindChild( theSavePoint, /*create=*/0 );
01549     if ( !savePointLabel.IsNull() )
01550       so = GetSObject( savePointLabel );
01551     else // ... if it does not exist - create a new one
01552       so = builder->NewObjectToTag( so, theSavePoint );
01553   }
01554 
01555   DF_Attribute* A;
01556   if (so) {
01557     builder->FindAttribute(so, A, "AttributeParameter");
01558     if ( !A ) { // first call of GetCommonParameters on "Interface Applicative" component
01559       A = builder->FindOrCreateAttribute(so, "AttributeParameter");
01560     }
01561     attParam = dynamic_cast<SALOMEDSImpl_AttributeParameter*>( A );
01562   }
01563   return attParam;
01564 }
01565 
01566 //============================================================================
01570 //============================================================================
01571 SALOMEDSImpl_AttributeParameter* SALOMEDSImpl_Study::GetModuleParameters(const char* theID,
01572                                                                          const char* theModuleName,
01573                                                                          int theSavePoint)
01574 {
01575   if(theSavePoint < -1) return NULL;
01576   SALOMEDSImpl_AttributeParameter* main_ap = GetCommonParameters(theID, theSavePoint);
01577   SALOMEDSImpl_SObject main_so = main_ap->GetSObject();
01578   SALOMEDSImpl_AttributeParameter* par = NULL;
01579 
01580   SALOMEDSImpl_ChildIterator it = NewChildIterator(main_so);
01581   std::string moduleName(theModuleName);
01582   for(; it.More(); it.Next()) {
01583     SALOMEDSImpl_SObject so(it.Value());
01584     if((par=(SALOMEDSImpl_AttributeParameter*)so.GetLabel().FindAttribute(SALOMEDSImpl_AttributeParameter::GetID()))) {
01585       if(!par->IsSet("AP_MODULE_NAME", (Parameter_Types)3)) continue; //3 -> PT_STRING
01586       if(par->GetString("AP_MODULE_NAME") == moduleName) return par;
01587     }
01588   }
01589 
01590   SALOMEDSImpl_StudyBuilder* builder = NewBuilder();
01591   SALOMEDSImpl_SObject so = builder->NewObject(main_so);
01592   par  = dynamic_cast<SALOMEDSImpl_AttributeParameter*>(builder->FindOrCreateAttribute(so, "AttributeParameter"));
01593   par->SetString("AP_MODULE_NAME", moduleName);
01594   return par;
01595 }
01596 
01597 //============================================================================
01601 //============================================================================
01602 void SALOMEDSImpl_Study::SetStudyLock(const char* theLockerID)
01603 {
01604   _lockers.push_back(theLockerID);
01605 }
01606 
01607 //============================================================================
01611 //============================================================================
01612 bool SALOMEDSImpl_Study::IsStudyLocked()
01613 {
01614   return (_lockers.size() > 0);
01615 }
01616 
01617 //============================================================================
01621 //============================================================================
01622 void SALOMEDSImpl_Study::UnLockStudy(const char* theLockerID)
01623 {
01624   std::vector<std::string>::iterator vsI = _lockers.begin();
01625   int length = _lockers.size();
01626   bool isFound = false;
01627   std::string id(theLockerID);
01628   for(int i = 0; i<length; i++, vsI++) {
01629     if(id == _lockers[i]) {
01630       isFound = true;;
01631       break;
01632     }
01633   }
01634   if(isFound) _lockers.erase(vsI);
01635 }
01636 
01637 //============================================================================
01641 //============================================================================
01642 std::vector<std::string> SALOMEDSImpl_Study::GetLockerID()
01643 {
01644   return _lockers;
01645 }
01646 
01647 //============================================================================
01651 //============================================================================
01652 void SALOMEDSImpl_Study::SetVariable(const std::string& theVarName,
01653                                      const double theValue,
01654                                      const SALOMEDSImpl_GenericVariable::VariableTypes theType)
01655 {
01656   bool modified = false;
01657   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
01658 
01659   if( aGVar == NULL ) {
01660 
01661     SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
01662 
01663     aSVar->setValue(theValue);
01664     myNoteBookVars.push_back(aSVar);
01665     modified = true;
01666   }
01667   else {
01668     if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
01669       modified = aSVar->setValue(theValue) || modified;
01670       modified = aSVar->setType(theType) || modified;
01671     }
01672   }
01673   if(modified)
01674     Modify();
01675 }
01676 
01677 //============================================================================
01681 //============================================================================
01682 void SALOMEDSImpl_Study::SetStringVariable(const std::string& theVarName,
01683                                       const std::string& theValue,
01684                                       const SALOMEDSImpl_GenericVariable::VariableTypes theType)
01685 {
01686   bool modified = false;
01687   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
01688 
01689   if( aGVar == NULL ) {
01690 
01691     SALOMEDSImpl_ScalarVariable* aSVar = new SALOMEDSImpl_ScalarVariable(theType, theVarName);
01692 
01693     aSVar->setStringValue(theValue);
01694     myNoteBookVars.push_back(aSVar);
01695     modified = true;
01696   }
01697   else {
01698     if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar)) {
01699       modified = aSVar->setStringValue(theValue) || modified;
01700       modified = aSVar->setType(theType) || modified;
01701     }
01702   }
01703   if(modified)
01704     Modify();
01705 }
01706 
01707 //============================================================================
01711 //============================================================================
01712 void SALOMEDSImpl_Study::SetStringVariableAsDouble(const std::string& theVarName,
01713                                              const double theValue,
01714                                              const SALOMEDSImpl_GenericVariable::VariableTypes theType)
01715 {
01716   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
01717   if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
01718     aSVar->setValue(theValue);
01719 }
01720 
01721 //============================================================================
01725 //============================================================================
01726 double SALOMEDSImpl_Study::GetVariableValue(const std::string& theVarName)
01727 {
01728   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
01729 
01730   if(aGVar != NULL )
01731     if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
01732       return aSVar->getValue();
01733 
01734   return 0;
01735 }
01736 
01737 //============================================================================
01741 //============================================================================
01742 std::string SALOMEDSImpl_Study::GetStringVariableValue(const std::string& theVarName)
01743 {
01744   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
01745 
01746   if(aGVar != NULL )
01747     if(SALOMEDSImpl_ScalarVariable* aSVar = dynamic_cast<SALOMEDSImpl_ScalarVariable*>(aGVar))
01748       return aSVar->getStringValue();
01749 
01750   return 0;
01751 }
01752 
01753 //============================================================================
01757 //============================================================================
01758 bool SALOMEDSImpl_Study::IsTypeOf(const std::string& theVarName,
01759                                   SALOMEDSImpl_GenericVariable::
01760                                   VariableTypes theType) const
01761 {
01762   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
01763 
01764   if(aGVar != NULL )
01765     return aGVar->Type() == theType;
01766 
01767   return false;
01768 }
01769 
01770 //============================================================================
01774 //============================================================================
01775 bool SALOMEDSImpl_Study::IsVariable(const std::string& theVarName) const
01776 {
01777   SALOMEDSImpl_GenericVariable* aGVar = GetVariable(theVarName);
01778   return (aGVar != NULL);
01779 }
01780 
01781 //============================================================================
01785 //============================================================================
01786 std::vector<std::string> SALOMEDSImpl_Study::GetVariableNames() const
01787 {
01788   std::vector<std::string> aResult;
01789 
01790   for(int i = 0; i < myNoteBookVars.size(); i++)
01791     aResult.push_back(myNoteBookVars[i]->Name());
01792 
01793   return aResult;
01794 }
01795 
01796 //============================================================================
01800 //============================================================================
01801 void SALOMEDSImpl_Study::AddVariable(SALOMEDSImpl_GenericVariable* theVariable)
01802 {
01803   myNoteBookVars.push_back(theVariable);
01804 }
01805 
01806 //============================================================================
01810 //============================================================================
01811 SALOMEDSImpl_GenericVariable* SALOMEDSImpl_Study::GetVariable(const std::string& theName) const
01812 {
01813   SALOMEDSImpl_GenericVariable* aResult = NULL;
01814   for(int i = 0; i < myNoteBookVars.size();i++) {
01815     if(theName.compare(myNoteBookVars[i]->Name()) == 0) {
01816       aResult = myNoteBookVars[i];
01817       break;
01818     }
01819   }
01820   return aResult;
01821 }
01822 
01823 //============================================================================
01827 //============================================================================
01828 bool SALOMEDSImpl_Study::RemoveVariable(const std::string& theVarName)
01829 {
01830   SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
01831   if( !aVariable )
01832     return false;
01833 
01834   std::string aValue = aVariable->SaveToScript();
01835   ReplaceVariableAttribute( theVarName, aValue );
01836 
01837   std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
01838   for( ; it != itEnd; it++ )
01839   {
01840     SALOMEDSImpl_GenericVariable* aVariableRef = *it;
01841     if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
01842     {
01843       myNoteBookVars.erase( it );
01844       Modify();
01845       break;
01846     }
01847   }
01848 
01849   return true;
01850 }
01851 
01852 //============================================================================
01856 //============================================================================
01857 bool SALOMEDSImpl_Study::RenameVariable(const std::string& theVarName, const std::string& theNewVarName)
01858 {
01859   SALOMEDSImpl_GenericVariable* aVariable = GetVariable( theVarName );
01860   if( !aVariable )
01861     return false;
01862 
01863   ReplaceVariableAttribute( theVarName, theNewVarName );
01864 
01865   std::vector<SALOMEDSImpl_GenericVariable*>::iterator it = myNoteBookVars.begin(), itEnd = myNoteBookVars.end();
01866   for( ; it != itEnd; it++ )
01867   {
01868     SALOMEDSImpl_GenericVariable* aVariableRef = *it;
01869     if( aVariableRef && theVarName.compare( aVariableRef->Name() ) == 0 )
01870     {
01871       aVariableRef->setName( theNewVarName );
01872       Modify();
01873       break;
01874     }
01875   }
01876 
01877   return true;
01878 }
01879 
01880 //============================================================================
01884 //============================================================================
01885 bool SALOMEDSImpl_Study::IsVariableUsed(const std::string& theVarName)
01886 {
01887   return FindVariableAttribute( theVarName );
01888 }
01889 
01890 //============================================================================
01894 //============================================================================
01895 bool SALOMEDSImpl_Study::FindVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
01896                                                SALOMEDSImpl_SObject theSObject,
01897                                                const std::string& theName)
01898 {
01899   SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
01900   for( ; anIter.More(); anIter.Next() )
01901     if( FindVariableAttribute( theStudyBuilder, anIter.Value(), theName ) )
01902       return true;
01903 
01904   DF_Attribute* anAttr;
01905   if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
01906   {
01907     if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
01908     {
01909       std::string aString = aStringAttr->Value();
01910 
01911       std::vector< std::vector<std::string> > aSections = ParseVariables( aString );
01912       for( int i = 0, n = aSections.size(); i < n; i++ )
01913       {
01914         std::vector<std::string> aVector = aSections[i];
01915         for( int j = 0, m = aVector.size(); j < m; j++ )
01916         {
01917           std::string aStr = aVector[j];
01918           if( aStr.compare( theName ) == 0 )
01919             return true;
01920         }
01921       }
01922     }
01923   }
01924   return false;
01925 }
01926 
01927 //============================================================================
01931 //============================================================================
01932 bool SALOMEDSImpl_Study::FindVariableAttribute(const std::string& theName)
01933 {
01934   SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
01935   SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
01936   for( ; aCompIter.More(); aCompIter.Next() )
01937   {
01938     SALOMEDSImpl_SObject aComp = aCompIter.Value();
01939     if( FindVariableAttribute( aStudyBuilder, aComp, theName ) )
01940       return true;
01941   }
01942   return false;
01943 }
01944 
01945 //============================================================================
01949 //============================================================================
01950 void SALOMEDSImpl_Study::ReplaceVariableAttribute(SALOMEDSImpl_StudyBuilder* theStudyBuilder,
01951                                                   SALOMEDSImpl_SObject theSObject,
01952                                                   const std::string& theSource,
01953                                                   const std::string& theDest)
01954 {
01955   SALOMEDSImpl_ChildIterator anIter = NewChildIterator( theSObject );
01956   for( ; anIter.More(); anIter.Next() )
01957     ReplaceVariableAttribute( theStudyBuilder, anIter.Value(), theSource, theDest );
01958 
01959   DF_Attribute* anAttr;
01960   if( theStudyBuilder->FindAttribute( theSObject, anAttr, "AttributeString" ) )
01961   {
01962     if( SALOMEDSImpl_AttributeString* aStringAttr = ( SALOMEDSImpl_AttributeString* )anAttr )
01963     {
01964       bool isChanged = false;
01965       std::string aNewString, aCurrentString = aStringAttr->Value();
01966 
01967       std::vector< std::vector<std::string> > aSections = ParseVariables( aCurrentString );
01968       for( int i = 0, n = aSections.size(); i < n; i++ )
01969       {
01970         std::vector<std::string> aVector = aSections[i];
01971         for( int j = 0, m = aVector.size(); j < m; j++ )
01972         {
01973           std::string aStr = aVector[j];
01974           if( aStr.compare( theSource ) == 0 )
01975           {
01976             isChanged = true;
01977             aStr = theDest;
01978           }
01979 
01980           aNewString.append( aStr );
01981           if( j != m - 1 )
01982             aNewString.append( ":" );
01983         }
01984         if( i != n - 1 )
01985           aNewString.append( "|" );
01986       }
01987 
01988       if( isChanged )
01989         aStringAttr->SetValue( aNewString );
01990     }
01991   }
01992 }
01993 
01994 //============================================================================
01998 //============================================================================
01999 void SALOMEDSImpl_Study::ReplaceVariableAttribute(const std::string& theSource, const std::string& theDest)
02000 {
02001   SALOMEDSImpl_StudyBuilder* aStudyBuilder = NewBuilder();
02002   SALOMEDSImpl_SComponentIterator aCompIter = NewComponentIterator();
02003   for( ; aCompIter.More(); aCompIter.Next() )
02004   {
02005     SALOMEDSImpl_SObject aComp = aCompIter.Value();
02006     ReplaceVariableAttribute( aStudyBuilder, aComp, theSource, theDest );
02007   }
02008 }
02009 
02010 //============================================================================
02014 //============================================================================
02015 std::vector< std::vector< std::string > > SALOMEDSImpl_Study::ParseVariables(const std::string& theVariables) const
02016 {
02017   return SALOMEDSImpl_Tool::splitStringWithEmpty( theVariables, OPERATION_SEPARATOR, VARIABLE_SEPARATOR );
02018 }
02019 
02020 //============================================================================
02024 //============================================================================
02025 void SALOMEDSImpl_Study::EnableUseCaseAutoFilling(bool isEnabled)
02026 {
02027   _errorCode = ""; _autoFill = isEnabled;
02028   if(isEnabled) {
02029     _builder->SetOnAddSObject(_cb);
02030     _builder->SetOnRemoveSObject(_cb);
02031   }
02032   else {
02033     _builder->SetOnAddSObject(NULL);
02034     _builder->SetOnRemoveSObject(NULL);
02035   }
02036 }
02037 
02038 //============================================================================
02042 //============================================================================
02043 std::vector<std::string> SALOMEDSImpl_Study::GetIORs()
02044 {
02045   std::vector<std::string> anIORs;
02046   std::map<std::string, DF_Label>::const_iterator MI;
02047   for(MI = myIORLabels.begin(); MI!=myIORLabels.end(); MI++)
02048     anIORs.push_back(MI->first);
02049 
02050   return anIORs;
02051 }
02052 
02053 //============================================================================
02057 //============================================================================
02058 bool SALOMEDSImpl_Study::addSO_Notification (const SALOMEDSImpl_SObject& theSObject)
02059 {
02060   if(_notifier)
02061     return _notifier->addSO_Notification(theSObject);
02062   else
02063     return false;
02064 }
02065 
02066 //============================================================================
02070 //============================================================================
02071 bool SALOMEDSImpl_Study::removeSO_Notification (const SALOMEDSImpl_SObject& theSObject)
02072 {
02073   if(_notifier)
02074     return _notifier->removeSO_Notification(theSObject);
02075   else
02076     return false;
02077 }
02078 
02079 //============================================================================
02084 //============================================================================
02085 bool SALOMEDSImpl_Study::modifySO_Notification (const SALOMEDSImpl_SObject& theSObject, int reason) 
02086 {
02087   if(_notifier)
02088     return _notifier->modifySO_Notification(theSObject, reason);
02089   else
02090     return false;
02091 }
02092 
02093 //============================================================================
02097 //============================================================================
02098 void SALOMEDSImpl_Study::setNotifier(SALOMEDSImpl_AbstractCallback* notifier) 
02099 {
02100   _notifier=notifier;
02101 }