Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
Posts: 78
Registered: ‎03-24-2009
Message 1 of 16 (262 Views)

Changing the color of an attribute

262 Views, 15 Replies
02-08-2013 09:56 AM

Can you change the color of a block attribute with lisp like you can with the built in Enhanced Attribute Editor function eg

I can change the value of the attribute LAMP but can I also change its color from the color of the layer its created on?

 

(while (not (equal ck "SEQEND"))
(setq e1 (entnext e1)
e2 (entget e1)
ck (cdr (assoc 0 e2)))
(if (= (cdr (assoc 2 e2)) "LAMP")
(progn
(setq e2 (subst (cons 1 lamp)(assoc 1 e2)e2))
(entmod e2)
(entupd e1)
(setq ctr (+ ctr 1))
)
)
)

*Expert Elite*
Posts: 2,385
Registered: ‎12-17-2004
Message 2 of 16 (243 Views)

Re: Changing the color of an attribute

02-08-2013 12:49 PM in reply to: rodb

rodb,
perhaps something like

 

  (setq e2 (subst (cons 1 lamp) (assoc 1 e2) e2))
  (if (assoc 62 e2)
    (setq e2 (subst (cons 62 2) (assoc 62 e2) e2));; color 2
    (setq e2 (append e2 (list (cons 62 2))));; color 2
  )
  (entmod e2)

 

hope that helps

Henrique

Valued Contributor
Posts: 78
Registered: ‎03-24-2009
Message 3 of 16 (229 Views)

Re: Changing the color of an attribute

02-09-2013 02:43 AM in reply to: hmsilva

Hi Henrique, thank you for that I will give it a go today I hope I can get it to work!

 

Rod.

 

Valued Contributor
Posts: 78
Registered: ‎03-24-2009
Message 4 of 16 (224 Views)

Re: Changing the color of an attribute

02-09-2013 03:06 AM in reply to: hmsilva

It is working! Now I just have to polish the routine to ask for a color as well as a lamp type with a drop down menu which I have done before some time ago. Thanks!

*Expert Elite*
Posts: 2,369
Registered: ‎11-24-2009
Message 5 of 16 (222 Views)

Re: Changing the color of an attribute

02-09-2013 03:29 AM in reply to: rodb

rodb wrote:

Can you change the color of a block attribute with lisp like you can with the built in Enhanced Attribute Editor function eg

I can change the value of the attribute LAMP but can I also change its color from the color of the layer its created on?




Not sure what you mean by "created on" , is that block layer or the ATTDEF layer? 

 

if its the former

 

