Jump to content

Select entities by color property.


teknomatika

Recommended Posts

I need one more help:

I need an autolisp routine that allows from a pick in just one entity, select all entities in drwaing that have the same color property.

The entities can be multiple and belong to different layers.

It is true that in autocad the "qselect" tool already exists, but this does not always work, in addition to not being very direct.

 

Tanks.

Link to comment
Share on other sites

This?

 

(defun c:SelectByColour ( / c d e l )
   (if (setq e (car (entsel)))
       (progn
           (setq c
               (cond
                   (   (cdr (assoc 62 (entget e)))   )
                   (   (abs (cdr (assoc 62 (tblsearch "LAYER" (cdr (assoc 8 (entget e)))))))   )
               )
           )                     
           (while (setq d (tblnext "LAYER" (null d)))
               (if (= c (abs (cdr (assoc 62 d))))
                   (setq l (cons "," (cons (cdr (assoc 2 d)) l)))
               )
           )
           (sssetfirst nil
               (ssget "_X"
                   (if l
                       (list
                           (cons -4 "<OR")
                               (cons 62 c)
                               (cons -4 "<AND")
                                   (cons 62 256)
                                   (cons 8 (apply 'strcat (cdr l)))
                               (cons -4 "AND>")
                           (cons -4 "OR>")
                       )
                       (list (cons 62 c))
                   )
               )
           )
       )
   )
   (princ)
)

Link to comment
Share on other sites

Lee Mac,

Excellent. That's right. Much more direct than the QSELECT.

However, I found that in the case of blocks, the selection includes the native color of the layer, even when the color of block it is different.

But this is a detail.

Tanks.

Link to comment
Share on other sites

However, I found that in the case of blocks, the selection includes the native color of the layer, even when the color of block it is different.

 

You will only be able to select Primary entities with ssget.

Link to comment
Share on other sites

This may be of interest to you (the FilteredSelection.LSP file): http://www.theswamp.org/index.php?topic=35028.msg402471#msg402471

 

eg.

Command: ft
Filter choice: [block/Color/Entity/Layer/linetYpe] <Layer>: c
Ignore locked layers? [Yes/No] <No>:
Select object for color:
Color: "(5 . Blue)" selected.
Select objects: Specify opposite corner: 1 found

Select objects: al
1 found (1 duplicate), 1 total

Select objects:

 

You can also use SSX or Filter

Link to comment
Share on other sites

  • 8 years later...

Maybe use repeat for each color number, see Tharwat post. 

 

Enter a string say 1,2,3 use parse csv to make a list then a foreach to process the list. Similar to 1st suggestion how do you want to do the 1 2 3 ?

 

 

Link to comment
Share on other sites

(defun c:SelectByColour ( / c d e l )
   (if (setq e (getint "\nColour index :"))
       (progn
           (setq c
               (cond
                   (   e   )
                   (   (abs (cdr (assoc 62 (tblsearch "LAYER" 0))))   )
               )
           )                     
           (while (setq d (tblnext "LAYER" (null d)))
               (if (= c (abs (cdr (assoc 62 d))))
                   (setq l (cons "," (cons (cdr (assoc 2 d)) l)))
               )
           )
           (sssetfirst nil
               (ssget
                   (if l
                       (list
                           (cons -4 "<OR")
                               (cons 62 c)
                               (cons -4 "<AND")
                                   (cons 62 256)
                                   (cons 8 (apply 'strcat (cdr l)))
                               (cons -4 "AND>")
                           (cons -4 "OR>")
                       )
                       (list (cons 62 c))
                   )
               )
           )
       )
   )
   (princ)
)

I have adopt above LeeMac code acc. to my need. thnx. I dont know if it is logical but it works :)

Link to comment
Share on other sites

Untested.

(defun c:SelectByColour (/ c d l)
  (if (setq c (getint "\nColour index :"))
    (progn
      (while (setq d (tblnext "LAYER" (null d)))
        (if (= c (abs (cdr (assoc 62 d))))
          (setq l (cons "," (cons (cdr (assoc 2 d)) l)))
        )
      )
      (sssetfirst
        nil
        (ssget
          (if l
            (list
              (cons -4 "<OR")
              (cons 62 c)
              (cons -4 "<AND")
              (cons 62 256)
              (cons 8 (apply 'strcat (cdr l)))
              (cons -4 "AND>")
              (cons -4 "OR>")
            )
            (list (cons 62 c))
          )
        )
      )
    )
  )
  (princ)
)

 

Link to comment
Share on other sites

  • 1 year later...

This is shorter (sorry Lee Mac: any comment is appreciated)

 

(defun c:SelectByColor(/ TargEnt TargColor)

 (setq TargEnt (car (entsel "\nSelect object with Color to select: ")))
 (setq TargColor (assoc 62 (entget TargEnt)))
 (sssetfirst nil (ssget "_X" (list TargColor)))
 (print TargColor)
 (princ)
)

 

Note: doesnt work for "ByLayer" Color.  Lee Mack's is perfet as alwayse :)

Edited by hseeda
Link to comment
Share on other sites

On 7/30/2022 at 2:21 PM, hseeda said:

This is shorter (sorry Lee Mac: any comment is appreciated)

 

(defun c:SelectByColor(/ TargEnt TargColor)

 (setq TargEnt (car (entsel "\nSelect object with Color to select: ")))
 (setq TargColor (assoc 62 (entget TargEnt)))
 (sssetfirst nil (ssget "_X" (list TargColor)))
 (print TargColor)
 (princ)
)

 

Note: doesnt work for "ByLayer" Color.  Lee Mack's is perfet as alwayse :)

 

Be sure to also test for a valid selection prior to calling (entget TargEnt) otherwise your code will error.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...