Number of Each Reference Document -- Part Quantity

Number of Each Reference Document -- Part Quantity

dalton98
Collaborator Collaborator
328 Views
1 Reply
Message 1 of 2

Number of Each Reference Document -- Part Quantity

dalton98
Collaborator
Collaborator

Hello. I'm trying to write a code that gives me the number of each specific part in my assembly.

The obvious answer is to look at the BOM, but I want it to use the reference documents like the rest of my code.

 

 

This is kind of what I want, but it gives the total number of parts in the assembly.

    ' Get the active assembly. 
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisDrawing.ModelDocument

    ' Get the assembly component definition. 
    Dim oAsmDef As AssemblyComponentDefinition 
    oAsmDef = oAsmDoc.ComponentDefinition 

    ' Get all of the leaf occurrences of the assembly. 
    Dim oLeafOccs As ComponentOccurrencesEnumerator 
    oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences 

    ' Iterate through the occurrences and print the name. 
	Dim cnt As Integer 
	cnt = 0
	
    Dim oOcc As ComponentOccurrence 
    For Each oOcc In oLeafOccs 
        If oOcc.BOMStructure = BOMStructureEnum.kNormalBOMStructure Then
			cnt = cnt + 1
			MessageBox.Show(oOcc.Name) 
		End If
			 
    Next 
	MessageBox.Show(cnt)

 

0 Likes
Accepted solutions (1)
329 Views
1 Reply
Reply (1)
Message 2 of 2

vpeuvion
Advocate
Advocate
Accepted solution

Hi,

You can try this.

    ' Get the active assembly. 
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisDrawing.ModelDocument

    ' Get the assembly component definition. 
    Dim oAsmDef As AssemblyComponentDefinition 
    oAsmDef = oAsmDoc.ComponentDefinition 

    ' Get all of the leaf occurrences of the assembly. 
    Dim oLeafOccs As ComponentOccurrencesEnumerator 
    oLeafOccs = oAsmDef.Occurrences.AllLeafOccurrences 
	
    Dim oOcc As ComponentOccurrence 
	Dim oOccList As New List(Of String)
	Dim oQtyList As New List(Of Integer)
    For Each oOcc In oLeafOccs 
        If oOcc.BOMStructure = BOMStructureEnum.kNormalBOMStructure Then
			If Not oOccList.Contains(oOcc.Name.Split(":")(0)) Then
				oOccList.Add(oOcc.Name.Split(":")(0))
				oQtyList.Add(1)
			Else
				Dim LastQty As Integer = oQtyList.Item(oOccList.IndexOf(oOcc.Name.Split(":")(0)))
				oQtyList.Item(oOccList.IndexOf(oOcc.Name.Split(":")(0))) = LastQty + 1
		 	End If
		End If	 
    Next 
	Dim Results As String =  Nothing
	For i = 0 To oOccList.Count-1
		Results = Results & oOccList.Item(i) & " : " & oQtyList.Item(i) & Chr(13)
	Next
	
	MessageBox.Show(Results)

 Hope this can help you.

Vincent.

0 Likes