Jump to content

The simplest update a attribute in a block many times


BIGAL

Recommended Posts

One of the most common requests here is always about updating attributes inside a block. Well I found myself with 15 blocks and needed to update 1 attribute the 5th one in all blocks. Yes I could pull apart a lisp I had but I thought quick and dirty as I could do it.

 

So opened up notepad typed this, copied and pasted to command line, pick pick pick etc all done changed the string and attribute number did a second change and hey all done. The only thing to know is attribute order starts at zero.

 

What I am saying some time we just want something real simple and right now and accept a error on exit.

 

Any other ideas ?

 

(defun c:xxxx ( / atts)
(while (setq atts (vlax-invoke (vlax-ename->vla-object (car (entsel "\nPick block")))'getattributes))
(vla-put-textstring (nth 4 atts ) "BIGAL")
))

 

Even title blocks in layouts I have my goto layout always loaded so

goto 2 xxxx pick

goto 3 xxxx pick

Link to comment
Share on other sites

Hi BIGAL,

Lee Mac explained here that its better to rely on the attribute's tag name, rather than its position inside the block definiton, hence:

(setq tag "MyAttributeTag")
(setq val "MyNewAttributeVal")
(setq tag (strcase tag))
(setq atts (vlax-invoke (vlax-ename->vla-object (car (entsel "\nPick block"))) 'GetAttributes))
(mapcar '(lambda (x) (if (= tag (strcase (vla-get-TagString))) (vla-put-TextString val))) atts)

 

But to change only the value in the first attribute occurence (with matching tag), this can be used:

(vl-some '(lambda (x) (and (= tag (strcase (vla-get-TagString))) (progn (vla-put-TextString val) T))) atts)

Link to comment
Share on other sites

Hi Grr you missed the whole point just having a bit of a laugh we get a bit too serious sometimes, I don't want to know the Tag name, if I wanted to go that way I would have pulled apart one of the smart routines that I have that does all sorts of checking.

 

My code is 2 lines yours is 5 does the same thing and I can use it on any block I choose, change the attribute position value and string and just copy and paste to the command line.

 

Yes I did read lee's comments and took it on board that in some situations it will not work. It took around 1 minute to do 15 blocks. Next time will be seconds as I saved it, will not use it very often.

Link to comment
Share on other sites

My code is 2 lines yours is 5 does the same thing and I can use it on any block I choose, change the attribute position value and string and just copy and paste to the command line.

 

Hi BIGAL,

 

The aim of any program is not to have a short list of codes but to make a program working flawlessly and count for every eventuality. me thinks.

 

Your codes would error if you picked nothing or picked the wrong object other than attributed block.

 

So its recommended to either add error handler to exit safely from the program with no error at the command line or just as I always prefer to do is that to use the right filter and check up before doing any sort of any action.

 

Following your way of coding here is a simple example, though you can add your block name in the construction of ssget function if the block is not Dynamic.

 

(defun c:Test ( / s atts att)
 (while (and (princ "\nSelect Attributed block :")
             (setq s (ssget "_+.:S:E:L" '((0 . "INSERT") (66 . 1))))
             (setq atts (vlax-invoke (vlax-ename->vla-object (ssname s 0)) 'getattributes))
             (setq att (nth 4 atts))
             )
   (vla-put-textstring att "BIGAL")
 )
(princ)
) (vl-load-com)

Link to comment
Share on other sites

Hi BIGAL,

 

The aim of any program is not to have a short list of codes but to make a program working flawlessly and count for every eventuality. me thinks.

 

 

Obviously every professional programmer will agree on that.

I had a bit of fun today and worked on this error-trapping idea [which might not be recommended by Lee] :

 

; Error-traps every provided evaluation
; every - will return everything thats obtained or nil, if error occured
(defun GetEvals ( every L / rec r )
 (defun rec ( L / r )
   (cond 
     ( (not L) L)
     ( (vl-catch-all-error-p (setq r (vl-catch-all-apply (function (lambda nil (eval (cond ( (atom (car L)) (list (car L)) ) ( (car L) ) )))))))
       (prompt (strcat "\nGetEvals, Error: " (vl-catch-all-error-message r) ))
     )
     ( (cons r (rec (cdr L))) )
   ); cond 
 ); defun rec
 (cond ( (vl-consp L) (setq r (rec L)) ) )
 (cond ( (not r) r) ( (not every) r) ( (apply '= (mapcar 'length (list L r))) r) )
); defun GetEvals

 

Few examples:

 

((lambda ( / e o ) (GetEvals t '((setq e (car (entsel "\nPick block: "))) (setq o (vlax-ename->vla-object e)) (vla-put-TextString (nth (getint "\nAttrib position: ") (vlax-invoke o 'GetAttributes)) "Grrr")))))
(GetEvals t '((vla-put-TextString (nth 1 (vlax-invoke (vlax-ename->vla-object (car (entsel "\nPick block: "))) 'GetAttributes)) "Grrr")))
(GetEvals t '((setq e (car (entsel "\nPick block: "))) (setq o (vlax-ename->vla-object e)) (vlax-invoke o 'GetAttributes)))
(GetEvals nil '((setq e (car (entsel "\nPick block: "))) (setq o (vlax-ename->vla-object e)) (vlax-invoke o 'GetAttributes)))

 

The difference between this and the (and) function is that this provides error-trapping on every evaluation, inside the provided list.

BTW I have another type of variation of such recursive subfunction, but its not very related to this discussion.

Link to comment
Share on other sites

The difference between this and the (and) function is that this provides error-trapping on every evaluation, inside the provided list.

 

I don't know why you are complicating things up to a level that is not need at all while a simple function of error could handle any unexpected occurrence that may take a place and could manage to exit any program safely.

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...