summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--TODO.txt2
-rw-r--r--context.cc1
-rw-r--r--control.cc22
-rw-r--r--examples/example018.scad23
-rw-r--r--module.cc9
-rw-r--r--openscad.h1
6 files changed, 50 insertions, 8 deletions
diff --git a/TODO.txt b/TODO.txt
index 8f4dbc4..64019cc 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 59c263a..15a2ea6 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 c898dfc..19c5255 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<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);
diff --git a/examples/example018.scad b/examples/example018.scad
new file mode 100644
index 0000000..15ed745
--- /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 e58cdd1..a319e1e 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 6a20789..8ad164d 100644
--- a/openscad.h
+++ b/openscad.h
@@ -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;
contact: Jan Huwald // Impressum