锁定和解锁层 (.NET)
您无法编辑锁定图层上的对象;但是,如果该层打开并解冻,它们仍然可见。您可以使锁定层成为当前图层,并且可以向其添加对象。您可以冻结和关闭锁定的图层,并更改其关联的颜色和线型。 使用该属性锁定或解锁图层。如果输入值 ,则图层将被锁定。如果输入值 ,则图层将解锁。IsLockedTRUEFALSE 锁定图层本示例创建一个名为“ABC”的新图层,然后锁定该图层。 VB.NETImports 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"
If acLyrTbl.Has(sLayerName) = False Then
Using acLyrTblRec As LayerTableRecord = New LayerTableRecord()
'' Assign the layer a name
acLyrTblRec.Name = sLayerName
'' Lock the layer
acLyrTblRec.IsLocked = True
'' Upgrade the Layer table for write
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
Dim acLyrTblRec As LayerTableRecord = acTrans.GetObject(acLyrTbl(sLayerName), _
OpenMode.ForWrite)
'' Lock the layer
acLyrTblRec.IsLocked = True
End If
'' 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";
if (acLyrTbl.Has(sLayerName) == false)
{
using (LayerTableRecord acLyrTblRec = new LayerTableRecord())
{
// Assign the layer a name
acLyrTblRec.Name = sLayerName;
// Lock the layer
acLyrTblRec.IsLocked = true;
// Upgrade the Layer table for write
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
{
LayerTableRecord 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 代码参考Sub LockLayer()
' Create a new layer called "ABC"
Dim layerObj As AcadLayer
Set layerObj = ThisDrawing.Layers.Add("ABC")
' Lock layer "ABC"
layerObj.Lock = True
End Sub
相关概念父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-10-29 12:16
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.