Modifying the Value of a Global Variable via a Macro on Solidworks

Hello

I use an inputbox dialog to request the height of an element (value saved under Height)

I would like my global variable which is also called Height to take this value via a macro. What line of code allows this?

Thank you for your feedback

Hello 

This Code should help you. 

Hello

I'm sorry but I'm going around in circles. I can't do it. I went through the code, but I don't understand much about it... I admit that a helping hand would be appreciated.

Dim Hauteur_Value As Integer

Dim Double Height

Hauteur_Value = InputBox("Please specify the Height .... ")

So I have a global variable called "Height". And I just want this variable to take the new value (Hauteur_Value).

Thank you for your enlightenment

Hello 

Attached is an approach, (to introduce decimals you must use the vergo not the dot), 

On the other hand, the selected function seems to work only with integers!? . 

 


img_20210915_164115.jpg
1 Like

Hello

EDIT: deletion of the code because it duplicates the answer of @Lynk

Kind regards

Hello

After a few bumps in my previous answer, here is a functional code based on Lynk's but which allows you to put a decimal value, a value that can be entered in the InputBox either with a period or with a comma:

Dim Variable As String
Dim Hauteur_Value As String
Dim ValeurText As String

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2

Sub main()
    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc
    
    Variable = "hauteur"

    Hauteur_Value = InputBox("Merci d'indiquer la Hauteur .... ")
    Hauteur_Value = Replace(Hauteur_Value, ".", ",")
    If IsNumeric(Hauteur_Value) Then
        ValeurText = Replace(Hauteur_Value, ",", ".")
    Else
        MsgBox "Valeur invalide"
        Exit Sub
    End If
    
    If Not swModel Is Nothing Then
        Dim swEqMgr As SldWorks.EquationMgr
        Set swEqMgr = swModel.GetEquationMgr
        If SetEquationValue(swEqMgr, Variable, ValeurText) Then
            swModel.ForceRebuild3 True
        Else
            MsgBox "Failed to find the equation " & name
        End If
    Else
        MsgBox "Please open the model"
    End If

End Sub

Function SetEquationValue(eqMgr As SldWorks.EquationMgr, name As String, value As String) As Boolean
    Dim index As Integer
    index = GetEquationIndexByName(eqMgr, name)
    If index <> -1 Then
        eqMgr.Equation(index) = """" & name & """=" & """" & ValeurText & """"
        SetEquationValue = True
    Else
        SetEquationValue = False
    End If
End Function

Function GetEquationIndexByName(eqMgr As SldWorks.EquationMgr, name As String) As Integer
    Dim i As Integer
    GetEquationIndexByName = -1
    For i = 0 To eqMgr.GetCount - 1
        Dim eqName As String
        eqName = Trim(Split(eqMgr.Equation(i), "=")(0))
        eqName = Mid(eqName, 2, Len(eqName) - 2)
        If UCase(eqName) = UCase(Variable) Then
            GetEquationIndexByName = i
            Exit Function
        End If
    Next
End Function

Kind regards

1 Like

A big thank you to the 2 of you for your feedback.

Roger, it's great what you sent me. Just 2 fixes to make:

If UCase(eqName) = UCase(name) Then  '********************** instead of Variable

eqMgr.Equation(index) = """" & name & """=" & """" & Value & """"                    ' ***************************instead of TextValue

 

core thank you!