Sponsored links

【AutoLISP FIRST STEP】 What is a Variable?

Sponsored links
AutoLISP
Sponsored links

You can give a nickname to a value, and you can use the nickname as the value. 

This nickname is called a Variable.

Sponsored links

What is a Variable?

You can put a value into a symbol.
Later, when you want to use the value, you can substitute it for the symbol.

For Example

You can attach a value to a symbol.
For example, let’s say a symbol of a symbol X

If you declare that…..

Jagaimo
Jagaimo

I attach 10 to symbol X

From here, X can be used the same as the number 10.

You can order to AutoCAD with X, such as “Make a line of length X“, “Make a same shape X times” etc…

This time, let’s declare that…

Jagaimo
Jagaimo

I attach “apple” to symbol X

Now, X can be used as a string “apple”.
“Make a layout tab named X” “Put the text X” etc…

The value of X can be changed to whatever you attach to it.

AutoLISP Function: setq

The most frequently used AutoLISP function might be this setq.

When we meet a new function, let’s check the Autodesk’s official page.

setq (AutoLISP)
Sets the value of a symbol or symbols to associated expressions

AutoCAD Help site


And, the required information for function setq are

  • Symbol (Variable name)
  • Value that will be needed later

You use AutoLISP function….

(setq symbol value)

Then the symbol becomes the substitute value.

Lets see how to make AutoLISP codes with the previous example.

Jagaimo
Jagaimo

I attach 10 to symbol X

In this case, a symbol is X, and a value that I want to use is 10.
So, if we use AutoLISP here it become….

Jagaimo
Jagaimo

(setq X 10)

In another case,

Jagaimo
Jagaimo

I attach “apple” to symbol X

It will be …

Jagaimo
Jagaimo

(setq X “apple”)

The point to be careful of, here, is that if the same symble is used, then the value will be overwritten.

(setq X 10)
(setq X “apple”)

In this case, “X = 10” is gone, and it became “X = apple“.

Therefore, if both the values need to be used later, the symbols have to be different.

(setq X 10)
(setq Y “apple”)

Now, X = 10, and Y is “apple”

Just for your information, if you want to define multiple valuables, you can program…

(setq X 10 Y “apple”)

The result is the same as
(setq X 10)
(setq Y “apple”)

To Check the Value of the symbol

If you want to know what value is attached to a symbol, you can use ! to check it.

Only what you need is input
!Variable_Name
in the command line of AutoCAD.

For Example

Please open your AutoCAD, and input…..

(setq X 1)
(setq Y "apple")

Now, the Variable X are Y were set.

Let’s check the value.
Please input

!X

on your command line.

Then, your AutoCAD will return…

1

Let’s try variable Y, too

!Y

Then, your AutoCAD will return…

"apple"

nil – Empty –

Now, let’s see what will happen when the variable is empty.

We didn’t define variable Z.

!Z
nil

AutoLISP returns nil which means empty here.

How to Delete Variables

Variables will be gone automatically when you close the drawing file, unless the program was written to save them.

If you want to delete the value right now, you can rewrite the valuables to nil which means empty.

(setq X nil Y nil)

Now, both variables for symbol X and Y are empty.

To Sum Up

  • A value can be attached to a symbol, and the symbol can be a substitute of the value later.
  • setq is a AutoLISP function which defines variables.
  • If you use same symbol, the value will be overwritten.
  • (setq X 1)(setq Y”apple”) and (setq X 1 Y “apple”) are same thing.
  • With !Variable_Name, you can see the value in the Variable.
  • nil means empty.
  • A value in the Variable will be deleted when the drawing file is closed unless it was programmed to save variables.
  • (setq Variable_Name nil) makes the symbol empty.

Related Articles

Here nil means empty, but it has another meaning “NO”.
In AutoLISP, Yes/No is T/nil

How to save variables on the computer.

Comments