1 // Copyright (C) 2007-2012 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.
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 #include "MEDCalculatorBrowserLiteStruct.hxx"
20 #include "MEDCalculatorBrowserStep.hxx"
22 #include "MEDLoader.hxx"
23 #include "MEDCouplingUMesh.hxx"
24 #include "MEDCouplingFieldDouble.hxx"
30 using namespace ParaMEDMEM;
32 // Default constructor
33 MEDCalculatorBrowserLiteStruct::MEDCalculatorBrowserLiteStruct() : _any_selection(false)
38 MEDCalculatorBrowserLiteStruct::~MEDCalculatorBrowserLiteStruct()
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)
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()));
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()
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)
73 res<<"\n\t"<<_meshes[i].str();
75 for (unsigned int i = 0; i < _fields.size(); i += 1)
77 res<<"\n\t"<<_fields[i].str();
83 // Select all fields and meshes
84 void MEDCalculatorBrowserLiteStruct::selectAll()
88 _any_selection = true;
92 void MEDCalculatorBrowserLiteStruct::selectAllMeshes()
94 for (unsigned int i = 0; i < _meshes.size(); i += 1)
96 _any_selection = true;
100 void MEDCalculatorBrowserLiteStruct::selectAllFields()
102 for (unsigned int i = 0; i < _fields.size(); i += 1)
104 _fields[i].selectAllSteps();
105 _fields[i].selectAllComponents();
107 _any_selection = true;
110 // Unselect all fields and meshes
111 void MEDCalculatorBrowserLiteStruct::unselectAll()
115 _any_selection = false;
118 // Unselect all meshes
119 void MEDCalculatorBrowserLiteStruct::unselectAllMeshes()
121 for (unsigned int i = 0; i < _meshes.size(); i += 1)
122 _meshes[i].unselect();
123 _any_selection=isSelection();
126 // Unselect all fields
127 void MEDCalculatorBrowserLiteStruct::unselectAllFields()
129 for (unsigned int i = 0; i < _fields.size(); i += 1)
131 _fields[i].unselectAllSteps();
132 _fields[i].unselectAllComponents();
134 _any_selection=isSelection();
137 // Return if there is any selection in this file or not
138 bool MEDCalculatorBrowserLiteStruct::isSelection()
140 for (unsigned int i = 0; i < _meshes.size(); i += 1)
142 if(_meshes[i].isSelected()) return true;
144 for (unsigned int i = 0; i < _fields.size(); i += 1)
146 if(_fields[i].isSelected()) return true;
151 // Return the name of the file
152 const std::string& MEDCalculatorBrowserLiteStruct::getName() const
157 // Return the full path name of the file
158 const std::string& MEDCalculatorBrowserLiteStruct::getFile() const
163 // Return the number of meshes
164 unsigned int MEDCalculatorBrowserLiteStruct::getNumberOfMeshes()
166 return _meshes.size();
169 // Return the number of fields
170 unsigned int MEDCalculatorBrowserLiteStruct::getNumberOfFields()
172 return _fields.size();
175 // Return the mesh name for the mesh at id = i
176 const std::string& MEDCalculatorBrowserLiteStruct::getMeshName(int i) const
178 return _meshes[i].getName();
181 // Return the field name for the field at id = i
182 const std::string& MEDCalculatorBrowserLiteStruct::getFieldName(int i) const
184 return _fields[i].getName();
187 // Return a non-const reference on the field at id = i
188 MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(int i)
193 const MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(int i) const
198 // Return a non-const reference on the field which name is equal to name
199 MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(const std::string& name)
201 return *std::find(_fields.begin(),_fields.end(),name);
204 // Return a non-const reference on the mesh at id = i
205 MEDCalculatorBrowserMesh& MEDCalculatorBrowserLiteStruct::getMesh(int i)
210 // Return a non-const reference on the mesh which name is equal to name
211 MEDCalculatorBrowserMesh& MEDCalculatorBrowserLiteStruct::getMesh(const std::string& name)
213 return *std::find(_meshes.begin(),_meshes.end(),name);
216 // Select a mesh according to its name
217 void MEDCalculatorBrowserLiteStruct::selectMesh(const std::string& name)
219 std::vector<MEDCalculatorBrowserMesh>::iterator it = std::find(_meshes.begin(),_meshes.end(),name);
220 if(it != _meshes.end())
222 _any_selection = true;
225 // Select a field according to its name
226 void MEDCalculatorBrowserLiteStruct::selectField(const std::string& name)
228 std::vector<MEDCalculatorBrowserField>::iterator it = std::find(_fields.begin(),_fields.end(),name);
229 if(it != _fields.end()){
230 it->selectAllSteps();
231 it->selectAllComponents();
233 _any_selection = true;
236 // Unselect a specific mesh according to its name
237 // Check if there is always selection
238 void MEDCalculatorBrowserLiteStruct::unselectMesh(const std::string& name)
240 std::vector<MEDCalculatorBrowserMesh>::iterator it = std::find(_meshes.begin(),_meshes.end(),name);
241 if(it != _meshes.end())
243 _any_selection=isSelection();
246 // Unselect a specific field according to its name
247 // check if there is always selection
248 void MEDCalculatorBrowserLiteStruct::unselectField(const std::string& name)
250 std::vector<MEDCalculatorBrowserField>::iterator it = std::find(_fields.begin(),_fields.end(),name);
251 if(it != _fields.end())
253 it->unselectAllSteps();
254 it->unselectAllComponents();
256 _any_selection=isSelection();
259 // Return a list of meshes names supporting time steps of a field
260 std::vector<std::string> MEDCalculatorBrowserLiteStruct::getCorrespondingMeshesFromField(int fieldind)
262 return _fields[fieldind].getCorrespondingMeshesFromField();
265 // Return a list of meshes supporting all fields of this file
266 std::vector<std::string> MEDCalculatorBrowserLiteStruct::getCorrespondingMeshesFromLS()
268 std::vector<std::string> res;
269 for (unsigned int i = 0; i < _meshes.size(); i += 1)
270 res.push_back(_meshes[i].getName());
274 // Equal to string operator, compare simplified name to input
275 bool MEDCalculatorBrowserLiteStruct::operator==(const std::string& nm)
280 // Set selection to true
281 void MEDCalculatorBrowserLiteStruct::setSelected(bool sel)
286 void MEDCalculatorBrowserLiteStruct::computeBaseName()
288 std::size_t p=_file.find_last_of("/");
289 if(p!=std::string::npos)
290 _name=_file.substr(p+1);