AfraLISP - Learn AutoLISP for AutoCAD productivity

Colours and Linetypes ByLayer

by Kenny Ramage

Each layer in an AutoCAD drawing has a default colour and a default linetype. Thus, the term "Bylayer" means that an object drawn on a particular layer has the default colour and linetype of that layer.

You can, if you wish, change the colour and linetype of an object drawn on a layer to suit your requirements. A good example of this is when dimensioning. Your dimensions reside on say layer "Dim" which has a default color of "1" and a default linetype of "Continuous". The extension lines though, are also on layer "Dim" but have a color of say "9". When you plot, colour "1" has a pen width 0f 0.35mm but colour "9" has a pen width of 0.15mm which will plot out the extension lines thinner.

Often though, you have to deal with a total mismatch of Layers, Colours and Linetypes and would like to convert all objects back to their "Bylayer" default values. Let's have a look at how we would go about this.

First of all, open a new, blank drawing and setup a Layer with the following properties :

Layer Name        "2"
Layer Colour        2 - "Yellow"
Layer Linetype    Dashed2

Now, draw a line on Layer "2" anywhere in the drawing then enter the following :

Command: (setq sel (ssget)) Select the Line
Select objects: 1 found
Select objects: <enter>
<Selection set: 8>
      
Command: (setq entity (ssname sel 0))
<Entity name: 1838e10>
      
Command: (setq name (entget entity))
((-1 . <Entity name: 1838e10>) (0 . "LINE") (330 . <Entity name: 18388c0>) (5 . "95A")
(100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "2")(100 . "AcDbLine")
(10 319.65 364.538 0.0) (11 338.809 500.026 0.0) (210 0.0 0.0 1.0))

We have just retrieved the entity list of the line object. The DXF Group Code 8, is the layer name. Group Code 62 is the colour of the object, but where is it?

Let's have a look for it. Type this :

Command: (setq layer (cdr (assoc 62 name)))
nil

It returns "nil". Hey Kenny, what's going here?
If an object is drawn on a layer using colour "Bylayer", no Group Code 62 exists as AutoCAD already knows what colour to draw the object with.

Changing the colour

O.K. let's mess AutoCAD around and change the colour :

Command: change
Select objects: 1 found
Select objects: <enter>
Specify change point or [Properties]: p
Enter property to change [Color/Elev/LAyer/LType/ltScale/LWeight/Thickness]: c
Enter new color <ByLayer>: 4
Enter property to change [Color/Elev/LAyer/LType/ltScale/LWeight/Thickness]: <enter>

Ha, take that you bounder. Let's look at the entity list again :

Command: (setq sel (ssget))
Select objects: 1 found
Select objects: <enter>
<Selection set: a>

Command: (setq entity (ssname sel 0))
<Entity name: 1838e10>

Command: (setq name (entget entity))
((-1 . <Entity name: 1838e10>) (0 . "LINE") (330 . <Entity name: 18388c0>)
(5 . "95A") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "2") (62 . 4)
(100 . "AcDbLine") (10 319.65 364.538 0.0) (11 338.809 500.026 0.0) (210 0.0 0.0 1.0))

Hey, Group Code 62 has suddenly appeared in our list and tells us that the object is now colour 4! Jeepers, AutoCAD is only clever.

But what happens if we want to change the line back to it's "Bylayer" colour and Linetype?

Have a look at the following :

Select the object :
Command: (setq sel (ssget))
Select objects: 1 found
Select objects: <enter>
<Selection set: 4>

Get it's name :
Command: (setq entity (ssname sel 0))
<Entity name: 1803610>

Retrieve the entity list :
Command: (setq name (entget entity))
((-1 . <Entity name: 1803610>) (0 . "LINE") (330 . <Entity name: 18030c0>) (5 . "95A") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "2") (62 . 4) (100 . "AcDbLine") (10 275.439 544.206 0.0) (11 357.967 544.206 0.0) (210 0.0 0.0 1.0))

Get the layer name :
Command: (setq layer (cdr (assoc 8 name)))
"2"

Use "tblsearch" to get the layer default details :
Command: (setq layerinf (tblsearch "LAYER" layer))
((0 . "LAYER") (2 . "2") (70 . 0) (62 . 2) (6 . "DASHED2"))

Retrieve the "Bylayer" colour :
Command: (setq layercol (cdr (assoc 62 layerinf)))
2

Construct a new list and append it to the entity list
Command: (setq name (append name (list (cons 62 layercol))))
((-1 . <Entity name: 1803610>) (0 . "LINE") (330 . <Entity name: 18030c0>) (5 . "95A") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "2") (62 . 4) (100 . "AcDbLine") (10 275.439 544.206 0.0) (11 357.967 544.206 0.0) (210 0.0 0.0 1.0) (62 . 2))

