Iterate through polyline segments using lisp

Iterate through polyline segments using lisp

prgmdl
Contributor Contributor
943 Views
24 Replies
Message 1 of 25

Iterate through polyline segments using lisp

prgmdl
Contributor
Contributor

Hi

 

How to iterate each segments of a 2d polyline to get properties using lisp? Thanks

0 Likes
Accepted solutions (1)
944 Views
24 Replies
Replies (24)
Message 2 of 25

ВeekeeCZ
Consultant
Consultant

2dpolyline or lwpolyline?

0 Likes
Message 3 of 25

Moshe-A
Mentor
Mentor

@prgmdl  hi,

 

Check this GP command. of course there are numerous ways to do it but this is the simple and this is the way to iterate any selected objects in AutoCAD (lines, circles, texts...)

 

first you retrieve (store in 'ss') the objects with (ssget) than you iterate them by

calling (ssname ss i) where 'i' is the index number of the object in 'ss'

 

 

enjoy

Moshe

 

; iterating 2d polylines for getting it's properties
(defun c:gp (/ ss i ename elist)
 (if (setq ss (ssget '((0 . "lwpolyline"))))
  (progn
    
   (setq i -1) 
   (repeat (sslength ss)
    (setq i (1+ i))
    (setq ename (ssname ss i))
    (setq elist (entget ename))
    (terpri)
    (princ elist)
   ); repeat
   
  ); progn
 ); if

 (princ)
); c:gp

 

0 Likes
Message 4 of 25

prgmdl
Contributor
Contributor

lwpolyline

0 Likes
Message 5 of 25

prgmdl
Contributor
Contributor

hi, I can't seem to can't get the property of an individual segment of the polyline using this, or am I missing something?

0 Likes
Message 6 of 25

Moshe-A
Mentor
Mentor

@prgmdl ,

 

Probably you are, the code i gave you is generic should work in any AutoCAD version (including LT 2024 and up)

if you change the code, then post what you are have.

 

what property of the polyline you looking?

 

Moshe

 

 

0 Likes
Message 7 of 25

ВeekeeCZ
Consultant
Consultant

Use

(dumpallproperties (car (entsel)))

to see all the properties you have available.

 

Here's an example to get a list of bulges:

 

(defun c:bulges ( / ent idx)

  (setq ent (car (entsel))
	idx 0)

  (repeat (fix (getpropertyvalue ent "EndParam"))
    (terpri)
    (princ idx)
    (princ ": ")
    (print (getpropertyvalue ent "vertices" idx "Bulge"))
    (setq idx (1+ idx)))

  (princ)
  )
0 Likes
Message 8 of 25

prgmdl
Contributor
Contributor

I am not good at lisp yet but I was trying to get the individual global width property of this polyline sample

prgmdl_0-1744712099359.png

 

 

0 Likes
Message 9 of 25

prgmdl
Contributor
Contributor

(defun c:gp (/ ss i ename elist)
(if (setq ss (ssget '((0 . "lwpolyline"))))
(progn
(setq i -1)
(repeat (sslength ss)
(setq i (1+ i))
(setq ename (ssname ss i))
(setq ent (entget ename))
(setq prop (assoc 43 ent))
(princ (rtos (cdr prop) 2 2))
)
)
)
(princ)
)

0 Likes
Message 10 of 25

prgmdl
Contributor
Contributor

is there a way to get the Global width?

0 Likes
Message 11 of 25

ВeekeeCZ
Consultant
Consultant

A "global" width is the same for the entire polyline.

 

(getpropertyvalue (car (entsel)) "ConstantWidth")

 

 

... as for segments, it's define by "StartWidth" and "EndWidth". Those are property names that you can use in the code posted before.

0 Likes
Message 12 of 25

Kent1Cooper
Consultant
Consultant

I agree that we need to know more about what you mean by the "properties" of a Polyline segment.  Presumably not what are usually called "properties" -- Layer, color, linetype, etc. -- which are those of the overall Polyline, so there's no point in going by segments.  You can get the coordinates of vertices in several ways as already suggested.  But since you ask about segments, width(s) and bulge factors are by segments, but do you want, for example, a list of the lengths of all segments?  And/or something else?

Kent Cooper, AIA
0 Likes
Message 13 of 25

prgmdl
Contributor
Contributor

there are situations when joining polylines of different ConstantWidth to be joined and I'd like to check each like this example

prgmdl_0-1744712834752.png

 

0 Likes
Message 14 of 25

ВeekeeCZ
Consultant
Consultant
Accepted solution

So, you would like to check if StartWidth matches the EndWidth per segment?

 

Possibly like this

(defun c:Widthcheck ( / ent idx w1 w2)
  
  (setq ent (car (entsel))
	idx 0)
  
  (repeat (fix (getpropertyvalue ent "EndParam"))
    
    (setq w1 (getpropertyvalue ent "vertices" idx "StartWidth"))
    (setq w2 (getpropertyvalue ent "vertices" idx "EndWidth"))
    
    (if (equal w1 w2 1e-3)
      (princ (strcat "\n" (itoa (1+ idx)) ": " (rtos w1)))
      (princ (strcat "\n" (itoa (1+ idx)) ": " (rtos w1) " vs. "(rtos w2))))
    (setq idx (1+ idx)))
  (princ)
  )

 

0 Likes
Message 15 of 25

prgmdl
Contributor
Contributor

I want to get the value of the Global width property of the individual segments of a polyline

0 Likes
Message 16 of 25

prgmdl
Contributor
Contributor

i will be using the value of the constantwidth for generating an offset curve

0 Likes
Message 17 of 25

prgmdl
Contributor
Contributor

the whole idea i was trying to do is similar to LM Advanced Polyoutline but out-process, and extract the constantwidth to generate individual surfaces. I am having some issues on some polylines that have varying constantwidth as shown here.

prgmdl_0-1744714798425.png

which originally generated from this

prgmdl_1-1744714893475.png

 

0 Likes
Message 18 of 25

ВeekeeCZ
Consultant
Consultant

@prgmdl wrote:

i will be using the value of the constantwidth for generating an offset curve


 

So what is it you need? I already gave you a code to get a global/constant width.

0 Likes
Message 19 of 25

prgmdl
Contributor
Contributor

I just saw now and will try

0 Likes
Message 20 of 25

Kent1Cooper
Consultant
Consultant

@prgmdl wrote:

I want to get the value of the Global width property of the individual segments of a polyline


... if a segment is of the same starting and ending width....

What do you want done with it once you get it?  If you just want to see it, you can simply select the Polyline, and in the Geometry category in the Properties palette, pick "Current vertex" and it will show the first one with an X marker, and the starting and ending widths of the segment downstream from there.  Pick in the up/down arrows to move through the vertices.

Kent1Cooper_1-1744719349389.png

You can easily see whether the Start and End segment widths are the same [the segment has constant width].

And if they are not?

If you want something done with the information [such as to apply it within some AutoLisp function], all the Start and End segment widths are available in the Polyline's entity data.

Kent Cooper, AIA