-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremap.py
More file actions
52 lines (38 loc) · 2.02 KB
/
Copy pathremap.py
File metadata and controls
52 lines (38 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Remap layers.
A layer map sends a source (layer, datatype) to a target one; anything not in the map passes through
unchanged. Polygons and labels are relabelled, not moved, so the geometry is identical, just on a different
layer. When two source layers map to the same target, their polygons collect on that target (use the merge
operation afterwards to union them).
"""
from __future__ import annotations
import argparse
import load as _load
def remap(layout, layer_map):
"""Return a new Layout with each layer rewritten through layer_map (unmapped layers pass through)."""
lm = {tuple(k): tuple(v) for k, v in layer_map.items()}
out = _load.Layout(name=layout.name)
for layer, polys in layout.polys.items():
target = lm.get(layer, layer)
out.polys.setdefault(target, []).extend(polys)
for layer, text, pos in layout.labels:
out.labels.append((lm.get(layer, layer), text, pos))
return out
def _validate():
layout = _load.from_gdstk_library(_load._build_demo_library())
moved = remap(layout, {(1, 0): (5, 0)})
ok_move = moved.layers == {(5, 0), (2, 0)} and abs(moved.area_on((5, 0)) - 4.0) < 1e-9
ok_passthrough = abs(moved.area_on((2, 0)) - 16.0) < 1e-9
collapsed = remap(layout, {(1, 0): (2, 0)}) # both layers land on (2, 0)
ok_collapse = collapsed.layers == {(2, 0)} and abs(collapsed.area_on((2, 0)) - 20.0) < 1e-9
relabel = remap(layout, {(10, 0): (11, 0)})
ok_label = relabel.labels[0][0] == (11, 0)
for label, cond in [("layer moved", ok_move), ("unmapped passes through", ok_passthrough),
("two layers collapse", ok_collapse), ("label relabelled", ok_label)]:
print(f"[{label}] {'PASS' if cond else 'FAIL'}")
print("RESULT:", "PASS" if all([ok_move, ok_passthrough, ok_collapse, ok_label]) else "FAIL")
def main():
ap = argparse.ArgumentParser(description="remap layers")
ap.add_argument("--validate", action="store_true")
ap.parse_args(); _validate()
if __name__ == "__main__":
main()