[OpenSCAD] modifying default-parameters / accessing children of children?

doug moen doug at moens.org
Mon Jun 6 15:36:20 EDT 2016


I don't know a good answer to this, except that this has bitten me as well.
I'm interested to know if there is a better way in the current language.

If this turns into a feature request, then I'll note that this is related
to the "lazy union" proposal. Also, OpenSCAD2 has slightly different
semantics for when groups are formed, in order to fix this problem. In
OpenSCAD2, you could use any of the following to implement module myfoo and
mylineup:

        // no { } around the body means no extra group is constructed
        module myfoo(a=5, b=6, c=3, d=4)
            foo(a,b,c,d);

        // { } creates a group, 'each' unpacks a group
        module myfoo(a=5, b=6, c=3, d=4)
        {
            each foo(a,b,c,d);
        }

        // this works because an extra group is not constructed around
children()
        module mylineup(space=20)
        {
            lineup(space)
            children();
        }

Of course, I've left out the issue of backward compatibility, which would
need to be considered for any such change. Perhaps the 'each' operator from
OpenSCAD2 could be added for explicitly unpacking a group, without breaking
compatibility.

On 6 June 2016 at 12:55, Roland Koebler <rk-list at simple-is-better.org>
wrote:

> Hi,
>
> I'm heavily using modules with default-parameters. Now, I would like to
> locally use different default-parameters, so I don't need to give the
> "new default parameters" at each module-invokation.
>
> One way that looked sensible to me, is using a wrapper-module with
> different default-parameters, but that doesn't work when using
> children(i) in the module.
>
> Questions:
>
> - Is there a way to locally modify default-parameters?
> - Is there a way to access the children of children or to unpack a group?
>
>
> Details:
>
> 1. Example of a working wrapper-module:
>
>    - Original module:
>
>         module foo(a=1, b=2, c=3, d=4)
>         {
>             ...
>         }
>
>     - Wrapper-module, to locally use different default-parameters, e.g.
> a=5, b=6.
>
>         module myfoo(a=5, b=6, c=3, d=4)
>         {
>             foo(a,b,c,d);
>         }
>
>     This works well, as long as "foo" doesn't use children(i).
>
> 2. Example with children:
>
>    - Original module + invocation
>      (slightly modified example from the documentation):
>
>         module lineup(space=10)
>         {
>             for(i=[0:$children-1])
>                 translate([space*i,0,0]) children(i);
>         }
>
>         lineup() {
>             sphere(d=8);
>             cube(7, center=true);
>             cylinder(d=7.5, h=8, center=true);
>         }
>
>     - Wrapper-module + invocation:
>
>         module mylineup(space=20)
>         {
>             lineup(space)
>             children();
>         }
>
>         mylineup() {
>             sphere(d=8);
>             cube(7, center=true);
>             cylinder(d=7.5, h=8, center=true);
>         }
>
>     Now, this fails, since lineup() now only sees 1 child, which
>     consists of sphere+cube+cylinder.
>
>     The following would work, but only for a defined number of children:
>
>         module mylineup(space=20)
>         {
>             lineup(space) {
>                 children(0);
>                 children(1);
>                 children(2);
>             }
>         }
>
>     So, is it possible to (a) unpack children, (b) access the children
>     of children or (c) use some other way/workaround?
>
>
> Best regards
> Roland
>
> _______________________________________________
> OpenSCAD mailing list
> Discuss at lists.openscad.org
> http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscad.org/pipermail/discuss_lists.openscad.org/attachments/20160606/f414c55a/attachment-0002.html>


More information about the Discuss mailing list