CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

为图层分配颜色 (.NET)

2023-1-1 13:23| 发布者: admin| 查看: 904| 评论: 0|来自: AutoCAD

每个图层都可以分配有其自己的颜色。图层的颜色由 Color 对象标识,该对象是 Colors 命名空间的一部分。此对象可以保存 RGB 值、ACI 编号(从 1 到 255 的整数)或配色系统颜色。

要为图层指定颜色,请使用属性。Color

注意:线条和圆圈等对象支持两种不同的属性来控制其当前颜色。该属性用于分配 RGB 值、ACI 编号或配色册颜色,而该属性仅支持 ACI 编号。ColorColorIndex

如果使用 ACI 颜色 0 或 ByBlock ,AutoCAD 将以默认颜色(白色或黑色,具体取决于您的配置)绘制新对象,直到将它们分组到一个块中。插入块时,块中的对象将继承当前属性设置。

如果使用 ACI 颜色 256 或 ByLayer,则新对象将继承绘制它们的图层的颜色。

设置图层的颜色

下面的示例创建三个新图层,并使用三种颜色方法中的每一种为每个图层分配不同的颜色。

VB.NET

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

        Dim nCnt As Integer = 0

        '' Add or change each layer in the drawing
        For Each sLayerName As String In sLayerNames

            If acLyrTbl.Has(sLayerName) = False Then
                Using acLyrTblRec As LayerTableRecord = 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 Then acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite)

                    '' Append the new layer to the Layer table and the transaction
                    acLyrTbl.Add(acLyrTblRec)
                    acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
                End Using
            Else
                '' Open the layer if it already exists for write
                Dim acLyrTblRec As LayerTableRecord = acTrans.GetObject(acLyrTbl(sLayerName), _
                                                                        OpenMode.ForWrite)

                '' Set the color of the layer
                acLyrTblRec.Color = acColors(nCnt)
            End If

            nCnt = nCnt + 1
        Next

        '' 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.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();
    }
}

VBA/ActiveX 代码参考

Sub SetLayerColor()
    Dim layerObj As AcadLayer
 
    ' Define an array of layer names
    Dim sLayerNames(2) As String
    sLayerNames(0) = "ACIRed"
    sLayerNames(1) = "TrueBlue"
    sLayerNames(2) = "ColorBookYelow"
 
    ' Define an array of colors
    Dim colorObj(2) As New AcadAcCmColor
 
    colorObj(0).ColorMethod = acColorMethodByACI
    colorObj(0).ColorIndex = acRed
    Call colorObj(1).SetRGB(23, 54, 232)
    Call colorObj(2).SetColorBookColor("PANTONE+ Pastels & Neons Coated", _
                                       "PANTONE Yellow 0131 C")
 
    Dim nCnt As Integer
 
    ' Step through each layer name and create a new layer
    For Each sLayerName In sLayerNames
       Set layerObj = ThisDrawing.Layers.Add(sLayerName)
       layerObj.TrueColor = colorObj(nCnt)
 
       nCnt = nCnt + 1
    Next
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 14:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部