pythonezdxf

How can I use python to write a dxf file from coordinates?


I am trying to use ezdxf to write a dxf file from a list of coordinates.

I am able to create a dxf file with a simple line drawn using the code below:

import ezdxf
doc = ezdxf.new('R2010') # create a new DXF drawing in R2010 fromat 

msp = doc.modelspace() # add new entities to the modelspace
msp.add_line((0, 0), (10, 0)) # add a LINE entity
doc.saveas('test_line.dxf') 

For the files that I am working with, there is also a "Z" coordinate, which I do not know how to pass in based on what I have seen in the docs. I also want to pass in hundreds of coordinates in not just a single line.


Solution

  • Try this:

    import ezdxf
    doc = ezdxf.new('R2010') # create a new DXF drawing in R2010 fromat 
    
    msp = doc.modelspace() # add new entities to the modelspace
    lines = [(0, 0, 0), (10, 0, 0)], [(0, 0, 0), (20, 0, 0)],
    for line in lines:
        start = line[0]
        end = line[1]
        msp.add_line(start, end) # add a LINE entity
    doc.saveas('test_line.dxf') 
    

    It includes de Z-axis and add several lines to the file.