Thanks for that, @MechMachineMan. I utilized that to make the rules below.
@Thetigerpirro / @Tigerpirro, this is the only code that you need to put in the rule that you want to check for the Event Trigger in:
'This code will fire an external iLogic rule which will add or remove a rule to/from the specified trigger.
SharedVariable("myRuleName") = "Rule0" 'This is the name of your rule. Include the full path for an external rule.
SharedVariable("myTrigger") = "Before Save Document" 'This is the trigger name exactly as it appears in the Event Triggers dialog box
SharedVariable("myAction") = "Add" 'Type "Add" to add the rule to the specified trigger or "Remove" to remove it
iLogicVb.RunExternalRule("EventTriggerSet")
SharedVariable.RemoveAll()
Then, you need to create an External Rule called "EventTriggerSet" which contains the following code:
Sub Main()
'This code will add or remove a rule to/from the specified trigger.
'It gets its instructions from shared variables created by another rule.
'It relies on the sub "SetRuleTrigger" and the function "GetEventTriggersSet".
Try
myRuleName = SharedVariable("myRuleName") 'This is the name of your rule. Include the full path for an external rule.
myTrigger = SharedVariable("myTrigger") 'This is the trigger name exactly as it appears in the Event Triggers dialog box
myAction = SharedVariable("myAction") 'Type "Add" or "Remove"
Catch
MessageBox.Show("The required inputs were not sent properly to the EventTriggerSet External Rule." & vbNewLine & vbNewLine & "Exiting rule", "EventTriggerSet Error",MessageBoxButtons.OK,MessageBoxIcon.Error)
Exit Sub
End Try
Call SetRuleTrigger(myRuleName, myTrigger, myAction)
End Sub
Sub SetRuleTrigger(oRuleName As String, myTrigger As String, oAction As String)
'MessageBox.Show("Attempting to access Event Triggers set.") '***DEBUGGING***
'Get access to the set of Event Triggers
Dim oEventTriggersSet As PropertySet
oEventTriggersSet = GetEventTriggersSet(ThisApplication.ActiveDocument)
'MessageBox.Show("Event Triggers set accessed successfully!") '***DEBUGGING***
'Get the proper Trigger name and ID
Select Case myTrigger
Case "After Open Document"
oTriggerName = "AfterDocOpen"
oTriggerID = 400
Case "Close Document"
oTriggerName = "DocClose"
oTriggerID = 500
Case "Before Save Document"
oTriggerName = "BeforeDocSave"
oTriggerID = 700
Case "After Save Document"
oTriggerName = "AfterDocSave"
oTriggerID = 800
Case "Any Model Parameter Change"
oTriggerName = "AfterAnyParamChange"
oTriggerID = 1000
Case "Part Geometry Change"
oTriggerName = "PartBodyChanged"
oTriggerID = 1200
Case "Material Change"
oTriggerName = "AfterMaterialChange"
oTriggerID = 1400
Case "Drawing View Change"
oTriggerName = "AfterDrawingViewsUpdate"
oTriggerID = 1500
Case "iProperty Change"
oTriggerName = "AfterAnyiPropertyChange"
oTriggerID = 1600
Case "Feature Suppression Change"
oTriggerName = "AfterFeatureSuppressionChange"
oTriggerID = 2000
Case "Component Suppression Change"
oTriggerName = "AfterComponentSuppressionChange"
oTriggerID = 2200
Case "iPart / iAssembly Change Component"
oTriggerName = "AfterComponentReplace"
oTriggerID = 2400
Case "New Document"
oTriggerName = "AfterDocNew"
oTriggerID = 2600
End Select
'First check if the rule trigger already exists.
'While we're at it, count the rules already in the Trigger of interest. If we need to add our rule trigger, we'll use this later on.
'MessageBox.Show("Attempting to check if rule trigger already exists.") '***DEBUGGING***
oRuleCount = 0
Dim oRuleTrigger As Inventor.Property
For Each oRuleTrigger In oEventTriggersSet
If oRuleTrigger.Name Like oTriggerName & "*" Then
oRuleCount = oRuleCount + 1
If oRuleTrigger.Value = oRuleName Then
'Our trigger does exist.
If oAction = "Add" Then
'The rule trigger already exists, so we don't need to add it. Exit the sub.
'MessageBox.Show("Trigger found! Exiting rule.") '***DEBUGGING***
Exit Sub
Else If oAction = "Remove" Then
'MessageBox.Show("Trigger found! Attempting to remove.") '***DEBUGGING***
oRuleTrigger.Delete
'We did what we came to do. Exit the sub.
'MessageBox.Show("Trigger removed! Exiting rule.") '***DEBUGGING***
Exit Sub
End If
End If
End If
Next
'If we got to here, the rule trigger wasn't found.
If oAction = "Remove" Then
'If we came to delete the rule trigger, it didn't exist anyway, so exit the sub.
'MessageBox.Show("Trigger not found! Exiting rule.") '***DEBUGGING***
Exit Sub
Else
'If we came to add the rule trigger, add it.
'MessageBox.Show("Trigger not found! Attempting to add.") '***DEBUGGING***
'We already counted how many rules are already in our trigger.
'We can use this to determine what ID name and number to assign to our new rule trigger.
'Since the ID counts start at 0, we don't have to add 1 to our count.
Dim oIDName As String = oTriggerName & oRuleCount
Dim oIDNumber As Integer = oTriggerID + oRuleCount
'MessageBox.Show("ID name = " & oIDName & vbNewLine & vbNewLine & "ID Number = " & oIDNumber) '***DEBUGGING***
'Add our rule trigger
oEventTriggersSet.Add(oRuleName, oIDName, oIDNumber)
'MessageBox.Show("Trigger added! Exiting rule.") '***DEBUGGING***
'Exit the rule.
Exit Sub
End If
End Sub
Function GetEventTriggersSet(oDoc As Document) As Inventor.PropertySet
On Error Resume Next
oEventTriggersSet = oDoc.PropertySets.Item("iLogicEventsRules")
If oEventTriggersSet Is Nothing Then
oEventTriggersSet = oDoc.PropertySets.Item("_iLogicEventsRules")
End If
If oEventTriggersSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then
Call oEventTriggersSet.Delete
oEventTriggersSet = oDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
End If
If oEventTriggersSet Is Nothing Then
oEventTriggersSet = oDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
End If
If oEventTriggersSet Is Nothing Then
MsgBox ("Unable to create the Event Triggers property for this file!", , "Event Triggers Not Set")
Err.Raise(1)
Exit Function
End If
On Error Goto 0
Return oEventTriggersSet
End Function
Yes, the External rule is very long. And maybe @MechMachineMan or @Curtis_Waguespack or someone else could make it be much more compact and efficient that I could. But the good news is you don't need to put this code in any of your rules. You just create that External rule once and then use the code in the first box to fire it whenever you want to add/remove a rule to an Event Trigger.
I tested it as best I could and it seems to work well, but it's probably not perfect. Please post here if you find any issues.
Hope this works for you.