# GDSII and OASIS exchange with gdspy, gdstk and KLayout
Use layouttk when a Python or CLI workflow needs to read, write, extract or compare hierarchical GDSII and OASIS files. Existing gdspy and gdstk scripts can exchange layout files with layouttk, and KLayout can independently read back the result. When adapting a script to the layouttk API, start with the database unit and the distinction between a stored cell definition and geometry placed through that cell's references.
## Map coordinate units and API calls
| Operation | gdspy | gdstk | KLayout Python | layouttk |
| --- | --- | --- | --- | --- |
| Choose a 1 nm DBU | `GdsLibrary(unit=1e-6, precision=1e-9)` | `Library(unit=1e-6, precision=1e-9)` | `layout.dbu = 0.001` | `Layout(dbu_nm=1)` |
| Draw a 2 µm × 1 µm rectangle | `Rectangle((0, 0), (2, 1), layer=7)` | `rectangle((0, 0), (2, 1), layer=7)` | `Box(0, 0, 2000, 1000)` | `Region.box((0, 0), (2000, 1000))` |
| Read a file | `GdsLibrary(infile=path)` | `read_gds(path)` or `read_oas(path)` | `layout.read(path)` | `Layout.read(path)` |
| Write a file | `lib.write_gds(path)` | `lib.write_gds(path)` or `lib.write_oas(path)` | `layout.write(path)` | `layout.write(path)` |
gdspy and gdstk drawing coordinates are in user units. At the units above, `2` corresponds to `2000` DBU. KLayout's integer `Box` uses DBU, while its `layout.dbu` expresses the DBU in micrometers. layouttk stores integer DBU and reports the DBU in meters. The file layer and datatype pair in layouttk is `Layer(7, 0)`. KLayout resolves the pair to an internal layer index with `layout.layer(7, 0)`. [gdspy reference](https://gdspy.readthedocs.io/en/stable/reference.html) · [gdstk guide](https://heitzmann.github.io/gdstk/gettingstarted.html) · [KLayout database API](https://www.klayout.de/doc/programming/database_api.html)
## Migrate a gdspy GDSII workflow
Save the gdspy library with `write_gds()`, then load it with `Layout.read()`. Use `dbu_meters` to inspect the precision carried by the file. To port the drawing code itself, use `Polygon` for editable geometry and `cell.add_reference()` for instances. gdspy drawing coordinates are in library user units. With a 1 µm user unit and 1 nm DBU, multiply each user-unit coordinate by 1,000 before integer insertion. [gdspy function-by-function mapping](https://docs.rs/layouttk/0.5.0/layouttk/guides/gdspy_coverage/)
## Extract a gdstk layer and verify it in KLayout
Install `layouttk`, `gdstk` and `klayout` in the same Python environment. Run the example in an empty directory. It creates an OASIS file with a child cell, extracts one layer from a selected top, and writes GDSII. KLayout checks the units, the cell placement and the 2 µm × 1 µm rectangle on layer `7/0`.
```python
import gdstk
import klayout.db as kdb
from layouttk import Layer, Layout
library = gdstk.Library(unit=1e-6, precision=1e-9)
pad = library.new_cell("PAD")
pad.add(gdstk.rectangle((0, 0), (2, 1), layer=7))
pad.add(gdstk.rectangle((0, 0), (1, 1), layer=8))
top = library.new_cell("TOP")
top.add(gdstk.Reference(pad, (3, 4)))
library.write_oas("input.oas")
selected = Layout.read("input.oas", top="TOP", layers=[Layer(7, 0)])
selected.check_write("gds")
selected.write("metal.gds")
verified = kdb.Layout()
verified.read("metal.gds")
assert abs(verified.dbu - 0.001) < 1e-12
assert set(verified.layer_infos()) == {kdb.LayerInfo(7, 0)}
root = verified.top_cell()
assert root.name == "TOP"
assert [inst.cell.name for inst in root.each_inst()] == ["PAD"]
placed = kdb.Region(root.begin_shapes_rec(verified.layer(7, 0)))
expected = kdb.Region(kdb.Box(3000, 4000, 5000, 5000))
assert (placed ^ expected).is_empty()
```
For a real conversion, inspect hierarchy, text, repetitions and properties in addition to filled geometry. `layouttk check input.oas --to gds --json` gives field-level diagnostics when a circle, named property, irregular repetition or other feature cannot be encoded in the target format. For a geometry comparison inside Python, `analysis.compare_layouts()` computes per-layer XOR on a common DBU and reports stored structure and metadata separately. Exact grid refinement can require an explicit resample before the comparison.
## Map KLayout Python and Ruby region queries
KLayout's `layout.layer(7, 0)` returns a database layer index. Use the file layer pair `Layer(7, 0)` in layouttk. A recursive KLayout query with `begin_shapes_rec` corresponds to `cell.region(Layer(7, 0))` when you need a union of placed material. For individual placed shapes, use `hierarchy.polygons`. When checking XOR across tools, align the physical DBU and compare from the same top cell. [KLayout Python and Ruby API mapping](https://docs.rs/layouttk/0.5.0/layouttk/guides/python_api/)
See the [gdspy migration matrix](https://docs.rs/layouttk/0.5.0/layouttk/guides/gdspy_coverage/) for API coverage, the [CLI guide](https://docs.rs/layouttk/0.5.0/layouttk/guides/command_line/) for shell-based extraction and conversion, and the [Python examples](https://docs.rs/layouttk/0.5.0/layouttk/guides/python_workflows/) for more KLayout and array workflows.