Sort BOM by category, assign BOM IDs

This commit is contained in:
Daniel Rojas 2021-10-24 20:54:17 +02:00 committed by Laurier Loiselle
parent 58e9a5fcce
commit 0cca1174db
No known key found for this signature in database
GPG Key ID: 345920CC72089A3F
3 changed files with 23 additions and 11 deletions

View File

@ -2,7 +2,7 @@
from collections import namedtuple
from dataclasses import dataclass
from enum import Enum
from enum import Enum, IntEnum
from typing import List, Optional, Union
import tabulate as tabulate_module
@ -18,7 +18,7 @@ BomHashList = namedtuple("BomHashList", BOM_HASH_FIELDS)
PartNumberInfo = namedtuple("PartNumberInfo", "pn manufacturer mpn supplier spn")
BomCategory = Enum(
BomCategory = IntEnum( # to enforce ordering in BOM
"BomEntry", "CONNECTOR CABLE WIRE ADDITIONAL_INSIDE ADDITIONAL_OUTSIDE"
)
QtyMultiplierConnector = Enum(
@ -65,14 +65,14 @@ def print_bom_debug(bom):
# fill rows
for hash, entry in bom.items():
cells = [
0,
entry["id"],
entry["qty"],
hash.qty_unit,
hash.description,
hash.amount.number if hash.amount else None,
hash.amount.unit if hash.amount else None,
", ".join(sorted(entry["designators"])),
entry["category"],
f"{entry['category']} ({entry['category'].name})",
]
rows.append(cells)
# remove empty columns

View File

@ -497,7 +497,7 @@ class WireClass:
else:
_hash = BomHash(
description=self.description,
qty_unit=1,
qty_unit=None,
amount=self.length,
partnumbers=self.partnumbers,
)

View File

@ -8,7 +8,7 @@ from typing import List
from graphviz import Graph
import wireviz.wv_colors
from wireviz.wv_bom import BomEntry, print_bom_debug
from wireviz.wv_bom import BomEntry, print_bom_debug, BomCategory
from wireviz.wv_dataclasses import (
AUTOGENERATED_PREFIX,
AdditionalComponent,
@ -90,6 +90,18 @@ class Harness:
for item in self.additional_bom_items:
self._add_to_internal_bom(item)
# sort BOM by category first, then alphabetically by description within category
self.bom = dict(
sorted(
self.bom.items(),
key=lambda x: (x[1]["category"], x[0].description)
)
)
# assign BOM IDs
for id, key in enumerate(self.bom.keys(), 1):
self.bom[key]["id"] = id
print_bom_debug(self.bom)
def _add_to_internal_bom(self, item: Component):
@ -118,12 +130,12 @@ class Harness:
if isinstance(item, TopLevelGraphicalComponent):
if isinstance(item, Connector):
cat = "connector"
cat = BomCategory.CONNECTOR
elif isinstance(item, Cable):
if item.category == "bundle":
cat = "wire"
cat = BomCategory.WIRE
else:
cat = "cable"
cat = BomCategory.CABLE
else:
cat = ""
@ -153,10 +165,10 @@ class Harness:
hash=comp.bom_hash,
designator=item.designator,
qty=comp.bom_qty,
category=f"{cat}_additional",
category=BomCategory.ADDITIONAL_INSIDE,
)
elif isinstance(item, AdditionalComponent):
cat = "additional"
cat = BomCategory.ADDITIONAL_OUTSIDE
_add(
hash=item.bom_hash,
qty=item.bom_qty,