[CommandMethod("ListLayers")]publicstaticvoidIterateLayers(){// Get the current document and database, and start a transactionDocumentdoc=Application.DocumentManager.MdiActiveDocument;using(Transactiontransaction=doc.TransactionManager.StartTransaction()){// This example returns the layer table for the current databaseLayerTablelayerTable=transaction.GetObject(doc.Database.LayerTableId,OpenMode.ForRead)asLayerTable;// Step through the Layer table and print each layer nameforeach(ObjectIdobjectIdinlayerTable){LayerTableRecordlayerTableRecord=transaction.GetObject(objectId,OpenMode.ForRead)asLayerTableRecord;doc.Editor.WriteMessage("\n"+layerTableRecord.Name);}// Dispose of the transaction}}
Check if Layer Exist
[CommandMethod("FindMyLayer")]publicstaticvoidFindMyLayer(){// Get the current document and database, and start a transactionDocumentdoc=Application.DocumentManager.MdiActiveDocument;using(Transactiontransaction=doc.TransactionManager.StartTransaction()){// Returns the layer table for the current databaseLayerTablelayerTable=transaction.GetObject(doc.Database.LayerTableId,OpenMode.ForRead)asLayerTable;// Check to see if MyLayer exists in the Layer tableif(layerTable.Has("MyLayer")!=true){doc.Editor.WriteMessage("\n'MyLayer' does not exist");}else{doc.Editor.WriteMessage("\n'MyLayer' exists");}// Dispose of the transaction}}