openscad 范围问题 a=a+1 解决方法

openscad scope issue a=a+1 workaround

令我惊讶的是 OpenSCAD User Manual 状态:

不,你还是做不到 a=a+1;

那么解决方法是什么?

我有多个嵌套的 for 循环,我想在最内层循环中更改位置:

module all(width,length,height) {
  x=0;
  y=0;
  z=0;
  for(with_left = [0:1:1]) {
    for(with_right = [0:1:1]) {
      for(with_opposite = [0:1:1]) {
        for(with_bottom = [0:1:1]) {
          for(with_45 = [0:1:1]) {
            translate([x,y,z]){
              rotate([0, 0, 0]){
                ejcorner(width, length, height, with_left,with_right,with_opposite,with_bottom,with_45);
              }
            }
            x=x+20;
            if (x>200) {
              y=y+20;
            }
            echo(str("x: ",x," y: ",y));
          }
        }
      }
    }
  }
}

目前我得到:

ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"
ECHO: "x: 20 y: 0"

你用数学来做,按照下面的代码片段。

function is_bit_set(x, b) = floor(x / pow(2, b)) % 2;

for (a = [0:1:63]) {
    with_45 = is_bit_set(a, 0);
    with_bottom = is_bit_set(a, 1);
    with_left = is_bit_set(a, 4);
    x = (20 * a) % 220;
    y = 20 * floor(a / 11);
    echo(x = x, y = y, l = with_left, b = with_bottom, w45 = with_45);
}

一个递归的解决方案似乎接近openscad的功能思想。 我还重命名了布尔变量。

module showoff(x,y,z, size, offset, wall_thickness, with_x90, with_x270, with_y90, with_y270, with_z90,with_z270, with_45) {
 translate([x, y, z]){
   ejcorner(size, offset, wall_thickness, with_x90, with_x270, with_y90, with_y270, with_z90,with_z270, with_45);
 }
 if (with_x90) {
   showoff(x+(size+offset)*2.5,y,z,size, offset, wall_thickness, false, with_x270, with_y90, with_y270, with_z90, with_z270,with_45);
 }
 if (with_x270) {
   showoff(x,y+(size+offset)*2.5,z,size, offset, wall_thickness, with_x90, false, with_y90, with_y270, with_z90, with_z270,with_45);
 }
 if (with_y90) {
   showoff(x+(size+offset)*5,y,z,size, offset, wall_thickness, with_x90, with_x270, false, with_y270, with_z90, with_z270,with_45);
 }
 if (with_y270) {
   showoff(x,y+(size+offset)*5,z,size, offset, wall_thickness, with_x90, with_x270, with_y90, false, with_z90, with_z270,with_45);
 }
 if (with_z90) {
   showoff(x,y+(size+offset)*10,z,size, offset, wall_thickness, with_x90, with_x270, with_y90, with_y270, false, with_z270, with_45);
 }
 if (with_z270) {
   showoff(x,y+(size+offset)*10,z,size, offset, wall_thickness, with_x90, with_x270, with_y90, with_y270, with_z90, false, with_45);
 }
 if (with_45) {
   showoff(x,y+(size+offset)*20,z,size, offset, wall_thickness, with_x90, with_x270, with_y90, with_y270, with_z90, with_z270, false);
 }
}

showoff(-50,50,0,14,15,4,true,true,true,true,true,true,true) ;