We need to iterate through the regular blocks (not model space or paper space, not dynamic blocks, not anonymous blocks, not external references, not overlay references, and not dependent blocks) of the current database from time to time. Here is some succinct and good code in C# to do so.
[CommandMethod("BlockIterator")]
public static void BlockIterator_Method()
{
Database database = HostApplicationServices.WorkingDatabase;
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
BlockTable blkTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
foreach (ObjectId id in blkTable)
{
BlockTableRecord btRecord = (BlockTableRecord)transaction.GetObject(id, OpenMode.ForRead);
if (!btRecord.IsLayout && !btRecord.IsDependent
&& !btRecord.IsDynamicBlock && !btRecord.IsAnonymous
&& !btRecord.IsFromExternalReference && !btRecord.IsFromOverlayReference)
{
//TODO: Access to the block
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nBlock name: {0}", btRecord.Name));
}
}
transaction.Commit();
}
}
If the command is run, something like the following may be printed out to the command line window.
Command: BlockIterator
Block name: Rev
Block name: title-NO FIELD CODES
The leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard) provides various wizards, coders, and widgets to help program AutoCAD addins in C#, VB.NET or even C++/CLI.
Posted by: |