Wednesday, May 18, 2022

iLogic: Custom Sort PartsList with Temporary Column

 



Issue: 

You have a parts list that has a challenging sort criteria, due to needing to sort for values that might or might not exist in the same column. 

In the example above we want to sort the description column based on all items with a mark number (MK) value, and then sort those without a mark number alphabetically.



Solution:

Although we can't accomplish this out of the box, here are 3 code examples to do this that use iLogic and some API calls.


--------------------------------------------------------------
V1: Version to handle single parts list on the active sheet
--------------------------------------------------------------
sSortColumnName = "KEYWORDS"

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the first parts list on the active sheet.
Dim oPartsList As PartsList
Try
	oPartsList = oDrawDoc.ActiveSheet.PartsLists.Item(1)
Catch
	Return 'exit rule
End Try

Dim oADoc As AssemblyDocument
oADoc = oPartsList.ReferencedDocumentDescriptor.ReferencedDocument
Dim oPropSet As PropertySet
oPropSet = oADoc.PropertySets.Item("Inventor Summary Information")

'add temporary column to the parts list
oID = oPropSet.Item(sSortColumnName).PropId
Try
	oPartsList.PartsListColumns.Add _
	(PropertyTypeEnum.kFileProperty, oPropSet.InternalName, oID)
Catch
End Try

' Iterate through the contents of the parts list.
Dim i As Long
For i = 1 To oPartsList.PartsListRows.Count

	'get the Description value
	oCell = oPartsList.PartsListRows.Item(i).Item("DESCRIPTION")

	'split the string at the comma
	'expecting a string like: 
	'		Bracket, MK B-114
	sArray = Split(oCell.Value, ",")
	sType = sArray(0)

	Try
		sMK = sArray(1)
	Catch 'error when no comma in string
		sMK = ""
	End Try


	'get the temp column cell
	oTempColumnCell = oPartsList.PartsListRows.Item(i).Item(sSortColumnName)

	'write to temp column
	If sMK.Contains("MK") Then
		'strip off the MK
		sMK = Replace(sMK, "MK ", "")
		oTempColumnCell.Value = sMK
	Else
		oTempColumnCell.Value = sType
	End If
Next

'sort and renumber
oPartsList.Sort(sSortColumnName)
oPartsList.Renumber
'remove temp column
oPartsList.PartsListColumns(sSortColumnName).remove

--------------------------------------------------------------
V2: Version for multiple sheets
--------------------------------------------------------------
sSortColumnName = "KEYWORDS"

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oCurrentSheet As Sheet
oCurrentSheet = oDrawDoc.ActiveSheet

Dim oSheet As Sheet
For Each oSheet In oDrawDoc.Sheets
	oSheet.Activate

	' Set a reference to the first parts list on the active sheet.
	Dim oPartsList As PartsList
	Try
		oPartsList = oDrawDoc.ActiveSheet.PartsLists.Item(1)
	Catch
		Return 'exit rule
	End Try

	Dim oADoc As AssemblyDocument
	oADoc = oPartsList.ReferencedDocumentDescriptor.ReferencedDocument
	Dim oPropSet As PropertySet
	oPropSet = oADoc.PropertySets.Item("Inventor Summary Information")

	'add temporary column to the parts list
	oID = oPropSet.Item(sSortColumnName).PropId
	Try
		oPartsList.PartsListColumns.Add _
		(PropertyTypeEnum.kFileProperty, oPropSet.InternalName, oID)
	Catch
	End Try

	' Iterate through the contents of the parts list.
	Dim i As Long
	For i = 1 To oPartsList.PartsListRows.Count

		'get the Description value
		oCell = oPartsList.PartsListRows.Item(i).Item("DESCRIPTION")

		'split the string at the comma
		'expecting a string like: 
		'		Bracket, MK B-114
		sArray = Split(oCell.Value, ",")
		sType = sArray(0)

		Try
			sMK = sArray(1)
		Catch 'error when no comma in string
			sMK = ""
		End Try


		'get the temp column cell
		oTempColumnCell = oPartsList.PartsListRows.Item(i).Item(sSortColumnName)

		'write to temp column
		If sMK.Contains("MK") Then
			'strip off the MK
			sMK = Replace(sMK, "MK ", "")
			oTempColumnCell.Value = sMK
		Else
			oTempColumnCell.Value = sType
		End If
	Next

	'sort and renumber
	oPartsList.Sort(sSortColumnName)
	oPartsList.Renumber
	'remove temp column
	oPartsList.PartsListColumns(sSortColumnName).Remove
Next
oCurrentSheet.activate
InventorVb.DocumentUpdate()



--------------------------------------------------------------
V3: Version for multiple parts list on the active sheet
--------------------------------------------------------------
sSortColumnName = "KEYWORDS"

' Set a reference to the drawing document.
' This assumes a drawing document is active.
Dim oDrawDoc As DrawingDocument
oDrawDoc = ThisApplication.ActiveDocument

Dim oPartsList As PartsList = ThisApplication.CommandManager.Pick _
(SelectionFilterEnum.kDrawingPartsListFilter, "Select a Parts List to sort.")

If oPartsList Is Nothing Then Return 'exit rule

Dim oADoc As AssemblyDocument
oADoc = oPartsList.ReferencedDocumentDescriptor.ReferencedDocument
Dim oPropSet As PropertySet
oPropSet = oADoc.PropertySets.Item("Inventor Summary Information")

'add temporary column to the parts list
oID = oPropSet.Item(sSortColumnName).PropId
Try
	oPartsList.PartsListColumns.Add _
	(PropertyTypeEnum.kFileProperty, oPropSet.InternalName, oID)
Catch
End Try

' Iterate through the contents of the parts list.
Dim i As Long
For i = 1 To oPartsList.PartsListRows.Count

	'get the Description value
	oCell = oPartsList.PartsListRows.Item(i).Item("DESCRIPTION")

	'split the string at the comma
	'expecting a string like: 
	'		Bracket, MK B-114
	sArray = Split(oCell.Value, ",")
	sType = sArray(0)

	Try
		sMK = sArray(1)
	Catch 'error when no comma in string
		sMK = ""
	End Try


	'get the temp column cell
	oTempColumnCell = oPartsList.PartsListRows.Item(i).Item(sSortColumnName)

	'write to temp column
	If sMK.Contains("MK") Then
		'strip off the MK
		sMK = Replace(sMK, "MK ", "")
		oTempColumnCell.Value = sMK
	Else
		oTempColumnCell.Value = sType
	End If
Next

'sort and renumber
oPartsList.Sort(sSortColumnName)
oPartsList.Renumber
'remove temp column
oPartsList.PartsListColumns(sSortColumnName).Remove

InventorVb.DocumentUpdate()