Axial alignment of holes

Axial alignment of holes

GeorgK
Advisor Advisor
4,735 Views
42 Replies
Message 1 of 43

Axial alignment of holes

GeorgK
Advisor
Advisor

Hello together,

 

is there a way to calculate the axial alignment of holes to see if they fit?

 

Alignment.jpg

 

Thank you very much

 

Georg

0 Likes
4,736 Views
42 Replies
Replies (42)
Message 2 of 43

Anonymous
Not applicable

I am also looking at doing this but with tapped holes and threads too. I know there is a way of finding the vector of a tapped hole (similar with threads) but have only just started looking.

For Each Hole In PartDef.Features.HoleFeatures

Set HTI = Hole.TapInfo

HTI.ThreadDirection

 

I would like to hear of any progress you make.

0 Likes
Message 3 of 43

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK,

 

Try the following code to check alignment between holes.

 

Sub Main()

    Dim oFace1 As Face
    Dim oFace2 As Face

    Set oFace1 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Select first hole")
    Set oFace2 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Select second hole")

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument
    
    Dim oAxis1 As WorkAxis
    Dim oAxis2 As WorkAxis
    
    If oDoc.DocumentType = kPartDocumentObject Then
                
        Dim partCompdef As PartComponentDefinition
        Set partCompdef = oDoc.ComponentDefinition
    
        Set oAxis1 = partCompdef.WorkAxes.AddFixed(oFace1.Geometry.BasePoint, oFace1.Geometry.AxisVector)
        Set oAxis2 = partCompdef.WorkAxes.AddFixed(oFace2.Geometry.BasePoint, oFace2.Geometry.AxisVector)

        Call AlignmentCheck(oAxis1, oAxis2)

    ElseIf oDoc.DocumentType = kAssemblyDocumentObject Then
        
        Dim assyCompdef As AssemblyComponentDefinition
        Set assyCompdef = oDoc.ComponentDefinition

        Set oAxis1 = assyCompdef.WorkAxes.AddFixed(oFace1.Geometry.BasePoint, oFace1.Geometry.AxisVector)
        Set oAxis2 = assyCompdef.WorkAxes.AddFixed(oFace2.Geometry.BasePoint, oFace2.Geometry.AxisVector)

        Call AlignmentCheck(oAxis1, oAxis2)
        
    End If

    Call oAxis1.Delete
    Call oAxis2.Delete

End Sub

Sub AlignmentCheck(oAxis1 As WorkAxis, oAxis2 As WorkAxis)

    If oAxis1.Line.IsColinearTo(oAxis2.Line) Then
        MsgBox ("Holes are aligned")
    Else
        MsgBox ("Holes are misaligned")
    End If
    
End Sub

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 4 of 43

GeorgK
Advisor
Advisor

Hello Chandra,

 

thank you very much for your help.

 

Alignment_of_holes.png

 

Is there any better solution to do this automatically for all holes in a plate and colorize them?

 

Georg

 

0 Likes
Message 5 of 43

GeorgK
Advisor
Advisor

There is a good explanation:

 

http://www.i-logic.com/TruePos/

0 Likes
Message 6 of 43

Anonymous
Not applicable

Surely this would only work in limited circumstances? What if the centre line of the hole is not perpendicular to the XY plane?

0 Likes
Message 7 of 43

GeorgK
Advisor
Advisor

The centre line of the hole is always perpendicular to the XY plane.

0 Likes
Message 8 of 43

Anonymous
Not applicable

Unfortunately I work with more complex assemblies where this isn't the case, do you have any ideas for when this is the situation?

0 Likes
Message 9 of 43

GeorgK
Advisor
Advisor

My assemblies are very complex. But I reduce it to 2 plates and check it.

0 Likes
Message 10 of 43

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK,

 

I found a better solution for current situation. Try the following VBA code to compare hole alignment between bodies.

 

Steps involved in this solution.

 

  1. Selecting 2 bodies or plates
  2. Creating work axis for all holes in both bodies or plates
  3. Comparing each work axis of one body or plate with all work axis of another body or plate. If there are any misalignment, then work axis will be renamed to "misaligned".
  4. For recognizing misalignment, work axes of second body are deleted.

 

Sub Main()

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim oCompdef As AssemblyComponentDefinition
    Set oCompdef = oDoc.ComponentDefinition

    Dim oBody1 As SurfaceBody
    Dim oBody2 As SurfaceBody

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

    Dim oFace As Face
    Dim oAxis As WorkAxis
    Dim oAxisColl1 As ObjectCollection
    Set oAxisColl1 = ThisApplication.TransientObjects.CreateObjectCollection()

    For Each oFace In oBody1.Faces
        If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
            Set oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)
            Call oAxisColl1.Add(oAxis)
        End If
    Next

    Dim oAxisColl2 As ObjectCollection
    Set oAxisColl2 = ThisApplication.TransientObjects.CreateObjectCollection()

    For Each oFace In oBody2.Faces
        If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
            Set oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)
            Call oAxisColl2.Add(oAxis)
        End If
    Next

    Call CompareAxes(oAxisColl1, oAxisColl2)
    Dim i As Integer
    
    For i = 1 To oAxisColl2.Count
        Set oAxis = oAxisColl2.Item(i)
        Call oAxis.Delete
    Next
    
End Sub

Sub CompareAxes(oAxisColl1 As ObjectCollection, oAxisColl2 As ObjectCollection)

    Dim oAxis1 As WorkAxis
    Dim oAxis2 As WorkAxis
    Dim aligned As Boolean
    Dim i As Integer
    Dim j As Integer
    
    For i = 1 To oAxisColl1.Count
        aligned = False
        Set oAxis1 = oAxisColl1.Item(i)
        For j = 1 To oAxisColl2.Count
            Set oAxis2 = oAxisColl2.Item(j)
            If oAxis1.Line.IsColinearTo(oAxis2.Line) Then
                aligned = True
                Exit For
            End If
        Next
        If aligned = False Then
            oAxis1.Name = oAxis1.Name & "_misaligned"
        End If
    Next

End Sub

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 11 of 43

Anonymous
Not applicable

Thank you for the code, however having looked at it I was wondering if the code would find a 'misalignment' between two holes / cylinders that were never supposed to be aligned but happened to be on the same bodies as two aligned ones for example?

 

0 Likes
Message 12 of 43

GeorgK
Advisor
Advisor

Hello Chandra,

 

thats a great and quick solution. Is it possible to put the Axis in a folder like Aligned and Misaligned?

 

But I would like to compare in the next step the hole diameters to see if the pairing fits (plate1: hole 6,6 / plate 2: M6 => OK; plate1: hole 6,6 / plate 2: M8 => nOK)

 

Thank you

 

Georg

0 Likes
Message 13 of 43

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK,

 

Changed the code to separate axes in folders ("Aligned axis" and "Misaligned axis").

 

Sub Main()

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim oCompdef As AssemblyComponentDefinition
    Set oCompdef = oDoc.ComponentDefinition

    Dim oBody1 As SurfaceBody
    Dim oBody2 As SurfaceBody

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

    Dim oFace As Face
    Dim oAxis As WorkAxis
    Dim oAxisColl1 As ObjectCollection
    Set oAxisColl1 = ThisApplication.TransientObjects.CreateObjectCollection()

    For Each oFace In oBody1.Faces
        If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
            Set oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)
            Call oAxisColl1.Add(oAxis)
        End If
    Next

    Dim oAxisColl2 As ObjectCollection
    Set oAxisColl2 = ThisApplication.TransientObjects.CreateObjectCollection()

    For Each oFace In oBody2.Faces
        If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
            Set oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)
            Call oAxisColl2.Add(oAxis)
        End If
    Next

    Call CompareAxes(oAxisColl1, oAxisColl2)
    Dim i As Integer
    
    For i = 1 To oAxisColl2.Count
        Set oAxis = oAxisColl2.Item(i)
        Call oAxis.Delete
    Next
    
    Dim oPane As BrowserPane
    Set oPane = oDoc.BrowserPanes.ActivePane

    Dim alignedNodes As ObjectCollection
    Set alignedNodes = ThisApplication.TransientObjects.CreateObjectCollection
    
    Dim misAlignedNodes As ObjectCollection
    Set misAlignedNodes = ThisApplication.TransientObjects.CreateObjectCollection

    For i = 4 To oCompdef.WorkAxes.Count
        Set oAxis = oCompdef.WorkAxes.Item(i)
        Dim misaligned As Boolean
        misaligned = EndsWith(oAxis.Name, "misaligned")
        
        Dim oNode As BrowserNode
        
        If misaligned = True Then
            Set oNode = oPane.GetBrowserNodeFromObject(oAxis)
            misAlignedNodes.Add oNode
        Else
            Set oNode = oPane.GetBrowserNodeFromObject(oAxis)
            alignedNodes.Add oNode
        End If
    
    Next

    Dim alignedAxes As BrowserFolder
    Set alignedAxes = oPane.AddBrowserFolder("Aligned Axes", alignedNodes)
    
    Dim misAlignedAxes As BrowserFolder
    Set misAlignedAxes = oPane.AddBrowserFolder("MisAligned Axes", misAlignedNodes)
    
End Sub

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

Sub CompareAxes(oAxisColl1 As ObjectCollection, oAxisColl2 As ObjectCollection)

    Dim oAxis1 As WorkAxis
    Dim oAxis2 As WorkAxis
    Dim aligned As Boolean
    Dim i As Integer
    Dim j As Integer
    
    For i = 1 To oAxisColl1.Count
        aligned = False
        Set oAxis1 = oAxisColl1.Item(i)
        For j = 1 To oAxisColl2.Count
            Set oAxis2 = oAxisColl2.Item(j)
            If oAxis1.Line.IsColinearTo(oAxis2.Line) Then
                aligned = True
                Exit For
            End If
        Next
        If aligned = False Then
            oAxis1.Name = oAxis1.Name & "_misaligned"
        End If
    Next

End Sub


Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 14 of 43

GeorgK
Advisor
Advisor

Hello Chandra,

 

that's realy a time saver. How could I to get a Text-File with matching holes?

 

plate 1: oFace.CreatedByFeature.ExtendedName + oFace.CreatedByFeature.HoleDiameter.Value - plate 2: oFace.CreatedByFeature.ExtendedName + oFace.CreatedByFeature.HoleDiameter.Value

 

This should be added in the next Inventor release.

 

Georg

 

PS I will add this function

 

Public Sub CylindricalFaceAxis()

Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument

' Set a reference to component definition of the active document.
' This assumes that a part or assembly document is active.
Dim oCompDef As ComponentDefinition
Set oCompDef = oDoc.ComponentDefinition

Dim oFace As face


Set oFace = ThisApplication.CommandManager.Pick(kPartFaceFilter, "Select face")

If Not oFace.SurfaceType = kCylinderSurface Then
    MsgBox "A cylindrical face must be selected."
    Exit Sub
End If

Dim oCylinder As Cylinder
Set oCylinder = oFace.Geometry

On Error Resume Next
Dim oClientGraphics As ClientGraphics
Set oClientGraphics = oCompDef.ClientGraphicsCollection.Item("HoleAxisAlignmentGraphics")
If Err.Number = 0 Then
On Error GoTo 0
' An existing client graphics object was successfully obtained so clean up.
oClientGraphics.Delete

' update the display to see the results.
ThisApplication.ActiveView.Update
Else
Err.Clear
On Error GoTo 0

' Set a reference to the transient geometry object for user later.
Dim oTransGeom As TransientGeometry
Set oTransGeom = ThisApplication.TransientGeometry

' Create the ClientGraphics object.
Set oClientGraphics = oCompDef.ClientGraphicsCollection.Add("HoleAxisAlignmentGraphics")

' Create a new graphics node within the client graphics objects.
Dim oCurvesNode As GraphicsNode
Set oCurvesNode = oClientGraphics.AddNode(1)
oCurvesNode.Selectable = True

Dim oPoint1 As Point
Set oPoint1 = oCylinder.BasePoint

Dim oPoint2 As Point
Set oPoint2 = oCylinder.BasePoint

Dim oAxisVec As Vector
Set oAxisVec = oCylinder.AxisVector.AsVector

' Scale the vector
oAxisVec.ScaleBy 5

oPoint2.TranslateBy oAxisVec

' Create a transient line segment object
Dim oLineSegment As LineSegment
Set oLineSegment = oTransGeom.CreateLineSegment(oPoint1, oPoint2)

' Create an line graphics object within the node.
Dim oLineGraphics As CurveGraphics
Set oLineGraphics = oCurvesNode.AddCurveGraphics(oLineSegment)

'oLineGraphics.Color = ThisApplication.TransientObjects.CreateColor(255, 0, 0) ' rot
oLineGraphics.Color = ThisApplication.TransientObjects.CreateColor(0, 102, 0) ' grün

' Update the view to see the resulting curve.
ThisApplication.ActiveView.Update
End If
End Sub
0 Likes
Message 15 of 43

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK,

 

Currently, I don't have any solution to match holes.

 

May I know, why the above function is required?

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 16 of 43

GeorgK
Advisor
Advisor

Hello @chandra.shekar.g,

 

we have a lot of plates. The holes for the screw connections are manually added because our customer wants that. Now I have to check if the holes fit together and if the screws are in the right size.

 

