Salome HOME
Merge branch 'master' into V9_dev
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_IParameters.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 #define _PT_ID_ "_PT_OBJECT_ID_"
35
36 /*!
37   Constructor
38 */
39 SALOMEDSImpl_IParameters::SALOMEDSImpl_IParameters(SALOMEDSImpl_AttributeParameter* ap)
40 {
41   if(!ap) return;
42   _ap = ap;
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 void SALOMEDSImpl_IParameters::setIdParameter(const std::string& entry, const std::string& value)
130 {
131   if(!_ap) return;
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);
136   }
137   v = _ap->GetStrArray(entry);
138   v.push_back(_PT_ID_);
139   v.push_back(value);
140   _ap->SetStrArray(entry, v);
141 }
142
143
144 std::string SALOMEDSImpl_IParameters::getIdParameter(const std::string& entry)
145 {
146   if(!_ap) return "";
147   if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
148   std::vector<std::string> v = _ap->GetStrArray(entry);
149   int length = v.size();
150   for(int i = 0; i<length; i+=1) {
151     if(v[i] == _PT_ID_) return v[i+1];
152   }
153   return "";
154 }
155
156 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterNames(const std::string& entry)
157 {
158   std::vector<std::string> v, names;
159   if(!_ap) return v; 
160   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
161   v = _ap->GetStrArray(entry);
162   int length = v.size();
163   for(int i = 0; i<length; i+=2) {
164     names.push_back(v[i]);
165   }
166   return names;
167 }
168
169 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterValues(const std::string& entry)
170 {
171   std::vector<std::string> v, values;
172   if(!_ap) return v; 
173   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
174   v = _ap->GetStrArray(entry);
175   int length = v.size();
176   for(int i = 1; i<length; i+=2) {
177     values.push_back(v[i]);
178   }
179   return values; 
180 }
181
182 int SALOMEDSImpl_IParameters::getNbParameters(const std::string& entry)
183 {
184   if(!_ap) return -1;
185   if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
186   return  _ap->GetStrArray(entry).size()/2;
187 }
188
189 std::vector<std::string> SALOMEDSImpl_IParameters::getEntries()
190 {
191   std::vector<std::string> v;
192   if(!_ap) return v;
193   if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
194   return _ap->GetStrArray(_AP_ENTRIES_LIST_);
195 }
196
197 void SALOMEDSImpl_IParameters::setProperty(const std::string& name, const std::string& value)
198 {
199   if(!_ap) return;
200   if(!_ap->IsSet(name, PT_STRING)) {
201     append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
202   }
203   _ap->SetString(name, value);
204 }
205
206 std::string SALOMEDSImpl_IParameters::getProperty(const std::string& name)
207 {
208   if(!_ap) return "";
209   if(!_ap->IsSet(name, PT_STRING)) return "";
210   return _ap->GetString(name);
211 }
212
213 std::vector<std::string> SALOMEDSImpl_IParameters::getProperties()
214 {
215   std::vector<std::string> v;
216   if(!_ap) return v;
217   if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
218   return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
219 }
220
221 std::string SALOMEDSImpl_IParameters::decodeEntry(const std::string& entry)
222 {
223   int pos = entry.rfind("_");
224   if(pos < 0 || pos >= entry.size()) return entry;
225
226   std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
227   
228   if(_compNames.find(compName) == _compNames.end()) {
229     SALOMEDSImpl_SObject so = SALOMEDSImpl_Study::GetStudyImpl( _ap->GetSObject().GetLabel() )->FindComponent(compName);
230     if(!so) return entry;
231     compID = so.GetID();
232     _compNames[compName] = compID;
233   }
234   else compID = _compNames[compName];
235  
236   std::string newEntry(compID);
237   newEntry += (":"+tail);
238   
239   return newEntry;
240 }
241
242
243 bool SALOMEDSImpl_IParameters::isDumpPython(SALOMEDSImpl_Study* study, const std::string& theID)
244 {
245   std::string anID;
246   if(theID == "") anID = getDefaultVisualComponent();
247   else anID = theID;
248
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_);
253 }
254
255
256 int SALOMEDSImpl_IParameters::getLastSavePoint(SALOMEDSImpl_Study* study, const std::string& theID)
257 {
258   std::string anID;
259   if(theID == "") anID = getDefaultVisualComponent();
260   else anID = theID;
261
262
263   SALOMEDSImpl_SObject so = study->FindComponent(anID);
264   if(!so) return -1;
265
266   SALOMEDSImpl_StudyBuilder* builder = study->NewBuilder();
267   SALOMEDSImpl_ChildIterator anIter = study->NewChildIterator( so );
268   int tag = -1;
269   for(; anIter.More(); anIter.Next())
270   {
271     SALOMEDSImpl_SObject val( anIter.Value() );
272     DF_Attribute* genAttr;
273     if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val.Tag();
274   }
275
276   return tag;
277 }
278
279
280
281 std::string SALOMEDSImpl_IParameters::getStudyScript(SALOMEDSImpl_Study* study, int savePoint, const std::string& theID)
282 {
283   std::string anID;
284   if(theID == "") anID = getDefaultVisualComponent();
285   else anID = theID;
286
287   SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
288   SALOMEDSImpl_IParameters ip(ap);
289
290   std::string dump("");
291
292   dump += "import iparameters\n";
293   dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1), True)\n\n";
294   
295   
296   std::vector<std::string> v = ip.getProperties();
297   if(v.size() > 0) {
298     dump += "#Set up visual properties:\n";
299     for(int i = 0; i<v.size(); i++) {
300       std::string prp = ip.getProperty(v[i]);
301       dump += "ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
302     }
303   }
304
305   v = ip.getLists();
306   if(v.size() > 0) {
307     dump += "#Set up lists:\n";
308     for(int i = 0; i<v.size(); i++) {
309       std::vector<std::string> lst = ip.getValues(v[i]);
310       dump += "# fill list "+v[i]+"\n";
311       for(int j = 0; j < lst.size(); j++) {
312         if (lst[j].find('\"') == -1)
313           dump += "ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
314         else
315           dump += "ipar.append(\""+v[i]+"\", \"\"\""+lst[j]+"\"\"\")\n";
316       }
317     }
318   }
319
320   return dump;
321 }
322
323 std::string SALOMEDSImpl_IParameters::getDefaultScript(SALOMEDSImpl_Study* study, 
324                                                   const std::string& moduleName, 
325                                                   const std::string& shift, 
326                                                   const std::string& theID)
327 {
328   std::string anID;
329   if(theID == "") anID = getDefaultVisualComponent();
330   else anID = theID;
331
332   std::string dump("");
333
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;
338
339   SALOMEDSImpl_AttributeParameter* ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
340   ip = SALOMEDSImpl_IParameters(ap);
341
342
343   dump += shift +"import iparameters\n";
344   dump += shift + "ipar = iparameters.IParameters(salome.myStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
345   
346   std::vector<std::string> v = ip.getProperties();
347   if(v.size() > 0) {
348     dump += shift +"#Set up visual properties:\n";
349     for(int i = 0; i<v.size(); i++) {
350       std::string prp = ip.getProperty(v[i]);
351       dump += shift +"ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
352     }
353   }
354
355   v = ip.getLists();
356   if(v.size() > 0) {
357     dump +=  shift +"#Set up lists:\n";
358     for(int i = 0; i<v.size(); i++) {
359       std::vector<std::string> lst = ip.getValues(v[i]);
360       dump += shift +"# fill list "+v[i]+"\n";
361       for(int j = 0; j < lst.size(); j++)
362         dump += shift +"ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
363     }
364   }
365
366   v = ip.getEntries();
367   if(v.size() > 0) {
368     dump += shift + "#Set up entries:\n";
369     for(int i = 0; i<v.size(); i++) {
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();
376       
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;
381       
382       if(!onlyId) {
383         dump += shift + "# set up entry " + v[i] +" ("+so_name+")" + " parameters" + "\n";
384         if(hasId) {
385           int idIndex = std::distance(names.begin(), it);
386           dump += shift + "objId = " + values[idIndex] + "\n";
387         }
388            
389         for(int j = 0; j < names.size() && j < values.size(); j++) {
390           if(names[j] == _PT_ID_) continue;
391           if(hasId)
392             dump += shift + "ipar.setParameter(" + "objId" + ", \"" + names[j] + "\", \"" + values[j] + "\")\n";
393           else
394             dump += shift + "ipar.setParameter(\"" + v[i] + "\", \"" + names[j] + "\", \"" + values[j] + "\")\n";
395         }
396       }
397     }
398   }
399   
400   return dump;  
401 }
402
403
404 std::string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
405 {
406   return "Interface Applicative";
407 }
408
409