AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Working with Layers & Styles.

I've had a lot of queries from people asking me how to create layers, change layers, change text styles, etc. using AutoLisp.
This tutorial will take you through the steps of doing just that.

Before you do anything in regards to layers and styles, it's always best to retrieve the current layer, or text style, so that you can restore it. To retrieve the current layer, use the following coding :

	(setq oldlayer (getvar "CLAYER"))

This will retrieve the name of the current layer and store it in variable "oldlayer". To restore the previous layer is just as simple:

	(setvar "CLAYER" oldlayer)

This will set the current layer to the previous layer name stored in variable "oldlayer".

You would use exactly the same syntax for retrieving and restoring the name
of the current Text Style :

	(setq oldstyle (getvar "TEXTSTYLE"))
	(setvar "TEXTSTYLE" oldstyle)

You might think that this would be a good way of changing layers and style.
The problem here, is that if the layer name you want to change to does not exist your program will crash.

You can use the "tblsearch" function to test if a layer or style exists :

	(tblsearch "LAYER" "TESTLAYER")
	(tblsearch "STYLE" "MYSTYLE")

If Layer "TESTLAYER" or Style "MYSTYLE" exists in your drawing, tblsearch will return "True", otherwise it will return "Nil".

Here's an example that tests if a layer exist, and if it does, sets the current layer to that layer:

	(setq oldlayer (getvar "clayer"))
	;retrieve the current layer

	(setq flag (tblsearch "LAYER" "NEWLAYER"))
	;find out if the layer exists
	
	(if flag
	;if the layer exists

		(setvar "CLAYER" "NEWLAYER")
		;set current layer to "NEWLAYER"

	);end if

This though, is quite a lot of coding just to check if a layer exists.
A better way, that applies to both Layers and Text Styles is to use the command function:

	(command "Layer" "M" "NEWLAYER" "")
	(command "Style" "Italict" "Italict.shx" "" "" "" "" "" "")
Both of these examples will change the layer or style, but will also create a new layer or text style if the layer or style does not exist within the drawing. (The style .shx file must exist and be within the AutoCad search path.)
I've also been asked that when I want to place text, dimensions, etc. into my drawing, how do I ensure that they are drawn on a particular layer?
Now, you could modify the AutoCad Menu to achieve this, but I prefer to create
a partial menu with the modified macros in place.

The way to go about this is to simply prefix the menu macro with one that changes to the specific layer :

	[Dtext]^C^C^CLayer;M;5;;_Dtext

This ensures that I am on Layer 5 when I add text to a drawing.

Well, I hope this has helped you. Keep well.....

 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright © 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website