I need help: Hole alignment checker for misaligned holes

I need help: Hole alignment checker for misaligned holes

Anonymous
Not applicable
4,087 Views
28 Replies
Message 1 of 29

I need help: Hole alignment checker for misaligned holes

Anonymous
Not applicable

We recently had a problem with a job at work. where a few of the holes on a bar did not align with the sheet it was bolted to. no one noticed till after we received the parts from the machine shop. One of my friends was telling me about solid works hole alignment checker.  I was wondering could I simulate this so I started searching the forums and this what i found. 

https://forums.autodesk.com/t5/inventor-customization/axial-alignment-of-holes/m-p/7203162

I am New to VBA so I truly do not understand it the code 100%. when I place the code in inventor says it no longer supports set command.

(Could this be done using Ilogic)

Accepted solutions (3)
4,088 Views
28 Replies
Replies (28)
Message 2 of 29

AlexFielder
Advisor
Advisor

Hi @Anonymous 

The code listed in that thread starts off as VBA (you can tell this because they make use of "set") and then migrates to VB.NET (which should work within Inventor's iLogic toolset):

 

Private Sub Body_Faces()
        Dim m_Inventor As Inventor.Application
        m_Inventor = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

        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 = m_Inventor.ActiveDocument

        Dim oCompdef As AssemblyComponentDefinition
        oCompdef = oDoc.ComponentDefinition

        Dim oBody1 As SurfaceBody
        Dim oBody2 As SurfaceBody

        oBody1 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select first body")
        oBody2 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select a body to compare")

        Dim oFace1 As Face
        Dim oFace2 As Face
        Dim oFaceColl1 As ObjectCollection
        oFaceColl1 = m_Inventor.TransientObjects.CreateObjectCollection()
        Dim oFaceColl2 As ObjectCollection
        oFaceColl2 = m_Inventor.TransientObjects.CreateObjectCollection()
        Dim oAxis1 As WorkAxis
        Dim oAxisColl1 As ObjectCollection
        oAxisColl1 = m_Inventor.TransientObjects.CreateObjectCollection()
        Dim oAxis2 As WorkAxis
        Dim oAxisColl2 As ObjectCollection
        oAxisColl2 = m_Inventor.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)

        m_Inventor.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

If you have some sample files or a pack & go assembly you can share that might help me/anyone else test that my above iLogic statement is in fact true.

 

Thanks,

 

Alex.

0 Likes
Message 3 of 29

Anonymous
Not applicable

Align error.PNGThis is the error I'm receiving. i assume it has to do with the way it is calling out the objects. thank you for the help

0 Likes
Message 4 of 29

AlexFielder
Advisor
Advisor

That's easy* - simply change the first method to "Sub Main()" and it'll work.

 

This usually happens when you copy in a pure VB.NET (or VBA) rule. iLogic will throw this message as it effectively compiles the rule into a .dll of its own, so needs "Sub Main()" to know where the entry point is when you're using "Sub" or "Function" methods.

 

*It's only easy if you know how.

0 Likes
Message 5 of 29

Anonymous
Not applicable

Thank you for all the help i have minor issues now, sub main was the key to get it working.

so know  all the code is not triggering its giving an error 

Sub main()
        Dim m_Inventor As Inventor.Application
        m_Inventor = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

        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 = m_Inventor.ActiveDocument

        Dim oCompdef As AssemblyComponentDefinition
        oCompdef = oDoc.ComponentDefinition

        Dim oBody1 As SurfaceBody
        Dim oBody2 As SurfaceBody

        oBody1 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select first body")
        oBody2 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select a body to compare")

        Dim oFace1 As Face
        Dim oFace2 As Face
        Dim oFaceColl1 As ObjectCollection
        oFaceColl1 = m_Inventor.TransientObjects.CreateObjectCollection()
        Dim oFaceColl2 As ObjectCollection
        oFaceColl2 = m_Inventor.TransientObjects.CreateObjectCollection()
        Dim oAxis1 As WorkAxis
        Dim oAxisColl1 As ObjectCollection
        oAxisColl1 = m_Inventor.TransientObjects.CreateObjectCollection()
        Dim oAxis2 As WorkAxis
        Dim oAxisColl2 As ObjectCollection
        oAxisColl2 = m_Inventor.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)

        m_Inventor.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

error 1.PNGerror 2.PNG 

0 Likes
Message 6 of 29

AlexFielder
Advisor
Advisor

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.

