Why is Counting Holes so Slow?

Why is Counting Holes so Slow?

etckerry
Enthusiast Enthusiast
686 Views
2 Replies
Message 1 of 3

Why is Counting Holes so Slow?

etckerry
Enthusiast
Enthusiast

Hello,

 

I have written a routine to count the holes in a part.  It generates accurate counts, but it is incredibly slow - I can't figure out why.  Also, if I don't have the part checked out, it asks me if I want to check it out - I also cannot figure this out, since I am not modifying the document in any way.

 

It seems that the slow down and the "dirtying" occur at about the same place - on the "Continue For" lines in ProcessPattern.  Any ideas on either my speed issue or the dirtying issue?

 

I removed some code to clean up what needs to be shown here - consider the ProcessPart() method as the main function.

 

Public Class RivetCounter

    Private counts As New Hashtable

    Private Sub AddCount(ByVal diameter As Double, ByVal number As Integer)

        If counts.ContainsKey(diameter) Then
            counts(diameter) = CType(counts(diameter), Integer) + number
        Else
            counts(diameter) = number
        End If

    End Sub

    Private Sub ProcessPart(ByRef part As Inventor.PartDocument, ByVal quantity As Integer)

        Dim hole As Inventor.HoleFeature
        For Each hole In part.ComponentDefinition.Features.HoleFeatures
            ProcessHoleFeature(hole, quantity)
        Next hole

        Dim rectPat As Inventor.RectangularPatternFeature
        For Each rectPat In part.ComponentDefinition.Features.RectangularPatternFeatures
            ProcessPattern(rectPat.ParentFeatures, quantity * rectPat.PatternElements.Count, rectPat.ConsumeInputs)
        Next rectPat

        Dim circPat As Inventor.CircularPatternFeature
        For Each circPat In part.ComponentDefinition.Features.CircularPatternFeatures
            ProcessPattern(circPat.ParentFeatures, quantity * circPat.PatternElements.Count, circPat.ConsumeInputs)
        Next circPat

    End Sub

    Private Sub ProcessHoleFeature(ByRef hole As Inventor.HoleFeature, ByVal quantity As Integer)

        ' Apparently Inventor uses cm as internal units
        AddCount(hole.HoleDiameter.ModelValue / 2.54, quantity * hole.HoleCenterPoints.Count)

    End Sub

    Private Sub ProcessPattern(ByRef features As Inventor.ObjectCollection, ByVal quantity As Integer, ByVal consumeInputs As Boolean)

        Dim i As Integer
        For i = 1 To features.Count
            ' Try to process the feature as each of the types we are interested in
            ' On failures, continue trying the next type of feature
            ' On success, continue with the next feature object

            ' HoleFeature
            Try
                Dim hole As Inventor.HoleFeature = CType(features(i), Inventor.HoleFeature)
                If consumeInputs Then
                    ProcessHoleFeature(hole, quantity)
                Else
                    ProcessHoleFeature(hole, quantity - 1)
                End If
                Continue For
            Catch ex As Exception
                ' Not a hole
            End Try

            ' RectangularPatternFeature
            Try
                Dim rectPat As Inventor.RectangularPatternFeature = CType(features(i), Inventor.RectangularPatternFeature)
                If consumeInputs Then
                    ProcessPattern(rectPat.ParentFeatures, quantity * rectPat.PatternElements.Count, rectPat.ConsumeInputs)
                Else
                    ProcessPattern(rectPat.ParentFeatures, quantity * (rectPat.PatternElements.Count - 1), rectPat.ConsumeInputs)
                End If
                Continue For
            Catch ex As Exception
                ' Not a rectangular pattern
            End Try

            ' CircularPatternFeature
            Try
                Dim circPat As Inventor.CircularPatternFeature = CType(features(i), Inventor.CircularPatternFeature)
                If consumeInputs Then
                    ProcessPattern(circPat.ParentFeatures, quantity * circPat.PatternElements.Count, circPat.ConsumeInputs)
                Else
                    ProcessPattern(circPat.ParentFeatures, quantity * (circPat.PatternElements.Count - 1), circPat.ConsumeInputs)
                End If
                Continue For
            Catch ex As Exception
                ' Not a circular pattern
            End Try
        Next i

    End Sub

End Class

 

 

Thanks for your help!

 

-Kerry

0 Likes
Accepted solutions (1)
687 Views
2 Replies
Replies (2)
Message 2 of 3

ekinsb
Alumni
Alumni
Accepted solution

Wow, this was a tricky one to find.  Change your declaration of

 

Private Sub ProcessPattern(ByRef features As Inventor.ObjectCollection, ByVal quantity As Integer, ByVal consumeInputs As Boolean)

 

to

 

Private Sub ProcessPattern(ByVal features As Inventor.ObjectCollection, ByVal quantity As Integer, ByVal consumeInputs As Boolean)

 

It was reassigning the set of parent features for the pattern every time the sub returned.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 3 of 3

j.romo
Advocate
Advocate

Can you share the whole code?Smiley Happy

0 Likes