OpenSCAD how to access a value within a Matrix

六眼飞鱼酱① 提交于 2019-12-12 02:55:50

问题


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:


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


来源:https://stackoverflow.com/questions/30265260/openscad-how-to-access-a-value-within-a-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!