Georg

0 Likes
Message 17 of 43

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK,

 

Try the following VBA code to check hole size.

 

Sub Main()

    Dim oDoc As AssemblyDocument
    Set oDoc = ThisApplication.ActiveDocument

    Dim oCompdef As AssemblyComponentDefinition
    Set oCompdef = oDoc.ComponentDefinition

    Dim oFace1 As Face
    Dim oFace2 As Face

    Set oFace1 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Select first hole")
    Set oFace2 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Select second hole")

    Dim oCylinder1 As Cylinder
    Set oCylinder1 = oFace1.Geometry

    Dim oCylinder2 As Cylinder
    Set oCylinder2 = oFace2.Geometry

    Dim aligned As Boolean
    aligned = Check_Alignment(oCylinder1, oCylinder2, oCompdef)

    Dim holeMathced As Boolean
    holeMathced = Compare_Radius(oCylinder1.Radius, oCylinder2.Radius)

    If aligned = True Then
        If holeMathced = True Then
            MsgBox ("Holes are aligned and matched each other")
        Else
            MsgBox ("Holes are aligned but mismatched each other")
        End If
    Else
        If holeMathced = True Then
            MsgBox ("Holes are misaligned but matched each other")
        Else
            MsgBox ("Holes are misaligned and mismatched each other")
        End If
    End If
    
End Sub

Function Check_Alignment(oCylinder1 As Cylinder, oCylinder2 As Cylinder, compDef As AssemblyComponentDefinition) As Boolean

    Dim oAxis1 As WorkAxis
    Dim oAxis2 As WorkAxis
    
    Set oAxis1 = compDef.WorkAxes.AddFixed(oCylinder1.BasePoint, oCylinder1.AxisVector)
    Set oAxis2 = compDef.WorkAxes.AddFixed(oCylinder2.BasePoint, oCylinder2.AxisVector)
    
    If oAxis1.Line.IsColinearTo(oAxis2.Line) Then
        Check_Alignment = True
    Else
        Check_Alignment = False
    End If
    
    Call oAxis1.Delete
    Call oAxis2.Delete

End Function

Function Compare_Radius(oRadius1 As Double, oRadius2 As Double) As Boolean
    If Round(oRadius1, 4) = Round(oRadius2, 4) Then
        Compare_Radius = True
    Else
        Compare_Radius = False
    End If
End Function

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 18 of 43

GeorgK
Advisor
Advisor

Hello Chandra,

 

Thank you very much for your help.

 

To compare the holes is more than to compare the diameter.

 

Some examples:

Plate 1: / Plate 2:

M6 / Ø 6,1 => OK - special type

M6 / Ø 6,2 => OK - special type

M6 / Ø 6,3 => OK - special type

M6 / Ø 6,4 => OK - special type

M6 / Ø 6,5 => OK - special type

M6 / Ø 6,6  => OK - default

M5 / Ø 5,5 => OK - default

Ø 5,5 / Ø 5,5 => OK - default

Ø 4 / Ø 5,5 => nOK

 

With the

 

ExtendedName = (Zylindrische Senkung Ø6,5 mm x 3,4 mm, Ø5,50000000 mm Durch alle Tiefe)

 

I get all the information to compare the holes and see if they match. Therefore I thougt of a hole list for plate 1 /plate2 and the compared holes.

Maybe the best way is to compare a list with two rows for holes that match together. If the hole from plate 1 and plate 2 match the list everthing is OK or not. Maybe you have other ideas?

 

Best regards

 

