Skip to content

Instantly share code, notes, and snippets.

@jmwright
Last active December 5, 2023 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmwright/b1f4e3cbcb2152a240b03a8779a202b4 to your computer and use it in GitHub Desktop.
Save jmwright/b1f4e3cbcb2152a240b03a8779a202b4 to your computer and use it in GitHub Desktop.
Example of Creating a CadQuery Object With the Hylang Lisp Dialect
#!/usr/bin/env hy
; A port of the CadQuery example here: https://github.com/CadQuery/cadquery/blob/master/examples/Ex100_Lego_Brick.py
; Information about the Hylang Lisp dialect: http://hylang.org/
(import cadquery :as cq)
(import cadquery.vis [show])
; User parameters
(setv lbumps 2) ; number of bumps long
(setv wbumps 4) ; number of bumps wide
(setv thin True) ; True for thin, False for thick
; Parameters that make a Lego brick a Lego brick
(setv pitch 8.0)
(setv clearance 0.1)
(setv bumpDiam 4.8)
(setv bumpHeight 1.8)
; Whether or not the user wants a thick brick
(if thin
(setv height 3.2)
(setv height 9.6))
; Derived parameters
(setv t
(% (- pitch (- (* 2.0 clearance) bumpDiam)) 2.0))
(setv postDiam
(- pitch t)) ; works out to 6.5
(setv total_length
(- (* lbumps pitch) (* 2.0 clearance)))
(setv total_width
(- (* wbumps pitch) (* 2.0 clearance)))
; Define the overall shape of the brick
(setv brick (cq.Workplane "XY"))
(setv brick (brick.box total_length total_width height))
; Shell inwards not outwards
(setv brick (brick.faces "<Z"))
(setv brick (brick.shell (* -1.0 t)))
; make the bumps on the top
(setv brick (.workplane (brick.faces ">Z")))
(setv brick (brick.rarray pitch pitch lbumps wbumps True))
(setv brick (brick.circle (/ bumpDiam 2.0)))
(setv brick (brick.extrude bumpHeight))
(defn multibumps [brick]
"Handles adding the tubes to a multi-bump brick"
(setv brick (.workplane (brick.faces "<Z") :invert True))
(setv brick (brick.rarray pitch pitch (- lbumps 1) (- wbumps 1) :center True))
(setv brick (.circle (brick.circle (/ postDiam 2.0)) (/ bumpDiam 2.0)))
(setv brick (brick.extrude (- height t)))
(return brick)
)
(defn lenbumps [brick]
"Handles adding studs to length >1 bricks with width 1"
(setv brick (.workplane (brick.faces "<Z") :invert True))
(setv brick (brick.rarray pitch pitch (- lbumps 1) 1 :center True))
(setv brick (.extrude (brick.circle t) (- height t)))
(return brick)
)
(defn widbumps [brick]
"Handles adding studs to width >1 bricks with length 1"
(setv brick (.workplane (brick.faces "<Z") :invert True))
(setv brick (brick.rarray pitch pitch 1 (- wbumps 1) :center True))
(setv brick (.extrude (brick.circle t) (- height t)))
(return brick)
)
; Add posts on the bottom. posts are different diameter depending on geometry
; Solid studs for 1 bump, tubes for multiple, none for 1x1
(cond
(and (> lbumps 1) (> wbumps 1)) (setv brick (multibumps brick))
(> lbumps 1) (setv brick (lenbumps brick))
(> wbumps 1) (setv brick (widbumps brick))
)
(show brick)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment