In this Article...
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.
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]: mEnter 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>: newstyleNew 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.
- We need to use save option in dimstyle to create a new one.
- 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.
“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.
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
This is awesome. Thank you for this. I minimized the amount of time spent combing my code for mistakes purely thanks to you.