# Getting Started

GDSII files contain a hierarchical representation of any polygonal geometry. They are mainly used in the microelectronics industry for the design of mask layouts, but are also employed in other areas.

Because it is a hierarchical format, repeated structures, such as identical transistors, can be defined once and referenced multiple times in the layout, reducing the file size.

There is one important limitation in the GDSII format: it only supports weakly simple polygons, that is, polygons whose segments are allowed to intersect, but not cross.

In particular, curves and shapes with holes are not directly supported. Holes can be defined, nonetheless, by connecting their boundary to the boundary of the enclosing shape. In the case of curves, they must be approximated by a polygon. The number of points in the polygonal approximation can be increased to better approximate the original curve up to some acceptable error.

The original GDSII format limits the number of vertices in a polygon to 199. This limit seems arbitrary, as the maximal number of vertices that can be stored in a GDSII record is 8190. Nonetheless, most modern software disregard both limits and allow an arbitrary number of points per polygon. Gdspy follows the modern version of GDSII, but this is an important issue to keep in mind if the generated file is to be used in older systems.

The units used to represent shapes in the GDSII format are defined by the user. The default unit in gdspy is 1 µm (10⁻⁶ m), but that can be easily changed by the user.

## First GDSII

Let’s create our first GDSII file:

    import gdspy

    # The GDSII file is called a library, which contains multiple cells.
    lib = gdspy.GdsLibrary()

    # Geometry must be placed in cells.
    cell = lib.new_cell('FIRST')

    # Create the geometry (a single rectangle) and add it to the cell.
    rect = gdspy.Rectangle((0, 0), (2, 1))
    cell.add(rect)

    # Save the library in a file called 'first.gds'.
    lib.write_gds('first.gds')

    # Optionally, save an image of the cell as SVG.
    cell.write_svg('first.svg')

    # Display all cells using the internal viewer.
    gdspy.LayoutViewer()

After importing the gdspy module, we create a library lib to hold the design. Then a `gdspy.Cell` is created and the rectangle is added to the cell. All shapes in the GDSII format exist inside cells. A cell can be imagined as a piece of paper where the layout will be defined. Later, the cells can be used to create a hierarchy of geometries, ass we’ll see in References.

Finally, the whole structure is saved in a file called “first.gds” in the current directory. By default, all created cells are included in this operation.

The GDSII file can be opened in a number of viewers and editors, such as KLayout. Alternatively, gdspy includes a simple viewer that can also be used: `gdspy.LayoutViewer`.

## Polygons

General polygons can be defined by an ordered list of vertices. The orientation of the vertices (clockwise/counter-clockwise) is not important.

    # Create a polygon from a list of vertices
    points = [(0, 0), (2, 2), (2, 6), (-6, 6), (-6, -6), (-4, -4), (-4, 4), (0, 4)]
    poly = gdspy.Polygon(points)

### Holes

As mentioned in Getting Started, holes have to be connected to the outer boundary of the polygon, as in the following example:

    # Manually connect the hole to the outer boundary
    cutout = gdspy.Polygon(
        [(0, 0), (5, 0), (5, 5), (0, 5), (0, 0), (2, 2), (2, 3), (3, 3), (3, 2), (2, 2)]
    )

### Circles

The `gdspy.Round` class creates circles, ellipses, doughnuts, arcs and slices. In all cases, the arguments tolerance or number_of_points will control the number of vertices used to approximate the curved shapes.

If the number of vertices in the polygon is larger than max_points (199 by default), it will be fractured in many smaller polygons with at most max_points vertices each.

    # Circle centered at (0, 0), with radius 2 and tolerance 0.1
    circle = gdspy.Round((0, 0), 2, tolerance=0.01)

    # To create an ellipse, simply pass a list with 2 radii.
    # Because the tolerance is small (resulting a large number of
    # vertices), the ellipse is fractured in 2 polygons.
    ellipse = gdspy.Round((4, 0), [1, 2], tolerance=1e-4)

    # Circular arc example
    arc = gdspy.Round(
        (2, 4),
        2,
        inner_radius=1,
        initial_angle=-0.2 * numpy.pi,
        final_angle=1.2 * numpy.pi,
        tolerance=0.01,
    )

### Curves

Constructing complex polygons by manually listing all vertices in `gdspy.Polygon` can be challenging. The class `gdspy.Curve` can be used to facilitate the creation of polygons by drawing their shapes step-by-step. It uses a syntax similar to the SVG path specification.

A short summary of the available methods is presented below:

Method | Primitive
--- | ---
L/l | Line segments
H/h | Horizontal line segments
V/v | Vertical line segments
C/c | Cubic Bezier curve
S/s | Smooth cubic Bezier curve
Q/q | Quadratic Bezier curve
T/t | Smooth quadratic Bezier curve
B/b | General degree Bezier curve
I/i | Smooth interpolating curve
arc | Elliptical arc

