Sitemap
Khan Mohammad Hekmat

Khan Mohammad Hekmat

I am a Civil Engineer and AutoCAD specialist with over 20 years of experience. I live in Washington DC metropolitan area and work full time as Civil Engineer.

Press enter or click to view image in full size

In AutoLISP, variables are used to store and manipulate data. Here are some key points about variables in AutoLISP:

2 min readSep 28, 2023

--

  1. Variable Naming Conventions:
  2. . — Variable names in AutoLISP are case-insensitive.
  3. . — Variable names should start with a letter (A-Z or a-z) followed by letters, numbers, or hyphens (-).
  4. . — Common convention is to use hyphen-separated words for variable names (e.g., `my-variable`).

2. Variable Declaration:

. — Variables can be declared and assigned values using the `setq` function. For example:

. (setq my-variable 42)

3. Data Types:

. — AutoLISP variables can hold various data types, including numbers, strings, lists, and symbols.

4. Scope:

. — Variables in AutoLISP can have different scopes:

. — Local Variables: Declared within a specific function or block and are only accessible within that scope.

. — Global Variables: Declared outside of any function or block and are accessible throughout the entire AutoLISP session.

5. Accessing Variables:

. — To access the value of a variable, you can simply refer to its name. For example:

.

Get Khan Mohammad Hekmat’s stories in your inbox

Join Medium for free to get updates from this writer.

. (setq x 10)

. (princ x) ; Prints the value of x (10) in the command line.

.

6. Modifying Variables:

. — You can modify the value of a variable using the `setq` function. For example:

. (setq x 5)

. (setq x (+ x 1)) ; Increment x by 1

7. Variable Scope Examples:

. — Local variables are typically used within functions, and their scope is limited to that function:

. (defun my-function ()

. (setq local-variable 20) ; This is a local variable

. (princ local-variable)

. )

. (my-function) ; Calls the function and prints the local variable.

. (princ local-variable) ; This would result in an error because local-variable is not accessible here.

. — Global variables are accessible throughout the AutoLISP session:

. (setq global-variable 100) ; This is a global variable

. (princ global-variable) ; Prints the global variable.

Remember to be mindful of variable scoping to prevent naming conflicts and ensure variables are accessible where they are needed.

--

--

Khan Mohammad Hekmat
Khan Mohammad Hekmat

Written by Khan Mohammad Hekmat

I am a Civil Engineer and AutoCAD specialist with over 20 years of experience. I live in Washington DC metropolitan area and work full time as Civil Engineer.