• 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
You are here: Home / AutoCAD / AutoLISP / Creating layer and styles with AutoLISP

Creating layer and styles with AutoLISP

May 27, 2011 by Edwin Prakoso 3 Comments

In this Article...

  • Creating a new layer
  • Working with text styles and dimension styles
    • Text Styles
    • Dimension Styles
  • Changing the properties

We learned how we can work with layers when we create objects in our AutoLISP program. By changing current layer or style in the program, it will make sure our objects to use specific properties. For example, we want when we place the grid label, we place it on annotation layer. This is a good way to be implemented with your company standard.

But what we did before is simply change it. If it doesn’t find it in name list, it simply gives a warning. Ask the user to create it first. That’s not cool.

The more logical thing to do is, when it doesn’t find it, it creates a new one.

create layer with autolisp

Creating a new layer

Creating new one is easy. We use LAYER command.

Type –layer on command prompt. You will see the list of available options. Remember, you can check the command we can use at command line first. Use – (dash) to load the command without dialog box. That’s how we use it with AutoLISP.

Command: -layer

Current layer:  “0”
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: m

Enter name for new layer (becomes the current layer) <0>: newlayer
Enter an option
[?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Fre
eze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:

The bold text with red color is what we’re going to use in AutoLISP.

  • -Layer is the command.
  • M will create new one, it will ask you for it’s name and then make it current.
  • This command will keep asking you to enter an option. So we press [enter] to end the command.

So we can write it as a LISP program below:

(command "_layer" "m" "NEWLAYER" "")

Easy, right?

This will create new layer named NEWLAYER (if there is no newlayer in the list) and make it current. The good thing about it is, if there is existing layer with that name, it will accept it and set make it current. So we don’t have to add code to check if it already exist.

We can replace this code we made before:

(defun c:cntext ()
(setq flag (tblsearch "LAYER" "A-Anno")) ;looking for existing
;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
)

With much simpler code like this

defun c:newla ()
(setq oldlayer (getvar "CLAYER")) ;get the current first
(command "_layer" "m" "newtest" "") ;create and change current
(setq textcontent (getstring "
Type Text: "))
(setq textloc (getpoint "
Text Position: "))
(command "_TEXT" textloc "" "" textcontent)
(setvar "CLAYER" oldlayer)
)

Working with text styles and dimension styles

So what about text styles and dimension styles?

Text Styles

What about text styles? We can use style command. It also can create new style and use it as current.

Let examine in command line how the sequence is.

Command: -STYLE
Enter name of text style or [?] <Standard>: newstyle

New style.
Specify full font name or font filename (TTF or SHX) <txt>: arial
Specify height of text or [Annotative] <0.0000>:
Specify width factor <1.0000>:

Specify obliquing angle <0>:
Display text backwards? [Yes/No] <No>:
Display text upside-down? [Yes/No] <No>:

“newstyle” is now the current text style.

There are 7 options AutoCAD will ask you.

Let’s see this sample. You can use this line to create text styles

(command "-style" "newstyle" "arial" "" "" "" "" "")

That code will create a new text style with name newstyle and using arial.ttf as it’s font. It ignores the other questions and accept default values.

Another example, this will create a style with height 2.5 and width factor 0.85.

(command "-style" "newstyle" "arial" 2.5 0.85 0 "N" "N")

* Note: if you use shx font, you need to press [enter] one more time. SHX fonts support vertical alignment, and AutoCAD will ask one more question, if you want to make it vertical.

Dimension Styles

Unfortunately when working with dimstyle, we need to use conditional IF.

  1. We need to use save option in dimstyle to create a new one.
  2. Or if it doesn’t exist, we need to use restore to change the current dimension style.

The problem is when we use save dimstyle, and AutoCAD find it already exist, it will ask one more question. It asks us to confirm if we want to redefine the existing.

So we use this code:

(defun c:tst ()
(setq flag (tblsearch "dimstyle" "newdimstyle")) ;looking for dimstyle
;then do this if exist
(if flag
(command "-dimstyle" "R" "NEWDIMSTYLE")
(command "-dimstyle" "S" "NEWDIMSTYLE" "")
); end if
)

Changing the properties

You already know how to create a text style and set it’s properties, because they’re asked when you create a new one.

Next, we will cover how we can change the properties of layers and dimenion styles. So we can create it with red color, specific linetype, lineweight, etc.

About Edwin Prakoso

I work as a Sr. Consultant in PT Cipta Satria Informatika. 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 Tagged With: dimension styles, layer, text styles

4 1 vote
Article Rating
Subscribe
Login
Notify of
guest

guest

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

3 Comments
Inline Feedbacks
View all comments
Ryan
Ryan
2 years ago

“Next, we will cover how we can change the properties of layers and dimenion styles. So we can create it with red color, specific linetype, lineweight, etc.” Why did this never happen? :'( This is what I’m trying to figure out lol.

2
Reply
kal
kal
10 years ago

how to write autolisp
to generate a new layer
AutoCAD
even him
give the new layer a new name automatically
and give the new layer has a new color automatically
thank you

0
Reply
Josh
Josh
12 years ago

This is awesome. Thank you for this. I minimized the amount of time spent combing my code for mistakes purely thanks to you.

0
Reply
wpdiscuz   wpDiscuz

Featured

aseptic tank

Easy Design with Master Skeleton Concept in Inventor

Are you familiar with master skeleton design concept in Inventor? Read how to do it in our tutorial here! From sketches to complete assembly.

Recent Articles

  • Autodesk Construction Cloud Activity Log
  • Exporting AutoCAD Plant 3D Model to Navisworks
  • Autodesk Data Connector for Power BI is Now Available

Advertisement

New on CADnotes

  • Autodesk Construction Cloud Activity Log
  • Exporting AutoCAD Plant 3D Model to Navisworks
  • Autodesk Data Connector for Power BI is Now Available
  • Autodesk Forma Design Contest
  • Revit 2025: Toposolid Enhancements

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 – 2025 CADnotes · Feedback · Privacy Policy · Become an affiliate

wpDiscuz