This will give you some ideas.
Code:
;;=============================================================
;; cSel.lsp by Charles Alan Butler
;; Copyright 2005
;; by Precision Drafting & Design All Rights Reserved.
;; Contact at ab2draft@TampaBay.rr.com
;;
;; Version 1.0 Beta July 13,2005
;;
;; C o l o r S e l e c t
;; Creates a selection set of objects of a color
;; Objects with color ByLayer are selected by color also
;; User picks objects to determine the color
;; Then User selects objects for ss or presses enter to
;; get all objects on the selected color
;; You may select the selection set before starting this
;; routine. Then select the layers to keep in the set
;;=============================================================
(defun c:csel (/ ent col colors layers ss col:lst col:prompt ss:first
elst filter lay:lst x ent:lst get_layer get_color_name)
(vl-load-com)
;; return a list of layers using the colors in the list
;; col:lst is a list of color numbers
(defun get_layer (col:lst / lay lays doc)
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(vlax-for lay (vla-get-layers doc)
(if (and (member (vla-get-color lay) col:lst)
(not (vl-string-search "|" (vla-get-name lay)))
)
(setq lays (cons (vla-get-name lay) lays))
)
)
lays
)
;; return the color name from the color number supplied
;; else return the number as a string
(defun get_color_name (c# / col)
(setq col (assoc c# '((1 "Red")
(2 "Yellow")
(3 "Green")
(4 "Cyan")
(5 "Blue")
(6 "Magenta")
(7 "Black/White")
(8 "Dark Grey")
(9 "Grey"))
))
(or (cadr col) (itoa c#))
)
;; =================================================================
;; Main Routine
;; =================================================================
;; get anything already selected
(setq ss:first (cadr(ssgetfirst))
ss (ssadd))
;; Get user selected colors
(if ss:first
(setq col:prompt "\nSelect the object to choose color to use.")
(setq col:prompt "\nSelect object for color filter.")
)
;;------------------------------------------------------------------
(while (setq ent (entsel col:prompt))
(redraw (car ent) 3) ; highlite the object
(setq ent:lst (cons (car ent) ent:lst))
(setq col (cdr(assoc 62 (entget (car ent))))); get the color
(if (null col) ; color is ByLayer, get layer color
(setq col (cdr (assoc 62
(tblsearch "layer"
(cdr (assoc 8 (entget (car ent))))))))
)
(setq col:lst (cons col col:lst))
(prompt (strcat "\n*-* Selected Color # -> " (get_color_name col)))
)
;;------------------------------------------------------------------
;; Un HighLite the entities
(and ent:lst (mapcar '(lambda (x) (redraw x 4)) ent:lst))
(if (> (length col:lst) 0); got color to work with
(progn
(setq col:lst (vl-sort col:lst '<)) ; removes douplicates
(setq colors "" layers "")
(setq lay:lst (get_layer col:lst)) ; get layers using the colors
(foreach itm col:lst ; combine color names into one , del string
(setq colors (strcat colors (itoa itm) ",")))
(setq colors (substr colors 1 (1- (strlen colors)))); remove the last ,
(foreach itm lay:lst ; combine layer names into one , del string
(setq layers (strcat layers itm ",")))
(setq layers (substr layers 1 (1- (strlen layers)))); remove the last ,
;;==============================================================
(if ss:first ; ALREADY GOT SELECTION SET
(while (setq ent (ssname ss:first 0))
(setq elst (entget ent))
(if (or (and (assoc 62 elst) ; got a color
(member (abs (cdr(assoc 62 elst))) col:lst)) ; color match
(and layers
(member (cdr(assoc 8 elst)) lay:lst)
(or (null (assoc 62 elst))
(= (cdr(assoc 62 elst)) 256))) ; bylayer
)
(ssadd (ssname ss:first 0) ss)
)
(ssdel (ssname ss:first 0) ss:first)
)
;; else get a selection set to work with
(progn
(prompt (strcat "\nOK >>--> Select objects for Selection set or "
"ENTER for All objects on color(s) " colors))
;; create the filter
(if layers
(setq filter (append
(cons '(-4 . "<OR") (mapcar '(lambda (x) (cons 62 x)) col:lst))
(list '(-4 . "<AND")
(cons 8 layers)
'(62 . 256) ; ByLayer
'(-4 . "AND>")
'(-4 . "OR>")
)))
(setq filter (list (cons 62 colors)))
)
;; get objects using filter with user select
(if (null (setq ss (ssget filter)))
;; or get ALL objects using filter
(setq ss (ssget "_X" filter))
)
)
)
;;==============================================================
(if (> (sslength ss) 0)
(progn
(prompt (strcat "\n" (itoa (sslength ss))
" Object(s) selected on color(s) " colors
"\nStart an ACAD command."))
(sssetfirst nil ss)
)
(prompt "\n*** Nothing Selected ***")
)
)
)
(princ)
)
(prompt "\nSelect by color loaded, Enter cSel to run.")