image.png
image.png

  1. #if zcad
  2. using ZwSoft.ZwCAD.Runtime;
  3. using ZwSoft.ZwCAD.ApplicationServices;
  4. using ZwSoft.ZwCAD.DatabaseServices;
  5. using ZwSoft.ZwCAD.EditorInput;
  6. using App = ZwSoft.ZwCAD.ApplicationServices.Application;
  7. #else
  8. using App= Autodesk.AutoCAD.ApplicationServices.Application;
  9. using Autodesk.AutoCAD.ApplicationServices;
  10. using Autodesk.AutoCAD.DatabaseServices;
  11. using Autodesk.AutoCAD.EditorInput;
  12. using Autodesk.AutoCAD.Runtime;
  13. #endif
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.IO;
  20. using System.Windows.Forms;
  21. namespace Testdeleteblock
  22. {
  23. public class Class1
  24. {
  25. public static Document doc = App.DocumentManager.MdiActiveDocument;
  26. public static Database db = doc.Database;
  27. public static Editor ed = doc.Editor;
  28. [CommandMethod("TestEraseBlk")]
  29. public void TestEraseBlk()
  30. {
  31. var blkName = "Terminal1";
  32. try
  33. {
  34. var blkId = GetBlkId(db, blkName);
  35. if (blkId.IsNull)
  36. throw new System.Exception(string.Format("\n Block not found: {0}", blkName));
  37. if (!EraseBlkRefs(blkId))
  38. throw new System.Exception(string.Format("\n Failed to Erase BlockReferences for: {0}", blkName));
  39. if (!EraseBlk(blkId))
  40. throw new System.Exception(string.Format("\n Failed to Erase Block: {0}", blkName));
  41. ed.WriteMessage("\n Block Erased: {0}", blkName);
  42. }
  43. catch (System.Exception ex)
  44. {
  45. ed.WriteMessage(ex.Message);
  46. }
  47. }
  48. [CommandMethod("EraseBlk")]
  49. public void GetTextCST()
  50. {
  51. OpenFileDialog openFileDialog = new OpenFileDialog();
  52. openFileDialog.Filter = "dwg files (*.dwg)|*.dwg|All files (*.*)|*.*";
  53. openFileDialog.FilterIndex = 1;
  54. openFileDialog.RestoreDirectory = true;
  55. openFileDialog.Multiselect = true;
  56. if (openFileDialog.ShowDialog() == DialogResult.OK)
  57. {
  58. PromptStringOptions pStrOpts = new PromptStringOptions("\nEnter your name: ");
  59. pStrOpts.AllowSpaces = true;
  60. PromptResult pStrRes = ed.GetString(pStrOpts);
  61. if (pStrRes.Status != PromptStatus.OK) return;
  62. App.ShowAlertDialog("The name entered was: " + pStrRes.StringResult);
  63. string Blockname = pStrRes.StringResult;
  64. foreach (string text in openFileDialog.FileNames)
  65. {
  66. try
  67. {
  68. EraseBlk(text, Blockname);
  69. }
  70. catch { }
  71. }
  72. }
  73. }
  74. public void EraseBlk(string Filename, string Blockname)
  75. {
  76. using (Database database = new Database(false, true))
  77. {
  78. var blkName = Blockname;
  79. #if zcad
  80. database.ReadDwgFile(Filename, FileShare.ReadWrite, false, "");
  81. #else
  82. database.ReadDwgFile(Filename, FileOpenMode.OpenForReadAndReadShare, false, "");
  83. #endif
  84. database.CloseInput(true);
  85. using (Transaction transaction = database.TransactionManager.StartTransaction())
  86. {
  87. ObjectId blkId = ObjectId.Null;
  88. BlockTable bt = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
  89. if (bt.Has(blkName))
  90. blkId = bt[blkName];
  91. if (blkId.IsNull)
  92. throw new System.Exception(string.Format("\n Block not found: {0}", blkName));
  93. if (!EraseBlkRefs(blkId))
  94. throw new System.Exception(string.Format("\n Failed to Erase BlockReferences for: {0}", blkName));
  95. if (!EraseBlk(blkId))
  96. throw new System.Exception(string.Format("\n Failed to Erase Block: {0}", blkName));
  97. transaction.Commit();
  98. database.SaveAs(Filename, DwgVersion.Current);
  99. }
  100. }
  101. }
  102. public static ObjectId GetBlkId(Database db, string blkName)
  103. {
  104. ObjectId blkId = ObjectId.Null;
  105. if (db == null)
  106. return ObjectId.Null;
  107. if (string.IsNullOrWhiteSpace(blkName))
  108. return ObjectId.Null;
  109. using (Transaction tr = db.TransactionManager.StartTransaction())
  110. {
  111. BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  112. if (bt.Has(blkName))
  113. blkId = bt[blkName];
  114. tr.Commit();
  115. }
  116. return blkId;
  117. }
  118. public static bool EraseBlkRefs(ObjectId blkId)
  119. {
  120. bool blkRefsErased = false;
  121. if (blkId.IsNull) return false;
  122. Database db = blkId.Database;
  123. if (db == null) return false;
  124. using (Transaction tr = db.TransactionManager.StartTransaction())
  125. {
  126. BlockTableRecord blk = (BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);
  127. var blkRefs = blk.GetBlockReferenceIds(true, true);
  128. if (blkRefs != null && blkRefs.Count > 0)
  129. {
  130. foreach (ObjectId blkRefId in blkRefs)
  131. {
  132. BlockReference blkRef = (BlockReference)tr.GetObject(blkRefId, OpenMode.ForWrite);
  133. blkRef.Erase();
  134. }
  135. blkRefsErased = true;
  136. }
  137. tr.Commit();
  138. }
  139. return blkRefsErased;
  140. }
  141. public static bool EraseBlk(ObjectId blkId)
  142. {
  143. bool blkIsErased = false;
  144. if (blkId.IsNull) return false;
  145. Database db = blkId.Database;
  146. if (db == null) return false;
  147. using (Transaction tr = db.TransactionManager.StartTransaction())
  148. {
  149. BlockTableRecord blk = (BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);
  150. var blkRefs = blk.GetBlockReferenceIds(true, true);
  151. if (blkRefs == null || blkRefs.Count == 0)
  152. {
  153. blk.UpgradeOpen();
  154. blk.Erase();
  155. blkIsErased = true;
  156. }
  157. tr.Commit();
  158. }
  159. return blkIsErased;
  160. }
  161. }
  162. }

