Trying to Handle API Events in iLogic Rule Broke Part

Trying to Handle API Events in iLogic Rule Broke Part

SometimesInventorMakesMeAngry
Advocate Advocate
578 Views
5 Replies
Message 1 of 6

Trying to Handle API Events in iLogic Rule Broke Part

SometimesInventorMakesMeAngry
Advocate
Advocate

I was trying to see if API events work in an iLogic rule because I need some code to be local to a document. I came up with the code below and, sure enough, it worked! However, I think it worked too well because I get about 8 messageboxes every time the document is saved. Setting docEvents = Nothing doesn't fix it. Suppressing and even deleting the rule doesn't fix it.

Class Main
	
	Dim thisDocument As Document
	Dim WithEvents docEvents As DocumentEvents
	
	Sub Main()
		
		thisDocument = ThisDoc.document
		docEvents = thisDocument.documentevents
		
	End Sub
	
	Sub Handler(BeforeOrAfter As EventTimingEnum, 
               Context As NameValueMap, 
               ByRef HandlingCode As HandlingCodeEnum) Handles docEvents.onsave
		
		If BeforeOrAfter = EventTimingEnum.kBefore
            MessageBox.Show("It Works")
		End If
		
	End Sub
	
End Class

 

0 Likes
579 Views
5 Replies
Replies (5)
Message 2 of 6

SometimesInventorMakesMeAngry
Advocate
Advocate

I found that restarting Inventor fixes the problem. If this is anything like subscribing and unsubscribing to events in C#, I might also be able to RemoveHandler with the same Sub without having to restart but I haven't tested it.

 

I think it's ok to leave this up in case someone else has the same problem in the future as I could find nothing related to this in a forum search.

 

Edit: I know there is an event trigger for this code, but it was a proof of concept to see if I could handle events that are not included in the event triggers, but are in the API.

0 Likes
Message 3 of 6

Yep.  When you create an event handler, you have to also have a plan for how to dispose of it.  If you created it then exited the code/rule, it will continue running in the background, irretrievable.  The main way to get rid of them is to restart Inventor (and hope you don't have it set-up to start when Inventor starts again 😉).

A while back I wrote a contribution post about creating custom event handlers, you might be interested in.  It contains an example iLogic rule too.   It also contains links Inventor's online help pages about many of the built-in methods for creating event handlers.  It also explains the two different sets of keywords/phrases used to set them up, and where they can be used.

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 6

SometimesInventorMakesMeAngry
Advocate
Advocate

Thank you. I could have read your post and saved some time researching event handling in VB.

0 Likes
Message 5 of 6

eugen.gutol
Contributor
Contributor

Hi @WCrihfield , the link to the community post leads to missing page, do you have a new link ?

I'm dealing with ModelingEvents and can't end/clear them -_-*

0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor

Hi @eugen.gutol.  Unfortunately, they took down all of those 'contribution posts' that all of us had posted in the 'knowledge.autodesk.com' area a years or so back.  There is no 'new' or updated link for any of them either.  Some folks opted to create their own, private 'blog' sites online, to post stuff like that, but I did not decide to go that route.  However, I did capture some content from some of my more popular online posts to MS Word documents, then converted them to PDFs.  I will attach one of those.

 

I have learned a lot over the past few years since I created those posts, so looking at them again now, they look like they need to be updated.  Many of my newer code examples for 'handling' events are larger and include additional ways to eliminate the event handler without having to restart Inventor.  But that article is still informational for those just getting started with handling events.

 

The main ways to eliminate an event handler are:

  • When appropriate, use the AddHandler & RemoveHandler statements to set it up, instead of using the WithEvents & Handles clause.  This will give you more control to be able to 'remove' the handler before it would naturally be eliminated.
  • When appropriate, use one or more other event handlers to 'remove' an existing event handler when a specific event happens.
  • For more manual control, when used within iLogic rules, you can incorporate the use of iLogic's SharedVariable system.  This allows you to store data within Inventor's session memory, without that effecting any documents, and can retrieve that data within other rules (during that same session of Inventor).  Using this, we can 'record' that we have already started or initialized a specific event handler, to avoid creating the same one again...and can record (or check) the 'status' of that event handler, to know whether we should allow it to continue, or if it should be eliminated.  When using that system, we can manually set / change the 'status' of that SharedVariable (using another simple iLogic rule), so that when that event handler runs again, and that status is checked, the event handler will be eliminated.
  • When possible/appropriate, use DocumentEvents instead of ApplicationEvents, because once the Document is terminated (not just visibly closed), those event handlers associated with it will also be naturally eliminated.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes