iLogic Remove all Event Triggers & add new Event Trigger

iLogic Remove all Event Triggers & add new Event Trigger

KWarrenCA
Advocate Advocate
684 Views
7 Replies
Message 1 of 8

iLogic Remove all Event Triggers & add new Event Trigger

KWarrenCA
Advocate
Advocate

I'm working on some code to update some older files which may or may not contain an event trigger. What I'm trying to come up with is an ilogic that I can have it clear out any event triggers that are existing and add the new one in if it doesn't exist. This would be added to my existing code so that this would run before the save. The current code will add the event correctly but once I save again it leaves a rule in the event but has no name to it and errors out.

Sub Main()
	

RemoveRulesfromEventTrigger
AssemblyEvents


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


Sub RemoveRulesfromEventTrigger()


   Try
Dim EventTrigger = ThisDoc.Document.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")
'MsgBox(x.Count)

If EventTrigger.Name = "StockNumber-Assembly_Copy" Then
	Return
	Else
		ThisDoc.Document.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}").Delete
	End If
	Catch
	End Try

End Sub

Sub AssemblyEvents



	On Error Resume Next
	Dim EventPropSet As Inventor.PropertySet
	EventPropSet = GetiLogicEventPropSet(ThisApplication.ActiveDocument)
	' To make sure that the document has an iLogic DocumentInterest, add a temporary rule
	'EventPropSet.Add("file://Accessory List", "AfterDrawingViewsUpdate", 1801)
	EventPropSet.Add("file://StockNumber-Assembly_Copy", "BeforeDocSave", 700)

	'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

	InventorVb.DocumentUpdate()


End Sub

Prior to save.pngAfter save.png 

0 Likes
Accepted solutions (2)
685 Views
7 Replies
Replies (7)
Message 2 of 8

C_Haines_ENG
Collaborator
Collaborator

What is wrong with your current code?

0 Likes
Message 3 of 8

KWarrenCA
Advocate
Advocate

@C_Haines_ENG I keep having the issue where if it is ran multiple times it clears out the event trigger and makes it a blank rule. I also get the issue that it doesn't actually run the rule most times.

0 Likes
Message 4 of 8

C_Haines_ENG
Collaborator
Collaborator
Accepted solution

Certainly simpler than your code, but you should be able to tweak it how you will need. The issue was in the "ItemPropById". I just ignored it outright in favor of a For Loop.

 

Sub Main

	Dim oDoc As Document = ThisDoc.Document

	Dim oEvents As Inventor.PropertySet

	Try
		oEvents = oDoc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")
	Catch
		oEvents = oDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
	End Try

	For Each oItem As [Property] In oEvents
		oItem.Delete
	Next
	
	oEvents.Add("file://StockNumber-Assembly_Copy", "BeforeDocSave", 700)

End Sub

 

 

0 Likes
Message 5 of 8

KWarrenCA
Advocate
Advocate

@C_Haines_ENG That seems to work a lot better. One issue I'm having is when the rule is added by iLogic it doesn't run before save but if I manually add it to the event triggers it works correctly.

0 Likes
Message 6 of 8

KWarrenCA
Advocate
Advocate
Accepted solution

Figured out there is an issue with ilogic not adding the event trigger but if you add it manually it works. See link below for more explanation. I added this line of code before @C_Haines_ENG code and it works every time.

 

https://forums.autodesk.com/t5/inventor-programming-ilogic/event-triggers-not-firing/m-p/12340987 

If ThisDoc.Document.DocumentInterests.HasInterest("{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}") = True
	Logger.Debug("hasInterest")
ElseIf ThisDoc.Document.DocumentInterests.HasInterest("{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}") = False
	Logger.Debug("hasInterest False ")
	iLogicVb.Automation.AddRule(ThisDoc.Document, "TriggerRule", "'This rule is used to activate the iLogic environment.")
	Logger.Info("The local rule TriggerRule was created to activate the iLogic environment.")
End If

 

Message 7 of 8

Jesse_Glaze
Participant
Participant

I am playing with this rule and love it.. but i can't get it to add more rules. I have split the code into pieces so i can trigger them piecemeal from other ilogic rules (see below). I was hoping to be able to write a separate ilogic rule to delete the triggers, then run the add triggers rule several times to add back all the triggers I want. I can run the add trigger rule one time, then subsequent tries fail. Suggestions?

 

Delete Triggers:

Sub Main

	If ThisDoc.Document.DocumentInterests.HasInterest("{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}") = True
		Logger.Debug("hasInterest")
	ElseIf ThisDoc.Document.DocumentInterests.HasInterest("{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}") = False
		Logger.Debug("hasInterest False ")
		iLogicVb.Automation.AddRule(ThisDoc.Document, "TriggerRule", "'This rule is used to activate the iLogic environment.")
		Logger.Info("The local rule TriggerRule was created to activate the iLogic environment.")
	End If

	Dim oDoc As Document = ThisDoc.Document

	Dim oEvents As Inventor.PropertySet

	Try
		oEvents = oDoc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")
	Catch
		oEvents = oDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
	End Try

	For Each oItem As [Property] In oEvents
		oItem.Delete
	Next
	
End Sub

Add Triggers:

Sub Main

	Dim RuleName As String = "file://" & SharedVariable("SetEventTriggersInput1")
	Dim TriggerName As String = SharedVariable("SetEventTriggersInput2")
	Dim TriggerCode As String = SharedVariable("SetEventTriggersInput3")


	If ThisDoc.Document.DocumentInterests.HasInterest("{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}") = True
		Logger.Debug("hasInterest")
	ElseIf ThisDoc.Document.DocumentInterests.HasInterest("{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}") = False
		Logger.Debug("hasInterest False ")
		iLogicVb.Automation.AddRule(ThisDoc.Document, "TriggerRule", "'This rule is used to activate the iLogic environment.")
		Logger.Info("The local rule TriggerRule was created to activate the iLogic environment.")
	End If

	Dim oDoc As Document = ThisDoc.Document

	Dim oEvents As Inventor.PropertySet

	Try
		oEvents = oDoc.PropertySets.Item("{2C540830-0723-455E-A8E2-891722EB4C3E}")
	Catch
		oEvents = oDoc.PropertySets.Add("iLogicEventsRules", "{2C540830-0723-455E-A8E2-891722EB4C3E}")
	End Try

'	For Each oItem As [Property] In oEvents
'		oItem.Delete
'	Next
	
	oEvents.Add(RuleName, TriggerName, TriggerCode)

End Sub

 

0 Likes
Message 8 of 8

WCrihfield
Mentor
Mentor

Hi @Jesse_Glaze.  I believe we usually need to Update the document after doing things like deleting &/or adding custom PropertySets, before we try adding properties into the new set.

If you truly just want to get rid of all existing 'Event Triggers' settings for a specific Document, then there is no need for the 'DocumentInterest' check.  You can just use something extremely simple, like the following example:

Dim oDoc As Inventor.Document = ThisDoc.Document
Dim oSet As Inventor.PropertySet = Nothing
If oDoc.PropertySets.PropertySetExists("{2C540830-0723-455E-A8E2-891722EB4C3E}", oSet) Then
	Try : oSet.Delete() : Catch : End Try
End If

Even the Try-Catch statement in that simple example is only in there in case the Document is 'ReadOnly' for some reason.  It is easy enough to re-create the PropertySet, if needed.

I see a lot of examples on the forum where folks are deleting existing event trigger settings just before adding new ones, but that is not always necessary.  And it is especially not needed when you actually intend to either keep them, or add them right back after deleting them.  There are ways to add more settings (Propertys) to the existing ones, without deleting the existing ones first, if that is your intention.  I have likely posted one or more examples that use the built-in PropertySet.GetPropertyInfo method, and PropertySet.SetPropertyValues method for preserving existing property data while the whole set is deleted, then adds them back after the set is created again.  Those methods were often used in the process of 'un-hiding' the PropertySet, which was another unnecessary process.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes