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