2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008,2009 Oliver Gloth +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include "gmshwriter.h"
25 GmshWriter::GmshWriter()
27 setFormat("Gmsh files(*.msh)");
30 void GmshWriter::writeAscii1(vtkUnstructuredGrid
*grid
)
32 QFile
file(getFileName());
33 file
.open(QIODevice::WriteOnly
| QIODevice::Text
);
36 f
<< grid
->GetNumberOfPoints() << '\n';
37 for (vtkIdType nodeId
= 0; nodeId
< grid
->GetNumberOfPoints(); ++nodeId
) {
39 grid
->GetPoints()->GetPoint(nodeId
, x
.data());
40 f
.setRealNumberPrecision(16);
41 f
<< nodeId
+1 << ' ' << x
[0] << ' ' << x
[1] << ' ' << x
[2] << '\n';
45 f
<< grid
->GetNumberOfCells() << '\n';
46 EG_VTKDCC(vtkIntArray
, cell_code
, grid
, "cell_code");
47 for (vtkIdType cellId
= 0; cellId
< grid
->GetNumberOfCells(); ++cellId
) {
50 grid
->GetCellPoints(cellId
, Npts
, pts
);
52 if (grid
->GetCellType(cellId
) == VTK_TRIANGLE
) {
53 f
<< " 2 " << cell_code
->GetValue(cellId
) << " 0 " << Npts
;
54 for (int i
= 0; i
< Npts
; ++i
) {
57 } else if (grid
->GetCellType(cellId
) == VTK_QUAD
) {
58 f
<< " 3 " << cell_code
->GetValue(cellId
) << " 0 " << Npts
;
59 for (int i
= 0; i
< Npts
; ++i
) {
62 } else if (grid
->GetCellType(cellId
) == VTK_TETRA
) {
63 f
<< " 4 0 0 " << Npts
;
65 for (int i
= 0; i
< Npts
; ++i
) {
69 //f << ' ' << pts[0]+1 << ' ' << pts[1]+1 << ' ' << pts[3]+1 << ' ' << pts[2]+1;
70 } else if (grid
->GetCellType(cellId
) == VTK_PYRAMID
) {
71 f
<< " 7 0 0 " << Npts
;
72 for (int i
= 0; i
< Npts
; ++i
) {
75 } else if (grid
->GetCellType(cellId
) == VTK_WEDGE
) {
76 f
<< " 6 0 0 " << Npts
;
77 for (int i
= 3; i
< Npts
; ++i
) {
80 for (int i
= 0; i
< Npts
-3; ++i
) {
83 } else if (grid
->GetCellType(cellId
) == VTK_HEXAHEDRON
) {
84 f
<< " 5 0 0 " << Npts
;
85 for (int i
= 0; i
< Npts
; ++i
) {
94 void GmshWriter::writeAscii2(vtkUnstructuredGrid
*grid
)
96 QFile
file(getFileName());
97 file
.open(QIODevice::WriteOnly
| QIODevice::Text
);
99 f
<< "$MeshFormat\n2.0 0 8\n$EndMeshFormat\n";
101 f
<< grid
->GetNumberOfPoints() << '\n';
102 for (vtkIdType nodeId
= 0; nodeId
< grid
->GetNumberOfPoints(); ++nodeId
) {
104 grid
->GetPoints()->GetPoint(nodeId
, x
.data());
105 f
<< nodeId
+1 << ' ' << x
[0] << ' ' << x
[1] << ' ' << x
[2] << '\n';
109 f
<< grid
->GetNumberOfCells() << '\n';
110 EG_VTKDCC(vtkIntArray
, cell_code
, grid
, "cell_code");
111 for (vtkIdType cellId
= 0; cellId
< grid
->GetNumberOfCells(); ++cellId
) {
114 grid
->GetCellPoints(cellId
, Npts
, pts
);
116 if (grid
->GetCellType(cellId
) == VTK_TRIANGLE
) {
117 f
<< " 2 1 " << cell_code
->GetValue(cellId
);
118 for (int i
= 0; i
< Npts
; ++i
) {
119 f
<< ' ' << pts
[i
]+1;
121 } else if (grid
->GetCellType(cellId
) == VTK_QUAD
) {
122 f
<< " 3 1 " << cell_code
->GetValue(cellId
);
123 for (int i
= 0; i
< Npts
; ++i
) {
124 f
<< ' ' << pts
[i
]+1;
126 } else if (grid
->GetCellType(cellId
) == VTK_TETRA
) {
128 for (int i
= 0; i
< Npts
; ++i
) {
129 f
<< ' ' << pts
[i
]+1;
131 } else if (grid
->GetCellType(cellId
) == VTK_PYRAMID
) {
133 for (int i
= 0; i
< Npts
; ++i
) {
134 f
<< ' ' << pts
[i
]+1;
136 } else if (grid
->GetCellType(cellId
) == VTK_WEDGE
) {
138 for (int i
= 3; i
< Npts
; ++i
) {
139 f
<< ' ' << pts
[i
]+1;
141 for (int i
= 0; i
< Npts
-3; ++i
) {
142 f
<< ' ' << pts
[i
]+1;
144 } else if (grid
->GetCellType(cellId
) == VTK_HEXAHEDRON
) {
146 for (int i
= 0; i
< Npts
; ++i
) {
147 f
<< ' ' << pts
[i
]+1;
152 f
<< "$EndElements\n";
155 void GmshWriter::operate()
158 readOutputFileName();
160 if (format
== ascii1
) writeAscii1(grid
);
161 else if (format
== ascii2
) writeAscii2(grid
);
163 } catch (Error err
) {