Possible? Full File Path of External Rule when Run

Possible? Full File Path of External Rule when Run

techP5D3Q
Participant Participant
1,140 Views
13 Replies
Message 1 of 14

Possible? Full File Path of External Rule when Run

techP5D3Q
Participant
Participant

I would like to access the full directory path of the External Rule that is currently being executed. (Same manner as ActiveDocument.FullFileName".)

 

For example, if the External Rule "RuleNamePath" were executing, the access would return something like this:

 

"C:\...\Libraries\iLogic\Experimental Rules\RuleNamePath.txt"

 

I hit a dead end.

 

I can obtain the base file name of the current executing External Rule with:

 

Dim thisRuleName As String = iLogicVb.RuleName ' Added circa Inventor 2019.

 

I can list all the External Rule directories:

iLogicVb.Automation.FileOptions.ExternalRuleDirectories

 

 

But that does not help me resolve the (common for me) case of duplicate file names under different subdirectories.

 

(For example work proceeds in parallel on a new version of a rule under "\Experimental Rules\" that has been released as a stable version under "\Production Rules\".)

 

I had hoped that this structure would provide exactly the path I was looking for -- but it returns the same calling document (IDW, IPT, IAM) for both Internal and External Rule cases:

iLogicVb.RuleDocument.FullFileName

 

Is there a way to obtain this path?

 

Thank you.

0 Likes
Accepted solutions (1)
1,141 Views
13 Replies
Replies (13)
Message 2 of 14

JelteDeJong
Mentor
Mentor

I'm not aware of a direct function that can do that directly but you could do a fast search. something like this:

For Each directory In iLogicVb.Automation.FileOptions.ExternalRuleDirectories
	
	Dim directoryInfo As New System.IO.DirectoryInfo(directory)
	Dim thisRuleName As String = iLogicVb.RuleName
    Dim files = directoryInfo.GetFiles(thisRuleName & "*", IO.SearchOption.AllDirectories)

    If (files.Count = 1) Then
        Dim fileName = files(0).FullName
        MsgBox(fileName)
    End If
Next

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 14

techP5D3Q
Participant
Participant

Thank you @JelteDeJong for that code snippet.

 

I considered using this approach, but was hoping there was something more direct (like  iLogicVb.RuleDocument.FullFileName returning the path to the External Rule).

 

Iterating through External Rule directories will fail if iLogicVb.RuleName is not unique. I might be working on an update to a rule that is in use.  ( "\Experimental Rules\" versus "\Production Rules\".)

 

A second reason I wanted a full path to the External Rule was to make the rule automatically relocatable if it contained an Event Trigger assignment.  Currently moving a rule like this forces a manual edit to update the path to the rule.  As long as the RuleName *is* unique, iterating though directories would work.

 

Both issues (duplicate RuleNames and relocation) would be solved if there were a direct method to obtain the full path to the External Rule file that is executing.

 

0 Likes
Message 4 of 14

WCrihfield
Mentor
Mentor

Just a thought, but I wander if the text file for the external iLogic rule would be ReadOnly while the rule is in progress?  If that was the case, and there were multiple external rules with the same name, you could find each external rule file with that same name, and test if the file was ReadOnly or not, as a test of which one you were currently in the midst of running.  But I really doubt it would work that way, since code is usually copied and held in session memory for things like EventHandlers.  For instance, I can create and run a rule that creates an event handler.  Then after I have ran that rule, and the event handler is still active, I can change the contents of the rule that created it, but that will not actually effect how the already existing event handler will work, until you either restart Invenor, or remove the event handler, and recreate it.

Another thought would be something like a contents comparison, where you could maybe read in the text of the file, and compare it to the text of the current rule, but I'm not sure at the moment how that would work.

Basically, if you have two external iLogic rules with the same name, you should definately find a way to rename one of them.  Do you specify the full file name of the external rule when you run them, or do you always just run them directly from the External Rules tab, manually, or by button?  I'm guessing if you tried to run one of them by code, without specifing its full file name, it would probably always just run the first one it found.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 14

techP5D3Q
Participant
Participant

Thank you @WCrihfield for your thoughts.

 

I can create and run a rule that creates an event handler. Then after I have ran that rule, and the event handler is still active, I can change the contents of the rule that created it, but that will not actually effect how the already existing event handler will work, until you either restart Inventor, or remove the event handler, and recreate it.



Good to know. Will file this for later use.

 

Another thought would be something like a contents comparison, where you could maybe read in the text of the file, and compare it to the text of the current rule [...]

 

Interesting idea. MD5sum and SHA256 can be run from VB calls, but I think this approach exceeds my threshold of pain versus benefit. At least for today.

 

Basically, if you have two external iLogic rules with the same name, you should definitely find a way to rename one of them.

 

I like to keep production and experimental rules in separate subdirectories. Users have the option of running stable code or trying prototypes just by selecting the Run Rule from a different directory. Simple copy/paste of rules in the file manager worked fine to mange locations until I wanted to add the option for a rule to set itself up to run in response to a future trigger - that requires a manual edit of the subdirectory in the source code when assigning the event trigger.

 

Do you specify the full file name of the external rule when you run them, or do you always just run them directly from the External Rules tab, manually, or by button?

 

The rules that I am coding are simply run from the External Rules Tab. As a workflow enhancement I wanted to give the user the option of executing the rule one time, or to set it up to run automatically at a trigger event.


I'm guessing if you tried to run one of them by code, without specifying its full file name, it would probably always just run the first one it found.

 

Have not tried rules that call other rules yet. I will burn that bridge when I get to it. 🙂

 

I still wish that iLogicVb.RuleDocument.FullFileName would return the correct location for an External Rule.

 

Thank you very much.

0 Likes
Message 6 of 14

WCrihfield
Mentor
Mentor

You could always search within the Inventor Ideas forum to see if anyone else has requested an ability like that yet, and if none are found, you could create your own idea post about it.  It might take a long time to get enough votes, but you never know.  It sounds like you would want them to add something like:

iLgoicVb.RuleFullFileName

...where that would show the full file name of the external rule...when it was actually an extenal rule, and not an internal rule.  Otherwise it would maybe just return an empty String, because there is no standalone file for an internal iLogic rule.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 14

Frederick_Law
Mentor
Mentor

Why do you want to ID the file?

What will you do after you got the full path?

 

How about adding "Logger.Info("Experimental Rule")"?

0 Likes
Message 8 of 14

techP5D3Q
Participant
Participant

 

I will check the Ideas Forum.

 

Thank you @WCrihfield 

0 Likes
Message 9 of 14

techP5D3Q
Participant
Participant

In order to test for an existing trigger and then to properly add an External Rule to a trigger event I need the full path to the External Rule. This format works, but I manually fill in the subdirectory:

 

Dim thisRuleFullName As String = "file://" + thisRuleSubDir + "\" + thisRuleName 

The name of the rule, is now easy to get:

 

Dim thisRuleName As String = iLogicVb.RuleName ' Added circa Inventor 2019.

 Would be helpful if the path were as easy to obtain.

 

Thank you, @Frederick_Law 

0 Likes
Message 10 of 14

Frederick_Law
Mentor
Mentor

You try to replace "Experimental Rules" with "Production Rules" in trigger?

 

I think we are looking at a solution to your problem.

What was the "problem" this "solution" trying to solve?

0 Likes
Message 11 of 14

techP5D3Q
Participant
Participant

You try to replace "Experimental Rules" with "Production Rules" in trigger?

 

Of course, I do that manually right now.  Works great - so long as this manual edit is performed every time an External Rule is moved from one subdirectory to another.

 

Forget a manual edit and the old copy continues to run when the trigger is assigned.

 

I have to believe that somewhere deep in the bowels of Inventor External Rule structures there is a FullFileName string waiting to be surfaced.

 

 

What was the "problem" this "solution" trying to solve?

 

The problem is that there is (apparently) no reliable way to programmatically obtain the full path of an External Rule.

 

It seems only logical that if ActiveDocument.FullFileName can provide the full path to the current active document - there should be a way to obtain the full path to current External Rule that is executing. 

 

For internal rules iLogicVb.RuleDocument.FullFileName provides that function - External Rules are left out in the cold.

 

 

 

0 Likes
Message 12 of 14

Frederick_Law
Mentor
Mentor

If you use different name for the rule in Experiment and Production, all you need to do is rename the Experiment to Production and replace the file.

 

Your problem is trying to run Experiment and Production at the same time, I think.

0 Likes
Message 13 of 14

Maxim-CADman77
Advisor
Advisor
Accepted solution

iLogicVB.RuleFullFileName Property is available since release 2025.

Please vote for Inventor-Idea Text Search within Option Names

Message 14 of 14

techP5D3Q
Participant
Participant

Thank you for pointing this out!

 

I missed the new function that was added a year later than my original posting.

 

Below is a code segment that I used to test the new function.

 

(Made a copy of the same rule and placed it in two different directories.)

 

Thanks,

-phil

 

Sub main
	
Dim s As String = ""

Dim oDoc As Document = ThisApplication.ActiveDocument

' This returns the document path where it rule is invoked - regardless of the rule being Internal or External.
s += vbNewLine + "iLogicVb.RuleDocument.FullFileName:" + vbNewLine + vbTab + iLogicVb.RuleDocument.FullFileName + vbNewLine

' This provides a list of the external rule directories, but does not indicate which one is in use by the external rule that is being executed.
' (Just displaying the first entry.)
s += vbNewLine + "ExternalRuleDirectories" + vbNewLine + vbTab + iLogicVb.Automation.FileOptions.ExternalRuleDirectories.GetValue(0) + vbNewLine

' This solves the problem of determining exactly which external rule is being run. (New in release 2025)
' Provides a valid path for use in setting up a trigger to the external rule using iLogic code.
' Enables clean development of rule in an experimental directory in parallel with production use. 
s += vbNewLine + "RuleFullFilename" + vbNewLine + vbTab + iLogicVb.RuleFullFilename + vbNewLine

MessageBox.Show(s)
End Sub