/
export.cc
245 lines (221 loc) · 7.42 KB
/
export.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* OpenSCAD (www.openscad.org)
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
* Marius Kintel <marius@kintel.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* As a special exception, you have permission to link this program
* with the CGAL library and distribute executables, as long as you
* follow the requirements of the GNU GPL in regard to all of the
* software in the executable aside from CGAL.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "printutils.h"
#include "polyset.h"
#include "dxfdata.h"
#include <QApplication>
#include <QProgressDialog>
#include <errno.h>
#ifdef ENABLE_CGAL
#include "cgal.h"
void cgal_nef3_to_polyset(PolySet *ps, CGAL_Nef_polyhedron *root_N)
{
CGAL_Polyhedron P;
root_N->p3.convert_to_Polyhedron(P);
typedef CGAL_Polyhedron::Vertex Vertex;
typedef CGAL_Polyhedron::Vertex_const_iterator VCI;
typedef CGAL_Polyhedron::Facet_const_iterator FCI;
typedef CGAL_Polyhedron::Halfedge_around_facet_const_circulator HFCC;
for (FCI fi = P.facets_begin(); fi != P.facets_end(); ++fi) {
HFCC hc = fi->facet_begin();
HFCC hc_end = hc;
Vertex v1, v2, v3;
v1 = *VCI((hc++)->vertex());
v3 = *VCI((hc++)->vertex());
do {
v2 = v3;
v3 = *VCI((hc++)->vertex());
double x1 = CGAL::to_double(v1.point().x());
double y1 = CGAL::to_double(v1.point().y());
double z1 = CGAL::to_double(v1.point().z());
double x2 = CGAL::to_double(v2.point().x());
double y2 = CGAL::to_double(v2.point().y());
double z2 = CGAL::to_double(v2.point().z());
double x3 = CGAL::to_double(v3.point().x());
double y3 = CGAL::to_double(v3.point().y());
double z3 = CGAL::to_double(v3.point().z());
ps->append_poly();
ps->append_vertex(x1, y1, z1);
ps->append_vertex(x2, y2, z2);
ps->append_vertex(x3, y3, z3);
} while (hc != hc_end);
}
}
/*!
Saves the current 3D CGAL Nef polyhedron as STL to the given absolute filename.
*/
void export_stl(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog *pd)
{
CGAL_Polyhedron P;
root_N->p3.convert_to_Polyhedron(P);
typedef CGAL_Polyhedron::Vertex Vertex;
typedef CGAL_Polyhedron::Vertex_const_iterator VCI;
typedef CGAL_Polyhedron::Facet_const_iterator FCI;
typedef CGAL_Polyhedron::Halfedge_around_facet_const_circulator HFCC;
setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output
FILE *f = fopen(filename.toUtf8().data(), "w");
if (!f) {
PRINTA("Can't open STL file \"%1\" for STL export: %2",
filename, QString(strerror(errno)));
set_output_handler(NULL, NULL);
return;
}
fprintf(f, "solid OpenSCAD_Model\n");
int facet_count = 0;
for (FCI fi = P.facets_begin(); fi != P.facets_end(); ++fi) {
HFCC hc = fi->facet_begin();
HFCC hc_end = hc;
Vertex v1, v2, v3;
v1 = *VCI((hc++)->vertex());
v3 = *VCI((hc++)->vertex());
do {
v2 = v3;
v3 = *VCI((hc++)->vertex());
double x1 = CGAL::to_double(v1.point().x());
double y1 = CGAL::to_double(v1.point().y());
double z1 = CGAL::to_double(v1.point().z());
double x2 = CGAL::to_double(v2.point().x());
double y2 = CGAL::to_double(v2.point().y());
double z2 = CGAL::to_double(v2.point().z());
double x3 = CGAL::to_double(v3.point().x());
double y3 = CGAL::to_double(v3.point().y());
double z3 = CGAL::to_double(v3.point().z());
QString vs1, vs2, vs3;
vs1.sprintf("%f %f %f", x1, y1, z1);
vs2.sprintf("%f %f %f", x2, y2, z2);
vs3.sprintf("%f %f %f", x3, y3, z3);
if (vs1 != vs2 && vs1 != vs3 && vs2 != vs3) {
double nx = (y1-y2)*(z1-z3) - (z1-z2)*(y1-y3);
double ny = (z1-z2)*(x1-x3) - (x1-x2)*(z1-z3);
double nz = (x1-x2)*(y1-y3) - (y1-y2)*(x1-x3);
double nlength = sqrt(nx*nx + ny*ny + nz*nz);
// Avoid generating normals for polygons with zero area
double eps = 0.000001;
if (nlength < eps) nlength = 1.0;
fprintf(f, " facet normal %f %f %f\n",
nx / nlength, ny / nlength, nz / nlength);
fprintf(f, " outer loop\n");
fprintf(f, " vertex %s\n", vs1.toAscii().data());
fprintf(f, " vertex %s\n", vs2.toAscii().data());
fprintf(f, " vertex %s\n", vs3.toAscii().data());
fprintf(f, " endloop\n");
fprintf(f, " endfacet\n");
}
} while (hc != hc_end);
if (pd) {
pd->setValue(facet_count++);
QApplication::processEvents();
}
}
fprintf(f, "endsolid OpenSCAD_Model\n");
fclose(f);
setlocale(LC_NUMERIC, ""); // Set default locale
}
void export_off(CGAL_Nef_polyhedron*, QString, QProgressDialog*)
{
PRINTF("WARNING: OFF import is not implemented yet.");
}
/*!
Saves the current 2D CGAL Nef polyhedron as DXF to the given absolute filename.
*/
void export_dxf(CGAL_Nef_polyhedron *root_N, QString filename, QProgressDialog *)
{
FILE *f = fopen(filename.toUtf8().data(), "w");
if (!f) {
PRINTA("Can't open DXF file \"%1\" for DXF export: %2",
filename, QString(strerror(errno)));
set_output_handler(NULL, NULL);
return;
}
setlocale(LC_NUMERIC, "C"); // Ensure radix is . (not ,) in output
// Some importers (e.g. QCAD) needs a HEADER section specifying AutoCAD 2000 as
// the file format for LWPOLYLINE entities to work
fprintf(f, " 0\n"
"SECTION\n"
" 2\n"
"HEADER\n"
" 9\n"
"$ACADVER\n"
" 1\n"
"AC1015\n"
" 0\n"
"ENDSEC\n");
// Some importers (e.g. Inkscape) needs a BLOCKS section to be present
fprintf(f, " 0\n"
"SECTION\n"
" 2\n"
"BLOCKS\n"
" 0\n"
"ENDSEC\n");
fprintf(f, " 0\n"
"SECTION\n"
" 2\n"
"ENTITIES\n");
DxfData dd(*root_N);
for (int i=0; i<dd.paths.size(); i++)
{
if (dd.paths[i].points.size() < 2)
// not a valid polygon
continue;
// Use the LWPOLYLINE class - this makes it easier to handle complete
// objects (as paths) in Inkscape.
fprintf(f, " 0\n");
fprintf(f, "LWPOLYLINE\n");
// Some importers (e.g. Inkscape) need a layer to be specified
fprintf(f, " 8\n");
fprintf(f, "0\n");
// number of vertices
fprintf(f, " 90\n");
fprintf(f, "%d\n", dd.paths[i].points.size());
// polygon flag (closed, ...)
fprintf(f, " 70\n");
fprintf(f, "%d\n", dd.paths[i].is_closed ? 1 : 0);
// add all points
for (int j=0; j<dd.paths[i].points.size(); j++) {
DxfData::Point *p = dd.paths[i].points[j];
fprintf(f, " 10\n");
fprintf(f, "%f\n", p->x);
fprintf(f, " 20\n");
fprintf(f, "%f\n", p->y);
}
}
fprintf(f, " 0\n");
fprintf(f, "ENDSEC\n");
// Some importers (e.g. Inkscape) needs an OBJECTS section with a DICTIONARY entry
fprintf(f, " 0\n"
"SECTION\n"
" 2\n"
"OBJECTS\n"
" 0\n"
"DICTIONARY\n"
" 0\n"
"ENDSEC\n");
fprintf(f, " 0\n");
fprintf(f, "EOF\n");
fclose(f);
setlocale(LC_NUMERIC, ""); // Set default locale
}
#endif