I'm trying to use the vlax-get-property on several entities in a selection set and each entity is a different color, but when I use the:
(setq entColor (vlax-get-property oEnt 'Color))
If keeps returning the color "256" on all entities in the selection set. Even though I know for a fact that they are all different colors. Is there a step I'm missing?
Solved! Go to Solution.
Solved by Lee_Mac. Go to Solution.
Solved by dgorsman. Go to Solution.
256 means "bylayer". So the objects may appear different colors because they are on different layers.
First you need to test the entity color; if "fixed" then that is the color, if BYLAYER then you need to get the layer the entity is on and get the color of that layer, if BYBLOCK you need to check the parent block and get the color of that (which could in turn be BYLAYER).
Below are example functions demonstrating how to obtain the colour of an entity or object (or the corresponding layer colour) using Vanilla AutoLISP or Visual LISP respectively:
Vanilla AutoLISP
(defun getentcolour ( ent / enx ) (setq enx (entget ent)) (cond ( (cdr (assoc 62 enx))) ( (abs (cdr (assoc 62 (tblsearch "layer" (cdr (assoc 8 enx))))))) ) )
Test Program:
(defun c:test1 ( / ent ) (if (setq ent (car (entsel))) (getentcolour ent) ) )
Visual LISP
(defun getobjcolour ( obj / col ) (if (= acbylayer (setq col (vla-get-color obj))) (vla-get-color (vla-item (vla-get-layers (vla-get-document obj)) (vla-get-layer obj))) col ) )
Test Program:
(defun c:test2 ( / ent ) (if (setq ent (car (entsel))) (getobjcolour (vlax-ename->vla-object ent)) ) )
Getting the TrueColor property (returns as an AcCmColor object) provides a number of useful methods and properties, which also work with some useful constants like acRed and acByLayer. These can make the code a little more human readable.