2009-07-17 04:34:15 +04:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2010-02-01 12:34:18 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2009-07-17 04:34:15 +04:00
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "module.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "context.h"
|
2010-01-06 13:48:51 +03:00
|
|
|
#include "printutils.h"
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "builtin.h"
|
|
|
|
#include "polyset.h"
|
|
|
|
#include "dxfdata.h"
|
2010-02-09 16:48:09 +03:00
|
|
|
#include "progress.h"
|
2010-01-30 07:26:05 +03:00
|
|
|
#include "openscad.h" // get_fragments_from_r()
|
2009-07-17 04:34:15 +04:00
|
|
|
|
2009-07-23 20:00:38 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2010-01-07 23:49:27 +03:00
|
|
|
#include <QTime>
|
|
|
|
#include <QApplication>
|
2010-01-30 07:26:05 +03:00
|
|
|
#include <QProgressDialog>
|
2010-01-07 23:49:27 +03:00
|
|
|
|
2009-07-17 04:34:15 +04:00
|
|
|
class DxfRotateExtrudeModule : public AbstractModule
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DxfRotateExtrudeModule() { }
|
2010-01-06 22:38:45 +03:00
|
|
|
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
|
2009-07-17 04:34:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class DxfRotateExtrudeNode : public AbstractPolyNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int convexity;
|
|
|
|
double fn, fs, fa;
|
2009-07-20 21:45:39 +04:00
|
|
|
double origin_x, origin_y, scale;
|
2009-07-17 04:34:15 +04:00
|
|
|
QString filename, layername;
|
2010-01-06 22:38:45 +03:00
|
|
|
DxfRotateExtrudeNode(const ModuleInstantiation *mi) : AbstractPolyNode(mi) {
|
2009-07-20 21:45:39 +04:00
|
|
|
convexity = 0;
|
|
|
|
fn = fs = fa = 0;
|
|
|
|
origin_x = origin_y = scale = 0;
|
|
|
|
}
|
2009-07-17 04:34:15 +04:00
|
|
|
virtual PolySet *render_polyset(render_mode_e mode) const;
|
|
|
|
virtual QString dump(QString indent) const;
|
|
|
|
};
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
AbstractNode *DxfRotateExtrudeModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const
|
2009-07-17 04:34:15 +04:00
|
|
|
{
|
|
|
|
DxfRotateExtrudeNode *node = new DxfRotateExtrudeNode(inst);
|
|
|
|
|
2009-07-20 21:45:39 +04:00
|
|
|
QVector<QString> argnames = QVector<QString>() << "file" << "layer" << "origin" << "scale";
|
2009-07-17 04:34:15 +04:00
|
|
|
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");
|
2009-07-20 21:45:39 +04:00
|
|
|
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);
|
2009-07-17 04:34:15 +04:00
|
|
|
|
2010-02-25 13:21:11 +03:00
|
|
|
if(!file.text.isNull())
|
|
|
|
node->filename = c.get_absolute_path(file.text);
|
|
|
|
else
|
|
|
|
node->filename = file.text;
|
|
|
|
|
2009-07-17 04:34:15 +04:00
|
|
|
node->layername = layer.text;
|
2009-08-10 11:18:35 +04:00
|
|
|
node->convexity = (int)convexity.num;
|
2009-07-20 21:45:39 +04:00
|
|
|
origin.getv2(node->origin_x, node->origin_y);
|
|
|
|
node->scale = scale.num;
|
2009-07-17 04:34:15 +04:00
|
|
|
|
|
|
|
if (node->convexity <= 0)
|
|
|
|
node->convexity = 1;
|
|
|
|
|
2009-07-20 21:45:39 +04:00
|
|
|
if (node->scale <= 0)
|
|
|
|
node->scale = 1;
|
|
|
|
|
2010-01-07 23:49:27 +03:00
|
|
|
if (node->filename.isEmpty()) {
|
|
|
|
foreach (ModuleInstantiation *v, inst->children) {
|
|
|
|
AbstractNode *n = v->evaluate(inst->ctx);
|
|
|
|
if (n)
|
|
|
|
node->children.append(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 04:34:15 +04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_builtin_dxf_rotate_extrude()
|
|
|
|
{
|
|
|
|
builtin_modules["dxf_rotate_extrude"] = new DxfRotateExtrudeModule();
|
2010-01-07 23:49:27 +03:00
|
|
|
builtin_modules["rotate_extrude"] = new DxfRotateExtrudeModule();
|
2009-07-17 04:34:15 +04:00
|
|
|
}
|
|
|
|
|
2010-02-20 17:37:20 +03:00
|
|
|
PolySet *DxfRotateExtrudeNode::render_polyset(render_mode_e) const
|
2009-07-17 04:34:15 +04:00
|
|
|
{
|
2009-07-28 16:53:52 +04:00
|
|
|
QString key = mk_cache_id();
|
2010-01-06 13:48:51 +03:00
|
|
|
if (PolySet::ps_cache.contains(key)) {
|
|
|
|
PRINT(PolySet::ps_cache[key]->msg);
|
2009-07-28 22:17:29 +04:00
|
|
|
return PolySet::ps_cache[key]->ps->link();
|
2010-01-06 13:48:51 +03:00
|
|
|
}
|
2009-07-28 16:53:52 +04:00
|
|
|
|
2010-01-06 13:48:51 +03:00
|
|
|
print_messages_push();
|
2010-01-07 23:49:27 +03:00
|
|
|
DxfData *dxf;
|
|
|
|
|
|
|
|
if (filename.isEmpty())
|
|
|
|
{
|
2010-02-01 13:01:05 +03:00
|
|
|
#ifdef ENABLE_CGAL
|
2010-01-07 23:49:27 +03:00
|
|
|
CGAL_Nef_polyhedron N;
|
|
|
|
N.dim = 2;
|
|
|
|
foreach(AbstractNode * v, children) {
|
|
|
|
if (v->modinst->tag_background)
|
|
|
|
continue;
|
|
|
|
N.p2 += v->render_cgal_nef_polyhedron().p2;
|
|
|
|
}
|
|
|
|
dxf = new DxfData(N);
|
|
|
|
|
2010-02-01 13:01:05 +03:00
|
|
|
#else // ENABLE_CGAL
|
|
|
|
PRINT("WARNING: Found rotate_extrude() statement without dxf file but compiled without CGAL support!");
|
|
|
|
dxf = new DxfData();
|
|
|
|
#endif // ENABLE_CGAL
|
2010-01-07 23:49:27 +03:00
|
|
|
} else {
|
|
|
|
dxf = new DxfData(fn, fs, fa, filename, layername, origin_x, origin_y, scale);
|
|
|
|
}
|
2009-07-17 04:34:15 +04:00
|
|
|
|
|
|
|
PolySet *ps = new PolySet();
|
|
|
|
ps->convexity = convexity;
|
|
|
|
|
2010-01-07 23:49:27 +03:00
|
|
|
for (int i = 0; i < dxf->paths.count(); i++)
|
2009-07-17 04:34:15 +04:00
|
|
|
{
|
|
|
|
double max_x = 0;
|
2010-01-07 23:49:27 +03:00
|
|
|
for (int j = 0; j < dxf->paths[i].points.count(); j++) {
|
|
|
|
max_x = fmax(max_x, dxf->paths[i].points[j]->x);
|
2009-07-17 04:34:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int fragments = get_fragments_from_r(max_x, fn, fs, fa);
|
|
|
|
|
2010-01-07 23:49:27 +03:00
|
|
|
double points[fragments][dxf->paths[i].points.count()][3];
|
2009-07-17 04:34:15 +04:00
|
|
|
|
|
|
|
for (int j = 0; j < fragments; j++) {
|
|
|
|
double a = (j*2*M_PI) / fragments;
|
2010-01-07 23:49:27 +03:00
|
|
|
for (int k = 0; k < dxf->paths[i].points.count(); k++) {
|
|
|
|
if (dxf->paths[i].points[k]->x == 0) {
|
2009-07-17 04:34:15 +04:00
|
|
|
points[j][k][0] = 0;
|
|
|
|
points[j][k][1] = 0;
|
|
|
|
} else {
|
2010-01-07 23:49:27 +03:00
|
|
|
points[j][k][0] = dxf->paths[i].points[k]->x * sin(a);
|
|
|
|
points[j][k][1] = dxf->paths[i].points[k]->x * cos(a);
|
2009-07-17 04:34:15 +04:00
|
|
|
}
|
2010-01-07 23:49:27 +03:00
|
|
|
points[j][k][2] = dxf->paths[i].points[k]->y;
|
2009-07-17 04:34:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < fragments; j++) {
|
|
|
|
int j1 = j + 1 < fragments ? j + 1 : 0;
|
2010-01-07 23:49:27 +03:00
|
|
|
for (int k = 0; k < dxf->paths[i].points.count(); k++) {
|
|
|
|
int k1 = k + 1 < dxf->paths[i].points.count() ? k + 1 : 0;
|
2009-07-17 04:34:15 +04:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-06 13:48:51 +03:00
|
|
|
PolySet::ps_cache.insert(key, new PolySet::ps_cache_entry(ps->link()));
|
|
|
|
print_messages_pop();
|
2010-01-07 23:49:27 +03:00
|
|
|
delete dxf;
|
2010-01-06 13:48:51 +03:00
|
|
|
|
2009-07-17 04:34:15 +04:00
|
|
|
return ps;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DxfRotateExtrudeNode::dump(QString indent) const
|
|
|
|
{
|
|
|
|
if (dump_cache.isEmpty()) {
|
|
|
|
QString text;
|
2009-07-23 20:00:38 +04:00
|
|
|
struct stat st;
|
|
|
|
memset(&st, 0, sizeof(struct stat));
|
|
|
|
stat(filename.toAscii().data(), &st);
|
2010-01-07 23:49:27 +03:00
|
|
|
text.sprintf("rotate_extrude(file = \"%s\", cache = \"%x.%x\", layer = \"%s\", "
|
2010-01-15 21:36:53 +03:00
|
|
|
"origin = [ %g %g ], scale = %g, convexity = %d, "
|
|
|
|
"$fn = %g, $fa = %g, $fs = %g) {\n",
|
2009-07-23 20:00:38 +04:00
|
|
|
filename.toAscii().data(), (int)st.st_mtime, (int)st.st_size,
|
2009-07-28 16:53:52 +04:00
|
|
|
layername.toAscii().data(), origin_x, origin_y, scale, convexity,
|
2010-02-28 06:21:53 +03:00
|
|
|
fn, fa, fs);
|
2010-01-07 23:49:27 +03:00
|
|
|
foreach (AbstractNode *v, children)
|
|
|
|
text += v->dump(indent + QString("\t"));
|
|
|
|
text += indent + "}\n";
|
2009-07-17 04:34:15 +04:00
|
|
|
((AbstractNode*)this)->dump_cache = indent + QString("n%1: ").arg(idx) + text;
|
|
|
|
}
|
|
|
|
return dump_cache;
|
|
|
|
}
|
|
|
|
|