03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-7)

 

1.7、Assign Color to a Layer指定图层颜色

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对象来明确。Color对象使用包括RGB值、ACI索引号(1-255的整数)、以及配色系统颜色在内的颜色方案。

To assign a color to a layer, use the Color property.

为图层设置颜色,用Color属性。

Note Objects 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颜色256或ByLayer,则新对象继承所在图层的颜色。

Set the color of a layer 设置图层颜色

The following example creates three new layers and each is assigned a different color using each of the three color methods.

下列实例创建三个新图层并使用三个颜色方法为每个图层赋予不同的颜色。

 

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(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

 

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(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();

  }

}

 

VBA/ActiveX Code Reference

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(R) pastel 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值