Skip to content

Instantly share code, notes, and snippets.

@0xff07
Last active January 4, 2019 03:07
Show Gist options
  • Save 0xff07/a0883afca629cf68bde1f02495390519 to your computer and use it in GitHub Desktop.
Save 0xff07/a0883afca629cf68bde1f02495390519 to your computer and use it in GitHub Desktop.
Simple AutoLisp script to draw some common LEGO parts.
(defun c:LEGO ( / name)
(if (setq name (getstring "Enter component Name (Brick/Plate/round_brick/round_plate)"))
(if (or (eq name "plate") (eq name "P"))(draw_plate)
(if (or (eq name "brick") (eq name "B"))(draw_brick)
(if (or (eq name "round_plate") (eq name "RP"))(draw_round_plate)
(if (or (eq name "round_cone") (eq name "RC"))(draw_round_cone)
(if (or (eq name "round_brick") (eq name "RB"))(draw_round_brick))))))
);end if
)
(defun draw_brick ( / P)
(graphscr)
(setq P (getpoint "choose the starting point"))
(command "osmode" 16383)
(setq M (getint "Enter Row Number"))
(setq N (getint "Enter Col Number"))
(command "osnap" "off")
(__brick_like P M N 9.6)
(command "osmode" 16383)
)
(defun draw_plate ( / P)
(graphscr)
(command "osmode" 16383)
(setq P (getpoint "choose the starting point"))
(setq M (getint "Enter Row Number"))
(setq N (getint "Enter Col Number"))
(command "osnap" "off")
(__brick_like P M N 3.2)
(command "osmode" 16383)
)
(defun draw_round_brick ( / P)
(graphscr)
(command "osmode" 16383)
(setq P (getpoint "choose the starting point"))
(command "osnap" "off")
(__round_brick P 7.9 3.9 T)
(command "osmode" 16383)
)
(defun draw_round_plate ( / P)
(graphscr)
(setq P (getpoint "choose the starting point"))
(command "osmode" 16383)
(command "osnap" "off")
(__round_brick P 1.2 3.9 NIL)
(command "osmode" 16383)
)
(defun draw_round_cone ( / P)
(graphscr)
(command "osmode" 16383)
(setq P (getpoint "choose the starting point"))
(command "osnap" "off")
(__round_brick P 8.0 2.5 T)
(command "osmode" 16383)
)
(defun __brick_like (P M N height / start x y P0 P1 Pc base tmp iter_x iter_y)
(setq x (- (nth 0 P) 4.0))
(setq y (+ (nth 1 P) 4.0))
(setq z (- (nth 2 P) 1.6))
(setq P0 (list x y z))
(setq P1 (list (+ x (* 8.0 N)) (- y (* 8.0 M)) z))
(setq Pc (list (+ x 4.0) (- y 4.0) (+ z height)))
(command "box" P0 P1 height "")
(setq base (entlast))
(setq iter_x (nth 0 Pc))
(setq iter_y (nth 1 Pc))
(setq iter_z (nth 2 Pc))
(repeat M
(repeat N
(command "cylinder" (list iter_x iter_y iter_z) 2.5 1.6 "")
(setq tmp (entlast))
(command "union" base tmp "")
(setq base (entlast))
(setq iter_x (+ iter_x 8.0))
);end repeat N
(setq iter_x (nth 0 Pc))
(setq iter_y (- iter_y 8.0))
);end repeat M
)
(defun __round_brick (P height top hollow / base tmp)
(setq x (nth 0 P))
(setq y (nth 1 P))
(setq z (- (nth 2 P) 1.6))
(command "cylinder" (list x y z) 3.2 1.6 "")
(setq base (entlast))
;(command "cylinder" (list x y (+ z 1.7)) 3.9 height "")
(command "cone" (list x y (+ z 1.6)) 4.0 "T" top height "")
(setq tmp (entlast))
(command "union" base tmp "")
(command "cylinder" (list x y (+ z (+ height 1.7))) 2.5 1.7 "")
(setq tmp (entlast))
(command "union" base tmp "")
(if hollow
(progn
(command "cylinder" (list x y z) 1.55 11.3 "")
(setq tmp (entlast))
(command "subtract" base "" tmp "")
);end progn
);end if hollow
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment