If triger exist, run it...if not carry on

If triger exist, run it...if not carry on

Anonymous
Not applicable
736 Views
8 Replies
Message 1 of 9

If triger exist, run it...if not carry on

Anonymous
Not applicable

Looking for the syntax to refer to a rule. I'm trying to create a rule that will run a trigger if it exists; if it doesn't it will keep going as normal.

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

WCrihfield
Mentor
Mentor

Can you explain in more detail what you are trying to do?  You can run other local rules, other external rules, and also VBA macro's from any iLogic rule, but you don't really run "triggers".  If you are referring to the iLogic Event Triggers, then behind the scenes those settings are stored as (usually hidden) iProperties that contain the name of the type of event, and a PropId that is within a specific range for that type of event, and the name of the rule that it is so run when that event happens.  There is also the "iTrigger" route, where you insert a variable at the top of your rule (something like:  trigger = iTrigger), then when you manually click the iTrigger button [Manage tab > iLogic panel > iTrigger button], it will trigger all local rules that contain that variable to run.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

WCrihfield
Mentor
Mentor

Also, if you didn't already know, there are a series of built-in iLogic "Snippets" that show you how to run other rules and macros and such.  With the iLogic Rule Editor dialog open, under the [Snippets > System tab > Run Other] group.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 9

Anonymous
Not applicable

@WCrihfield sorry after re-reading that wasn't very clear. I have a rule (rule A) that i want to run when saving. I need a rule (rule B)to check if access to rule A is available. if access is available, run it; if not do nothing.

 

As for the snippets, i have access to those and they have helped tremendously. The problem i've not figured out how to navigate is finding understanding the how to refer to an individual item that i'm trying to find (i.e. how do i refer to a rule as "ThisDocument.xxx.yyy"). i completely made up the part in parenthesis, but this is my bigger issue.

 

 

0 Likes
Message 5 of 9

WCrihfield
Mentor
Mentor

OK. Here is a simple iLogic rule that will look for another iLogic rule within the active document.  If it finds the rule, it will run it.  If it doesn't find the rule, it will let you know, then exit the rule without errors.

Here's the code:

Dim oDoc As Document = ThisApplication.ActiveDocument
'Name of the rule to look for
Dim oRuleName As String = "Rule 1"
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRule As iLogicRule
'Check if a rule with that name exists or not
Try
	oRule = oAuto.GetRule(oDoc, oRuleName)
Catch 'it didn't find it
	MsgBox("That rule was not found. Exiting.", vbOKOnly, " ")
	Exit Sub
End Try
oAuto.RunRuleDirect(oRule)

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

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 9

Anonymous
Not applicable

does it matter if the rule is external?

0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor

The code I posted can be in either a local rule or an external rule.  That part doesn't matter.  What does matter is the rule you are looking for must be a local rule.  If the rule you are looking for is an external rule, that would be done a different way.  Do you need to know how to find an external rule?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 9

Anonymous
Not applicable
Yes I'm working with external rules.
0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor
Accepted solution

OK. So, the simplest way to deal with this situation is to just encapsulate your attempt to run the external rule within a Try...Catch block, similar to this:

(You only have to use/specify the oTargetDocument if you want the external rule to act upon a different document other than the 'active' document.)

 

Dim oRuleName As String = "External Rule 1"
Dim oTargetDocument As Document = ThisApplication.Documents.Open("C:\Temp\Test Part.ipt")
Try
	'this rule will act on the active document
	iLogicVb.RunExternalRule(oRuleName)
	'or if you want to specify a different document, you can use this:
	'iLogicVb.Automation.RunExternalRule(oTargetDocument,oRuleName)
Catch oEx As Exception
	MsgBox("That external rule was not found. Exiting." & vbCrLf & _
	"The Error Message is as follows:" & vbCrLf & _
	oEx.Message & vbCrLf &  vbCrLf & _
	"Its 'StackTrace is as follows:" & vbCrLf & _
	oEx.StackTrace & vbCrLf & vbCrLf & _
	"Its source is as follows:" & vbCrLf & _
	oEx.Source, vbOKOnly + vbExclamation, "Couldn't Delete That Node")
	Exit Sub
End Try

 

 But if you want to really search for this external rule, here is an iLogic code that goes into more detail, attempting to find the actual file.

 

'Name of the rule to look for (without path or file extension)
'(Would be faster and simpler with path & file extension though.)
Dim oRuleName As String = "Rule 1"
Dim oAuto As IiLogicAutomation = iLogicVb.Automation
Dim oRuleDirs() As String = oAuto.FileOptions.ExternalRuleDirectories
Dim oRuleFile As String
'Search for this rule within each of the possible directories
'Searching using all three possible file extensions
For Each oRuleDir As String In oRuleDirs
	If IO.File.Exists(oRuleDir & "\" & oRuleName & ".iLogicVb") Then
		'the rule was found in this oRuleDir with this file extension
		oRuleFile = oRuleDir & "\" & oRuleName & ".iLogicVb"
	ElseIf IO.File.Exists(oRuleDir & "\" & oRuleName & ".txt") Then
		'the rule was found in this oRuleDir with this file extension
		oRuleFile = oRuleDir & "\" & oRuleName & ".txt"
	ElseIf IO.File.Exists(oRuleDir & "\" & oRuleName & ".vb") Then
		'the rule was found in this oRuleDir with this file extension
		oRuleFile = oRuleDir & "\" & oRuleName & ".vb"
	End If
Next
If oRuleFile = vbNullString Then
	MsgBox("The specified external rule was not found. Exiting.", vbOKOnly, " ")
	Exit Sub
Else
	'set-up to act upon the 'active' document
	oAuto.RunExternalRule(ThisApplication.ActiveDocument, oRuleFile)
End If

 

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes