How to use the gdspy.boolean function in gdspy

To help you get started, we’ve selected a few gdspy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github heitzmann / gdspy / tests / cellarray.py View on Github external
def test_notempty():
    name = "ca_notempty"
    c = gdspy.Cell(name)
    ref = gdspy.CellArray(name, 2, 3, (3, 2), (1, -1), 90, 2, True)
    ref.translate(-1, 1)
    c.add(gdspy.Rectangle((0, 0), (1, 2), 2, 3))
    assert ref.area() == 48
    assert ref.area(True) == {(2, 3): 48}
    err = numpy.array(((0, 0), (8, 5))) - ref.get_bounding_box()
    assert numpy.max(numpy.abs(err)) < 1e-15
    assert ref.origin[0] == ref.origin[1] == 0
    r = gdspy.boolean(
        [gdspy.Rectangle((0, 0), (8, 2)), gdspy.Rectangle((0, 3), (8, 5))],
        ref.get_polygons(),
        "xor",
        1e-6,
        0,
    )
    assert r is None
    d = ref.get_polygons(True)
    assert len(d.keys()) == 1
    r = gdspy.boolean(
        [gdspy.Rectangle((0, 0), (8, 2)), gdspy.Rectangle((0, 3), (8, 5))],
        d[(2, 3)],
        "xor",
        1e-6,
        0,
    )
github heitzmann / gdspy / tests / tutils.py View on Github external
def assertsame(c1, c2, tolerance=1e-6):
    d1 = c1.get_polygons(by_spec=True)
    d2 = c2.get_polygons(by_spec=True)
    for key in d1:
        assert key in d2
        result = gdspy.boolean(
            d1[key], d2[key], "xor", precision=1e-7, layer=key[0], datatype=100
        )
        if result is not None:
            r1 = gdspy.boolean(
                d1[key],
                gdspy.offset(d2[key], tolerance, precision=1e-7),
                "not",
                precision=1e-7,
                layer=key[0],
                datatype=99,
            )
            r2 = gdspy.boolean(
                d2[key],
                gdspy.offset(d1[key], tolerance, precision=1e-7),
                "not",
                precision=1e-7,
                layer=key[0],
                datatype=99,
            )
            # if not (r1 is None and r2 is None):
github heitzmann / gdspy / tests / functions.py View on Github external
def test_boolean():
    op1 = gdspy.Rectangle((0, 0), (3, 3))
    op2 = gdspy.Rectangle((1, 1), (2, 2))
    result = [
        [(0, 0), (3, 0), (3, 3), (0, 3), (0, 0), (1, 1), (1, 2), (2, 2), (2, 1), (1, 1)]
    ]
    assert equals(gdspy.boolean(op1, op2, "not"), result)
    op3 = gdspy.Rectangle((0, 0), (2, 2))
    assert equals(gdspy.boolean([op2, op3], None, "or"), op3)
github heitzmann / gdspy / tests / tutils.py View on Github external
d2 = c2.get_polygons(by_spec=True)
    for key in d1:
        assert key in d2
        result = gdspy.boolean(
            d1[key], d2[key], "xor", precision=1e-7, layer=key[0], datatype=100
        )
        if result is not None:
            r1 = gdspy.boolean(
                d1[key],
                gdspy.offset(d2[key], tolerance, precision=1e-7),
                "not",
                precision=1e-7,
                layer=key[0],
                datatype=99,
            )
            r2 = gdspy.boolean(
                d2[key],
                gdspy.offset(d1[key], tolerance, precision=1e-7),
                "not",
                precision=1e-7,
                layer=key[0],
                datatype=99,
            )
            # if not (r1 is None and r2 is None):
            #    c1.add(result)
            #    c2.add(result)
            #    if r1 is not None:
            #        c1.add(r1)
            #    if r2 is not None:
            #        c2.add(r2)
            #    gdspy.LayoutViewer(cells=[c1, c2])
            assert r1 is None
