Renumber all non-annotation component IDs left-to-right, top-to-bottom. Components are grouped by refdes PREFIX (not symbol), so everything sharing a prefix is numbered in one sequence — every subcircuit block uses 'X', and e.g. nmos/pmos both use 'M', so their refdes must not coll
(scene)
| 6 | |
| 7 | |
| 8 | def rename_left_right_top_bottom(scene) -> int: |
| 9 | """ |
| 10 | Renumber all non-annotation component IDs left-to-right, top-to-bottom. |
| 11 | |
| 12 | Components are grouped by refdes PREFIX (not symbol), so everything sharing a |
| 13 | prefix is numbered in one sequence — every subcircuit block uses 'X', and |
| 14 | e.g. nmos/pmos both use 'M', so their refdes must not collide. Sort order: |
| 15 | y ascending (top first), then x ascending (left first) within the same row. |
| 16 | The per-prefix counter is seeded to match (keep consistent with _next_id). |
| 17 | |
| 18 | Returns the number of components whose ID actually changed. |
| 19 | """ |
| 20 | groups: dict[str, list[ComponentItem]] = defaultdict(list) |
| 21 | for item in scene.items(): |
| 22 | if isinstance(item, ComponentItem) and item.symbol_name not in _ANNOTATION: |
| 23 | prefix = SYMBOL_PREFIX.get(item.symbol_name, "X") |
| 24 | groups[prefix].append(item) |
| 25 | |
| 26 | changed = 0 |
| 27 | for prefix, comps in groups.items(): |
| 28 | comps.sort(key=lambda c: (round(c.pos().y()), round(c.pos().x()))) |
| 29 | for i, comp in enumerate(comps, start=1): |
| 30 | new_id = f"{prefix}{i}" |
| 31 | if comp.instance_id != new_id: |
| 32 | comp.instance_id = new_id |
| 33 | comp.update_labels() |
| 34 | changed += 1 |
| 35 | scene._counters[prefix] = len(comps) + 1 |
| 36 | |
| 37 | return changed |
no test coverage detected