LISP to change Attribute text color

LISP to change Attribute text color

burniksapwet
Enthusiast Enthusiast
12,554 Views
21 Replies
Message 1 of 22

LISP to change Attribute text color

burniksapwet
Enthusiast
Enthusiast

Guys,

 

           I need help from someone who could possibly create a LISP for us. Basically what we need is a way to change a block attribute to a different color. We want something that can be done on selected attributes. So let’s say for example a text attribute inside a block is "by layer" we would like a lisp to be able to change it to color 14 for example instead of having to do that one by one. At the same time we would also need something in reserve, meaning changing it back color 14 to “by layer." Thanks guys hope that you can help us out with this problem of ours. I know this request will be easy to some of you. Thanks for the help in advance.

 

 

Accepted solutions (2)
12,555 Views
21 Replies
Replies (21)
Message 2 of 22

tcorey
Mentor
Mentor
Using LISP is a great idea, but do you know about the block attribute manager? BATTMAN command. Take a look


Tim Corey
MicroCAD Training and Consulting, Inc.
Redding, CA
Autodesk Gold Reseller

New knowledge is the most valuable commodity on earth. -- Kurt Vonnegut
0 Likes
Message 3 of 22

leothebuilder
Advisor
Advisor

And if the BATTMAN command doesn't work, post your question here:

 

http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/bd-p/130

0 Likes
Message 4 of 22

ВeekeeCZ
Consultant
Consultant
Accepted solution

Try this easyone, using -attedit AutoCAD command.

 

(defun C:ATTEC (/ ss) ;Attribute Edit Color
  (command "_-attedit" "_y" "" "" "" PAUSE "" "_c" PAUSE "_n")
  (princ)
)

 If you want to BYLAYER, type simply BYLAYER instead of color number.

Message 5 of 22

Mr_J
Contributor
Contributor

BeekeeCZ, you rock! However I have a follow up question. Is there a way to select what color you want to change it to, and then pick ALL the ones you want to change? Or select EVERYTHING you want to change then run the lisp? Your command is great but it seems I have to type the color each time I run the command so when you have multiple blocks that have attributes it gets a bit tedious. Your lisp makes it very simple but I was curious if there was a way to make it that much easier.

 

Example: I will have tons of these kinds of symbols in a drawing and I will need to change a handful.

instrumentation.jpeg

Message 6 of 22

hmsilva
Mentor
Mentor
Accepted solution

Two different approaches:

 

