Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Valued Mentor
jonmcfarlandWS
Posts: 267
Registered: ‎09-22-2010
Message 1 of 9 (5,603 Views)
Accepted Solution

Sum of polyline lengths

5603 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
----------------------------------------------------------------------------------------------

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

 

*Expert Elite*
3wood
Posts: 1,153
Registered: ‎03-25-2009
Message 2 of 9 (5,594 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

CAD KITS

*Expert Elite*
pbejse
Posts: 2,529
Registered: ‎11-24-2009
Message 3 of 9 (5,581 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)
                                                         (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

 

*Expert Elite*
Kent1Cooper
Posts: 5,879
Registered: ‎09-13-2004
Message 4 of 9 (5,569 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:

 

(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
Valued Mentor
jonmcfarlandWS
Posts: 267
Registered: ‎09-22-2010
Message 5 of 9 (5,498 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
_Tharwat
Posts: 644
Registered: ‎07-02-2010
Message 6 of 9 (5,489 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 :smileywink:

____________________________________________________
Get learn and learn and practice , to become experienced guy .
*Expert Elite*
Kent1Cooper
Posts: 5,879
Registered: ‎09-13-2004
Message 7 of 9 (5,480 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*
pbejse
Posts: 2,529
Registered: ‎11-24-2009
Message 8 of 9 (5,467 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)
                                                         (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.:smileyhappy:

 

Glad we could help.

 

*Expert Elite*
pbejse
Posts: 2,529
Registered: ‎11-24-2009
Message 9 of 9 (5,466 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 :smileywink:


I myself did that once before.

 

"Good for TIA "

 

where TIA is thanks in advance .. only human tharwat  :smileywink:

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.