more sort help.

more sort help.

Anonymous
Not applicable
700 Views
9 Replies
Message 1 of 10

more sort help.

Anonymous
Not applicable

top level assm. would be 12345000

new parts would be 12345001 to 12345009

new sub assm. would be 12345010 

reused parts would be 11111001 to 11111009

reused assm. would be 11111010

i need to get a total number of new parts with total weight from the model. 

0 Likes
Accepted solutions (1)
701 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable

still having some trouble with this one. any help would be greatly appreciated. 

0 Likes
Message 3 of 10

Anonymous
Not applicable

no longer need weight just a count of new parts. here's what i have so far. it seems to miss all the part numbers 

 

 

Sub Main() 
    ' Get the active assembly. 
    Dim oAsmDoc As AssemblyDocument 
    oAsmDoc = ThisApplication.ActiveDocument
	
	' Get the assembly component definition. 
    Dim oAsmDef As AssemblyComponentDefinition 
    oAsmDef = oAsmDoc.ComponentDefinition.iProperties.Value("Project", "Part Number")
	
	oNum = oAsmDef.iProperties.Value("Project", "Part Number",Left(12345000,5))

    ' 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 
	oOcc = oLeafOccs(iProperties.Value("Project", "Part Number",Left(12345000,5)))
    For Each oOcc In oLeafOccs 
		If oLeafOccs = oNum Then
		
			cnt = cnt + 1
			MessageBox.Show(oOcc.Name) 
		End If
			 
    Next 
	MessageBox.Show(cnt)
End Sub

.  

0 Likes
Message 4 of 10

HideoYamada
Advisor
Advisor

Hello,

 

Try this code (VBA).

The code counts the Parts which part number is started with "12345".

 

Option Explicit

'
' This Code is written in VBA.
'
Sub Main()
    Dim topAssyDoc As AssemblyDocument
    Set topAssyDoc = ThisApplication.ActiveEditDocument
    Dim count As Integer
    count = CountNewParts(topAssyDoc.ComponentDefinition.Occurrences)
    MsgBox "Count : " & count
End Sub

Function CountNewParts(occs As ComponentOccurrences) As Integer
    CountNewParts = 0
    Dim occ As ComponentOccurrence
    For Each occ In occs
        If IsNewPart(occ) Then
            CountNewParts = CountNewParts + 1
        ElseIf TypeOf occ.Definition Is AssemblyComponentDefinition Then
            CountNewParts = CountNewParts + CountNewParts(occ.Definition.Occurrences)
        End If
    Next occ
End Function

Function IsNewPart(occ As ComponentOccurrence) As Boolean
    IsNewPart = False
    If Not TypeOf occ.Definition Is PartComponentDefinition Then
        Exit Function
    End If
    Dim oPartDoc As PartDocument
    Set oPartDoc = occ.Definition.Document
    Dim partNumber As String
    partNumber = oPartDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").Value
    If Left(partNumber, 5) = "12345" Then
        IsNewPart = True
    End If
End Function

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 5 of 10

Anonymous
Not applicable

that does count everything. is there a way to automatically extract the first 5 digits of the top level assembly for the search string? also i only need to count .ipt files that start with the first 5 digits of the top level assembly. this counts subassemblies as parts too. thanks for helping me with this.

0 Likes
Message 6 of 10

Anonymous
Not applicable

also i only need number of new part numbers created not number of new part occurrences. thanks for your help

0 Likes
Message 7 of 10

HideoYamada
Advisor
Advisor

Hi,

 


@Anonymous wrote:

that does count everything. is there a way to automatically extract the first 5 digits of the top level assembly for the search string? also i only need to count .ipt files that start with the first 5 digits of the top level assembly. this counts subassemblies as parts too. thanks for helping me with this.


Delete or comment out these two lines :

'        ElseIf TypeOf occ.Definition Is AssemblyComponentDefinition Then
'            CountNewParts = CountNewParts + CountNewParts(occ.Definition.Occurrences)

=====

Freeradical

 Hideo Yamada

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 8 of 10

HideoYamada
Advisor
Advisor

@Anonymous wrote:

also i only need number of new part numbers created not number of new part occurrences. thanks for your help


Ah, this needs a bit complex code. Just wait...

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 9 of 10

HideoYamada
Advisor
Advisor
Accepted solution

Hello Keith,

 

I translated the VBA code into iLogic because I want to use .Net Collection Library.

 

Option Explicit On
Sub Main()
    Dim topAssyDoc As AssemblyDocument = ThisApplication.ActiveEditDocument
    Dim newPartDocuments As New System.Collections.Generic.List(Of PartDocument)
    CountNewParts(topAssyDoc.ComponentDefinition.Occurrences, newPartDocuments)
    MessageBox.Show("Count : " + newPartDocuments.Count.ToString())
    newPartDocuments.Clear()
End Sub

Sub CountNewParts(occs As ComponentOccurrences, newPartDocuments As System.Collections.Generic.List(Of PartDocument)) 
    Dim occ As ComponentOccurrence
    For Each occ In occs
        If IsNewPart(occ) Then
            Dim oPartDoc As PartDocument = occ.Definition.Document
            If Not newPartDocuments.Contains(oPartDoc) Then
                newPartDocuments.Add(oPartDoc)
            End If
'        ElseIf TypeOf occ.Definition Is AssemblyComponentDefinition Then
'            CountNewParts(occ.Definition.Occurrences, newPartDocuments)
        End If
    Next occ
End Sub

Function IsNewPart(occ As ComponentOccurrence) As Boolean
    IsNewPart = False
    If Not TypeOf occ.Definition Is PartComponentDefinition Then
        Exit Function
    End If
    Dim oPartDoc As PartDocument = occ.Definition.Document
    Dim partNumber As String
    partNumber = oPartDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").Value
    If Left(partNumber, 5) = "12345" Then
        IsNewPart = True
    End If
End Function

This code reports the number of new part documents (not occurrences of new part documents).

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 10 of 10

Anonymous
Not applicable

i made a little mod but your code works great. thanks for your help

 

 

 

Sub Main()

    Dim topAssyDoc As AssemblyDocument = ThisApplication.ActiveEditDocument

    Dim newPartDocuments As New System.Collections.Generic.List(Of PartDocument)

        Dim sSN As String

        sSN = InputBox("Please enter first five digits of parts you are interested in:", "Input needed", "xxxxx")

    CountNewParts(topAssyDoc.ComponentDefinition.Occurrences, newPartDocuments, sSN)

    MessageBox.Show("Count : " + newPartDocuments.Count.ToString())

    newPartDocuments.Clear()

End Sub

 

Sub CountNewParts(occs As ComponentOccurrences, newPartDocuments As System.Collections.Generic.List(Of PartDocument), sSN As String)

    Dim occ As ComponentOccurrence

    For Each occ In occs

        If IsNewPart(occ, sSN) Then

            Dim oPartDoc As PartDocument = occ.Definition.Document

            If Not newPartDocuments.Contains(oPartDoc) Then

                newPartDocuments.Add(oPartDoc)

           End If

        ElseIf TypeOf occ.Definition Is AssemblyComponentDefinition Then

           CountNewParts(occ.Definition.Occurrences, newPartDocuments,sSN)

        End If

    Next occ

End Sub

0 Likes