锁定和解锁图层
 
 

You cannot edit objects on a locked layer; however, they are still visible if the layer is on and thawed. You can make a locked layer current and you can add objects to it. You can freeze and turn off locked layers and change their associated colors and linetypes.

用户不能编辑锁定图层上的对象,但如果图层是打开和解冻的,这些对象仍然可见。可以将锁定图层置为当前并在其上添加对象。可以冻结和关闭锁定图层,也可以更改其关联的颜色和线型。 

Use the IsLocked property to lock or unlock a layer. If you input a value of TRUE, the layer is locked. If you input a value of FALSE, the layer is unlocked.

要锁定或解锁图层,请使用 IsLocked 特性。如果为此特性输入 TRUE,将锁定图层;如果输入 FALSE,将解锁图层。 

锁定图层

This example creates a new layer called “ABC” and then locks the layer.

本例创建一个称为“ABC”的新图层,然后锁定该图层。 

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("LockLayer")> _
Public Sub LockLayer()
  '' 获得当前文档和数据库   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
 
      '' Lock the layer
      acLyrTblRec.IsLocked = 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;
 
[CommandMethod("LockLayer")]
public static void LockLayer()
{
  // 获得当前文档和数据库   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;
      }
 
      // Lock the layer
      acLyrTblRec.IsLocked = true;
 
      // Save the changes and dispose of the transaction
      acTrans.Commit();
  }
}
VBA/ActiveX 代码参考