The following example creates four circles and assigns a different color to each circle using four different methods.
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);
using (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();
}
}