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"
25 #include "guimainwindow.h"
27 GmshWriter::GmshWriter()
29 setFormat("Gmsh files(*.msh)");
32 void GmshWriter::writeAscii1(vtkUnstructuredGrid
*m_Grid
)
34 QFile
file(getFileName());
35 file
.open(QIODevice::WriteOnly
| QIODevice::Text
);
38 f
<< m_Grid
->GetNumberOfPoints() << '\n';
39 for (vtkIdType nodeId
= 0; nodeId
< m_Grid
->GetNumberOfPoints(); ++nodeId
) {
41 m_Grid
->GetPoints()->GetPoint(nodeId
, x
.data());
42 f
.setRealNumberPrecision(16);
43 f
<< nodeId
+1 << ' ' << x
[0] << ' ' << x
[1] << ' ' << x
[2] << '\n';
47 f
<< m_Grid
->GetNumberOfCells() << '\n';
48 EG_VTKDCC(vtkIntArray
, cell_code
, m_Grid
, "cell_code");
49 for (vtkIdType cellId
= 0; cellId
< m_Grid
->GetNumberOfCells(); ++cellId
) {
50 EG_GET_CELL(cellId
, m_Grid
);
52 if (m_Grid
->GetCellType(cellId
) == VTK_TRIANGLE
) {
53 f
<< " 2 " << cell_code
->GetValue(cellId
) << " 0 " << num_pts
;
54 for (int i
= 0; i
< num_pts
; ++i
) {
57 } else if (m_Grid
->GetCellType(cellId
) == VTK_QUAD
) {
58 f
<< " 3 " << cell_code
->GetValue(cellId
) << " 0 " << num_pts
;
59 for (int i
= 0; i
< num_pts
; ++i
) {
62 } else if (m_Grid
->GetCellType(cellId
) == VTK_TETRA
) {
63 f
<< " 4 0 0 " << num_pts
;
65 for (int i
= 0; i
< num_pts
; ++i
) {
69 //f << ' ' << pts[0]+1 << ' ' << pts[1]+1 << ' ' << pts[3]+1 << ' ' << pts[2]+1;
70 } else if (m_Grid
->GetCellType(cellId
) == VTK_PYRAMID
) {
71 f
<< " 7 0 0 " << num_pts
;
72 for (int i
= 0; i
< num_pts
; ++i
) {
75 } else if (m_Grid
->GetCellType(cellId
) == VTK_WEDGE
) {
76 f
<< " 6 0 0 " << num_pts
;
77 for (int i
= 3; i
< num_pts
; ++i
) {
80 for (int i
= 0; i
< num_pts
-3; ++i
) {
83 } else if (m_Grid
->GetCellType(cellId
) == VTK_HEXAHEDRON
) {
84 f
<< " 5 0 0 " << num_pts
;
85 for (int i
= 0; i
< num_pts
; ++i
) {
94 void GmshWriter::writeAscii2(vtkUnstructuredGrid
*m_Grid
)
96 QFile
file(getFileName());
97 file
.open(QIODevice::WriteOnly
| QIODevice::Text
);
99 f
<< "$MeshFormat\n2.0 0 8\n$EndMeshFormat\n";
101 f
<< m_Grid
->GetNumberOfPoints() << '\n';
102 for (vtkIdType nodeId
= 0; nodeId
< m_Grid
->GetNumberOfPoints(); ++nodeId
) {
104 m_Grid
->GetPoints()->GetPoint(nodeId
, x
.data());
105 f
<< nodeId
+1 << ' ' << x
[0] << ' ' << x
[1] << ' ' << x
[2] << '\n';
109 f
<< m_Grid
->GetNumberOfCells() << '\n';
110 EG_VTKDCC(vtkIntArray
, cell_code
, m_Grid
, "cell_code");
111 for (vtkIdType cellId
= 0; cellId
< m_Grid
->GetNumberOfCells(); ++cellId
) {
112 EG_GET_CELL(cellId
, m_Grid
);
114 if (m_Grid
->GetCellType(cellId
) == VTK_TRIANGLE
) {
115 f
<< " 2 1 " << cell_code
->GetValue(cellId
);
116 for (int i
= 0; i
< num_pts
; ++i
) {
117 f
<< ' ' << pts
[i
]+1;
119 } else if (m_Grid
->GetCellType(cellId
) == VTK_QUAD
) {
120 f
<< " 3 1 " << cell_code
->GetValue(cellId
);
121 for (int i
= 0; i
< num_pts
; ++i
) {
122 f
<< ' ' << pts
[i
]+1;
124 } else if (m_Grid
->GetCellType(cellId
) == VTK_TETRA
) {
126 for (int i
= 0; i
< num_pts
; ++i
) {
127 f
<< ' ' << pts
[i
]+1;
129 } else if (m_Grid
->GetCellType(cellId
) == VTK_PYRAMID
) {
131 for (int i
= 0; i
< num_pts
; ++i
) {
132 f
<< ' ' << pts
[i
]+1;
134 } else if (m_Grid
->GetCellType(cellId
) == VTK_WEDGE
) {
136 for (int i
= 3; i
< num_pts
; ++i
) {
137 f
<< ' ' << pts
[i
]+1;
139 for (int i
= 0; i
< num_pts
-3; ++i
) {
140 f
<< ' ' << pts
[i
]+1;
142 } else if (m_Grid
->GetCellType(cellId
) == VTK_HEXAHEDRON
) {
144 for (int i
= 0; i
< num_pts
; ++i
) {
145 f
<< ' ' << pts
[i
]+1;
150 f
<< "$EndElements\n";
153 void GmshWriter::operate()
156 QFileInfo
file_info(GuiMainWindow::pointer()->getFilename());
157 readOutputFileName(file_info
.completeBaseName() + ".msh");
159 if (format
== ascii1
) writeAscii1(m_Grid
);
160 else if (format
== ascii2
) writeAscii2(m_Grid
);
162 } catch (Error err
) {