Clifford Wolf:

Added child() statement



git-svn-id: http://svn.clifford.at/openscad/trunk@275 b57f626f-c46c-0410-a088-ec61d464b74c
stl_dim
clifford 2010-01-13 12:24:24 +00:00
parent 367d7b3244
commit 7f944e1e25
6 changed files with 50 additions and 8 deletions

View File

@ -49,12 +49,10 @@ o Advanced Transformations
- Add statement for 2D and 3D minkowski sum
- Add statement for refinement via surface subdivision
- Add statement for intersections in cartesian product of childs
- Add statement for mirroring objects (without inverting them)
o Function-Module-Interface
- Pass a module instanciation to a function (e.g. for a volume() function)
- Pass a function to a module instanciation (e.g. for dynamic extrusion paths)
o Language Frontend
- Allow using the childs of a module instance inside the module
- Allow local variables and functions everywhere (not only on module level)
o DXF Import
- Support for POLYLINE entity

View File

@ -26,6 +26,7 @@ Context::Context(const Context *parent)
this->parent = parent;
functions_p = NULL;
modules_p = NULL;
inst_p = NULL;
ctx_stack.append(this);
}

View File

@ -24,6 +24,7 @@
#include "printutils.h"
enum control_type_e {
CHILD,
ECHO,
ASSIGN,
FOR,
@ -81,6 +82,26 @@ void for_eval(AbstractNode *node, int l, const QVector<QString> &call_argnames,
AbstractNode *ControlModule::evaluate(const Context*, const ModuleInstantiation *inst) const
{
if (type == CHILD)
{
int n = 0;
if (inst->argvalues.size() > 0) {
double v;
if (inst->argvalues[0].getnum(v))
n = v;
}
for (int i = Context::ctx_stack.size()-1; i >= 0; i--) {
const Context *c = Context::ctx_stack[i];
if (c->inst_p) {
if (n < c->inst_p->children.size())
return c->inst_p->children[n]->evaluate(c->inst_p->ctx);
return NULL;
}
c = c->parent;
}
return NULL;
}
AbstractNode *node;
if (type == INT_FOR)
@ -135,6 +156,7 @@ AbstractNode *ControlModule::evaluate(const Context*, const ModuleInstantiation
void register_builtin_control()
{
builtin_modules["child"] = new ControlModule(CHILD);
builtin_modules["echo"] = new ControlModule(ECHO);
builtin_modules["assign"] = new ControlModule(ASSIGN);
builtin_modules["for"] = new ControlModule(FOR);

23
examples/example018.scad Normal file
View File

@ -0,0 +1,23 @@
module step(len, mod)
{
for (i = [0:$children-1])
translate([ len*(i - ($children-1)/2), 0, 0 ]) child((i+mod) % $children);
}
for (i = [1:4])
{
translate([0, -250+i*100, 0]) step(100, i)
{
sphere(30);
cube(60, true);
cylinder(r = 30, h = 50, center = true);
union() {
cube(45, true);
rotate([45, 0, 0]) cube(50, true);
rotate([0, 45, 0]) cube(50, true);
rotate([0, 0, 45]) cube(50, true);
}
}
}

View File

@ -117,6 +117,9 @@ AbstractNode *Module::evaluate(const Context *ctx, const ModuleInstantiation *in
Context c(ctx);
c.args(argnames, argexpr, inst->argnames, inst->argvalues);
c.inst_p = inst;
c.set_variable("$children", Value(double(inst->children.size())));
c.functions_p = &functions;
c.modules_p = &modules;
@ -131,12 +134,6 @@ AbstractNode *Module::evaluate(const Context *ctx, const ModuleInstantiation *in
node->children.append(n);
}
foreach(ModuleInstantiation *v, inst->children) {
AbstractNode *n = v->evaluate(inst->ctx);
if (n != NULL)
node->children.append(n);
}
return node;
}

View File

@ -396,6 +396,7 @@ public:
QHash<QString, Value> config_variables;
const QHash<QString, AbstractFunction*> *functions_p;
const QHash<QString, AbstractModule*> *modules_p;
const ModuleInstantiation *inst_p;
static QVector<const Context*> ctx_stack;