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
|
/*
* OpenSCAD (www.openscad.at)
* Copyright (C) 2009 Clifford Wolf <clifford@clifford.at>
*
* 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.
*
* 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
*
*/
#define INCLUDE_ABSTRACT_NODE_DETAILS
#include "openscad.h"
#include "printutils.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
class DxfRotateExtrudeModule : public AbstractModule
{
public:
DxfRotateExtrudeModule() { }
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
};
class DxfRotateExtrudeNode : public AbstractPolyNode
{
public:
int convexity;
double fn, fs, fa;
double origin_x, origin_y, scale;
QString filename, layername;
DxfRotateExtrudeNode(const ModuleInstantiation *mi) : AbstractPolyNode(mi) {
convexity = 0;
fn = fs = fa = 0;
origin_x = origin_y = scale = 0;
}
virtual PolySet *render_polyset(render_mode_e mode) const;
virtual QString dump(QString indent) const;
};
AbstractNode *DxfRotateExtrudeModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const
{
DxfRotateExtrudeNode *node = new DxfRotateExtrudeNode(inst);
QVector<QString> argnames = QVector<QString>() << "file" << "layer" << "origin" << "scale";
QVector<Expression*> argexpr;
Context c(ctx);
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
node->fn = c.lookup_variable("$fn").num;
node->fs = c.lookup_variable("$fs").num;
node->fa = c.lookup_variable("$fa").num;
Value file = c.lookup_variable("file");
Value layer = c.lookup_variable("layer", true);
Value convexity = c.lookup_variable("convexity", true);
Value origin = c.lookup_variable("origin", true);
Value scale = c.lookup_variable("scale", true);
node->filename = file.text;
node->layername = layer.text;
node->convexity = (int)convexity.num;
origin.getv2(node->origin_x, node->origin_y);
node->scale = scale.num;
if (node->convexity <= 0)
node->convexity = 1;
if (node->scale <= 0)
node->scale = 1;
return node;
}
void register_builtin_dxf_rotate_extrude()
{
builtin_modules["dxf_rotate_extrude"] = new DxfRotateExtrudeModule();
}
PolySet *DxfRotateExtrudeNode::render_polyset(render_mode_e) const
{
QString key = mk_cache_id();
if (PolySet::ps_cache.contains(key)) {
PRINT(PolySet::ps_cache[key]->msg);
return PolySet::ps_cache[key]->ps->link();
}
print_messages_push();
DxfData dxf(fn, fs, fa, filename, layername, origin_x, origin_y, scale);
PolySet *ps = new PolySet();
ps->convexity = convexity;
for (int i = 0; i < dxf.paths.count(); i++)
{
double max_x = 0;
for (int j = 0; j < dxf.paths[i].points.count(); j++) {
max_x = fmax(max_x, dxf.paths[i].points[j]->x);
}
int fragments = get_fragments_from_r(max_x, fn, fs, fa);
double points[fragments][dxf.paths[i].points.count()][3];
for (int j = 0; j < fragments; j++) {
double a = (j*2*M_PI) / fragments;
for (int k = 0; k < dxf.paths[i].points.count(); k++) {
if (dxf.paths[i].points[k]->x == 0) {
points[j][k][0] = 0;
points[j][k][1] = 0;
} else {
points[j][k][0] = dxf.paths[i].points[k]->x * sin(a);
points[j][k][1] = dxf.paths[i].points[k]->x * cos(a);
}
points[j][k][2] = dxf.paths[i].points[k]->y;
}
}
for (int j = 0; j < fragments; j++) {
int j1 = j + 1 < fragments ? j + 1 : 0;
for (int k = 0; k < dxf.paths[i].points.count(); k++) {
int k1 = k + 1 < dxf.paths[i].points.count() ? k + 1 : 0;
if (points[j][k][0] != points[j1][k][0] ||
points[j][k][1] != points[j1][k][1] ||
points[j][k][2] != points[j1][k][2]) {
ps->append_poly();
ps->append_vertex(points[j ][k ][0],
points[j ][k ][1], points[j ][k ][2]);
ps->append_vertex(points[j1][k ][0],
points[j1][k ][1], points[j1][k ][2]);
ps->append_vertex(points[j ][k1][0],
points[j ][k1][1], points[j ][k1][2]);
}
if (points[j][k1][0] != points[j1][k1][0] ||
points[j][k1][1] != points[j1][k1][1] ||
points[j][k1][2] != points[j1][k1][2]) {
ps->append_poly();
ps->append_vertex(points[j ][k1][0],
points[j ][k1][1], points[j ][k1][2]);
ps->append_vertex(points[j1][k ][0],
points[j1][k ][1], points[j1][k ][2]);
ps->append_vertex(points[j1][k1][0],
points[j1][k1][1], points[j1][k1][2]);
}
}
}
}
PolySet::ps_cache.insert(key, new PolySet::ps_cache_entry(ps->link()));
print_messages_pop();
return ps;
}
QString DxfRotateExtrudeNode::dump(QString indent) const
{
if (dump_cache.isEmpty()) {
QString text;
struct stat st;
memset(&st, 0, sizeof(struct stat));
stat(filename.toAscii().data(), &st);
text.sprintf("dxf_rotate_extrude(file = \"%s\", cache = \"%x.%x\", layer = \"%s\", "
"origin = [ %f %f ], scale = %f, convexity = %d, "
"$fn = %f, $fa = %f, $fs = %f);\n",
filename.toAscii().data(), (int)st.st_mtime, (int)st.st_size,
layername.toAscii().data(), origin_x, origin_y, scale, convexity,
fn, fs, fa);
((AbstractNode*)this)->dump_cache = indent + QString("n%1: ").arg(idx) + text;
}
return dump_cache;
}
|