.Net/C#读取CAD软件dwg数据表实体
使用ACadSharp库读取CAD软件dwg数据表实体
文末附ACadSharp.dll库文件及源码
CadDocReader
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 115 116 117 118 119 120 121 122 123 124
| using ACadSharp; using ACadSharp.Entities; using ACadSharp.IO; using CSMath; using System.Text.RegularExpressions;
namespace NCad.Toolkits { internal class CadDocReader { public CadDocReader(string filePath) { this.FilePath = filePath; }
public string FilePath { get; private set; }
public void ReadTable() { string filePath = this.FilePath; if (!File.Exists(filePath)) { throw new FileNotFoundException(filePath); } CadDocument cadDocument = LoadCadDocument(filePath); foreach (var blockRecord in cadDocument.BlockRecords) { if (!blockRecord.Name.StartsWith("*T")) { continue; } if (blockRecord.Entities.Count < 1) { continue; } for (int i = 0; i < blockRecord.Entities.Count; ++i) { Entity entity = blockRecord.Entities[i]; if (entity.ObjectName.EndsWith("TEXT")) { MText mText = entity as MText; string entityText; XYZ insertPoint; if (mText == null) { TextEntity textEntity = entity as TextEntity; entityText = textEntity.Value; insertPoint = textEntity.InsertPoint; } else { entityText = mText.Value; insertPoint = mText.InsertPoint; } if (!string.IsNullOrEmpty(entityText)) { string pattern = @"\\f.*?;"; entityText = Regex.Replace(entityText, pattern, string.Empty).TrimStart('{').TrimEnd('}'); } Console.Write(entityText); Console.Write("\t"); } } Console.WriteLine(); Console.WriteLine(); } }
private CadDocument LoadCadDocument(string filePath) { string extension = Path.GetExtension(filePath); CadDocument cadDocument = null; if (extension.Equals(".dwg", StringComparison.OrdinalIgnoreCase)) { DwgReader dwgReader = null; try { dwgReader = new DwgReader(filePath); cadDocument = dwgReader.Read(); } catch { throw; } finally { dwgReader?.Dispose(); } } else if (extension.Equals(".dxf", StringComparison.OrdinalIgnoreCase)) { DxfReader dxfReader = null; try { dxfReader = new DxfReader(filePath); cadDocument = dxfReader.Read(); } catch { throw; } finally { dxfReader?.Dispose(); } } else { throw new NotSupportedException($"文件:{filePath},不支持类型:{extension}"); } return cadDocument; } } }
|
调用代码
1 2 3
| string dwgFile = "D:\\admin\\Desktop\\测试图\\01-平面图.dwg"; CadDocReader cadDocReader = new CadDocReader(dwgFile); cadDocReader.ReadTable();
|
测试输出
1
| 用地信息列表 类型 权属单位 地块面积(㎡) 拆迁/临迁房屋面积(㎡) 征地 AAAAAAA 5674.3934 0.0000 征地 BBBBBBB 5297.4953 0.0000 借地 CCCCCCCC 92314.0540 0.0000 拆迁 DDDDDDD 2497.0416 23213213.6568 临迁 EEEEEEE 1456.0170 343423434.5650 临迁 FFFFFFF 494.0636 453535.7878 临迁 GGGGGGG 494.0468 567676.6760 临迁 HHHHHHH 558.6505 645546.8780 临迁 IIIIIII 682.7144 3432424.8787 临迁 JJJJJJJ 682.7144 5676767.4342 临迁 KKKKKKK 682.7144 2324324.67868
|
用地信息列表
类型 | 权属单位 | 地块面积(㎡) | 拆迁/临迁房屋面积(㎡) |
征地 | AAAAAAA | 5674.3934 | 0.0000 |
征地 | BBBBBBB | 5297.4953 | 0.0000 |
借地 | CCCCCCCC | 92314.0540 | 0.0000 |
拆迁 | DDDDDDD | 2497.0416 | 23213213.6568 |
临迁 | EEEEEEE | 1456.0170 | 343423434.5650 |
临迁 | FFFFFFF | 494.0636 | 453535.7878 |
临迁 | GGGGGGG | 494.0468 | 567676.6760 |
临迁 | HHHHHHH | 558.6505 | 645546.8780 |
临迁 | IIIIIII | 682.7144 | 3432424.8787 |
临迁 | JJJJJJJ | 682.7144 | 5676767.4342 |
临迁 | KKKKKKK | 682.7144 | 2324324.67868 |
ACadSharp.dll库文件及源码
https://shaoshao.lanzouw.com/b0ny05i9a
密码:4ih4