1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "MEDMEM_VtkMedDriver.hxx"
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"
34 using namespace MEDMEM;
35 using namespace MED_EN;
37 VTK_MED_DRIVER::VTK_MED_DRIVER(): GENDRIVER(VTK_DRIVER), _fields(0)
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 )
48 VTK_MED_DRIVER::VTK_MED_DRIVER(const VTK_MED_DRIVER & driver):
49 GENDRIVER(driver), _fields(driver._fields)
53 VTK_MED_DRIVER::~VTK_MED_DRIVER()
55 const char* LOC = "VTK_MED_DRIVER::~VTK_MED_DRIVER()";
61 GENDRIVER * VTK_MED_DRIVER::copy() const
63 return new VTK_MED_DRIVER(*this) ;
66 void VTK_MED_DRIVER::openConst() const
68 const char * LOC ="VTK_MED_DRIVER::open() : ";
71 if ( _fileName == "" )
72 throw MED_EXCEPTION( LOCALIZED( STRING(LOC)
73 << "_fileName is |\"\"|, please set a correct fileName before calling open()"));
75 // check if can open the file
77 _vtkFile.open(_fileName.c_str());
79 throw MED_EXCEPTION( LOCALIZED( STRING(LOC) << "Could not open file "<< _fileName));
84 void VTK_MED_DRIVER::open() {
88 void VTK_MED_DRIVER::closeConst() const {
90 const char* LOC = "VTK_MED_DRIVER::close() : ";
95 void VTK_MED_DRIVER::close() {
100 void VTK_MED_DRIVER::write() const
102 const char* LOC = "VTK_MED_DRIVER::write() : ";
105 // VTK supports only one dataset per a file (in Simple Legacy Formats)
106 // so we write the first mesh only
108 const int NumberOfMeshes = ( !_fields.empty() ) ? 1 : 0;
111 for (int i=0; i<NumberOfMeshes; i++)
113 const GMESH * myMesh = _fields.at(i)->getSupport()->getMesh();
115 for (unsigned j=0; j<_fields.size(); j++)
117 const FIELD_ * myField = _fields.at(j);
120 if( myMesh == myField->getSupport()->getMesh() )
122 if (MED_NODE == myField->getSupport()->getEntity())
124 if (myField->getSupport()->isOnAllElements())
126 writeField(myField,STRING(myField->getName()) << "_" << myField->getIterationNumber() << "_" << myField->getOrderNumber() ) ;
130 MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" which is not on all nodes !");
135 catch ( MED_EXCEPTION& e )
138 MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" : "<<e.what());
142 // second : field on cell
143 for (unsigned j=0; j<_fields.size(); j++)
145 const FIELD_ * myField = _fields.at(j);
148 if( myMesh == myField->getSupport()->getMesh() )
150 if (MED_CELL == myField->getSupport()->getEntity())
152 if (myField->getSupport()->isOnAllElements())
154 writeField(myField,STRING(myField->getName()) << "_" << myField->getIterationNumber() << "_" << myField->getOrderNumber() );
158 MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" which is not on all cells !");
163 catch ( MED_EXCEPTION& e )
166 MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" : "<<e.what());
172 throw MED_EXCEPTION( LOCALIZED( STRING(LOC) << "Some errors have been found during writing !" ) );
176 void VTK_MED_DRIVER::writeMesh(const GMESH * myMesh) const
178 const char * LOC = "VTK_MED_DRIVER::writeMesh() : ";
181 VTK_MESH_DRIVER meshDriver( _fileName, myMesh );
187 void VTK_MED_DRIVER::writeField(const FIELD_ * myField,string name) const
189 const char* LOC = "VTK_MED_DRIVER::writeField() : ";
192 med_type_champ type = myField->getValueType() ;
193 GENDRIVER* driver = 0;
198 if ( myField->getInterlacingType() == MED_FULL_INTERLACE )
199 driver = new VTK_FIELD_DRIVER<int>(_fileName,
200 static_cast< const FIELD<int,FullInterlace>* >(myField));
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));
207 driver = new VTK_FIELD_DRIVER<int>(_fileName,
208 static_cast< const FIELD<int,NoInterlace>* >(myField));
213 if ( myField->getInterlacingType() == MED_FULL_INTERLACE )
214 driver = new VTK_FIELD_DRIVER<double>(_fileName,
215 static_cast< const FIELD<double,FullInterlace>* >(myField));
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));
222 driver = new VTK_FIELD_DRIVER<double>(_fileName,
223 static_cast< const FIELD<double,NoInterlace>* >(myField));
228 MESSAGE_MED(PREFIX_MED << "Could not write field "<<name<<" the type is not int or double !");
234 driver->writeAppend();
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);