Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

BOM renumber problem

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
manu.marjanen
534 Views, 5 Replies

BOM renumber problem

I change the part "levytyö_1" Item number to 200 by using this code. 

manumarjanen_0-1632744896805.png

Then renumber Item column by using this code.

Why this part "Levytyö_1" does not stay last in the row?

 

manumarjanen_1-1632745025057.png

 

5 REPLIES 5
Message 2 of 6
WCrihfield
in reply to: manu.marjanen

Hi @manu.marjanen.  In your second code, when you are looping through the BOMRows, the order of the looping doesn't go by the ItemNumber property's value.  It's a bit hard to explain, but the natural order of a 'For Each' type of loop (or a For i = 1 To oGroup.Count type of loop) is usually whichever Item was created first, is the first to be processed, and the last Item created will be the last to be processed.  If you want to loop through them based on their ItemNumber property value, you would have to set-up the code for the loop process differently.  I hope this makes sense to you.

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 6

Hi @manu.marjanen 

Have you tried using the renumbering method of the BOMView object?

Given that the structured view is enabled. This single row of code should renumber the BOM for you 🙂

ThisDoc.Document.ComponentDefinition.BOM.BOMViews(2).Renumber(1, 1)
Message 4 of 6
manu.marjanen
in reply to: WCrihfield

Can you show me what you mean, here is the code.

Dim app As Inventor.Application = ThisApplication
Dim doc As Document = app.ActiveDocument
Dim compDef As AssemblyComponentDefinition = doc.ComponentDefinition
Dim Occ As ComponentOccurrence
Dim Count As Integer = 1

For Each itm In compDef.BOM.BOMViews(2).BOMRows
itm.ItemNumber = Count
Count = Count + 1
Next
Message 5 of 6
WCrihfield
in reply to: manu.marjanen

It can be complicated when trying to sort BOM rows in a custom way, then renumber them afterwards.  The simplest way I can think of in this case would be to add each BOMRow to a SortedDictionary, with the ItemNumber as the Key for each row.  A SortedDictionary is a type of variable/object that can hold many items/objects, each paired with a 'Key'.  The 'Key' can then be used to find or sort the objects within the collection.  In this case we want the rows to be sorted by their existing ItemNumber property, then we want to step through them in that order and change the ItemNumber value to a new incremental value.  Here is the code I created for your custom situation.

Dim app As Inventor.Application = ThisApplication
Dim adoc As AssemblyDocument = app.ActiveDocument
Dim compDef As AssemblyComponentDefinition = adoc.ComponentDefinition
'create a variable to hold all BOMRows and their current ItemNumber value
Dim oRows As New SortedDictionary(Of Integer, Inventor.BOMRow)
'add each row to this collection
For Each oBOMRow As BOMRow In compDef.BOM.BOMViews(2).BOMRows
	oRows.Add(CInt(oBOMRow.ItemNumber), oBOMRow)
Next
'they are already automatically sorted by their ItemNumber value now
'now we will change the ItemNumber value to the incremental value, in descending order
Dim Count As Integer = 0
For Each oRow As KeyValuePair(Of Integer, Inventor.BOMRow) In oRows
	Count = Count + 1
	'the oRow.Value is the BOMRow
	oRow.Value.ItemNumber = Count
Next

 This process will only work if the ItemNumber is an Integer (no alphabet characters or special characters).

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 6
manu.marjanen
in reply to: WCrihfield

Thanks for the help, this worked.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report