AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Calculating Points - Polar

The Polar function is defined in the AutoCAD Customization
manual as follows :

POLAR : Returns the UCS 3D point at a specified angle and
distance from a point.

(polar pt ang dist)

This, I believe, is one of the most useful functions in the AutoCAD stable.
In a nutshell, you feed it a point, tell it the angle and distance from that point that you want to be, and it will return the second point.
Can you imagine having to do that using car, cadr, etc.
First you would have to break the list down into each separate component, do all the calculations on each individual item, and then re-construct the list.
What a pain!!

Following is an example of how to use POLAR to construct a simple square
or rectangle from values input by the user :

(defun DTR (a)					;degrees to radians function
(* PI (/ a 180.0))
);defun
;=========================================
(defun C:BOX1 (/ IP P1 P2 P3 LENGTH HEIGHT	;define function and declare
		OLDSNAP OLDBLIP OLDLIGHT)	;variables as local

(setq OLDSNAP (getvar "OSMODE")			;store system variables
      OLDBLIP (getvar "BLIPMODE")
     OLDLIGHT (getvar "HIGHLIGHT")
);setq
;=========================================
(setvar "CMDECHO" 0)				;change system variables
(setvar "BLIPMODE" 0)
(setq IP (getpoint "\nInsertion Point: "))	;get insertion point
(setvar "OSMODE" 0)				;switch off snap
(setq LENGTH (getreal "\nEnter Length: ")	;get length of box
      HEIGHT (getreal "\nEnter Height: ")	;get height of box
);setq
;=========================================
(setq P1 (polar IP (DTR 0.0) LENGTH)		;calculate first corner
      P2 (polar P1 (DTR 90.0) HEIGHT)		;calculate second corner
      P3 (polar P2 (DTR 180.0) LENGTH)		;calculate third corner
);setq
;=========================================
(command "PLINE" IP "W" "" ""
	 P1 P2 P3 "C"
);command					;draw the box
;=========================================
(prompt "\nRotation Angle: ")			;prompt the user for rotation
(command "ROTATE" "LAST" "" IP pause)		;rotate the box
;=========================================
(setvar "OSMODE" OLDSNAP)			;reset system variables
(setvar "BLIPMODE" OLDBLIP)
(setvar "HIGHLIGHT" OLDLIGHT)
(princ)						;exit quietly
);defun

To save you typing here's the source code : Box1 Lisp (1 Kb)

As you can see, we first need to write a function to convert radians to degrees. This is because when we deal with angles in AutoLISP they must be in Radians.

Hint : This could be a Global function loaded from your Acad Lisp file.

Now to our main routine.

The first thing that we do is define the function and declare all the variables as local. (Only used within this program.)
Then we save certain system variables, before changing them, so that we can reset them later.
Next we ask the user for Insertion Point, Length and Height of the box.
We then use the POLAR function to calculate the remaining 3 corners of the box. (PT1, PT2 and PT3).
Then, just to be kind, we allow the user to rotate the box.
(That's why we drew the box using a Pline.)
Lastly, we reset all the system variables that we changed back to their original state.

 
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