优化版:

  1. public void EraseBlk(string Filename, string Blockname)
  2. {
  3. using (Database database = new Database(false, true))
  4. {
  5. var blkName = Blockname;
  6. database.ReadDwgFile(Filename, FileOpenMode.OpenForReadAndReadShare, false, "");
  7. database.CloseInput(true);
  8. using (Transaction transaction = database.TransactionManager.StartTransaction())
  9. {
  10. ObjectId blkId = ObjectId.Null;
  11. BlockTable bt = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
  12. if (bt.Has(blkName))
  13. blkId = bt[blkName];
  14. if (blkId.IsNull)
  15. throw new System.Exception(string.Format("\n Block not found: {0}", blkName));
  16. //删除块参照
  17. BlockTableRecord blk = (BlockTableRecord)transaction.GetObject(blkId, OpenMode.ForRead);
  18. var blkRefs = blk.GetBlockReferenceIds(true, true);
  19. if (blkRefs != null && blkRefs.Count > 0)
  20. {
  21. foreach (ObjectId blkRefId in blkRefs)
  22. {
  23. BlockReference blkRef = (BlockReference)transaction.GetObject(blkRefId, OpenMode.ForWrite);
  24. blkRef.Erase();
  25. }
  26. }
  27. //删除块表
  28. if (blkRefs == null || blkRefs.Count == 0)
  29. {
  30. blk.UpgradeOpen();
  31. blk.Erase();
  32. }
  33. transaction.Commit();
  34. database.SaveAs(Filename, DwgVersion.Current);
  35. }
  36. }
  37. }