JimYuan's Blog

Sharing the things I learned

0%

AutoLisp_Note

Reference

https://help.autodesk.com/cloudhelp/2018/CHS/AutoCAD-AutoLISP-Tutorials/files/index.htm#!

Basic Tutorial

Tutoiral: Getting Started(AutoLISP)

Autodesk explanation Link

AutoLISP is based on the LISP(LISt Processing) programming
langauage.

A list is a structure enclosed by parentheses.

Usually, the first element in the list is the name of a function, and the following elements are called arguments.

1
(function_name [argument1 argumentX ...])

The ! (exclamation point) character can only be used at the AutoCAD Command prompt and is used to return the current value of an AutoLISP variable.


1
(+ 0.01 (* 2 0.875))
  • 2 * 0.875 = 1.75
  • 0.01 + 1.75 = 1.76

1
(setq nDist (getreal "\nEnter a distance: "))

The getreal function prompts the user for a real numeric value. The value provided is then passed to the setq function and assigned to the nDist user-defined variable.


1
(alert (strcat "Welcome" "to" "AutoLISP!"))

The stract function combines all the strings into a single string value. The value returned bt the strcat function is then passed to the alert function and displayed in a message box.

Tutoiral: Creating a New Custom Command and Controllign wiht System Variables(AutoLISP)

Your custom commands can use standard AutoCAD commands with the command function, or they can directly manipulate objects using AutoLISP functions.

Creating a New Function

defun means “define function.”

1
(defun function_name ([arguments] [/ local_variables ...]) expr ...)

The defun function also allows you to define a list of arguments that can be passed to the function and a list of user-defined variable that are “local” to the function.

1
2
3
4
5
6
7
(defun display-msg (msg mode / )
(if (= mode 0)
(prompt (strcat "\n" msg))
(alert msg)
)
(princ)
)
  • (display-msg “Hello from AutoLISP!” 0)
  • (display-msg “Hello from AutoLISP!” 1)

Creating a Custom Command

A custom command is a function that is defined with the defun function, but uses a special naming convention: they use the characters c: as a prefix. This distinguishes them from other functions.

You can define a function that accepts arguments, but you should never define a function that will be used as a custom command to accpet arguments.

1
2
3
4
(defun c:hello ( / msg)
(setq msg (getstring T "\nEnter a message: "))
(alert msg)
)

msg is a varible only exist inside the c:hello scope.

You can save your AutoLISP expressions to a file with the LSP file extension so they can be reused and loaded into other drawings.

Accessing and Setting System Variable Value

Getter/ Setter

  • getvar: Returns the current value of a system variable
  • setvar: Assigns a new value to a system variable

The following explain how to get and set the value of the OSMODE(Object Snap Mode) system varible.

  1. At the Command prompt, enter (setq cur_osmode (getvar “osmode”))
    The current value of the OSMODE system variable is returned and assigned to the user-defined variable of cur_osmode. While OSMODE returns an integer value, the value is the sum of multiple “bit-code“ values. For example, the value 35 indicates that the EndPoint(1), Midpoint(2) and Intersection(32) running object snaps are enabled.
  2. At the Command prompt, enter osnap

Creating, Loading, and Opening an AutoLISP File(AutoLISP)

AutoLISP is an interpretive language, so it can be stored in an ASCII text file, loaded, and then executed directly within AutoCAD.

AutoLISP files typically have an .lsp file extension, but they can also have the .mnl file extension. Both LSP and MNL files can be edited with a text editor.

MNL files are associated with user interface customization and they are automatically loaded into AutoCAD when a customization (CUI/ CUIx) file of the same name is loaded. For example, the acad.mnl is automatically loaded into AutoCAD when the acad.cuix file is loaded.

We can open any text editor create and .lsp file and load it into AutoCAD.

The way to load it into AutoCAD.

  • Manage > Application> Load Application(Find the correct file)