Using (nentsel…) and changing an existing block insert instance
Here’s the real-world example (happened yesterday) that triggers quick Lisp lesson number “005″. The issue revolves around the color yellow.
The challenge
I reviewed some drawings from a client. Everything looked fine with dark background. But a light background, some of the attributed blocks were difficult to read - yellow ones. Very uncomfortable at best, bordering on impossible at worst.
Normally, I would just figure out what “layer” the attributed text was on and force that layer’s color from yellow to a better color.
But there was a problem. This block’s single yellow text attribute definition was not set up to have its COLOR display “ByLayer”. It was “hard-coded” to be yellow ALL the time.
How can we tell?… Here is the result of the AutoCAD “LIST” command on the upper “52″ bubble. Note that the single attribute, tag name BUBNUM, has been defined to always display as ”YELLOW”.
A Solution
Let’s fix this. What we’ll do now is build up a little AutoLISP tool to force a picked attribute to display in a different color. If you’d like to follow along “hands on”, download the above sample dwg file here. What we want our little AutoLISP tool to do is this: we pick on a yellow attribute. Our tool reads the attribute and, if the attribute’s color was NOT set up “BYLAYER”, changes its color to color “red”. This should show up nicely whether a dark or light background is selected.
Step-by-Step: Difference between (entsel) and (nentsel)
In each of the previous lessons here, here, here, and here we have looked at the AutoLisp (entsel) entity selection function.
With the example dwg file open/active on your screen, type this at the “Command:” prompt:
Command: (entsel) [Enter]
Select object: [pick on text in upper bubble] (<Entity name: 7ffffb04930> (-0.0405614 0.0279495 0.0))
The (entsel) function returns a two-element list: the first element is the entity name of the picked entity (i.e. the block insert instance) and the second element is the XY coordinate of the mouse pick point. We just want the entity name, the first element of this returned list. Use the AutoLISP “car” function to extract the first element. So, let’s do it again. Type this at the “Command:” prompt and pick on the upper bubble:
Command: (car (entsel)) [Enter]
Select object: [pick on text in upper bubble] <Entity name: 7ffffb04930>
Select object: [pick on text in upper bubble] ((-1 . <Entity name: 7ffffb04930>) (0 . “INSERT”) (330 . <Entity
name: 7ffffb039f0>) (5 . “18B”) (100 . “AcDbEntity”) (67 . 0) (410 . “Model”)
(8 . “0″) (100 . “AcDbBlockReference”) (66 . 1) (2 . “bubble”) (10 0.0 0.0 0.0)
(41 . 1.0) (42 . 1.0) (43 . 1.0) (50 . 0.0) (70 . 0) (71 . 0) (44 . 0.0) (45 .
0.0) (210 0.0 0.0 1.0))
Select object: [pick on text in upper bubble] ((-1 . <Entity name: 7ffffb04940>) (0 . “ATTRIB”) (330 . <Entity
name: 7ffffb04930>) (5 . “18C”) (100 . “AcDbEntity”) (67 . 0) (410 . “Model”)
(8 . “0″) (62 . 2) (100 . “AcDbText”) (10 -0.0606988 -0.0385562 0.0) (40 .
0.078125) (1 . “52″) (50 . 0.0) (41 . 1.0) (51 . 0.0) (7 . “Standard”) (71 . 0)
(72 . 4) (11 0.0 0.0 0.0) (210 0.0 0.0 1.0) (100 . “AcDbAttribute”) (280 . 0)
(2 . “BUBNUM”) (70 . 0) (73 . 0) (74 . 0) (280 . 0))
Select object: [pick right on the attribute]((-1 . <Entity name: 7ffffb26940>) (0 . “ATTRIB”) (330 . <Entity
name: 7ffffb26930>) (5 . “18C”) (100 . “AcDbEntity”) (67 . 0) (410 . “Model”)
(8 . “0″) (62 . 2)(100 . “AcDbText”) (10 -0.0606988 -0.0385562 0.0) (40 .
0.078125) (1 . “52″) (50 . 0.0) (41 . 1.0) (51 . 0.0) (7 . “Standard”) (71 . 0)
(72 . 4) (11 0.0 0.0 0.0) (210 0.0 0.0 1.0) (100 . “AcDbAttribute”) (280 . 0)
(2 . “BUBNUM”) (70 . 0) (73 . 0) (74 . 0) (280 . 0))
((-1 . <Entity name: 7ffffb26940>) (0 . “ATTRIB”) (330 . <Entity name:
7ffffb26930>) (5 . “18C”) (100 . “AcDbEntity”) (67 . 0) (410 . “Model”) (8 .
“0″) (62 . 1)(100 . “AcDbText”) (10 -0.0606988 -0.0385562 0.0) (40 . 0.078125)
(1 . “52″) (50 . 0.0) (41 . 1.0) (51 . 0.0) (7 . “Standard”) (71 . 0) (72 . 4)
(11 0.0 0.0 0.0) (210 0.0 0.0 1.0) (100 . “AcDbAttribute”) (280 . 0) (2 .
“BUBNUM”) (70 . 0) (73 . 0) (74 . 0) (280 . 0))
… and the attribute instantly changes from yellow (color 2) to red (color 1) !
Wrapping all this into a little utility
That was instructive, but very painful. Let’s put all of these steps into a little ASCII text file, give it a “.lsp” extension, and APPLOAD it to use when needed.
Here it is using the Visual Lisp editor (any ASCII text editor can be used).
APPLOAD the file and, when you need it, type ATTR2COLOR [Enter] at the Command: prompt. Change the color of as many “non-BYLAYER” colored attributes as you can pick!






Excellent job of explaining the how and why. This has definately helped me out. Keep using this approach to sharing your knowldege. Thank you.
Comment by Wayne — September 10, 2009 @ 8:49 am
[...] (Note: previous postings: Lesson 001, Lesson 002, Lesson 003, Lesson 004, and Lesson 005) [...]
Pingback by Nate’s Simple AutoLISP – Lesson 006 « AutoCAD Electrical Etcetera — September 26, 2009 @ 6:18 pm