Sunday, October 11, 2015

Laser Box AutoLISP Code


This code can be used to generate a laser box with dove-tail edges.  It includes a kerf feature and should be used as in AutoCAD.  You need to know how to use AutoLISP macros to use this.

Sorry this is fairly long and I didn't put in many comments so hopefully you can make sense of it.  If not comment and I will explain further.

; Jig Saw Line X Outy
(defun jsbx1 (x1 x2 y1 n s krf)
  
  (setq n2 (* n 2))
  (setq dx (- x2 x1))
  (setq px (* (/ dx (+ n2 1)) 2.0))
  (setq tx (+ (/ px 2.0) krf))
  (setq gx (- (/ px 2.0) krf))

  (setq p1 (list x1 y1 0.0))

  (repeat n
    (list 
      (setq p2 (list (+ (car p1) tx)    y1    0))
      (setq p3 (list (+ (car p1) tx) (+ y1 s) 0))
      (setq p4 (list (+ (car p2) gx) (+ y1 s) 0))
      (setq p5 (list (+ (car p2) gx)    y1    0))
      (command "pline" p1 p2 p3 p4 p5 "")
      (setq p1 p5)
  ))
  
  (setq p2 (list (+ (car p1) tx) y1 0))
  
  (command "pline" p1 p2 "")
)

; Jig Saw Line Y Outy
(defun jsby1 (x1 y1 y2 n s krf)

  (setq n2 (* n 2))
  (setq dy (- y2 y1))
  (setq py (* (/ dy(+ n2 1)) 2.0))
  (setq ty (+ (/ py 2.0) krf))
  (setq gy (- (/ py 2.0) krf))

  (setq p1 (list x1 y1 0.0))

  (repeat n
    (list 
      (setq p2 (list    x1    (+ (cadr p1) ty) 0))
      (setq p3 (list (+ x1 s) (+ (cadr p1) ty) 0))
      (setq p4 (list (+ x1 s) (+ (cadr p2) gy) 0))
      (setq p5 (list    x1    (+ (cadr p2) gy) 0))
      (command "pline" p1 p2 p3 p4 p5 "")
      (setq p1 p5)
  ))
  
  (setq p2 (list x1 (+ (cadr p1) ty) 0))
  
  (command "pline" p1 p2 "")
)

; Jig Saw Line X Inny
(defun jsbx2 (x1 x2 y1 n s krf)
  
  (setq n2 (* n 2))
  (setq dx (- x2 x1))
  (setq px (* (/ dx (+ n2 1)) 2.0))
  (setq tx (+ (/ px 2.0) krf))
  (setq gx (- (/ px 2.0) krf))

  (setq p1 (list (+ x1 (abs s)) (+ y1 s) 0.0))
  (setq p2 (list (+ x1 gx krf) (+ y1 s) 0.0))
  (setq p3 (list (car p2) y1 0.0))
  (command "pline" p1 p2 p3 "")
  (setq p1 p3)
  
  (repeat (- n 1)
    (list
      (setq p2 (list (+ (car p1) tx)    y1    0))
      (setq p3 (list (+ (car p1) tx) (+ y1 s) 0))
      (setq p4 (list (+ (car p3) gx) (+ y1 s) 0))
      (setq p5 (list (+ (car p3) gx)    y1    0))
      (command "pline" p1 p2 p3 p4 p5 "")
      (setq p1 p5)
  ))
   
  (setq p2 (list (+ (car p1) tx)          y1    0))
  (setq p3 (list (+ (car p1) tx)       (+ y1 s) 0))
  (setq p4 (list (- (+ (car p3) gx krf) (abs s)) (+ y1 s) 0))
  
  (command "pline" p1 p2 p3 p4 "")
)

; Jig Saw Line Y Inny
(defun jsby2 (x1 y1 y2 n s krf)

  (setq n2 (* n 2))
  (setq dy (- y2 y1))
  (setq py (* (/ dy(+ n2 1)) 2.0))
  (setq ty (+ (/ py 2.0) krf))
  (setq gy (- (/ py 2.0) krf))
  
  (setq p1 (list (+ x1 s) (+ y1 (abs s)) 0.0))
  (setq p2 (list (+ x1 s ) (+ y1 gy krf) 0.0))
  (setq p3 (list x1 (cadr p2) 0.0))
  (command "pline" p1 p2 p3 "")
  (setq p1 p3)

  (repeat (- n 1)
    (list
      (setq p2 (list    x1    (+ (cadr p1) ty) 0))
      (setq p3 (list (+ x1 s) (+ (cadr p1) ty) 0))
      (setq p4 (list (+ x1 s) (+ (cadr p3) gy) 0))
      (setq p5 (list    x1    (+ (cadr p3) gy) 0))
      (command "pline" p1 p2 p3 p4 p5 "")
      (setq p1 p5)
  ))

  (setq p2 (list    x1    (+ (cadr p1) ty)    0))
  (setq p3 (list (+ x1 s) (+ (cadr p1) ty)    0))
  (setq p4 (list (+ x1 s) (- (+ (cadr p3) gy krf) (abs s)) 0))
  (command "pline" p1 p2 p3 p4 "")
)

; Jig Saw Line X Mixed
(defun jsbx3 (x1 x2 y1 n s krf)
  
  (setq n2 (* n 2))
  (setq dx (- x2 x1))
  (setq px (* (/ dx (+ n2 1)) 2.0))
  (setq tx (+ (/ px 2.0) krf))
  (setq gx (- (/ px 2.0) krf))

  (setq p1 (list (+ x1 (abs s))              y1 0.0))
  (setq p2 (list (- (+ (car p1) tx) (abs s)) y1 0.0))
  
  (command "pline" p1 p2 "")
  (setq p1 p2)
  
  (repeat (- n 1)
    (list

      (setq p2 (list    (car p1)     (+ y1 s) 0))
      (setq p3 (list (+ (car p1) gx) (+ y1 s) 0))
      (setq p4 (list (+ (car p1) gx)    y1    0))
      (setq p5 (list (+ (car p3) tx)    y1    0))

      (command "pline" p1 p2 p3 p4 p5 "")
      (setq p1 p5)
  ))
   
  (setq p2 (list       (car p1)           (+ y1 s) 0))
  (setq p3 (list    (+ (car p1) gx)       (+ y1 s) 0))
  (setq p4 (list    (+ (car p1) gx)          y1    0))
  (setq p5 (list (- (+ (car p3) tx) (abs s)) y1    0))
  (command "pline" p1 p2 p3 p4 p5 "")
)

; Jig Saw Line Y Mixed
(defun jsby3 (x1 y1 y2 n s krf)

  (setq n2 (* n 2))
  (setq dy (- y2 y1))
  (setq py (* (/ dy(+ n2 1)) 2.0))
  (setq ty (+ (/ py 2.0) krf))
  (setq gy (- (/ py 2.0) krf))
  
  (setq p1 (list (+ x1 s) y1   0.0))
  (setq p2 (list (car p1) (+ y1 gy krf) 0.0))
  
  (command "pline" p1 p2 "")
  (setq p1 p2)

  (repeat (- n 1)
    (list
      
      (setq p2 (list    x1    (cadr p1) 0))
      (setq p3 (list    x1    (+ (cadr p1) ty) 0))
      (setq p4 (list (+ x1 s) (+ (cadr p1) ty) 0))
      (setq p5 (list (+ x1 s) (+ (cadr p3) gy) 0))
      
      (command "pline" p1 p2 p3 p4 p5 "")
      (setq p1 p5)
  ))

  (setq p2 (list    x1       (cadr p1)         0))
  (setq p3 (list    x1    (+ (cadr p1) ty)     0))
  (setq p4 (list (+ x1 s)    (cadr p3)         0))
  (setq p5 (list (+ x1 s) (+ (cadr p3) gy krf) 0))
  (command "pline" p1 p2 p3 p4 p5 "")
)

;(jsb 4.0 6.0 5 0.11 0.01)
;Outy
(defun jsb1 (x1 x2 y1 y2 nx ny s krf)
   (jsbx1 x1 x2 y1    nx    s  krf)
   (jsby1 x1 y1    y2      ny    s  krf)
   (jsbx1 x1 x2 (+ y2 krf)   nx (- s) krf)
   (jsby1 (+ x2 krf) y1  y2  ny (- s) krf)
)

;(jsb 4.0 6.0 5 0.11 0.01)
;Inny
(defun jsb2 (x1 x2 y1 y2 nx ny s krf)
   (jsbx2 x1 x2      y1    nx    s  krf)
   (jsby2 x1 y1    y2      ny    s  krf)
   (jsbx2 x1 x2 (+ y2 krf)   nx (- s) krf)
   (jsby2 (+ x2 krf) y1  y2  ny (- s) krf)
)

;(jsb 4.0 6.0 5 0.11 0.01)
;Mixed
(defun jsb3 (x1 x2 y1 y2 nx ny s krf)
   (jsbx3 x1 x2   y1  nx    s  krf)
   (jsbx3 x1 x2   (+ y2 krf) nx (- s) krf)
   (jsby3 x1 y1 y2    ny    s  krf)
   (jsby3 (+ x2 krf) y1    y2      ny (- s) krf)
)

(defun jsb (x y z nx ny nz s krf)
  (jsb1 0.0 x 0.0 y nx ny s krf)
  (jsb1 (+ x 0.5 s) (+ x x 0.5 s) 0.0 y nx ny s krf)
  (jsb2 (+ (* x 2) 1.0 s) (+ (* x 3) 1.0 s) 0.0 z nx nz s krf)
  (jsb2 (+ (* x 3) 1.5 s) (+ (* x 4) 1.5 s) 0.0 z nx nz s krf)
  (jsb3 (+ (* x 4) 2.0 s) (+ (* x 4) z 2.0 s) 0.0 y nz ny s krf)
  (jsb3 (+ (* x 4) z 2.5 s) (+ (* x 4) z z 2.5 s) 0.0 y nz ny s krf)
)

The main routine is jsb.  At the AutoCAD command line you would type:

(jsb 2.0 3.0 4.0 2 3 4 0.125 0.01)

to generate a box 2"x3"x4" with 2 tabs, 3 tabs, and 4 tabs respectively for 1/8" wood with a laser width (kerf) of 1/100th of an inch.  Don't forget to turn off object snap (f3) (and other snaps).  You should get something like the following: