• Home
  • Training Books
  • Subscribe to Our Email Newsletter
  • About
    • Contributors
    • Feedback
    • Contact
    • Privacy policy
    • Cookie Policy

CADnotes

CAD Tutorials and Best Practices for professionals and students

  • Featured
  • AutoCAD
    • AutoLISP
  • Revit
    • Revit Architecture Basic
    • Revit MEP Basic Tutorial
  • Inventor
  • MicroStation
    • MicroStation Basic Tutorial
  • CADnotes on YouTube
You are here: Home / AutoCAD / AutoLISP / AutoLISP tutorial: Working with layers

AutoLISP tutorial: Working with layers

April 21, 2011 by Edwin Prakoso 1 Comment

In this Article...

  • Working with layers
    • Changing the current layer
  • Sample: creating text
    • Check for existing layer

An AutoLISP program doesn’t have to be difficult. We can create simple LISP program and get many benefits from it. It’s not just about speed up the drawing creation, but also to maintain our standard.

In system variable and COND conditional, we discussed how we can use block to create grids. This will ensure the users (or you) to use the same block for every drawing. Now let’s see how to control the other properties you need to control: layers and styles. Let’s explore the possibility to use it to control our drawing standard.

Working with layers

layer manager

Layers are the most important thing to manage our drawings. If you’re not familiar with layer standard, CADD manager has a short article about the concept.

Changing the current layer

We can change the current layer using setvar. We did it before here. Current layer variable is controlled by CLAYER.

(setvar "CLAYER" "LAYERNAME")

Sample: creating text

Now let’s try an example. We want to create a LISP program to create text, and use a default layer. This is what we do:

  1. Get current layer and save it to a variable.
  2. Set current layer to a different layer. In this sample, annotation layer.
  3. We ask for required data for creating text. In this sample we ask for text content and position. You may want to consider ask for height and rotation angle. But it’s not necessary in this sample.
  4. Set the current layer to previous layer.

This is the code after we put it all together:

(defun c:cntext ()
(setq oldlayer (getvar "CLAYER")) ; get current layer
(setvar "CLAYER" "A-Anno")        ; change layer to annotation layer
(setq textcontent (getstring "
Type Text: "))
(setq textloc (getpoint "
Text Position: "))
(command "_TEXT" textloc "" "" textcontent) ;create the text
(setvar "CLAYER" oldlayer) ;restore active layer
)

Check for existing layer

It should works. But we still have a problem. What if the layer doesn’t exist? AutoCAD returns error warning.

; error: AutoCAD variable setting rejected: “clayer” “layername”

We need to check if the layer exist or not first. We can do it by using this code:

(setq flag (tblsearch "LAYER" "A-Anno"))

Then add conditional IF like this:

(if flag

(PUT WHAT YOU WANT AUTOCAD TO DO HERE)

(ELSE DO THIS)

 

)

We have discussed about using conditional IF in AutoLISP before. The problem is IF only allows you to run one statement. So we need to add (progn GROUP OF STATEMENTS) to run several statements.

Let’s see the complete code below:

(defun c:cntext ()
(setq flag (tblsearch "LAYER" "A-Anno")) ;looking for A-Anno layer
;then do this if exist
(if flag
(progn ;grouping statement
(setq oldlayer (getvar "CLAYER"))
(setvar "CLAYER" "A-Anno")
(setq textcontent (getstring "
Type Text: "))
(setq textloc (getpoint "
Text Position: "))
(command "_TEXT" textloc "" "" textcontent)
(setvar "CLAYER" oldlayer)
) ;end statement group
;else - and give warning if it doesn't exist
(alert "No A-Anno layer. Create it first to maintain standard.")
) ; end if
)

Now AutoCAD will try to find the layer first before executing our command. If it doesn’t exist, then it will give a warning.

notification

If the layer exists, then it will run our program.

There are several possibilities to handle this situation. We will see in the next tutorial, other alternatives to deal with non-exist layers.

credit: this article was written based on this AfraLISP tutorial.

About Edwin Prakoso

I work as a Solution Consultant in Datech Solutions, Tech Data Indonesia. I've been using AutoCAD since R14 and Revit since Revit Building 9. I occasionally write for AUGIWorld magazine and I am also active in Autodesk discussion forum. I'm a member of Autodesk Expert Elite, an appreciation for individuals who give contributions to the Autodesk community.
Connect with me on twitter or LinkedIn.

Filed Under: AutoLISP

0 0 votes
Article Rating
Subscribe
Login
Notify of
guest

guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Inline Feedbacks
View all comments
Rob
Rob
8 years ago

The code doesnt seem to work if the layer exists. Whatever I type in “Type Text” (i.e. DDD) it tells me after the command is run that its an unknown command

Specify start point of text or [Justify/Style]:
Specify rotation angle of text :
Enter text:
Command: DDD Unknown command “DDD”. Press F1 for help.
Command: “A-Anno”

I tried typing the code myself or even copy/ pasted. What could the issue be?? It seems to be using whatever I typed in as text as a command. Thanks.

0
Reply
wpdiscuz   wpDiscuz
Join Our Free Email Newsletter
  Thank you for Signing Up
Please correct the marked field(s) below.
1,true,6,Contact Email,2 1,false,1,First Name,2 1,false,1,Last Name,2

Featured

save AutoCAD workspace

How to create classic workspace in AutoCAD

Do you know that AutoCAD 2015 doesn’t have classic workspace? Don’t worry, you still can create it and use toolbar. Read how to create it here.

Recent Articles

  • Model-Based Progress Tracking in Autodesk Build
  • Understanding Surface and Cut Patterns in Revit
  • Revit 2024.1 Update is Released

Advertisement

New on CADnotes

  • Model-Based Progress Tracking in Autodesk Build
  • Understanding Surface and Cut Patterns in Revit
  • Revit 2024.1 Update is Released
  • What’s New in Revit 2024: Bending Detail
  • What’s New in Revit 2024: The Dark Theme

Meet the Authors

avatar for
avatar for
avatar for
avatar for
avatar for
avatar for

Get Connected

CADnotes on FacebookCADnotes on InstagramCADnotes on TwitterCADnotes on YouTube

© 2009 – 2023 CADnotes · Feedback · Privacy Policy · Become an affiliate

wpDiscuz