2 ;; Atreus 2 case design
3 ;; Copyright © 2019 Phil Hagelberg and contributors
4 ;; released under the GPLv3 or later
7 ;; * port notch on bottom plate and spacer
12 ;; glowforge uses 96 dpi, 25.4 mm in an inch
13 (define scale (/ 96 25.4))
18 (define cols 6) ; per hand
20 (define angle (degrees->radians 10))
21 (define corner-radius 6.0)
23 (define alps-switch-width 15.887)
24 (define alps-switch-height 13.087)
25 (define cherry-switch-width 13.62)
26 (define cherry-switch-height 13.72)
28 (define switch-height (if cherry? cherry-switch-height alps-switch-height))
29 (define switch-width (if cherry? cherry-switch-width alps-switch-width))
31 (define switch-spacing 19.0)
33 (define screw-radius 1.4) ; for M3 screws + kerf
35 (define side-screw-distance (* switch-spacing rows))
36 (define bottom-screw-distance (* switch-spacing cols))
37 (define left corner-radius)
38 (define bottom 95) ; outer bottom
39 (define left-top (+ left (* side-screw-distance (sin angle))))
40 (define top (- bottom (* side-screw-distance (cos angle))))
41 (define right (- width corner-radius))
42 (define right-top (- right (* side-screw-distance (sin angle))))
43 (define mid-bottom (+ bottom (* bottom-screw-distance (sin angle)) -3))
44 (define mid-offset 25)
45 (define mid-x (/ width 2))
46 (define mid-left (- mid-x mid-offset))
47 (define mid-right (+ mid-x mid-offset))
49 (define hull-coords (list (list right-top top)
51 (list mid-right mid-bottom)
52 (list mid-left mid-bottom)
58 `(g () ,@(for/list ([s (append (take hull-coords 2)
59 ;; the bottom middle has only one screw but
61 (list (list (/ width 2) mid-bottom))
62 (drop hull-coords 4))])
63 `(circle ((r ,(number->string screw-radius))
64 (cx ,(number->string (first s)))
65 (cy ,(number->string (second s))))))))
68 (define outline-coords (append hull-coords (take hull-coords 2)))
70 (define (to-next-screw? theta current-screw)
71 (let* ([current (list-ref outline-coords current-screw)]
72 [cx (first current)] [cy (second current)]
73 [next (list-ref outline-coords (add1 current-screw))]
74 [nx (first next)] [ny (second next)]
75 [dx (- nx cx)] [dy (- ny cy)]
76 [next-theta (- (radians->degrees (atan dy dx)))])
77 (= (floor (modulo (floor next-theta) 180))
78 (floor (modulo (- theta 90) 180)))))
80 ;; trace the outline by going from screw to screw until you've gone full-circle
81 (define (outline-points coords theta current-screw)
82 (if (< -360 (- theta 90) 360)
83 (let* ([current (list-ref outline-coords current-screw)]
84 [sx (first current)] [sy (second current)]
85 [x (+ sx (* (cos (degrees->radians theta)) corner-radius))]
86 [y (- sy (* (sin (degrees->radians theta)) corner-radius))]
87 [coords (cons (format "~s,~s" x y) coords)])
88 (if (to-next-screw? theta current-screw)
89 (outline-points coords theta (add1 current-screw))
90 (outline-points coords (sub1 theta) current-screw)))
95 (define port-curve (list (format "~s,~s" mid-right (- top corner-radius))
96 (format "~s,~s" (+ mid-x corner-radius)
99 (format "~s,~s" (- mid-x corner-radius)
102 (format "~s,~s" mid-left (- top corner-radius))))
104 (define (outline with-port?)
105 `(polygon ((points ,(string-join (let ((noport (outline-points '() 90 0)))
107 (append noport port-curve)
112 (define column-offsets `(8 5 0 6 11 ,(+ 8 switch-spacing switch-spacing)))
114 (define (switch row col)
115 (let* ([x (* (+ 1 col) switch-spacing)]
116 [y (+ (list-ref column-offsets col) (* switch-spacing row))])
117 `(rect ((height ,(number->string switch-height))
118 (width ,(number->string switch-width))
119 (x ,(number->string x))
120 (y ,(number->string y))))))
122 (define hand-height (+ (* switch-spacing rows) (- switch-spacing switch-height)
123 (list-ref column-offsets 0)))
124 (define switch-x-offset -6.5)
125 (define switch-y-offset (- bottom hand-height -3.5))
128 `(g ((transform ,(format "translate(~s, ~s) rotate(~s, ~s, ~s)"
129 switch-x-offset switch-y-offset
130 (radians->degrees angle)
132 ,@(for/list ([col (in-range cols)]
134 [row (if (= 5 col) '(0 1) (in-range rows))])
137 (define switches-right
138 `(g ((transform ,(format "translate(~s,~s) scale(-1, 1)" width 0)))
141 (define logo-doc (call-with-input-file "logo-fragment.svg" read-xml))
143 (define pcb-doc (call-with-input-file "pcb-fragment.svg" read-xml))
145 (define (layer plate)
146 (document (prolog '() false '())
148 `(svg ((xmlns:svg "http://www.w3.org/2000/svg")
149 (height ,(number->string (* height scale)))
150 (width ,(number->string (* width scale))))
151 ,@(if (eq? plate 'switch)
152 `((g ((transform "translate(436, 115)")
154 ,(xml->xexpr (document-element logo-doc))))
156 ,@(if (eq? plate 'spacer)
157 (list (xml->xexpr (document-element pcb-doc)))
159 (g ((transform ,(format "scale(~s, ~s)" scale scale))
164 ,(outline (not (eq? plate 'switch)))
165 ,@(if (eq? plate 'switch)
166 (list switches switches-right)
170 ;; to laser cut these, you have to open them in inkscape, then save,
171 ;; and then upload; for some reason the glowforge crashes if you try to cut
174 (define (write-out-layer layer-name)
175 (call-with-output-file (format "case2-~a.svg" (symbol->string layer-name))
177 (display "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" out)
178 (display-xml (layer layer-name) out))
181 (write-out-layer 'switch)
182 (write-out-layer 'bottom)
183 (write-out-layer 'spacer)