code works until i add more then Two parts to assembly

code works until i add more then Two parts to assembly

Anonymous
Not applicable
442 Views
5 Replies
Message 1 of 6

code works until i add more then Two parts to assembly

Anonymous
Not applicable

can someone assist me to figure out why my hole checker keeps throwing an error? does not work if I use create command on an assembly more than once sometimes, fails if I exit and go back into an assembly and try to run the code it instantly fails.

  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
                        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 = 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
            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

 

 

Error in rule: Chech Hole Alignment, in document: Hole Checker.iam

Object reference not set to an instance of an object.

(More Info Tab)

System.NullReferenceException: Object reference not set to an instance of an object.
at LmiRuleScript.Main()
at Autodesk.iLogic.Exec.AppDomExec.ExecRuleInAssembly(Assembly assem)
at iLogic.RuleEvalContainer.ExecRuleEval(String execRule)

 

0 Likes
443 Views
5 Replies
Replies (5)
Message 2 of 6

JaneFan
Autodesk
Autodesk

The code is working pretty well on my computer when I tried it with an assembly including two components constrained by hole axis aligned. From the error you mentioned, it seems some object is missed being found. Try to add some Logger.Debug between the suspicious lines if you are using inventor newer than 2018, or add Msgbox("") if inventor version is older or the same as 2018 to output some message to help find the missing object.




Jane Fan
Inventor/Fusion QA Engineer
0 Likes
Message 3 of 6

Anonymous
Not applicable

Hello, 

I have tried adding msgbox and the code failed at line 13 which is pretty much the start. I currently dont understand why it is not checking the complete part 

0 Likes
Message 4 of 6

DRoam
Mentor
Mentor

I don't get the the object reference error when I run your code with 2 or more parts. I do get it if I cancel selecting one of the parts, though. But I assume you're not doing that.

 

Which line do you mean by #13? I don't know if you're counting Sub Main() and/or the empty lines.

0 Likes
Message 5 of 6

Anonymous
Not applicable

I was just counting lines, I also receive this error if I use create command more than once on an assembly.

0 Likes
Message 6 of 6

JaneFan
Autodesk
Autodesk

Do you mean it fails at the following line? 

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

It sounds like something special with your selections. What kind of components did you select with the Pick() method? It would be helpful to see the issue if you could provide a simple model.




Jane Fan
Inventor/Fusion QA Engineer
0 Likes