2017-10-17 90 views
0

我目前正在爲AutoCAD生成圖形代碼,爲了生成圖形,必須選擇某些選項,並且當選擇了錯誤的選項時,該人員可以點擊返回去糾正這個選項。下面是我的代碼,問題是代碼不會在單擊「返回」時刪除之前選擇的數據,例如,如果我選擇「SBPP」點擊輸入並且需要我選擇「Louvres」選項,則I想回去,並從「SBPP」到「STD管道」,先前選擇「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

這是一個流程邏輯問題。你必須使用週期來完成你的目標。 「撤消」你以前的選項不是由您定義,我猜:

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. 取得了較爲傳統的代碼格式

第二部分 - 替代

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 
+0

感謝變化@LS_ᴅᴇᴠ但代碼「做之前詢問」還在做着同樣的事情,當選項是在輸入框中選擇的,該圖層是通過圖形打開的,當您點擊後,它不會刪除先前的圖層,而是在其上創建一個不同的圖形並創建一個笨拙的圖形。 – Kwezi

+0

當然它不......那個「撤銷」代碼不是你指定的。讓我檢查 –

+0

在這個 – Kwezi

相關問題