# Example code for writing dxf files with arcs, circles, and ellipses

**URL:** <https://discourse.processing.org/t/example-code-for-writing-dxf-files-with-arcs-circles-and-ellipses/47754>\
**Category:** Development\
**Created:** [January 15, 2026, 3:15pm UTC](https://discourse.processing.org/t/example-code-for-writing-dxf-files-with-arcs-circles-and-ellipses/47754 "2026-01-15T15:15:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack23sms](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@jack23sms](https://discourse.processing.org/u/jack23sms)\
**Post date:** [January 15, 2026, 3:15pm UTC](https://discourse.processing.org/t/example-code-for-writing-dxf-files-with-arcs-circles-and-ellipses/47754/1 "2026-01-15T15:15:03Z")

</div>

I have seen a few questions about writing dxf files that contain arcs or circles. The processing dxf library treats these by breaking them down to lines which generates excessively long files. I recently ran across an [old forum post](https://forum.processing.org/one/topic/real-circles-arcs-using-dxf-export.html) where circles were generated but not arcs and the code was incomplete. I went back through the dxf documentation and found the needed information for creating arcs in dxf. Below is a modification of the example code from the old post, extending it to show how to create lines and arcs as well as circles. Page 86 of the autodesk [dxf reference document](https://images.autodesk.com/adsk/files/autocad_2012_pdf_dxf-reference_enu.pdf) discusses the format for ellipses, which I am sure can be treated in the same way. Points are discussed on page 123.  
EDIT: Code for drawing an ellipse tacked on at the end.  
This code does not use the dxf library and works in P2D.

```auto
String[] expDxf = {"0", "SECTION", "2","ENTITIES","0"};
 String fileName = "file2.dxf";
 
 float[] x = {10, 20, 30};
 float[] y = {10, 50, 100};
 float theta = 30; // beginning of arc in degrees
 float phi = 160; // end of arc

 float rad = 40; // radius
 String layer = "0";

void dxfLine(float x0, float y0, float x1, float y1){
  int oldLen = expDxf.length;
  int newLen = (expDxf.length+16);
  expDxf = expand(expDxf, newLen );
   expDxf [oldLen] = "LINE";
  expDxf [oldLen+1] = "8"; // layer name
  expDxf [oldLen+2] = layer;
  expDxf [oldLen+3] = "10"; // x position of start
  expDxf [oldLen+4] = str(x0);
  expDxf [oldLen+5] = "20"; // y position of start
  expDxf [oldLen+6] = str(y0);
  expDxf [oldLen+7] = "30"; // z position of start
  expDxf [oldLen+8] = "0.0"; // z value is zero
    expDxf [oldLen+9] = "11"; // x position of end
  expDxf [oldLen+10] = str(x1);
  expDxf [oldLen+11] = "21"; // y position of end
  expDxf [oldLen+12] = str(y1);
  expDxf [oldLen+13] = "31"; // z position of end
  expDxf [oldLen+14] = "0.0"; // z value is zero
  expDxf [oldLen+15] = "0";
}

void dxfCircle(float x, float y, float rad){ // , String layer) {
  int oldLen = expDxf.length;
  int newLen = (expDxf.length+12);
  expDxf = expand(expDxf, newLen );
  expDxf [oldLen] = "CIRCLE";
  expDxf [oldLen+1] = "8"; // layer name
  expDxf [oldLen+2] = "0";
  expDxf [oldLen+3] = "10"; // x position of center
  expDxf [oldLen+4] = str(x);
  expDxf [oldLen+5] = "20"; // y position of center
  expDxf [oldLen+6] = str(y);
  expDxf [oldLen+7] = "30"; // z position of center
  expDxf [oldLen+8] = "0.0";
  expDxf [oldLen+9] = "40"; // radius
  expDxf [oldLen+10] = str(rad);
  expDxf [oldLen+11] = "0";
}

// note: theta and phi are the starting and ending angles of the arc

void dxfArc(float x, float y, float rad, float theta, float phi){
    int oldLen = expDxf.length;
  int newLen = (expDxf.length+16);
  expDxf = expand(expDxf, newLen );
  expDxf [oldLen] = "ARC";
  expDxf [oldLen+1] = "8"; // layer name
  expDxf [oldLen+2] = layer;
  expDxf [oldLen+3] = "10"; // x position of center
  expDxf [oldLen+4] = str(x);
  expDxf [oldLen+5] = "20"; // y position of center
  expDxf [oldLen+6] = str(y);
  expDxf [oldLen+7] = "30"; // z position of center
  expDxf [oldLen+8] = "0.0";
  expDxf [oldLen+9] = "40"; // radius
  expDxf [oldLen+10] = str(rad);
  expDxf [oldLen+11] = "50"; // sstarting angle
  expDxf [oldLen+12] = str(theta);
  expDxf [oldLen+13] = "51"; // endng angle
  expDxf [oldLen+14] = str(phi);
  expDxf [oldLen+15] = "0";
}

void dxfEnd() {
  expDxf = expand(expDxf, expDxf.length+3 );
  expDxf[expDxf.length-3] = "ENDSEC";
  expDxf[expDxf.length-2] = "0";
  expDxf[expDxf.length-1] = "EOF" ;
  saveStrings(fileName, expDxf);
}

void setup(){
size(100,100);
noLoop();
dxfLine(x[0],y[0],x[1],y[1]);
dxfCircle(x[0],y[0],rad);
dxfArc(x[1],y[1],rad*2,theta, phi);
dxfEnd();
}

```

EDIT: ok, here is a method for generating an ellipse:

```auto
////// x and y define the center of the ellipse, 
///// endX and endY define the end of major axis, 
///// ratio is the ratio of minor to major axis length
void dxfEllipse(float x, float y, float endX, float endY, float ratio){
  int oldLen = expDxf.length;
  int newLen = (expDxf.length+18);
  expDxf = expand(expDxf, newLen );
  expDxf [oldLen] = "ELLIPSE";
  expDxf [oldLen+1] = "8"; // layer name
  expDxf [oldLen+2] = layer;
  expDxf [oldLen+3] = "10"; // x position of center
  expDxf [oldLen+4] = str(x);
  expDxf [oldLen+5] = "20"; // y position of center
  expDxf [oldLen+6] = str(y);
  expDxf [oldLen+7] = "30"; // z position of center
  expDxf [oldLen+8] = "0.0";
  expDxf [oldLen+9] = "11"; // x position of end of major axis
  expDxf [oldLen+10] = str(endX);
  expDxf [oldLen+11] = "21"; // y 
  expDxf [oldLen+12] = str(endY);
  expDxf [oldLen+13] = "31"; // z 
  expDxf [oldLen+14] = "0.0";
  expDxf [oldLen+15] = "40"; // ratio of axis lengths
  expDxf [oldLen+16] = str(ratio);
  expDxf [oldLen+17] = "0";
}

```
