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