SDXF – Python Library for DXF

Stani’s DXF library for python is an excellent tool for writing lines, circles, and polygons in DXF format. DXF (Document Exchange Format) is an ASCII file format published by AutoCad which can be read by a variety of programs, including Blender, Maya, CorelDraw, and a host of others.

I’ll attempt to document the library here as I figure it out, but am no longer actively working with this library. As of July 2012, it has a new home on GitHub! If you have an update / bug fix, you can submit a pull request.

Download SDXF 1.1.1

Changes in 1.1.1

  • Added support for LWPOLYLINE, so you can now make a continuous polyline instead of several separate lines

Example Code

This will draw a line from (0,0) to (1,1) and “Hello World” at (3,0)

import sdxf


#set the color of the text layer to green

#add drawing elements
d.append(sdxf.Text('Hello World!',point=(3,0),layer="textlayer"))
d.append(sdxf.Line(points=[(0,0),(1,1)], layer="drawinglayer"))




An Entity is an individual drawing object, like a line or circle. These are appended to the Drawing and rendered in the order they’re added.


Layers are used to organize entities. Layers can be assigned colors to make drawings easier to read. An entity can be assigned to a new layer on the fly, without explicitly defining the layer first.



Blocks are reusable symbols. A block is defined once and can then be appended to the drawing using the Insert entity. A block can be inserted multiple times into a drawing, at different points.

#define the block, a Solid and an Arc

#create a new drawing

#add the block to the Blocks table, so it can be referenced later

#add entities to the drawing, including the block using the Insert entity

Supported Entities

These entities are currently in the library. In addition to passing the arguments for the individual entity type, you can pass common arguments (group codes) which are available for all entities.

Common Group Codes

  • color – the color of the entity. Represented by a number. See the Color List below.
  • extrusion – ?
  • layer – which layer to place the element on. You do not need to explicitly declare a layer before assigning entities to it
  • lineType – ?
  • lineTypeScale – ?
  • thickness – thickness of the entity lines
  • parent – ?


Draws an arc (part of a circle).

  • center (x, y, z) – The center of the circle from which the arc is to be taken. Z is optional.
  • radius – The radius from the center to the arc
  • startAngle – The angle, in degrees, for the start of the arc.
  • endAngle – The angle, in degrees, for the end of the arc


Draws a circle.

  • center (x,y,z) – the center of the circle. Z is optional.
  • radius – the radius of the circle


Creates a 3d face. A 3d face takes 4 points, which may or may not all be on the same plane.


Blocks are added to a file using the Insert entity. The block must be added to the Blocks table before it can be used.

  • name – Block name (defined when the block was added to the Blocks table)
  • point – Insertion point (x,y,z) to add the block
  • xscale – x scale factor; optional, defaults to 1
  • yscale – y scale factor; optional, defaults to 1
  • zscale – z scale factor; optional, defaults to 1
  • cols – column count; optional, defaults to 1
  • colspacing – column spacing; optional, defaults to 0
  • rows – row count; optional, defaults to 1
  • rowspacing – row spacing; optional, defaults to 0
  • rotation – rotation angle; optional, defaults to 0


Makes a line! Takes a list containing two points. Points can be (x,y) or (x,y,z)



Makes a line with vertexes. Takes a list of points.

linePoints = [(0,0),(1,1),(1,0)]


In the current implementation, actually just makes a bunch of Lines rather than a polyline.


A point in space. Takes a point (duh).


From what I can tell, this creates a 3D solid by taking either 3 or 4 points and then extruding the face in various directions.


Renders a string as text at the given point.

Text('Hello World!',point=(3,0))


I think this is like Text but supports line breaks. In this version of SDXF it just creates multiple Text entities.


These are not actual DXF entities, but are classes that make building other shapes easier.


Creates a rectangle using 4 lines. Could probably be modified to use LwPolyLine instead.

  • point – lower left (?) corner point
  • width – rectangle width
  • height – rectangle height
  • solid – ?
  • line – ?

Line List

Creates a bunch of lines from a set of points. Currently used instead of PolyLine.

  • points – list of verticies
  • closed – whether to close the shape; defaults to 0

Color List

The colors may vary depending on the rendering software used.
1 – Red
2 – Yellow
3 – Green
4 – Cyan
5 – Blue
6 – Magenta
7 – White
8 – Black

24 thoughts on “SDXF – Python Library for DXF

  1. […] started documenting the library, it’s called SDXF and is pretty thorough. I don’t know Python, or DXF, but so I’m picking up both as I go […]

  2. […] I generated the pattern with a python script I wrote, using the sdxf library. […]

  3. Andrew Price says:

    Thanks so much for this page Kellbot! I couldn’t find a copy of sdxf anywhere, much less such a useful and concise explanation.

    I am hoping to produce a solar concentrator dish with it — will let you know how it turns out.

    Andy :)

  4. morgans says:

    Hey! great page! you save mi life.! 1 cuestion: how can open a dxf that alredy exits?

  5. annoymous says:

    I guess that xscale DXF code within Text class should be 40 instead of 41 (line #302)

  6. Sun says:

    This is a great tool for the kind of thing I’m trying to do (highway alignment related calculations). Unfortunately the LWPOLYLINE object doesn’t seem to work. Whenever I try and import the dxf into AutoCAD, I get an error:

    “Undefined group code 70 for object on line 138.”

    When I use the POLYLINE object (which produces many individual lines) it imports ok, but the LWPOLYLINE doesn’t seem to work.

    I’ll do more tests and see what I can discover.

    1. Kellbot says:

      Hm, interesting. I haven’t tried it with AutoCAD, just QCAD, and I honestly haven’t put it through its paces, so there’s bound to be a few snags in LWPOLYLINE. Let me know if you figure out where it’s coming from!

  7. demizer says:

    I am a Machinist and dxf is practically my life. I am currently working on a C library for dxf, so this can be helpful to me. Thanks!

  8. Sun says:

    I did a bit of messing around with the original PolyLine class during my morning commute and it seems to work now (ie. it generates a dxf containing a polyline that AutoCAD will accept), but it still needs a lot of testing. In particular I want to see if it works for polylines with different Z coordinates on each vertex. (I only tested it with an XY list.) Kell, let me know if you want me to send it to you as it is now, otherwise I’ll debug it a bit more first.

  9. I fussed with the PolyLine class and now Autocad2009 seems to accept it.

    class PolyLine(_Entity):
    def __init__(self,points,flag=1,width=None,**common): #Not sure if any other values of flag will work
    def __str__(self):
    result= ‘0\nPOLYLINE\n%s\n66\n%s\n’%\
    result+=’%s’ %_point((0.,0.,0.))
    for point in self.points:
    result+=’\n0\nVERTEX\n8\n%s\n%s’% (self.layer,_point(point))
    if self.width:result+=’\n40\n%s\n41\n%s’%(self.width,self.width)
    return result

  10. song says:

    I have the same issue about LwPolyLine.
    It’s show “Undefined group code 70 for object on line 138.” when I open dxf file with AutoCAD.
    Please let me know if it can be solved.

  11. nimpbus says:

    thanks a bunch! This saves me a headache!

  12. Derek says:

    DWG TrueView and Intellicad will open the dxf generated via the module test using the following class modified from the one provided by David:
    class PolyLine(_Entity):
    def __init__(self, points, flag=0, width=None, **common):
    _Entity.__init__(self, **common)
    self.points = points
    self.flag = flag
    self.width = width

    def __str__(self):
    result = ‘0\nPOLYLINE\n%s\n66\n1\n70\n%s\n’ % (self._common(), self.flag)
    result += ‘%s’ % _point((0.0, 0.0, 0.0))
    for point in self.points:
    result += ‘\n0\nVERTEX\n8\n%s\n%s’ % (self.layer,_point(point))
    if self.width:
    result += ‘\n40\n%s\n41\n%s’ % (self.width, self.width)
    result += ‘\n0\nSEQEND’
    return result
    I’ve set code 66 as 1 and added code 70 to accept the standard polyline flags.

    In the “PolyLine” call in the module test, change “closed” to “flag”; leave the value set as “1” for a closed polyline.

    I’ve been using the following resources to help understand the file format:

  13. Panos M. says:

    If you visit the following site you will find a different branch of sdxf that is used by Blender and it supports Polyline. :
    You can download the library from here:

    If you have made significant changes from 1.1 perhaps the two projects could be merged?

  14. Chinnaswamy says:

    Hi ,
    I have tried to run Example code of Hello World with Python 2.6.4. When tried to run the program, the message is “There’s an error in your program: invalid syntax”.

    The item that highlighted in the pythons code is “6” of Python 2.6.4.

    Ca you please help in sorting out.



  15. Johannes Schmitz says:

    how can I draw a point, I can not find out the exact syntax.


    This does not work for me.
    I am opening the resulting file with qcad. Drawing Lines and Text like in the example works fine.


  16. Johannes Schmitz says:

    I found that even in your test code at the end of
    There is no test for Point.
    You should complete your testcases.

    1. Kellbot says:

      Hi johannes,

      I’m not working on this project at the moment, but if you make improvements I’d be happy to add them.

      I need to move this onto GitHub but haven’t had the spare cycles to do so. Perhaps someone already has?!

  17. Hola podrías publicar la documentación completa de esta librería?? Gracias

    1. Kellbot says:

      Hi Franco, this is all the documentation there is, sorry! But if you play with it and want to add to the documentation, send it to me and I can add it.

      Hola Franco, esta es toda la documentación que hay, lo siento! Pero si usted trabaja con él y desea agregar a la documentación, me lo envíe y puedo añadir.

  18. GBurnell says:

    For those banging their headers against a brick wall with lwpolylines:
    The issue with LWPOLYLINEs is that they were only introduced in R14. Autocad (unlike perhaps other dxf readers?) is a bit strict and believes the version number it is given in the dxf file and carps if it sees entities that shouldn’t exist in R10 files. Unfortunately bumping the release number up to emulate R14 files isn’t sufficient as you also need to have the code generate handles for lots of entities. So far I can’t get a set of handles that doesn’t make autocad fall over…

  19. zyw says:

    Thanks a lot for the simple but very useful library. It’s so useful and simple. It’s very good.

  20. Giuliano says:

    I have this traceback
    Traceback (most recent call last):
    File “”, line 171, in
    File “”, line 143, in main
    File “C:\Program Files\GRASS GIS
    6.4.3svn\etc\python\”, line 556, in __init__
    TypeError: __init__() got an unexpected keyword argument
    (Thu Oct 09 04:41:51 2014) Comando terminato (3 sec)
    (Thu Oct 09 04:43:59 2014) dns=C:\Users\Giuliano\Desktop4_riu_chixinagghju_prova.RASexport.sdf output_esond=C:\Users\Giuliano\Desktop\provissima2.dxf
    Traceback (most recent call last):
    File “”, line 171, in
    File “”, line 143, in main
    File “C:\Program Files\GRASS GIS
    6.4.3svn\etc\python\”, line 556, in __init__
    TypeError: __init__() got an unexpected keyword argument

  21. Julien says:

    Is there a way to read DWG/DXF files and convert to SVG ?

Leave a Reply