Measuring Total Length and Total Areas from multiple objects with AutoCAD

autolispMy work involves a lot of measurements in order to get my quantities off the drawings and make the estimations I need. For this work I only work with AutoCAD. I strongly believe that it is more than enough as it comes with lots of good and handy tools to do the job. Although, some things still “missing” or at least it cannot cover anyone’s demand for commands.

The use of LISP and the creation of custom commands from scratch or manipulating/using what others have created can easily solve this “problem”.

One useful command I use almost every time I am on AutoCAD is TLEN. It lets you get the total length of objects that you have selected and in combination with FILTER command it can become one extremely handy. It is created from Tee Square Graphics (take a look at their site as they have lots of DIY tools for AutoCAD) and the code is this:

(defun C:TLEN (/ ss tl n ent itm obj l)
(setq ss (ssget)
tl 0
n (1- (sslength ss)))
(while (>= n 0)
(setq ent (entget (setq itm (ssname ss n)))
obj (cdr (assoc 0 ent))
l (cond
((= obj "LINE")
(distance (cdr (assoc 10 ent))(cdr (assoc 11 ent))))
((= obj "ARC")
(* (cdr (assoc 40 ent))
(if (minusp (setq l (- (cdr (assoc 51 ent))
(cdr (assoc 50 ent)))))
(+ pi pi l) l)))
((or (= obj "CIRCLE")(= obj "SPLINE")(= obj "POLYLINE")
(= obj "LWPOLYLINE")(= obj "ELLIPSE"))
(command "_.area" "_o" itm)
(getvar "perimeter"))
(T 0))
tl (+ tl l)
n (1- n)))
(alert (strcat "Total length of selected objects is " (rtos tl)))
(princ)
)

Anther tool that I am using frequently is AREAM. It is the same concept as TLEN but it is used for areas. Its a very handy way to measure the total area of many objects at once and again if it is combined with FILTER it becomes a very handy command. This command is written by Jimmy Bergmark who also has a very interesting website that you should visit.
The AREAM code is:

(defun c:aream (/ olderr oldcmdecho errexit undox restore ss1 nr en tot_area)
(defun errexit (s)
(restore)
)
(defun undox ()
(command "._undo" "_E")
(setvar "cmdecho" oldcmdecho)
(setq *error* olderr)
(princ)
)
(setq olderr *error*
restore undox
*error* errexit
)
(setq oldcmdecho (getvar "cmdecho"))
(setvar "cmdecho" 0)
(command "._UNDO" "_BE")
(if (setq ss1 (ssget '((-4 . "<OR") (0 . "POLYLINE") (0 . "LWPOLYLINE") (0 . "CIRCLE") (0 . "ELLIPSE") (0 . "SPLINE") (0 . "REGION") (-4 . "OR>")
)
)
)
(progn
(setq nr 0)
(setq tot_area 0.0)
(setq en (ssname ss1 nr))
(while en
(command "._area" "_O" en)
(setq tot_area (+ tot_area (getvar "area")))
(setq nr (1+ nr))
(setq en (ssname ss1 nr))
)
(princ "\nTotal Area = ")
(princ tot_area)
)
)
(setq ss1 nil)
(restore)
)

Just copy and paste the code above to a blank txt file and after saving it, change the extension from .txt to .lsp. Then Drag and Drop it into AutoCAD and its done. The code is loaded into AutoCAD and the custom command is ready to be used. If you don’t want to drag and drop the LISP files every time you open a drawing you can pre-load them following these steps.