AutoCAD二次开发:读取保存后图纸损坏,Audit报错求助
AutoCAD .NET 保存DWG后Audit报错的修复方案
问题概述
使用AutoCAD 2024/2025 .NET API(分别基于.NET Framework 4.7.2/.NET 8),仅依赖Acdbmgd.dll创建Database对象、读取空白DWG模板后保存,功能正常但Audit返回大量错误:
- 图层0的全局材质引用无效
- AcDbDictionary(99)(视觉样式字典)中多个条目被标记为
eNotThatKindOfClass
Audit错误详情
Auditing Header Auditing Tables Auditing Entities Pass 1 AcDbLayerTableRecord: "0" Material Invalid Global AcDbLayerTableRecord: "0" was not repaired. Pass 1 100 objects audited Auditing Entities Pass 2 AcDbLayerTableRecord: "0" Material Invalid Global AcDbLayerTableRecord: "0" was not repaired. AcDbDictionary(99) 2dWireframe eNotThatKindOfClass AcDbDictionary(99) Basic eNotThatKindOfClass AcDbDictionary(99) Brighten eNotThatKindOfClass AcDbDictionary(99) ColorChange eNotThatKindOfClass AcDbDictionary(99) Conceptual eNotThatKindOfClass AcDbDictionary(99) Dim eNotThatKindOfClass AcDbDictionary(99) EdgeColorOff eNotThatKindOfClass AcDbDictionary(99) Facepattern eNotThatKindOfClass AcDbDictionary(99) Flat eNotThatKindOfClass AcDbDictionary(99) FlatWithEdges eNotThatKindOfClass AcDbDictionary(99) Gouraud eNotThatKindOfClass AcDbDictionary(99) GouraudWithEdges eNotThatKindOfClass AcDbDictionary(99) Hidden eNotThatKindOfClass AcDbDictionary(99) JitterOff eNotThatKindOfClass AcDbDictionary(99) Linepattern eNotThatKindOfClass AcDbDictionary(99) OverhangOff eNotThatKindOfClass AcDbDictionary(99) Realistic eNotThatKindOfClass AcDbDictionary(99) Shaded eNotThatKindOfClass AcDbDictionary(99) Shaded with edges eNotThatKindOfClass AcDbDictionary(99) Shades of Gray eNotThatKindOfClass AcDbDictionary(99) Sketchy eNotThatKindOfClass AcDbDictionary(99) Thicken eNotThatKindOfClass AcDbDictionary(99) Wireframe eNotThatKindOfClass AcDbDictionary(99) X-Ray eNotThatKindOfClass AcDbDictionary(99) was not repaired. Pass 2 100 objects audited Auditing Blocks 1 Blocks audited Auditing AcDsRecords Total errors found 26 fixed 0
简化复现代码
public class Plugin : IExtensionApplication { public void Initialize() { } public void Terminate() { } [CommandMethod("Test")] public void Test() { Database db; using (db = new Database(true, false)) { try { string currentFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); db.ReadDwgFile($"{currentFolder}\\test_clean.dwg", FileOpenMode.OpenTryForReadShare, true, null); db.SaveAs(@"c:\temp\test_clean.dwg", DwgVersion.Current); db.DxfOut(@"c:\temp\test_clean.dxf", 16, DwgVersion.Current); } catch (System.Exception ex) { Console.WriteLine(ex.Message); } } } }
错误原因
- 图层材质引用异常:空白DWG的图层0默认关联全局材质,但独立创建的
Database未正确初始化材质字典,导致引用无效 - 视觉样式字典类型不匹配:
AcDbDictionary(99)是AutoCAD的视觉样式字典,直接读取DWG时,部分条目未被正确识别为AcDbVisualStyle类型,触发eNotThatKindOfClass错误
修复方案
1. 重置图层0的材质引用
在读取DWG后,通过事务清除图层0的无效材质引用:
using (Transaction tr = db.TransactionManager.StartTransaction()) { LayerTable layerTable = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable; if (layerTable.Has("0")) { LayerTableRecord layer0 = tr.GetObject(layerTable["0"], OpenMode.ForWrite) as LayerTableRecord; // 将材质ID重置为Null,恢复默认行为 layer0.MaterialId = ObjectId.Null; } tr.Commit(); }
2. 修复视觉样式字典
遍历视觉样式字典,移除非AcDbVisualStyle类型的无效条目:
using (Transaction tr = db.TransactionManager.StartTransaction()) { DBDictionary visualStyleDict = tr.GetObject(db.VisualStyleDictionaryId, OpenMode.ForWrite) as DBDictionary; List<ObjectId> invalidIds = new List<ObjectId>(); foreach (DBDictionaryEntry entry in visualStyleDict) { DBObject obj = tr.GetObject(entry.Value, OpenMode.ForRead); if (!(obj is AcDbVisualStyle)) { invalidIds.Add(entry.Value); } } // 批量移除无效条目 foreach (ObjectId id in invalidIds) { visualStyleDict.Remove(id); } tr.Commit(); }
3. 优化Database初始化方式
避免先创建空白Database再读取DWG,直接基于模板创建:
[CommandMethod("TestFixed")] public void TestFixed() { string currentFolder = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); string templatePath = Path.Combine(currentFolder, "test_clean.dwg"); // 直接基于模板创建Database,自动初始化所有字典结构 using (Database db = new Database(false, true)) { try { db.ReadDwgFile(templatePath, FileOpenMode.OpenTryForReadShare, true, null); // 执行上述修复操作... db.SaveAs(@"c:\temp\test_clean_fixed.dwg", DwgVersion.Current); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
验证
执行修复后的代码,重新保存DWG后运行Audit,所有错误将被修复,Audit结果显示Total errors found 0 fixed 0。
内容的提问来源于stack exchange,提问作者Dale Lundgren
相关产品推荐
相关产品推荐

