Search code examples
vbaloopscheckboxuserformsolidworks

Generate values based on selected checkboxes within Groupname


Currently, I have a VBA code that saves a certain number of .SLDPRT files within a Solidworks assembly as .STEP or .X_T files, based on a userform. My userform looks like the following and its output is used to generate a "saveas" filename. enter image description here

Code1 is coupled with GroupName1 and Code2 is coupled with GroupName2. Example: The user fills in Code1 (e.g. "536") and Code2 (e.g. "373") and selects part1, part3 and part6. Subsequently, because both part1 and part3 are part of GroupName1, Code1 should be implemented in the new filename. Only part6 should have the Code2. "REV" is the same for all the parts, as well as the choice for step/xt.

Currently, saving multiple parts works, but they all contain the value for Code1. I don't know how to implement this piece of code into my current loop.

This is the Userform code associated with my question:

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

This information is transferred to my main Sub partially placed below. A loop saves the amount of parts that is selected. At the moment, each part that gets saved includes "MldPartCode1" (for now). I want this to be dependent on the name of the part that gets saved. Based on the group that part/checkbox is in, that code should be either Code1 or 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

Hopefully my question is clear enough to give me any advice. If you have question, do not hesitate to ask. And I want to thank you in advance for taking the time to read my question.


Solution

  • You can insert in your For...Next loop a check for the checkbox name something like this:

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

    and change for mldpartcode the mldPartCode1 variable in the finalName assignment.