discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

algorithmically building children

J
jon
Wed, Mar 25, 2020 7:11 PM

Torsten:

I explained why I think they are useful; you just said "no". Please
explain yourself

Jon

On 3/25/2020 2:43 PM, Torsten Paul wrote:

On 25.03.20 19:37, jon wrote:

Does the compiler have source code pragmas, so that you can
say "this code uses lazy unions"?

No, and I don't think it should have one.

ciao,
Torsten.


OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

Torsten: I explained why I think they are useful; you just said "no". Please explain yourself Jon On 3/25/2020 2:43 PM, Torsten Paul wrote: > On 25.03.20 19:37, jon wrote: >> Does the compiler have source code pragmas, so that you can >> say "this code uses lazy unions"? > No, and I don't think it should have one. > > ciao, > Torsten. > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org >
TP
Torsten Paul
Wed, Mar 25, 2020 7:17 PM

On 25.03.20 20:11, jon wrote:

I explained why I think they are useful; you just
said "no". Please explain yourself

Please see that issue for the discussion:
https://github.com/openscad/openscad/issues/3142

ciao,
Torsten.

On 25.03.20 20:11, jon wrote: > I explained why I think they are useful; you just > said "no". Please explain yourself Please see that issue for the discussion: https://github.com/openscad/openscad/issues/3142 ciao, Torsten.
A
AKADAP
Wed, Mar 25, 2020 10:04 PM

Thank you for the confirmation.

Since my original path is blocked, and I would prefer not to use a solution
that will be broken in the future, I have found an alternate way forward.
The attached code shows an alternate, working solution to the problem I was
trying to solve:

Drivers=[
[3.81,34.55,7.18,13.9],//Largest Philips Jewelers Screwdriver
[2.98,30.02,6.19,12.81],
[2.38,25,5.52,11.62],
[1.97,20.85,4.5,10.49],
[1.57,18.47,3.85,9.46],
[1.37,17.35,3.27,8.53] //Smallest Philips Jewelers Screwdriver
];

Disperse(n=len(Drivers)-1,Drivers=Drivers,Position=5);

module Disperse(n=3,Index=0,Drivers,Position=0){
translate([Position,0,0])DriverHole(Driver=Drivers[Index]);
if(n>0){

Disperse(n=n-1,Index=Index+1,Drivers=Drivers,Position=Position+10+Drivers[Index][3]);
}
}

module DriverHole(Driver=[1,20,4,6],HeightOfRow=25,HandleHoleDepth=30){
translate([0,0,-1])cylinder(d=Driver[0],h=HeightOfRow+1);
//Transition to make it easier to put the screwdriver in the hole

translate([0,0,HeightOfRow-.1])cylinder(d1=Driver[0],d2=Driver[2],h=(Driver[2]-Driver[0])/2+.2);

translate([0,0,HeightOfRow+(Driver[2]-Driver[0])/2])cylinder(d=Driver[2],h=HandleHoleDepth+1);
}

--
Sent from: http://forum.openscad.org/

