画層に色を割り当てる(.NET)

各画層には独自の色を割り当てることができます。画層の色は Colors 名前空間の一部である Color オブジェクトで指定します。このオブジェクトは、RGB 値、ACI 番号(1 から 255 の整数)、またはカラー ブックの色を保持することができます。

色を画層に割り当てるには、Color プロパティを使用します。

注: 線分や円などのオブジェクトは、現在の色をコントロールできる 2 種類のプロパティをサポートしています。Color プロパティは、RGB 値、ACI 番号、またはカラー ブックの色を割り当てるために使用されますが、ColorIndex プロパティは ACI 番号のみをサポートします。

ACI の色 0 または ByBlock を使用すると、AutoCAD は新しいオブジェクトを作成する際、ブロックにグループ化されるまで既定の色(設定に応じて白また黒)を使用します。ブロックを挿入すると、ブロック内のオブジェクトは現在のプロパティ設定を継承します。

ACI の色 256 または ByLayer を使用する場合、新しいオブジェクトは作成される下の画層の色を継承します。

画層の色を設定する

次の例では、3 つの新しい画層を作成し、3 つの色のメソッドを使用してそれぞれに異なる色を割り当てます。

C# の例

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
 
[CommandMethod("SetLayerColor")]
public static void SetLayerColor()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the Layer table for read
        LayerTable acLyrTbl;
        acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
                                     OpenMode.ForRead) as LayerTable;

        // Define an array of layer names
        string[] sLayerNames = new string[3];
        sLayerNames[0] = "ACIRed";
        sLayerNames[1] = "TrueBlue";
        sLayerNames[2] = "ColorBookYellow";

        // 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+ Pastels & Neons Coated");

        int nCnt = 0;

        // Add or change each layer in the drawing
        foreach (string sLayerName in sLayerNames)
        {
            if (acLyrTbl.Has(sLayerName) == false)
            {
                using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
                {
                    // Assign the layer a name
                    acLyrTblRec.Name = sLayerName;

                    // Set the color of the layer
                    acLyrTblRec.Color = acColors[nCnt];

                    // Upgrade the Layer table for write
                    if (acLyrTbl.IsWriteEnabled == false) acTrans.GetObject(acCurDb.LayerTableId, 
                                                                            OpenMode.ForWrite);

                    // Append the new layer to the Layer table and the transaction
                    acLyrTbl.Add(acLyrTblRec);
                    acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
                }
            }
            else
            {
                // Open the layer if it already exists for write
                LayerTableRecord acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
                                                                 OpenMode.ForWrite) as LayerTableRecord;

                // Set the color of the layer
                acLyrTblRec.Color = acColors[nCnt];
            }

            nCnt = nCnt + 1;
        }

        // Save the changes and dispose of the transaction
        acTrans.Commit();
    }
}