iLogic to create an event trigger not working

iLogic to create an event trigger not working

Coffee_CAD
Participant Participant
1,875 Views
7 Replies
Message 1 of 8

iLogic to create an event trigger not working

Coffee_CAD
Participant
Participant

(Inventor 2021.3.1)

 

Hi All,

 

I have been working through the great articles posted by @WCrihfield for using iLogic rules to add event triggers. I am running into a problem much like @gcoombridge was describing in this post: Using iLogic to create an event trigger not working (for me) . However, the solution options in that post are not ideal for my use case.

 

I have successfully used an iLogic rule to add an event trigger to the BeforeDocSave events. I will try my best to describe the behavior I am seeing.

 

When I run the rule in a part/assembly that has never had any event triggers applied, the event trigger shows up in the list but does not fire when the document is saved.

 

If I run the rule in a part/assembly that has previously had an event trigger set manually, my new trigger is added and fires correctly when the document is saved. This is true even if the property set for event triggers is completely deleted and then I use my iLogic rule to add it back.

 

It appears that something about manually adding an event trigger changes an unknown setting in the document. But when I start with a document that never had an event trigger, using my rule to automatically add the event triggers custom property set and the specific trigger ID does not result in the rule firing.

 

Can anyone help with ideas to figure out what's going on?

 

I have already modified my templates by manually adding an event trigger and then deleting the trigger and related custom property set. So any new parts created should work  fine moving forward. The problem is I have a lot of existing parts and assemblies that I would like to be able to use this rule to set the event trigger.

 

 

Accepted solutions (1)
1,876 Views
7 Replies
Replies (7)
Message 2 of 8

gcoombridge
Advisor
Advisor

Seems like there is some kind of attribute set created when you manually build one that the code isn't replicating. A bit beyond me I'm afraid and I don't think this area is documented in the programming help. It was part geometry change I was having an issue with rather than save from memory...

Use iLogic Copy? Please consider voting for this long overdue idea (not mine):https://forums.autodesk.com/t5/inventor-ideas/string-replace-for-ilogic-design-copy/idi-p/3821399
0 Likes
Message 3 of 8

WCrihfield
Mentor
Mentor

Hi @Coffee_CAD.  Thanks for the very detailed explanation about this situation.  Every little bit of constructive feedback and interaction about these things helps us all develop better and more stable solutions.  Since I'm not a seasoned software development employee at Autodesk, specializing in API matters, I don't have all the answers to why these things seem to work sometimes, for some people, and not other times, for other people.  I might even venture to speculate that they likely don't either.  Even though I have worked with these Event Triggers quite a bit through iLogic, a lot of my experience with them has been directed at work related automation solutions.  I likely haven't fully explored 'all' possible aspects this hidden system yet.  Autodesk may have a good reason for having kept this system hidden all this time.  There may be some minor 'loose ends' on the back side of this system that we just can't control/access through the iLogic Add-in.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 8

Coffee_CAD
Participant
Participant
Accepted solution

Hi@WCrihfield  and @gcoombridge, thanks for the responses. I definitely agree it is probably undocumented for a reason. However, using iLogic to add these rules to triggers is a really useful functionality.

 

One issue that may be unique to my problem is that I am trying to add an external rule to a trigger as opposed to a local rule.

 

After a lot of trial and error I came up with a solution that is working for me so far. If I assign a local iLogic rule to a trigger with my code, it will make the external rule trigger work as well. Since most of my documents won't have an existing local rule, I just added some code to temporarily create a local rule and trigger.  Then the code deletes it the temporary rule. So I never see the temporary rule or trigger and my external rule triggers as expected. My guess is adding the local rule sets whatever unknown attribute that is needed, but that is a very blind guess.

 

I am adding my code below in case it proves helpful for anyone else in the future. I have only done minimal testing on it so far and I am sure it is not as robust as it could be.

 

Dim oDoc As Document = ThisApplication.ActiveDocument

' 'file://' is the prefix added to the name of the iLogic rule for external rules
Dim sRuleName As String = "file://Set Weight"

Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRule As iLogicRule


'Apparently there is no way to check if an external rule name is valid
'the only option is to try and run it to see if it works.
'The .GetRule method does not throw an exception even if the 
'external rule name is not valid. 
'So it is not a good test to see if the external rule exists.
'Ref: https://forums.autodesk.com/t5/inventor-customization/check-if-external-ilogic-rule-is-loaded/td-p/5223581
'See message 4 of 5.


Dim oEventsPropSet As Inventor.PropertySet
Dim sSetName As String = "iLogicEventsRules"
Dim sInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"

'Check to see if the document has an existing custom property set indicating
'event triggers have been used before or are active.
'Using the internal name to avoid checking for _ before iLogicEventsRules
Try
	oEventsPropSet = oDoc.PropertySets.Item(sInternalName)
Catch
	oEventsPropSet = oDoc.PropertySets.Add(sSetName, sInternalName)
End Try
	

Dim oProperty As Inventor.Property
Dim intPropId As Integer

'Add the desired rule to the Before Document Save event.
'Start at the low end of the property ID set and move up to find the next
'available property ID.
For intPropId = 700 To 799
	Try
		oProperty = oEventsPropSet.ItemByPropId(intPropId)
		If oProperty.Value = sRuleName Then
			MsgBox("This rule has already been added to that event trigger. Exiting.",vbOKOnly," ")
			Exit Sub
		End If
	Catch
		oProperty = oEventsPropSet.Add(sRuleName, "BeforeDocSave" & intPropId, intPropId)
		Exit For
	End Try
Next


'In some cases, if the document has never had a trigger set manually or a local trigger
'added with code, the external rule trigger will show up in the list, but it will not run.
'Adding a local trigger appears to fix this problem.
'The code below makes a blank temporary iLogic rule local to the document and sets a trigger for it.
'The trigger is then deleted along with the temporary rule.


Dim sTempRuleName As String = "TempRule"
Dim bRuleExists As Boolean = False
Dim sBlank As String


'Check to see if the document already has a local rule with the same name as the sTempRuleName
'If the rule exists, we will just use it, but will be sure not to delete the rule when done.
Try
	oRule = oAuto.GetRule(oDoc, sTempRuleName)
	'This next line was required because the GetRule function would never throw an exception even
	'if the temp rule did not exist.
	sBlank = oRule.Name 
	bRuleExists = True
Catch
	oRule = oAuto.AddRule(oDoc, sTempRuleName, "")
End Try


'Adding the temporary rule trigger after the desired external rule trigger
'so that when the temporary trigger is deleted, the external rule trigger will have the
'lowest property ID. This is mostly just to keep things clean.
'For example, this avoids the situation where the "Set Weight" trigger
'is PropID 701 and no trigger with PropID 700 exists. It does not appear that the triggers
'are sorted by property ID. Rules fire in the order shown in the event triggers menu, so 
'the system must use a different attribute to track which order to fire the rules in

Dim intTempPropId As Integer

For intTempPropId = 700 To 799
	Try
		oProperty = oEventsPropSet.ItemByPropId(intTempPropId)
		If oProperty.Value = sTempRuleName Then
			'If a trigger for the temporary rule happens to already exist, just exit the for loop.
			'That existing trigger should make the external rule trigger work and we do not want to 
			'delete the existing trigger that was there for an unrelated reason.
			Exit Sub
		End If
	Catch
		oProperty = oEventsPropSet.Add(sTempRuleName, "BeforeDocSave" & intTempPropId, intTempPropId)
		
		'Delete the temporary local rule trigger that was just created.
		oEventsPropSet.ItemByPropId(intTempPropId).Delete

		'If the temporary local rule did not exist before we started, 
		'delete the rule To clean up the Document.
		If bRuleExists = False
			oAuto.DeleteRule(oDoc, sTempRuleName)
		End If
		
		Exit For
	End Try

Next

 

Another interesting thing I noticed is the property ID number does not affect the firing order of the rules. So I guess if you wanted multiple rules to fire in a specific order all rules for that trigger would need to be deleted and then added back in the order you wanted them to run. That is, if you wanted to create a specific firing order using iLogic instead of manually dragging them to rearrange. Or if you wanted to add a rule to a trigger that fires before already assigned rules.

 

 

 

Message 5 of 8

WCrihfield
Mentor
Mentor

As far as it not throwing an exception (error) when checking if a local rule exists, how about setting the oRule variable's value to 'Nothing' just before attempting to get that rule, then after attempting to get that rule, check if oRule is still 'Nothing' (or similar check).  That 'GetRule' method is supposed to return an iLogicRule object, so if it doesn't return an iLogicRule object, even if there is no exception thrown, you should still be able to check the variable afterwards to see if it has a value (is pointing to an iLogicRule) or not.  Just a thought.

As for the issue of looking for an external rule, I have some iLogic code that can Get/Set the directories where Inventor will look for external rules to display them in the iLogic browser.  And if needed, you could simply search the contents of those directories for the external rule file.  I also have the code for searching for files within specified directories, or to simply check for a file's existence, if needed.  I still don't know of a way to access the setting within the 'Advanced iLogic Configuration' dialog, where you can choose which file extension to use for external rule file names, by iLogic code yet, but that's something that is so seldom changed that it shouldn't be a problem for most folks though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 8

Coffee_CAD
Participant
Participant

Just coming back to this after being away for a long weekend. Thanks for the suggestion. I updated my rule based on what you said because I think your approach is cleaner.

 

Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRule As iLogicRule

Dim sTempRuleName As String = "TempRule"
Dim bRuleExists As Boolean = False

oRule = Nothing
oRule = oAuto.GetRule(oDoc, sTempRuleName)

If oRule IsNot Nothing Then
	bRuleExists = True
Else
	oRule = oAuto.AddRule(oDoc, sTempRuleName, "")
End If

 

I also like the idea of using the file system to make sure the external rule is present in the correct directory. I'll work on implementing that. I think I have all the code I need already to check if the file exists.

 

Thanks again for the help.

 

0 Likes
Message 7 of 8

To_Efficiency_and_Beyond
Enthusiast
Enthusiast

I used the code contained here (and tried a variation found elsewhere). It added the external rule(s) to the event trigger(s) properly. If I check the event triggers window and right click one of the added rules, it opens the proper code. Everything looks to be exactly right...... except they don't trigger.

If i manually setup the same event trigger configuration, it looks the same and works perfectly. Anyone having a similar issue?

0 Likes
Message 8 of 8

WCrihfield
Mentor
Mentor

Hi @To_Efficiency_and_Beyond.  I don't think I have ever encountered this specific issue myself yet, but yes, I have heard about multiple other people sometimes having this problem.  I am not sure about what causes this, or how to fix it yet though.

However, @Coffee_CAD seems to be suggesting here that, in the case where you have not yet added a rule to the Event Triggers before, either manually, or by code, then you may need to do so at least once before this main process will work (at least sometimes).  So he developed the process above to add a temporary, empty, local rule to the Event Triggers by code, then either leave it, or delete it, before doing so with the rule he really wants to add to it.  The thought is that this process may be flipping some type of internal switch, setting, or attribute that will then make the next step work.  Then proceed to add your normal iLogic rule to the Event Triggers by code, and that may avoid this odd situation.  I honestly have not tried this specific extra process yet myself, since I haven't encountered this situation yet.  I do know though, that this property set will not exist yet in the document, if you have never attempted to add any rules to the Event Triggers before in the document.  I am relatively sure that the 'internal name' of the property set should always be the same, but I don't know if its 'regular name' may change in situations where other languages are being used (other than English).  I just know that when the 'regular name' starts with the underscore ("_"), that property set will be 'hidden', and that Autodesk has chosen to make this property set hidden by default.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)