Jump to content

SUM length by lineweight


Nickvnlr

Recommended Posts

I want to be able to make a selection of lines/polylines/etc and get a report of total length separated by lineweight.

This is the LISP I have so far, but it just adds the length of all lines in the drawing and puts the results in the command prompt.  I would like to insert the results as text. Any help is appreciated.  Thank you!

 

(defun c:SumLengthsByLineweight (/ _length ss e cur data)
  (setq _Length (lambda (en) (vlax-get en 'Length)))
  (if (setq ss (ssget "_X" '((0 . "LINE,POLYLINE,LWPOLYLINE"))))
    (progn
      (repeat (sslength ss)
        (setq e (vlax-ename->vla-object (ssname ss 0)))
        (if (setq cur (assoc (setq lyn (vla-get-LineWeight e)) data))
          (setq data (subst (list (car cur) (+ (_Length e) (cadr cur))) cur data))
          (setq data (cons (list lyn (_Length e)) data))
        )
        (ssdel (ssname ss 0) ss)
      )
      (foreach itm data (print itm) 
      (princ))
    )
  )
)

 

Link to comment
Share on other sites

2 hours ago, Nickvnlr said:
I would like to insert the results as text.

https://lee-mac.com/lengthfield.html

Length Field

Upon issuing the command syntax LF (Length Field) at the AutoCAD command-line, the program first prompts the user to make a selection of objects for which to return the length summation.

At this prompt, the user may select any number of Arcs, Circles, Lines, 2D Polylines (light or heavy), or 3D Polylines.

Link to comment
Share on other sites

 

In this code, you can see how to insert text into a drawing:

;; inserting the sum of the length of linear objects into the drawing
(defun c:LPLINE (/ e ss l p i)
  (if
    (setq l 0.0 ss (ssget '((0 . "LINE,SPLINE,LWPOLYLINE,POLYLINE,ARC,CIRCLE,ELLIPSE"))))
    (progn
      (repeat (setq i (sslength ss))
        (setq e (ssname ss (setq i (1- i)))
              l (+ l (vlax-curve-getDistAtParam e (vlax-curve-getEndParam e)))
        )
      )
      (if
        (setq p (getpoint "\nSpecify a point to insert text: "))
        (entmake
          (list
            '(0 . "TEXT")
            '(100 . "AcDbText")
            (cons 10 (trans p 1 0))
            (cons 40 (/ 5))
            (cons 1 (rtos l))
          )
        )
        (princ (strcat "\nTotal length = " (rtos l)))
      )
    )
  )
  (princ)
)

quick selection by line weight, then this lisp...

Edited by Nikon
Link to comment
Share on other sites

I can't figure out how to get it to work with multiple lines/items.

Link to comment
Share on other sites

Thanks, but it gives me an error when trying to insert the table.

 

Command: CRVTOTAL
Select objects: Specify opposite corner: 27 found
Select objects:
Specify insertion point for table <exit>: Error: bad argument type: VLA-OBJECT nil

 

I think there are functions not supported by LT.

Link to comment
Share on other sites

Ah, that explains it. My apologies, LT does have major limitation. If summarising it into a table is not achievable, then perhaps you prefer a csv/txt export? Or just in the command line?

Link to comment
Share on other sites

That works.  Thank you!  This gives me something to play with and tweak to get what I want.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...