; AttX changes selected Block Attrib matching Tag value to X ; OP: ; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/block-attribute-reset-lisp/m-p/12959096#M470223 (defun c:AttX (/ GetTags SelectTag PickTag ss blkname tags tag newval i blkEnt ent attdata tagFound) ;; Function to get attribute tags from a block definition ; modified to return a string of tags separated by space (defun GetTags (blockname / blk obj taglist) ; (setq taglist '()) (setq taglist "") (if (setq blk (tblobjname "BLOCK" blockname)) (progn (setq obj (entnext blk)) (while (and obj (/= (cdr (assoc 0 (entget obj))) "ENDBLK")) (if (eq (cdr (assoc 0 (entget obj))) "ATTDEF") ; (setq taglist (cons (cdr (assoc 2 (entget obj))) taglist)) (setq taglist (strcat taglist " " (cdr (assoc 2 (entget obj))))) ) (setq obj (entnext obj)) ) ) ) (if taglist (substr taglist 2)) ) ;; Function to prompt the user to select an attribute tag from a list (defun SelectTag (taglist / selectedtag i options) (if taglist (progn (setq i 0) (setq options "") (foreach tag taglist (setq options (strcat options (itoa i) ". " tag "\n")) (setq i (1+ i)) ) (setq selectedtag (nth (getint (strcat "\nEnter the number corresponding to the tag:\n" options)) taglist)) ) ) selectedtag ) ;; function to prompt for selection by picking from cursor ; taglist must be a string of choices separated by space (defun PickTag (taglist / dynmode selected options) ; dynamic input with selection options ; Also when you set dynmode to 1 or 3 during the getkword prompt ; when the cursor moves from the command prompt into the drawing area ; a cursor menu appears to choose from (if(<= (rem (getvar"dynmode") 2) 0) ; if even # chks negative value as well (progn (setq dynmode (getvar"dynmode")) ; save current setting (setvar "dynmode" 1) ; enable dynmode ) ) ; if (setq options (vl-string-translate " " "/" taglist)) ; replace space with slash (initget 1 taglist) (setq selectedtag (getkword (strcat"\nSelect One [" options "]: "))) (if dynmode (setvar "dynmode" dynmode)) ; restore original dynmode setting selectedtag ) ;; Prompt user to select blocks (setq ss (ssget '((0 . "INSERT")(66 . 1)))) ; select blocks with attribute ;; If blocks are selected, proceed (if ss (progn ;; Prompt user to select the tag to be reset (setq blk (ssname ss 0)) ;; Select the first block from the selection (setq blkname (cdr (assoc 2 (entget blk)))) (setq tags (GetTags blkname)) ; (setq tag (SelectTag tags)) (setq tag (PickTag tags)) ;; Prompt user to enter the new value for the attribute tag (setq newval (getstring "\nEnter the new value for the attribute: ")) ;; Iterate over the selected blocks and update the attribute (setq i 0) (while (< i (sslength ss)) (setq blkent (ssname ss i)) ;; Use a loop to go through all entities in the block reference (setq ent (entnext blkEnt)) ; Start with the first entity (setq tagFound nil) ; Initialize flag to check if tag is found (while ent ;; Check if the entity is an attribute and matches the selected tag (if (and (eq (cdr (assoc 0 (entget ent))) "ATTRIB") (equal (cdr (assoc 2 (entget ent))) tag)) (progn ;; Update the attribute value (setq attdata (entget ent)) (entmod (subst (cons 1 newval) (assoc 1 attdata) attdata)) (entupd ent) (setq tagFound t ent nil) ; Set flag if tag is updated ) (setq ent (entnext ent)) ; Move to the next entity ) ) ;; Check if the tag was not found in the block (if (not tagFound) (princ (strcat "\nTag " tag " not found in block " (cdr (assoc 2 (entget blkent)))))) (setq i (1+ i)) ) ) (princ "\nNo blocks selected.") ) (princ) ) (prompt "\n>>>...AttX.lsp is now loaded. Type 'AttX' to start ...<<<") (princ)