2

我知道当我使用它时,它会创建一组生成的孩子。我创建了一个名为 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 运算符返回一个组而不是一组子级。

为什么for要添加一个组开始?(也导致需要intersection_for

有没有办法让我的网格操作员模块来处理组的孩子?

4

3 回答 3

2

我个人希望 for() 中元素的分组/联合在某个时候成为可选的。

如果您不介意从源代码编译 OpenSCAD,您可以在今天尝试一下。存在一个持续存在的问题Lazy union (aka. no implicit union) 和一个补丁Make for() UNION 可选

于 2019-03-21T18:31:43.463 回答
1

刚刚更新了我对OpenSCAD的认识,有更好的解决方案:

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

这里的技巧是通过特殊变量(以 $ 开头)控制模块产生的形状,这些变量可以像示例中一样使用,使用 let() 的注释行需要开发版本的 openscad。

于 2018-12-24T08:53:56.793 回答
0

我猜你想要这个:

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

请注意,您只需要一个 for 语句即可遍历 x 和 y 值。

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

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);
于 2018-12-14T10:26:22.020 回答