5 Replies Latest reply on Aug 19, 2016 11:12 AM by Dan Hofstetter

    Script to change model based on Excel file

    Dan Hofstetter

      I have been having trouble with "Server Busy" errors in SolidWorks 2013 when opening a .SLDPRT file that has a slot feature that is LPatterned many times (300 or more small slots in one part).  The dimensions of the slot and number of instances are defined in a Design Table file.  I have not been able to prevent the "Server Busy" warning, and it won't go away on its own no matter how long I wait.  I am automating SolidWorks from an Excel macro, and am not always at my desk to click the "Retry" button every time this occurs, so I am looking for a workaround.

       

      I think one way to avoid the Server Busy error is to delete the design table from the .SLDPRT file, then change the dimensions and suppression states of all design table variables using VBA code (pushing the dimension values via the API).  My thought was to go through the Excel design table file one column at a time, read the variable name, decide if it is a dimension or state, then change the dimension or state based on the entered value in the next row.

       

      I got the following code snippet to work:

      Sub Change_SW_Dims()

       

          Set swApp = CreateObject("SldWorks.Application")

          Set Part = swApp.ActiveDoc

          Part.Parameter("TankDX@Tank Profile Plan View").SystemValue = 1600 * 0.0254

          boolstatus = Part.Extension.SelectByID2("Slot 1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

          Part.EditUnsuppress2

          Part.EditRebuild

          Part.ClearSelection

       

      End Sub

       

      Before spending a lot of time playing around with this, I thought I'd search for something similar that I could modify.  I have not been able to find similar code yet, and was wondering if anyone might be able to point me in the right direction?

        • Re: Script to change model based on Excel file
          Dan Hofstetter

          Ok, I got this working, here is the code in case anyone else ever needs to do something similar.  I don't see an option to include this as anything other than plain text, sorry:

           

          Sub Change_SW_Dims()

          '-----------------------------------------------------------

          'First read all values from DesignTable.xlsx

          'into a two-dimensional array

          '-----------------------------------------------------------

              Dim xlApp As Excel.Application

              Dim wb As Workbook

           

           

              Set xlApp = CreateObject("Excel.Application")

              xlApp.Visible = True 'For troubleshooting so the window appears

              xlApp.Workbooks.Open ("F:\TEMP\DesignTable.xlsx")

              Set wb = xlApp.Workbooks(1)

              wb.Worksheets(1).Activate 'Activates the first sheet in workbook regardless of name

           

           

              Dim varArray() As Variant

              Dim rngSource As Range

              Dim nRows, nCols As Integer

              Set rngSource = wb.Worksheets(1).Range("B2:JB3")

              nRows = rngSource.Rows.Count

              nCols = rngSource.Columns.Count

              ReDim varArray(1 To rngSource.Rows.Count, 1 To rngSource.Columns.Count)

              varArray = rngSource.Value

           

           

              Debug.Print ("Rows = " & rngSource.Rows.Count)

              Debug.Print ("Cols = " & rngSource.Columns.Count)

           

           

              For i = 1 To rngSource.Columns.Count

                  Debug.Print (varArray(1, i) & " = " & varArray(2, i))

              Next i

           

           

              xlApp.Workbooks("DesignTable.xlsx").Close

              xlApp.Quit

           

           

          '------------------------------------------------------------------

          'Next change dimension values and suppression states in SolidWorks

          '------------------------------------------------------------------

           

           

              Set swApp = CreateObject("SldWorks.Application")

              Set Part = swApp.ActiveDoc

             

              For i = 1 To nCols

                  If Left(varArray(1, i), 1) <> "$" Then

                      'Debug.Print ("Not a state")

                      Debug.Print (i & ": " & varArray(1, i) & " = " & varArray(2, i))

                      Part.Parameter(varArray(1, i)).SystemValue = varArray(2, i) * 0.0254

                  Else

                      Debug.Print (i & ": " & varArray(1, i) & " = " & varArray(2, i))

                      boolstatus = Part.Extension.SelectByID2(varArray(1, i), "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

                      If (varArray(2, i) = "Suppressed") Then

                          Part.EditSuppress2

                      Else

                          If (varArray(2, i) = "Unsuppressed") Then

                              Part.EditUnsuppress2

                          End If

                      End If

                  End If

              Next i

             

              Part.EditRebuild

              Part.ClearSelection

           

           

          End Sub

            • Re: Script to change model based on Excel file
              Deepak Gupta

              You can always attach files here.

               

              Check #18 of Forum Posting on how to attach files while replying to a post.

                • Re: Script to change model based on Excel file
                  Dan Hofstetter

                  Thanks for the tip!  In general when searching for help with code I look for snippets in help forums first, where copy and paste is easy and doesn't require me to download anything.  I don't use this forum much and couldn't remember if you had requirements for pasting code snippets - some of the other programming forum moderators get upset if you don't post them in a quote block.  Cheers!

                    • Re: Script to change model based on Excel file
                      Deepak Gupta

                      No you're free to paste them or upload.

                       

                      You may format them if required like this.

                       

                       

                          Set swApp = CreateObject("SldWorks.Application")
                          Set Part = swApp.ActiveDoc
                      
                          For i = 1 To nCols
                              If Left(varArray(1, i), 1) <> "$" Then
                                  'Debug.Print ("Not a state")
                                  Debug.Print (i & ": " & varArray(1, i) & " = " & varArray(2, i))
                                  Part.Parameter(varArray(1, i)).SystemValue = varArray(2, i) * 0.0254
                              Else
                                  Debug.Print (i & ": " & varArray(1, i) & " = " & varArray(2, i))
                                  boolstatus = Part.Extension.SelectByID2(varArray(1, i), "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)
                                  If (varArray(2, i) = "Suppressed") Then
                                      Part.EditSuppress2
                                  Else
                                      If (varArray(2, i) = "Unsuppressed") Then
                                          Part.EditUnsuppress2
                                      End If
                                  End If
                              End If
                          Next i
                      
                          Part.EditRebuild
                          Part.ClearSelection
                      
                      
                      End Sub
                      

                       

                      This is available in advanced editor only. Click on reply and then click on "use advanced editor" on right side

                       

                      And then format as required