Jump to content

Polyline segments length MIN, MAX, AVG


MP7

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

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