Here’s a simple C# code snippet showing how to access the RGB color and color index values for a layer. The code iterates all LayerTableRecords in the drawing and prints the values we’re querying to the command line.
using System; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Colors; namespace LayerColor { public class GetRGBValuesClass { public GetRGBValuesClass() { } [CommandMethod("GetRGB")] public void GetRGB() { Database dBase = Application.DocumentManager.MdiActiveDocument.Database; Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = dBase.TransactionManager; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; using (Transaction myT = tm.StartTransaction()) { try { LayerTable lt = (LayerTable)tm.GetObject(dBase.LayerTableId, OpenMode.ForRead); //Iterate all records in table foreach (ObjectId ltrId in lt) { LayerTableRecord ltr = (LayerTableRecord)tm.GetObject(ltrId, OpenMode.ForRead); Color colour = ltr.Color; ed.WriteMessage("nThe name of the layer is: " +  
; ltr.Name.ToString()); ed.WriteMessage("nRed: " + colour.ColorValue.R.ToString()); ed.WriteMessage("nGreen: " + colour.ColorValue.G.ToString()); ed.WriteMessage("nBlue: " + colour.ColorValue.B.ToString()); ed.WriteMessage("nColor Index value of the layer is:" + colour.ColorIndex.ToString() + "n"); } myT.Commit(); } catch (Autodesk.AutoCAD.Runtime.Exception e1) { //Handle errors } } } } }

Leave a Reply