Author Topic: In a bind for finding color in drawing  (Read 1295 times)

0 Members and 1 Guest are viewing this topic.

Rabbit

  • Guest
In a bind for finding color in drawing
« on: August 26, 2014, 05:57:44 PM »
I need a bit of code to find if a color is within an exact area of a drawing.

I'm using  (setq sset (ssget "_C" '(60.46695 116.17197) '(558.46695 2597.03571))) to get all the entities.

I need to cycle through these entities to see if anything with the color 82 is visible on the screen.  They could be text, lines, mleaders, blocks (dynamic with visibility states) etc...

I tried using (setq sset (ssget "_C" '(60.46695 116.17197) '(558.46695 2597.03571) '((62 . 82)))) to filter out the color, but was not able to use it on entities that have their color set to BYLAYER.  And, it did not work on blocks.  Maybe there is another way other than doing a selection set.

The final outcome is if the color 82 is shown on the screen, within those coordinates, then the code will do some extra commands.

Sincerely,
Rabbit

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: In a bind for finding color in drawing
« Reply #1 on: August 26, 2014, 06:50:21 PM »
Maybe you can use some of this:
Code: [Select]
;;=============================================================
;;     cSel.lsp by Charles Alan Butler
;;            Copyright 2005           
;;   by Precision Drafting & Design All Rights Reserved.
;;        Contact CAB at TheSwamp.org   
;;
;;    Version 1.1 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"))
               ))
    (if col
      (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 the color to use.")
    (setq col:prompt "\nSelect all objects to filter or [Enter] for ALL.")
  )
  ;;------------------------------------------------------------------
  (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.")
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.