Check if partlist is sortet

Check if partlist is sortet

Darkforce_the_ilogic_guy
Advisor Advisor
392 Views
3 Replies
Message 1 of 4

Check if partlist is sortet

Darkforce_the_ilogic_guy
Advisor
Advisor

I have a ilogic that sort a Part liste by Part number like this.. Is there anyway to check if the partlist is already sortet ?

 

Darkforce_the_ilogic_guy_0-1685094857355.png

 

0 Likes
393 Views
3 Replies
Replies (3)
Message 2 of 4

JhoelForshav
Mentor
Mentor

Hi @Darkforce_the_ilogic_guy 

You didn't mention if you sort by string or numeric value.

SortByString.png

Here's an example if you sort by string:

Dim oSheet As Sheet = ThisDrawing.ActiveSheet.Sheet
Dim oPartsList As PartsList = oSheet.PartsLists(1)
Dim Sorted As Boolean = True

For i = 1 To oPartsList.PartsListRows.Count - 1
    Dim currentPN As String = oPartsList.PartsListRows(i).Item("PART NUMBER").Value.ToString
    Dim nextPN As String = oPartsList.PartsListRows(i + 1).Item("PART NUMBER").Value.ToString
    
    If String.Compare(currentPN, nextPN, StringComparison.OrdinalIgnoreCase) > 0 Then
        Sorted = False
        Exit For
    End If
Next

If Sorted Then
    MsgBox("The parts list is sorted.")
Else
    MsgBox("The parts list is not sorted.")
End If

 

0 Likes
Message 3 of 4

Darkforce_the_ilogic_guy
Advisor
Advisor

I use this code to Sort it

 

 

Try
Dim SheetsNumber
SheetsNumber = ThisApplication.ActiveDocument.Sheets.Count


For i = 1 To SheetsNumber 
	
Dim oPartslistCheck As PartsList
oPartslistCheck = ThisApplication.ActiveDocument.Sheets(i).PartsLists(1)

Dim oDoc As DrawingDocument
oDoc = ThisApplication.ActiveDocument

Call oDoc.SelectSet.Select(oPartslistCheck)

InventorVb.RunMacro("ApplicationProject", "PartsListSort", "FormatSelectedPartsList")
Next
Catch

End Try

...VBA Code

Option Explicit
Option Base 1


Public Function GetActiveDrawing() As DrawingDocument

If ThisApplication.ActiveDocument.DocumentType = kDrawingDocumentObject Then
Set GetActiveDrawing = ThisApplication.ActiveDocument
Else
MsgBox "Must have a drawing active", vbOKOnly, "Error"
End If

End Function



Public Function GetSelectedPartsList() As PartsList

Dim oDrawDoc As DrawingDocument
Set oDrawDoc = GetActiveDrawing
If oDrawDoc Is Nothing Then Exit Function

Dim oSelectSet As SelectSet


Set oSelectSet = oDrawDoc.SelectSet

If oSelectSet.Count = 0 Then Exit Function
If oSelectSet.Item(1).Type <> kPartsListObject Then Exit Function
Set GetSelectedPartsList = oSelectSet.Item(1)



End Function



Public Function FormatPartsList(oPartsList As PartsList) As PartsList

On Error Resume Next

oPartsList.Sort "PART NUMBER", True
oPartsList.Renumber
oPartsList.SaveItemOverridesToBOM


If Err Then
Err.Clear
MsgBox "Could not sort Parts List", , "Error"
End If

Set FormatPartsList = oPartsList

End Function



Public Sub FormatSelectedPartsList()

Dim oPartsList As PartsList
Set oPartsList = GetSelectedPartsList
If oPartsList Is Nothing Then Exit Sub

FormatPartsList oPartsList

End Sub

 

0 Likes
Message 4 of 4

JhoelForshav
Mentor
Mentor

Hi @Darkforce_the_ilogic_guy 

When I use your macro to sort a partslist, then try the code I provided in my previous reply, it works fine for me. Does it not work for you?

If it doesn't work, could you please provide a dataset (assembly and drawing) where it doesn't work so I can investigate further? 🙂

0 Likes