How to assign part numbers to purchased items in the BOM structure.

How to assign part numbers to purchased items in the BOM structure.

tkddud711
Advocate Advocate
954 Views
9 Replies
Message 1 of 10

How to assign part numbers to purchased items in the BOM structure.

tkddud711
Advocate
Advocate

hello?
In an assembly environment, I would like to give part numbers to all purchased items from full assemblies to sub-assemblies.

I would like to grant them in the following order:

Example) P0001, P0002, P0003.......P0010, P0011, P0013

And the order of sorting before numbering is as follows.

1.First sort by: Vendor
2. Second sort by : Description
3. Third sort by : Stock Number

I want to configure ilogic like this.

Is it possible?

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

JhoelForshav
Mentor
Mentor

Hi @tkddud711 

Try this iLogic rule 🙂

 

Sub Main
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim purchasedItems As New List(Of PurchasedItem)
	Dim refDocs As DocumentsEnumerator = oAsm.AllReferencedDocuments
	For Each oDoc As Document In refDocs
		If oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0
			If oDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure
				Dim dtp As PropertySet = oDoc.PropertySets("Design Tracking Properties")
				purchasedItems.Add(New PurchasedItem(oDoc, dtp("Vendor").Value, dtp("Description").Value, dtp("Stock Number").Value))
			End If
		End If
	Next

	purchasedItems = purchasedItems.OrderBy(Function(item) item.Vendor) _
	.ThenBy(Function(item) item.Description) _
	.ThenBy(Function(item) item.StockNumber) _
	.ToList()

	Dim i As Integer = 1
	For Each pItem As PurchasedItem In purchasedItems
		pItem.Document.PropertySets("Design Tracking Properties")("Part Number").Value = "P" & i.ToString("0000")
		i += 1
	Next

End Sub
Class PurchasedItem
	Public Document As Inventor.Document
	Public Vendor As String
	Public Description As String
	Public StockNumber As String

	Sub New(doc As Inventor.Document, ven As String, desc As String, stock As String)
		Document = doc
		Vendor = ven
		Description = desc
		StockNumber = stock
	End Sub
End Class
Message 3 of 10

bradeneuropeArthur
Mentor
Mentor

can you send an example of files (parts assembly structure)?

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 4 of 10

tkddud711
Advocate
Advocate

First of all thank you for your answer.
I tried running ilogic , pItem.Document.PropertySets("Design Tracking Properties")("Part Number").Value = "P" & i.ToString("0000") on line 21
An error occurred in the code above.
What could be the problem?

0 Likes
Message 5 of 10

JhoelForshav
Mentor
Mentor

Hi @tkddud711 

At first I thought it might be because your Inventor isn't in english, and therefore we couldn't access Design Tracking Properties by displayname. But if that was the case the code would've failed already on line 8. Are all the purchased components checked out of vault? Otherwise that might be a problem trying to change the part number of a locked file...

 

Could you attach a data set of an assembly that gives you an error so I can debug the code using that? 🙂

0 Likes
Message 6 of 10

tkddud711
Advocate
Advocate

Just in case, I removed the read-only purchases of parts in the content center and it works!
If so, is it possible to write code that skips read-only parts?

0 Likes
Message 7 of 10

JhoelForshav
Mentor
Mentor

Hi @tkddud711 

This rule will skip read-only files 🙂

Sub Main
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim purchasedItems As New List(Of PurchasedItem)
	Dim refDocs As DocumentsEnumerator = oAsm.AllReferencedDocuments
	For Each oDoc As Document In refDocs
		If oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0
			If oDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure
				Dim dtp As PropertySet = oDoc.PropertySets("Design Tracking Properties")
				purchasedItems.Add(New PurchasedItem(oDoc, dtp("Vendor").Value, dtp("Description").Value, dtp("Stock Number").Value))
			End If
		End If
	Next

	purchasedItems = purchasedItems.OrderBy(Function(item) item.Vendor) _
	.ThenBy(Function(item) item.Description) _
	.ThenBy(Function(item) item.StockNumber) _
	.ToList()

	Dim i As Integer = 1
	For Each pItem As PurchasedItem In purchasedItems
		Dim ro As Boolean = False
		If pItem.Document.FullDocumentName <> ""
		Dim fi As New System.IO.FileInfo(pItem.Document.FullDocumentName)
		ro =  fi.IsReadOnly
		End If
		If Not ro
			pItem.Document.PropertySets("Design Tracking Properties")("Part Number").Value = "P" & i.ToString("0000")
			i += 1
		End If
	Next

End Sub
Class PurchasedItem
	Public Document As Inventor.Document
	Public Vendor As String
	Public Description As String
	Public StockNumber As String

	Sub New(doc As Inventor.Document, ven As String, desc As String, stock As String)
		Document = doc
		Vendor = ven
		Description = desc
		StockNumber = stock
	End Sub
End Class
0 Likes
Message 8 of 10

tkddud711
Advocate
Advocate

I get the same error on line 27.
I will upload the work file
Please test.

0 Likes
Message 9 of 10

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @tkddud711 

I just tried the code with your dataset and I don't get any error at all... Here's an updated completely language neutral code. Maybe that will help.

 

Please share the error message you get if it doesn't work 🙂

 

Sub Main
	Dim oAsm As AssemblyDocument = ThisDoc.Document
	Dim purchasedItems As New List(Of PurchasedItem)
	Dim refDocs As DocumentsEnumerator = oAsm.AllReferencedDocuments
	For Each oDoc As Document In refDocs
		If oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oDoc).Count > 0
			If oDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kPurchasedBOMStructure
				Dim dtp As PropertySet = oDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}")
				purchasedItems.Add(New PurchasedItem(oDoc, dtp.ItemByPropId(30).Value, dtp.ItemByPropId(29).Value, dtp.ItemByPropId(55).Value))
			End If
		End If
	Next

	purchasedItems = purchasedItems.OrderBy(Function(item) item.Vendor) _
	.ThenBy(Function(item) item.Description) _
	.ThenBy(Function(item) item.StockNumber) _
	.ToList()

	Dim i As Integer = 1
	For Each pItem As PurchasedItem In purchasedItems
		Try
			pItem.Document.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value = "P" & i.ToString("0000")
			i += 1
		Catch ex As Exception
			MsgBox("Couldn't set part number for component: " & pItem.Document.DisplayName & _
			vbCrLf & "Error message:" & vbCrLf & ex.Message)
		End Try
	Next

End Sub
Class PurchasedItem
	Public Document As Inventor.Document
	Public Vendor As String
	Public Description As String
	Public StockNumber As String

	Sub New(doc As Inventor.Document, ven As String, desc As String, stock As String)
		Document = doc
		Vendor = ven
		Description = desc
		StockNumber = stock
	End Sub
End Class
0 Likes
Message 10 of 10

tkddud711
Advocate
Advocate

It works perfectly!

I really want to thank you for your help.

You made my day.