Using the eval function

Using the eval function

pball
Mentor Mentor
1,699 Views
11 Replies
Message 1 of 12

Using the eval function

pball
Mentor
Mentor

Unfortunately it seems the eval function is not availble in inventor's vba. I know other programs with vba can use that function so I'm wondering if it's possible to make it accessable in inventor vba. I tried loading references to some microsoft office libraries because I know excel and other office programs can use eval, but that doesn't work. Anyone have a solution for this, I've already had to use work arounds to not being able to use eval before.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
1,700 Views
11 Replies
Replies (11)
Message 2 of 12

jdkriek
Advisor
Advisor

Add reference to MS Script Control

 

Public Sub eValTest()
    'Add ref to MS Script Control
    Dim oSC As ScriptControl
    Set oSC = New ScriptControl
    oSC.Language = "VBScript"
    Dim sResult As String
    sResult = oSC.eVal("4*5")
    MsgBox sResult, vbOKOnly, "Result"
End Sub
Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


0 Likes
Message 3 of 12

pball
Mentor
Mentor

Thanks, your example works perfectly. Though what I'm trying to do isn't working so well.

 

I'm trying to dynamically call a function and I'm getting a type mismatch error on "etest". Would you happen to know why this happening.

 

Public Sub et()
    'Add ref to MS Script Control
    Dim oSC As ScriptControl
    Set oSC = New ScriptControl
    oSC.Language = "VBScript"
    Dim fname As String
    fname = "etest()"
    noop = oSC.Eval(fname)
End Sub

Public Function etest()
    Debug.Print "works"
End Function

 

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
Message 4 of 12

jdkriek
Advisor
Advisor

Here you go:

 

Public Sub et()
    'Add ref to MS Script Control
    Dim oSC As New ScriptControl
    oSC.Language = "VBScript"
    fname = oSC.Eval(etest())
End Sub

Public Function etest()
    Debug.Print "works"
End Function
Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


0 Likes
Message 5 of 12

pball
Mentor
Mentor

I'm sorry but doing it that was doesn't help me. I'm looking to call a function which name is in a variable. Basically I'd like a function which can in turn call another function which name was passed to the first function as a variable

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
Message 6 of 12

jdkriek
Advisor
Advisor

I see now, just take the quotes off here:

 

Public Sub et()
    'Add ref to MS Script Control
    Dim oSC As New ScriptControl
    oSC.Language = "VBScript"
    Dim fname As String
    fname = etest() 'no quotes
    noop = oSC.Eval(fname)
End Sub

Public Function etest()
    Debug.Print "works"
End Function
Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


Message 7 of 12

pball
Mentor
Mentor

Wow that was simple. I feel like banging my head on my desk now, it also might wake me up some lol. It should be much easier and simpler to remake this recursive function I have which calls other functions to do stuff.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
Message 8 of 12

jdkriek
Advisor
Advisor

I agree, it should be easier - thier scope of VBA is limited.

Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


0 Likes
Message 9 of 12

pball
Mentor
Mentor

Well I'm sorry to say but I found an issue with this. The eval function works but my end goal isn't working correctly.

 

Public Sub et()
    'Add ref to MS Script Control
    Dim oSC As New ScriptControl
    oSC.Language = "VBScript"
    Dim fname As String
    fname = etest() 'no quotes
    Debug.Print "shouldn't run etest yet"
    noop = oSC.Eval(fname)
End Sub

Public Function etest()
    Debug.Print "works"
End Function

 The above code appears to work but it doesn't in the way intended. I want the function to be called stored in a variable and executed later. But this code is executing the function when trying to add it to the variable. When I run this code the function etest runs before the debug.print line which shows the eval line isn't where it's being evaluated.

 

All the examples I've found set the function to be called as a string in a variable and use that variable in the evaluate function. That results in a type mismatch error when I try that. I'm wondering if things work differently in programs that have a native evaluate function and it just won't work in inventor.

Check out my style edits for the Autodesk forums
pball's Autodesk Forum Style

Userscript to edit forum links to jump to first unread post
Jump To First Post Userscript
0 Likes
Message 10 of 12

jdkriek
Advisor
Advisor

Indeed, for the life of me I don't understand why Inventor VBA just doesn't have a Eval() function built-in.

 

I mean AutoCAD VBA does.

Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


0 Likes
Message 11 of 12

Wannes.V
Enthusiast
Enthusiast

Hi,

 

What do I need to import to use eval function with ilogic?

 

 

Imports Microsoft.VisualBasic.???

 

what does 

'Add ref to MS Script Control
mean?

Looking for a easy way to calculate math expression string to numeric value.

 

0 Likes
Message 12 of 12

WCrihfield
Mentor
Mentor

Hi @Wannes.V.  Maybe the following tool will work better for you in an iLogic rule.

UnitsOfMeasure.GetValueFromExpression 

The UnitsOfMeasure object can be accessed either directly from the main Inventor Application, of from an Inventor Document object directly.  But keep in mind that the value it gives you will always be in 'database units', which may be different from your default document units.  For example, centimeters is the database unit for length or distance, and radians is the database unit for angles, and so on.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes