;; ===================================================================== ;; SumArea.lsp - total area of closed objects ;; Command: SUMAREA ;; AutoCAD / GstarCAD (AutoLISP). Description and installation: ;; https://fixcontrol.co/tools/autocad-lisp-scripts ;; ;; WHAT IT DOES: ;; You select closed objects (polylines, circles, ellipses, regions, ;; splines), the script sums their areas and shows the result: ;; - on the command line, on a separate row (the number alone - you ;; can select and copy it, e.g. from the F2 history window), ;; - in the Windows clipboard (just press Ctrl+V, e.g. in Excel), ;; - in a message box (optional). ;; Open polylines are skipped - their count and handles are listed on ;; the command line. ;; Result in square drawing units, with a decimal comma. ;; ;; OPTIONS (SETTINGS at the start of the function below): ;; showAlert - T = show a message box with the result, nil = command line only ;; copyToClip - T = copy the result to the clipboard, nil = do not copy ;; decimals - number of decimal places (default 2) ;; ===================================================================== ;; number as text with a decimal comma and a fixed number of decimals (defun SumArea-fmt (num prec / oldDimzin str) (setq oldDimzin (getvar 'DIMZIN)) (setvar 'DIMZIN 0) (setq str (vl-string-translate "." "," (rtos num 2 prec))) (setvar 'DIMZIN oldDimzin) str ) ;; text to the Windows clipboard; returns T on success (defun SumArea-clip (str / html res) (setq res (vl-catch-all-apply '(lambda () (setq html (vlax-create-object "htmlfile")) (vlax-invoke (vlax-get (vlax-get html 'ParentWindow) 'ClipboardData) 'SetData "Text" str) ) ) ) (if html (vlax-release-object html)) (not (vl-catch-all-error-p res)) ) (defun c:SumArea (/ ss i ent obj total_area str_area ent_data obj_type skipped cnt showAlert copyToClip decimals) (vl-load-com) ;; ---- SETTINGS ---- (setq showAlert T) (setq copyToClip T) (setq decimals 2) (setq total_area 0.0 cnt 0 skipped nil) (prompt "\nSelect objects (polylines, circles, ellipses, regions, splines): ") (setq ss (ssget '((0 . "LWPOLYLINE,POLYLINE,CIRCLE,ELLIPSE,REGION,SPLINE")))) (if ss (progn (repeat (setq i (sslength ss)) (setq ent (ssname ss (setq i (1- i)))) (setq ent_data (entget ent)) (setq obj_type (cdr (assoc 0 ent_data))) (setq obj (vlax-ename->vla-object ent)) ; a polyline must be closed (bit 1 in code 70) (if (and (wcmatch obj_type "*POLYLINE") (= (logand (cdr (assoc 70 ent_data)) 1) 0)) (setq skipped (cons (cdr (assoc 5 ent_data)) skipped)) (setq total_area (+ total_area (vla-get-area obj)) cnt (1+ cnt)) ) ) (if skipped (progn (princ (strcat "\nSkipped " (itoa (length skipped)) " open polylines, handles:")) (foreach h skipped (princ (strcat " " h))) ) ) (setq str_area (SumArea-fmt total_area decimals)) (princ (strcat "\nTotal area of " (itoa cnt) " objects [drawing units^2]:")) (princ (strcat "\n" str_area)) (if (and copyToClip (SumArea-clip str_area)) (princ "\n(result copied to the clipboard - Ctrl+V)") ) (if showAlert (alert (strcat "Total area: " str_area (if skipped (strcat "\nOpen polylines skipped: " (itoa (length skipped))) "") (if copyToClip "\n\nResult copied to the clipboard." ""))) ) ) (princ "\nNo objects selected.") ) (princ) ) (princ "\nSUMAREA loaded. Type SUMAREA to run.") (princ)