Autolisp Programming Notes PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

 Why AutoLISP?

One of the AutoCADs greatest assets is its adaptability. You can control just about every aspect
of AutoCADs operations, from appearance of its drawing editor to its variety of menus.Key
element of this adaptability is AutoCADs built-in programming language–autolisp. With
autolisp you can virtually write your own commands and redefine others.

 What Is AutoLISP?
Autolisp is part of LISP programming. LISP programming is used in artificial intelligence
programming. Meaning of LISP is List Processing.Autolisp program can be written on any editor
like notepad,wordpad,Norton,visual lisp editor,etc.

Steps To Load A Lisp File In AutoCAD Application


•Write program on any editor
•Save the program with extension .lsp in ASCII text form and not in rich text format(.rtf) or
document format (.doc)
•Load the file from autocad menu (tools>visual lisp>load application)
•When message of lisp file successfully loaded is displayed ,type command given in program
at autocad command prompt,the program is executed.
Applications of Autolisp
• AutoLISP is based on the LISP programming language, which is
simple to learn and very powerful. Because AutoCAD has a built-in
LISP interpreter, you can enter AutoLISP code at the Command
prompt or load AutoLISP code from external files.
• AutoLISP is an application interface for automating of design tasks.
When an AutoLISP application is loaded, it functions in its own
namespace for each drawing that is open. A namespace is an
insulated environment keeping AutoLISP applications that are
specific to one drawing from having symbol or variable name and
value conflicts with those in another drawing. For example, the
following line of code sets a different value to the symbol a when
executed in each open drawing.
• (setq a (getvar "DWGNAME"))
AutoLISP applications can prompt the user for input, access built-in AutoCAD
commands directly, and modify or create objects directly in the drawing database.
By creating AutoLISP routines you can add discipline-specific or workflow driven
commands to AutoCAD. Some of the standard AutoCAD commands are actually
AutoLISP applications.
You may choose to experiment by entering code at the Command prompt, which
allows you to see the results immediately. This makes AutoLISP an easy language to
experiment with, regardless of your programming experience.
AutoLISP provides three file formats for applications:
•Reading an LSP file (.lsp)—an ASCII text file that contains AutoLISP program code.
•Reading an FAS file (.fas)—a binary, compiled version of a single LSP program file.
•Reading a VLX file (.vlx)—a compiled set of one or more LSP and/or dialog control
language (DCL) files. (VLX and DCL files are not supported in AutoCAD for Mac.)
Note: Like-named AutoLISP application files are loaded based on their Modified
time stamp; the LSP, FAS, or VLX file with the most recent time stamp is loaded
unless you specify the full file name (including the file name extension).
 Data Types In Autolisp

1.STRINGS: A set of characters is string e.g. (“computer graphics”)

2. INTEGER: A whole number e.g. 487

3.REAL :Decimal numbers e.g. 456.678,57.23,28.0

4.LIST:Group of data e.g.(1 2 3 4)

5. SYMBOL: Variable name

6.FILE DESCRIPTOR :File handling functions

7.OBJECT NAMES: Elements defined in AutoCAD e.g. line , rectangle , circle , polygon

8.PICKSET: Temporary group of entities selected during select commands.


String Functions
Functions: substr,strcase,strlen,strcat

1.substr: This function retrieves part of string.

Syntax: (substr “ string” startpoint number of character)

e.g. (substr “Machine Drawing” 1 3) – would return “Mac”


(substr “Machine Drawing” 8 4) – would return “Draw”

2.strcase:This function converts to upper case or lower case

Syntax: (strcase “string” flag)

Flag: To set to upper case or lower case


Flag- T –sets to lower case
nil–sets to upper case

e.g.(strcase “ Machine Drawing” T) – would return “machine drawing”


(strcase “ Machine Drawing” nil) – would return “MACHINE DRAWING”
3. strlen:This function returns the length of string in character

Syntax: (strlen “string”)


