Macro Equation

Hello again everyone,

sorry to publish without testing, so I'm following up

 

 

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

 

 

Sub Management()

  assignmentPrincipalObjects

Bottom Height Management

  If Test("D1@Esq1", "500") Then

    boolstatus = swModel.EditRebuild3()

  Else

    MsgBox "Problem"

  End If

 

'Width...

'Depth...

'Thickness...

 

End Sub

 

Sub AssignmentPrincipalObjects() 'Can be placed in another module without PB

  Set swApp = Application.SldWorks

  Set swModel = swApp.ActiveDoc

 'Set= .....

End Sub

 

Function Test(NomVar As String, ValVar As String) As Boolean 'Can be placed in another module without PB

  Dim swEquationMgr As SldWorks.EquationMgr

  Dim vSplit as Variant

  Set swEquationMgr = swModel.GetEquationMgr

  Test = False

  For i = 0 To swEquationMgr.GetCount - 1

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

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

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

    If vSplit(0) = VarName, Then

      swEquationMgr.equation(i) = Replace(swEquationMgr.equation(i), vSplit(1), ValVar)

      Test = True

      Exit Function

    End If

  Next i

End Function

 

I just tested and quickly reworked this code, it's up to you JB to 'put it in your own way'.

Using functions (here Test)  with passing arguments (here NomVar and ValVar) allows you to organize your things in several modules. Regarding the sub entitled Management, I can see it in a module of your UserForm, to be seen.

 

Regards MC

Good evening everyone,

 

I come back to the sentence "Concerning the sub entitled Management, I can see it in a module of your UserForm, to see." of my last word. You should know that your [UserForm1.TextBox2.Value] (the value of the Name control 'TextBox2' of your custom Name dialog box 'Userform1') does not have a large range (see scope of the API help). It won't be known if you call it from code placed in a different module than the one associated with your [UserForm1]. Well now it's the case for a macro in Word but I haven't checked with SW. Finally, [UserForm1] and [TextBox2] are default names, it would be wise when you create a dialog box to rename them immediately, for example [UserForm1] rename to [BDChoisirBackground] and [TextBox2] rename to [BottomHeight]. I know it's titillating but your code will only be clearer.

 

Happy Prog

 

A+

 

MC

Maxim

 

Is it normal that this macro only works in a part file and not in an edited part of an assembly?

 

The loop is not done in an edited piece:

For i = 0 To swEquationMgr.GetCount - 1
    vSplit = Split(swEquationMgr.Equation(i), "=")
    vSplit(0) = Replace(vSplit(0), Chr(34), Empty)
    vSplit(0) = Replace(vSplit(0), Chr(32), Empty)
    If vSplit(0) = VarName, Then
        swEquationMgr.Equation(i) = Replace(swEquationMgr.Equation(i), vSplit(1), ValVar)
        Modif_equation = True
        Exit Function
    End If
Next i

 

Thank you