Skip to content

Commit d72816e

Browse files
committed
Improve sorting
Sort by reference prefix instead of first char + footprint + inverse of count + natural order of references.
1 parent dc04dec commit d72816e

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

InteractiveHtmlBom/generate_interactive_bom.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/python2
2-
from shutil import copy
32
from datetime import datetime
43
import pcbnew
54
import wx
@@ -12,25 +11,25 @@
1211
import units
1312

1413

15-
def natural_sort(l):
14+
def generate_bom(pcb, filter_layer=None):
1615
"""
17-
Natural sort for strings containing numbers
16+
Generate BOM from pcb layout.
17+
:param filter_layer: include only parts for given layer
18+
:return: BOM table (qty, value, footprint, refs)
1819
"""
19-
2020
def convert(text): return int(text) if text.isdigit() else text.lower()
2121

2222
def alphanum_key(key): return [convert(c)
2323
for c in re.split('([0-9]+)', key)]
2424

25-
return sorted(l, key=alphanum_key)
25+
def natural_sort(l):
26+
"""
27+
Natural sort for strings containing numbers
28+
"""
29+
30+
return sorted(l, key=alphanum_key)
2631

2732

28-
def generate_bom(pcb, filter_layer=None):
29-
"""
30-
Generate BOM from pcb layout.
31-
:param filter_layer: include only parts for given layer
32-
:return: BOM table (qty, value, footprint, refs)
33-
"""
3433
attr_dict = {0: 'Normal',
3534
1: 'Normal+Insert',
3635
2: 'Virtual'
@@ -68,9 +67,10 @@ def generate_bom(pcb, filter_layer=None):
6867
len(valrefs[1]), valrefs[0], footprint, natural_sort(valrefs[1]))
6968
bom_table.append(line)
7069

71-
# sort table by reference prefix and quantity
70+
# sort table by reference prefix, footprint and quantity
7271
def sort_func(row):
73-
qty, _, _, rf = row
72+
qty, _, fp, rf = row
73+
prefix = re.findall('^[A-Z]+', rf[0])[0]
7474
ref_ord = {
7575
"C": 1,
7676
"R": 2,
@@ -82,10 +82,11 @@ def sort_func(row):
8282
"X": 8,
8383
"F": 9,
8484
"S": 10,
85-
"J": 1001,
86-
"P": 1002
87-
}.get(rf[0][0], 1000)
88-
return ref_ord, -qty
85+
"CNN": 1997,
86+
"J": 1998,
87+
"P": 1999
88+
}.get(prefix, 1000)
89+
return ref_ord, fp, -qty, alphanum_key(rf[0])
8990

9091
bom_table = sorted(bom_table, key=sort_func)
9192

0 commit comments

Comments
 (0)