Hi, I have a external rule that I would like to add a trigger to itself (or interna rule) once triggered. I can't seem to find a good way to do this, so suggenstions are appreciated.
Have a nice day.
Hi,
It is not very clear what you are asking because a rule that triggers itself would cause an endless loop.
If you want to make sure that another rule is triggered after the first, you could add a user defined parameter at the end of the first rule:
trigger_01=trigger_01+1
and at the beginning of the last.
dim trigger As trigger_01
Because the rule updates the parameter, all rules that contain this parameter will be excecuted.
Hope this helps!
I think i might have explained poorly. What I want is for the rule to set a trigger for itself or other rules in the prat/assembly, for example, "run before save".
Hi Tigerpirro
It looks like asked about this a few months ago, did that suggestion not work?
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
Hi Tigerpirro,
Here's something I have on hand, that I've tried in the past but can not get to work. Maybe @MjDeck could help us with this?
I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com
'On Error Resume Next Auto = iLogicVb.Automation Dim iLogicAuto As Object iLogicAuto = Auto Dim oiLogicDoc As Document 'capture current document oiLogicDoc = ThisApplication.ActiveDocument 'load up ilogic automation Dim oiLogicAuto As Object = iLogicVb.Automation 'Get container For all Of the ilogic rules Dim oRules As Object = oiLogicAuto.rules(oiLogicDoc) 'variable For the events rules Dim oiLogicPropSet As PropertySet 'Set our Property Set since we know it exists Now oiLogicPropSet = oiLogicDoc.PropertySets.Item("iLogicEventsRules") oiLogicPropSet.Add("Rule2", "BeforeDocSave0", 700) 'oiLogicPropSet.Add("N:\CAD\Inventor\iLogic\myExternalRule.iLogicVb", "BeforeDocSave0", 700) '[ 'Event Trigger 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 ']
I can't get it to run. Everything seems to be working smothly up to the last two codes.
SyntaxEditor Code Snippet
'Set our Property Set since we know it exists Now oiLogicPropSet = oiLogicDoc.PropertySets.Item("iLogicEventsRules") 'oiLogicPropSet.Add("A", "BeforeDocSave0", 700) 'oiLogicPropSet.Add("C:\_Wahlter\Anpassningar_Library\iLogic\U.txt", "BeforeDocSave0", 700)
Hopefully this works a little better. It's using code I've gathered from the multiple threads about the issue. Largely MegaJerk's contributions.
Sub Main() 'On Error Resume Next Dim oiLogicDoc As Document oiLogicDoc = ThisApplication.ActiveDocument Dim oiLogicPropSet As PropertySet oiLogicPropSet = GetiLogicEventPropSet(oiLogicDoc)
'If adding more than 1 trigger of the same type, you need to increment the values;
oiLogicPropSet.Add("Rule2", "BeforeDocSave0", 700) 'oiLogicPropSet.Add("Rule3", "BeforeDocSave1", 701)
'oiLogicPropSet.Add("N:\CAD\Inventor\iLogic\myExternalRule.iLogicVb", "BeforeDocSave0", 700)
End Sub
Function GetiLogicEventPropSet(cDocument As Document) As Inventor.PropertySet
On Error Resume Next
iLogicEventPropSet = cDocument.PropertySets.Item("iLogicEventsRules")
If iLogicEventPropSet Is Nothing Then
iLogicEventPropSet = cDocument.PropertySets.Item("_iLogicEventsRules")
End If
If iLogicEventPropSet.InternalName <> "{2C540830-0723-455E-A8E2-891722EB4C3E}" Then
Call iLogicEventPropSet.Delete
iLogicEventPropSet = cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
End If
If iLogicEventPropSet Is Nothing Then
iLogicEventPropSet = cDocument.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
End If
If iLogicEventPropSet 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 iLogicEventPropSet
End Function
@MechMachineMan, the above code only seems to work if there are no existing rules under the desired trigger. Is this true? Can this be fixed?
Also, it throws an error if the desired rule is already under the desired trigger. Can this be fixed as well?
Also, do you have any code for deleting a rule of a specific name under a specific trigger if it's already there?
@MechMachineMan wrote:
As I have commented in the code, if you want to use the same trigger for
various rules, you have to increment the values in the line.
I realized what I was doing wrong. I had 3 existing rules under my trigger, so I had to change the third argument number to 704 (which I did), but I also had to increment the second argument (the name) to "BeforeDocSave4".
I guess, as a precaution in case there are already several existing triggers, I should assign it a large number like "750" and "BeforeDocSave50"? Is that reasonable?
@MechMachineMan wrote:
Yes, just delete the iProperty that has the corresponding information.
Probably best done as a function that loops through the propertyset;
I.e.;
Sub DeleteTrigger(oEventPropset As PropertySet, oRuleName as String,
oTriggerType As String)
.. code
End Sub
Hm ok, I'll see if I can come up with the code to loop through the correct trigger and delete the correct rule.
Thanks.
Here is a sample of a sub to delete a specific trigger for a specific rule.
'PropValue is Rule Name
'PropName is the TriggerName
'PropID is the Trigger #
Sub DeleteTrigger(oEventPropset As PropertySet, oPropValue As String, oPropName As String) Dim oProp As Inventor.Property For j = oEventPropSet.Count To 1 Step-1 oProp = oEventPropSet.Item(j) If oProp.Value = oPropValue And oProp.Name Like oPropName & "*" oProp.Delete Exit Sub End If Next End Sub
to implement it, remove the # at the end of the Trigger name as shown in the example below and it will delete it regardless of the increment.
SyntaxEditor Code Snippet
Sub Main() Dim EventPropSet As Inventor.PropertySet EventPropSet = GetiLogicEventPropSet(ThisApplication.ActiveDocument) 'Call ListiProps(EventPropSet) Call DeleteTrigger(EventPropset, "Rule0", "BeforeDocSave") End Sub
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.
Good day DRoam I have used the rule and it works great but I need help with adding external rules, when I add an external rule it adds it to event trigger but it doesn't trigger how do I add the file path of the external rules to your rule please help.
thanks Dean
H @Dean.kichenbrand, sure, I think I know what you need to do. Just go to the Tools tab, click the bottom "Options" button with the down arrow (the title of the panel), and click "iLogic Configuration".
There you can add the path of your external iLogic rules. Keep in mind that Inventor does NOT automatically include sub-folders of the directories you add. So you'll need to add each folder that actually contains your rules, you can't just add a top-level folder that contains folders with rules.
I've been using the rules above in my workflow for some time now and they've been working great, glad they're working for you, too!
Thanks for the reply the problem is that after I run the rule to add the external rules to the event trigger the triggers doesn't work but the external rules name is there but its almost like they're not linked or the rule can't be found as shown on the picture so what I'm asking can I add a path to the rule in the rule.
example:
SharedVariable("myRuleName") = "C:\ILOGIC RULES\AREA" 'This is the name of your rule. Include the full path for an external rule.
or is there something I did wrong
thanks a lot
Dean Kichenbrand
Ah you know what, I completely forgot that I couldn't get the code to work right with adding External rules. What I ended up doing was adding some lines to the beginning of the first code snippet (the shorter one, that you have to add to your "source rule") to first create a LOCAL iLogic rule which contains a single line commanding Inventor to fire the desired External rule. And then you use the remaining lines to fire the supplementary External Rule (the long one) which adds the newly-created local rule to the Event Trigger of choice. It ended up looking like this:
Sub Main Dim RuleName As String = "Before Save Document" ' This is the name of your local rule that will fire the desired External rule Dim RuleText As String = "iLogicVb.RunExternalRule(""SmartPart Before Save Document"")" 'Leave all quotation marks and change the text inside the quotation marks to the name of the External Rule you wish to trigger (do not include the Path) Dim oDoc As Document = ThisDoc.Document Dim iLogicAddIn As ApplicationAddIn = ThisApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}") ' Get the iLogic automation object Dim iLogic As Object = iLogicAddIn.Automation ' Get the list of iLogic rules in the current Inventor document Dim oRuleList As Object = iLogic.Rules(oDoc) If oRuleList Is Nothing Then Goto AddRuleStart Dim oRule As Object For Each oRule In oRuleList If oRule.Name = RuleName Then ' A rule with the same name already exists... Exit Sub End If Next AddRuleStart: iLogic.AddRule(oDoc, RuleName, RuleText) SharedVariable("myRuleName") = "Before Save Document" '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.Remove("myRuleName") SharedVariable.Remove("myTrigger") SharedVariable.Remove("myAction") End Sub
So not as nice and compact as originally. I think I could change things around a bit to make the code you have to put in your "source rule" more compact. If I get the chance I'll do that.
But unfortunately this is the best solution for now. I don't think there's a reliable way to add external rule triggers using iLogic. Our best hope is for this idea to be implemented: Add iLogic functions for handling Event Triggers. Please give it a vote if you haven't already. Thanks!
Can't find what you're looking for? Ask the community or share your knowledge.