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.
This will draw a line from (0,0) to (1,1) and “Hello World” at (3,0)
import sdxf d=sdxf.Drawing() #set the color of the text layer to green d.layers.append(sdxf.Layer(name="textlayer",color=3)) #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")) d.saveas('hello_world.dxf')
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 b=Block('test') b.append(Solid(points=[(0,0,0),(1,0,0),(1,1,0),(0,1,0)],color=1)) b.append(Arc(center=(1,0,0),color=2)) #create a new drawing d=Drawing() #add the block to the Blocks table, so it can be referenced later d.blocks.append(b) #add entities to the drawing, including the block using the Insert entity d.append(Circle(center=(1,1,0),color=3)) d.append(Face(points=[(0,0,0),(1,0,0),(1,1,0),(0,1,0)],color=4)) d.append(Insert('test',point=(3,3,3),cols=5,colspacing=2)) d.append(Line(points=[(0,0,0),(1,1,1)]))
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.
Draws an arc (part of a circle).
Draws a 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.
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)] LwPolyLine(points=linePoints,flag=1)
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.
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.
Creates a bunch of lines from a set of points. Currently used instead of PolyLine.
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