Now modify the definition data of an object :
Command: (entmod name)
((-1 . <Entity name: 1803610>) (0 . "LINE") (330 . <Entity name: 18030c0>) (5 . "95A") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "2") (62 . 4) (100 . "AcDbLine") (10 275.439 544.206 0.0) (11 357.967 544.206 0.0) (210 0.0 0.0 1.0) (62 . 2))

And finally, update the object :
Command: (entupd entity)
<Entity name: 1803610>

Your line should have changed back to colour Yellow.

Let's check if our entity list has been changed :

Command: (setq sel (ssget))
Select objects: 1 found
Select objects: <enter>
<Selection set: 6>
Command: (setq entity (ssname sel 0))
<Entity name: 1803610>
Command: (setq name (entget entity))
((-1 . <Entity name: 1803610>) (0 . "LINE") (330 . <Entity name: 18030c0>)
(5 . "95A") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "2")
(62 . 2) (100 . "AcDbLine") (10 275.439 544.206 0.0)
(11 357.967 544.206 0.0) (210 0.0 0.0 1.0))

Yep, everything is hunky dory! Our entity list has been updated with the correct values.

Changing Linetypes

Hey, this is great. Let's try the same thing, but this time with Linetypes.

Change your line to Linetype "CENTER2" :

Command: CHANGE
Select objects: 1 found
Select objects: <enter>
Specify change point or [Properties]: P
Enter property to change [Color/Elev/LAyer/LType/ltScale/LWeight/Thickness]: LT
Enter new linetype name <ByLayer>: CENTER2
Enter property to change [Color/Elev/LAyer/LType/ltScale/LWeight/Thickness]: <enter>

Now let's change it back to it's "Bylayer" Linetype :

Command: (setq sel (ssget))
Select objects: 1 found
Select objects: <enter>
<Selection set: 4>

Command: (setq entity (ssname sel 0))
<Entity name: 17db268>

Command: (setq name (entget entity))
((-1 . <Entity name: 17db268>) (0 . "LINE") (330 . <Entity name: 17dacc0>) (5 . "965")
(100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "2") (62 . 2) (6 ."CENTER2")
(100 . "AcDbLine") (10 170.804 532.425 0.0) (11 237.122 628.15 0.0) (210 0.0 0.0 1.0))

Command: (setq layer (cdr (assoc 8 name)))
"2"

Command: (setq layerinf (tblsearch "LAYER" layer))
((0 . "LAYER") (2 . "2") (70 . 0) (62 . 2) (6 . "DASHED2"))

This time select Group Code 6 for the default Linetype :

Command: (setq layerltype (cdr (assoc 6 layerinf)))
"DASHED2"

Command: (setq name (append name (list (cons 6 layerltype))))
((-1 . <Entity name: 17db268>) (0 . "LINE") (330 . <Entity name: 17dacc0>) (5 . "965")
(100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "2") (62 . 2) (6 ."CENTER2")
(100 . "AcDbLine") (10 170.804 532.425 0.0) (11 237.122 628.15 0.0) (210 0.0 0.0 1.0)
(6 . "DASHED2"))

Command: (entmod name)
(-1 . <Entity name: 17db268>) (0 . "LINE") (330 . <Entity name: 17dacc0>) (5 . "965")
(100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "2") (62 . 2) (6 ."CENTER2")
(100 . "AcDbLine") (10 170.804 532.425 0.0) (11 237.122 628.15 0.0) (210 0.0 0.0 1.0)
(6 . "DASHED2"))

Command: (entupd entity)
<Entity name: 17db268>

An Example Program

Now, is that not interesting? Let's put it into practice. Open Notepad and paste this coding into it, saving it as "ColorToLayer.Lsp" :

;CODING BEGINS HERE

(defun c:ColorToLayer ()

;clear the loop control variables
(setq i 0 n 0)

;prompt the user
(prompt "\n Select entities to analyze ")
;get the selection set (setq sel (ssget)) ;get the number of objects (setq n (sslength sel)) ;start the loop (repeat n ;get the entity name (setq entity (ssname sel i)) ;now get the entity list (setq name (entget entity)) ;if not Bylayer (if (not (assoc 6 name)) ;do the following (progn ;retrieve the layer name (setq layer (cdr (assoc 8 name))) ;get the layer data (setq layerinf (tblsearch "LAYER" layer)) ;extract the default layer colour (setq layercol (cdr (assoc 62 layerinf))) ;construct an append the new list (setq name (append name (list (cons 62 layercol)))) ;update the entity (entmod name) ;update the screen (entupd entity) );progn );if ;increment the counter (setq i (1+ i)) ;loop );repeat (princ) );defun (princ) ;CODING END HERE

You can easily write coding that will do exactly the same for Linetypes and even incorporate the two if you wish. You could even take it a step further and develop a fully fledged Layer Mapping Application Manager. Makes you think doesn't it?

No, I haven't forgotten! The source coding is here.