03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-6)
2011-08-22 11:15
531 查看
1.6、Lock and Unlock Layers锁定和解锁图层
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 associatedcolors 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属性来锁定或解锁图层。IsLocked属性值为TRUE则锁定图层,IsLocked属性值为FALSE则解锁图层。
Lock a layer 锁定图层
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 Code Reference
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
相关文章推荐
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-3)
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(2)使用颜色
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-7)
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-9)
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-1)
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-4)
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-5)
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(3)使用线型
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-2)
- 03-05 创建和编辑AutoCAD实体(五) 使用图层、颜色和线型(1)使用图层(1-8)
- 03-07 创建和编辑AutoCAD实体(七) 向图形中添加文字(4)使用Unicode字符及其他...
- 03-06 创建和编辑AutoCAD实体(六) 保存和恢复图层状态
- 03-03 创建和编辑AutoCAD实体(三) 使用选择集(1)
- 03-03 创建和编辑AutoCAD实体(三) 使用选择集(2)
- 03-07 创建和编辑AutoCAD实体(七) 向图形中添加文字(1)使用文字样式
- 03-07 创建和编辑AutoCAD实体(七) 向图形中添加文字(2)使用单行文字
- 03-07 创建和编辑AutoCAD实体(七) 向图形中添加文字(3)使用多行文字
- 03-04 创建和编辑AutoCAD实体(四) 编辑二维命名对象 (3)
- 03-01 创建和编辑AutoCAD实体(一) 打开和关闭对象
- 03-02 创建和编辑AutoCAD实体(二) 创建对象(1)