Jump to content

Adding prefix to chosen attributes in block


Pshepyoor

Recommended Posts

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:

obraz.thumb.png.d608318eb22ae3b67ccbe567c9a325bb.png

 

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 by Pshepyoor
Some changes and additions
Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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 by mhupp
updated code
  • Thanks 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by mhupp
Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...