Perhaps have a play with this.
The PromptEntityOptions will select anything derived from Dimension.
Will be fairly simple to modify if you want to select multiple objects at one time.
[CommandMethod("EXPDim")]
public void ExplodeDimensions()
{
Document doc = CadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
RXClass rxClass = RXObject.GetClass(typeof(Dimension));
DBObjectCollection explodedObjects = new DBObjectCollection();
PromptEntityOptions peo = new PromptEntityOptions(
"\nSelect a dimension to explode : ");
peo.SetRejectMessage($"Selected object is not an {rxClass.Name}");
peo.AddAllowedClass(typeof(Dimension), exactMatch: false);
bool finished = false;
while (!finished)
{
PromptEntityResult result = ed.GetEntity(peo);
if (result.Status != PromptStatus.OK)
{
finished = true;
return;
}
using (var tr = db.TransactionManager.StartOpenCloseTransaction())
{
explodedObjects.Clear();
Entity ent = (Entity)tr.GetObject(
result.ObjectId,
OpenMode.ForRead);
ent.Explode(explodedObjects);
// optional (EraseOriginal(ed))
//
if (explodedObjects.Count > 0 )
{
ent.UpgradeOpen();
ent.Erase();
}
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(
db.CurrentSpaceId,
OpenMode.ForWrite);
foreach (DBObject obj in explodedObjects)
{
Entity explodedEntity = (Entity)obj;
if(explodedEntity is MText)
{
// just for giggles, to visually identify dimensions processed.
explodedEntity.ColorIndex = 211;
}
btr.AppendEntity(explodedEntity);
tr.AddNewlyCreatedDBObject(explodedEntity, true);
}
tr.Commit();
}
}
}
internal bool EraseOriginal(Editor ed)
{
PromptKeywordOptions pko = new PromptKeywordOptions("\nErase original objects?")
{
AllowNone = true
};
pko.Keywords.Add("Yes");
pko.Keywords.Add("No");
pko.Keywords.Default = "Yes";
PromptResult pkr = ed.GetKeywords(pko);
if (pkr.Status != PromptStatus.OK) return false;
return (pkr.StringResult == "Yes");
}
modified note 20230904:
changed variable name @Anonymous to rxClass because the post software here didn't display @Anonymous.
This routine has had minimal testing, so review the code and test.
Regards,
// Called Kerry or kdub in my other life.
Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub
NZST UTC+12 : class keyThumper<T> : Lazy<T>; another Swamper