;; CADLisp Tools - Equipment Tag Numbering
(vl-load-com)

(defun ct-eq:locked-layer-p (name / flags row)
  (and (setq row (tblsearch "LAYER" name))
       (setq flags (cdr (assoc 70 row)))
       (/= 0 (logand flags 20))))

(defun ct-eq:xref-p (data / block flags)
  (and data
       (setq block (tblsearch "BLOCK" (cdr (assoc 2 data))))
       (setq flags (cdr (assoc 70 block)))
       (/= 0 (logand flags 12))))

(defun ct-eq:attributes (obj / result)
  (setq result (vl-catch-all-apply 'vlax-invoke (list obj 'GetAttributes)))
  (if (vl-catch-all-error-p result) nil result))

(defun c:EQTAGNO (/ *error* att attrs data doc ent failed found i number obj prefix
                    skipped ss step tag undo-started updated write-result wrote)
  (defun *error* (message)
    (if (and undo-started doc)
      (vl-catch-all-apply 'vla-EndUndoMark (list doc)))
    (if (and message (not (wcmatch (strcase message) "*CANCEL*,*QUIT*,*EXIT*")))
      (prompt (strcat "\nEQTAGNO error: " message)))
    (princ))
  (setq tag (strcase (getstring T "\nAttribute tag to update: ")))
  (if (= tag "")
    (prompt "\nAttribute tag cannot be blank.")
    (progn
      (setq prefix (getstring T "\nTag prefix: ")
            number (getint "\nStarting number <1>: "))
      (if (null number) (setq number 1))
      (initget 2)
      (setq step (getint "\nIncrement (non-zero) <1>: "))
      (if (null step) (setq step 1))
      (prompt "\nSelect attributed equipment blocks in sequence: ")
      (if (setq ss (ssget '((0 . "INSERT") (66 . 1))))
        (progn
          (setq doc (vla-get-ActiveDocument (vlax-get-acad-object))
                undo-started (not (vl-catch-all-error-p
                                   (vl-catch-all-apply 'vla-StartUndoMark (list doc))))
                i 0 updated 0 skipped 0 failed 0)
          (repeat (sslength ss)
            (setq ent (ssname ss i) data (and ent (entget ent)) found nil wrote nil)
            (if (and data
                     (not (ct-eq:xref-p data))
                     (not (ct-eq:locked-layer-p (cdr (assoc 8 data))))
                     (not (vl-catch-all-error-p
                            (setq obj (vl-catch-all-apply 'vlax-ename->vla-object (list ent))))))
              (progn
                (setq attrs (ct-eq:attributes obj))
                (foreach att attrs
                  (if (= (strcase (vla-get-TagString att)) tag)
                    (progn
                      (setq found T
                            write-result
                              (vl-catch-all-apply 'vla-put-TextString
                                (list att (strcat prefix (itoa number)))))
                      (if (vl-catch-all-error-p write-result)
                        (setq failed (1+ failed))
                        (setq wrote T)))))))
            (if wrote
              (setq updated (1+ updated) number (+ number step))
              (setq skipped (1+ skipped)))
            (setq i (1+ i)))
          (if undo-started
            (progn (vla-EndUndoMark doc) (setq undo-started nil)))
          (prompt (strcat "\nUpdated " (itoa updated) "; skipped " (itoa skipped)
                          "; write failures " (itoa failed) ".")))
        (prompt "\nNo attributed blocks selected."))))
  (princ))
