Ilogic - Sort / renumber only first level of the BOM

Ilogic - Sort / renumber only first level of the BOM

j.wolbers-NoTech
Enthusiast Enthusiast
1,244 Views
10 Replies
Message 1 of 11

Ilogic - Sort / renumber only first level of the BOM

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi,

 

I have an Ilogic code that sorts the BOM according to a certain order and then renumbers it. Furthermore, this code also ensures that the parts list in a drawing is sorted by position number.

 

With a large composition, saving now takes quite a long time because all levels of the BOM are sorted and renumbered. However, it is only important to me that the First level BOM is sorted and renumbered.

The BOM setting must remain on all levels!

 

Is this possible?

 

Sub main ()

 

Dim doc As Document = ThisDoc.Document
Dim partslist As PartsList
Dim oPartsList As partslist
Dim aDoc As AssemblyDocument
Dim pDoc As PartDocument
Dim typeDoc As String, typeint As String

 

typeint = doc.DocumentType

 

If typeint = "12291" Then typeDoc = "Assembly"
If typeint = "12292" Then typeDoc = "Drawing"
If typeint = "12290" Then typeDoc = "Part"
    
    

 

If typeDoc = "Part" Then Exit Sub
    
If typeDoc = "Drawing" Then
    Dim dDoc As DrawingDocument = ThisDoc.Document
    On Error GoTo Errorhandler
    If dDoc.ActiveSheet.PartsLists.Item(1).PartsListRows.Count = 1 Then Exit Sub
End If

 


If (doc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject) Then
    Dim dDoc As DrawingDocument = ThisDoc.Document
    partslist = dDoc.ActiveSheet.PartsLists.Item(1)
Else
    aDoc = doc
End If

 


if (doc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject)
Dim BOM As BOM = aDoc.ComponentDefinition.BOM
BOM.StructuredViewEnabled = True

 

Dim oBOMView As BOMView = BOM.BOMViews("Structured")

 

oBOMView.Sort("BOM Structure", True, "Vendor", False, "Authority", True )
oBOMView.Renumber(1, 1)
end if

 

If (doc.DocumentType = DocumentTypeEnum.kDrawingDocumentObject) Then
    partslist.Sort("POS")
End If

 

Exit Sub
Errorhandler :
MsgBox("please add partlist first", vbOKOnly)

 

End Sub

 

Kind regards,

Jeffrey

0 Likes
Accepted solutions (1)
1,245 Views
10 Replies
Replies (10)
Message 2 of 11

WCrihfield
Mentor
Mentor

This isn't likely to be a "Solution", but I basically reformatted your code a bit to hopefully make it a little easier to read and follow.  And added in some checks to help avoid potential errors.

Sub main()
	Dim oDocType As DocumentTypeEnum = ThisApplication.ActiveDocumentType
	If oDocType = DocumentTypeEnum.kPartDocumentObject Then
		Exit Sub
	ElseIf oDocType = DocumentTypeEnum.kDrawingDocumentObject Then
		Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
		Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
		If oSheet.PartsLists.Count = 0 Then
			Exit Sub
		Else
			Dim oPList As PartsList = oSheet.PartsLists.Item(1)
			If oPList.PartsListRows.Count = 1 Then
				Exit Sub
			Else
				Try
					oPList.Sort("POS")
				Catch
					MsgBox("Something went wrong while attempting to 'Sort' the Drawing's PartsList.", vbOKOnly, " ")
				End Try
			End If
		End If
	ElseIf oDocType = DocumentTypeEnum.kAssemblyDocumentObject Then
		Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
		Dim BOM As BOM = oADoc.ComponentDefinition.BOM
		BOM.StructuredViewEnabled = True
		Dim oBOMView As BOMView = BOM.BOMViews("Structured")
		Try
			oBOMView.Sort("BOM Structure", True, "Vendor", False, "Authority", True)
		Catch
			MsgBox("Something went wrong while attempting to 'Sort' the Assembly's BOM.", vbOKOnly, " ")
		End Try
		oBOMView.Renumber(1, 1)
	End If
End Sub

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

If you have time, please... Vote For My IDEAS 💡and Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 11

WCrihfield
Mentor
Mentor
Accepted solution

I just had another thought on the subject.  You said "The BOM setting must remain on all levels", but what if we turned the "StructuredViewFirstLevelOnly" setting of the BOM to True, just before we do the Sort method on it, then turn that setting back off right afterwords?  Do you think that may work for you?  I modified my earlier code to include this functionality.  I haven't tried it yet, due to how custom it is, but you can try it to see if it works for you.

