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
|
/*
* 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 <QFile>
#include <sys/types.h>
#include <sys/stat.h>
enum import_type_e {
TYPE_STL,
TYPE_OFF,
TYPE_DXF
};
class ImportModule : public AbstractModule
{
public:
import_type_e type;
ImportModule(import_type_e type) : type(type) { }
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
};
class ImportNode : public AbstractPolyNode
{
public:
import_type_e type;
QString filename;
QString layername;
int convexity;
double fn, fs, fa;
double origin_x, origin_y, scale;
ImportNode(const ModuleInstantiation *mi, import_type_e type) : AbstractPolyNode(mi), type(type) { }
virtual PolySet *render_polyset(render_mode_e mode) const;
virtual QString dump(QString indent) const;
};
AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const
{
ImportNode *node = new ImportNode(inst, type);
QVector<QString> argnames;
if (type == TYPE_DXF) {
argnames = QVector<QString>() << "file" << "layer" << "convexity" << "origin" << "scale";
} else {
argnames = QVector<QString>() << "file" << "convexity";
}
QVector<Expression*> argexpr;
// Map old argnames to new argnames for compatibility
QVector<QString> inst_argnames = inst->argnames;
for (int i=0; i<inst_argnames.size(); i++) {
if (inst_argnames[i] == "filename")
inst_argnames[i] = "file";
if (inst_argnames[i] == "layername")
inst_argnames[i] = "layer";
}
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;
node->filename = c.lookup_variable("file").text;
node->layername = c.lookup_variable("layer", true).text;
node->convexity = c.lookup_variable("convexity", true).num;
if (node->convexity <= 0)
node->convexity = 1;
Value origin = c.lookup_variable("origin", true);
node->origin_x = node->origin_y = 0;
origin.getv2(node->origin_x, node->origin_y);
node->scale = c.lookup_variable("scale", true).num;
if (node->scale <= 0)
node->scale = 1;
return node;
}
void register_builtin_import()
{
builtin_modules["import_stl"] = new ImportModule(TYPE_STL);
builtin_modules["import_off"] = new ImportModule(TYPE_OFF);
builtin_modules["import_dxf"] = new ImportModule(TYPE_DXF);
}
PolySet *ImportNode::render_polyset(render_mode_e) const
{
PolySet *p = new PolySet();
p->convexity = convexity;
if (type == TYPE_STL)
{
handle_dep(filename);
QFile f(filename);
if (!f.open(QIODevice::ReadOnly)) {
PRINTF("WARNING: Can't open import file `%s'.", filename.toAscii().data());
return p;
}
QByteArray data = f.read(5);
if (data.size() == 5 && QString(data) == QString("solid"))
{
int i = 0;
double vdata[3][3];
QRegExp splitre = QRegExp("\\s*(vertex)?\\s+");
f.readLine();
while (!f.atEnd())
{
QString line = QString(f.readLine()).remove("\n").remove("\r");
if (line.contains("solid") || line.contains("facet") || line.contains("endloop"))
continue;
if (line.contains("outer loop")) {
i = 0;
continue;
}
if (line.contains("vertex")) {
QStringList tokens = line.split(splitre);
bool ok[3] = { false, false, false };
if (tokens.size() == 4) {
vdata[i][0] = tokens[1].toDouble(&ok[0]);
vdata[i][1] = tokens[2].toDouble(&ok[1]);
vdata[i][2] = tokens[3].toDouble(&ok[2]);
}
if (!ok[0] || !ok[1] || !ok[2]) {
PRINTF("WARNING: Can't parse vertex line `%s'.", line.toAscii().data());
i = 10;
} else if (++i == 3) {
p->append_poly();
p->append_vertex(vdata[0][0], vdata[0][1], vdata[0][2]);
p->append_vertex(vdata[1][0], vdata[1][1], vdata[1][2]);
p->append_vertex(vdata[2][0], vdata[2][1], vdata[2][2]);
}
}
}
}
else
{
f.read(80-5+4);
while (1) {
struct {
float i, j, k;
float x1, y1, z1;
float x2, y2, z2;
float x3, y3, z3;
unsigned short acount;
} __attribute__ ((packed)) data;
if (f.read((char*)&data, sizeof(data)) != sizeof(data))
break;
p->append_poly();
p->append_vertex(data.x1, data.y1, data.z1);
p->append_vertex(data.x2, data.y2, data.z2);
p->append_vertex(data.x3, data.y3, data.z3);
}
}
}
if (type == TYPE_OFF)
{
PRINTF("WARNING: OFF import is not implemented yet.");
}
if (type == TYPE_DXF)
{
DxfData dd(fn, fs, fa, filename, layername, origin_x, origin_y, scale);
p->is2d = true;
dxf_tesselate(p, &dd, 0, true, false, 0);
dxf_border_to_ps(p, &dd);
}
return p;
}
QString ImportNode::dump(QString indent) const
{
if (dump_cache.isEmpty()) {
QString text;
struct stat st;
memset(&st, 0, sizeof(struct stat));
stat(filename.toAscii().data(), &st);
if (type == TYPE_STL)
text.sprintf("import_stl(file = \"%s\", cache = \"%x.%x\", convexity = %d);\n",
filename.toAscii().data(), (int)st.st_mtime, (int)st.st_size, convexity);
if (type == TYPE_OFF)
text.sprintf("import_off(file = \"%s\", cache = \"%x.%x\", convexity = %d);\n",
filename.toAscii().data(), (int)st.st_mtime, (int)st.st_size, convexity);
if (type == TYPE_DXF)
text.sprintf("import_dxf(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;
}
|