13 Replies Latest reply on Dec 4, 2014 11:31 AM by Mike Campbell

    Using IEquationMgr in SolidWorks 2014 macro

    Mike Campbell

      I'm having difficulty gaining access to the functionality of IEquationMgr in a simple macro. I'm hoping someone will offer to show me how to integrate it in.

        • Re: Using IEquationMgr in SolidWorks 2014 macro
          Daniel Andersson

          This example is found in the SolidWorks API reference. Get Equation Values Example (VBA)
          If you still got trouble, please share a code snippet or give us more information about what you want to do and what errors you are getting.

            • Re: Using IEquationMgr in SolidWorks 2014 macro
              Mike Campbell

              I may be corrected but I don't believe EquationMgr has all the functionality of IEquationMgr. Most specifically I need to be able to set the value of LinkToFile.

                • Re: Using IEquationMgr in SolidWorks 2014 macro
                  Daniel Andersson

                  I'm not a professional coder. Just a happy mech. engineer getting lost sometimes...

                   

                  But I do believe that IEquationMgr is equal to EquationMgr. The IEquationMgr is the interface and found in the api refernce. You will find them both in the VBA editor as exposed and the result is same.

                   

                  I belive that the "I" is a suffix that states that it is a interface, just as people use to add suffix "s" for string variables etc etc. Perhaps I'm wrong since I'm not that skilled and base this on my assumptions. I would be very happy to learn if there is an difference in IEquationMgr and Sldworks.EquationMgr.

                   

                  Made a sample... with both using the "I"... and as I regulary do, without the suffix  "I". To me, they both give me the same result in the debug window.

                  Code below will print True/False if there is a linked file, and also the file path.

                   

                  Option Explicit

                  Dim I_swApp As ISldWorks
                  Dim I_swModel As IModelDoc2
                  Dim I_swEqMgr As IEquationMgr

                   

                  Dim swApp As SldWorks.SldWorks
                  Dim swModel As SldWorks.ModelDoc2
                  Dim swEqMgr As SldWorks.EquationMgr

                   

                  Sub main()

                  Set I_swApp = Application.SldWorks
                  Set I_swModel = I_swApp.ActiveDoc
                  Set I_swEqMgr = I_swModel.GetEquationMgr

                  Debug.Print I_swModel.GetPathName
                  Debug.Print I_swEqMgr.LinkToFile & " - " & I_swEqMgr.FilePath

                   

                  Set swApp = Application.SldWorks
                  Set swModel = swApp.ActiveDoc
                  Set swEqMgr = swModel.GetEquationMgr

                  Debug.Print swModel.GetPathName
                  Debug.Print swEqMgr.LinkToFile & " - " & swEqMgr.FilePath

                  End Sub

                    • Re: Using IEquationMgr in SolidWorks 2014 macro
                      Mike Campbell

                      I have some code to show. Where's the "insert code" tool...?

                        • Re: Using IEquationMgr in SolidWorks 2014 macro
                          Daniel Andersson

                          I just paste it, select it and then press the quote symbol ".

                           

                          The other way is to use the advanced editor for the forum.  You find it in the upper right corner of the text box. In advance mode you can select text and press the icon >> and then select how the text should be formated.

                            • Re: Using IEquationMgr in SolidWorks 2014 macro
                              Mike Campbell

                              You are not incorrect in as far as you go with your answer. I want to set LinkToFile to be false. The code below will report that it is toggled but upon examination you'll see nothing's changed in the Equations folder. SW "help" specifically states that it can be set. There's something I've left out and I suspect it relates to "Property LinkToFile As System.Boolean" as found here. Attached is the nothing file I've been testing.

                               

                              Option Explicit

                               

                              Dim swApp As ISldWorks

                              Dim swModel As IModelDoc2

                              Dim swEqnMgr As IEquationMgr

                              Dim bolStat As Boolean

                              Dim lngCount As Long

                              Dim i As Long

                               

                              Sub main()

                               

                              Set swApp = Application.SldWorks

                              Set swModel = swApp.ActiveDoc

                              Set swEqnMgr = swModel.GetEquationMgr

                               

                              lngCount = swEqnMgr.GetCount

                              'Toggle link-to-file for each equation

                              For i = 0 To lngCount - 1

                                  Debug.Print swEqnMgr.LinkToFile

                                      If swEqnMgr.LinkToFile = False Then

                                          swEqnMgr.LinkToFile = True

                                      Else

                                          swEqnMgr.LinkToFile = False

                                      End If

                                  Debug.Print swEqnMgr.LinkToFile

                              Next

                               

                              End Sub

                                • Re: Using IEquationMgr in SolidWorks 2014 macro
                                  Daniel Andersson

                                  Hi,

                                   

                                  I hope that I'm wrong on this one... but it seems like it is not possible.

                                   

                                  Found this post which explains the behavior: Getting LinkToFile from each equation???

                                  It is currently not possible to set if LinkToFile for each equation with API from what I can see. Also in the API reference it does not seem to be possible to get the LinkToFile for a specific equation (No way to use the index that is needed).

                                   

                                  Further on, I tested to set LinkToFile to false and FilePath to "". Then code remove the LinkToFile but the equations seems to be disabled/controlled in another way then. See image below.

                                  capture1.PNG

                                  If you want to remove them and not having them grayed out. I would just loop thru and get each equation, then delete them and then add them again.

                                  Something like this... note that this will be for all configurations, you have to loop thru all configurations to get the correct suppression state and remove the links (bugged out gray ones...).

                                   

                                  EDIT 2014-12-01: Removed non-working macro.

                                    • Re: Using IEquationMgr in SolidWorks 2014 macro
                                      Mike Campbell

                                      Daniel,

                                       

                                      I appreciate your extended effort. Just so you don't go off in the wrong direction, in effect what I'd like to do is to remove the checkmark beside "Link to external file". In the end, I have no problem doing it the hard way. The pseudo code becomes:

                                       

                                      Remember all equations

                                      Delete all equations

                                      Set file path to "" for external file

                                      Write the equations from memory

                                       

                                      This works except that I've not addressed multiple configurations.

                                       

                                      Option Explicit

                                       

                                      Dim swApp As ISldWorks

                                      Dim swModel As IModelDoc2

                                      Dim swEqnMgr As IEquationMgr

                                      Dim lngCount As Long

                                      Dim i As Long

                                      Dim strEqn() As String

                                      Dim bolStat As Boolean

                                       

                                      Sub main()

                                       

                                      Set swApp = Application.SldWorks

                                      Set swModel = swApp.ActiveDoc

                                      Set swEqnMgr = swModel.GetEquationMgr

                                       

                                      lngCount = swEqnMgr.GetCount

                                       

                                      ReDim strEqn(lngCount - 1, 0)

                                      For i = 0 To lngCount - 1

                                          strEqn(i, 0) = swEqnMgr.Equation(i) 'Remember equations

                                      Next

                                       

                                      swEqnMgr.FilePath = "" 'Remove path to referenced text file

                                       

                                      For i = 0 To lngCount - 1

                                          swEqnMgr.LinkToFile = False

                                      Next

                                       

                                      Do While swEqnMgr.Equation(0) <> ""

                                          swEqnMgr.Delete (0) 'Delete the next equation until none remain

                                      Loop

                                       

                                      For i = 0 To lngCount - 1

                                          i = swEqnMgr.Add(i, strEqn(i, 0)) 'Repopulate list of equations

                                      Next

                                       

                                      bolStat = swModel.ForceRebuild3(True)

                                       

                                      swModel.Save

                                       

                                      End Sub

                                        • Re: Using IEquationMgr in SolidWorks 2014 macro
                                          Daniel Andersson

                                          I sometimes get stuck in my mind when I'm just about to do a breif test and the ideas and the tst just grows... and then time runs away. I started of do the same thing as you but got caught by the idea to go thru the configurations as well. Just let me know if you want further ideas / guidance to loop the configurations. I will hold the update of the "potato" example I posted above.

                                           

                                          Just a few comments to your code.

                                          ReDim strEqn(lngCount - 1, 0)

                                          In VBA it is the last dimension you can redim, IF the array has been (re)dimensioned previously. Therefore I would recommend to get used to just redim the last dimension

                                          ReDim strEqn(0, lngCount - 1)

                                          This would make you to change the code in other places as well... just wanted to highlight what I have run into myself.

                                          So far you do not need a multi dimensional array, but you might have plans for it later.

                                           

                                          For i = 0 To lngCount - 1

                                              swEqnMgr.LinkToFile = False

                                          Next

                                          The FOR statement is not need since it will only set the boolean value to False as many times as the count of equations. swEqnMgr.LinkToFile is not dependent on the variable i. Just set swEqnMgr.LinkToFile = False and you will have the same result.

                                           

                                              i = swEqnMgr.Add(i, strEqn(i, 0)) 'Repopulate list of equations

                                          In the title you give a hint that you use this macro in SW2014. The IEquationMgr::Add is obsolete and replaced with IEquationMgr::Add2. And in SW2014 you also have IEquationMgr::Add3.

                                           

                                          When looping thru the configurations you have to catch the suppression state

                                            • Re: Using IEquationMgr in SolidWorks 2014 macro
                                              Mike Campbell

                                              Daniel,

                                               

                                              I like to leave behind something that's altogether correct. Look this over and let me know what you think. Note the IEquationMgr is not involved.

                                              Option Explicit

                                               

                                              'Preconditions: File is open; equations do exist; box is checked next to "Link to external file"

                                              'Postconditions: Box is UNchecked next to "Link to external file"

                                               

                                              Dim swApp         As SldWorks.SldWorks

                                              Dim swModel       As SldWorks.ModelDoc2

                                              Dim swEqnMgr      As EquationMgr

                                              Dim lngCount      As Long

                                              Dim i             As Long

                                              Dim strEqn()      As String

                                              Dim bolStat       As Boolean

                                               

                                              Sub main()

                                               

                                              Set swApp = Application.SldWorks

                                              Set swModel = swApp.ActiveDoc

                                              Set swEqnMgr = swModel.GetEquationMgr

                                               

                                              lngCount = swEqnMgr.GetCount 'Get the number of equations

                                               

                                              ReDim strEqn(lngCount - 1)

                                              For i = 0 To lngCount - 1

                                                  strEqn(i) = swEqnMgr.Equation(i) 'Remember equations

                                              Next

                                               

                                              swEqnMgr.FilePath = "" 'Remove path to referenced text file

                                               

                                              swEqnMgr.LinkToFile = False 'Remove check mark from box

                                               

                                              Do While swEqnMgr.Equation(0) <> ""

                                                  swEqnMgr.Delete (0) 'Delete equations until none remain

                                              Loop

                                               

                                              For i = 0 To lngCount - 1

                                                  swEqnMgr.Equation(i) = swEqnMgr.Add2(i, strEqn(i), True) 'Repopulate list of equations

                                              Next

                                               

                                              bolStat = swModel.ForceRebuild3(True) 'Rebuid model

                                               

                                              End Sub

                                              1 person found this helpful
                                                • Re: Using IEquationMgr in SolidWorks 2014 macro
                                                  Daniel Andersson

                                                  Very good intention to leave something behind if someone else needs to dig into this.

                                                   

                                                  Only comments I have is about the Add2 method and the ForceRebuild3 at the end, and that the suppressions states is not considered for configurations (perhaps you could update the macro with just a comment that configurations is not considered.). I assumed that you have already tested and de-bugged the code, so I skipped that part.

                                                   

                                                  The Solve (last argument in the Add2 method) has following remarks in the API reference:

                                                  Setting the Solve argument to false delays evaluation of this equation, which

                                                  enhances performance when there are many equations to solve. When Solve is set

                                                  to false, the equation appears in the FeatureManager design tree only after

                                                  calling IEquationaMgr::EvaluateAll or

                                                  IModelDoc2::EditRebuild3.

                                                  So I would have set it to False and then call the IModelDoc2::EditRebuild3 method where you do the ForceRebuild3. Sorry if I influenced you with my macro above (which I have removed due to the faulty code I made up, I do not recall the code in details.)

                                                   

                                                  Setting the argument for Solve to False and IModelDoc2::EditRebuild3 at the end would speed up the macro quite much if you have alot of equations to be solved.

                                                   

                                                  Thats all, Glad that you got this working!

                                                   

                                                  P.s I did not think about handling expections, such as if no SW document is open, if drawing is the active document, if a equation fails to be added etc. If you want to discuss that further we could do that with PMs or in a new thread.