Here's the updated code:

Sub main()
	Dim oDocType As DocumentTypeEnum = ThisApplication.ActiveDocumentType
	If oDocType = DocumentTypeEnum.kPartDocumentObject Then
		Exit Sub
	ElseIf oDocType = DocumentTypeEnum.kDrawingDocumentObject Then
		Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
		Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
		If oSheet.PartsLists.Count = 0 Then
			Exit Sub
		Else
			Dim oPList As PartsList = oSheet.PartsLists.Item(1)
			If oPList.PartsListRows.Count = 1 Then
				Exit Sub
			Else
				Try
					oPList.Sort("POS")
				Catch
					MsgBox("Something went wrong while attempting to 'Sort' the Drawing's PartsList.", vbOKOnly, " ")
				End Try
			End If
		End If
	ElseIf oDocType = DocumentTypeEnum.kAssemblyDocumentObject Then
		Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
		Dim BOM As BOM = oADoc.ComponentDefinition.BOM
		BOM.StructuredViewEnabled = True
		BOM.StructuredViewFirstLevelOnly = True '<<<< 1st change >>>>
		'May need to update the document after this setting to make sure it takes effect, before proceeding
		oADoc.Update
		Dim oBOMView As BOMView = BOM.BOMViews("Structured")
		Try
			oBOMView.Sort("BOM Structure", True, "Vendor", False, "Authority", True)
		Catch
			MsgBox("Something went wrong while attempting to 'Sort' the Assembly's BOM.", vbOKOnly, " ")
		End Try
		oBOMView.Renumber(1, 1)
		'Set it back the way it was now that we're done
		BOM.StructuredViewFirstLevelOnly = False '<<<< 2nd change >>>>
	End If
End Sub

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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 11

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @WCrihfield 

 

I had not received an email with the news that there was a new response to this topic.

Hence my late response...

 

This could indeed be a solution.
I hope to be able to test the code today or tomorrow.
You'll hear from me as soon as I know more.

 

Regards,

Jeffrey

0 Likes
Message 5 of 11

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @WCrihfield 

 

The restructured code works.

Thanks a lot

 

Regards,

Jeffrey

0 Likes
Message 6 of 11

nisse
Explorer
Explorer

Hi @WCrihfield 

 

I have a small problem with this ilogic code.
When I save the assembly the BOM is sorted. If I then save the 2D drawing, the parts list is sorted by pos. number.

 

However when I save the assembly in the 2D drawing (not in the assembly itself!) the BOM is not sorted again.

 

Maybe you know how I can make this happen?

 

 

Kinds regards,

Jeffrey

0 Likes
Message 7 of 11

j.wolbers-NoTech
Enthusiast
Enthusiast

@WCrihfield 

 

I accidentally posted the above comment with a colleague's account.

Hence, you will see another name!

 

Regards,

Jeffrey

0 Likes
Message 8 of 11

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi,

 

No one who can help me with this?

 

Regards,

Jeffrey

0 Likes
Message 9 of 11

WCrihfield
Mentor
Mentor

I guess I don't really understand what you said in your post a week ago when you posted from the "nisse" account.  Can you explain the problem differently?

It sounds like:  When the assembly is the active document and you run the rule, it works OK, right?  And when the drawing is the active document and you run the rule, it works OK, right?  But then, for some reason, the assembly is being saved indirectly, while you are working with the drawing, and that saving event, somehow, is causing the assembly's BOM to become unsorted again?  It sounds like they are changing each other back and forth.  I'm not sure this is something that can be fixed, when both are sorted in different ways, and they are both connected, to a certain degree.  Unless I'm still just not understanding the problem correctly.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 11

j.wolbers-NoTech
Enthusiast
Enthusiast

Hi @WCrihfield 

 

I'll try to explain it differently.

 

The Ilogic rule runs automatically in both assemblies and drawings (before save).

 

When I save an assembly the BOM is renumbered.
If I then open the drawing and save it, the parts list is placed in order of position number.

So this works well.

 

When I make a drawing from an assembly that has not yet been saved, it does not work properly.

When saving the drawing, Inventor will also save the assembly.

But in this case, the BOM is not re-sorted.

 

So sorting the BOM will not work if the assembly is saved in the ''background''.

 

Kinds regards,

Jeffrey

0 Likes
Message 11 of 11

WCrihfield
Mentor
Mentor

Let's try something a little different for this situation and see if that works better.

