1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
20 #include "MEDCalculatorBrowserLiteStruct.hxx"
21 #include "MEDCalculatorBrowserStep.hxx"
23 #include "MEDLoader.hxx"
24 #include "MEDCouplingUMesh.hxx"
25 #include "MEDCouplingFieldDouble.hxx"
31 using namespace ParaMEDMEM;
33 // Default constructor
34 MEDCalculatorBrowserLiteStruct::MEDCalculatorBrowserLiteStruct() : _any_selection(false)
39 MEDCalculatorBrowserLiteStruct::~MEDCalculatorBrowserLiteStruct()
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 informations
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)
53 std::vector<std::string> meshNames=MEDLoader::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=MEDLoader::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()));
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()
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)
74 res<<"\n\t"<<_meshes[i].str();
76 for (unsigned int i = 0; i < _fields.size(); i += 1)
78 res<<"\n\t"<<_fields[i].str();
84 // Select all fields and meshes
85 void MEDCalculatorBrowserLiteStruct::selectAll()
89 _any_selection = true;
93 void MEDCalculatorBrowserLiteStruct::selectAllMeshes()
95 for (unsigned int i = 0; i < _meshes.size(); i += 1)
97 _any_selection = true;
101 void MEDCalculatorBrowserLiteStruct::selectAllFields()
103 for (unsigned int i = 0; i < _fields.size(); i += 1)
105 _fields[i].selectAllSteps();
106 _fields[i].selectAllComponents();
108 _any_selection = true;
111 // Unselect all fields and meshes
112 void MEDCalculatorBrowserLiteStruct::unselectAll()
116 _any_selection = false;
119 // Unselect all meshes
120 void MEDCalculatorBrowserLiteStruct::unselectAllMeshes()
122 for (unsigned int i = 0; i < _meshes.size(); i += 1)
123 _meshes[i].unselect();
124 _any_selection=isSelection();
127 // Unselect all fields
128 void MEDCalculatorBrowserLiteStruct::unselectAllFields()
130 for (unsigned int i = 0; i < _fields.size(); i += 1)
132 _fields[i].unselectAllSteps();
133 _fields[i].unselectAllComponents();
135 _any_selection=isSelection();
138 // Return if there is any selection in this file or not
139 bool MEDCalculatorBrowserLiteStruct::isSelection()
141 for (unsigned int i = 0; i < _meshes.size(); i += 1)
143 if(_meshes[i].isSelected()) return true;
145 for (unsigned int i = 0; i < _fields.size(); i += 1)
147 if(_fields[i].isSelected()) return true;
152 // Return the name of the file
153 const std::string& MEDCalculatorBrowserLiteStruct::getName() const
158 // Return the full path name of the file
159 const std::string& MEDCalculatorBrowserLiteStruct::getFile() const
164 // Return the number of meshes
165 unsigned int MEDCalculatorBrowserLiteStruct::getNumberOfMeshes()
167 return _meshes.size();
170 // Return the number of fields
171 unsigned int MEDCalculatorBrowserLiteStruct::getNumberOfFields()
173 return _fields.size();
176 // Return the mesh name for the mesh at id = i
177 const std::string& MEDCalculatorBrowserLiteStruct::getMeshName(int i) const
179 return _meshes[i].getName();
182 // Return the field name for the field at id = i
183 const std::string& MEDCalculatorBrowserLiteStruct::getFieldName(int i) const
185 return _fields[i].getName();
188 // Return a non-const reference on the field at id = i
189 MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(int i)
194 const MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(int i) const
199 // Return a non-const reference on the field which name is equal to name
200 MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(const std::string& name)
202 return *std::find(_fields.begin(),_fields.end(),name);
205 // Return a non-const reference on the mesh at id = i
206 MEDCalculatorBrowserMesh& MEDCalculatorBrowserLiteStruct::getMesh(int i)
211 // Return a non-const reference on the mesh which name is equal to name
212 MEDCalculatorBrowserMesh& MEDCalculatorBrowserLiteStruct::getMesh(const std::string& name)
214 return *std::find(_meshes.begin(),_meshes.end(),name);
217 // Select a mesh according to its name
218 void MEDCalculatorBrowserLiteStruct::selectMesh(const std::string& name)
220 std::vector<MEDCalculatorBrowserMesh>::iterator it = std::find(_meshes.begin(),_meshes.end(),name);
221 if(it != _meshes.end())
223 _any_selection = true;
226 // Select a field according to its name
227 void MEDCalculatorBrowserLiteStruct::selectField(const std::string& name)
229 std::vector<MEDCalculatorBrowserField>::iterator it = std::find(_fields.begin(),_fields.end(),name);
230 if(it != _fields.end()){
231 it->selectAllSteps();
232 it->selectAllComponents();
234 _any_selection = true;
237 // Unselect a specific mesh according to its name
238 // Check if there is always selection
239 void MEDCalculatorBrowserLiteStruct::unselectMesh(const std::string& name)
241 std::vector<MEDCalculatorBrowserMesh>::iterator it = std::find(_meshes.begin(),_meshes.end(),name);
242 if(it != _meshes.end())
244 _any_selection=isSelection();
247 // Unselect a specific field according to its name
248 // check if there is always selection
249 void MEDCalculatorBrowserLiteStruct::unselectField(const std::string& name)
251 std::vector<MEDCalculatorBrowserField>::iterator it = std::find(_fields.begin(),_fields.end(),name);
252 if(it != _fields.end())
254 it->unselectAllSteps();
255 it->unselectAllComponents();
257 _any_selection=isSelection();
260 // Return a list of meshes names supporting time steps of a field
261 std::vector<std::string> MEDCalculatorBrowserLiteStruct::getCorrespondingMeshesFromField(int fieldind)
263 return _fields[fieldind].getCorrespondingMeshesFromField();
266 // Return a list of meshes supporting all fields of this file
267 std::vector<std::string> MEDCalculatorBrowserLiteStruct::getCorrespondingMeshesFromLS()
269 std::vector<std::string> res;
270 for (unsigned int i = 0; i < _meshes.size(); i += 1)
271 res.push_back(_meshes[i].getName());
275 // Equal to string operator, compare simplified name to input
276 bool MEDCalculatorBrowserLiteStruct::operator==(const std::string& nm)
281 // Set selection to true
282 void MEDCalculatorBrowserLiteStruct::setSelected(bool sel)
287 void MEDCalculatorBrowserLiteStruct::computeBaseName()
289 std::size_t p=_file.find_last_of("/");
290 if(p!=std::string::npos)
291 _name=_file.substr(p+1);