Graphics2D turn DXF Java package ---- JDXF

Today think of SES node file is the question of how to export CAD files of the Java graphics, suddenly found JDXF Java package, but in May 2018 the first edition was published, I feel the next read. Original at the following address: https://jsevy.com/wordpress/

Articles from a variety of websites that the author is a very interesting old man, all kinds of things get very interesting.

Closer to home, Java JDXF is to provide support to generate DXF files using Java AWT graphical Java Development Kit, provides a special subclass of Graphics2D, DXFGraphics, with its DXF file (in fact, is a text format) associated with and presented to the draw command DXF syntax. Therefore, a series of standard Java graphics rendering method invoked on DXFGraphics instance will create a structured DXF file.

The Java Development Kit under the MIT license the license issued.

The basic process:

DXFGraphics class implements the standard Java Graphics2D class definition graphics operations. To create a DXF graphics file, you first need to use the standard Java instance on DXFGraphics graphics rendering calls (including drawings, conversion, etc.). These Java DXFGraphics coded as DXF drawing commands associated with the object DXFDocument, and then it can be retrieved and saved as a text string to a DXF file.

DXFGraphics with DXFDocument associated. The basic workflow is as follows:

/* 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);
}

characteristic

JDXF package provides most of the drawing operation and the standard Java class Graphics2D graphics available. However, there is no certain method to achieve (some because similar operations DXF missing). These unrealized will be ignored, or throw an UnsupportedOperationException In use, as shown below. As shown below, some additional features are currently no or limited support.

prompt:

JDXF library adds DXF standard parameters is not critical, but in order to open the file in AutoCAD, these elements are required.

 

Published 34 original articles · won praise 9 · views 90000 +

Guess you like

Origin blog.csdn.net/tianyatest/article/details/104421374