The uppercase version of the methods considers that all coordinates are absolute, whereas the lowercase considers that they are relative to the current end point of the curve. Except for `gdspy.Curve.I()`, `gdspy.Curve.i()` and `gdspy.Curve.arc()`, they accept variable numbers of arguments that are used as coordinates to construct the primitive.

    # Construct a curve made of a sequence of line segments
    c1 = gdspy.Curve(0, 0).L(1, 0, 2, 1, 2, 2, 0, 2)
    p1 = gdspy.Polygon(c1.get_points())

    # Construct another curve using relative coordinates
    c2 = gdspy.Curve(3, 1).l(1, 0, 2, 1, 2, 2, 0, 2)
    p2 = gdspy.Polygon(c2.get_points())

Coordinate pairs can be given as a complex number: real and imaginary parts are used as x and y coordinates, respectively. That is useful to define points in polar coordinates.

Elliptical arcs have syntax similar to `gdspy.Round`, but they allow for an extra rotation of the major axis of the ellipse.

    # Use complex numbers to facilitate writing polar coordinates
    c3 = gdspy.Curve(0, 2).l(4 * numpy.exp(1j * numpy.pi / 6))
    # Elliptical arcs have syntax similar to gdspy.Round
    c3.arc((4, 2), 0.5 * numpy.pi, -0.5 * numpy.pi)
    p3 = gdspy.Polygon(c3.get_points())

Other curves can be constructed as cubic, quadratic and general-degree Bezier curves. Additionally, a smooth interpolating curve can be calculated with the methods `gdspy.Curve.I()` and `gdspy.Curve.i()`, which have a number of arguments to control the shape of the curve.

    # Cubic Bezier curves can be easily created with C and c
    c4 = gdspy.Curve(0, 0).c(1, 0, 1, 1, 2, 1)
    # Smooth continuation with S or s
    c4.s(1, 1, 0, 1).S(numpy.exp(1j * numpy.pi / 6), 0, 0)
    p4 = gdspy.Polygon(c4.get_points())

    # Similarly for quadratic Bezier curves
    c5 = gdspy.Curve(5, 3).Q(3, 2, 3, 0, 5, 0, 4.5, 1).T(5, 3)
    p5 = gdspy.Polygon(c5.get_points())

    # Smooth interpolating curves can be built using I or i, including
    # closed shapes
    c6 = gdspy.Curve(0, 3).i([(1, 0), (2, 0), (1, -1)], cycle=True)
    p6 = gdspy.Polygon(c6.get_points())

### Transformations

All polygons can be transformed trough `gdspy.PolygonSet.translate()`, `gdspy.PolygonSet.rotate()`, `gdspy.PolygonSet.scale()`, and `gdspy.PolygonSet.mirror()`. The transformations are applied in-place, i.e., no polygons are created.

    poly = gdspy.Rectangle((-2, -2), (2, 2))
    poly.rotate(numpy.pi / 4)
    poly.scale(1, 0.5)

### Layer and Datatype

All shapes in the GDSII format are tagged with 2 properties: layer and datatype (or texttype in the case of `gdspy.Label`). They are always 0 by default, but can be any integer in the range from 0 to 255.

These properties have no predefined meaning. It is up to the system using the GDSII file to chose with to do with those tags. For example, in the CMOS fabrication process, each layer could represent a different lithography level.

In the example below, a single file stores different fabrication masks in separate layer and datatype configurations.

    # Layer/datatype definitions for each step in the fabrication
    ld_fulletch = {"layer": 1, "datatype": 3}
    ld_partetch = {"layer": 2, "datatype": 3}
    ld_liftoff = {"layer": 0, "datatype": 7}

    p1 = gdspy.Rectangle((-3, -3), (3, 3), **ld_fulletch)
    p2 = gdspy.Rectangle((-5, -3), (-3, 3), **ld_partetch)
    p3 = gdspy.Rectangle((5, -3), (3, 3), **ld_partetch)
    p4 = gdspy.Round((0, 0), 2.5, number_of_points=6, **ld_liftoff)

## References

References give the GDSII format its hierarchical features. They work by reusing a cell content in another cell (without actually copying the whole geometry). As a simplistic example, imagine the we are designing a simple electronic circuit that uses hundreds of transistors, but they all have the same shape. We can draw the transistor just once and reference it throughout the circuit, rotating or mirroring each instance as necessary.

Besides creating single references with `gdspy.CellReference`, it is possible to create full 2D arrays with a single entity using `gdspy.CellArray`. Both are exemplified below.

    # Create a cell with a component that is used repeatedly
    contact = gdspy.Cell("CONTACT")
    contact.add([p1, p2, p3, p4])

    # Create a cell with the complete device
    device = gdspy.Cell("DEVICE")
    device.add(cutout)
    # Add 2 references to the component changing size and orientation
    ref1 = gdspy.CellReference(contact, (3.5, 1), magnification=0.25)
    ref2 = gdspy.CellReference(contact, (1, 3.5), magnification=0.25, rotation=90)
    device.add([ref1, ref2])

    # The final layout has several repetitions of the complete device
    main = gdspy.Cell("MAIN")
    main.add(gdspy.CellArray(device, 3, 2, (6, 7)))

## Paths

Besides polygons, the GDSII format defines paths, witch are polygonal chains with associated width and end caps. The width is a single number, constant throughout the path, and the end caps can be flush, round, or extended by a custom distance.

There is no specification for the joins between adjacent segments, so it is up to the system using the GDSII file to specify those. Usually the joins are straight extensions of the path boundaries up to some beveling limit. Gdspy also uses this specification for the joins.

It is possible to circumvent all of the above limitations within gdspy by storing paths as polygons in the GDSII file. The disadvantage of this solution is that other software will not be able to edit the geometry as paths, since that information is lost.

The construction of paths (either GDSII paths or polygonal paths) in gdspy is quite rich. There are 3 classes that can be used depending on the requirements of the desired path.

### Polygonal-Only Paths

The class `gdspy.Path` is designed to allow the creation of path-like polygons in a piece-wise manner. It is the most computationally efficient class between the three because it does not calculate joins. That means the user is responsible for designing the joins. The paths can end up with discontinuities if care is not taken when creating them.

    # Start a path at (0, 0) with width 1
    path1 = gdspy.Path(1, (0, 0))

    # Add a segment to the path goin in the '+y' direction
    path1.segment(4, "+y")

    # Further segments or turns will folow the current path direction
    # to ensure continuity
    path1.turn(2, "r")
    path1.segment(1)
    path1.turn(3, "rr")

Just as with Circles, all curved geometry is approximated by line segments. The number of segments is similarly controlled by a tolerance or a number_of_points argument. Curves also include fracturing to limit the number of points in each polygon.

More complex paths can be constructed with the methods `gdspy.Path.bezier()`, `gdspy.Path.smooth()`, and `gdspy.Path.parametric()`. The example below demonstrates a couple of possibilities.

    path2 = gdspy.Path(0.5, (0, 0))

    # Start the path with a smooth Bezier S-curve
    path2.bezier([(0, 5), (5, 5), (5, 10)])

    # We want to add a spiral curve to the path.  The spiral is defined
    # as a parametric curve.  We make sure spiral(0) = (0, 0) so that
    # the path is continuous.
    def spiral(u):
        r = 4 - 3 * u
        theta = 5 * u * numpy.pi
        x = r * numpy.cos(theta) - 4
        y = r * numpy.sin(theta)
        return (x, y)

    # It is recommended to also define the derivative of the parametric
    # curve, otherwise this derivative must be calculated nummerically.
    # The derivative is used to define the side boundaries of the path,
    # so, in this case, to ensure continuity with the existing S-curve,
    # we make sure the the direction at the start of the spiral is
    # pointing exactly upwards, as if is radius were constant.
    # Additionally, the exact magnitude of the derivative is not
    # important; gdspy only uses its direction.
    def dspiral_dt(u):
        theta = 5 * u * numpy.pi
        dx_dt = -numpy.sin(theta)
        dy_dt = numpy.cos(theta)
        return (dx_dt, dy_dt)

    # Add the parametric spiral to the path
    path2.parametric(spiral, dspiral_dt)

The width of the path does not have to be constant. Each path component can linearly taper the width of the path by using the final_width argument. In the case of a parametric curve, more complex width changes can be created by setting final_width to a function.

Finally, parallel paths can be created simultaneously with the help of arguments number_of_paths, distance, and final_distance.

    # Start 3 parallel paths with center-to-center distance of 1.5
    path3 = gdspy.Path(0.1, (-5.5, 3), number_of_paths=3, distance=1.5)

    # Add a segment tapering the widths up to 0.5
    path3.segment(2, "-y", final_width=0.5)

    # Add a bezier curve decreasing the distance between paths to 0.75
    path3.bezier([(0, -2), (1, -3), (3, -3)], final_distance=0.75)

    # Add a parametric section to modulate the width with a sinusoidal
    # shape.  Note that the algorithm that determines the number of
    # evaluations of the parametric curve does not take the width into
    # consideration, so we have to manually increase this parameter.
    path3.parametric(
        lambda u: (5 * u, 0),
        lambda u: (1, 0),
        final_width=lambda u: 0.4 + 0.1 * numpy.cos(10 * numpy.pi * u),
        number_of_evaluations=256,
    )

    # Add a circular turn and a final tapering segment.
    path3.turn(3, "l")
    path3.segment(2, final_width=1, final_distance=1.5)

### Flexible Paths

Although very efficient, `gdspy.Path` is limited in the type of path it can provide. For example, if we simply want a path going through a sequence of points, we need a class that can correctly compute the joins between segments. That’s one of the advantages of class `gdspy.FlexPath`. Other path construction methods are similar to those in `gdspy.Path`.

A few features of `gdspy.FlexPath` are:

- paths can be stored as proper GDSII paths;

- end caps and joins can be specified by the user;

- each parallel path can have a different width;

- spacing between parallel paths is arbitrary; the user specifies the offset of each path individually.

    # Path defined by a sequence of points and stored as a GDSII path
    sp1 = gdspy.FlexPath(
        [(0, 0), (3, 0), (3, 2), (5, 3), (3, 4), (0, 4)], 1, gdsii_path=True
    )

    # Other construction methods can still be used
    sp1.smooth([(0, 2), (2, 2), (4, 3), (5, 1)], relative=True)

    # Multiple parallel paths separated by 0.5 with different widths,
    # end caps, and joins.  Because of the join specification, they
    # cannot be stared as GDSII paths, only as polygons.
    sp2 = gdspy.FlexPath(
        [(12, 0), (8, 0), (8, 3), (10, 2)],
        [0.3, 0.2, 0.4],
        0.5,
        ends=["extended", "flush", "round"],
        corners=["bevel", "miter", "round"],
    )
    sp2.arc(2, -0.5 * numpy.pi, 0.5 * numpy.pi)
    sp2.arc(1, 0.5 * numpy.pi, 1.5 * numpy.pi)

