Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Optional, Union
- import cadquery as cq
- from cadquery import Wire, Vector
- from cadquery.selectors import Selector
- def sub_edges(self,
- sub: Union['cq.Workplane', str],
- selector: Optional[Union[Selector, str]] = None,
- tag: Optional[str] = None,
- ):
- """
- Subtract given edges from current workplane edges, optionally filtered through selectors.
- Useful for applying fillets/chamfers to specific areas of the board without using
- complicated selectors.
- """
- wp = self.edges(selector, tag)
- if isinstance(sub, str):
- sub = self.workplaneFromTagged(sub)
- wp.objects = [o for o in wp.objects if o not in sub.objects]
- return wp
- def rounded_rect(self,
- width,
- height,
- radius):
- """
- Like cadquery 2D rect, but with a corner radius.
- """
- points = [
- Vector(width / -2.0, height / -2.0, 0),
- Vector(width / 2.0, height / -2.0, 0),
- Vector(width / 2.0, height / 2.0, 0),
- Vector(width / -2.0, height / 2.0, 0),
- ]
- points.append(points[0])
- w = Wire.makePolygon(points, False)
- w = w.fillet2D(radius, w.Vertices())
- return self.eachpoint(lambda loc: w.moved(loc), True)
- cq.Workplane.sub_edges = sub_edges
- cq.Workplane.rounded_rect = rounded_rect
- # Dynamic params
- thickness = 4
- width = 141
- height = 225
- half_width = width / 2
- half_height = height / 2
- tool_radius = 1.5875 # 1/8"
- # Initial 2D workpiece
- result = cq.Workplane("XY").rect(width, height, True)
- # Alpha keyboard sketch
- alpha_x = -7.5
- alpha_y = -30.5
- alpha_size = (10, 10)
- alpha_points = [(x * 14.25 + alpha_x, y * -14.2 + alpha_y,) for x in range(5) for y in range(6)]
- result = result.pushPoints(alpha_points).rounded_rect(*alpha_size, tool_radius)
- # Num keyboard sketch
- num_x = -20.5
- num_y = -66.5
- num_radius = 5
- num_dx = -14.5
- num_dy = -12
- num_points = [(x * num_dx + num_x, y * num_dy + num_y) for x in range(3) for y in range(4)]
- result = result.pushPoints(num_points).circle(num_radius) # .cutThruAll()
- # Initial extrusion to 6 mm
- result = result.extrude(6)
- old_edges = result.edges()
- # Cut off the outer shape
- chunk = cq.Workplane("XY").workplane(2).rect(width, height, True).extrude(4)
- 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)
- result = result.cut(chunk)
- del chunk
- result = result.sub_edges(old_edges, '|Z').fillet(tool_radius)
- old_edges = result.edges()
- # Cut off the L shaped keyguard
- chunk = cq.Workplane("XY").workplane(4).rect(width, height, True).extrude(2)
- 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)
- result = result.cut(chunk)
- del chunk
- result = result.sub_edges(old_edges, '|Z').fillet(tool_radius).sub_edges(old_edges, '#Z and >Z').chamfer(1)
- old_edges = result.edges()
- # Screen
- result = result.pushPoints([(0, 61.5)]).rect(100, 75).cutThruAll() \
- .sub_edges(old_edges, '|Z').fillet(tool_radius) \
- .sub_edges(old_edges, '#Z exc <Z').chamfer(thickness - 1)
- old_edges = result.edges()
- del old_edges
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement