Been a bit busy today, but squeezed in some time for this. I assumed your drawing's parts list is only focused on stuff found in the first level of the Structured BOM view, because I think trying to go deeper with this specific process would likely be more trouble than its worth. I tried collecting a few different object types (BrowserNode, ComponentDefinition, etc.) while looping the browser pane, each with its drawbacks, until going back to the basics and just using a String. What I came up with is working in my test files so far (assembly document & drawing document for that assembly). The code is started while the drawing is active, then gets the model reference from the first view on the active sheet. Then either finds or creates a parts list. Then I call my custom function to search through the model browser of the model, and return a List of FullFileName's. In the function, each component it comes across, it attempts to extract the full file name of the document it is representing. And if it encounters an OccurrencePattern, it just works with the 'source/input' components, because all the rest are just quantity/copies, not originals. Then back at the main Sub area, we use the 'Index' of each name returned to reposition each PartsListRow. As you can see, it is a pain in the butt digging down into a PartsListRow to get a full file name to compare to, but it works. In retrospect, the assembly BOM angle does look like less trouble.
Here's the code I came up with:
Sub Main
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
MsgBox("A Drawing Document must be active for this rule to work. Exiting.", vbCritical, "iLogic")
Exit Sub
End If
Dim oDDoc As DrawingDocument = ThisApplication.ActiveDocument
oTG = ThisApplication.TransientGeometry
oSheet = oDDoc.ActiveSheet
oView = oSheet.DrawingViews.Item(1)
Dim oADoc As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument
Dim oPList As PartsList
If oSheet.PartsLists.Count = 0 Then
oPos1 = oTG.CreatePoint2d(0, 0) '<<< WE CAN CHANGE THIS LATER >>>
oLevel = PartsListLevelEnum.kStructured 'fist level only Structured view
oSheet.PartsLists.Add(oADoc, oPos1, oLevel)
Else
oPList = oSheet.PartsLists.Item(1) 'or Pick, or find by Title
End If
'now get order needed
Dim oFileNames As List(Of String) = GetFileNamesInOrder(oADoc)
' MsgBox("oDocs.Count = " & oFileNames.Count, , "")
' For Each oFileName In oFileNames
' MsgBox("oFileName = " & oFileName,,"")
' Next
'now reposition rows to match needed order
For Each oRow As PartsListRow In oPList.PartsListRows
Try
Dim oDBOMRow As DrawingBOMRow = oRow.ReferencedRows.Item(1)
oCD = oDBOMRow.BOMRow.ComponentDefinitions.Item(1)
Dim oRowDoc As Document = oCD.Document
'check what 'Index' this one is in our list
oIndex = oFileNames.IndexOf(oRowDoc.FullFileName)
oRow.Reposition(oIndex + 1, True) 'specify next index, then use insert before, to position at that index
Catch oEx As Exception
MsgBox("Reposition method failed." & vbCrLf & _
oEx.Message & vbCrLf & oEx.StackTrace,,"")
End Try
Next
End Sub
Function GetFileNamesInOrder(oAsmDoc As AssemblyDocument) As List(Of String)
'oAsmDoc.Activate
Dim oList As New List(Of String)
oTopNode = oAsmDoc.BrowserPanes.Item("Model").TopNode
For Each oNode As BrowserNode In oTopNode.BrowserNodes
Try
If TypeOf oNode.NativeObject Is ComponentOccurrence Then
Dim oOcc As ComponentOccurrence = oNode.NativeObject
Dim oDoc As Document = oOcc.ReferencedDocumentDescriptor.ReferencedDocument
If oList.Contains(oDoc.FullFileName) Then
Continue For 'don't dig into any possible sub-components
Else
oList.Add(oDoc.FullFileName) 'add to the end
End If
ElseIf TypeOf oNode.NativeObject Is OccurrencePattern Then
Dim oOccPatt As OccurrencePattern = oNode.NativeObject
'process 'input' items only - others are just extra quantity items, not originals
For Each oObj In oOccPatt.ParentComponents 'ObjectCollection
If Not TypeOf oObj Is ComponentOccurrence Then Continue For
Dim oPOcc As ComponentOccurrence = oObj
Dim oODoc As Document = oPOcc.ReferencedDocumentDescriptor.ReferencedDocument
If oList.Contains(oODoc.FullFileName) Then
Continue For 'don't dig into any possible sub-components
Else
oList.Add(oODoc.FullFileName) 'add to the end
End If
Next
End If
Catch
End Try
Next
Return oList
End Function
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 💡 or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)