The following example shows other features, such as width tapering, arbitrary offsets, and custom joins and end caps.

    # Path corners and end caps can be custom functions.
    # This corner function creates 'broken' joins.
    def broken(p0, v0, p1, v1, p2, w):
        # Calculate intersection point p between lines defined by
        # p0 + u0 * v0 (for all u0) and p1 + u1 * v1 (for all u1)
        den = v1[1] * v0[0] - v1[0] * v0[1]
        lim = 1e-12 * (v0[0] ** 2 + v0[1] ** 2) * (v1[0] ** 2 + v1[1] ** 2)
        if den ** 2 < lim:
            # Lines are parallel: use mid-point
            u0 = u1 = 0
            p = 0.5 * (p0 + p1)
        else:
            dx = p1[0] - p0[0]
            dy = p1[1] - p0[1]
            u0 = (v1[1] * dx - v1[0] * dy) / den
            u1 = (v0[1] * dx - v0[0] * dy) / den
            p = 0.5 * (p0 + v0 * u0 + p1 + v1 * u1)
        if u0 <= 0 and u1 >= 0:
            # Inner corner
            return [p]
        # Outer corner
        return [p0, p2, p1]

    # This end cap function creates pointy caps.
    def pointy(p0, v0, p1, v1):
        r = 0.5 * numpy.sqrt(numpy.sum((p0 - p1) ** 2))
        v0 /= numpy.sqrt(numpy.sum(v0 ** 2))
        v1 /= numpy.sqrt(numpy.sum(v1 ** 2))
        return [p0, 0.5 * (p0 + p1) + 0.5 * (v0 - v1) * r, p1]

    # Paths with arbitrary offsets from the center and multiple layers.
    sp3 = gdspy.FlexPath(
        [(0, 0), (0, 1)],
        [0.1, 0.3, 0.5],
        offset=[-0.2, 0, 0.4],
        layer=[0, 1, 2],
        corners=broken,
        ends=pointy,
    )
    sp3.segment((3, 3), offset=[-0.5, -0.1, 0.5])
    sp3.segment((4, 1), width=[0.2, 0.2, 0.2], offset=[-0.2, 0, 0.2])
    sp3.segment((0, -1), relative=True)

The corner type ‘circular bend’ (together with the bend_radius argument) can be used to automatically curve the path. This feature is used in Example: Integrated Photonics.

    # Path created with automatic bends of radius 5
    points = [(0, 0), (0, 10), (20, 0), (18, 15), (8, 15)]
    sp4 = gdspy.FlexPath(
        points, 0.5, corners="circular bend", bend_radius=5, gdsii_path=True
    )

    # Same path, generated with natural corners, for comparison
    sp5 = gdspy.FlexPath(points, 0.5, layer=1, gdsii_path=True)

### Robust Paths

In some situations, `gdspy.FlexPath` is unable to properly calculate all the joins. This often happens when the width or offset of the path is relatively large with respect to the length of the segments being joined. Curves that join other curves or segments at sharp angles are an example of such situation.

The class `gdspy.RobustPath` can be used in such scenarios where robustness is more important than efficiency due to sharp corners or large offsets in the paths. The drawbacks of using `gdspy.RobustPath` are the loss in computation efficiency (compared to the other 2 classes) and the impossibility of specifying corner shapes. The advantages are, as mentioned earlier, more robustness when generating the final geometry, and freedom to use custom functions to parameterize the widths or offsets of the paths in any construction method.

    # Create 4 parallel paths in different layers
    lp = gdspy.RobustPath(
        (50, 0),
        [2, 0.5, 1, 1],
        [0, 0, -1, 1],
        ends=["extended", "round", "flush", "flush"],
        layer=[0, 2, 1, 1],
    )
    lp.segment((45, 0))
    lp.segment(
        (5, 0),
        width=[lambda u: 2 + 16 * u * (1 - u), 0.5, 1, 1],
        offset=[
            0,
            lambda u: 8 * u * (1 - u) * numpy.cos(12 * numpy.pi * u),
            lambda u: -1 - 8 * u * (1 - u),
            lambda u: 1 + 8 * u * (1 - u),
        ],
    )
    lp.segment((0, 0))
    lp.smooth(
        [(5, 10)],
        angles=[0.5 * numpy.pi, 0],
        width=0.5,
        offset=[-0.25, 0.25, -0.75, 0.75],
    )
    lp.parametric(
        lambda u: numpy.array((45 * u, 4 * numpy.sin(6 * numpy.pi * u))),
        offset=[
            lambda u: -0.25 * numpy.cos(24 * numpy.pi * u),
            lambda u: 0.25 * numpy.cos(24 * numpy.pi * u),
            -0.75,
            0.75,
        ],
    )

Note that, analogously to `gdspy.FlexPath`, `gdspy.RobustPath` can be stored as a GDSII path as long as its width is kept constant.

## Text

In the context of a GDSII file, text is supported in the form of labels, which are ASCII annotations placed somewhere in the geometry of a given cell. Similar to polygons, labels are tagged with layer and texttype values (texttype is the label equivalent of the polygon datatype). They are supported by the class `gdspy.Label`.

Additionally, gdspy offers the possibility of creating text as polygons to be included with the geometry. The class `gdspy.Text` creates polygonal text that can be used in the same way as any other polygons in gdspy. The font used to render the characters contains only horizontal and vertical edges, which is important for some laser writing systems.

    # Label anchored at (1, 3) by its north-west corner
    label = gdspy.Label("Sample label", (1, 3), "nw")

    # Horizontal text with height 2.25
    htext = gdspy.Text("12345", 2.25, (0.25, 6))

    # Vertical text with height 1.5
    vtext = gdspy.Text("ABC", 1.5, (10.5, 4), horizontal=False)

    rect = gdspy.Rectangle((0, 0), (10, 6), layer=10)

## Geometry Operations

Gdspy offers a number of functions and methods to modify existing geometry. The most useful operations include `gdspy.boolean()`, `gdspy.slice()`, `gdspy.offset()`, and `gdspy.PolygonSet.fillet()`.

### Boolean Operations

Boolean operations (`gdspy.boolean()`) can be performed on polygons, paths and whole cells. Four operations are defined: union (‘or’), intersection (‘and’), subtraction (‘not’), and symmetric subtraction (‘xor’).

They can be computationally expensive, so it is usually advisable to avoid using boolean operations whenever possible. If they are necessary, keeping the number of vertices is all polygons as low as possible also helps.

    # Create some text
    text = gdspy.Text("GDSPY", 4, (0, 0))
    # Create a rectangle extending the text's bounding box by 1
    bb = numpy.array(text.get_bounding_box())
    rect = gdspy.Rectangle(bb[0] - 1, bb[1] + 1)

    # Subtract the text from the rectangle
    inv = gdspy.boolean(rect, text, "not")

### Slice Operation

As the name indicates, a slice operation subdivides a set of polygons along horizontal or vertical cut lines.

In a few cases, a boolean operation can be substituted by one or more slice operations. Because `gdspy.slice()` is ususally much simpler than `gdspy.boolean()`, it is a good idea to use the former if possible.

    ring1 = gdspy.Round((-6, 0), 6, inner_radius=4)
    ring2 = gdspy.Round((0, 0), 6, inner_radius=4)
    ring3 = gdspy.Round((6, 0), 6, inner_radius=4)

    # Slice the first ring across x=-3, the second ring across x=-3
    # and x=3, and the third ring across x=3
    slices1 = gdspy.slice(ring1, -3, axis=0)
    slices2 = gdspy.slice(ring2, [-3, 3], axis=0)
    slices3 = gdspy.slice(ring3, 3, axis=0)

    slices = gdspy.Cell("SLICES")

    # Keep only the left side of slices1, the center part of slices2
    # and the right side of slices3
    slices.add(slices1[0])
    slices.add(slices2[1])
    slices.add(slices3[1])

### Offset Operation

The function `gdspy.offset()` expands or contracts polygons by a fixed amount. It can operate on individual polygons or sets of them, in which case it may make sense to use the argument join_first to operate on the whole geometry as if a boolean ‘or’ was executed beforehand.

    rect1 = gdspy.Rectangle((-4, -4), (1, 1))
    rect2 = gdspy.Rectangle((-1, -1), (4, 4))

    # Offset both polygons
    # Because we join them first, a single polygon is created.
    outer = gdspy.offset([rect1, rect2], 0.5, join_first=True, layer=1)

### Fillet Operation

The method `gdspy.PolygonSet.fillet()` can be used to round polygon corners. It doesn’t have a join_first argument as `gdspy.offset()`, so if it will be used on a polygon, that polygon should probably not be fractured.

    multi_path = gdspy.Path(2, (-3, -2))
    multi_path.segment(4, "+x")
    multi_path.turn(2, "l").turn(2, "r")
    multi_path.segment(4)

    # Create a copy with joined polygons and no fracturing
    joined = gdspy.boolean(multi_path, None, "or", max_points=0)
    joined.translate(0, -5)

    # Fillet applied to each polygon in the path
    multi_path.fillet(0.5)

    # Fillet applied to the joined copy
    joined.fillet(0.5)

## GDSII Library

All the information used to create a GDSII file is kept within an instance of `GdsLibrary`. Besides all the geometric and hierarchical information, this class also holds a name and the units for all entities. The name can be any ASCII string. It is simply stored in the GDSII file and has no other purpose in gdspy. The units require some attention because they can impact the resolution of the polygons in the library when written to a file.

### Units in GDSII

Two values are defined: unit and precision. The value of unit defines the unit size—in meters—for all entities in the library. For example, if `unit = 1e-6` (10⁻⁶ m, the default value), a vertex at (1, 2) should be interpreted as a vertex in real world position (1 × 10⁻⁶ m, 2 × 10⁻⁶ m). If unit changes to 0.001, then that same vertex would be located (in real world coordinates) at (0.001 m, 0.002 m), or (1 mm, 2 mm).

The value of precision has to do with the type used to store coordinates in the GDSII file: signed 4-byte integers. Because of that, a finer coordinate grid than 1 unit is usually desired to define coordinates. That grid is defined, in meters, by precision, which defaults to `1e-9` (10⁻⁹ m). When the GDSII file is written, all vertices are snapped to the grid defined by precision. For example, for the default values of unit and precision, a vertex at (1.0512, 0.0001) represents real world coordinates (1.0512 × 10⁻⁶ m, 0.0001 × 10⁻⁶ m), or (1051.2 × 10⁻⁹ m, 0.1 × 10⁻⁹ m), which will be rounded to integers: (1051 × 10⁻⁹ m, 0 × 10⁻⁹ m), or (1.051 × 10⁻⁶ m, 0 × 10⁻⁶ m). The actual coordinate values written in the GDSII file will be the integers (1051, 0). By reducing the value of precision from 10⁻⁹ m to 10⁻¹² m, for example, the coordinates will have 3 additional decimal places of precision, so the stored values would be (1051200, 100).

The downside of increasing the number of decimal places in the file is reducing the range of coordinates that can be stored (in real world units). That is because the range of coordinate values that can be written in the file are [-(2³²); 2³¹ - 1] = [-2,147,483,648; 2,147,483,647]. For the default precsision, this range is [-2.147483648 m; 2.147483647 m]. If precision is set to 10⁻¹² m, the same range is reduced by 1000 times: [-2.147483648 mm; 2.147483647 mm].

### Saving a GDSII File

To save a GDSII file, simply use the `gdspy.GdsLibrary.write_gds()` method, as in the First GDSII.

An SVG image from a specific cell can also be exported through `gdspy.Cell.write_svg()`, which was also demonstrated in First GDSII.

### Loading a GDSII File

To load an existing GDSII file (or to work simultaneously with multiple libraries), a new instance of `GdsLibrary` can be created or an existing one can be used:

    # Load a GDSII file into a new library
    gdsii = gdspy.GdsLibrary(infile='filename.gds')

    # Use a previously-created library to load the file contents into
    existing_library.read_gds('filename.gds')

In either case, care must be taken to merge the units from the library and the file, which is controlled by the argument units in `gdspy.GdsLibrary.read_gds()` (keyword argument in `gdspy.GdsLibrary`).

Access to the cells in the loaded library is provided through the dictionary `gdspy.GdsLibrary.cell_dict` (cells indexed by name). The method `gdspy.GdsLibrary.top_level()` can be used to find the top-level cells in the library (cells on the top of the hierarchy, i.e., cell that are not referenced by any other cells).

## Examples

### Integrated Photonics

This example demonstrates the use of gdspy primitives to create more complex structures.

These structures are commonly used in the field of integrated photonics.

`photonics.py`