e.g. (strlen “machine ”)-would return 7
(strlen “ ”)- would return 0

4. strcat:This function adds multiple strings together to form on string.

Syntax: (strcat “machine” “drawing” “graphics”) -would return “machine


drawing graphics”
Math Functions
Functions: +, -, /, *,1+,1-,cos,sin,atan,sqrt,expt

1. + : Addition 4. / :Division
Syntax: (/ 2 5 )
Syntax: (+ 2 5 6)
5. 1+ : Returns value increased by 1
2.- : Subtraction Syntax: (1+ 3)- returns 4

Syntax: (- 2 5 6) 6. 1- :Returns value decreased by 1


Syntax: (1- 3)-Returns 2
3.* : Multiplcation

Syntax: (* 2 5 )
7.cosine: returns cosine of no. in radians 10.sqrt: returns square root of number

Syntax: (cos pi) – returns -1.0 Syntax: (sqrt 4) – returns 2


(cos (+ pi pi))– returns 1.0
11.expt: returns a number raised to power
8.sine: returns sine of no. in radians
Syntax: (expt 2 2.0) –returns 4.0
Syntax: (sin 1.0) – returns 0.841471

9.atan: returns arc tangent of no. in radians

Syntax: (atan 1) – returns 0.783598


;Sample Program On Mathematical Operation

(defun c:result()
(setq a(getreal"\n enter 1st no:")
b(getreal"\nenter 2nd no:")
c(getreal"\n enter 3rd no:")
total(+ a b c)
per(*(/ total 300.0) 100.0)
)
)
 Data Type Conversions
Function : abs , atof , atoi , fix , rtos , float , itoa

1.abs:This function returns absolute value of no.

Syntax: (abs 346)-returns 346


(abs -346.3)-returns 346.3

2.atoi:This function converts string to integer.

Syntax: (atoi “346”)- returns 346


(atoi “346.3”)- returns 346.3
(atoi “man345”) – returns 0

3.itoa: This function coverts an integer to string.

Syntax: (itoa 346)-returns “346”


(itoa 5)-returns “5”
4.atof:This function converts string to real.

Syntax: (atof “ 5”)-returns 5.0


(abs “45”)-returns 45.0

5.fix :This function converts real to integer.

Syntax: (fix 346.0)-returns 346


(fix 42.5)-returns 42

6.float:This function converts integer to real.

Syntax: (abs 346)-returns 346.0


(abs 42)-returns 42.0
;Sample program for string function & Data type conversion

(defun c:age()
(setq age(getint"\nenter your age:")
days(* age 365)
weeks(/ days 7)
)
(princ(strcat"\nyou are"(itoa days)"days old!"))
(princ(strcat"\nyou are"(itoa weeks)“weeks old!!"))
(princ)
)
LIST Filtering Functions
Functions: car, cdr, cadr, caddr, caar, cddr

1.car: (core address register)-Function returns first item of list


Syntax: (car(1 2 3))-returns 1

2.cdr: (core data register)-Function returns list that includes everything but first item
Syntax: (cdr(1 2 3))-returns (2 3)

3.cadr: Function returns second item of list


Syntax: (cadr(1 2 3))-returns 2

4.caddr:Function returns third item of list


Syntax: (caddr(1 2 3))-returns 3

5.caar:-Function returns first item of first list


Syntax: (caar(1 2 3)(4 5 6))-returns 1

6.cddr: Function returns everything after second item in list


Syntax: (car(1 2 3 4 5 6))-returns (3 4 5 6)
;sample program for List filtering
;To draw a rectangle (Given: Two Corner points)

(defun C:RECTC()
(setq pt1 (getpoint "\nEnter a corner : ")
pt2 (getpoint "\nEnter diagonal corner : ")
pt3 (list (car pt2) (cadr pt1))
pt4 (list (car pt1) (cadr pt2))
)
(command "line" pt1 pt3 pt2 pt4 pt1 "")
)
Defining a point in polar form

Syntax: (setq pt1 (polar pt0 theta radius))

;To Draw A Rectangle : Given Length & Width


;Sample Program To Define A Point In Polar Form
(defun C:RECTLB()
(setq pt1 (getpoint "\nEnter a corner : ")
L (getreal "\nEnter length of rectangle : ")
W(getreal "\nEnter width of rectangle : ")
pt3 (polar pt1 0 L)
pt2 (polar pt3 (/ pi 2.0) W)
pt4 (polar pt2 pi L)
)
(command "line" pt1 pt3 pt2 pt4 "c")
)
;To Draw A Plate With Central Hole Given Length, Width, Hole
Diameter
;Sample Program For List Flitering & Polar Point Definition
(defun C:PLATE()
(setq pt1 (getpoint "\nEnter Plate corner : ")
L (getreal "\nEnter Length of Plate : ")
W (getreal "\nEnter Width of Plate : ")
d (getreal "\nEnter Hole Diameter : ")
pt3 (polar pt1 0 L)
pt2 (polar pt3 (/ pi 2.0) W)
pt4 (polar pt2 pi L)
)
(command "pline" pt1 pt3 pt2 pt4 "c")
(setq pc (list (+ (car pt1) (/ L 2.0)) (+ (cadr pt1) (/ W 2.0))))
(command "circle" pc (/ d 2.0))
)
Selection of Entities

Functions: entsel,ssget

1.entsel: This function prompts for user to pick one entity

Syntax: (entsel)

2.ssget: This function prompts for user to select more than one entity

Syntax: (ssget “w”)-prompt user for selection using window


(ssget “c”)-prompt user for selection using crossing mode
(ssget “L”)-selects last entity
(ssget “P”)-selects previous entity
(ssget “X”)-selects all entities in the drawing.
(defun c:myprog()
(if(setq myent(entsel))
(progn
(setq pt1(getpoint"\base point:")
pt2(getpoint"\ndisplacement:")
)
(command "move" (car myent) "" pt1 pt2)
)
(alert "please select entity!")
)
(princ)
)
(defun c:ro()
(if(setq myent(ssget "l"))
(progn
(setq pt1(getpoint"\base point")
pt2(getpoint"\select point of copy:")
)
(command "copy" myent "" "m" pt1 pt2)
)
(alert "please select entities!")
)
(princ)
)
 Decision Making With Auto Lisp

The IF function first test to weather to condition is mate and then performs one
option or another depending on the out come of test .

These sequence of operation is often referred as If- then -else conditional


statement . If a condition is met then perform computation A, else perform
computation B.
Syntax :
( (if (test _expression )
(expression )(optional__Expression)
)
;program on decision making
(defun c:quadrant()
(setq P0(getpoint"\nEnter P0:"))
(setq X(car P0))
(setq Y(cadr P0))
(if (and (> X 0) (> Y 0))
(prompt"\npoint is in the first quadrant:"))
(if (and (< X 0) (> Y 0))
(prompt"\npoint is in the second quadrant:"))
(if (and (< X 0) (< Y 0))
(prompt"\npoint is in the third quadrant:"))
(if (and (> X 0) (< Y 0))
(prompt"\npoint is in the fourth quadrant:"))
)
(defun c:cal()
(print "1. addition")
(print "2. subtraction")
(print "3. multiplication")
(print "4. division")
(setq ch(getint"\nenter your choice:")
a(getreal"\nenter first no:")
b(getreal"\nenter second no:")
)
(if(= ch 1)
(progn
(setq c(+ a b))
(print c)
))
(if(= ch 2)
(progn
(setq c(- a b))
(print c)
))
(if(= ch 3)
(progn
(setq c(* a b))
(print c)
))
(if(= ch 4)
(progn
(setq c(/ a b))
(print c)
))
)
THANK YOU !!!

You might also like