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: 

Auto-Sort Bill of Material

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Anonymous
1101 Views, 6 Replies

Auto-Sort Bill of Material

Anonymous
Not applicable

Hi Everyone,

 

I'm trying to create a rule that will do the following;

  1. Sort by Item # in Bill of Material
  2. Renumber Item # in Bill of Material
  3. Sort by Item # in Parts List (but not override the BoM Item #)
  4. Auto Update in Parts List

I created the code below but, I am running into an "object not defined" error.  I'm not confident this is the only error that will come from this though.  If anyone could take a look and let me know where I am going wrong, I would appreciate it.

 

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

'BOM RENUMBER

' Set a reference to the BOM
Dim oBOM As BOM
	oBOM.StructuredViewFirstLevelOnly = False
	oBOM.StructuredViewEnabled = True
	Dim oStructuredBOMView As BOMView
    oStructuredBOMView = oBOM.BOMViews.Item("Structured")
Call oStructuredBOMView.Renumber(1, 1)

'PART LIST SORT

oPartsList1 = oDrawDoc.ActiveSheet.PartsLists.Item(1)
oPartsList1.Sort2("ITEM", 1, "DESCRIPTION", 1, "SIZE", 1, AutoSortOnUpdate, True)

Thanks

Auto-Sort Bill of Material

Hi Everyone,

 

I'm trying to create a rule that will do the following;

  1. Sort by Item # in Bill of Material
  2. Renumber Item # in Bill of Material
  3. Sort by Item # in Parts List (but not override the BoM Item #)
  4. Auto Update in Parts List

I created the code below but, I am running into an "object not defined" error.  I'm not confident this is the only error that will come from this though.  If anyone could take a look and let me know where I am going wrong, I would appreciate it.

 

Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

'BOM RENUMBER

' Set a reference to the BOM
Dim oBOM As BOM
	oBOM.StructuredViewFirstLevelOnly = False
	oBOM.StructuredViewEnabled = True
	Dim oStructuredBOMView As BOMView
    oStructuredBOMView = oBOM.BOMViews.Item("Structured")
Call oStructuredBOMView.Renumber(1, 1)

'PART LIST SORT

oPartsList1 = oDrawDoc.ActiveSheet.PartsLists.Item(1)
oPartsList1.Sort2("ITEM", 1, "DESCRIPTION", 1, "SIZE", 1, AutoSortOnUpdate, True)

Thanks

6 REPLIES 6
Message 2 of 7
Sergio.D.Suárez
in reply to: Anonymous

Sergio.D.Suárez
Mentor
Mentor

 

Hi, this is a code that used at a time to add materials. I hope I can serve you. regards!

 

Private Sub Main()
	Dim tip As Document = ThisDoc.Document
	If tip.DocumentType = Inventor.DocumentTypeEnum.kDrawingDocumentObject  Then	
	    Delete_BOM
		Add_BOM
	Else
		MessageBox.Show("Open Drawing Document", "Insert Bill of materials")
	End If
End Sub

Sub Delete_BOM
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
For Each oPartList As PartsList In oSheet.PartsLists
	On Error Resume Next 
	oPartList.Delete
Next
End Sub

Sub Add_BOM
On Error Resume Next
	Dim oDrawingDoc As DrawingDocument
	oDrawingDoc = ThisApplication.ActiveDocument
	Dim oSheet As Sheet
	oSheet = oDrawingDoc.ActiveSheet
	Dim oDrawingView As DrawingView
	oDrawingView = oSheet.DrawingViews(1)
	Dim oBorder As Border
	oBorder = oSheet.Border
	Dim oPlacementPoint As Point2d
	xrev = oBorder.RangeBox.MaxPoint.X
	yrev = oBorder.RangeBox.MaxPoint.Y
	oPlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(xrev, yrev)
	Dim oPartsList1 As PartsList
	oPartsList1 = oSheet.PartsLists.Add(oDrawingView, oPlacementPoint, 46594)
'kFirstLevelComponents 46593 First level components. 
'kPartsOnly 46594 Parts only. 
'kStructured 46593 Structured first level components. 
'kStructuredAllLevels 46595 Structured all components. 

	oPartsLis1t = oDrawingDoc.ActiveSheet.PartsLists.Item(1)
	Dim myparam As String
	myparam = InputBox("Enter the name of the column to sort", "Sort Bill of materials", "DESCRIPTION")

	oPartsList1.Sort(myparam)
	oPartsList1.Renumber
	oPartsList1.Style.UpdateFromGlobal
	
	'oPartsList1.Style = oDrawingDoc.StylesManager.PartsListStyles.Item("Select Style BOM")
	
	'Relocate in lower position
	Dim newposition As Point2d
	newposition = oPartsList1.Position
	newposition.Y=oBorder.RangeBox.MinPoint.Y + 2.2 + (oPartsList1.RangeBox.MaxPoint.Y - oPartsList1.RangeBox.MinPoint.Y)
	oPartsList1.Position = newposition
	InventorVb.DocumentUpdate()
End Sub

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes

 

Hi, this is a code that used at a time to add materials. I hope I can serve you. regards!

 

Private Sub Main()
	Dim tip As Document = ThisDoc.Document
	If tip.DocumentType = Inventor.DocumentTypeEnum.kDrawingDocumentObject  Then	
	    Delete_BOM
		Add_BOM
	Else
		MessageBox.Show("Open Drawing Document", "Insert Bill of materials")
	End If
End Sub

Sub Delete_BOM
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument
Dim oSheet As Sheet
oSheet = oDrawDoc.ActiveSheet
For Each oPartList As PartsList In oSheet.PartsLists
	On Error Resume Next 
	oPartList.Delete
Next
End Sub

Sub Add_BOM
On Error Resume Next
	Dim oDrawingDoc As DrawingDocument
	oDrawingDoc = ThisApplication.ActiveDocument
	Dim oSheet As Sheet
	oSheet = oDrawingDoc.ActiveSheet
	Dim oDrawingView As DrawingView
	oDrawingView = oSheet.DrawingViews(1)
	Dim oBorder As Border
	oBorder = oSheet.Border
	Dim oPlacementPoint As Point2d
	xrev = oBorder.RangeBox.MaxPoint.X
	yrev = oBorder.RangeBox.MaxPoint.Y
	oPlacementPoint = ThisApplication.TransientGeometry.CreatePoint2d(xrev, yrev)
	Dim oPartsList1 As PartsList
	oPartsList1 = oSheet.PartsLists.Add(oDrawingView, oPlacementPoint, 46594)
'kFirstLevelComponents 46593 First level components. 
'kPartsOnly 46594 Parts only. 
'kStructured 46593 Structured first level components. 
'kStructuredAllLevels 46595 Structured all components. 

	oPartsLis1t = oDrawingDoc.ActiveSheet.PartsLists.Item(1)
	Dim myparam As String
	myparam = InputBox("Enter the name of the column to sort", "Sort Bill of materials", "DESCRIPTION")

	oPartsList1.Sort(myparam)
	oPartsList1.Renumber
	oPartsList1.Style.UpdateFromGlobal
	
	'oPartsList1.Style = oDrawingDoc.StylesManager.PartsListStyles.Item("Select Style BOM")
	
	'Relocate in lower position
	Dim newposition As Point2d
	newposition = oPartsList1.Position
	newposition.Y=oBorder.RangeBox.MinPoint.Y + 2.2 + (oPartsList1.RangeBox.MaxPoint.Y - oPartsList1.RangeBox.MinPoint.Y)
	oPartsList1.Position = newposition
	InventorVb.DocumentUpdate()
End Sub

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

Message 3 of 7
rhasell
in reply to: Anonymous

rhasell
Advisor
Advisor
Accepted solution

Hi

 

You have mixed the BOM and Parts list in one rule.

These are are in separate environments, that one of the reasons why it is generating an error.

 

The BOM is in the Assembly environment, and the parts list in the drawing environment.

 

Here is some working code for you.

You just need to add/adjust the sort criteria.

 

'Dim oDrawDoc As DrawingDocument
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument

'BOM RENUMBER

' Set a reference to the BOM
Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM
	oBOM.StructuredViewFirstLevelOnly = False
	oBOM.StructuredViewEnabled = True
	Dim oStructuredBOMView As BOMView
    oStructuredBOMView = oBOM.BOMViews.Item("Structured")
	'Call oStructuredBOMView.Sort("BOM Structure",True ,"TYPE",True ,"Stock Number",True)
	Call oStructuredBOMView.Sort("Description", True)
Call oStructuredBOMView.Renumber(1, 1)

However, I have been down this road before, and thanks to a lot of help from Curtis, I have found that sorting the Parts list (In the drawing environment) is way better. It is more predictable and consistent.

 

The reason for doing so was due to the fact that if you try an sort the BOM with more than one criteria, it never worked properly.

 

Reg
2025.1.2
Please Accept as a solution / Kudos
0 Likes

Hi

 

You have mixed the BOM and Parts list in one rule.

These are are in separate environments, that one of the reasons why it is generating an error.

 

The BOM is in the Assembly environment, and the parts list in the drawing environment.

 

Here is some working code for you.

You just need to add/adjust the sort criteria.

 

