C#基于Autocad的二次开发(5.图形镜像)

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

部分场景需要将图形中的内容根据x轴或y轴,甚至自定义的对称轴进行镜像,这时需要依照目标图形以及对称轴生成镜像图形。

ACD.CadCore项目中创建Extensions.Autodesk.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
public static partial class Extensions
{
/// <summary>
/// 镜像图形
/// </summary>
/// <param name="ent">图形对象的ObjectId</param>
/// <param name="point1">第一个镜像点</param>
/// <param name="point2">第二个镜像点</param>
/// <param name="isEraseSource">是否删除原图形</param>
/// <returns>返回新的图形对象 没有加入图形数据库的情况</returns>
public static Entity MirrorEntity(this Entity ent, Point3d point1, Point3d point2, bool isEraseSource)
{

// 声明一个图形对象用于返回
Entity entR;

if (ent.IsNewObject == true)
{
// 计算镜像的变换矩阵
Matrix3d mt = Matrix3d.Mirroring(new Line3d(point1, point2));
entR = ent.GetTransformedCopy(mt);
}
else
{
entR = ent.ObjectId.MirrorEntity(point1, point2, isEraseSource);
}
return entR;
}

public static Entity MirrorEntity(this ObjectId entId, Point3d point1, Point3d point2, bool isEraseSource)
{
// 声明一个图形对象用于返回
Entity entR;
// 计算镜像的变换矩阵
Matrix3d mt = Matrix3d.Mirroring(new Line3d(point1, point2));
// 打开事务处理
using (Transaction trans = entId.Database.TransactionManager.StartTransaction())
{
// 判断是否删除原对象
if (isEraseSource)
{
// 打开原对象
Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForWrite);
// 执行变换
ent.TransformBy(mt);
entR = ent;
}
else
{

// 打开原对象
Entity ent = (Entity)trans.GetObject(entId, OpenMode.ForRead);
entR = ent.GetTransformedCopy(mt);
}
return entR;
}
}
}

CSHARP

接着在ACD.CadCore项目中创建ACDOperation.Reverse.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
public partial class ACDOperation
{
public bool Reverse(string originalFullPath, string reverseFullPath, Point3d point1, Point3d point2)
{
if (!File.Exists(originalFullPath)) return false;

if (File.Exists(reverseFullPath)) return true;

try
{
string directoryPath = Path.GetDirectoryName(reverseFullPath);
if (!Directory.Exists(directoryPath)) Directory.CreateDirectory(directoryPath);

File.Copy(originalFullPath, reverseFullPath);
}
catch (IOException)
{
return false;
}

using (Database wblockDb = new Database(false, true))
{
try
{
wblockDb.ReadDwgFile(reverseFullPath, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
wblockDb.CloseInput(true);

using (Transaction tr = wblockDb.TransactionManager.StartTransaction())
{
BlockTable currblockTable = tr.GetObject(wblockDb.BlockTableId, OpenMode.ForRead) as BlockTable;

BlockTableRecord currblockTableRecord = tr.GetObject(currblockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

foreach (var item in currblockTableRecord)
{
Entity entity = tr.GetObject(item, OpenMode.ForWrite) as Entity;

if (entity is Dimension)
{
var dimension = entity as Dimension;
var normal = dimension.Normal;
dimension.MirrorEntity(point1, point2, true);
dimension.Normal = normal;
}
else
{
entity.MirrorEntity(point1, point2, true);
}
}
tr.Commit();
}
wblockDb.SaveAs(reverseFullPath, DwgVersion.Current);
}
catch (Exception)
{
return false;
}
}
return true;
}
}

CSHARP

通过originalFullPath文件路径,读取目标图形,将目标图形中的所有对象依次执行镜像操作,最后提交事物,保存新的图形文件到reverseFullPath。
镜像后,其中Dimension标注对象会存在标注内容颠倒的问题,需要特殊处理


C#基于Autocad的二次开发(5.图形镜像)
https://wangyuangen.github.io/2024/05/20/CsharpAutocad5/
作者
Yuangen Wang
发布于
2024年5月20日
许可协议