Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Is it possible to add an event trigger using iLogic

7 REPLIES 7
Reply
Message 1 of 8
Anonymous
1563 Views, 7 Replies

Is it possible to add an event trigger using iLogic

Is it Possible to add an event trigger using iLogic? 

 

I've come up with a nice little rule that i want to run on existing drawings. Going forward, it's easy...i add the rule to a template. Retro is a bit more challenging. 

7 REPLIES 7
Message 2 of 8
AMN3161
in reply to: Anonymous

Yes there is, someone on the forums just helped me with this

 

Do you want a external rule to add something to the event trigger?

 

 

Below will look for a rule in the active document and add it to the event trigger for before save


Dim oDoc As Inventor.Document = ThisApplication.ActiveDocument
Dim oRuleName As String = "Master_Parts_List_Assembly"
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRule As iLogicRule
Try
        oRule = oAuto.GetRule(oDoc,oRuleName)
Catch
        MsgBox("The specified rule was not found in the active document. Exiting.",vbOKOnly," ")
        Exit Sub
End Try

''Added to create iLogicEventRules property set if it does not exist.
Try
        customIPropSet = oDoc.PropertySets.Item("iLogicEventsRules")
Catch
End Try
Try
        If customIPropSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then
               Call customIPropSet.Delete
               customIPropSet = oDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
        End If
Catch 
End Try
Try
        If customIPropSet Is Nothing Then
               customIPropSet = oDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
        End If
Catch 
End Try
''End of code to create property set

Dim oETPropSet As PropertySet
Try
        oETPropSet = oDoc.PropertySets.Item("iLogicEventsRules")
Catch
        oETPropSet = oDoc.PropertySets.Item("_iLogicEventsRules")
End Try
If oETPropSet Is Nothing Then
        MsgBox("Couldn't find the Event Triggers property set. Exiting.", vbOKOnly + vbExclamation, " ")
        Exit Sub
End If

Dim oProperty As Inventor.Property
Dim oPropId As Long
For oPropId = 700 To 799
        Try
               oProperty = oETPropSet.ItemByPropId(oPropId)
               If oProperty.Value = oRuleName Then
                       MsgBox("This rule has already been added to that event trigger. Exiting.",vbOKOnly," ")
                       Exit Sub
               End If
        Catch
               oProperty = oETPropSet.Add(oRuleName, "BeforeDocSave" & oPropId, oPropId)
               Exit Sub
        End Try
        oPropId = oPropId + 1
Next

@WCrihfield the one who made this not me, but i have a different external rule to insert a rule called "Master_Parts_List_Assembly" in the active part that is currently opened. Then another external rule that will add it to the before save trigger


Message 3 of 8
WCrihfield
in reply to: Anonymous

Can you provide a more details about your situation and what you're wanting to accomplish?

Do you want this rule to run for All drawing documents, or only for certain drawing documents?

Which event to you want to trigger your rule to run?

Is your new rule local (saved within the drawing document itself) or is it an external rule?

(I'm assuming it is a local rule, since you said you've included it in a template drawing file.)

If it is a local rule, is there a reason it must remain a local rule and not an external rule?

If you want the rule to remain a local rule, and you want to add this rule to older drawing documents too, I can show you how to 'inject' that rule into other documents (just ask).

What is your rule supposed to do?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 8
Anonymous
in reply to: WCrihfield

@WCrihfield 

I have a rule that "processes"  my part document from my drawing document as long as the part file is set to trigger before the document is saved. This works beautifully in new parts using a template. For existing parts, the triggers aren't setup to fire before saving. Can I start the ilogic rule in my drawing with code that first says "add these triggers to the part document that fire before saving" then Refdoc.Save()...they fire then all is well.

Message 5 of 8
jzcrouse
in reply to: Anonymous

We ran into this at my work with multiple older parts having outdated local rule event triggers. We switched to global rules since we occasionally alter them, but obviously the old parts/rules are out of date. so I made this with some help from some of @MegaJerk 's work. 

