McBin

m3_bolt.scad

// M3 Bolt - Hex Head, 12mm Thread Length
// Dimensions based on ISO/DIN standards
// M3 thread: major dia 3mm, pitch 0.5mm
// Hex head: 5.5mm across flats, 2mm height

$fn = 64;  // high-res for smooth threads

// --- PARAMETERS ---
major_dia   = 3.0;     // M3 major diameter (mm)
pitch       = 0.5;     // M3 thread pitch (mm)
thread_len  = 12.0;    // Threaded length (mm)
head_width  = 5.5;     // Hex head across flats (mm)
head_height = 2.0;     // Hex head height (mm)
shaft_dia   = 2.9;     // Slightly under major dia for clean thread

// --- MODULES ---
module hexagon(w, h) {
    // w = width across flats, h = height
    r = w / 2 / cos(30);  // radius to vertices
    linear_extrude(height = h)
        circle(r = r, $fn = 6);
}

module thread_profile() {
    // Basic triangular thread profile
    // M3 pitch 0.5mm => thread depth ~0.27mm
    d = 0.27;
    polygon(points = [
        [0, 0],
        [pitch * 0.5, d],
        [pitch, 0]
    ]);
}

module thread(dia, len, p) {
    // Threaded shaft using intersection of helix & cylinder
    difference() {
        cylinder(d = dia, h = len);

        // Cut thread grooves
        for (i = [0 : p : len + p]) {
            translate([0, 0, i])
                linear_extrude(height = p * 1.1, twist = 360, slices = 20)
                    translate([dia/2, 0, 0])
                        circle(d = p * 0.6);
        }
    }
}

module bolt() {
    // Shaft (smooth core)
    color("silver") {
        difference() {
            // Core cylinder
            cylinder(d = shaft_dia, h = thread_len);

            // Thread groove - helical cut
            for (z = [0 : pitch : thread_len]) {
                translate([0, 0, z]) {
                    rotate([0, 0, z * 360 / pitch]) {
                        translate([shaft_dia/2, 0, 0])
                            rotate([90, 0, 0])
                                cylinder(d = 0.35, h = 0.01, $fn = 6);
                    }
                }
            }
        }
    }

    // Thread (approximate helical ridge)
    // Simpler approach: thread as a series of slices
    for (z = [0 : pitch/4 : thread_len - pitch/4]) {
        hull() {
            translate([0, 0, z])
                cylinder(d = major_dia, h = 0.01);
            translate([0, 0, z + pitch/4])
                cylinder(d = major_dia, h = 0.01);
        }
    }

    // Hex head
    translate([0, 0, thread_len])
        hexagon(head_width, head_height);

    // Chamfer on top of head
    translate([0, 0, thread_len + head_height - 0.2])
        cylinder(d1 = head_width / cos(30) * 2 - 0.8, d2 = head_width / cos(30) * 2 - 1.6, h = 0.4);
}

// --- RENDER ---
bolt();

// Uncomment for flat view:
// translate([-10, 0, 0]) rotate([0, 90, 0]) bolt();
Copied to clipboard!