list the length of all segments of a polyline

list the length of all segments of a polyline

Anonymous
Not applicable
12,512 Views
48 Replies
Message 1 of 49

list the length of all segments of a polyline

Anonymous
Not applicable

sir,

 

can I get a lisp programme for getting the segment length of all the segments of a polyline. a sampe polyline is attached

Accepted solutions (1)
12,513 Views
48 Replies
Replies (48)
Message 2 of 49

DannyNL
Advisor
Advisor

You already could get the length from the properties, but if you need to do it with LISP see example code below.

 

(defun c:Test (/ T_Entity T_Object)
   (if
      (and
         (setq T_Entity (car (entsel "\nSelect polyline: ")))
         (= (vla-get-ObjectName (setq T_Object (vlax-ename->vla-object T_Entity))) "AcDbPolyline")
      )
      (princ (strcat "\nPolyline length is " (rtos (vla-get-Length T_Object))))
      (princ "\n ** Nothing selected or not a polyline.")
   )
   (princ)
)
0 Likes
Message 3 of 49

Anonymous
Not applicable

sir

 

I need the segment wise length of the polyline, which means if a polyline consists of 4 segments and length of the segments are 2, 5, 7 and 8. then the programme should list the above values.

 

and the total length of the polyline would be 22

0 Likes
Message 4 of 49

DannyNL
Advisor
Advisor
Accepted solution

Ok, my bad.

See code below, but with your example polyline the list gets pretty long Smiley Happy

 

(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)))
         )         
         (princ (strcat "\n\n ** Total polyline length is " (rtos (vla-get-Length T_Object))))
      )
      (princ "\n ** Nothing selected or not a polyline.")
   )
   (princ)
)
Message 5 of 49

Anonymous
Not applicable

thanks a lot, it served my purpose.

Message 6 of 49

Anonymous
Not applicable

only thing is I want the value upto 6 digits after decimal

0 Likes
Message 7 of 49

DannyNL
Advisor
Advisor

Glad I could help Smiley Happy

And no problem, only a small addition to the existing code.

 

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

 

0 Likes
Message 8 of 49

Anonymous
Not applicable

DannyNL,

 

Would you be able to adjust this lisp you made to have all the segment lengths be put into a single column output table so I would be able to export the lengths? I just want the lengths of the individual polyline segments to be input into our spreadsheet. Any help on this would be greatly appreciated.

 

Thank you.

0 Likes
Message 9 of 49

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... have all the segment lengths be put into a single column output table so I would be able to export the lengths? I just want the lengths of the individual polyline segments to be input into our spreadsheet. ....


 

You don't need the middle-man of a Table to then be exported -- you can send them directly  to a spreadsheet file:

(defun C:PSLE (/ pl psle n); = Polyline Segment Lengths Export
  (setq
    pl (car (entsel "\nPolyline for Segment-Length Export: "))
    psle (open "C:/PSLE.csv" "w"); replace with your drive:filepath/filename
    n 0
  ); setq
  (repeat (fix (vlax-curve-getEndParam pl))
    (write-line
      (rtos
        (abs
          (-
            (vlax-curve-getDistAtParam pl n)
            (vlax-curve-getDistAtParam pl (setq n (1+ n)))
          ); -
        ); abs
        2 6
      ); rtos
      psle
    ); write-line
  ); repeat
  (close psle)
); defun
Kent Cooper, AIA
0 Likes
Message 10 of 49

Anonymous
Not applicable

Kent1Cooper,

 

Thank you that worked well. I got sent a spreadsheet that is looking for a little more information to be exported. Would you be able to modify it to be able to get all of the columns in the attached word document filled?

 

Thank you.

 

0 Likes
Message 11 of 49

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... modify it to be able to get all of the columns in the attached word document filled? ....


 

Not without some more information about what those categories need in them.  But you can probably work it out from this start.  It assumes sequential numbering for the Feeder ID, and uses the cumulative distance along the overall Polyline at the vertices for the "Run Start" and "Run End" for each segment, but that's also just an assumption -- maybe you need a location or something.  I had no idea what to assume about the # of Turbines, but maybe you can figure out what to put there instead of my "whatever".

 

