Assign a color value to an object
 
 
 

The following example creates 4 circles and assigns a different color to each circle using four different methods.

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Colors
 
<CommandMethod("SetObjectColor")> _
Public Sub SetObjectColor()
  '' Get the current document and database
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  '' Start a transaction
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
 
      '' Define an array of colors for the layers
      Dim acColors(2) As Color
      acColors(0) = Color.FromColorIndex(ColorMethod.ByAci, 1)
      acColors(1) = Color.FromRgb(23, 54, 232)
      acColors(2) = Color.FromNames("PANTONE Yellow 0131 C", _
                                    "PANTONE(R) pastel coated")
 
      '' Open the Block table for read
      Dim acBlkTbl As BlockTable
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
                                   OpenMode.ForRead)
 
      '' Open the Block table record Model space for write
      Dim acBlkTblRec As BlockTableRecord
      acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                      OpenMode.ForWrite)
 
      '' Create a circle object and assign it the ACI value of 4
      Dim acPt As Point3d = New Point3d(0, 3, 0)
      Dim acCirc As Circle = New Circle()
      acCirc.Center = acPt
      acCirc.Radius = 1
      acCirc.ColorIndex = 4
 
      acBlkTblRec.AppendEntity(acCirc)
      acTrans.AddNewlyCreatedDBObject(acCirc, True)
 
      Dim nCnt As Integer = 0
 
      While (nCnt < 3)
          '' Create a copy of the circle
          Dim acCircCopy As Circle
          acCircCopy = acCirc.Clone()
 
          '' Shift the copy along the Y-axis
          acPt = New Point3d(acPt.X, acPt.Y + 3, acPt.Z)
          acCircCopy.Center = acPt
 
          '' Assign the new color to the circle
          acCircCopy.Color = acColors(nCnt)
 
          acBlkTblRec.AppendEntity(acCircCopy)
          acTrans.AddNewlyCreatedDBObject(acCircCopy, True)
 
          nCnt = nCnt + 1
      End While
 
      '' Save the changes and dispose of the transaction
      acTrans.Commit()
  End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
 
[CommandMethod("SetObjectColor")]
public static void SetObjectColor()
{
  // Get the current document and database
  Document acDoc = Application.DocumentManager.MdiActiveDocument;
  Database acCurDb = acDoc.Database;
 
  // Start a transaction
  using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  {
      // Define an array of colors for the layers
      Color[] acColors = new Color[3];
      acColors[0] = Color.FromColorIndex(ColorMethod.ByAci, 1);
      acColors[1] = Color.FromRgb(23, 54, 232);
      acColors[2] = Color.FromNames("PANTONE Yellow 0131 C",
                                    "PANTONE(R) pastel coated");
 
      // Open the Block table for read
      BlockTable acBlkTbl;
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
                                   OpenMode.ForRead) as BlockTable;
 
      // Open the Block table record Model space for write
      BlockTableRecord acBlkTblRec;
      acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
                                      OpenMode.ForWrite) as BlockTableRecord;
 
      // Create a circle object and assign it the ACI value of 4
      Point3d acPt = new Point3d(0, 3, 0); 
      Circle acCirc = new Circle();
      acCirc.Center = acPt;
      acCirc.Radius = 1;
      acCirc.ColorIndex = 4;
 
      acBlkTblRec.AppendEntity(acCirc);
      acTrans.AddNewlyCreatedDBObject(acCirc, true);
 
      int nCnt = 0;
 
      while (nCnt < 3)
      {
          // Create a copy of the circle
          Circle acCircCopy;
          acCircCopy = acCirc.Clone() as Circle;
 
          // Shift the copy along the Y-axis
          acPt = new Point3d(acPt.X, acPt.Y + 3, acPt.Z);
          acCircCopy.Center = acPt;
 
          // Assign the new color to the circle
          acCircCopy.Color = acColors[nCnt];
 
          acBlkTblRec.AppendEntity(acCircCopy);
          acTrans.AddNewlyCreatedDBObject(acCircCopy, true);
 
          nCnt = nCnt + 1;
      }
 
      // Save the changes and dispose of the transaction
      acTrans.Commit();
  }
}
VBA/ActiveX Code Reference