Salome HOME
Merge branch 'V7_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   SALOMEDSImpl_SObject so = _ap->GetSObject();
44   _study = so.GetStudy();
45 }
46
47 SALOMEDSImpl_IParameters::~SALOMEDSImpl_IParameters()
48 {
49   _compNames.clear();
50 }
51
52 int SALOMEDSImpl_IParameters::append(const std::string& listName, const std::string& value)
53 {
54   if(!_ap) return -1;
55   std::vector<std::string> v;
56   if(!_ap->IsSet(listName, PT_STRARRAY)) {
57     if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) _ap->SetStrArray(_AP_LISTS_LIST_, v);
58     if(listName != _AP_ENTRIES_LIST_ && 
59        listName != _AP_PROPERTIES_LIST_) {
60       append(_AP_LISTS_LIST_, listName);
61     }
62     _ap->SetStrArray(listName, v);
63   }
64   v = _ap->GetStrArray(listName);
65   v.push_back(value);
66   _ap->SetStrArray(listName, v);
67   return (v.size()-1);
68 }
69
70 int SALOMEDSImpl_IParameters::nbValues(const std::string& listName)
71 {
72   if(!_ap) return -1;
73   if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
74   std::vector<std::string> v = _ap->GetStrArray(listName);
75   return v.size();
76 }
77
78 std::vector<std::string> SALOMEDSImpl_IParameters::getValues(const std::string& listName)
79 {
80   std::vector<std::string> v;
81   if(!_ap) return v;
82   if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
83   return _ap->GetStrArray(listName);
84 }
85
86
87 std::string SALOMEDSImpl_IParameters::getValue(const std::string& listName, int index)
88 {
89   if(!_ap) return "";
90   if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
91   std::vector<std::string> v = _ap->GetStrArray(listName);
92   if(index >= v.size()) return ""; 
93   return v[index];
94 }
95
96 std::vector<std::string> SALOMEDSImpl_IParameters::getLists()
97 {
98   std::vector<std::string> v;
99   if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
100   return _ap->GetStrArray(_AP_LISTS_LIST_);
101 }
102
103 void SALOMEDSImpl_IParameters::setParameter(const std::string& entry, const std::string& parameterName, const std::string& value)
104 {
105   if(!_ap) return;
106   std::vector<std::string> v;
107   if(!_ap->IsSet(entry, PT_STRARRAY)) {
108     append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
109     _ap->SetStrArray(entry, v);
110   }
111   v = _ap->GetStrArray(entry);
112   v.push_back(parameterName);
113   v.push_back(value);
114   _ap->SetStrArray(entry, v);
115 }
116
117
118 std::string SALOMEDSImpl_IParameters::getParameter(const std::string& entry, const std::string& parameterName)
119 {
120   if(!_ap) return "";
121   if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
122   std::vector<std::string> v = _ap->GetStrArray(entry);
123   int length = v.size();
124   for(int i = 0; i<length; i+=1) {
125     if(v[i] == parameterName) return v[i+1];
126   }
127   return "";
128 }
129
130
131 void SALOMEDSImpl_IParameters::setIdParameter(const std::string& entry, const std::string& value)
132 {
133   if(!_ap) return;
134   std::vector<std::string> v;
135   if(!_ap->IsSet(entry, PT_STRARRAY)) {
136     append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
137     _ap->SetStrArray(entry, v);
138   }
139   v = _ap->GetStrArray(entry);
140   v.push_back(_PT_ID_);
141   v.push_back(value);
142   _ap->SetStrArray(entry, v);
143 }
144
145
146 std::string SALOMEDSImpl_IParameters::getIdParameter(const std::string& entry)
147 {
148   if(!_ap) return "";
149   if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
150   std::vector<std::string> v = _ap->GetStrArray(entry);
151   int length = v.size();
152   for(int i = 0; i<length; i+=1) {
153     if(v[i] == _PT_ID_) return v[i+1];
154   }
155   return "";
156 }
157
158 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterNames(const std::string& entry)
159 {
160   std::vector<std::string> v, names;
161   if(!_ap) return v; 
162   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
163   v = _ap->GetStrArray(entry);
164   int length = v.size();
165   for(int i = 0; i<length; i+=2) {
166     names.push_back(v[i]);
167   }
168   return names;
169 }
170
171 std::vector<std::string> SALOMEDSImpl_IParameters::getAllParameterValues(const std::string& entry)
172 {
173   std::vector<std::string> v, values;
174   if(!_ap) return v; 
175   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
176   v = _ap->GetStrArray(entry);
177   int length = v.size();
178   for(int i = 1; i<length; i+=2) {
179     values.push_back(v[i]);
180   }
181   return values; 
182 }
183
184 int SALOMEDSImpl_IParameters::getNbParameters(const std::string& entry)
185 {
186   if(!_ap) return -1;
187   if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
188   return  _ap->GetStrArray(entry).size()/2;
189 }
190
191 std::vector<std::string> SALOMEDSImpl_IParameters::getEntries()
192 {
193   std::vector<std::string> v;
194   if(!_ap) return v;
195   if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
196   return _ap->GetStrArray(_AP_ENTRIES_LIST_);
197 }
198
199 void SALOMEDSImpl_IParameters::setProperty(const std::string& name, const std::string& value)
200 {
201   if(!_ap) return;
202   if(!_ap->IsSet(name, PT_STRING)) {
203     append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
204   }
205   _ap->SetString(name, value);
206 }
207
208 std::string SALOMEDSImpl_IParameters::getProperty(const std::string& name)
209 {
210   if(!_ap) return "";
211   if(!_ap->IsSet(name, PT_STRING)) return "";
212   return _ap->GetString(name);
213 }
214
215 std::vector<std::string> SALOMEDSImpl_IParameters::getProperties()
216 {
217   std::vector<std::string> v;
218   if(!_ap) return v;
219   if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
220   return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
221 }
222
223 std::string SALOMEDSImpl_IParameters::decodeEntry(const std::string& entry)
224 {
225   if(!_study) return entry;
226   int pos = entry.rfind("_");
227   if(pos < 0 || pos >= entry.size()) return entry;
228
229   std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
230   
231   if(_compNames.find(compName) == _compNames.end()) {
232     SALOMEDSImpl_SObject so = _study->FindComponent(compName);
233     if(!so) return entry;
234     compID = so.GetID();
235     _compNames[compName] = compID;
236   }
237   else compID = _compNames[compName];
238  
239   std::string newEntry(compID);
240   newEntry += (":"+tail);
241   
242   return newEntry;
243 }
244
245
246 bool SALOMEDSImpl_IParameters::isDumpPython(SALOMEDSImpl_Study* study, const std::string& theID)
247 {
248   std::string anID;
249   if(theID == "") anID = getDefaultVisualComponent();
250   else anID = theID;
251
252   SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), 0);
253   if(!ap) return false;
254   if(!ap->IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN)) return false;
255   return (bool)ap->GetBool(_AP_DUMP_PYTHON_);
256 }
257
258
259 int SALOMEDSImpl_IParameters::getLastSavePoint(SALOMEDSImpl_Study* study, const std::string& theID)
260 {
261   std::string anID;
262   if(theID == "") anID = getDefaultVisualComponent();
263   else anID = theID;
264
265
266   SALOMEDSImpl_SObject so = study->FindComponent(anID);
267   if(!so) return -1;
268
269   SALOMEDSImpl_StudyBuilder* builder = study->NewBuilder();
270   SALOMEDSImpl_ChildIterator anIter = study->NewChildIterator( so );
271   int tag = -1;
272   for(; anIter.More(); anIter.Next())
273   {
274     SALOMEDSImpl_SObject val( anIter.Value() );
275     DF_Attribute* genAttr;
276     if(builder->FindAttribute(val, genAttr, "AttributeParameter")) tag = val.Tag();
277   }
278
279   return tag;
280 }
281
282
283
284 std::string SALOMEDSImpl_IParameters::getStudyScript(SALOMEDSImpl_Study* study, int savePoint, const std::string& theID)
285 {
286   std::string anID;
287   if(theID == "") anID = getDefaultVisualComponent();
288   else anID = theID;
289
290   SALOMEDSImpl_AttributeParameter* ap = study->GetCommonParameters((char*)anID.c_str(), savePoint);
291   SALOMEDSImpl_IParameters ip(ap);
292
293   std::string dump("");
294
295   dump += "import iparameters\n";
296   dump += "ipar = iparameters.IParameters(salome.myStudy.GetCommonParameters(\""+anID+"\", 1), True)\n\n";
297   
298   
299   std::vector<std::string> v = ip.getProperties();
300   if(v.size() > 0) {
301     dump += "#Set up visual properties:\n";
302     for(int i = 0; i<v.size(); i++) {
303       std::string prp = ip.getProperty(v[i]);
304       dump += "ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
305     }
306   }
307
308   v = ip.getLists();
309   if(v.size() > 0) {
310     dump += "#Set up lists:\n";
311     for(int i = 0; i<v.size(); i++) {
312       std::vector<std::string> lst = ip.getValues(v[i]);
313       dump += "# fill list "+v[i]+"\n";
314       for(int j = 0; j < lst.size(); j++) {
315         if (lst[j].find('\"') == -1)
316           dump += "ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
317         else
318           dump += "ipar.append(\""+v[i]+"\", \"\"\""+lst[j]+"\"\"\")\n";
319       }
320     }
321   }
322
323   return dump;
324 }
325
326 std::string SALOMEDSImpl_IParameters::getDefaultScript(SALOMEDSImpl_Study* study, 
327                                                   const std::string& moduleName, 
328                                                   const std::string& shift, 
329                                                   const std::string& theID)
330 {
331   std::string anID;
332   if(theID == "") anID = getDefaultVisualComponent();
333   else anID = theID;
334
335   std::string dump("");
336
337   int savePoint = SALOMEDSImpl_IParameters::getLastSavePoint(study, anID);
338   if(savePoint < 0) return dump;
339   SALOMEDSImpl_IParameters ip = SALOMEDSImpl_IParameters(study->GetCommonParameters(anID.c_str(), savePoint));
340   if(!isDumpPython(study)) return dump;
341
342   SALOMEDSImpl_AttributeParameter* ap = study->GetModuleParameters(anID.c_str(), moduleName.c_str(), savePoint);
343   ip = SALOMEDSImpl_IParameters(ap);
344
345
346   dump += shift +"import iparameters\n";
347   dump += shift + "ipar = iparameters.IParameters(theStudy.GetModuleParameters(\""+anID+"\", \""+moduleName+"\", 1))\n\n";
348   
349   std::vector<std::string> v = ip.getProperties();
350   if(v.size() > 0) {
351     dump += shift +"#Set up visual properties:\n";
352     for(int i = 0; i<v.size(); i++) {
353       std::string prp = ip.getProperty(v[i]);
354       dump += shift +"ipar.setProperty(\""+v[i]+"\", \""+prp+"\")\n";
355     }
356   }
357
358   v = ip.getLists();
359   if(v.size() > 0) {
360     dump +=  shift +"#Set up lists:\n";
361     for(int i = 0; i<v.size(); i++) {
362       std::vector<std::string> lst = ip.getValues(v[i]);
363       dump += shift +"# fill list "+v[i]+"\n";
364       for(int j = 0; j < lst.size(); j++)
365         dump += shift +"ipar.append(\""+v[i]+"\", \""+lst[j]+"\")\n";
366     }
367   }
368
369   v = ip.getEntries();
370   if(v.size() > 0) {
371     dump += shift + "#Set up entries:\n";
372     for(int i = 0; i<v.size(); i++) {
373       std::vector<std::string> names = ip.getAllParameterNames(v[i]);
374       std::vector<std::string> values = ip.getAllParameterValues(v[i]);
375       std::string decodedEntry = ip.decodeEntry(v[i]);
376       SALOMEDSImpl_SObject so = study->FindObjectID(decodedEntry);
377       std::string so_name("");
378       if(so) so_name = so.GetName();
379       
380       //Try to find id parameter 
381       std::vector<std::string>::iterator it = std::find(names.begin(), names.end(), _PT_ID_ );
382       bool hasId = it !=  names.end();
383       bool onlyId = hasId && names.size() == 1;
384       
385       if(!onlyId) {
386         dump += shift + "# set up entry " + v[i] +" ("+so_name+")" + " parameters" + "\n";
387         if(hasId) {
388           int idIndex = std::distance(names.begin(), it);
389           dump += shift + "objId = " + values[idIndex] + "\n";
390         }
391            
392         for(int j = 0; j < names.size() && j < values.size(); j++) {
393           if(names[j] == _PT_ID_) continue;
394           if(hasId)
395             dump += shift + "ipar.setParameter(" + "objId" + ", \"" + names[j] + "\", \"" + values[j] + "\")\n";
396           else
397             dump += shift + "ipar.setParameter(\"" + v[i] + "\", \"" + names[j] + "\", \"" + values[j] + "\")\n";
398         }
399       }
400     }
401   }
402   
403   return dump;  
404 }
405
406
407 std::string SALOMEDSImpl_IParameters::getDefaultVisualComponent()
408 {
409   return "Interface Applicative";
410 }
411
412