Back to index

salome-kernel  6.5.0
SALOMEDSImpl_IParameters.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 #include "SALOMEDSImpl_IParameters.hxx"
00024 #include <utilities.h>
00025 
00026 #include "SALOMEDSImpl_SObject.hxx"
00027 #include "SALOMEDSImpl_ChildIterator.hxx"
00028 
00029 #define _AP_LISTS_LIST_ "AP_LISTS_LIST"
00030 #define _AP_ENTRIES_LIST_ "AP_ENTRIES_LIST"
00031 #define _AP_PROPERTIES_LIST_ "AP_PROPERTIES_LIST"
00032 #define _AP_DUMP_PYTHON_ "AP_DUMP_PYTHON"
00033 
00034 #define _PT_ID_ "_PT_OBJECT_ID_"
00035 
00039 SALOMEDSImpl_IParameters::SALOMEDSImpl_IParameters(SALOMEDSImpl_AttributeParameter* ap)
00040 {
00041   if(!ap) return;
00042   _ap = ap;
00043   SALOMEDSImpl_SObject so = _ap->GetSObject();
00044   _study = so.GetStudy();
00045 }
00046 
00047 SALOMEDSImpl_IParameters::~SALOMEDSImpl_IParameters()
00048 {
00049   _compNames.clear();
00050 }
00051 
00052 int SALOMEDSImpl_IParameters::append(const std::string& listName, const std::string& value)
00053 {
00054   if(!_ap) return -1;
00055   std::vector<std::string> v;
00056   if(!_ap->IsSet(listName, PT_STRARRAY)) {
00057     if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) _ap->SetStrArray(_AP_LISTS_LIST_, v);
00058     if(listName != _AP_ENTRIES_LIST_ && 
00059        listName != _AP_PROPERTIES_LIST_) {
00060       append(_AP_LISTS_LIST_, listName);
00061     }
00062     _ap->SetStrArray(listName, v);
00063   }
00064   v = _ap->GetStrArray(listName);
00065   v.push_back(value);
00066   _ap->SetStrArray(listName, v);
00067   return (v.size()-1);
00068 }
00069 
00070 int SALOMEDSImpl_IParameters::nbValues(const std::string& listName)
00071 {
00072   if(!_ap) return -1;
00073   if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
00074   std::vector<std::string> v = _ap->GetStrArray(listName);
00075   return v.size();
00076 }
00077 
00078 std::vector<std::string> SALOMEDSImpl_IParameters::getValues(const std::string& listName)
00079 {
00080   std::vector<std::string> v;
00081   if(!_ap) return v;
00082   if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
00083   return _ap->GetStrArray(listName);
00084 }
00085 
00086 
00087 std::string SALOMEDSImpl_IParameters::getValue(const std::string& listName, int index)
00088 {
00089   if(!_ap) return "";
00090   if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
00091   std::vector<std::string> v = _ap->GetStrArray(listName);
00092   if(index >= v.size()) return ""; 
00093   return v[index];
00094 }
00095 
00096 std::vector<std::string> SALOMEDSImpl_IParameters::getLists()
00097 {
00098   std::vector<std::string> v;
00099   if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
00100   return _ap->GetStrArray(_AP_LISTS_LIST_);
00101 }
00102 
00103 void SALOMEDSImpl_IParameters::setParameter(const std::string& entry, const std::string& parameterName, const std::string& value)
00104 {
00105   if(!_ap) return;
00106   std::vector<std::string> v;
00107   if(!_ap->IsSet(entry, PT_STRARRAY)) {
00108     append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
00109     _ap->SetStrArray(entry, v);
00110   }
00111   v = _ap->GetStrArray(entry);
00112   v.push_back(parameterName);
00113   v.push_back(value);
00114   _ap->SetStrArray(entry, v);
00115 }
00116 
00117 
00118 std::string SALOMEDSImpl_IParameters::getParameter(const std::string& entry, const std::string& parameterName)
00119 {
00120   if(!_ap) return "";
00121   if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
00122   std::vector<std::string> v = _ap->GetStrArray(entry);
00123   int length = v.size();
00124   for(int i = 0; i<length; i+=1) {
00125     if(v[i] == parameterName) return v[i+1];
00126   }
00127   return "";
00128 }
00129 
00130 
00131 void SALOMEDSImpl_IParameters::setIdParameter(const std::string& entry, const std::string& value)
00132 {
00133   if(!_ap) return;
00134   std::vector<std::string> v;
00135   if(!_ap->IsSet(entry, PT_STRARRAY)) {
00136     append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
00137     _ap->SetStrArray(entry, v);
00138   }
00139   v = _ap->GetStrArray(entry);
00140   v.push_back(_PT_ID_);
00141   v.push_back(value);
00142   _ap->SetStrArray(entry, v);
00143 }
00144 
00145 
00146 std::string SALOMEDSImpl_IParameters::getIdParameter(const std::string& entry)
00147 {
00148   if(!_ap) return "";
00149   if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
00150   std::vector<std::string> v = _ap->GetStrArray(entry);
00151   int length = v.size();
00152   for(int i = 0; i<length; i+=1) {
00153     if(v[i] == _PT_ID_) return v[i+1];
00154   }
00155   return "";
00156 }
00157 
00158 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterNames(const std::string& entry)
00159 {
00160   std::vector<std::string> v, names;
00161   if(!_ap) return v; 
00162   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
00163   v = _ap->GetStrArray(entry);
00164   int length = v.size();
00165   for(int i = 0; i<length; i+=2) {
00166     names.push_back(v[i]);
00167   }
00168   return names;
00169 }
00170 
00171 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterValues(const std::string& entry)
00172 {
00173   std::vector<std::string> v, values;
00174   if(!_ap) return v; 
00175   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
00176   v = _ap->GetStrArray(entry);
00177   int length = v.size();
00178   for(int i = 1; i<length; i+=2) {
00179     values.push_back(v[i]);
00180   }
00181   return values; 
00182 }
00183 
00184 int SALOMEDSImpl_IParameters::getNbParameters(const std::string& entry)
00185 {
00186   if(!_ap) return -1;
00187   if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
00188   return  _ap->GetStrArray(entry).size()/2;
00189 }
00190 
00191 std::vector<std::string> SALOMEDSImpl_IParameters::getEntries()
00192 {
00193   std::vector<std::string> v;
00194   if(!_ap) return v;
00195   if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
00196   return _ap->GetStrArray(_AP_ENTRIES_LIST_);
00197 }
00198 
00199 void SALOMEDSImpl_IParameters::setProperty(const std::string& name, const std::string& value)
00200 {
00201   if(!_ap) return;
00202   if(!_ap->IsSet(name, PT_STRING)) {
00203     append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
00204   }
00205   _ap->SetString(name, value);
00206 }
00207 
00208 std::string SALOMEDSImpl_IParameters::getProperty(const std::string& name)
00209 {
00210   if(!_ap) return "";
00211   if(!_ap->IsSet(name, PT_STRING)) return "";
00212   return _ap->GetString(name);
00213 }
00214 
00215 std::vector<std::string> SALOMEDSImpl_IParameters::getProperties()
00216 {
00217   std::vector<std::string> v;
00218   if(!_ap) return v;
00219   if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
00220   return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
00221 }
00222 
00223 std::string SALOMEDSImpl_IParameters::decodeEntry(const std::string& entry)
00224 {
00225   if(!_study) return entry;
00226   int pos = entry.rfind("_");
00227   if(pos < 0 || pos >= entry.size()) return entry;
00228 
00229   std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
00230   
00231   if(_compNames.find(compName) == _compNames.end()) {
00232     SALOMEDSImpl_SObject so = _study->FindComponent(compName);
00233     if(!so) return entry;
00234     compID = so.GetID();
00235     _compNames[compName] = compID;
00236   }
00237   else compID = _compNames[compName];
00238  
00239   std::string newEntry(compID);
00240   newEntry += (":"+tail);
00241   
00242   return newEntry;
00243 }
00244 
00245 
00246 bool SALOMEDSImpl_IParameters::isDumpPython(SALOMEDSImpl_Study* study, const std::string& theID)
00247 {
00248   std::string anID;
00249   if(theID == "") anID = getDefaultVisualComponent();
00250   else anID = theID;
00251 
00252   SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), 0);
00253   if(!ap) return false;
00254   if(!ap->IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN)) return false;
00255   return (bool)ap->GetBool(_AP_DUMP_PYTHON_);
00256 }
00257 
00258 
00259 int SALOMEDSImpl_IParameters::getLastSavePoint(SALOMEDSImpl_Study* study, const std::string& theID)
00260 {
00261   std::string anID;
00262   if(theID == "") anID = getDefaultVisualComponent();
00263   else anID = theID;
00264 
00265 
00266   SALOMEDSImpl_SObject so = study->FindComponent(anID);
00267   if(!so) return -1;
00268 
00269   SALOMEDSImpl_StudyBuilder* builder = study->NewBuilder();
00270   SALOMEDSImpl_ChildIterator anIter = study->NewChildIterator( so );
00271   int tag = -1;
00272   for(; anIter.More(); anIter.Next())
00273   {
00274     SALOMEDSImpl_SObject val( anIter.Value() );
00275     DF_Attribute* genAttr;
00276     if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val.Tag();
00277   }
00278 
00279   return tag;
00280 }
00281 
00282 
00283 
00284 std::string SALOMEDSImpl_IParameters::getStudyScript(SALOMEDSImpl_Study* study, int savePoint, const std::string& theID)
00285 {
00286   std::string anID;
00287   if(theID == "") anID = getDefaultVisualComponent();
00288   else anID = theID;
00289 
00290   SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
00291   SALOMEDSImpl_IParameters ip(ap);
00292 
00293   std::string dump("");
00294 
00295   dump += "import iparameters\n";
00296   dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1), True)\n\n";
00297   
00298   
00299   std::vector<std::string> v = ip.getProperties();
00300   if(v.size() > 0) {
00301     dump += "#Set up visual properties:\n";
00302     for(int i = 0; i<v.size(); i++) {
00303       std::string prp = ip.getProperty(v[i]);
00304       dump += "ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
00305     }
00306   }
00307 
00308   v = ip.getLists();
00309   if(v.size() > 0) {
00310     dump += "#Set up lists:\n";
00311     for(int i = 0; i<v.size(); i++) {
00312       std::vector<std::string> lst = ip.getValues(v[i]);
00313       dump += "# fill list "+v[i]+"\n";
00314       for(int j = 0; j < lst.size(); j++) {
00315         if (lst[j].find('\"') == -1)
00316           dump += "ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
00317         else
00318           dump += "ipar.append(\""+v[i]+"\", \"\"\""+lst[j]+"\"\"\")\n";
00319       }
00320     }
00321   }
00322 
00323   return dump;
00324 }
00325 
00326 std::string SALOMEDSImpl_IParameters::getDefaultScript(SALOMEDSImpl_Study* study, 
00327                                                   const std::string& moduleName, 
00328                                                   const std::string& shift, 
00329                                                   const std::string& theID)
00330 {
00331   std::string anID;
00332   if(theID == "") anID = getDefaultVisualComponent();
00333   else anID = theID;
00334 
00335   std::string dump("");
00336 
00337   int savePoint = SALOMEDSImpl_IParameters::getLastSavePoint(study, anID);
00338   if(savePoint < 0) return dump;
00339   SALOMEDSImpl_IParameters ip = SALOMEDSImpl_IParameters(study->GetCommonParameters(anID.c_str(), savePoint));
00340   if(!isDumpPython(study)) return dump;
00341 
00342   SALOMEDSImpl_AttributeParameter* ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
00343   ip = SALOMEDSImpl_IParameters(ap);
00344 
00345 
00346   dump += shift +"import iparameters\n";
00347   dump += shift + "ipar = iparameters.IParameters(theStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
00348   
00349   std::vector<std::string> v = ip.getProperties();
00350   if(v.size() > 0) {
00351     dump += shift +"#Set up visual properties:\n";
00352     for(int i = 0; i<v.size(); i++) {
00353       std::string prp = ip.getProperty(v[i]);
00354       dump += shift +"ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
00355     }
00356   }
00357 
00358   v = ip.getLists();
00359   if(v.size() > 0) {
00360     dump +=  shift +"#Set up lists:\n";
00361     for(int i = 0; i<v.size(); i++) {
00362       std::vector<std::string> lst = ip.getValues(v[i]);
00363       dump += shift +"# fill list "+v[i]+"\n";
00364       for(int j = 0; j < lst.size(); j++)
00365         dump += shift +"ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
00366     }
00367   }
00368 
00369   v = ip.getEntries();
00370   if(v.size() > 0) {
00371     dump += shift + "#Set up entries:\n";
00372     for(int i = 0; i<v.size(); i++) {
00373       std::vector<std::string> names = ip.getAllParameterNames(v[i]);
00374       std::vector<std::string> values = ip.getAllParameterValues(v[i]);
00375       std::string decodedEntry = ip.decodeEntry(v[i]);
00376       SALOMEDSImpl_SObject so = study->FindObjectID(decodedEntry);
00377       std::string so_name("");
00378       if(so) so_name = so.GetName();
00379       
00380       //Try to find id parameter 
00381       std::vector<std::string>::iterator it = std::find(names.begin(), names.end(), _PT_ID_ );
00382       bool hasId = it !=  names.end();
00383       bool onlyId = hasId && names.size() == 1;
00384       
00385       if(!onlyId) {
00386        dump += shift + "# set up entry " + v[i] +" ("+so_name+")" + " parameters" + "\n";
00387        if(hasId) {
00388          int idIndex = std::distance(names.begin(), it);
00389          dump += shift + "objId = " + values[idIndex] + "\n";
00390        }
00391           
00392        for(int j = 0; j < names.size() && j < values.size(); j++) {
00393          if(names[j] == _PT_ID_) continue;
00394          if(hasId)
00395            dump += shift + "ipar.setParameter(" + "objId" + ", \"" + names[j] + "\", \"" + values[j] + "\")\n";
00396          else
00397            dump += shift + "ipar.setParameter(\"" + v[i] + "\", \"" + names[j] + "\", \"" + values[j] + "\")\n";
00398        }
00399       }
00400     }
00401   }
00402   
00403   return dump;  
00404 }
00405 
00406 
00407 std::string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
00408 {
00409   return "Interface Applicative";
00410 }
00411 
00412