diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index 3500307..7f01ffd 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -181,9 +181,11 @@ class Cable: image: Optional[Image] = None notes: Optional[MultilineHypertext] = None colors: List[Colors] = field(default_factory=list) + wirelabels: List[Wire] = field(default_factory=list) color_code: Optional[ColorScheme] = None show_name: bool = True show_wirecount: bool = True + show_wirenumbers: Optional[bool] = None ignore_in_bom: bool = False additional_components: List[AdditionalComponent] = field(default_factory=list) @@ -233,6 +235,10 @@ class Cable: raise Exception('Unknown number of wires. Must specify wirecount or colors (implicit length)') self.wirecount = len(self.colors) + if self.wirelabels: + if self.shield and 's' in self.wirelabels: + raise Exception('"s" may not be used as a wire label for a shielded cable.') + # if lists of part numbers are provided check this is a bundle and that it matches the wirecount. for idfield in [self.manufacturer, self.mpn, self.pn]: if isinstance(idfield, list): @@ -243,21 +249,24 @@ class Cable: else: raise Exception('lists of part data are only supported for bundles') + # by default, show wire numbers for cables, hide for bundles + if not self.show_wirenumbers: + self.show_wirenumbers = self.category != 'bundle' + for i, item in enumerate(self.additional_components): if isinstance(item, dict): self.additional_components[i] = AdditionalComponent(**item) # The *_pin arguments accept a tuple, but it seems not in use with the current code. - def connect(self, from_name: Optional[Designator], from_pin: NoneOrMorePins, via_pin: OneOrMoreWires, + def connect(self, from_name: Optional[Designator], from_pin: NoneOrMorePins, via_wire: OneOrMoreWires, to_name: Optional[Designator], to_pin: NoneOrMorePins) -> None: from_pin = int2tuple(from_pin) - via_pin = int2tuple(via_pin) + via_wire = int2tuple(via_wire) to_pin = int2tuple(to_pin) if len(from_pin) != len(to_pin): raise Exception('from_pin must have the same number of elements as to_pin') for i, _ in enumerate(from_pin): - # self.connections.append((from_name, from_pin[i], via_pin[i], to_name, to_pin[i])) - self.connections.append(Connection(from_name, from_pin[i], via_pin[i], to_name, to_pin[i])) + self.connections.append(Connection(from_name, from_pin[i], via_wire[i], to_name, to_pin[i])) def get_qty_multiplier(self, qty_multiplier: Optional[CableMultiplier]) -> float: if not qty_multiplier: diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index be8cf1a..42593ab 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -35,30 +35,46 @@ class Harness: def add_bom_item(self, item: dict) -> None: self.additional_bom_items.append(item) - def connect(self, from_name: str, from_pin: (int, str), via_name: str, via_pin: (int, str), to_name: str, to_pin: (int, str)) -> None: - for (name, pin) in zip([from_name, to_name], [from_pin, to_pin]): # check from and to connectors + def connect(self, from_name: str, from_pin: (int, str), via_name: str, via_wire: (int, str), to_name: str, to_pin: (int, str)) -> None: + # check from and to connectors + for (name, pin) in zip([from_name, to_name], [from_pin, to_pin]): if name is not None and name in self.connectors: connector = self.connectors[name] + # check if provided name is ambiguous if pin in connector.pins and pin in connector.pinlabels: - if connector.pins.index(pin) == connector.pinlabels.index(pin): - # TODO: Maybe issue a warning? It's not worthy of an exception if it's unambiguous, but maybe risky? - pass - else: + if connector.pins.index(pin) != connector.pinlabels.index(pin): raise Exception(f'{name}:{pin} is defined both in pinlabels and pins, for different pins.') + # TODO: Maybe issue a warning if present in both lists but referencing the same pin? if pin in connector.pinlabels: if connector.pinlabels.count(pin) > 1: raise Exception(f'{name}:{pin} is defined more than once.') - else: - index = connector.pinlabels.index(pin) - pin = connector.pins[index] # map pin name to pin number - if name == from_name: - from_pin = pin - if name == to_name: - to_pin = pin + index = connector.pinlabels.index(pin) + pin = connector.pins[index] # map pin name to pin number + if name == from_name: + from_pin = pin + if name == to_name: + to_pin = pin if not pin in connector.pins: raise Exception(f'{name}:{pin} not found.') - self.cables[via_name].connect(from_name, from_pin, via_pin, to_name, to_pin) + # check via cable + if via_name in self.cables: + cable = self.cables[via_name] + # check if provided name is ambiguous + if via_wire in cable.colors and via_wire in cable.wirelabels: + if cable.colors.index(via_wire) != cable.wirelabels.index(via_wire): + raise Exception(f'{via_name}:{via_wire} is defined both in colors and wirelabels, for different wires.') + # TODO: Maybe issue a warning if present in both lists but referencing the same wire? + if via_wire in cable.colors: + if cable.colors.count(via_wire) > 1: + raise Exception(f'{via_name}:{via_wire} is used for more than one wire.') + via_wire = cable.colors.index(via_wire) + 1 # list index starts at 0, wire IDs start at 1 + elif via_wire in cable.wirelabels: + if cable.wirelabels.count(via_wire) > 1: + raise Exception(f'{via_name}:{via_wire} is used for more than one wire.') + via_wire = cable.wirelabels.index(via_wire) + 1 # list index starts at 0, wire IDs start at 1 + + self.cables[via_name].connect(from_name, from_pin, via_wire, to_name, to_pin) if from_name in self.connectors: self.connectors[from_name].activate_pin(from_pin) if to_name in self.connectors: @@ -197,10 +213,22 @@ class Harness: wirehtml.append('') # conductor table wirehtml.append(' ') - for i, connection_color in enumerate(cable.colors, 1): + for i, (connection_color, wirelabel) in enumerate(zip_longest(cable.colors, cable.wirelabels), 1): wirehtml.append(' ') wirehtml.append(f' ') - wirehtml.append(f' ') + wirehtml.append(f' ') wirehtml.append(f' ') wirehtml.append(' ')
 
{wv_colors.translate_color(connection_color, self.color_mode)}') + + wireinfo = [] + if cable.show_wirenumbers: + wireinfo.append(str(i)) + colorstr = wv_colors.translate_color(connection_color, self.color_mode) + if colorstr: + wireinfo.append(colorstr) + if cable.wirelabels: + wireinfo.append(wirelabel if wirelabel is not None else '') + wirehtml.append(f' {":".join(wireinfo)}') + + wirehtml.append(f'