We need to iterate through all the blocks of the current database with some conditions such as not wanting model space and paper space 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)
{
//Access to the block (not model/paper space)
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: *T1
Block name: Structural Base1
Block name: Pile Detail
Block name: title-NO FIELD CODES
Block name: Drawing Title
Block name: *B6
Block name: *B7
Block name: Structural Base1|Gridline
Block name: Structural Base1|Gridline-Left
Block name: Structural Base1|tailright
Block name: Structural Base1|taildown
Block name: Structural Base1|tailup
Block name: Structural Base1|tailleft
Block name: *D|REF14
Block name: Structural Base1|Section - Up
Block name: Structural Base1|Section - Right
Block name: Structural Base1|Section - Down
Block name: Structural Base1|Section – Left
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: |