KFactory

2.4.6 · active · verified Fri Apr 17

KFactory is a Python library providing a high-level, Pythonic API for KLayout, a popular GDSII layout editor. It simplifies the creation and manipulation of GDSII layouts for chip design, acting as an abstraction layer over KLayout's native Python API (pya). It is often used in conjunction with `gdsfactory` for photonic integrated circuit (PIC) design. The library is actively maintained, with the current version being 2.4.6, and typically releases updates as new KLayout features are exposed or `gdsfactory` integration evolves.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates creating a basic KCell, defining a layer using the `kf.kcl.layer` API, adding a simple rectangle and text, and saving it to a GDSII file. The `c.show()` method is commented out but can be used to launch the KLayout GUI and view the cell directly, assuming KLayout is properly installed and discoverable.

import kfactory as kf
import os

c = kf.KCell('my_first_cell')

# Create a KLayout KCellLayer object for layer (1,0)
# Arguments are (layer_id, datatype_id)
LAYER_1_0 = kf.kcl.layer(1, 0)

# Add a rectangle to the cell on LAYER_1_0
# Coordinates are in nanometers (1000 = 1um, 2000 = 2um)
c.create_rectangle(LAYER_1_0, 1000, 2000)

# You can also add text
c.create_text("Hello KFactory!", LAYER_1_0, 1000)

# Save the cell to a GDSII file
output_gds_path = os.path.join(os.getcwd(), 'my_first_cell.gds')
c.write(output_gds_path)
print(f"GDSII file saved to: {output_gds_path}")

# To visualize, KFactory can launch KLayout if installed and configured
# Uncomment the line below to open the cell in KLayout GUI
# c.show() 

view raw JSON →