updated to modern VTK
[engrid-github.git] / src / libengrid / gmshwriter.cpp
blob8ebd399764bf4965638c53f3af2a4ac11ebfe059
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
7 // + enGrid is free software: you can redistribute it and/or modify +
8 // + it under the terms of the GNU General Public License as published by +
9 // + the Free Software Foundation, either version 3 of the License, or +
10 // + (at your option) any later version. +
11 // + +
12 // + enGrid is distributed in the hope that it will be useful, +
13 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
14 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
15 // + GNU General Public License for more details. +
16 // + +
17 // + You should have received a copy of the GNU General Public License +
18 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "gmshwriter.h"
23 #include <QFileInfo>
24 #include "engrid.h"
25 #include "guimainwindow.h"
27 GmshWriter::GmshWriter()
29 setFormat("Gmsh files(*.msh)");
32 void GmshWriter::writeAscii1(vtkUnstructuredGrid *m_Grid)
34 QFile file(getFileName());
35 file.open(QIODevice::WriteOnly | QIODevice::Text);
36 QTextStream f(&file);
37 f << "$NOD\n";
38 f << m_Grid->GetNumberOfPoints() << '\n';
39 for (vtkIdType nodeId = 0; nodeId < m_Grid->GetNumberOfPoints(); ++nodeId) {
40 vec3_t x;
41 m_Grid->GetPoints()->GetPoint(nodeId, x.data());
42 f.setRealNumberPrecision(16);
43 f << nodeId+1 << ' ' << x[0] << ' ' << x[1] << ' ' << x[2] << '\n';
45 f << "$ENDNOD\n";
46 f << "$ELM\n";
47 f << m_Grid->GetNumberOfCells() << '\n';
48 EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
49 for (vtkIdType cellId = 0; cellId < m_Grid->GetNumberOfCells(); ++cellId) {
50 EG_GET_CELL(cellId, m_Grid);
51 f << cellId+1;
52 if (m_Grid->GetCellType(cellId) == VTK_TRIANGLE) {
53 f << " 2 " << cell_code->GetValue(cellId) << " 0 " << num_pts;
54 for (int i = 0; i < num_pts; ++i) {
55 f << ' ' << pts[i]+1;
57 } else if (m_Grid->GetCellType(cellId) == VTK_QUAD) {
58 f << " 3 " << cell_code->GetValue(cellId) << " 0 " << num_pts;
59 for (int i = 0; i < num_pts; ++i) {
60 f << ' ' << pts[i]+1;
62 } else if (m_Grid->GetCellType(cellId) == VTK_TETRA) {
63 f << " 4 0 0 " << num_pts;
65 for (int i = 0; i < num_pts; ++i) {
66 f << ' ' << pts[i]+1;
69 //f << ' ' << pts[0]+1 << ' ' << pts[1]+1 << ' ' << pts[3]+1 << ' ' << pts[2]+1;
70 } else if (m_Grid->GetCellType(cellId) == VTK_PYRAMID) {
71 f << " 7 0 0 " << num_pts;
72 for (int i = 0; i < num_pts; ++i) {
73 f << ' ' << pts[i]+1;
75 } else if (m_Grid->GetCellType(cellId) == VTK_WEDGE) {
76 f << " 6 0 0 " << num_pts;
77 for (int i = 3; i < num_pts; ++i) {
78 f << ' ' << pts[i]+1;
80 for (int i = 0; i < num_pts-3; ++i) {
81 f << ' ' << pts[i]+1;
83 } else if (m_Grid->GetCellType(cellId) == VTK_HEXAHEDRON) {
84 f << " 5 0 0 " << num_pts;
85 for (int i = 0; i < num_pts; ++i) {
86 f << ' ' << pts[i]+1;
89 f << '\n';
91 f << "$ENDELM\n";
94 void GmshWriter::writeAscii2(vtkUnstructuredGrid *m_Grid)
96 QFile file(getFileName());
97 file.open(QIODevice::WriteOnly | QIODevice::Text);
98 QTextStream f(&file);
99 f << "$MeshFormat\n2.0 0 8\n$EndMeshFormat\n";
100 f << "$Nodes\n";
101 f << m_Grid->GetNumberOfPoints() << '\n';
102 for (vtkIdType nodeId = 0; nodeId < m_Grid->GetNumberOfPoints(); ++nodeId) {
103 vec3_t x;
104 m_Grid->GetPoints()->GetPoint(nodeId, x.data());
105 f << nodeId+1 << ' ' << x[0] << ' ' << x[1] << ' ' << x[2] << '\n';
107 f << "$EndNodes\n";
108 f << "$Elements\n";
109 f << m_Grid->GetNumberOfCells() << '\n';
110 EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
111 for (vtkIdType cellId = 0; cellId < m_Grid->GetNumberOfCells(); ++cellId) {
112 EG_GET_CELL(cellId, m_Grid);
113 f << cellId+1;
114 if (m_Grid->GetCellType(cellId) == VTK_TRIANGLE) {
115 f << " 2 1 " << cell_code->GetValue(cellId);
116 for (int i = 0; i < num_pts; ++i) {
117 f << ' ' << pts[i]+1;
119 } else if (m_Grid->GetCellType(cellId) == VTK_QUAD) {
120 f << " 3 1 " << cell_code->GetValue(cellId);
121 for (int i = 0; i < num_pts; ++i) {
122 f << ' ' << pts[i]+1;
124 } else if (m_Grid->GetCellType(cellId) == VTK_TETRA) {
125 f << " 4 1 0 ";
126 for (int i = 0; i < num_pts; ++i) {
127 f << ' ' << pts[i]+1;
129 } else if (m_Grid->GetCellType(cellId) == VTK_PYRAMID) {
130 f << " 7 1 0 ";
131 for (int i = 0; i < num_pts; ++i) {
132 f << ' ' << pts[i]+1;
134 } else if (m_Grid->GetCellType(cellId) == VTK_WEDGE) {
135 f << " 6 1 0 ";
136 for (int i = 3; i < num_pts; ++i) {
137 f << ' ' << pts[i]+1;
139 for (int i = 0; i < num_pts-3; ++i) {
140 f << ' ' << pts[i]+1;
142 } else if (m_Grid->GetCellType(cellId) == VTK_HEXAHEDRON) {
143 f << " 5 1 0 ";
144 for (int i = 0; i < num_pts; ++i) {
145 f << ' ' << pts[i]+1;
148 f << '\n';
150 f << "$EndElements\n";
153 void GmshWriter::operate()
155 try {
156 QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
157 readOutputFileName(file_info.completeBaseName() + ".msh");
158 if (isValid()) {
159 if (format == ascii1) writeAscii1(m_Grid);
160 else if (format == ascii2) writeAscii2(m_Grid);
162 } catch (Error err) {
163 err.display();