Salome HOME
6958cb02a384cedd077bfbbf303425f14192b04a
[tools/medcoupling.git] / src / MEDLoader / MEDFileMeshSupport.cxx
1 // Copyright (C) 2007-2017  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 (EDF R&D)
20
21 #include "MEDFileMeshSupport.hxx"
22 #include "MEDLoaderBase.hxx"
23 #include "MEDFileMeshLL.hxx"
24 #include "MEDFileSafeCaller.txx"
25
26 #include "InterpKernelAutoPtr.hxx"
27
28 using namespace MEDCoupling;
29
30 MEDFileMeshSupports *MEDFileMeshSupports::New(const std::string& fileName)
31 {
32   MEDFileUtilities::AutoFid fid(OpenMEDFileForRead(fileName));
33   return New(fid);
34 }
35
36 MEDFileMeshSupports *MEDFileMeshSupports::New(med_idt fid)
37 {
38   return new MEDFileMeshSupports(fid);
39 }
40
41 MEDFileMeshSupports *MEDFileMeshSupports::New()
42 {
43   return new MEDFileMeshSupports;
44 }
45
46 MEDFileMeshSupports::MEDFileMeshSupports(med_idt fid)
47 {
48   int nbSM(MEDnSupportMesh(fid));
49   _supports.resize(nbSM);
50   for(int i=0;i<nbSM;i++)
51     {
52       INTERP_KERNEL::AutoPtr<char> msn(MEDLoaderBase::buildEmptyString(MED_NAME_SIZE));
53       INTERP_KERNEL::AutoPtr<char> description(MEDLoaderBase::buildEmptyString(MED_COMMENT_SIZE));
54       med_axis_type axType;
55       int nAxis(MEDsupportMeshnAxis(fid,i+1));
56       INTERP_KERNEL::AutoPtr<char> axisName(new char[MED_SNAME_SIZE*nAxis+1]),axisUnit(new char[MED_SNAME_SIZE*nAxis+1]);
57       int spaceDim(0),meshDim(0);
58       MEDFILESAFECALLERRD0(MEDsupportMeshInfo,(fid,i+1,msn,&spaceDim,&meshDim,description,&axType,axisName,axisUnit));
59       std::string name(MEDLoaderBase::buildStringFromFortran(msn,MED_NAME_SIZE));
60       _supports[i]=MEDFileUMesh::New(fid,name);
61     }
62 }
63
64 MEDFileMeshSupports::MEDFileMeshSupports()
65 {
66 }
67
68 MEDFileMeshSupports::~MEDFileMeshSupports()
69 {
70 }
71
72 std::vector<const BigMemoryObject *> MEDFileMeshSupports::getDirectChildrenWithNull() const
73 {
74   std::size_t sz(_supports.size());
75   std::vector<const BigMemoryObject *> ret(sz);
76   for(std::size_t i=0;i<sz;i++)
77     ret[i]=_supports[i];
78   return ret;
79 }
80
81 std::size_t MEDFileMeshSupports::getHeapMemorySizeWithoutChildren() const
82 {
83   return _supports.capacity()*sizeof(MCAuto<MEDFileUMesh>);
84 }
85
86 void MEDFileMeshSupports::writeLL(med_idt fid) const
87 {
88   for(std::vector< MCAuto<MEDFileUMesh> >::const_iterator it=_supports.begin();it!=_supports.end();it++)
89     if((*it).isNotNull())
90       (*it)->writeLL(fid);
91 }
92
93 const MEDFileUMesh *MEDFileMeshSupports::getSupMeshWithName(const std::string& name) const
94 {
95   std::vector<std::string> mns;
96   for(std::vector< MCAuto<MEDFileUMesh> >::const_iterator it=_supports.begin();it!=_supports.end();it++)
97     {
98       if((*it).isNotNull())
99         {
100           std::string na((*it)->getName());
101           if(na==name)
102             return *it;
103           else
104             mns.push_back(na);
105         }
106     }
107   std::ostringstream oss;
108   oss << "MEDFileMeshSupports::getSupMeshWithName : no such name \"" << name << "\". Possibilitities are :";
109   std::copy(mns.begin(),mns.end(),std::ostream_iterator<std::string>(oss,","));
110   oss << " !";
111   throw INTERP_KERNEL::Exception(oss.str());
112 }
113
114 int MEDFileMeshSupports::getNumberOfNodesPerCellOf(const std::string& name) const
115 {
116   const MEDFileUMesh *sup(getSupMeshWithName(name));
117   return sup->getNumberOfNodes();
118 }