github heitzmann / gdspy / tests / cellreference.py View on Github external
def test_notempty():
    name = "cr_notempty"
    c = gdspy.Cell(name)
    ref = gdspy.CellReference(name, (1, -1), 90, 2, True)
    ref.translate(-1, 1)
    c.add(gdspy.Rectangle((0, 0), (1, 2), 2, 3))
    assert ref.area() == 8
    assert ref.area(True) == {(2, 3): 8}
    err = numpy.array(((0, 0), (4, 2))) - ref.get_bounding_box()
    assert numpy.max(numpy.abs(err)) < 1e-15
    assert ref.origin[0] == ref.origin[1] == 0
    r = gdspy.boolean(
        ref.get_polygons(), gdspy.Rectangle((0, 0), (4, 2)), "xor", 1e-6, 0
    )
    assert r is None
    d = ref.get_polygons(True)
    assert len(d.keys()) == 1
    r = gdspy.boolean(d[(2, 3)], gdspy.Rectangle((0, 0), (4, 2)), "xor", 1e-6, 0)
    assert r is None
github heitzmann / gdspy / docs / makeimages.py View on Github external
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)
    draw(gdspy.Cell("offset_operation").add([outer, rect1, rect2]))

    # Fillet Operation
    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)
    draw(gdspy.Cell("fillet_operation").add([joined, multi_path]))

    # Text
    # 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))
github stanfordnqp / spins-b / spins / gds / gen_gds.py View on Github external
# any polygons satisfy the current overlap number.

            target_polys = np.where(overlap_list == overlap_num)[0]
            while target_polys.size != 0:

                # We iterate through each polygon in our polygon list until no
                # polygon satisfies the current overlap number.
                for i in target_polys[::-1]:
                    containment_list = np.nonzero(containment_mx[i, :])[0]
                    # Concatenate all the contained polygons to subtract out.
                    poly_list = sum([
                        gds_polygons[j].polygons for j in containment_list
                    ], [])
                    # Note that we had to turn precision from the default 1e-3
                    # to 1e-6 to avoid errors in the NOT operation.
                    gds_polygons[i] = gdspy.boolean(gds_polygons[i],
                                                    gdspy.PolygonSet(poly_list),
                                                    "not",
                                                    layer=0,
                                                    precision=1e-6)
                    for j in containment_list:
                        gds_polygons[j] = None
                        test_points[j] = None

                for k in range(len(gds_polygons) - 1, -1, -1):
                    if gds_polygons[k] is None:
                        del gds_polygons[k]
                        del test_points[k]

                containment_mx = []
                for polygon in gds_polygons:
                    containment_mx.append(gdspy.inside(test_points, [polygon]))
github heitzmann / gdspy / docs / makeimages.py View on Github external
lambda u: 0.25 * numpy.cos(24 * numpy.pi * u),
            -0.75,
            0.75,
        ],
    )
    draw(gdspy.Cell("robust_paths").add(lp))

    # Boolean Operations
    # 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")
    draw(gdspy.Cell("boolean_operations").add(inv))

    # Slice Operation
    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
github heitzmann / gdspy / docs / _static / fonts.py View on Github external
c.L(*points)
        elif code == path.CURVE3:
            c.Q(*points)
        elif code == path.CURVE4:
            c.C(*points)
        elif code == path.CLOSEPOLY:
            poly = c.get_points()
            if poly.size > 0:
                if poly[:, 0].min() < xmax:
                    i = len(polys) - 1
                    while i >= 0:
                        if gdspy.inside(
                            poly[:1], [polys[i]], precision=0.1 * tolerance
                        )[0]:
                            p = polys.pop(i)
                            poly = gdspy.boolean(
                                [p],
                                [poly],
                                "xor",
                                precision=0.1 * tolerance,
                                max_points=0,
                            ).polygons[0]
                            break
                        elif gdspy.inside(
                            polys[i][:1], [poly], precision=0.1 * tolerance
                        )[0]:
                            p = polys.pop(i)
                            poly = gdspy.boolean(
                                [p],
                                [poly],
                                "xor",
                                precision=0.1 * tolerance,