openscad

SCAD shifting a slot to the center of frame


frame

In the image above, you can see a rectangular frame and a small slot off to the side on one side. I want to get that slot in the center of that side, but my code is not doing that.

This is the code I have for the slot:

// Create the top cutout for the headband in the middle of the shorter side
        translate([outer_length / 2 - slot_width / 2, -outer_width / 2, -border_height]) {
            cube([slot_width, border_height + 1, slot_depth]);
        }
    }

In my mind, if I /2 that should do it, but not sure what is going on. Any ideas?

I tried switching the outer_length with Outer_width, and adding a 0 in front, but hasn't worked.

Here is the code for the whole frame, including code for the slot:

// Parameters for the rectangular frame
outer_length = 85;       // Outer length of the frame (in mm)
outer_width = 65;        // Outer width of the frame (in mm)
border_thickness = 2;    // Thickness of the raised border (in mm)
border_height = 10;      // Height of the raised border (in mm)
inner_length = outer_length - 2 * border_thickness;  // Inner length of the frame (in mm)
inner_width = outer_width - 2 * border_thickness;    // Inner width of the frame (in mm)
bottom_thickness = 1;    // Thickness of the thin opaque bottom layer (in mm)
corner_radius = 5;       // Radius for rounded corners (in mm)
slot_width = 10;         // Width for headband slots (in mm)
slot_depth = 5;          // Depth for headband slots (in mm)

// Function to create a rectangular frame with rounded corners, a hollow interior, and a thin opaque bottom layer
module rec_frame() {
    difference() {
        // Create the outer shell with rounded corners and raised border
        translate([0, 0, -border_height])
        linear_extrude(height = border_height + bottom_thickness) {
            offset(r = corner_radius) {
                square([outer_length, outer_width], center = true);
            }
        }

        // Create the inner hollow part with rounded corners
        translate([0, 0, -border_height])
        linear_extrude(height = border_height + bottom_thickness) {
            offset(r = corner_radius - 1) {
                square([inner_length, inner_width], center = true);
            }
        }

        // Create the thin opaque bottom layer
        translate([0, 0, -bottom_thickness])
        square([outer_length, outer_width], center = true);
        
        // Create the top cutout for the headband in the middle of the shorter side
        translate([outer_length / 2 - slot_width / 2, -outer_width / 2, -border_height]) {
            cube([slot_width, border_height + 1, slot_depth]);
        }
    }

    // Create the thin opaque bottom layer
    translate([0, 0, -bottom_thickness - border_height])
    linear_extrude(height = bottom_thickness) {
        offset(r = corner_radius) {
            square([outer_length, outer_width], center = true);
        }
    }
}

// Create the frame using the module
rec_frame();

For the second slot that is indented

        // Create the indent for the headband in the center of the frame
        translate([outer_length / 2 - slot_width / 2, 0, -border_height - indent_depth]) {
            cube([slot_width, outer_width, indent_depth]);
        }

        // Create the top slot in the indent for the headband
        translate([outer_length / 2 - slot_width / 2, 0, -border_height - indent_depth]) {
            cube([slot_width, outer_width, slot_depth]);
        }

        // Create the bottom slot in the indent for the headband
        translate([outer_length / 2 - slot_width / 2, 0, -border_height - indent_depth - slot_depth]) {
            cube([slot_width, outer_width, slot_depth]);
        }
    }```

Solution

  • The cube that you're cutting out for the slot hole cube is created without center = true, so it's edge-aligned along the Y axis. To center it on the Y axis, all you have do is move it half its own width in the negative Y direction.

    In the translate, change the Y parameter from - outer_width / 2 to - slot_width / 2:

    translate([outer_length / 2 - slot_width / 2, - slot_width / 2, -border_height]) {
        cube([slot_width, border_height + 1, slot_depth]);
    }
    

    enter image description here