Each layer can have its own color assigned to it. Colors for a layer are identified by the Color object which is part of the Colors namespace. This object can hold an RGB value, ACI number (an integer from 1 to 255), or a color book color.
每个图层都有自己的颜色。图层的颜色是通过 Colors 命名空间中的 Color 对象来标识的。此对象可以是 RGB 值、ACI 编号(从 1 到 255 的整数)或命名颜色。
To assign a color to a layer, use the Color property.
要指定图层颜色,请使用 Color 属性。
NoteObjects such as lines and circles support two different properties to control their current color. The Color property is used to assign an RGB value, ACI number, or a color book color, while the ColorIndex property only supports ACI numbers.
注意像直线和圆这样的对象支持两个不同的属性来控制它们的当前颜色。Color 属性用于指定一个 RGB 值,ACI 编号或命名颜色,而 ColorIndex 属性仅支持 ACI 编号。
If you use the ACI color 0 or ByBlock, AutoCAD draws new objects in the default color (white or black, depending on your configuration) until they are grouped into a block. When the block is inserted, the objects in the block inherit the current property setting.
如果使用ACI中编号为0或 ByBlock,AutoCAD 绘制的新对象将使用默认颜色(白色或黑色,具体要依赖于你的配置)直到它们被放入块中。当插入块时,块中的对象将继承当前设置中的属性。
If you use the ACI color 256 or ByLayer, new objects inherit the color of the layer upon which they are drawn.
如果使用ACI中编号为0或 ByLayer,新对象将继承它们所在层的颜色。
The following example creates three new layers and each is assigned a different color using each of the three color methods.
下面的示例创建三个新图层并使用三个不同的颜色方法给每个图层指定不同的颜色。
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Colors
<CommandMethod("SetLayerColor")> _
Public Sub SetLayerColor()
'' 获得当前文档和数据库 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()
'' 以只读方式打开图层表 Open the Layer table for read
Dim acLyrTbl As LayerTable
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
OpenMode.ForRead)
'' 定义图层名的数组 Define an array of layer names
Dim sLayerNames(2) As String
sLayerNames(0) = "ACIRed"
sLayerNames(1) = "TrueBlue"
sLayerNames(2) = "ColorBookYellow"
''定义图层的颜色的数组 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")
Dim nCnt As Integer = 0
'' 添加或更改在图形中的每个图层 Add or change each layer in the drawing
For Each sLayerName As String In sLayerNames
Dim acLyrTblRec As LayerTableRecord
If acLyrTbl.Has(sLayerName) = False Then
acLyrTblRec = New LayerTableRecord()
'' 指定图层名称 Assign the layer a name
acLyrTblRec.Name = sLayerName
'' 升级层表为可写Upgrade the Layer table for write
If acLyrTbl.IsWriteEnabled = False Then acLyrTbl.UpgradeOpen()
''追加新的图层到层表和事务中 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
acLyrTblRec = acTrans.GetObject(acLyrTbl(sLayerName), _
OpenMode.ForWrite)
End If
'' 设置图层的颜色 Set the color of the layer
acLyrTblRec.Color = acColors(nCnt)
nCnt = nCnt + 1
Next
''保存更改并销毁事务 Save the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
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(R) pastel coated");
int nCnt = 0;
// Add or change each layer in the drawing
foreach (string sLayerName in sLayerNames)
{
LayerTableRecord acLyrTblRec;
if (acLyrTbl.Has(sLayerName) == false)
{
acLyrTblRec = new LayerTableRecord();
// Assign the layer a name
acLyrTblRec.Name = sLayerName;
// Upgrade the Layer table for write
if (acLyrTbl.IsWriteEnabled == false) acLyrTbl.UpgradeOpen();
// 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
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();
}
}