(https://forums.autodesk.com/t5/inventor-customization/adding-an-ilogic-rule-to-an-event-trigger-wit...

 

We only have a couple rules to add so I just did it the long way but you could use a for each function to make this go faster, especially with more rules. I found it was easier to have a global form that any user can use to do reset event triggers rather than have every one have the code injector. 


Dim
doc As Document = ThisApplication.ActiveDocument
'CHECK TO SEE IF EVENT TRIGGER PROPERYSET EXISTS Try Call doc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}") Catch End Try 'DELETES ANY PREEXISTING EVENT TRIGGERS. Dim eventrig As PropertySet = doc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}") Call eventrig.Delete 'REESTABLISHES THE EVENT TRIGGER PROPERTY SET Call doc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}") 'SET LOCATION OF EVENT TRIGGERS. Dim rulearray() As String = {"IPROPERTIES_UPDATE PART", "MASS_UPDATE_RULE"} Dim locarray() As String = {"BeforeDocSave", "AfterAnyiPropertyChange", "PartBodyChanged" } Dim idarray() As Integer = {700,1600,1200} eventrig = doc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}") 'ADD EVENT TRIGGERS Call eventrig.Add(rulearray(0), locarray(0), idarray(0)) Call eventrig.Add(rulearray(0), locarray(1), idarray(1)) Call eventrig.Add(rulearray(0), locarray(2), idarray(2))

'ADDING THE 1 TO THE LOCATIONARRAY AND ID ARRAY IS NECESSARY TO ADD MULTIPLE RULES AFTER THE ONES ABOVE.
'IF A 3RD RULE IS ADDED TO THE SAME EVENT TRIGGER "2" WOULD BE USED AND SO ON... Call eventrig.Add(rulearray(1), locarray(0)&"1", idarray(0)+1) Call eventrig.Add(rulearray(1), locarray(1)&"1", idarray(1)+1)

'EVENT TIGGER NAMES AND 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
Message 6 of 8
WCrihfield
in reply to: Anonymous

If your drawing document only has one model document being represented within it, you should be able to use this line to get the model file:  (the single model document can be either a Part or an Assembly)

 

Dim oMDoc As Document = ThisDrawing.ModelDocument

 

 But, if your drawing document is showing multiple different model documents, we will have to loop through each DrawingView and get the model document being represented within each view.

 

Another thing I still don't know is if your 'older' model documents already contain the needed rules that you wish to add to the model's Event Triggers.  If the target rules aren't already in the older model files, then where are the rules you are wanting to add to the Event Triggers of the model file; or how do you plan to supply/specify which rules to add to the model's event triggers?

 

   The following iLogic rule assumes the active drawing document is only representing a single model document, and that the target rules are already within the model document.  You just need to edit this code to change the names of the model rules you want it to add to the model's Event Triggers.  Right now I'm just specifying 3 generic rule names, but you can have as many as 99 or as few as 1.  If you only need to add 1 rule, you can get rid of the List object and the loop through the List's contents.

   This rule also assumes you don't need to preserve any existing Event Triggers settings, because it deletes the existing property set that contains those setting (if they exist), then recreates that property set.  Then it adds each of your specified rule names to the 'BeforeDocSave' event.

 

So...if you run the following rule as an external rule, while the drawing document is active, it will get the model document, get its Event Triggers property set, then add the rule names (specified within this rule) to the BeforeDocSave event of the event triggers of the model file.  If any of those rules don't exist within the model file, it will let you know then exit the rule.

 

Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oMDoc As Document = ThisDrawing.ModelDocument

Dim oRuleNames As New List(Of String)
oRuleNames.Add("First Rule Name") '<<<<<<<!!! EDIT THIS RULE NAME !!! >>>>>>>>>
oRuleNames.Add("Second Rule Name") '<<<<<<<!!! EDIT THIS RULE NAME !!! >>>>>>>>>
oRuleNames.Add("Third Rule Name") '<<<<<<<!!! EDIT THIS RULE NAME !!! >>>>>>>>>

'Making sure those rules exist within the model document
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRule As iLogicRule
Dim oRuleName As String
For Each oRuleName In oRuleNames
	Try
		oRule = oAuto.GetRule(oMDoc, oRuleName)
	Catch
		MsgBox("The rule named " & oRuleName & " was not found within the model document. Exiting.", vbOKOnly + vbCritical, " ")
		Exit Sub
	End Try
Next

'Getting the Event Triggers iProperty set
Dim oEventsPropSet As Inventor.PropertySet
Dim oSetName As String = "iLogicEventsRules"
Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
Try
	oEventsPropSet = oMDoc.PropertySets.Item(oSetName)
Catch
	oEventsPropSet = oMDoc.PropertySets.Item("_" & oSetName)
End Try
If oEventsPropSet IsNot Nothing Then
	oEventsPropSet.Delete
	oEventsPropSet = oMDoc.PropertySets.Add(oSetName, oInternalName)
Else
	oEventsPropSet = oMDoc.PropertySets.Add(oSetName, oInternalName)
End If

'Define the event's PropId range
Dim oRangeStart As Integer = 700
Dim oRangeEnd As Integer = 799
Dim oPropId As Integer = oRangeStart

'Start adding the rule names to the event triggers
For Each oRuleName In oRuleNames
	If oPropId <= oRangeEnd Then
		oEventsPropSet.Add(oRuleName, "BeforeDocSave" & oPropId.ToString, oPropId)
	Else
		MsgBox("You've reached the maximum number of rules you can add under this event. Exiting.", vbOKOnly, " ")
		Exit Sub
	End If
	oPropId = oPropId + 1
Next
Exit Sub

 

I also have multiple resources available dealing with the (hidden by default) Event Triggers property set.

In these resources I attempt to explain how everything works and how to do it yourself using iLogic code.

One shows how to 'Un-Hide' this set.

Another shows how to find & inspect an existing property set to confirm table data, then how to add a single rule to it.

Another shows how to copy all local iLogic rules as well as the Event Triggers settings from one document to another.

Another post, that may help with the situation of working with older documents, shows how to use an external iLogic rule to create & place local iLogic rules within other documents.

 

Also, FYI @jzcrouse:

 Within those reference posts you'll find a table which shows updated data about which events use which PropId ranges.  I built this as an Excel spreadsheet from personal research and testing, instead of copying from old posts.  One of the main differences is the 'AfterDrawingViewsUpdate' event.  The older posts show it using the 1500 to 1599 range, but my testing shows this range to actually be 1800 to 1899.  This data was captured from actually adding multiple rules to each of the different types of events within all the different types of documents, then afterwards, capturing all the data from the resulting property set and all its properties.  Each time you edit or update a 'Contribution' post, it updates the main date on the post, and you'll see that all these posts have been recently updated with the latest findings.  I don't recall exactly when the 'AfterDrawingViewsUpdate' event changed from using the 1500's to the 1800's, though due to having skipped over an Inventor release here and there and having had multiple Autodesk Accounts over the years.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 8
MegaJerk
in reply to: WCrihfield

Thank you for keeping up with this stuff @WCrihfield

 

Though I haven't used Inventor for a very long time now, it's good to see people still updating old work and keeping the community up to date.

Below is a table of your new PropID Ranges for Event Triggers. I'll do my best to update my old work to point to some of yours to prevent any confusion for future people. Because this forum doesn't allow you to edit older posts (which is unfortunate to say the least), I won't resurrect older threads, but will try to point people towards your stuff when I am notified of activity. At least I'll be able to fix github.

Additionally, I noticed you had trouble pasting your table here to the forums so I'll also include the html code you can plonk right into the 'HTML' source in any given post on these forums. 

 

Name (Name Shown in Dialog) Property Name (& DisplayName) PropId Range Doc Types Available In
After Open DocumentAfterDocOpen400 to 499Assembly, Part, Drawing
Close DocumentDocClose500 to 599Assembly, Part, Drawing
Before Save DocumentBeforeDocSave700 to 799Assembly, Part, Drawing
After Save DocumentAfterDocSave800 to 899Assembly, Part, Drawing
After Model Parameter ChangeAfterAnyParamChange1000 to 1099Assembly, Part
Part Geometry ChangePartBodyChanged1200 to 1299Part
Material ChangeAfterMaterialChange1400 to 1499Part
iProperty ChangeAfterAnyiPropertyChange1600 to 1699Assembly, Part, Drawing
Drawing View ChangeAfterDrawingViewsUpdate1800 to 1899Drawing
Feature Suppression ChangeAfterFeatureSuppressionChange2000 to 2099Part
Component Suppression ChangeAfterComponentSuppressionChange2200 to 2299Assembly
iPart or iAssembly Change ComponentAfterComponentReplace2400 to 2499Assembly
New DocumentAfterDocNew2600 to 2699Assembly, Part, Drawing



