Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Sum of polyline lengths

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
jonmcfarlandGY
25681 Views, 8 Replies

Sum of polyline lengths

Hi All,

 

Is it possible to determine the sum of the lengths of all of the polylines on a certain layer?  I'm trying to determine the total length of joint sealant on a building's exterior.

 

Thanks,

Jon

Please select "Accept as Solution" if your question was answered
8 REPLIES 8
Message 2 of 9
3wood
in reply to: jonmcfarlandGY

Please isolate the layer and then try ADDLINES.vlx

 

3wood

CAD KITS

Message 3 of 9
pbejse
in reply to: jonmcfarlandGY


@jonmcfarlandWS wrote:

Hi All,

 

Is it possible to determine the sum of the lengths of all of the polylines on a certain layer?  I'm trying to determine the total length of joint sealant on a building's exterior.

 

Thanks,

(defun c:TLenAll  (/ _length ss e cur data)
      (setq _Length
                 (lambda (en)
                       (vlax-get en 'Length)
                       ))
      (if (setq ss (ssget "_X" '((0 . "LWPOLYLINE"))))
            (progn
                  (repeat (sslength ss)
                        (setq e (vlax-ename->vla-object
                                      (ssname ss 0)))
                        (if (setq cur (assoc (setq lyn  (vla-get-layer
                                                              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))
                  )
            )
      )

 


For all polylines on all layers

 

or

(defun c:TLenLayer  (/ _length ss e cur data)
      (setq _Length
                 (lambda (en)
                       (vlax-get en 'Length)
                       ))
      (if   (and (setq lyn (getstring "\nEnter Layer Name: "))
                 (tblsearch "LAYER" lyn)
                 (setq ss (ssget "_X" (list '(0 . "LWPOLYLINE")(cons 8 lyn))))
                 (setq data (list lyn 0)))
            (progn
                  (repeat (sslength ss)
                        (setq e (vlax-ename->vla-object
                                      (ssname ss 0)))
                              (setq data (list lyn (+ (_Length e)
                                                         (cadr data))))
                        (ssdel (ssname ss 0) ss)
                        )
                  
                  )
            )(print data)(princ)
      )

 

prompt for for specific layer

 

HTH

 

Message 4 of 9


@jonmcfarlandWS wrote:

.... 

Is it possible to determine the sum of the lengths of all of the polylines on a certain layer?  I'm trying to determine the total length of joint sealant on a building's exterior.

....


If you draw sealant joints on only that Layer, and draw only sealant joints on that Layer [not notes about the joints, or leaders, or whatever], but if you might want to use things other than Polylines sometimes [e.g. Lines for plain straight joints, or Circles for the caulk around circular windows, etc.] then try this:

 

(vl-load-com); if needed

 

(defun C:TLL (/ total); = Total Length by Layer
  (foreach
    ent
    (mapcar 'cadr (ssnamex (ssget "_X" '((8 . "A-ELEV-CALK"))))); <---put in correct Layer name
    (setq total
      (+
        (cond (total) (0))
        (vlax-curve-getDistAtParam ent (vlax-curve-getEndParam ent))
      ); +
    ); setq
  ); foreach
); defun

 

The Length VLA property is good for Polylines and Lines, but not Arcs or Circles, which is why it uses a length calculation that works for any entity type with linearity.

 

If you do use only Lightweight Polylines for the joints, and if you might have other kinds of entities on that Layer that don't represent joints, and if you don't have any PLines on that Layer that represent anything other than joints, then you can do this:

 

(defun C:TLL (/ total)
  (foreach
    ent
    (mapcar 'cadr (ssnamex (ssget "_X" '((8 . "A-ELEV-CALK") (0 . "LWPOLYLINE"))))); <---put in correct Layer name
    (setq total
      (+
        (cond (total) (0))
        (vla-get-Length (vlax-ename->vla-object ent))
      ); +
    ); setq
  ); foreach
); defun

 

In either case, if you want the total length retained in the 'total' variable, rather than simply returned, change the (defun) top line to:

 

(defun C:TLL ()

 

to "globalize" that variable.

 

And you can take out the "_X" from the (ssget) function, if you want the User to pick a limited area, so [for instance] you can get a total for each elevation of a building separately, even when they're all in the same drawing, rather than a total for everything in the whole drawing at once, BUT the conversion of the selection set into a list of entity names would be more complicated.

Kent Cooper, AIA
Message 5 of 9
jonmcfarlandGY
in reply to: pbejse

Thank you HTH and Kent for your help.  You both had elegent and accurate solutions (as always).  If I could change one thing, it would be to show the results in feet, rather than inches.  Regardless, the routines work fine as is.

 

Thank you both,

 

 

Jon

Please select "Accept as Solution" if your question was answered
Message 6 of 9
_Tharwat
in reply to: jonmcfarlandGY


@jonmcfarlandWS wrote:

Thank you HTH and Kent for your help.

 

 


HTH = Hope This Helps Smiley Wink

Message 7 of 9


@jonmcfarlandWS wrote:

Thank you HTH and Kent for your help.  You both had elegent and accurate solutions (as always).  If I could change one thing, it would be to show the results in feet, rather than inches.  Regardless, the routines work fine as is.

....


You're welcome.  You could just run the 'total' variable's value through a division to convert it from inches to feet:
 

(/ total 12)

 

Depending on how you want to use that information, you could do other things, such as convert it to text [whether to report it at the Command: prompt line, or to put some Text in the drawing]:

 

(strcat (rtos (/ total 12) 2 2) "'")
 

[that's a ' for feet surrounded by double quotes at the end].  Change the last 2 to whatever number of decimal places you want.

Kent Cooper, AIA
Message 8 of 9
pbejse
in reply to: jonmcfarlandGY


@jonmcfarlandWS wrote:

Thank you HTH and Kent for your help.  You both had elegent and accurate solutions (as always).  If I could change one thing, it would be to show the results in feet, rather than inches.  Regardless, the routines work fine as is.

 

Thank you both,

 


 

You are welcome jon,

 

Wil that be 15.25 (units) as  1'-3 1/4" as the current units is inches and you want the equivalent in feet?

for TLenLayer

(progn
                  (repeat (sslength ss)
                        (setq e (vlax-ename->vla-object
                                      (ssname ss 0)))
                              (setq data (list lyn (+ (_Length e)
                                                         (cadr data))))
                        (ssdel (ssname ss 0) ss)
                        )
                  (print (list (car data) (rtos (cadr data) 4 2)))
                  )

for TlenAll

 (foreach
                         itm  data
                        (print (list (car itm) (rtos (cadr itm) 4 2)))
                        (princ))

 

or 15.25 is 1.27' no fractions but with the ' symbol? or no symbol at all?

for TLenLayer

(progn
                  (repeat (sslength ss)
                        (setq e (vlax-ename->vla-object
                                      (ssname ss 0)))
                              (setq data (list lyn (+ (_Length e)
                                                         (cadr data))))
                        (ssdel (ssname ss 0) ss)
                        )
                  (print (list (car data) (/ (cadr data) 12)))
                  )

 

for TlenAll

 (foreach
                         itm  data
                        (print (list (car itm) (/ (cadr itm) 12)))
                        (princ)) 

 

or 15.25 is 15.25' as the current unit is decimal/feet?

 

 Just messing with your head there jon.Smiley Happy

 

Glad we could help.

 

Message 9 of 9
pbejse
in reply to: _Tharwat


@_Tharwat wrote:

@jonmcfarlandWS wrote:

Thank you HTH and Kent for your help.

 

 


HTH = Hope This Helps Smiley Wink


I myself did that once before.

 

"Good for TIA "

 

where TIA is thanks in advance .. only human tharwat  Smiley Wink

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost