Private Sub VerifyTrigger(ByVal cDoc As Document, ByVal rName As String, ByVal trigName As String) 'Let's set up some new data containers. 2 Strings and an Integer. Dim BaseID As Integer Dim BaseName As String Dim BaseUse As String 'Now... Let's talk. 'At this point I have to sort of point out why I'm doing the things I'm doing, sadly, it takes more 'than a simple 1 line comment. 'Event Triggers work in a sort of strange way. As I accidentally found out, - 'each Event Trigger is just a Property in the PropertySet called "iLogicEventsRules" 'Though unlike a regular property, you MUST assign a PropertyID to them when you create them, 'AND the property ID, MUST fall withen a certain range, otherwise, the trigger will simply not work. ' 'Below is a list of CheckList Box Names / Property Names / Property IDs. ' 'The Checklist Box Name is just the name that the user sees next to the checkboxes while they are 'selecting the various Event Triggers. I've tried to keep the names as close to what you would see 'in Inventor, had you set it up manually. ' 'The Property Name is the Name that is passed to the newly created Property. It's not as simple as 'passing just that name to the new property. You also have to tack on a numeric value to it! ' Example : AfterDocOpen1 'The numeric value simply tells Inventor the order in which to fire off the values associated with 'Event Triggers of the same type. To give you an example, let's assume that you want Rule1 and Rule2 'to both fire after the document is opened. If you were look at the properties of both of them, 'you would find that Rule1 would have a property name of AfterDocOpen0, and Rule2 would have a 'property name of AfterDocOpen1. ' ' 'The PropertyID is the default base Property ID that must be passed along to the property. 'Using the example above (Rule1 and Rule2), the entire property items would read out as : ' '("Rule1", "AfterDocOpen0", 400) ' & '("Rule2", "AfterDocOpen1", 401) ' 'Now that I think about it, I don't believe that I have tested out whether or not the 'numerical ending for the PropertyName should be the same amount added to the base PropertyID, 'but in the code below, I keep both values the same as it seems a rather fragile thing. ' 'I should also note that if you fail to include a PropertyID, and simply let Inventor set one 'for you, it will choose a number that is out of range, and though it will still 'look' as if 'it has created a working Trigger, it will actually do nothing. ' 'Below is a list of the Event Triggers for .ipt files (Solid and Sheet Metal) - '.iam files (Assembly), and .idw/.dwg files (Drawing Files). I do not know if this if the complete list 'of all available triggers inside of Inventor, as there maybe some filetypes / variations that I have 'overlooked (do not use regularly / at all). If you find that this list is incomplete, feel free to 'contact me using the information posted at the very topmost comments in this code. ' ' ' CheckList Box Name: : Property Name : : Property ID ' ---------------------------------------------------------------------------------------- ' After Open Document : AfterDocOpen : 400 ' Close(Document) : DocClose : 500 ' Before Save Document : BeforeDocSave : 700 ' After Save Document : AfterDocSave : 800 ' Any Model Parameter Change : AfterAnyParamChange : 1000 ' Part Geometry Change** : PartBodyChanged : 1200 ' Material Change** : AfterMaterialChange : 1400 ' Drawing View Change*** : AfterDrawingViewsUpdate : 1500 ' iProperty(Change) : AfterAnyiPropertyChange : 1600 ' Feature Suppression Change** : AfterFeatureSuppressionChange : 2000 ' Component Suppression Change* : AfterComponentSuppressionChange : 2200 ' iPart / iAssembly Change Component* : AfterComponentReplace : 2400 ' New Document : AfterDocNew : 2600 ' ' ---------------------------------------------------------------------------------------- 'Let's look at the Event Trigger that user has selected (that has been passed to use here) 'If it matches the Case, then we will change the values of BaseName to the correct PropertyName 'We will change BaseID to the correct Base PropertyID 'And we will set where this trigger can be used, with the BaseUse value. 'A BaseUse of "All" means that it's a Trigger that can be used across the file types (.ipt/.iam/.idw/.dwg) 'A BaseUse of "Part" means that the trigger can only be used on a .ipt file. 'A BaseUse of "Assembly" means that the trigger can only be used on a .iam file. 'A BaseUse of "Drawing" means that the trigger can only be used on a .idw/.dwg file. Select Case trigName Case "After Open Document" BaseName = "AfterDocOpen" BaseID = 400 BaseUse = "All" Case "Close Document" BaseName = "DocClose" BaseID = 500 BaseUse = "All" Case "Before Save Document" BaseName = "BeforeDocSave" BaseID = 700 BaseUse = "All" Case "After Save Document" BaseName = "AfterDocSave" BaseID = 800 BaseUse = "All" Case "Any Model Parameter Change" BaseName = "AfterAnyParamChange" BaseID = 1000 BaseUse = "All" Case "Part Geometry Change**" BaseName = "PartBodyChanged" BaseID = 1200 BaseUse = "Part" Case "Material Change**" BaseName = "AfterMaterialChange" BaseID = 1400 BaseUse = "Part" Case "Drawing View Change***" BaseName = "AfterDrawingViewsUpdate" BaseID = 1500 BaseUse = "Drawing" Case "iProperty Change" BaseName = "AfterAnyiPropertyChange" BaseID = 1600 BaseUse = "All" Case "Feature Suppression Change**" BaseName = "AfterFeatureSuppressionChange" BaseID = 2000 BaseUse = "Part" Case "Component Suppression Change*" BaseName = "AfterComponentSuppressionChange*" BaseID = 2200 BaseUse = "Assembly" Case "iPart / iAssembly Change Component*" BaseName = "AfterComponentReplace" BaseID = 2400 BaseUse = "Assembly" Case "New Document" BaseName = "AfterDocNew" BaseID = 2600 BaseUse = "All" Case Else 'If the value passed, doesn't match any of the Cases, then we'll throw up a crazy message box! 'It also means that I probably have a spelling error :( BaseUse = "None" MessageBox.Show("Trigger Event Error in Selection", "Trigger Event Selection Error") Exit Sub End Select 'First we check what the BaseUse is set to, then we check that against the current document's type. 'If they match up, we pass the current Document, the rule name, the trigger name, the base trigger name, and 'the base trigger ID to the sub SetEventProperty If BaseUse = "All" Then If cDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject _ Or cDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject _ Or cDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then SetEventProperty(cDoc, rName, trigName, BaseName, BaseID) Else Exit Sub End If ElseIf BaseUse = "Drawing" Then If cDoc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then SetEventProperty(cDoc, rName, trigName, BaseName, BaseID) Else Exit Sub End If ElseIf BaseUse = "Assembly" Then If cDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then SetEventProperty(cDoc, rName, trigName, BaseName, BaseID) Else Exit Sub End If ElseIf BaseUse = "Part" Then If cDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then SetEventProperty(cDoc, rName, trigName, BaseName, BaseID) Else Exit Sub End If ElseIf BaseUse = "None" Then Exit Sub End If End Sub Private Sub SetEventProperty(ByVal cDocument As Document, ByVal RuleName As String, ByVal CheckBoxTrigName As String, ByVal TriggerPropName As String, ByVal BaseID As Integer) 'Because we need to compare so many values, we'll need to set up a few containers Dim propItemCounter As Integer Dim TriggerID As Integer Dim CurrentID As Integer Dim EndHolder As Integer Dim CurrentEnd As Integer Dim customIPropSet As PropertySet 'EndHolder will act as the numerical value that will be attached to the end of the property name. EndHolder = 0 'TriggerID will hold on to the Trigger's base PropertyID value. TriggerID = BaseID Try customIPropSet = cDocument.PropertySets.Item("iLogicEventsRules") If customIPropSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then Call customIPropSet.Delete() Call cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}") End If Catch ex As Exception Try Call cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}") Catch ex2 As Exception MessageBox.Show("Unable to create the Event Triggers property for this file!", "Event Triggers Not Set") Exit Sub End Try End Try 'Sets customIPropSet to equal the Item that contains the Event Triggers of the open document. customIPropSet = cDocument.PropertySets.Item("iLogicEventsRules") 'If there is an item (Event Trigger) found, then... If customIPropSet.Count > 0 Then 'We'll make a loop! This will go through each Property (Event Trigger), 1 Property at a time For propItemCounter = 1 To customIPropSet.Count Step 1 'This is for testing purposes. Because the individual Properties that are inside of a PropertySet 'are not exposed in VB.Net outright, you can uncomment the two lines of code below to allow you to 'see each Property as it goes through the count, one by one. '---------------------------- Dim megatest As [Property] megatest = customIPropSet.Item(propItemCounter) Dim nameTest As String nameTest = Strings.Left(customIPropSet.Item(propItemCounter).Name, Len(TriggerPropName)) Dim idTest As Integer idTest = (Convert.ToInt32(Math.Abs(customIPropSet.Item(propItemCounter).PropId / 100))) * 100 If idTest = BaseID Then 'If the property name is equal, then we need to see if the Value of the property (AKA: The Rule Name) is 'equal to the new rule that we made previously. If it is, Then we're all done! The trigger already exists! If customIPropSet.Item(propItemCounter).Value = RuleName Then Exit Sub Else 'UPDATE Feb/3/2012 'Because of the unexpected things that I talk about above, I am making another statement here that will check 'to see if the PropertyName equals our TriggerPropName. If nameTest = TriggerPropName Then 'If that isn't the case, it's alright. But, we'll need to get that numerical value from the end of the current Property 'because we will need it later to determine which numerical value is needed for the end of our PropertyName. 'The below line is simply looking at the last character of the current Property's Name, which should be a number, and making sure that it IS a number If IsNumeric(Strings.Right(customIPropSet.Item(propItemCounter).Name, Len(customIPropSet.Item(propItemCounter).Name) - Len(TriggerPropName))) = True Then 'If it is a number, then we need to convert that value (because it's still a String), into an Integer, and set CurrentEnd to equal it. CurrentEnd = Convert.ToInt16(Strings.Right(customIPropSet.Item(propItemCounter).Name, Len(customIPropSet.Item(propItemCounter).Name) - Len(TriggerPropName))) 'We'll also go on and grab the Property ID of the open Property CurrentID = customIPropSet.Item(propItemCounter).PropId 'If the CurrentEnd is larger than our EndHolder (which was defined at the beginning of this sub), Then we need our EndHolder to 'equal 1 more than what is currently in use. Remember that we can't end up with two 'AfterDocSave0' entries. If CurrentEnd > EndHolder Then EndHolder = CurrentEnd + 1 ElseIf CurrentEnd = EndHolder Then 'However, if CurrentEnd is = to EndHolder, then we'll just add 1 to it. EndHolder = EndHolder + 1 End If 'We do the same thing for the Property ID. If CurrentID > TriggerID Then TriggerID = CurrentID + 1 ElseIf CurrentID = TriggerID Then TriggerID = TriggerID + 1 End If Else 'If for some reason that last character in the Property Name isn't a numeric value, then we have more work to do... MessageBox.Show("The end string for Var: CurrentEnd, was not a Numeric Value. If you're reading this, something needs debugging!", "Error in SetEventProperty Sub") Exit Sub End If Else CurrentEnd = (customIPropSet.Item(propItemCounter).PropId - BaseID) CurrentID = customIPropSet.Item(propItemCounter).PropId If CurrentEnd > EndHolder Then EndHolder = CurrentEnd + 1 ElseIf CurrentEnd = EndHolder Then EndHolder = EndHolder + 1 End If If CurrentID > TriggerID Then TriggerID = CurrentID + 1 ElseIf CurrentID = TriggerID Then TriggerID = TriggerID + 1 End If End If End If End If 'Now we do this all over again for every other item/property (if there are any) in the list 'each time checking to see if the Trigger already exists, or incrementing the values of the numeric ending for the PropertyName 'and PropertyID. Next End If 'Now that we're done gathering all of the data on the existing Properties / Items, let's add our own to the list. 'We call up our CustomIPropSet.Add. 'RuleName will be the name of the rule we made earlier. 'TriggerPropName & EndHolder equals the BaseID + the numeric value needed to show when it will fire. 'TriggerID is the new PropertyID that is next in line for use by Inventor. Call customIPropSet.Add(RuleName, TriggerPropName & EndHolder, TriggerID) 'Let's end this sub! 'Now we go all the way back to the sub Button1_Click 'Whew! End Sub