From e5f0637183a93849fd0b1c3cd93394bb6520ad2c Mon Sep 17 00:00:00 2001 From: Guillaume Collic Date: Fri, 17 May 2024 11:23:06 +0200 Subject: [PATCH] feat: add a second latch option --- boxes/generators/sidehingebox.py | 77 ++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/boxes/generators/sidehingebox.py b/boxes/generators/sidehingebox.py index d87b3939..97c6dd25 100644 --- a/boxes/generators/sidehingebox.py +++ b/boxes/generators/sidehingebox.py @@ -51,8 +51,8 @@ def __init__(self) -> None: help="radius of the hinge inner circle") self.argparser.add_argument( - "--cherrymx_latch", action="store", type=boolarg, default=False, - help="add a latch, based on 3D printing and a cherry mx compatible mechanical keyboard switch") + "--cherrymx_latches", action="store", type=int, default=0, + help="add one or two latches, based on 3D printing and a cherry mx compatible mechanical keyboard switch") def render(self): @@ -61,7 +61,7 @@ def render(self): p = self.play * t hinge_radius = self.hinge_radius hinge_center = self.hinge_center if self.hinge_center else 2*t + hinge_radius - with_latch = self.cherrymx_latch + latches = self.cherrymx_latches self.mx_width = 15.4 self.mx_length = t+16.4+2.8 #2.8 can be removed if the switch is trimmed flush @@ -82,36 +82,44 @@ def render(self): fingered_ho = ho - gap - 2*hinge_center with self.saved_context(): - self.inner_side(x, hi, hinge_center, hinge_radius, fingered_hi, with_latch=with_latch, reverse=True) - d_side_callback = [ - lambda:self.fingerHolesAt(self.mx_length+t/2, 0, self.mx_width), - ] if with_latch else None - self.rectangularWall(yi, hi, "fFeF", callback=d_side_callback, move="right", label="inner - full side D") - self.inner_side(x, hi, hinge_center, hinge_radius, fingered_hi) + self.inner_side(x, hi, hinge_center, hinge_radius, fingered_hi, latches, reverse=True) + self.rectangularWall( + yi, + hi, + "fFeF", + callback=[lambda:self.back_cb(yi, latches)], + move="right", + label="inner - full side D") + self.inner_side(x, hi, hinge_center, hinge_radius, fingered_hi, latches) self.rectangularWall(0, hi, "ffef", move="up only") with self.saved_context(): - self.outer_side(x, ho, hinge_center, hinge_radius, fingered_ho, with_latch=with_latch) + self.outer_side(x, ho, hinge_center, hinge_radius, fingered_ho, latches) with self.saved_context(): self.rectangularWall(yo, fingered_ho, "fFeF", move="up", label="outer - small side B") self.moveTo(t+p,0) self.rectangularWall(yi, fingered_hi, "eFfF", move="right", label="inner - small side B") self.rectangularWall(yo, 0, "fFeF", move="right only") - self.outer_side(x, ho, hinge_center, hinge_radius, fingered_ho, reverse=True) + self.outer_side(x, ho, hinge_center, hinge_radius, fingered_ho, latches, reverse=True) self.rectangularWall(0, ho, "ffef", move="up only") bottom_callback = [ lambda:self.fingerHolesAt(x-self.mx_width-t/2, 0, self.mx_length), - lambda:self.fingerHolesAt(self.mx_length+t/2, 0, self.mx_width), - ] if with_latch else None + lambda:self.back_cb(yi, latches), + lambda:self.fingerHolesAt(self.mx_width+t/2, 0, self.mx_length) if latches>1 else None, + ] if latches else None self.rectangularWall(x, yi, "FFFF", callback=bottom_callback, move="right", label="inner - bottom") self.rectangularWall(x, yo, "FEFF", move="right", label="outer - upper lid") for _ in range(2): self.rectangularWall(2*t, 1.5*t, "eeee", move="right") - if with_latch: - self.rectangularWall(self.mx_width, self.mx_width, "eeee", move="right") - self.rectangularWall(self.mx_width, self.mx_width, "ffef", move="right") - self.rectangularWall(self.mx_length, self.mx_width, "ffeF", move="right") + + if latches: + for _ in range(latches): + with self.saved_context(): + self.rectangularWall(self.mx_width, self.mx_width, "eeee", move="right") + self.rectangularWall(self.mx_width, self.mx_width, "ffef", move="right") + self.rectangularWall(self.mx_length, self.mx_width, "ffeF", move="right") + self.rectangularWall(self.mx_length, self.mx_width, "ffeF", move="up only") self.text(f""" OpenSCAD code for 3D printing the cherry MX latch button: ############################################# @@ -161,16 +169,26 @@ def render(self): mx_inner(); }}""") - - def inner_side_cb(self, x): - self.fingerHolesAt(x-self.mx_width-self.thickness/2, 0, self.mx_width) - self.circle(x-self.mx_width/2, self.mx_width/2, 5.7+self.burn) - - def inner_side(self, x, h, hinge_center, hinge_radius, fingered_h, with_latch=False, reverse=False): + def back_cb(self, y, latches): + if latches>0: + self.fingerHolesAt(self.mx_length+self.thickness/2, 0, self.mx_width) + if latches>1: + self.fingerHolesAt(y-self.mx_length-self.thickness/2, 0, self.mx_width) + + def inner_side_cb(self, x, reverse): + if reverse: + self.fingerHolesAt(x-self.mx_width-self.thickness/2, 0, self.mx_width) + self.circle(x-self.mx_width/2, self.mx_width/2, 5.7+self.burn) + else: + self.fingerHolesAt(self.mx_width+self.thickness/2, 0, self.mx_width) + self.circle(self.mx_width/2, self.mx_width/2, 5.7+self.burn) + + def inner_side(self, x, h, hinge_center, hinge_radius, fingered_h, latches, reverse=False): sides = Inner2SidesEdge( self, x, h, hinge_center, hinge_radius, fingered_h, reverse ) noop_edge = edges.NoopEdge(self, margin=self.thickness if reverse else 0) + self.rectangularWall( x, h, @@ -178,17 +196,20 @@ def inner_side(self, x, h, hinge_center, hinge_radius, fingered_h, with_latch=Fa move="right", label="inner - hinge side " + ("A" if reverse else "C"), callback=[ - lambda: self.inner_side_cb(x) - ] if with_latch and reverse else None, + lambda: self.inner_side_cb(x, reverse) + ] if (latches and reverse) or latches>1 else None, ) - def outer_side(self, x, h, hinge_center, hinge_radius, fingered_h, with_latch=False, reverse=False): + def outer_side(self, x, h, hinge_center, hinge_radius, fingered_h, latches, reverse=False): t = self.thickness sides = Outer2SidesEdge( self, x, h, hinge_center, hinge_radius, fingered_h, reverse ) noop_edge = edges.NoopEdge(self, margin=t if reverse else 0) + latch_x, latch_y = (t+self.mx_width/2, self.mx_width/2) + if reverse: + latch_x, latch_y = latch_y, latch_x self.rectangularWall( x, h, @@ -198,8 +219,8 @@ def outer_side(self, x, h, hinge_center, hinge_radius, fingered_h, with_latch=Fa callback=[ None, None, - lambda: self.circle(t+self.mx_width/2, self.mx_width/2, 5.7+self.burn) - ] if with_latch and not reverse else None, + lambda: self.circle(latch_x, latch_y, 5.7+self.burn) + ] if (latches and not reverse) or latches>1 else None, ) class Inner2SidesEdge(edges.BaseEdge):