Back to index

salome-kernel  6.5.0
SALOMEDS_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 "SALOMEDS_IParameters.hxx"
00024 #include <utilities.h>
00025 
00026 #define PT_INTEGER   0
00027 #define PT_REAL      1
00028 #define PT_BOOLEAN   2
00029 #define PT_STRING    3
00030 #define PT_REALARRAY 4
00031 #define PT_INTARRAY  5
00032 #define PT_STRARRAY  6
00033 
00034 #define _AP_LISTS_LIST_ "AP_LISTS_LIST"
00035 #define _AP_ENTRIES_LIST_ "AP_ENTRIES_LIST"
00036 #define _AP_PROPERTIES_LIST_ "AP_PROPERTIES_LIST"
00037 #define _AP_DUMP_PYTHON_ "AP_DUMP_PYTHON"
00038 
00039 #define _PT_ID_ "_PT_OBJECT_ID_"
00040 
00041 
00045 SALOMEDS_IParameters::SALOMEDS_IParameters(const _PTR(AttributeParameter)& ap)
00046 {
00047   if(!ap) return;
00048   _ap = ap;
00049   _PTR(SObject) so = _ap->GetSObject();
00050   _study = so->GetStudy();
00051 }
00052 
00053 SALOMEDS_IParameters::~SALOMEDS_IParameters()
00054 {
00055   _compNames.clear();
00056 }
00057 
00058 int SALOMEDS_IParameters::append(const std::string& listName, const std::string& value)
00059 {
00060   if(!_ap) return -1;
00061   std::vector<std::string> v;
00062   if(!_ap->IsSet(listName, PT_STRARRAY)) {
00063     if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) _ap->SetStrArray(_AP_LISTS_LIST_, v);
00064     if(listName != _AP_ENTRIES_LIST_ && 
00065        listName != _AP_PROPERTIES_LIST_) {
00066       append(_AP_LISTS_LIST_, listName);
00067     }
00068     _ap->SetStrArray(listName, v);
00069   }
00070   v = _ap->GetStrArray(listName);
00071   v.push_back(value);
00072   _ap->SetStrArray(listName, v);
00073   return (v.size()-1);
00074 }
00075 
00076 int SALOMEDS_IParameters::nbValues(const std::string& listName)
00077 {
00078   if(!_ap) return -1;
00079   if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
00080   std::vector<std::string> v = _ap->GetStrArray(listName);
00081   return v.size();
00082 }
00083 
00084 std::vector<std::string> SALOMEDS_IParameters::getValues(const std::string& listName)
00085 {
00086   std::vector<std::string> v;
00087   if(!_ap) return v;
00088   if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
00089   return _ap->GetStrArray(listName);
00090 }
00091 
00092 
00093 std::string SALOMEDS_IParameters::getValue(const std::string& listName, int index)
00094 {
00095   if(!_ap) return "";
00096   if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
00097   std::vector<std::string> v = _ap->GetStrArray(listName);
00098   if(index >= v.size()) return ""; 
00099   return v[index];
00100 }
00101 
00102 std::vector<std::string> SALOMEDS_IParameters::getLists()
00103 {
00104   std::vector<std::string> v;
00105   if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
00106   return _ap->GetStrArray(_AP_LISTS_LIST_);
00107 }
00108 
00109 void SALOMEDS_IParameters::setParameter(const std::string& entry, const std::string& parameterName, const std::string& value)
00110 {
00111   if(!_ap) return;
00112   std::vector<std::string> v;
00113   if(!_ap->IsSet(entry, PT_STRARRAY)) {
00114     append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
00115     _ap->SetStrArray(entry, v);
00116   }
00117   v = _ap->GetStrArray(entry);
00118   v.push_back(parameterName);
00119   v.push_back(value);
00120   _ap->SetStrArray(entry, v);
00121 }
00122 
00123 
00124 std::string SALOMEDS_IParameters::getParameter(const std::string& entry, const std::string& parameterName)
00125 {
00126   if(!_ap) return "";
00127   if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
00128   std::vector<std::string> v = _ap->GetStrArray(entry);
00129   int length = v.size();
00130   for(int i = 0; i<length; i+=1) {
00131     if(v[i] == parameterName) return v[i+1];
00132   }
00133   return "";
00134 }
00135 
00136 
00137 std::vector<std::string> SALOMEDS_IParameters::getAllParameterNames(const std::string& entry)
00138 {
00139   std::vector<std::string> v, names;
00140   if(!_ap) return v; 
00141   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
00142   v = _ap->GetStrArray(entry);
00143   int length = v.size();
00144   for(int i = 0; i<length; i+=2) {
00145     names.push_back(v[i]);
00146   }
00147   return names;
00148 }
00149 
00150 
00151 std::string SALOMEDS_IParameters::getIdParameter(const std::string& entry)
00152 {
00153   if(!_ap) return "";
00154   if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
00155   std::vector<std::string> v = _ap->GetStrArray(entry);
00156   int length = v.size();
00157   for(int i = 0; i<length; i+=1) {
00158     if(v[i] == _PT_ID_) return v[i+1];
00159   }
00160   return "";
00161 }
00162 
00163 void SALOMEDS_IParameters::setIdParameter(const std::string& entry, const std::string& value)
00164 {
00165   if(!_ap) return;
00166   std::vector<std::string> v;
00167   if(!_ap->IsSet(entry, PT_STRARRAY)) {
00168     append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
00169     _ap->SetStrArray(entry, v);
00170   }
00171   v = _ap->GetStrArray(entry);
00172   v.push_back(_PT_ID_);
00173   v.push_back(value);
00174   _ap->SetStrArray(entry, v);
00175 }
00176 
00177 std::vector<std::string> SALOMEDS_IParameters::getAllParameterValues(const std::string& entry)
00178 {
00179   std::vector<std::string> v, values;
00180   if(!_ap) return v; 
00181   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
00182   v = _ap->GetStrArray(entry);
00183   int length = v.size();
00184   for(int i = 1; i<length; i+=2) {
00185     values.push_back(v[i]);
00186   }
00187   return values; 
00188 }
00189 
00190 int SALOMEDS_IParameters::getNbParameters(const std::string& entry)
00191 {
00192   if(!_ap) return -1;
00193   if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
00194   return  _ap->GetStrArray(entry).size()/2;
00195 }
00196 
00197 std::vector<std::string> SALOMEDS_IParameters::getEntries()
00198 {
00199   std::vector<std::string> v;
00200   if(!_ap) return v;
00201   if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
00202   return _ap->GetStrArray(_AP_ENTRIES_LIST_);
00203 }
00204 
00205 void SALOMEDS_IParameters::setProperty(const std::string& name, const std::string& value)
00206 {
00207   if(!_ap) return;
00208   if(!_ap->IsSet(name, PT_STRING)) {
00209     append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
00210   }
00211   _ap->SetString(name, value);
00212 }
00213 
00214 std::string SALOMEDS_IParameters::getProperty(const std::string& name)
00215 {
00216   if(!_ap) return "";
00217   if(!_ap->IsSet(name, PT_STRING)) return "";
00218   return _ap->GetString(name);
00219 }
00220 
00221 std::vector<std::string> SALOMEDS_IParameters::getProperties()
00222 {
00223   std::vector<std::string> v;
00224   if(!_ap) return v;
00225   if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
00226   return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
00227 }
00228 
00229 
00230 std::vector<std::string> SALOMEDS_IParameters::parseValue(const std::string& value, const char separator, bool fromEnd)
00231 {
00232   std::string val(value);
00233   std::vector<std::string> v;
00234   int pos;
00235   if(fromEnd) pos = val.rfind(separator);
00236   else pos = val.find(separator);
00237 
00238   if(pos < 0) {
00239     v.push_back(value);
00240     return v;
00241   }
00242 
00243   std::string part1, part2;
00244   part1 = val.substr(0, pos);
00245   part2 = val.substr(pos+1, val.size());
00246   v.push_back(part1);
00247   v.push_back(part2);
00248   return v;
00249 }
00250 
00251 std::string SALOMEDS_IParameters::encodeEntry(const std::string& entry, const std::string& compName)
00252 {
00253   std::string tail(entry, 6, entry.length()-1);
00254   std::string newEntry(compName);
00255   newEntry+=("_"+tail);
00256   return newEntry;
00257 }
00258 
00259 std::string SALOMEDS_IParameters::decodeEntry(const std::string& entry)
00260 {
00261   if(!_study) return entry;
00262   int pos = entry.rfind("_");
00263   if(pos < 0 || pos >= entry.length()) return entry;
00264 
00265   std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
00266   
00267   if(_compNames.find(compName) == _compNames.end()) {
00268     _PTR(SObject) so = _study->FindComponent(compName);
00269     if(!so) return entry;
00270     compID = so->GetID();
00271     _compNames[compName] = compID;
00272   }
00273   else compID = _compNames[compName];
00274  
00275   std::string newEntry(compID);
00276   newEntry += (":"+tail);
00277   
00278   return newEntry;
00279 }
00280 
00281 void SALOMEDS_IParameters::setDumpPython(_PTR(Study) study, const std::string& theID)
00282 {
00283   std::string anID;
00284   if(theID == "") anID = getDefaultVisualComponent();
00285   else anID = theID;
00286 
00287   _PTR(AttributeParameter) ap = study->GetCommonParameters(anID, 0);
00288   ap->SetBool(_AP_DUMP_PYTHON_, !isDumpPython(study, theID));
00289 }
00290 
00291 bool SALOMEDS_IParameters::isDumpPython(_PTR(Study) study, const std::string& theID)
00292 {
00293   std::string anID;
00294   if(theID == "") anID = getDefaultVisualComponent();
00295   else anID = theID;
00296 
00297   _PTR(AttributeParameter) ap = study->GetCommonParameters(anID, 0);
00298   if(!ap) return false;
00299   if(!ap->IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN)) return false;
00300   return (bool)ap->GetBool(_AP_DUMP_PYTHON_);
00301 }
00302 
00303 std::string SALOMEDS_IParameters::getDefaultVisualComponent()
00304 {
00305   return "Interface Applicative";
00306 }
00307 
00308 
00309