1 // Copyright (C) 2007-2008 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
22 #include "MEDMEM_VtkMeshDriver.hxx"
24 #include "MEDMEM_DriversDef.hxx"
26 #include "MEDMEM_Family.hxx"
27 #include "MEDMEM_Group.hxx"
28 #include "MEDMEM_Coordinate.hxx"
29 #include "MEDMEM_Connectivity.hxx"
30 #include "MEDMEM_Mesh.hxx"
31 #include "MEDMEM_CellModel.hxx"
32 #include "MEDMEM_Grid.hxx"
37 using namespace MEDMEM;
38 using namespace MED_EN;
42 VTK_MESH_DRIVER::VTK_MESH_DRIVER(): GENDRIVER(VTK_DRIVER),
43 _ptrMesh((MESH * const)MED_NULL)
45 _vtkFile = new ofstream();
46 // What about _id in Gendriver ?
50 VTK_MESH_DRIVER::VTK_MESH_DRIVER(const string & fileName,
52 GENDRIVER(fileName, WRONLY, VTK_DRIVER),
56 // Send an exception because a VTK_MESH_DRIVER object cannot be instantied
57 // from a file and there is no read for that kind of driver
59 // throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "This driver is only used to write in VTK format So thie object can not be instantied using a file!"));
61 // _ptrMesh->addDriver(*this); // OU RECUPERER L'ID.
62 MESSAGE_MED("VTK_MESH_DRIVER::VTK_MESH_DRIVER(const string & fileName, MESH * ptrMesh) : "
63 << "WARNING this driver is only used to write in VTK format So the object can not be instantied using a file!");
65 _vtkFile = new ofstream();
68 VTK_MESH_DRIVER::VTK_MESH_DRIVER(const VTK_MESH_DRIVER & driver):
70 _ptrMesh(driver._ptrMesh),
71 _meshName(driver._meshName)
73 // next string commented by skl for bug NPAL14840
74 //_ptrMesh->addDriver(*this);
75 _vtkFile = new ofstream();
78 VTK_MESH_DRIVER::~VTK_MESH_DRIVER()
80 const char* LOC = "VTK_MESH_DRIVER::~VTK_MESH_DRIVER()";
94 void VTK_MESH_DRIVER::openConst() const throw (MEDEXCEPTION)
96 const char * LOC = "VTK_MESH_DRIVER::openConst()" ;
99 MESSAGE_MED(LOC<<" : _fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
101 if ( _fileName == "" )
102 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
103 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
107 if (!(*_vtkFile).is_open())
108 (*_vtkFile).open(_fileName.c_str()) ;
110 // _status = MED_OPENED ;
114 SCRUTE_MED((*_vtkFile).is_open());
115 SCRUTE_MED(_vtkFile);
120 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "
126 void VTK_MESH_DRIVER::open() {
130 void VTK_MESH_DRIVER::closeConst() const throw (MEDEXCEPTION)
132 const char * LOC = "VTK_MESH_DRIVER::closeConst() " ;
135 SCRUTE_MED(_vtkFile);
136 SCRUTE_MED(*_vtkFile);
139 if ((*_vtkFile).is_open())
143 // _status = MED_CLOSED ;
146 SCRUTE_MED(_vtkFile);
147 SCRUTE_MED(*_vtkFile);
148 SCRUTE_MED(_vtkFile->is_open());
150 if ( (*_vtkFile) && _vtkFile->is_open() )
151 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not close file "
158 void VTK_MESH_DRIVER::close() {
162 void VTK_MESH_DRIVER::setMeshName(const string & meshName) { _meshName = meshName; };
163 string VTK_MESH_DRIVER::getMeshName() const { return _meshName; };
165 void VTK_MESH_DRIVER::read(void) throw (MEDEXCEPTION)
167 const char * LOC = "VTK_MESH_DRIVER::read() : " ;
172 throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "This driver is only used to write in VTK format !"));
178 void VTK_MESH_DRIVER::write(void) const
181 const char * LOC = "void VTK_MESH_DRIVER::write(void) const : ";
184 // Well we must open vtk file first, because there are
185 // no other driver than MED for VTK that do it !
189 int SpaceDimension = _ptrMesh->getSpaceDimension() ;
190 int NumberOfNodes = _ptrMesh->getNumberOfNodes() ;
191 if ( SpaceDimension < 1 )
192 throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "Mesh is empty"));
194 (*_vtkFile) << "# vtk DataFile Version 2.0" << endl
195 << "maillage from MedMemory" << endl ;
196 // only ASCII for the moment (binary came later :-)
197 (*_vtkFile) << "ASCII" << endl ;
200 (*_vtkFile) << "DATASET UNSTRUCTURED_GRID" << endl ;
201 // put points (all point are in 3D, so if we are in 1D or 2D, we complete by zero !
202 (*_vtkFile) << "POINTS " << NumberOfNodes << " float" << endl ;
203 const double *coordinate = _ptrMesh->getCoordinates(MED_FULL_INTERLACE) ;
204 for (int i=0;i<NumberOfNodes;i++) {
205 for (int j=0;j<SpaceDimension;j++)
206 (*_vtkFile) << coordinate[i*SpaceDimension+j] << " " ;
207 if (SpaceDimension==1)
208 (*_vtkFile) << "0 0" ;
209 if (SpaceDimension==2)
211 (*_vtkFile) << endl ;
214 // we put connectivity
215 // how many cells and how many value in connectivity :
216 int cells_types_count = _ptrMesh->getNumberOfTypes(MED_CELL) ;
217 // int * cells_count = _ptrMesh->get_cells_count() ;
218 // int cells_sum = cells_count[cells_types_count] ;
219 int cells_sum = _ptrMesh->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) ;
220 const CELLMODEL * cells_type = _ptrMesh->getCellsTypes(MED_CELL) ;
221 // int connectivity_sum = 0 ;
223 //const int * connectivity = _ptrMesh->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,MED_ALL_ELEMENTS) ; !! UNUSED VARIABLE !!
224 const int * connectivityIndex = _ptrMesh->getConnectivityIndex(MED_NODAL,MED_CELL) ;
226 int connectivity_sum = connectivityIndex[cells_sum]-1 ;
228 (*_vtkFile) << "CELLS " << cells_sum << " " << connectivity_sum+cells_sum << endl ;
229 // we put connectivity
230 for (int i=0;i<cells_types_count;i++) {
231 int *filter = (int*) NULL ; // index in vtk connectivity
232 switch (cells_type[i].getType())
235 filter = new int[1] ;
240 filter = new int[2] ;
249 filter = new int[3] ;
256 filter = new int[4] ;
270 filter = new int[4] ;
273 filter[2] = 3 ; // 3td element in med are 4th in vtk (array begin at 0 !)
274 filter[3] = 2 ; // 4th element in med are 3rd in vtk (array begin at 0 !)
278 filter = new int[5] ;
280 filter[1] = 3 ; // 2nd element in med are 4th in vtk (array begin at 0 !)
282 filter[3] = 1 ; // 4th element in med are 2nd in vtk (array begin at 0 !)
287 filter = new int[6] ;
297 filter = new int[8] ;
325 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getName() ) ) ;
326 int nodes_cell = cells_type[i].getNumberOfNodes();
327 int numberOfCell = _ptrMesh->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
328 const int * connectivityArray = _ptrMesh->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,cells_type[i].getType());
329 for (int j=0;j<numberOfCell;j++) {
330 (*_vtkFile) << nodes_cell << " " ;
331 for (int k=0;k<nodes_cell;k++)
332 (*_vtkFile) << connectivityArray[j*nodes_cell+filter[k]] - 1 << " " ;
333 (*_vtkFile) << endl ;
338 (*_vtkFile) << endl ;
340 (*_vtkFile) << "CELL_TYPES " << cells_sum << endl ;
341 for (int i=0;i<cells_types_count;i++) {
343 switch (cells_type[i].getType())
411 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getType() ) ) ;
412 int numberOfCell = _ptrMesh->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
413 for (int j=0;j<numberOfCell;j++)
414 (*_vtkFile) << vtkType << endl ;
420 GENDRIVER * VTK_MESH_DRIVER::copy(void) const
422 return new VTK_MESH_DRIVER(*this);