1 // Copyright (C) 2007-2021 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, or (at your option) any later version.
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)
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);
65 return (int)(v.size()-1); //!< TODO: conversion from size_t to int
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);
73 return (int)v.size(); //!< TODO: conversion from size_t to int
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 >= (int)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 size_t length = v.size();
122 for(int i = 0; i<(int)length; i+=1) {
123 if(v[i] == parameterName) return v[i+1];
129 void SALOMEDSImpl_IParameters::setIdParameter(const std::string& entry, const std::string& value)
132 std::vector<std::string> v;
133 if(!_ap->IsSet(entry, PT_STRARRAY)) {
134 append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
135 _ap->SetStrArray(entry, v);
137 v = _ap->GetStrArray(entry);
138 v.push_back(_PT_ID_);
140 _ap->SetStrArray(entry, v);
144 std::string SALOMEDSImpl_IParameters::getIdParameter(const std::string& entry)
147 if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
148 std::vector<std::string> v = _ap->GetStrArray(entry);
149 size_t length = v.size();
150 for(int i = 0; i<(int)length; i+=1) {
151 if(v[i] == _PT_ID_) return v[i+1];
156 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterNames(const std::string& entry)
158 std::vector<std::string> v, names;
160 if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
161 v = _ap->GetStrArray(entry);
162 size_t length = v.size();
163 for(int i = 0; i<(int)length; i+=2) {
164 names.push_back(v[i]);
169 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterValues(const std::string& entry)
171 std::vector<std::string> v, values;
173 if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
174 v = _ap->GetStrArray(entry);
175 size_t length = v.size();
176 for(int i = 1; i<(int)length; i+=2) {
177 values.push_back(v[i]);
182 int SALOMEDSImpl_IParameters::getNbParameters(const std::string& entry)
185 if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
186 return int(_ap->GetStrArray(entry).size()/2); //!< TODO: conversion from size_t to int
189 std::vector<std::string> SALOMEDSImpl_IParameters::getEntries()
191 std::vector<std::string> v;
193 if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
194 return _ap->GetStrArray(_AP_ENTRIES_LIST_);
197 void SALOMEDSImpl_IParameters::setProperty(const std::string& name, const std::string& value)
200 if(!_ap->IsSet(name, PT_STRING)) {
201 append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
203 _ap->SetString(name, value);
206 std::string SALOMEDSImpl_IParameters::getProperty(const std::string& name)
209 if(!_ap->IsSet(name, PT_STRING)) return "";
210 return _ap->GetString(name);
213 std::vector<std::string> SALOMEDSImpl_IParameters::getProperties()
215 std::vector<std::string> v;
217 if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
218 return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
221 std::string SALOMEDSImpl_IParameters::decodeEntry(const std::string& entry)
223 size_t pos = entry.rfind("_");
224 if(pos >= entry.size()) return entry;
226 std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
228 if(_compNames.find(compName) == _compNames.end()) {
229 SALOMEDSImpl_SObject so = SALOMEDSImpl_Study::GetStudyImpl( _ap->GetSObject().GetLabel() )->FindComponent(compName);
230 if(!so) return entry;
232 _compNames[compName] = compID;
234 else compID = _compNames[compName];
236 std::string newEntry(compID);
237 newEntry += (":"+tail);
243 bool SALOMEDSImpl_IParameters::isDumpPython(SALOMEDSImpl_Study* study, const std::string& theID)
246 if(theID == "") anID = getDefaultVisualComponent();
249 SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), 0);
250 if(!ap) return false;
251 if(!ap->IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN)) return false;
252 return (bool)ap->GetBool(_AP_DUMP_PYTHON_);
256 int SALOMEDSImpl_IParameters::getLastSavePoint(SALOMEDSImpl_Study* study, const std::string& theID)
259 if(theID == "") anID = getDefaultVisualComponent();
263 SALOMEDSImpl_SObject so = study->FindComponent(anID);
266 SALOMEDSImpl_StudyBuilder* builder = study->NewBuilder();
267 SALOMEDSImpl_ChildIterator anIter = study->NewChildIterator( so );
269 for(; anIter.More(); anIter.Next())
271 SALOMEDSImpl_SObject val( anIter.Value() );
272 DF_Attribute* genAttr;
273 if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val.Tag();
281 std::string SALOMEDSImpl_IParameters::getStudyScript(SALOMEDSImpl_Study* study, int savePoint, const std::string& theID)
284 if(theID == "") anID = getDefaultVisualComponent();
287 SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
288 SALOMEDSImpl_IParameters ip(ap);
290 std::string dump("");
292 dump += "import iparameters\n";
293 dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1), True)\n\n";
296 std::vector<std::string> v = ip.getProperties();
298 dump += "#Set up visual properties:\n";
299 for(int i = 0; i<(int)v.size(); i++) {//TODO: mismatch signed/unsigned
300 std::string prp = ip.getProperty(v[i]);
301 dump += "ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
307 dump += "#Set up lists:\n";
308 for(int i = 0; i<(int)v.size(); i++) { //TODO: mismatch signed/unsigned
309 std::vector<std::string> lst = ip.getValues(v[i]);
310 dump += "# fill list "+v[i]+"\n";
311 for(int j = 0; j < (int)lst.size(); j++) { //TODO: mismatch signed/unsigned
312 if (lst[j].find('\"') == std::string::npos)
313 dump += "ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
315 dump += "ipar.append(\""+v[i]+"\", \"\"\""+lst[j]+"\"\"\")\n";
323 std::string SALOMEDSImpl_IParameters::getDefaultScript(SALOMEDSImpl_Study* study,
324 const std::string& moduleName,
325 const std::string& shift,
326 const std::string& theID)
329 if(theID == "") anID = getDefaultVisualComponent();
332 std::string dump("");
334 int savePoint = SALOMEDSImpl_IParameters::getLastSavePoint(study, anID);
335 if(savePoint < 0) return dump;
336 SALOMEDSImpl_IParameters ip = SALOMEDSImpl_IParameters(study->GetCommonParameters(anID.c_str(), savePoint));
337 if(!isDumpPython(study)) return dump;
339 SALOMEDSImpl_AttributeParameter* ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
340 ip = SALOMEDSImpl_IParameters(ap);
343 dump += shift +"import iparameters\n";
344 dump += shift + "ipar = iparameters.IParameters(salome.myStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
346 std::vector<std::string> v = ip.getProperties();
348 dump += shift +"#Set up visual properties:\n";
349 for(int i = 0; i<(int)v.size(); i++) { //TODO: mismtach signed/unsigned
350 std::string prp = ip.getProperty(v[i]);
351 dump += shift +"ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
357 dump += shift +"#Set up lists:\n";
358 for(int i = 0; i<(int)v.size(); i++) { //TODO: mismatch signed/unsigned
359 std::vector<std::string> lst = ip.getValues(v[i]);
360 dump += shift +"# fill list "+v[i]+"\n";
361 for(int j = 0; j < (int)lst.size(); j++) //TODO: mismatch signed/unsigned
362 dump += shift +"ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
368 dump += shift + "#Set up entries:\n";
369 for(int i = 0; i<(int)v.size(); i++) { //TODO: mismatch signed/unsigned
370 std::vector<std::string> names = ip.getAllParameterNames(v[i]);
371 std::vector<std::string> values = ip.getAllParameterValues(v[i]);
372 std::string decodedEntry = ip.decodeEntry(v[i]);
373 SALOMEDSImpl_SObject so = study->FindObjectID(decodedEntry);
374 std::string so_name("");
375 if(so) so_name = so.GetName();
377 //Try to find id parameter
378 std::vector<std::string>::iterator it = std::find(names.begin(), names.end(), _PT_ID_ );
379 bool hasId = it != names.end();
380 bool onlyId = hasId && names.size() == 1;
383 dump += shift + "# set up entry " + v[i] +" ("+so_name+")" + " parameters" + "\n";
385 int idIndex = (int)std::distance(names.begin(), it); //!< TODO: conversion from __int64 to int
386 dump += shift + "objId = " + values[idIndex] + "\n";
389 for(int j = 0; j < (int)names.size() && j < (int)values.size(); j++) { //TODO: mismtach siged/unsigned
390 if(names[j] == _PT_ID_) continue;
392 dump += shift + "ipar.setParameter(" + "objId" + ", \"" + names[j] + "\", \"" + values[j] + "\")\n";
394 dump += shift + "ipar.setParameter(\"" + v[i] + "\", \"" + names[j] + "\", \"" + values[j] + "\")\n";
404 std::string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
406 return "Interface Applicative";