1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "SALOMEDSImpl_IParameters.hxx"
20 #include <utilities.h>
22 #include "SALOMEDSImpl_SObject.hxx"
23 #include "SALOMEDSImpl_ChildIterator.hxx"
27 #define _AP_LISTS_LIST_ "AP_LISTS_LIST"
28 #define _AP_ENTRIES_LIST_ "AP_ENTRIES_LIST"
29 #define _AP_PROPERTIES_LIST_ "AP_PROPERTIES_LIST"
30 #define _AP_DUMP_PYTHON_ "AP_DUMP_PYTHON"
35 SALOMEDSImpl_IParameters::SALOMEDSImpl_IParameters(SALOMEDSImpl_AttributeParameter* ap)
39 SALOMEDSImpl_SObject so = _ap->GetSObject();
40 _study = so.GetStudy();
43 SALOMEDSImpl_IParameters::~SALOMEDSImpl_IParameters()
48 int SALOMEDSImpl_IParameters::append(const string& listName, const string& value)
52 if(!_ap->IsSet(listName, PT_STRARRAY)) {
53 if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) _ap->SetStrArray(_AP_LISTS_LIST_, v);
54 if(listName != _AP_ENTRIES_LIST_ &&
55 listName != _AP_PROPERTIES_LIST_) {
56 append(_AP_LISTS_LIST_, listName);
58 _ap->SetStrArray(listName, v);
60 v = _ap->GetStrArray(listName);
62 _ap->SetStrArray(listName, v);
66 int SALOMEDSImpl_IParameters::nbValues(const string& listName)
69 if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
70 vector<string> v = _ap->GetStrArray(listName);
74 vector<string> SALOMEDSImpl_IParameters::getValues(const string& listName)
78 if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
79 return _ap->GetStrArray(listName);
83 string SALOMEDSImpl_IParameters::getValue(const string& listName, int index)
86 if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
87 vector<string> v = _ap->GetStrArray(listName);
88 if(index >= v.size()) return "";
92 vector<string> SALOMEDSImpl_IParameters::getLists()
95 if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
96 return _ap->GetStrArray(_AP_LISTS_LIST_);
99 void SALOMEDSImpl_IParameters::setParameter(const string& entry, const string& parameterName, const string& value)
103 if(!_ap->IsSet(entry, PT_STRARRAY)) {
104 append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
105 _ap->SetStrArray(entry, v);
107 v = _ap->GetStrArray(entry);
108 v.push_back(parameterName);
110 _ap->SetStrArray(entry, v);
114 string SALOMEDSImpl_IParameters::getParameter(const string& entry, const string& parameterName)
117 if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
118 vector<string> v = _ap->GetStrArray(entry);
119 int length = v.size();
120 for(int i = 0; i<length; i+=1) {
121 if(v[i] == parameterName) return v[i+1];
127 vector<string> SALOMEDSImpl_IParameters::getAllParameterNames(const string& entry)
129 vector<string> v, names;
131 if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
132 v = _ap->GetStrArray(entry);
133 int length = v.size();
134 for(int i = 0; i<length; i+=2) {
135 names.push_back(v[i]);
140 vector<string> SALOMEDSImpl_IParameters::getAllParameterValues(const string& entry)
142 vector<string> v, values;
144 if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
145 v = _ap->GetStrArray(entry);
146 int length = v.size();
147 for(int i = 1; i<length; i+=2) {
148 values.push_back(v[i]);
153 int SALOMEDSImpl_IParameters::getNbParameters(const string& entry)
156 if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
157 return _ap->GetStrArray(entry).size()/2;
160 vector<string> SALOMEDSImpl_IParameters::getEntries()
164 if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
165 return _ap->GetStrArray(_AP_ENTRIES_LIST_);
168 void SALOMEDSImpl_IParameters::setProperty(const string& name, const std::string& value)
171 if(!_ap->IsSet(name, PT_STRING)) {
172 append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
174 _ap->SetString(name, value);
177 string SALOMEDSImpl_IParameters::getProperty(const string& name)
180 if(!_ap->IsSet(name, PT_STRING)) return "";
181 return _ap->GetString(name);
184 vector<string> SALOMEDSImpl_IParameters::getProperties()
188 if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
189 return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
192 string SALOMEDSImpl_IParameters::decodeEntry(const string& entry)
194 if(!_study) return entry;
195 int pos = entry.rfind("_");
196 if(pos < 0 || pos >= entry.size()) return entry;
198 string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
200 if(_compNames.find(compName) == _compNames.end()) {
201 SALOMEDSImpl_SObject so = _study->FindComponent(compName);
202 if(!so) return entry;
204 _compNames[compName] = compID;
206 else compID = _compNames[compName];
208 string newEntry(compID);
209 newEntry += (":"+tail);
215 bool SALOMEDSImpl_IParameters::isDumpPython(SALOMEDSImpl_Study* study, const string& theID)
218 if(theID == "") anID = getDefaultVisualComponent();
221 SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), 0);
222 if(!ap) return false;
223 if(!ap->IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN)) return false;
224 return (bool)ap->GetBool(_AP_DUMP_PYTHON_);
228 int SALOMEDSImpl_IParameters::getLastSavePoint(SALOMEDSImpl_Study* study, const string& theID)
231 if(theID == "") anID = getDefaultVisualComponent();
235 SALOMEDSImpl_SObject so = study->FindComponent(anID);
238 SALOMEDSImpl_StudyBuilder* builder = study->NewBuilder();
239 SALOMEDSImpl_ChildIterator anIter = study->NewChildIterator( so );
241 for(; anIter.More(); anIter.Next())
243 SALOMEDSImpl_SObject val( anIter.Value() );
244 DF_Attribute* genAttr;
245 if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val.Tag();
253 string SALOMEDSImpl_IParameters::getStudyScript(SALOMEDSImpl_Study* study, int savePoint, const std::string& theID)
256 if(theID == "") anID = getDefaultVisualComponent();
259 SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
260 SALOMEDSImpl_IParameters ip(ap);
264 dump += "import iparameters\n";
265 dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1))\n\n";
268 vector<string> v = ip.getProperties();
270 dump += "#Set up visual properties:\n";
271 for(int i = 0; i<v.size(); i++) {
272 string prp = ip.getProperty(v[i]);
273 dump += "ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
279 dump += "#Set up lists:\n";
280 for(int i = 0; i<v.size(); i++) {
281 vector<string> lst = ip.getValues(v[i]);
282 dump += "# fill list "+v[i]+"\n";
283 for(int j = 0; j < lst.size(); j++)
284 dump += "ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
291 string SALOMEDSImpl_IParameters::getDefaultScript(SALOMEDSImpl_Study* study,
292 const string& moduleName,
297 if(theID == "") anID = getDefaultVisualComponent();
302 int savePoint = SALOMEDSImpl_IParameters::getLastSavePoint(study, anID);
303 if(savePoint < 0) return dump;
304 SALOMEDSImpl_IParameters ip = SALOMEDSImpl_IParameters(study->GetCommonParameters(anID.c_str(), savePoint));
305 if(!isDumpPython(study)) return dump;
307 SALOMEDSImpl_AttributeParameter* ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
308 ip = SALOMEDSImpl_IParameters(ap);
311 dump += shift +"import iparameters\n";
312 dump += shift + "ipar = iparameters.IParameters(theStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
314 vector<string> v = ip.getProperties();
316 dump += shift +"#Set up visual properties:\n";
317 for(int i = 0; i<v.size(); i++) {
318 string prp = ip.getProperty(v[i]);
319 dump += shift +"ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
325 dump += shift +"#Set up lists:\n";
326 for(int i = 0; i<v.size(); i++) {
327 vector<string> lst = ip.getValues(v[i]);
328 dump += shift +"# fill list "+v[i]+"\n";
329 for(int j = 0; j < lst.size(); j++)
330 dump += shift +"ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
336 dump += shift + "#Set up entries:\n";
337 for(int i = 0; i<v.size(); i++) {
338 vector<string> names = ip.getAllParameterNames(v[i]);
339 vector<string> values = ip.getAllParameterValues(v[i]);
340 string decodedEntry = ip.decodeEntry(v[i]);
341 SALOMEDSImpl_SObject so = study->FindObjectID(decodedEntry);
343 if(so) so_name = so.GetName();
344 dump += shift + "# set up entry " + v[i] +" ("+so_name+")" + " parameters" + "\n";
345 for(int j = 0; j < names.size() && j < values.size(); j++)
346 dump += shift + "ipar.setParameter(\"" + v[i] + "\", \"" + names[j] + "\", \"" + values[j] + "\")\n";
354 string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
356 return "Interface Applicative";