Macro Equation

Hello

 

I want to be able to change the value of global variables in equations. (As shown in attachment)

I know how to add by this method:

Dim swEquationMgr As Object

Set swEquationMgr = swModel.GetEquationMgr

swEquationMgr.Add -1, """Mass [kg]""= ""SW-Mass""/100"

How do I modify one that has already been created?

 

Thank you


capture_equation.png

Hello

 

Here's an example from the SolidWorks VBA help:

 

    'Modify dimension equation at index, 1, in all configurations
    longEquation = swEquationMgr. SetEquationAndConfigurationOption(1, """D1@Boss-Extrude1"" = 0.07", swAllConfiguration, Empty)
    If longEquation <> 1 Then ErrorMsg SwApp, "Failed to modify a dimension equation"

 

 

http://help.solidworks.com/2014/english/api/sldworksapi/Add_Equations_Example_VB.htm

1 Like

And a question on this topic about SolidWorks formum talks about it, it uses a split to separate the equation into 2 :

 

  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

 

 

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

1 Like

 

+1 @Lucas Prieur

See EquationMgr interface for interface methods

 

http://help.solidworks.com/2014/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IEquationMgr_members.html?id=4ca69bcb18484952864592709e47c43e#Pg0

 

6 Likes

Just for my personal culture...

 

I don't really understand what Jean bapt means by modifying values?

 

If I click on one of the values, I can change it to my liking...

 

I'd just like to understand this question and its purpose =) (why do vba etc...)

 

As I have no basis in APIs, I think my misunderstanding comes from there. ^^

 

Thank you!

1 Like

@Bart: either it has to modify several files en masse, or its overall value changes according to other parameters internal to the part or assembly or according to an external list (Excel file for example).

My goal is to assemble parts in an assembly and set the height, width, length, and thickness when importing.

This is the best solution I have found.

 

That is to say that on a Userform I enter all this data and I modify it in the equations.

Yes, that's the question I asked a few months ago, except that I didn't get a clear answer^^

 

I understand me.

 

I made an assmebling, and in it I put all my equations that interact in the rooms.

 

The only thing is that I have to click on the sides to change them... And the rest follows, but on, a useform would be + appropriate I think.

I'm in 2012 and there are functions that SW doesn't know.

 

Like SetEquationAndConfigurationOption for example

 

In:

'Modify dimension equation at index, 1, in all configurations
longEquation = swEquationMgr.SetEquationAndConfigurationOption(1, """Height"" = 50", swAllConfiguration, Empty)
If longEquation <> 1 Then ErrorMsg SwApp, "Failed to modify a dimension equation"

 

This link is really not clear...

http://help.solidworks.com/2012/English/api/sldworksapi/Add_Equation_And_Evaluate_All_Example_VB.htm

 

I get to this code that doesn't work.

 

Sub Test()

Dim swApp           As SldWorks.SldWorks

Dim Part            As SldWorks.ModelDoc2

Dim swModel As SldWorks.ModelDoc2

Dim swEquationMgr   As SldWorks.EquationMgr

Dim i As Integer

Dim vSplit As Variant

Const VARIABLE_NAME As String = "Height"

Const NEW_VALUE As String = "50"

 

Set swApp = Application.SldWorks

Set swModel = swApp.ActiveDoc

Set swEquationMgr = swModel.GetEquationMgr

 

 For i = 0 To swEquationMgr.GetCount - 1

        vSplit = Split(swEquationMgr.Equation(i), " = ")

        vSplit(0) = Replace(vSplit(0), Chr(34), Empty)

        If vSplit(0) = VARIABLE_NAME Then _

            swEquationMgr.Equation(i) = Replace(swEquationMgr.Equation(i), vSplit(1), NEW_VALUE)

    Next i

End Sub

 

thank you for your help

 

JB

Hello

I don't know Sw, I work under invent

 

Why not go through an excel sheet that will calculate the dimensions and SW will only have to update it.

 

I don't see the point of going through macros (unless you don't have excel).

 

I set up complete stairs in excel sheets and each piece of inventor updates it with this sheet.

 

Thank you

1 Like

In which line do you encounter an error?

Franck:

Adding the entry of an excel sheet would make the system more complex and I don't want to make it heavier.

All this is done from a Userform to make it more interactive.

 

The thing is that there are no bugs, the macro is realized and nothing changes...

Try removing the _ from the line:

If vSplit(0) = VARIABLE_NAME Then _

 

and puts 

 

swEqnMgr.Equation(i) = Replace(swEqnMgr.Equation(i), vSplit(1), NEW_VALUE)

 

on the next line, followed by an "End if"

 

Then put a stop to this line

swEqnMgr.Equation(i) = Replace(swEqnMgr.Equation(i), vSplit(1), NEW_VALUE)

 

Does your macro stop?

 

Otherwise, you have to go through all the values of your vSplit(0) for example with a debug.print or more with a msgbox!

So it works haphazardly...

 

I noticed that when I go in the management of equations, if the cell activated is for example: =90 of the height. I put OK to close the box.

 

I run the macro and I get in my msgbox:

Height  (i.e. what is sought)

and Length = 70

Width = 95

...

(example on attached image)

If it's the =70 of Length I get Height = 90 and  Length

 

 

Current Code:

Const VARIABLENAME As String = "Height"
Const NEW_VALUE As String = "70"

 

    For i = 0 To swEquationMgr.GetCount - 1
        vSplit = Split(swEquationMgr.Equation(i), " = ")
        vSplit(0) = Replace(vSplit(0), Chr(34), Empty)
        
        MsgBox vSplit(0)
        If vSplit(0) = VARIABLENAME Then
            swEquationMgr.Equation(i) = Replace(swEquationMgr.Equation(i), vSplit(1), NEW_VALUE)
        End If
    Next i


capture.png

Maybe a syntax problem?

 

Try an lcase (lowercase = lowercase) or ucase (uppercase = uppercase) for your two variables, either

 

VARIABLENAME and vSplit(1).

 

And you can put an else to your yew to put a breakpoint there and see in which case it doesn't work.

I didn't quite understand how to write the code...

Like this?

VARIABLENAME = LCase(lowercaser = lowercase)

 

I sorted the name of vSplit(0) into Nom_eq so that I had Height and not Height = 90

Here's my code

NB = Len(VARIABLENAME)

 

For i = 0 To swEquationMgr.GetCount - 1

    vSplit = Split(swEquationMgr.Equation(i), " = ")

    MsgBox vSplit(0)

    vSplit(0) = Replace(vSplit(0), Chr(34), Empty)

    Nom_eq = Left(vSplit(0), NB)

 

    If Nom_eq = VARIABLENAME Then

        swEquationMgr.Equation(i) = Replace(swEquationMgr.Equation(i), vSplit(1), NEW_VALUE)

        'MsgBox vSplit(1)

    Else

    End If

Next i

 

But I have a bug that is displayed on the line

swEquationMgr.Equation(i) = Replace(swEquationMgr.Equation(i), vSplit(1), NEW_VALUE)

(attachment)

 

JB


capture2.png

Hi all.

 

message1/2:

A little in a macro can make it unusable! JB, I looked at your macro and the grain of sand is in the code snippet [vSplit=Split(swEqua.equation(i), " = ")] and more specifically in [" = "].

Explanation: The equations of a SW model are alphanumeric strings, and, like all strings, they can be manipulated by VBA functions present in Word, Excel, Access, .... and SW.

For your code, the Split function will take the content of the ith equation [swEqua.equation(i)], split it in two at the level of [" = "], then as vSplit is a Variant, vSplit(0) will be automatically assigned to the left of [" = "] and vSplit(1) will be automatically assigned to the right of [" = "].

It is for [" = "] that the bottom hurts, because a SW equation can be ""D1@Ext"=50+20" as well as ""D1@Ext" = 50 + 20" or ""D1@Ext"    =    50    +    20". I write this word with a word processor and when using it, I always ask for non-printable characters to be displayed. This allows me not only to see but also to count the four spaces on either side of the equal sign of the writing of the last equation (the one above). SW, he doesn't care or rather manages these superfluous or non-present spaces. Conclusion, the manipulator can add an equation by leaving droppings (double, triple spaces) but you will have to deal with it, and above all, take into account this thorny problem in your programming (your code). In another message my code reviewed, expanded and commented out in the form of a Sub (function) with the name 'ChangeVariableValueInQuaAndRebuildPart' which you call here with another Sub with the name 'try01'. The function 'ChangeVariableValueInQuaAndRebuildPart' requires two arguments that I named 'VarName' and 'ValVar'.

Note: I am attaching in another message a test piece to be opened (it contains the dimensions "D1@Ext" and "D1@Esq".

 

A+

1 Like

Hello again, here is my code and the test part as an example

 

 

Sub essay01()

  ChangeVariableValueInQuaAnd RebuildPart "D1@Ext", "80"

  ChangeVariableValueInQuaAndRebuildPart "D1@Esq", "44.6"

End Sub

 

Sub ChangeVariableValueInQuaAndRebuildPart(NomVar as String, ValVar As String)

 Note: an equation is an alphanumeric string and is treated as such with the usual VBA functions.

 'Declaration Objects *****************************

  Dim swApp As SldWorks.SldWorks

  Dim swModel As SldWorks.ModelDoc2

  Dim swEquaMgr As SldWorks.EquationMgr

 

 'Assignment of Objects*****************************

  Set swApp = Application.SldWorks    ' swApp is now an object containing the SW application.

  Set swModel = swApp.ActiveDoc       ' SwModel is now an object containing the active model (part or assembly) SW.

  Set swEqua = swModel.GetEquationMgr ' swEquaMgr is now an object containing the set of equations of the model present in feature manager.

 

 'Declaration of local variables******************

  Dim NumEqua As Integer 'NumEqua is the Number of the equation

  Dim NbEqua As Integer  'NbEqua is the Number of Equations. If NbEqua=0 then there is an equation, if NbEqua=1 then there are 2 equations, if NbEqua= -1 then there is no equation.

  Dim ChScind As Variant 'ChScind is a Split Chain.

  Const Title01 As String = "Constat   (MC)"

 Variable Allocation

  NbEqua = swEqua.GetCount - 1

 

 'Procedure ************************************

    For NumEqua = 0 TB NbEqua' For NumEqua ranging from 0 to NbEqua (in steps of 1 by default)

         ChScind = Split(swEqua.equation(NumEqua), "=") 'ChScind(0) now contains the part of the equation to the left of the "=" sign. ChScind(1) now contains the part of the equation to the right of the "=" sign

         ChScind(0) = Replace(ChScind(0), Chr(34), Empty) 'Systematic replacement of the first and last character of Scind(0) (with ascii number 34) by nothing (empty), in other words we remove all the chr(34) ["] of Scind(0) (this is a formatting conditioning the following test).

         ChScind(0) = Replace(ChScind(0), Chr(32), Empty) 'Systematic replacement of the characters of Scind(0) (with the ascii number 32) by nothing (empty), in other words we remove all the spaces [" "] of Scind(0) (this is a formatting conditioning the following test).

         If ChScind(0) = VarName, Then

             swEqua.equation(NumEqua) = Replace(swEqua.equation(NumEqua), ChScind(1), ValVar)

             If swModel.EditRebuild3() Then' if the rebuild of the part returns true then

                 MsgBox "Reconstructed part with new value in equation!", vbInformation, Title01

              Else' otherwise

                 MsgBox "Modified equation but part reconstruction error!", vbInformation, Title01

             End If 'end of if

             Exit Sub ' leave this Sub

         End If

    Next NumEqua

   

    If NbEqua = -1 Then

        MsgBox "No equation is associated with the model!", vbExclamation, Title01

    Else

        MsgBox "No equation modification possible!" & Chr$(13) & Chr$(13) & "'" & VarName & "'" & " was not found in any of them.", vbExclamation, Title01

    End If

   

End Sub 'end of sub

 

A+


part_essai01.sldprt
1 Like

Hello Maxime,

 

Your program works great!

What I want to do is more modify the variables to lighten the changes (attachment)

In the meantime I managed to get my code to work:

 

Sub management ()

'Height
Nom_cote = "Height"
Val_cote = UserForm1.TextBox2.Value
Call Test

'Width...

'Depth...

'Thickness...

End Sub

 

Sub Test()

Dim swApp           As SldWorks.SldWorks

Dim Part            As SldWorks.ModelDoc2

Dim swModel         As SldWorks.ModelDoc2

Dim swEquationMgr   As SldWorks.EquationMgr

Dim i               As Integer

Dim vSplit          as Variant

Dim NB              As Integer

Dim Nom_eq          As String

Dim OLD_VALUE       As Integer

 

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Set swModel = swApp.ActiveDoc

Set swEquationMgr = swModel.GetEquationMgr

NB = Len(Nom_cote)

 

For i = 0 To swEquationMgr.GetCount - 1

    vSplit = Split(swEquationMgr.Equation(i), " = ")

    vSplit(0) = Replace(vSplit(0), Chr(34), Empty)

    Nom_eq = Left(vSplit(0), NB)

    OLD_VALUE = Right(vSplit(0), Len(vSplit(0)) - InStrRev(vSplit(0), "="))

    If Nom_eq = Nom_cote Then

        swEquationMgr.Equation(i) = Replace(swEquationMgr.Equation(i), OLD_VALUE, Val_cote)

    Else

    End If

Next i

boolstatus = Part.EditRebuild3()
End Sub

 

As I explained, it works, but I'm afraid that this hacking will be limited.

 

and thank you for your help on the different forums!


equations.zip

Hi JB,

 

Why make it complicated when you can make it simple!

 

Sub management ()

Bottom Height Management
if Test "Height",UserForm1.TextBox2.Value then

        boolstatus = Part.EditRebuild3() 'If Test returns True then Rebuild Part

else

        msgbox "Problem"

end if

' Rq: click behind the 't' in Test and type a space to see that Test requests, in a tooltip, two arguments separated by a comma (help with the prog.). In this way, Test can be in another module without any problem.

'Width...

'Depth...

'Thickness...

End Sub

 

Sub Test(NomVar As String, ValVar As String) As Bolean

Dim swApp As SldWorks.SldWorks

Dim Part As SldWorks.ModelDoc2

Dim swModel As SldWorks.ModelDoc2

Dim swEquationMgr As SldWorks.EquationMgr

Dim i As Integer

Dim vSplit As Variant

 

Set swApp = Application.SldWorks

 

Set Part = swApp.ActiveDoc

Set swModel = swApp.ActiveDoc

Set swEquationMgr = swModel.GetEquationMgr

 

Test=False

For i = 0 To swEquationMgr.GetCount - 1'Know that with each interchange, swEquationMgr.GetCount - 1 is recalculated. It's better to use a local variable like in my macro

    vSplit = Split(swEquationMgr.Equation(i), "=") ' I removed the spaces on each side of the equals sign!!

    vSplit(0) = Replace(vSplit(0), Chr(34), Empty) 'I substitute the quotation marks contained in split(0) with nothing

    vSplit(0) = Replace(vSplit(0), Chr(32), Empty) 'I substitute the spaces contained in split(0) with nothing (I can, because the name of a dimension is necessarily a string without spaces!!)

    If Split(0) = VarName, Then

        swEquationMgr.Equation(i) = Replace(swEquationMgr.Equation(i), Split(1), ValVar) 'I replace Split(1) (the value of the variable NomVar with the value ValVar.

         Test=True

         Exit sub 'reasoning by the absurd, if there are 1000000000000000000000 equations you can go drink coffee even with a 64-bit with multi-body multi-head arithmetic coprocessor.

    End If

Next i


End Sub

 

There may be errors I just typed quickly without testing!!

 

Regards MC