(defun C:PSLE (/ pl psle n); = Polyline Segment Lengths Export
  (setq
    pl (car (entsel "\nPolyline for Segment-Length Export: "))
    psle (open "C:/PSLE.csv" "w")
    n 0
  ); setq
  (write-line "Feeder" psle); top line
  (write-line "ID,Run Start,Run End,Length,# of Turbines" psle); headings
  (repeat (fix (vlax-curve-getEndParam pl))
    (write-line
      (strcat
        (itoa (1+ n)) "," ; ID
        (rtos (vlax-curve-getDistAtParam pl n) 2 6) "," ; Run Start
        (rtos (vlax-curve-getDistAtParam pl (1+ n)) 2 6) "," ; Run End
        (rtos ; Length
          (abs
            (-
              (vlax-curve-getDistAtParam pl n)
              (vlax-curve-getDistAtParam pl (setq n (1+ n)))
            ); -
          ); abs
          2 6
        ); rtos
        ","
        "whatever" ; # of Turbines
      ); strcat
      psle
    ); write-line
  ); repeat
  (close psle)
); defun

 

Kent Cooper, AIA
Message 12 of 49

Anonymous
Not applicable

Kent1Cooper,

 

How do you use the lisp to export the values into the spreadsheet we have developed? I seem to not have found a way to do this.

 

Thank you.

0 Likes
Message 13 of 49

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

How do you use the lisp to export the values into the spreadsheet we have developed? 

….

 

Will using the (open) function with the append option [instead of the write option] do it for you?

Kent Cooper, AIA
0 Likes
Message 14 of 49

Anonymous
Not applicable

Kent1Cooper,

 

That doesn't seem to allow me to open the table with that.

0 Likes
Message 15 of 49

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... That doesn't seem to allow me to open the table with that.


[Sorry, but I never have a need to deal with relating to external files of other types in AutoCAD.  I know enough to write to a .CSV file, and to read from pretty simple files like those and in plain text, but that's about it -- I don't know enough about things like data extraction or tables.  I hope someone else with experience doing such things can help.]

Kent Cooper, AIA
0 Likes
Message 16 of 49

sameerulcdc
Contributor
Contributor

hi this lsp is working good 

thank you

please can u update to select multiple line  in one time

0 Likes
Message 17 of 49

Kent1Cooper
Consultant
Consultant

@sameerulcdc wrote:

.... please can u update to select multiple line  in one time


And what would be the delimiter between the reporting on each Polyline?  Simply starting the ID numbers over again at 1?  An empty line?  Some kind of item header such as the Handle?  Something else?  A small sample file with the format you would expect [with only a few Polylines' worth] would answer all questions.

Kent Cooper, AIA
0 Likes
Message 18 of 49

sameerulcdc
Contributor
Contributor

Thank you

like this format please  

0 Likes
Message 19 of 49

CADaSchtroumpf
Advisor
Advisor

A possible way...

(defun c:dist-vtx2CSV ( / js prec tmp f_open str_sep l_pt oldim n ent pr lst_len dist_start dist_end seg_len)
  (princ "\nSelect polyline")
  (setq js
    (ssget
      (list
        '(0 . "LWPOLYLINE")
        (cons 67 (if (eq (getvar "CVPORT") 1) 1 0))
        (cons 410 (if (eq (getvar "CVPORT") 1) (getvar "CTAB") "Model"))
      )
    )
  )
  (cond
    (js
      (initget 4)
      (setq
        prec (getint (strcat "\nNumber of digits? <" (itoa (getvar "LUPREC")) ">: "))
        tmp (vl-filename-mktemp "tmp.csv")
        f_open (open tmp "w")
        str_sep ";"
        oldim (getvar "dimzin")
        n -1
      )
      (if (not prec) (setq prec (getvar "LUPREC")))
      (setvar "dimzin" 0)
      (repeat (sslength js)
        (setq
          ent (ssname js (setq n (1+ n)))
          pr -1
          lst_len nil
        )
        (repeat (fix (vlax-curve-getEndParam ent))
          (setq
            dist_start (vlax-curve-GetDistAtParam ent (setq pr (1+ pr)))
            dist_end (vlax-curve-GetDistAtParam ent (1+ pr))
            seg_len (- dist_end dist_start)
            lst_len (cons seg_len lst_len)
          )
        )
        (write-line (strcat (itoa n) str_sep (apply 'strcat (mapcar '(lambda (x) (strcat (rtos x 2 prec) str_sep)) (reverse lst_len)))) f_open)
      )
      (close f_open)
      (startapp "notepad" tmp)
      (setvar "dimzin" oldim)
    )
  )
  (prin1)
)
0 Likes
Message 20 of 49

Kent1Cooper
Consultant
Consultant

@sameerulcdc wrote:

.... like this format please  


That looks like you're after a modification of the routine at Message 9, not Message 11.  Is that right?

Kent Cooper, AIA
0 Likes