Advertisement
snaipperi

Untitled

Aug 7th, 2021
1,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.36 KB | None | 0 0
  1. from typing import Optional, Union
  2.  
  3. import cadquery as cq
  4. from cadquery import Wire, Vector
  5. from cadquery.selectors import Selector
  6.  
  7.  
  8. def sub_edges(self,
  9.               sub: Union['cq.Workplane', str],
  10.               selector: Optional[Union[Selector, str]] = None,
  11.               tag: Optional[str] = None,
  12.               ):
  13.     """
  14.    Subtract given edges from current workplane edges, optionally filtered through selectors.
  15.    Useful for applying fillets/chamfers to specific areas of the board without using
  16.    complicated selectors.
  17.    """
  18.     wp = self.edges(selector, tag)
  19.     if isinstance(sub, str):
  20.         sub = self.workplaneFromTagged(sub)
  21.     wp.objects = [o for o in wp.objects if o not in sub.objects]
  22.     return wp
  23.  
  24.  
  25. def rounded_rect(self,
  26.                  width,
  27.                  height,
  28.                  radius):
  29.     """
  30.    Like cadquery 2D rect, but with a corner radius.
  31.    """
  32.     points = [
  33.         Vector(width / -2.0, height / -2.0, 0),
  34.         Vector(width / 2.0, height / -2.0, 0),
  35.         Vector(width / 2.0, height / 2.0, 0),
  36.         Vector(width / -2.0, height / 2.0, 0),
  37.     ]
  38.     points.append(points[0])
  39.  
  40.     w = Wire.makePolygon(points, False)
  41.     w = w.fillet2D(radius, w.Vertices())
  42.     return self.eachpoint(lambda loc: w.moved(loc), True)
  43.  
  44.  
  45. cq.Workplane.sub_edges = sub_edges
  46. cq.Workplane.rounded_rect = rounded_rect
  47.  
  48.  
  49. # Dynamic params
  50. thickness = 4
  51.  
  52. width = 141
  53. height = 225
  54. half_width = width / 2
  55. half_height = height / 2
  56.  
  57. tool_radius = 1.5875  # 1/8"
  58.  
  59. # Initial 2D workpiece
  60. result = cq.Workplane("XY").rect(width, height, True)
  61.  
  62. # Alpha keyboard sketch
  63. alpha_x = -7.5
  64. alpha_y = -30.5
  65. alpha_size = (10, 10)
  66. alpha_points = [(x * 14.25 + alpha_x, y * -14.2 + alpha_y,) for x in range(5) for y in range(6)]
  67. result = result.pushPoints(alpha_points).rounded_rect(*alpha_size, tool_radius)
  68.  
  69. # Num keyboard sketch
  70. num_x = -20.5
  71. num_y = -66.5
  72. num_radius = 5
  73. num_dx = -14.5
  74. num_dy = -12
  75. num_points = [(x * num_dx + num_x, y * num_dy + num_y) for x in range(3) for y in range(4)]
  76. result = result.pushPoints(num_points).circle(num_radius)  # .cutThruAll()
  77.  
  78. # Initial extrusion to 6 mm
  79. result = result.extrude(6)
  80. old_edges = result.edges()
  81.  
  82.  
  83. # Cut off the outer shape
  84. chunk = cq.Workplane("XY").workplane(2).rect(width, height, True).extrude(4)
  85. chunk = chunk.faces('>Z').workplane().moveTo(0, -half_height).line(57.5, 0).line(0, 23).line(10, 0).line(0, 56).line(-7, 0).line(0, 36).line(10, 0).line(0, 91).line(-10, 0).line(0, 11).line(10, 0).line(0, 8).line(-70.5, 0).mirrorY().cutBlind(-4)
  86. result = result.cut(chunk)
  87. del chunk
  88. result = result.sub_edges(old_edges, '|Z').fillet(tool_radius)
  89. old_edges = result.edges()
  90.  
  91. # Cut off the L shaped keyguard
  92. chunk = cq.Workplane("XY").workplane(4).rect(width, height, True).extrude(2)
  93. chunk = chunk.faces('>Z').workplane().moveTo(46.5, 4.48).line(0, -27).line(-68, 0).line(0, -37.5).line(-34.5, 0).line(0, 64.5).lineTo(46.5, 4.48).wire().cutBlind(-2)
  94. result = result.cut(chunk)
  95. del chunk
  96. result = result.sub_edges(old_edges, '|Z').fillet(tool_radius).sub_edges(old_edges, '#Z and >Z').chamfer(1)
  97. old_edges = result.edges()
  98.  
  99.  
  100. # Screen
  101. result = result.pushPoints([(0, 61.5)]).rect(100, 75).cutThruAll() \
  102.     .sub_edges(old_edges, '|Z').fillet(tool_radius) \
  103.     .sub_edges(old_edges, '#Z exc <Z').chamfer(thickness - 1)
  104.  
  105. old_edges = result.edges()
  106.  
  107. del old_edges
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement