利用 netDxf与DocumentUltimate 生成CAD钻孔柱状图

一.前言

 本来考虑利用.net 的 cadLib 来生成CAD钻孔柱状图,到其官网一看….Professional版本RMB居然要6000+,嗯..真香…但是买不起买不起。
于是搜索了一些开源插件,发现几款不错的读写dxf的cad插件。

netDxfhttps://github.com/haplokuon/netDxf
    这个插件可以读写dxf,支持多种实体,包括hatch,以及一些常用实体等 不过可惜的是table只能读,不能使用行列来构建。
lxMilia.Dxfhttps://github.com/IxMilia/Dxf
      这个插件跟NetDxf差不多,不过在读写上比较麻烦,并且不能很好的支持hatch与table,似乎只有特定版本。
ezdxf: https://github.com/mozman/ezdxf
    python 的读写dxf ,其中支持行列生成CAD table,并且也支持hatch,唯一不足的是hatch需要R2000以上 ,table 却只支持R12..两种没办法一起使用。

DocumentUltimate : 该插件能进行dxf 转pdf 还有展示pdf 啊,展示word。pdf转word 之类。

想了想,table不就是一个个的矩形框,里面放文字、线条等实体吗?于是因为基于.net,便选用netDxf+DocumentUltimate 。

二.设计

成果图
这里写图片描述

其中最重要的是抽象出 Cell 类,该类中包含以下主要属性与方法:

using netDxf;
using netDxf.Entities;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static WindowsFormsApplication1.Constant;

namespace ConsoleApplication1
{
   public class MyCell
    {

        private int row;
        private int col;

        private Vector3 LTPoint;
        private Vector3 LBPoint;
        private Vector3 RTPoint;
        private Vector3 RBPoint;

        private double width;
        private double height;


       private List<EntityObject> entityObj = new List<EntityObject>(); //CAD实体对象 



        public MyCell(MText text, double width,double height, Vector3 LTPoint,Location location,LineLocation lineLocation):this(width,height,LTPoint, lineLocation)
        {
            text.Position = setStartPoint(location);
            entityObj.Add(text);
        }



        public MyCell(double width, double height, Vector3 LTPoint,LineLocation lineLocation)
        {
            this.width = width;
            this.height = height;
            this.LTPoint = LTPoint;

            setOtherPoint();
            setLine(lineLocation);
        }


        private void setOtherPoint()
        {
            this.LBPoint = new Vector3(LTPoint.X, LTPoint.Y - height,0);
            this.RTPoint = new Vector3(LTPoint.X + width, LTPoint.Y,0);
            this.RBPoint = new Vector3(LTPoint.X + width, LTPoint.Y - height, 0);
        }
        /// <summary>
        /// 得到实体 在Cell中的位置
        /// </summary>
        /// <param name="objLocation">枚举类型 ,对象展示在矩形中的位置</param>
        /// <returns></returns>
        public Vector3 setStartPoint( Location objLocation)
        {
            double x, y;
            var addValue = 0.5;
            switch (objLocation)
            {
                case Location.TOP:
                     x = (RTPoint.X+ LTPoint.X) / 2;
                    y = LTPoint.Y;
                    break;
                case Location.LEFT:
                    x = LTPoint.X+ addValue;
                    y = (LBPoint.Y + LTPoint.Y) / 2;
                    break;
                case Location.BOTTOM:

                    x = ((RTPoint.X + LTPoint.X) / 2);
                    y = LBPoint.Y+ addValue;
                    break;
                case Location.RIGHT:
                    x = RTPoint.X;
                    y = (LBPoint.Y + LTPoint.Y) / 2;
                    break;
                case Location.CENTER:
                    x = (RTPoint.X + LTPoint.X) / 2;
                    y = (LBPoint.Y + LTPoint.Y) / 2;
                    break;
                case Location.NONE:

                    x = LTPoint.X + addValue;
                    y = LTPoint.Y - addValue;
                    break;
                default:
                    x = LTPoint.X;
                    y = LTPoint.Y;
                    break;
            }
            return new Vector3(x, y, 0);
        }

        /// <summary>
        /// 画矩形边框线
        /// </summary>
        /// <param name="lineLocation">我的枚举类型</param>
        private void setLine(LineLocation lineLocation) 
        {
            switch (lineLocation)
            {
                case LineLocation.NO_BOTTOM:
                    LwPolyline poline = new LwPolyline();
                    Polyline line = new Polyline(new List<Vector3> {  LBPoint, LTPoint,RTPoint, RBPoint });
                    entityObj.Add(line);
                    break;
                case LineLocation.ALL:
                     poline = new LwPolyline();
                     line = new Polyline(new List<Vector3> { LTPoint, LBPoint, RBPoint, RTPoint, LTPoint });
                    entityObj.Add(line);
                    break;
                case LineLocation.NO_BOTTOM_TOP:
                    Line line1 = new Line(LTPoint,LBPoint);
                    Line line2 = new Line(RTPoint,RBPoint);
                    entityObj.Add(line1);
                    entityObj.Add(line2);
                    break;
                default:
                    break;
            }

        }


        #region Get AND Set







        #endregion
    }
}

1.1) 通过4个角点构建一个个的矩形,并利用row 与 col 属性,构建二维数组Cells。Cell类中还包括了画线以及设置实体的位置。

1.2) 构建其他实体:例如我的柱状图有文本、长短线、hatch填充、曲线等。于是便构建这些实体,将其添加到cell 类 中的 entityObj。

1.3)建立操作类:实现 新建实体、添加实体到cell…

1.4)遍历Cells 中的entityObj 生成dxf文件。

1.5)利用DocumentUltimate 转换dxf 成Pdf :

       /// <summary>
        /// 创建PDF
        /// </summary>
        /// <param name="dxfPath"></param>
        /// <param name="pdfPath"></param>
        private static void createPdf(string dxfPath, string pdfPath)
        {
            BackSlashPath documentConverter = new BackSlashPath(dxfPath);
            BackSlashPath outputDocument = new BackSlashPath(pdfPath);
            var outputFormat = (DocumentFormat)Enum.Parse(typeof(DocumentFormat), "Pdf");
            var result = GleamTech.DocumentUltimate.DocumentConverter.Convert(documentConverter, outputDocument, outputFormat);

        }
  • 5
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 17
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值