Search code examples
modelingcad3d-modelling3d-modelopenscad

OpenSCAD difference/intersection not working


I was trying to create a difference if y == 0, however when I put the last 'cube' in it fills in other parts of the shape that should not be filled in & it doesn't even cut out what it should've. However, when I comment out the final cube it works fine (except it doesn't have the last difference obviously). I have tried using openscad.net & the software. Both of them have the same effect. What am I doing wrong?

With cube uncommented

With cube commented

s = 20+8; //Block size in mm

l = 2; //In "blocks"
w = 2; //In "blocks"
h = 40; //In mm

t = 1;

for (x = [0:l-1]) {
    for (y = [0:w-1]) {
        translate([s*x-s*l/2, s*y-s*w/2, -h/2]) {
            if (x==0) {
                translate([-s*(2/28)-t, s*(16/28)+t/2, 0]) {
                    cube([s*(2/28)+t, s*(8/28)-t, h]);
                }
                translate([-s*(4/28), s*(14/28)+t/2, 0]) {
                    cube([s*(2/28)-t, s*(12/28)-t, h]);
                }
            }
            if (x==l-1) {
                translate([s, s*(4/28)+t/2, 0]) {
                    cube([s*(2/28)+t, s*(8/28)-t, h]);
                }
                translate([s+s*(2/28)+t, s*(2/28)+t/2, 0]) {
                    cube([s*(2/28)-t, s*(12/28)-t, h]);
                }
            }
            if (y==0) {
                translate([s*(4/28)+t/2, -s*(2/28)-t, 0]) {
                    cube([s*(8/28)-t, s*(2/28)+t, h]);
                }
                translate([s*(2/28)+t/2, -s*(4/28), 0]) {
                    cube([s*(12/28)-t, s*(2/28)-t, h]);
                }
            }
            difference() {
                cube([s, s, h]);
                intersection() {
                    if (x == 0) {
                        translate([0, s*(4/28), 0]) {
                            cube([s*(2/28), s*(8/28), h]);
                        }
                        translate([s*(2/28), s*(2/28), 0]) {
                            cube([s*(2/28), s*(12/28), h]);
                        }
                    }
                    if (x==l-1) {
                        translate([s-s*(4/28), s*(14/28), 0]) {
                            cube([s*(2/28), s*(12/28), h]);
                        }
                        translate([s-s*(2/28), s*(16/28), 0]) {
                            cube([s*(2/28), s*(8/28), h]);
                        }
                    }
                    if (y==0) {
                        translate([s*(14/28), -s*(4/28), 0]) {
                            cube([s*(12/28), s*(2/28), h]);
                        }
                    }
                }
            }
        }
    }
}

Solution

  • The reason for your results seems to be that when y==0, your intersection results in an empty object, hence nothing is subtracted. If you slim down your design to a smaller example exhibiting this behavior, it would be a lot easier to debug. Hint: You can use the # and % operators to highlight objects for debugging (# includes it in the CSG tree, % removes it from the CSG tree).