Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_IParameters.cxx
1 // Copyright (C) 2007-2012  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 "SALOMEDS_IParameters.hxx"
24 #include <utilities.h>
25
26 #define PT_INTEGER   0
27 #define PT_REAL      1
28 #define PT_BOOLEAN   2
29 #define PT_STRING    3
30 #define PT_REALARRAY 4
31 #define PT_INTARRAY  5
32 #define PT_STRARRAY  6
33
34 #define _AP_LISTS_LIST_ "AP_LISTS_LIST"
35 #define _AP_ENTRIES_LIST_ "AP_ENTRIES_LIST"
36 #define _AP_PROPERTIES_LIST_ "AP_PROPERTIES_LIST"
37 #define _AP_DUMP_PYTHON_ "AP_DUMP_PYTHON"
38
39 #define _PT_ID_ "_PT_OBJECT_ID_"
40
41
42 /*!
43   Constructor
44 */
45 SALOMEDS_IParameters::SALOMEDS_IParameters(const _PTR(AttributeParameter)& ap)
46 {
47   if(!ap) return;
48   _ap = ap;
49   _PTR(SObject) so = _ap->GetSObject();
50   _study = so->GetStudy();
51 }
52
53 SALOMEDS_IParameters::~SALOMEDS_IParameters()
54 {
55   _compNames.clear();
56 }
57
58 int SALOMEDS_IParameters::append(const std::string& listName, const std::string& value)
59 {
60   if(!_ap) return -1;
61   std::vector<std::string> v;
62   if(!_ap->IsSet(listName, PT_STRARRAY)) {
63     if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) _ap->SetStrArray(_AP_LISTS_LIST_, v);
64     if(listName != _AP_ENTRIES_LIST_ && 
65        listName != _AP_PROPERTIES_LIST_) {
66       append(_AP_LISTS_LIST_, listName);
67     }
68     _ap->SetStrArray(listName, v);
69   }
70   v = _ap->GetStrArray(listName);
71   v.push_back(value);
72   _ap->SetStrArray(listName, v);
73   return (v.size()-1);
74 }
75
76 int SALOMEDS_IParameters::nbValues(const std::string& listName)
77 {
78   if(!_ap) return -1;
79   if(!_ap->IsSet(listName, PT_STRARRAY)) return 0;
80   std::vector<std::string> v = _ap->GetStrArray(listName);
81   return v.size();
82 }
83
84 std::vector<std::string> SALOMEDS_IParameters::getValues(const std::string& listName)
85 {
86   std::vector<std::string> v;
87   if(!_ap) return v;
88   if(!_ap->IsSet(listName, PT_STRARRAY)) return v;
89   return _ap->GetStrArray(listName);
90 }
91
92
93 std::string SALOMEDS_IParameters::getValue(const std::string& listName, int index)
94 {
95   if(!_ap) return "";
96   if(!_ap->IsSet(listName, PT_STRARRAY)) return "";
97   std::vector<std::string> v = _ap->GetStrArray(listName);
98   if(index >= v.size()) return ""; 
99   return v[index];
100 }
101
102 std::vector<std::string> SALOMEDS_IParameters::getLists()
103 {
104   std::vector<std::string> v;
105   if(!_ap->IsSet(_AP_LISTS_LIST_, PT_STRARRAY)) return v;
106   return _ap->GetStrArray(_AP_LISTS_LIST_);
107 }
108
109 void SALOMEDS_IParameters::setParameter(const std::string& entry, const std::string& parameterName, const std::string& value)
110 {
111   if(!_ap) return;
112   std::vector<std::string> v;
113   if(!_ap->IsSet(entry, PT_STRARRAY)) {
114     append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
115     _ap->SetStrArray(entry, v);
116   }
117   v = _ap->GetStrArray(entry);
118   v.push_back(parameterName);
119   v.push_back(value);
120   _ap->SetStrArray(entry, v);
121 }
122
123
124 std::string SALOMEDS_IParameters::getParameter(const std::string& entry, const std::string& parameterName)
125 {
126   if(!_ap) return "";
127   if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
128   std::vector<std::string> v = _ap->GetStrArray(entry);
129   int length = v.size();
130   for(int i = 0; i<length; i+=1) {
131     if(v[i] == parameterName) return v[i+1];
132   }
133   return "";
134 }
135
136
137 std::vector<std::string> SALOMEDS_IParameters::getAllParameterNames(const std::string& entry)
138 {
139   std::vector<std::string> v, names;
140   if(!_ap) return v; 
141   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
142   v = _ap->GetStrArray(entry);
143   int length = v.size();
144   for(int i = 0; i<length; i+=2) {
145     names.push_back(v[i]);
146   }
147   return names;
148 }
149
150
151 std::string SALOMEDS_IParameters::getIdParameter(const std::string& entry)
152 {
153   if(!_ap) return "";
154   if(!_ap->IsSet(entry, PT_STRARRAY)) return "";
155   std::vector<std::string> v = _ap->GetStrArray(entry);
156   int length = v.size();
157   for(int i = 0; i<length; i+=1) {
158     if(v[i] == _PT_ID_) return v[i+1];
159   }
160   return "";
161 }
162
163 void SALOMEDS_IParameters::setIdParameter(const std::string& entry, const std::string& value)
164 {
165   if(!_ap) return;
166   std::vector<std::string> v;
167   if(!_ap->IsSet(entry, PT_STRARRAY)) {
168     append(_AP_ENTRIES_LIST_, entry); //Add the entry to the internal list of entries
169     _ap->SetStrArray(entry, v);
170   }
171   v = _ap->GetStrArray(entry);
172   v.push_back(_PT_ID_);
173   v.push_back(value);
174   _ap->SetStrArray(entry, v);
175 }
176
177 std::vector<std::string> SALOMEDS_IParameters::getAllParameterValues(const std::string& entry)
178 {
179   std::vector<std::string> v, values;
180   if(!_ap) return v; 
181   if(!_ap->IsSet(entry, PT_STRARRAY)) return v;
182   v = _ap->GetStrArray(entry);
183   int length = v.size();
184   for(int i = 1; i<length; i+=2) {
185     values.push_back(v[i]);
186   }
187   return values; 
188 }
189
190 int SALOMEDS_IParameters::getNbParameters(const std::string& entry)
191 {
192   if(!_ap) return -1;
193   if(!_ap->IsSet(entry, PT_STRARRAY)) return -1;
194   return  _ap->GetStrArray(entry).size()/2;
195 }
196
197 std::vector<std::string> SALOMEDS_IParameters::getEntries()
198 {
199   std::vector<std::string> v;
200   if(!_ap) return v;
201   if(!_ap->IsSet(_AP_ENTRIES_LIST_, PT_STRARRAY)) return v;
202   return _ap->GetStrArray(_AP_ENTRIES_LIST_);
203 }
204
205 void SALOMEDS_IParameters::setProperty(const std::string& name, const std::string& value)
206 {
207   if(!_ap) return;
208   if(!_ap->IsSet(name, PT_STRING)) {
209     append(_AP_PROPERTIES_LIST_, name); //Add the property to the internal list of properties
210   }
211   _ap->SetString(name, value);
212 }
213
214 std::string SALOMEDS_IParameters::getProperty(const std::string& name)
215 {
216   if(!_ap) return "";
217   if(!_ap->IsSet(name, PT_STRING)) return "";
218   return _ap->GetString(name);
219 }
220
221 std::vector<std::string> SALOMEDS_IParameters::getProperties()
222 {
223   std::vector<std::string> v;
224   if(!_ap) return v;
225   if(!_ap->IsSet(_AP_PROPERTIES_LIST_, PT_STRARRAY)) return v;
226   return _ap->GetStrArray(_AP_PROPERTIES_LIST_);
227 }
228
229
230 std::vector<std::string> SALOMEDS_IParameters::parseValue(const std::string& value, const char separator, bool fromEnd)
231 {
232   std::string val(value);
233   std::vector<std::string> v;
234   int pos;
235   if(fromEnd) pos = val.rfind(separator);
236   else pos = val.find(separator);
237
238   if(pos < 0) {
239     v.push_back(value);
240     return v;
241   }
242
243   std::string part1, part2;
244   part1 = val.substr(0, pos);
245   part2 = val.substr(pos+1, val.size());
246   v.push_back(part1);
247   v.push_back(part2);
248   return v;
249 }
250
251 std::string SALOMEDS_IParameters::encodeEntry(const std::string& entry, const std::string& compName)
252 {
253   std::string tail(entry, 6, entry.length()-1);
254   std::string newEntry(compName);
255   newEntry+=("_"+tail);
256   return newEntry;
257 }
258
259 std::string SALOMEDS_IParameters::decodeEntry(const std::string& entry)
260 {
261   if(!_study) return entry;
262   int pos = entry.rfind("_");
263   if(pos < 0 || pos >= entry.length()) return entry;
264
265   std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
266   
267   if(_compNames.find(compName) == _compNames.end()) {
268     _PTR(SObject) so = _study->FindComponent(compName);
269     if(!so) return entry;
270     compID = so->GetID();
271     _compNames[compName] = compID;
272   }
273   else compID = _compNames[compName];
274  
275   std::string newEntry(compID);
276   newEntry += (":"+tail);
277   
278   return newEntry;
279 }
280
281 void SALOMEDS_IParameters::setDumpPython(_PTR(Study) study, const std::string& theID)
282 {
283   std::string anID;
284   if(theID == "") anID = getDefaultVisualComponent();
285   else anID = theID;
286
287   _PTR(AttributeParameter) ap = study->GetCommonParameters(anID, 0);
288   ap->SetBool(_AP_DUMP_PYTHON_, !isDumpPython(study, theID));
289 }
290
291 bool SALOMEDS_IParameters::isDumpPython(_PTR(Study) study, const std::string& theID)
292 {
293   std::string anID;
294   if(theID == "") anID = getDefaultVisualComponent();
295   else anID = theID;
296
297   _PTR(AttributeParameter) ap = study->GetCommonParameters(anID, 0);
298   if(!ap) return false;
299   if(!ap->IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN)) return false;
300   return (bool)ap->GetBool(_AP_DUMP_PYTHON_);
301 }
302
303 std::string SALOMEDS_IParameters::getDefaultVisualComponent()
304 {
305   return "Interface Applicative";
306 }
307
308
309