The “Burst” command from the express tools is quite useful when exploding block references with attributes. Unlike the usual explode command, it leaves the attributes unchanged when a block reference is exploded.
Here is a sample code to mimic the Burst command using the AutoCAD .Net API. It first explodes a block reference and replaces any attribute definitions in the exploded entity collection by a DBText.
[CommandMethod("EB", CommandFlags.UsePickSet)]
public void ExplodeBock()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
PromptSelectionResult psr = ed.SelectImplied();
if (psr.Status != PromptStatus.OK)
{
ed.WriteMessage(@"Please select the block references " +
@"to explode and then run the command");
return;
}
using (SelectionSet ss = psr.Value)
{
if (ss.Count <= 0)
{
ed.WriteMessage(@"Please select the block references " +
@"to explode and then run the command");
return;
}
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
ObjectId msId = SymbolUtilityServices.GetBlockModelSpaceId(db);
BlockTableRecord ms = tr.GetObject(msId, OpenMode.ForWrite)
as BlockTableRecord;
foreach (SelectedObject selectedEnt in ss)
{
BlockReference blockRef = tr.GetObject(
selectedEnt.ObjectId, OpenMode.ForRead) as BlockReference;
if (blockRef != null)
{
DBObjectCollection toAddColl = new DBObjectCollection();
BlockTableRecord blockDef = tr.GetObject(
blockRef.BlockTableRecord, OpenMode.ForRead)
as BlockTableRecord;
// Create a text for const and visible attributes
foreach (ObjectId entId in blockDef)
{
if (entId.ObjectClass.Name == "AcDbAttributeDefinition")
{
AttributeDefinition attDef = tr.GetObject(
entId, OpenMode.ForRead) as AttributeDefinition;
if (attDef.Constant && !attDef.Invisible)
{
DBText text = new DBText
{
Height = attDef.Height,
TextString = attDef.TextString,
Position = attDef.Position.TransformBy(
blockRef.BlockTransform)
};
toAddColl.Add(text);
}
}
}
// Create a text for non-const and visible attributes
foreach (ObjectId attRefId in blockRef.AttributeCollection)
{
AttributeReference attRef = tr.GetObject(
attRefId, OpenMode.ForRead) as AttributeReference;
if (attRef.Invisible == false)
{
DBText text = new DBText
{
Height = attRef.Height,
TextString = attRef.TextString,
Position = attRef.Position
};
toAddColl.Add(text);
}
}
// Get the entities from the block reference
// Attribute definitions have been taken care of..
// So ignore them
DBObjectCollection entityColl = new DBObjectCollection();
blockRef.Explode(entityColl);
foreach (Entity ent in entityColl)
{
if (!(ent is AttributeDefinition))
{
toAddColl.Add(ent);
}
}
// Add the entities to modelspace
foreach (Entity ent in toAddColl)
{
ms.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
}
// Erase the block reference
blockRef.UpgradeOpen();
blockRef.Erase();
}
// Commit the transaction for the current selected object
tr.Commit();
}
}
}
}

Leave a Reply