;;; ------------------------------------------------------------
;;; AutoCATS ¸®½À ÀÚ·á½Ç
;; ¸í·É: NINC
;; ±â´É: ±âÁØ ¹®ÀÚÀÇ ³¡ ¹øÈ£¸¦ Áõ°¡½ÃÅ°¸ç Å¬¸¯ À§Ä¡¿¡ ¿¬¼Ó º¹»ç (P-001 ¡æ P-002 ¡¦)
;; ÃâÃ³: https://www.autocats.co.kr/lisp/number-increment
;; ¹öÀü: 1.0 (2026-07-20)
;; ¶óÀÌ¼±½º: ÀÚÀ¯ »ç¿ë¡¤¼öÁ¤, Àç¹èÆ÷ ½Ã ÃâÃ³(autocats.co.kr) Ç¥±â
;; ¾ÈÀü: *error* ÇÚµé·¯ + UNDO ±×·ì(ÇÑ ¹øÀÇ U·Î ÀüÃ¼ Ãë¼Ò), ÆÄ±«Àû µ¿ÀÛ ¾øÀ½
;;; ------------------------------------------------------------

(defun c:NINC (/ *error* ninc-split ninc-pad oldecho undo-open sel ent data raw
                 pair head tail num width step idx pt newstr newdata)

  (defun *error* (msg)
    (if undo-open
      (progn (command "_.UNDO" "_End") (setq undo-open nil)))
    (if oldecho (setvar "CMDECHO" oldecho))
    (if (and msg (not (wcmatch (strcase msg) "*CANCEL*,*QUIT*,*EXIT*,*Ãë¼Ò*")))
      (princ (strcat "\n[NINC] Áß´Ü: " msg)))
    (princ))

  ;; ¹®ÀÚ ³¡ÀÇ ¼ýÀÚ¸¦ ºÐ¸®ÇÑ´Ù: "P-001" ¡æ ("P-" . "001"), "12" ¡æ ("" . "12")
  (defun ninc-split (s / i)
    (setq i (strlen s))
    (while (and (> i 0) (wcmatch (substr s i 1) "#"))
      (setq i (1- i)))
    (cons (substr s 1 i) (substr s (1+ i))))

  ;; ÀÚ¸®¼ö¸¦ À¯ÁöÇÏ¸ç 0À» Ã¤¿î´Ù: (ninc-pad 2 3) ¡æ "002"
  (defun ninc-pad (n w / s)
    (setq s (itoa n))
    (while (< (strlen s) w) (setq s (strcat "0" s)))
    s)

  (setq sel (entsel "\n[NINC] ±âÁØÀÌ µÉ ¹øÈ£ ¹®ÀÚ(TEXT)¸¦ ¼±ÅÃÇÏ¼¼¿ä: "))
  (setq ent (if sel (car sel) nil))
  (if (and ent (= (cdr (assoc 0 (entget ent))) "TEXT"))
    (progn
      (setq data (entget ent)
            raw  (cdr (assoc 1 data))
            pair (ninc-split raw)
            head (car pair)
            tail (cdr pair))
      (if (= tail "")
        (princ "\n[NINC] ¹®ÀÚ ³¡¿¡ ¼ýÀÚ°¡ ¾ø½À´Ï´Ù. ¿¹: P-001, A12, 15")
        (progn
          (setq num (atoi tail) width (strlen tail))
          (setq step (getint "\nÁõ°¡ °£°Ý (À½¼ö °¡´É) <1>: "))
          (if (or (null step) (= step 0)) (setq step 1))
          (setq oldecho (getvar "CMDECHO"))
          (setvar "CMDECHO" 0)
          (command "_.UNDO" "_Begin")
          (setq undo-open T idx 0)
          (while (setq pt (getpoint "\n´ÙÀ½ ¹øÈ£¸¦ ³õÀ» À§Ä¡ Å¬¸¯ (Enter Á¾·á): "))
            (setq idx    (1+ idx)
                  newstr (strcat head (ninc-pad (+ num (* idx step)) width))
                  newdata (subst (cons 1 newstr) (assoc 1 data) data)
                  newdata (subst (cons 10 pt) (assoc 10 newdata) newdata))
            (if (assoc 11 newdata)
              (setq newdata (subst (cons 11 pt) (assoc 11 newdata) newdata)))
            (entmake (vl-remove-if '(lambda (x) (member (car x) '(-1 5 330))) newdata))
            (princ (strcat "  ¡æ " newstr)))
          (command "_.UNDO" "_End")
          (setq undo-open nil)
          (setvar "CMDECHO" oldecho)
          (princ (strcat "\n[NINC] " (itoa idx) "°³ »ý¼º ¿Ï·á. (U ÇÑ ¹øÀ¸·Î ÀüÃ¼ Ãë¼Ò °¡´É)")))))
    (princ "\n[NINC] ÇÑ ÁÙ ¹®ÀÚ(TEXT)¸¦ ¼±ÅÃÇØ¾ß ÇÕ´Ï´Ù. (MTEXT´Â Áö¿øÇÏÁö ¾Ê½À´Ï´Ù)"))
  (princ))

(princ "\n[AutoCATS] NINC ·Îµå ¿Ï·á - ¸í·ÉÇà¿¡ NINC ¸¦ ÀÔ·ÂÇÏ¼¼¿ä.")
(princ)
