diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index 6bc9eb8..52ff983 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from typing import Optional, List, Any +from typing import Optional, List, Any, Union from dataclasses import dataclass, field from wireviz.wv_helper import int2tuple from wireviz import wv_colors @@ -10,6 +10,9 @@ from wireviz import wv_colors @dataclass class Connector: name: str + manufacturer: Optional[str] = None + manufacturer_part_number: Optional[str] = None + internal_part_number: Optional[str] = None category: Optional[str] = None type: Optional[str] = None subtype: Optional[str] = None @@ -61,6 +64,9 @@ class Connector: @dataclass 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 category: Optional[str] = None type: Optional[str] = None gauge: Optional[float] = None @@ -118,6 +124,16 @@ class Cable: raise Exception('Unknown number of wires. Must specify wirecount or colors (implicit length)') 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]: + if isinstance(idfield, list): + if self.category == "bundle": + # check the length + if len(idfield) != self.wirecount: + raise Exception('lists of part data must match wirecount') + else: + raise Exception('lists of part data are only supported for bundles') + # for BOM generation self.wirecount_and_shield = (self.wirecount, self.shield) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index d15b998..81fecd6 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -62,6 +62,18 @@ class Harness: subtype = f', {connector.subtype}' if connector.subtype else '' color = wv_colors.translate_color(connector.color, self.color_mode) if connector.color else '' infostring = f'{connector.type}{subtype} {color}' + + # id = identification + identification = [connector.manufacturer, + f'MPN: {connector.manufacturer_part_number}' if connector.manufacturer_part_number else '', + f'IPN: {connector.internal_part_number}' if connector.internal_part_number else ''] + identification = list(filter(None, identification)) + if(len(identification) > 0): + infostring = f'{infostring}
' + for attrib in identification: + infostring = f'{infostring}{attrib}, ' + infostring = infostring[:-2] # remove trainling comma and space + infostring_l = infostring if connector.ports_right else '' infostring_r = infostring if connector.ports_left else '' @@ -83,6 +95,10 @@ class Harness: >'''.format(infostring_l=infostring_l, infostring_r=infostring_r, colorbar=colorbar)) else: # not a ferrule + identification = [connector.manufacturer, + f'MPN: {connector.manufacturer_part_number}' if connector.manufacturer_part_number else '', + f'IPN: {connector.internal_part_number}' if connector.internal_part_number else ''] + attributes = [connector.type, connector.subtype, f'{connector.pincount}-pin' if connector.show_pincount else''] @@ -95,7 +111,7 @@ class Harness: pinouts[0].append(f'{pinnumber}') if connector.ports_right: pinouts[2].append(f'{pinnumber}') - label = [connector.name if connector.show_name else '', attributes, pinouts, connector.notes] + label = [connector.name if connector.show_name else '', identification, attributes, pinouts, connector.notes] dot.node(key, label=nested(label)) if len(connector.loops) > 0: @@ -124,8 +140,12 @@ class Harness: elif cable.gauge_unit.upper() == 'AWG': awg_fmt = f' ({mm2_equiv(cable.gauge)} mm\u00B2)' - attributes = [cable.type, - f'{len(cable.colors)}x' if cable.show_wirecount else '', + 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 = list(filter(None, identification)) + + attributes = [f'{len(cable.colors)}x' if cable.show_wirecount else '', f'{cable.gauge} {cable.gauge_unit}{awg_fmt}' if cable.gauge else '', '+ S' if cable.shield else '', f'{cable.length} m' if cable.length > 0 else ''] @@ -136,6 +156,11 @@ class Harness: html = f'{html}' # name+attributes table if cable.show_name: html = f'{html}' + if(len(identification) > 0): # print an identification row if values specified + html = f'{html}' # end identification row html = f'{html}' # attribute row for attrib in attributes: html = f'{html}' @@ -158,6 +183,21 @@ class Harness: bgcolor = wv_colors.translate_color(connection, 'hex') bgcolor = bgcolor if bgcolor != '' else '#ffffff' html = f'{html}' + 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]}') + # print parameters into a table row under the wire + if(len(wireidentification) > 0): + html = f'{html}' if cable.shield: p = ['', 'Shield', ''] @@ -254,7 +294,7 @@ class Harness: bom_connectors = [] bom_cables = [] # connectors - connector_group = lambda c: (c.type, c.subtype, c.pincount) + connector_group = lambda c: (c.type, c.subtype, c.pincount, c.manufacturer, c.manufacturer_part_number, c.internal_part_number) groups = Counter([connector_group(v) for v in self.connectors.values()]) for group in groups: items = {k: v for k, v in self.connectors.items() if connector_group(v) == group} @@ -268,13 +308,23 @@ class Harness: name = f'Connector{conn_type}{conn_subtype}{conn_pincount}{conn_color}' item = {'item': name, 'qty': len(designators), 'unit': '', 'designators': designators if shared.category != 'ferrule' else ''} + if shared.manufacturer is not None: # set manufacturer only if it exists + item['manufacturer'] = shared.manufacturer + if shared.manufacturer_part_number is not None: # set part number only if it exists + item['manufacturer part number'] = shared.manufacturer_part_number + if shared.internal_part_number is not None: # set part number only if it exists + item['internal part number'] = shared.internal_part_number 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? # Otherwise, it can be removed from the cable_group because it will allways be empty. - cable_group = lambda c: (c.category, c.type, c.gauge, c.gauge_unit, c.wirecount, c.shield) + cable_group = lambda c: (c.category, c.type, c.gauge, c.gauge_unit, c.wirecount, c.shield, + c.manufacturer if not isinstance(c.manufacturer, list) else None, + c.manufacturer_part_number if not isinstance(c.manufacturer_part_number, list) else None, + c.internal_part_number if not isinstance(c.manufacturer_part_number, list) else None + ) groups = Counter([cable_group(v) for v in self.cables.values() if v.category != 'bundle']) for group in groups: items = {k: v for k, v in self.cables.items() if cable_group(v) == group} @@ -287,11 +337,17 @@ class Harness: 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} - bom_cables.append(item) + if shared.manufacturer is not None: # set manufacturer only if it exists + item['manufacturer'] = shared.manufacturer + if shared.manufacturer_part_number is not None: # set part number only if it exists + item['manufacturer part number'] = shared.manufacturer_part_number + if shared.internal_part_number is not None: # set part number only if it exists + item['internal part number'] = shared.internal_part_number + bom_cables.append(item) # bundles (ignores wirecount) wirelist = [] # list all cables again, since bundles are represented as wires internally, with the category='bundle' set - bundle_group = lambda b: (b.type, b.gauge, b.gauge_unit, b.length) # TODO: Why is b.lenght included? + bundle_group = lambda b: (b.type, b.gauge, b.gauge_unit, b.length) # TODO: Why is b.length included? groups = Counter([bundle_group(v) for v in self.cables.values() if v.category == 'bundle']) for group in groups: items = {k: v for k, v in self.cables.items() if bundle_group(v) == group} @@ -299,10 +355,13 @@ class Harness: for bundle in items.values(): # add each wire from each bundle to the wirelist for color in bundle.colors: - wirelist.append({'type': shared.type, 'gauge': shared.gauge, 'gauge_unit': shared.gauge_unit, - 'length': shared.length, 'color': color, 'designator': bundle.name}) - # join similar wires from all the bundles to a single BOM item - wire_group = lambda w: (w['type'], w['gauge'], w['gauge_unit'], w['color']) + wireinfo = {'gauge': shared.gauge, 'gauge_unit': shared.gauge_unit, 'length': shared.length, 'color': color, 'designator': bundle.name} + wireinfo['manufacturer'] = bundle.manufacturer[index] if isinstance(bundle.manufacturer, list) else None + wireinfo['manufacturer part number'] = bundle.manufacturer_part_number[index] if isinstance(bundle.manufacturer_part_number, list) else None + wireinfo['internal part number'] = bundle.internal_part_number[index] if isinstance(bundle.internal_part_number, list) else None + wirelist.append(wireinfo) + # join similar wires from all the bundles to a single BOM item + wire_group = lambda w: (w['type'], w['gauge'], w['gauge_unit'], w['color'], w['manufacturer'], w['manufacturer part number'], w['internal part number']) groups = Counter([wire_group(v) for v in wirelist]) for group in groups: items = [v for v in wirelist if wire_group(v) == group] @@ -317,6 +376,12 @@ class Harness: gauge_color = f', {shared["color"]}' if shared['color'] != '' else '' name = f'Wire{wire_type}{gauge_name}{gauge_color}' item = {'item': name, 'qty': round(total_length, 3), 'unit': 'm', 'designators': designators} + if shared['manufacturer'] is not None: # set manufacturer only if it exists + item['manufacturer'] = shared['manufacturer'] + if shared['manufacturer part number'] is not None: # set part number only if it exists + item['manufacturer part number'] = shared['manufacturer part number'] + if shared['internal part number'] is not None: # set part number only if it exists + item['internal part number'] = shared['internal part number'] bom_cables.append(item) bom_cables = sorted(bom_cables, key=lambda k: k['item']) # https://stackoverflow.com/a/73050 bom.extend(bom_cables) @@ -325,6 +390,10 @@ class Harness: def bom_list(self): bom = self.bom() keys = ['item', 'qty', 'unit', 'designators'] + # check if any optional fields are set and add to keys if they are + for fieldname in ["manufacturer", "manufacturer part number", "internal part number"]: + if any(fieldname in x for x in bom): + keys.append(fieldname) bom_list = [] bom_list.append([k.capitalize() for k in keys]) # create header row with keys for item in bom: diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 81b4771..27d9d55 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -10,6 +10,7 @@ import yaml if __name__ == '__main__': sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) + from wireviz.Harness import Harness diff --git a/tutorial/readme.md b/tutorial/readme.md index dc7bbcb..1ed541d 100644 --- a/tutorial/readme.md +++ b/tutorial/readme.md @@ -361,3 +361,61 @@ Output: [Bill of Materials](tutorial07.bom.tsv) +## 8 - Part numbers + +* 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 + +[Source](tutorial08.yml): + +```yaml +connectors: + X1: &template1 # define a template for later use + type: Molex KK 254 + pincount: 4 + subtype: female + manufacturer: Molex + manufacturer_part_number: 22013047 + X2: + <<: *template1 # reuse template + internal_part_number: CON4 + X3: + <<: *template1 # reuse template + +cables: + W1: + wirecount: 4 + length: 1 + gauge: 0.25 mm2 + color_code: IEC + manufacturer: CablesCo + manufacturer_part_number: ABC123 + internal_part_number: 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] + + +connections: + - + - X1: [1-4] + - W1: [1-4] + - X2: [1-4] + - + - X1: [1-4] + - W2: [1-4] + - X3: [1-4] +``` + + +Output: + +![](tutorial08.png) + +[Bill of Materials](tutorial08.bom.tsv) diff --git a/tutorial/tutorial08.bom.tsv b/tutorial/tutorial08.bom.tsv new file mode 100644 index 0000000..682dd4a --- /dev/null +++ b/tutorial/tutorial08.bom.tsv @@ -0,0 +1,7 @@ +Item Qty Unit Designators Manufacturer Manufacturer part number Internal part number +Connector, Molex KK 254, female, 4 pins 2 X1, X3 Molex 22013047 +Connector, Molex KK 254, female, 4 pins 1 X2 Molex 22013047 CON4 +Cable, 4 x 0.25 mm² 1 m W1 CablesCo ABC123 CAB1 +Wire, 0.25 mm², BK 2 m W2 WiresCo W1-BK WIRE2 +Wire, 0.25 mm², RD 1 m W2 WiresCo W1-RD WIRE3 +Wire, 0.25 mm², YE 1 m W2 WiresCo W1-YE WIRE1 diff --git a/tutorial/tutorial08.gv b/tutorial/tutorial08.gv new file mode 100644 index 0000000..4eb5221 --- /dev/null +++ b/tutorial/tutorial08.gv @@ -0,0 +1,36 @@ +graph { +// Graph generated by WireViz +// https://github.com/formatc1702/WireViz + graph [bgcolor=white fontname=arial nodesep=0.33 rankdir=LR ranksep=2] + node [fillcolor=white fontname=arial shape=record style=filled] + edge [fontname=arial style=bold] + X1 [label="X1|{Molex|MPN: 22013047}|{Molex KK 254|female|4-pin}|{{1|2|3|4}}"] + X2 [label="X2|{Molex|MPN: 22013047|IPN: CON4}|{Molex KK 254|female|4-pin}|{{1|2|3|4}}"] + X3 [label="X3|{Molex|MPN: 22013047}|{Molex KK 254|female|4-pin}|{{1|2|3|4}}"] + edge [color="#000000:#666600:#000000"] + X1:p1r:e -- W1:w1:w + W1:w1:e -- X2:p1l:w + edge [color="#000000:#ff0000:#000000"] + X1:p2r:e -- W1:w2:w + W1:w2:e -- X2:p2l:w + edge [color="#000000:#ff8000:#000000"] + X1:p3r:e -- W1:w3:w + W1:w3:e -- X2:p3l:w + edge [color="#000000:#ffff00:#000000"] + X1:p4r:e -- W1:w4:w + W1:w4:e -- X2:p4l:w + W1 [label=<
{cable.name}
' + for attrib in identification: + html = f'{html}' + html = f'{html}
{attrib}
{attrib}
' + for attrib in wireidentification: + html = f'{html}' + html = f'{html}
{attrib}
W1
CablesCoMPN: ABC123IPN: CAB1
4x0.25 mm²1 m
 
X1:1BNX2:1
X1:2RDX2:2
X1:3OGX2:3
X1:4YEX2:4
 
> fillcolor=white margin=0 shape=box style=""] + edge [color="#000000:#ffff00:#000000"] + X1:p1r:e -- W2:w1:w + W2:w1:e -- X3:p1l:w + edge [color="#000000:#000000:#000000"] + X1:p2r:e -- W2:w2:w + W2:w2:e -- X3:p2l:w + edge [color="#000000:#000000:#000000"] + X1:p3r:e -- W2:w3:w + W2:w3:e -- X3:p3l:w + edge [color="#000000:#ff0000:#000000"] + X1:p4r:e -- W2:w4:w + W2:w4:e -- X3:p4l:w + W2 [label=<
W2
4x0.25 mm²1 m
 
X1:1YEX3:1
WiresCoMPN: W1-YEIPN: WIRE1
X1:2BKX3:2
WiresCoMPN: W1-BKIPN: WIRE2
X1:3BKX3:3
WiresCoMPN: W1-BKIPN: WIRE2
X1:4RDX3:4
WiresCoMPN: W1-RDIPN: WIRE3
 
> fillcolor=white margin=0 shape=box style="filled,dashed"] +} diff --git a/tutorial/tutorial08.html b/tutorial/tutorial08.html new file mode 100644 index 0000000..4dc11ee --- /dev/null +++ b/tutorial/tutorial08.html @@ -0,0 +1,294 @@ +

Diagram

+ + + + + +%3 + + + +X1 + +X1 + +Molex + +MPN: 22013047 + +Molex KK 254 + +female + +4-pin + +1 + +2 + +3 + +4 + + + +W1 + + +W1 + +CablesCo +MPN: ABC123 +IPN: CAB1 + +4x + +0.25 mm² + +1 m +  +X1:1 +BN +X2:1 + + + +X1:2 +RD +X2:2 + + + +X1:3 +OG +X2:3 + + + +X1:4 +YE +X2:4 + + + +  + + + +X1:e--W1:w + + + + + + +X1:e--W1:w + + + + + + +X1:e--W1:w + + + + + + +X1:e--W1:w + + + + + + +W2 + + +W2 + +4x + +0.25 mm² + +1 m +  +X1:1 +YE +X3:1 + + + +WiresCo +MPN: W1-YE +IPN: WIRE1 +X1:2 +BK +X3:2 + + + +WiresCo +MPN: W1-BK +IPN: WIRE2 +X1:3 +BK +X3:3 + + + +WiresCo +MPN: W1-BK +IPN: WIRE2 +X1:4 +RD +X3:4 + + + +WiresCo +MPN: W1-RD +IPN: WIRE3 +  + + + +X1:e--W2:w + + + + + + +X1:e--W2:w + + + + + + +X1:e--W2:w + + + + + + +X1:e--W2:w + + + + + + +X2 + +X2 + +Molex + +MPN: 22013047 + +IPN: CON4 + +Molex KK 254 + +female + +4-pin + +1 + +2 + +3 + +4 + + + +X3 + +X3 + +Molex + +MPN: 22013047 + +Molex KK 254 + +female + +4-pin + +1 + +2 + +3 + +4 + + + +W1:e--X2:w + + + + + + +W1:e--X2:w + + + + + + +W1:e--X2:w + + + + + + +W1:e--X2:w + + + + + + +W2:e--X3:w + + + + + + +W2:e--X3:w + + + + + + +W2:e--X3:w + + + + + + +W2:e--X3:w + + + + + + +

