Table of Contents
AutoLISP Fundamental Concepts
- () parenthesis enclose both the command/function name and the argument
- (alert “Hello World”
- Visual Basic
- alert (“Hello World”)
- “AutoLISP expressions can be nested one inside of another, and they are evaluated from the innermost to the outermost expression.” (AutoCAD Platform Customization AutoLISP by Lee Ambrosius, p. 2)
- (- 10 7) ; returns 3
- (- 10 (- 4 3)) ;returns 9 not 3
- 10 - (4 - 3) = 9
- 10 - 4 - 3 = 3 WRONG
- Typing AutoLISP code
- AutoCAD Command prompt
- !var = view or echo the value of variable var at the AutoCAD command prompt
-
- _$ var ; view or echo the value of variable var the the Visual LISP Console Window, don't need !
- application sensitive, not document sensitive so no matter what drawing is open
- Esc key clears the console
- highlight the lines of code to run, then Tools → Load Selection
- Text Editor (Notepad or VLIDE/VLISP)
AutoLISP Programs to Create
- Do simple math expressions
3(4^2-8+2) = 30
-
(defun C:hello() ; x = 3(4^2-8+2) math order of operations question (setq x (* 3 (+ (- (expt 4 2) 8) 2))) (setq y (strcat "3(4^2-8+2) = " (rtos x))) (alert y) ) ;end defun hello
- How do you draw a rectangle on the layout to represent a title block border?
- Options
- Display the background color setting (uniform background RGB 33,40,48 or Hex# 212830 dull black)
- Registry Key
- [HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\R22.0\ACAD-1000:409\Profiles\«C3D_Imperial»\Drawing Window]
- “Background”=dword:00302821
; ; registry access code ; (setq new_support_path (strcat "C:\\temp\\acad\support;" (getenv "ACAD"))) (setenv "ACAD" new_support_path) (setenv "Background" "dword:00302821")
- Plotting Edge of Paper
- References
- How do you run a block of code in the if statement?
Visual LISP Questions
VLIDE
- How do you start the Visual Lisp Integrated Development Environment (VLIDE)?
- Command: VLIDE
- How do you run LISP program?
- Command: APPLOAD
Output - Alert and princ
- How do you write “Hello World” to the screen or alert box?
- Command: (alert “Hello World\nFrom Jeff”)
- VLIDE
; C: means call this function (defun C:hello() (alert "Hello World\nFrom Jeff") ; writes to the screen (princ "Hello Command Prompt") ; writes to the command prompt (princ) ; gets rid of the last function return value of nil ) ;end defun hello
- save as M:\dwg\MansukhVisualLISP-Tutoring\hello.lsp
- Command: APPLOAD
- M:\dwg\MansukhVisualLISP-Tutoring\hello.lsp
- Load button
- Command: hello
- Note - this must be done every time you restart AutoCAD unless you click the Startup Suite button
AutoCAD LISP - FAQ
- What is the difference between AutoLISP and VisualLISP?
- VisualLISP uses VBA? All commands start with vla- or vl- or vlax-
- Is LISP case sensitive?
- Can you have a multi-line LISP comment? Yes
;| start of multi line comment (alert "Hello World") |; end of multi line comment (princ "Hello Jeff") (princ) ; needed to remove nil return value from previous princ function
- How do you run a LISP program?
- APPLOAD
- How do you add the path to LISP programs?
- OPTIONS → Files → Supported File Search Path
- Can you have multiple paths for LISP programs? Say one path to Network drive, another path to local project folder.
- What is a trusted folder?
- OPTIONS → Security tab
- How do you verify publisher?
- Where do I put LISP programs that automatically run when I open AutoCAD or a DWG?
Visual LISP
- What is a .vlx file type
- Visual Lisp Executable and it is used to combine multiple AutoLISP (.lsp) tools/files into a single.
- VLIDE → File → Make Application → Expert (simple is only for .lsp)
- creates a .prv (used to rebuild the application, say you got new .lsp file)
- What is a .fas file type
- Fast loadable
AutoLISP Displaying Messages
- prompt - displays string at the AutoCAD Command prompt
- princ - displays a value at the AutoCAD Command prompt or to an open file. Strings are displayed without the enclosing quotation marks
- prin1 - displays a value at the AutoCAD Command prompt or to an open file. Strings are enclosed in quotation marks
- print - displays a value at the AutoCAD Command prompt or to an open file, but a blank line is placed before the value and a space is placed after the value. Strings are enclosed in quotation marks.
- alert - displays a dialog box containing an error or warning message
- terpri - prints a newline to the AutoCAD Command prompt
- write-char
- write-line
- References
Define Function - defun
- AutoLISP Expression
; assign variable dRadius the value 1.25 (setq dRadius 1.25)
- setq is setq Core AutoLISP function
- Return the variable at the Command prompt, instead of using echo dRadius, in AutoLISP we use ! in front of the variable name
!dRadius
- Draw a circle using a LISP variable
(command "circle" "0,0" dRadius)
- Using operators and nested expressions. To add two number like 5 + 3 in AutoLISP we do
(+ 5 3)
- So lets try a nested expression which is the same as Order of Operations in Pre-Algebra math class
( (setq dRadius (+ 5 3)) (command "circle" "0,0" dRadius) )
- How do you get input from the user at the command line? Use getreal to input value such as 1.23
(setq nDist (getreal "\nEnter a distance: "))
- How do I concatenate strings and display information in a message box to the user? Use alert AutoLISP function and the strcat AutoLISP function
(alert (strcat "Hello World" " from The Great!"))
-
- (defun myfunc (x / temp) …) ;| One argument x, one local variable temp |;
; defun = Define Function (defun c:divide8 () (/ 8 2) )
- to call this function in AutoCAD, need to load it, then type divide8
- If we didn't include c: in the function name, then to call it, use parentheses (divide8)
Conditional Statements
- Converts a String to a Float ATOF, ATOF (AutoLISP) Core Function
- The if function normally evaluates one then expression if the test expression evaluates to anything but nil. The following example uses progn to evaluate two expressions following if:
(if (= a b) ; test if a = b but in AutoLISP evaluator is first (progn (princ "\nA = B ") ; expression 1 (setq a (+ a 10) b (- b 10)) ; expression 2 ) )
- References
Coding Formatting Conventions - Easy to Read
AutoLISP Data Types
- Integers
- Reals
- Strings
- Lists
- Selection Sets
- Entity Name
- VLA-objects objects in a drawing can be represented as ActiveX (VLA) objects
- File Descriptors is a point to a file opened by the AutoLISP open function
- Symbols and Variables - AutoLISP uses symbols to refer to functions and data holders
- References
Selection Set
Template VisualLISP Code
- AutoLISP Function Comments - provide program title, purpose, author, creation date, revisions. Document the WHY not HOW.
;---|----1----|----2----|----3----|----4----|----5----|----6----|----7----|----8 ; Title: ; Purpose: ; Author: ; Creation: ; Inputs: ; Outputs: ; Revisions: ; YYYY-MM-DD: ;---|----1----|----2----|----3----|----4----|----5----|----6----|----7----|----8 ; ; Loads the extended AutoLISP functions related to ActiveX support ; see http://help.autodesk.com/view/ACD/2018/ENU/?guid=GUID-6C7A8632-C12F-42BD-909E-68D804863AE2 ; (vl-load-com) ; ; Comments ; single line ;| inline comment |; ;| multiline comment |; ; http://help.autodesk.com/view/ACD/2018/ENU/?guid=GUID-4D4664AD-301F-4E6E-AD65-4B7CE6A258B8 ; ; exiting quietly, see http://help.autodesk.com/view/ACD/2018/ENU/?guid=GUID-58A6EE05-20AD-4F5E-A067-1E891C99E726 (princ)
- vl-load-com see vl-load-com (AutoLISP/ActiveX)
- VLIDE = Visual Lisp Intergrated Development Environment
- Apropos - autocomplete About the Apropos Feature (Visual LISP IDE
AutoLISP
AutoCAD Programmers
- Antonio Mamenta Fiverr - Antoniomamenta - Philippines
- years of experience in civil design using AutoCAD and Civil 3D
Visual LISP with ActiveX functions
- Visual LISP function = ActiveX method
- vla-addline
(vl-load-com) (vla-get-length)
- References
- Autodesk Help - Functions Reference (AutoLISP)