Salome HOME
Merge from V6_main 13/12/2012
[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   int err_count = 0;
110
111   for (int i=0; i<NumberOfMeshes; i++)
112   {
113     const GMESH * myMesh = _fields.at(i)->getSupport()->getMesh();
114     writeMesh(myMesh) ;
115     for (unsigned j=0; j<_fields.size(); j++)
116     {
117       const FIELD_ * myField = _fields.at(j);
118       try
119       {
120         if( myMesh == myField->getSupport()->getMesh() )
121         {
122           if (MED_NODE == myField->getSupport()->getEntity())
123           {
124             if (myField->getSupport()->isOnAllElements())
125             {
126               writeField(myField,STRING(myField->getName()) << "_" << myField->getIterationNumber() << "_" << myField->getOrderNumber() ) ;
127             }
128             else
129             {
130               MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" which is not on all nodes !");
131             }
132           }
133         }
134       }
135       catch ( MED_EXCEPTION& e )
136         {
137           err_count++;
138           MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" : "<<e.what());
139       }
140     }
141
142     // second : field on cell
143     for (unsigned j=0; j<_fields.size(); j++)
144     {
145       const FIELD_ * myField = _fields.at(j);
146       try
147       {
148         if( myMesh == myField->getSupport()->getMesh() )
149         {
150           if (MED_CELL == myField->getSupport()->getEntity())
151           {
152             if (myField->getSupport()->isOnAllElements())
153             {
154               writeField(myField,STRING(myField->getName()) << "_" << myField->getIterationNumber() << "_" << myField->getOrderNumber() );
155             }
156             else
157             {
158               MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" which is not on all cells !");
159             }
160           }
161         }
162       }
163       catch ( MED_EXCEPTION& e )
164       {
165         err_count++;
166         MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" : "<<e.what());
167       }
168     }
169   } // loop on meshes
170
171   if (err_count > 0)
172     throw MED_EXCEPTION( LOCALIZED( STRING(LOC) << "Some errors have been found during writing !" ) );
173   END_OF_MED(LOC);
174 }
175
176 void VTK_MED_DRIVER::writeMesh(const GMESH * myMesh) const
177 {
178   const char * LOC = "VTK_MED_DRIVER::writeMesh() : ";
179   BEGIN_OF_MED(LOC);
180
181   VTK_MESH_DRIVER meshDriver( _fileName, myMesh );
182   meshDriver.write();
183
184   END_OF_MED(LOC);
185 }
186
187 void VTK_MED_DRIVER::writeField(const FIELD_ * myField,string name) const
188 {
189   const char* LOC = "VTK_MED_DRIVER::writeField() : ";
190   BEGIN_OF_MED(LOC);
191
192   med_type_champ type = myField->getValueType() ;
193   GENDRIVER* driver = 0;
194   switch (type)
195     {
196     case MED_INT32 :
197
198       if ( myField->getInterlacingType() == MED_FULL_INTERLACE )
199         driver = new VTK_FIELD_DRIVER<int>(_fileName,
200                                            static_cast< const FIELD<int,FullInterlace>* >(myField));
201
202       else if ( myField->getInterlacingType() == MED_NO_INTERLACE_BY_TYPE )
203         driver = new VTK_FIELD_DRIVER<int>(_fileName,
204                                            static_cast< const FIELD<int,NoInterlaceByType>* >(myField));
205
206       else
207         driver = new VTK_FIELD_DRIVER<int>(_fileName,
208                                            static_cast< const FIELD<int,NoInterlace>* >(myField));
209       break;
210
211     case MED_REEL64 : 
212
213       if ( myField->getInterlacingType() == MED_FULL_INTERLACE )
214         driver = new VTK_FIELD_DRIVER<double>(_fileName,
215                                               static_cast< const FIELD<double,FullInterlace>* >(myField));
216
217       else if ( myField->getInterlacingType() == MED_NO_INTERLACE_BY_TYPE )
218         driver = new VTK_FIELD_DRIVER<double>(_fileName,
219                                               static_cast< const FIELD<double,NoInterlaceByType>*>(myField));
220
221       else
222         driver = new VTK_FIELD_DRIVER<double>(_fileName,
223                                               static_cast< const FIELD<double,NoInterlace>* >(myField));
224       break;
225
226     default :
227       {
228         MESSAGE_MED(PREFIX_MED << "Could not write field "<<name<<" the type is not int or double !");
229       }
230     }
231
232   if ( driver )
233     {
234       driver->writeAppend();
235       delete driver;
236     }
237   END_OF_MED(LOC);
238 }
239
240 // void VTK_MED_DRIVER::writeSupport(SUPPORT * mySupport) const {
241 //   const char* LOC = "VTK_MED_DRIVER::writeSupport(SUPPORT *) : ";
242 //   BEGIN_OF_MED(LOC);
243 //   MESSAGE_MED(PREFIX_MED << "Not yet implemented, acting on the object " << *mySupport);
244 //   END_OF_MED(LOC);
245 // }