开始之前确保已完成步骤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 { 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标注对象会存在标注内容颠倒的问题,需要特殊处理