Salome HOME
updated copyright message
[modules/kernel.git] / src / SALOMEDS / SALOMEDS_IParameters.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, 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 "SALOMEDS_IParameters.hxx"
24 #include <SALOMEDSClient_ClientFactory.hxx>
25 #include <SALOME_KernelServices.hxx>
26 #include <utilities.h>
27
28 #define PT_INTEGER   0
29 #define PT_REAL      1
30 #define PT_BOOLEAN   2
31 #define PT_STRING    3
32 #define PT_REALARRAY 4
33 #define PT_INTARRAY  5
34 #define PT_STRARRAY  6
35
36 #define _AP_LISTS_LIST_ "AP_LISTS_LIST"
37 #define _AP_ENTRIES_LIST_ "AP_ENTRIES_LIST"
38 #define _AP_PROPERTIES_LIST_ "AP_PROPERTIES_LIST"
39 #define _AP_DUMP_PYTHON_ "AP_DUMP_PYTHON"
40
41 #define _PT_ID_ "_PT_OBJECT_ID_"
42
43
44 /*!
45   Constructor
46 */
47 SALOMEDS_IParameters::SALOMEDS_IParameters(const _PTR(AttributeParameter)& ap)
48 {
49   if(!ap) return;
50   _ap = ap;
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 (int)(v.size()-1); //!< TODO: conversion from size_t to int
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 (int)v.size(); //!< TODO: conversion from size_t to int
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 >= (int)v.size()) return "";  //TODO: mismatch signed/unsigned
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 = (int)v.size(); //!< TODO: conversion from size_t to int
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 = (int)v.size(); //!< TODO: conversion from size_t to int
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 = (int)v.size(); //!< TODO: conversion from size_t to int
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 = (int)v.size(); //!< TODO: conversion from size_t to int
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  (int)(_ap->GetStrArray(entry).size()/2); //!< TODO: conversion from size_t to int
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 = (int)val.rfind(separator); //!< TODO: conversion from size_t to int
236   else pos = (int)val.find(separator); //!< TODO: conversion from size_t to int
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   int pos = (int)entry.rfind("_"); //!< TODO: conversion from size_t to int
262   if(pos < 0 || pos >= (int)entry.length()) return entry; //TODO: mismatch signed/unsigned
263
264   std::string compName(entry, 0, pos), compID, tail(entry, pos+1, entry.length()-1);
265   
266   if(_compNames.find(compName) == _compNames.end()) {
267     _PTR(SObject) so = ClientFactory::Study(KERNEL::getStudyServant())->FindComponent(compName);
268     if(!so) return entry;
269     compID = so->GetID();
270     _compNames[compName] = compID;
271   }
272   else compID = _compNames[compName];
273  
274   std::string newEntry(compID);
275   newEntry += (":"+tail);
276   
277   return newEntry;
278 }
279
280 void SALOMEDS_IParameters::setDumpPython(const std::string& theID)
281 {
282   std::string anID;
283   if(theID == "") anID = getDefaultVisualComponent();
284   else anID = theID;
285
286   _PTR(AttributeParameter) ap = ClientFactory::Study(KERNEL::getStudyServant())->GetCommonParameters(anID, 0);
287   ap->SetBool(_AP_DUMP_PYTHON_, !isDumpPython(theID));
288 }
289
290 bool SALOMEDS_IParameters::isDumpPython(const std::string& theID)
291 {
292   std::string anID;
293   if(theID == "") anID = getDefaultVisualComponent();
294   else anID = theID;
295
296   _PTR(AttributeParameter) ap = ClientFactory::Study(KERNEL::getStudyServant())->GetCommonParameters(anID, 0);
297   if(!ap) return false;
298   if(!ap->IsSet(_AP_DUMP_PYTHON_, PT_BOOLEAN)) return false;
299   return (bool)ap->GetBool(_AP_DUMP_PYTHON_);
300 }
301
302 std::string SALOMEDS_IParameters::getDefaultVisualComponent()
303 {
304   return "Interface Applicative";
305 }
306
307
308