;; ===================================================================== ;; SumLines.lsp - total length of lines and polylines ;; Command: SUMLINES ;; AutoCAD / GstarCAD (AutoLISP). Description and installation: ;; https://fixcontrol.co/tools/autocad-lisp-scripts ;; ;; WHAT IT DOES: ;; You select lines (LINE) and polylines (LWPOLYLINE, POLYLINE), ;; the script sums their lengths 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). ;; Result in 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 SumLines-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 SumLines-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:SumLines (/ ss i ent total_len str_len showAlert copyToClip decimals) (vl-load-com) ; required for the vlax-curve functions ;; ---- SETTINGS ---- (setq showAlert T) (setq copyToClip T) (setq decimals 2) (setq total_len 0.0) (prompt "\nSelect lines and polylines: ") (setq ss (ssget '((0 . "LINE,LWPOLYLINE,POLYLINE")))) (if ss (progn (repeat (setq i (sslength ss)) (setq ent (ssname ss (setq i (1- i)))) (setq total_len (+ total_len (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent)))) ) (setq str_len (SumLines-fmt total_len decimals)) (princ (strcat "\nTotal length of " (itoa (sslength ss)) " objects [drawing units]:")) (princ (strcat "\n" str_len)) (if (and copyToClip (SumLines-clip str_len)) (princ "\n(result copied to the clipboard - Ctrl+V)") ) (if showAlert (alert (strcat "Total length: " str_len (if copyToClip "\n\nResult copied to the clipboard." ""))) ) ) (princ "\nNo objects selected.") ) (princ) ) (princ "\nSUMLINES loaded. Type SUMLINES to run.") (princ)