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

时间:2019-03-11 06:13:17

标签: scope openscad

令我惊讶的是,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"

2 个答案:

答案 0 :(得分:1)

您可以通过数学来实现,就像下面的代码片段一样。

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

答案 1 :(得分:0)

递归解决方案似乎与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) ;