Wednesday, August 17, 2011

Built for Speed: Running iLogic Rules Automatically When Saved, For New and Existing (pre-iLogic) Inventor Files

You've created what is possibly the best collection of iLogic rules ever! And you'd like the rules to run on your existing parts, assemblies and drawings when you save them, after making edits and updates. But after a bit of investigation you find that Autodesk hasn't provided a way to run rules on existing (pre-iLogic) files without manually adding an event trigger to each one. Now you must rely on your memory or the memory of your co-workers, to either add an event trigger, or run the rule manually. Is there a better way?

Engine model courtesy of GrabCAD and Terry Stonehocker

Jürgen Wagner has posted an add-in created by his colleague that allows you to get very close to the goal of running rules automatically in existing (pre-iLogic) files. It works by running a rule called IPT for every IPT that is saved, a rule called IAM for every IAM that is saved, etc. You can add any iLogic code to these rules, and then it will be run each time the file type is saved.

You can download and install this add-in by visiting Jürgen's blog (written in German)  Incidentally, Jürgen's blog contains some outstanding Inventor tips and tricks besides just this add-in, and I always enjoy checking in to see what he's come up with. So a big thanks for the time and effort to Jürgen.

I'll detail the procedure to set this add in up and get it running for you in the following steps.

  • First, I'll visit Jürgen's blog page and find the dropbox download link to obtain the zip file containing 32 bit and 64 bit versions of the add-in for Inventor 2010, 2011 and 2012 ( here is the direct link also).
  • Next, I'll ensure that Inventor is not open.
  • Then, I'll extract the contents of the zip file and then select and run the version that matches my operating system and Inventor release.  

  • Next  I'll choose the install path (or just accept the defaults). Here is a translated version of the extraction dialog:
  • Once the install is done, I'll start Inventor and go to the Tools tab and click the Add-Ins button to ensure the add-in installed correctly.
  • I'll look for the add-in called MuM iLogic Run on Save, then click OK to exit the Add-Ins dialog box once I've confirmed the add-in is present.
    • Next, with no files open, I'll go to the tools tab and expand the Options panel to reveal the iLogic Configuration button.
    • Then I'll set the path for the folder in which I store my external Inventor rules. Note that this would typically be next to, or under, the project (IPJ) file path.

    • To access the iLogic Browser I need to have a file open, so I'll open any file, or I'll start a new file from any template.
    • Then I'll go to the Manage tab and click the iLogic Browser button.
    • I'll Switch to the External Rules tab, and then click on the top level node and choose Create New External Rule.

    • Next, I'll name the rule IPT as shown. Note that in this screen shot I have several other external rules already created:

    • This will create a rule file called IPT.iLogicVb. The resulting IPT rule will be empty until I add some code. The way the add-in works, is that it runs this IPT rule every time I save an IPT file.
    • Next, I'll repeat this for all of the Inventor file extensions (IPT, IAM, IPN, IDW and DWG), until I have an empty rule for each Inventor file type. Here is my iLogic Browser and the folder containing my external rules, for comparison:

    • Next I'll edit the IPT rule (by right-clicking it in the browser) and then add some code:
    • You can add code to this rule as you would any other iLogic rule, but I prefer to use these automatic save rules to call other existing rules. So I'll use the RunExternalRule line to call some of my other external rules when the IPT rule is run:

    • I'll add several rules that I will typically want to run on IPT files when I'm saving them. These can be new or existing IPT files. The basic syntax is:
    iLogicVb.RunExternalRule("Your Rule Name Here")

      Okay, so that's really all there is to it. Now whenever I save any IPT file, the IPT rule is triggered, and in my example, it will run five other rules.

      But what if I don't want the rule to run on every single IPT file I save? For instance I might only want the Export STEP rule to be triggered in certain cases.

      To deal with this, I can add a "skip it" conditional statement that looks at an iProperty flag that is set by another rule. For instance, I might create an external rule called Skip-It, that has this simple line in it:

      iProperties.Value("Summary", "Comments") = "X"

      I can then run the Skip-It rule in any file that I know I don't want a particular rule to run in, and it will write X to the Comments iProperty:

       Then I would set up my Export STEP rule to check for the X in the Comments iProperty, and do nothing if the X is found:

      '---start iLogic ----
      'check for skip it flag
      If iProperties.Value("Summary", "Comments") = "X" Then
      ' Get the STEP translator Add-In.
      Dim oSTEPTranslator As TranslatorAddIn
      oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById _
      Dim oContext As TranslationContext
      oContext = ThisApplication.TransientObjects.CreateTranslationContext
      Dim oOptions As NameValueMap
      oOptions = ThisApplication.TransientObjects.CreateNameValueMap

      If oSTEPTranslator.HasSaveCopyAsOptions _
      (ThisApplication.ActiveDocument, oContext, oOptions) Then
          oOptions.Value("ApplicationProtocolType") = 3
          oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
          Dim oData As DataMedium
          oData = ThisApplication.TransientObjects.CreateDataMedium
          oData.FileName = ThisDoc.PathAndFileName(False) & ".stp"
      oSTEPTranslator.SaveCopyAs _
      (ThisApplication.ActiveDocument, oContext, oOptions, oData)
      End If
      End If
      '---end iLogic ----

      This is just one example of using a condition to control when a rule is or isn't run, I'm sure you'll come up with your own (probably more sophisticated) conditions.

      Note that the MuM iLogic Run on Save add-in tool is provided "as is" and does not include support or other assistance from MuM, Jürgen, or myself. But I've tested it on Inventor 2010 and 2012 and have found it to work without issue, so give it a try.

      Since this was first written, others in the Inventor community have been inspired to improve upon the idea. Here are a couple of links with other tools that can be used to add iLogic rule to existing files as well: