How to get layer color number

How to get layer color number

jaimuthu
Advocate Advocate
3,528 Views
8 Replies
Message 1 of 9

How to get layer color number

jaimuthu
Advocate
Advocate

(DEFUN C:CN()

(SETQ

ENT(CAR (ENTSEL "\n Select object:"))

obj(vlax-ename->vla-object ENT)


NO(RTOS(VLA-GET-COLOR OBJ))

)

(ALERT NO)

(PRINT NO)

)

 

 

i create this but i did not get the layer colour number plse help 

0 Likes
3,529 Views
8 Replies
Replies (8)
Message 2 of 9

Kent1Cooper
Consultant
Consultant

That may get the color of the object, but not of its Layer.  The object's color can be an override, but if it is getting its color from the Layer, it will be ByLayer, and the routine returns 256 for that.  Try this minimal adjustment to get the color of the Layer it's drawn on:

 

(DEFUN C:CN()
  (SETQ
    ENT (CAR (ENTSEL "\n Select object:"))
    obj (vlax-ename->vla-object ENT)
    NO (itoa (cdr (assoc 62 (tblsearch "layer" (VLA-GET-LAYER OBJ)))))
  )
  (ALERT NO)
  (PRINT NO)
)

 

I changed the (RTOS) in your original to (itoa) above for the sake of those who use Architectural Imperial Units, because with (rtos), it is converted to feet and inches [256 wouldn't occur as a Layer's color, but, for example, 255 comes across as 21'-3"].  That could be avoided in another way, by keeping (rtos) but adding mode and precision arguments:

....

    NO (RTOS (cdr (assoc 62 (tblsearch "layer" (VLA-GET-LAYER OBJ)))) 2 0)

....

 

If you prefer, it could be made to report the override color of the object if its color is not ByLayer, or the color of the Layer if it is ByLayer.  It could also be made to use words ["Red" etc.] for color numbers from 1 to 7, or from 0 to 7 if you want to account for "ByBlock" as a color assignment.

Kent Cooper, AIA
0 Likes
Message 3 of 9

dgorsman
Consultant
Consultant

Checking to see if something was actually picked before trying to do things with it is probably a good idea... something along the lines of:

 

(if (setq foo (entsel))
   (progn

      [entget, assoc, etc.]

   )
)

 

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


0 Likes
Message 4 of 9

Kent1Cooper
Consultant
Consultant

@dgorsman wrote:

Checking to see if something was actually picked before trying to do things with it is probably a good idea... ....


Yes, that's one of several enhancements/improvements I would include.  With some of the other possibilities added:

 

(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

Kent Cooper, AIA
0 Likes
Message 5 of 9

jaimuthu
Advocate
Advocate
it will be work thanks Kent1Cooper

and very great explain for these thanks
0 Likes
Message 6 of 9

scot-65
Advisor
Advisor
🙂
Always check user input!

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 7 of 9

Anonymous
Not applicable

What if you knew the layer.

Ihave a check to see if the layer exist and if is does it changes an image to that color.

 

Please help.


(defun spcchk()

;define the function

 

; check if use want to proceed

  (if (not (tblsearch "layer" curfn))  ;;;curfin is an input from edit box
  (progn
    (alert (princ "Function does not exist please choose an existing function."))
   ); progn


  (progn

 

 

;;;problem areas  trying to get layer number

 

(vlax-for layer (vla-get-Layers (vla-get-ActiveDocument (vlax-get-Acad-Object))))
(vla-get-name layer)
 (if (= curfin (setq fncol(vla-get-color curfn)))

 

;end of problem area

 


  (setq w (dimx_tile "cr1")    ;get image tile width
        h (dimy_tile "cr1")    ;get image tile height
 
);setq
 
  (start_image "cr1")     ;start the image
  (fill_image 0 0 w h (itoa fncol))   ;fill it with input color

  (end_image)

)


  );if
);defun

0 Likes
Message 8 of 9

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

What if you knew the layer.

Ihave a check to see if the layer exist and if is does it changes an image to that color.

 

Please help.

 

...

No time to look closely at all of it right now, but one thing I noticed:

 

You have the curfn variable name spelled with an i in it -- curfin -- in at least one place.

Kent Cooper, AIA
0 Likes
Message 9 of 9

Anonymous
Not applicable

I got it!!!!!  This is what I used.

 

Thanks for the help.

 

 

(defun spcchk() ;define the function

 

; check if use want to proceed

  (if (not (tblsearch "layer" curfn))  ;;;curfin is an input from edit box

 

  (progn
    (alert (princ "Function does not exist please choose an existing function."))
   ); progn

 (progn
 

;;;problem areas


(setq laycol (cdr (assoc 62 (tblsearch "layer" curfn))))


  (setq w (dimx_tile "cr1")    ;get image tile width
           h (dimy_tile "cr1")    ;get image tile height
 
   );setq
 
  (start_image "cr1")     ;start the image

  (fill_image 0 0 w h laycol)   ;fill it with input color


  (end_image)

   )


  );if
);defun

0 Likes