;;; ------------------------------------------------------------
;;; AutoCATS ¸®½À ÀÚ·á½Ç
;; ¸í·É: XYLABEL
;; ±â´É: Å¬¸¯ÇÑ Á¡ÀÇ ÇöÀç UCS X,Y ÁÂÇ¥¸¦ ¿¬¼ÓÀ¸·Î ¹®ÀÚ Ç¥±â
;; ÃâÃ³: https://www.autocats.co.kr/lisp/coordinate-label
;; ¹öÀü: 1.0 (2026-07-23)
;; ¶óÀÌ¼±½º: ÀÚÀ¯ »ç¿ë¡¤¼öÁ¤, Àç¹èÆ÷ ½Ã ÃâÃ³(autocats.co.kr) Ç¥±â
;; ¾ÈÀü: *error* ÇÚµé·¯ + UNDO ±×·ì, ±âÁ¸ °´Ã¼ ¼öÁ¤ ¾øÀ½
;;; ------------------------------------------------------------

(vl-load-com)

(defun autocats-xylabel-string (point precision)
  (strcat "X=" (rtos (car point) 2 precision)
          ", Y=" (rtos (cadr point) 2 precision)))

(defun autocats-xylabel-create (point precision height / insertion)
  (setq insertion
        (trans
          (list (car point) (+ (cadr point) (* 1.5 height)) (caddr point))
          1 0))
  (entmakex
    (list
      (cons 0 "TEXT")
      (cons 10 insertion)
      (cons 40 height)
      (cons 1 (autocats-xylabel-string point precision))
      (cons 7 (getvar "TEXTSTYLE")))))

(defun c:XYLABEL (/ *error* doc undo-open precision height point count)
  (defun *error* (msg)
    (if undo-open
      (progn (vla-EndUndoMark doc) (setq undo-open nil)))
    (if (and msg (not (wcmatch (strcase msg) "*CANCEL*,*QUIT*,*EXIT*,*Ãë¼Ò*")))
      (princ (strcat "\n[XYLABEL] Áß´Ü: " msg)))
    (princ))

  (setq precision (getint (strcat "\n[XYLABEL] ¼Ò¼ö ÀÚ¸´¼ö <" (itoa (getvar "DIMDEC")) ">: ")))
  (if (or (null precision) (< precision 0) (> precision 8))
    (setq precision (getvar "DIMDEC")))
  (setq height (getreal (strcat "\n¹®ÀÚ ³ôÀÌ <" (rtos (getvar "TEXTSIZE") 2 precision) ">: ")))
  (if (or (null height) (<= height 0.0)) (setq height (getvar "TEXTSIZE")))
  (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))
        count 0)
  (vla-StartUndoMark doc)
  (setq undo-open T)
  (while (setq point (getpoint "\nÁÂÇ¥¸¦ Ç¥±âÇÒ Á¡ Å¬¸¯ (Enter Á¾·á): "))
    (if (autocats-xylabel-create point precision height)
      (setq count (1+ count))))
  (vla-EndUndoMark doc)
  (setq undo-open nil)
  (princ (strcat "\n[XYLABEL] " (itoa count) "°³ ÁÂÇ¥ Ç¥±â ¿Ï·á."))
  (princ))

(princ "\n[AutoCATS] XYLABEL ·Îµå ¿Ï·á - ¸í·ÉÇà¿¡ XYLABEL ¸¦ ÀÔ·ÂÇÏ¼¼¿ä.")
(princ)
