本帖最后由 zjh2785 于 2018-11-9 03:37 编辑
外部有一个文件, 我要删除其中的某一个块(没有任何参照引用)
用我这段代码, 无法删除,也没有任何异常发生;
- public static bool DeleteBlock(string path, string fileName, string blockName)
- {
- try
- {
- using (Database db = new Database(false, true))
- {
- db.ReadDwgFile(path + "\\" + fileName, FileOpenMode.OpenForReadAndWriteNoShare, true, “”);
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
- if (!bt.Has(blockName))
- {
- return false;
- }
- BlockTableRecord btr = tr.GetObject(bt[blockName], OpenMode.ForWrite) as BlockTableRecord;
- btr.Erase();//无法删除,单步调试的时候直接走过,没有异常;用btr.Erase(true)也无效;
- tr.Commit();
- }
- db.SaveAs(fileName, false, DwgVersion.AC1024);
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- return true;
- }
|