spacer.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import cadquery as cq
  2. from cadquery.occ_impl.exporters import ExportTypes
  3. #from ocp_vscode import show, show_object
  4. import math
  5. ###### Parameter #####
  6. a_thick = 1.7
  7. a_diam = 2.4
  8. a_gap_width = 0.8
  9. overhang_a_width = a_gap_width /2 * 0.85
  10. overhang_a_height = 3
  11. b_thick = 4.2
  12. b_diam = 3.1
  13. b_gap_width = a_gap_width
  14. overhang_b_width = b_gap_width /2 * 0.9
  15. overhang_b_height = overhang_a_height
  16. thick_eps = 0.1
  17. space_height = 2
  18. space_diam = 7
  19. overhang_top = 0.5
  20. cut_height = 2.8 # use this for easier printing
  21. cut_center = False # if false, cut is at the edge of smallest diameter of the spacer
  22. spacer = cq.Workplane("XY").cylinder(space_height, space_diam/2)
  23. clip_a = (
  24. spacer.faces(">Z").workplane()
  25. .circle(a_diam/2-thick_eps).extrude(a_thick, combine=False)
  26. .faces(">Z").workplane()
  27. .circle(a_diam/2+overhang_a_width).workplane(offset=overhang_a_height+thick_eps)
  28. .circle(overhang_top).loft()
  29. .faces(">Z").workplane()
  30. .rect(a_gap_width, 100)
  31. .cutThruAll()
  32. )
  33. clip_b = (
  34. spacer.faces("<Z").workplane()
  35. .circle(b_diam/2-thick_eps).extrude(b_thick, combine=False)
  36. .faces("<Z").workplane()
  37. .circle(b_diam/2+overhang_b_width).workplane(offset=overhang_b_height+thick_eps)
  38. .circle(overhang_top).loft()
  39. .faces("<Z").workplane()
  40. .rect(b_gap_width, 100)
  41. .cutThruAll()
  42. )
  43. spacer = spacer.union(clip_a).union(clip_b)
  44. if cut_height > 0:
  45. if cut_center:
  46. spacer_cut = spacer.cut(cq.Workplane("XY").box(100, cut_height, 100))
  47. else:
  48. min_diam = min(a_diam, b_diam) - 2*thick_eps
  49. min_gap_width = min(a_gap_width, b_gap_width)
  50. center_to_min_edge = math.sqrt(min_diam**2 - (min_gap_width)**2)/2
  51. spacer_cut = spacer.cut(cq.Workplane("XY").box(100, cut_height, 100, centered=(True, False, True)).translate((0, -center_to_min_edge, 0)))
  52. spacer = spacer.cut(spacer_cut)
  53. ############
  54. #show_object(spacer)
  55. with open(r"spacer_2.5.step", "w") as fp:
  56. cq.exporters.exportShape(spacer, ExportTypes.STEP, fp)