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 dxfText = 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);
}

User Guide

More examples and discussion can be found on the JDXF User Guide.

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.

Enhancements for version 1.2

  • Configure to generate true DXF circles and arcs; earlier versions used the more general elliptical arcs for all such curves. Includes the method generateCircularArcs(boolean) in DXFDocument to control this behavior in case the previous behavior is desired (default is to use the new approach and generate circles and arcs when appropriate).
  • Added the method setPrecisionDigits(int) in DXFDocument to control the number of decimal digits to be output in the DXF file, to deal with rounding issues; default is to use 10 digits.

Enhancements for version 1.3

  • Configure to generate true DXF points from zero-length Java lines and zero-radius Java circles; earlier versions simply inserted the corresponding degenerate lines or circles into the DXF output. Includes the method generatePoints((boolean) in DXFDocument to control this behavior in case the previous behavior is desired (default is to use the new approach and generate DXF points in place of degenerate circles and lines).

Enhancements for version 1.4

  • Add support for drawing onto different DXF layers through a call to the setLayer(String) method of the main DXFDocument class:
DXFDocument dxfDocument = new 
    DXFDocument("Example");
DXFGraphics dxfGraphics =
    dxfDocument.getGraphics();

// set layer - drawing goes 
// onto Layer 1
dxfDocument.setLayer("Layer 1");
graphics.drawRect(1000, 500, 150, 150);

// set new layer - all subsequent drawing 
// goes onto Layer 2
dxfDocument.setLayer("Layer 2");
graphics.drawOval(1000, 500, 150, 150);

Enhancements for version 1.5

  • Add support for dashed lines

Enhancements for version 1.6

  • Improve efficiency of String operations for greatly enhanced execution speed

Enhancements for version 2.0

  • Add support for generation  and insertion of DXF Blocks from Java Shapes
  • Add color/linewidth/linetype parameters to Layer definitions, and support for assigning value “ByLayer” to entities’ parameters so that they will take on the pen characteristics of the layer they’re placed on

Enhancements for version 2.1

  • Add support for Xrecords to include non-graphical information in DXF files

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

The example source can be downloaded from the link below.

Documentation

Downloads

The downloads are supplied as compressed 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 JDXF library.

Current Version

Version
Library (.jar)
Source
Test App Source
2.1 jdxf_lib_v2.1.1.zip jdxf_src_v2.1.1.zip jdxf_test_v2.1.1.zip
Previous Versions
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
1.2 jdxf_lib_v1.2.zip jdxf_src_v1.2.zip jdxf_test_v1.2.zip
1.3 jdxf_lib_v1.3.zip jdxf_src_v1.3.zip jdxf_test_v1.3.zip
1.4 jdxf_lib_v1.4.zip jdxf_src_v1.4.zip jdxf_test_v1.4.zip
1.5 jdxf_lib_v1.5.zip jdxf_src_v1.5.zip jdxf_test_v1.5.zip
1.5.1 jdxf_lib_v1.5.1.zip jdxf_src_v1.5.1.zip jdxf_test_v1.5.zip
1.6 jdxf_lib_v1.6.zip jdxf_src_v1.6.zip jdxf_test_v1.5.zip
2.0 jdxf_lib_v2.0.zip jdxf_src_v2.0.zip jdxf_test_v2.0.zip
2.1 jdxf_lib_v2.1.zip jdxf_src_v2.1.zip jdxf_test_v2.1.zip
2.1.1 jdxf_lib_v2.1.1.zip jdxf_src_v2.1.1.zip jdxf_test_v2.1.1.zip

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
1.2 2020-03-03
  • Configure to generate DXF circles and arcs where appropriate in place of general elliptical arcs
  • Add call to configure number of decimal digits of precision to use
1.3 2020-05-08
  • Configure to generate DXF points for zero-length Java lines and zero-radius circles
  • Fix bug in draw(Shape) method for Line2D handling
1.4 2020-12-09
  • Add support for layers
1.5 2021-02-03
  • Add support for dashed lines
  • Fix bug in text scaling
1.5.1 2021-05-14
  • Change PlotStyleName handle value to 0 in DXFLayer for AutoCAD 2022 compatibility
1.6 2022-04-08
  • Replace raw String operations with StringBuilder objects to improve execution speed
2.0 2022-10-24
  • Support Block generation from Java Shapes
  • Add color and line styles to Layers, and support for ByLayer value in entities so they inherit the color/line of the layer they’re in
2.1 2023-04-03
  • Add support for Xrecords to include non-graphical information
2.1.1 2024-09-01
  • Fix bug in DXFInsert scaling for transformation angles pi/2 and -pi/2

 

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