2 Replies Latest reply on Sep 25, 2013 8:52 AM by Alex Brewer

    GetEquationMgr not returning anything?

    Alex Brewer

      Hello,

       

      I'm trying to create a macro that opens a read-only file from a server location, saves a writable copy to the users hard drive, then opens that part and changes some equations based on input through an excel worksheet.

       

      What's happening is the part is opened, saved as copy, all parts are closed. The new part is opened, which is where stuff breaks down. The equation manager isn't returning anything when being called and the subroutine doesn't run. I can't really find anyone with my problem, so I'm at a loss.

       

      The part most definitely has equations to change, so that's not the problem.

       

       

      Here's my code:

       

      Dim swApp As SldWorks.SldWorks
      Dim swModel As SldWorks.ModelDoc2
      Dim swEqnMgr As SldWorks.EquationMgr
      Dim xlApp As Excel.Application
      Dim xlWb As Excel.Workbook
      Dim strPart As String
      Dim lErrors As Long
      Dim lWarnings As Long
      Dim fLength As Long
      Dim fHeight As Long
      Dim fWidth As Long
      Dim pin_centers As Long
      Dim savefilepath As String
      Dim filename As String
      Dim partsaved As Boolean
      Dim NewFileOpenPath As String
      Dim i As Integer
      Dim vSplit As Variant
      Dim boolstatus As Boolean

      Sub main()

       

         
         
          Set xlApp = Excel.Application
          Set xlWb = xlApp.ActiveWorkbook
         
          fLength = xlWb.ActiveSheet.Cells(2, 2).value
          fWidth = xlWb.ActiveSheet.Cells(2, 3).value
          fHeight = xlWb.ActiveSheet.Cells(2, 4).value
          pin_centers = xlWb.ActiveSheet.Cells(2, 5).value
          'rollover_trunion_state = xlWb.ActiveSheet.Cells(2, 6).value
          savefilepath = Application.DefaultFilePath
          strPart = "C:\Users\admin\Desktop\stuff\solid model templates\f template current\test\test wo design table - copy.sldprt"
          filename = fLength & " x " & fWidth & " x " & fHeight & " with " & pin_centers & "pinctrs"
          NewFileOpenPath = savefilepath & "\" & filename & ".sldprt"
         
        
         
          If pin_centers < (fLength + 2) Then
              pin_centers = InputBox("Your pin center distance looks a little wacky. Why don't you try and enter something more sensible?", "Pin center distance check", CStr(ActiveSheet.Cells(2, 5)))
          End If
         
          On Error Resume Next
          Set swApp = GetObject(, "sldworks.application")
         
          If swApp Is Nothing Then
              Set swApp = CreateObject("SldWorks.Application")
              swApp.Visible = True
          Else
              If MsgBox("This macro is about to close all documents open in solidworks, without saving. If you would like to make changes to or save any of these files, press cancel and start the macro when you are ready. Otherwise, press ok.", vbOKCancel, "Continue?") = vbCancel Then
                  Exit Sub
              End If
          End If
         
         
         
          Set swModel = swApp.OpenDoc6(strPart, swDocPART, swOpenDocOptions_slient, "", lErrors, lWarnings)
          partsaved = swModel.SaveAs4(NewFileOpenPath, swSaveAsCurrentVersion, swSaveAsOptions_Copy, lErrors, lWarnings)

          boolstatus = swApp.CloseAllDocuments(True)
         
          Set swModel = Nothing
          Set swModel = swApp.OpenDoc6(NewFileOpenPath, swDocPART, swOpenDocOptions_slient, "", lErrors, lWarnings)
          Set swModel = swApp.ActiveDoc

         
          swModel.ForceRebuild3 (False)
         
         
          Call changevar("length", fLength)
          Call changevar("width", fWidth)
          Call changevar("height", fHeight)
          Call changevar("pin_center_distance", pin_centers)


          'call changevar("rollover_trunion_state",rollover_trunion_state)
         
         
          MsgBox "You have created a f with the following dimensions:" & vbNewLine & "Length: " & fLength & vbNewLine & _
          "Width: " & fWidth & vbNewLine & _
          "Height: " & fHeight & vbNewLine & _
          "Pin Centers: " & pin_centers & vbNewLine & vbNewLine & _
          "It has been saved in the following location: " & vbNewLine & _
          savefilepath
         


      End Sub

       

      Sub changevar(VARIABLE_NAME As String, NEW_VALUE As Long)

          Set swApp = Application.SldWorks
          Set swModel = swApp.ActiveDoc
          Set swEqnMgr = swModel.GetEquationMgr
         
          NEW_VALUE = CStr(NEW_VALUE)
          For i = 0 To swEqnMgr.GetCount - 1
              vSplit = Split(swEqnMgr.Equation(i), "=")
              vSplit(0) = Replace(vSplit(0), Chr(34), Empty)
             
              If vSplit(0) = VARIABLE_NAME Then _
                  swEqnMgr.Equation(i) = Replace(swEqnMgr.Equation(i), vSplit(1), NEW_VALUE)
          Next i
      End Sub

       


      End Sub

       

       

       

       

      I modified code that Keith Rice provided earlier to create the subroutine, credit goes to him.

        • Re: GetEquationMgr not returning anything?
          Keith Rice

          Alex,

           

          WIthin your changevar() subroutine, remove these two lines because they aren't necessary:

           

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

           

          Also, at the very end of your code, why do you have an extra "End Sub"? Unless you didn't paste all of your code, that shouldn't be there.

           

          Does that make any difference? If not, please provide specific details concerning what is wrong. You wrote, "The equation manager isn't returning anything when being called and the subroutine doesn't run." This is very vague.

           

          Finally, have you written a test macro that does nothing more but try to change the equations in one of the parts you are working with? That way you can really isolate the issue and see where the problem lies. For example, something like this:

           

          ======

          'Declare variables up here...

           

          Sub main()

               Set swApp = Application.SldWorks.

               Set swModel = swApp.ActiveDoc

               fLength = 2       'arbitrary value

               Call changevar("length", fLength)

          End Sub

           

          Sub changevar(VARIABLE_NAME As String, NEW_VALUE As Long)

              Set swApp = Application.SldWorks
              Set swModel = swApp.ActiveDoc
              Set swEqnMgr = swModel.GetEquationMgr
             
              NEW_VALUE = CStr(NEW_VALUE)
              For i = 0 To swEqnMgr.GetCount - 1
                  vSplit = Split(swEqnMgr.Equation(i), "=")
                  vSplit(0) = Replace(vSplit(0), Chr(34), Empty)
                 
                  If vSplit(0) = VARIABLE_NAME Then _
                      swEqnMgr.Equation(i) = Replace(swEqnMgr.Equation(i), vSplit(1), NEW_VALUE)
              Next i
          End Sub

           

          ======

           

          Keith

          SolidWorks API Training

            • Re: GetEquationMgr not returning anything?
              Alex Brewer

              Keith,


              The extra End Sub was a copy/paste thing, it's not in the actual module.


              If I step through the macro, when it gets to "For i = 0 To swEqnMgr.GetCount - 1" it skips to the end of the subroutine, meaning swEqnMgr.GetCount is 1 or 0. The hover text over the variable swEqnMgr says "swEqnMgr = nothing", so I'm assuming it doesn't pick up on the equations in the part file.


              I tried writing a test macro similar to yours, testing only the subroutine. It hasn't thrown any errors. I tried creating a test part that took all those parameters and I had the same problem.


              It's got to be something to do with opening and closing the part.

               

              I believe my error is happening where I open the template part, save it, close everything, then open the saved copy of the template file.

               

               

               

              EDIT: I guess I'm a true programmer now. I got into work this morning, ran my code, and it worked. Nothing had changed. I don't know what happened.