;;; ------------------------------------------------------------
;;; AutoCATS ¸®½À ÀÚ·á½Ç
;; ¸í·É: LSUM
;; ±â´É: ¼±ÅÃÇÑ ¼±¡¤Æú¸®¼±¡¤È£¡¤¿ø¡¤½ºÇÃ¶óÀÎÀÇ ±æÀÌ ÇÕ°è °è»ê + µµ¸é ±âÀÔ(¼±ÅÃ)
;; ÃâÃ³: https://www.autocats.co.kr/lisp/length-sum
;; ¹öÀü: 1.0 (2026-07-20)
;; ¶óÀÌ¼±½º: ÀÚÀ¯ »ç¿ë¡¤¼öÁ¤, Àç¹èÆ÷ ½Ã ÃâÃ³(autocats.co.kr) Ç¥±â
;; ¾ÈÀü: *error* ÇÚµé·¯ Æ÷ÇÔ, µµ¸é ÀúÀå¡¤Á¾·á µî ÆÄ±«Àû µ¿ÀÛ ¾øÀ½
;;; ------------------------------------------------------------

(defun c:LSUM (/ *error* ss idx ent len res sum cnt skip pt)

  (defun *error* (msg)
    (if (and msg (not (wcmatch (strcase msg) "*CANCEL*,*QUIT*,*EXIT*,*Ãë¼Ò*")))
      (princ (strcat "\n[LSUM] Áß´Ü: " msg)))
    (princ))

  (vl-load-com)
  (princ "\n[LSUM] ±æÀÌ¸¦ ÇÕ»êÇÒ °´Ã¼(¼±¡¤Æú¸®¼±¡¤È£¡¤¿ø¡¤½ºÇÃ¶óÀÎ¡¤Å¸¿ø)¸¦ ¼±ÅÃÇÏ¼¼¿ä.")
  (setq ss (ssget '((0 . "LINE,LWPOLYLINE,POLYLINE,ARC,CIRCLE,SPLINE,ELLIPSE"))))
  (if ss
    (progn
      (setq idx 0 sum 0.0 cnt 0 skip 0)
      (while (< idx (sslength ss))
        (setq ent (ssname ss idx)
              res (vl-catch-all-apply
                    '(lambda ()
                       (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent)))))
        (if (or (vl-catch-all-error-p res) (null res))
          (setq skip (1+ skip))
          (setq sum (+ sum res) cnt (1+ cnt)))
        (setq idx (1+ idx)))
      (princ (strcat "\n[LSUM] ±æÀÌ ÇÕ°è: " (rtos sum 2 4)
                     "  (ÇÕ»ê " (itoa cnt) "°³ / °Ç³Ê¶Ü " (itoa skip) "°³, ÇöÀç µµ¸é ´ÜÀ§ ±âÁØ)"))
      (if (> cnt 0)
        (progn
          (initget "Y N")
          (if (= (getkword "\nÇÕ°è¸¦ µµ¸é¿¡ ¹®ÀÚ·Î ±âÀÔÇÒ±î¿ä? [¿¹(Y)/¾Æ´Ï¿À(N)] <N>: ") "Y")
            (progn
              (setq pt (getpoint "\n¹®ÀÚ »ðÀÔ À§Ä¡ Å¬¸¯: "))
              (if pt
                (entmake (list (cons 0 "TEXT")
                               (cons 10 pt)
                               (cons 40 (getvar "TEXTSIZE"))
                               (cons 1 (rtos sum 2 4))
                               (cons 7 (getvar "TEXTSTYLE")))))))))
      )
    (princ "\n[LSUM] ¼±ÅÃµÈ °´Ã¼°¡ ¾ø½À´Ï´Ù."))
  (princ))

(princ "\n[AutoCATS] LSUM ·Îµå ¿Ï·á - ¸í·ÉÇà¿¡ LSUM À» ÀÔ·ÂÇÏ¼¼¿ä.")
(princ)
