2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008-2010 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 "gmshreader.h"
24 #include "correctsurfaceorientation.h"
27 #include "guimainwindow.h"
29 void GmshReader::readAscii1(vtkUnstructuredGrid
*m_Grid
)
31 vtkIdType Nnodes
, Ncells
;
32 QFile
file(getFileName());
33 file
.open(QIODevice::ReadOnly
| QIODevice::Text
);
37 if (word
!= "$NOD") EG_ERR_RETURN("$NOD expected");
39 EG_VTKSP(vtkUnstructuredGrid
, ug
);
40 QVector
<vtkIdType
> idxmap(Nnodes
+ 1);
41 QVector
<vec3_t
> x(Nnodes
);
42 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
44 f
>> ir
>> x
[i
][0] >> x
[i
][1] >> x
[i
][2];
48 if (word
!= "$ENDNOD") EG_ERR_RETURN("$ENDNOD expected");
50 if (word
!= "$ELM") EG_ERR_RETURN("$ELM expected");
52 allocateGrid(ug
, Ncells
, Nnodes
, false);
53 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
54 ug
->GetPoints()->SetPoint(i
, x
[i
].data());
56 EG_VTKSP(vtkIntArray
, cell_code
);
57 cell_code
->SetName("cell_code");
58 cell_code
->SetNumberOfValues(Ncells
);
59 ug
->GetCellData()->AddArray(cell_code
);
60 for (vtkIdType i
= 0; i
< Ncells
; ++i
) {
62 int elm_type
, reg_phys
;
67 cell_code
->SetValue(i
, reg_phys
);
70 if (elm_type
== 2) { // triangle
71 for (vtkIdType j
= 0; j
< 3; ++j
) {
73 pts
[j
] = idxmap
[node
];
75 ug
->InsertNextCell(VTK_TRIANGLE
,3,pts
);
76 } else if (elm_type
== 3) { // quad
77 for (vtkIdType j
= 0; j
< 4; ++j
) {
79 pts
[j
] = idxmap
[node
];
81 ug
->InsertNextCell(VTK_QUAD
,4,pts
);
82 } else if (elm_type
== 4) { // tetrahedron
83 for (vtkIdType j
= 0; j
< 4; ++j
) {
85 pts
[j
] = idxmap
[node
];
90 ug
->InsertNextCell(VTK_TETRA
,4,pts
);
91 } else if (elm_type
== 5) { // hexhedron
92 for (vtkIdType j
= 0; j
< 8; ++j
) {
94 pts
[j
] = idxmap
[node
];
96 ug
->InsertNextCell(VTK_HEXAHEDRON
,8,pts
);
97 } else if (elm_type
== 6) { // prism/wedge
98 for (vtkIdType j
= 0; j
< 3; ++j
) {
100 pts
[j
+3] = idxmap
[node
];
102 for (vtkIdType j
= 0; j
< 3; ++j
) {
104 pts
[j
] = idxmap
[node
];
106 ug
->InsertNextCell(VTK_WEDGE
,6,pts
);
107 } else if (elm_type
== 7) { // pyramid
108 for (vtkIdType j
= 0; j
< 5; ++j
) {
110 pts
[j
] = idxmap
[node
];
112 ug
->InsertNextCell(VTK_PYRAMID
,5,pts
);
115 ug
->GetCellData()->AddArray(cell_code
);
116 m_Grid
->DeepCopy(ug
);
119 void GmshReader::readAscii2(vtkUnstructuredGrid
*m_Grid
)
121 vtkIdType Nnodes
, Ncells
;
122 QFile
file(getFileName());
123 file
.open(QIODevice::ReadOnly
| QIODevice::Text
);
124 QTextStream
f(&file
);
127 if (word
!= "$MeshFormat") EG_ERR_RETURN("$MeshFormat expected");
132 if (word
!= "$EndMeshFormat") EG_ERR_RETURN("$EndMeshFormat expected");
134 if (word
!= "$Nodes") EG_ERR_RETURN("$Nodes expected");
136 EG_VTKSP(vtkUnstructuredGrid
, ug
);
137 QVector
<vtkIdType
> idxmap(Nnodes
+ 1);
138 QVector
<vec3_t
> x(Nnodes
);
139 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
141 f
>> ir
>> x
[i
][0] >> x
[i
][1] >> x
[i
][2];
145 if (word
!= "$EndNodes") EG_ERR_RETURN("$EndNotes expected");
147 if (word
!= "$Elements") EG_ERR_RETURN("$Elements expected");
149 allocateGrid(ug
, Ncells
, Nnodes
, false);
150 for (vtkIdType i
= 0; i
< Nnodes
; ++i
) {
151 ug
->GetPoints()->SetPoint(i
, x
[i
].data());
153 EG_VTKSP(vtkIntArray
, cell_code
);
154 cell_code
->SetName("cell_code");
155 cell_code
->SetNumberOfValues(Ncells
);
156 ug
->GetCellData()->AddArray(cell_code
);
158 for (vtkIdType i
= 0; i
< Ncells
; ++i
) {
160 int elm_type
, Ntags
, bc
;
170 for (int j
= 1; j
< Ntags
; ++j
) f
>> word
;
172 cell_code
->SetValue(i
, bc
);
175 if (elm_type
== 2) { // triangle
176 for (vtkIdType j
= 0; j
< 3; ++j
) {
178 pts
[j
] = idxmap
[node
];
180 ug
->InsertNextCell(VTK_TRIANGLE
,3,pts
);
181 } else if (elm_type
== 3) { // quad
182 for (vtkIdType j
= 0; j
< 4; ++j
) {
184 pts
[j
] = idxmap
[node
];
186 ug
->InsertNextCell(VTK_QUAD
,4,pts
);
187 } else if (elm_type
== 4) { // tetrahedron
188 for (vtkIdType j
= 0; j
< 4; ++j
) {
190 pts
[j
] = idxmap
[node
];
192 ug
->InsertNextCell(VTK_TETRA
,4,pts
);
193 } else if (elm_type
== 5) { // hexhedron
194 for (vtkIdType j
= 0; j
< 8; ++j
) {
196 pts
[j
] = idxmap
[node
];
198 ug
->InsertNextCell(VTK_HEXAHEDRON
,8,pts
);
199 } else if (elm_type
== 6) { // prism/wedge
200 for (vtkIdType j
= 0; j
< 3; ++j
) {
202 pts
[j
+3] = idxmap
[node
];
204 for (vtkIdType j
= 0; j
< 3; ++j
) {
206 pts
[j
] = idxmap
[node
];
208 ug
->InsertNextCell(VTK_WEDGE
,6,pts
);
209 } else if (elm_type
== 7) { // pyramid
210 for (vtkIdType j
= 0; j
< 5; ++j
) {
212 pts
[j
] = idxmap
[node
];
214 ug
->InsertNextCell(VTK_PYRAMID
,5,pts
);
217 ug
->GetCellData()->AddArray(cell_code
);
218 m_Grid
->DeepCopy(ug
);
221 void GmshReader::operate()
224 QFileInfo
file_info(GuiMainWindow::pointer()->getFilename());
225 readInputFileName(file_info
.completeBaseName() + ".msh");
227 if (format
== ascii1
) {
230 if (format
== ascii2
) {
233 createBasicFields(m_Grid
, m_Grid
->GetNumberOfCells(), m_Grid
->GetNumberOfPoints());
234 UpdateCellIndex(m_Grid
);
235 CorrectSurfaceOrientation corr_surf
;
236 corr_surf
.setGrid(m_Grid
);
239 } catch (Error err
) {