1.4、Turn Layers On and Off打开和关闭图层
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.
使用代表图层的图层表记录对象的IsOff属性来实现打开或关闭图层。 IsOff属性值为TRUE,将关闭图层;IsOff属性值为FALSE则打开图层。
Turn off a layer 关闭图层
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.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.Center = new Point3d(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 Code Reference
Sub TurnLayerOff()
' Create a new layer called "ABC"
Dim layerObj As AcadLayer
Set layerObj = ThisDrawing.Layers.Add("ABC")
' Turn off layer "ABC"
layerObj.LayerOn = False
' Create a circle
Dim circleObj As AcadCircle
Dim center(0 To 2) As Double
Dim radius As Double
center(0) = 2: center(1) = 2: center(2) = 0
radius = 1
Set circleObj = ThisDrawing.ModelSpace. AddCircle(center,radius)
' Assign the circle to the "ABC" layer
circleObj.Layer = "ABC"
circleObj.Update
ThisDrawing.Regen acActiveViewport
End Sub