After days of laborious work to create yourself the golden iLogic rule that your company cannot live without, the realization suddenly strikes that your golden rule will only work on newly created files.
How can you retrofit an iLogic rule to already existing files?
In iLogic, there is not such a thing like "automacros" such as in VBA macros.
How do you avoid having to paste your iLogic code manually in each and every file?
External rule anyone?
A piece to the solution of this puzzle is to use an external rule.
An external rule has one big advantage over a local rule.
It can be modified in a single central location while still affecting all files that call that external rule.
Calling an external rule in iLogic is very simple:
iLogicVb.RunExternalRule("externalrule.txt")
To focus attention, let's do something very basic in the external rule like bringing up a message box that displays the filename.
The rule in externalrule.txt to accomplish this, would be
MessageBox.Show( "File name of active document is " + Chr(13) + ThisDoc.PathAndFileName(True), "File name" )
But how can you get this one-liner systematically included in existing files?
API coming to the rescue
You can achieve this by using the AddRule command from the iLogic API as documented in the file called "Autodesk.iLogic.Interfaces.xml".
This file is located in the Inventor bin folder if you like to dive in for more details.
To put it all together, I created a VBA ivb project and an external rule. Both files are all you need to make it work and can be downloaded here.
Here is the procedure to use the files. It is important that you follow all these steps without skipping any.
1) First unzip the ivb and txt files to any folder. Example: C:\Shared\myivbprojects
2) In Inventor Tools > Options > iLogic configuration and add the name of your folder to the external rule directory paths.
Note : this command sits in the expanded panel by default, so it is rather difficult to find.
Figure 1: Adding location of external rules
3) Load the ivb project in the VBA editor via File > Load project ...
4) Run the macro initialize_iLogic_rule() in module Start_stop
5) Create a new Inventor file or open an existing file (if you want to treat multiple files you can also drag and drop files from Windows Explorer to Inventor)
6) An iLogic rule called "Myrule1" will be automatically created (if you run the rule you will see that it will try to display the filename)
7) Do not forget to turn off the automatic rule creation process if you no longer need it. You can do so by running the macro called stop_initialize_iLogic_rule() in module Start_stop.
Step 7 is really important if you want to stop adding rules automatically.
Extrapolation to more rules
Above code is dealing with a single external rule. What if you want to add multiple rules in one operation?
For example if you want to add a second rule, you will have to change just a single line of code and make sure that the extra string you provide matches the external file name Myrule2.txt etc:
To create two rules simultaneously, modify this line
listofrules = Array("Myrule1")
By adding an extra string
listofrules = Array("Myrule1", "Myrule2")
Make sure that the external rules with corresponding names exist:
Myrule1.txt , Myrule2.txt , etc
Now if someone can find the trick to automatically set triggers to run rules then we would have a complete automated solution … Anyone out there?
Have fun
Bob