TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: danny on May 23, 2005, 07:51:50 PM

Title: Select all objects on a color
Post 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,
Title: Select all objects on a color
Post by: CAB on May 23, 2005, 08:17:42 PM
Perhaps this one:
http://www.theswamp.org/phpBB2/viewtopic.php?p=39127#39127
Title: Select all objects on a color
Post by: danny on May 23, 2005, 08:32:55 PM
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.
Title: Select all objects on a color
Post by: danny on May 24, 2005, 02:48:38 PM
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.
Code: [Select]
(defun c:c2lay ()
  (setq sset
(ssget
  "x"
  '((62.8))
)
  )
  (if sset
    (repeat (sslength sset)
      (command "_.chprop" sset "" "layer" "xxfine" "")
    )
  )
)
Title: Select all objects on a color
Post by: Jürg Menzi on May 24, 2005, 02:52:08 PM
Quote from: danny
Code: [Select]
'((62.8))
must be:
Code: [Select]
'((62 . 8))note the spaces...
Cheers
Title: Select all objects on a color
Post by: whdjr on May 24, 2005, 03:00:58 PM
You also have to step thru each item in a selection set using nth.

Try this:
Code: [Select]
(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" "")
    )
  )
)
Title: Select all objects on a color
Post by: Jürg Menzi on May 24, 2005, 03:06:15 PM
ahem...
Code: [Select]
(if sset
 (command "_.chprop" sset "" "layer" "xxfine" "")
)
Should be enough

Cheers
Title: Select all objects on a color
Post by: whdjr on May 24, 2005, 03:08:12 PM
Yeah that would work as well.  I didn't think of that I just fixed his repeat problem.
Title: Select all objects on a color
Post by: danny on May 24, 2005, 03:20:42 PM
So essentially, this is all I need?
Code: [Select]
(defun c:c2lay ()
  (setq   sset
    (ssget
      "x"
      '((62 . 8))
    )
  )
  (if sset
    (command "_.chprop" sset "" "layer" "xxfine" "")  
  )
)
Title: Select all objects on a color
Post by: CAB on May 24, 2005, 03:24:33 PM
As long as it's not 'bylayer' :)
Title: Select all objects on a color
Post by: whdjr on May 24, 2005, 03:25:38 PM
Yup!

However it is nice to localize your variables:
Code: [Select]
(defun c:c2lay (/ sset)
Title: Select all objects on a color
Post by: danny on May 24, 2005, 03:29:30 PM
Quote
As long as it's not 'bylayer'

oh darn... Just noticed that too.  How can I select colors even if its 'bylayer'?
Title: Select all objects on a color
Post by: Jeff_M on May 24, 2005, 05:52:50 PM
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......
Title: Select all objects on a color
Post by: Jeff_M on May 24, 2005, 06:05:07 PM
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)
Title: Select all objects on a color
Post by: danny on May 24, 2005, 07:26:55 PM
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
Code: [Select]
WHITE = _XBOLD
YELLOW = _BOLD
CYAN = _MED
RED = _FINE
MAGENTA = _XFINE
GREY = _XXFINE

how or where would I input this in your code?
Title: Select all objects on a color
Post by: Jeff_M on May 24, 2005, 07:47:50 PM
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:
Code: [Select]

(setq colorlist '((7 . "_XBOLD")
 (2 . " _BOLD")
 (4 . "_MED")
 (1 . " _FINE")
 (5 . "_XFINE")
 (8 . "_XXFINE")
 ))

(foreach x colorlist
  (color2layer (car x) (cdr x))
  )

Title: Select all objects on a color
Post by: danny on May 24, 2005, 08:05:25 PM
hmm.  I got an error
Code: [Select]
Automation Error. Calling method SetObjectId of interface IAcadBaseObject failed
this what I used
Code: [Select]
(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]
Title: Select all objects on a color
Post by: Jeff_M on May 24, 2005, 09:53:08 PM
Quote from: danny
hmm.  I got an error
Code: [Select]
Automation 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.
Quote from: danny

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?
Title: Select all objects on a color
Post by: danny on May 24, 2005, 10:14:15 PM
cool....Its working.
Much mahalo's Jeff  :D