updated to modern VTK
[engrid-github.git] / src / libengrid / blenderwriter.cpp
blob828c6168a1b6616ab08316efac46853416ebd2a7
1 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // + +
3 // + This file is part of enGrid. +
4 // + +
5 // + Copyright 2008-2014 enGits GmbH +
6 // + +
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. +
11 // + +
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. +
16 // + +
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/>. +
19 // + +
20 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 #include "blenderwriter.h"
22 #include "guimainwindow.h"
24 BlenderWriter::BlenderWriter()
26 setFormat("Blender/Engrid files(*.begc)");
29 void BlenderWriter::operate()
31 try {
32 QFileInfo file_info(GuiMainWindow::pointer()->getFilename());
33 readOutputFileName(file_info.completeBaseName() + ".begc");
34 if (isValid()) {
35 QFile file(getFileName());
36 file.open(QIODevice::WriteOnly | QIODevice::Text);
37 QTextStream f(&file);
39 // write number of objects
40 QSet<int> bcs = GuiMainWindow::pointer()->getAllBoundaryCodes();
41 int N_BoundaryCodes = bcs.size();
42 qWarning()<<"N_BoundaryCodes="<<N_BoundaryCodes;
43 f << N_BoundaryCodes << "\n";
45 // write names of objects
46 foreach(int bc, bcs) {
47 QString BCname = getBC(bc).getName();
48 qWarning()<<"BCname="<<BCname;
49 f << BCname << "\n";
52 // write objects
53 int offset = 0;
54 foreach(int bc, bcs) {
56 QVector <vtkIdType> object_cells;
57 QSet <int> object_bc_set;
58 object_bc_set.insert(bc);
59 getSurfaceCells(object_bc_set, object_cells, m_Grid);
61 EG_VTKSP(vtkUnstructuredGrid,SubGrid);
62 getSubGrid(m_Grid,object_cells,SubGrid);
64 int N_verts = SubGrid->GetNumberOfPoints();
65 int N_faces = SubGrid->GetNumberOfCells();
66 qWarning()<<"N_verts="<<N_verts << " N_faces=" << N_faces;
67 f << N_verts << " " << N_faces << "\n";
69 // prepare to write vertices and faces
70 QVector <vec3_t> subvertices_data(N_verts);
71 QMap <vtkIdType, vtkIdType> subvertices_idx;
72 int idx = 0;
73 QVector <bool> vertex_written(m_Grid->GetNumberOfPoints(), false);
74 foreach (vtkIdType cellId, object_cells) {
75 EG_GET_CELL(cellId, m_Grid);
76 for (int i = 0; i < num_pts; ++i) {
77 if(!vertex_written[pts[i]]) {
78 vec3_t x;
79 m_Grid->GetPoints()->GetPoint(pts[i], x.data());
80 if(idx<0 || idx>=N_verts) EG_BUG;
81 subvertices_data[idx] = x;
82 subvertices_idx[pts[i]]=idx;
83 idx++;
84 vertex_written[pts[i]] = true;
89 // write vertices
90 foreach(vec3_t x, subvertices_data) {
91 f << x[0] << ' ' << x[1] << ' ' << x[2] << '\n';
94 // write faces
95 foreach (vtkIdType cellId, object_cells) {
96 EG_GET_CELL(cellId, m_Grid);
97 f << num_pts;
98 for (int i = 0; i < num_pts; ++i) {
99 f << ' ' << offset + subvertices_idx[pts[i]];
101 f << '\n';
104 offset+=N_verts;
106 }// end of loop through objects
108 file.close();
110 }// end of if(isValid)
111 } catch (Error err) {
112 err.display();