擦除以前选择的数据

时间:2017-10-17 07:08:20

标签: vba autocad

我目前正在制作AutoCAD的图纸生成代码,为了生成图纸,必须选择某些选项,当选择了错误的选项时,此人可以点击返回去修正选项。下面是我的代码,问题是当"返回"时,代码不会删除先前选择的数据。单击,例如,如果我选择" SBPP"点击进入,它带我去" Louvres"选项,然后我想回到" SBPP"到" STD Piping",先前选择的" SBPP"必须删除:

Sub DWG ()
    Pump: ans = InputBox("1 = STD Piping" & vbCrLf & _
               "2 = Omit Pump" & vbCrLf & _
               "3 = SBPP", "Pump Piping")

    Select Case ans

    Case "1":
    : Set layerObj = ThisDrawing.Layers.Add("PUMP_PIPING_" & Size)
                layerObj.LayerOn = True
    Case "2":

    : Set layerObj = ThisDrawing.Layers.Add("OMIT_PUMP_" & Size)
                layerObj.LayerOn = True
    Case "3":

    : Set layerObj = ThisDrawing.Layers.Add("STBP_" & Size)
                layerObj.LayerOn = True


    Case Else: MsgBox "Wrong Input Dude.", vbCritical, MSG: Exit Sub

    End Select

 '______________________________________________________________________________
    'Option for Louvres

        Louver: ans = InputBox("1 = STD Louvers" & vbCrLf & _
                "2 = IND louvers" & vbCrLf & _
               "3 = Back ", "Pump Piping")

    Select Case ans

    Case "1":

    : Set layerObj = ThisDrawing.Layers.Add("LOUVRES_STD")
                layerObj.LayerOn = True

    Case "2":

    : Set layerObj = ThisDrawing.Layers.Add("LOUVRES_INDUS")
                layerObj.LayerOn = True

    Case "3":

    If ans = 3 Then
    GoTo Pump
    End If

    Case Else: MsgBox "Wrong Input Dude.", vbCritical, MSG: Exit Sub

    End Select
End Sub

如何修改此代码,以便在"返回"单击它会删除以前选择的数据吗?

1 个答案:

答案 0 :(得分:1)

这是一个流逻辑问题。您必须使用周期来完成目标。 “撤消”您以前的选项并非由您定义,我猜:

Sub DWG()
    Dim ans1 As String, ans2 As String, err_msg As String

    Do 'This cycle will begin and repeat when [Back] is choosen

        Select Case ans1 'Undo previous operation
        Case 1
            ThisDrawing.Layers("PUMP_PIPING_" & size).Delete
        Case "2"
            ThisDrawing.Layers.Add("OMIT_PUMP_" & size).Delete
        Case "3"
            ThisDrawing.Layers.Add("STBP_" & size).Delete
        End Select


        err_msg = ""

        Do 'This cycle will repeat until first answer is accepted
            ans1 = InputBox(err_msg & _
                          "1 = STD Piping" & vbCrLf & _
                          "2 = Omit Pump" & vbCrLf & _
                          "3 = SBPP", "Pump Piping")

            Select Case ans1
            Case "1"
                Set layerObj = ThisDrawing.Layers.Add("PUMP_PIPING_" & size)
                layerObj.LayerOn = True
            Case "2"
                Set layerObj = ThisDrawing.Layers.Add("OMIT_PUMP_" & size)
                layerObj.LayerOn = True
            Case "3"
                Set layerObj = ThisDrawing.Layers.Add("STBP_" & size)
                layerObj.LayerOn = True
            Case "" '[Cancel] button
                Exit Sub
            Case Else
                err_msg = "Wrong Input Dude." & vbCrLf & vbCrLf
                ans1 = ""
            End Select
        Loop While ans1 = ""

        err_msg = ""
        Do
            ans2 = InputBox(err_msg & _
                          "1 = STD Louvers" & vbCrLf & _
                          "2 = IND louvers" & vbCrLf & _
                          "3 = Back ", "Pump Piping")

            Select Case ans2
            Case "1"
                Set layerObj = ThisDrawing.Layers.Add("LOUVRES_STD")
                layerObj.LayerOn = True
            Case "2"
                Set layerObj = ThisDrawing.Layers.Add("LOUVRES_INDUS")
                layerObj.LayerOn = True
            Case "3"
                'Do nothing, loop control
            Case "" '[Cancel] button
                Exit Sub
            Case Else
                err_msg = "Wrong Input Dude." & vbCrLf & vbCrLf
                ans2 = ""
            End Select
        Loop While ans2 = ""
    Loop While ans2 = "3"
End Sub

也做了一些改变:

  1. 处理[取消]按钮;
  2. 在输入框中合并错误消息,因此用户在完成错误时不必按很多按钮;
  3. 制作更传统的代码格式
  4. 第二部分 - “在做之前询问”替代

    Sub DWG()
        Dim ans1, ans2
    
        ans1 = Choose(Val(InputBox("1 = STD Piping" & vbCrLf & "2 = Omit Pump" & vbCrLf & "3 = SBPP", "Pump Piping")), "PUMP_PIPING_", "PUMP_PIPING_", "PUMP_PIPING_")
        If IsNull(ans1) Then MsgBox "Wrong Input Dude.": Exit Sub
    
        ans2 = Choose(Val(InputBox("1 = STD Louvers" & vbCrLf & "2 = IND louvers")), "LOUVRES_STD", "LOUVRES_INDUS")
        If IsNull(ans2) Then MsgBox "Wrong Input Dude.": Exit Sub
    
        ThisDrawing.Layers.Add(ans1 & Size).LayerOn = True
        ThisDrawing.Layers.Add(ans2).LayerOn  = True
    
    End Sub