How to get the Total Occurrences in Active Document with API?

How to get the Total Occurrences in Active Document with API?

comic3344
Enthusiast Enthusiast
1,865 Views
3 Replies
Message 1 of 4

How to get the Total Occurrences in Active Document with API?

comic3344
Enthusiast
Enthusiast
Dear all,
 
At the right bottom corner of the Inventor window, there is  number of  the Total Occurrences in Active Document .
 
How to get this number with API ? 
 
I tried to use 
 
MsgBox(ToOcc.Definition.Occurrences.AllLeafOccurrences.Count.ToString & vbLf &
ToOcc.Definition.Occurrences.AllReferencedOccurrences(asmDoc).Count.ToString)
 
but both of  the results are wrong, the first is lower than the right number and the second is 0.
 
I guess the AllLeafOccurrences is the set for all parts, not assemblies.
 
The recursion way is work, but it lasts for a long time.
 
Please help me. Thank you! 
 
 
0 Likes
1,866 Views
3 Replies
Replies (3)
Message 2 of 4

HermJan.Otterman
Advisor
Advisor

recursive is the way to go..

 

Private Sub TraverseMain()
        Dim oDoc As Inventor.Document = _inventorApplication.ActiveDocument
        If oDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
            MsgBox("You need to open an Assembly (.iam)")
            Exit Sub 'quit
        End If

        Dim MainAssy As Inventor.AssemblyDocument = _inventorApplication.ActiveDocument
        TraverseTroughAssembly(MainAssy.ComponentDefinition.Occurrences)

        MsgBox(_componentCount)
    End Sub

    Private _componentCount As Integer = 0

    Private Sub TraverseTroughAssembly(ByVal oOccurrences As Inventor.ComponentOccurrences)

        For Each oOcc As ComponentOccurrence In oOccurrences

            If oOcc.DefinitionDocumentType = DocumentTypeEnum.kPartDocumentObject Then
                _componentCount += 1

            ElseIf oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

                On Error Resume Next
                Dim BOMqty As BOMQuantity = oOcc.Definition.BOMQuantity
                If Not BOMqty Is Nothing Then

                    _componentCount += 1

                    TraverseTroughAssembly(oOcc.SubOccurrences)
                End If

            End If
        Next

    End Sub

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


0 Likes
Message 3 of 4

comic3344
Enthusiast
Enthusiast

I know the recursion way is work, but it lasts for a long time.

 

There are thousands of parts and assemblies in my model.    -_-

 

Actually, the result at the right bottom corner means Inventor has calculated the number. 

 

Thank you all the same. 

Message 4 of 4

ra.prasanth02
Explorer
Explorer

Document activeDoc = InApp.ActiveDocument;
AssemblyDocument assdoc = activeDoc as AssemblyDocument;

ComponentOccurrences occs = assdoc.ComponentDefinition.Occurrences;

int Count = occs.AllReferencedOccurrences[assdoc .ComponentDefinition].Count;

 

This will give you the value that shows on inventor right bottom(Total occurrences in active document).