Visual LISP, AutoLISP and General Customization

## Visual LISP, AutoLISP and General Customization

Mentor
Posts: 253
Registered: ‎09-22-2010
Message 1 of 9 (3,576 Views)

# Sum of polyline lengths

3576 Views, 8 Replies
05-21-2012 06:50 AM

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

curtainwallBIM.blogspot.com
----------------------------------------------------------------------------------------------
*Expert Elite*
Posts: 908
Registered: ‎03-25-2009
Message 2 of 9 (3,567 Views)

# Re: Sum of polyline lengths

05-21-2012 07:26 AM in reply to: jonmcfarlandWS

3wood

*Expert Elite*
Posts: 2,369
Registered: ‎11-24-2009
Message 3 of 9 (3,554 Views)

# Re: Sum of polyline lengths

05-21-2012 08:55 AM in reply to: jonmcfarlandWS

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)
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)
(ssdel (ssname ss 0) ss)
)

)
)(print data)(princ)
)

prompt for for specific layer

HTH

*Expert Elite*
Posts: 4,965
Registered: ‎09-13-2004
Message 4 of 9 (3,542 Views)

# Re: Sum of polyline lengths

05-21-2012 11:48 AM in reply to: jonmcfarlandWS

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:

(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
Mentor
Posts: 253
Registered: ‎09-22-2010
Message 5 of 9 (3,471 Views)

# Re: Sum of polyline lengths

05-29-2012 11:23 AM 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

curtainwallBIM.blogspot.com
----------------------------------------------------------------------------------------------
Distinguished Mentor
Posts: 557
Registered: ‎07-02-2010
Message 6 of 9 (3,462 Views)

# Re: Sum of polyline lengths

05-29-2012 12:19 PM in reply to: jonmcfarlandWS

jonmcfarlandWS wrote:

Thank you HTH and Kent for your help.

HTH = Hope This Helps

____________________________________________________
Get learn and learn and practice , to become experienced guy .
*Expert Elite*
Posts: 4,965
Registered: ‎09-13-2004
Message 7 of 9 (3,453 Views)

# Re: Sum of polyline lengths

05-29-2012 02:26 PM in reply to: jonmcfarlandWS

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
*Expert Elite*
Posts: 2,369
Registered: ‎11-24-2009
Message 8 of 9 (3,440 Views)

# Re: Sum of polyline lengths

05-29-2012 10:13 PM in reply to: jonmcfarlandWS

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)
(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)
(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?

*Expert Elite*
Posts: 2,369
Registered: ‎11-24-2009
Message 9 of 9 (3,439 Views)

# Re: Sum of polyline lengths

05-29-2012 10:36 PM in reply to: _Tharwat

_Tharwat wrote:

jonmcfarlandWS wrote:

Thank you HTH and Kent for your help.

HTH = Hope This Helps

I myself did that once before.

"Good for TIA "

where TIA is thanks in advance .. only human tharwat

### You are not logged in.

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.