创建和命名图层
 
 

You can create new layers and assign color and linetype properties to those layers. Each individual layer is part of the Layers table. Use the Add function to create a new layer and add it to the Layers table.

用户可以创建新图层然后赋予这些图层颜色和线型特性。每个单独图层都是层表的一部分。使用 Add 函数创建一个新图层然后添加它到层表中去。

You can assign a name to a layer when it is created. To change the name of a layer after it has been created, use the Name property. Layer names can include up to 255 characters and contain letters, digits, and the special characters dollar sign ($), hyphen (-), and underscore (_).

用户可以在创建图层时指定图层名称。要在创建图层后更改图层名,请使用 Name 特性。图层名称最多可包含255个字符,名称中可以包含字母、数字以及特殊字符美元符号 ($)、连字符 (-) 和下划线 (_)。

For more information about creating layers, see “Create and Name Layers” in the AutoCAD User's Guide.

更多关于创建图层的详细信息,请参见《AutoCAD 用户手册》中的“创建和命名图层”。  

创建新图层,指定为红色,然后向图层添加对象

The following code creates a new layer and circle object. The new layer is assigned the color red. The circle is assigned to the layer, and the color of the circle changes accordingly.

以下代码创建一个新图层和一个圆。新的图层指定使用红色。圆被指定到该图层,然后其颜色也相应改变。  

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Colors
 
<CommandMethod("CreateAndAssignALayer")> _
Public Sub CreateAndAssignALayer()
  '' 获得当前文档和数据库   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)
 
      Dim sLayerName As String = "Center"
 
      If acLyrTbl.Has(sLayerName) = False Then
          Dim acLyrTblRec As LayerTableRecord = New LayerTableRecord()
 
          '' 赋值层 ACI 颜色 1 和一个名字  Assign the layer the ACI color 1 and a name
          acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 1)
          acLyrTblRec.Name = sLayerName
 
          '' 升级层表为可写  Upgrade the Layer table for write
          acLyrTbl.UpgradeOpen()
 
          '' 添加新的层到层表和事务中去  Append the new layer to the Layer table and the transaction
          acLyrTbl.Add(acLyrTblRec)
          acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
      End If
 
      '' 以只读方式打开块表   Open the Block table for read
      Dim acBlkTbl As BlockTable
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
                                   OpenMode.ForRead)
 
      '' 以写方式打开模型空间块表记录   Open the Block table record Model space for write
      Dim acBlkTblRec As BlockTableRecord
      acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                      OpenMode.ForWrite)
 
      ''创建一个圆对象 Create a circle object
      Dim acCirc As Circle = New Circle()
      acCirc.SetDatabaseDefaults()
      acCirc.Center = New Point3d(2, 2, 0)
      acCirc.Radius = 1
      acCirc.Layer = sLayerName
 
      acBlkTblRec.AppendEntity(acCirc)
      acTrans.AddNewlyCreatedDBObject(acCirc, True)
 
      ''保存更改并销毁事务   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.Geometry;
using Autodesk.AutoCAD.Colors;
 
[CommandMethod("CreateAndAssignALayer")]
public static void CreateAndAssignALayer()
{
  // 获得当前文档和数据库   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;
 
      string sLayerName = "Center";
 
      if (acLyrTbl.Has(sLayerName) == false)
      {
          LayerTableRecord acLyrTblRec = new LayerTableRecord();
 
          // Assign the layer the ACI color 1 and a name
          acLyrTblRec.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
          acLyrTblRec.Name = sLayerName;
 
          // Upgrade the Layer table for write
          acLyrTbl.UpgradeOpen();
 
          // Append the new layer to the Layer table and the transaction
          acLyrTbl.Add(acLyrTblRec);
          acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
      }
 
      // 以只读方式打开块表   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
      Circle acCirc = new Circle();
      acCirc.SetDatabaseDefaults();
      acCirc.Center = new Point3d(2, 2, 0);
      acCirc.Radius = 1;
      acCirc.Layer = sLayerName;
 
      acBlkTblRec.AppendEntity(acCirc);
      acTrans.AddNewlyCreatedDBObject(acCirc, true);
 
      // Save the changes and dispose of the transaction
      acTrans.Commit();
  }
}
VBA/ActiveX 代码参考