0 Likes
Message 7 of 29

Anonymous
Not applicable

thank you again. still puts out a couple errorserror 3.PNGerror 4.PNG

0 Likes
Message 8 of 29

AlexFielder
Advisor
Advisor

The code is attempting to add data to one of the files you had open, are you sure they're not read-only?

 

Failing that, do you have the time to put together a test assembly that you can share here? I don't currently have time to make one myself.

0 Likes
Message 9 of 29

Anonymous
Not applicable

yes you are correct this was trying to place in a file I do not have so wen tthrough all the code again and here is the one that seems to work. 

  Sub main Body_Faces()
        Dim m_Inventor As Inventor.Application
        m_Inventor = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

        Dim oDoc As AssemblyDocument
        oDoc = m_Inventor.ActiveDocument

        Dim oCompdef As AssemblyComponentDefinition
        oCompdef = oDoc.ComponentDefinition

        Dim oBody1 As SurfaceBody
        Dim oBody2 As SurfaceBody

        oBody1 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select first body")
        oBody2 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select a body to compare")

        Dim oFace As Face
        Dim oAxis As WorkAxis

        For Each oFace In oBody1.Faces

            If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
                oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)

                Dim comparedFace As Face
                comparedFace = CompareFaces(oAxis, oBody2, oCompdef)

                If comparedFace Is Nothing Then
                    oAxis.Name = oAxis.Name & "_misaligned"
                Else
                    Dim oHoleFeature1 As HoleFeature
                    oHoleFeature1 = oFace.CreatedByFeature

                    Dim dia1 As Double
                    dia1 = oHoleFeature1.HoleDiameter.Value
                    dia1 = dia1 * 10 'Converting cm to mm

                    Dim oHoleFeature2 As HoleFeature
                    oHoleFeature2 = comparedFace.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")
                    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")
                    Else
                        Debug.Print("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => Not OK")
                    End If

                End If

            End If

        Next

        Dim oPane As BrowserPane
        oPane = oDoc.BrowserPanes.ActivePane

        Dim alignedNodes As ObjectCollection
        alignedNodes = m_Inventor.TransientObjects.CreateObjectCollection

        Dim misAlignedNodes As ObjectCollection
        misAlignedNodes = m_Inventor.TransientObjects.CreateObjectCollection

        For i = 4 To oCompdef.WorkAxes.Count
            oAxis = oCompdef.WorkAxes.Item(i)
            Dim misaligned As Boolean
            misaligned = EndsWith(oAxis.Name, "misaligned")

            Dim oNode As BrowserNode

            If misaligned = True Then
                oNode = oPane.GetBrowserNodeFromObject(oAxis)
                misAlignedNodes.Add(oNode)
            Else
                oNode = oPane.GetBrowserNodeFromObject(oAxis)
                alignedNodes.Add(oNode)
            End If

        Next

        Dim alignedAxes As BrowserFolder
        alignedAxes = oPane.AddBrowserFolder("Aligned Axes", alignedNodes)

        Dim misAlignedAxes As BrowserFolder
        misAlignedAxes = oPane.AddBrowserFolder("MisAligned Axes", misAlignedNodes)

    End Sub

    Public Function CompareFaces(ByVal oAxis1 As WorkAxis, ByVal oBody As SurfaceBody, ByVal compDef As AssemblyComponentDefinition) As Face
        For Each oFace As Face In oBody.Faces
            If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
                Dim oAxis As WorkAxis

                oAxis = compDef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)

                If oAxis.Line.IsColinearTo(oAxis1.Line) Then
                    oAxis.Delete()
                    Return oFace
                    Exit Function
                Else
                    oAxis.Delete()
                End If
            End If
        Next
        Return Nothing
    End Function

    Public Function EndsWith(Str As String, ending As String) As Boolean
        Dim endingLen As Integer
        endingLen = Len(ending)
        EndsWith = (Microsoft.VisualBasic.Right(Trim(UCase(str)), endingLen) = UCase(ending))
    End Function

 

0 Likes
Message 10 of 29

Anonymous
Not applicable

sorry I have never posted a file on here. this file has code ran once on it 

0 Likes
Message 11 of 29

Anonymous
Not applicable

I'm pretty sure I replied to myself 

0 Likes
Message 12 of 29

AlexFielder
Advisor
Advisor
Accepted solution

You were nearly there. Try this updated rule:

Sub Main()
    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 oFace As Face
    Dim oAxis As WorkAxis

    For Each oFace In oBody1.Faces

        If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
            oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)

            Dim comparedFace As Face
            comparedFace = CompareFaces(oAxis, oBody2, oCompdef)

            If comparedFace Is Nothing Then
                oAxis.Name = oAxis.Name & "_misaligned"
            Else
                Dim oHoleFeature1 As HoleFeature
                oHoleFeature1 = oFace.CreatedByFeature

                Dim dia1 As Double
                dia1 = oHoleFeature1.HoleDiameter.Value
                dia1 = dia1 * 10 'Converting cm to mm

                Dim oHoleFeature2 As HoleFeature
                oHoleFeature2 = comparedFace.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")
                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")
                Else
                    Debug.Print("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => Not OK")
                End If

            End If

        End If

    Next

    Dim oPane As BrowserPane
    oPane = oDoc.BrowserPanes.ActivePane

    Dim alignedNodes As ObjectCollection
    alignedNodes = ThisApplication.TransientObjects.CreateObjectCollection

    Dim misAlignedNodes As ObjectCollection
    misAlignedNodes = ThisApplication.TransientObjects.CreateObjectCollection

    For i = 4 To oCompdef.WorkAxes.Count
        oAxis = oCompdef.WorkAxes.Item(i)
        Dim misaligned As Boolean = oAxis.Name.EndsWith("misaligned")
		
        Dim oNode As BrowserNode

        If misaligned = True Then
            oNode = oPane.GetBrowserNodeFromObject(oAxis)
            misAlignedNodes.Add(oNode)
        Else
            oNode = oPane.GetBrowserNodeFromObject(oAxis)
            alignedNodes.Add(oNode)
        End If

    Next

    Dim alignedAxes As BrowserFolder
    alignedAxes = oPane.AddBrowserFolder("Aligned Axes", alignedNodes)

    Dim misAlignedAxes As BrowserFolder
    misAlignedAxes = oPane.AddBrowserFolder("MisAligned Axes", misAlignedNodes)

End Sub

Public Function CompareFaces(ByVal oAxis1 As WorkAxis, ByVal oBody As SurfaceBody, ByVal compDef As AssemblyComponentDefinition) As Face
    For Each oFace As Face In oBody.Faces
        If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
            Dim oAxis As WorkAxis

            oAxis = compDef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)

            If oAxis.Line.IsColinearTo(oAxis1.Line) Then
                oAxis.Delete()
                Return oFace
                Exit Function
            Else
                oAxis.Delete()
            End If
        End If
    Next
    Return Nothing
End Function

I tried the above on your assembly and it works perfectly. Two suggestions I have which may prove to be useful additions are:

 

1. Wrap the placement of new work axes in a transaction so you can undo the creation in one click.

2. Search for and delete existing renamed work axes and associated folders before adding new.

 

🙂

0 Likes
Message 13 of 29

Anonymous
Not applicable

I really appreciate all the help. there is one section of code I don't understand. I'm pretty sure this is checking and comparing hole diameter but I don't see it showing this data anywhere on the screen. I have so many questions I don't want to take up all your time.


                    Dim dia1 As Double
                    dia1 = oHoleFeature1.HoleDiameter.Value
                    dia1 = dia1 * 10 'Converting cm to mm

                    Dim oHoleFeature2 As HoleFeature
                    oHoleFeature2 = comparedFace.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")
                    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")
                    Else
                        Debug.Print("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => Not OK")
                    End If

                End If

            End If

    

 

0 Likes
Message 14 of 29

AlexFielder
Advisor
Advisor
Accepted solution

If you were using Inventor 2019, you could pipe the output from the iLogic to the iLogic logging panel. You can swap the "debug.print()" for messagebox.show() if you want to prompt the user in lieu of using Inventor 2019.

0 Likes
Message 15 of 29

Anonymous
Not applicable

yes thaterror 5.PNG worked perfect 

Message 16 of 29

Anonymous
Not applicable

is using sub. like running three separate instances of code? when I place my transaction code can I add it to a new submenu and add otrans.end   

0 Likes
Message 17 of 29

Anonymous
Not applicable

