1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SALOMEDSImpl_IParameters.hxx"
24 #include <utilities.h>
26 #include "SALOMEDSImpl_SObject.hxx"
27 #include "SALOMEDSImpl_ChildIterator.hxx"
29 #define _AP_LISTS_LIST_ "AP_LISTS_LIST"
30 #define _AP_ENTRIES_LIST_ "AP_ENTRIES_LIST"
31 #define _AP_PROPERTIES_LIST_ "AP_PROPERTIES_LIST"
32 #define _AP_DUMP_PYTHON_ "AP_DUMP_PYTHON"
37 SALOMEDSImpl_IParameters::SALOMEDSImpl_IParameters(SALOMEDSImpl_AttributeParameter* ap)
41 SALOMEDSImpl_SObject so = _ap->GetSObject();
42 _study = so.GetStudy();
45 SALOMEDSImpl_IParameters::~SALOMEDSImpl_IParameters()
50 int SALOMEDSImpl_IParameters::append(const std::string& listName, const std::string& value)
53 std::vector<std::string> v;
54 if(!_ap->IsSet(listName, PT_STRARRAY)) {
55 if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) _ap->SetStrArray(_AP_LISTS_LIST_, v);
56 if(listName != _AP_ENTRIES_LIST_ &&
57 listName != _AP_PROPERTIES_LIST_) {
58 append(_AP_LISTS_LIST_, listName);
60 _ap->SetStrArray(listName, v);
62 v = _ap->GetStrArray(listName);
64 _ap->SetStrArray(listName, v);
68 int SALOMEDSImpl_IParameters::nbValues(const std::string& listName)
71 if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
72 std::vector<std::string> v = _ap->GetStrArray(listName);
76 std::vector<std::string> SALOMEDSImpl_IParameters::getValues(const std::string& listName)
78 std::vector<std::string> v;
80 if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
81 return _ap->GetStrArray(listName);
85 std::string SALOMEDSImpl_IParameters::getValue(const std::string& listName, int index)
88 if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
89 std::vector<std::string> v = _ap->GetStrArray(listName);
90 if(index >= v.size()) return "";
94 std::vector<std::string> SALOMEDSImpl_IParameters::getLists()
96 std::vector<std::string> v;
97 if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
98 return _ap->GetStrArray(_AP_LISTS_LIST_);
101 void SALOMEDSImpl_IParameters::setParameter(const std::string& entry, const std::string& parameterName, const std::string& value)
104 std::vector<std::string> v;
105 if(!_ap->IsSet(entry, PT_STRARRAY)) {
106 append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
107 _ap->SetStrArray(entry, v);
109 v = _ap->GetStrArray(entry);
110 v.push_back(parameterName);
112 _ap->SetStrArray(entry, v);
116 std::string SALOMEDSImpl_IParameters::getParameter(const std::string& entry, const std::string& parameterName)
119 if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
120 std::vector<std::string> v = _ap->GetStrArray(entry);
121 int length = v.size();
122 for(int i = 0; i<length; i+=1) {
123 if(v[i] == parameterName) return v[i+1];
129 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterNames(const std::string& entry)
131 std::vector<std::string> v, names;
133 if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
134 v = _ap->GetStrArray(entry);
135 int length = v.size();
136 for(int i = 0; i<length; i+=2) {
137 names.push_back(v[i]);
142 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterValues(const std::string& entry)
144 std::vector<std::string> v, values;
146 if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
147 v = _ap->GetStrArray(entry);
148 int length = v.size();
149 for(int i = 1; i<length; i+=2) {
150 values.push_back(v[i]);
155 int SALOMEDSImpl_IParameters::getNbParameters(const std::string& entry)
158 if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
159 return _ap->GetStrArray(entry).size()/2;
162 std::vector<std::string> SALOMEDSImpl_IParameters::getEntries()
164 std::vector<std::string> v;
166 if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
167 return _ap->GetStrArray(_AP_ENTRIES_LIST_);
170 void SALOMEDSImpl_IParameters::setProperty(const std::string& name, const std::string& value)
173 if(!_ap->IsSet(name, PT_STRING)) {
174 append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
176 _ap->SetString(name, value);
179 std::string SALOMEDSImpl_IParameters::getProperty(const std::string& name)
182 if(!_ap->IsSet(name, PT_STRING)) return "";
183 return _ap->GetString(name);
186 std::vector<std::string> SALOMEDSImpl_IParameters::getProperties()
188 std::vector<std::string> v;
190 if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
191 return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
194 std::string SALOMEDSImpl_IParameters::decodeEntry(const std::string& entry)
196 if(!_study) return entry;
197 int pos = entry.rfind("_");
198 if(pos < 0 || pos >= entry.size()) return entry;
200 std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
202 if(_compNames.find(compName) == _compNames.end()) {
203 SALOMEDSImpl_SObject so = _study->FindComponent(compName);
204 if(!so) return entry;
206 _compNames[compName] = compID;
208 else compID = _compNames[compName];
210 std::string newEntry(compID);
211 newEntry += (":"+tail);
217 bool SALOMEDSImpl_IParameters::isDumpPython(SALOMEDSImpl_Study* study, const std::string& theID)
220 if(theID == "") anID = getDefaultVisualComponent();
223 SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), 0);
224 if(!ap) return false;
225 if(!ap->IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN)) return false;
226 return (bool)ap->GetBool(_AP_DUMP_PYTHON_);
230 int SALOMEDSImpl_IParameters::getLastSavePoint(SALOMEDSImpl_Study* study, const std::string& theID)
233 if(theID == "") anID = getDefaultVisualComponent();
237 SALOMEDSImpl_SObject so = study->FindComponent(anID);
240 SALOMEDSImpl_StudyBuilder* builder = study->NewBuilder();
241 SALOMEDSImpl_ChildIterator anIter = study->NewChildIterator( so );
243 for(; anIter.More(); anIter.Next())
245 SALOMEDSImpl_SObject val( anIter.Value() );
246 DF_Attribute* genAttr;
247 if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val.Tag();
255 std::string SALOMEDSImpl_IParameters::getStudyScript(SALOMEDSImpl_Study* study, int savePoint, const std::string& theID)
258 if(theID == "") anID = getDefaultVisualComponent();
261 SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
262 SALOMEDSImpl_IParameters ip(ap);
264 std::string dump("");
266 dump += "import iparameters\n";
267 dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1))\n\n";
270 std::vector<std::string> v = ip.getProperties();
272 dump += "#Set up visual properties:\n";
273 for(int i = 0; i<v.size(); i++) {
274 std::string prp = ip.getProperty(v[i]);
275 dump += "ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
281 dump += "#Set up lists:\n";
282 for(int i = 0; i<v.size(); i++) {
283 std::vector<std::string> lst = ip.getValues(v[i]);
284 dump += "# fill list "+v[i]+"\n";
285 for(int j = 0; j < lst.size(); j++) {
286 if (lst[j].find('\"') == -1)
287 dump += "ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
289 dump += "ipar.append(\""+v[i]+"\", \"\"\""+lst[j]+"\"\"\")\n";
297 std::string SALOMEDSImpl_IParameters::getDefaultScript(SALOMEDSImpl_Study* study,
298 const std::string& moduleName,
299 const std::string& shift,
300 const std::string& theID)
303 if(theID == "") anID = getDefaultVisualComponent();
306 std::string dump("");
308 int savePoint = SALOMEDSImpl_IParameters::getLastSavePoint(study, anID);
309 if(savePoint < 0) return dump;
310 SALOMEDSImpl_IParameters ip = SALOMEDSImpl_IParameters(study->GetCommonParameters(anID.c_str(), savePoint));
311 if(!isDumpPython(study)) return dump;
313 SALOMEDSImpl_AttributeParameter* ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
314 ip = SALOMEDSImpl_IParameters(ap);
317 dump += shift +"import iparameters\n";
318 dump += shift + "ipar = iparameters.IParameters(theStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
320 std::vector<std::string> v = ip.getProperties();
322 dump += shift +"#Set up visual properties:\n";
323 for(int i = 0; i<v.size(); i++) {
324 std::string prp = ip.getProperty(v[i]);
325 dump += shift +"ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
331 dump += shift +"#Set up lists:\n";
332 for(int i = 0; i<v.size(); i++) {
333 std::vector<std::string> lst = ip.getValues(v[i]);
334 dump += shift +"# fill list "+v[i]+"\n";
335 for(int j = 0; j < lst.size(); j++)
336 dump += shift +"ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
342 dump += shift + "#Set up entries:\n";
343 for(int i = 0; i<v.size(); i++) {
344 std::vector<std::string> names = ip.getAllParameterNames(v[i]);
345 std::vector<std::string> values = ip.getAllParameterValues(v[i]);
346 std::string decodedEntry = ip.decodeEntry(v[i]);
347 SALOMEDSImpl_SObject so = study->FindObjectID(decodedEntry);
348 std::string so_name("");
349 if(so) so_name = so.GetName();
350 dump += shift + "# set up entry " + v[i] +" ("+so_name+")" + " parameters" + "\n";
351 for(int j = 0; j < names.size() && j < values.size(); j++)
352 dump += shift + "ipar.setParameter(\"" + v[i] + "\", \"" + names[j] + "\", \"" + values[j] + "\")\n";
360 std::string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
362 return "Interface Applicative";