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 "gmshreader.h"
22 #include "correctsurfaceorientation.h"
25 #include "guimainwindow.h"
27 void GmshReader::readAscii1(vtkUnstructuredGrid
*m_Grid
)
29 vtkIdType Nnodes
, Ncells
;
30 QFile
file(getFileName());
31 file
.open(QIODevice::ReadOnly
| QIODevice::Text
);
35 if (word
!= "$NOD") EG_ERR_RETURN("$NOD expected");
37 EG_VTKSP(vtkUnstructuredGrid
, ug
);
38 QVector
<vtkIdType
> idxmap(Nnodes
+ 1);
39 QVector
<vec3_t
> x(Nnodes
);
40 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
42 f
>> ir
>> x
[i
][0] >> x
[i
][1] >> x
[i
][2];
46 if (word
!= "$ENDNOD") EG_ERR_RETURN("$ENDNOD expected");
48 if (word
!= "$ELM") EG_ERR_RETURN("$ELM expected");
50 allocateGrid(ug
, Ncells
, Nnodes
, false);
51 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
52 ug
->GetPoints()->SetPoint(i
, x
[i
].data());
54 EG_VTKSP(vtkIntArray
, cell_code
);
55 cell_code
->SetName("cell_code");
56 cell_code
->SetNumberOfValues(Ncells
);
57 ug
->GetCellData()->AddArray(cell_code
);
58 for (vtkIdType i
= 0; i
< Ncells
; ++i
) {
60 int elm_type
, reg_phys
;
65 cell_code
->SetValue(i
, reg_phys
);
68 if (elm_type
== 2) { // triangle
69 for (vtkIdType j
= 0; j
< 3; ++j
) {
71 pts
[j
] = idxmap
[node
];
73 ug
->InsertNextCell(VTK_TRIANGLE
,3,pts
);
74 } else if (elm_type
== 3) { // quad
75 for (vtkIdType j
= 0; j
< 4; ++j
) {
77 pts
[j
] = idxmap
[node
];
79 ug
->InsertNextCell(VTK_QUAD
,4,pts
);
80 } else if (elm_type
== 4) { // tetrahedron
81 for (vtkIdType j
= 0; j
< 4; ++j
) {
83 pts
[j
] = idxmap
[node
];
88 ug
->InsertNextCell(VTK_TETRA
,4,pts
);
89 } else if (elm_type
== 5) { // hexhedron
90 for (vtkIdType j
= 0; j
< 8; ++j
) {
92 pts
[j
] = idxmap
[node
];
94 ug
->InsertNextCell(VTK_HEXAHEDRON
,8,pts
);
95 } else if (elm_type
== 6) { // prism/wedge
96 for (vtkIdType j
= 0; j
< 3; ++j
) {
98 pts
[j
+3] = idxmap
[node
];
100 for (vtkIdType j
= 0; j
< 3; ++j
) {
102 pts
[j
] = idxmap
[node
];
104 ug
->InsertNextCell(VTK_WEDGE
,6,pts
);
105 } else if (elm_type
== 7) { // pyramid
106 for (vtkIdType j
= 0; j
< 5; ++j
) {
108 pts
[j
] = idxmap
[node
];
110 ug
->InsertNextCell(VTK_PYRAMID
,5,pts
);
113 ug
->GetCellData()->AddArray(cell_code
);
114 m_Grid
->DeepCopy(ug
);
117 void GmshReader::readAscii2(vtkUnstructuredGrid
*m_Grid
)
119 vtkIdType Nnodes
, Ncells
;
120 QFile
file(getFileName());
121 file
.open(QIODevice::ReadOnly
| QIODevice::Text
);
122 QTextStream
f(&file
);
125 if (word
!= "$MeshFormat") EG_ERR_RETURN("$MeshFormat expected");
130 if (word
!= "$EndMeshFormat") EG_ERR_RETURN("$EndMeshFormat expected");
132 if (word
!= "$Nodes") EG_ERR_RETURN("$Nodes expected");
134 EG_VTKSP(vtkUnstructuredGrid
, ug
);
135 QVector
<vtkIdType
> idxmap(Nnodes
+ 1);
136 QVector
<vec3_t
> x(Nnodes
);
137 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
139 f
>> ir
>> x
[i
][0] >> x
[i
][1] >> x
[i
][2];
143 if (word
!= "$EndNodes") EG_ERR_RETURN("$EndNotes expected");
145 if (word
!= "$Elements") EG_ERR_RETURN("$Elements expected");
147 allocateGrid(ug
, Ncells
, Nnodes
, false);
148 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
149 ug
->GetPoints()->SetPoint(i
, x
[i
].data());
151 EG_VTKSP(vtkIntArray
, cell_code
);
152 cell_code
->SetName("cell_code");
153 cell_code
->SetNumberOfValues(Ncells
);
154 ug
->GetCellData()->AddArray(cell_code
);
156 for (vtkIdType i
= 0; i
< Ncells
; ++i
) {
158 int elm_type
, Ntags
, bc
;
168 for (int j
= 1; j
< Ntags
; ++j
) f
>> word
;
170 cell_code
->SetValue(i
, bc
);
173 if (elm_type
== 2) { // triangle
174 for (vtkIdType j
= 0; j
< 3; ++j
) {
176 pts
[j
] = idxmap
[node
];
178 ug
->InsertNextCell(VTK_TRIANGLE
,3,pts
);
179 } else if (elm_type
== 3) { // quad
180 for (vtkIdType j
= 0; j
< 4; ++j
) {
182 pts
[j
] = idxmap
[node
];
184 ug
->InsertNextCell(VTK_QUAD
,4,pts
);
185 } else if (elm_type
== 4) { // tetrahedron
186 for (vtkIdType j
= 0; j
< 4; ++j
) {
188 pts
[j
] = idxmap
[node
];
190 ug
->InsertNextCell(VTK_TETRA
,4,pts
);
191 } else if (elm_type
== 5) { // hexhedron
192 for (vtkIdType j
= 0; j
< 8; ++j
) {
194 pts
[j
] = idxmap
[node
];
196 ug
->InsertNextCell(VTK_HEXAHEDRON
,8,pts
);
197 } else if (elm_type
== 6) { // prism/wedge
198 for (vtkIdType j
= 0; j
< 3; ++j
) {
200 pts
[j
+3] = idxmap
[node
];
202 for (vtkIdType j
= 0; j
< 3; ++j
) {
204 pts
[j
] = idxmap
[node
];
206 ug
->InsertNextCell(VTK_WEDGE
,6,pts
);
207 } else if (elm_type
== 7) { // pyramid
208 for (vtkIdType j
= 0; j
< 5; ++j
) {
210 pts
[j
] = idxmap
[node
];
212 ug
->InsertNextCell(VTK_PYRAMID
,5,pts
);
215 ug
->GetCellData()->AddArray(cell_code
);
216 m_Grid
->DeepCopy(ug
);
219 void GmshReader::operate()
222 QFileInfo
file_info(GuiMainWindow::pointer()->getFilename());
223 readInputFileName(file_info
.completeBaseName() + ".msh");
225 if (format
== ascii1
) {
228 if (format
== ascii2
) {
231 createBasicFields(m_Grid
, m_Grid
->GetNumberOfCells(), m_Grid
->GetNumberOfPoints());
232 updateCellIndex(m_Grid
);
233 CorrectSurfaceOrientation corr_surf
;
234 corr_surf
.setGrid(m_Grid
);
237 } catch (Error err
) {