Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Count component quantity based on part number

gi
Enthusiast

Count component quantity based on part number

gi
Enthusiast
Enthusiast

I have ilogic rule which count quantity of parts based on file name, but sometimes happen that different file has the same part number and should be calculate as the same part. So how can I change my code to calculate parts quantity based on part number. Below is ilogic code which I'm using now.

Dim openDoc As Document
openDoc = ThisDoc.Document
Dim docFile As Document
If openDoc.DocumentType = 12291 Then    
	For Each docFile In openDoc.AllReferencedDocuments
		Dim FNamePos As Long
		Dim docFName As String
		
		FNamePos = InStrRev(docFile.FullFileName, "\", -1)		
		docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)
		
		If docFile.IsModifiable = True Then
			Dim assemblyDoc As AssemblyDocument
			Dim assemblyDef As AssemblyComponentDefinition
			Dim partQty As ComponentOccurrencesEnumerator
			
			assemblyDoc = openDoc			
			assemblyDef = assemblyDoc.ComponentDefinition			
			partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)
			If IsNumeric(partQty.Count) = True Then
			
				If IsNumeric(iProperties.Value(docFName, "Custom", "szt/maszynę")) = False Then
					iProperties.Value(docFName, "Custom", "szt/maszynę") = 0
				End If 			
				
				If partQty.Count <>  iProperties.Value(docFName, "Custom", "szt/maszynę") Then				
					iProperties.Value(docFName, "Custom", "szt/maszynę") = partQty.Count
				End If 					 
			End If 
		End If
	Next
Else
MessageBox.Show("Musisz otworzyć dokument głównego zespołu przed uruchomieniem tego skryptu!", "Niewłaściwy typ pliku!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
End If
0 Likes
Reply
Accepted solutions (1)
1,230 Views
6 Replies
Replies (6)

gi
Enthusiast
Enthusiast

I posted old version of code in previous message. Below is current code.

Dim openDoc As Document
Dim docFile As Document
Dim assemblyDoc As AssemblyDocument
Dim assemblyDef As AssemblyComponentDefinition
Dim partQty As ComponentOccurrencesEnumerator
Dim FNamePos As Long
Dim docFName As String
Dim Projekt As String

ThisApplication.StatusBarText = "Pracuję ... nie martw się teraz!"

openDoc = ThisDoc.Document        
Projekt = iProperties.Value("Project", "Project")
Projekt = InputBox(" Wpisz nazwę projektu / maszyny ", "Obliczanie ilości części w maszynie" , Projekt)
iProperties.Value("Project", "Project") = Projekt
If openDoc.DocumentType = kAssemblyDocumentObject Then  
    For Each docFile In openDoc.AllReferencedDocuments
    FNamePos = InStrRev(docFile.FullFileName, "\", -1)        
        docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)
        If docFile.IsModifiable = True Then
        assemblyDoc = openDoc        
        assemblyDef = assemblyDoc.ComponentDefinition
        partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)
                Try
                    If partQty.Count <>  iProperties.Value(docFName, "Custom", "szt/maszynę") Then                
                        iProperties.Value(docFName, "Custom", "szt/maszynę") = partQty.Count
                    End If                      
                    Catch
                        iProperties.Value(docFName, "Custom", "szt/maszynę") = partQty.Count
                    End Try
                Try
                    If Projekt <>  iProperties.Value(docFName, "Project", "Project") Then                
                        iProperties.Value(docFName, "Project", "Project") = Projekt
                    End If                      
                    Catch
                        iProperties.Value(docFName, "Project", "Project") = Projekt
                End Try
        End If
    Next
    ThisApplication.StatusBarText = "Ready"
