代码之家  ›  专栏  ›  技术社区  ›  epeleg

openscad钻入组由创建

  •  1
  • epeleg  · 技术社区  · 6 年前

    我创建了一个名为grid的模块,如下所示:

    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);
            }
        }
    }
    

    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    
    }
    

    将立方体排列成一个漂亮的网格。

    然而,我最初的希望和意图是这样使用它:

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

    失败的原因是for操作符返回一个组而不是一组子对象。

    交叉口 )

    我的网格操作符模块是否有办法处理组中的子对象?

    3 回复  |  直到 6 年前
        1
  •  2
  •   Kjell    6 年前

    如果您不介意从源代码处编译OpenSCAD,您可以今天就开始尝试。 有一个持续的问题 Lazy union (aka. no implicit union) 还有一块补丁 Make for() UNION optional

        2
  •  1
  •   Shmozart    6 年前

    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();
    

        3
  •  0
  •   Shmozart    6 年前

    我猜你想要这个:

    for(x=[...], y=[...]) {
        translate([x,y,0]) children();
    }
    

    我从您的评论中了解到的是,您希望网格节点中的对象是参数化的,并且参数取决于索引。原问题中没有提到这一要求。在这种情况下,解决方案取决于你的问题背景,我猜。我看到的两种可能性是:

    module grid_of_parametric_modules(other_param)
    {
        for(i=[0:24])
        {
            x=_x(i);
            y=_y(i);
            translate([x,y,0]) parametric_module(i_param(i), other_param);
        }
    }
    

    function grid_pos(i) = [_x(i), _y(i), 0];
    
    ....
    
    for(i=[0:24])
        translate(grid_pos(i)) parametric_module(i);
    
    推荐文章