© 2006 autodesk autocad: secrets every user should know chapter 8 - autolisp by example: getting...
TRANSCRIPT
![Page 1: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/1.jpg)
© 2006 Autodesk
AutoCAD: Secrets Every User Should Know
Chapter 8 - AutoLISP by Example: Getting Started
![Page 2: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/2.jpg)
2© 2006 Autodesk
Key Topics
- Entity Selection and Manipulation
- Constructing New Points
- String Conversion and Manipulation
- Debugging and Troubleshooting Techniques
- Annotating and Error-Trapping
![Page 3: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/3.jpg)
3© 2006 Autodesk
AutoLISP Based on LISP from 1950s Good User Language
Can use AutoCAD commands Can use AutoCAD variables
VLISP Added AutoCAD 200 Uses AutoCAD Commands
VBA and VB.NET
C++
Programming Tools
![Page 4: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/4.jpg)
4© 2006 Autodesk
Writing Text Editor Visual LISP Editor NO Word Processors
Saving ASCII Text File .LSP extension ACADDOC.lsp Loads in Each Drawing ACAD.lsp Loads Only When AutoCAD Fires Up
Saving AutoLISP Programs
![Page 5: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/5.jpg)
5© 2006 Autodesk
Three ways to load AutoLISP Type it at the command line
or copy/paste to the command line
Load a text file that contains AutoLISP code
(load “program.lsp”) APPLOAD Startup Suite
Use the load function of the Visual LISP editor
Loading Programs
![Page 6: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/6.jpg)
6© 2006 Autodesk
Start Visual LISP Editor VLISP or VLIDE ToolsAutoLISPVisual
LISP Editor
Four Windows Text editor: type your program
(more than one can be open) Visual LISP console: type
variable names or pieces of code
Trace window: use while debugging
Build Output window: warnings and error messages
Visual LISP Editor
![Page 7: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/7.jpg)
7© 2006 Autodesk
Visual LISP - Color Coding
Color Used for
Blue AutoLISP functions like /, DEFUN, and SETQ
Magenta Strings, which are always between quotation marks
Black User-created items, such as program variables and function names
Green Integer values
Teal Real numbers, which must have a decimal point
Maroon on gray Comments, which are preceded by a semicolon
![Page 8: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/8.jpg)
8© 2006 Autodesk
VLISP Tools
![Page 9: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/9.jpg)
9© 2006 Autodesk
AutoLISP by Example
Or format like this:
![Page 10: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/10.jpg)
10© 2006 Autodesk
Program Structure
• Programs Contain AutoLISP Functions DEFUN, SETVAR, +, and -
• Function Name Is Preceded by an Opening Parenthesis (defun, or (command, or (+
• Balance Parentheses
• Separate Atoms with Spaces or Quotation Marks
• Functions Are Followed by Arguments, if They’re Necessary
![Page 11: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/11.jpg)
11© 2006 Autodesk
Breaking Down the Code
(defun c:00() Defun is an AutoLISP expression
C: means “command” not drive
(setvar “osmode” 4143) Setvar is an AutoLISP expression
“osmode” is a system variable
4143 is a bit sum setting
) Closes opening parenthesis in line 1
![Page 12: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/12.jpg)
12© 2006 Autodesk
Bit Sum 1, 2, 4, 8, 16, 32, 64, 128, etc.
Why 4143?
End, Mid, Cen, Nod, Int, and Ext as running osnaps
Type OSMODE at the command line
Returns 4143
1+2+4+8+32+4098 = 4143.
![Page 13: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/13.jpg)
13© 2006 Autodesk
Building on the Basics
![Page 14: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/14.jpg)
14© 2006 Autodesk
System Variables
Over 550 in AutoCAD 2007
Examples: APERTURE; AUPREC; LUPREC
Some saved in drawing, others in Registry
(getvar “aperture”) (setvar “aperture” 3)
Environment Variables – Operating System
Examples: “MaxHatch” "HideSystemPrinters"
Saved in Registry
(getenv “MaxHatch”) (setenv “MaxHatch” “100000000”)
Background - Variables
![Page 15: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/15.jpg)
15© 2006 Autodesk
Program Variables
Defined by the programmer using SETQ
Global (defun C:MID ()
Retain their value when program terminates
Useful for debugging
Often Changed to Local
Can be used to create a persistent default value
Local (defun C:MID (/ os ap m)
Return to nil when program terminates
!variable at command line to get value
Background - Variables
![Page 16: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/16.jpg)
16© 2006 Autodesk
SETQ
![Page 17: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/17.jpg)
17© 2006 Autodesk
Deciphering the Code
![Page 18: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/18.jpg)
18© 2006 Autodesk
Creating New Coordinates
Structural Lumber Symbol
Get Point1 and Point 2 from User
Create Point3 from X of Point 1 and Y of Point2
Create Point4 from X of Point 2 and Y of Point1
![Page 19: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/19.jpg)
19© 2006 Autodesk
Creating New Coordinate Points - LIST
![Page 20: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/20.jpg)
20© 2006 Autodesk
Creating New Points - POLAR
Dimension Between Walls
![Page 21: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/21.jpg)
21© 2006 Autodesk
Creating New Points - POLAR
Polar pt Angle Distance
![Page 22: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/22.jpg)
22© 2006 Autodesk
Calculations
![Page 23: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/23.jpg)
23© 2006 Autodesk
Calculations - More
![Page 24: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/24.jpg)
24© 2006 Autodesk
Calculations in Programs
![Page 25: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/25.jpg)
25© 2006 Autodesk
Angles
Radians vs. Degrees
AutoLISP uses Radians
Radian: included angle of arc with length of 1
Degrees Radians
0 0
90 Pi/2
180 Pi
270 3pi/2
360 2pi
![Page 26: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/26.jpg)
26© 2006 Autodesk
Angles
Radian Problems
![Page 27: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/27.jpg)
27© 2006 Autodesk
Angles
Radians vs. Degrees Conversion Functions
![Page 28: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/28.jpg)
28© 2006 Autodesk
Transparent Commands
Two Methods
‘I2M will work for many command functions
Call non-command functions as Lisp
![Page 29: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/29.jpg)
29© 2006 Autodesk
Combining Programs
SSECT and RC
![Page 30: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/30.jpg)
30© 2006 Autodesk
Ten Basic Rules
1. Save as ASCII.
2. Use Equal Number of Opening and Closing Parens.
3. DEFUN Means Define Function; C:ZX Is a Command.
4. Balance Quotation Marks.
5. Backslash Is a Control Character: \\ or / for Folders.
6. Leave Space After Forward Slash for Local Variables.
7. \n Controls New Line in Prompt and Is Lowercase.
8. Use Semicolons for Comments.
9. Get Values at Beginning; Use SETQ to Save Them.
10. Don't Get Discouraged!
![Page 31: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/31.jpg)
31© 2006 Autodesk
Automatic Loading
APPLOAD Startup Suite
Any LSP program placed by user
ACAD.lsp
Loads when AutoCAD starts IF in the path
Loads in subsequent drawings IF ACADLSPASDOC=1
ACADDOC.lsp
Loads with each drawing IF in the path
(There are some others, but you probably don’t want to mess them up)
![Page 32: © 2006 Autodesk AutoCAD: Secrets Every User Should Know Chapter 8 - AutoLISP by Example: Getting Started](https://reader036.vdocuments.site/reader036/viewer/2022062321/56649e9e5503460f94b9f209/html5/thumbnails/32.jpg)
32© 2006 Autodesk
Automatic Loading
Startup Functions