Else
MessageBox.Show("Musisz otworzyć dokument głównego zespołu przed uruchomieniem tego skryptu!", "Niewłaściwy typ pliku!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
End If 
0 Likes

JhoelForshav
Mentor
Mentor

Hi @gi 

I don't know exactly what you want. Like where you want to store the count of each part number.

Maybe this code will work for you? It creates a custom property for the count of each part number in the assembly document.

 

Dim openDoc As Document
openDoc = ThisDoc.Document
Dim docFile As Document
If openDoc.DocumentType = 12291 Then    
	For Each docFile In openDoc.AllReferencedDocuments
		
		If docFile.IsModifiable = True Then
			Dim qty As Integer = openDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(docFile).Count		
			Dim oPartnumber As String = docFile.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value
			Dim oProp As Inventor.Property
			Try
				oProp = openDoc.PropertySets.Item("Inventor User Defined Properties").Item(oPartnumber)
				oProp.Value = oProp.Value + qty
			Catch
				oProp = openDoc.PropertySets.Item("Inventor User Defined Properties").Add(qty, oPartnumber)
			End Try
		End If
	Next
Else
MessageBox.Show("Musisz otworzyć dokument głównego zespołu przed uruchomieniem tego skryptu!", "Niewłaściwy typ pliku!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
End If

gi
Enthusiast
Enthusiast

Look at the second post. I published right code which I currently use. I want to store it in custom property called "szt/maszynę". Right now everything works fine. I have custom property "szt/maszynę" in every part from master assembly and in that custom property there is stored quantity of that part in the master assembly. But counting of every part is based on file name. Sometimes there are parts with different file names, but they should be considered as the same part. That's why I want to count the number of parts based on the part number not the file number.

0 Likes

JhoelForshav
Mentor
Mentor
Accepted solution

Ok, so you want to store these values in the actual part documents?

Then we'll have to loop through all the documents, to do a count on each part number. Since the same part number can exist in more than one part document, the count can increase along the loop, meaning we'll have to go back to an already handled document to adjust the property again.

I think the best way would be to just do the counting and store the values in a dictionary in a first loop, then in a second loop write these values to the documents.

Something like this:

Dim openDoc As Document
Dim docFile As Document
Dim assemblyDoc As AssemblyDocument
Dim assemblyDef As AssemblyComponentDefinition
Dim partQty As ComponentOccurrencesEnumerator
Dim FNamePos As Long
Dim docFName As String
Dim Projekt As String

Dim oDict As New Dictionary(Of String, Integer)

ThisApplication.StatusBarText = "Pracuję ... nie martw się teraz!"

openDoc = ThisDoc.Document
Projekt = iProperties.Value("Project", "Project")
Projekt = InputBox(" Wpisz nazwę projektu / maszyny ", "Obliczanie ilości części w maszynie", Projekt)
iProperties.Value("Project", "Project") = Projekt
If openDoc.DocumentType = kAssemblyDocumentObject Then
	For Each docFile In openDoc.AllReferencedDocuments
		FNamePos = InStrRev(docFile.FullFileName, "\", -1)
		docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) -FNamePos)
		If docFile.IsModifiable = True Then
			assemblyDoc = openDoc
			assemblyDef = assemblyDoc.ComponentDefinition
			partQty = assemblyDef.Occurrences.AllReferencedOccurrences(docFile)
			Dim oPartNumber As String = iProperties.Value(docFName, "Project", "Part Number")
			If oDict.ContainsKey(oPartNumber)
				oDict.Item(oPartNumber) = oDict.Item(oPartNumber) + partQty.Count
			Else
				oDict.Add(oPartNumber, partQty.Count)
			End If
			Try
				If Projekt <> iProperties.Value(docFName, "Project", "Project") Then
					iProperties.Value(docFName, "Project", "Project") = Projekt
				End If
			Catch
				iProperties.Value(docFName, "Project", "Project") = Projekt
			End Try
		End If
	Next
	For Each docFile In openDoc.AllReferencedDocuments
		FNamePos = InStrRev(docFile.FullFileName, "\", -1)
		docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) -FNamePos)
		Try
			iProperties.Value(docFName, "Custom", "szt/maszynę") = oDict.Item(iProperties.Value(docFName, "Project", "Part Number"))
		Catch
		End Try
	Next
	ThisApplication.StatusBarText = "Ready"
Else
	MessageBox.Show("Musisz otworzyć dokument głównego zespołu przed uruchomieniem tego skryptu!", "Niewłaściwy typ pliku!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If 

lmc.engineering
Advocate
Advocate

Hi,

 

I made this for my assemblies, it used the BOM to count parts using part number and stores it it a custom iProperty in each part. It also adds the BOM item number for recalling later in drawings. Maybe of use to you?

 

Sub Main()
		oDoc = ThisDoc.ModelDocument
		If oDoc.DocumentType = kPartDocumentObject Then
			MessageBox.Show("This rule can only be run in an assembly file - exiting rule", "iLogic")
		Return
		End If
		
        Dim oAssemblyDocument As AssemblyDocument
        oAssemblyDocument = ThisDoc.Document

        Dim oAssemblyComponentDefinition As AssemblyComponentDefinition
        oAssemblyComponentDefinition = oAssemblyDocument.ComponentDefinition
        If oAssemblyComponentDefinition.RepresentationsManager.ActiveLevelOfDetailRepresentation.LevelOfDetail <> LevelOfDetailEnum.kMasterLevelOfDetail Then Return
        Dim oBOM As BOM
        oBOM = oAssemblyComponentDefinition.BOM
        oBOM.StructuredViewEnabled = True
        Dim oBOMView As BOMView
		oBOMView = oBOM.BOMViews.Item("Structured")

        Call RecursiveCheckAndSetProps(oBOMView.BOMRows)
   End Sub

    Sub RecursiveCheckAndSetProps(ByVal oRowsElements As BOMRowsEnumerator)
		Dim partNumber As String
        For Each oBOMRow As BOMRow In oRowsElements
            Dim oComponentDefinition As ComponentDefinition
            oComponentDefinition = oBOMRow.ComponentDefinitions.Item(1)

            Dim oBOMItemNumber As String
            oBOMItemNumber = oBOMRow.ItemNumber() 'this is item number in the BOM
			
			Dim oBOMQtyNumber As Integer
            oBOMQtyNumber = oBOMRow.ItemQuantity 'this is item number in the BOM
			
			partNumber = oComponentDefinition.Document.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").value
             'MessageBox.Show(oBOMQtyNumber & vbLf & oBOMRow.ReferencedFileDescriptor.FullFileName, "BOM Number") 'just to show what's going on

			Dim oComponentDefinitionPropertySet As PropertySet
            oComponentDefinitionPropertySet = oComponentDefinition.Document.PropertySets.Item("Inventor User Defined Properties")
            'custom property tab
 			Try
                'if already exists then set it 
                oComponentDefinitionPropertySet.Item("PartQty").Value = oBOMQtyNumber
            Catch ex As Exception
                'else add it
                oComponentDefinitionPropertySet.Add(oBOMQtyNumber, "PartQty")
            End Try
			
			Try
                'if already exists then set it 
                oComponentDefinitionPropertySet.Item("BOM Number").Value = oBOMItemNumber
            Catch ex As Exception
                'else add it
                oComponentDefinitionPropertySet.Add(oBOMItemNumber, "BOM Number")
            End Try
            'creates the custom property and inputs the value
			
			If oBOMQtyNumber > 1 Then
				SortDuplicates(partNumber ,oComponentDefinition ,oBOMItemNumber ,oBOMQtyNumber )
			End If

            If Not oBOMRow.ChildRows Is Nothing Then
                Call RecursiveCheckAndSetProps(oBOMRow.ChildRows)
            End If
        Next
    End Sub
	
	Sub SortDuplicates(partNumber As String,oComponentDefinition As ComponentDefinition,oBOMItemNumber As String,oBOMQtyNumber As Integer)
		Dim oAssemblyDocument As AssemblyDocument
        oAssemblyDocument = ThisDoc.Document
		Dim oComponentDefinitionPropertySet As PropertySet
		
		For Each oDoc As Document In oAssemblyDocument.AllReferencedDocuments
			If oDoc.ComponentDefinition.Document.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")("Part Number").value = partNumber Then
				
            oComponentDefinitionPropertySet = oDoc.ComponentDefinition.Document.PropertySets.Item("Inventor User Defined Properties")
            'custom property tab
 			Try
                'if already exists then set it 
                oComponentDefinitionPropertySet.Item("PartQty").Value = oBOMQtyNumber
            Catch ex As Exception
                'else add it
                oComponentDefinitionPropertySet.Add(oBOMQtyNumber, "PartQty")
            End Try
			
			Try
                'if already exists then set it 
                oComponentDefinitionPropertySet.Item("BOM Number").Value = oBOMItemNumber
            Catch ex As Exception
                'else add it
                oComponentDefinitionPropertySet.Add(oBOMItemNumber, "BOM Number")
            End Try
			End If
		Next
	End Sub

 

0 Likes

gi
Enthusiast
Enthusiast

Thank you. That code works great 😀

0 Likes