0% found this document useful (0 votes)
979 views

Autolisp Example Programs

This document provides examples of Autolisp programs and functions. It includes functions for calculating logs, greatest common divisors, remainders, minimums, and maximums of numbers. It also includes two example programs, one to calculate a user's age in days and weeks, and another to calculate the distance between two points picked in AutoCAD. It discusses alternative ways to calculate distances between points using the distance function or getdist command.

Uploaded by

ntqqjty
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
979 views

Autolisp Example Programs

This document provides examples of Autolisp programs and functions. It includes functions for calculating logs, greatest common divisors, remainders, minimums, and maximums of numbers. It also includes two example programs, one to calculate a user's age in days and weeks, and another to calculate the distance between two points picked in AutoCAD. It discusses alternative ways to calculate distances between points using the distance function or getdist command.

Uploaded by

ntqqjty
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Autolisp Example Programs

Some more functions:


Log function:
log Returns the natural log of a number.
Eg: (log 10) ;Returns 2.30259
(log 20) ;Returns 2.99573
Argument can be real or integer.
Gcd function:
gcd Returns the greatest common denominator of two
integer numbers.
(gcd 12 24) ;Returns 6
Argument must be an integer number.

Some more functions:


Rem function:
Returns the reminder after dividing number 1 by number 2
and so on.
Syntax: (rem num1 num2 ...)
Eg: (rem 10 3) ;Returns 1
(rem 10.7 7) ;Returns 3.7
(rem 10.7 2 3.3 4.2); Returns 0.7
Min and max functions:
min gets the minimum number from a list of numbers.
Eg: (min 3 2 6 8) ;Returns 2
max gets the maximum number from a list of numbers.
Eg: (max 3 2 6 8) ;Returns 8
Argument can be integer or real.

Example Program 1:
Description: To obtain age of the user and convert it into
days and weeks.
(defun C:myProg()
(setq intAge (getint "\n Enter your Age: "))
(setq intDays (* intAge 365))
(setq intWeeks (/ intDays 7))
(princ (strcat "\n You are " (itoa intDays) " Days old!"))
(princ (strcat "\n You are " (itoa intWeeks) " Weeks old!"))
(princ)
)

Execution:
Command: (load "myProg")<enter>
Command: myProg<enter>

Command: Enter your Age: 39<enter>


Command: You are 14235 Days old!
Command: You are 2033 Weeks old!

Example Program 2:
(defun C:myprog1()
(setq pt1(getpoint "\n Pick First Point: "))
(setq pt2(getpoint "\n Pick Last Point: "))
(setq x1 (car pt1)) ;x-coord of point one
(setq x2 (car pt2)) ;x-coord of point two
(setq y1 (cadr pt1)) ;y-coord of point one
(setq y2 (cadr pt2)) ;y-coord of point two
(setq xdis (- x2 x1)) ;distance in x direction between points
(setq ydis (- y2 y1)) ;distance in y direction between points
(setq slpdis (sqrt (+ (expt xdis 2.0) (expt ydis 2.0)))) ;Asq+Bsq=C
sq
(princ (strcat "\n Distance = " (rtos slpdis 4 4)))
(princ)
)

Execution:
Command: (load myprog1)<enter>
Command: myprog1<enter>

Command: Pick First Point:<pick>


Command: Pick Last Point:<pick>
Command: Distance = 3'-4 1/2"

Alternative programs to get distance


between two points:
1)Using distance function:
Distance
Measures the distance from two known points.
eg:
(setq dist1 (distance pnt1 pnt2)) ;returns the distance
between pnt1 and pnt2 and assigns the distance to a
variable called dist1.

Alternative programs to get distance


between two points:
2)Using getdist user input function:
getdist
Needs a value either by picking on the screen or a
number from the keyboard.
eg:
(getdist "\nHow long is the line? :") ;Pick two points or
type a length and AutoLisp will return that length.

Alternative programs to get distance


between two points:
3)Using distance command through autolisp:

command
Eg: (command "dist" pt1 pt2)(princ)

You might also like