public static class BlockExtensions
{
[CommandMethod("9900")]
public static void MyMethod()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
var entities = SelectObjectsOnScreen();
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockReference bref = (BlockReference)tr.GetObject(entities[0].ObjectId, OpenMode.ForRead);
var yyy = BlockRefPropertyExtractor(bref);
tr.Commit();
}
}
public static List<Entity> SelectObjectsOnScreen()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
List<Entity> listOfEntities = new List<Entity>();
using (tr)
{
// Request for objects to be selected in the drawing area
PromptSelectionResult PrSeRes = doc.Editor.GetSelection();
// If the prompt status is OK, objects were selected
if (PrSeRes.Status == PromptStatus.OK)
{
SelectionSet SelSet = PrSeRes.Value;
// Step through the objects in the selection set
foreach (SelectedObject SelObj in SelSet)
{
// Check to make sure a valid SelectedObject object was returned
if (SelObj != null)
{
// Open the selected object for write
Entity entity = (Entity)tr.GetObject(SelObj.ObjectId, OpenMode.ForWrite);
if (entity != null)
{
listOfEntities.Add(entity);
}
}
}
}
tr.Commit();
}
return listOfEntities;
}
public static (ObjectId, Point3d, string, string, Dictionary<string, string>) BlockRefPropertyExtractor(BlockReference bref)
{
//--------------------------------------------------------------------Mandatory variables
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
//--------------------------------------------------------------------Mandatory variables
ObjectId objId = default;
Point3d basePoint = default;
string blockName = default;
string layer = default;
Dictionary<string, string> attributes = default;
BlockTableRecord block = null;
using (tr)
{
objId = bref.BlockTableRecord;
block = tr.GetObject(objId, OpenMode.ForRead) as BlockTableRecord;
basePoint = bref.Position;
blockName = bref.Name;
layer = bref.Layer;
if (bref.AttributeCollection.Count > 0)
{
foreach (ObjectId att in bref.AttributeCollection)
{
AttributeDefinition attribute = tr.GetObject(att, OpenMode.ForRead) as AttributeDefinition;
attributes.Add(attribute.Tag, attribute.TextString);
}
}
tr.Commit();
}
return (objId, basePoint, blockName, layer, attributes);
}
}