Autolisp Programming with AutoCAD: Customizing Autocad Using Auto Lisp Codes for Creating a Flywheel Drawing

Autolisp Programming with AutoCAD: Customizing Autocad Using Auto Lisp Codes for Creating a Flywheel Drawing

For creating only front view of a small flywheel will take at least eight commands (!) in combination with some precise cursor location selection. Imagine you are working with a flywheel manufacturer’s design department, where day in and day out you have to prepare flywheel drawing. You can actually create the whole drawing of flywheel by only one command if you could write an auto lisp program somewhat similar to what we are going to discuss here.

If you are new to AutoCAD customization, you can find some autocad customization basics here.

Writing the auto lisp program

We will write an auto lisp program, which can create front view of a flywheel. The auto lisp program will be such that it will ask for user input for the diameter of the flywheel and for the number of fixing hole. The program will look somewhat like below:

(defun C:flywheel()

(setq a(getdist “enter the dia of the flywheel:”))

(setq b(getint “enter the number of M10 fixing hole:”))

(setq c(getpoint “click where the centre point of the flywheel will be?”))

(setq d(/ a 2))

(command “circle” c d)

(command “circle” c 50)

(setq e(/ 360 b))

(setq g(car c))

(setq h(cadr c))

(setq i(+ g 0))

(setq j(+ h 100))

(setq f(list i j))

(command “circle” f 12)

(setq eee(/ pi 180))

(setq ee(* eee e))

(setq nn 0)

(repeat b (setq nn (+ 1 nn))

(setq eeee(* nn ee))

(setq oo(sin eeee))

(setq pp(cos eeee))

(setq ooo oo)

(setq ppp pp)

(setq m(* 100 ooo))

(setq n(* 100 ppp))

(setq k(+ i m))

(setq l(+ h n))

(setq f1(list k l))

(command “circle” f1 12))

(command “circle” c 100)

(command “circle” c (- d 200)))

Discussion about the program

At the first line, defun is an auto lisp command, which is used to define a user define function. The word after the letter C: is used to call the function from autocad. Here it is flywheel, so after loading the program, if you type flywheel at the command prompt of autocad, the whole program should be executed. Please note that the defun function starts with “(“at the first line of the program and ends with the “)” at the last line of the program.

Another important command used here is setq, it is used for assigning something to a variable. Say, you want to assign 5 with the variable X, and then you may have to go like:

(setq X 5)

getpoint, getdist and getint are used for getting user inputs in terms of points, distances and integer values respectively.

Command function is used for using autocad commands in auto lisp codes. Observe the individual autocad command behavior carefully before using it with command function.

We have used the loop statement repeat in order to draw multiple circles as per user inputs. Syntax of the repeat is:

(repeat n expr…])

n indicates the number of times the expr.. needs to be repeated. Expr.. Typically are the combinations of auto lisp codes to be executed under repetition cycles. In our case, we have created for holes for bolting the flywheel.

The program discussed here is just an approach to write auto lisp codes for your specific needs and there are many number of combinations of codes are possible to achieve a task.