Salome HOME
51c08ee02075855d22c3c95b400cec23389c9bd1
[modules/med.git] / src / MEDCalculator / MEDCalculatorBrowserLiteStruct.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF 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, or (at your option) any later version.
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 // Author : Anthony Geay (CEA/DEN)
20 #include "MEDCalculatorBrowserLiteStruct.hxx"
21 #include "MEDCalculatorBrowserStep.hxx"
22
23 #include "MEDLoader.hxx"
24 #include "MEDCouplingUMesh.hxx"
25 #include "MEDCouplingFieldDouble.hxx"
26
27 #include <sstream>
28 #include <deque>
29 #include <string>
30
31 using namespace MEDCoupling;
32
33 //  Default constructor
34 MEDCalculatorBrowserLiteStruct::MEDCalculatorBrowserLiteStruct() : _any_selection(false)
35 {
36 }
37
38 //  Destructor
39 MEDCalculatorBrowserLiteStruct::~MEDCalculatorBrowserLiteStruct()
40 {
41
42 }
43
44 //  Constructor with parameters :
45 //  - f, full file name with path
46 //  - n, file name (file.med)
47 //  Read the med file to get meshes and fields information
48 //  Fill meshes vector with meshes names
49 //  Fill fields vector creating using field constructor with MED and fieldname parameters
50 MEDCalculatorBrowserLiteStruct::MEDCalculatorBrowserLiteStruct(const char *f) : _file(f), _any_selection(false)
51 {
52   computeBaseName();
53   std::vector<std::string> meshNames=GetMeshNames(_file.c_str());
54   for(std::vector<std::string>::const_iterator iter=meshNames.begin();iter!=meshNames.end();iter++)
55     _meshes.push_back(MEDCalculatorBrowserMesh((*iter).c_str()));
56   std::vector<std::string> fieldNames=GetAllFieldNames(_file.c_str());
57   for(std::vector<std::string>::const_iterator iter=fieldNames.begin();iter!=fieldNames.end();iter++)
58     _fields.push_back(MEDCalculatorBrowserField(_file.c_str(),(*iter).c_str()));
59 }
60
61 //  str method
62 //  Construct a std::string to print LiteStruct, using std::cout for example
63 //  Put x or o for selected or not
64 //  Add Field::str() for each field
65 //  Add Mesh::str() for each mesh
66 //  Return a std::string
67 std::string MEDCalculatorBrowserLiteStruct::str()
68 {
69   std::ostringstream res;
70   _any_selection?res<<"x ":res<<"o ";
71   res<<"LiteStruct "<<_name;
72   for (unsigned int i = 0; i < _meshes.size(); i += 1)
73     {
74       res<<"\n\t"<<_meshes[i].str();
75     }
76   for (unsigned int i = 0; i < _fields.size(); i += 1)
77     {
78       res<<"\n\t"<<_fields[i].str();
79     }
80
81   return res.str();
82 }
83
84 //  Select all fields and meshes
85 void MEDCalculatorBrowserLiteStruct::selectAll()
86 {
87   selectAllMeshes();
88   selectAllFields();
89   _any_selection = true;
90 }
91
92 //  Select all meshes
93 void MEDCalculatorBrowserLiteStruct::selectAllMeshes()
94 {
95   for (unsigned int i = 0; i < _meshes.size(); i += 1)
96     _meshes[i].select();
97   _any_selection = true;
98 }
99
100 //  Select all fields
101 void MEDCalculatorBrowserLiteStruct::selectAllFields()
102 {
103   for (unsigned int i = 0; i < _fields.size(); i += 1)
104     {
105       _fields[i].selectAllSteps();
106       _fields[i].selectAllComponents();
107     }
108   _any_selection = true;
109 }
110
111 //  Unselect all fields and meshes
112 void MEDCalculatorBrowserLiteStruct::unselectAll()
113 {
114   unselectAllMeshes();
115   unselectAllFields();
116   _any_selection = false;
117 }
118
119 //  Unselect all meshes
120 void MEDCalculatorBrowserLiteStruct::unselectAllMeshes()
121 {
122   for (unsigned int i = 0; i < _meshes.size(); i += 1)
123     _meshes[i].unselect();
124   _any_selection=isSelection();
125 }
126
127 //  Unselect all fields
128 void MEDCalculatorBrowserLiteStruct::unselectAllFields()
129 {
130   for (unsigned int i = 0; i < _fields.size(); i += 1)
131     {
132       _fields[i].unselectAllSteps();
133       _fields[i].unselectAllComponents();
134     }
135   _any_selection=isSelection();
136 }
137
138 //  Return if there is any selection in this file or not
139 bool MEDCalculatorBrowserLiteStruct::isSelection()
140 {
141   for (unsigned int i = 0; i < _meshes.size(); i += 1)
142     {
143       if(_meshes[i].isSelected()) return true;
144     }
145   for (unsigned int i = 0; i < _fields.size(); i += 1)
146     {
147       if(_fields[i].isSelected()) return true;
148     }
149   return false;
150 }
151
152 //  Return the name of the file
153 const std::string& MEDCalculatorBrowserLiteStruct::getName() const
154 {
155   return _name;
156 }
157
158 //  Return the full path name of the file
159 const std::string& MEDCalculatorBrowserLiteStruct::getFile() const
160 {
161   return _file;
162 }
163
164 //  Return the number of meshes
165 unsigned int MEDCalculatorBrowserLiteStruct::getNumberOfMeshes()
166 {
167   return _meshes.size();
168 }
169
170 //  Return the number of fields
171 unsigned int MEDCalculatorBrowserLiteStruct::getNumberOfFields()
172 {
173   return _fields.size();
174 }
175
176 //  Return the mesh name for the mesh at id = i
177 const std::string& MEDCalculatorBrowserLiteStruct::getMeshName(int i) const
178 {
179   return _meshes[i].getName();
180 }
181
182 //  Return the field name for the field at id = i
183 const std::string& MEDCalculatorBrowserLiteStruct::getFieldName(int i) const
184 {
185   return _fields[i].getName();
186 }
187
188 //  Return a non-const reference on the field at id = i
189 MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(int i)
190 {
191   return _fields[i];
192 }
193
194 const MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(int i) const
195 {
196   return _fields[i];
197 }
198
199 //  Return a non-const reference on the field which name is equal to name
200 MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(const std::string& name)
201 {
202   return *std::find(_fields.begin(),_fields.end(),name);
203 }
204
205 //  Return a non-const reference on the mesh at id = i
206 MEDCalculatorBrowserMesh& MEDCalculatorBrowserLiteStruct::getMesh(int i)
207 {
208   return _meshes[i];
209 }
210
211 //  Return a non-const reference on the mesh which name is equal to name
212 MEDCalculatorBrowserMesh& MEDCalculatorBrowserLiteStruct::getMesh(const std::string& name)
213
214   return *std::find(_meshes.begin(),_meshes.end(),name);
215 }
216
217 //  Select a mesh according to its name
218 void MEDCalculatorBrowserLiteStruct::selectMesh(const std::string& name)
219 {
220   std::vector<MEDCalculatorBrowserMesh>::iterator it = std::find(_meshes.begin(),_meshes.end(),name);
221   if(it != _meshes.end())
222     it->select();
223   _any_selection = true;
224 }
225
226 //  Select a field according to its name
227 void MEDCalculatorBrowserLiteStruct::selectField(const std::string& name)
228 {
229   std::vector<MEDCalculatorBrowserField>::iterator it = std::find(_fields.begin(),_fields.end(),name);
230   if(it != _fields.end()){
231     it->selectAllSteps();
232     it->selectAllComponents();
233   }
234   _any_selection = true;
235 }
236
237 //  Unselect a specific mesh according to its name
238 //  Check if there is always selection
239 void MEDCalculatorBrowserLiteStruct::unselectMesh(const std::string& name)
240 {
241   std::vector<MEDCalculatorBrowserMesh>::iterator it = std::find(_meshes.begin(),_meshes.end(),name);
242   if(it != _meshes.end())
243     it->unselect();
244   _any_selection=isSelection();
245 }
246
247 //  Unselect a specific field according to its name
248 //  check if there is always selection
249 void MEDCalculatorBrowserLiteStruct::unselectField(const std::string& name)
250 {
251   std::vector<MEDCalculatorBrowserField>::iterator it = std::find(_fields.begin(),_fields.end(),name);
252   if(it != _fields.end())
253     {
254       it->unselectAllSteps();
255       it->unselectAllComponents();
256     }
257   _any_selection=isSelection();
258 }
259
260 //  Return a list of meshes names supporting time steps of a field
261 std::vector<std::string> MEDCalculatorBrowserLiteStruct::getCorrespondingMeshesFromField(int fieldind)
262 {
263   return _fields[fieldind].getCorrespondingMeshesFromField();
264 }
265
266 //  Return a list of meshes supporting all fields of this file
267 std::vector<std::string> MEDCalculatorBrowserLiteStruct::getCorrespondingMeshesFromLS()
268 {
269   std::vector<std::string> res;
270   for (unsigned int i = 0; i < _meshes.size(); i += 1)
271     res.push_back(_meshes[i].getName());
272   return res;
273 }
274
275 //  Equal to string operator, compare simplified name to input
276 bool MEDCalculatorBrowserLiteStruct::operator==(const std::string& nm)
277 {
278   return _name==nm;
279 }
280
281 //  Set selection to true
282 void MEDCalculatorBrowserLiteStruct::setSelected(bool sel)
283 {
284   _any_selection=sel;
285 }
286
287 void MEDCalculatorBrowserLiteStruct::computeBaseName()
288 {
289   std::size_t p=_file.find_last_of("/");
290   if(p!=std::string::npos)
291     _name=_file.substr(p+1);
292   else
293     {
294       _name=_file;
295     }
296 }