Sub Main Dim oDoc As Document = ThisDoc.Document 'this next line uses custom Function to allow selection of internal iLogic rule from this document Dim sRuleName As String = SelectInternalRule(oDoc) If sRuleName = "" Then Logger.Debug("No iLogic rule name was specified. Exiting routine.") Return 'if nothing was selected, exit rule End If Dim oEventChoice As KeyValuePair(Of String, Integer) = ChooseEventTriggersEvent() If IsNothing(oEventChoice) OrElse oEventChoice.Key = "" Then Logger.Debug("No Event Choice Was Returned. Exiting routine.") Return End If Dim sEventInternalName As String = oEventChoice.Key 'different and specific for each event Dim iPropIDRangeStart As Integer = oEventChoice.Value 'different and specific for each event AddRuleToEventTriggers(oDoc, sRuleName, sEventInternalName, iPropIDRangeStart, False) oDoc.Update2(True) End Sub Function SelectInternalRule(oDoc As Inventor.Document) As String Dim oRules As IEnumerable = iLogicVb.Automation.Rules(oDoc) If oRules Is Nothing Then Return String.Empty Dim oRuleNames As New List(Of String) For Each oRule As iLogicRule In oRules oRuleNames.Add(oRule.Name) Next Dim sChoice As String = InputListBox("", oRuleNames, "", "Choose A Rule", "List Of Rule Names") Return sChoice End Function Function ChooseEventTriggersEvent() As KeyValuePair(Of String, Integer) Dim oDict As New Dictionary(Of String, Integer) oDict.Add("AfterDocOpen", 400) oDict.Add("DocClose", 500) oDict.Add("BeforeDocSave", 700) oDict.Add("AfterDocSave", 800) oDict.Add("AfterAnyParamChange", 1000) oDict.Add("PartBodyChanged", 1200) oDict.Add("AfterMaterialChange", 1400) oDict.Add("AfterAnyiPropertyChange", 1600) oDict.Add("AfterDrawingViewsUpdate", 1800) oDict.Add("AfterFeatureSuppressionChange", 2000) oDict.Add("AfterComponentSuppressionChange", 2200) oDict.Add("AfterComponentReplace", 2400) oDict.Add("AfterDocNew", 2600) oDict.Add("AfterModelStateActivated", 2800) oDict.Add("AfterAnyUserParamChange", 3000) Dim oEventNames As List(Of String) = oDict.Keys.ToList Dim sChosenEventName As String = InputListBox("Choose Event Name.", oEventNames, "", "Event Names", "List Of Event Names") If sChosenEventName = "" Then Return Nothing For Each oEntry As KeyValuePair(Of String, Integer) In oDict If oEntry.Key = sChosenEventName Then Return oEntry Next Return Nothing End Function Sub AddRuleToEventTriggers(oDoc As Document, sRuleName As String, sEventName As String, iPropIDStart As Integer, bIsExternalRule As Boolean) '<<< checking values of 'input' parameters >>> If oDoc Is Nothing Then Return If oDoc.IsModifiable = False Then Logger.Debug("Supplied Document is not modifiable. Exiting routine.") Return End If If sRuleName = "" Then Logger.Debug("Empty Rule Name Supplied. Exiting routine.") Return End If If sEventName = "" Then Logger.Debug("Empty Event Name Supplied. Exiting routine.") Return End If If iPropIDStart = 0 Then Logger.Debug("Invalid PropID Range Start Value Supplied. Exiting routine.") Return End If '<<< end of checking 'input' parameters >>> Const sExternalRulePrefix As String = "file://" If (bIsExternalRule = True) AndAlso (sRuleName.StartsWith(sExternalRulePrefix) = False) Then sRuleName = sExternalRulePrefix & sRuleName End If Const iLogicClientId As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}" Dim oTempRule As iLogicRule = Nothing Const sTempInternalRuleName As String = "DeleteMe" If oDoc.DocumentInterests.HasInterest(iLogicClientId) = False Then oTempRule = iLogicVb.Automation.AddRule(oDoc, sTempInternalRuleName, "") Logger.Trace("Empty temporary internal iLogic rule named '" & sTempInternalRuleName & "' was added to this document.") End If 'internal name for event, as needed for start of Property.Name value Dim oSet As PropertySet = Nothing Const sSetName As String = "_iLogicEventsRules" Const sSetInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}" If Not oDoc.PropertySets.PropertySetExists(sSetInternalName, oSet) Then oSet = oDoc.PropertySets.Add(sSetName, sSetInternalName) Logger.Trace("Hidden Event Triggers PropertySet was created.") End If Dim oProp As Inventor.Property = Nothing Dim iPropIDEnd As Integer = (iPropIDStart + 99) For iPropID As Integer = iPropIDStart To iPropIDEnd 'range is different & specific for each event Try 'check if a Property already exists at this PropID oProp = oSet.ItemByPropId(iPropID) If oProp.Value = sRuleName Then 'if so, then check its Value for this rule's name Logger.Info("This rule has already been added to this event trigger.") 'if a temp rule was added, remove it now If oTempRule IsNot Nothing Then iLogicVb.Automation.DeleteRule(oDoc, sTempInternalRuleName) Logger.Trace("Temporary internal iLogic rule named '" & sTempInternalRuleName & "' was just deleted from this document.") End If Exit Sub 'this rule was found, so exit routine End If Continue For 'there is a different rule at this iPropID, so skip to next Property, if any Catch 'what to do if code above causes error 'no property was found with that PropID, and this rule has not been found, so now create one for this rule oProp = oSet.Add(sRuleName, sEventName & iPropID, iPropID) Exit For 'exit the loop, because we have already added the rule to the event End Try Next 'go to next PropID to check it, if any 'if a temp rule was added, remove it now If oTempRule IsNot Nothing Then iLogicVb.Automation.DeleteRule(oDoc, sTempInternalRuleName) Logger.Trace("Temporary internal iLogic rule named '" & sTempInternalRuleName & "' was just deleted from this document.") End If End Sub