Georg

 

 

 ' Selecting 2 bodies Or plates
        ' Creating work axis for all holes in both bodies Or plates
        ' Comparing each work axis of one body Or plate with all work axis of another body Or plate. If there are any misalignment, then work axis will be renamed to "misaligned".
        ' For recognizing misalignment, work axes of second body are deleted.

        Dim m_inventorApp As Inventor.Application = Nothing

        ' Try to get an active instance of Inventor
        Try
            m_inventorApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application")

            '###########################

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


        Dim oTrans As Transaction
        oTrans = m_inventorApp.TransactionManager.StartTransaction(m_inventorApp.ActiveDocument, "Bohrungen prüfen")


        Dim oDoc As AssemblyDocument
        oDoc = m_inventorApp.ActiveDocument

        Dim oCompdef As AssemblyComponentDefinition
        oCompdef = oDoc.ComponentDefinition

        Dim oBody1 As SurfaceBody
        Dim oBody2 As SurfaceBody

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

        Dim oFace As Face
        Dim oAxis As WorkAxis
        Dim oAxisColl1 As ObjectCollection
        oAxisColl1 = m_inventorApp.TransientObjects.CreateObjectCollection()

        For Each oFace In oBody1.Faces
            If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
                oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)
                Call oAxisColl1.Add(oAxis)
                Debug.Print(oFace.CreatedByFeature.ExtendedName)
            End If


            Try
                'If oFace.CreatedByFeature.Type = ObjectTypeEnum.kHoleFeatureObject Then
                Dim oHoleFeature As HoleFeature
                oHoleFeature = oFace.CreatedByFeature
                Debug.Print("'############################")
                Debug.Print("Name - erste Wahl = " & oHoleFeature.Name)
                Debug.Print("ExtendedName = " & oHoleFeature.ExtendedName)
                'Debug.Print("ExtentType = " & oHoleFeature.ExtentType)
                Debug.Print("HoleDiameter 1 = " & oHoleFeature.HoleDiameter.Value)
                Debug.Print("'############################")
                'End If
            Catch ex As Exception

            End Try

        Next

        Dim oAxisColl2 As ObjectCollection
        oAxisColl2 = m_inventorApp.TransientObjects.CreateObjectCollection()

        For Each oFace In oBody2.Faces
            If oFace.SurfaceType = SurfaceTypeEnum.kCylinderSurface Then
                oAxis = oCompdef.WorkAxes.AddFixed(oFace.Geometry.BasePoint, oFace.Geometry.AxisVector)
                Call oAxisColl2.Add(oAxis)
                'MsgBox(oFace.CreatedByFeature.ExtendedName)
            End If

            Try
                'If oFace.CreatedByFeature.Type = ObjectTypeEnum.kHoleFeatureObject Then
                Dim oHoleFeature As HoleFeature
                oHoleFeature = oFace.CreatedByFeature
                Debug.Print("'############################")
                Debug.Print("Name - zweite Wahl = " & oHoleFeature.Name)
                Debug.Print("ExtendedName = " & oHoleFeature.ExtendedName)
                'Debug.Print("ExtentType = " & oHoleFeature.ExtentType)
                Debug.Print("HoleDiameter 2 = " & oHoleFeature.HoleDiameter.Value)
                Debug.Print("'############################")
                'End If
            Catch ex As Exception

            End Try
        Next

        Call CompareAxes(oAxisColl1, oAxisColl2)
        Dim i As Integer

        For i = 1 To oAxisColl2.Count
            oAxis = oAxisColl2.Item(i)
            Call oAxis.Delete()
        Next

        Dim oPane As BrowserPane
        oPane = oDoc.BrowserPanes.ActivePane

        Dim alignedNodes As ObjectCollection
        alignedNodes = m_inventorApp.TransientObjects.CreateObjectCollection

        Dim misAlignedNodes As ObjectCollection
        misAlignedNodes = m_inventorApp.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)

        oTrans.End()
    End Sub

    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

    Sub CompareAxes(oAxisColl1 As ObjectCollection, oAxisColl2 As ObjectCollection)

        Dim oAxis1 As WorkAxis
        Dim oAxis2 As WorkAxis
        Dim aligned As Boolean
        Dim i As Integer
        Dim j As Integer

        For i = 1 To oAxisColl1.Count
            aligned = False
            oAxis1 = oAxisColl1.Item(i)
            For j = 1 To oAxisColl2.Count
                oAxis2 = oAxisColl2.Item(j)
                If oAxis1.Line.IsColinearTo(oAxis2.Line) Then
                    aligned = True
                    Exit For
                End If
            Next
            If aligned = False Then
                oAxis1.Name = oAxis1.Name & "_misaligned"
            End If
        Next
End Sub

 

0 Likes
Message 19 of 43

dg2405
Advocate
Advocate

Are in your assembly screws or something else in that holes? I improved my macro for collision detection, so if screws are in it should be no problem to solve your problem.

0 Likes
Message 20 of 43

chandra.shekar.g
Autodesk Support
Autodesk Support

Hi @GeorgK,

 

 

Initially, alignment between holes of plate 1 and plate 2 are compared. If alignment is perfect, then that hole diameter will be compared with hole diameter which is aligned.

 

Results are calculated and printed. Same scenario is implemented in the following VB.net code.

 

    Public Sub 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

Please feel free to contact if there is any doubt.

 

If solves problem, click on "Accept as solution" / give a "Kudo".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network