cadquery/notebook_nueva/hinged_lid.py

421 lines
13 KiB
Python
Raw Normal View History

2023-04-04 21:04:53 +00:00
import cadquery as cq
from cadquery import exporters
import screen_pillars
2023-04-04 21:04:53 +00:00
from modelo import (
height,
mounting_pillar_positions,
ti_depth,
ti_radius,
width,
2023-04-05 20:35:11 +00:00
thickness as model_thickness,
shell_t,
2023-04-04 21:04:53 +00:00
)
2023-04-08 17:41:27 +00:00
import screen_mount
2023-04-05 20:35:11 +00:00
import keyboard
hinge_radius = 5.5
2023-04-04 21:04:53 +00:00
screw_radius = 1.5 # M3
ring_radius = 5 # M3
hinge_offset = max(p[1] for p in mounting_pillar_positions) + 6
hinge_width = 25
2023-04-06 20:42:06 +00:00
thickness = 43
2023-04-04 21:04:53 +00:00
2023-04-08 17:24:03 +00:00
mounting_pillar_positions = [(x, -y) for x, y in mounting_pillar_positions]
mounting_pillars = (
cq.Sketch()
.push(mounting_pillar_positions)
.trapezoid(-12, 12, 90, mode="a")
.circle(ti_radius, mode="s")
.clean()
)
# This is a constant used to control how far back the hinges go
# when open. It's arbitrary and can be adjusted experimentally
# printing small samples
hinge_slant = shell_t + 2
2023-04-04 21:04:53 +00:00
2023-04-15 18:53:52 +00:00
# M3 hex nut dimensions
m3_hn_diam = 5.5
m3_hn_hole = 3
m3_hn_thickness = 2.5
bezel_width = m3_hn_diam + 2
bezel_height = 1
bezel_thickness = 2
2023-04-06 19:59:32 +00:00
2023-04-04 21:04:53 +00:00
def model():
2023-04-05 20:35:11 +00:00
# Create a 2-part hinged lid
2023-04-04 21:04:53 +00:00
model = (
cq.Workplane("XY")
# Hollow box
2023-04-05 20:35:11 +00:00
.workplane(offset=-thickness / 2)
.box(width, height, thickness)
2023-04-08 17:24:03 +00:00
.tag("base")
2023-04-07 20:30:57 +00:00
.edges("|X and >Z and <Y")
.fillet(10)
.edges("|X and >Z and >Y")
.fillet(5)
2023-04-06 20:39:18 +00:00
.edges("|Z")
.fillet(2)
2023-04-05 20:35:11 +00:00
.faces("<Z")
.shell(-shell_t)
2023-04-04 21:04:53 +00:00
.faces(">X")
2023-04-07 20:30:57 +00:00
.workplane()
2023-04-05 20:35:11 +00:00
.center(height / 2 - hinge_offset, thickness / 2 - hinge_radius)
2023-04-04 21:04:53 +00:00
.tag("rightSide")
2023-04-07 20:30:57 +00:00
# Outer surface of the hinge
.workplaneFromTagged("rightSide")
2023-04-04 21:04:53 +00:00
.placeSketch(cq.Sketch().circle(hinge_radius))
2023-04-08 17:24:03 +00:00
.extrude(-hinge_width)
.workplaneFromTagged("rightSide")
.workplane(offset=-width + hinge_width)
.placeSketch(cq.Sketch().circle(hinge_radius))
.extrude(-hinge_width)
2023-04-05 20:35:11 +00:00
# Cut middle section between the hinges
.workplaneFromTagged("rightSide")
.workplane(offset=-hinge_width)
.placeSketch(
cq.Sketch().polygon(
[
(-hinge_radius, -hinge_radius),
(-hinge_radius, 0),
(-hinge_radius - hinge_slant, hinge_radius),
(-hinge_slant, hinge_radius),
(-hinge_slant, hinge_radius - hinge_slant),
(hinge_radius, hinge_radius - hinge_slant),
2023-04-05 20:35:11 +00:00
(hinge_radius, -hinge_radius),
(-hinge_radius, -hinge_radius),
]
)
)
.cutBlind(-width + 2 * hinge_width - 1)
2023-04-08 17:24:03 +00:00
# Pillars to attach to base
.workplaneFromTagged("base")
.workplane(
centerOption="CenterOfBoundBox", offset=model_thickness - thickness / 2
)
2023-04-08 17:41:27 +00:00
.workplaneFromTagged("base")
.workplane(offset=thickness / 2 - shell_t)
2023-04-08 17:24:03 +00:00
.center(-width / 2, height / 2 - shell_t)
.placeSketch(mounting_pillars)
2023-04-08 17:41:27 +00:00
.extrude(-10)
2023-04-04 21:04:53 +00:00
# Hole for screws
2023-04-05 20:35:11 +00:00
.workplaneFromTagged("rightSide")
2023-04-04 21:04:53 +00:00
.placeSketch(cq.Sketch().circle(screw_radius))
2023-04-08 17:24:03 +00:00
.cutBlind(-hinge_width)
.workplaneFromTagged("rightSide")
.workplane(offset=-width + hinge_width)
.placeSketch(cq.Sketch().circle(screw_radius))
.cutBlind(-hinge_width)
2023-04-04 21:04:53 +00:00
# Holes for rings & screw heads
2023-04-05 20:35:11 +00:00
.workplaneFromTagged("rightSide")
2023-04-04 21:04:53 +00:00
.placeSketch(cq.Sketch().circle(ring_radius))
.cutBlind(-5)
2023-04-05 20:35:11 +00:00
.workplaneFromTagged("rightSide")
.workplane(offset=-width + 4)
2023-04-04 21:04:53 +00:00
.placeSketch(cq.Sketch().circle(ring_radius))
.cutBlind(-5)
# Split hinge halves
.faces(">X")
2023-04-05 20:35:11 +00:00
.workplaneFromTagged("rightSide")
.workplane(offset=-hinge_width / 2)
2023-04-04 21:04:53 +00:00
.placeSketch(cq.Sketch().trapezoid(hinge_radius * 2 + 1, hinge_radius * 2, 90))
2023-04-05 20:35:11 +00:00
.cutBlind(-1)
.workplaneFromTagged("rightSide")
.workplane(offset=-hinge_width)
.placeSketch(cq.Sketch().trapezoid(hinge_radius * 2 + 1, hinge_radius * 2, 90))
.cutBlind(-1)
.workplaneFromTagged("rightSide")
.workplane(offset=-width + hinge_width / 2)
.placeSketch(cq.Sketch().trapezoid(hinge_radius * 2 + 1, hinge_radius * 2, 90))
.cutBlind(-1)
.workplaneFromTagged("rightSide")
.workplane(offset=-width + hinge_width)
2023-04-04 21:04:53 +00:00
.placeSketch(cq.Sketch().trapezoid(hinge_radius * 2 + 1, hinge_radius * 2, 90))
.cutBlind(-1)
# Threaded inserts
2023-04-05 20:35:11 +00:00
.workplaneFromTagged("rightSide")
.workplane(offset=-hinge_width / 2)
2023-04-04 21:04:53 +00:00
.placeSketch(cq.Sketch().circle(ti_radius))
.cutBlind(-ti_depth)
2023-04-05 20:35:11 +00:00
.workplaneFromTagged("rightSide")
.workplane(offset=-width + hinge_width / 2)
2023-04-04 21:04:53 +00:00
.placeSketch(cq.Sketch().circle(ti_radius))
.cutBlind(ti_depth)
# Split two halves
2023-04-05 20:35:11 +00:00
# First cut for the right hinge
2023-04-04 21:04:53 +00:00
.workplaneFromTagged("rightSide")
.placeSketch(
cq.Sketch()
.polygon(
[
(0, 0),
(-hinge_radius - 0.2, 0),
(-hinge_radius - hinge_slant, hinge_radius),
2023-04-04 21:04:53 +00:00
(0, hinge_radius),
(0, 0),
]
)
2023-04-05 20:35:11 +00:00
.polygon(
[
(-hinge_radius - 0.2, 0),
(-hinge_radius - 0.2, -1000),
(-hinge_radius, -1000),
(-hinge_radius, 0),
(-hinge_radius - 0.2, 0),
]
)
2023-04-04 21:04:53 +00:00
.circle(hinge_radius, mode="s")
)
2023-04-05 20:35:11 +00:00
.cutBlind(-hinge_width / 2 - 1)
# Second cut for the right hinge
2023-04-04 21:04:53 +00:00
.workplaneFromTagged("rightSide")
.workplane(offset=-hinge_width / 2)
.placeSketch(
cq.Sketch()
.polygon(
[
(0, 0),
(hinge_radius + 0.2, 0),
(hinge_radius + 0.2 + hinge_slant, hinge_radius),
2023-04-04 21:04:53 +00:00
(0, hinge_radius),
(0, 0),
]
)
.circle(hinge_radius, mode="s")
)
2023-04-05 20:35:11 +00:00
.cutBlind(-hinge_width / 2 - 1)
# First cut for the left hinge
2023-04-04 21:04:53 +00:00
.workplaneFromTagged("rightSide")
2023-04-05 20:35:11 +00:00
.workplane(offset=-width + hinge_width)
2023-04-04 21:04:53 +00:00
.placeSketch(
cq.Sketch()
.polygon(
[
(0, 0),
(hinge_radius + 0.2, 0),
(hinge_radius + 0.2 + hinge_slant, hinge_radius),
2023-04-04 21:04:53 +00:00
(0, hinge_radius),
(0, 0),
]
)
.circle(hinge_radius, mode="s")
)
2023-04-05 20:35:11 +00:00
.cutBlind(-hinge_width / 2 - 1)
# Second cut for the left hinge
2023-04-04 21:04:53 +00:00
.workplaneFromTagged("rightSide")
.workplane(offset=-width + hinge_width / 2)
.placeSketch(
cq.Sketch()
.polygon(
[
(0, 0),
(-hinge_radius - 0.2, 0),
(-hinge_radius - hinge_slant, hinge_radius),
2023-04-04 21:04:53 +00:00
(0, hinge_radius),
(0, 0),
]
)
2023-04-05 20:35:11 +00:00
.polygon(
[
(-hinge_radius - 0.2, 0),
(-hinge_radius - 0.2, -1000),
(-hinge_radius, -1000),
(-hinge_radius, 0),
(-hinge_radius - 0.2, 0),
]
)
2023-04-04 21:04:53 +00:00
.circle(hinge_radius, mode="s")
)
2023-04-05 20:35:11 +00:00
.cutBlind(-hinge_width / 2 - 1)
)
2023-04-08 17:41:27 +00:00
# Screen mount
model = (
# 1st layer
2023-04-08 17:41:27 +00:00
model.workplaneFromTagged("base")
.center(0, -32)
.workplane(offset=thickness / 2 - shell_t)
.tag("screen_plane")
2023-04-08 17:41:27 +00:00
.placeSketch(
cq.Sketch()
.trapezoid(
screen_mount.scr_w + 2 * bezel_width,
screen_mount.scr_h + 2 * bezel_height,
90,
2023-04-08 17:41:27 +00:00
)
.vertices()
.fillet(2)
)
.extrude(-2 - screen_mount.scr_thickness)
2023-04-15 18:53:52 +00:00
# Hole for screws
2023-04-08 17:41:27 +00:00
.workplaneFromTagged("screen_plane")
.workplane(offset=1)
.rect(
screen_mount.scr_w + 2 * bezel_width - m3_hn_diam - 1,
screen_mount.scr_h + 2 * bezel_height - m3_hn_diam - 1,
forConstruction=True,
)
.vertices()
2023-04-15 18:53:52 +00:00
.hole(m3_hn_hole, depth=10)
# Holes for captured nuts
.workplaneFromTagged("screen_plane")
.workplane(offset=1)
.rect(
screen_mount.scr_w + 2 * bezel_width - m3_hn_diam - 1,
screen_mount.scr_h + 2 * bezel_height - m3_hn_diam - 1,
forConstruction=True,
2023-04-08 17:41:27 +00:00
)
.vertices()
2023-04-15 18:53:52 +00:00
.hole(m3_hn_diam, depth=m3_hn_thickness + 0.5)
# Remove middle of the screen holder
2023-04-08 17:41:27 +00:00
.workplaneFromTagged("screen_plane")
.placeSketch(
cq.Sketch().trapezoid(
2023-04-15 18:53:52 +00:00
screen_mount.scr_w - 40, screen_mount.scr_h + 2 * bezel_height, 90
)
2023-04-08 17:41:27 +00:00
)
.cutBlind(-100)
2023-04-15 18:53:52 +00:00
# Hole to place screen
2023-04-08 17:41:27 +00:00
.workplaneFromTagged("screen_plane")
.workplane(offset=-screen_mount.scr_thickness - 2)
2023-04-08 17:41:27 +00:00
.placeSketch(cq.Sketch().trapezoid(screen_mount.scr_w, screen_mount.scr_h, 90))
.cutBlind(screen_mount.scr_thickness)
)
2023-04-05 20:35:11 +00:00
# Cut off shape of the base
model = (
model.workplaneFromTagged("rightSide")
.center(-height + hinge_offset, -thickness + hinge_radius)
.placeSketch(
cq.Sketch().polygon(
[
(0, 0),
(0, keyboard.front_thickness),
(shell_t, keyboard.front_thickness),
(keyboard.actual_height + shell_t, keyboard.back_thickness),
(keyboard.actual_height + shell_t, model_thickness),
(height, model_thickness),
(height, 0),
(0, 0),
]
)
)
.cutBlind(-1000)
2023-04-04 21:04:53 +00:00
)
return model
2023-04-15 18:53:52 +00:00
def front_bezel():
model = (
cq.Workplane("XY")
# Hollow box
.tag("base")
.placeSketch(
cq.Sketch()
.trapezoid(
screen_mount.scr_w + 2 * bezel_width + 2 * bezel_thickness,
screen_mount.scr_h + 2 * bezel_height + 2 * bezel_thickness,
90,
)
.vertices()
.fillet(2)
)
.extrude(-2 - screen_mount.scr_thickness - bezel_thickness)
.workplaneFromTagged("base")
.workplane(offset=-bezel_thickness)
.placeSketch(
cq.Sketch()
.trapezoid(
screen_mount.scr_w + 2 * bezel_width,
screen_mount.scr_h + 2 * bezel_height,
90,
)
.vertices()
.fillet(2)
)
.cutBlind(-100)
# Holes for screws
.workplaneFromTagged("base")
.rect(
screen_mount.scr_w + 2 * bezel_width - m3_hn_diam - 1,
screen_mount.scr_h + 2 * bezel_height - m3_hn_diam - 1,
forConstruction=True,
)
.vertices()
.hole(m3_hn_hole, depth=10)
# Viewport hole
.workplaneFromTagged("base")
.placeSketch(
cq.Sketch()
.trapezoid(
screen_mount.vis_w,
screen_mount.vis_h,
90,
)
.vertices()
.fillet(2)
)
.cutBlind("last")
# Cable gap
.workplaneFromTagged("base")
.workplane(offset=-screen_mount.scr_thickness - bezel_thickness)
.center(0, 10)
.placeSketch(
cq.Sketch()
.trapezoid(
screen_mount.vis_w,
screen_mount.vis_h,
90,
)
.vertices()
.fillet(2)
)
.cutBlind(-10)
)
return model
2023-04-04 21:04:53 +00:00
if __name__ == "__main__":
model = model()
2023-04-05 20:35:11 +00:00
2023-04-04 21:04:53 +00:00
exporters.export(model, "hinged_lid.stl")
2023-04-05 20:35:11 +00:00
2023-04-15 18:53:52 +00:00
exporters.export(front_bezel(), "front_bezel.stl")
2023-04-05 20:35:11 +00:00
exporters.export(
model,
"hinged_lid.svg",
opt={
"projectionDir": (0, 0, -1),
2023-04-05 20:35:11 +00:00
"strokeWidth": 0.3,
},
)
offset_width = -width / 2
right_side = (
model.faces(">X")
.workplane(centerOption="CenterOfBoundBox", offset=offset_width)
.split(keepTop=True)
)
exporters.export(right_side, "right_hinged_lid.stl")
left_side = (
model.faces(">X")
.workplane(centerOption="CenterOfBoundBox", offset=offset_width)
.split(keepBottom=True)
)
exporters.export(left_side, "left_hinged_lid.stl")