尝试一下并适应它。它消除了所有的错误陷阱。
基本原则是:在命令开始时设置一个布尔值,然后在命令结束时将其设置为false。如果begincommand的布尔值为真,则endcommand不会被触发。
- Option Explicit
- Private PrevLayer As AcadLayer
- Private CmdActive As Boolean
- Function EscPrompt() As Boolean
- Dim varCancel As Variant
- varCancel = ThisDrawing.GetVariable("LASTPROMPT")
- If InStr(1, varCancel, "*Cancel*") 0 Or _
- InStr(1, varCancel, "") 0 Then 'this takes care of vbarun, toolbarsetc w/ ^C^C
- If CmdActive > 0 Then
- EscPrompt = True
- End If
- End If
-
- End Function
- Public Property Get oPrevLayer() As AcadLayer
- If IsObject(PrevLayer) Then
- If PrevLayer Is Nothing Then
- Set oPrevLayer = ThisDrawing.Layers("0")
- Set PrevLayer = ThisDrawing.ActiveLayer
- End If
- End If
- Set oPrevLayer = PrevLayer
- End Property
- Public Property Let oPrevLayer(l As AcadLayer)
- Set PrevLayer = l
- End Property
- Public Sub AcadDocument_BeginCommand(ByVal CommandName As String)
- Dim oLayer As AcadLayer
-
- If CmdActive Then
- If EscPrompt Then
- ThisDrawing.ActiveLayer = oPrevLayer
- End If
- End If
- Select Case UCase(CommandName)
- ' blnActive = False
-
- Case "BHATCH"
- Set oLayer = ThisDrawing.Layers("T-Hatch")
- CmdActive = True
- ThisDrawing.ActiveLayer = oLayer
-
- End Select
-
- End Sub
- Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
- If CmdActive Then CmdActive = False
- Select Case UCase(CommandName)
- Case "BHATCH"
- ThisDrawing.ActiveLayer = oPrevLayer
-
- Case Else
- Exit Sub
- End Select
-
- Set PrevLayer = Nothing
- End Sub
启动Hatch命令时,绘制矩形并观察图层特性管理器。后来试着逃跑。修复不是直接的,画另一个矩形并观察它的变化。 |