打开和关闭图层
 
 

Layers in which are turned off are regenerated with the drawing but are not displayed or plotted. By turning layers off, you avoid regenerating the drawing every time you thaw a layer. When you turn a layer on that has been turned off, AutoCAD redraws the objects on that layer.

关闭的图层将和图形一起重生成,但不能显示或打印。通过关闭图层,可以避免每次解冻图层时都重生成图形。打开已关闭的图层时,AutoCAD 将重画该图层上的对象。  

Use the IsOff property on the Layer Table Record object that represents the layer you want to turn on or off. If you input a value of TRUE, the layer is turned off. If you input a value of FALSE, the layer is turned on.

使用代表图层的 LayerTableRecord 对象的 IsOff 属性打开和关闭图层。如果为此特性输入 TRUE,将关闭图层;如果输入 FALSE,将打开图层。  

关闭图层

This example creates a new layer and turns it off, and then adds a circle to the layer so that the circle is no longer visible.

本例创建一个新图层并关闭它,然后添加一个圆到这个图层中,所以这个圆不再可见。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("TurnLayerOff")> _
Public Sub TurnLayerOff()
  '' 获得当前文档和数据库   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 = "ABC"
      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
          acLyrTbl.UpgradeOpen()
 
          '' 追加一个新的图层到层表和事务中去  Append the new layer to the Layer table and the transaction
          acLyrTbl.Add(acLyrTblRec)
          acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)
      Else
          acLyrTblRec = acTrans.GetObject(acLyrTbl(sLayerName), _
                                          OpenMode.ForWrite)
      End If
 
      '' 关闭图层  Turn the layer off
      acLyrTblRec.IsOff = True
 
      '' 以只读方式打开块表   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;
 
[CommandMethod("TurnLayerOff")]
public static void TurnLayerOff()
{
  // 获得当前文档和数据库   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 = "ABC";
      LayerTableRecord acLyrTblRec;
 
      if (acLyrTbl.Has(sLayerName) == false)
      {
          acLyrTblRec = new LayerTableRecord();
 
          // Assign the layer a name
          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);
      }
      else
      {
          acLyrTblRec = acTrans.GetObject(acLyrTbl[sLayerName],
                                          OpenMode.ForWrite) as LayerTableRecord;
      }
 
      // Turn the layer off
      acLyrTblRec.IsOff = 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 代码参考