It's probably due to the VB.NET code originally having been written to control Inventor from an external .exe - here's a diff I just made with my suggested changes:
https://www.diffchecker.com/aFbbGegb
and here's the changed code:
Sub main()
If System.IO.File.Exists("c:\Temp\Bohrungen.txt") Then
System.IO.File.Delete("c:\Temp\Bohrungen.txt")
End If
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("c:\Temp\Bohrungen.txt", True)
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument
Dim oCompdef As AssemblyComponentDefinition
oCompdef = oDoc.ComponentDefinition
Dim oBody1 As SurfaceBody
Dim oBody2 As SurfaceBody
oBody1 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select first body")
oBody2 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select a body to compare")
Dim oFace1 As Face
Dim oFace2 As Face
Dim oFaceColl1 As ObjectCollection
oFaceColl1 = ThisApplication.TransientObjects.CreateObjectCollection()
Dim oFaceColl2 As ObjectCollection
oFaceColl2 = ThisApplication.TransientObjects.CreateObjectCollection()
Dim oAxis1 As WorkAxis
Dim oAxisColl1 As ObjectCollection
oAxisColl1 = ThisApplication.TransientObjects.CreateObjectCollection()
Dim oAxis2 As WorkAxis
Dim oAxisColl2 As ObjectCollection
oAxisColl2 = ThisApplication.TransientObjects.CreateObjectCollection()
For Each oFace1 In oBody1.Faces
If oFace1.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
oAxis1 = oCompdef.WorkAxes.AddFixed(oFace1.Geometry.BasePoint, oFace1.Geometry.AxisVector)
If IsAxisDuplicate(oAxis1, oAxisColl1) = False Then
oAxisColl1.Add(oAxis1)
oFaceColl1.Add(oFace1)
Else
oAxis1.Delete()
End If
End If
Next
For Each oFace2 In oBody2.Faces
If oFace2.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
oAxis2 = oCompdef.WorkAxes.AddFixed(oFace2.Geometry.BasePoint, oFace2.Geometry.AxisVector)
If IsAxisDuplicate(oAxis2, oAxisColl2) = False Then
oAxisColl2.Add(oAxis2)
oFaceColl2.Add(oFace2)
Else
oAxis2.Delete()
End If
End If
Next
For i As Integer = 1 To oAxisColl1.Count
oAxis1 = oAxisColl1.Item(i)
Dim j As Integer = 0
j = CompareAlignment(oAxis1, oAxisColl2)
If j > 0 Then
oFace1 = oFaceColl1.Item(i)
oFace2 = oFaceColl2.Item(j)
CompareFace(oFace1, oFace2, file)
oFace1.AttributeSets.Add("Aligned")
oFace2.AttributeSets.Add("Aligned")
End If
Next
For Each oFace1 In oFaceColl1
If oFace1.AttributeSets.Count = 0 Then
Dim oHoleFeature As HoleFeature
oHoleFeature = oFace1.CreatedByFeature
Dim dia As Double
dia = oHoleFeature.HoleDiameter.Value * 10 ' Converting cm to mm
file.WriteLine("Hole diameter of plate 1 = " & "Ø" & dia & " - " & oHoleFeature.ExtendedName & " => misaligned")
End If
Next
For Each oFace2 In oFaceColl2
If oFace2.AttributeSets.Count = 0 Then
Dim oHoleFeature As HoleFeature
oHoleFeature = oFace2.CreatedByFeature
Dim dia As Double
dia = oHoleFeature.HoleDiameter.Value * 10 ' Converting cm to mm
file.WriteLine("Hole diameter of plate 2 = " & "Ø" & dia & " - " & oHoleFeature.ExtendedName & " => misaligned")
End If
Next
CleanAxis(oAxisColl1)
CleanAxis(oAxisColl2)
ThisApplication.StatusBarText = "Ausgabe in c:\Temp\Bohrungen.txt"
file.Close()
Process.Start("c:\Temp\Bohrungen.txt")
End Sub
Public Sub CleanAxis(ByVal oAxisColl As ObjectCollection)
For Each oAxis As WorkAxis In oAxisColl
oAxis.Delete()
Next
End Sub
Public Sub CompareFace(ByVal oFace1 As Face, ByVal oFace2 As Face, ByVal File As System.IO.StreamWriter)
Dim oHoleFeature1 As HoleFeature
oHoleFeature1 = oFace1.CreatedByFeature
Dim dia1 As Double
dia1 = oHoleFeature1.HoleDiameter.Value
dia1 = dia1 * 10 'Converting cm to mm
Dim oHoleFeature2 As HoleFeature
oHoleFeature2 = oFace2.CreatedByFeature
Dim dia2 As Double
dia2 = oHoleFeature2.HoleDiameter.Value
dia2 = dia2 * 10 'Converting cm to mm
If Math.Round(dia1, 4) = Math.Round(dia2, 4) Then
Debug.Print("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => OK - Default")
file.WriteLine("Hole diameter of plate 1 = " & "Ø" & dia1 & " - " & oHoleFeature1.ExtendedName & " compared with Hole diameter of plate 2 = " & "Ø" & dia2 & " - " & oHoleFeature2.ExtendedName & " => OK - Default")
ElseIf Math.Round(dia1, 4) >= Math.Round(dia2, 4) And Math.Round(dia1, 4) <= Math.Round(dia2, 4) + 0.5 Then
Debug.Print("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => OK - Special Type")
file.WriteLine("Hole diameter of plate 1 = " & "Ø" & dia1 & " - " & oHoleFeature1.ExtendedName & " compared with Hole diameter of plate 2 = " & "Ø" & dia2 & " - " & oHoleFeature2.ExtendedName & " => OK - Special Type")
Else
Debug.Print("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => Not OK")
file.WriteLine("Hole diameter of plate 1 = " & "Ø" & dia1 & " - " & oHoleFeature1.ExtendedName & " compared with Hole diameter of plate 2 = " & "Ø" & dia2 & " - " & oHoleFeature2.ExtendedName & " => Not OK")
End If
End Sub
Public Function CompareAlignment(ByVal oAxis As WorkAxis, ByVal oAxisCollection As ObjectCollection) As Integer
For i As Integer = 1 To oAxisCollection.Count
Dim axis As WorkAxis
axis = oAxisCollection.Item(i)
If oAxis.Line.IsColinearTo(axis.Line) Then
Return i
Exit Function
End If
Next
Return 0
End Function
Public Function IsAxisDuplicate(ByVal oAxis As WorkAxis, ByVal oAxisColl As ObjectCollection) As Boolean
If oAxisColl.Count > 0 Then
For i As Integer = 1 To oAxisColl.Count
Dim Axis As WorkAxis
Axis = oAxisColl.Item(i)
If Axis.Line.IsColinearTo(oAxis.Line) Then
Return True
End If
Next
Return False
Else
Return False
End If
End Function
I basically changed all the references to m_Inventor to ThisApplication. It should work with these changes, but let me know if not.