1 // Copyright (C) 2007-2012 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"
34 #define _PT_ID_ "_PT_OBJECT_ID_"
39 SALOMEDSImpl_IParameters::SALOMEDSImpl_IParameters(SALOMEDSImpl_AttributeParameter* ap)
43 SALOMEDSImpl_SObject so = _ap->GetSObject();
44 _study = so.GetStudy();
47 SALOMEDSImpl_IParameters::~SALOMEDSImpl_IParameters()
52 int SALOMEDSImpl_IParameters::append(const std::string& listName, const std::string& value)
55 std::vector<std::string> v;
56 if(!_ap->IsSet(listName, PT_STRARRAY)) {
57 if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) _ap->SetStrArray(_AP_LISTS_LIST_, v);
58 if(listName != _AP_ENTRIES_LIST_ &&
59 listName != _AP_PROPERTIES_LIST_) {
60 append(_AP_LISTS_LIST_, listName);
62 _ap->SetStrArray(listName, v);
64 v = _ap->GetStrArray(listName);
66 _ap->SetStrArray(listName, v);
70 int SALOMEDSImpl_IParameters::nbValues(const std::string& listName)
73 if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
74 std::vector<std::string> v = _ap->GetStrArray(listName);
78 std::vector<std::string> SALOMEDSImpl_IParameters::getValues(const std::string& listName)
80 std::vector<std::string> v;
82 if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
83 return _ap->GetStrArray(listName);
87 std::string SALOMEDSImpl_IParameters::getValue(const std::string& listName, int index)
90 if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
91 std::vector<std::string> v = _ap->GetStrArray(listName);
92 if(index >= v.size()) return "";
96 std::vector<std::string> SALOMEDSImpl_IParameters::getLists()
98 std::vector<std::string> v;
99 if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
100 return _ap->GetStrArray(_AP_LISTS_LIST_);
103 void SALOMEDSImpl_IParameters::setParameter(const std::string& entry, const std::string& parameterName, const std::string& value)
106 std::vector<std::string> v;
107 if(!_ap->IsSet(entry, PT_STRARRAY)) {
108 append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
109 _ap->SetStrArray(entry, v);
111 v = _ap->GetStrArray(entry);
112 v.push_back(parameterName);
114 _ap->SetStrArray(entry, v);
118 std::string SALOMEDSImpl_IParameters::getParameter(const std::string& entry, const std::string& parameterName)
121 if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
122 std::vector<std::string> v = _ap->GetStrArray(entry);
123 int length = v.size();
124 for(int i = 0; i<length; i+=1) {
125 if(v[i] == parameterName) return v[i+1];
131 void SALOMEDSImpl_IParameters::setIdParameter(const std::string& entry, const std::string& value)
134 std::vector<std::string> v;
135 if(!_ap->IsSet(entry, PT_STRARRAY)) {
136 append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
137 _ap->SetStrArray(entry, v);
139 v = _ap->GetStrArray(entry);
140 v.push_back(_PT_ID_);
142 _ap->SetStrArray(entry, v);
146 std::string SALOMEDSImpl_IParameters::getIdParameter(const std::string& entry)
149 if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
150 std::vector<std::string> v = _ap->GetStrArray(entry);
151 int length = v.size();
152 for(int i = 0; i<length; i+=1) {
153 if(v[i] == _PT_ID_) return v[i+1];
158 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterNames(const std::string& entry)
160 std::vector<std::string> v, names;
162 if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
163 v = _ap->GetStrArray(entry);
164 int length = v.size();
165 for(int i = 0; i<length; i+=2) {
166 names.push_back(v[i]);
171 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterValues(const std::string& entry)
173 std::vector<std::string> v, values;
175 if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
176 v = _ap->GetStrArray(entry);
177 int length = v.size();
178 for(int i = 1; i<length; i+=2) {
179 values.push_back(v[i]);
184 int SALOMEDSImpl_IParameters::getNbParameters(const std::string& entry)
187 if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
188 return _ap->GetStrArray(entry).size()/2;
191 std::vector<std::string> SALOMEDSImpl_IParameters::getEntries()
193 std::vector<std::string> v;
195 if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
196 return _ap->GetStrArray(_AP_ENTRIES_LIST_);
199 void SALOMEDSImpl_IParameters::setProperty(const std::string& name, const std::string& value)
202 if(!_ap->IsSet(name, PT_STRING)) {
203 append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
205 _ap->SetString(name, value);
208 std::string SALOMEDSImpl_IParameters::getProperty(const std::string& name)
211 if(!_ap->IsSet(name, PT_STRING)) return "";
212 return _ap->GetString(name);
215 std::vector<std::string> SALOMEDSImpl_IParameters::getProperties()
217 std::vector<std::string> v;
219 if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
220 return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
223 std::string SALOMEDSImpl_IParameters::decodeEntry(const std::string& entry)
225 if(!_study) return entry;
226 int pos = entry.rfind("_");
227 if(pos < 0 || pos >= entry.size()) return entry;
229 std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
231 if(_compNames.find(compName) == _compNames.end()) {
232 SALOMEDSImpl_SObject so = _study->FindComponent(compName);
233 if(!so) return entry;
235 _compNames[compName] = compID;
237 else compID = _compNames[compName];
239 std::string newEntry(compID);
240 newEntry += (":"+tail);
246 bool SALOMEDSImpl_IParameters::isDumpPython(SALOMEDSImpl_Study* study, const std::string& theID)
249 if(theID == "") anID = getDefaultVisualComponent();
252 SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), 0);
253 if(!ap) return false;
254 if(!ap->IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN)) return false;
255 return (bool)ap->GetBool(_AP_DUMP_PYTHON_);
259 int SALOMEDSImpl_IParameters::getLastSavePoint(SALOMEDSImpl_Study* study, const std::string& theID)
262 if(theID == "") anID = getDefaultVisualComponent();
266 SALOMEDSImpl_SObject so = study->FindComponent(anID);
269 SALOMEDSImpl_StudyBuilder* builder = study->NewBuilder();
270 SALOMEDSImpl_ChildIterator anIter = study->NewChildIterator( so );
272 for(; anIter.More(); anIter.Next())
274 SALOMEDSImpl_SObject val( anIter.Value() );
275 DF_Attribute* genAttr;
276 if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val.Tag();
284 std::string SALOMEDSImpl_IParameters::getStudyScript(SALOMEDSImpl_Study* study, int savePoint, const std::string& theID)
287 if(theID == "") anID = getDefaultVisualComponent();
290 SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
291 SALOMEDSImpl_IParameters ip(ap);
293 std::string dump("");
295 dump += "import iparameters\n";
296 dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1), True)\n\n";
299 std::vector<std::string> v = ip.getProperties();
301 dump += "#Set up visual properties:\n";
302 for(int i = 0; i<v.size(); i++) {
303 std::string prp = ip.getProperty(v[i]);
304 dump += "ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
310 dump += "#Set up lists:\n";
311 for(int i = 0; i<v.size(); i++) {
312 std::vector<std::string> lst = ip.getValues(v[i]);
313 dump += "# fill list "+v[i]+"\n";
314 for(int j = 0; j < lst.size(); j++) {
315 if (lst[j].find('\"') == -1)
316 dump += "ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
318 dump += "ipar.append(\""+v[i]+"\", \"\"\""+lst[j]+"\"\"\")\n";
326 std::string SALOMEDSImpl_IParameters::getDefaultScript(SALOMEDSImpl_Study* study,
327 const std::string& moduleName,
328 const std::string& shift,
329 const std::string& theID)
332 if(theID == "") anID = getDefaultVisualComponent();
335 std::string dump("");
337 int savePoint = SALOMEDSImpl_IParameters::getLastSavePoint(study, anID);
338 if(savePoint < 0) return dump;
339 SALOMEDSImpl_IParameters ip = SALOMEDSImpl_IParameters(study->GetCommonParameters(anID.c_str(), savePoint));
340 if(!isDumpPython(study)) return dump;
342 SALOMEDSImpl_AttributeParameter* ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
343 ip = SALOMEDSImpl_IParameters(ap);
346 dump += shift +"import iparameters\n";
347 dump += shift + "ipar = iparameters.IParameters(theStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
349 std::vector<std::string> v = ip.getProperties();
351 dump += shift +"#Set up visual properties:\n";
352 for(int i = 0; i<v.size(); i++) {
353 std::string prp = ip.getProperty(v[i]);
354 dump += shift +"ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
360 dump += shift +"#Set up lists:\n";
361 for(int i = 0; i<v.size(); i++) {
362 std::vector<std::string> lst = ip.getValues(v[i]);
363 dump += shift +"# fill list "+v[i]+"\n";
364 for(int j = 0; j < lst.size(); j++)
365 dump += shift +"ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
371 dump += shift + "#Set up entries:\n";
372 for(int i = 0; i<v.size(); i++) {
373 std::vector<std::string> names = ip.getAllParameterNames(v[i]);
374 std::vector<std::string> values = ip.getAllParameterValues(v[i]);
375 std::string decodedEntry = ip.decodeEntry(v[i]);
376 SALOMEDSImpl_SObject so = study->FindObjectID(decodedEntry);
377 std::string so_name("");
378 if(so) so_name = so.GetName();
380 //Try to find id parameter
381 std::vector<std::string>::iterator it = std::find(names.begin(), names.end(), _PT_ID_ );
382 bool hasId = it != names.end();
383 bool onlyId = hasId && names.size() == 1;
386 dump += shift + "# set up entry " + v[i] +" ("+so_name+")" + " parameters" + "\n";
388 int idIndex = std::distance(names.begin(), it);
389 dump += shift + "objId = " + values[idIndex] + "\n";
392 for(int j = 0; j < names.size() && j < values.size(); j++) {
393 if(names[j] == _PT_ID_) continue;
395 dump += shift + "ipar.setParameter(" + "objId" + ", \"" + names[j] + "\", \"" + values[j] + "\")\n";
397 dump += shift + "ipar.setParameter(\"" + v[i] + "\", \"" + names[j] + "\", \"" + values[j] + "\")\n";
407 std::string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
409 return "Interface Applicative";