Visual LISP, AutoLISP and General Customization

## Visual LISP, AutoLISP and General Customization

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

# Sum of polyline lengths

4218 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

Please select "Accept as Solution" if your question was answered.
curtainwallBIM.blogspot.com
----------------------------------------------------------------------------------------------
*Expert Elite*
Posts: 971
Registered: ‎03-25-2009
Message 2 of 9 (4,209 Views)

# Re: Sum of polyline lengths

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

Please isolate the layer and then try ADDLINES.vlx

3wood

*Expert Elite*
Posts: 2,406
Registered: ‎11-24-2009
Message 3 of 9 (4,196 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: 5,242
Registered: ‎09-13-2004
Message 4 of 9 (4,184 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 (4,113 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

Please select "Accept as Solution" if your question was answered.
curtainwallBIM.blogspot.com
----------------------------------------------------------------------------------------------
Distinguished Mentor
Posts: 574
Registered: ‎07-02-2010
Message 6 of 9 (4,104 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: 5,242
Registered: ‎09-13-2004
Message 7 of 9 (4,095 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,406
Registered: ‎11-24-2009
Message 8 of 9 (4,082 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?

Just messing with your head there jon.

Glad we could help.

*Expert Elite*
Posts: 2,406
Registered: ‎11-24-2009
Message 9 of 9 (4,081 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.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

### Need installation help?

Start with some of our most frequented solutions to get help installing your software.

### Maintenance Subscription Resources

Upgrading to a 2015 product? Make sure to check these out 1st!