Cosplay/nhf/parts/springs.py

60 lines
1.8 KiB
Python

import math
import cadquery as Cq
def torsion_spring(radius=12,
height=20,
thickness=2,
omega=90,
tail_length=25,
right_handed: bool = False):
"""
Produces a torsion spring with abridged geometry since sweep is very slow in
cq-editor.
"""
if right_handed:
omega = -omega
base = (
Cq.Workplane('XY')
.cylinder(height=height, radius=radius,
centered=(True, True, False))
)
base.faces(">Z").tag("top")
base.faces("<Z").tag("bot")
box_shift = -radius if right_handed else radius-thickness
result = (
base
.cylinder(height=height, radius=radius - thickness, combine='s',
centered=(True, True, True))
.transformed(
offset=(0, box_shift),
rotate=(0, 0, 0))
.box(
length=tail_length,
width=thickness,
height=thickness,
centered=False)
.copyWorkplane(Cq.Workplane('XY'))
.transformed(
offset=(0, 0, height - thickness),
rotate=(0, 0, omega))
.center(-tail_length, box_shift)
.box(
length=tail_length,
width=thickness,
height=thickness,
centered=False)
)
r = -radius if right_handed else radius
plane = result.copyWorkplane(Cq.Workplane('XY'))
plane.polyline([(0, r, 0), (tail_length, r, 0)],
forConstruction=True).tag("dir_bot")
omega = math.radians(omega)
c, s = math.cos(omega), math.sin(omega)
l = -tail_length
plane.polyline([
(-s * r, c * r, height),
(c * l - s * r, c * r + s * l, height)],
forConstruction=True).tag("dir_top")
return result