AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Page I. Page II. Page III. Home.

The Basics in a Nutshell - Page IV.

Snap settings in AutoCad can cause havoc with an AutoLisp routine.
Therefore, it's a very good idea to switch off the snap at the beginning of any AutoLisp routine and only switch it on when it is required. Blips on your screen are also very annoying, so switch them off too. Please, just remember to return the AutoCad settings that you change, back to their original state before exiting your routine.
Let's edit our routine to include this :

(defun c:testbeam ()
	;define the function
;********************************************************
	
	;Save System Variables

	(setq oldsnap (getvar "osmode"))
	;save snap settings

	(setq oldblipmode (getvar "blipmode"))
	;save blipmode setting

;********************************************************
	;Switch OFF System Variables

	(setvar "osmode" 0)
	;Switch OFF snap

	(setvar "blipmode" 0)
	;Switch OFF Blipmode
	
;********************************************************
	;Get User Inputs	
	
	(setq lb (getdist "\nLength of Beam : "))
	;get the length of the beam

	(setq hb (getdist "\nHeight of Beam : "))
	;get the height of the beam

	(setq wt (getdist "\nFlange Thickness : "))
	;get the thickness of the flange

	(setq ep (getdist "\nEnd Plate Thickness : "))
	;get the thickness of the end plate

	(setq nl (getdist "\nLength of Notch : "))
	;get the length of notch

	(setq nd (getdist "\nDepth of Notch : "))
	;get the depth of the notch

	;End of User Inputs
;*********************************************************
	;Get Insertion Point
	
	(setvar "osmode" 32)
	;switch ON snap
	
	(setq ip (getpoint "\nInsertion Point : "))
	;get the insertion point
	
	(setvar "osmode" 0)
	;switch OFF snap
	
;********************************************************
	;Start of Polar Calculations

	(setq p2  (polar ip (dtr 180.0) (- (/ lb 2) nl)))
	(setq p3  (polar p2 (dtr 270.0) wt))
	(setq p4  (polar p2 (dtr 270.0) nd))
	(setq p5  (polar p4 (dtr 180.0) nl))
	(setq p6  (polar p5 (dtr 180.0) ep))
	(setq p7  (polar p6 (dtr 270.0) (- hb nd)))
	(setq p8  (polar p7 (dtr 0.0) ep))
	(setq p9  (polar p8 (dtr 90.0) wt))
	(setq p10 (polar p9 (dtr 0.0) lb))
	(setq p11 (polar p8 (dtr 0.0) lb))
	(setq p12 (polar p11 (dtr 0.0) ep))
	(setq p13 (polar p12 (dtr 90.0) (- hb nd)))
	(setq p14 (polar p13 (dtr 180.0) ep))
	(setq p15 (polar p14 (dtr 180.0) nl))
	(setq p16 (polar p15 (dtr 90.0) (- nd wt)))
	(setq p17 (polar p16 (dtr 90.0) wt))
	;End of Polar Calculations
;**********************************************************
	;Start of Command Function

	(command "Line" ip p2 p4 p6 p7 p12 p13 p15 p17 "c"
		 "Line" p3 p16 ""
		 "Line" p9 p10 ""
		 "Line" p5 p8 ""
		 "Line" p11 p14 ""
	)        ;End Command
	;End of Command Function
;**********************************************************
	
	;Reset System Variable

	(setvar "osmode" oldsnap)
	;Reset snap

	(setvar "blipmode" oldblipmode)
	;Reset blipmode
	
;*********************************************************
	(princ)
	;finish cleanly

)	;end of defun

;**********************************************************
;This function converts Degrees to Radians.

(defun dtr (x)
	;define degrees to radians function

	(* pi (/ x 180.0))
	;divide the angle by 180 then
	;multiply the result by the constant PI

)	;end of function

;**********************************************************
(princ)	;load cleanly
;**********************************************************

Load and run the program. If you were having problems, they should now have disappeared. (I hope!!). Did you notice how we switched on the snap just before asking for the insertion point?
This, of course, is to allow the user to snap to an insertion point.

We still have another problem though. What would happen if the user entered an illegal value, such as zero, or a negative number.
This could cause very strange results.

To guard against this we will use the (initget) function.
Here's how it works :

	(initget (+ 1 2 4))
	(setq lb (getdist "\nLength of Beam : "))

