Event Handler fires twice despite calling for the "after" EventTimingEnum

Event Handler fires twice despite calling for the "after" EventTimingEnum

Erumb_Tony
Contributor Contributor
333 Views
5 Replies
Message 1 of 6

Event Handler fires twice despite calling for the "after" EventTimingEnum

Erumb_Tony
Contributor
Contributor

Hi everyone. Firstly I have set an event trigger to run the rule below whenever a Part file is opened. The code then handles the event of user changing the "Thickness" parameter. But unfortunately it fires twice for every sheetmetal .ipt file that it detects the parameter change. I get that it fires twice for every parameter change, but I have still added the "kAfter" condition and still get this problem. I have very limited knowledge of event handlers and I get dizzy trying to understand how it works. Despite repeatedly tweaking different aspects of the code, I couldn't get it to run only once per thickness update.

 

Also, please let me know:

1. If this approach is efficient and won't cause lags to the user.

2. Will this create duplication of event handling triggers when the user has multiple sheet metal .ipt's open at the same time ?

 

Sub main
	
	Dim doc As Document = ThisApplication.ActiveDocument
	If doc.DocumentType <> kpartdocumentobject
		Exit Sub
	Else
		Logger.Info("Program running on " & doc.DisplayName)
	End If
	
	'----CREATION DATE CHECK----
	
	Dim fso As Object = CreateObject("Scripting.FileSystemObject") 
	Dim filePath As String = doc.FullFileName
	Dim creationDate As Date = fso.GetFile(filePath).DateCreated
	Dim thresholdDate As Date = "#1/1/2025#"
	If creationDate < thresholdDate
		Logger.Info("Date check failed: " & creationDate)
		Exit Sub
	Else 
		Logger.Info("Date check passed: " & creationDate)
	End If
	
	'----SHEET METAL CHECK----
	
	If doc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}"
		Dim Mevents As ModelingEvents = ThisApplication.ModelingEvents
		AddHandler Mevents.OnParameterChange, AddressOf ParameterHasChanged 'PARAMETER CHANGE EVENT
		
		Dim docevents As DocumentEvents = doc.DocumentEvents
		AddHandler docevents.onclose, AddressOf closedoc 'DOCUMENT CLOSED EVENT
	End If
		
		
End Sub



Sub ParameterHasChanged(doc As Document, Changedparameter As Parameter, BeforeOrAfter As EventTimingEnum, oContext As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
	
	Logger.Info("Parameter named - " & Changedparameter.Name & " - has changed")
	
		If BeforeOrAfter = kafter
			If Changedparameter.Name = "Thickness" 
				'Thickness has changed
				Logger.Info("Initiating Execution due to change in " & Changedparameter.Name)
				Dim IsExecuting As Boolean
				Execute(doc, IsExecuting)
				Logger.Info("Completed Successfully.")
			End If
		End If
End Sub

Sub closedoc(beforeorAfter As EventTimingEnum, oContext As Object, ByRef handlingcode As HandlingCodeEnum)
'just removes the handler

	Dim doc As Document = ThisApplication.ActiveDocument
	Dim docevents As DocumentEvents = doc.DocumentEvents
	Dim Mevents As ModelingEvents = ThisApplication.ModelingEvents
	
	Dim UIE As UserInterfaceEvents = ThisApplication.UserInterfaceManager.UserInterfaceEvents
	
	If beforeorAfter = EventTimingEnum.kBefore

		RemoveHandler Mevents.OnParameterChange, AddressOf ParameterHasChanged
		Logger.Info("Event handler for Model Parameter is removed on " & doc.DisplayName)
	End If
End Sub

Sub Execute(doc As PartDocument, IsExecuting As Boolean)

	If IsExecuting = True
		Exit Sub
	End If
	
	IsExecuting = True
iLogicVb.Automation.RulesEnabled = False
	
	'Main execution code yet to be added here
'I have added the log line below for now
Logger.Info("Execution !")


			iLogicVb.Automation.RulesEnabled = True
			IsExecuting = False
		
End Sub

 

0 Likes
Accepted solutions (1)
334 Views
5 Replies
Replies (5)
Message 2 of 6

marcin_otręba
Advisor
Advisor

hi, each event will fire twice, but you decide what to do next using beforeorafter, only problem i see is that not everything is inside beforeorafter if statement… if something is outside the statement then it will be fired executed twice.

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 3 of 6

Erumb_Tony
Contributor
Contributor

@marcin_otręba, Thanks for replying. I have placed the "Execute" sub-routine within the If condition in line #47, so I am not sure I get what you mean by everything not being inside the beforeorafter if statement. Can you elaborate ?

0 Likes
Message 4 of 6

marcin_otręba
Advisor
Advisor
Accepted solution

change it to:

 

Sub ParameterHasChanged1(doc As Document, Changedparameter As Parameter, BeforeOrAfter As EventTimingEnum, oContext As NameValueMap, ByRef HandlingCode As HandlingCodeEnum)
		If BeforeOrAfter = EventTimingEnum.kAfter Then
			If Changedparameter.Name = "Thickness" Then
				Logger.Info("Parameter named - " & Changedparameter.Name & " - has changed  " & BeforeOrAfter)
				'Thickness has changed
				Logger.Info("Initiating Execution due to change in " & Changedparameter.Name)
				Dim IsExecuting As Boolean
				Execute(doc, IsExecuting)
				Logger.Info("Completed Successfully.")
			End If
		End If
End Sub

Sub closedoc(beforeorAfter As EventTimingEnum, oContext As Object, ByRef handlingcode As HandlingCodeEnum)
'just removes the handler
	If beforeorAfter = EventTimingEnum.kBefore Then
		Dim doc As Document = ThisApplication.ActiveDocument
		Dim docevents As DocumentEvents = doc.DocumentEvents
		Dim Mevents As ModelingEvents = ThisApplication.ModelingEvents
	
		Dim UIE As UserInterfaceEvents = ThisApplication.UserInterfaceManager.UserInterfaceEvents
		RemoveHandler Mevents.OnParameterChange, AddressOf ParameterHasChanged1
		Logger.Info("Event handler for Model Parameter is removed on " & doc.DisplayName & "  " & beforeorAfter)
	End If
End Sub

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 5 of 6

jjstr8
Collaborator
Collaborator

@Erumb_Tony :  After some digging, it looks like the OnClose event is not always triggering, so the OnParameterChange handler doesn't always get removed. Opening the file again would add the handler a second (or more) time. It's not uncommon in addins for events to not fire because the reference to DocumentEvents (or any other events object) is disposed of. I imagine it's similar in iLogic. Try setting up a shared variable to keep docevents "alive". I would also do the suggestion @marcin_otręba  made about your "sub closedoc". I saw that if was last open document closing, ActiveDocument is nothing in the after case and would trigger an exception. Inventor swallowed it so you didn't see it, but it's still good to avoid it and have everything in the "before" if statement.

 

...
Dim docEvents As DocumentEvents = doc.DocumentEvents
SharedVariable("thisDocEvents") = docEvents
AddHandler docevents.OnClose, AddressOf closedoc 'DOCUMENT CLOSED EVENT
...

 

Message 6 of 6

Erumb_Tony
Contributor
Contributor

Hi @jjstr8 & @marcin_otręba . Thanks a lot & apologies for the late response ! I got caught up in other work. I will modify my code according to your suggestions and get back as soon as possible

0 Likes