;;; TORVA LISP - Lintel Beam (torva.kr/lisp.html)
;;; LTB : two corners -> closed rectangle on layer LINTEL (color 4, HIDDEN) + name text on LINTEL-NAME (color 4)
;;;       Wider than tall: name centred above the top edge. Taller than wide: name turned 90 deg, left of the left edge
;;; Options at the first corner: N name (default LB1), H text height
;;; Text height 3.5 x DIMSCALE (350 at 1:100), gap to the edge 1 x DIMSCALE (100 at 1:100). Current text style
;;; Name and height are kept until the drawing closes. Layers are created if missing. Works in any UCS. U undoes one run

(vl-load-com)

(if (null *TV-LINTEL-NAME*) (setq *TV-LINTEL-NAME* "LB1"))

(defun tv-lintel-ds () (if (> (getvar "DIMSCALE") 0) (getvar "DIMSCALE") 1.0))

;; text height: user value, else 3.5 x DIMSCALE
(defun tv-lintel-th () (cond (*TV-LINTEL-TH*) ((* 3.5 (tv-lintel-ds)))))

;; create the layer if missing; linetype from acad(iso).lin, Continuous if it will not load
(defun tv-lintel-layer (doc name col lt)
  (if (null (tblsearch "LAYER" name))
    (progn
      (if (and (/= (strcase lt) "CONTINUOUS") (null (tblsearch "LTYPE" lt)))
        (vl-catch-all-apply 'vla-Load
          (list (vla-get-Linetypes doc) lt (if (= 1 (getvar "MEASUREMENT")) "acadiso.lin" "acad.lin"))))
      (if (null (tblsearch "LTYPE" lt)) (setq lt "Continuous"))
      (entmake (list '(0 . "LAYER") '(100 . "AcDbSymbolTableRecord") '(100 . "AcDbLayerTableRecord")
                     (cons 2 name) '(70 . 0) (cons 62 col) (cons 6 lt))))))

;; first corner; N / H change settings, Enter = nil
(defun tv-lintel-start (/ p v)
  (setq p "N")
  (while (= (type p) 'STR)
    (initget "N H")
    (setq p (getpoint (strcat "\n첫 모서리 [이름(N)/글자 높이(H)] <" *TV-LINTEL-NAME* ", " (rtos (tv-lintel-th) 2 0) ">: ")))
    (cond
      ((= p "N")
       (setq v (getstring T (strcat "\n인방보 이름 <" *TV-LINTEL-NAME* ">: ")))
       (if (/= v "") (setq *TV-LINTEL-NAME* v)))
      ((= p "H")
       (initget 6)
       (if (setq v (getdist (strcat "\n글자 높이 <" (rtos (tv-lintel-th) 2 0) ">: "))) (setq *TV-LINTEL-TH* v)))))
  p)

(defun c:LTB (/ *error* old ce doc p1 p2 z nrm ua x1 y1 x2 y2 w h gap tp rot o)
  (setq ce (getvar "CMDECHO") old *error*
        doc (vla-get-ActiveDocument (vlax-get-acad-object)))
  (defun *error* (msg)
    (command "_.UNDO" "_E")
    (setvar "CMDECHO" ce)
    (setq *error* old)
    (if (not (wcmatch (strcase msg) "*CANCEL*,*QUIT*,*EXIT*,*BREAK*")) (princ (strcat "\nError: " msg)))
    (princ))
  (setvar "CMDECHO" 0)
  (command "_.UNDO" "_BE")
  (if (and (setq p1 (tv-lintel-start))
           (setq p2 (getcorner p1 "\n반대 구석: ")))
    (progn
      (tv-lintel-layer doc "LINTEL" 4 "HIDDEN")
      (tv-lintel-layer doc "LINTEL-NAME" 4 "Continuous")
      ;; points are UCS; entities take OCS of the UCS normal
      (setq z (caddr p1) nrm (trans '(0.0 0.0 1.0) 1 0 T)
            ua (angle '(0.0 0.0 0.0) (trans '(1.0 0.0 0.0) 1 nrm T))
            x1 (min (car p1) (car p2)) x2 (max (car p1) (car p2))
            y1 (min (cadr p1) (cadr p2)) y2 (max (cadr p1) (cadr p2))
            w (- x2 x1) h (- y2 y1) gap (tv-lintel-ds))
      (entmake (append (list '(0 . "LWPOLYLINE") '(100 . "AcDbEntity") '(8 . "LINTEL") '(100 . "AcDbPolyline")
                             '(90 . 4) '(70 . 1) (cons 38 (caddr (trans (list x1 y1 z) 1 nrm))))
                       (mapcar '(lambda (q) (setq o (trans (list (car q) (cadr q) z) 1 nrm)) (list 10 (car o) (cadr o)))
                               (list (list x1 y1) (list x2 y1) (list x2 y2) (list x1 y2)))
                       (list (cons 210 nrm))))
      (if (> w h)
        (setq tp (list (/ (+ x1 x2) 2.0) (+ y2 gap) z) rot 0.0)
        (setq tp (list (- x1 gap) (/ (+ y1 y2) 2.0) z) rot (/ pi 2.0)))
      (setq tp (trans tp 1 nrm))
      (entmake (list '(0 . "TEXT") '(100 . "AcDbEntity") '(8 . "LINTEL-NAME") '(100 . "AcDbText")
                     (cons 10 tp) (cons 11 tp) (cons 40 (tv-lintel-th)) (cons 1 *TV-LINTEL-NAME*)
                     (cons 50 (+ ua rot)) (cons 7 (getvar "TEXTSTYLE")) '(72 . 1) '(73 . 1) (cons 210 nrm)))
      (princ (strcat "\nLintel " *TV-LINTEL-NAME* " " (rtos (max w h) 2 0) " x " (rtos (min w h) 2 0)))))
  (command "_.UNDO" "_E")
  (setvar "CMDECHO" ce)
  (setq *error* old)
  (princ))

(princ "\nTORVA Lintel Beam: LTB | torva.kr")
(princ)

;;; ---- TVKEY: change a shortcut - shared by every TORVA LISP (torva.kr/lisp.html) ----
;;; TVKEY -> command -> new shortcut. The original command keeps working
;;; Shortcuts are saved on this PC as TORVA-KEYS and restored whenever a TORVA LISP loads
;;; (Every TORVA LISP carries this block; loading several just redefines the same functions)

;; "A1=PY;B2=CN;" -> (("A1" . "PY") ("B2" . "CN"))
(defun tv-key-list (/ s i out)
  (setq s (cond ((getenv "TORVA-KEYS")) ("")) out nil)
  (while (setq i (vl-string-search ";" s))
    (setq out (cons (substr s 1 i) out) s (substr s (+ i 2))))
  (if (/= s "") (setq out (cons s out)))
  (vl-remove nil
    (mapcar '(lambda (x / j) (if (setq j (vl-string-search "=" x)) (cons (substr x 1 j) (substr x (+ j 2)))))
            (reverse out))))

(defun tv-key-save (lst)
  (setenv "TORVA-KEYS" (apply 'strcat (mapcar '(lambda (p) (strcat (car p) "=" (cdr p) ";")) lst))))

;; is cmd a loaded LISP command
(defun tv-key-cmd-p (cmd)
  (member (type (eval (read (strcat "C:" cmd)))) '(SUBR USUBR EXRXSUBR)))

(defun tv-key-def (key cmd)
  (eval (list 'defun (read (strcat "C:" key)) '() (list (read (strcat "C:" cmd))) '(princ))))

;; is key an alias in acad.pgp ("L,   *LINE")
(defun tv-key-pgp-p (key / f h l hit)
  (if (setq f (findfile "acad.pgp"))
    (progn
      (setq h (open f "r"))
      (while (and (not hit) (setq l (read-line h)))
        (if (and (not (wcmatch l ";*")) (wcmatch (strcase l) (strcat key ",*"))) (setq hit T)))
      (close h)))
  hit)

;; reason the key cannot be used, nil if fine
(defun tv-key-bad (key lst)
  (cond
    ((not (vl-every '(lambda (c) (or (<= 48 c 57) (<= 65 c 90))) (vl-string->list key))) "영문·숫자만")
    ((getcname key) "AutoCAD 명령 이름")
    ((tv-key-pgp-p key) "acad.pgp 별칭과 같음")
    ((and (tv-key-cmd-p key) (not (assoc key lst))) "이미 있는 리습 명령")))

;; "My shortcuts" lines at the end of the file: (tv-my-key "A1" "BN"). Bad key -> reason only
(defun tv-my-key (key cmd / why)
  (setq key (strcase key) cmd (strcase cmd))
  (cond ((not (tv-key-cmd-p cmd)))
        ((setq why (tv-key-bad key (list (cons key cmd)))) (princ (strcat "\nShortcut " key ": " why)))
        (T (tv-key-def key cmd))))

(defun c:TVKEY (/ cmd key lst why)
  (setq lst (tv-key-list)
        cmd (strcase (getstring "\n단축키를 붙일 명령 [목록(?)]: ")))
  (cond
    ((= cmd ""))
    ((= cmd "?")
     (if lst
       (foreach p lst (princ (strcat "\n  " (car p) " = " (cdr p))))
       (princ "\nNo shortcuts set")))
    ((not (tv-key-cmd-p cmd)) (princ (strcat "\n" cmd " is not a loaded LISP command")))
    (T
     (setq key (strcase (getstring (strcat "\n" cmd " 의 새 단축키 [지우기(-)]: "))))
     (cond
       ((= key ""))
       ((= key "-")
        (foreach p lst
          (if (= (cdr p) cmd) (set (read (strcat "C:" (car p))) nil)))
        (tv-key-save (vl-remove-if '(lambda (p) (= (cdr p) cmd)) lst))
        (princ (strcat "\nShortcut for " cmd " removed")))
       ((= key cmd))
       ((setq why (tv-key-bad key lst)) (princ (strcat "\n" key ": " why)))
       (T
        (tv-key-save (cons (cons key cmd) (vl-remove-if '(lambda (p) (= (car p) key)) lst)))
        (tv-key-def key cmd)
        (princ (strcat "\n" key " now runs " cmd))))))
  (princ))

;; on load: restore saved shortcuts whose command is loaded
(foreach p (tv-key-list)
  (if (tv-key-cmd-p (cdr p)) (tv-key-def (car p) (cdr p))))
(princ "\nTVKEY: change a shortcut | torva.kr")
(princ)

;;; ---- My shortcuts: lines set at torva.kr download land below. Edit or add lines to change them ----
