From ba799aab7dbe14f2cedb6171dc6e02ba4cff9754 Mon Sep 17 00:00:00 2001 From: Correl Date: Tue, 6 Jun 2023 00:14:58 -0400 Subject: [PATCH] Add initial OpenSCAD case files --- openscad/.gitignore | 2 + openscad/Makefile | 12 +++++ openscad/case.scad | 9 ++++ openscad/generate.py | 95 +++++++++++++++++++++++++++++++++++++++ openscad/requirements.txt | 1 + 5 files changed, 119 insertions(+) create mode 100644 openscad/.gitignore create mode 100644 openscad/Makefile create mode 100644 openscad/case.scad create mode 100644 openscad/generate.py create mode 100644 openscad/requirements.txt diff --git a/openscad/.gitignore b/openscad/.gitignore new file mode 100644 index 0000000..6f2b4ce --- /dev/null +++ b/openscad/.gitignore @@ -0,0 +1,2 @@ +*.stl +pcb.scad diff --git a/openscad/Makefile b/openscad/Makefile new file mode 100644 index 0000000..1fb6afa --- /dev/null +++ b/openscad/Makefile @@ -0,0 +1,12 @@ +.PHONY: all clean + +all: case.stl + +pcb.scad: generate.py ../kicad/Digital\ Audio\ Switch.kicad_pcb + python3 $< > $@ + +case.stl: case.scad pcb.scad + openscad -o $@ case.scad + +clean: + rm -f case.stl pcb.scad diff --git a/openscad/case.scad b/openscad/case.scad new file mode 100644 index 0000000..e2e913f --- /dev/null +++ b/openscad/case.scad @@ -0,0 +1,9 @@ +$fn = 50; + +use ; + +color("green", 0.5) +translate([0,0,10]) + pcb(1.6); + +mounting_supports(h=10,r=4); diff --git a/openscad/generate.py b/openscad/generate.py new file mode 100644 index 0000000..6eb15d6 --- /dev/null +++ b/openscad/generate.py @@ -0,0 +1,95 @@ +import sexpdata as s + + +def cadr(x): + return s.car(s.cdr(x)) + + +def cddr(x): + return s.cdr(s.cdr(x)) + + +with open("../kicad/Digital Audio Switch.kicad_pcb", "r") as f: + kicad_pcb = s.load(f) + + +# Assumes the board is cut out with a single rectangle +[rect] = [ + s.cdr(x) + for x in kicad_pcb + if s.car(x) == s.Symbol("gr_rect") and [s.Symbol("layer"), "Edge.Cuts"] in s.cdr(x) +] +[[start_x, start_y]] = [s.cdr(x) for x in rect if s.car(x) == s.Symbol("start")] +[[end_x, end_y]] = [s.cdr(x) for x in rect if s.car(x) == s.Symbol("end")] +[start_x, start_y, end_x, end_y] = [ + min(start_x, end_x), + min(start_y, end_y), + max(start_x, end_x), + max(start_y, end_y), +] +width = end_x - start_x +height = end_y - start_y +print( + f""" +module board() {{ + square([{width}, {height}]); +}} + """.strip() +) + +footprints = [ + s.cdr(x) + for x in kicad_pcb + if s.car(x) == s.Symbol("footprint") and s.car(s.cdr(x)).startswith("MountingHole:") +] + +holes = [] +for hole in footprints: + [[hole_x, hole_y]] = [s.cdr(x) for x in hole if s.car(x) == s.Symbol("at")] + pads = [cddr(x) for x in hole if s.car(x) == s.Symbol("pad")] + for pad in pads: + [[pad_x, pad_y]] = [s.cdr(x) for x in pad if s.car(x) == s.Symbol("at")] + drills = [cadr(x) for x in pad if s.car(x) == s.Symbol("drill")] + for diameter in drills: + # print([hole_x + pad_x, hole_y + pad_y, diameter]) + pos_x = (hole_x + pad_x) - start_x + pos_y = (hole_y + pad_y) - start_y + holes.append((pos_x, pos_y, diameter)) + +print("module mounting_holes() {") +for pos_x, pos_y, diameter in holes: + print(f"translate([{pos_x}, {pos_y}]) circle(d={diameter});") +print("}") + +print("module mounting_posts(r, h) {") +for pos_x, pos_y, diameter in holes: + print( + " ".join( + [ + f"translate([{pos_x}, {pos_y}]) ", + f"linear_extrude(h)", + f"circle(d={diameter} + (r * 2));", + ] + ) + ) + +print("}") + +print( + """ +module pcb(h=1) { + linear_extrude(h) + difference() { + board(); + mounting_holes(); + } +} + +module mounting_supports(r, h) { + difference() { + mounting_posts(r=r, h=h); + linear_extrude(h+1) mounting_holes(); + } +} +""" +) diff --git a/openscad/requirements.txt b/openscad/requirements.txt new file mode 100644 index 0000000..2182f64 --- /dev/null +++ b/openscad/requirements.txt @@ -0,0 +1 @@ +sexpdata