OnParameterChange Event in iLogic

OnParameterChange Event in iLogic

Anonymous
Not applicable
2,618 Views
12 Replies
Message 1 of 13

OnParameterChange Event in iLogic

Anonymous
Not applicable

I would like to instantiate a custom class object when an inventor iLogic form is launched. From there, I would like to use a number of class methods based on any number of specific parameters changing. 

 

Is there a way to use a While loop to check if the iLogic form is open? As long as the iLogic form is open, I would like to monitor for specific parameter changes and execute class methods when they change.

 

Is there a way to use the ModelingEvents.OnParameterChange event in iLogic and, if so, would someone mind showing a quick example?

0 Likes
Accepted solutions (1)
2,619 Views
12 Replies
Replies (12)
Message 2 of 13

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@Anonymous,

 

Try below iLogic code to trigger OnParameterChange event. After running iLogic rule, it starts to listen OnParameterChange event. On changing parameter in "Parameter" dialog, message box will prompt and shows before and after values of "Parameter".

 

Sub Main()
Dim oModelEvent As ModelingEvents
oModelEvent = ThisApplication.ModelingEvents

AddHandler oModelEvent.OnParameterChange, AddressOf oModelEvent_ParameterChange
End Sub

Sub oModelEvent_ParameterChange(DocumentObject As _Document, Parameter As Parameter, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)

If BeforeOrAfter = kBefore Then
MessageBox.Show("Before changing parameter : " & Parameter.Value)
ElseIf BeforeOrAfter = kAfter Then
MessageBox.Show("After changing parameter : " & Parameter.Value)
End If

End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 3 of 13

Anonymous
Not applicable

Thanks. Exactly what I was looking for.

0 Likes
Message 4 of 13

Anonymous
Not applicable

One quick question... should I explicitly destroy these objects before Main() is finished executing? This rule only needs to be run once to watch the .OnParameterChange event, I'm unsure if consistently running a rule containing this code would cause any kind of performance issues. Or, perhaps, I create a separate rule containing this code to run when the document opens... but then in my other rules, would I still be able to access the same ParameterChange event?

0 Likes
Message 5 of 13

MjDeck
Autodesk
Autodesk

It's possible to do this in iLogic, but there are limits on what you can do in the event handler. Here's a sample rule that will add the handler, and only respond to parameter changes while a form named "Form 1" is running. You can edit the code to change the name of the form.
Run the separate rule OnParameterChangeRemoveHandler to remove the event handler.

Because it uses Logger functions, this sample requires Inventor 2019. But if you remove the Logger functions and objects, it should run on 2018 and earlier.


Mike Deck
Software Developer
Autodesk, Inc.

Message 6 of 13

Anonymous
Not applicable

Thanks, Mike!

0 Likes
Message 7 of 13

Anonymous
Not applicable

Mike,

 

 Do you know why the message box will not show for BeforeOrAfter = kBefore?

 

Shared Sub oModelEvent_ParameterChange(DocumentObject As Document, Parameter As Parameter, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
If FormIsRunning("Form 1") Then
	If BeforeOrAfter = kBefore Then
		MessageBox.Show("Before changing parameter: " & Parameter.Value)
	ElseIf BeforeOrAfter = kAfter Then
		StaticLogger.Info("After changing parameter {0} = {1}", Parameter.Name, Parameter.Value)
	End If
End If
End Sub

I thought that a parameter change event was triggered both before and after the parameter is changed? I see the parameter name and value in the logger, but nothing for the kBefore in a messagebox. 

0 Likes
Message 8 of 13

Anonymous
Not applicable

It seems, for some reason, that MessageBox.Show() function will not execute from an external rule.. ? Events for both kBefore and kAfter are, in fact, executed, but the MessageBox.Show() function doesn't run from the external rule.

0 Likes
Message 9 of 13

MjDeck
Autodesk
Autodesk

Every time you change the OnParameterChangeIfForm rule, you have to run the OnParameterChangeRemoveHandler rule.
This is the best way I could think of to do it. Otherwise, you can end up with more than one event handler running at the same time. Every time you edit the rule and it actually creates a new event handler, the old one will be still in memory, but there's no way to get to it.

So before you edit the OnParameterChangeIfForm rule, run the OnParameterChangeRemoveHandler rule.

If you miss the remove handler step, the only way to get rid of the old handler is to shut down and restart Inventor. You can save your work before shutting down, no problem there.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 10 of 13

Anonymous
Not applicable

Got it. Thanks!

0 Likes
Message 11 of 13

b.vonklass
Participant
Participant

I would like to pick this topic up again. Since I am new here, I am not sure if this is the right place or way to do that.

 

The ability to monitor the value of a Parameter before and after a change is made is exactly what I'm looking for. So far so good. Is it possible to adapted the code so that the OnParameterChange event is executed only for the change of a SPECIFIC user defined parameter and not all parameters? Has anybody an idea on that?!?

0 Likes
Message 12 of 13

MjDeck
Autodesk
Autodesk

Yes, you can do it by checking the parameter name in the event handler. The event handler will still be executed for all parameters, but you don't have to do anything with it if it's not the parameter you want. Here's a sample for a parameter named "Length".
Change the line:

If FormIsRunning("Form 1") Then

to

If FormIsRunning("Form 1") And Parameter.Name = "Length" Then

 


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 13 of 13

BLA_fpineda
Enthusiast
Enthusiast

Hi there,

 

Nice code @chandra.shekar.g very usefull.

Let me post this code addapted for the people that can run specific rules only when on specific parameter change.

Sub Main
 Dim oModelEvent As ModelingEvents 
 oModelEvent = ThisApplication.ModelingEvents 
 AddHandler oModelEvent.OnParameterChange, AddressOf oModelEvent_ParameterChange
End Sub

 Sub oModelEvent_ParameterChange(DocumentObject As _Document, Parameter As Parameter, BeforeOrAfter As EventTimingEnum, Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
	If Parameter.Name = "Parameter1" Then 'Change "Parameter1" for the name of your parameter 1
		iLogicVb.RunExternalRule("Rule1") 'Change "Rule1" for the name of associated rule when Paramer1 changes
	Else If Parameter.Name = "Paramer2" Then 'Change "Parameter2" for the name of your parameter 2
		iLogicVb.RunExternalRule("Rule2") 'Change "Rule2" for the name of associated rule when Paramer2 changes
	Else If Parameter.Name = "Parameter3" Then 'Change "Parameter3" for the name of your parameter 3
		iLogicVb.RunExternalRule("Rule3") 'Change "Rule3" for the name of associated rule when Paramer3 changes	
	End If
 End Sub

 

0 Likes