Cosplay/nhf/parts/springs.py

57 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
result.polyline([(0, r, 0), (tail_length, r, 0)],
forConstruction=True).tag("directrix_bot")
c, s = math.cos(omega * math.pi / 180), math.sin(omega * math.pi / 180)
result.polyline([
(s * tail_length, c * r - s * tail_length, height),
(c * tail_length + s * r, c * r - s * tail_length, height)],
forConstruction=True).tag("directrix_top")
return result