今天想到SES节点文件采用Java绘图后如何导出CAD文件的问题,突然发现JDXF Java包,而且是2018年5月才发布的第一版,感觉看了下。原文网址如下:https://jsevy.com/wordpress/
从网站的各种文章来看,作者是个非常有意思的老头,弄的各种东西也非常有趣。
言归正传,Java JDXF是为使用Java AWT图形生成DXF文件提供支持的Java开发包,提供了一个特殊的Graphics2D子类,DXFGraphics,它与DXF文件(其实是文本格式)相关联,并将draw命令呈现到DXF语法中。因此,在DXFGraphics实例上调用的一系列标准Java图形绘制方法将创建一个结构化的DXF文件。
该Java开发包使在MIT license 许可之下发布的。
基本流程:
DXFGraphics类实现标准Java Graphics2D类定义的图形操作。要创建DXF图形文件,首先需要在DXFGraphics实例上使用标准的Java图形绘制调用(包括绘图、转换等)。DXFGraphics将这些Java绘图命令编码为相关联的DXFDocument中的DXF对象,然后可以将其作为文本字符串检索并保存到DXF文件中。
DXFGraphics与DXFDocument相关联。基本工作流程如下:
/* 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);
}
特性
JDXF包提供了标准Java图形和Graphics2D类中可用的大部分绘图操作。但是,某些方法目前还没有实现(有些是因为DXF中缺少类似的操作)。这些未实现的操作将被忽略,或者在使用时抛出一个UnsupportedOperationException,如下所示。如下所示,一些附加功能目前没有或有限的支持。
提示:
JDXF库添加了标准并不严格要求的DXF参数,但是为了在AutoCAD中打开文件,这些元素是必需的。