Back to index

salome-med  6.5.0
MEDMEM_VtkMedDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "MEDMEM_VtkMedDriver.hxx"
00024 
00025 #include <sstream>
00026 
00027 #include "MEDMEM_define.hxx"
00028 #include "MEDMEM_Field.hxx"
00029 #include "MEDMEM_Support.hxx"
00030 #include "MEDMEM_Mesh.hxx"
00031 #include "MEDMEM_CellModel.hxx"
00032 
00033 using namespace std;
00034 using namespace MEDMEM;
00035 using namespace MED_EN;
00036 
00037 VTK_MED_DRIVER::VTK_MED_DRIVER(): GENDRIVER(VTK_DRIVER), _fields(0)
00038 {
00039 }
00040 
00041 
00042 VTK_MED_DRIVER::VTK_MED_DRIVER(const string & fileName,
00043                                const vector< const FIELD_* >& fields):
00044   GENDRIVER(fileName, MED_EN::RDWR, VTK_DRIVER), _fields( fields )
00045 {
00046 }
00047 
00048 VTK_MED_DRIVER::VTK_MED_DRIVER(const VTK_MED_DRIVER & driver):
00049   GENDRIVER(driver), _fields(driver._fields)
00050 {
00051 }
00052 
00053 VTK_MED_DRIVER::~VTK_MED_DRIVER()
00054 {
00055   const char* LOC = "VTK_MED_DRIVER::~VTK_MED_DRIVER()";
00056   BEGIN_OF_MED(LOC);
00057 
00058   END_OF_MED(LOC);
00059 }
00060 
00061 GENDRIVER * VTK_MED_DRIVER::copy() const
00062 {
00063   return new VTK_MED_DRIVER(*this) ;
00064 }
00065 
00066 void VTK_MED_DRIVER::openConst() const
00067 {
00068   const char * LOC ="VTK_MED_DRIVER::open() : ";
00069   BEGIN_OF_MED(LOC);
00070 
00071   if ( _fileName == "" )
00072     throw MED_EXCEPTION( LOCALIZED( STRING(LOC) 
00073                                     << "_fileName is |\"\"|, please set a correct fileName before calling open()"));
00074 
00075   // check if can open the file
00076   ofstream _vtkFile;
00077   _vtkFile.open(_fileName.c_str()); 
00078   if (!_vtkFile)
00079     throw MED_EXCEPTION( LOCALIZED( STRING(LOC) << "Could not open file "<< _fileName));
00080   
00081   END_OF_MED(LOC);
00082 }
00083 
00084 void VTK_MED_DRIVER::open() {
00085   openConst() ;
00086 }
00087 
00088 void VTK_MED_DRIVER::closeConst() const {
00089 
00090   const char* LOC = "VTK_MED_DRIVER::close() : ";
00091   BEGIN_OF_MED(LOC);
00092   END_OF_MED(LOC);
00093 }
00094 
00095 void VTK_MED_DRIVER::close() {
00096   closeConst() ;
00097 }
00098 
00099 
00100 void VTK_MED_DRIVER::write() const
00101 {
00102   const char* LOC = "VTK_MED_DRIVER::write() : ";
00103   BEGIN_OF_MED(LOC);
00104 
00105   // VTK supports only one dataset per a file (in Simple Legacy Formats)
00106   // so we write the first mesh only
00107 
00108   const int NumberOfMeshes = ( !_fields.empty() ) ? 1 : 0;
00109 
00110   for (int i=0; i<NumberOfMeshes; i++)
00111   {
00112     const GMESH * myMesh = _fields.at(i)->getSupport()->getMesh();
00113     writeMesh(myMesh) ;
00114     for (unsigned j=0; j<_fields.size(); j++)
00115     {
00116       const FIELD_ * myField = _fields.at(j);
00117       if( myMesh == myField->getSupport()->getMesh() )
00118       {
00119         if (MED_NODE == myField->getSupport()->getEntity())
00120         {
00121           if (myField->getSupport()->isOnAllElements())
00122           {
00123             writeField(myField,STRING(myField->getName()) << "_" << myField->getIterationNumber() << "_" << myField->getOrderNumber() ) ;
00124           }
00125           else
00126           {
00127             MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" which is not on all nodes !");
00128           }
00129         }
00130       }
00131     }
00132 
00133     // second : field on cell
00134     for (unsigned j=0; j<_fields.size(); j++)
00135     {
00136       const FIELD_ * myField = _fields.at(j);
00137       if( myMesh == myField->getSupport()->getMesh() )
00138         if (MED_CELL == myField->getSupport()->getEntity())
00139         {
00140           if (myField->getSupport()->isOnAllElements())
00141           {
00142             writeField(myField,STRING(myField->getName()) << "_" << myField->getIterationNumber() << "_" << myField->getOrderNumber() );
00143           }
00144           else
00145           {
00146             MESSAGE_MED(PREFIX_MED << "Could not write field "<<myField->getName()<<" which is not on all cells !");
00147           }
00148         }
00149     }
00150   } // loop on meshes
00151 
00152   END_OF_MED(LOC);
00153 }
00154 
00155 void VTK_MED_DRIVER::writeMesh(const GMESH * myMesh) const
00156 {
00157   const char * LOC = "VTK_MED_DRIVER::writeMesh() : ";
00158   BEGIN_OF_MED(LOC);
00159 
00160   VTK_MESH_DRIVER meshDriver( _fileName, myMesh );
00161   meshDriver.write();
00162 
00163   END_OF_MED(LOC);
00164 }
00165 
00166 void VTK_MED_DRIVER::writeField(const FIELD_ * myField,string name) const
00167 {
00168   const char* LOC = "VTK_MED_DRIVER::writeField() : ";
00169   BEGIN_OF_MED(LOC);
00170 
00171   med_type_champ type = myField->getValueType() ;
00172   GENDRIVER* driver = 0;
00173   switch (type)
00174     {
00175     case MED_INT32 :
00176 
00177       if ( myField->getInterlacingType() == MED_FULL_INTERLACE )
00178         driver = new VTK_FIELD_DRIVER<int>(_fileName,
00179                                            static_cast< const FIELD<int,FullInterlace>* >(myField));
00180 
00181       else if ( myField->getInterlacingType() == MED_NO_INTERLACE_BY_TYPE )
00182         driver = new VTK_FIELD_DRIVER<int>(_fileName,
00183                                            static_cast< const FIELD<int,NoInterlaceByType>* >(myField));
00184 
00185       else
00186         driver = new VTK_FIELD_DRIVER<int>(_fileName,
00187                                            static_cast< const FIELD<int,NoInterlace>* >(myField));
00188       break;
00189 
00190     case MED_REEL64 : 
00191 
00192       if ( myField->getInterlacingType() == MED_FULL_INTERLACE )
00193         driver = new VTK_FIELD_DRIVER<double>(_fileName,
00194                                               static_cast< const FIELD<double,FullInterlace>* >(myField));
00195 
00196       else if ( myField->getInterlacingType() == MED_NO_INTERLACE_BY_TYPE )
00197         driver = new VTK_FIELD_DRIVER<double>(_fileName,
00198                                               static_cast< const FIELD<double,NoInterlaceByType>*>(myField));
00199 
00200       else
00201         driver = new VTK_FIELD_DRIVER<double>(_fileName,
00202                                               static_cast< const FIELD<double,NoInterlace>* >(myField));
00203       break;
00204 
00205     default :
00206       {
00207         MESSAGE_MED(PREFIX_MED << "Could not write field "<<name<<" the type is not int or double !");
00208       }
00209     }
00210 
00211   if ( driver )
00212     {
00213       driver->writeAppend();
00214       delete driver;
00215     }
00216   END_OF_MED(LOC);
00217 }
00218 
00219 // void VTK_MED_DRIVER::writeSupport(SUPPORT * mySupport) const {
00220 //   const char* LOC = "VTK_MED_DRIVER::writeSupport(SUPPORT *) : ";
00221 //   BEGIN_OF_MED(LOC);
00222 //   MESSAGE_MED(PREFIX_MED << "Not yet implemented, acting on the object " << *mySupport);
00223 //   END_OF_MED(LOC);
00224 // }