@BrandonW9 wrote:
I got a "like" notification on this post, so I felt obliged to come back and warn everyone that the event triggers were NOT running correctly when set by this code.
It added them nicely, and I could have sworn that an initial test confirmed it was working, but then in production, it was not working. The triggers looked like they were set, but were not running, and caused a ton of chaos and confusion. I finally ditched this whole idea because I couldn't get it to work.
I suspect that perhaps this part of the API has a bug or something, but I don't know enough about such things to confirm. Nonetheless. Do not trust event triggers that have been set by this code.
Hi @BrandonW9 ,
The like notification was me...
This is the version of the rule that I just put together based on @DRoam 's example above... I combined his two rules into one external rule.
This version and the resulting triggers are working so far, but I've not fully tested it.
In this version it first deletes all existing triggers, then it creates a trigger per file type for each rule in the array list, and does this for each trigger type specified (in this example it is only for Before Save and Material Change).
This is my result when running the version I've posted here.... Test Rule2 and Test Rule 200 and Test Rule all have message boxes that pop up on the events to show that they are triggering/running... so far so good.

One thing that I noticed is that if the folder is wrong or the rule name is wrong, in such a way that the rule does not exist ( or does not exist in the specified folder) then it creates the trigger even though there is no rule to trigger... I wonder if that is the issue you are seeing.


I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Sub Main
oDoc = ThisApplication.ActiveEditDocument
'-----------------------------------------------------------
'[ delete all existing event triggers
'Get access to the set of Event Triggers
Dim oEventTriggersSet As PropertySet
oEventTriggersSet = GetEventTriggersSet(oDoc)
Dim oRuleTrigger As Inventor.Property
For Each oRuleTrigger In oEventTriggersSet
oRuleTrigger.Delete
Next
']
'-----------------------------------------------------------
'external iLogic rule subfolder
oSubFolder = "Template Rules\"
Dim oRuleList As New ArrayList
'-----------------------------------------------------------
'[ Before Save Document trigger
oTrigger = "Before Save Document"
'clear rule list
oRuleList.Clear
'create rule list for each doc type
Select Case oDoc.DocumentType
Case kPartDocumentObject
oRuleList.add("Test Rule2")
oRuleList.add(oSubFolder & "Test Rule 200")
Case kAssemblyDocumentObject
oRuleList.add("Assembly iLogic Rules" )
Case kDrawingDocumentObject
oRuleList.add("Drawing iLogic Rules" )
End Select
'run the other SetRuleTrigger sub for each
'item in the rule list
'to set Before Save trigger
For Each oItem in oRuleList
Call SetRuleTrigger(oItem, "External" , oTrigger, "Add")
Next
']
'-----------------------------------------------------------
'[ Material Change trigger
oTrigger = "Material Change"
'clear rule list
oRuleList.Clear
'create rule list for each doc type
Select Case oDoc.DocumentType
Case kPartDocumentObject
oRuleList.add("Test Rule")
oRuleList.add("Material" )
Case kAssemblyDocumentObject
oRuleList.add("Material" )
Case kDrawingDocumentObject
End Select
'run the other SetRuleTrigger sub for each item in the rule list
For Each oItem in oRuleList
Call SetRuleTrigger(oItem, "External" , oTrigger, "Add")
Next
']
'-----------------------------------------------------------
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
MessageBox.Show("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
Sub SetRuleTrigger(myRuleName As String, myRuleLocation As String, myTrigger As String, myAction As String)
Dim myRuleNameFull As String
If myRuleLocation Is "External" Then
myRuleNameFull = "file://" & myRuleName
Else
myRuleNameFull = myRuleName
End If
'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 = myRuleNameFull Then
'Our trigger does exist.
If myAction = "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 myAction = "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 myAction = "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(myRuleNameFull, oIDName, oIDNumber)
'MessageBox.Show("Trigger added! Exiting rule.") '***DEBUGGING***
'Exit the rule.
Exit Sub
End If
End Sub