`photonics.gds`

      1######################################################################
      2#                                                                    #
      3#  Copyright 2009 Lucas Heitzmann Gabrielli.                         #
      4#  This file is part of gdspy, distributed under the terms of the    #
      5#  Boost Software License - Version 1.0.  See the accompanying       #
      6#  LICENSE file or <http://www.boost.org/LICENSE_1_0.txt>            #
      7#                                                                    #
      8######################################################################
      9
     10import numpy
     11import gdspy
     12
     13
     14def grating(
     15    period,
     16    number_of_teeth,
     17    fill_frac,
     18    width,
     19    position,
     20    direction,
     21    lda=1,
     22    sin_theta=0,
     23    focus_distance=-1,
     24    focus_width=-1,
     25    tolerance=0.001,
     26    layer=0,
     27    datatype=0,
     28):
     29    """
     30    Straight or focusing grating.
     31
     32    period          : grating period
     33    number_of_teeth : number of teeth in the grating
     34    fill_frac       : filling fraction of the teeth (w.r.t. the period)
     35    width           : width of the grating
     36    position        : grating position (feed point)
     37    direction       : one of {'+x', '-x', '+y', '-y'}
     38    lda             : free-space wavelength
     39    sin_theta       : sine of incidence angle
     40    focus_distance  : focus distance (negative for straight grating)
     41    focus_width     : if non-negative, the focusing area is included in
     42                      the result (usually for negative resists) and this
     43                      is the width of the waveguide connecting to the
     44                      grating
     45    tolerance       : same as in `path.parametric`
     46    layer           : GDSII layer number
     47    datatype        : GDSII datatype number
     48
     49    Return `PolygonSet`
     50    """
     51    if focus_distance < 0:
     52        p = gdspy.L1Path(
     53            (
     54                position[0] - 0.5 * width,
     55                position[1] + 0.5 * (number_of_teeth - 1 + fill_frac) * period,
     56            ),
     57            "+x",
     58            period * fill_frac,
     59            [width],
     60            [],
     61            number_of_teeth,
     62            period,
     63            layer=layer,
     64            datatype=datatype,
     65        )
     66    else:
     67        neff = lda / float(period) + sin_theta
     68        qmin = int(focus_distance / float(period) + 0.5)
     69        p = gdspy.Path(period * fill_frac, position)
     70        c3 = neff ** 2 - sin_theta ** 2
     71        w = 0.5 * width
     72        for q in range(qmin, qmin + number_of_teeth):
     73            c1 = q * lda * sin_theta
     74            c2 = (q * lda) ** 2
     75            p.parametric(
     76                lambda t: (
     77                    width * t - w,
     78                    (c1 + neff * numpy.sqrt(c2 - c3 * (width * t - w) ** 2)) / c3,
     79                ),
     80                tolerance=tolerance,
     81                max_points=0,
     82                layer=layer,
     83                datatype=datatype,
     84            )
     85            p.x = position[0]
     86            p.y = position[1]
     87        sz = p.polygons[0].shape[0] // 2
     88        if focus_width == 0:
     89            p.polygons[0] = numpy.vstack((p.polygons[0][:sz, :], [position]))
     90        elif focus_width > 0:
     91            p.polygons[0] = numpy.vstack(
     92                (
     93                    p.polygons[0][:sz, :],
     94                    [
     95                        (position[0] + 0.5 * focus_width, position[1]),
     96                        (position[0] - 0.5 * focus_width, position[1]),
     97                    ],
     98                )
     99            )
    100        p.fracture()
    101    if direction == "-x":
    102        return p.rotate(0.5 * numpy.pi, position)
    103    elif direction == "+x":
    104        return p.rotate(-0.5 * numpy.pi, position)
    105    elif direction == "-y":
    106        return p.rotate(numpy.pi, position)
    107    else:
    108        return p
    109
    110
    111if __name__ == "__main__":
    112    # Examples
    113    lib = gdspy.GdsLibrary()
    114
    115    # Negative resist example
    116    width = 0.45
    117    bend_radius = 50.0
    118    ring_radius = 20.0
    119    taper_len = 50.0
    120    input_gap = 150.0
    121    io_gap = 500.0
    122    wg_gap = 20.0
    123    ring_gaps = [0.06 + 0.02 * i for i in range(8)]
    124
    125    ring = lib.new_cell("NRing")
    126    ring.add(
    127        gdspy.Round((ring_radius, 0), ring_radius, ring_radius - width, tolerance=0.001)
    128    )
    129
    130    grat = lib.new_cell("NGrat")
    131    grat.add(
    132        grating(
    133            0.626,
    134            28,
    135            0.5,
    136            19,
    137            (0, 0),
    138            "+y",
    139            1.55,
    140            numpy.sin(numpy.pi * 8 / 180),
    141            21.5,
    142            width,
    143            tolerance=0.001,
    144        )
    145    )
    146
    147    taper = lib.new_cell("NTaper")
    148    taper.add(gdspy.Path(0.12, (0, 0)).segment(taper_len, "+y", final_width=width))
    149
    150    c = lib.new_cell("Negative")
    151    for i, gap in enumerate(ring_gaps):
    152        path = gdspy.FlexPath(
    153            [(input_gap * i, taper_len)],
    154            width=width,
    155            corners="circular bend",
    156            bend_radius=bend_radius,
    157            gdsii_path=True,
    158        )
    159        path.segment((0, 600 - wg_gap * i), relative=True)
    160        path.segment((io_gap, 0), relative=True)
    161        path.segment((0, 300 + wg_gap * i), relative=True)
    162        c.add(path)
    163        c.add(gdspy.CellReference(ring, (input_gap * i + width / 2 + gap, 300)))
    164    c.add(gdspy.CellArray(taper, len(ring_gaps), 1, (input_gap, 0), (0, 0)))
    165    c.add(
    166        gdspy.CellArray(
    167            grat, len(ring_gaps), 1, (input_gap, 0), (io_gap, 900 + taper_len)
    168        )
    169    )
    170
    171    # Positive resist example
    172    width = 0.45
    173    ring_radius = 20.0
    174    big_margin = 10.0
    175    small_margin = 5.0
    176    taper_len = 50.0
    177    bus_len = 400.0
    178    input_gap = 150.0
    179    io_gap = 500.0
    180    wg_gap = 20.0
    181    ring_gaps = [0.06 + 0.02 * i for i in range(8)]
    182
    183    ring_margin = gdspy.Rectangle(
    184        (0, -ring_radius - big_margin),
    185        (2 * ring_radius + big_margin, ring_radius + big_margin),
    186    )
    187    ring_hole = gdspy.Round(
    188        (ring_radius, 0), ring_radius, ring_radius - width, tolerance=0.001
    189    )
    190    ring_bus = gdspy.Path(
    191        small_margin, (0, taper_len), number_of_paths=2, distance=small_margin + width
    192    )
    193    ring_bus.segment(bus_len, "+y")
    194
    195    p = gdspy.Path(
    196        small_margin, (0, 0), number_of_paths=2, distance=small_margin + width
    197    )
    198    p.segment(21.5, "+y", final_distance=small_margin + 19)
    199    grat = lib.new_cell("PGrat")
    200    grat.add(p)
    201    grat.add(
    202        grating(
    203            0.626,
    204            28,
    205            0.5,
    206            19,
    207            (0, 0),
    208            "+y",
    209            1.55,
    210            numpy.sin(numpy.pi * 8 / 180),
    211            21.5,
    212            tolerance=0.001,
    213        )
    214    )
    215
    216    p = gdspy.Path(big_margin, (0, 0), number_of_paths=2, distance=big_margin + 0.12)
    217    p.segment(
    218        taper_len, "+y", final_width=small_margin, final_distance=small_margin + width
    219    )
    220    taper = lib.new_cell("PTaper")
    221    taper.add(p)
    222
    223    c = lib.new_cell("Positive")
    224    for i, gap in enumerate(ring_gaps):
    225        path = gdspy.FlexPath(
    226            [(input_gap * i, taper_len + bus_len)],
    227            width=[small_margin, small_margin],
    228            offset=small_margin + width,
    229            gdsii_path=True,
    230        )
    231        path.segment((0, 600 - bus_len - bend_radius - wg_gap * i), relative=True)
    232        path.turn(bend_radius, "r")
    233        path.segment((io_gap - 2 * bend_radius, 0), relative=True)
    234        path.turn(bend_radius, "l")
    235        path.segment((0, 300 - bend_radius + wg_gap * i), relative=True)
    236        c.add(path)
    237        dx = width / 2 + gap
    238        c.add(
    239            gdspy.boolean(
    240                gdspy.boolean(
    241                    ring_bus, gdspy.copy(ring_margin, dx, 300), "or", precision=1e-4
    242                ),
    243                gdspy.copy(ring_hole, dx, 300),
    244                "not",
    245                precision=1e-4,
    246            ).translate(input_gap * i, 0)
    247        )
    248    c.add(gdspy.CellArray(taper, len(ring_gaps), 1, (input_gap, 0), (0, 0)))
    249    c.add(
    250        gdspy.CellArray(
    251            grat, len(ring_gaps), 1, (input_gap, 0), (io_gap, 900 + taper_len)
    252        )
    253    )
    254
    255    # Save to a gds file and check out the output
    256    lib.write_gds("photonics.gds")
    257    gdspy.LayoutViewer(lib)

### Using System Fonts

This example uses matplotlib to render text using any typeface present in the system. The glyph paths are then transformed into polygon arrays that can be used to create gdspy.PolygonSet objects.

`fonts.py`

`fonts.gds`

     1######################################################################
     2#                                                                    #
     3#  Copyright 2009 Lucas Heitzmann Gabrielli.                         #
     4#  This file is part of gdspy, distributed under the terms of the    #
     5#  Boost Software License - Version 1.0.  See the accompanying       #
     6#  LICENSE file or <http://www.boost.org/LICENSE_1_0.txt>            #
     7#                                                                    #
     8######################################################################
     9
    10from matplotlib.font_manager import FontProperties
    11from matplotlib.textpath import TextPath
    12import gdspy
    13
    14
    15def render_text(text, size=None, position=(0, 0), font_prop=None, tolerance=0.1):
    16    path = TextPath(position, text, size=size, prop=font_prop)
    17    polys = []
    18    xmax = position[0]
    19    for points, code in path.iter_segments():
    20        if code == path.MOVETO:
    21            c = gdspy.Curve(*points, tolerance=tolerance)
    22        elif code == path.LINETO:
    23            c.L(*points)
    24        elif code == path.CURVE3:
    25            c.Q(*points)
    26        elif code == path.CURVE4:
    27            c.C(*points)
    28        elif code == path.CLOSEPOLY:
    29            poly = c.get_points()
    30            if poly.size > 0:
    31                if poly[:, 0].min() < xmax:
    32                    i = len(polys) - 1
    33                    while i >= 0:
    34                        if gdspy.inside(
    35                            poly[:1], [polys[i]], precision=0.1 * tolerance
    36                        )[0]:
    37                            p = polys.pop(i)
    38                            poly = gdspy.boolean(
    39                                [p],
    40                                [poly],
    41                                "xor",
    42                                precision=0.1 * tolerance,
    43                                max_points=0,
    44                            ).polygons[0]
    45                            break
    46                        elif gdspy.inside(
    47                            polys[i][:1], [poly], precision=0.1 * tolerance
    48                        )[0]:
    49                            p = polys.pop(i)
    50                            poly = gdspy.boolean(
    51                                [p],
    52                                [poly],
    53                                "xor",
    54                                precision=0.1 * tolerance,
    55                                max_points=0,
    56                            ).polygons[0]
    57                        i -= 1
    58                xmax = max(xmax, poly[:, 0].max())
    59                polys.append(poly)
    60    return polys
    61
    62
    63if __name__ == "__main__":
    64    fp = FontProperties(family="serif", style="italic")
    65    text = gdspy.PolygonSet(render_text("Text rendering", 10, font_prop=fp), layer=1)
    66    lib = gdspy.GdsLibrary()
    67    cell = lib.new_cell("TXT")
    68    cell.add(text)
    69    lib.write_gds("fonts.gds")
    70    gdspy.LayoutViewer(lib)