JDXF: Java DXF Library

The Java JDXF library provides support for generation of DXF files for use with CAD programs using standard Java AWT Graphics “draw” and “fill” commands. The library provides a special Graphics2D subclass, DXFGraphics, that is associated with a DXF document and that renders draw commands into DXF syntax. A sequence of standard Java Graphics drawing method calls on a DXFGraphics instance will thus create a structured DXF document that, when opened with a standard CAD program, will display the Java image as a CAD design that can be modified within the CAD program.

This library is released under the MIT License.

Basic Workflow

The class DXFGraphics implements the graphics operations defined by the standard Java Graphics2D class. To create a DXF drawing, you use the standard Java Graphics drawing calls (including drawing, transformations, etc.) on an instance of DXFGraphics. The DXFGraphics will encode these Java drawing commands as DXF objects in an associated DXFDocument, which can then be retrieved as a text string and saved in a DXF file.

A DXFGraphics is associated with a DXFDocument. The basic workflow is:

/* Create a DXF document and get its associated DXFGraphics instance */

DXFDocument dxfDocument = new 
    DXFDocument("Example");
DXFGraphics dxfGraphics = 
    dxfDocument.getGraphics();
 

/* Do drawing commands as on any other Graphics. If you have a paint(Graphics) method, you can just use it with the DXFGraphics instance since it's a subclass of Graphics. */
paint(dxfGraphics);
 
/* Get the DXF output as a string - it's just text - and  save  in a file for use with a CAD package */
String stringOutput = dxfDocument.toDXFString();
String filePath = “path/to/file.dxf”;
FileWriter fileWriter = new FileWriter(filePath);
fileWriter.write(dxfText);
fileWriter.flush();
fileWriter.close();


/* For drawing, just use standard Java
   drawing operations */
public void paint(Graphics graphics)
{
  // set pen characteristics
  graphics.setColor(Color.RED);
  graphics.setStroke(new BasicStroke(3));
  
  // draw stuff - line, rectangles, ovals, ...
  graphics.drawLine(0, 0, 1000, 500);
  graphics.drawRect(1000, 500, 150, 150);
  graphics.drawRoundRect(20, 200, 130, 100, 20, 
                                             10);
  graphics.drawOval(200, 800, 200, 400);
  graphics.drawArc(100, 1900, 400, 200, 60, 150);

  // can draw filled shapes, which get 
  // implemented as DXF hatches
  graphics.setColor(Color.BLUE);
  graphics.fillRect(100, 100, 100, 50);
  int[] xPoints = {200, 300, 250};
  int[] yPoints = {200, 250, 300};
  graphics.fillPolygon(xPoints, yPoints, 
                                xPoints.length);

  // text too
  graphics.setFont(new Font(Font.MONOSPACED, 
                            Font.PLAIN, 38));
  graphics.drawString("Some 38-point monospaced   
      blue text at position 480, 400", 480, 400);

  // and even transformations
  graphics.shear(0.1f, 0.2f);
  graphics.drawRect(100, 100, 200, 200);
}

 

Features

The JDXF library provides most of the drawing operations available in the standard Java Graphics and Graphics2D classes. However, certain methods are unimplemented at present (some due to lack of support in DXF). These unimplemented operations will be ignored or throw an UnsupportedOperationException if used, as indicated below. Some additional features currently have no or limited support, as indicated below.

Supported operations

  • Drawing operations, including
    • drawLine, drawPolyline
    • drawPolygon, fillPolygon
    • drawArc, fillArc
    • drawOval, fillOval
    • drawRect, fillRect, clearRect
    • drawRoundRect, fillRoundRect
    • draw(Shape), fill(Shape), clear(Shape)
  • Text operations
    • drawString, drawChars, drawBytes
    • registerFont
  • Drawing properties
    • Color
    • Line width
    • Font properties: serif/sans-serif, size, bold, italic
  • Affine transformation operations
    • scale
    • shear
    • translate
    • rotate

Extensions

The library provides methods for creating and drawing B-splines on the standard Java Graphics, as well as generating DXF code for splines.

Not supported

  • Image drawing – throws UnsupportedOperationException
  • Paint xfermodes – Ignored
  • Clipping, including clipRect, clipRegion, clipPath, etc. – Ignored

Enhancements for version 1.1

  • Added method setUnits in DXFDocument for specifying the units that measurements should be interpreted as in the DXF file
  • Added the ability to load external fonts through a new DXFGraphicsEnvironment class for use in addition to the default fonts. When these external font files (TrueType, e.g.) are then loaded into the CAD program along with the DXF file, the font sizing and spacing will be much more consistent between the Java screen representation and the CAD program representation.

Example DXF Files

Some example files generated with JDXF are at the links below. The Java source code used to generate these through the JDXF Library is available in the Example Code section.

Example Code

A test project is available that illustrates how to use the library and that can also be used to test its functionality. The class JDXFTestPanel illustrates the use of the JDXFGraphics class and its draw methods.

  • JDXFTest.java
  • JDXFTestPanel.java

Download example source

Documentation

Downloads

The downloads are supplied as gzipped tar archives; the library archive contains the compiled gnuapdf.jar file for inclusion in a project, while the source archive contains the Java source files that can be used to rebuild the .jar file or included as source files in a referencing project. The test app source contains source for a simple test app that uses the classes in the ADXF library.

Version
Library (.jar)
Source
Test App Source
 1.0 jdxf_lib_v1.0.tar.gz jdxf_src_v1.0.tar.gz jdxf_test_v1.0.tar.gz
1.1.1 jdxf_lib_v1.1.1.tar.gz jdxf_src_v1.1.1.tar.gz jdxf_test_v1.1.tar.gz

Revision History

Version
Date Changes
1.0 2018-05-22
  • Initial release
1.1 2019-05-18
  • Loadable font support
  • Units specification
  • Bug fix for quadratic and cubic path iterator segments
1.1.1 2019-06-10
  • Bug fixes for LibreCAD, Kabeja
  • Closed flag for closed polylines
  • Double precision throughout

 

Notes

The JDXF library adds DXF elements that are not strictly required by the standard, but are required in order to open the files in AutoCAD.

Computing, lutherie, mathematics, finance, and other resources