The AutoCAD .NET class BlockReference has a method, ExplodeToOwnerSpace(), which can explode itself and add resultant entities into its owner space, either Model or Space.
It has some severe limitations, however. The first is that the BlockReference.ExplodeToOwnerSpace method only takes care of the first level blocks. Another severe limitation is that the BlockReference.ExplodeToOwnerSpace method does not return the resultant entities back so as for us to take any further operations onto them.
The previous article addressed both limitations of the BlockReference.ExplodeToOwnerSpace and created two more versions of our own from scratch, which are concise, reliable, more powerful and flexible at the same time.
As mentioned there, the same ExplodeToOwnerSpace2 and ExplodeToOwnerSpace3 can apply to more running contexts such as side databases. In this post, let’s prove it.
public static void ExplodeDrawing_Method()
Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
OpenFileDialog fileDia = new OpenFileDialog("Pick DWG", @"c:\temp\blocks.dwg", "dwg", "Pick a drawing to explode", OpenFileDialog.OpenFileDialogFlags.NoFtpSites | OpenFileDialog.OpenFileDialogFlags.NoShellExtensions | OpenFileDialog.OpenFileDialogFlags.NoUrls);
if (fileDia.ShowDialog() == System.Windows.Forms.DialogResult.OK)
Database db = new Database(false, true);
db.ReadDwgFile(fileDia.Filename, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
using (Transaction dbTr = db.TransactionManager.StartTransaction())
BlockTableRecord btr = (BlockTableRecord)dbTr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
foreach (ObjectId id in btr)
if (id.ObjectClass.DxfName == "INSERT")
db.SaveAs(db.Filename + "_exploded.dwg", DwgVersion.Current);
catch (System.Exception ex)
The code explodes all blocks regardless of nested levels in an external drawing, as specified.
We created a test drawing. Before the code was run, it contained a few blocks, many of which contained nested blocks; each nested block might contain further nested blocks, repeatedly.
After the drawing was exploded by the command, it did not contain any block references (INSERTs) anymore, as indicated by the QSELECT command. As can also be seen, thousands of entities were resulted by the recursive explosing from the around dozen block references.
Please try it and see. Enjoy!
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various project wizards, item wizards, coders and widgets to help program AutoCAD .NET addins.