AutoLISP Fundamental Concepts

  1. () parenthesis enclose both the command/function name and the argument
    • (alert “Hello World”
    • Visual Basic
    • alert (“Hello World”)
  2. 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
  3. Typing AutoLISP code

AutoLISP Programs to Create

  1. 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
  1. How do you draw a rectangle on the layout to represent a title block border?
  2. Options

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
      • 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?
    • Acad.lsp - run when AutoCAD starts
    • AcadDoc.lsp - run when a drawing opens
    • what about using APPLOAD → Startup Suite

Visual LISP

  1. What is a .vlx file type
    1. Visual Lisp Executable and it is used to combine multiple AutoLISP (.lsp) tools/files into a single.
    2. VLIDE → File → Make Application → Expert (simple is only for .lsp)
    3. creates a .prv (used to rebuild the application, say you got new .lsp file)
  2. What is a .fas file type
    1. Fast loadable

AutoLISP Displaying Messages

Define Function - defun

  • AutoLISP Expression
    • ; assign variable dRadius the value 1.25
      (setq dRadius 1.25)
    • 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

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)
    
  • VLIDE = Visual Lisp Intergrated Development Environment

AutoLISP

AutoCAD Programmers

Visual LISP with ActiveX functions

Navigation

Civil Engineering Engineering - Computer Engineering - Electrical Mechanical Engineering

Print/export
QR Code
QR Code engineer-civil:lisp (generated for current page)