Search code examples
scopeopenscad

openscad scope issue a=a+1 workaround


To my surprise the OpenSCAD User Manual states:

No, you still can't do a=a+1;

What is the workaround then?

I have multiple nested for loops where I want to change the position in the most inner loop:

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

Currently I get:

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"

Solution

  • A recursive solution seems to be close to the functional idea of openscad. I have also renamed the boolean variables.

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