We need to iterate through the layer table of the current database from time to time. Here is some succinct and good code in C# to do so.
[CommandMethod("LayerIterator")]
public static void LayerIterator_Method()
{
Database database = HostApplicationServices.WorkingDatabase;
using (Transaction transaction = database.TransactionManager.StartTransaction())
{
SymbolTable symTable = (SymbolTable)transaction.GetObject(database.LayerTableId, OpenMode.ForRead);
foreach (ObjectId id in symTable)
{
LayerTableRecord symbol = (LayerTableRecord)transaction.GetObject(id, OpenMode.ForRead);
//TODO: Access to the symbol
MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nName: {0}", symbol.Name));
}
transaction.Commit();
}
}
If the command is run, something like the following may be printed out to the command line window.
Command: LayerIterator
Name: 0
Name: DEFPOINTS
Name: Struc_Plan_Fdn_Dim
Name: TEXT
Name: PS_Viewport
Name: PS_Annot
Name: Structural Base1|1_Structural_Anchor_Bolt_Plan_BP
Name: Structural Base1|1_Structural_Anchor_Bolt_Plan_SteelCol
Name: Structural Base1|1_Structural_Anchor_Bolt_Plan_AnBolt
Name: Structural Base1|1_Structural_Anchor_Bolt_Plan_GB
Name: Structural Base1|1_Structural_Anchor_Bolt_Plan_HSS
Name: Structural Base1|1_Structural_Plan_WCol
Name: Structural Base1|1_Arch_Plan_Dim
Name: Structural Base1|1_Struc_Plan_GB
Name: Structural Base1|1_Struc_Plan_Fdn_Grid
Name: Structural Base1|1_Arch_Plan_ExistBldg
Name: Structural Base1|1_Struc_Plan_Fdn_Pile
Name: Structural Base1|1_Struc_Plan_Fdn_Dim
Name: Structural Base1|1_Struc_Plan_Fdn_HAIRPIN
Name: Structural Base1|LAYER65A
Name: Structural Base1|B-015-1
Name: Structural Base1|1_Struc_Plan_Fdn_Rebar
Name: Structrual Base1
Name: Pile Detail
Name: TS-TEXT2
Name: Pile Detail|STRUC_DETAIL_DOWELS
Name: Pile Detail|STRUC_DETAIL_REINF
Name: Pile Detail|STRUC_DETAIL_TEXT
Name: Pile Detail|STRUC_DETAIL_CONC
Name: TB_TEXT1
Name: TB_TEXT3
Name: TB_BORDER
Name: TB_INFORMATION
Name: TB_TEXT4
Name: TB_CORNERS
Name: TITLE
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: |