# Get started with GDSII and OASIS in layouttk
## Start with a layout you already have
To extract a layer from a GDSII or OASIS file, you need the filename, the top cell name and the layer pair. Download and unpack the command-line version for your system, then follow the extraction steps. Once installed, the tool can process files on an internal network. [Release downloads](https://github.com/zwh42/layouttk/releases/latest) · [Extract a layer step by step](https://docs.rs/layouttk/0.3.0/layouttk/guides/command_line/)
If your team has set up a coding agent, you can ask it to “Extract layer `7/0` from top cell `TOP` in `chip.oas`. Save it as `metal.gds` and check the layer in the output file.”
## Install the library
To generate a small test layout, use a standard Python 3.11 or newer installation. Open a terminal in your working folder and run the following commands. The second command prints the installed version.
```sh
python -m pip install layouttk
python -c "import layouttk; print(layouttk.__version__)"
```
Installation packages are available for Linux x86-64 (glibc 2.28+), Apple Silicon macOS and 64-bit x86 Windows. The test uses the standard, GIL-enabled build of CPython. For NumPy and PyTorch examples, see the [Python workflows](https://docs.rs/layouttk/0.3.0/layouttk/guides/python_workflows/).
## Create your first layout
Save this example as `first_mask.py` and run `python first_mask.py` in an empty folder. It writes `mask.gds` and `mask.oas`. Both files contain one rectangle on layer `7/0`, which means layer 7, datatype 0.
```python
from layouttk import Layer, Layout, Region
metal = Layer(7, 0)
mask = Layout("FIRST_MASK", dbu_nm=1)
pad = mask.add_cell("PAD")
pad.add_region(Region.box((0, 0), (1000, 500)), layer=metal)
top = mask.add_cell("TOP")
top.add_reference(pad, (2000, 3000))
for filename in ("mask.gds", "mask.oas"):
mask.write(filename)
restored = Layout.read(filename)
assert restored.top_cells == ["TOP"]
assert restored.cell("TOP").layers() == (metal,)
expected = Region.box((2000, 3000), (3000, 3500))
assert not (restored.cell("TOP").region(metal) ^ expected)
print("PAD: 1.0 × 0.5 µm; TOP: one placed pad")
```
Each grid step in this example is one nanometre. The rectangle measures 1 × 0.5 µm. The script opens both files again and checks the rectangle’s shape and position.
## Inspect the result
Open either output file in KLayout. Select the top cell `TOP` and layer `7/0`. The rectangle’s lower-left corner should be at (2, 3) µm. The `PAD` cell defines the rectangle, and `TOP` places it at that position.
With the CLI installed, run these commands in the directory containing the example output.
```sh
layouttk inspect mask.oas
layouttk extract mask.oas metal.gds --top TOP --layer 7/0
layouttk check metal.gds --to oas
```
The first command prints a JSON inventory. The second keeps the selected layer and both cell definitions. The third checks that the GDSII result can be written as OASIS. Add `--json` to `check` for individual diagnostics.
## Look up a familiar operation
| Create a library and choose its grid | `kdb.Layout()`, `ly.dbu = 0.001` (µm) | `GdsLibrary(unit=1e-6, precision=1e-9)` | `Layout(dbu_nm=1)` |
| Define a cell | `ly.create_cell("PAD")` | `lib.new_cell("PAD")` | `mask.add_cell("PAD")` |
| Address a drawing layer | `ly.layer(7, 0)` returns an internal index | `layer=7, datatype=0` | `Layer(7, 0)` is the file pair |
| Draw the tutorial pad | `shapes(i).insert(kdb.Box(0, 0, 1000, 500))` on a cell | `cell.add(gdspy.Rectangle((0, 0), (1, 0.5), layer=7))` at the units above | `pad.add_region(Region.box((0, 0), (1000, 500)), layer=metal)` |
| Place a cell | `CellInstArray` and `Trans` | `CellReference` | `top.add_reference(pad, (2000, 3000))` |
| Get recursive geometry | `kdb.Region(cell.begin_shapes_rec(i))` | `cell.get_polygons(by_spec=True)` | `top.region(metal)` for a union or `hierarchy.polygons` for individual contours |
| Open and save a mask | `ly.read(path)`, `ly.write(path)` | `GdsLibrary(infile=...)`, `lib.write_gds(...)` | `Layout.read(path)`, `mask.write(path)` |
The KLayout column uses integer database coordinates. gdspy drawing arguments use the library's user unit. A coordinate of `1` in the gdspy example corresponds to `1000` DBU here. For original usage and concepts see the [KLayout database guide](https://www.klayout.de/doc/programming/database_api.html) and [gdspy getting started](https://gdspy.readthedocs.io/en/stable/gettingstarted.html).
## Choose your next operation
| Read an existing mask | `Layout.read(path, top="TOP", layers=[metal])` | Get the actual names and layer pairs from an inventory first. Selected definitions can be empty. |
| Draw separate shapes and transform them | `Polygon`, `PolygonSet`, `ellipse`, `elliptic_sector`, `fillet` | Drafts can carry fractional coordinates until insertion. |
| Draw a route or waveguide | `FlexPath` for a polyline, `RobustPath` for variable profiles, `Curve` for a centreline | State the sampling tolerance in DBU. Inspect the outlines when bends or width vary. |
| Compute Boolean geometry | `a \| b`, `a & b`, `a - b`, `a ^ b` on `Region` | Region area measures unioned material. Use `hierarchy.area_by_layer` for the sum over individual shapes. |
| Attach labels or properties | `Layout.add_text`, `Property.gds`, `Property.named`, `Cell.set_properties` | TEXT marks a location. Use `add_polygon_text` for glyphs that must become mask material. Choose the property owner explicitly. |
| Replicate, merge or rename cells | `Cell.add_reference`, `Layout.set_repetition`, `hierarchy.import_cells`, `hierarchy.rename_cell` | Inspect name mappings and grid conversion when importing. Offsets are in the parent's coordinate system for placements. |
| Inspect changes or cut a window | `analysis.compare_layouts`, `query_window`, `clip_windows` | A query selects complete intersecting contours. A clip trims them at the boundary. |
| Exchange memory or arrays | `Layout.to_bytes`, `Layout.from_bytes`, `PolygonBatch` | Pass the file format when producing bytes. Packed polygon data needs vertices and ring boundaries. |
| See or share a result | `write_viewer` for offline HTML, `write_svg` or `svg_fragment` for a drawing | Choose a cell and control hierarchy depth on large designs. |
| Work with GDSII records directly | `GdsWriter`, `cell_bytes`, `element_bytes`, `get_gds_units`, `gdsii_hash` | Use record hashes for content checks and per-layer XOR for filled geometry. |
| Use Rust | `read`, `ReadOptions`, `Layout::retain_tree`, `check_write`, `write` | The Rust API and CLI use the same readers and encoders. [Rust API reference](https://docs.rs/layouttk/0.3.0/layouttk/) |
## Troubleshooting a first script
- **An extraction looks empty.** Inspect `top_cells` and `layers` before selecting. `(7, 0)` and `(7, 1)` select different records. Ask the right parent for a recursive region. Text labels carry no filled area.
- **The drawing is a thousand times too large or small.** Print `dbu_meters`. Convert integer coordinates to µm by multiplying by `dbu_meters * 1e6`. To change grids while preserving physical size, call `resample`. Use `reinterpret_dbu` only if you intend to change the physical scale.
- **A GDSII path fails to export to OASIS.** Inspect `layout.diagnostics("oas")`. A path with an odd full width in DBU needs a half-DBU for its OASIS half-width. For that diagnostic, `write(path, regrid_factor=2)` preserves its physical dimensions. Other diagnostics need their own remedy.
- **A NumPy array or tensor cannot be shared.** Check CPU placement, integer dtype, contiguity and writability. Request `copy=True` when conversion or a device transfer is intended.
- **A selected array is too large to expand.** Inspect references, layer pairs and compact bounds first. A call to `region` needs the placed contours. A layer filter can reduce the retained shapes before that operation.
Look up signatures and ownership in the [Python API reference](https://docs.rs/layouttk/0.3.0/layouttk/guides/python_api/). Continue with [executable Python recipes](https://docs.rs/layouttk/0.3.0/layouttk/guides/python_workflows/), [polygon drafting and rounding](https://docs.rs/layouttk/0.3.0/layouttk/guides/polygon_drafts/) or [file exchange with gdspy, gdstk and KLayout](https://docs.rs/layouttk/0.3.0/layouttk/guides/interoperability/).