2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008-2013 enGits GmbH +
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"
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
);
39 f
<< m_Grid
->GetNumberOfPoints() << '\n';
40 for (vtkIdType nodeId
= 0; nodeId
< m_Grid
->GetNumberOfPoints(); ++nodeId
) {
42 m_Grid
->GetPoints()->GetPoint(nodeId
, x
.data());
43 f
.setRealNumberPrecision(16);
44 f
<< nodeId
+1 << ' ' << x
[0] << ' ' << x
[1] << ' ' << x
[2] << '\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
) {
53 m_Grid
->GetCellPoints(cellId
, Npts
, pts
);
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
) {
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
) {
65 } else if (m_Grid
->GetCellType(cellId
) == VTK_TETRA
) {
66 f
<< " 4 0 0 " << Npts
;
68 for (int i
= 0; i
< Npts
; ++i
) {
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
) {
78 } else if (m_Grid
->GetCellType(cellId
) == VTK_WEDGE
) {
79 f
<< " 6 0 0 " << Npts
;
80 for (int i
= 3; i
< Npts
; ++i
) {
83 for (int i
= 0; i
< Npts
-3; ++i
) {
86 } else if (m_Grid
->GetCellType(cellId
) == VTK_HEXAHEDRON
) {
87 f
<< " 5 0 0 " << Npts
;
88 for (int i
= 0; i
< Npts
; ++i
) {
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";
104 f
<< m_Grid
->GetNumberOfPoints() << '\n';
105 for (vtkIdType nodeId
= 0; nodeId
< m_Grid
->GetNumberOfPoints(); ++nodeId
) {
107 m_Grid
->GetPoints()->GetPoint(nodeId
, x
.data());
108 f
<< nodeId
+1 << ' ' << x
[0] << ' ' << x
[1] << ' ' << x
[2] << '\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
) {
117 m_Grid
->GetCellPoints(cellId
, Npts
, pts
);
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
) {
131 for (int i
= 0; i
< Npts
; ++i
) {
132 f
<< ' ' << pts
[i
]+1;
134 } else if (m_Grid
->GetCellType(cellId
) == VTK_PYRAMID
) {
136 for (int i
= 0; i
< Npts
; ++i
) {
137 f
<< ' ' << pts
[i
]+1;
139 } else if (m_Grid
->GetCellType(cellId
) == VTK_WEDGE
) {
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
) {
149 for (int i
= 0; i
< Npts
; ++i
) {
150 f
<< ' ' << pts
[i
]+1;
155 f
<< "$EndElements\n";
158 void GmshWriter::operate()
161 QFileInfo
file_info(GuiMainWindow::pointer()->getFilename());
162 readOutputFileName(file_info
.completeBaseName() + ".msh");
164 if (format
== ascii1
) writeAscii1(m_Grid
);
165 else if (format
== ascii2
) writeAscii2(m_Grid
);
167 } catch (Error err
) {