Macro to add equation using named dimension variable

Hi All,

   I am new to macros and am looking for a little help getting a macro to work the way I want it to. I am using an input box for the user to input the sketch number that contains a named dimension I want to use in the equation. I am able to get all of the variables to show up the way I want in the code but do not know how to get them to write into the equation list in my part when I am using the variables. If I hard code the sketch names they write into the equation list fine. I am thinking I do not have the variables called out properly...

Thanks, Mike

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

boolstatus = Part.Extension.SelectByID2("Equations", "EQNFOLDER", 0, 0, 0, False, 0, Nothing, 0)

Dim nmbText As String

    nmbText = InputBox("Specify The Sketch Number") 'user enters sketch number with the named dimensions in it

    Dim nmb As Integer
    Dim wnText As String
    Dim lnText As String
    Dim WText As String
    Dim LText As String

    nmb = CInt(nmbText) 'this is the sketch number
    wn = "W@Sketch" & nmb 'full width sketch name
    ln = "L@Sketch" & nmb 'full length sketch name

Dim swEquationMgr As Object

Set swEquationMgr = Part.GetEquationMgr()

'the following worked as long as my sketch is named Sketch1 frequently this is something other than Sketch1
'swEquationMgr.Add -1, """SquareFeet""= ""W@Sketch1"" * ""L@Sketch1"" / 144"

'the follwing does not work attempting to use variables to make the equation


swEquationMgr.Add -1, """SquareFeet""= wn * nmb / 144"

boolstatus = Part.ForceRebuild()

Part.Save

End Sub

SolidworksApi macros
Answers 8
NP
NP 2017-08-23 Best Answer

Hi Mike,

If you want use variables in the equation then you will need to add them first before you can use them. In your code above, if you want to use 'wn' and 'ln' in the equation, then you will need to add them first using equation manager.

I believe 'swEquationMgr.Add -1, """SquareFeet""= wn * nmb / 144"' should be 'swEquationMgr.Add -1, """SquareFeet""= wn * ln/ 144"'

Hope this helps.

Regards,

ML
ML 2017-08-24

Hello Nilesh,

     Thanks for the reply. Assuming the user entered sketch number 3 in the input box this is what I would like the equation to look like in the equation manager after running this macro  "SquareFeet"= "W@Sketch3" * "L@Sketch3" / 144 I only want to use the variables in the code to combine the dimension name with the sketch number coming from the user input box. is there a way to get the variable values to be used in the formula?      

    nmb = CInt(nmbText)           'this is the sketch number coming from the user input box

    wn = "W@Sketch" & nmb           'full width dimension sketch name

     ln = "L@Sketch" & nmb           'full length dimension sketch name

     swEquationMgr.Add -1, """SquareFeet""= ""wn"" * ""ln""/ 144"      'Need the variable values for wn and ln to be written in the equation

Thanks, Mike

    

  

NP
NP 2017-08-24

Hi Mike,

If you just want to use variables 'wn' and 'ln' for calculation purpose then it is possible but you will have to get the dimension values not the name, but if you want to add an equation  """SquareFeet""= ""wn"" * ""ln""/ 144", then you will need to add 'wn' and 'ln' as global variables in equation manager using IEquationMgr.Add or Add2 method first. Here are some test codes:

<p>Option Explicit</p><p></p><p></p><p>Sub main()</p><p></p><p></p><p>    Dim swApp       As SldWorks.SldWorks</p><p>    Dim swModel     As ModelDoc2</p><p>    Dim swSelMgr    As SelectionMgr</p><p>    Dim swDisDim    As DisplayDimension</p><p>    Dim swDim       As Dimension</p><p>    Dim swEqnMgr    As EquationMgr</p><p>    Dim wn          As Double</p><p>    Dim ln          As Double</p><p>    Dim SqrFeet     As Double</p><p></p><p></p><p>    Set swApp = Application.SldWorks</p><p>    Set swModel = swApp.ActiveDoc</p><p>    </p><p>    'Add equations</p><p>    Set swEqnMgr = swModel.GetEquationMgr</p><p>    Debug.Print swEqnMgr.Add2(-1, """SquareFeet"" = ""W@Sketch1"" * ""L@Sketch1"" / 144", False)</p><p>    Debug.Print swEqnMgr.Add2(-1, """wn""=""W@Sketch1""", False)</p><p>    Debug.Print swEqnMgr.Add2(-1, """ln""=""L@Sketch1""", False)</p><p>    Debug.Print swEqnMgr.Add2(-1, """SquareFeet1"" = ""wn"" * ""ln"" / 144", False)</p><p>    swEqnMgr.EvaluateAll</p><p>    </p><p>    'Calculate SquareFeet using variable.</p><p>    Set swSelMgr = swModel.SelectionManager</p><p>    </p><p>    'Select "W@Sketch"</p><p>    swModel.Extension.SelectByID2 "W@Sketch1", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0</p><p>    Set swDisDim = swSelMgr.GetSelectedObject6(1, -1)</p><p>    Set swDim = swDisDim.GetDimension2(0)</p><p>    Debug.Print swDim.FullName</p><p>    wn = swDim.GetSystemValue2("") * 1000</p><p>    swModel.ClearSelection2 True</p><p>    </p><p>    'Select "L@Sketch"</p><p>    swModel.Extension.SelectByID2 "L@Sketch1", "DIMENSION", 0, 0, 0, False, 0, Nothing, 0</p><p>    Set swDisDim = swSelMgr.GetSelectedObject6(1, -1)</p><p>    Set swDim = swDisDim.GetDimension2(0)</p><p>    Debug.Print swDim.FullName</p><p>    ln = swDim.GetSystemValue2("") * 1000</p><p>    swModel.ClearSelection2 True</p><p>    </p><p>    'Calculates Square Feet</p><p>    SqrFeet = wn * ln / 144</p><p>    </p><p>    Debug.Print SqrFeet</p><p>    </p><p>    swModel.EditRebuild3</p><p>End Sub</p>

Assuming that you have 'Sketch1' and two dimensions 'W@Sketch1' and 'L@Sketch1'. The first equation 'Debug.Print swEqnMgr.Add2(-1, """SquareFeet"" = ""W@Sketch1"" * ""L@Sketch1"" / 144", False)' will be added without any trouble and you will see '0' printed in the immediate window which is the index of the equation (0 based).

If you comment out next two lines 'Debug.Print swEqnMgr.Add2(-1, """wn""=""W@Sketch1""", False)' and 'Debug.Print swEqnMgr.Add2(-1, """ln""=""L@Sketch1""", False)' and then try to add equation 'Debug.Print swEqnMgr.Add2(-1, """SquareFeet1"" = ""wn"" * ""ln"" / 144", False)', it will fail and print '-1' in immediate window. To add this equation you must have to un-comment other two lines.

Second part of the macro gets dimension values of both dimensions 'W@Sketch1' and 'L@Sketch1', assigns them to 'wn' and 'ln' variables and then calculates square feet.

Hope this is clear.

ML
ML 2017-09-07

Nilesh,

    Sorry for not getting back to you sooner.... When we use this macro the sketch name may not always be sketch 1.  I am trying to use an input box to have the user enter the sketch number that is being used and put that into our square foot equation.

Thanks for your help!

DG
DG 2017-09-07

You can use InputBox and then use the value for the sketch number.

<p>Dim sNumber As String</p><p>sNumber = InputBox("Enter Sketch Number", "Enter Sketch Number", sNumber)</p><p>sNumber = "Sketch" & sNumber</p>

And now replace Sketch1 with sNumber

ML
ML 2017-09-07

Deepak,

I don't think I have the formatting correct to use the sNumber variable in the code where the equations are being added. How should that be called out in the code?

sNumber = InputBox("Enter Sketch Number", "Enter Sketch Number", sNumber)
    sNumber = "Sketch" & sNumber

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

    'Add equations
    Set swEqnMgr = swModel.GetEquationMgr
    Debug.Print swEqnMgr.Add2(-1, """SquareFeet"" = ""W@sNumber"" * ""L@sNumber"" / 144", False)
    Debug.Print swEqnMgr.Add2(-1, """wn""=""W@sNumber""", False)
    Debug.Print swEqnMgr.Add2(-1, """ln""=""L@sNumber""", False)
    Debug.Print swEqnMgr.Add2(-1, """SquareFeet1"" = ""wn"" * ""ln"" / 144", False)
    swEqnMgr.EvaluateAll

Thanks, Mike

NP
NP 2017-09-07

Hi Mike,

Try Following:

<p>Option Explicit</p><p></p><p>Sub main()</p><p></p><p>    Dim swApp      As SldWorks.SldWorks</p><p>    Dim swModel    As ModelDoc2</p><p>    Dim swSelMgr    As SelectionMgr</p><p>    Dim swDisDim    As DisplayDimension</p><p>    Dim swDim      As Dimension</p><p>    Dim swEqnMgr    As EquationMgr</p><p>    Dim sNumber    As String</p><p>    Dim sWidth      As String</p><p>    Dim sLength    As String</p><p>    Dim sSqFeet    As String</p><p></p><p>    sNumber = InputBox("Enter Sketch Number", "Enter Sketch Number", sNumber)</p><p>    sWidth = Chr(34) & "W@Sketch" & sNumber & Chr(34)</p><p>    sLength = Chr(34) & "L@Sketch" & sNumber & Chr(34)</p><p>    sSqFeet = sWidth & "*" & sLength & "/144"</p><p></p><p>    Set swApp = Application.SldWorks</p><p>    Set swModel = swApp.ActiveDoc</p><p>     </p><p>    'Add equations</p><p>    Set swEqnMgr = swModel.GetEquationMgr</p><p>    Debug.Print swEqnMgr.Add2(-1, """SquareFeet"" =" & sSqFeet, False)</p><p>    Debug.Print swEqnMgr.Add2(-1, """wn""=" & sWidth, False)</p><p>    Debug.Print swEqnMgr.Add2(-1, """ln""=" & sLength, False)</p><p>    Debug.Print swEqnMgr.Add2(-1, """SquareFeet1"" = ""wn"" * ""ln"" / 144", False)</p><p>    swEqnMgr.EvaluateAll</p><p>     </p><p>    swModel.EditRebuild3</p><p>End Sub</p>
ML
ML 2017-09-08

That works

Thanks for all of the help!