OpenSCAD how to access a value within a Matrix

2019-09-03 02:25发布

I'm trying to either access and assign the values assigned to coordinates through the forloop to their single variables as below, or at least be able to access the values separately in the Matrix.

for ( coordinates = [ [  15,  15,  2],
                      [  15, -15,  2],
                      [ -15, -15,  2],
                      [ -15,  15,  2] ]) 
{
    x = coordinates[0];
    y = coordinates[1];
    z = coordinates[2];
    translate([x+4, y, z]){ 
        cube([x,y,z]);
    }
}

1条回答
相关推荐>>
2楼-- · 2019-09-03 03:14

First off, standard variables are set at compile-time in OpenSCAD, not run-time (official documentation stating that), so you can't assign values to them in a loop. You'll have to inline references to coordinates to use the values in it.

The second issue is that you can't make a cube with a negative size, or so I'm guessing from the fact that I get no output from the second through fourth iterations of the loop as provided. You can wrap the values passed into cube in abs() calls to get the absolute value to ensure it's positive.

Here's a working sample of inlining the coordinates variable and using abs() to pass positive values to cube():

for ( coordinates = [ [  15,  15,  2],
                      [  15, -15,  2],
                      [ -15, -15,  2],
                      [ -15,  15,  2] ])
{
    translate([coordinates[0] + 4, coordinates[1], coordinates[2]]) { 
        cube([abs(coordinates[0]), abs(coordinates[1]), abs(coordinates[2])]);
    }
}
查看更多
登录 后发表回答