Added basic compilation instructions to README.md
[engrid-github.git] / src / libengrid / gmshwriter.cpp
blobc7449bfb40268e2b70ba11cf4c47b5a1e84e5b2b
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 "guimainwindow.h"
26 GmshWriter::GmshWriter()
28 setFormat("Gmsh files(*.msh)");
31 void GmshWriter::writeAscii1(vtkUnstructuredGrid *m_Grid)
33 QFile file(getFileName());
34 file.open(QIODevice::WriteOnly | QIODevice::Text);
35 QTextStream f(&file);
36 f << "$NOD\n";
37 f << m_Grid->GetNumberOfPoints() << '\n';
38 for (vtkIdType nodeId = 0; nodeId < m_Grid->GetNumberOfPoints(); ++nodeId) {
39 vec3_t x;
40 m_Grid->GetPoints()->GetPoint(nodeId, x.data());
41 f.setRealNumberPrecision(16);
42 f << nodeId+1 << ' ' << x[0] << ' ' << x[1] << ' ' << x[2] << '\n';
44 f << "$ENDNOD\n";
45 f << "$ELM\n";
46 f << m_Grid->GetNumberOfCells() << '\n';
47 EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
48 for (vtkIdType cellId = 0; cellId < m_Grid->GetNumberOfCells(); ++cellId) {
49 vtkIdType Npts;
50 vtkIdType *pts;
51 m_Grid->GetCellPoints(cellId, Npts, pts);
52 f << cellId+1;
53 if (m_Grid->GetCellType(cellId) == VTK_TRIANGLE) {
54 f << " 2 " << cell_code->GetValue(cellId) << " 0 " << Npts;
55 for (int i = 0; i < Npts; ++i) {
56 f << ' ' << pts[i]+1;
58 } else if (m_Grid->GetCellType(cellId) == VTK_QUAD) {
59 f << " 3 " << cell_code->GetValue(cellId) << " 0 " << Npts;
60 for (int i = 0; i < Npts; ++i) {
61 f << ' ' << pts[i]+1;
63 } else if (m_Grid->GetCellType(cellId) == VTK_TETRA) {
64 f << " 4 0 0 " << Npts;
66 for (int i = 0; i < Npts; ++i) {
67 f << ' ' << pts[i]+1;
70 //f << ' ' << pts[0]+1 << ' ' << pts[1]+1 << ' ' << pts[3]+1 << ' ' << pts[2]+1;
71 } else if (m_Grid->GetCellType(cellId) == VTK_PYRAMID) {
72 f << " 7 0 0 " << Npts;
73 for (int i = 0; i < Npts; ++i) {
74 f << ' ' << pts[i]+1;
76 } else if (m_Grid->GetCellType(cellId) == VTK_WEDGE) {
77 f << " 6 0 0 " << Npts;
78 for (int i = 3; i < Npts; ++i) {
79 f << ' ' << pts[i]+1;
81 for (int i = 0; i < Npts-3; ++i) {
82 f << ' ' << pts[i]+1;
84 } else if (m_Grid->GetCellType(cellId) == VTK_HEXAHEDRON) {
85 f << " 5 0 0 " << Npts;
86 for (int i = 0; i < Npts; ++i) {
87 f << ' ' << pts[i]+1;
90 f << '\n';
92 f << "$ENDELM\n";
95 void GmshWriter::writeAscii2(vtkUnstructuredGrid *m_Grid)
97 QFile file(getFileName());
98 file.open(QIODevice::WriteOnly | QIODevice::Text);
99 QTextStream f(&file);
100 f << "$MeshFormat\n2.0 0 8\n$EndMeshFormat\n";
101 f << "$Nodes\n";
102 f << m_Grid->GetNumberOfPoints() << '\n';
103 for (vtkIdType nodeId = 0; nodeId < m_Grid->GetNumberOfPoints(); ++nodeId) {
104 vec3_t x;
105 m_Grid->GetPoints()->GetPoint(nodeId, x.data());
106 f << nodeId+1 << ' ' << x[0] << ' ' << x[1] << ' ' << x[2] << '\n';
108 f << "$EndNodes\n";
109 f << "$Elements\n";
110 f << m_Grid->GetNumberOfCells() << '\n';
111 EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
112 for (vtkIdType cellId = 0; cellId < m_Grid->GetNumberOfCells(); ++cellId) {
113 vtkIdType Npts;
114 vtkIdType *pts;
115 m_Grid->GetCellPoints(cellId, Npts, pts);
116 f << cellId+1;
117 if (m_Grid->GetCellType(cellId) == VTK_TRIANGLE) {
118 f << " 2 1 " << cell_code->GetValue(cellId);
119 for (int i = 0; i < Npts; ++i) {
120 f << ' ' << pts[i]+1;
122 } else if (m_Grid->GetCellType(cellId) == VTK_QUAD) {
123 f << " 3 1 " << cell_code->GetValue(cellId);
124 for (int i = 0; i < Npts; ++i) {
125 f << ' ' << pts[i]+1;
127 } else if (m_Grid->GetCellType(cellId) == VTK_TETRA) {
128 f << " 4 1 0 ";
129 for (int i = 0; i < Npts; ++i) {
130 f << ' ' << pts[i]+1;
132 } else if (m_Grid->GetCellType(cellId) == VTK_PYRAMID) {
133 f << " 7 1 0 ";
134 for (int i = 0; i < Npts; ++i) {
135 f << ' ' << pts[i]+1;
137 } else if (m_Grid->GetCellType(cellId) == VTK_WEDGE) {
138 f << " 6 1 0 ";
139 for (int i = 3; i < Npts; ++i) {
140 f << ' ' << pts[i]+1;
142 for (int i = 0; i < Npts-3; ++i) {
143 f << ' ' << pts[i]+1;
145 } else if (m_Grid->GetCellType(cellId) == VTK_HEXAHEDRON) {
146 f << " 5 1 0 ";
147 for (int i = 0; i < Npts; ++i) {
148 f << ' ' << pts[i]+1;
151 f << '\n';
153 f << "$EndElements\n";
156 void GmshWriter::operate()
158 try {
159 QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
160 readOutputFileName(file_info.completeBaseName() + ".msh");
161 if (isValid()) {
162 if (format == ascii1) writeAscii1(m_Grid);
163 else if (format == ascii2) writeAscii2(m_Grid);
165 } catch (Error err) {
166 err.display();