Ilogic rules, creating triggers

Ilogic rules, creating triggers

Thetigerpirro
Advocate Advocate
6,192 Views
51 Replies
Message 1 of 52

Ilogic rules, creating triggers

Thetigerpirro
Advocate
Advocate

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.

0 Likes
6,193 Views
51 Replies
Replies (51)
Message 21 of 52

DRoam
Mentor
Mentor

Hallelujah! In my efforts to make the code more concise, I stumbled upon how to properly add an external trigger! The solution was so simple, I can't believe I never tried it before. All I had to do was a quick test on an already-existing proper external rule trigger, and see what Inventor called it. Turns out Inventor names External iLogic triggers in this way:

 

"file://[parent folder]\[rule name]"

 

And that's it! Once I incorporated that into my code, it's able to add and remove external rule triggers perfectly.

 

So here are the modified "source code" and "supporting rule". As before, the "supporting rule" code must go in an external rule called "EventTriggerSet". Then you may simply add the "source code" lines to any rule from which you want to check for an Event Trigger. Simply change the values as specified in the comments to suit what you're trying to accomplish.

 

Source Code (place in rule from which you want to check for Event Triggers):

'The code below will fire an external iLogic rule called "EventTriggerSet" which will add/remove a rule to/from the specified trigger.

SharedVariable("myRuleName") = "Rule1"                  'Specify the name of the rule you wish to be triggered. If the rule is external, precede the name by "[parent folder]\", i.e. "iLogic Folder\My Rule"
SharedVariable("myRuleLocation") = "Local"              'Specify if the rule is "Local" or "External"
SharedVariable("myTrigger") = "Before Save Document"    'Specify the name of the trigger for the rule (exactly as it appears in the Event Triggers dialog box)
SharedVariable("myAction") = "Add"                      'Specify whether you want to "Add" or "Remove" the rule from the trigger
	
iLogicVb.RunExternalRule("EventTriggerSet")

SharedVariable.Remove("myRuleName")
SharedVariable.Remove("myRuleLocation")
SharedVariable.Remove("myTrigger")
SharedVariable.Remove("myAction")

 

Supporting Rule (place in an External rule called "EventTriggerSet"):

Sub Main()
	'This code will add/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 variable specifies the name of the rule you wish to be triggered
		myRuleLocation = SharedVariable("myRuleLocation") 'This variable specifies if the rule is "Local" or "External"
		myTrigger = SharedVariable("myTrigger")           'This variable specifies the name of the trigger for the rule (exactly as it appears in the Event Triggers dialog box)
		myAction = SharedVariable("myAction")             'This variable specifies whether to "Add" or "Remove" the rule from the trigger
	Catch
		MessageBox.Show("The required inputs were not sent received." & vbNewLine & vbNewLine & "Exiting rule.", "EventTriggerSet Error",MessageBoxButtons.OK,MessageBoxIcon.Error)
		Exit Sub
	End Try
		
	Call SetRuleTrigger(myRuleName, myRuleLocation, myTrigger, myAction)
	
End Sub

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

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

 

 

 

 

It's probably still not perfect and may need some debugging, so anyone who experiences issues please post them here.

 

Message 22 of 52

Dean.kichenbrand
Contributor
Contributor

awesome!!! Thanks a lot for the help!! defiantly going to use it and let you know if there are any bugs!!.

 

 

0 Likes
Message 23 of 52

MechMachineMan
Advisor
Advisor

I believe you are missing some logic:

 

If a DIFFERENT rule of triggerid = 400 already exists that you want to keep, adding a new rule should make the new triggerid = 401 and so on.

 

Don't quote me on it though!


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 24 of 52

Dean.kichenbrand
Contributor
Contributor

Hi DRoam, I was wondering is it posable to convert this to VBA?? ,instead of ilogic code.

0 Likes
Message 25 of 52

BrandonW9
Advocate
Advocate

Just wanted to say thank you! This piece of code works well!

 

I just wanted a way for people to easily set event triggers for any of the filetypes because we have a piece of ilogic that runs in each of them, so i modified the main sub as follows.

 

I'm sure theres a more elegant way to do this, but this is working for me right now:

Sub Main()
	'This code will add/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".
	Select Case ThisDoc.Document.DocumentType
		Case kPartDocumentObject
			Try
				myRuleName = "PART iLogic Tools"       'Specify the name of the rule you wish to be triggered. If the rule is external, precede the name by "[parent folder]\", i.e. "iLogic Folder\My Rule"
				myRuleLocation = "External"              			'Specify if the rule is "Local" or "External"
				myTrigger = "Before Save Document"   				'Specify the name of the trigger for the rule (exactly as it appears in the Event Triggers dialog box)
				myAction = "Add"                      				'Specify whether you want to "Add" or "Remove" the rule from the trigger
			Catch
				
			End Try
				
			Call SetRuleTrigger(myRuleName, myRuleLocation, myTrigger, myAction)
			Try
				myRuleName = "RenameImates"       
				myRuleLocation = "External"              			
				myTrigger = "Before Save Document"   				
				myAction = "Add"                      			
			Catch
				
			End Try
				
			Call SetRuleTrigger(myRuleName, myRuleLocation, myTrigger, myAction)
		Case kAssemblyDocumentObject
			Try
				myRuleName = "ASSY iLogic Tools"      
				myRuleLocation = "External"              			
				myTrigger = "Before Save Document"   				
				myAction = "Add"                      				
			Catch
	
			End Try
			
			Call SetRuleTrigger(myRuleName, myRuleLocation, myTrigger, myAction)
		Case kDrawingDocumentObject
			Try
				myRuleName = "DRAWING iLogic Tools"      
				myRuleLocation = "External"              			
				myTrigger = "Before Save Document"   				
				myAction = "Add"                      				
			Catch
	
			End Try
			
			Call SetRuleTrigger(myRuleName, myRuleLocation, myTrigger, myAction)
	End Select
End Sub
Message 26 of 52

BrandonW9
Advocate
Advocate

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.

0 Likes
Message 27 of 52

Curtis_Waguespack
Consultant
Consultant

@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.

 

 

1.PNG

 

 

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.

 

1.PNG

 

2.PNG

 

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

EESignature

0 Likes
Message 28 of 52

BrandonW9
Advocate
Advocate

Wow! Thanks! Maybe ill have to try to make a new version of my rule and see if i can get it to work.

Message 29 of 52

Anonymous
Not applicable

Hello !

 

you can set different triggers under the Manage tab --> iLogic --> Event Triggers. All you have to do is drag and drop your rule under the condition that you want it to trigger. See picture for visual.

trigger.png

 

Hope that helps !

0 Likes
Message 30 of 52

dariuszm
Advocate
Advocate

HI,

 

I was trying to use this iLogic rule and I have same problem all the time like on this screencast.

 

The attached rule are not triggering until you add another (or same) trigger manually. After You do some manual move in this file, then it's "repair" and You cant use it.

 

In attachment I'm sending file without triggers. Can You help me resolve this- is it working for You?

0 Likes
Message 31 of 52

MjDeck
Autodesk
Autodesk

@dariuszm , here's a workaround to fix the problem of adding triggers to a file that has no triggers and no rules.
In the rule above, replace this line:

iLogicVb.RunExternalRule("EventTriggerSet")

With this:

' To make sure the document has an iLogic DocumentInterest, add a temporary rule
Dim tempRule = iLogicVb.Automation.AddRule(ThisDoc.Document, "TemporaryRule_392856A2", "")

iLogicVb.RunExternalRule("EventTriggerSet")

iLogicVb.Automation.DeleteRule(ThisDoc.Document, tempRule.Name)

The document needs an iLogic DocumentInterest, or the event triggers won't work.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 32 of 52

dariuszm
Advocate
Advocate
Hi,

This is solution. I ask Autodesk support about this, and they give me same workaround.
Hope this issue will be resolve soon.
0 Likes
Message 33 of 52

DRoam
Mentor
Mentor

FYI, for anyone who hasn't yet, please add your vote to this idea:

 

Idea: Add iLogic functions for handling Event Triggers

 

Hopefully enough votes will get this added to the iLogic team's backlog. Would be great to have built-in functions for this rather than messing around with mile-long scripts and precarious workarounds.

0 Likes
Message 34 of 52

griptechshop
Advocate
Advocate

Hi MjDeck,

