AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Home.

Page II.

Working With Areas

The easiest way of calculating areas in AutoCAD is to simply use the "Area" command. This command though, leaves a lot to be desired when in comes to usage. And the resulting format? Well I'm not going to even mention that. (Hang on, you've got to. That's what this tutorial is all about.) Oop's, silly me.
As you have probably noticed, when you use the "Area" command, it returns the result in the square units of whatever units you are drawing in. eg. If you are drawing in millimeters, it returns the result in square millimeters. No more, and no less.
Now this may suit some folk, but me, no way. Even though I work in millimeters, I want the result returned in square meters AND I want to be able to place the area as text, anywhere in the drawing that suits me AND nicely formatted with a little square meter thingymebob at the end .
So, what do we do? Easy, we open notepad and start banging away on the keyboard.

;;;M2 Lisp - Version 1.0 - 2nd August 2001
;;;=============================================================
;;;This function will calculate the area (m2) from
;;;points input by the user. The user then has the
;;;option of placing a text label into the drawing using
;;;the current text style/height at a user defined
;;;insertion point.
;;;=============================================================
;;;Written by Kenny Ramage August 2001
;;;=============================================================
;;;=============================================================
;;;Define Main Function
;;;=============================================================

(defun C:M2 ( / os oom laag oec oudosmode p q opp oppm oppma oppmat
		tekst pos pos2 flag1 antw lw)

   (setvar "cmdecho" 0)

   (command "undo" "m")

   (setq oom (getvar "orthomode")
         laag (getvar "clayer")
	 oudosmode (getvar "osmode")
         olderr *error*
         *error* opperr
   );setq

   (setvar "orthomode" 0)
   (print)
   (prompt "\nArea Lisp V-1.0 Written by Kenny Ramage
	      - kramage@mweb.com.na")
   (prompt "\nPick the corners of the area you want to calculate : ")

   (initget 1)
   (setq p (getpoint "\nFrom point : ")
         q (list (car p)(cadr p))
   );setq

   (command "layer" "m" "5" ""
            "pline" q "w" "0" ""
   );command

   (while (= (getvar "cmdnames") "PLINE")
           (redraw (entlast) 3)
           (prompt "\nTo point [Close/Undo] : ")
           (command pause)
   );while

   (command "area" "e" "l")

   (setq opp (getvar "area")
         oppm (/ opp 1000000.0)
         oppma (rtos oppm 2 3)
         oppmat (strcat oppma "m")
         tekst (strcat "\nArea = " oppmat "2")
   );setq

   (setq lw (entlast))

   (command "layer" "m" laag "")

   (prompt tekst)

   (setq flag1 T)

   (while flag1
   
   (setq antw (getstring "\nInsert Area Label? [y/n]  : "))

   (setq antw (strcase antw))

   (if (or (= antw "Y")(= antw ""))

       (progn
             (if (not (tblsearch "layer" "4"))
                 (command "layer" "m" "4" "c" "4" "4" "")
                 (command "layer" "t" "4" "on" "4" "u" 
			  "4" "s" "4" "")
             );if

             (setvar "osmode" 0)
             (setq pos (getpoint "\nInsertion Point : "))

             (if (= (cdr (assoc 40 (tblsearch "style"
		 (getvar "textstyle")))) 0)
                 (command "text" "j" "c" pos "" "0" oppmat)
                 (command "text" "j" "c" pos "0" oppmat)
             );if

             (setq pos2 (cadr (textbox (entget (entlast))))
                   pos2 (list (+ (car pos)(/ (car pos2) 2.0))
			(+ (cadr pos)(cadr pos2)))
             );setq

             (if (= (cdr (assoc 40 (tblsearch "style" 
		 (getvar "textstyle")))) 0)
                 (command "text" "j" "tl" pos2 "" "0" "2")
                 (command "text" "j" "tl" pos2 "0" "2")
             );if

             (command "scale" "l" "" pos2 ".5")

       );progn

   );if

	(if (or (or (= antw "Y")(= antw "N")(= antw "")))
		
		(setq flag1 nil)
	);if

   );while

   (command "erase" lw ""
            "redrawall"
   );command

;;;==================================================
;;;Reset System Variables and Restore Error Handler
;;;=================================================

   (setq *error* olderror)
   (command "layer" "m" laag "")
   (setvar "osmode" oudosmode)
   (setvar "orthomode" oom)

   (princ)
);defun C:M2
;;;===================================================
;;;Define Error Trap
;;;===================================================

(defun opperr (s)

      (if (/= s "Function cancelled") 
          (princ (strcat "\nError: " s))
      );if

      (setq *error* olderr)
      (setvar "osmode" oudosmode)
      (setvar "orthomode" oom)
      (command "undo" "b"
               "layer" "m" laag ""
               "redrawall"
      );command

);defun opperr
;;;===================================================
(princ)
;;;End Area Lisp
;;;===================================================
;;;==================================================
 
Load the application and then type "M2" to run. Select the area that you would like to calculate by picking selected points. A polyline will be traced from each point  that you have selected. When you have finished, select "C." (Close.)
The area will now be displayed at the command line. You will now be given the option to add a label to your drawing if you so wish.

But now we'll get really clever and try and fool AutoCAD into working out the area that we want automatically. Interested? Read on............

 
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