View Full Version : LISP to calculate area of rectangle
CorkedSoup
2006-06-19, 02:58 PM
A colleague of mine, came to me requesting a lisp that calculate's the area of a rectangle. He wants to be able to run the routine, start the rectangle, be able to see the area of the rectangle before closing the rectangle.
Any suggestions?
T.Willey
2006-06-19, 05:05 PM
I think you will have to use (getpoint.... then use (grread... while you use (getcorner...
I don't know if that is possible, but that is what came to mind while reading your post.
rkmcswain
2006-06-19, 05:43 PM
A colleague of mine, came to me requesting a lisp that calculate's the area of a rectangle. He wants to be able to run the routine, start the rectangle, be able to see the area of the rectangle before closing the rectangle.
Any suggestions?
Try this. You will have to wrap this in a (defun) if desired. While the command is running, the area will be displayed in the status bar.
; --------------------------------
(setq r0 0.0
r45 (* 0.25 pi)
r90 (* 0.5 pi)
r135 (* 0.75 pi)
r180 pi
r225 (* 1.25 pi)
r270 (* 1.5 pi)
r315 (* 1.75 pi)
)
(setq pt1 (getpoint "\nSelect first point: ") time T)
(while time
(setq drag (grread t 1 1))
(cond ((= (car drag) 5) ;<- moving cursor
(setq pt2 (cadr drag))
(setq xdim (abs (- (car pt1)(car pt2))))
(setq ydim (abs (- (cadr pt1)(cadr pt2))))
(setq myarea (* xdim ydim))
(grtext -1 (strcat "Area = " (rtos myarea)))
(setq vec (angle pt1 pt2))
(cond ((and (>= vec 0.0) (< vec r90))
(setq x r0 y r90)
)
((and (>= vec r90) (< vec r180))
(setq x r180 y r90)
)
((and (>= vec r180) (< vec r270))
(setq x r180 y r270)
)
((>= vec r270)
(setq x r0 y r270)
)
)
(setq pt3 (polar pt1 x xdim) pt4 (polar pt1 y ydim))
(redraw)
(grvecs (list 7 pt1 pt3 7 pt3 pt2 7 pt2 pt4 7 pt4 pt1))
)
((= (car drag) 3) ;<- picked point
(setq time nil)
(command "._pline" pt1 pt3 pt2 pt4 "_C")
)
)
)
CorkedSoup
2006-06-19, 05:59 PM
Thats awesome, thanks. Now I can't get it to work. :S :roll:
I'm just getting into writing Lisps ... and I'm not that dangerous yet.
rkmcswain
2006-06-19, 06:10 PM
Now I can't get it to work.
What doesn't work about it?
CorkedSoup
2006-06-19, 06:18 PM
I put the code into a text file, added the defun d:rar to it, and I get the message below.
Command: _appload rar.lsp successfully loaded.
Command: ; error: An error has occurred inside the *error* functionbad
function: "malformed list on input"
CorkedSoup
2006-06-19, 06:23 PM
What doesn't work about it?
This is what I saved as rar.lsp
(defun c:rar ()
; --------------------------------
(setq r0 0.0
r45 (* 0.25 pi)
r90 (* 0.5 pi)
r135 (* 0.75 pi)
r180 pi
r225 (* 1.25 pi)
r270 (* 1.5 pi)
r315 (* 1.75 pi)
)
(setq pt1 (getpoint "\nSelect first point: ") time T)
(while time
(setq drag (grread t 1 1))
(cond ((= (car drag) 5) ;<- moving cursor
(setq pt2 (cadr drag))
(setq xdim (abs (- (car pt1)(car pt2))))
(setq ydim (abs (- (cadr pt1)(cadr pt2))))
(setq myarea (* xdim ydim))
(grtext -1 (strcat "Area = " (rtos myarea)))
(setq vec (angle pt1 pt2))
(cond ((and (>= vec 0.0) (< vec r90))
(setq x r0 y r90)
)
((and (>= vec r90) (< vec r180))
(setq x r180 y r90)
)
((and (>= vec r180) (< vec r270))
(setq x r180 y r270)
)
((>= vec r270)
(setq x r0 y r270)
)
)
(setq pt3 (polar pt1 x xdim) pt4 (polar pt1 y ydim))
(redraw)
(grvecs (list 7 pt1 pt3 7 pt3 pt2 7 pt2 pt4 7 pt4 pt1))
)
((= (car drag) 3) ;<- picked point
(setq time nil)
(command "._pline" pt1 pt3 pt2 pt4 "_C")
)
)
)
rkmcswain
2006-06-19, 06:56 PM
John,
You are missing the closing parenthesis at the end of the file.
You may also want to make all the variables local by changing the defun line to:
(defun c:rar (/ DRAG MYAREA PT1 PT2 PT3 PT4 R0 R135 R180 R225 R270 R315 R45 R90 TIME VEC X XDIM Y YDIM)
Let's see if that fixes it.
FYI: The Visual Lisp editor would have helped you locate the error and compile the list of variables.
CorkedSoup
2006-06-19, 06:58 PM
Thats wonderful! Thanks man.
CorkedSoup
2006-06-19, 07:08 PM
Now to add another aspect to it ...
Can that translate the size of the rectangle as well??
And what about overiding the units to read back in metres to two decimal places.
Robert.Hall
2006-06-19, 08:28 PM
This works with a limitation.
If you have done anything to the status bar, then you will not see the area listed.
My status bar is set to show the current dimscale.
Running this routine simply makes the status bar flicker and the user does not
have a chance to see the text.
rkmcswain
2006-06-19, 10:03 PM
Can that translate the size of the rectangle as well??
I'm not sure what you mean by "translate the size".
And what about overiding the units to read back in metres to two decimal places.
It simply reports the area in square units using the current precision settings.
The (rtos myarea) is where you can change the precision, look up RTOS in the developers guide.
rkmcswain
2006-06-19, 10:11 PM
If you have done anything to the status bar, then you will not see the area listed.
My status bar is set to show the current dimscale.
Running this routine simply makes the status bar flicker and the user does not
have a chance to see the text.
On what AutoCAD version?
I used this line to set the modemacro
(setvar "modemacro" "$(getvar,dimstyle)")
...and the lisp still reports the area from the moment the first point in picked until the second point is picked. It reverts back to displaying the dimstyle after the command is over. This is on 2006 and 2007.
Of course the HELP file does have a disclaimer:
Because these functions depend on code in AutoCAD, their operation can be expected to change from release to release. There is no guarantee that applications calling these functions will be upward compatible. Also, they depend on current hardware configurations. In particular, applications that call grtext are not likely to work the same on all configurations
CorkedSoup
2006-06-20, 01:15 PM
Translate being, the actual size of the rectangle, even if it's a properties of object command after the rectangle has been established.
Powered by vBulletin® Version 4.1.11 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.