getting points from a LWPOLYLINE

1,400 views
Skip to first unread message

Mauricio Camacho

unread,
Feb 23, 2017, 8:17:46 PM2/23/17
to python-ezdxf

 Hi,

I have a drawing that i want to read the points from a LWPOLYLINE drawing, I tried with an example from the tutorial but it doesn't work.
What am I doing wrong?

The code is below, the drawing as attachment.

CODE:
/*********************************************************************************************/
import ezdxf   #using 0.7.9

dwg = ezdxf.readfile("C:\\Python Projects\\readpolyline\\draw.dxf")

modelspace = dwg.modelspace()

line = modelspace.query('LWPOLYLINE')[0]
with line.points() as points:
     print('found a point')

/*********************************************end*********************************************/
draw.dxf

Manfred Moitzi

unread,
Feb 23, 2017, 11:19:32 PM2/23/17
to python-ezdxf
from pprint import pprint
import ezdxf   # using 0.7.9

dwg = ezdxf.readfile("draw.dxf")

modelspace = dwg.modelspace()

# model space contains no LWPOLYLINE entities
# just one block reference named '835Z1187-114_P1_FC&R-&C'
# query the block reference - take the first, because there is just one block reference
blockref = modelspace.query('INSERT')[0]
# get the block definition
block = dwg.blocks.get(blockref.dxf.name)
# block behaves like any other layout objects (modelspace)

for num, line in enumerate(block.query('LWPOLYLINE'), start=1):
    print("\n{}. POLYLINE:".format(num))
    with line.points() as points:
        pprint(points)  
        # points is a list of tuples (x, y, start_width, end_width, bulge)
        # you can add and remove points in this list, and the associated LWPOLYLINE will be updated
        # add a point: points.append((x, y, 0, 0, 0))
        # remove some points: del points[2:4]
        # change a point: points[3] = (x, y, 0, 0, 0)

Best regards,
Manfred
points.py

Mauricio Camacho

unread,
Feb 24, 2017, 1:11:02 PM2/24/17
to python-ezdxf
Thank you Manfred, I appreciate your help.

massap...@gmail.com

unread,
Apr 21, 2019, 12:54:20 PM4/21/19
to python-ezdxf
Mr. Moitzi/Manfred,
I know how much you find the Query framework "wonderful", but is it the only way to get the list of a LWPolyline points ?
What about  "LWPolyline.points()"should it not return a list of points ?
I get this answer as I tried:
    ptLW=[pts for pts in i.points()]
TypeError: '_GeneratorContextManager' object is not iterable

I'm re-writing all my work with ezdxf (not to use anymore dxfgrabber) and I'm getting sometime confused

Gr. Stephane

Manfred Moitzi

unread,
Apr 22, 2019, 12:52:40 AM4/22/19
to python-ezdxf
LWPolyline.points() creates a context manager, which returns a standard Python list of (x, y, start_width, end_width, bulge) tuples, usage is shown in the example above, look for with line.points() as points:.

If you just need the coordinates of the points, use LWPolyline.vertices() which is a generator for (x, y) tuples.

da zwenk

unread,
Apr 23, 2019, 1:02:43 PM4/23/19
to python-ezdxf
Hi Manfred,

i took your example and did some testing but the point coordinates are not updated.
The linked script loads the dxf iterates over all LWPolyline features, iterates over all points of a LWPolyline, changes the x and y coordinate. But the saved files has the same coordinates as the loaded. What do i miss?

Reply all
Reply to author
Forward
0 new messages