Pshepyoor Posted July 12, 2023 Posted July 12, 2023 (edited) Hello. I'm using Autocad LT 2024. I found a code, which I think will be (after some changes) perfect for my problem or at least I think so. (defun c:atrybuty ( / as el en i ss str typ ) (initget "Prefix Suffix") (setq typ (cond ((getkword "\nAdd Prefix or Suffix? [Prefix/Suffix] <Prefix>: ")) ("Prefix"))) (setq str (getstring t (strcat typ " to Add: "))) (if (setq ss (ssget '((0 . "INSERT") (66 . 1)))) (repeat (setq i (sslength ss)) (setq en (ssname ss (setq i (1- i)))) (while (eq "ATTRIB" (cdr (assoc 0 (setq el (entget (setq en (entnext en))))))) (setq as (cdr (assoc 1 el))) (if (eq "Prefix" typ) (if (not (wcmatch as (strcat str "*"))) (entmod (subst (cons 1 (strcat str as)) (assoc 1 el) el)) ) (if (not (wcmatch as (strcat "*" str))) (entmod (subst (cons 1 (strcat as str)) (assoc 1 el) el)) ) ) ) ) ) (princ) ) My block after editing looks like this: I need to add a prefix but only for attributes: WEJ_1, WEJ_2, WEJ_3, WEJ_4, WYJ_1, WYJ_2. But now this code adds prefix to every attribute after clicking on the block. Can anyone help me and upgrade this code so it will add prefix only to attributes which I mentioned? Best Regards Mikołaj Edited July 12, 2023 by Pshepyoor Some changes and additions Quote
BIGAL Posted July 13, 2023 Posted July 13, 2023 With out a block to check against, then some comments, inside the while you need to check is the attribute text containing WEJ or WYJ and if yes then do the entmod. Look at WCMATCH in lisp help. There are other methods also like change based on tag names, another is change based on attribute creation order, could be like change item 2,5,8,11,14,17. The advantage of this is no need to check for text. Quote
mhupp Posted July 13, 2023 Posted July 13, 2023 (edited) I'm assuming those are those are the attribute tag names. see if this works. (defun c:BlkUpdate ( / typ str blk) (vl-load-com) (initget "Prefix Suffix") (setq typ (cond ((getkword "\nAdd Prefix or Suffix? [<Prefix>/Suffix]: ")) ("Prefix"))) (setq str (getstring t (strcat typ " to Add: "))) (setq blk (car (entsel "\nSelect Block: "))) (foreach att '("WEJ_1" "WEJ_2" "WEJ_3" "WEJ_4" "WYJ_1" "WYJ_2") (if (and (eq typ "Prefix") (> (strlen (setq x (getpropertyvalue blk att)) 0)) (setpropertyvalue blk att (strcat str x)) (setpropertyvalue blk att (strcat x str)) ) ) (vla-Regen (vla-get-activedocument (vlax-get-acad-object)) acAllViewports) ) Edited July 13, 2023 by mhupp updated code 1 Quote
Pshepyoor Posted July 13, 2023 Author Posted July 13, 2023 2 hours ago, mhupp said: I'm assuming those are those are the attribute tag names. see if this works. (defun c:BlkUpdate ( / typ str blk) (vl-load-com) (initget "Prefix Suffix") (setq typ (cond ((getkword "\nAdd Prefix or Suffix? [<Prefix>/Suffix]: ")) ("Prefix"))) (setq str (getstring t (strcat typ " to Add: "))) (setq blk (car (entsel "\nSelect Block: "))) (foreach att '("WEJ_1" "WEJ_2" "WEJ_3" "WEJ_4" "WYJ_1" "WYJ_2") (if (eq typ "Prefix") (setpropertyvalue blk att (strcat str (getpropertyvalue blk att)))) (setpropertyvalue blk att (strcat (getpropertyvalue blk att) str))) ) ) (vla-Regen (vla-get-activedocument (vlax-get-acad-object)) acAllViewports) ) Thank you very much for your reply. It works but I had to remove "(setpropertyvalue blk att (strcat (getpropertyvalue blk att) str)))", because it also added suffix. Also is it possible to check if the attribute value is 0 and don't add prefix to the empty attribute? If not then then the code you've sent me is more than enough. Thank you very much. Quote
Pshepyoor Posted July 13, 2023 Author Posted July 13, 2023 3 hours ago, mhupp said: I'm assuming those are those are the attribute tag names. see if this works. (defun c:BlkUpdate ( / typ str blk) (vl-load-com) (initget "Prefix Suffix") (setq typ (cond ((getkword "\nAdd Prefix or Suffix? [<Prefix>/Suffix]: ")) ("Prefix"))) (setq str (getstring t (strcat typ " to Add: "))) (setq blk (car (entsel "\nSelect Block: "))) (foreach att '("WEJ_1" "WEJ_2" "WEJ_3" "WEJ_4" "WYJ_1" "WYJ_2") (if (eq typ "Prefix") (setpropertyvalue blk att (strcat str (getpropertyvalue blk att)))) (setpropertyvalue blk att (strcat (getpropertyvalue blk att) str))) ) ) (vla-Regen (vla-get-activedocument (vlax-get-acad-object)) acAllViewports) ) Also is there a possibility to use this function to all the blocks named for example "test_1" on the drawing? Quote
mhupp Posted July 13, 2023 Posted July 13, 2023 (edited) 16 hours ago, Pshepyoor said: ... because it also added suffix. Also is it possible to check if the attribute value is 0 and don't add prefix to the empty attribute? 16 hours ago, Pshepyoor said: Also is there a possibility to use this function to all the blocks named for example "test_1" on the drawing? Can't really test code because that function doesn't work with my BricsCAD. had to many ) will only add suffix if option is chosen. also ignores empty Attributes. And switched out the entsel with ssget all blocks named test_1. also added an undo option if you mis-typed. (defun c:BlkUpdate ( / typ str SS blk) (vl-load-com) (vla-startundomark (setq Drawing (vla-get-activedocument (vlax-get-acad-object)))) (initget "Prefix Suffix") (setq typ (cond ((getkword "\nAdd Prefix or Suffix? [<Prefix>/Suffix]: ")) ("Prefix"))) (setq str (getstring t (strcat typ " to Add: "))) ;(setq blk (car (entsel "\nSelect Block: "))) ;Select one block at a time. (setq SS (ssget "_X" '((0 . "INSERT") (2 . "test_1")))) ;select all blocks by name (foreach blk (mapcar 'cadr (ssnamex ss)) (foreach att '("WEJ_1" "WEJ_2" "WEJ_3" "WEJ_4" "WYJ_1" "WYJ_2") (if (and (eq typ "Prefix") (> (strlen (setq x (getpropertyvalue blk att))) 0)) (setpropertyvalue blk att (strcat str x)) (setpropertyvalue blk att (strcat x str)) ) ) ) (vla-endundomark Drawing) (vla-Regen Drawing acAllViewports) ) Edited July 13, 2023 by mhupp Quote
Pshepyoor Posted July 14, 2023 Author Posted July 14, 2023 8 hours ago, mhupp said: Can't really test code because that function doesn't work with my BricsCAD. had to many ) will only add suffix if option is chosen. also ignores empty Attributes. And switched out the entsel with ssget all blocks named test_1. also added an undo option if you mis-typed. Thank you for help and your hard work, but sadly after choosing what text to add as prefix and confirming it does nothing Quote
mhupp Posted July 15, 2023 Posted July 15, 2023 You asked for a lisp that would only work with tag names listed. that is what this line is doing. (foreach att '("WEJ_1" "WEJ_2" "WEJ_3" "WEJ_4" "WYJ_1" "WYJ_2") The block tag names have to match above for them to update any attribute. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.