This function works on the "sum of the bits" system.

	1 = Disallows the user from pressing "Enter".
	2 = Disallows the user from entering "Zero".
	3 = Disallows the user from entering a "Negative Number".

The same function could have been written like this :

	(initget 7)
	(setq lb (getdist "\nLength of Beam : "))

Let's add this function to our routine :

(defun c:testbeam ()
	;define the function
;********************************************************
	;Save System Variables

	(setq oldsnap (getvar "osmode"))
	;save snap settings

	(setq oldblipmode (getvar "blipmode"))
	;save blipmode setting

;********************************************************
	;Switch OFF System Variables

	(setvar "osmode" 0)
	;Switch OFF snap

	(setvar "blipmode" 0)
	;Switch OFF Blipmode

;********************************************************
	;Get User Inputs	
	
	(initget (+ 1 2 4))
	;check user input
	
	(setq lb (getdist "\nLength of Beam : "))
	;get the length of the beam
	
	(initget (+ 1 2 4))
	;check user input
	
	(setq hb (getdist "\nHeight of Beam : "))
	;get the height of the beam
	
	(initget (+ 1 2 4))
	;check user input
	
	(setq wt (getdist "\nFlange Thickness : "))
	;get the thickness of the flange
	
	(initget (+ 1 2 4))
	;check user input
	
	(setq ep (getdist "\nEnd Plate Thickness : "))
	;get the thickness of the end plate
	
	(initget (+ 1 2 4))
	;check user input
	
	(setq nl (getdist "\nLength of Notch : "))
	;get the length of notch
	
	(initget (+ 1 2 4))
	;check user input
	
	(setq nd (getdist "\nDepth of Notch : "))
	;get the depth of the notch

	;End of User Inputs
;*********************************************************
	;Get Insertion Point

	(setvar "osmode" 32)
	;switch ON snap

	(setq ip (getpoint "\nInsertion Point : "))
	;get the insertion point

	(setvar "osmode" 0)
	;switch OFF snap

;********************************************************
	;Start of Polar Calculations

	(setq p2  (polar ip (dtr 180.0) (- (/ lb 2) nl)))
	(setq p3  (polar p2 (dtr 270.0) wt))
	(setq p4  (polar p2 (dtr 270.0) nd))
	(setq p5  (polar p4 (dtr 180.0) nl))
	(setq p6  (polar p5 (dtr 180.0) ep))
	(setq p7  (polar p6 (dtr 270.0) (- hb nd)))
	(setq p8  (polar p7 (dtr 0.0) ep))
	(setq p9  (polar p8 (dtr 90.0) wt))
	(setq p10 (polar p9 (dtr 0.0) lb))
	(setq p11 (polar p8 (dtr 0.0) lb))
	(setq p12 (polar p11 (dtr 0.0) ep))
	(setq p13 (polar p12 (dtr 90.0) (- hb nd)))
	(setq p14 (polar p13 (dtr 180.0) ep))
	(setq p15 (polar p14 (dtr 180.0) nl))
	(setq p16 (polar p15 (dtr 90.0) (- nd wt)))
	(setq p17 (polar p16 (dtr 90.0) wt))
	;End of Polar Calculations
;**********************************************************
	;Start of Command Function

	(command "Line" ip p2 p4 p6 p7 p12 p13 p15 p17 "c"
		 "Line" p3 p16 ""
		 "Line" p9 p10 ""
		 "Line" p5 p8 ""
		 "Line" p11 p14 ""
	)        ;End Command
	;End of Command Function
;**********************************************************
	;Reset System Variable

	(setvar "osmode" oldsnap)
	;Reset snap

	(setvar "blipmode" oldblipmode)
	;Reset blipmode

;*********************************************************
	(princ)
	;finish cleanly

)	;end of defun

;**********************************************************
;This function converts Degrees to Radians.

(defun dtr (x)
	;define degrees to radians function

	(* pi (/ x 180.0))
	;divide the angle by 180 then
	;multiply the result by the constant PI

)	;end of function

;**********************************************************
(princ)	;load cleanly
;**********************************************************


