Salome HOME
Removed includes and libraries of OCC
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_IParameters.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "SALOMEDSImpl_IParameters.hxx"
20 #include <utilities.h>
21
22 #include "SALOMEDSImpl_SObject.hxx"
23 #include "SALOMEDSImpl_ChildIterator.hxx"
24
25 using namespace std;
26
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"
31
32 /*!
33   Constructor
34 */
35 SALOMEDSImpl_IParameters::SALOMEDSImpl_IParameters(SALOMEDSImpl_AttributeParameter* ap)
36 {
37   if(!ap) return;
38   _ap = ap;
39   SALOMEDSImpl_SObject so = _ap->GetSObject();
40   _study = so.GetStudy();
41 }
42
43 SALOMEDSImpl_IParameters::~SALOMEDSImpl_IParameters()
44 {
45   _compNames.clear();
46 }
47
48 int SALOMEDSImpl_IParameters::append(const string& listName, const string& value)
49 {
50   if(!_ap) return -1;
51   vector<string> v;
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);
57     }
58     _ap->SetStrArray(listName, v);
59   }
60   v = _ap->GetStrArray(listName);
61   v.push_back(value);
62   _ap->SetStrArray(listName, v);
63   return (v.size()-1);
64 }
65
66 int SALOMEDSImpl_IParameters::nbValues(const string& listName)
67 {
68   if(!_ap) return -1;
69   if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
70   vector<string> v = _ap->GetStrArray(listName);
71   return v.size();
72 }
73
74 vector<string> SALOMEDSImpl_IParameters::getValues(const string& listName)
75 {
76   vector<string> v;
77   if(!_ap) return v;
78   if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
79   return _ap->GetStrArray(listName);
80 }
81
82
83 string SALOMEDSImpl_IParameters::getValue(const string& listName, int index)
84 {
85   if(!_ap) return "";
86   if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
87   vector<string> v = _ap->GetStrArray(listName);
88   if(index >= v.size()) return ""; 
89   return v[index];
90 }
91
92 vector<string> SALOMEDSImpl_IParameters::getLists()
93 {
94   vector<string> v;
95   if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
96   return _ap->GetStrArray(_AP_LISTS_LIST_);
97 }
98
99 void SALOMEDSImpl_IParameters::setParameter(const string& entry, const string& parameterName, const string& value)
100 {
101   if(!_ap) return;
102   vector<string> v;
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);
106   }
107   v = _ap->GetStrArray(entry);
108   v.push_back(parameterName);
109   v.push_back(value);
110   _ap->SetStrArray(entry, v);
111 }
112
113
114 string SALOMEDSImpl_IParameters::getParameter(const string& entry, const string& parameterName)
115 {
116   if(!_ap) return "";
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];
122   }
123   return "";
124 }
125
126
127 vector<string> SALOMEDSImpl_IParameters::getAllParameterNames(const string& entry)
128 {
129   vector<string> v, names;
130   if(!_ap) return v; 
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]);
136   }
137   return names;
138 }
139
140 vector<string> SALOMEDSImpl_IParameters::getAllParameterValues(const string& entry)
141 {
142   vector<string> v, values;
143   if(!_ap) return v; 
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]);
149   }
150   return values; 
151 }
152
153 int SALOMEDSImpl_IParameters::getNbParameters(const string& entry)
154 {
155   if(!_ap) return -1;
156   if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
157   return  _ap->GetStrArray(entry).size()/2;
158 }
159
160 vector<string> SALOMEDSImpl_IParameters::getEntries()
161 {
162   vector<string> v;
163   if(!_ap) return v;
164   if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
165   return _ap->GetStrArray(_AP_ENTRIES_LIST_);
166 }
167
168 void SALOMEDSImpl_IParameters::setProperty(const string& name, const std::string& value)
169 {
170   if(!_ap) return;
171   if(!_ap->IsSet(name, PT_STRING)) {
172     append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
173   }
174   _ap->SetString(name, value);
175 }
176
177 string SALOMEDSImpl_IParameters::getProperty(const string& name)
178 {
179   if(!_ap) return "";
180   if(!_ap->IsSet(name, PT_STRING)) return "";
181   return _ap->GetString(name);
182 }
183
184 vector<string> SALOMEDSImpl_IParameters::getProperties()
185 {
186   vector<string> v;
187   if(!_ap) return v;
188   if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
189   return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
190 }
191
192 string SALOMEDSImpl_IParameters::decodeEntry(const string& entry)
193 {
194   if(!_study) return entry;
195   int pos = entry.rfind("_");
196   if(pos < 0 || pos >= entry.size()) return entry;
197
198   string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
199   
200   if(_compNames.find(compName) == _compNames.end()) {
201     SALOMEDSImpl_SObject so = _study->FindComponent(compName);
202     if(!so) return entry;
203     compID = so.GetID();
204     _compNames[compName] = compID;
205   }
206   else compID = _compNames[compName];
207  
208   string newEntry(compID);
209   newEntry += (":"+tail);
210   
211   return newEntry;
212 }
213
214
215 bool SALOMEDSImpl_IParameters::isDumpPython(SALOMEDSImpl_Study* study, const string& theID)
216 {
217   string anID;
218   if(theID == "") anID = getDefaultVisualComponent();
219   else anID = theID;
220
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_);
225 }
226
227
228 int SALOMEDSImpl_IParameters::getLastSavePoint(SALOMEDSImpl_Study* study, const string& theID)
229 {
230   string anID;
231   if(theID == "") anID = getDefaultVisualComponent();
232   else anID = theID;
233
234
235   SALOMEDSImpl_SObject so = study->FindComponent(anID);
236   if(!so) return -1;
237
238   SALOMEDSImpl_StudyBuilder* builder = study->NewBuilder();
239   SALOMEDSImpl_ChildIterator anIter = study->NewChildIterator( so );
240   int tag = -1;
241   for(; anIter.More(); anIter.Next())
242   {
243     SALOMEDSImpl_SObject val( anIter.Value() );
244     DF_Attribute* genAttr;
245     if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val.Tag();
246   }
247
248   return tag;
249 }
250
251
252
253 string SALOMEDSImpl_IParameters::getStudyScript(SALOMEDSImpl_Study* study, int savePoint, const std::string& theID)
254 {
255   string anID;
256   if(theID == "") anID = getDefaultVisualComponent();
257   else anID = theID;
258
259   SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
260   SALOMEDSImpl_IParameters ip(ap);
261
262   string dump("");
263
264   dump += "import iparameters\n";
265   dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1))\n\n";
266   
267   
268   vector<string> v = ip.getProperties();
269   if(v.size() > 0) {
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";
274     }
275   }
276
277   v = ip.getLists();
278   if(v.size() > 0) {
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";
285     }
286   }
287
288   return dump;
289 }
290
291 string SALOMEDSImpl_IParameters::getDefaultScript(SALOMEDSImpl_Study* study, 
292                                                   const string& moduleName, 
293                                                   const string& shift, 
294                                                   const string& theID)
295 {
296   string anID;
297   if(theID == "") anID = getDefaultVisualComponent();
298   else anID = theID;
299
300   string dump("");
301
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;
306
307   SALOMEDSImpl_AttributeParameter* ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
308   ip = SALOMEDSImpl_IParameters(ap);
309
310
311   dump += shift +"import iparameters\n";
312   dump += shift + "ipar = iparameters.IParameters(theStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
313   
314   vector<string> v = ip.getProperties();
315   if(v.size() > 0) {
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";
320     }
321   }
322
323   v = ip.getLists();
324   if(v.size() > 0) {
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";
331     }
332   }
333
334   v = ip.getEntries();
335   if(v.size() > 0) {
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);
342       string so_name("");
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";
347     }
348   }
349   
350   return dump;  
351 }
352
353
354 string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
355 {
356   return "Interface Applicative";
357 }
358
359