• Blog posts

    Bio

    By Dennis Collin

    AutoCAD_Tip___Using_AutoLisp_to_calculate_total_length_of_multiple_objects_-_1.PNG

    On a recent training course, I was asked if AutoCAD could list the total length of a wide number of objects including lines, arcs, polylines, circles etc. If this is attempted via conventional methods like the List command or Properties, the information returned will be limited due to the different parameters of the different selected object types.

    AutoCAD_Tip___Using_AutoLisp_to_calculate_total_length_of_multiple_objects_-_2.PNG

    For example, in the image above the properties dialogue displays a value of varies when selecting a series of lines, due to them possessing different lengths rather than a sum. However, the full version of AutoCAD can make use of its programming environment and many free elements of Lisp code are available to download or write to perform this task instead.

    This routine supports the selection of many different element types including lines, polylines, arcs, circles, ellipses, splines, multi-lines, regions and polygons. The routine scans this selection of objects and determines a total length value and displays it on the command line for reference.

    Creating a Lisp routine

    Create a new file in a text editing program like note pad and type in, or copy and paste, the following elements of code as shown below.

    (defun c:TLENGTH(/ di ent n pt1 pt2 sel)

      (vl-load-com)

      (if (ssget '((0 . "LINE,POLYLINE,LWPOLYLINE,ARC,CIRCLE,ELLIPSE,SPLINE,MLINE,REGION,MPOLYGON")))

        (progn

          (setq di 0)

          (vlax-for ent (setq sel (vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object))))

                   (cond

                     ((member (vla-get-objectname ent) '("AcDbLine" "AcDb3dPolyline" "AcDbPolyline"))

                       (setq di (+ di (vla-get-length ent)))

                     )

                     ((eq (vla-get-objectname ent) "AcDbArc")

                       (setq di (+ di (vla-get-arclength ent)))

                     )

                     ((eq (vla-get-objectname ent) "AcDbCircle")

                       (setq di (+ di (vla-get-circumference ent)))

                     )

                     ((member (vla-get-objectname ent) '("AcDbSpline" "AcDbEllipse"))

                       (setq di (+ di (vlax-curve-getdistatparam ent (vlax-curve-getendparam ent))))

                     )

                     ((eq (vla-get-objectname ent) "AcDbMline")

                       (setq n 0 pt2 nil)

                       (while (nth n (setq lst (vlax-get ent 'Coordinates)))

                         (setq pt1 (list (nth n lst)(nth (1+ n) lst)(nth (+ n 2) lst)))

                         (and pt2

                                  (setq di (+ di (distance pt1 pt2)))

                         )

                         (setq pt2 pt1

                                      n (+ n 3)

                         )

                       )

                     )

                     ((member (vla-get-objectname ent) '("AcDbMPolygon" "AcDbRegion"))

                       (setq di (+ di (vla-get-perimeter ent)))

                     )

                   )

          )

          (if (eq (vla-get-count sel) 1)

    Lisp Code Credit: Patrick_35 – Acad 2007 Forum 

    Save the text above as Tlength.lsp and load it into the startup suite via the APPLOAD command or similar. Typing in Tlength will prompt the selection of several objects and the total length will be reported to the AutoCAD command line as seen in the image below.

    AutoCAD_Tip___Using_AutoLisp_to_calculate_total_length_of_multiple_objects_-_4.PNG

    I have previously posted about some other tasks we can achieve with AutoLisp here:

    Trimming to multiple polyline boundaries

    https://www.cadlinecommunity.co.uk/hc/en-us/articles/4402639004689-AutoCAD-Tip-Using-Lisp-to-cut-elements-within-multiple-polyline-boundaries

    Inverting object selection sets

    https://www.cadlinecommunity.co.uk/hc/en-us/articles/360007838237-AutoCAD-Inverting-the-selection-of-objects-using-LISP

    The APPLOAD (Application Load Function) here:

    https://www.cadlinecommunity.co.uk/hc/en-us/articles/360003707777-AutoCAD-Improving-productivity-with-external-routines