Now, everything should be running fine except for one thing.
What happens if we want to draw two beams with exactly the same values? We would have to go through the whole routine again, entering exactly the same inputs. We could, of course, set up each variable to default to the last value used, but we would still have to run through the whole routine. Here's a better way. We'll enclose the complete routine in a (while) loop. Have a look :
(defun c:testbeam ()
	;define the function
;********************************************************
	;Save System Variables

	(setq oldsnap (getvar "osmode"))
	;save snap settings

	(setq oldblipmode (getvar "blipmode"))
	;save blipmode setting

;********************************************************
	;Switch OFF System Variables

	(setvar "osmode" 0)
	;Switch OFF snap

	(setvar "blipmode" 0)
	;Switch OFF Blipmode

;********************************************************
	;Get User Inputs	
	
	(initget (+ 1 2 4))
	;check user input
	(setq lb (getdist "\nLength of Beam : "))
	;get the length of the beam

	(initget (+ 1 2 4))
	;check user input
	(setq hb (getdist "\nHeight of Beam : "))
	;get the height of the beam

	(initget (+ 1 2 4))
	;check user input
	(setq wt (getdist "\nFlange Thickness : "))
	;get the thickness of the flange

	(initget (+ 1 2 4))
	;check user input
	(setq ep (getdist "\nEnd Plate Thickness : "))
	;get the thickness of the end plate

	(initget (+ 1 2 4))
	;check user input
	(setq nl (getdist "\nLength of Notch : "))
	;get the length of notch

	(initget (+ 1 2 4))
	;check user input
	(setq nd (getdist "\nDepth of Notch : "))
	;get the depth of the notch

	;End of User Inputs
;*********************************************************
	;Get Insertion Point

	(setvar "osmode" 32)
	;switch ON snap
	
	(while
	;start of while loop	
	
	(setq ip (getpoint "\nInsertion Point : "))
	;get the insertion point

	(setvar "osmode" 0)
	;switch OFF snap

;********************************************************
	;Start of Polar Calculations

	(setq p2  (polar ip (dtr 180.0) (- (/ lb 2) nl)))
	(setq p3  (polar p2 (dtr 270.0) wt))
	(setq p4  (polar p2 (dtr 270.0) nd))
	(setq p5  (polar p4 (dtr 180.0) nl))
	(setq p6  (polar p5 (dtr 180.0) ep))
	(setq p7  (polar p6 (dtr 270.0) (- hb nd)))
	(setq p8  (polar p7 (dtr 0.0) ep))
	(setq p9  (polar p8 (dtr 90.0) wt))
	(setq p10 (polar p9 (dtr 0.0) lb))
	(setq p11 (polar p8 (dtr 0.0) lb))
	(setq p12 (polar p11 (dtr 0.0) ep))
	(setq p13 (polar p12 (dtr 90.0) (- hb nd)))
	(setq p14 (polar p13 (dtr 180.0) ep))
	(setq p15 (polar p14 (dtr 180.0) nl))
	(setq p16 (polar p15 (dtr 90.0) (- nd wt)))
	(setq p17 (polar p16 (dtr 90.0) wt))
	;End of Polar Calculations
;**********************************************************
	;Start of Command Function

	(command "Line" ip p2 p4 p6 p7 p12 p13 p15 p17 "c"
		 "Line" p3 p16 ""
		 "Line" p9 p10 ""
		 "Line" p5 p8 ""
		 "Line" p11 p14 ""
	)        ;End Command
	;End of Command Function
;**********************************************************
	
	(setvar "osmode" 32)
	;Switch ON snap

	);end of while loop
	
;**********************************************************
	;Reset System Variable

	(setvar "osmode" oldsnap)
	;Reset snap

	(setvar "blipmode" oldblipmode)
	;Reset blipmode

;*********************************************************
	(princ)
	;finish cleanly

)	;end of defun

;**********************************************************
;This function converts Degrees to Radians.

(defun dtr (x)
	;define degrees to radians function

	(* pi (/ x 180.0))
	;divide the angle by 180 then
	;multiply the result by the constant PI

)	;end of function

;**********************************************************
(princ)	;load cleanly
;**********************************************************

The programme will now repeat itself indefinetly, asking for an insertion point and drawing a beam, until the user presses enter.
This is how it works. The (while) function will continue to evaluate an expression until the expression evaluates to nil. As long as the user selects a point, the expression is true. But, when the user selects "Enter" the expression returns "nil" and the programme moves out of the loop. (AutoLisp evaluates "Enter" as "nil")


I hope you have understood this tutorial and that it has given you a better understanding of AutoLisp. You will find a more detailed explanation of many of the functions used here in other tutorials on this site.
If you would like the source coding for this AutoLisp Tutorial
then Click Here. Cheers for Now......

Page I. Page II. Page III. Home.
 
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright © 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website