;;; ------------------------------------------------------------
;;; AutoCATS ¸®½À ÀÚ·á½Ç
;; ¸í·É: LLABEL
;; ±â´É: ¼±ÅÃÇÑ °î¼± ÁßÁ¡¿¡ ±æÀÌ¸¦ Á¢¼± ¹æÇâ ¹®ÀÚ·Î Ç¥±â
;; ÃâÃ³: https://www.autocats.co.kr/lisp/length-label
;; ¹öÀü: 1.0 (2026-07-23)
;; ¶óÀÌ¼±½º: ÀÚÀ¯ »ç¿ë¡¤¼öÁ¤, Àç¹èÆ÷ ½Ã ÃâÃ³(autocats.co.kr) Ç¥±â
;; ¾ÈÀü: *error* ÇÚµé·¯ + UNDO ±×·ì, ¿øº» °î¼± ¼öÁ¤ ¾øÀ½
;;; ------------------------------------------------------------

(vl-load-com)

(defun autocats-llabel-length (ent / result)
  (setq result
        (vl-catch-all-apply
          '(lambda ()
             (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent)))))
  (if (vl-catch-all-error-p result) nil result))

(defun autocats-llabel-readable-angle (radians)
  (if (and (> radians (/ pi 2.0)) (< radians (* 1.5 pi)))
    (+ radians pi)
    radians))

(defun autocats-llabel-create (ent precision height / length distance point param deriv angle result)
  (setq length (autocats-llabel-length ent))
  (if length
    (progn
      (setq distance (/ length 2.0)
            point (vlax-curve-getPointAtDist ent distance)
            param (vlax-curve-getParamAtPoint ent point)
            deriv (vlax-curve-getFirstDeriv ent param)
            angle (autocats-llabel-readable-angle
                    (atan (cadr deriv) (car deriv)))
            result
            (entmakex
              (list
                (cons 0 "TEXT")
                (cons 10 point)
                (cons 40 height)
                (cons 1 (rtos length 2 precision))
                (cons 7 (getvar "TEXTSTYLE"))
                (cons 50 angle))))
      result)
    nil))

(defun c:LLABEL (/ *error* doc undo-open ss idx ent precision height created skipped result)
  (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[LLABEL] Áß´Ü: " msg)))
    (princ))

  (setq precision (getint (strcat "\n[LLABEL] ¼Ò¼ö ÀÚ¸´¼ö <" (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")))
  (princ "\n[LLABEL] ±æÀÌ¸¦ Ç¥±âÇÒ °î¼±À» ¼±ÅÃÇÏ¼¼¿ä.")
  (setq ss (ssget '((0 . "LINE,LWPOLYLINE,POLYLINE,ARC,CIRCLE,SPLINE,ELLIPSE"))))
  (if ss
    (progn
      (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))
            idx 0 created 0 skipped 0)
      (vla-StartUndoMark doc)
      (setq undo-open T)
      (while (< idx (sslength ss))
        (setq ent (ssname ss idx)
              result
              (vl-catch-all-apply
                'autocats-llabel-create
                (list ent precision height)))
        (if (or (vl-catch-all-error-p result) (null result))
          (setq skipped (1+ skipped))
          (setq created (1+ created)))
        (setq idx (1+ idx)))
      (vla-EndUndoMark doc)
      (setq undo-open nil)
      (princ (strcat "\n[LLABEL] ¿Ï·á: " (itoa created)
                     "°³ Ç¥±â / " (itoa skipped) "°³ °Ç³Ê¶Ü")))
    (princ "\n[LLABEL] ¼±ÅÃµÈ °î¼±ÀÌ ¾ø½À´Ï´Ù."))
  (princ))

(princ "\n[AutoCATS] LLABEL ·Îµå ¿Ï·á - ¸í·ÉÇà¿¡ LLABEL ¸¦ ÀÔ·ÂÇÏ¼¼¿ä.")
(princ)
