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 #ifndef VTK_MESH_DRIVER_HXX
24 #define VTK_MESH_DRIVER_HXX
32 #include "MEDMEM_define.hxx"
33 #include "MEDMEM_GenDriver.hxx"
35 #include "MEDMEM_STRING.hxx"
36 #include "MEDMEM_Exception.hxx"
37 #include "MEDMEM_Utilities.hxx"
48 Driver Vtk for MESH (only for writing).
50 Generic part : implement open and close methods.
58 class MEDMEM_EXPORT _VTK_BinaryWriter;
60 class MEDMEM_EXPORT VTK_MESH_DRIVER : public GENDRIVER
64 const GMESH * _ptrMesh;
65 std::string _meshName;
66 mutable std::ofstream * _vtkFile ; // The _vtkFile used to write Meshes to _filename
67 mutable _VTK_BinaryWriter* _binaryFile;
78 VTK_MESH_DRIVER(const std::string & fileName, const GMESH * ptrMesh) ;
82 VTK_MESH_DRIVER(const VTK_MESH_DRIVER & driver) ;
92 void openConst() const throw (MEDEXCEPTION);
93 void closeConst() const throw (MEDEXCEPTION);
95 void write( void ) const throw (MEDEXCEPTION) ;
96 void read ( void ) throw (MEDEXCEPTION) ;
99 Set the name of the MESH asked in file.
101 It could be different than the name of the MESH object.
103 void setMeshName(const string & meshName) ;
105 Get the name of the MESH asked in file.
107 string getMeshName() const ;
110 GENDRIVER * copy ( void ) const;
112 template <typename T>
113 void writeBinary(const T* data, int nbValues) const throw (MEDEXCEPTION);
116 class MEDMEM_EXPORT _VTK_BinaryWriter
118 std::string _fileName;
121 _VTK_BinaryWriter(const std::string file);
122 bool open(bool append=false) const;
125 template <typename T>
126 void write(const T* data, int nbValues) const throw (MEDEXCEPTION)
129 // if ( sizeof(T) == sizeof(char))
132 // for ( int i=0; i < nbValues;++i )
133 // cout << data[i] << " ";
134 const void* toWrite = (const void* ) data;
136 if ( sizeof(T) != sizeof(char))
139 toWrite = (const void* )( swappedData = new T[ nbValues ]);
140 memcpy( swappedData, data, nbValues * sizeof(T));
141 int* intBuf = ((int*) swappedData) - 1;
142 int* bufEnd = (int*)((char*) swappedData + nbValues * sizeof(T));
143 while ( ++intBuf < bufEnd )
144 *intBuf = swapBytes( *intBuf );
147 ssize_t nbWritten = ::_write( _binaryFile, toWrite, nbValues * sizeof(T));
149 ssize_t nbWritten = ::write( _binaryFile, toWrite, nbValues * sizeof(T));
152 delete [] swappedData;
154 throw MEDEXCEPTION(LOCALIZED(STRING("_VTK_BinaryWriter::Failed to write into ")<< _fileName));
158 template <typename T>
159 void VTK_MESH_DRIVER::writeBinary(const T* data, int nbValues) const throw (MEDEXCEPTION)
161 _binaryFile->write( data, nbValues );
167 #endif /* VTK_MESH_DRIVER_HXX */