admin管理员组文章数量:1604667
一.前言
本来考虑利用 的 cadLib 来生成CAD钻孔柱状图,到其官网一看….Professional版本RMB居然要6000+,嗯..真香…但是买不起买不起。
于是搜索了一些开源插件,发现几款不错的读写dxf的cad插件。
netDxf :https://github/haplokuon/netDxf
这个插件可以读写dxf,支持多种实体,包括hatch,以及一些常用实体等 不过可惜的是table只能读,不能使用行列来构建。
lxMilia.Dxf:https://github/IxMilia/Dxf
这个插件跟NetDxf差不多,不过在读写上比较麻烦,并且不能很好的支持hatch与table,似乎只有特定版本。
ezdxf: https://github/mozman/ezdxf
python 的读写dxf ,其中支持行列生成CAD table,并且也支持hatch,唯一不足的是hatch需要R2000以上 ,table 却只支持R12..两种没办法一起使用。
DocumentUltimate : 该插件能进行dxf 转pdf 还有展示pdf 啊,展示word。pdf转word 之类。
想了想,table不就是一个个的矩形框,里面放文字、线条等实体吗?于是因为基于,便选用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);
}
本文标签: 钻孔柱状图netdxfDocumentUltimatecad
版权声明:本文标题:利用 netDxf与DocumentUltimate 生成CAD钻孔柱状图 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1728463625a1159228.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论