Lets try two different iLogic rules instead of one.  One designed to be ran when the assembly is the active document, and the other designed for when the drawing is the active document.  Each version still contains some code to interact with the other document, but as a secondary process at the end.  Both can be external rules.  You can put the one within the iLogic Event Trigger of the assembly document, and the other can be under the Event Trigger of the drawing document.  Then, no matter which document you are working with (active), it will first attempt to sort (active) document first, then sort the (other) document last, and save each while specifying to not save dependent documents.  Hopefully including this save functionality in the code won't cause an endless loop of triggering the "before save" event, and running the rules.  😉  If it does, you may have to remove it.  But if we are making changes to the (other) document, it seems like we will need to save it before closing it, or we will loose it.

Here's the code for the Drawing document:

Sub Main()
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
		MsgBox("A Drawing Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oDDoc As DrawingDocument = ThisDrawing.Document
	Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
	Dim oPList As PartsList
	If oSheet.PartsLists.Count = 0 Then
		Exit Sub
	Else
		oPList = oSheet.PartsLists.Item(1)
		If oPList.PartsListRows.Count = 1 Then
			Exit Sub
		Else
			Try
				oPList.Sort("POS")
			Catch
				MsgBox("Something went wrong while attempting to 'Sort' the Drawing's PartsList.", vbOKOnly, " ")
			End Try
		End If
	End If
	
	'iLogicVb.RunExternalRule("Sort Assembly BOM")
	'or
	Sort_Assembly_BOM()
	oDDoc.Save2(False) 'false means don't save dependants
End Sub

Sub Sort_Assembly_BOM()
	Dim oADoc As AssemblyDocument
	If ThisDrawing.ModelDocument Is Nothing Then
		Exit Sub
	Else
		If ThisDrawing.ModelDocument.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
			Exit Sub
		Else
			oADoc = ThisDrawing.ModelDocument
		End If
	End If

	Dim BOM As BOM = oADoc.ComponentDefinition.BOM
	BOM.StructuredViewEnabled = True
	BOM.StructuredViewFirstLevelOnly = True
	oADoc.Update
	Dim oBOMView As BOMView = BOM.BOMViews("Structured")
	Try
		oBOMView.Sort("BOM Structure", True, "Vendor", False, "Authority", True)
	Catch
		MsgBox("Something went wrong while attempting to 'Sort' the Assembly's BOM.", vbOKOnly, " ")
	End Try
	oBOMView.Renumber(1, 1)
	BOM.StructuredViewFirstLevelOnly = False
	oADoc.Save2(False) 'false means don't save dependants
End Sub

 And here is the code for the assembly document:

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule (" & iLogicVb.RuleName & ") to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim BOM As BOM = oADoc.ComponentDefinition.BOM
	BOM.StructuredViewEnabled = True
	BOM.StructuredViewFirstLevelOnly = True
	oADoc.Update
	Dim oBOMView As BOMView = BOM.BOMViews("Structured")
	Try
		oBOMView.Sort("BOM Structure", True, "Vendor", False, "Authority", True)
	Catch
		MsgBox("Something went wrong while attempting to 'Sort' the Assembly's BOM.", vbOKOnly, " ")
	End Try
	oBOMView.Renumber(1, 1)
	BOM.StructuredViewFirstLevelOnly = False
	
	Sort_Drg_PList(oADoc)
	oADoc.Save2(False) 'false means don't save dependants
End Sub

Sub Sort_Drg_PList(oAsmDoc As AssemblyDocument)
	'get the assembly's file path
	Dim oPath As String = IO.Path.GetDirectoryName(oAsmDoc.FullFileName)
	'get assembly's file name only (without extension)
	Dim oName As String = IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName)
	Dim oDrgFile As String = oPath & "\" & oName & ".idw"
	Dim oDDoc As DrawingDocument
	'check if the drawing exists
	If IO.File.Exists(oDrgFile) Then
		'open the drawing
		oDDoc = ThisApplication.Documents.Open(oDrgFile, False)
	Else
		Exit Sub
	End If
	Dim oSheet As Inventor.Sheet
	Dim oPList As PartsList
	For Each oSheet In oDDoc.Sheets
		If oSheet.PartsLists.Count > 0 Then
			oPList = oSheet.PartsLists.Item(1)
			If oPList.PartsListRows.Count = 1 Then
				Continue For
			Else
				Try
					oPList.Sort("POS")
				Catch
					MsgBox("Something went wrong while attempting to 'Sort' the Drawing's PartsList.", vbOKOnly, " ")
				End Try
			End If
		End If
	Next
	oDDoc.Save2(False) 'false means don't save dependants
End Sub

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

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)