+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Forum Newbie
    Using
    not specified
    Join Date
    Nov 2005
    Location
    Las Vegas
    Posts
    4

    Default LISP to select by color

    Registered forum members do not see this ad.

    I'm trying to make a LISP that cleans up a drawing. When I import the drawing everything is on the same layer, just different colors. I need to delete certain colors. I'm not sure how to do selection by color other than quick select, and that needs manual input, which I'm trying to avoid.

  2. #2
    Super Member David Bethel's Avatar
    Discipline
    Multi-disciplinary
    David Bethel's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Commercial Food Service
    Using
    AutoCAD pre 2000
    Join Date
    Dec 2003
    Location
    Newport News, Virginia
    Posts
    1,925

    Default

    This snippet would erase all entities colored 2 6 121 & 250

    AS IS
    Code:
      (foreach c '(2 6 121 250)
        (and (setq ss (ssget "X" (list (cons 62 c))))
             (command "_.ERASE" ss "")))
    That could be a bit dangerous. Good Luck

    Opps - Missed the "X" first time. -David
    R12 (Dos) - A2K

  3. #3
    Super Member CAB's Avatar
    Using
    AutoCAD 2000
    Join Date
    May 2004
    Location
    Tampa, Florida
    Posts
    801

    Default

    Registered forum members do not see this ad.

    Here is one way.
    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
          &#40;setq col&#58;lst &#40;vl-sort col&#58;lst '<&#41;&#41; ; removes douplicates
          &#40;setq colors "" layers ""&#41;
          &#40;setq lay&#58;lst &#40;get_layer col&#58;lst&#41;&#41; ; get layers using the colors
          &#40;foreach itm  col&#58;lst ; combine color names into one , del string
            &#40;setq colors &#40;strcat colors &#40;itoa itm&#41; ","&#41;&#41;&#41;
          &#40;setq colors &#40;substr colors 1 &#40;1- &#40;strlen colors&#41;&#41;&#41;&#41;; remove the last ,
          &#40;foreach itm  lay&#58;lst ; combine layer names into one , del string
            &#40;setq layers &#40;strcat layers itm ","&#41;&#41;&#41;
          &#40;setq layers &#40;substr layers 1 &#40;1- &#40;strlen layers&#41;&#41;&#41;&#41;; remove the last ,
          ;;==============================================================
          &#40;if ss&#58;first ; ALREADY GOT SELECTION SET
            &#40;while &#40;setq ent &#40;ssname ss&#58;first 0&#41;&#41;
              &#40;setq elst &#40;entget ent&#41;&#41;
              &#40;if &#40;or &#40;and &#40;assoc 62 elst&#41; ; got a color
                           &#40;member &#40;abs &#40;cdr&#40;assoc 62 elst&#41;&#41;&#41; col&#58;lst&#41;&#41; ; color match
                      &#40;and layers
                           &#40;member &#40;cdr&#40;assoc 8 elst&#41;&#41; lay&#58;lst&#41;
                           &#40;or &#40;null &#40;assoc 62 elst&#41;&#41;
                             &#40;= &#40;cdr&#40;assoc 62 elst&#41;&#41; 256&#41;&#41;&#41;  ; bylayer
                      &#41;
                &#40;ssadd &#40;ssname ss&#58;first 0&#41; ss&#41;
              &#41;
              &#40;ssdel &#40;ssname ss&#58;first 0&#41; ss&#58;first&#41;
            &#41;
            ;; else get a selection set to work with
            &#40;progn 
              &#40;prompt &#40;strcat "\nOK >>--> Select objects for Selection set or "
                              "ENTER for All objects on color&#40;s&#41; " colors&#41;&#41;
              ;;  create the filter
              &#40;if layers
                &#40;setq filter &#40;append
                               &#40;cons '&#40;-4 . "<OR"&#41; &#40;mapcar '&#40;lambda &#40;x&#41; &#40;cons 62 x&#41;&#41; col&#58;lst&#41;&#41;
                               &#40;list '&#40;-4 . "<AND"&#41;
                                     &#40;cons 8 layers&#41;
                                     '&#40;62 . 256&#41; ;  ByLayer
                                     '&#40;-4 . "AND>"&#41;
                                     '&#40;-4 . "OR>"&#41;
                                    &#41;&#41;&#41;
                &#40;setq filter &#40;list &#40;cons 62 colors&#41;&#41;&#41; 
              &#41;
              ;;  get objects using filter with user select
              &#40;if &#40;null &#40;setq ss &#40;ssget filter&#41;&#41;&#41;
                ;; or get ALL objects using filter
                &#40;setq ss &#40;ssget "_X" filter&#41;&#41;
              &#41;
            &#41;
          &#41;
          ;;==============================================================
          &#40;if &#40;> &#40;sslength ss&#41; 0&#41;
            &#40;progn
              &#40;prompt &#40;strcat "\n" &#40;itoa &#40;sslength ss&#41;&#41;
                          " Object&#40;s&#41; selected on color&#40;s&#41; " colors
                          "\nStart an ACAD command."&#41;&#41;
              &#40;sssetfirst nil ss&#41;
            &#41;
            &#40;prompt "\n***  Nothing Selected  ***"&#41;
          &#41;
        &#41;
      &#41;
      &#40;princ&#41;
    &#41;
    &#40;prompt "\nSelect by color loaded, Enter cSel to run."&#41;

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts