随便写的有关图层代码
<BR>Public g_LayerInfoArray() As LayerInfoObj'-----------------------------------------------------------------------------
Type LayerInfoObj<BR> layerObj As AcadLayer<BR> layerName As String<BR> isOn As Boolean<BR> isLock As Boolean<BR> isFreeze As Boolean<BR>End Type
'----保存所有图层状态-------------------------------------------------------------------------
Public Sub GetLayerInfoArray()<BR> On Error GoTo ErrorHandler<BR> ReDim g_LayerInfoArray(0)<BR> <BR> Dim lay As AcadLayer<BR> Dim lays As AcadLayers<BR> Set lays = ThisDrawing.Layers<BR> <BR> Dim i As Integer<BR> <BR> For i = 0 To lays.count - 1<BR> Set lay = lays.Item(i)<BR> ReDim Preserve g_LayerInfoArray(UBound(g_LayerInfoArray) + 1)<BR> <BR> With g_LayerInfoArray(UBound(g_LayerInfoArray))<BR> Set .layerObj = lay<BR> .layerName = lay.name<BR> .isOn = lay.LayerOn<BR> .isLock = lay.Lock<BR> .isFreeze = lay.Freeze<BR> End With<BR> Next<BR> <BR> Exit Sub<BR>ErrorHandler:<BR> Debug.Print "Sub GetLayerInfoArray() occurs errors: " + Err.Description<BR>End Sub
'----恢复所有图层状态-----------------------------------------------------
Public Sub SetLayerInfoArray()<BR> On Error GoTo ErrorHandler<BR> <BR> Dim i As Integer<BR> Dim lay As AcadLayer<BR> <BR> For i = 1 To UBound(g_LayerInfoArray)<BR> Set lay = g_LayerInfoArray(i).layerObj<BR> If LayerExist(lay.name) Then<BR> With lay<BR> .LayerOn = g_LayerInfoArray(i).isOn<BR> .Lock = g_LayerInfoArray(i).isLock<BR> If IsCurrentLayer(lay.name) = False Then<BR> .Freeze = g_LayerInfoArray(i).isFreeze<BR> End If<BR> End With<BR> End If<BR> Next<BR> <BR> Exit Sub<BR>ErrorHandler:<BR> Debug.Print "Sub SetLayerInfoArray() occurs errors: " + Err.Description<BR>End Sub
Public Sub ClearLayerInfoArray()<BR> ReDim g_LayerInfoArray(0)<BR>End Sub
'-----------------------------------------------------------------------------<BR>'-----------------------------------------------------------------------------
Public Function LayerExist(ByVal layerName As String) As Boolean<BR> On Error GoTo ErrorHandler<BR> LayerExist = False<BR> <BR> Dim layerObj As AcadLayer<BR> Set layerObj = ThisDrawing.Layers.Item(layerName)<BR> <BR> LayerExist = True<BR> <BR> Exit Function<BR>ErrorHandler:<BR>End Function
Public Function IsCurrentLayer(ByVal layerName As String) As Boolean<BR> Dim sysVarName As String<BR> Dim varData As Variant<BR> <BR> sysVarName = "CLAYER"<BR> varData = ThisDrawing.GetVariable(sysVarName)<BR> <BR> If StrComp(UCase$(varData), UCase(layerName)) = 0 Then<BR> IsCurrentLayer = True<BR> Else<BR> IsCurrentLayer = False<BR> End If<BR> <BR>End Function
Public Sub CloseAllLayers()<BR> On Error GoTo ErrorHandler<BR> <BR> Dim layerObj As AcadLayer<BR> Dim layersObj As AcadLayers<BR> Set layersObj = ThisDrawing.Layers<BR> <BR> Dim i As Integer<BR> For i = 0 To layersObj.count - 1<BR> Set layerObj = layersObj.Item(i)<BR> layerObj.LayerOn = False<BR> Next<BR> <BR> Exit Sub<BR>ErrorHandler:<BR>End Sub
Public Sub OpenLayer(ByVal layerName As String, ByVal bOnOff As Boolean)<BR> On Error GoTo ErrorHandler<BR> If LayerExist(layerName) Then<BR> Dim layerObj As AcadLayer<BR> Set layerObj = ThisDrawing.Layers.Item(layName)<BR> layerObj.LayerOn = bOnOff<BR> End If<BR> <BR> Exit Sub<BR>ErrorHandler:<BR>End Sub
Public Sub LockLayer(ByVal layerName As String, ByVal bLock As Boolean)<BR> On Error GoTo ErrorHandler<BR> If LayerExist(layerName) Then<BR> Dim layerObj As AcadLayer<BR> Set layerObj = ThisDrawing.Layers.Item(layName)<BR> layerObj.Lock = bLock<BR> End If<BR> <BR> Exit Sub<BR>ErrorHandler:<BR>End Sub
Public Sub FreezeLayer(ByVal layerName As String, ByVal bFreeze As Boolean)<BR> On Error GoTo ErrorHandler<BR> If LayerExist(layerName) Then<BR> Dim layerObj As AcadLayer<BR> Set layerObj = ThisDrawing.Layers.Item(layName)<BR> layerObj.Lock = bFreeze<BR> End If<BR> <BR> Exit Sub<BR>ErrorHandler:<BR>End Sub
Public Sub SetClayer(ByVal layerName As String)<BR> If IsCurrentLayer(layerName) = False And LayerExist(layerName) Then<BR> Dim sysVarName As String<BR> Dim sysVarData As Variant<BR> <BR> sysVarName = "CLAYER"<BR> sysVarData = layerName<BR> ThisDrawing.SetVariable sysVarName, sysVarData<BR> End If<BR>End Sub<BR> 干啥用的?
回复
在处理数据前 保存和恢复 图层状态, 以及图层是否存在等等. 保存和恢复图层状态,本身CAD已经提供了对象来处理,而且也可以通过程序控制:<BR><A href="http://www.mjtd.com/object/acad2004/idh_layerstatemanager.htm" target="_blank" >LayerStateManager</A> 保存和恢复使用图层属性管理器设置的图层。
回复
谢谢mccad,知道了<FONT color=#0033ff><b>LayerStateManager</b></FONT>
页:
[1]