How to calculate string ("1+2") with API?

In API of Solidworks, with excel function. Calculate Str.

Private Sub del()

   Dim Str

      Str = "1 + 2"

   Dim Xls As Excel.Application

      Set Xls = GetObject(, "Excel.Application")

      Debug.Print Xls.Evaluate(Str)

      Stop

   Dim SwApp As SldWorks.SldWorks          

End Sub

Help me. How to calculate the string of arithmetic.

SolidworksApi macros
Answers 4
JB
JB 2017-07-22 Best Answer

Please see usage of "Evaluate" function at Distance Mate Macro - DASSAULT: SOLIDWORKS 3D Design - Eng-Tips (near end of discussion)

YN
YN 2017-07-22

using the macro and (unthinkingly) tried to enter a mathematical expression rather than a straight number.  Obviously, that failed.  But I found the undocumented function "Evaluate". \

don't string mathermatical . "1+2"

JC
JC 2017-09-15
<p>Dim swApp As SldWorks.SldWorks</p><p>Sub main()</p><p></p><p></p><p>Set swApp = Application.SldWorks</p><p>Dim SwModel As ModelDoc2</p><p>Set SwModel = swApp.ActiveDoc</p><p>Dim Thisval As Double</p><p>Thisval = GetValueFromText(SwModel, "1 + 2 *4")</p><p>End Sub</p><p></p><p></p><p></p><p></p><p>Function GetValueFromText(ByVal SwModel As ModelDoc2, ByVal ThisTxt As String) As Double</p><p>    Dim Retval As Double</p><p>    Dim UserUnit As UserUnit</p><p>    Set UserUnit = SwModel.GetUserUnit(swUserUnitsType_e.swLengthUnit)</p><p>    UserUnit.ConvertToSystemValue ThisTxt, Retval</p><p>  </p><p>    Retval = UserUnit.ConvertToUserUnit(Retval, False, False)</p><p>    Debug.Print "Value evaluates to " & Retval</p><p>    GetValueFromText = Retval</p><p>End Function</p>
MC
MC 2017-09-18

Depending on what you are trying to accomplish there is a method of SldWorks.IUserUnit that may be of interest:

IUserUnit.ConvertToSystemValue

Converts a text string to a system value.  This takes the value directly from a textbox control and passes it in to convert to a double value. This imitates the behavior of when working in the SolidWorks user interface and passing a value like "10 / 2" into one of the values on a dialog box.