Salome HOME
Merge from V5_1_4_BR (5_1_4rc2) 09/06/2010
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_IParameters.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "SALOMEDSImpl_IParameters.hxx"
24 #include <utilities.h>
25
26 #include "SALOMEDSImpl_SObject.hxx"
27 #include "SALOMEDSImpl_ChildIterator.hxx"
28
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"
33
34 /*!
35   Constructor
36 */
37 SALOMEDSImpl_IParameters::SALOMEDSImpl_IParameters(SALOMEDSImpl_AttributeParameter* ap)
38 {
39   if(!ap) return;
40   _ap = ap;
41   SALOMEDSImpl_SObject so = _ap->GetSObject();
42   _study = so.GetStudy();
43 }
44
45 SALOMEDSImpl_IParameters::~SALOMEDSImpl_IParameters()
46 {
47   _compNames.clear();
48 }
49
50 int SALOMEDSImpl_IParameters::append(const std::string& listName, const std::string& value)
51 {
52   if(!_ap) return -1;
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);
59     }
60     _ap->SetStrArray(listName, v);
61   }
62   v = _ap->GetStrArray(listName);
63   v.push_back(value);
64   _ap->SetStrArray(listName, v);
65   return (v.size()-1);
66 }
67
68 int SALOMEDSImpl_IParameters::nbValues(const std::string& listName)
69 {
70   if(!_ap) return -1;
71   if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
72   std::vector<std::string> v = _ap->GetStrArray(listName);
73   return v.size();
74 }
75
76 std::vector<std::string> SALOMEDSImpl_IParameters::getValues(const std::string& listName)
77 {
78   std::vector<std::string> v;
79   if(!_ap) return v;
80   if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
81   return _ap->GetStrArray(listName);
82 }
83
84
85 std::string SALOMEDSImpl_IParameters::getValue(const std::string& listName, int index)
86 {
87   if(!_ap) return "";
88   if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
89   std::vector<std::string> v = _ap->GetStrArray(listName);
90   if(index >= v.size()) return ""; 
91   return v[index];
92 }
93
94 std::vector<std::string> SALOMEDSImpl_IParameters::getLists()
95 {
96   std::vector<std::string> v;
97   if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
98   return _ap->GetStrArray(_AP_LISTS_LIST_);
99 }
100
101 void SALOMEDSImpl_IParameters::setParameter(const std::string& entry, const std::string& parameterName, const std::string& value)
102 {
103   if(!_ap) return;
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);
108   }
109   v = _ap->GetStrArray(entry);
110   v.push_back(parameterName);
111   v.push_back(value);
112   _ap->SetStrArray(entry, v);
113 }
114
115
116 std::string SALOMEDSImpl_IParameters::getParameter(const std::string& entry, const std::string& parameterName)
117 {
118   if(!_ap) return "";
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];
124   }
125   return "";
126 }
127
128
129 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterNames(const std::string& entry)
130 {
131   std::vector<std::string> v, names;
132   if(!_ap) 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]);
138   }
139   return names;
140 }
141
142 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterValues(const std::string& entry)
143 {
144   std::vector<std::string> v, values;
145   if(!_ap) 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]);
151   }
152   return values; 
153 }
154
155 int SALOMEDSImpl_IParameters::getNbParameters(const std::string& entry)
156 {
157   if(!_ap) return -1;
158   if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
159   return  _ap->GetStrArray(entry).size()/2;
160 }
161
162 std::vector<std::string> SALOMEDSImpl_IParameters::getEntries()
163 {
164   std::vector<std::string> v;
165   if(!_ap) return v;
166   if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
167   return _ap->GetStrArray(_AP_ENTRIES_LIST_);
168 }
169
170 void SALOMEDSImpl_IParameters::setProperty(const std::string& name, const std::string& value)
171 {
172   if(!_ap) return;
173   if(!_ap->IsSet(name, PT_STRING)) {
174     append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
175   }
176   _ap->SetString(name, value);
177 }
178
179 std::string SALOMEDSImpl_IParameters::getProperty(const std::string& name)
180 {
181   if(!_ap) return "";
182   if(!_ap->IsSet(name, PT_STRING)) return "";
183   return _ap->GetString(name);
184 }
185
186 std::vector<std::string> SALOMEDSImpl_IParameters::getProperties()
187 {
188   std::vector<std::string> v;
189   if(!_ap) return v;
190   if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
191   return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
192 }
193
194 std::string SALOMEDSImpl_IParameters::decodeEntry(const std::string& entry)
195 {
196   if(!_study) return entry;
197   int pos = entry.rfind("_");
198   if(pos < 0 || pos >= entry.size()) return entry;
199
200   std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
201   
202   if(_compNames.find(compName) == _compNames.end()) {
203     SALOMEDSImpl_SObject so = _study->FindComponent(compName);
204     if(!so) return entry;
205     compID = so.GetID();
206     _compNames[compName] = compID;
207   }
208   else compID = _compNames[compName];
209  
210   std::string newEntry(compID);
211   newEntry += (":"+tail);
212   
213   return newEntry;
214 }
215
216
217 bool SALOMEDSImpl_IParameters::isDumpPython(SALOMEDSImpl_Study* study, const std::string& theID)
218 {
219   std::string anID;
220   if(theID == "") anID = getDefaultVisualComponent();
221   else anID = theID;
222
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_);
227 }
228
229
230 int SALOMEDSImpl_IParameters::getLastSavePoint(SALOMEDSImpl_Study* study, const std::string& theID)
231 {
232   std::string anID;
233   if(theID == "") anID = getDefaultVisualComponent();
234   else anID = theID;
235
236
237   SALOMEDSImpl_SObject so = study->FindComponent(anID);
238   if(!so) return -1;
239
240   SALOMEDSImpl_StudyBuilder* builder = study->NewBuilder();
241   SALOMEDSImpl_ChildIterator anIter = study->NewChildIterator( so );
242   int tag = -1;
243   for(; anIter.More(); anIter.Next())
244   {
245     SALOMEDSImpl_SObject val( anIter.Value() );
246     DF_Attribute* genAttr;
247     if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val.Tag();
248   }
249
250   return tag;
251 }
252
253
254
255 std::string SALOMEDSImpl_IParameters::getStudyScript(SALOMEDSImpl_Study* study, int savePoint, const std::string& theID)
256 {
257   std::string anID;
258   if(theID == "") anID = getDefaultVisualComponent();
259   else anID = theID;
260
261   SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
262   SALOMEDSImpl_IParameters ip(ap);
263
264   std::string dump("");
265
266   dump += "import iparameters\n";
267   dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1))\n\n";
268   
269   
270   std::vector<std::string> v = ip.getProperties();
271   if(v.size() > 0) {
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";
276     }
277   }
278
279   v = ip.getLists();
280   if(v.size() > 0) {
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";
288         else
289           dump += "ipar.append(\""+v[i]+"\", \"\"\""+lst[j]+"\"\"\")\n";
290       }
291     }
292   }
293
294   return dump;
295 }
296
297 std::string SALOMEDSImpl_IParameters::getDefaultScript(SALOMEDSImpl_Study* study, 
298                                                   const std::string& moduleName, 
299                                                   const std::string& shift, 
300                                                   const std::string& theID)
301 {
302   std::string anID;
303   if(theID == "") anID = getDefaultVisualComponent();
304   else anID = theID;
305
306   std::string dump("");
307
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;
312
313   SALOMEDSImpl_AttributeParameter* ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
314   ip = SALOMEDSImpl_IParameters(ap);
315
316
317   dump += shift +"import iparameters\n";
318   dump += shift + "ipar = iparameters.IParameters(theStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
319   
320   std::vector<std::string> v = ip.getProperties();
321   if(v.size() > 0) {
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";
326     }
327   }
328
329   v = ip.getLists();
330   if(v.size() > 0) {
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";
337     }
338   }
339
340   v = ip.getEntries();
341   if(v.size() > 0) {
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";
353     }
354   }
355   
356   return dump;  
357 }
358
359
360 std::string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
361 {
362   return "Interface Applicative";
363 }
364
365