Table Code:

 

<table style="width:100%">
    <tbody>
        <tr>
            <td>
                <strong>
                    Name (Name Shown in Dialog)
                </strong>
            </td>
            <td>
                <strong>
                    Property Name (& DisplayName)
                </strong>
            </td>
            <td>
                <strong>
                    PropId Range
                </strong>
            </td>
            <td>
                <strong>
                    Doc Types Available In
                </strong>
            </td>
        </tr>
        <tr>
            <td>
                After Open Document
            </td>
            <td>
                AfterDocOpen
            </td>
            <td>
                400 to 499
            </td>
            <td>
                Assembly, Part, Drawing
            </td>
        </tr>
        <tr>
            <td>
                Close Document
            </td>
            <td>
                DocClose
            </td>
            <td>
                500 to 599
            </td>
            <td>
                Assembly, Part, Drawing
            </td>
        </tr>
        <tr>
            <td>
                Before Save Document
            </td>
            <td>
                BeforeDocSave
            </td>
            <td>
                700 to 799
            </td>
            <td>
                Assembly, Part, Drawing
            </td>
        </tr>
        <tr>
            <td>
                After Save Document
            </td>
            <td>
                AfterDocSave
            </td>
            <td>
                800 to 899
            </td>
            <td>
                Assembly, Part, Drawing
            </td>
        </tr>
        <tr>
            <td>
                After Model Parameter Change
            </td>
            <td>
                AfterAnyParamChange
            </td>
            <td>
                1000 to 1099
            </td>
            <td>
                Assembly, Part
            </td>
        </tr>
        <tr>
            <td>
                Part Geometry Change
            </td>
            <td>
                PartBodyChanged
            </td>
            <td>
                1200 to 1299
            </td>
            <td>
                Part
            </td>
        </tr>
        <tr>
            <td>
                Material Change
            </td>
            <td>
                AfterMaterialChange
            </td>
            <td>
                1400 to 1499
            </td>
            <td>
                Part
            </td>
        </tr>
        <tr>
            <td>
                iProperty Change
            </td>
            <td>
                AfterAnyiPropertyChange
            </td>
            <td>
                1600 to 1699
            </td>
            <td>
                Assembly, Part, Drawing
            </td>
        </tr>
        <tr>
            <td>
                Drawing View Change
            </td>
            <td>
                AfterDrawingViewsUpdate
            </td>
            <td>
                1800 to 1899
            </td>
            <td>
                Drawing
            </td>
        </tr>
        <tr>
            <td>
                Feature Suppression Change
            </td>
            <td>
                AfterFeatureSuppressionChange
            </td>
            <td>
                2000 to 2099
            </td>
            <td>
                Part
            </td>
        </tr>
        <tr>
            <td>
                Component Suppression Change
            </td>
            <td>
                AfterComponentSuppressionChange
            </td>
            <td>
                2200 to 2299
            </td>
            <td>
                Assembly
            </td>
        </tr>
        <tr>
            <td>
                iPart or iAssembly Change Component
            </td>
            <td>
                AfterComponentReplace
            </td>
            <td>
                2400 to 2499
            </td>
            <td>
                Assembly
            </td>
        </tr>
        <tr>
            <td>
                New Document
            </td>
            <td>
                AfterDocNew
            </td>
            <td>
                2600 to 2699
            </td>
            <td>
                Assembly, Part, Drawing
            </td>
        </tr>
    </tbody>
</table>

 

(note: I'm not using the <th>Table Header</th> tags because they end up being ignored, so it's <td>Table Data</td> all the way. ;)) 

 



If my solution worked or helped you out, please don't forget to hit the kudos button 🙂
iLogicCode Injector: goo.gl/uTT1IB

GitHub
Message 8 of 8
WCrihfield
in reply to: MegaJerk

Sweet!  Thank you.  And thanks for the pioneering work you did in discovering this functionality in the first place.  You provided some good foundations for others to later build upon in their quests to further 'the cause' of Autodesk Automation and the creation of more and better custom automation solutions.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report