#if zcad
using ZwSoft.ZwCAD.Runtime;
using ZwSoft.ZwCAD.ApplicationServices;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.EditorInput;
using App = ZwSoft.ZwCAD.ApplicationServices.Application;
#else
using App= Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
#endif
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace Testdeleteblock
{
public class Class1
{
public static Document doc = App.DocumentManager.MdiActiveDocument;
public static Database db = doc.Database;
public static Editor ed = doc.Editor;
[CommandMethod("TestEraseBlk")]
public void TestEraseBlk()
{
var blkName = "Terminal1";
try
{
var blkId = GetBlkId(db, blkName);
if (blkId.IsNull)
throw new System.Exception(string.Format("\n Block not found: {0}", blkName));
if (!EraseBlkRefs(blkId))
throw new System.Exception(string.Format("\n Failed to Erase BlockReferences for: {0}", blkName));
if (!EraseBlk(blkId))
throw new System.Exception(string.Format("\n Failed to Erase Block: {0}", blkName));
ed.WriteMessage("\n Block Erased: {0}", blkName);
}
catch (System.Exception ex)
{
ed.WriteMessage(ex.Message);
}
}
[CommandMethod("EraseBlk")]
public void GetTextCST()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "dwg files (*.dwg)|*.dwg|All files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
openFileDialog.Multiselect = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
PromptStringOptions pStrOpts = new PromptStringOptions("\nEnter your name: ");
pStrOpts.AllowSpaces = true;
PromptResult pStrRes = ed.GetString(pStrOpts);
if (pStrRes.Status != PromptStatus.OK) return;
App.ShowAlertDialog("The name entered was: " + pStrRes.StringResult);
string Blockname = pStrRes.StringResult;
foreach (string text in openFileDialog.FileNames)
{
try
{
EraseBlk(text, Blockname);
}
catch { }
}
}
}
public void EraseBlk(string Filename, string Blockname)
{
using (Database database = new Database(false, true))
{
var blkName = Blockname;
#if zcad
database.ReadDwgFile(Filename, FileShare.ReadWrite, false, "");
#else
database.ReadDwgFile(Filename, FileOpenMode.OpenForReadAndReadShare, false, "");
#endif
database.CloseInput(true);
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
ObjectId blkId = ObjectId.Null;
BlockTable bt = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
if (bt.Has(blkName))
blkId = bt[blkName];
if (blkId.IsNull)
throw new System.Exception(string.Format("\n Block not found: {0}", blkName));
if (!EraseBlkRefs(blkId))
throw new System.Exception(string.Format("\n Failed to Erase BlockReferences for: {0}", blkName));
if (!EraseBlk(blkId))
throw new System.Exception(string.Format("\n Failed to Erase Block: {0}", blkName));
transaction.Commit();
database.SaveAs(Filename, DwgVersion.Current);
}
}
}
public static ObjectId GetBlkId(Database db, string blkName)
{
ObjectId blkId = ObjectId.Null;
if (db == null)
return ObjectId.Null;
if (string.IsNullOrWhiteSpace(blkName))
return ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (bt.Has(blkName))
blkId = bt[blkName];
tr.Commit();
}
return blkId;
}
public static bool EraseBlkRefs(ObjectId blkId)
{
bool blkRefsErased = false;
if (blkId.IsNull) return false;
Database db = blkId.Database;
if (db == null) return false;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord blk = (BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);
var blkRefs = blk.GetBlockReferenceIds(true, true);
if (blkRefs != null && blkRefs.Count > 0)
{
foreach (ObjectId blkRefId in blkRefs)
{
BlockReference blkRef = (BlockReference)tr.GetObject(blkRefId, OpenMode.ForWrite);
blkRef.Erase();
}
blkRefsErased = true;
}
tr.Commit();
}
return blkRefsErased;
}
public static bool EraseBlk(ObjectId blkId)
{
bool blkIsErased = false;
if (blkId.IsNull) return false;
Database db = blkId.Database;
if (db == null) return false;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord blk = (BlockTableRecord)tr.GetObject(blkId, OpenMode.ForRead);
var blkRefs = blk.GetBlockReferenceIds(true, true);
if (blkRefs == null || blkRefs.Count == 0)
{
blk.UpgradeOpen();
blk.Erase();
blkIsErased = true;
}
tr.Commit();
}
return blkIsErased;
}
}
}
优化版:
public void EraseBlk(string Filename, string Blockname)
{
using (Database database = new Database(false, true))
{
var blkName = Blockname;
database.ReadDwgFile(Filename, FileOpenMode.OpenForReadAndReadShare, false, "");
database.CloseInput(true);
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
ObjectId blkId = ObjectId.Null;
BlockTable bt = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
if (bt.Has(blkName))
blkId = bt[blkName];
if (blkId.IsNull)
throw new System.Exception(string.Format("\n Block not found: {0}", blkName));
//删除块参照
BlockTableRecord blk = (BlockTableRecord)transaction.GetObject(blkId, OpenMode.ForRead);
var blkRefs = blk.GetBlockReferenceIds(true, true);
if (blkRefs != null && blkRefs.Count > 0)
{
foreach (ObjectId blkRefId in blkRefs)
{
BlockReference blkRef = (BlockReference)transaction.GetObject(blkRefId, OpenMode.ForWrite);
blkRef.Erase();
}
}
//删除块表
if (blkRefs == null || blkRefs.Count == 0)
{
blk.UpgradeOpen();
blk.Erase();
}
transaction.Commit();
database.SaveAs(Filename, DwgVersion.Current);
}
}
}