diff --git a/src/wireviz/wv_dataclasses.py b/src/wireviz/wv_dataclasses.py index 1009ade..37d53a9 100644 --- a/src/wireviz/wv_dataclasses.py +++ b/src/wireviz/wv_dataclasses.py @@ -293,6 +293,42 @@ class AdditionalComponent(GraphicalComponent): self.explicit_qty = False +@dataclass +class StripSpec: + """Strip length specification for sleeve or insulation.""" + length: float = 0 + length_unit: str = "mm" + + def __str__(self) -> str: + return f"{self.length} {self.length_unit}" if self.length > 0 else "" + + +@dataclass +class Strip: + """Wire stripping specifications for a connector.""" + sleeve: Optional[StripSpec] = None + insulation: Optional[StripSpec] = None + + def __post_init__(self) -> None: + if isinstance(self.sleeve, str): + self.sleeve = Strip._parse_length(self.sleeve) + if isinstance(self.insulation, str): + self.insulation = Strip._parse_length(self.insulation) + + @staticmethod + def _parse_length(spec: str) -> StripSpec: + parts = spec.strip().split(" ", 1) + try: + length = float(parts[0]) + except (ValueError, IndexError): + raise Exception( + f"Strip length '{spec}' must be a number, " + "or number and unit separated by a space" + ) + unit = parts[1].strip() if len(parts) > 1 else "mm" + return StripSpec(length=length, length_unit=unit) + + @dataclass class TopLevelGraphicalComponent(GraphicalComponent): # abstract class # component properties @@ -317,6 +353,7 @@ class Connector(TopLevelGraphicalComponent): shorts_hide_lable: bool = False # pin information in particular pincount: Optional[int] = None + strip: Optional[Strip] = None pins: List[Pin] = field(default_factory=list) # legacy pinlabels: List[Pin] = field(default_factory=list) # legacy pincolors: List[str] = field(default_factory=list) # legacy @@ -367,6 +404,11 @@ class Connector(TopLevelGraphicalComponent): if isinstance(self.image, dict): self.image = Image(**self.image) + if isinstance(self.strip, dict): + self.strip = Strip(**self.strip) + elif self.strip is None: + self.strip = Strip() + self.ports_left = False self.ports_right = False self.visible_pins = {} diff --git a/src/wireviz/wv_graphviz.py b/src/wireviz/wv_graphviz.py index 29ab520..aff9ee6 100644 --- a/src/wireviz/wv_graphviz.py +++ b/src/wireviz/wv_graphviz.py @@ -63,6 +63,16 @@ def gv_node_component(component: Component) -> Table: str(component.color) if component.color else None, ] + # strip info for connectors + line_strip = None + if isinstance(component, Connector) and (component.strip.sleeve or component.strip.insulation): + strip_parts = [] + if component.strip.sleeve: + strip_parts.append(f"Strip sleeve: {component.strip.sleeve}") + if component.strip.insulation: + strip_parts.append(f"Strip insulation: {component.strip.insulation}") + line_strip = [Td(" | ".join(strip_parts))] + if component.additional_parameters: line_additional_parameters = nested_table_dict(component.additional_parameters) else: @@ -87,6 +97,7 @@ def gv_node_component(component: Component) -> Table: line_name, line_pn, line_info, + line_strip, line_additional_parameters, line_ports, line_image,