'Dim oDrawDoc As DrawingDocument
Dim oDoc As AssemblyDocument
oDoc = ThisApplication.ActiveDocument

'BOM RENUMBER

' Set a reference to the BOM
Dim oBOM As BOM
oBOM = oDoc.ComponentDefinition.BOM
	oBOM.StructuredViewFirstLevelOnly = False
	oBOM.StructuredViewEnabled = True
	Dim oStructuredBOMView As BOMView
    oStructuredBOMView = oBOM.BOMViews.Item("Structured")
	'Call oStructuredBOMView.Sort("BOM Structure",True ,"TYPE",True ,"Stock Number",True)
	Call oStructuredBOMView.Sort("Description", True)
Call oStructuredBOMView.Renumber(1, 1)

However, I have been down this road before, and thanks to a lot of help from Curtis, I have found that sorting the Parts list (In the drawing environment) is way better. It is more predictable and consistent.

 

The reason for doing so was due to the fact that if you try an sort the BOM with more than one criteria, it never worked properly.

 

Reg
2025.1.2
Please Accept as a solution / Kudos
Message 4 of 7
rhasell
in reply to: rhasell

rhasell
Advisor
Advisor

Hi

 

Here is a snippet of my code used to sort the Parts List

Once again, you need to set the Criteria, and it is important that column labels exist in the physical Parts List.

 

On Error Resume Next
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oPartsList1 As PartsList
oPartsList1 = oDrawDoc.ActiveSheet.PartsLists.Item(1)
oPartsList1.Sort("COST CENTER",1,"TYPE", 1, "STOCK NUMBER", 1) ' Sort
oPartsList1.Renumber
oPartsList1.SaveItemOverridesToBOM 'Save Back the the assembly
Reg
2025.1.2
Please Accept as a solution / Kudos

Hi

 

Here is a snippet of my code used to sort the Parts List

Once again, you need to set the Criteria, and it is important that column labels exist in the physical Parts List.

 

On Error Resume Next
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oPartsList1 As PartsList
oPartsList1 = oDrawDoc.ActiveSheet.PartsLists.Item(1)
oPartsList1.Sort("COST CENTER",1,"TYPE", 1, "STOCK NUMBER", 1) ' Sort
oPartsList1.Renumber
oPartsList1.SaveItemOverridesToBOM 'Save Back the the assembly
Reg
2025.1.2
Please Accept as a solution / Kudos
Message 5 of 7
Anonymous
in reply to: rhasell

Anonymous
Not applicable

Thank you for your help.  When I ran this rule I was given the following error.

 

Error in rule: Rule0, in document: TWSWS-201-L.idw

Unable to cast COM object of type 'Inventor._DocumentClass' to interface type 'Inventor.AssemblyDocument'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{29F0D465-C114-11D2-B77F-0060B0F159EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

Unfortunately, my hands are tied when it comes to using the BOM instead of the parts list.

0 Likes

Thank you for your help.  When I ran this rule I was given the following error.

 

Error in rule: Rule0, in document: TWSWS-201-L.idw

Unable to cast COM object of type 'Inventor._DocumentClass' to interface type 'Inventor.AssemblyDocument'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{29F0D465-C114-11D2-B77F-0060B0F159EF}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

 

Unfortunately, my hands are tied when it comes to using the BOM instead of the parts list.

Message 6 of 7
rhasell
in reply to: Anonymous

rhasell
Advisor
Advisor
Accepted solution

Hi

 

The reason for the failure is because you ran the code from within a drawing. The code is meant to be run in the Assembly environment. If you run it from an assembly it will work. (Also the rule currently only works with a Structured BOM, is that what you wanted?)

 

If you run the second snippet of code that I posted from the drawing environment ("TWSWS-201-L.idw") that will work. If you do not want to write the changes back to to the assembly, then just comment out the last line line. (This rule will run on both Structured and Parts only)

Reg
2025.1.2
Please Accept as a solution / Kudos
0 Likes

Hi

 

The reason for the failure is because you ran the code from within a drawing. The code is meant to be run in the Assembly environment. If you run it from an assembly it will work. (Also the rule currently only works with a Structured BOM, is that what you wanted?)

 

If you run the second snippet of code that I posted from the drawing environment ("TWSWS-201-L.idw") that will work. If you do not want to write the changes back to to the assembly, then just comment out the last line line. (This rule will run on both Structured and Parts only)

Reg
2025.1.2
Please Accept as a solution / Kudos
Message 7 of 7
Anonymous
in reply to: rhasell

Anonymous
Not applicable

Thank you for your help!  This is exactly what I needed.

Thank you for your help!  This is exactly what I needed.

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

Post to forums  

Autodesk Design & Make Report