Rule added to event trigger by another rule is not triggering

Rule added to event trigger by another rule is not triggering

daniel.puchta
Enthusiast Enthusiast
510 Views
4 Replies
Message 1 of 5

Rule added to event trigger by another rule is not triggering

daniel.puchta
Enthusiast
Enthusiast

Hello,

I have written this rule that adds another rule into event trigger. The problem is that the event trigger is not triggering the rule. I have to close the document, then reopen it and it works. Also if I add the rule manually, it is working okay.

I really need to make the event trigger working without having to close the document. Before that I do another things by another rules and sometimes they do it wrong, so I can not just save the document, close it and reopen it.

Code:


Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oRuleName As String = "Rule_to_add"
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
Dim oEventsPropSet As Inventor.PropertySet
Dim oSetName As String = "iLogicEventsRules"
Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
Try
oEventsPropSet = oDoc.PropertySets.Item(oSetName)
Catch
oEventsPropSet = oDoc.PropertySets.Add(oSetName,oInternalName)
End Try
If oEventsPropSet Is Nothing Then
oEventsPropSet = oDoc.PropertySets.Add(oSetName, oInternalName)
End If
Dim oProperty As Inventor.Property
Dim oPropId As Integer
For oPropId = 1600 To 1699
Try
oProperty = oEventsPropSet.ItemByPropId(oPropId)
If oProperty.Value = oRuleName Then
Exit Sub
End If
Catch
oProperty = oEventsPropSet.Add(oRuleName, "AfterAnyiPropertyChange" & oPropId, oPropId)
Exit Sub
End Try
oPropId = oPropId + 1
Next



 
I do it in drawing, I am using Inventor Professional 2020 64 bit, Build 396.

I appreciate your help, thank you in advance.

0 Likes
511 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @daniel.puchta.  It looks like the iProperty set name you are searching for is missing the underscore (_) at the beginning.  That underscore at the beginning of the set name is what 'hides' the 'hidden' property set.  When that property set gets created automatically by you adding a rule into it, it is created with that underscore at the start of the set name.  You may have seen some examples of similar code that attempts to 'unhide' that property set by deleting it then recreating it without that underscore at the start of its name, but that is generally not necessary, and if you don't assign it the same 'internal name' it wont work, because it won't be recognized.  Here are a few of my contribution posts about working with the Event Triggers hidden property set (Link1, Link2, Link3, Link4).  I haven't updated some of those posts with the newer events available in Inventor 2022 yet, but you can probably figure it out from there.  What I'm thinking is that if the 'hidden' set already exists, and may already have stuff in it, your rule bay be trying to create another property set (with the 'unhidden' name), then putting your property into that second set.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 5

daniel.puchta
Enthusiast
Enthusiast

Hello WCrihfield,

thank you for your advice. I tried to modify my rule with this code:

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oRuleName As String = "Cislo_vykresu_update"

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

Dim oEventsPropSet As Inventor.PropertySet
Dim oSetName As String = "iLogicEventsRules"
Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
Try
	oEventsPropSet = oDoc.PropertySets.Item(oSetName)
Catch
	oEventsPropSet = oDoc.PropertySets.Item("_" & oSetName)
End Try
If oEventsPropSet Is Nothing Then
	oEventsPropSet = oDoc.PropertySets.Add(oSetName, oInternalName)
End If

Dim oProperty As Inventor.Property
Dim oPropId As Integer
For oPropId = 1600 to 1699
	Try
		oProperty = oEventsPropSet.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 = oEventsPropSet.Add(oRuleName, "AfterAnyiPropertyChange" & oPropId, oPropId)
		Exit Sub
	End Try
	oPropId = oPropId + 1
Next

It throws this error:

Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))

Do you have any idea why is this happening?

 

0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor

Quick questions.  Is the rule you are trying to add to the Event Triggers a 'local' rule (saved within that document), or is it an 'external' rule?  Did you update, then save the document after adding the rule to the Event Triggers, but before testing if the 'trigger' is working?  I do know that when adding an external rule to the Event Triggers by code, the rule name usually needs to have "file://" added to its beginning.  I guess this tells it that it is an external rule, so don't try to find it within the local document, or something like that.

 

I modified your code a bit.  Try this and see if it performs better than before.

Dim oDoc As Document = ThisApplication.ActiveDocument
Dim oRuleName As String = "Cislo_vykresu_update"

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

'I don't think that the GetRule method will throw an error if the rule is not found.
'but caution doesn't hurt.
Try
	oRule = oAuto.GetRule(oDoc, oRuleName)
Catch
	MsgBox("The specified rule was not found in the active document. Exiting.", vbCritical,"")
	Exit Sub
End Try

'if the oRule variable has not been assigned a Value, it then 'Is Nothing'
If oRule Is Nothing Then
	MsgBox("The specified rule was not found in the active document. Exiting.", vbCritical, "")
	Exit Sub
End If

oPropSets = oDoc.PropertySets
Dim oEventsPropSet As Inventor.PropertySet
Dim oSetName As String = "_iLogicEventsRules"
Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
Try
	oEventsPropSet = oPropSets.Item(oSetName)
Catch
	oEventsPropSet = oPropSets.Item(oInternalName)
Catch
	oEventsPropSet = oPropSets.Item("iLogicEventsRules") 'in case it has been 'un-hidden'
Catch
	'it was not found by any of those names, so go ahead and create it
	oEventsPropSet = oPropSets.Add(oSetName, oInternalName) 'using 'default' names
End Try
'it has been either found or created now
'if its name(s) do not match 'default' names, then delete it and re-create with 'default' names
'deleting will loose any properties (settings) within
If oEventsPropSet.InternalName <> oInternalName Or _
	oEventsPropSet.Name <> oSetName Then
	oEventsPropSet.Delete
	oEventsPropSet = oPropSets.Add(oSetName, oInternalName)
End If

Dim oProperty As Inventor.Property
Dim oPropId As Integer
For oPropId = 1600 To 1699
	Try
		'if this fails, then there is no property there, so go to Catch part
		oProperty = oEventsPropSet.ItemByPropId(oPropId)
		'if property was found there, check to see if its Value is our rule name
		If oProperty.Value = oRuleName Then
			MsgBox("This rule has already been added to that event trigger. Exiting.", vbInformation,"")
			Exit Sub
		End If
	Catch
		'if it reaches this point, it has not found the rule yet, and has ran out of properties to check
		'so create the new property with our rule name as its value
		oProperty = oEventsPropSet.Add(oRuleName, "AfterAnyiPropertyChange" & oPropId, oPropId)
		'no need to go any further, so exit Sub (exits the rule, without looping any more)
		Exit Sub
	End Try
	oPropId = oPropId + 1
Next

oDoc.Update
oDoc.Save

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 5

daniel.puchta
Enthusiast
Enthusiast

Hello WCrihfield,

thank you for your help. Quick answer - the rule is local, saved in document. 

I tried your modified code, and as my older version, it adds the rule to event trigger, but the rule is not triggered when the event happens (when I change drawing property).
To make triggering work I need to close the document and reopen it. I know this is possible to do by rule, but I need to make this working without the need of saving the document.

I dont want to save the document, because the rule is part of more complex workflow where there are a lot of changes made by rules to the drawing data, and sometimes there is error, so I dont want to override previous version of the document.

So it is not possible to make event trigger working without the need of saving the document? For me this would be sollution too, the rule which is added to event trigger, is not critical, it only improves workflow. And this rule will work after reopening the document.

 

0 Likes