C#基于Autocad的二次开发(3.连线绘制)

开始之前确保已完成步骤1创建解决方案和类库项目,并在对应项目中安装nuget包

开始绘制连线操作之前,首先在ACD.Domain项目中创建”点位类”(步骤2中已创建,这里略),”连线类”。两点确认一条直线,所有连线类中需要有两个点位,同插入块的操作一样,线条也需要绘制到指定的图层,线条具有不同的类型,比如:线性标注,普通的轮廓线等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 /// <summary>
/// 连线类
/// </summary>
public class LinkLineParam
{
public LineType LineType { get; set; }

public ACDPoint3D StartPoint { get; set; }

public ACDPoint3D EndPoint { get; set; }
/// <summary>
/// 图层名称
/// </summary>
public string LayerName { get; set; }
/// <summary>
/// 标注偏移方向和单位
/// </summary>
public double DimPosition { get; set; }

public LinkLineParam() { }

public LinkLineParam (LineType lineType, ACDPoint3D startPoint, ACDPoint3D endPoint, string layerName, double dimPosition)
{
LineType = lineType;
StartPoint = startPoint;
EndPoint = endPoint;
LayerName = layerName;
DimPosition = dimPosition;
}
}

LineType枚举类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/// <summary>
/// 线条类型
/// </summary>
public enum LineType
{
/// <summary>
///
/// </summary>
[Description("轮廓线")]
BlockLinkLine=0,

/// <summary>
///
/// </summary>
[Description("标注线")]
DimensionLine,

/// <summary>
///
/// </summary>
[Description("折痕线")]
CreaseLine
}

ACD.CadCore项目中创建类文件ACDOperation.LinkLine.cs,实现连线操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public partial class ACDOperation
{
public bool LinkLine(string dwgFilePath, string newFilePath, IEnumerable<LinkLineParam> lines)
{
bool result = true;

CheckFilePath(dwgFilePath, newFilePath);

File.Copy(dwgFilePath, newFilePath, true);

using (Database blockDb = new Database(false, true))
{
blockDb.ReadDwgFile(newFilePath, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
blockDb.CloseInput(true);

result = LinkLine(blockDb, lines);
}

if (!result) File.Delete(newFilePath);

return result;
}

public bool LinkLine(IDynamicMetaObjectProvider database, IEnumerable<LinkLineParam> lines)
{
bool result = true;

Line line;
LayerTable layers;
LayerTableRecord layer = null;
Point3d startPoint, endPoint;

Database curDb = database as Database;

using (Transaction curTrans = curDb.TransactionManager.StartTransaction())
{
try
{
layers = curTrans.GetObject(curDb.LayerTableId, OpenMode.ForRead) as LayerTable;

BlockTableRecord curSpace = curTrans.GetObject(curDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;


foreach (var param in lines)
{
endPoint = new Point3d(param.EndPoint.X, param.EndPoint.Y, param.EndPoint.Z);
startPoint = new Point3d(param.StartPoint.X, param.StartPoint.Y, param.StartPoint.Z);

if (layers.Has(param.LayerName))
{
layer = curTrans.GetObject(layers[param.LayerName], OpenMode.ForWrite) as LayerTableRecord;
}
if (param.LineType == LineType.DimensionLine)
{
//线性标注
AlignedDimension dimension = new AlignedDimension();
dimension.XLine1Point = startPoint;
dimension.XLine2Point = endPoint;
dimension.DimLinePoint = GetDimLinePoint(startPoint, endPoint,param.DimPosition);

if (!layer.IsNull()) dimension.LayerId = layer.Id;

curSpace.AppendEntity(dimension);
curTrans.AddNewlyCreatedDBObject(dimension, true);
}
else
{
//轮廓线
line = new Line(startPoint, endPoint);

if (!layer.IsNull()) line.LayerId = layer.Id;

curSpace.AppendEntity(line);
curTrans.AddNewlyCreatedDBObject(line, true);
}
}
curTrans.Commit();
}
catch (Autodesk.AutoCAD.Runtime.Exception)
{
curTrans.Abort();
result = false;
}
}
return result;
}

private Point3d GetDimLinePoint(Point3d startPoint, Point3d endPoint, double dimPosition)
{
double x = 0, y = 0, z = 0;

double maxY = Math.Max(endPoint.Y, startPoint.Y);
double minY = Math.Min(endPoint.Y, startPoint.Y);
double balanceY = maxY - minY;
if (balanceY != 0)
{
x = endPoint.X + dimPosition;
y = minY + balanceY / 2d;
return new Point3d(x, y, z);
}

double maxX = Math.Max(endPoint.X, startPoint.X);
double minX = Math.Min(endPoint.X, startPoint.X);
double balanceX = maxX - minX;
if (balanceX != 0)
{
y = endPoint.Y + dimPosition;
x = minX + balanceX / 2d;
return new Point3d(x, y, z);
}

return new Point3d(x, y, z);
}
}

连线操作也是在当前工作文档空间中完成,普通的轮廓线需要创建一根线条,确定线条两点的具体坐标,并指定绘制的图层,接着将线条追加到当前文档空间中。标注线条额外需要确定标注文本内容的所在位置,这里通过GetDimLinePoint()方法传递连线两点的坐标计算出一个平均值赋予标注文本内容的坐标


C#基于Autocad的二次开发(3.连线绘制)
https://wangyuangen.github.io/2024/05/20/CsharpAutocad3/
作者
Yuangen Wang
发布于
2024年5月20日
许可协议