Author Topic: Lisp to change all objects on a certain color to ByLayer...  (Read 12213 times)

0 Members and 1 Guest are viewing this topic.

chauny274

  • Guest
Lisp to change all objects on a certain color to ByLayer...
« on: April 15, 2009, 02:56:39 PM »
Hey guys, need alittle help with this lisp. It's not working and I can't figure out why...

For the new conduit that we are routing on our drawings, my boss wanted me to put it on the same layer as the old conduit but change the color from ByLayer that is set to blue, to 160, so that we could print it out alittle bolder to stand out. Now I'm trying to write a lisp to change everything that is set to 160 back to ByLayer. This is what I've come up with so far and it only deals with lines but it still doesn't work. Any help is appreciated. Also, is there any way to select everything on that color, rather then selecting all lines, then all p-lines then all splines on that color? I couldn't figure that out.
Thanks in advance guys!

Code: [Select]
(defun c:conduitbylayer ()
(setq ss1 (ssget "_X" '((0 . "LINE") (62 . 160)))) ;CREATE SELECTION SET
(if (/= ss1 nil) ;IF
(setq numsel (sslength ss1)) ;THEN
(setq numsel 0) ;ELSE
)
(setq numsel (1- numsel))
(while (/= numsel -1)
(setq ent1 (ssname ss1 numsel))
(setq entdata1 (entget ent1))
(setq entdata1 (subst (cons 62 -1) (assoc 62 entdata1) entdata1 ))
(entmod entdata1)
)
) ;END OF ROUTINE

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #1 on: April 15, 2009, 03:03:07 PM »
SETBYLAYER


Oh, wait... what version of AutoCAD??
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

M-dub

  • Guest
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #2 on: April 15, 2009, 03:03:53 PM »
Have you tried QSELECT to select all objects that match that criteria?

chauny274

  • Guest
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #3 on: April 15, 2009, 03:04:18 PM »
I use autocad 2009 but a few people around here that would need to use it use 2008.

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #4 on: April 15, 2009, 03:05:03 PM »
I use autocad 2009 but a few people around here that would need to use it use 2008.
SETBYLAYER works in 2008 so it looks like you're all set!   :wink:
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

chauny274

  • Guest
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #5 on: April 15, 2009, 03:07:04 PM »
Have you tried QSELECT to select all objects that match that criteria?
I have tried that and it works fine, but we have changed the color of all text to a different color, all new equipment to a different color, and all new conduit, so rather than using qselect three times in every drawing I was hoping to create just a button that would change everything on those three colors that we chose back to bylayer. I also don't want to select everything and put everything back to bylayer cause there are some objects in our clients border that isn't set to bylayer and we don't want to change that.

Also, qselect can't be used in lisp programs, can it?

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #6 on: April 15, 2009, 03:07:06 PM »
And here's some code to do it too  :-P

Code: [Select]
(defun c:conduitbylayer (/ n ent ss1)
  (vl-load-com)
  (setq n -1)
  (if (setq ss1 (ssget "_X" '((62 . 160))))
    ;;CREATE SELECTION SET RJP-took out the (0 . "LINE") filter
    (while (setq ent (ssname ss1 (setq n (1+ n))))
      (vla-put-color (vlax-ename->vla-object ent) 256)
    )
  )
) ;END OF ROUTINE
« Last Edit: April 15, 2009, 03:23:03 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

chauny274

  • Guest
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #7 on: April 15, 2009, 03:08:07 PM »
I use autocad 2009 but a few people around here that would need to use it use 2008.
SETBYLAYER works in 2008 so it looks like you're all set!   :wink:
I'll look into it, never heard of this command. Thanks!

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #8 on: April 15, 2009, 03:10:40 PM »
I use autocad 2009 but a few people around here that would need to use it use 2008.
SETBYLAYER works in 2008 so it looks like you're all set!   :wink:
I'll look into it, never heard of this command. Thanks!
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

chauny274

  • Guest
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #9 on: April 15, 2009, 03:11:24 PM »
And here's some code to do it too  :-P

Code: [Select]
(defun c:conduitbylayer (/ n ent)
  (vl-load-com)
  (setq n -1)
  (if (setq ss1 (ssget "_X" '((62 . 160))))
    ;;CREATE SELECTION SET RJP-took out the (0 . "LINE") filter
    (while (setq ent (ssname ss1 (setq n (1+ n))))
      (vla-put-color (vlax-ename->vla-object ent) 256)
    )
  )
) ;END OF ROUTINE

Wow, works great, I definitely need to study up on some more lisp. I'm still going to look up the bylayer command but this works great.

Thanks!

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #10 on: April 15, 2009, 03:12:44 PM »
Code: [Select]
(setq ss1 (ssget "_X" '((0 . "LINE") (62 . 160)))) ;CREATE SELECTION SET
This is telling it to only select ' Line 's that are color 160.  If you want to select all objects that are color 160, then just remove the filter part for lines.  So it would look like
Code: [Select]
(setq ss1 (ssget "_X" '((62 . 160)))) ;CREATE SELECTION SET

The portion below is where it is changing the color.  -1 is not a valid number for a color.  The number for ByLayer is 256, so you would only need to change the -1 to 256, and all should be find.
Code: [Select]
(setq entdata1 (subst (cons 62 -1) (assoc 62 entdata1) entdata1 ))


Edit:  To slow again.... =(**
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

M-dub

  • Guest
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #11 on: April 15, 2009, 03:16:17 PM »
I didn't know you had a few different colours to deal with... I was going to post this ... and will anyway.  It doesn't matter what the objects are... text, lines, blocks, etc.

ronjonp

  • Needs a day job
  • Posts: 7531
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #12 on: April 15, 2009, 03:19:33 PM »
And here's some code to do it too  :-P

Code: [Select]
(defun c:conduitbylayer (/ n ent)
  (vl-load-com)
  (setq n -1)
  (if (setq ss1 (ssget "_X" '((62 . 160))))
    ;;CREATE SELECTION SET RJP-took out the (0 . "LINE") filter
    (while (setq ent (ssname ss1 (setq n (1+ n))))
      (vla-put-color (vlax-ename->vla-object ent) 256)
    )
  )
) ;END OF ROUTINE

Wow, works great, I definitely need to study up on some more lisp. I'm still going to look up the bylayer command but this works great.

Thanks!

 :kewl:

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

chauny274

  • Guest
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #13 on: April 15, 2009, 03:24:55 PM »
Code: [Select]
(setq ss1 (ssget "_X" '((0 . "LINE") (62 . 160)))) ;CREATE SELECTION SET
This is telling it to only select ' Line 's that are color 160.  If you want to select all objects that are color 160, then just remove the filter part for lines.  So it would look like
Code: [Select]
(setq ss1 (ssget "_X" '((62 . 160)))) ;CREATE SELECTION SET

The portion below is where it is changing the color.  -1 is not a valid number for a color.  The number for ByLayer is 256, so you would only need to change the -1 to 256, and all should be find.
Code: [Select]

(setq entdata1 (subst (cons 62 -1) (assoc 62 entdata1) entdata1 ))


Edit:  To slow again.... =(**

Oh, ok. A co-worker who thinks he is a lisp expert told me that bylayer is -1. Thanks for that! Also, when I tried the first time I wrote this I didn't have the filter for line in there and it wouldn't creat the selection set without me adding it in there, don't know why, but it's working now, must have had the code mixed up.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Lisp to change all objects on a certain color to ByLayer...
« Reply #14 on: April 15, 2009, 04:05:08 PM »
You're welcome then, for the tidbit of information I was able to supply.   :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.