View Full Version : Selection set of polylines that are a specific color
ccowgill
2005-10-31, 11:30 PM
Can someone give me a hand with writing the line that selects all Polylines of a certain color? This is what I have so far and it keeps giving me an error
; error: bad SSGET list value
(setq co (getstring "\nColor to Add?"))
(setq SSET (ssget (list (cons 0 "*POLYLINE")(cons 62 co)))
CAB2k
2005-11-01, 12:19 AM
This will give you some ideas.
;;=============================================================
;; 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.")
ccowgill
2005-11-01, 12:26 AM
I'm not to familar with vla commands, is there somewhere that I can get a list of all the functions and what they do?
The help files will help with a lot of the vlisp commands. You can access it through the Developer's Guide. I think that is the name of it.
tyshofner
2005-11-01, 02:17 PM
Or if you don't want to use Visual Lisp, this will work (corrected portions in red)
(setq co (getstring "\nColor to Add?"))
(setq SSET (ssget "X" (list (cons 0 "*POLYLINE")(cons 62 (atoi co)))))
You just needed to convert the color value from a string to an integer, and I added the "X" so that the ssget will select all automatically.
Ty :mrgreen:
ccowgill
2005-11-01, 08:41 PM
Or if you don't want to use Visual Lisp, this will work (corrected portions in red)
(setq co (getstring "\nColor to Add?"))
(setq SSET (ssget "X" (list (cons 0 "*POLYLINE")(cons 62 (atoi co)))))
You just needed to convert the color value from a string to an integer, and I added the "X" so that the ssget will select all automatically.
Ty :mrgreen:
So in other words, if I change GETSTRING to GETINT, it would also take care of it?
So in other words, if I change GETSTRING to GETINT, it would also take care of it?
And remove the atoi function Ty added. ;)
CAB2k
2005-11-01, 09:31 PM
That will not select objects that are color bylayer where the layer color is
the color you want also.
ccowgill
2005-11-02, 10:28 AM
Thanks for the assistance. the routine gives an option to choose the layer, I just wanted to be able to chose the color in case the Polylines are all on the same layer, just different colors
CAB2k
2005-11-02, 01:09 PM
I see, it's getting more clear as to what you are dealing with.
The objects will only be "POLYLINE" not "LWPOLYLINE" and a set color or "BYLAYER" color.
In that case the direction you are headed will work. Sorry for the confusion.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.