fixed bug in search method
[engrid.git] / src / gmshwriter.cpp
blob895ea4f74fa4cd31ae9673548458cfbb0b3f8ddf
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008,2009 Oliver Gloth +
7 // + +
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. +
12 // + +
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. +
17 // + +
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/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #include "gmshwriter.h"
25 #include <QFileInfo>
26 #include "guimainwindow.h"
28 GmshWriter::GmshWriter()
30 setFormat("Gmsh files(*.msh)");
33 void GmshWriter::writeAscii1(vtkUnstructuredGrid *m_Grid)
35 QFile file(getFileName());
36 file.open(QIODevice::WriteOnly | QIODevice::Text);
37 QTextStream f(&file);
38 f << "$NOD\n";
39 f << m_Grid->GetNumberOfPoints() << '\n';
40 for (vtkIdType nodeId = 0; nodeId < m_Grid->GetNumberOfPoints(); ++nodeId) {
41 vec3_t x;
42 m_Grid->GetPoints()->GetPoint(nodeId, x.data());
43 f.setRealNumberPrecision(16);
44 f << nodeId+1 << ' ' << x[0] << ' ' << x[1] << ' ' << x[2] << '\n';
46 f << "$ENDNOD\n";
47 f << "$ELM\n";
48 f << m_Grid->GetNumberOfCells() << '\n';
49 EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
50 for (vtkIdType cellId = 0; cellId < m_Grid->GetNumberOfCells(); ++cellId) {
51 vtkIdType Npts;
52 vtkIdType *pts;
53 m_Grid->GetCellPoints(cellId, Npts, pts);
54 f << cellId+1;
55 if (m_Grid->GetCellType(cellId) == VTK_TRIANGLE) {
56 f << " 2 " << cell_code->GetValue(cellId) << " 0 " << Npts;
57 for (int i = 0; i < Npts; ++i) {
58 f << ' ' << pts[i]+1;
60 } else if (m_Grid->GetCellType(cellId) == VTK_QUAD) {
61 f << " 3 " << cell_code->GetValue(cellId) << " 0 " << Npts;
62 for (int i = 0; i < Npts; ++i) {
63 f << ' ' << pts[i]+1;
65 } else if (m_Grid->GetCellType(cellId) == VTK_TETRA) {
66 f << " 4 0 0 " << Npts;
68 for (int i = 0; i < Npts; ++i) {
69 f << ' ' << pts[i]+1;
72 //f << ' ' << pts[0]+1 << ' ' << pts[1]+1 << ' ' << pts[3]+1 << ' ' << pts[2]+1;
73 } else if (m_Grid->GetCellType(cellId) == VTK_PYRAMID) {
74 f << " 7 0 0 " << Npts;
75 for (int i = 0; i < Npts; ++i) {
76 f << ' ' << pts[i]+1;
78 } else if (m_Grid->GetCellType(cellId) == VTK_WEDGE) {
79 f << " 6 0 0 " << Npts;
80 for (int i = 3; i < Npts; ++i) {
81 f << ' ' << pts[i]+1;
83 for (int i = 0; i < Npts-3; ++i) {
84 f << ' ' << pts[i]+1;
86 } else if (m_Grid->GetCellType(cellId) == VTK_HEXAHEDRON) {
87 f << " 5 0 0 " << Npts;
88 for (int i = 0; i < Npts; ++i) {
89 f << ' ' << pts[i]+1;
92 f << '\n';
94 f << "$ENDELM\n";
97 void GmshWriter::writeAscii2(vtkUnstructuredGrid *m_Grid)
99 QFile file(getFileName());
100 file.open(QIODevice::WriteOnly | QIODevice::Text);
101 QTextStream f(&file);
102 f << "$MeshFormat\n2.0 0 8\n$EndMeshFormat\n";
103 f << "$Nodes\n";
104 f << m_Grid->GetNumberOfPoints() << '\n';
105 for (vtkIdType nodeId = 0; nodeId < m_Grid->GetNumberOfPoints(); ++nodeId) {
106 vec3_t x;
107 m_Grid->GetPoints()->GetPoint(nodeId, x.data());
108 f << nodeId+1 << ' ' << x[0] << ' ' << x[1] << ' ' << x[2] << '\n';
110 f << "$EndNodes\n";
111 f << "$Elements\n";
112 f << m_Grid->GetNumberOfCells() << '\n';
113 EG_VTKDCC(vtkIntArray, cell_code, m_Grid, "cell_code");
114 for (vtkIdType cellId = 0; cellId < m_Grid->GetNumberOfCells(); ++cellId) {
115 vtkIdType Npts;
116 vtkIdType *pts;
117 m_Grid->GetCellPoints(cellId, Npts, pts);
118 f << cellId+1;
119 if (m_Grid->GetCellType(cellId) == VTK_TRIANGLE) {
120 f << " 2 1 " << cell_code->GetValue(cellId);
121 for (int i = 0; i < Npts; ++i) {
122 f << ' ' << pts[i]+1;
124 } else if (m_Grid->GetCellType(cellId) == VTK_QUAD) {
125 f << " 3 1 " << cell_code->GetValue(cellId);
126 for (int i = 0; i < Npts; ++i) {
127 f << ' ' << pts[i]+1;
129 } else if (m_Grid->GetCellType(cellId) == VTK_TETRA) {
130 f << " 4 1 0 ";
131 for (int i = 0; i < Npts; ++i) {
132 f << ' ' << pts[i]+1;
134 } else if (m_Grid->GetCellType(cellId) == VTK_PYRAMID) {
135 f << " 7 1 0 ";
136 for (int i = 0; i < Npts; ++i) {
137 f << ' ' << pts[i]+1;
139 } else if (m_Grid->GetCellType(cellId) == VTK_WEDGE) {
140 f << " 6 1 0 ";
141 for (int i = 3; i < Npts; ++i) {
142 f << ' ' << pts[i]+1;
144 for (int i = 0; i < Npts-3; ++i) {
145 f << ' ' << pts[i]+1;
147 } else if (m_Grid->GetCellType(cellId) == VTK_HEXAHEDRON) {
148 f << " 5 1 0 ";
149 for (int i = 0; i < Npts; ++i) {
150 f << ' ' << pts[i]+1;
153 f << '\n';
155 f << "$EndElements\n";
158 void GmshWriter::operate()
160 try {
161 QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
162 readOutputFileName(file_info.completeBaseName() + ".msh");
163 if (isValid()) {
164 if (format == ascii1) writeAscii1(m_Grid);
165 else if (format == ascii2) writeAscii2(m_Grid);
167 } catch (Error err) {
168 err.display();