Back to index

salome-med  6.5.0
MEDCalculatorBrowserLiteStruct.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 #include "MEDCalculatorBrowserLiteStruct.hxx"
00020 #include "MEDCalculatorBrowserStep.hxx"
00021 
00022 #include "MEDLoader.hxx"
00023 #include "MEDCouplingUMesh.hxx"
00024 #include "MEDCouplingFieldDouble.hxx"
00025 
00026 #include <sstream>
00027 #include <deque>
00028 #include <string>
00029 
00030 using namespace ParaMEDMEM;
00031 
00032 //  Default constructor
00033 MEDCalculatorBrowserLiteStruct::MEDCalculatorBrowserLiteStruct() : _any_selection(false)
00034 {
00035 }
00036 
00037 //  Destructor
00038 MEDCalculatorBrowserLiteStruct::~MEDCalculatorBrowserLiteStruct()
00039 {
00040 
00041 }
00042 
00043 //  Constructor with parameters :
00044 //  - f, full file name with path
00045 //  - n, file name (file.med)
00046 //  Read the med file to get meshes and fields informations
00047 //  Fill meshes vector with meshes names
00048 //  Fill fields vector creating using field constructor with MED and fieldname parameters
00049 MEDCalculatorBrowserLiteStruct::MEDCalculatorBrowserLiteStruct(const char *f) : _file(f), _any_selection(false)
00050 {
00051   computeBaseName();
00052   std::vector<std::string> meshNames=MEDLoader::GetMeshNames(_file.c_str());
00053   for(std::vector<std::string>::const_iterator iter=meshNames.begin();iter!=meshNames.end();iter++)
00054     _meshes.push_back(MEDCalculatorBrowserMesh((*iter).c_str()));
00055   std::vector<std::string> fieldNames=MEDLoader::GetAllFieldNames(_file.c_str());
00056   for(std::vector<std::string>::const_iterator iter=fieldNames.begin();iter!=fieldNames.end();iter++)
00057     _fields.push_back(MEDCalculatorBrowserField(_file.c_str(),(*iter).c_str()));
00058 }
00059 
00060 //  str method
00061 //  Construct a std::string to print LiteStruct, using std::cout for example
00062 //  Put x or o for selected or not
00063 //  Add Field::str() for each field
00064 //  Add Mesh::str() for each mesh
00065 //  Return a std::string
00066 std::string MEDCalculatorBrowserLiteStruct::str()
00067 {
00068   std::ostringstream res;
00069   _any_selection?res<<"x ":res<<"o ";
00070   res<<"LiteStruct "<<_name;
00071   for (unsigned int i = 0; i < _meshes.size(); i += 1)
00072     {
00073       res<<"\n\t"<<_meshes[i].str();
00074     }
00075   for (unsigned int i = 0; i < _fields.size(); i += 1)
00076     {
00077       res<<"\n\t"<<_fields[i].str();
00078     }
00079 
00080   return res.str();
00081 }
00082 
00083 //  Select all fields and meshes
00084 void MEDCalculatorBrowserLiteStruct::selectAll()
00085 {
00086   selectAllMeshes();
00087   selectAllFields();
00088   _any_selection = true;
00089 }
00090 
00091 //  Select all meshes
00092 void MEDCalculatorBrowserLiteStruct::selectAllMeshes()
00093 {
00094   for (unsigned int i = 0; i < _meshes.size(); i += 1)
00095     _meshes[i].select();
00096   _any_selection = true;
00097 }
00098 
00099 //  Select all fields
00100 void MEDCalculatorBrowserLiteStruct::selectAllFields()
00101 {
00102   for (unsigned int i = 0; i < _fields.size(); i += 1)
00103     {
00104       _fields[i].selectAllSteps();
00105       _fields[i].selectAllComponents();
00106     }
00107   _any_selection = true;
00108 }
00109 
00110 //  Unselect all fields and meshes
00111 void MEDCalculatorBrowserLiteStruct::unselectAll()
00112 {
00113   unselectAllMeshes();
00114   unselectAllFields();
00115   _any_selection = false;
00116 }
00117 
00118 //  Unselect all meshes
00119 void MEDCalculatorBrowserLiteStruct::unselectAllMeshes()
00120 {
00121   for (unsigned int i = 0; i < _meshes.size(); i += 1)
00122     _meshes[i].unselect();
00123   _any_selection=isSelection();
00124 }
00125 
00126 //  Unselect all fields
00127 void MEDCalculatorBrowserLiteStruct::unselectAllFields()
00128 {
00129   for (unsigned int i = 0; i < _fields.size(); i += 1)
00130     {
00131       _fields[i].unselectAllSteps();
00132       _fields[i].unselectAllComponents();
00133     }
00134   _any_selection=isSelection();
00135 }
00136 
00137 //  Return if there is any selection in this file or not
00138 bool MEDCalculatorBrowserLiteStruct::isSelection()
00139 {
00140   for (unsigned int i = 0; i < _meshes.size(); i += 1)
00141     {
00142       if(_meshes[i].isSelected()) return true;
00143     }
00144   for (unsigned int i = 0; i < _fields.size(); i += 1)
00145     {
00146       if(_fields[i].isSelected()) return true;
00147     }
00148   return false;
00149 }
00150 
00151 //  Return the name of the file
00152 const std::string& MEDCalculatorBrowserLiteStruct::getName() const
00153 {
00154   return _name;
00155 }
00156 
00157 //  Return the full path name of the file
00158 const std::string& MEDCalculatorBrowserLiteStruct::getFile() const
00159 {
00160   return _file;
00161 }
00162 
00163 //  Return the number of meshes
00164 unsigned int MEDCalculatorBrowserLiteStruct::getNumberOfMeshes()
00165 {
00166   return _meshes.size();
00167 }
00168 
00169 //  Return the number of fields
00170 unsigned int MEDCalculatorBrowserLiteStruct::getNumberOfFields()
00171 {
00172   return _fields.size();
00173 }
00174 
00175 //  Return the mesh name for the mesh at id = i
00176 const std::string& MEDCalculatorBrowserLiteStruct::getMeshName(int i) const
00177 {
00178   return _meshes[i].getName();
00179 }
00180 
00181 //  Return the field name for the field at id = i
00182 const std::string& MEDCalculatorBrowserLiteStruct::getFieldName(int i) const
00183 {
00184   return _fields[i].getName();
00185 }
00186 
00187 //  Return a non-const reference on the field at id = i
00188 MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(int i)
00189 {
00190   return _fields[i];
00191 }
00192 
00193 const MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(int i) const
00194 {
00195   return _fields[i];
00196 }
00197 
00198 //  Return a non-const reference on the field which name is equal to name
00199 MEDCalculatorBrowserField& MEDCalculatorBrowserLiteStruct::getField(const std::string& name)
00200 {
00201   return *std::find(_fields.begin(),_fields.end(),name);
00202 }
00203 
00204 //  Return a non-const reference on the mesh at id = i
00205 MEDCalculatorBrowserMesh& MEDCalculatorBrowserLiteStruct::getMesh(int i)
00206 {
00207   return _meshes[i];
00208 }
00209 
00210 //  Return a non-const reference on the mesh which name is equal to name
00211 MEDCalculatorBrowserMesh& MEDCalculatorBrowserLiteStruct::getMesh(const std::string& name)
00212 { 
00213   return *std::find(_meshes.begin(),_meshes.end(),name);
00214 }
00215 
00216 //  Select a mesh according to its name
00217 void MEDCalculatorBrowserLiteStruct::selectMesh(const std::string& name)
00218 {
00219   std::vector<MEDCalculatorBrowserMesh>::iterator it = std::find(_meshes.begin(),_meshes.end(),name);
00220   if(it != _meshes.end())
00221     it->select();
00222   _any_selection = true;
00223 }
00224 
00225 //  Select a field according to its name
00226 void MEDCalculatorBrowserLiteStruct::selectField(const std::string& name)
00227 {
00228   std::vector<MEDCalculatorBrowserField>::iterator it = std::find(_fields.begin(),_fields.end(),name);
00229   if(it != _fields.end()){
00230     it->selectAllSteps();
00231     it->selectAllComponents();
00232   }
00233   _any_selection = true;
00234 }
00235 
00236 //  Unselect a specific mesh according to its name
00237 //  Check if there is always selection
00238 void MEDCalculatorBrowserLiteStruct::unselectMesh(const std::string& name)
00239 {
00240   std::vector<MEDCalculatorBrowserMesh>::iterator it = std::find(_meshes.begin(),_meshes.end(),name);
00241   if(it != _meshes.end())
00242     it->unselect();
00243   _any_selection=isSelection();
00244 }
00245 
00246 //  Unselect a specific field according to its name
00247 //  check if there is always selection
00248 void MEDCalculatorBrowserLiteStruct::unselectField(const std::string& name)
00249 {
00250   std::vector<MEDCalculatorBrowserField>::iterator it = std::find(_fields.begin(),_fields.end(),name);
00251   if(it != _fields.end())
00252     {
00253       it->unselectAllSteps();
00254       it->unselectAllComponents();
00255     }
00256   _any_selection=isSelection();
00257 }
00258 
00259 //  Return a list of meshes names supporting time steps of a field
00260 std::vector<std::string> MEDCalculatorBrowserLiteStruct::getCorrespondingMeshesFromField(int fieldind)
00261 {
00262   return _fields[fieldind].getCorrespondingMeshesFromField();
00263 }
00264 
00265 //  Return a list of meshes supporting all fields of this file
00266 std::vector<std::string> MEDCalculatorBrowserLiteStruct::getCorrespondingMeshesFromLS()
00267 {
00268   std::vector<std::string> res;
00269   for (unsigned int i = 0; i < _meshes.size(); i += 1)
00270     res.push_back(_meshes[i].getName());
00271   return res;
00272 }
00273 
00274 //  Equal to string operator, compare simplified name to input
00275 bool MEDCalculatorBrowserLiteStruct::operator==(const std::string& nm)
00276 {
00277   return _name==nm;
00278 }
00279 
00280 //  Set selection to true
00281 void MEDCalculatorBrowserLiteStruct::setSelected(bool sel)
00282 {
00283   _any_selection=sel;
00284 }
00285 
00286 void MEDCalculatorBrowserLiteStruct::computeBaseName()
00287 {
00288   std::size_t p=_file.find_last_of("/");
00289   if(p!=std::string::npos)
00290     _name=_file.substr(p+1);
00291   else
00292     {
00293       _name=_file;
00294     }
00295 }