用Java簡單封裝了一個導出DXF文件的庫,沒有使用其他第三方接口,目前支持輸出點、多段線、單行文本、塊、圖層、支持更改任意一個圖元的顏色,塊支持自定義屬性(增強屬性塊,屬性字段可自定義添加),多段線的凸度計算不出來沒頭緒先不搞,後續功能有空再升級繪製其他圖元。。。
調用過程如下:
DXFDocument dxfDocument = DXFDocument.newInstance();
//region 定義圖層
//初始化線圖層
LayerTableEntity lineLayer = new LayerTableEntity("line");
//設置圖層顏色
lineLayer.setLayerColorIndex(2);
//定義圖層到文檔
dxfDocument.createLayer(lineLayer);
//初始化點圖層
LayerTableEntity pointLayer = new LayerTableEntity("point");
//設置圖層顏色
pointLayer.setLayerColorIndex(10);
//定義圖層到文檔
dxfDocument.createLayer(pointLayer);
//endregion
//region 定義塊
BlockCustom blockCustom = new BlockCustom(dxfDocument, "WEIPI");
//塊內容定義
LWPolyLineEntity lwPolyLineEntity = new LWPolyLineEntity(0, 0, 0);
lwPolyLineEntity.addDXFPoint(new DXFPoint(0.1, 0, 0));
lwPolyLineEntity.addDXFPoint(new DXFPoint(-0.1, 0, 0));
lwPolyLineEntity.setWidth(0.2);
blockCustom.addEntity(lwPolyLineEntity);
//塊屬性定義
Att attPointName = new Att("x", "0");
Att attPointHeight = new Att("y", "0");
blockCustom.addAtt(attPointName);
blockCustom.addAtt(attPointHeight);
blockCustom.addAtt( new Att("PointName", "Point-1"));
//定義塊到文檔中
dxfDocument.defineBlock(blockCustom);
//endregion
//region 線、塊繪製
Random random = new Random();
//產生100條多段線每條線50個點,顏色隨機,
for (int j = 1; j <= 20; j++) {
LWPolyLineEntity polyLineEntity = new LWPolyLineEntity(0, 0, 0);
polyLineEntity.setLayer(lineLayer);
polyLineEntity.setColor(random.nextInt(240));
for (int i = 0; i < 50; i++) {
double x = i * j;
double y = 10 * Math.sin(i * j) + j * 10;
polyLineEntity.addDXFPoint(new DXFPoint(x, y, 0));
BlockEntity blockEntity = new BlockEntity(dxfDocument, x, y, 0.0, blockCustom);
blockEntity.setLayer(pointLayer);
//添加屬性
Att pn = new Att("PointName", "Point" + j + "-" + i);
pn.setTextColorIndex(random.nextInt(240));
Att xAtt = new Att("x", x + "");
//設置該屬性不顯示到圖中
xAtt.setAttStatus(Att.ATT_STATUS_UNVISIBLE);
Att yAtt = new Att("y", y + "");
yAtt.setAttStatus(Att.ATT_STATUS_UNVISIBLE);
blockEntity.addAtt(xAtt);
blockEntity.addAtt(yAtt);
blockEntity.addAtt(pn);
blockEntity.setTextColorIndex(random.nextInt(240));
dxfDocument.addEntity(blockEntity);
}
dxfDocument.addEntity(polyLineEntity);
}
//endregion
try {
dxfDocument.writeToDXF("78.dxf");
} catch (Exception e) {
e.printStackTrace();
}
文件預覽
細節:
屬性查看
文件有點大 5M