[CommandMethod("RemDin")]
public void RemoveDinBLock()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
List<string> BlockNameList = new List<string>(); ///Собрать список блоков
System.Collections.IDictionaryEnumerator oEnu = db.SummaryInfo.CustomProperties;
while (oEnu.MoveNext())
{
ed.WriteMessage("Ключ : \"{0}\", Наименование блока \"{1}\n", oEnu.Key.ToString(),oEnu.Value.ToString());
BlockNameList.Add(oEnu.Value.ToString());
}
ObjectId xrefId = ObjectId.Null;
using (Transaction tr = doc.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
foreach (ObjectId btrId in bt)
{
// получаем запись таблицы блоков и смотри анонимная ли она
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(btrId, OpenMode.ForWrite);
if (btr.IsDynamicBlock)
{
// получаем все анонимные блоки динамического блока
ObjectIdCollection anonymousIds = btr.GetAnonymousBlockIds();
ObjectIdCollection dynBlockRefs = new ObjectIdCollection();
foreach (ObjectId anonymousBtrId in anonymousIds)
{
// получаем анонимный блок
BlockTableRecord anonymousBtr =
(BlockTableRecord)tr.GetObject(anonymousBtrId,
OpenMode.ForRead);
// получаем все вставки этого блока
ObjectIdCollection blockRefIds =
anonymousBtr.GetBlockReferenceIds(true, true);
foreach (ObjectId id in blockRefIds)
{
dynBlockRefs.Add(id);
foreach (string blockname in BlockNameList)
{
if (btr.Name == blockname)
{
foreach (ObjectId dinId in dynBlockRefs)
{
BlockReference bref = (BlockReference)tr.GetObject(dinId, OpenMode.ForWrite);
bref.Erase();
bref.UpgradeOpen();
}
btr.UpdateAnonymousBlocks();
btr.UpgradeOpen();
btr.Erase();
}
}
}
// ed.WriteMessage(String.Format("Динамическому блоку \"{0}\" соответствуют \"{1} \" анонимных блоков и \"{2}\" вставок блока\n",
//btr.Name, anonymousIds.Count, dynBlockRefs.Count));
}
}
// удаляю главный блок штампа (не дин-ий)
else if (btr.Name == "SHTAMP")
{
ObjectIdCollection ShtIdS = btr.GetBlockReferenceIds(true, true);
foreach (ObjectId ShtId in ShtIdS)
{
BlockReference ShtBref = (BlockReference)tr.GetObject(ShtId, OpenMode.ForWrite);
ShtBref.UpgradeOpen();
ShtBref.Erase();
}
btr.UpgradeOpen();
btr.Erase();
}
// Detach logotip
else if (btr.IsFromExternalReference || btr.IsFromOverlayReference)
{
ObjectIdCollection XrefIds = btr.GetBlockReferenceIds(true, true);
foreach(ObjectId xId in XrefIds)
{
BlockReference xbref =(BlockReference)tr.GetObject(xId,OpenMode.ForWrite);
if (btr.Name == "Logo")
{
xrefId = xbref.BlockTableRecord;
db.DetachXref(xrefId);
}
xbref.Erase();
xbref.UpgradeOpen();
}
ed.WriteMessage("\nExternal reference detached.");
}
}
tr.Commit();
ed.WriteMessage("\nШтамп удалён");
}
}
/// Удалить логотип
private static void DetachXref(Database db, string xrefName)
{
using (Transaction trans = db.TransactionManager.StartOpenCloseTransaction())
{
XrefGraph xrefGraph = db.GetHostDwgXrefGraph(true);
int xrefCount = xrefGraph.NumNodes;
for (int i = 0; i < xrefCount; i++)
{
XrefGraphNode xrefNode = xrefGraph.GetXrefNode(i);
if (xrefNode.Name.ToLower() == xrefName.ToLower())
{
ObjectId xrefId = xrefNode.BlockTableRecordId;
db.DetachXref(xrefId);
break;
}
}
trans.Commit();
}
}