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

(Not an Autodesk Employee)