Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Use function in external rule

3 REPLIES 3
Reply
Message 1 of 4
dave
707 Views, 3 Replies

Use function in external rule

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)
3 REPLIES 3
Message 2 of 4
thomaskennedy
in reply to: dave

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

Message 3 of 4
dave
in reply to: thomaskennedy

Having trouble making it work. I copied/pasted everything here. Not sure why, maybe a spacing thing? (I'm a little new to the programming world and can't really tell for sure)

Also, I'm trying to avoid editing the code where I'm calling the function. Problem I'm trying to get around - I have quite a few models stored using this external function and I'm hoping to not have to open all of them individually and edit. Otherwise I would just pass the parameter value I need right into the function.

Thx,
Dave
Message 4 of 4
adam.nagy
in reply to: dave

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,



Adam Nagy
Autodesk Platform Services

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report