Autodesk Inventor – iLogic – Create a New iLogic Rule Automatically

Question: Can you automatically create an iLogic rule in an Inventor file?

Answer: Yes you can – there’s loads of ways to do this. Some simpler than others.

  1. Google the fantastic ‘iLogic Code Injector’ which allows you to batch create iLogic rules in multiple Inventor files at once.
  2. Check out this ‘Being Inventive’  article. However if you’re just dabbling with iLogic it’ll probably be a bit scary – so you might want option 3…..
  3. Copy and paste my simple iLogic rule below. Run it in any Inventor file and check out the new iLogic rule you’ve just created!

You ask – why would you want an iLogic rule that creates an iLogic rule? Good question. There could be loads of uses for it. In my case I needed to do a lookup from an Excel spreadsheet to set some parameters in an Inventor model. After the model is sized I wanted to create a link to the parameters in the Excel spreadsheet by creating an iLogic rule that links the model and spreadsheet permanently.

A useful trick to have up your sleeve anyway…

Hope you enjoy.

SyntaxEditor Code Snippet

' Start of iLogic rule =================================================
' This iLogic rule automatically creates a new iLogic rule called 'Luke's Amazing iLogic Rule' in the active document.

Dim RuleName As String = "Lukes Amazing iLogic Rule"
Dim RuleText As String = "' Hello - This is an automatically created iLogic rule" & vbCrLf & _
                         "' What does it do?" & vbCrLf & _
                         "' Just changes the title of your Inventor application I'm afraid" & vbCrLf & _
                         "' You'll have to come up with something more exciting.... " & vbCrLf & vbCrLf & _
                         "ThisApplication.Caption = ThisApplication.Username"
                         
Dim oDoc As Document = ThisApplication.ActiveEditDocument                         
Dim RuleAlreadyExists As Boolean = False
' Define the iLogic addin
Dim iLogicAddIn As ApplicationAddIn = ThisApplication.ApplicationAddIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
' Get the iLogic automation object
Dim iLogic As Object = iLogicAddIn.Automation
' Get the list of iLogic rules in the current Inventor document
Dim RuleList As Object = iLogic.Rules(oDoc)

' Loop through all rules in document
For Each R As Object In RuleList
    If R.Name = RuleName Then
        ' A rule with the same name already exists...
        RuleAlreadyExists = True
        Exit For
    End If
Next

If RuleAlreadyExists Then
    Dim ReplaceRule As MsgBoxResult = MsgBox("A rule called '" & RuleName & "' already exists - replace it?", 36, "Excitech iLogic")
    If ReplaceRule = vbYes Then
        ' Delete the existing rule
        iLogic.DeleteRule(oDoc, RuleName)
        ' Add the new rule
        iLogic.AddRule(oDoc, RuleName, RuleText)
    Else
        MsgBox("Operation cancelled...", 64, "Excitech iLogic")
        Exit Sub
    End If
Else
    ' No existing rule to delete - simply add the new rule
    iLogic.AddRule(oDoc, RuleName, RuleText)
End If

' End of iLogic rule =================================================
Advertisements

6 thoughts on “Autodesk Inventor – iLogic – Create a New iLogic Rule Automatically

  1. Great rule but when i trying to insert the rule
    “iLogicVb.RunExternalRule(“Drawingnumber”)”
    It won’t work because of the quotes inside the quotes. How can I solve this?
    Thanks in advance!

    Like

    1. Hi Tommy,
      Good question – you can insert speech marks in the string with double speech marks as shown below. Does this help?
      Luke

      Dim RuleText As String = “‘ Hello – this iLogic Rule simply runs another rule” & vbCrLf & _
      “iLogicVb.RunRule(“”Random iLogic Rule””)”

      Like

  2. No unfortunately not the following error pops up: Object reference not set to an instance of an object. The only thing i changed is : “iLogicVb.RunExternalRule(“”Drawingnumber””)”

    Like

    1. Hi Tommy, I tried it myself (creating text to run an external rule), and it worked fine. Are you sure you actually have an external rule called ‘Drawingnumber’ and the spelling/capitalisation is correct?

      Like

      1. Ah I found the problem, I don’t have any “local”rules in my documents. only external rules. So it can’t generate a rules list to find an existing rule. When I comment out the messageboxes, and just Always delete and replace the rule it works fine.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s