Last active
July 3, 2025 16:13
-
-
Save xkstein/f395a8d864d3943a6ed7d2ea780dd714 to your computer and use it in GitHub Desktop.
Find Length KLayout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <klayout-macro> | |
| <description>Find Resonator Length</description> | |
| <version/> | |
| <category>pymacros</category> | |
| <prolog/> | |
| <epilog/> | |
| <doc/> | |
| <autorun>true</autorun> | |
| <autorun-early>false</autorun-early> | |
| <priority>0</priority> | |
| <shortcut/> | |
| <show-in-menu>true</show-in-menu> | |
| <group-name/> | |
| <menu-path/> | |
| <interpreter>python</interpreter> | |
| <dsl-interpreter-name/> | |
| <text>import pya | |
| import math | |
| app = pya.Application.instance() | |
| view = app.main_window().current_view() | |
| menu = app.main_window().menu() | |
| def find_length(): | |
| selection = pya.Region() | |
| dbu = [] | |
| for path in view.object_selection: | |
| dbu.append(view.cellview(path.cv_index).layout().dbu) | |
| selection.insert(path.shape.polygon) | |
| if selection.is_empty(): | |
| pya.MessageBox.warning('Warning', 'No Selection, select a resonator', pya.MessageBox.Ok) | |
| return | |
| if any([ unit != dbu[0] for unit in dbu ]): | |
| pya.MessageBox.warning('Warning', 'Warning: some of the selection is in a layer with a different unit', pya.MessageBox.Ok) | |
| return | |
| dbu = dbu[0] | |
| selection.merge() | |
| perimeter = dbu * selection.perimeter() | |
| # area = dbu * dbu * selection.area() | |
| length = perimeter / 2 | |
| pya.MessageBox.info('Length Estimate', f'Approximate Length: {length}\n\nRemember: this only really works for convex closed loops (if measuring a spiral, subtract the waveguide width)', pya.MessageBox.Ok) | |
| # Maybe more general formula? | |
| # length = perimeter / 4 + math.sqrt(perimeter ** 2 / 16 - area) | |
| if __name__ == "__main__": | |
| if view is not None: | |
| find_length() | |
| </text> | |
| </klayout-macro> |
Author
xkstein
commented
Jul 2, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment