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 <TCollection_AsciiString.hxx>
24 #include "SALOMEDSImpl_SObject.hxx"
25 #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(const Handle(SALOMEDSImpl_AttributeParameter)& ap)
39 if(ap.IsNull()) return;
41 Handle(SALOMEDSImpl_SObject) so = _ap->GetSObject();
42 _study = so->GetStudy();
45 SALOMEDSImpl_IParameters::~SALOMEDSImpl_IParameters()
50 int SALOMEDSImpl_IParameters::append(const string& listName, const string& value)
52 if(_ap.IsNull()) return -1;
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 string& listName)
70 if(_ap.IsNull()) return -1;
71 if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
72 vector<string> v = _ap->GetStrArray(listName);
76 vector<string> SALOMEDSImpl_IParameters::getValues(const string& listName)
79 if(_ap.IsNull()) return v;
80 if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
81 return _ap->GetStrArray(listName);
85 string SALOMEDSImpl_IParameters::getValue(const string& listName, int index)
87 if(_ap.IsNull()) return "";
88 if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
89 vector<string> v = _ap->GetStrArray(listName);
90 if(index >= v.size()) return "";
94 vector<string> SALOMEDSImpl_IParameters::getLists()
97 if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
98 return _ap->GetStrArray(_AP_LISTS_LIST_);
101 void SALOMEDSImpl_IParameters::setParameter(const string& entry, const string& parameterName, const string& value)
103 if(_ap.IsNull()) return;
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 string SALOMEDSImpl_IParameters::getParameter(const string& entry, const string& parameterName)
118 if(_ap.IsNull()) return "";
119 if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
120 vector<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 vector<string> SALOMEDSImpl_IParameters::getAllParameterNames(const string& entry)
131 vector<string> v, names;
132 if(_ap.IsNull()) return v;
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 vector<string> SALOMEDSImpl_IParameters::getAllParameterValues(const string& entry)
144 vector<string> v, values;
145 if(_ap.IsNull()) return v;
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 string& entry)
157 if(_ap.IsNull()) return -1;
158 if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
159 return _ap->GetStrArray(entry).size()/2;
162 vector<string> SALOMEDSImpl_IParameters::getEntries()
165 if(_ap.IsNull()) return 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 string& name, const std::string& value)
172 if(_ap.IsNull()) return;
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 string SALOMEDSImpl_IParameters::getProperty(const string& name)
181 if(_ap.IsNull()) return "";
182 if(!_ap->IsSet(name, PT_STRING)) return "";
183 return _ap->GetString(name);
186 vector<string> SALOMEDSImpl_IParameters::getProperties()
189 if(_ap.IsNull()) return v;
190 if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
191 return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
194 string SALOMEDSImpl_IParameters::decodeEntry(const string& entry)
196 if(!_study) return entry;
197 int pos = entry.rfind("_");
198 if(pos < 0 || pos >= entry.length()) return entry;
200 string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
202 if(_compNames.find(compName) == _compNames.end()) {
203 Handle(SALOMEDSImpl_SObject) so = _study->FindComponent((char*)compName.c_str());
204 if(so.IsNull()) return entry;
205 compID = so->GetID().ToCString();
206 _compNames[compName] = compID;
208 else compID = _compNames[compName];
210 string newEntry(compID);
211 newEntry += (":"+tail);
217 bool SALOMEDSImpl_IParameters::isDumpPython(const Handle(SALOMEDSImpl_Study)& study, const string& theID)
220 if(theID == "") anID = getDefaultVisualComponent();
223 Handle(SALOMEDSImpl_AttributeParameter) ap = study->GetCommonParameters((char*)anID.c_str(), 0);
224 if(ap.IsNull()) 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(const Handle(SALOMEDSImpl_Study)& study, const string& theID)
233 if(theID == "") anID = getDefaultVisualComponent();
237 Handle(SALOMEDSImpl_SObject) so = study->FindComponent((char*)anID.c_str());
238 if(so.IsNull()) return -1;
240 Handle(SALOMEDSImpl_StudyBuilder) builder = study->NewBuilder();
241 Handle(SALOMEDSImpl_ChildIterator) anIter ( study->NewChildIterator( so ) );
243 for(; anIter->More(); anIter->Next())
245 Handle(SALOMEDSImpl_SObject) val( anIter->Value() );
246 Handle(SALOMEDSImpl_GenericAttribute) genAttr;
247 if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val->Tag();
255 string SALOMEDSImpl_IParameters::getStudyScript(const Handle(SALOMEDSImpl_Study)& study, int savePoint, const std::string& theID)
258 if(theID == "") anID = getDefaultVisualComponent();
261 Handle(SALOMEDSImpl_AttributeParameter) ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
262 SALOMEDSImpl_IParameters ip(ap);
266 dump += "import iparameters\n";
267 dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1))\n\n";
270 vector<string> v = ip.getProperties();
272 dump += "#Set up visual properties:\n";
273 for(int i = 0; i<v.size(); i++) {
274 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 vector<string> lst = ip.getValues(v[i]);
284 dump += "# fill list "+v[i]+"\n";
285 for(int j = 0; j < lst.size(); j++)
286 dump += "ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
293 string SALOMEDSImpl_IParameters::getDefaultScript(const Handle(SALOMEDSImpl_Study)& study,
294 const string& moduleName,
299 if(theID == "") anID = getDefaultVisualComponent();
304 int savePoint = SALOMEDSImpl_IParameters::getLastSavePoint(study, anID);
305 if(savePoint < 0) return dump;
306 SALOMEDSImpl_IParameters ip = SALOMEDSImpl_IParameters(study->GetCommonParameters(anID.c_str(), savePoint));
307 if(!isDumpPython(study)) return dump;
309 Handle(SALOMEDSImpl_AttributeParameter) ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
310 ip = SALOMEDSImpl_IParameters(ap);
313 dump += shift +"import iparameters\n";
314 dump += shift + "ipar = iparameters.IParameters(theStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
316 vector<string> v = ip.getProperties();
318 dump += shift +"#Set up visual properties:\n";
319 for(int i = 0; i<v.size(); i++) {
320 string prp = ip.getProperty(v[i]);
321 dump += shift +"ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
327 dump += shift +"#Set up lists:\n";
328 for(int i = 0; i<v.size(); i++) {
329 vector<string> lst = ip.getValues(v[i]);
330 dump += shift +"# fill list "+v[i]+"\n";
331 for(int j = 0; j < lst.size(); j++)
332 dump += shift +"ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
338 dump += shift + "#Set up entries:\n";
339 for(int i = 0; i<v.size(); i++) {
340 vector<string> names = ip.getAllParameterNames(v[i]);
341 vector<string> values = ip.getAllParameterValues(v[i]);
342 string decodedEntry = ip.decodeEntry(v[i]);
343 Handle(SALOMEDSImpl_SObject) so = study->FindObjectID((char*)decodedEntry.c_str());
345 if(!so.IsNull()) so_name = so->GetName().ToCString();
346 dump += shift + "# set up entry " + v[i] +" ("+so_name+")" + " parameters" + "\n";
347 for(int j = 0; j < names.size() && j < values.size(); j++)
348 dump += shift + "ipar.setParameter(\"" + v[i] + "\", \"" + names[j] + "\", \"" + values[j] + "\")\n";
356 string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
358 return "Interface Applicative";