TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: danny on May 23, 2005, 07:51:50 PM
-
I've seen a routine like this on the Swamp before, just can't seem to find it. Can anyone help? Select all objects on a color and change layer.
Thanks,
-
Perhaps this one:
http://www.theswamp.org/phpBB2/viewtopic.php?p=39127#39127
-
CAB,
That is a great routine, and I also like quick select. But, I need to be able to run it in a script file. Sorry, I should have been more specific.
-
o.k I tried this, but got a bad ssget list. What did I do wrong? Just trying to get all object on color 8 and change them to layer xxfine.
(defun c:c2lay ()
(setq sset
(ssget
"x"
'((62.8))
)
)
(if sset
(repeat (sslength sset)
(command "_.chprop" sset "" "layer" "xxfine" "")
)
)
)
-
'((62.8))
must be:'((62 . 8))
note the spaces...
Cheers
-
You also have to step thru each item in a selection set using nth.
Try this:
(defun c:c2lay (/ sset len)
(setq sset (ssget "x" '((62 . 8))))
(if sset
(repeat (setq len (sslength sset))
(setq len (1- len))
(command "_.chprop" (nth len sset) "" "layer" "xxfine" "")
)
)
)
-
ahem...
(if sset
(command "_.chprop" sset "" "layer" "xxfine" "")
)
Should be enough
Cheers
-
Yeah that would work as well. I didn't think of that I just fixed his repeat problem.
-
So essentially, this is all I need?
(defun c:c2lay ()
(setq sset
(ssget
"x"
'((62 . 8))
)
)
(if sset
(command "_.chprop" sset "" "layer" "xxfine" "")
)
)
-
As long as it's not 'bylayer' :)
-
Yup!
However it is nice to localize your variables:
(defun c:c2lay (/ sset)
-
As long as it's not 'bylayer'
oh darn... Just noticed that too. How can I select colors even if its 'bylayer'?
-
You need to make a list of layers that are of the color you want to filter.....Someone, coulda even been me, posted something that does this sometime in the past year or so. Lemme check......
-
OK, here's one I made up that also catches all entities within blocks...... uses ActiveX methods & proporties and no selection set.
After thinking about this, if you want to keep trying your own solution I'm not posting the code here. If you want to see and/or use it, I'm posting it to the Lilly Pond (http://www.theswamp.org/lilly_pond/jeff_m/color2layer.LSP?nossi=1)
-
Jeff,
I was thinking about trying ActiveX, if plain lisp didn't work. I wish I had enough time to learn this method. Eventually I'll get there...Thanks to the Swamp for everthing, I've become twice as efficient. Well...I do spend a lot of time browsing around in here. :)
So, if I had this list
WHITE = _XBOLD
YELLOW = _BOLD
CYAN = _MED
RED = _FINE
MAGENTA = _XFINE
GREY = _XXFINE
how or where would I input this in your code?
-
danny, I'd do it by placing each pair of colors/layers into a list, then calling the function in a foreach or mapcar, like this:
(setq colorlist '((7 . "_XBOLD")
(2 . " _BOLD")
(4 . "_MED")
(1 . " _FINE")
(5 . "_XFINE")
(8 . "_XXFINE")
))
(foreach x colorlist
(color2layer (car x) (cdr x))
)
-
hmm. I got an error
Automation Error. Calling method SetObjectId of interface IAcadBaseObject failed
this what I used
(defun color2layer (color layer / atts doc lay lays lokt)
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for lay (vla-get-layers doc)
(if (and (= color (vla-get-color lay))
(not (vl-string-search "|" (vla-get-name lay)))
)
(setq lays (cons (vla-get-name lay) lays))
)
(if (eq :vlax-true (vla-get-lock lay))
(progn
(setq lokt (cons (vla-get-name lay) lokt))
(vla-put-lock lay :vlax-false)
)
)
)
(vla-startundomark doc)
(setq lay (vla-add (vla-get-layers doc) layer))
(vla-put-color lay color)
(vlax-for blk (vla-get-blocks doc)
(vlax-for ent blk
(if (or (eq (vla-get-color ent) color)
(member (vla-get-layer ent) lays)
)
(vla-put-layer ent layer)
)
(if (and (vlax-property-available-p ent "hasattributes")
(vla-get-hasattributes ent)
(setq atts (vlax-invoke ent "getattributes"))
)
(progn
(foreach att atts
(if (or (eq (vla-get-color att) color)
(member (vla-get-layer att) lays)
)
(vla-put-layer att layer)
)
(vla-update att)
)
)
);
)
)
(if lokt
(foreach lay lokt
(vla-put-lock (vla-item (vla-get-layers doc) lay) :vlax-true)
)
)
(vla-endundomark doc)
(princ)
)
;;sample usage
(defun c:col2lay ()
(setq colorlist '((7 . "_XBOLD")
(2 . " _BOLD")
(4 . "_MED")
(1 . " _FINE")
(5 . "_XFINE")
(8 . "_XXFINE")
))
(foreach x colorlist
(color2layer (car x) (cdr x))
)
(princ)
)
It set "_XBOLD" to all the layers. Forced colors as well as bylayer.[/quote]
-
hmm. I got an errorAutomation Error. Calling method SetObjectId of interface IAcadBaseObject failed
This is due to there being a leading space in a layer name. Check the list and remove any spaces......I know the list I posted had them, sorry about that, I didn't check it before posting.
this what I used
<snip>
It set "_XBOLD" to all the layers. Forced colors as well as bylayer.
Try it again with the revised list. If it still comes out funky could you email me a portion of the drawing in R2000 format?
-
cool....Its working.
Much mahalo's Jeff :D