(vl-load-com)
;; select multi blocks,
;; to change all selected attrubutes color...
(defun c:demo (/ blk col i ss)
  (if (and (princ "\nSelect blocks to change attributes color: ")
           (setq ss (ssget '((0 . "INSERT") (66 . 1))))
           (setq col (acad_colordlg 7))
      )
    (repeat (setq i (sslength ss))
      (setq blk (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
      (foreach att (vlax-safearray->list
                     (vlax-variant-value (vla-getattributes blk))
                   )
        (if (vlax-write-enabled-p att)
          (vla-put-color att col)
        )
      )
    )
  )
  (princ)
)

;; change attributes color by pick...
(defun c:demo1 (/ att col sel)
  (if (and (princ "\nEnter new attributes color: ")
           (setq col (acad_colordlg 7))
      )
    (while (and (setq sel (car (nentsel "\nSelect attribute text to change color: ")))
                (setq att (vlax-ename->vla-object sel))
                (= (vla-get-objectname att) "AcDbAttribute")
                (vlax-write-enabled-p att)
           )
      (vla-put-color att col)
    )
  )
  (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 7 of 22

Mr_J
Contributor
Contributor

Fantastic!!! That is exactly what I needed thanks!

Message 8 of 22

hmsilva
Mentor
Mentor

@Anonymous wrote:

Fantastic!!! That is exactly what I needed thanks!


You're welcome, Mr_J
Glad I could help

Henrique

EESignature

Message 9 of 22

burniksapwet
Enthusiast
Enthusiast

Thanks for the solution hmsilva for the solution . This is exactly what we were looking for.

Message 10 of 22

hmsilva
Mentor
Mentor

@burniksapwet wrote:

Thanks for the solution hmsilva for the solution . This is exactly what we were looking for.


You're welcome, burniksapwet
Glad I could help

Henrique

EESignature

0 Likes
Message 11 of 22

kthmarks
Contributor
Contributor

Wow,  I have been looking for some time for a routine to do this.  If I may, if I wanted the routine to single out a specific attribute within selected blocks, how would that work?  The Attribute is called Bay# and I want to change it to color 90.

 

Hopefully some one can help.

0 Likes
Message 12 of 22

hmsilva
Mentor
Mentor

@kthmarks wrote:

Wow,  I have been looking for some time for a routine to do this.  If I may, if I wanted the routine to single out a specific attribute within selected blocks, how would that work?  The Attribute is called Bay# and I want to change it to color 90.

 

Hopefully some one can help.


Untested...

 

(vl-load-com)
(defun c:demo (/ blk i ss)
    (if (and (princ "\nSelect blocks to change \"Bay#\" attributes to color 90: ")
             (setq ss (ssget '((0 . "INSERT") (66 . 1))))
        )
        (repeat (setq i (sslength ss))
            (setq blk (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
            (foreach att (vlax-safearray->list (vlax-variant-value (vla-getattributes blk)))
                (if (and (vlax-write-enabled-p att)
                         (eq (vla-get-tagstring att) "Bay#")
                    )
                    (vla-put-color att 90)
                )
            )
        )
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

0 Likes
Message 13 of 22

kthmarks
Contributor
Contributor

Thank you for the effort.  I should have been clearer, the BAY# Attribute occurs in several different blocks, RackA, RackB, RackC.  The program would need to grab the Attribute within the blocks.  There are other attributes also in the block but it needs to only change BAY#.

 

Thank you for your help.

0 Likes
Message 14 of 22

hmsilva
Mentor
Mentor

@kthmarks wrote:

Thank you for the effort.  I should have been clearer, the BAY# Attribute occurs in several different blocks, RackA, RackB, RackC.  The program would need to grab the Attribute within the blocks.  There are other attributes also in the block but it needs to only change BAY#.

 

Thank you for your help.


It's your goal to change all attributes color in BAY# TAG in all the dwg without selecting the blocks?

 

Henrique

EESignature

0 Likes
Message 15 of 22

kthmarks
Contributor
Contributor

No, only selected blocks.  I have a routine that lets me select one at a time (below) but the best scenerio would be to select specific blocks via (crossing) window and have that Bay# attribute change.  I guess the trick is that the attribute occurs in three different blocks, RackA, RackB, RackC.

 

Keep in mind, I know just enough to be dangerous.

 

(defun C:B2G (/ BIDSel BIDLoc)
(defun *error* (msg)
(princ "\nInvalid selection.")
(princ)
);_ end defun *error*

(defun SelectID (msg)
(setq BIDLoc (cadr (setq BIDSel (entsel (strcat "\nPick " msg " to Change: ")))))
);_ end defun SelectID

(defun CHGIt (/)
(command "-attedit" "y" "" "" "" BIDLoc "" "C" "90" "" "")
);_ end defun CHGIt

(setvar "cmdecho" 0)

(SelectID "attribute")
(CHGIt)
(while BIDSel
(SelectID "next attribute")
(if (= (type BIDSel) 'list)
(CHGIt)
(setq BIDSel nil)
) ;_ end if
) ;_ end while
(princ)
)

 

 

0 Likes
Message 16 of 22

hmsilva
Mentor
Mentor

@kthmarks wrote:

No, only selected blocks.  I have a routine that lets me select one at a time (below) but the best scenerio would be to select specific blocks via (crossing) window and have that Bay# attribute change.  I guess the trick is that the attribute occurs in three different blocks, RackA, RackB, RackC.

...


If in 'Bay#' # it's not a wildcard... the code in message #12 should work as expected.

If not, post a sample dwg with with some blocks.

 

Henrique

EESignature

0 Likes
Message 17 of 22

kthmarks
Contributor
Contributor

Thanks Henrique.  No, it didn't work.  Attached is a sample drawing.

 

To clarify further,multiple blocks have the same attribute.  I have attached a revised sample.

0 Likes
Message 18 of 22

hmsilva
Mentor
Mentor

@kthmarks wrote:

Thanks Henrique.  No, it didn't work.  Attached is a sample drawing.

...


As my friend @ВeekeeCZ as as detected... it was a text case issue... BAY# Bay#...

 

Revised code...

 

(vl-load-com)
(defun c:demo (/ blk i ss)
    (if (and (princ "\nSelect blocks to change \"Bay#\" attributes to color 90: ")
             (setq ss (ssget '((0 . "INSERT") (66 . 1))))
        )
        (repeat (setq i (sslength ss))
            (setq blk (vlax-ename->vla-object (ssname ss (setq i (1- i)))))
            (foreach att (vlax-safearray->list (vlax-variant-value (vla-getattributes blk)))
                (if (and (vlax-write-enabled-p att)
                         (eq (strcase (vla-get-tagstring att)) "BAY#")
                    )
                    (vla-put-color att 90)
                )
            )
        )
    )
    (princ)
)

 

Hope this helps,
Henrique

EESignature

Message 19 of 22

kthmarks
Contributor
Contributor

Perfect!  Thank you, I really appreciate your help with this. 

0 Likes
Message 20 of 22

hmsilva
Mentor
Mentor

@kthmarks wrote:

Perfect!  Thank you, I really appreciate your help with this. 


You're welcome!
Glad I could help 🙂

Henrique

EESignature