DeleteSetOfPoints function working
[engrid.git] / gmshwriter.cpp
blobc03b06f6e0294fd90fbbbbfd995afdcd8a032494
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 GmshWriter::GmshWriter()
27 setFormat("Gmsh files(*.msh)");
30 void GmshWriter::writeAscii1(vtkUnstructuredGrid *grid)
32 QFile file(getFileName());
33 file.open(QIODevice::WriteOnly | QIODevice::Text);
34 QTextStream f(&file);
35 f << "$NOD\n";
36 f << grid->GetNumberOfPoints() << '\n';
37 for (vtkIdType nodeId = 0; nodeId < grid->GetNumberOfPoints(); ++nodeId) {
38 vec3_t x;
39 grid->GetPoints()->GetPoint(nodeId, x.data());
40 f.setRealNumberPrecision(16);
41 f << nodeId+1 << ' ' << x[0] << ' ' << x[1] << ' ' << x[2] << '\n';
43 f << "$ENDNOD\n";
44 f << "$ELM\n";
45 f << grid->GetNumberOfCells() << '\n';
46 EG_VTKDCC(vtkIntArray, cell_code, grid, "cell_code");
47 for (vtkIdType cellId = 0; cellId < grid->GetNumberOfCells(); ++cellId) {
48 vtkIdType Npts;
49 vtkIdType *pts;
50 grid->GetCellPoints(cellId, Npts, pts);
51 f << cellId+1;
52 if (grid->GetCellType(cellId) == VTK_TRIANGLE) {
53 f << " 2 " << cell_code->GetValue(cellId) << " 0 " << Npts;
54 for (int i = 0; i < Npts; ++i) {
55 f << ' ' << pts[i]+1;
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) {
60 f << ' ' << pts[i]+1;
62 } else if (grid->GetCellType(cellId) == VTK_TETRA) {
63 f << " 4 0 0 " << Npts;
65 for (int i = 0; i < Npts; ++i) {
66 f << ' ' << pts[i]+1;
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) {
73 f << ' ' << pts[i]+1;
75 } else if (grid->GetCellType(cellId) == VTK_WEDGE) {
76 f << " 6 0 0 " << Npts;
77 for (int i = 3; i < Npts; ++i) {
78 f << ' ' << pts[i]+1;
80 for (int i = 0; i < Npts-3; ++i) {
81 f << ' ' << pts[i]+1;
83 } else if (grid->GetCellType(cellId) == VTK_HEXAHEDRON) {
84 f << " 5 0 0 " << Npts;
85 for (int i = 0; i < Npts; ++i) {
86 f << ' ' << pts[i]+1;
89 f << '\n';
91 f << "$ENDELM\n";
94 void GmshWriter::writeAscii2(vtkUnstructuredGrid *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 << grid->GetNumberOfPoints() << '\n';
102 for (vtkIdType nodeId = 0; nodeId < grid->GetNumberOfPoints(); ++nodeId) {
103 vec3_t x;
104 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 << grid->GetNumberOfCells() << '\n';
110 EG_VTKDCC(vtkIntArray, cell_code, grid, "cell_code");
111 for (vtkIdType cellId = 0; cellId < grid->GetNumberOfCells(); ++cellId) {
112 vtkIdType Npts;
113 vtkIdType *pts;
114 grid->GetCellPoints(cellId, Npts, pts);
115 f << cellId+1;
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) {
127 f << " 4 1 0 ";
128 for (int i = 0; i < Npts; ++i) {
129 f << ' ' << pts[i]+1;
131 } else if (grid->GetCellType(cellId) == VTK_PYRAMID) {
132 f << " 7 1 0 ";
133 for (int i = 0; i < Npts; ++i) {
134 f << ' ' << pts[i]+1;
136 } else if (grid->GetCellType(cellId) == VTK_WEDGE) {
137 f << " 6 1 0 ";
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) {
145 f << " 5 1 0 ";
146 for (int i = 0; i < Npts; ++i) {
147 f << ' ' << pts[i]+1;
150 f << '\n';
152 f << "$EndElements\n";
155 void GmshWriter::operate()
157 try {
158 readOutputFileName();
159 if (isValid()) {
160 if (format == ascii1) writeAscii1(grid);
161 else if (format == ascii2) writeAscii2(grid);
163 } catch (Error err) {
164 err.display();