Parameter in ilogic classes

Parameter in ilogic classes

johnster100
Collaborator Collaborator
1,393 Views
5 Replies
Message 1 of 6

Parameter in ilogic classes

johnster100
Collaborator
Collaborator

Hi,

i've written a rule which contains some classes. I have an issue where ilogic is not recognising the parameters unless they are in the class ThisRule.

 

Is there a way around this?

 

I've created a simple example which hopefully demonstrates what I 'm trying to say (it's attached).

 

Here is the code:

Public Class ThisRule
	Sub main
		
		Call ParameterTest.TestParam()
		
	End Sub
End Class

Public Class ParameterTest
	
	Public Shared Sub TestParam()
		msgbox(Test)
	End Sub
	
	
End Class

 

When executed the messagebox is empty (The parameter call Test = 1).

 

thanks,

John

0 Likes
1,394 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

I'm not sure if this will help you or not, but I believe the Class "ThisRule" is implied in all ilogic rules, unless otherwise specified.  So any time you don't enclose your code within a Class of its own, it is implied that it is within the "ThisRule" class.  Same with the Sub Main ... End Sub, if you don't enclose your code within them (and don't use any other Subs or Functions), they are implied for you.  So, unless the "Straight VB Code" option is turned on for that iLogic rule, you don't really need to call-out the ThisRule class.

   So you will just have to do some extra steps to make it work.  Basically, it is no longer recognizing that local parameter as it normally would.  So you have to dig down to that parameter the long way.  You have to define the Inventor application (without using ThisApplication, because that was under the ThisRule class, which is not available within your other class), then define the UserParameter object in which your value is contained, then set its value as the input to the MsgBox().

   If I just put a regular String value (enclosed in quotation marks "") it will work too, because it's not trying to access a parameter.

   Here's a working example of your code:

 

Class ThisRule
	Sub Main
		ParameterTest.TestParam()
	End Sub
End Class

Class ParameterTest
	Shared Sub TestParam()
		'MsgBox(Test)   'doesn't work because it doesn't recognize the local parameter this way
		
		'define & get the (already running) Inventor Application
		Dim oInv As Inventor.Application = GetObject(, "Inventor.Application")
		'define & get the UserParameter
		Dim oParam As Inventor.UserParameter = oInv.ActiveDocument.ComponentDefinition.Parameters.UserParameters.Item("Test")
		'put its value as the input to the MsgBox
		MsgBox(oParam.Value) 'does work
		'or simply use a regular String value like this
		'MsgBox("Test") 'does work
	End Sub
End Class

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

WCrihfield
Mentor
Mentor

@johnster100

Also notice that the same code also works when you comment out the lines "Class This Rule" & "End Class" surrounding the first sub, because, as I stated earlier, the class "ThisRule" is already implied.

 

'Class ThisRule
	Sub Main
		ParameterTest.TestParam()
	End Sub
'End Class

Class ParameterTest
	Shared Sub TestParam()
		'MsgBox(Test)   'doesn't work because it doesn't recognize the local parameter this way
		
		'define & get the (already running) Inventor Application
		Dim oInv As Inventor.Application = GetObject(, "Inventor.Application")
		'define & get the UserParameter
		Dim oParam As Inventor.UserParameter = oInv.ActiveDocument.ComponentDefinition.Parameters.UserParameters.Item("Test")
		'put its value as the input to the MsgBox
		MsgBox(oParam.Value) 'does work
		'or simply use a regular String value like this
		'MsgBox("Test") 'does work
	End Sub
End Class

 

 If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click 'LIKE' 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 6

DRoam
Mentor
Mentor

I believe this is a limitation of iLogic. The short answer is, those blue parameters won't work anywhere outside of ThisRule, or even in a sub-class of ThisRule. Read on below if you'd like more explanation.

 

First, I don't think there's usually any reason to explicitly declare the "ThisRule" class. Some of the special iLogic functions and variables are declared in the ThisRule class, so declaring anything outside of it just increase your chances of those iLogic functions not working. I would just avoid declaring ThisRule explicitly, and then all of your functions should have access to all iLogic functionality. That said, there are exceptions, and you've run into one of them.

 

I said above that all of your functions should have access to all iLogic functionality, and this is true. However, what you're trying to do is have another class access special iLogic functionality (either a child class, if you put it inside of "ThisRule", or a separate class entirely if you put it outside). Some special iLogic functionality is accessible, in a roundabout way, from another class, but the "blue parameter" functionality, I'm fairly certain, is not.

 

Those blue parameters are a special iLogic thing that don't end up looking anything like what you see in your rule after the iLogic system adds its helper code and re-complies. They're basically a renamed to a different variable that's declared within the ThisRule class. The variable gets written to (with the Parameter's value) when the rule starts, and then gets read from (to store as the Parameter's new value) when the rule is finished.

 

The problem is, you can't pre-emptively "guess" at what the variable's new name will be and use it elsewhere besides the ThisRule class. Even if you do, the iLogic system will detect this and just name the new variable something else.

 

So the short answer is, using those blue parameters outside of ThisRule (or in a sub-class of ThisRule) is, unfortunately, not possible.

0 Likes
Message 5 of 6

johnster100
Collaborator
Collaborator

thanks a lot for the replies guys.

 

I'll change my code so what I need to read the parameters is in the ThisRule class. 

 

thanks again,

John

0 Likes
Message 6 of 6

DRoam
Mentor
Mentor

That said, you certainly have other options.

 

One is the "straight-API" method that @WCrihfield gave above. This doesn't use any of the special iLogic functionality that you may be familiar/comfortable with, but it certainly works.

 

However, there's another way that does allow you to use most iLogic functionality within a custom class: while a sub-class can't use iLogic functions directly, it can use them with a little help.

 

Below is an example of what's probably the simplest way to do so. I won't go into explaining the nitty-gritty of how it works; all you need to know is, if you have any sub-classes in your rule, you need to do three things:

 

  1. Add the "ClassHelper" class outside of "Sub Main".
  2. Add the "ClassHelper.ParentRule = Me" line at the very top of "Sub Main".
  3. Add the "Inherits ClassHelper" line at the very top of each sub-class.

And that's it. Now helpful iLogic features like "ThisApplication", "ThisDoc", and Parameter()" will work in your sub-classes! This could be very handy if you don't want to take the time to implement your own "straight-API" version of those functions. (Unfortunately, the "blue parameters" still won't work in sub-classes, but at least it's better than nothing.)

 

Hope this is helpful!

 

Sub Main
	ClassHelper.ParentRule = Me

	Call ParameterTest.TestParam
	Call AnotherClass.ShowInfo
End Sub

Public Class ParameterTest
	Inherits ClassHelper

	Public Shared Sub TestParam()
		MsgBox(Parameter("Test"))
	End Sub
End Class

Public Class AnotherClass
	Inherits ClassHelper

	Public Shared Sub ShowInfo()
		MsgBox(ThisDoc.Document.DisplayName)
		MsgBox(ThisApplication.Documents.Count)
	End Sub
End Class

Public MustInherit Class ClassHelper
	Public Shared ParentRule As ThisRule

	Public ReadOnly Shared Property ThisApplication As Inventor.Application
		Get
			Return ParentRule.ThisApplication
		End Get
	End Property

	Public ReadOnly Shared Property ThisDoc As ICadDoc
		Get
			Return ParentRule.ThisDoc
		End Get
	End Property

	Public ReadOnly Shared Property Parameter As IParamDynamic
		Get
			Return ParentRule.Parameter
		End Get
	End Property
End Class