Jump to content
MP7

Polyline segments length MIN, MAX, AVG

Recommended Posts

MP7

Hello,

 

I'm using a nice LISP which lists the segments length of a polyline you select, but I would just like to add shortest, longest and average below the text "total polyline length is..."

like

MIN: xx

MAX: yy

AVG: zz

spIdCLam_t.jpg

(defun c:Test (/ T_Entity T_Object T_Start T_End T_SegmentLengths T_Count)
   (if
      (and
         (setq T_Entity (car (entsel "\nSelect polyline: ")))
         (= (vla-get-ObjectName (setq T_Object (vlax-ename->vla-object T_Entity))) "AcDbPolyline")
      )
      (progn
         (setq T_Start (vlax-curve-getStartParam T_Object))
         (setq T_End   (vlax-curve-getEndParam T_Object))
         (while (< T_Start T_End)
            (setq T_SegmentLengths (append T_SegmentLengths (list (- (vlax-curve-getDistAtParam T_Object (setq T_Start (1+ T_Start))) (vlax-curve-getDistAtParam T_Object (1- T_Start))))))
         )
         (setq T_Count 0)
         (foreach T_Item T_SegmentLengths
            (princ (strcat "\nSegment " (itoa (setq T_Count (1+ T_Count))) ": " (rtos T_Item (getvar "LUNITS") 6)))
         )         
         (princ (strcat "\n\n ** Total polyline length is " (rtos (vla-get-Length T_Object) (getvar "LUNITS") 6)))
      )
      (princ "\n ** Nothing selected or not a polyline.")
   )
   (princ)
)

Thx

MINMAXLENGHTS.lsp

Share this post


Link to post
Share on other sites
dlanorh

Try this. I've altered the lisp slightly as your didn't need to store everything as a list of lists, just a list. The list is built using the cons function not append as this is quicker but means you have to reverse the final list to end up with the correct order. As a single list it also makes it easier to sort and extract the max & min length.

 

(defun c:Test (/ T_Entity T_Object T_Start T_End T_SegmentLengths T_Count)
   (if
      (and
         (setq T_Entity (car (entsel "\nSelect polyline: ")))
         (= (vla-get-ObjectName (setq T_Object (vlax-ename->vla-object T_Entity))) "AcDbPolyline")
      )
      (progn
         (setq T_Start (vlax-curve-getStartParam T_Object))
         (setq T_End   (vlax-curve-getEndParam T_Object))
         (while (< T_Start T_End)
            (setq T_SegmentLengths (cons  (- (vlax-curve-getDistAtParam T_Object (setq T_Start (1+ T_Start))) (vlax-curve-getDistAtParam T_Object (1- T_Start))) T_SegmentLengths))
         )
         (setq T_Count 0)
         (foreach T_Item (reverse T_SegmentLengths)
            (princ (strcat "\nSegment " (itoa (setq T_Count (1+ T_Count))) ": " (rtos T_Item (getvar "LUNITS") 6)))
         )
         (princ (strcat "\n\n ** Minimum Segment length is " (rtos (car (vl-sort T_SegmentLengths '<)) (getvar "LUNITS") 6)))
         (princ (strcat "\n\n ** Maximum Segment length is " (rtos (car (vl-sort T_SegmentLengths '>)) (getvar "LUNITS") 6)))
         (princ (strcat "\n\n ** Average Segment length is " (rtos (/ (vla-get-Length T_Object) T_Count) (getvar "LUNITS") 6)))
         (princ (strcat "\n\n ** Total polyline length is " (rtos (vla-get-Length T_Object) (getvar "LUNITS") 6)))
      )
      (princ "\n ** Nothing selected or not a polyline.")
   )
   (princ)
)

 

Edited by dlanorh
  • Thanks 1

Share this post


Link to post
Share on other sites
MP7

Holy cow, yeah that's just perfect man! Awesome! Thx a bunch man!

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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