Using Ilogic code to copy an external rule into the Rules on Events (Before Save Document)

Using Ilogic code to copy an external rule into the Rules on Events (Before Save Document)

Kersh
Contributor Contributor
739 Views
8 Replies
Message 1 of 9

Using Ilogic code to copy an external rule into the Rules on Events (Before Save Document)

Kersh
Contributor
Contributor

Hi,

I have a need to add an event trigger, that copies the external rule into the 'Rules on Events' area, under Before save document. I'd like to incorperate this into an existing iLogic code that replaces a drawing template and few other things.

Is it possible to use iLogic to copy event triggers into Rules on events?

Kersh_0-1730671214793.png

 

0 Likes
Accepted solutions (1)
740 Views
8 Replies
Replies (8)
Message 2 of 9

daltonNYAW9
Advocate
Advocate

Add rule to event trigger: https://forums.autodesk.com/t5/inventor-programming-ilogic/adding-external-rule-to-event-trigger/m-p...

 

Try this:

Sub Main
Dim directoryPath As String = iLogicVb.Automation.FileOptions.ExternalRuleDirectories(0)

Dim rulePath As String = directoryPath & "\Title to Drawing.txt"

Dim content As String = ""
Using textReader As New System.IO.StreamReader(rulePath)
	content = textReader.ReadToEnd
End Using

iLogicVb.Automation.AddRule(ThisDoc.Document, "Title to Drawing", "")
iLogicVb.Automation.GetRule(ThisDoc.Document, "Title to Drawing").Text = content



Dim EventPropSet As Inventor.PropertySet
EventPropSet = GetiLogicEventPropSet(ThisDoc.Document)
EventPropSet.Add("Title to Drawing", "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


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

 

Message 3 of 9

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Kersh.  Here is an example iLogic rule for a task like that.  This is actually a pretty complex procedure that is accessing an area of Inventor's API that was originally 'hidden' to us 'users' by the folks at Autodesk, and it involved years of trial & error experimentation by us Inventor API users, so it can be error prone or result in problematic behavior if not done perfectly.  The internal task of finding this 'hidden' PropertySet, that may not even exist yet, or creating it, can be done far more efficiently than it was done in the past, using primarily just its 'internal' name, and optionally its 'default' name, and the PropertySets.PropertySetExists method, as I am showing in my example code below.

 

There are several variables involved which make the overall process, and what all is requires, and what all is involved (what all needs to be known) difficult to fully explain in one place.  There are special requirements about the rule's name that changes between 'internal' rules and 'external' rules, besides just the 'prefix' shown in this example, that go all the way back to how you have your 'iLogic Configuration' settings set-up.  There is a requirement about whether or not this document already has an iLogic influence effecting it yet or not also.  Each event you see in the dialog has a different 'internal name' than the one you see in that dialog, and has a very specific PropID range that must be used just for that one specific event, for it to work properly.  That PropID range has already been known to change at least once before, for one of the known events, since we have known about this association, so it is a good idea to monitor that association for each release of Inventor.  Also, special care must be taken in several areas, such as to not delete, or over-write any already existing settings (properties) in that set, when adding new rule(s) into it ; and not to add the same rule multiple times under the same event.  This is done by sequentially checking any/all existing settings under the event of interest before making any changes.

Sub Main
	Dim oDoc As Inventor.Document = ThisDoc.Document
	'required for external rule names in these Event Triggers properties
	Dim sExternalRulePrefix As String = "file://"
	Dim sExtRuleName As String = sExternalRulePrefix & "Title to Drawing"
	'required to prepare document (it must have DocumentInterest for iLogic add-in)
	DocumentEventTriggersPreparations(oDoc, True)
	'event internal name is different than what you see in Event Triggers dialog
	Dim sEventInternalName As String = "BeforeDocSave"
	Dim oSet As Inventor.PropertySet = Nothing
	Dim sSetDefaultName As String = "_iLogicEventsRules"
	'this internal name must never be different, or it will not work
	Dim sSetInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
	If Not oDoc.PropertySets.PropertySetExists(sSetInternalName, oSet) Then
		oSet = oDoc.PropertySets.Add(sSetDefaultName, sSetInternalName)
	End If
	Dim oProp As Inventor.Property = Nothing
	'PropID range is unique/specific to each specific event
	For iPropID As Integer = 800 To 899 'range is specific to each event
		Try
			oProp = oSet.ItemByPropId(iPropID)
			If oProp.Value = sExtRuleName Then
				'MsgBox("This rule has already been added to this event trigger.  Exiting.", vbExclamation, "")
				Logger.Info("This rule has already been added to this event trigger.")
				Exit For 'exit this loop/iteration
			End If
			Continue For 'there is already an existing rule at this iPropID, so skip to next
		Catch
			'no property was found with that PropID, and this rule has not been found, so now create one for this rule
			oProp = oSet.Add(sExtRuleName, sEventType & iPropID, iPropID)
			Exit For 'exit the loop, because we have already added the rule to the event
		End Try
	Next
	DocumentEventTriggersPreparations(oDoc, False)
End Sub

Sub DocumentEventTriggersPreparations(oDoc As Inventor.Document, bBefore As Boolean)
	Dim iLogicClientId As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"
	Dim sTempRuleName As String = "DeleteMe"
	If bBefore Then
		If oDoc.DocumentInterests.HasInterest(iLogicClientId) = False Then
			iLogicVb.Automation.AddRule(oDoc, sTempRuleName, "")
		End If
	Else
		If iLogicVb.Automation.GetRule(oDoc, sTempRuleName) IsNot Nothing Then
			iLogicVb.Automation.DeleteRule(oDoc, sTempRuleName)
		End If
	End If
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 9

Kersh
Contributor
Contributor

This almost works. It throws an error when I have existing rules in Before Save.

Kersh_0-1730753096491.png

Kersh_1-1730753133686.png

If I remove the others, then run it. It works fine.

The other thing I'm trying to do is add it to an existing external txt file that runs as ilogic, via Button Constructor, but this is throwig a bunch of errors. 
Any idea on this?

Original code in *.txt file (note I'm doing a few other things first, so it's down the bottom)

 

'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE updates the drawing style, deletes drawing templates and unsed drawing resources, then re-inserts clean template
 
'access styles manager
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oStyles As Inventor.DrawingStylesManager
oStyles = oDrawDoc.StylesManager
 
'update all styles by iterating through them
 
Dim iStyles As Integer
 
For iStyles = 1 To oStyles.Styles.Count
If oStyles.Styles.Item(iStyles).UpToDate = False Then
oStyles.Styles.Item(iStyles).UpdateFromGlobal
End If
Next
 
iStyles = 0
 
'delete sheet formats
 
ThisDrawing.ResourceFileName = "E:\_Vault_Workspace\Templates\KDS-Windsor ANSI B (Imperial).dwg"
ThisDrawing.KeepExtraResources = True
 
Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveEditDocument
 
Dim oCurrentNumber  As Sheet
oCurrentNumber = oDoc.ActiveSheet
 
' Iterate through the sheets, and delete the title blocks and symbols
 
For Each oSheet In oDoc.Sheets
    oSheet.Activate
    
    Try
    oSheet.TitleBlock.Delete
    Catch
    'catch error if no TitleBlock found
    End Try
 
    Try
    oSheet.Border.Delete
    Catch
    'catch error if no border found
    End Try
    
Dim doc As DrawingDocument = ThisDoc.Document
 
'Delete un-used Sheets
For Each sheet As Sheet In doc.Sheets
If (Sheet.DrawingViews.Count = 0) Then
Sheet.Delete()
End If
Next
 
'Delete Sheet Formats
For Each sheetFormat As SheetFormat In doc.SheetFormats
SheetFormat.Delete()
Next
 
'Delete unused TitleBlocks
For Each titleBlockDefinition As TitleBlockDefinition In doc.TitleBlockDefinitions
If (Not TitleBlockDefinition.IsReferenced) Then
TitleBlockDefinition.Delete()
End If
Next
 
'Delete unused Borders
For Each borderDefinition As BorderDefinition In doc.BorderDefinitions
If ((Not BorderDefinition.IsReferenced) And (Not BorderDefinition.IsDefault)) Then
BorderDefinition.Delete()
End If
Next
 
'Delete un-used Sketch Symbols
For Each sketchedSymbolDefinition As SketchedSymbolDefinition In doc.SketchedSymbolDefinitions
If (Not SketchedSymbolDefinition.IsReferenced) Then
SketchedSymbolDefinition.Delete()
End If
Next
 
 
    'set new TitleBlock
    ActiveSheet.TitleBlock = "KDS-Windsor Title 'B'"
    
    
    'set new border
    ActiveSheet.Border = "KDS-Windsor Border 'B'"
Next
 
'set back to original sheet
oCurrentNumber.Activate
 
 
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE asks if you want to update the Drawing units, and pushes it to DRAWING iProp "UNITS" which is used in the title block.
 
'Promt the user to answer yes or no
myquestion = MessageBox.Show("Define Drawing UNITS?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
 
'If they answered YES
If myquestion = vbYes Then
'Promt the user for the value of a variable
myprop = InputRadioBox("Choose Drawing Units", "mm", "ft", true, Title :="Drawing Units")
'myprop = InputBox("Please Enter the UNITS Here ( mm or ft )", " ", "mm")
 
If myprop = Yes
iProperties.Value("Custom", "UNITS") = "ft"
Else
iProperties.Value("Custom", "UNITS") = "mm"
End If
 
'assign the value of that variable to a custom property
 
 
'Thank the user for filling out the custom property
'MessageBox.Show("Great Job!!", "GOLD STAR")
 
'If they answered NO
Else
'MessageBox.Show("Well.. Thats ok. Maybe next time", "Don't forget it!")
 
'You must close out the IF statement
End If
 
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE pulls "Title" and "Summary" from the part/assy and pushes it into the same DRAWING iProps. 
'Push Part/Assy iProperties to Drawing iProperties
 
mDoc = ThisDrawing.ModelDocument
titleProp = mDoc.PropertySets.Item("Summary Information").Item("Title")
iProperties.Value("Summary", "Title") = titleProp.Value
 
subjProp = mDoc.PropertySets.Item("Summary Information").Item("Subject")
iProperties.Value("Summary", "Subject") = subjProp.Value
 
descProp = mDoc.PropertySets.Item("Design Tracking Properties").Item("Description")
iProperties.Value("Project", "Description") = descProp.Value
'
 
'Forces update after rule runs 
iLogicVb.UpdateWhenDone = True
 
'--------------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE asks if you want dimension precision set to zero, and either does so or moves on.
 
Dim oDrawingDoc as DrawingDocument    
 
DWG = MessageBox.Show("To Zero?", "Set Dimension Precision?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
 
If DWG = vbYes Then
Dim Doc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim s As Sheet = Doc.ActiveSheet
Dim dims As Inventor.DrawingDimensions = s.DrawingDimensions
 
For Each d As Inventor.DrawingDimension In dims
Try 
If d.Precision = 1 Then d.Precision = 0
 
Catch
End Try 
Next
 
Else
'do nothing
End If 
 
'--------------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------------------------
 
'Add Title to Drawing Event Trigger
 
Sub Main
Dim directoryPath As String = iLogicVb.Automation.FileOptions.ExternalRuleDirectories(0)
 
Dim rulePath As String = directoryPath & "\Title to Drawing.txt"
 
Dim content As String = ""
Using textReader As New System.IO.StreamReader(rulePath)
content = textReader.ReadToEnd
End Using
 
iLogicVb.Automation.AddRule(ThisDoc.Document, "Title to Drawing", "")
iLogicVb.Automation.GetRule(ThisDoc.Document, "Title to Drawing").Text = content
 
 
 
Dim EventPropSet As Inventor.PropertySet
EventPropSet = GetiLogicEventPropSet(ThisDoc.Document)
EventPropSet.Add("Title to Drawing", "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
 
 
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
 
 
Gives:
Kersh_3-1730753542857.png

 

0 Likes
Message 5 of 9

Kersh
Contributor
Contributor

Weirdly this works but it's copying the external rule into 'After Save Document', not before, even tho youro code clearly seems to state 'before'

 

Kersh_0-1730753716417.png

 

Similar to the other chaps code, If I try and add this to an existing external txt file that is run with a button, it throws a bunch of errors. Could this be because of the Sub Main grouping? The rest of the code in this text file seems to run OK without grouping.

 

Text file below (your code is added to the end of other functions)

 

'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE updates the drawing style, deletes drawing templates and unsed drawing resources, then re-inserts clean template
 
'access styles manager
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oStyles As Inventor.DrawingStylesManager
oStyles = oDrawDoc.StylesManager
 
'update all styles by iterating through them
 
Dim iStyles As Integer
 
For iStyles = 1 To oStyles.Styles.Count
If oStyles.Styles.Item(iStyles).UpToDate = False Then
oStyles.Styles.Item(iStyles).UpdateFromGlobal
End If
Next
 
iStyles = 0
 
'delete sheet formats
 
ThisDrawing.ResourceFileName = "E:\_Vault_Workspace\Templates\KDS-Windsor ANSI B (Imperial).dwg"
ThisDrawing.KeepExtraResources = True
 
Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveEditDocument
 
Dim oCurrentNumber  As Sheet
oCurrentNumber = oDoc.ActiveSheet
 
' Iterate through the sheets, and delete the title blocks and symbols
 
For Each oSheet In oDoc.Sheets
    oSheet.Activate
    
    Try
    oSheet.TitleBlock.Delete
    Catch
    'catch error if no TitleBlock found
    End Try
 
    Try
    oSheet.Border.Delete
    Catch
    'catch error if no border found
    End Try
    
Dim doc As DrawingDocument = ThisDoc.Document
 
'Delete un-used Sheets
For Each sheet As Sheet In doc.Sheets
If (Sheet.DrawingViews.Count = 0) Then
Sheet.Delete()
End If
Next
 
'Delete Sheet Formats
For Each sheetFormat As SheetFormat In doc.SheetFormats
SheetFormat.Delete()
Next
 
'Delete unused TitleBlocks
For Each titleBlockDefinition As TitleBlockDefinition In doc.TitleBlockDefinitions
If (Not TitleBlockDefinition.IsReferenced) Then
TitleBlockDefinition.Delete()
End If
Next
 
'Delete unused Borders
For Each borderDefinition As BorderDefinition In doc.BorderDefinitions
If ((Not BorderDefinition.IsReferenced) And (Not BorderDefinition.IsDefault)) Then
BorderDefinition.Delete()
End If
Next
 
'Delete un-used Sketch Symbols
For Each sketchedSymbolDefinition As SketchedSymbolDefinition In doc.SketchedSymbolDefinitions
If (Not SketchedSymbolDefinition.IsReferenced) Then
SketchedSymbolDefinition.Delete()
End If
Next
 
 
    'set new TitleBlock
    ActiveSheet.TitleBlock = "KDS-Windsor Title 'B'"
    
    
    'set new border
    ActiveSheet.Border = "KDS-Windsor Border 'B'"
Next
 
'set back to original sheet
oCurrentNumber.Activate
 
 
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE asks if you want to update the Drawing units, and pushes it to DRAWING iProp "UNITS" which is used in the title block.
 
'Promt the user to answer yes or no
myquestion = MessageBox.Show("Define Drawing UNITS?", " ", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
 
'If they answered YES
If myquestion = vbYes Then
'Promt the user for the value of a variable
myprop = InputRadioBox("Choose Drawing Units", "mm", "ft", true, Title :="Drawing Units")
'myprop = InputBox("Please Enter the UNITS Here ( mm or ft )", " ", "mm")
 
If myprop = Yes
iProperties.Value("Custom", "UNITS") = "ft"
Else
iProperties.Value("Custom", "UNITS") = "mm"
End If
 
'assign the value of that variable to a custom property
 
 
'Thank the user for filling out the custom property
'MessageBox.Show("Great Job!!", "GOLD STAR")
 
'If they answered NO
Else
'MessageBox.Show("Well.. Thats ok. Maybe next time", "Don't forget it!")
 
'You must close out the IF statement
End If
 
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE pulls "Title" and "Summary" from the part/assy and pushes it into the same DRAWING iProps. 
'Push Part/Assy iProperties to Drawing iProperties
 
mDoc = ThisDrawing.ModelDocument
titleProp = mDoc.PropertySets.Item("Summary Information").Item("Title")
iProperties.Value("Summary", "Title") = titleProp.Value
 
subjProp = mDoc.PropertySets.Item("Summary Information").Item("Subject")
iProperties.Value("Summary", "Subject") = subjProp.Value
 
descProp = mDoc.PropertySets.Item("Design Tracking Properties").Item("Description")
iProperties.Value("Project", "Description") = descProp.Value
'
 
'Forces update after rule runs 
iLogicVb.UpdateWhenDone = True
 
'--------------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------------------------
' JKH - This CODE asks if you want dimension precision set to zero, and either does so or moves on.
 
Dim oDrawingDoc as DrawingDocument    
 
DWG = MessageBox.Show("To Zero?", "Set Dimension Precision?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
 
If DWG = vbYes Then
Dim Doc As Inventor.DrawingDocument = ThisApplication.ActiveDocument
Dim s As Sheet = Doc.ActiveSheet
Dim dims As Inventor.DrawingDimensions = s.DrawingDimensions
 
For Each d As Inventor.DrawingDimension In dims
Try 
If d.Precision = 1 Then d.Precision = 0
 
Catch
End Try 
Next
 
Else
'do nothing
End If 
 
'--------------------------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------------------------
 
'Add Title to Drawing Event Trigger
 
Sub Main
Dim oDoc As Inventor.Document = ThisDoc.Document
'required for external rule names in these Event Triggers properties
Dim sExternalRulePrefix As String = "file://"
Dim sExtRuleName As String = sExternalRulePrefix & "Title to Drawing"
'required to prepare document (it must have DocumentInterest for iLogic add-in)
DocumentEventTriggersPreparations(oDoc, True)
'event internal name is different than what you see in Event Triggers dialog
Dim sEventInternalName As String = "BeforeDocSave"
Dim oSet As Inventor.PropertySet = Nothing
Dim sSetDefaultName As String = "_iLogicEventsRules"
'this internal name must never be different, or it will not work
Dim sSetInternalName As String = "{2C540830-0723-455E-A8E2-891722EB4C3E}"
If Not oDoc.PropertySets.PropertySetExists(sSetInternalName, oSet) Then
oSet = oDoc.PropertySets.Add(sSetDefaultName, sSetInternalName)
End If
Dim oProp As Inventor.Property = Nothing
'PropID range is unique/specific to each specific event
For iPropID As Integer = 800 To 899 'range is specific to each event
Try
oProp = oSet.ItemByPropId(iPropID)
If oProp.Value = sExtRuleName Then
'MsgBox("This rule has already been added to this event trigger.  Exiting.", vbExclamation, "")
Logger.Info("This rule has already been added to this event trigger.")
Exit For 'exit this loop/iteration
End If
Continue For 'there is already an existing rule at this iPropID, so skip to next
Catch
'no property was found with that PropID, and this rule has not been found, so now create one for this rule
oProp = oSet.Add(sExtRuleName, sEventType & iPropID, iPropID)
Exit For 'exit the loop, because we have already added the rule to the event
End Try
Next
DocumentEventTriggersPreparations(oDoc, False)
End Sub
 
Sub DocumentEventTriggersPreparations(oDoc As Inventor.Document, bBefore As Boolean)
Dim iLogicClientId As String = "{3BDD8D79-2179-4B11-8A5A-257B1C0263AC}"
Dim sTempRuleName As String = "DeleteMe"
If bBefore Then
If oDoc.DocumentInterests.HasInterest(iLogicClientId) = False Then
iLogicVb.Automation.AddRule(oDoc, sTempRuleName, "")
End If
Else
If iLogicVb.Automation.GetRule(oDoc, sTempRuleName) IsNot Nothing Then
iLogicVb.Automation.DeleteRule(oDoc, sTempRuleName)
End If
End If
End Sub
Kersh_1-1730754512585.png

 

 

0 Likes
Message 6 of 9

daltonNYAW9
Advocate
Advocate

Looks like you need to add a new property to the 'Event trigger' property set for every unique trigger.

Add this:

iLogicVb.Automation.DeleteRule(ThisDoc.Document, "Title to Drawing")
iLogicVb.Automation.AddRule(ThisDoc.Document, "Title to Drawing", "")
iLogicVb.Automation.GetRule(ThisDoc.Document, "Title to Drawing").Text = content



Dim EventPropSet As Inventor.PropertySet
EventPropSet = GetiLogicEventPropSet(ThisDoc.Document)
For Each prop As Inventor.Property In EventPropSet
	If prop.Value = "Title to Drawing"
		Exit Sub
	End If
Next
EventPropSet.Add("Title to Drawing", "BeforeDocSave" & EventPropSet.Count + 1, 700 + EventPropSet.Count + 1)

 

And I think the reason your getting errors now is because those lines arnt under a 'Sub'

Just have the 'Sub Main' start at the top of your code and end at the bottom.
The best practice would to create unique sub routines for each of the processes in your code.
Ex:

Sub Main
	UpdateDrawing()
End Sub

Sub UpdateDrawing
	
End Sub


Function GetiLogicEventPropSet(cDocument As Document) As Inventor.PropertySet
	'----------
End Function
Message 7 of 9

daltonNYAW9
Advocate
Advocate

I must've read this wrong b/c I thought u wanted to add the external rule to the current document's rules. If you want to use it as an external rule use the example provided by @WCrihfield . And change the 800 value to 700.

Message 8 of 9

WCrihfield
Mentor
Mentor

Yep, that was my mistake.  In my code example, I was specifying the correct event internal name, but not the correct PropID range.  It should have been from 700 o 799, not from 800 to 899.  Also, as I mentioned in my post above, I believe one of those events listed in that 'older' post has been moved from one PropID range to another.  The 'AfterDrawingViewsUpdate' event is now between 1800 to 1899 PropID range.  I do not know when that change happened though.  There are also multiple more events in the Event Triggers now than there were back then.

 

I know this because I developed an 'inspection' routine for checking these things a couple years ago.  It essentially involves creating or opening one of each of the 3 main document types, then creating a single internal iLogic rule in it (its contents do not matter), and having an external rule ready.  Then placing the internal rule, and the external rule under each of the events available in the 'This Document' tab of the Event Triggers dialog.  Then, using an iLogic rule to access that PropertySet, recording its settings, and iterating through every property in it, recording their settings, then writing all that data to the iLogic Log window, or out to a text file.  This confirms how the individual properties get named (which includes the internal event name, and the ending index Integer), and what their PropID is, which tells us what range rules under that event must be within, and what its Value is, which sometimes includes the prefix for external rules.  But that depends on if or how you have set-up your iLogic Configuration settings.  If you have properly set-up those settings to include the directories where to find all of your external rules, then there is no need to specify the whole path of the external rule file, and only the external rule's name is needed.  When you only specified its name, it puts that prefix in front of it.  If you specified the full file path, file name, and file extension, then it might not include that prefix, but I never do it that way.  Although I have not tried putting more than 100 rules under one event before, to check if the PropID range would go past that 99 mark, but I highly doubt it, since there are multiple existing events whose PropID ranges start only 100 value from each other already.  I attached a PDF copy of my Excel file for those associations, but I am still using 2024.3 version, so it may have changed for 2025, not sure.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 9

Kersh
Contributor
Contributor

I got Wesleys code to work fine for my needs. I just fixed up the 700-799 text, and placed it in it's own external rule txt file. Then called that rule in my other code, by adding :

'Add Title to Drawing Event Trigger

iLogicVb.RunExternalRule("T-to-D-Add")

 

So now it runs all my other code, then runs Wesleys code afterwards. Mint! Many Thanks to you both.

0 Likes