list the length of all segments of a polyline

list the length of all segments of a polyline

Anonymous
Not applicable
12,513 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,514 Views
48 Replies
Replies (48)
Message 21 of 49

sameerulcdc
Contributor
Contributor

yes message 9

0 Likes
Message 22 of 49

sameerulcdc
Contributor
Contributor

this is ok, But all induvial value must in separate columns 

not in one column.

0 Likes
Message 23 of 49

CADaSchtroumpf
Advisor
Advisor

@sameerulcdc  a écrit :

this is ok, But all induvial value must in separate columns 

not in one column.


Change in the code the line

str_sep ";"

By

str_sep ","

Or

str_sep "\t"

At your convenience

0 Likes
Message 24 of 49

Kent1Cooper
Consultant
Consultant

@sameerulcdc wrote:

yes message 9


Minimally tested adjustment:

 

(defun C:PSLE (/ ss psle i pl seg str); = Polyline Segment Lengths Export
  (prompt "\nTo Export Polyline Segment Lengths,")
  (if (setq ss (ssget '((0 . "*POLYLINE"))))
    (progn ; then
      (setq
        psle (open "C:/temp/PSLE.csv" "w"); <-- replace with your drive:filepath/filename
        i 0 ; index for Polylines
      ); setq
      (repeat (sslength ss); step through Polylines
        (setq
          pl (ssname ss i)
          seg 0
          str (strcat (itoa i) ","); start with index number
        ); setq
        (repeat (fix (vlax-curve-getEndParam pl)); step through segments
          (setq str
            (strcat
              str
              (rtos
                (abs
                  (-
                    (vlax-curve-getDistAtParam pl seg)
                    (vlax-curve-getDistAtParam pl (setq seg (1+ seg)))
                  ); -
                ); abs
                2 6 ; <-- EDIT mode/precision as desired
              ); rtos
              ","
            ); strcat
          ); setq
        ); repeat [segments]
        (write-line str psle)
        (setq i (1+ i)); for next Polyline
      ); repeat [Polylines]
      (close psle)
    ); progn
  ); if
  (princ)
); defun

 

Kent Cooper, AIA
0 Likes
Message 25 of 49

sameerulcdc
Contributor
Contributor

thank you its working.

little more thinks need to update please

1. when i used this for one group then i need to used same file other other group also. same excel file need with one row empty 

2. my dwg are in millimeter, i no need to have any precision and can we make length value to round off to 5 

     if length is 5002.860  in the dwg, i need it to 5005 out put 

 

thank you .

0 Likes
Message 26 of 49

sameerulcdc
Contributor
Contributor

thank you it is also working.

little more thinks need to update please

 

my dwg are in millimeter can we make length value to round off to 5 

     if length is 5002.860  in the dwg, i need it to 5005 out put 

 

 

0 Likes
Message 27 of 49

Kent1Cooper
Consultant
Consultant

@sameerulcdc wrote:

...

1. ... i need to used same file other other group also. same excel file need with one row empty 

2. ... round off to 5 ....


1.  Change the "w" in the (open) function to "a" instead [for "append"].  That by itself won't introduce the empty row, but that can be added if it otherwise works for you.

2.  There are many round-off-to-the-nearest-X-value routines in the Forums, which can be found with a Search.  Are you capable of incorporating one?

Kent Cooper, AIA
0 Likes
Message 28 of 49

CADaSchtroumpf
Advisor
Advisor

@sameerulcdc  a écrit :

thank you it is also working.

little more thinks need to update please

 

my dwg are in millimeter can we make length value to round off to 5 

     if length is 5002.860  in the dwg, i need it to 5005 out put 

 

 


With this?

(defun roundUpTo (i rnd / r)
  (if (equal 0 (setq r (rem i rnd)) 1E-4)
    i
    (+ i (- rnd r))
  )
)
(defun c:dist-vtx2CSV ( / js 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
      (setq
        tmp (vl-filename-mktemp "tmp.csv")
        f_open (open tmp "w")
        str_sep ","
        oldim (getvar "dimzin")
        n -1
      )
      (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 (roundUpTo (fix (atoi (rtos x 2 0))) 5) 2 0) str_sep)
                )
                (reverse lst_len)
              )
            )
          )
          f_open
        )
      )
      (close f_open)
      (startapp "notepad" tmp)
      (setvar "dimzin" oldim)
    )
  )
  (prin1)
)
0 Likes
Message 29 of 49

Kent1Cooper
Consultant
Consultant

@sameerulcdc wrote:

.... i need to used same file other other group also. same excel file need with one row empty  ....


Another important question:  After that empty row, with the start of a new group, should the index number start over at 0, or continue counting up from the last index number before?  The first is easy, but the second would be a lot harder if your usage might be over multiple drawings, or after closing and later re-opening one drawing.

Kent Cooper, AIA
0 Likes
Message 30 of 49

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

@sameerulcdc wrote:

.... i need to used same file other other group also. same excel file need with one row empty  ....


