AutoLISP 1.01 - Your first program

 

This lesson will teach you how to create 3 simple programs to set the Units command with appropriate Dimension variables setup.

 

At the AutoCAD Command prompt, enter VLIDE or VLISP to start Visual LISP

From the VLISP File menu, choose “New File”

Minimize AutoCAD and VLISP windows

Right-click on the Icon used to start AutoCAD, select “Properties”

Write down the “Start In:” folder name

Reopen VLISP window

From the VLISP File menu, choose “Save As”

Create a file named “acaddoc.lsp” in the “Start In:” folder

If “acaddoc.lsp” all ready exists, then create a new file called “acaduser.lsp” in the “Start In:” folder

 

Copy and paste the next section of code into your VLISP program

 

;UD = change units to decimal with inch mark for dimensions

(defun C:UD ()

  (princ “\nSetting Units to Decimal”)

  (setvar "CMDECHO" 0)

  (setvar "LUNITS" 2)

  (setvar "LUPREC" 3)

  (setvar "AUNITS" 0)

  (setvar "AUPREC" 3)

  (setvar "DIMPOST" (chr 34))

  (princ)

)

;UA = change units to architectural without inch mark for dimensions

(defun C:UA ()

  (princ “\nSetting Units to Architectural”)

  (setvar "CMDECHO" 0)

  (setvar "LUNITS" 4)

  (setvar "LUPREC" 5)

  (setvar "AUNITS" 0)

  (setvar "AUPREC" 3)

  (setvar "DIMPOST" ".")

  (princ)

)

;UF = change units to fractional with inch mark for dimensions

(defun C:UF ()

  (princ “\nSetting Units to Fractional”)

  (setvar "CMDECHO" 0)

  (setvar "LUNITS" 5)

  (setvar "LUPREC" 4)

  (setvar "AUNITS" 0)

  (setvar "AUPREC" 3)

  (setvar "DIMPOST" (chr 34))

  (princ)

)

 

 

Save the AutoLISP program file

 

Explanation of program -

;UD = …..                   a comment line used to describe program

(defun                         define function

C:UF                           creates new AutoCAD command “UF”

note - C: is NOT a drive letter and is required

                                    make sure UF is not a pre-existing command in AutoCAD

()                                  used by AutoLISP to create temporary variables

(princ                          print text to the command prompt line (info for user)

“\n                                print text on the next line

Settings….”)              text to print

(setvar                        same as the SETVAR command

“CMDECHO”             SETVAR variable name to set

                                    make sure to enclose the variable name in “”

0                                  set the SETVAR variable to 0=Off, 1=On

(princ)                         execute a quiet finish to the new command

)                                   final parentheses to end program

 

NOTES -

1. You must have matching pairs of ( ) - use Ctrl-[ and Ctrl-] to search for pairs

2. The “C:” is required to make the command available at the command prompt

3. Any SETVAR variable can be used in the programs

4. To test a line of code, type it at the command prompt in AutoCAD

 

If the “acaddoc.lsp” didn’t previously exist, every time you create a new drawing or open an existing drawing, then programs will be loaded.

 

If the “acaddoc.lsp” previously existed and you created the “acaduser.lsp” file,

       You will have to edit the existing “acaddoc.lsp” file and add the following line -

       (load “acaduser”)

 

Create a new drawing or open an existing drawing

Type UD, UA or UF at the command prompt

Use the SETVAR command to make sure the variables have been changed

 

Use the VLIDE Help menu, “Visual Lisp Help Topics” or F1 for additional help