2015-05-15 40 views
-1

我试图通过forloop访问并分配给坐标的值赋给它们的单个变量,如下所示,或者至少能够在矩阵中分别访问这些值。OpenSCAD如何访问矩阵内的值

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

首先,标准变量被设置在OpenSCAD编译时,不运行时(official documentation指出),所以你不能在一个循环中给它们赋值。您必须内嵌参考coordinates以使用其中的值。

第二个问题是,你不能制作一个负面尺寸的立方体,或者我从事实上没有从循环的第二到第四个迭代中得到输出的猜测。您可以将传入多维数据集的值封装在abs()调用中以获取绝对值以确保其为正值。

这里的内联coordinates变量并使用abs()传递正值到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])]); 
    } 
}