Simple Set Event Trigger with Ilogic

Simple Set Event Trigger with Ilogic

AMN3161
Advocate Advocate
2,998 Views
23 Replies
Message 1 of 24

Simple Set Event Trigger with Ilogic

AMN3161
Advocate
Advocate

I have been seeing a few rather large posts about having logic set your event trigger but all of the posts are very clearly over my head

 

I have this to insert a rule into my assembly

 

Dim oRuleName As String = "Master_Parts_List_Assembly"
Dim oTxtFileName As String = "C:\_VAULT_WORKSPACE\JOBS\FAB\Ilogic Rules\External Rules\Referenced Rules\Run a Specific Rule in all parts (REF).txt"
Dim oRuleText As String = IO.File.ReadAllText(oTxtFileName)
Dim oDoc As Document = ThisApplication.ActiveEditDocument
Dim oRuleExists As Boolean = False
Dim iLogicAuto = iLogicVb.Automation
iLogicAuto.RulesEnabled = True
iLogicAuto.RulesOnEventsEnabled = True
Dim oRule As iLogicRule
Try
	oRule = iLogicAuto.GetRule(oDoc, oRuleName)
	oAns = MsgBox("A Rule named '" & oRuleName & "' already exists." & vbCrLf &
	"Its Text = " & vbCrLf &
	oRule.Text & vbCrLf &
	"Do you want to replace its text?", vbYesNo + vbQuestion,"")
	If oAns = vbNo Then Return '(or Exit Sub)
Catch
	oRule = iLogicAuto.AddRule(oDoc, oRuleName, "")
End Try
oRule.Text = oRuleText
iLogicVb.DocumentUpdate
oDoc.Save

The that code will insert this into my assembly

Dim oDoc As AssemblyDocument = ThisDoc.Document

'define the ilogicAutomation
Dim iLogicAuto As Object 
iLogicAuto = iLogicVb.Automation 

Dim oRuleName As String = "Master_Parts_List" 
Dim oRule As Object 

For Each doc As Document In oDoc.AllReferencedDocuments
	On Error Resume Next
	oRule = iLogicAuto.GetRule(doc, oRuleName) 
	'run the rule
	iLogicAuto.RunRuleDirect(oRule) 
Next

 I am trying to have the other designers simply run a external rule in a assembly then another in a part to link our parts to a excel Bill of Material

 

I seen some lengthy chunks of code that revolve around something like this

("Master_Parts_List_Assembly", "BeforeDocSave0", 700)

 

I just cant figure out how to have a very simple code that just adds the Master_Parts_List_Assembly rule to a before document save. I dont need multiple runs just a simple run this before a save

 

any help would be appreciated 

0 Likes
Accepted solutions (1)
2,999 Views
23 Replies
Replies (23)
Message 21 of 24

WCrihfield
Mentor
Mentor

Hi @eric.smyth.  If you follow the link(s) I provided above to one of my contribution post, you will find updated information about this process, the proper names to use for some of the newer event triggers, and updated code used for it.  For that specific event, the internal event name you should use for the first part of the property name is "AfterAnyParamChange", and the Property ID range should be from 1000 to 1099.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 22 of 24

damian.mazurekKJYGW
Participant
Participant

Hi @WCrihfield,
I use code from your link to add event trigger. Generally everything works but I have problem that has been raised. Rules which have been added to the file, where no rules were added manually doesn't work.

Is it possible to figure out this problem ?
I tried to use this part of code to solve this problem but it doesn't help

 

 

0 Likes
Message 23 of 24

WCrihfield
Mentor
Mentor

Hi @damian.mazurekKJYGW.  I have heard others mention having problems like that before too.  It seems that this hidden iProperty set needs to be 'initiated', one way or another, before using it for the first time by code.  So, some folks use an additional code process to prepare this newly created set for use, to help eliminate this problem.  You can create a new empty local rule, then place it into the set, then immediately remove the rule from the set, then delete that rule.  Then they add the rule to it that they really want to add, and that seems to work OK.

Here is an example of this process done by code.  I will likely need to update my contribution post with code like this, so others can benefit from it, while avoiding this problem.  I put the main processes down into separate sub routines, to make it more modular, but you could combine the two Sub's into one if you wanted.  If you don't combine them, just make sure they are both available; or if they are in another external rule (that has been set-up correctly), you can include a reference to them at the top of your rule to make them available.

This example adds a 'local/internal' iLogic rule named "ShowFullFileName" to the 'Before Save Document' Event.

Sub Main
	Dim oDoc As Document = ThisDoc.Document
	AddRuleToEventTriggers(oDoc, "ShowFullFileName", "BeforeDocSave", 700, False)
	oDoc.Update
End Sub

Sub AddRuleToEventTriggers(oDoc As Document, RuleName As String, EventName As String, PropIDStart As Integer, ExternalRule As Boolean)
	Dim oSet As PropertySet = Nothing
	Dim oNewlyCreated As Boolean = False
	Dim oInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
	If Not oDoc.PropertySets.PropertySetExists(oInternalName, oSet) Then
		oSet = oDoc.PropertySets.Add("_iLogicEventsRules", oInternalName)
		oNewlyCreated = True
	End If
	If oNewlyCreated Then 	InitializeEventTriggers(oSet) '<<< make sure this Sub is available >>>
	If ExternalRule Then RuleName = "file://" & RuleName
	Dim oProp As Inventor.Property = Nothing
	For oPropID As Integer = PropIDStart To (PropIDStart + 99)
		Try
			oProp = oSet.ItemByPropId(oPropID)
			If oProp.Value = RuleName Then
				MsgBox("This rule has already been added to this event trigger.  Exiting.", vbExclamation, "")
				Exit Sub
			End If
		Catch
			oProp = oSet.Add(RuleName, EventName & oPropID, oPropID)
			Exit For
		End Try
	Next
End Sub

Sub InitializeEventTriggers(oETPropSet As PropertySet)
	If IsNothing(oETPropSet) Then Exit Sub
	Dim oSourceDoc As Document = oETPropSet.Parent.Parent
	Dim oAuto As IiLogicAutomation = iLogicVb.Automation
	Dim oRule As iLogicRule = oAuto.AddRule(oSourceDoc, "DeleteMe", "")
	oETPropSet.Add(oRule.Name, "BeforeDocSave700", 700)
	oSourceDoc.Update
	oETPropSet.Item("BeforeDocSave700").Delete
	oAuto.DeleteRule(oSourceDoc, "DeleteMe")
	oSourceDoc.Update
End Sub

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 24 of 24

WCrihfield
Mentor
Mentor

I updated my contribution post with a slightly condensed version of the code I posted above, where I eliminated the two Sub routines so that all code is within the Sub Main area.  And the post now mentions this initialization problem, and how to solve it.

https://knowledge.autodesk.com/community/article/329361 

I created a few new part documents using the 'New' button, then tried these codes out on them as a test, and everything seemed to work OK for me.  But I haven't experienced this problem as much as others have, so the real test will be if it works OK for others.

 

I can't remember if this problem persists from a Template file when you use the New button to generate new documents from it.  For example, if the Template document has never had its Event Triggers PropertySet created or initialized, will all the new documents generated from it through the New process (not Copy or Open & SaveAs) be the same way as the Template.  If it does persist that process, then you could just make sure your Template has had its Event Triggers PropertySet created & initialized, so that it will carry over to all new documents you generate from it.  I do believe that this status does carry over if you just copy the template, or open it then use SaveAs to generate new documents.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)