2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 // + This file is part of enGrid. +
6 // + Copyright 2008,2009 Oliver Gloth +
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 "stlreader.h"
24 #include "correctsurfaceorientation.h"
26 #include "vtkEgPolyDataToUnstructuredGridFilter.h"
27 #include <vtkSTLReader.h>
28 #include <vtkCleanPolyData.h>
30 StlReader::StlReader()
32 setFormat("STL files(*.stl *.STL)");
35 void StlReader::operate()
39 vtkSTLReader
*stl
= vtkSTLReader::New();
41 stl
->SetFileName(getCFileName());
43 EG_VTKSP(vtkPolyData
, poly
);
44 poly
->DeepCopy(stl
->GetOutput());
47 for (vtkIdType cellId
= 0; cellId
< poly
->GetNumberOfCells(); ++cellId
) {
49 poly
->GetCellPoints(cellId
, Npts
, pts
);
50 for (int i
= 0; i
< Npts
; ++i
) {
52 poly
->GetPoints()->GetPoint(pts
[i
], x1
.data());
54 poly
->GetPoints()->GetPoint(pts
[0], x2
.data());
56 poly
->GetPoints()->GetPoint(pts
[i
+1], x2
.data());
58 L
= min(L
, (x1
-x2
).abs());
61 EG_VTKSP(vtkCleanPolyData
, poly_clean
);
62 poly_clean
->ToleranceIsAbsoluteOn();
63 poly_clean
->SetAbsoluteTolerance(0.5*L
);
64 poly_clean
->SetInput(poly
);
65 EG_VTKSP(vtkEgPolyDataToUnstructuredGridFilter
, poly2ugrid
);
66 poly2ugrid
->SetInput(poly_clean
->GetOutput());
69 allocateGrid(grid
, poly2ugrid
->GetOutput()->GetNumberOfCells(), poly2ugrid
->GetOutput()->GetNumberOfPoints());
70 for (vtkIdType id_node
= 0; id_node
< poly2ugrid
->GetOutput()->GetNumberOfPoints(); ++id_node
) {
72 poly2ugrid
->GetOutput()->GetPoints()->GetPoint(id_node
, x
.data());
73 grid
->GetPoints()->SetPoint(id_node
, x
.data());
75 for (vtkIdType id_cell
= 0; id_cell
< poly2ugrid
->GetOutput()->GetNumberOfCells(); ++id_cell
) {
76 vtkIdType N_pts
, *pts
;
77 vtkIdType type_cell
= poly2ugrid
->GetOutput()->GetCellType(id_cell
);
78 poly2ugrid
->GetOutput()->GetCellPoints(id_cell
, N_pts
, pts
);
79 grid
->InsertNextCell(type_cell
, N_pts
, pts
);
82 EG_VTKDCC(vtkIntArray
, bc
, grid
, "cell_code");
83 for (vtkIdType cellId
= 0; cellId
< grid
->GetNumberOfCells(); ++cellId
) {
84 bc
->SetValue(cellId
, 999);
87 CorrectSurfaceOrientation corr_surf
;
88 corr_surf
.setGrid(grid
);