using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(GetBlockName.MyCommands))]
namespace GetBlockName
{
public class MyCommands
{
[CommandMethod("blockName")]
static public void blockName()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptEntityOptions options =
new PromptEntityOptions("\nУкажите вставку блока для получения строки фильтра: ");
options.SetRejectMessage("\nДопустима только вставка блока ");
options.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult acSSPrompt = ed.GetEntity(options);
string filterstr = "";
using (Transaction tx = db.TransactionManager.StartTransaction()) {
BlockReference blockRef = tx.GetObject(acSSPrompt.ObjectId,
OpenMode.ForRead) as BlockReference;
ObjectId btrMainId = blockRef.DynamicBlockTableRecord;
if (btrMainId.IsNull) btrMainId = blockRef.BlockTableRecord;
BlockTableRecord btrMain = tx.GetObject(btrMainId, OpenMode.ForRead) as BlockTableRecord;
filterstr = btrMain.Name;
foreach (ObjectId idAnno in btrMain.GetAnonymousBlockIds())
{
BlockTableRecord btrAnno = tx.GetObject(idAnno, OpenMode.ForRead) as BlockTableRecord;
filterstr += ",`";
filterstr += btrAnno.Name;
}
ed.WriteMessage("\nСтрока фильтра: " + filterstr);
tx.Commit();
}
}
}
}