Basically it has to do with how you specify the target document that you want the code to effect. In a local rule, you have more control for making sure your target document is the 'local' document. Here is a link to one of my contribution posts which I recommend you read that goes into the subject in much more detail. The types of event triggers being created by the code I posted above will effect all documents that you may deal with while it is running/active. The event handler will remain active in the background until you either close Inventor, or , as was my intention, until you close that original assembly document that it was created for. If you are working with multiple other documents while your main/original assembly is open, those events the code is 'listening for' may happen within those other documents, which would trigger the code to run, unless the code is correctly checking to make sure that the document that the event happened in was your local original assembly.
I updated my code snippet for use as a 'local' rule. The main Sub doesn't need to know anything about the 'active' or 'local' document at the time, because it is only creating the event handlers. Those other two Sub's both check to make sure they are only acting upon the 'local' assembly document, using the ThisDoc.Document term.
Sub Main
Dim oAsmEvents As AssemblyEvents = ThisApplication.AssemblyEvents
AddHandler oAsmEvents.OnNewOccurrence, AddressOf oAsmEvents_OnNewOccurrence
Dim oAppEvents As ApplicationEvents = ThisApplication.ApplicationEvents
AddHandler oAppEvents.OnCloseDocument, AddressOf ApplicationEvents_OnCloseDocument
End Sub
Public Sub oAsmEvents_OnNewOccurrence(oAsmDoc As AssemblyDocument, oOcc As ComponentOccurrence, oTiming As Inventor.EventTimingEnum, oContext As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
'to make sure the assembly document that the event happened in is the 'local' assembly
'otherwise don't do anything (don't work for other assembly documents that may also be open)
'ThisDoc.Document will always refer to the 'local' document, when used in a 'local' rule
If oAsmDoc Is ThisDoc.Document Then
'<<<< RUN YOUR RULE FROM WITHIN THIS SUB >>>>
'iLogicVb.RunRule("RuleName")
'iLogicVb.RunExternalRule()
'iLogicVb.Automation.RunRule()
'iLogicVb.Automation.RunExternalRule()
'MsgBox("Ran your Rule successfully.",,"")
End If
End Sub
Public Sub ApplicationEvents_OnCloseDocument(oDoc As Inventor._Document, oFullName As String, oTiming As Inventor.EventTimingEnum, oContext As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
If oDoc Is ThisDoc.Document Then
Dim oAsmEvents As AssemblyEvents = ThisApplication.AssemblyEvents
Dim oAppEvents As ApplicationEvents = ThisApplication.ApplicationEvents
RemoveHandler oAsmEvents.OnNewOccurrence, AddressOf oAsmEvents_OnNewOccurrence
MsgBox("Removed 'OnNewEditObject' Event handler.",,"")
RemoveHandler oAppEvents.OnCloseDocument, AddressOf ApplicationEvents_OnCloseDocument
MsgBox("Removed 'OnCloseDocument' Event handler.",,"")
End If
End Sub
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

(Not an Autodesk Employee)