how to get color number

how to get color number

Anonymous
Not applicable
2,017 Views
4 Replies
Message 1 of 5

how to get color number

Anonymous
Not applicable

hello. always thank you everyone

this lsp is get the color number from selected object.

 

I want I get the color number of the selected block inside block object ?

Do not use the bedit command.
 
thanks for reading.
 

(defun C:CN (/ esel obj col laycol msg)
  (if (setq esel (entsel "\nSelect object to report its Layer's color:"))
    (progn
      (setq
        obj (vlax-ename->vla-object (car esel))
        col (vla-get-color obj)
        laycol (cdr (assoc 62 (tblsearch "layer" (vla-get-layer obj))))
      ); setq
      (alert
        (setq msg
          (strcat
            "Color of object's Layer is " (itoa laycol) "."
            (if (/= col 256); only if not ByLayer
              (strcat
                "\nObject has override color "
                (if (= col 0) "ByBlock" (itoa col))
                "."
              ); strcat
              "" ; [add nothing to msg if ByLayer]
            ); if
          ); strcat & msg
        ); setq
      ); alert
      (prompt (strcat "\n" msg))
    ); progn
  ); if
  (princ)
); defun

0 Likes
Accepted solutions (3)
2,018 Views
4 Replies
Replies (4)
Message 2 of 5

ВeekeeCZ
Consultant
Consultant
Accepted solution

@Anonymous wrote:
(defun C:CN (/ esel obj col laycol msg)

  (if (setq esel (nentsel "\nSelect object to report its Layer's color:"))
    (progn
      (setq
        obj (vlax-ename->vla-object (car esel))
        col (vla-get-color obj)
        laycol (cdr (assoc 62 (tblsearch "layer" (vla-get-layer obj))))
      ); setq
      (alert
        (setq msg
          (strcat
            "Color of object's Layer is " (itoa laycol) "."
            (if (/= col 256); only if not ByLayer
              (strcat
                "\nObject has override color "
                (if (= col 0) "ByBlock" (itoa col))
                "."
              ); strcat
              "" ; [add nothing to msg if ByLayer]
            ); if
          ); strcat & msg
        ); setq
      ); alert
      (prompt (strcat "\n" msg))
    ); progn
  ); if
  (princ)
); defun


 

Just add 'n' as for nested.

0 Likes
Message 3 of 5

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

....

I want I get the color number of the selected block inside block object ?

....

 

If you really mean the color of a nested Block  inside another Block, then just changing to (nentsel) won't do it.  That will give you the deepest-nested non-Block object  in a nested Block, or a nested nested Block, however many levels down it is.  From the AutoLisp Reference:

When the selected object is a component of a block reference other than an attribute, nentsel returns a list containing four elements.

....

The fourth element is a list containing the entity name of the block that contains the selected object. If the selected object is in a nested block (a block within a block), the list also contains the entity names of all blocks in which the selected object is nested, starting with the innermost block and continuing outward until the name of the block that was inserted in the drawing is reported.

 

So if you want a nested Block, rather than the selected nested object, you will need to work with that list.  In simplest terms, if you want the Block that's nested one level down in the top-level Block:

 

(setq nlist (nentsel)); pick a nested object
(setq nest1 (cadr (reverse (last nlist)))); the nested-one-level-down Block entity name
(entget nest1); its entity data, from which you can get a color assignment if there is one

 

You can use different parts of that (last nlist) to get different Block entity names if things are nested more than one level down, for instance to get the deepest-nested Block that contains the object picked on:

 

(setq nest1 (car (last nlist)))

Kent Cooper, AIA
0 Likes
Message 4 of 5

ronjonp
Mentor
Mentor
Accepted solution

You could show all the layers with this simple mod:

(defun c:cn (/ esel obj col laycol msg)
  (cond	((setq esel (nentsel "\nSelect object to report its Layer's color:"))
	 ;; RJP added multiple item layer output
	 (setq esel (if	(cadddr esel)
		      (append (list (car esel)) (cadddr esel))
		      (list (car esel))
		    )
	 )
	 (setq msg "")
	 (foreach x esel
	   (setq obj	(vlax-ename->vla-object x)
		 col	(vla-get-color obj)
		 laycol	(cdr (assoc 62 (tblsearch "layer" (vla-get-layer obj))))
	   )				; setq
	   (setq msg (strcat msg
			     "\nColor of object [ "
			     ;; RJP added type of object selected
			     (if (vlax-property-available-p obj 'name)
			       (strcat "BLOCK: " (vla-get-name obj))
			       (vla-get-objectname obj)
			     )
			     " ] layer is "
			     (itoa laycol)
			     "."
			     (if (= col 256) ; only if ByLayer
			       ""
			       (strcat ", Object has override color "
				       (if (= col 0)
					 "ByBlock"
					 (itoa col)
				       )
				       "."
			       )	; strcat
					; [add nothing to msg if ByLayer]
			     )		; if
		     )			; strcat & msg
	   )				; setq
	 )
	 (prompt msg)
	 (alert msg)
	)
  )					; if
  (princ)
)					; defun

 

0 Likes
Message 5 of 5

Anonymous
Not applicable

thank you all

It was a great help.
0 Likes