Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openscad drill into group created by for

Tags:

openscad

I know that when I use for it creates a group of the generated children. I created a module called grid like so:

module grid(x0,y0,dx,dy,nx,ny) {
    for (x=[0:1:nx-1]) {
        for(y=[0:1:ny-1]) {
            i=x*nx+y;
            echo(i);
            translate([x0+x*dx,y0+y*dy,0]) children(i);
        }
    }
}

which when used like this:

grid(-50,-50,25,25,5,5) {
    cube([10,10,10],center=true);
    cube([10,10,10],center=true);
    cube([10,10,10],center=true);    
    cube([10,10,10],center=true);
    //.. continue to create 25 cubes total    
}

arranges the cubes in a nice grid.

however my original hope and intention was to use it like this:

grid(-50,-50,25,25,5,5) {
    for(i=[0:1:24]) {
        cube([10,10,10],center=true);
    } 
}

Which fails because the for operator returns a group and not a set of children.

Why does the for add a group to begin with? (also leading to the need for intersection_for)

And is there a way for my Grid operator module to handle the children of the group?

like image 669
epeleg Avatar asked Oct 26 '25 22:10

epeleg


2 Answers

I personally hope for the grouping/union for elements within a for() to become optional at some time.

If you don't mind compiling OpenSCAD from source, you could try it already today. There is an ongoing issue Lazy union (aka. no implicit union) and a patch here Make for() UNION optional

like image 165
Kjell Avatar answered Oct 28 '25 14:10

Kjell


Just updated my knowledge of OpenSCAD, there is a better solution:

module nice_cube()
{
    translate([0,0,$height/2]) cube([9,9,$height], center = true);
}

module nice_cylinder()
{
    translate([0,0,$height/2]) cylinder(d=10,h=$height, center = true);
}

module nice_text()
{
    linear_extrude(height=$height, center=false) text(str($height), size=5);
}

module nice_grid()
{
    for(i=[0:9], j=[0:9])
    {
        $height=(i+1)*(j+1);
        x=10*i;
        y=10*j;
        translate([x,y,0]) children();
        /* let($height=(i+1)*(j+1)) {children();} */
    }
}

nice_grid() nice_cube();
translate([0,-110,0]) nice_grid() nice_text();
translate([-110,0,0]) nice_grid() nice_cylinder();

The trick here is to control the shape produced by module by special variables (starting with $) those can be used like in example, commented line using let() requires development version of openscad.

like image 22
Shmozart Avatar answered Oct 28 '25 14:10

Shmozart