Hello,
using parent_module(n)
it is possible to know the name of the
parent. Is there a way to access the variables (the stack or bindings)
of that parent?
I find myself extracting common sequences of code to separate modules,
but then I also need to pass tons of parameters when I invoke these new
modules. It would be better if a module could access the bindings of the
parent modules directly. This is quite common in declarative languages
either in the form of bindings or closures.
For example, I want to put boxes on top of objects. (This is an
extremely reduced test case, in the real code the duplicated code is
much bigger and the passed parameters many more.)
The non-modular code is:
module object1(width, length, height) {
[...]
translate([width/2,length/2,height])
color("green")
cube(min(width,length)/10, center=true);
}
module object2(width, length, height) {
[...]
translate([width/2,length/2,height])
color("red")
cube(min(width,length)/10, center=true);
}
What I would like to write is:
module cap_on_top(clr) {
parent_width = parent_module_var(1, "width");
parent_length = parent_module_var(1, "length");
parent_height = parent_module_var(1, "height");
translate([parent_width/2,parent_length/2,parent_height])
color("green")
cube(min(parent_width,parent_length)/10, center=true);
}
module object1(width, length, height) {
[...]
cap_on_top("green");
}
module object2(width, length, height) {
[...]
cap_on_top("red");
}
nice, isn't it? Instead of that, what I have at the moment is
module cap_on_top(clr, parent_width,
parent_length, parent_height) {
translate([parent_width/2,parent_length/2,parent_height])
color("green")
cube(min(parent_width,parent_length)/10, center=true);
}
module object1(width, length, height) {
[...]
cap_on_top("green", width, length, height);
}
module object2(width, length, height) {
[...]
cap_on_top("red", width, length, height);
}
Would it be possible to extend the OpenSCAD language to support
accessing variable from parent modules? Or are there already tricks to
obtain the same result?
--
Gioele Barabucci gioele@svario.it
See special variables
https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Other_Language_Features#Special_variables
module cap_on_top1() {
parent_width = w;
parent_length = l;
parent_height = h;
test();
translate([parent_width/2,parent_length/2,parent_height])
color(c)
cube(min(parent_width,parent_length)/10, center=true);
}
module cap_on_top2() {
parent_width = $w;
parent_length = $l;
parent_height = $h;
test();
translate([parent_width/2,parent_length/2,parent_height])
color($c)
cube(min(parent_width,parent_length)/10, center=true);
}
module ob1(w,l,h) {
c="green";
children();
}
module ob2($w,$l,$h) {
$c="red";
children();
}
module test() {
echo(parent_module(2),parent_module(1)
,w=w,l=l,h=h,c=c
,$w=$w,$l=$l,$h=$h,$c=$c);
}
echo("1");
ob1(5,10,15) cap_on_top1();
echo("2");
ob2(15,10,5) cap_on_top2();
Admin - PM me if you need anything, or if I've done something stupid...
Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.
View this message in context: http://forum.openscad.org/Accessing-variables-in-parent-module-tp18317p18322.html
Sent from the OpenSCAD mailing list archive at Nabble.com.