From 7f944e1e25faa8d8c077ba78e4fd489b69270fb5 Mon Sep 17 00:00:00 2001 From: clifford Date: Wed, 13 Jan 2010 12:24:24 +0000 Subject: [PATCH] Clifford Wolf: Added child() statement git-svn-id: http://svn.clifford.at/openscad/trunk@275 b57f626f-c46c-0410-a088-ec61d464b74c --- TODO.txt | 2 -- context.cc | 1 + control.cc | 22 ++++++++++++++++++++++ examples/example018.scad | 23 +++++++++++++++++++++++ module.cc | 9 +++------ openscad.h | 1 + 6 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 examples/example018.scad diff --git a/TODO.txt b/TODO.txt index 8f4dbc46..64019cc9 100644 --- a/TODO.txt +++ b/TODO.txt @@ -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 diff --git a/context.cc b/context.cc index 59c263a1..15a2ea6d 100644 --- a/context.cc +++ b/context.cc @@ -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); } diff --git a/control.cc b/control.cc index c898dfc9..19c52557 100644 --- a/control.cc +++ b/control.cc @@ -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 &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); diff --git a/examples/example018.scad b/examples/example018.scad new file mode 100644 index 00000000..15ed745f --- /dev/null +++ b/examples/example018.scad @@ -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); + } + } +} diff --git a/module.cc b/module.cc index e58cdd1d..a319e1ee 100644 --- a/module.cc +++ b/module.cc @@ -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; } diff --git a/openscad.h b/openscad.h index 6a207890..8ad164d5 100644 --- a/openscad.h +++ b/openscad.h @@ -396,6 +396,7 @@ public: QHash config_variables; const QHash *functions_p; const QHash *modules_p; + const ModuleInstantiation *inst_p; static QVector ctx_stack;