2009-06-20 23:00:19 +04:00
|
|
|
/*
|
2011-01-21 04:21:09 +03:00
|
|
|
* OpenSCAD (www.openscad.org)
|
|
|
|
* Copyright (C) 2009-2011 Clifford Wolf <clifford@clifford.at> and
|
|
|
|
* Marius Kintel <marius@kintel.net>
|
2009-06-20 23:00:19 +04:00
|
|
|
*
|
|
|
|
* 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-06-20 23:00:19 +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"
|
|
|
|
#include "expression.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "builtin.h"
|
2009-12-09 17:22:50 +03:00
|
|
|
#include "printutils.h"
|
2009-06-20 23:00:19 +04:00
|
|
|
|
|
|
|
AbstractModule::~AbstractModule()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
AbstractNode *AbstractModule::evaluate(const Context*, const ModuleInstantiation *inst) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *node = new AbstractNode(inst);
|
2009-06-30 00:30:43 +04:00
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
foreach (ModuleInstantiation *v, inst->children) {
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *n = v->evaluate(inst->ctx);
|
2009-06-30 00:30:43 +04:00
|
|
|
if (n)
|
2011-08-04 06:58:17 +04:00
|
|
|
node->children.push_back(n);
|
2009-06-30 00:30:43 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
return node;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
QString AbstractModule::dump(QString indent, QString name) const
|
|
|
|
{
|
|
|
|
return QString("%1abstract module %2();\n").arg(indent, name);
|
|
|
|
}
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
ModuleInstantiation::~ModuleInstantiation()
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
|
|
|
foreach (Expression *v, argexpr)
|
|
|
|
delete v;
|
2010-01-06 22:38:45 +03:00
|
|
|
foreach (ModuleInstantiation *v, children)
|
2009-06-21 12:59:53 +04:00
|
|
|
delete v;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2010-02-12 16:20:15 +03:00
|
|
|
IfElseModuleInstantiation::~IfElseModuleInstantiation()
|
|
|
|
{
|
|
|
|
foreach (ModuleInstantiation *v, else_children)
|
|
|
|
delete v;
|
|
|
|
}
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
QString ModuleInstantiation::dump(QString indent) const
|
2009-06-21 10:53:46 +04:00
|
|
|
{
|
|
|
|
QString text = indent;
|
|
|
|
if (!label.isEmpty())
|
|
|
|
text += label + QString(": ");
|
|
|
|
text += modname + QString("(");
|
|
|
|
for (int i=0; i < argnames.size(); i++) {
|
|
|
|
if (i > 0)
|
|
|
|
text += QString(", ");
|
|
|
|
if (!argnames[i].isEmpty())
|
|
|
|
text += argnames[i] + QString(" = ");
|
2010-11-08 00:29:34 +03:00
|
|
|
text += QString::fromStdString(argexpr[i]->toString());
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
2009-06-21 12:59:53 +04:00
|
|
|
if (children.size() == 0) {
|
|
|
|
text += QString(");\n");
|
|
|
|
} else if (children.size() == 1) {
|
|
|
|
text += QString(")\n");
|
|
|
|
text += children[0]->dump(indent + QString("\t"));
|
|
|
|
} else {
|
|
|
|
text += QString(") {\n");
|
|
|
|
for (int i = 0; i < children.size(); i++) {
|
|
|
|
text += children[i]->dump(indent + QString("\t"));
|
|
|
|
}
|
|
|
|
text += QString("%1}\n").arg(indent);
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
AbstractNode *ModuleInstantiation::evaluate(const Context *ctx) const
|
2009-06-21 12:59:53 +04:00
|
|
|
{
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *node = NULL;
|
|
|
|
if (this->ctx) {
|
|
|
|
PRINTA("WARNING: Ignoring recursive module instanciation of '%1'.", modname);
|
|
|
|
} else {
|
2010-01-06 22:38:45 +03:00
|
|
|
ModuleInstantiation *that = (ModuleInstantiation*)this;
|
2009-07-01 13:48:25 +04:00
|
|
|
that->argvalues.clear();
|
|
|
|
foreach (Expression *v, that->argexpr) {
|
|
|
|
that->argvalues.append(v->evaluate(ctx));
|
|
|
|
}
|
|
|
|
that->ctx = ctx;
|
|
|
|
node = ctx->evaluate_module(this);
|
|
|
|
that->ctx = NULL;
|
|
|
|
that->argvalues.clear();
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
2009-07-01 13:48:25 +04:00
|
|
|
return node;
|
2009-06-21 12:59:53 +04:00
|
|
|
}
|
|
|
|
|
2009-06-20 23:00:19 +04:00
|
|
|
Module::~Module()
|
|
|
|
{
|
2009-06-21 15:41:11 +04:00
|
|
|
foreach (Expression *v, assignments_expr)
|
2009-06-20 23:00:19 +04:00
|
|
|
delete v;
|
|
|
|
foreach (AbstractFunction *v, functions)
|
|
|
|
delete v;
|
|
|
|
foreach (AbstractModule *v, modules)
|
|
|
|
delete v;
|
2010-01-06 22:38:45 +03:00
|
|
|
foreach (ModuleInstantiation *v, children)
|
2009-06-21 12:59:53 +04:00
|
|
|
delete v;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2010-01-06 22:38:45 +03:00
|
|
|
AbstractNode *Module::evaluate(const Context *ctx, const ModuleInstantiation *inst) const
|
2009-06-20 23:00:19 +04:00
|
|
|
{
|
|
|
|
Context c(ctx);
|
2009-07-01 13:48:25 +04:00
|
|
|
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
|
2009-06-20 23:00:19 +04:00
|
|
|
|
2010-01-13 15:24:24 +03:00
|
|
|
c.inst_p = inst;
|
|
|
|
c.set_variable("$children", Value(double(inst->children.size())));
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
c.functions_p = &functions;
|
|
|
|
c.modules_p = &modules;
|
|
|
|
|
2010-02-28 16:48:04 +03:00
|
|
|
if (!usedlibs.empty())
|
|
|
|
c.usedlibs_p = &usedlibs;
|
|
|
|
else
|
|
|
|
c.usedlibs_p = NULL;
|
|
|
|
|
2009-06-21 15:41:11 +04:00
|
|
|
for (int i = 0; i < assignments_var.size(); i++) {
|
2009-06-26 08:32:52 +04:00
|
|
|
c.set_variable(assignments_var[i], assignments_expr[i]->evaluate(&c));
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
|
|
|
|
2009-07-01 13:48:25 +04:00
|
|
|
AbstractNode *node = new AbstractNode(inst);
|
2009-06-21 15:41:11 +04:00
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2009-06-27 11:31:40 +04:00
|
|
|
AbstractNode *n = children[i]->evaluate(&c);
|
|
|
|
if (n != NULL)
|
2011-08-04 06:58:17 +04:00
|
|
|
node->children.push_back(n);
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
2009-06-21 10:53:46 +04:00
|
|
|
QString Module::dump(QString indent, QString name) const
|
|
|
|
{
|
2009-06-23 23:56:46 +04:00
|
|
|
QString text, tab;
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
text = QString("%1module %2(").arg(indent, name);
|
|
|
|
for (int i=0; i < argnames.size(); i++) {
|
|
|
|
if (i > 0)
|
|
|
|
text += QString(", ");
|
|
|
|
text += argnames[i];
|
|
|
|
if (argexpr[i])
|
2010-11-08 00:29:34 +03:00
|
|
|
text += QString(" = ") + QString::fromStdString(argexpr[i]->toString());
|
2009-06-23 23:56:46 +04:00
|
|
|
}
|
|
|
|
text += QString(") {\n");
|
|
|
|
tab = "\t";
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
{
|
|
|
|
QHashIterator<QString, AbstractFunction*> i(functions);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
2009-06-23 23:56:46 +04:00
|
|
|
text += i.value()->dump(indent + tab, i.key());
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
QHashIterator<QString, AbstractModule*> i(modules);
|
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
2009-06-23 23:56:46 +04:00
|
|
|
text += i.value()->dump(indent + tab, i.key());
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
}
|
2009-06-21 15:41:11 +04:00
|
|
|
for (int i = 0; i < assignments_var.size(); i++) {
|
2010-11-08 00:29:34 +03:00
|
|
|
text += QString("%1%2 = %3;\n").arg(indent + tab, assignments_var[i], QString::fromStdString(assignments_expr[i]->toString()));
|
2009-06-21 15:41:11 +04:00
|
|
|
}
|
2009-06-21 10:53:46 +04:00
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2009-06-23 23:56:46 +04:00
|
|
|
text += children[i]->dump(indent + tab);
|
|
|
|
}
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
text += QString("%1}\n").arg(indent);
|
2009-06-21 10:53:46 +04:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2009-06-20 23:00:19 +04:00
|
|
|
QHash<QString, AbstractModule*> builtin_modules;
|
|
|
|
|
|
|
|
void initialize_builtin_modules()
|
|
|
|
{
|
2009-06-21 15:41:11 +04:00
|
|
|
builtin_modules["group"] = new AbstractModule();
|
|
|
|
|
2009-06-30 00:30:43 +04:00
|
|
|
register_builtin_csgops();
|
|
|
|
register_builtin_transform();
|
|
|
|
register_builtin_primitives();
|
2009-07-25 06:37:21 +04:00
|
|
|
register_builtin_surface();
|
2009-06-30 00:30:43 +04:00
|
|
|
register_builtin_control();
|
2009-07-14 09:25:55 +04:00
|
|
|
register_builtin_render();
|
2009-10-14 16:34:50 +04:00
|
|
|
register_builtin_import();
|
2010-01-30 10:41:55 +03:00
|
|
|
register_builtin_projection();
|
2010-02-20 17:37:20 +03:00
|
|
|
register_builtin_cgaladv();
|
2009-07-17 03:11:08 +04:00
|
|
|
register_builtin_dxf_linear_extrude();
|
2009-07-17 04:34:15 +04:00
|
|
|
register_builtin_dxf_rotate_extrude();
|
2009-06-20 23:00:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_builtin_modules()
|
|
|
|
{
|
|
|
|
foreach (AbstractModule *v, builtin_modules)
|
|
|
|
delete v;
|
|
|
|
builtin_modules.clear();
|
|
|
|
}
|