....  After that empty row, with the start of a new group, should the index number start over at 0...?  ....


Assuming it starts over at 0, try this [minimally tested], which includes rounding to the nearest multiple of 5:

(defun C:PSLE (/ ss psle i pl str); = Polyline Segment Lengths Export
  (prompt "\nTo Export Polyline Segment Lengths,")
  (if (setq ss (ssget '((0 . "*POLYLINE"))))
    (progn ; then
      (setq
        psle (open "C:/temp/PSLE.csv" "a"); <-- replace with your drive:filepath/filename
        i 0 ; index for Polylines
      ); setq
      (repeat (sslength ss); step through Polylines
        (setq
          pl (ssname ss i)
          seg 0
          str (strcat (itoa i) ","); start with index number
        ); setq
        (repeat (fix (vlax-curve-getEndParam pl)); step through segments
          (setq str
            (strcat
              str
              (itoa
                (* 5
                  (fix
                    (+
                      (/
                        (abs
                          (-
                            (vlax-curve-getDistAtParam pl seg)
                            (vlax-curve-getDistAtParam pl (setq seg (1+ seg)))
                          ); -
                        ); abs
                        5
                      ); /
                      0.5
                    ); +
                  ); fix
                ); *
              ); itoa
              ","
            ); strcat
          ); setq
        ); repeat [segments]
        (write-line str psle)
        (setq i (1+ i)); for next Polyline
      ); repeat [Polylines]
      (write-line "" psle); empty row in anticipation of another group
      (close psle)
    ); progn
  ); if
  (princ)
); defun

 

Kent Cooper, AIA
0 Likes
Message 31 of 49

sameerulcdc
Contributor
Contributor

Kent1Cooper,

 

Thank you that worked v-well. 

 

Thank you.

0 Likes
Message 32 of 49

sameerulcdc
Contributor
Contributor

CADaSchtroumpf,

 

Thank you that working well also. 

 

Thank you.

0 Likes
Message 33 of 49

sameerulcdc
Contributor
Contributor

Hi Kent1cooper

thank you for your time and effort,

i need little more modification of this lsp please.

1. Assuming it starts over at 0, need to change to 1.

2. when i selected vertical direction lines by window, ... direction right to Left numbers 123....
& when i selected Horizontal direction lines by window, ... direction top to bottom numbers 123....

Please see the attached file for your reference. 

 

Thank you

0 Likes
Message 34 of 49

sameerulcdc
Contributor
Contributor

Hi CADaSchtroumpf

 

thank you for your time and effort,

i need little more modification of this lsp please.

1. Assuming it starts over at 0, need to change to 1.

2. when i selected vertical direction lines by window, ... direction right to Left numbers 123....
& when i selected Horizontal direction lines by window, ... direction top to bottom numbers 123....

Please see the attached file for your reference. 

0 Likes
Message 35 of 49

Kent1Cooper
Consultant
Consultant

Your example .csv file violates your request to start at 1.  Which should it be?

 

What if you select both horizontal and vertical objects?  Will anything ever be at 45° or similarly ambiguous?  Are you willing to be asked in the command whether what you will select should be treated as generally horizontal or vertical?

 

Your "horizontal" objects are "generally" horizontally oriented, but not simply horizontal in the same way that your vertical objects are simply vertical.  Might "vertical" ones also sometimes have bends in them, like the "horizontal" ones?

Kent Cooper, AIA
0 Likes
Message 36 of 49

sameerulcdc
Contributor
Contributor

Hi

* it should be start with 1. please

* Yes, if option is there it will good at command which i will select,  whether i will select as generally horizontal or vertical or any angle lines.

 

thank you.

 

0 Likes
Message 37 of 49

CADaSchtroumpf
Advisor
Advisor

@sameerulcdc 

Hi,

Although it gets a bit complex, here is my approach:
To determine if the segment is horizontal or vertical, I test the longest segment of the polyline.
Then I sort according to the X and Y of the vertices to classify the entities independently of the order of creation of the entities.
And finally I write the result as before in a file.

0 Likes
Message 38 of 49

sameerulcdc
Contributor
Contributor

Hi Kent1Cooper

 

* it should be start with 1. please

* Yes, if option is there it will good at command which i will select,  whether i will select as generally horizontal or vertical or any angle lines.

 

Please help me regarding this issue, 

 

thank you.

 

0 Likes
Message 39 of 49

ndnam1296
Participant
Participant

Dear Kent Cooper

Can you help me fix your lisp, add more infomation polyline, I need text inside polyline, thank you very much!

0 Likes
Message 40 of 49

Kent1Cooper
Consultant
Consultant

@ndnam1296 wrote:

Can you help me fix your lisp, add more infomation polyline, I need text inside polyline, thank you very much!


Which one?  What kind(s) of "more information"?  If you want Text inside the Polyline, is that in addition to exporting segment lengths to an external file, or instead of exporting?  There are many routines around that label the lengths of Polyline segments, for example DimPoly.lsp with its DPI command, >here<.

Kent Cooper, AIA