Bill of Materials

ItemQtyUnitDesignatorsManufacturerManufacturer part numberInternal part number
Connector, Molex KK 254, female, 4 pins2X1, X3Molex22013047
Connector, Molex KK 254, female, 4 pins1X2Molex22013047CON4
Cable, 4 x 0.25 mm²1mW1CablesCoABC123CAB1
Wire, 0.25 mm², BK2mW2WiresCoW1-BKWIRE2
Wire, 0.25 mm², RD1mW2WiresCoW1-RDWIRE3
Wire, 0.25 mm², YE1mW2WiresCoW1-YEWIRE1
\ No newline at end of file diff --git a/tutorial/tutorial08.png b/tutorial/tutorial08.png new file mode 100644 index 0000000..3d68820 Binary files /dev/null and b/tutorial/tutorial08.png differ diff --git a/tutorial/tutorial08.svg b/tutorial/tutorial08.svg new file mode 100644 index 0000000..13427b8 --- /dev/null +++ b/tutorial/tutorial08.svg @@ -0,0 +1,293 @@ + + + + + + +%3 + + + +X1 + +X1 + +Molex + +MPN: 22013047 + +Molex KK 254 + +female + +4-pin + +1 + +2 + +3 + +4 + + + +W1 + + +W1 + +CablesCo +MPN: ABC123 +IPN: CAB1 + +4x + +0.25 mm² + +1 m +  +X1:1 +BN +X2:1 + + + +X1:2 +RD +X2:2 + + + +X1:3 +OG +X2:3 + + + +X1:4 +YE +X2:4 + + + +  + + + +X1:e--W1:w + + + + + + +X1:e--W1:w + + + + + + +X1:e--W1:w + + + + + + +X1:e--W1:w + + + + + + +W2 + + +W2 + +4x + +0.25 mm² + +1 m +  +X1:1 +YE +X3:1 + + + +WiresCo +MPN: W1-YE +IPN: WIRE1 +X1:2 +BK +X3:2 + + + +WiresCo +MPN: W1-BK +IPN: WIRE2 +X1:3 +BK +X3:3 + + + +WiresCo +MPN: W1-BK +IPN: WIRE2 +X1:4 +RD +X3:4 + + + +WiresCo +MPN: W1-RD +IPN: WIRE3 +  + + + +X1:e--W2:w + + + + + + +X1:e--W2:w + + + + + + +X1:e--W2:w + + + + + + +X1:e--W2:w + + + + + + +X2 + +X2 + +Molex + +MPN: 22013047 + +IPN: CON4 + +Molex KK 254 + +female + +4-pin + +1 + +2 + +3 + +4 + + + +X3 + +X3 + +Molex + +MPN: 22013047 + +Molex KK 254 + +female + +4-pin + +1 + +2 + +3 + +4 + + + +W1:e--X2:w + + + + + + +W1:e--X2:w + + + + + + +W1:e--X2:w + + + + + + +W1:e--X2:w + + + + + + +W2:e--X3:w + + + + + + +W2:e--X3:w + + + + + + +W2:e--X3:w + + + + + + +W2:e--X3:w + + + + + + diff --git a/tutorial/tutorial08.yml b/tutorial/tutorial08.yml new file mode 100644 index 0000000..56a3800 --- /dev/null +++ b/tutorial/tutorial08.yml @@ -0,0 +1,41 @@ +connectors: + X1: &template1 # define a template for later use + type: Molex KK 254 + pincount: 4 + subtype: female + manufacturer: Molex + manufacturer_part_number: 22013047 + X2: + <<: *template1 # reuse template + internal_part_number: CON4 + X3: + <<: *template1 # reuse template + +cables: + W1: + wirecount: 4 + length: 1 + gauge: 0.25 mm2 + color_code: IEC + manufacturer: CablesCo + manufacturer_part_number: ABC123 + internal_part_number: 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] + + +connections: + - + - X1: [1-4] + - W1: [1-4] + - X2: [1-4] + - + - X1: [1-4] + - W2: [1-4] + - X3: [1-4]