Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- """Bearing-mount cylinder for Feetech STS3215 top adapter plate.
- Pygame sliders (0.02mm steps) → CadQuery STEP export."""
- import math, time, cadquery as cq, pygame
- # Top plate ref: OD 19.95, bolt circle ø14 4×M3 (ø3.2), center ø9, thick 4.5mm
- P = { # key: [value, min, max, label]
- "od": [19.80, 19.0, 20.2, "Outer Dia (6804-0.2)"],
- "bore": [ 9.00, 3.0, 12.0, "Center Bore"],
- "bolt_cd": [14.00, 12.0, 16.0, "Bolt Circle Dia"],
- "bolt_d": [ 3.25, 2.8, 3.8, "Bolt Hole (M3+0.25)"],
- "height": [10.00, 3.0, 25.0, "Height"],
- }
- STEP = 0.02
- KEYS = list(P.keys())
- W, H, SX, SW, SH, GAP, TY = 700, 420, 240, 360, 22, 48, 36
- def snap(v): return round(round(v / STEP) * STEP, 4)
- def export(p):
- body = cq.Workplane("XY").circle(p["od"]/2).circle(p["bore"]/2).extrude(p["height"])
- pts = [(p["bolt_cd"]/2*math.cos(math.radians(a)),
- p["bolt_cd"]/2*math.sin(math.radians(a))) for a in (0, 90, 180, 270)]
- body = body.faces(">Z").workplane().pushPoints(pts).hole(p["bolt_d"])
- fn = f"STS3215-{int(time.time())}.step"
- cq.exporters.export(body, fn)
- return fn
- def main():
- pygame.init()
- scr = pygame.display.set_mode((W, H))
- pygame.display.set_caption("STS3215 Bearing Cylinder")
- f = pygame.font.SysFont("menlo,consolas,monospace", 13)
- fb = pygame.font.SysFont("menlo,consolas,monospace", 15, bold=True)
- clk, drag, msg, msg_t = pygame.time.Clock(), None, "", 0
- def vals(): return {k: P[k][0] for k in KEYS}
- def vx(k): return SX + int((P[k][0] - P[k][1]) / (P[k][2] - P[k][1]) * SW)
- def xv(k, x): P[k][0] = snap(P[k][1] + max(0, min(1, (x - SX) / SW)) * (P[k][2] - P[k][1]))
- btn = pygame.Rect(W // 2 - 80, H - 56, 160, 36)
- run = True
- while run:
- mx, my = pygame.mouse.get_pos()
- for ev in pygame.event.get():
- if ev.type == pygame.QUIT or (ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE):
- run = False
- elif ev.type == pygame.MOUSEBUTTONDOWN and ev.button == 1:
- for i in range(len(KEYS)):
- if pygame.Rect(SX, TY + i * GAP, SW, SH).collidepoint(ev.pos):
- drag = i; xv(KEYS[i], ev.pos[0])
- if btn.collidepoint(ev.pos):
- try: fn = export(vals()); msg, msg_t = f"Saved {fn}", time.time()
- except Exception as e: msg, msg_t = str(e)[:60], time.time()
- elif ev.type == pygame.MOUSEBUTTONUP: drag = None
- elif ev.type == pygame.MOUSEMOTION and drag is not None: xv(KEYS[drag], ev.pos[0])
- scr.fill((32, 32, 36))
- scr.blit(fb.render("STS3215 Bearing-Mount Cylinder", True, (80, 180, 255)), (16, 8))
- for i, k in enumerate(KEYS):
- y = TY + i * GAP
- scr.blit(f.render(P[k][3], True, (200, 200, 200)), (12, y + 3))
- pygame.draw.rect(scr, (55, 55, 60), pygame.Rect(SX, y, SW, SH), border_radius=3)
- fx = vx(k)
- pygame.draw.rect(scr, (80, 180, 255), pygame.Rect(SX, y, fx - SX, SH), border_radius=3)
- pygame.draw.circle(scr, (255, 255, 255), (fx, y + SH // 2), 7)
- scr.blit(f.render(f"{P[k][0]:7.2f}mm", True, (200, 200, 200)), (SX + SW + 10, y + 3))
- # cross-section preview
- cx, cy, sc = 120, H - 110, 7.5
- p = vals()
- pygame.draw.circle(scr, (55, 55, 60), (cx, cy), int(p["od"] / 2 * sc), 2)
- pygame.draw.circle(scr, (120, 120, 120), (cx, cy), int(p["bore"] / 2 * sc), 1)
- for a in (0, 90, 180, 270):
- bx, by = cx + p["bolt_cd"]/2*sc*math.cos(math.radians(a)), cy + p["bolt_cd"]/2*sc*math.sin(math.radians(a))
- pygame.draw.circle(scr, (80, 180, 255), (int(bx), int(by)), max(2, int(p["bolt_d"] / 2 * sc)), 1)
- scr.blit(f.render("section", True, (100, 100, 100)), (cx - 22, cy + int(p["od"] / 2 * sc) + 6))
- # export button
- hov = btn.collidepoint(mx, my)
- pygame.draw.rect(scr, (70, 200, 110) if hov else (50, 160, 90), btn, border_radius=5)
- bl = fb.render("Export STEP", True, (255, 255, 255))
- scr.blit(bl, (btn.centerx - bl.get_width() // 2, btn.centery - bl.get_height() // 2))
- if msg and time.time() - msg_t < 4:
- scr.blit(f.render(msg, True, (180, 255, 180)), (W // 2 - 100, H - 18))
- pygame.display.flip(); clk.tick(60)
- pygame.quit()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment