Detecting Interference with iLogic

Autodesk Inventor - iLogic - Detecting Interfernce.png

Inventor has a decent set of tools available for checking component interference, but what about running an interference check on the entire model with one click?

I have adapted one of the Inventor API samples to work with iLogic, the code iterates through an assembly and highlights the interfering parts. The animated GIF below shows how the code works on a small assembly.

autodeskinventor-interferencechecker-clintbrown

Notes from the API sample tell us that the code is designed to work as follows:

  • If no occurrences are selected check for interference of all parts against all parts.
  • If one occurrence is selected, check for interference between that occurrence and the rest of the assembly.
  • If more than one occurrence is selected let the user decide if it should check for interference between the parts in the selection or between the selected parts and the rest of the assembly.

I have also put some error handling into my version of the code. If the code is run in a part file, or if there is a problem processing an assembly file, the user will be shown this message box.

2019-02-01_11h55_05

This is really easy to implement in any code, simple put in a line of code at the top of the rule which states “On Error GoTo ErrorCatcherName”, and then at the bottom of your code, simply start a line with “ErrorCatcherName :” with a message box below.

On Error GoTo Error1

'Ilogic code goes here

Error1 :
MessageBox.Show("We ran into a problem, Please ensure that you are in an assembly file, or try the manual Interference Checker", "@ClintBrown3D")

But beware that this will show your error message for ANY issue that comes up in the rule.

Here’s the full iLogic code:

On Error GoTo Error1 ' Code adapted from the Inventor API Sample to work with iLogic by Clint Brown
'Code originally posted at https://clintbrown.co.uk/interference
    Dim oDoc As AssemblyDocument
     oDoc = ThisApplication.ActiveDocument
    
    ' Find all selected occurrences and add them to an ObjectCollection.
    Dim oSelectedOccs As ObjectCollection
     oSelectedOccs = ThisApplication.TransientObjects.CreateObjectCollection
    Dim i As Long
'    For i = 1 To oDoc.Select.Count
For i = 1 To oDoc.SelectSet.Count
        If oDoc.Select.Item(i).Type = kComponentOccurrenceObject Then
            oSelectedOccs.Add(oDoc.Select.Item(i))
        End If
    Next
    
 
    Dim oResults As InterferenceResults
    Dim oCheck As ObjectCollection
     oCheck = ThisApplication.TransientObjects.CreateObjectCollection
    If oSelectedOccs.Count = 0 Then
        ' Add all occurrences to the object collection
        Dim oOcc As ComponentOccurrence
        For Each oOcc In oDoc.ComponentDefinition.Occurrences
            oCheck.Add(oOcc)
        Next
        
        ' Get the interference between everything.
         oResults = oDoc.ComponentDefinition.AnalyzeInterference(oCheck)
    ElseIf oSelectedOccs.Count = 1 Then
        ' Add all occurrences except the selected occurrence to the object collection.
        For Each oOcc In oDoc.ComponentDefinition.Occurrences
            If Not oOcc Is oSelectedOccs.Item(1) Then
                oCheck.Add(oOcc)
            End If
        Next
        
        ' Get the interference between the selected occurrence everything else.
         oResults = oDoc.ComponentDefinition.AnalyzeInterference(oSelectedOccs, oCheck)
    Else
        If MsgBox("Check interference between selected occurrences and all other occurrences?", "@ClintBrown3D", vbYesNo + vbQuestion) = vbYes Then
            ' Add all occurrences except the selected occurrences to the object collection.
            For Each oOcc In oDoc.ComponentDefinition.Occurrences
                ' Check to see if this occurrences is already selected.
                Dim bSelected As Boolean
                bSelected = False
                For i = 1 To oSelectedOccs.Count
                    If oSelectedOccs.Item(i) Is oOcc Then
                        bSelected = True
                        Exit For
                    End If
                Next
                
                If Not bSelected Then
                    oCheck.Add(oOcc)
                End If
            Next
           
            ' Check interference between the selected items.
             oResults = oDoc.ComponentDefinition.AnalyzeInterference(oSelectedOccs, oCheck)
        Else
            ' Check interference between the selected items.
             oResults = oDoc.ComponentDefinition.AnalyzeInterference(oSelectedOccs)
        End If
    End If
    
    If oResults.Count = 1 Then
        MsgBox("There is 1 interference.")
    ElseIf oResults.Count > 1 Then
		MessageBox.Show("There are " & oResults.Count & " interferences.", "@ClintBrown3D")
    End If

    If oResults.Count > 0 Then
		Dim oHS1 As HighlightSet
        oHS1 = oDoc.CreateHighlightSet
        oHS1.Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0)
        Dim oHS2 As HighlightSet
         oHS2 = oDoc.CreateHighlightSet
        oHS2.Color = ThisApplication.TransientObjects.CreateColor(0, 255, 0)
        
        For i = 1 To oResults.Count
            oHS1.Clear
            oHS2.Clear
            oHS1.AddItem(oResults.Item(i).OccurrenceOne)
            oHS2.AddItem(oResults.Item(i).OccurrenceTwo)
         	MessageBox.Show("Occurrences are highlighted from interference " & i, "@ClintBrown3D")

        Next
        oHS1.Clear
        oHS2.Clear
    Else
		MessageBox.Show("There is no interference :)", "@ClintBrown3D")
    End If
Return

Error1 :
MessageBox.Show("We ran into a problem, Please ensure that you are in an assembly file, or try the manual Interference Checker", "@ClintBrown3D")