I have an ilogic external rule which contains functions that I use from rules in my local assembly document. I'm trying to access the value of a parameter in the document from the external rule's function. Attached is a simple version of what I'm trying to do. If I manually run the external rule, it seems to work just fine, but when I call on the function from a seperate rule, its unable to get the parameter value. I get an error stating, "'Parameter' is not declared. It may be inaccessible due to its protection level."
Any ideas?
'this is the external rule. if I run this manually, it works Public Class test_functions Sub Main myresult = "" thisisatest(myresult) MsgBox(myresult) End Sub Function thisisatest(ByRef resultcreated As String) resultcreated = "it worked - " & Parameter("ParameterInDocument") End Function End Class
'this is my rule in the assembly document trying to use the external rule's function AddVbFile "z_testrule.iLogicVb" Dim external_rule As New test_functions myresult = "" external_rule.thisisatest(myresult) MsgBox(myresult)
I think you'll need to declare and pass in the iLogic Parameter interface.
In your external rule you'll need to declare it, and add a New() constructor :
Public Class test_functions
Private Parameter As IParamDynamic
Sub New(Parameter As IParamDynamic) Me.Parameter = Parameter End Sub
Function thisisatest(ByRef resultcreated As String) resultcreated = "it worked - " & Parameter("d18") Return resultcreated End Function End Class
Then when you call it, pass in the Parameter interface :
Dim external_rule As New test_functions(Parameter)
Seemed to work on my quick test.
Cheers
Tom
Hi Dave,
I got the feedback that the solution from Thomas is probably the best - thanks Thomas! 🙂
As a last resort, if you can definitely not change the code which is using the external functions, you could get access to the ThisApplication object using GetObject() - this will have issues if multiple instances of Inventor are open, as this will always return the first opened instance.
Public Class test_functions Private Parameter As IParamDynamic Private ThisApplication As Object Sub Main End Sub ' First created this just so that clicking OK when editing the rule will ' not cause an error, but then also added ThisApplication ' that will provide access to the Inventor API Sub New() ThisApplication = GetObject(,"Inventor.Application") End Sub Sub New(Parameter As IParamDynamic) Me.Parameter = Parameter End Sub Function thisisatest(ByRef resultcreated As String) resultcreated = "it worked - " & Parameter("d0") Return resultcreated End Function Function thisisatest2(ByRef resultcreated As String) doc = ThisApplication.ActiveDocument ' this way the values will be in the internal units ' e.g. length is in cm, so you need to convert it as needed ' You could use the UnitsOfMeasure class for help resultcreated = "it worked - " & doc.ComponentDefinition.Parameters("d0").Value Return resultcreated End Function End Class
Then you can call those functions like this:
AddVbFile "test.iLogicVb" ' first test Dim external_rule As New test_functions(Parameter) myresult = "" external_rule.thisisatest(myresult) MsgBox(myresult) ' second test Dim external_rule2 As New test_functions() myresult = "" external_rule2.thisisatest2(myresult) MsgBox(myresult)
I hope this helps.
Cheers,