GDSII Tool Kit (gdstk)
gdstk is a C++/Python library designed for the creation and manipulation of GDSII and OASIS files, commonly used in microelectronics design. It serves as a modern successor to the Gdspy library, offering key features such as Boolean operations, polygon offsetting, and efficient point-in-polygon queries. The current version is 1.0.0, with an irregular release cadence, focusing on robust geometric operations for CAD layouts. [1, 3, 7]
Common errors
-
TypeError: 'builtin_function_or_method' object is not subscriptable
cause Attempting to access elements of a GDSII/OASIS library (e.g., cells) using square brackets directly on the `Library.cells` attribute, which is a list, not a dictionary. `lib.cells` returns a list of Cell objects. [5]fixTo access cells by name, convert `lib.cells` to a dictionary or iterate through the list. Example: `cells_by_name = {c.name: c for c in lib.cells}; my_cell = cells_by_name['CELL_NAME']` or `for cell in lib.cells: if cell.name == 'CELL_NAME': my_cell = cell; break`. -
gdstk.gdstk.GdsError: Invalid GDSII file or corrupted stream.
cause The GDSII file being read is either malformed, corrupted, or not a valid GDSII format, preventing gdstk from parsing it correctly.fixEnsure the GDSII file is valid and not corrupted. Try opening it with a different GDSII viewer (e.g., KLayout) to confirm its integrity. If generated by another tool, check that tool's output settings. -
ValueError: Layer or datatype must be a non-negative integer.
cause Layer or datatype arguments to geometric functions (e.g., `gdstk.rectangle`, `gdstk.Polygon`) were provided with `None` or a non-integer value.fixAlways provide non-negative integer values for `layer` and `datatype` parameters when creating GDSII elements. For example, `gdstk.rectangle((0,0), (1,1), layer=0, datatype=0)`.
Warnings
- gotcha Older GDSII formats have a hard limit of 199 vertices per polygon. While gdstk supports modern GDSII which often ignores this, generated files for older systems might fail to open or render incorrectly if this limit is exceeded. [6]
- gotcha GDSII files only support 'weakly simple' polygons (segments can intersect but not cross). Complex shapes with holes or self-intersecting boundaries are not directly supported as GDSII paths and might be stored as polygonal objects, losing path information. [6]
- gotcha Performing boolean operations (`gdstk.boolean`) inside a Python loop for many individual polygons can be significantly slower than expected compared to batch processing or native CAD tools. This is a common performance bottleneck. [11]
- breaking Some users have reported issues with OASIS files losing paths or containing missing shapes after being read and then written back by gdstk, potentially leading to DRC errors in production flows. [12]
Install
-
pip install gdstk
Imports
- gdstk
import gdstk
- gdstk.Cell
cell = gdstk.Cell('my_cell')
Quickstart
import gdstk
# Create a new library
lib = gdstk.Library()
# Create a new cell and add it to the library
cell = lib.new_cell('FIRST')
# Create a rectangle and add it to the cell
rectangle = gdstk.rectangle((0, 0), (2, 2), layer=1, datatype=0)
cell.add(rectangle)
# Create a polygon from vertices and add it to the cell
polygon_points = [(3, 0), (5, 2), (5, 0)]
polygon = gdstk.Polygon(polygon_points, layer=2)
cell.add(polygon)
# Save the library to a GDSII file
lib.write_gds('first.gds')
print("Generated first.gds with a rectangle and a polygon.")