I have exactly the same problem as @dariuszm. I tried your solution but nothing changes... The rule appears under the trigger, the content of the rule is the right one but it doesn't trigger when it should... Nothing happens until I change the triggers manually.

Do you have any suggestions? Like, is there a code to simulate a manual change?

Thank you,

0 Likes
Message 35 of 52

MjDeck
Autodesk
Autodesk

@griptechshop , which version of Inventor are you using?
Can you post your rule that adds the trigger?


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 36 of 52

griptechshop
Advocate
Advocate
@MjDeck , I am using Autodesk Inventor Professional 2020 (64-Bit).

Here are the rules I use:

RULE1:

'The code below will fire an external iLogic rule called "EventTriggerSet" which will add/remove a rule to/from the specified trigger.

SharedVariable("myRuleName") = "C:\Users\Griptech\Documents\Inventor\GRIPTECH CAD FILES\RULES\UPDATE_DIMENSIONS.iLogicVb"                  'Specify the name of the rule you wish to be triggered. If the rule is external, precede the name by "[parent folder]\", i.e. "iLogic Folder\My Rule"
SharedVariable("myRuleLocation") = "External"              'Specify if the rule is "Local" or "External"
SharedVariable("myTrigger") = "Part Geometry Change"    'Specify the name of the trigger for the rule (exactly as it appears in the Event Triggers dialog box)
SharedVariable("myAction") = "Add"                      'Specify whether you want to "Add" or "Remove" the rule from the trigger
	
' To make sure the document has an iLogic DocumentInterest, add a temporary rule 

Dim tempRule = iLogicVb.Automation.AddRule(ThisDoc.Document, "TemporaryRule_392856A2", "")

iLogicVb.RunExternalRule("EventTriggerSet")

iLogicVb.Automation.DeleteRule(ThisDoc.Document, tempRule.Name) SharedVariable.Remove("myRuleName") SharedVariable.Remove("myRuleLocation") SharedVariable.Remove("myTrigger") SharedVariable.Remove("myAction")

 

Supporting Rule (place in an External rule called "EventTriggerSet"):

Sub Main()
	'This code will add/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 variable specifies the name of the rule you wish to be triggered
		myRuleLocation = SharedVariable("myRuleLocation") 'This variable specifies if the rule is "Local" or "External"
		myTrigger = SharedVariable("myTrigger")           'This variable specifies the name of the trigger for the rule (exactly as it appears in the Event Triggers dialog box)
		myAction = SharedVariable("myAction")             'This variable specifies whether to "Add" or "Remove" the rule from the trigger
	Catch
		MessageBox.Show("The required inputs were not sent received." & vbNewLine & vbNewLine & "Exiting rule.", "EventTriggerSet Error",MessageBoxButtons.OK,MessageBoxIcon.Error)
		Exit Sub
	End Try
		
	Call SetRuleTrigger(myRuleName, myRuleLocation, myTrigger, myAction)
	
End Sub

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

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

 

0 Likes
Message 37 of 52

MjDeck
Autodesk
Autodesk

I see the problem now. It won't work for the Part Geometry Change event. That's because iLogic has to create a new handler for that event, and it doesn't get a chance to do it in this workflow. This is another reason why we should provide a dedicated API in a future version of Inventor. 

To work around this problem you can save, close, and reopen the part. No need to open the Event Triggers dialog.


It's not a good idea to use an absolute path for the external rule name. Instead, you should use a path relative to a folder that you have set up in the Tools > Options > iLogic Configuration dialog.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes
Message 38 of 52

griptechshop
Advocate
Advocate

@MjDeck  Thanks for your answer. Unfortunately I had chosen to trigger the rule under part geometry change so that my code could execute under an "part editing in assembly" level. Closing part from there is not possible and I don't want to have to close the assembly and re open it again... 

 

Looking forward to having the dedicated API.

 

Cheers,

0 Likes
Message 39 of 52

LarsBJepsen
Advocate
Advocate

Any idea on what to do if i want to delete or create an event trigger for all drawings, and not just the active document?

0 Likes
Message 40 of 52

MjDeck
Autodesk
Autodesk

@LarsBJepsen ,the Event Triggers.png screenshot you attached shows how to do it. Event triggers in the Drawings tab apply to all drawings.


Mike Deck
Software Developer
Autodesk, Inc.

0 Likes