here is finished code with transaction modifier to undo all steps in one click

 
Sub Main() Dim m_Inventor As Inventor.Application m_Inventor = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") Dim oDoc As AssemblyDocument oDoc = m_Inventor.ActiveDocument Dim oCompdef As AssemblyComponentDefinition oCompdef = oDoc.ComponentDefinition Dim oBody1 As SurfaceBody Dim oBody2 As SurfaceBody oBody1 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select first body") oBody2 = m_Inventor.CommandManager.Pick(SelectionFilterEnum.kPartBodyFilter, "Select a body to compare") Dim oFace As Face Dim oAxis As WorkAxis Dim trans As Transaction = ThisApplication.TransactionManager.StartTransaction(oDoc, "Hole Alignment") For Each oFace In oBody1.Faces If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector) Dim comparedFace As Face comparedFace = CompareFaces(oAxis, oBody2, oCompdef) If comparedFace Is Nothing Then oAxis.Name = oAxis.Name & "_misaligned" Else Dim oHoleFeature1 As HoleFeature oHoleFeature1 = oFace.CreatedByFeature Dim dia1 As Double dia1 = oHoleFeature1.HoleDiameter.Value dia1 = dia1 * 10 'Converting cm to mm Dim oHoleFeature2 As HoleFeature oHoleFeature2 = comparedFace.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 messagebox.Show("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => OK - Default") ElseIf Math.Round(dia1, 4) >= Math.Round(dia2, 4) And Math.Round(dia1, 4) <= Math.Round(dia2, 4) + 0.5 Then messagebox.Show("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => OK - Special Type") Else messagebox.Show("Hole diameter of plate 1 = " & dia1 & " compared with Hole diameter of plate 2 = " & dia2 & " => Not OK") End If End If End If Next Dim oPane As BrowserPane oPane = oDoc.BrowserPanes.ActivePane Dim alignedNodes As ObjectCollection alignedNodes = m_Inventor.TransientObjects.CreateObjectCollection Dim misAlignedNodes As ObjectCollection misAlignedNodes = m_Inventor.TransientObjects.CreateObjectCollection For i = 4 To oCompdef.WorkAxes.Count oAxis = oCompdef.WorkAxes.Item(i) Dim misaligned As Boolean misaligned = EndsWith(oAxis.Name, "misaligned") Dim oNode As BrowserNode If misaligned = True Then oNode = oPane.GetBrowserNodeFromObject(oAxis) misAlignedNodes.Add(oNode) Else oNode = oPane.GetBrowserNodeFromObject(oAxis) alignedNodes.Add(oNode) End If Next Dim alignedAxes As BrowserFolder alignedAxes = oPane.AddBrowserFolder("Aligned Axes", alignedNodes) Dim misAlignedAxes As BrowserFolder misAlignedAxes = oPane.AddBrowserFolder("MisAligned Axes", misAlignedNodes) trans.End() End Sub Public Function CompareFaces(ByVal oAxis1 As WorkAxis, ByVal oBody As SurfaceBody, ByVal compDef As AssemblyComponentDefinition) As Face For Each oFace As Face In oBody.Faces If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then Dim oAxis As WorkAxis oAxis = compDef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector) If oAxis.Line.IsColinearTo(oAxis1.Line) Then oAxis.Delete() Return oFace Exit Function Else oAxis.Delete() End If End If Next Return Nothing End Function Public Function EndsWith(Str As String, ending As String) As Boolean Dim endingLen As Integer endingLen = Len(ending) EndsWith = (Microsoft.VisualBasic.Right(Trim(UCase(Str)), endingLen) = UCase(ending)) End Function

 

Message 18 of 29

Anonymous
Not applicable

Hi, 

Just got back to this project, I'm having an error after I opened original template back up

 

from all that have read online it has to do with how I'm choosing my document.

I tried placing this code and I received line errors after that.

  oDoc = m_Inventor.ActiveDocument is there another way this could be written 
oDoc = thisapplication.activedocument

error 1.PNGerror 2.PNG

0 Likes
Message 19 of 29

AlexFielder
Advisor
Advisor

This is exactly what I changed when we last spoke. I don't think you clicked the "diff" link I shared which showed this:

2019-02-14 13_46_13-Saved diff aFbbGegb - Diff Checker.png

 

diff link is here once again for your perusal: https://www.diffchecker.com/aFbbGegb

 

🙂

0 Likes
Message 20 of 29

Anonymous
Not applicable

so anywhere I have m.invetor I need to use ThisApplication. since I did not change all the places I was throwing an error correct?. I only replaced the first one that would explain the reason I was getting the error. I'm going to try it now