根据组名中选定的复选框生成值

问题描述 投票:0回答:1

目前,我有一个 VBA 代码,可以根据用户窗体将 Solidworks 装配体中一定数量的 .SLDPRT 文件保存为 .STEP 或 .X_T 文件。我的用户表单如下所示,其输出用于生成“saveas”文件名。 enter image description here

Code1 与 GroupName1 耦合,Code2 与 GroupName2 耦合。示例:用户填写Code1(例如“536”)和Code2(例如“373”)并选择part1、part3和part6。 随后,因为part1和part3都是GroupName1的一部分,所以Code1应该在新文件名中实现。只有第 6 部分应该有代码 2。 “REV”对于所有部件以及步骤/xt 的选择都是相同的。

目前,可以保存多个部分,但它们都包含 Code1 的值。我不知道如何将这段代码实现到我当前的循环中。

这是与我的问题相关的用户表单代码:

Private Sub CmdBtnUserParam_Click()
  'Check which checkboxes are selected and save in the array called ArrayParts
  Dim Control As Control, ArrayParts As String
  For Each Control In Me.Controls
  If TypeName(Control) = "CheckBox" Then
    If Control.Value Then
        ArrayParts = IIf(ArrayParts <> "", ArrayParts & ", ", "") & Control.Caption
    End If
  End If
  Next

  'CODES
  'Declare variables
  Dim CodeOne As String
  Dim CodeTwo As String

  'Set codes is equal to the text in the textboxes
  CodeOne = txtboxMCode1.Text 'Code1
  CodeTwo = txtboxMCode2.Text 'Code2

  'Call the UserInput subroutine to enter the variables
  Call UserInput(CodeOne, CodeTwo, ArrayParts)

  'End the macro
  End

End Sub

此信息已转移到我的主要子部分,部分放置在下面。循环保存所选零件的数量。目前,保存的每个部件都包含“MldPartCode1”(目前)。我希望这取决于保存的部分的名称。根据部件/复选框所在的组,该代码应该是 Code1 或 Code2。

Public Sub UserInput(InputCode1, InputCode2, InputArrayParts As String)
    Dim MldPartCode1, MldPartCode2 As String
    Dim ArrayList As Variant
    Dim ExtInit, ExtNew, PartNameFS, ProjectNr, XTFolder, REV  As String
    Dim PartNr, initName As String
    Dim SavePart As Variant
    Dim i As Integer
    Dim finalName As String
    Dim swModelActivated     As SldWorks.ModelDoc2
    Dim swModelToExport      As SldWorks.ModelDoc2
    Dim strModelName         As String

    'Create new pathname
    ExtInit = ".SLDPRT"                         'Old extension
    ExtNew = OptionExtension                    'Input from userform: either X_T or STEP
    MldPartCode1 = InputCode1                    'Input from userform
    MldPartCode2 = InputCode2                    'Input from userform
    REVCodeNR = "[REV" + InputREVCodeNr + "]"    'Input from userform
    ArrayList = Split(InputArrayParts, ",")      'Array with selected parts from userform
        
    For i = LBound(ArrayList) To UBound(ArrayList)
        initName = PathCut + ArrayList(i) + ExtInit
        finalName = PathCut & "XT\" & NumPart(initName) & "_" & MldPartCode1 & " " & ArrayList(i) & " " & REVCodeNR & ExtNew
        
        'Open and activate the correct model
        Set swModel = swApp.OpenDoc6(initName, 1, 0, "", nStatus, nWarnings)                                            'Open the model
        strModelName = swModel.GetTitle
        Set swModelActivated = swApp.ActivateDoc3(strModelName, False, swRebuildOnActivation_e.swUserDecision, nErrors) 'Activate the model
        Set swModelToExport = swApp.ActiveDoc

        'Reopen assembly
        Set swModel = swApp.OpenDoc6(PathInit, 1, 0, "", nStatus, nWarnings) 'Open the model
        Set swModelActivated = swApp.ActivateDoc3(PathInit, False, swRebuildOnActivation_e.swUserDecision, nErrors) 'Activate the model
        Set swModelToExport = swApp.ActiveDoc 'Get the activated model


        'Save the file as step
        swModelToExport.Extension.SaveAs3 finalName, 0, 1, Nothing, Nothing, nErrors, nWarnings

    Next
        
End Sub

希望我的问题足够清楚,能给我任何建议。如果您有疑问,请随时提问。我想提前感谢您花时间阅读我的问题。

vba loops checkbox userform solidworks
1个回答
0
投票

您可以在 For...Next 循环中插入对复选框名称的检查,如下所示:

Select Case arraylist(i)
  Case "part1", "part2", "part3", "part4"
  mldpartcode = MldPartCode1
Case Else
  mldpartcode = MldPartCode2
End Select

并更改

mldpartcode
赋值中的
mldPartCode1
变量。
    

© www.soinside.com 2019 - 2024. All rights reserved.