]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_VtkMedDriver.cxx
Salome HOME
Fix problem of make distcheck
[modules/med.git] / src / MEDMEM / MEDMEM_VtkMedDriver.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "MEDMEM_VtkMedDriver.hxx"
24
25 #include <sstream>
26
27 #include "MEDMEM_define.hxx"
28 #include "MEDMEM_Field.hxx"
29 #include "MEDMEM_Support.hxx"
30 #include "MEDMEM_Mesh.hxx"
31 #include "MEDMEM_CellModel.hxx"
32
33 using namespace std;
34 using namespace MEDMEM;
35 using namespace MED_EN;
36
37 VTK_MED_DRIVER::VTK_MED_DRIVER(): GENDRIVER(VTK_DRIVER), _fields(0)
38 {
39 }
40
41
42 VTK_MED_DRIVER::VTK_MED_DRIVER(const string & fileName,
43                                const vector< const FIELD_* >& fields):
44   GENDRIVER(fileName, MED_EN::RDWR, VTK_DRIVER), _fields( fields )
45 {
46 }
47
48 VTK_MED_DRIVER::VTK_MED_DRIVER(const VTK_MED_DRIVER & driver):
49   GENDRIVER(driver), _fields(driver._fields)
50 {
51 }
52
53 VTK_MED_DRIVER::~VTK_MED_DRIVER()
54 {
55   const char* LOC = "VTK_MED_DRIVER::~VTK_MED_DRIVER()";
56   BEGIN_OF_MED(LOC);
57
58   END_OF_MED(LOC);
59 }
60
61 GENDRIVER * VTK_MED_DRIVER::copy() const
62 {
63   return new VTK_MED_DRIVER(*this) ;
64 }
65
66 void VTK_MED_DRIVER::openConst() const
67 {
68   const char * LOC ="VTK_MED_DRIVER::open() : ";
69   BEGIN_OF_MED(LOC);
70
71   if ( _fileName == "" )
72     throw MED_EXCEPTION( LOCALIZED( STRING(LOC) 
73                                     << "_fileName is |\"\"|, please set a correct fileName before calling open()"));
74
75   // check if can open the file
76   ofstream _vtkFile;
77   _vtkFile.open(_fileName.c_str()); 
78   if (!_vtkFile)
79     throw MED_EXCEPTION( LOCALIZED( STRING(LOC) << "Could not open file "<< _fileName));
80   
81   END_OF_MED(LOC);
82 }
83
84 void VTK_MED_DRIVER::open() {
85   openConst() ;
86 }
87
88 void VTK_MED_DRIVER::closeConst() const {
89
90   const char* LOC = "VTK_MED_DRIVER::close() : ";
91   BEGIN_OF_MED(LOC);
92   END_OF_MED(LOC);
93 }
94
95 void VTK_MED_DRIVER::close() {
96   closeConst() ;
97 }
98
99
100 void VTK_MED_DRIVER::write() const
101 {
102   const char* LOC = "VTK_MED_DRIVER::write() : ";
103   BEGIN_OF_MED(LOC);
104
105   // VTK supports only one dataset per a file (in Simple Legacy Formats)
106   // so we write the first mesh only
107
108   const int NumberOfMeshes = ( !_fields.empty() ) ? 1 : 0;
109
110   for (int i=0; i<NumberOfMeshes; i++)
111   {
112     const GMESH * myMesh = _fields.at(i)->getSupport()->getMesh();
113     writeMesh(myMesh) ;
114     for (unsigned j=0; j<_fields.size(); j++)
115     {
116       const FIELD_ * myField = _fields.at(j);
117       if( myMesh == myField->getSupport()->getMesh() )
118       {
119         if (MED_NODE == myField->getSupport()->getEntity())
120         {
121           if (myField->getSupport()->isOnAllElements())
122           {
123             writeField(myField,STRING(myField->getName()) << "_" << myField->getIterationNumber() << "_" << myField->getOrderNumber() ) ;
124           }
125           else
126           {
127             MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" which is not on all nodes !");
128           }
129         }
130       }
131     }
132
133     // second : field on cell
134     for (unsigned j=0; j<_fields.size(); j++)
135     {
136       const FIELD_ * myField = _fields.at(j);
137       if( myMesh == myField->getSupport()->getMesh() )
138         if (MED_CELL == myField->getSupport()->getEntity())
139         {
140           if (myField->getSupport()->isOnAllElements())
141           {
142             writeField(myField,STRING(myField->getName()) << "_" << myField->getIterationNumber() << "_" << myField->getOrderNumber() );
143           }
144           else
145           {
146             MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" which is not on all cells !");
147           }
148         }
149     }
150   } // loop on meshes
151
152   END_OF_MED(LOC);
153 }
154
155 void VTK_MED_DRIVER::writeMesh(const GMESH * myMesh) const
156 {
157   const char * LOC = "VTK_MED_DRIVER::writeMesh() : ";
158   BEGIN_OF_MED(LOC);
159
160   VTK_MESH_DRIVER meshDriver( _fileName, myMesh );
161   meshDriver.write();
162
163   END_OF_MED(LOC);
164 }
165
166 void VTK_MED_DRIVER::writeField(const FIELD_ * myField,string name) const
167 {
168   const char* LOC = "VTK_MED_DRIVER::writeField() : ";
169   BEGIN_OF_MED(LOC);
170
171   med_type_champ type = myField->getValueType() ;
172   GENDRIVER* driver = 0;
173   switch (type)
174     {
175     case MED_INT32 :
176
177       if ( myField->getInterlacingType() == MED_FULL_INTERLACE )
178         driver = new VTK_FIELD_DRIVER<int>(_fileName,
179                                            static_cast< const FIELD<int,FullInterlace>* >(myField));
180
181       else if ( myField->getInterlacingType() == MED_NO_INTERLACE_BY_TYPE )
182         driver = new VTK_FIELD_DRIVER<int>(_fileName,
183                                            static_cast< const FIELD<int,NoInterlaceByType>* >(myField));
184
185       else
186         driver = new VTK_FIELD_DRIVER<int>(_fileName,
187                                            static_cast< const FIELD<int,NoInterlace>* >(myField));
188       break;
189
190     case MED_REEL64 : 
191
192       if ( myField->getInterlacingType() == MED_FULL_INTERLACE )
193         driver = new VTK_FIELD_DRIVER<double>(_fileName,
194                                               static_cast< const FIELD<double,FullInterlace>* >(myField));
195
196       else if ( myField->getInterlacingType() == MED_NO_INTERLACE_BY_TYPE )
197         driver = new VTK_FIELD_DRIVER<double>(_fileName,
198                                               static_cast< const FIELD<double,NoInterlaceByType>*>(myField));
199
200       else
201         driver = new VTK_FIELD_DRIVER<double>(_fileName,
202                                               static_cast< const FIELD<double,NoInterlace>* >(myField));
203       break;
204
205     default :
206       {
207         MESSAGE_MED(PREFIX_MED << "Could not write field "<<name<<" the type is not int or double !");
208       }
209     }
210
211   if ( driver )
212     {
213       driver->writeAppend();
214       delete driver;
215     }
216   END_OF_MED(LOC);
217 }
218
219 // void VTK_MED_DRIVER::writeSupport(SUPPORT * mySupport) const {
220 //   const char* LOC = "VTK_MED_DRIVER::writeSupport(SUPPORT *) : ";
221 //   BEGIN_OF_MED(LOC);
222 //   MESSAGE_MED(PREFIX_MED << "Not yet implemented, acting on the object " << *mySupport);
223 //   END_OF_MED(LOC);
224 // }