Java 生成dxf文件

    用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 

猜你喜欢

转载自blog.csdn.net/WPR13005655989/article/details/93773614
今日推荐