1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + This file is part of enGrid. +
5 // + Copyright 2008-2014 enGits GmbH +
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. +
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. +
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/>. +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "gmshwriter.h"
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
);
37 f
<< m_Grid
->GetNumberOfPoints() << '\n';
38 for (vtkIdType nodeId
= 0; nodeId
< m_Grid
->GetNumberOfPoints(); ++nodeId
) {
40 m_Grid
->GetPoints()->GetPoint(nodeId
, x
.data());
41 f
.setRealNumberPrecision(16);
42 f
<< nodeId
+1 << ' ' << x
[0] << ' ' << x
[1] << ' ' << x
[2] << '\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
) {
51 m_Grid
->GetCellPoints(cellId
, Npts
, pts
);
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
) {
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
) {
63 } else if (m_Grid
->GetCellType(cellId
) == VTK_TETRA
) {
64 f
<< " 4 0 0 " << Npts
;
66 for (int i
= 0; i
< Npts
; ++i
) {
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
) {
76 } else if (m_Grid
->GetCellType(cellId
) == VTK_WEDGE
) {
77 f
<< " 6 0 0 " << Npts
;
78 for (int i
= 3; i
< Npts
; ++i
) {
81 for (int i
= 0; i
< Npts
-3; ++i
) {
84 } else if (m_Grid
->GetCellType(cellId
) == VTK_HEXAHEDRON
) {
85 f
<< " 5 0 0 " << Npts
;
86 for (int i
= 0; i
< Npts
; ++i
) {
95 void GmshWriter::writeAscii2(vtkUnstructuredGrid
*m_Grid
)
97 QFile
file(getFileName());
98 file
.open(QIODevice::WriteOnly
| QIODevice::Text
);
100 f
<< "$MeshFormat\n2.0 0 8\n$EndMeshFormat\n";
102 f
<< m_Grid
->GetNumberOfPoints() << '\n';
103 for (vtkIdType nodeId
= 0; nodeId
< m_Grid
->GetNumberOfPoints(); ++nodeId
) {
105 m_Grid
->GetPoints()->GetPoint(nodeId
, x
.data());
106 f
<< nodeId
+1 << ' ' << x
[0] << ' ' << x
[1] << ' ' << x
[2] << '\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
) {
115 m_Grid
->GetCellPoints(cellId
, Npts
, pts
);
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
) {
129 for (int i
= 0; i
< Npts
; ++i
) {
130 f
<< ' ' << pts
[i
]+1;
132 } else if (m_Grid
->GetCellType(cellId
) == VTK_PYRAMID
) {
134 for (int i
= 0; i
< Npts
; ++i
) {
135 f
<< ' ' << pts
[i
]+1;
137 } else if (m_Grid
->GetCellType(cellId
) == VTK_WEDGE
) {
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
) {
147 for (int i
= 0; i
< Npts
; ++i
) {
148 f
<< ' ' << pts
[i
]+1;
153 f
<< "$EndElements\n";
156 void GmshWriter::operate()
159 QFileInfo
file_info(GuiMainWindow::pointer()->getFilename());
160 readOutputFileName(file_info
.completeBaseName() + ".msh");
162 if (format
== ascii1
) writeAscii1(m_Grid
);
163 else if (format
== ascii2
) writeAscii2(m_Grid
);
165 } catch (Error err
) {