Thank you for the confirmation. Since my original path is blocked, and I would prefer not to use a solution that will be broken in the future, I have found an alternate way forward. The attached code shows an alternate, working solution to the problem I was trying to solve: Drivers=[ [3.81,34.55,7.18,13.9],//Largest Philips Jewelers Screwdriver [2.98,30.02,6.19,12.81], [2.38,25,5.52,11.62], [1.97,20.85,4.5,10.49], [1.57,18.47,3.85,9.46], [1.37,17.35,3.27,8.53] //Smallest Philips Jewelers Screwdriver ]; Disperse(n=len(Drivers)-1,Drivers=Drivers,Position=5); module Disperse(n=3,Index=0,Drivers,Position=0){ translate([Position,0,0])DriverHole(Driver=Drivers[Index]); if(n>0){ Disperse(n=n-1,Index=Index+1,Drivers=Drivers,Position=Position+10+Drivers[Index][3]); } } module DriverHole(Driver=[1,20,4,6],HeightOfRow=25,HandleHoleDepth=30){ translate([0,0,-1])cylinder(d=Driver[0],h=HeightOfRow+1); //Transition to make it easier to put the screwdriver in the hole translate([0,0,HeightOfRow-.1])cylinder(d1=Driver[0],d2=Driver[2],h=(Driver[2]-Driver[0])/2+.2); translate([0,0,HeightOfRow+(Driver[2]-Driver[0])/2])cylinder(d=Driver[2],h=HandleHoleDepth+1); } -- Sent from: http://forum.openscad.org/
JB
Jordan Brown
Wed, Mar 25, 2020 10:22 PM

On 3/25/2020 11:39 AM, A. Craig West wrote:

Are you sure about the if (false) case? I'm sure I've used that
successfully to avoid creating a child

Well, check out the output that I included, where the "if" case said it
had two children (one for the if(true) and one for the if(false)).

Consider also:

union() {
    if (true) sphere();
    if (false) cube();
}

where the CSG tree is (slightly reformatted for readability):

union() {
    group() {
        sphere($fn = 0, $fa = 12, $fs = 2, r = 1);
    }
    group();
}

On Wed, 25 Mar 2020, 11:59 Jordan Brown,
<openscad@jordan.maileater.net mailto:openscad@jordan.maileater.net>
wrote:

 On 3/25/2020 12:25 AM, nop head wrote:
 You can't because /*for*/ implicitly unions it results, so only
 ever makes one child.
 More generally, all modules/operators make one child.

 A "for" always makes one child.
 An "if" always makes one child (even if it's false).
 A module always makes one child (even if it doesn't generate any
 geometry).

     module how_many(label, ) {
         echo(label, $children);
     }

     how_many("if") {
         if (true) cube();
         if (false) sphere();
     }

     how_many("for") {
         for (i=[0:10:20]) translate([i,i,i]) cube();
     }

     module empty() {
     }

     how_many("empty") {
         empty();
     }

 yields

     ECHO: "if", 2
     ECHO: "for", 1
     ECHO: "empty", 1

 I don't know the rules for the new lazy union mode that Paul
 describes.

 (Note:  One might think that an empty module should yield no
 children, but then you couldn't have a "placeholder" child in an
 invocation of a module that takes multiple children with different
 purposes, where children[1] and children[2] are not treated the same.)


 _______________________________________________
 OpenSCAD mailing list
 Discuss@lists.openscad.org <mailto:Discuss@lists.openscad.org>
 http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

OpenSCAD mailing list
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org

On 3/25/2020 11:39 AM, A. Craig West wrote: > Are you sure about the if (false) case? I'm sure I've used that > successfully to avoid creating a child Well, check out the output that I included, where the "if" case said it had two children (one for the if(true) and one for the if(false)). Consider also: union() { if (true) sphere(); if (false) cube(); } where the CSG tree is (slightly reformatted for readability): union() { group() { sphere($fn = 0, $fa = 12, $fs = 2, r = 1); } group(); } > On Wed, 25 Mar 2020, 11:59 Jordan Brown, > <openscad@jordan.maileater.net <mailto:openscad@jordan.maileater.net>> > wrote: > > On 3/25/2020 12:25 AM, nop head wrote: >> You can't because /*for*/ implicitly unions it results, so only >> ever makes one child. > > More generally, all modules/operators make one child. > > A "for" always makes one child. > An "if" always makes one child (even if it's false). > A module always makes one child (even if it doesn't generate any > geometry). > > module how_many(label, ) { > echo(label, $children); > } > > how_many("if") { > if (true) cube(); > if (false) sphere(); > } > > how_many("for") { > for (i=[0:10:20]) translate([i,i,i]) cube(); > } > > module empty() { > } > > how_many("empty") { > empty(); > } > > yields > > ECHO: "if", 2 > ECHO: "for", 1 > ECHO: "empty", 1 > > I don't know the rules for the new lazy union mode that Paul > describes. > > (Note:  One might think that an empty module should yield no > children, but then you couldn't have a "placeholder" child in an > invocation of a module that takes multiple children with different > purposes, where children[1] and children[2] are not treated the same.) > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org <mailto:Discuss@lists.openscad.org> > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org > > > _______________________________________________ > OpenSCAD mailing list > Discuss@lists.openscad.org > http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
JB
Jordan Brown
Wed, Mar 25, 2020 10:40 PM

You can do something kind of similar using $ variables to communicate
with the children.

Your top-level module would repeatedly invoke the children, with
different values of a $ variable, and the children would behave
differently depending on the values.

For instance:

module foreach(list) {
    for ($i=[0:len(list)-1]) {
        $val = list[$i];
        children();
    }
}

module mychild() {
    translate([0,$i*20,0]) text($val);
}

foreach(["alpha","beta","gamma"])
    mychild();

Here's one that's a bit more similar to your example:

module distribute(separation, list) {
    for ($i=[0:len(list)-1]) {
        $val = list[$i];
        translate($i * separation)
            children();
    }
}

module mychild() {
    text($val);
}

distribute([0,20,0], ["alpha","beta","gamma", "delta"])
    mychild();
You can do something kind of similar using $ variables to communicate with the children. Your top-level module would repeatedly invoke the children, with different values of a $ variable, and the children would behave differently depending on the values. For instance: module foreach(list) { for ($i=[0:len(list)-1]) { $val = list[$i]; children(); } } module mychild() { translate([0,$i*20,0]) text($val); } foreach(["alpha","beta","gamma"]) mychild(); Here's one that's a bit more similar to your example: module distribute(separation, list) { for ($i=[0:len(list)-1]) { $val = list[$i]; translate($i * separation) children(); } } module mychild() { text($val); } distribute([0,20,0], ["alpha","beta","gamma", "delta"]) mychild();