(defun demo  (str tag / ss y)
  (vl-load-com)
  (if (setq ss (ssget ":L" '((0 . "INSERT") (66 . 1))))
    (repeat (sslength ss)
      (if (setq y (vl-some '(lambda (x)
                              (if (eq (vla-get-tagstring x) tag)
                                x))
                           (vlax-invoke (vlax-ename->vla-object (ssname ss 0)) 'Getattributes)))
        (mapcar '(lambda (j k) (vlax-put y J k))
                '("Textstring" "Color")
                (list str (cdr (assoc 62 (tblsearch "Layer" (vla-get-layer y)))))))
      (ssdel (ssname ss 0) ss)))
  (princ)
  )

 

(demo  "New String" "LAMP")

 

HTH

 

EDIT: to add a color option

 

(defun demo2  (str tag col / ss y)
  (vl-load-com)
  (if (setq ss (ssget ":L" '((0 . "INSERT") (66 . 1))))
    (repeat (sslength ss)
      (if (setq y (vl-some '(lambda (x)
                              (if (eq (vla-get-tagstring x) tag)
                                x))
                           (vlax-invoke (vlax-ename->vla-object (ssname ss 0)) 'Getattributes)))
        (mapcar '(lambda (j k) (vlax-put y J k))
                '("Textstring" "Color")
                (list str col)))
      (ssdel (ssname ss 0) ss)))
  (princ)
  )

 

(demo2  "New String" "LAMP" 2)

 

*Expert Elite*
Posts: 2,369
Registered: ‎11-24-2009
Message 6 of 16 (217 Views)

Re: Changing the color of an attribute

02-09-2013 03:44 AM in reply to: pbejse

or even

 

(Defun c:demo3 ( / str tag col ss y)
   	(if (and
              (setq str (getstring  T "\nEnter New String:"))
              (not (eq "" str))
              (setq tag (strcase (getstring "\nEnter Tag to search:")))
              (not (eq "" tag))
              (setq col (setq lst (acad_colordlg 7 nil)))
              (setq ss (ssget ":L" '((0 . "INSERT") (66 . 1))))
              )
		(repeat (sslength ss)
		(if (setq y (vl-some '(lambda (x)
		              (if (eq (vla-get-tagstring x) tag)
		                x))
		           (vlax-invoke (vlax-ename->vla-object (ssname ss 0)) 'Getattributes)))
		(mapcar '(lambda (j k) (vlax-put y J k))
		'("Textstring" "Color")
		(list str col)))
		(ssdel (ssname ss 0) ss))
          	)
   (princ)
          )

 command: Demo3

Enter New String:hahaha

Enter Tag to search:LAMP

<Color dialog>

Select objects:

 

HTH

 

*Expert Elite*
Posts: 2,385
Registered: ‎12-17-2004
Message 7 of 16 (215 Views)

Re: Changing the color of an attribute

02-09-2013 03:56 AM in reply to: rodb

You're welcome, Rod

 

Henrique

Valued Contributor
Posts: 78
Registered: ‎03-24-2009
Message 8 of 16 (210 Views)

Re: Changing the color of an attribute

02-09-2013 08:21 AM in reply to: hmsilva

Thank you everyone. The program is working perfectly now. I was wanting to change the color of the attribute tag which is on a cyan layer by default not the color of the block layer. Using Henrique's option it works well. This is the program snippet now. I'm sure there are better ways of writing it but my Lisp is not that good.

 

(setvar "DYNMODE" 3)
(if (equal kelvin nil)
(setq kelvin 4))
(initget 7 "2800k 3000k 32000k 34000k 4000k 5000k")
(setq ctemp (getkword "\n Choose Colour Temperature [2800k/3000k/32000k/34000k/4000k/5000k] :"))
(setq kelvin (cond((= ctemp "2800k") 6)
((= ctemp "3000k") 1)
((= ctemp "32000k") 26)
((= ctemp "34000k") 3)
((= ctemp "4000k") 4)
((= ctemp "5000k") 5)

)
)
(prompt "\nPick your Blocks..")
(setq ss (ssget '((0 . "insert")))
sl (sslength ss)
ct 0
ctr 0)
(repeat sl
(setq e1 (ssname ss ct))
(setq ck 2)
(while (not (equal ck "SEQEND"))
(setq e1 (entnext e1)
e2 (entget e1)
ck (cdr (assoc 0 e2)))
(if (= (cdr (assoc 2 e2)) "LAMP")
(progn
(setq e2 (subst (cons 1 lmp)(assoc 1 e2)e2))
(if (assoc 62 e2)
(setq e2 (subst (cons 62 kelvin) (assoc 62 e2) e2))
(setq e2 (append e2 (list (cons 62 kelvin))))
)
(entmod e2)
(entupd e1)

 

Distinguished Contributor
area51visitor
Posts: 116
Registered: ‎03-05-2011
Message 9 of 16 (188 Views)

Re: Changing the color of an attribute

02-10-2013 12:58 AM in reply to: rodb

Hate to burst your bubble but "Battman" command works well for this.

- Brian
"Very funny, Scotty. Now beam down my clothes."
Valued Contributor
Posts: 78
Registered: ‎03-24-2009
Message 10 of 16 (182 Views)

Re: Changing the color of an attribute

02-10-2013 03:31 AM in reply to: area51visitor

But it does not allow you to select multiple blocks and the danger is it will assign a particular lamp to all those blocks, also the lisp is faster and much easier to use, it remembers your lamp and colour allowing you to pick and choose which blocks to edit. I have tried many times to get people in the office to use the built in AutoCAD commands like attedit but they simply forget how to use them and find them too long winded when they do remember them. Much better a Lisp with a clear menu pick option that you can find easily and asks direct questions then completes the task efficiently and remembers your choices.   

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community