2009-07-17 03:11:08 +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.
|
|
|
|
*
|
|
|
|
* 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"
|
2009-12-09 17:22:50 +03:00
|
|
|
#include "printutils.h"
|
2009-07-17 03:11:08 +04:00
|
|
|
|
2009-07-23 20:00:38 +04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2010-01-07 16:55:48 +03:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QTime>
|
|
|
|
|
2009-07-17 03:11:08 +04:00
|
|
|
class DxfLinearExtrudeModule : public AbstractModule
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DxfLinearExtrudeModule() { }
|
2010-01-06 22:38:45 +03:00
|
|
|
virtual AbstractNode *evaluate(const Context *ctx, const ModuleInstantiation *inst) const;
|
2009-07-17 03:11:08 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class DxfLinearExtrudeNode : public AbstractPolyNode
|
|
|
|
{
|
|
|
|
public:
|
2009-07-24 22:18:56 +04:00
|
|
|
int convexity, slices;
|
|
|
|
double fn, fs, fa, height, twist;
|
2009-07-20 21:45:39 +04:00
|
|
|
double origin_x, origin_y, scale;
|
2009-07-24 22:18:56 +04:00
|
|
|
bool center, has_twist;
|
2009-07-17 03:11:08 +04:00
|
|
|
QString filename, layername;
|
2010-01-06 22:38:45 +03:00
|
|
|
DxfLinearExtrudeNode(const ModuleInstantiation *mi) : AbstractPolyNode(mi) {
|
2009-07-24 22:18:56 +04:00
|
|
|
convexity = slices = 0;
|
|
|
|
fn = fs = fa = height = twist = 0;
|
2009-07-20 21:45:39 +04:00
|
|
|
origin_x = origin_y = scale = 0;
|
2009-07-24 22:18:56 +04:00
|
|
|
center = has_twist = false;
|
2009-07-20 21:45:39 +04:00
|
|
|
}
|
2009-07-17 03:11:08 +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 *DxfLinearExtrudeModule::evaluate(const Context *ctx, const ModuleInstantiation *inst) const
|
2009-07-17 03:11:08 +04:00
|
|
|
{
|
|
|
|
DxfLinearExtrudeNode *node = new DxfLinearExtrudeNode(inst);
|
|
|
|
|
2009-07-28 16:53:52 +04:00
|
|
|
QVector<QString> argnames = QVector<QString>() << "file" << "layer" << "height" << "origin" << "scale" << "center" << "twist" << "slices";
|
2009-07-17 03:11:08 +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 height = c.lookup_variable("height", true);
|
|
|
|
Value convexity = c.lookup_variable("convexity", true);
|
|
|
|
Value origin = c.lookup_variable("origin", true);
|
|
|
|
Value scale = c.lookup_variable("scale", true);
|
|
|
|
Value center = c.lookup_variable("center", true);
|
2009-07-24 22:18:56 +04:00
|
|
|
Value twist = c.lookup_variable("twist", true);
|
|
|
|
Value slices = c.lookup_variable("slices", true);
|
2009-07-17 03:11:08 +04:00
|
|
|
|
|
|
|
node->filename = file.text;
|
|
|
|
node->layername = layer.text;
|
|
|
|
node->height = height.num;
|
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;
|
|
|
|
|
|
|
|
if (center.type == Value::BOOL)
|
|
|
|
node->center = center.b;
|
2009-07-17 03:11:08 +04:00
|
|
|
|
|
|
|
if (node->height <= 0)
|
|
|
|
node->height = 100;
|
|
|
|
|
|
|
|
if (node->convexity <= 0)
|
|
|
|
node->convexity = 1;
|
|
|
|
|
2009-07-20 21:45:39 +04:00
|
|
|
if (node->scale <= 0)
|
|
|
|
node->scale = 1;
|
|
|
|
|
2009-07-24 22:18:56 +04:00
|
|
|
if (twist.type == Value::NUMBER) {
|
|
|
|
node->twist = twist.num;
|
|
|
|
if (slices.type == Value::NUMBER) {
|
2009-08-10 11:18:35 +04:00
|
|
|
node->slices = (int)slices.num;
|
2009-07-24 22:18:56 +04:00
|
|
|
} else {
|
2009-08-10 11:18:35 +04:00
|
|
|
node->slices = (int)fmax(2, fabs(get_fragments_from_r(node->height,
|
2009-07-24 22:18:56 +04:00
|
|
|
node->fn, node->fs, node->fa) * node->twist / 360));
|
|
|
|
}
|
|
|
|
node->has_twist = true;
|
|
|
|
}
|
|
|
|
|
2010-01-06 15:16:56 +03:00
|
|
|
if (node->filename.isEmpty()) {
|
2010-01-06 22:38:45 +03:00
|
|
|
foreach (ModuleInstantiation *v, inst->children) {
|
2010-01-06 15:16:56 +03:00
|
|
|
AbstractNode *n = v->evaluate(inst->ctx);
|
|
|
|
if (n)
|
|
|
|
node->children.append(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-17 03:11:08 +04:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_builtin_dxf_linear_extrude()
|
|
|
|
{
|
|
|
|
builtin_modules["dxf_linear_extrude"] = new DxfLinearExtrudeModule();
|
2010-01-06 15:16:56 +03:00
|
|
|
builtin_modules["linear_extrude"] = new DxfLinearExtrudeModule();
|
2009-07-17 03:11:08 +04:00
|
|
|
}
|
|
|
|
|
2009-07-24 22:18:56 +04:00
|
|
|
static void add_slice(PolySet *ps, DxfData::Path *pt, double rot1, double rot2, double h1, double h2)
|
2009-07-17 03:11:08 +04:00
|
|
|
{
|
|
|
|
for (int j = 1; j < pt->points.count(); j++)
|
|
|
|
{
|
|
|
|
int k = j - 1;
|
2009-07-24 22:18:56 +04:00
|
|
|
|
|
|
|
double jx1 = pt->points[j]->x * cos(rot1*M_PI/180) + pt->points[j]->y * sin(rot1*M_PI/180);
|
|
|
|
double jy1 = pt->points[j]->x * -sin(rot1*M_PI/180) + pt->points[j]->y * cos(rot1*M_PI/180);
|
|
|
|
|
|
|
|
double jx2 = pt->points[j]->x * cos(rot2*M_PI/180) + pt->points[j]->y * sin(rot2*M_PI/180);
|
|
|
|
double jy2 = pt->points[j]->x * -sin(rot2*M_PI/180) + pt->points[j]->y * cos(rot2*M_PI/180);
|
|
|
|
|
|
|
|
double kx1 = pt->points[k]->x * cos(rot1*M_PI/180) + pt->points[k]->y * sin(rot1*M_PI/180);
|
|
|
|
double ky1 = pt->points[k]->x * -sin(rot1*M_PI/180) + pt->points[k]->y * cos(rot1*M_PI/180);
|
|
|
|
|
|
|
|
double kx2 = pt->points[k]->x * cos(rot2*M_PI/180) + pt->points[k]->y * sin(rot2*M_PI/180);
|
|
|
|
double ky2 = pt->points[k]->x * -sin(rot2*M_PI/180) + pt->points[k]->y * cos(rot2*M_PI/180);
|
|
|
|
|
2009-07-17 03:11:08 +04:00
|
|
|
ps->append_poly();
|
|
|
|
if (pt->is_inner) {
|
2009-07-24 22:18:56 +04:00
|
|
|
ps->append_vertex(kx1, ky1, h1);
|
|
|
|
ps->append_vertex(jx1, jy1, h1);
|
|
|
|
ps->append_vertex(jx2, jy2, h2);
|
2009-07-17 03:11:08 +04:00
|
|
|
} else {
|
2009-07-24 22:18:56 +04:00
|
|
|
ps->insert_vertex(kx1, ky1, h1);
|
|
|
|
ps->insert_vertex(jx1, jy1, h1);
|
|
|
|
ps->insert_vertex(jx2, jy2, h2);
|
2009-07-17 03:11:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
ps->append_poly();
|
|
|
|
if (pt->is_inner) {
|
2009-07-24 22:18:56 +04:00
|
|
|
ps->append_vertex(kx2, ky2, h2);
|
|
|
|
ps->append_vertex(kx1, ky1, h1);
|
|
|
|
ps->append_vertex(jx2, jy2, h2);
|
2009-07-17 03:11:08 +04:00
|
|
|
} else {
|
2009-07-24 22:18:56 +04:00
|
|
|
ps->insert_vertex(kx2, ky2, h2);
|
|
|
|
ps->insert_vertex(kx1, ky1, h1);
|
|
|
|
ps->insert_vertex(jx2, jy2, h2);
|
2009-07-17 03:11:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-07 16:55:48 +03:00
|
|
|
static void report_func(const class AbstractNode*, void *vp, int mark)
|
|
|
|
{
|
|
|
|
QProgressDialog *pd = (QProgressDialog*)vp;
|
|
|
|
int v = (int)((mark*100.0) / progress_report_count);
|
|
|
|
pd->setValue(v < 100 ? v : 99);
|
|
|
|
QString label;
|
|
|
|
label.sprintf("Rendering Polygon Mesh using CGAL (%d/%d)", mark, progress_report_count);
|
|
|
|
pd->setLabelText(label);
|
|
|
|
QApplication::processEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
PolySet *DxfLinearExtrudeNode::render_polyset(render_mode_e rm) const
|
2009-07-17 03:11:08 +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-06 15:16:56 +03:00
|
|
|
DxfData *dxf;
|
|
|
|
|
|
|
|
if (filename.isEmpty())
|
|
|
|
{
|
2010-01-07 16:55:48 +03:00
|
|
|
QTime t;
|
2010-01-09 00:29:24 +03:00
|
|
|
QProgressDialog *pd = NULL;
|
2010-01-07 16:55:48 +03:00
|
|
|
|
|
|
|
if (rm == RENDER_OPENCSG)
|
|
|
|
{
|
|
|
|
PRINT_NOCACHE("Processing uncached linear_extrude outline...");
|
|
|
|
QApplication::processEvents();
|
|
|
|
|
|
|
|
t.start();
|
|
|
|
pd = new QProgressDialog("Rendering Polygon Mesh using CGAL...", QString(), 0, 100);
|
|
|
|
pd->setValue(0);
|
|
|
|
pd->setAutoClose(false);
|
|
|
|
pd->show();
|
|
|
|
QApplication::processEvents();
|
|
|
|
|
|
|
|
progress_report_prep((AbstractNode*)this, report_func, pd);
|
|
|
|
}
|
|
|
|
|
2010-01-06 15:16:56 +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-01-07 16:55:48 +03:00
|
|
|
|
|
|
|
if (rm == RENDER_OPENCSG) {
|
|
|
|
progress_report_fin();
|
|
|
|
int s = t.elapsed() / 1000;
|
|
|
|
PRINTF_NOCACHE("..rendering time: %d hours, %d minutes, %d seconds", s / (60*60), (s / 60) % 60, s % 60);
|
|
|
|
delete pd;
|
|
|
|
}
|
2010-01-06 15:16:56 +03:00
|
|
|
} else {
|
|
|
|
dxf = new DxfData(fn, fs, fa, filename, layername, origin_x, origin_y, scale);
|
|
|
|
}
|
2009-07-17 03:11:08 +04:00
|
|
|
|
|
|
|
PolySet *ps = new PolySet();
|
|
|
|
ps->convexity = convexity;
|
|
|
|
|
2009-07-20 21:45:39 +04:00
|
|
|
double h1, h2;
|
|
|
|
|
|
|
|
if (center) {
|
|
|
|
h1 = -height/2.0;
|
2009-07-22 13:46:13 +04:00
|
|
|
h2 = +height/2.0;
|
2009-07-20 21:45:39 +04:00
|
|
|
} else {
|
|
|
|
h1 = 0;
|
|
|
|
h2 = height;
|
|
|
|
}
|
|
|
|
|
2009-07-22 12:34:19 +04:00
|
|
|
bool first_open_path = true;
|
2010-01-06 15:16:56 +03:00
|
|
|
for (int i = 0; i < dxf->paths.count(); i++)
|
2009-07-22 12:34:19 +04:00
|
|
|
{
|
2010-01-06 15:16:56 +03:00
|
|
|
if (dxf->paths[i].is_closed)
|
2009-07-22 12:34:19 +04:00
|
|
|
continue;
|
|
|
|
if (first_open_path) {
|
|
|
|
PRINTF("WARING: Open paths in dxf_liniear_extrude(file = \"%s\", layer = \"%s\"):",
|
|
|
|
filename.toAscii().data(), layername.toAscii().data());
|
|
|
|
first_open_path = false;
|
|
|
|
}
|
|
|
|
PRINTF(" %9.5f %10.5f ... %10.5f %10.5f",
|
2010-01-06 15:16:56 +03:00
|
|
|
dxf->paths[i].points.first()->x / scale + origin_x,
|
|
|
|
dxf->paths[i].points.first()->y / scale + origin_y,
|
|
|
|
dxf->paths[i].points.last()->x / scale + origin_x,
|
|
|
|
dxf->paths[i].points.last()->y / scale + origin_y);
|
2009-07-22 12:34:19 +04:00
|
|
|
}
|
|
|
|
|
2009-07-23 00:53:37 +04:00
|
|
|
|
2009-07-24 22:18:56 +04:00
|
|
|
if (has_twist)
|
2009-07-17 03:11:08 +04:00
|
|
|
{
|
2010-01-06 15:16:56 +03:00
|
|
|
dxf_tesselate(ps, dxf, 0, false, true, h1);
|
|
|
|
dxf_tesselate(ps, dxf, twist, true, true, h2);
|
2009-07-24 22:18:56 +04:00
|
|
|
for (int j = 0; j < slices; j++)
|
|
|
|
{
|
|
|
|
double t1 = twist*j / slices;
|
|
|
|
double t2 = twist*(j+1) / slices;
|
|
|
|
double g1 = h1 + (h2-h1)*j / slices;
|
|
|
|
double g2 = h1 + (h2-h1)*(j+1) / slices;
|
2010-01-06 15:16:56 +03:00
|
|
|
for (int i = 0; i < dxf->paths.count(); i++)
|
2009-07-24 22:18:56 +04:00
|
|
|
{
|
2010-01-06 15:16:56 +03:00
|
|
|
if (!dxf->paths[i].is_closed)
|
2009-07-24 22:18:56 +04:00
|
|
|
continue;
|
2010-01-06 15:16:56 +03:00
|
|
|
add_slice(ps, &dxf->paths[i], t1, t2, g1, g2);
|
2009-07-24 22:18:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-06 15:16:56 +03:00
|
|
|
dxf_tesselate(ps, dxf, 0, false, true, h1);
|
|
|
|
dxf_tesselate(ps, dxf, 0, true, true, h2);
|
|
|
|
for (int i = 0; i < dxf->paths.count(); i++)
|
2009-07-24 22:18:56 +04:00
|
|
|
{
|
2010-01-06 15:16:56 +03:00
|
|
|
if (!dxf->paths[i].is_closed)
|
2009-07-24 22:18:56 +04:00
|
|
|
continue;
|
2010-01-06 15:16:56 +03:00
|
|
|
add_slice(ps, &dxf->paths[i], 0, 0, h1, h2);
|
2009-07-24 22:18:56 +04:00
|
|
|
}
|
2009-07-17 03:11:08 +04:00
|
|
|
}
|
|
|
|
|
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-06 15:16:56 +03:00
|
|
|
delete dxf;
|
2010-01-06 13:48:51 +03:00
|
|
|
|
2009-07-17 03:11:08 +04:00
|
|
|
return ps;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DxfLinearExtrudeNode::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-06 15:16:56 +03:00
|
|
|
text.sprintf("linear_extrude(file = \"%s\", cache = \"%x.%x\", layer = \"%s\", "
|
2009-10-28 15:29:58 +03:00
|
|
|
"height = %f, origin = [ %f %f ], scale = %f, center = %s, convexity = %d",
|
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(), height, origin_x, origin_y, scale,
|
2009-10-28 15:29:58 +03:00
|
|
|
center ? "true" : "false", convexity);
|
2009-07-28 16:53:52 +04:00
|
|
|
if (has_twist) {
|
|
|
|
QString t2;
|
|
|
|
t2.sprintf(", twist = %f, slices = %d", twist, slices);
|
|
|
|
text += t2;
|
|
|
|
}
|
|
|
|
QString t3;
|
2010-01-06 15:16:56 +03:00
|
|
|
t3.sprintf(", $fn = %f, $fa = %f, $fs = %f) {\n", fn, fs, fa);
|
2009-07-28 16:53:52 +04:00
|
|
|
text += t3;
|
2010-01-06 15:16:56 +03:00
|
|
|
foreach (AbstractNode *v, children)
|
|
|
|
text += v->dump(indent + QString("\t"));
|
|
|
|
text += indent + "}\n";
|
2009-07-17 03:11:08 +04:00
|
|
|
((AbstractNode*)this)->dump_cache = indent + QString("n%1: ").arg(idx) + text;
|
|
|
|
}
|
|
|
|
return dump_cache;
|
|
|
|
}
|
|
|
|
|