5 Replies Latest reply on Oct 13, 2014 1:28 AM by Daniel Andersson

    select Configuration name in userform

    Yong Ning

      Private Sub CommandButton2_Click()

         With UserForm1

            .Hide

            ''   →  Select configuration (tree) → Msgbox → run → function tt()

            Stop

            .Show

         End With

       

      End Sub

       

      Function tt()

        Dim SwApp As SldWorks.SldWorks, SwModel As ModelDoc2

          Set SwApp = GetObject(, "SldWorks.Application")

          Set SwModel = SwApp.ActiveDoc

        Dim SwSel As SelectionMgr

          Set swSelMgr = SwModel.SelectionManager

        Dim sConfig As Configuration

          Set sConfig = swSelMgr.GetSelectedObject5(1)

          Debug.Print sConfig.Name

       

      End Function

       

      ‘’

      why can send  picture in forum.solidworks?

      see picture →

      How to Configurate in BOM of Parts with API? | SOLIDWORKS Forum

      https://forum.solidworks.com/thread/83943

        • Re: select Configuration name in userform
          Daniel Andersson

          Do you want the method that is triggered when user clicks button CommandButton2 to wait for the user to select a configuration in the SW configuration  manager and then call method tt()?

            • Re: select Configuration name in userform
              Yong Ning

              Sorry , I cad read english,but don't write with english.

              1 clicks button CommandButton2, hide userform

              2 select a configuration in the SW configuration

              3 call function tt()

              4 msgbox Sw configuration name

               

              Thanks

               

                • Re: select Configuration name in userform
                  Daniel Andersson

                  I have an idea of how this could be done. Please note that I have not thought of all details when it comes to exceptions etc... see this a conceptual solution.. tweak it to fit it. You also might want to change to msgbox to be swapp.sendmsgtouser2...

                   

                  You already have that hide/show going of the user form. So I just post a idea of this might work... Note that there is a time out so the macro wont be stuck in the loop for ever. I use a loop that checks for a selection of the configuration. The macro time out after 10 secs. Try it and see if this something useful.

                   

                  Without knowing the macro you are building, I would recommend that you add a list of the configurations to the userform so the user do not leave the macro. That is what I should have gone for...

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim swModel As SldWorks.ModelDoc2

                  Dim swSelMgr As SldWorks.SelectionMgr

                  Dim swConfig As SldWorks.Configuration

                  Dim bTime As Boolean

                  Dim lRetVal As Long

                  Dim dTime As Date

                   

                  Sub main()

                  'Replace with your CommandButton2_Click...

                   

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                   

                  If swModel Is Nothing Then Exit Sub

                   

                  Set swSelMgr = swModel.SelectionManager

                   

                  lRetVal = MsgBox("Please select a configuration", vbOKCancel + vbExclamation, "Select configuration")

                  If lRetVal = 2 Then Exit Sub

                   

                  Start_:

                  swModel.ClearSelection2 True

                  dTime = Now

                  bTime = False

                   

                  Do While swSelMgr.GetSelectedObjectCount2(-1) < 1

                      If DateDiff("s", dTime, Now) > 10 Then 'if more then 10sec have elapsed, Time out and exit

                          bTime = True

                          Exit Do

                      End If

                  Loop

                   

                  If bTime = True Then

                      MsgBox "Configuration selection was timed out by macro"

                     

                  ElseIf swSelMgr.GetSelectedObjectType2(1) = 47 Then

                      Set swConfig = swSelMgr.GetSelectedObject5(1)

                      If Not swConfig Is Nothing Then

                          tt (swConfig.Name)

                          MsgBox "Back to Sub Main()" & vbNewLine & "Selected configuration: " & swConfig.Name

                      Else

                          MsgBox "Error while selecting configuration" & vbNewLine & "Please select a configuration", vbCritical, "Select configuration"

                          GoTo Start_: 'Start over the selection....

                      End If

                     

                  Else

                      'Something else was selected, ask user to select once again..

                      MsgBox "Error while selecting configuration" & vbNewLine & "Please select a configuration", vbCritical, "Select configuration"

                      GoTo Start_: 'Start over the selection....

                  End If

                         

                  End Sub

                   

                  Function tt(iConfig As String) As Boolean

                       MsgBox "This is function tt() speaking..." & vbNewLine & "Selected configuration: " & iConfig

                       ' Do what you want here...

                  End Function