diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index 2287e597..2f7cc14e 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -11,8 +11,8 @@ class Connector: name: str manufacturer: Optional[str] = None - manufacturer_part_number: Optional[str] = None - internal_part_number: Optional[str] = None + mpn: Optional[str] = None + pn: Optional[str] = None style: Optional[str] = None category: Optional[str] = None type: Optional[str] = None @@ -80,8 +80,8 @@ def activate_pin(self, pin): class Cable: name: str manufacturer: Optional[Union[str, List[str]]] = None - manufacturer_part_number: Optional[Union[str, List[str]]] = None - internal_part_number: Optional[Union[str, List[str]]] = None + mpn: Optional[Union[str, List[str]]] = None + pn: Optional[Union[str, List[str]]] = None category: Optional[str] = None type: Optional[str] = None gauge: Optional[float] = None @@ -140,7 +140,7 @@ def __post_init__(self): self.wirecount = len(self.colors) # if lists of part numbers are provided check this is a bundle and that it matches the wirecount. - for idfield in [self.manufacturer, self.manufacturer_part_number, self.internal_part_number]: + for idfield in [self.manufacturer, self.mpn, self.pn]: if isinstance(idfield, list): if self.category == "bundle": # check the length diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index a3aa0ce2..0c15b49d 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -7,7 +7,8 @@ from wireviz.wv_colors import get_color_hex from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \ nested_html_table, flatten2d, index_if_list, html_line_breaks, \ - graphviz_line_breaks, remove_line_breaks, open_file_read, open_file_write + graphviz_line_breaks, remove_line_breaks, open_file_read, open_file_write, \ + manufacturer_info_field from collections import Counter from typing import List from pathlib import Path @@ -88,9 +89,8 @@ def create_graph(self) -> Graph: for key, connector in self.connectors.items(): rows = [[connector.name if connector.show_name else None], - [connector.manufacturer, - f'MPN: {connector.manufacturer_part_number}' if connector.manufacturer_part_number else None, - f'IPN: {connector.internal_part_number}' if connector.internal_part_number else None], + [f'P/N: {connector.pn}' if connector.pn else None, + manufacturer_info_field(connector.manufacturer, connector.mpn)], [html_line_breaks(connector.type), html_line_breaks(connector.subtype), f'{connector.pincount}-pin' if connector.show_pincount else None, @@ -151,9 +151,9 @@ def create_graph(self) -> Graph: elif cable.gauge_unit.upper() == 'AWG': awg_fmt = f' ({mm2_equiv(cable.gauge)} mm\u00B2)' - identification = [cable.manufacturer if not isinstance(cable.manufacturer, list) else '', - f'MPN: {cable.manufacturer_part_number}' if (cable.manufacturer_part_number and not isinstance(cable.manufacturer_part_number, list)) else '', - f'IPN: {cable.internal_part_number}' if (cable.internal_part_number and not isinstance(cable.internal_part_number, list)) else ''] + identification = [f'P/N: {cable.pn}' if (cable.pn and not isinstance(cable.pn, list)) else '', + manufacturer_info_field(cable.manufacturer if not isinstance(cable.manufacturer, list) else None, + cable.mpn if not isinstance(cable.mpn, list) else None)] identification = list(filter(None, identification)) attributes = [html_line_breaks(cable.type) if cable.type else '', @@ -210,12 +210,12 @@ def create_graph(self) -> Graph: if(cable.category == 'bundle'): # for bundles individual wires can have part information # create a list of wire parameters wireidentification = [] - if isinstance(cable.manufacturer, list): - wireidentification.append(cable.manufacturer[i - 1]) - if isinstance(cable.manufacturer_part_number, list): - wireidentification.append(f'MPN: {cable.manufacturer_part_number[i - 1]}') - if isinstance(cable.internal_part_number, list): - wireidentification.append(f'IPN: {cable.internal_part_number[i - 1]}') + if isinstance(cable.pn, list): + wireidentification.append(f'P/N: {cable.pn[i - 1]}') + manufacturer_info = manufacturer_info_field(cable.manufacturer[i - 1] if isinstance(cable.manufacturer, list) else None, + cable.mpn[i - 1] if isinstance(cable.mpn, list) else None) + if manufacturer_info: + wireidentification.append(manufacturer_info) # print parameters into a table row under the wire if(len(wireidentification) > 0): html = f'{html}' @@ -337,7 +337,7 @@ def bom(self): bom_cables = [] bom_extra = [] # connectors - connector_group = lambda c: (c.type, c.subtype, c.pincount, c.manufacturer, c.manufacturer_part_number, c.internal_part_number) + connector_group = lambda c: (c.type, c.subtype, c.pincount, c.manufacturer, c.mpn, c.pn) for group in Counter([connector_group(v) for v in self.connectors.values()]): items = {k: v for k, v in self.connectors.items() if connector_group(v) == group} shared = next(iter(items.values())) @@ -349,14 +349,14 @@ def bom(self): conn_color = f', {shared.color}' if shared.color else '' name = f'Connector{conn_type}{conn_subtype}{conn_pincount}{conn_color}' item = {'item': name, 'qty': len(designators), 'unit': '', 'designators': designators if shared.show_name else '', - 'manufacturer': shared.manufacturer, 'manufacturer part number': shared.manufacturer_part_number, 'internal part number': shared.internal_part_number} + 'manufacturer': shared.manufacturer, 'mpn': shared.mpn, 'pn': shared.pn} bom_connectors.append(item) bom_connectors = sorted(bom_connectors, key=lambda k: k['item']) # https://stackoverflow.com/a/73050 bom.extend(bom_connectors) # cables # TODO: If category can have other non-empty values than 'bundle', maybe it should be part of item name? # The category needs to be included in cable_group to keep the bundles excluded. - cable_group = lambda c: (c.category, c.type, c.gauge, c.gauge_unit, c.wirecount, c.shield, c.manufacturer, c.manufacturer_part_number, c.internal_part_number) + cable_group = lambda c: (c.category, c.type, c.gauge, c.gauge_unit, c.wirecount, c.shield, c.manufacturer, c.mpn, c.pn) for group in Counter([cable_group(v) for v in self.cables.values() if v.category != 'bundle']): items = {k: v for k, v in self.cables.items() if cable_group(v) == group} shared = next(iter(items.values())) @@ -368,7 +368,7 @@ def bom(self): shield_name = ' shielded' if shared.shield else '' name = f'Cable{cable_type}, {shared.wirecount}{gauge_name}{shield_name}' item = {'item': name, 'qty': round(total_length, 3), 'unit': 'm', 'designators': designators, - 'manufacturer': shared.manufacturer, 'manufacturer part number': shared.manufacturer_part_number, 'internal part number': shared.internal_part_number} + 'manufacturer': shared.manufacturer, 'mpn': shared.mpn, 'pn': shared.pn} bom_cables.append(item) # bundles (ignores wirecount) wirelist = [] @@ -379,10 +379,10 @@ def bom(self): for index, color in enumerate(bundle.colors, 0): wirelist.append({'type': bundle.type, 'gauge': bundle.gauge, 'gauge_unit': bundle.gauge_unit, 'length': bundle.length, 'color': color, 'designator': bundle.name, 'manufacturer': index_if_list(bundle.manufacturer, index), - 'manufacturer part number': index_if_list(bundle.manufacturer_part_number, index), - 'internal part number': index_if_list(bundle.internal_part_number, index)}) + 'mpn': index_if_list(bundle.mpn, index), + 'pn': index_if_list(bundle.pn, index)}) # join similar wires from all the bundles to a single BOM item - wire_group = lambda w: (w.get('type', None), w['gauge'], w['gauge_unit'], w['color'], w['manufacturer'], w['manufacturer part number'], w['internal part number']) + wire_group = lambda w: (w.get('type', None), w['gauge'], w['gauge_unit'], w['color'], w['manufacturer'], w['mpn'], w['pn']) for group in Counter([wire_group(v) for v in wirelist]): items = [v for v in wirelist if wire_group(v) == group] shared = items[0] @@ -395,7 +395,7 @@ def bom(self): gauge_color = f', {shared["color"]}' if 'color' in shared != '' else '' name = f'Wire{wire_type}{gauge_name}{gauge_color}' item = {'item': name, 'qty': round(total_length, 3), 'unit': 'm', 'designators': designators, - 'manufacturer': shared['manufacturer'], 'manufacturer part number': shared['manufacturer part number'], 'internal part number': shared['internal part number']} + 'manufacturer': shared['manufacturer'], 'mpn': shared['mpn'], 'pn': shared['pn']} bom_cables.append(item) bom_cables = sorted(bom_cables, key=lambda k: k['item']) # sort list of dicts by their values (https://stackoverflow.com/a/73050) bom.extend(bom_cables) @@ -405,7 +405,7 @@ def bom(self): if isinstance(item.get('designators', None), List): item['designators'].sort() # sort designators if a list is provided item = {'item': name, 'qty': item.get('qty', None), 'unit': item.get('unit', None), 'designators': item.get('designators', None), - 'manufacturer': item.get('manufacturer', None), 'manufacturer part number': item.get('manufacturer_part_number', None), 'internal part number': item.get('internal_part_number', None)} + 'manufacturer': item.get('manufacturer', None), 'mpn': item.get('mpn', None), 'pn': item.get('pn', None)} bom_extra.append(item) bom_extra = sorted(bom_extra, key=lambda k: k['item']) bom.extend(bom_extra) @@ -414,11 +414,16 @@ def bom(self): def bom_list(self): bom = self.bom() keys = ['item', 'qty', 'unit', 'designators'] # these BOM columns will always be included - for fieldname in ['manufacturer', 'manufacturer part number', 'internal part number']: # these optional BOM columns will only be included if at least one BOM item actually uses them + for fieldname in ['pn', 'manufacturer', 'mpn']: # these optional BOM columns will only be included if at least one BOM item actually uses them if any(fieldname in x and x.get(fieldname, None) for x in bom): keys.append(fieldname) bom_list = [] - bom_list.append([k.capitalize() for k in keys]) # create header row with keys + # list of staic bom header names, headers not specified here are generated by capitilising the internal name + bom_headings = { + "pn": "P/N", + "mpn": "MPN" + } + bom_list.append([(bom_headings[k] if k in bom_headings else k.capitalize()) for k in keys]) # create header row with keys for item in bom: item_list = [item.get(key, '') for key in keys] # fill missing values with blanks item_list = [', '.join(subitem) if isinstance(subitem, List) else subitem for subitem in item_list] # convert any lists into comma separated strings diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 024c3ef4..13b660c9 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -117,4 +117,11 @@ def open_file_read(filename): return open(filename, 'r', encoding='UTF-8') def open_file_write(filename): - return open(filename, 'w', encoding='UTF-8') \ No newline at end of file + return open(filename, 'w', encoding='UTF-8') + + +def manufacturer_info_field(manufacturer, mpn): + if manufacturer or mpn: + return f'{manufacturer if manufacturer else "MPN"}{": " + str(mpn) if mpn else ""}' + else: + return None diff --git a/tutorial/tutorial08.md b/tutorial/tutorial08.md index 8369e90f..1fc884e7 100644 --- a/tutorial/tutorial08.md +++ b/tutorial/tutorial08.md @@ -3,3 +3,4 @@ * Part number information can be added to parts * Only provided fields will be added to the diagram and bom * Bundles can have part information specified by wire +* Additional parts can be added to the bom diff --git a/tutorial/tutorial08.yml b/tutorial/tutorial08.yml index 56a38005..2568f29b 100644 --- a/tutorial/tutorial08.yml +++ b/tutorial/tutorial08.yml @@ -3,11 +3,11 @@ connectors: type: Molex KK 254 pincount: 4 subtype: female - manufacturer: Molex - manufacturer_part_number: 22013047 + manufacturer: Molex # set manufacter name + mpn: 22013047 # set manufacturer part number X2: <<: *template1 # reuse template - internal_part_number: CON4 + pn: CON4 # set an internal part number X3: <<: *template1 # reuse template @@ -18,16 +18,16 @@ cables: gauge: 0.25 mm2 color_code: IEC manufacturer: CablesCo - manufacturer_part_number: ABC123 - internal_part_number: CAB1 + mpn: ABC123 + pn: CAB1 W2: category: bundle length: 1 gauge: 0.25 mm2 colors: [YE, BK, BK, RD] - manufacturer: [WiresCo,WiresCo,WiresCo,WiresCo] - manufacturer_part_number: [W1-YE,W1-BK,W1-BK,W1-RD] - internal_part_number: [WIRE1,WIRE2,WIRE2,WIRE3] + manufacturer: [WiresCo,WiresCo,WiresCo,WiresCo] # set a manufacter per wire + mpn: [W1-YE,W1-BK,W1-BK,W1-RD] + pn: [WIRE1,WIRE2,WIRE2,WIRE3] connections: @@ -39,3 +39,14 @@ connections: - X1: [1-4] - W2: [1-4] - X3: [1-4] + +additional_bom_items: + - # define an additional item to add to the bill of materials + description: Label, pinout information + qty: 2 + designators: + - X2 + - X3 + manufacturer: generic company + mpn: Label1 + pn: Label-ID-1