Component#

A Component is like an empty canvas, where you can add polygons, references to other Components and ports (to connect to other components)

In gdsfactory all dimensions are in microns

Polygons#

You can add polygons to different layers.

import gdsfactory as gf


def demo_polygons():
    # Create a blank component (essentially an empty GDS cell with some special features)
    c = gf.Component("demo")

    # Create and add a polygon from separate lists of x points and y points
    # (Can also be added like [(x1,y1), (x2,y2), (x3,y3), ... ]
    c.add_polygon(
        [(-8, 6, 7, 9), (-6, 8, 17, 5)], layer=(1, 0)
    )  # GDS layers are tuples of ints (but if we use only one number it assumes the other number is 0)
    return c


c = demo_polygons()
c.write_gds("demo.gds")  # write it to a GDS file. You can open it in klayout.
c.show()  # show it in klayout
c.plot()  # plot it in jupyter notebook
2024-10-31 18:37:58.814 | INFO     | gdsfactory.component:_write_library:2004 - Wrote to 'demo.gds'
2024-10-31 18:37:58.837 | WARNING  | gdsfactory.klive:show:49 - UserWarning: Could not connect to klive server. Is klayout open and klive plugin installed?
../_images/48f00721939453cb4b45b3ed446b29c1c937b6806b40ea4581140c26f87c9374.png

Exercise :

Make a component similar to the one above that has a second polygon in layer (2, 0)

c = gf.Component("myComponent2")
# Create some new geometry from the functions available in the geometry library
t = gf.components.text("Hello!")
r = gf.components.rectangle(size=[5, 10], layer=(2, 0))

# Add references to the new geometry to c, our blank component
text1 = c.add_ref(t)  # Add the text we created as a reference
# Using the << operator (identical to add_ref()), add the same geometry a second time
text2 = c << t
r = c << r  # Add the rectangle we created

# Now that the geometry has been added to "c", we can move everything around:
text1.movey(25)
text2.move([5, 30])
text2.rotate(45)
r.movex(-15)
r.movex(-15)

print(c)
c.plot()
myComponent2: uid 8142fc7a, ports [], references ['text_1', 'text_2', 'rectangle_1'], 0 polygons
2024-10-31 18:37:59.325 | WARNING  | gdsfactory.component:_write_library:1934 - UserWarning: Component myComponent2 has invalid transformations. Try component.flatten_offgrid_references() first.
../_images/c8172b9ec6f1c9c090a007b686a0c14c8f9e4408527a8bda831a91f8247bbbef.png

You define polygons both from gdstk or Shapely

from shapely.geometry.polygon import Polygon

import gdsfactory as gf

c = gf.Component("Mixed_polygons")
p0 = Polygon(zip((-8, 6, 7, 9), (-6, 8, 17, 5)))
p1 = p0.buffer(1)
p2 = p1.simplify(tolerance=0.1)
c.add_polygon(p0, layer=(1, 0))
c.add_polygon(p1, layer=(2, 0))
c.add_polygon(p2, layer=(3, 0))

c.add_polygon([(-8, 6, 7, 9), (-6, 8, 17, 5)], layer=(4, 0))
c.plot()
../_images/21bc653dd8453310ad4db866dbde45337d431c53f98846ee5b198462cbe7c9c4.png
p0
../_images/d4f26b4dbf440927046b9da7311a3f548600eba805b4931685d05e0c660eae2f.svg
p1 = p0.buffer(1)
p1
../_images/b0fc5c6bcd2952687cbd3f25e45101ce4ef91f10d47a5e3da1697ac130b479c1.svg
pnot = p1 - p0
pnot
../_images/09672a2d246aa2ab402f1f56a350f97998f676edd54100e603d4d58ec6f3bcc6.svg
c = gf.Component("exterior")
c.add_polygon(pnot, layer=(3, 0))
c.plot()
../_images/f72be331a9f84832f325c90a8388357e8ad5f881090f204cd802d85a7b8761f0.png
p_small = p0.buffer(-1)
p_small
../_images/e99aa2147a3681537e3010fcf37827e398002850141fe2c841af3b864c880f0a.svg
p_or = pnot | p_small
p_or
../_images/0c7df0c4a11bbb6de465f56ae81080792fa10a40c2ad6c44e5d6a5b4186f20e3.svg
c = gf.Component("p_or")
c.add_polygon(p_or, layer=(1, 0))
c.plot()
../_images/a77f9d94bf329a27006aaf31e9cd1a26607d835ec4a901c2366145113a910c7c.png
import shapely as sp

p5 = sp.envelope(p0)
p5
../_images/99d8c99cc248a4e9534c1b24dfac2c2e2d6cc0ca17dd8161b5d40bbd03474722.svg
p6 = p5 - p0
p6
../_images/6fd7170d5cd4c3133f4bd859bdb8920e3a0a7cc88841f85d11a0c7b6f1a604f9.svg
c = gf.Component("p6")
c.add_polygon(p6, layer=(1, 0))
c.plot()
../_images/a80e4aea1cb83fe3e7113d6c167d643e6d7a7567b7d73dd1e39a590d0e07d694.png
c = gf.Component("demo_multilayer")
p0 = c.add_polygon(p0, layer={(2, 0), (3, 0)})
c.plot()
../_images/ecd5c30ee968c314f49c3798cf363674d74ce929021c1893e778ae186c7f3a5f.png
c = gf.Component("demo_mirror")
p0 = c.add_polygon(p0, layer=(1, 0))
p9 = c.add_polygon(p0, layer=(2, 0))
p9.mirror()
c.plot()
../_images/6996c7edcf602bf521dd585055ab0096d8dfa02793631ab30af277c7ca4a5f16.png
c = gf.Component("demo_xmin")
p0 = c.add_polygon(p0, layer=(1, 0))
p9 = c.add_polygon(p0, layer=(2, 0))
p9.mirror()
p9.xmin = p0.xmax
c.plot()
../_images/905c1ee229754084a5f9213a4746188360980bc59ca1f33ed026f224c336b1e2.png
c = gf.Component("enclosure1")
r = c << gf.components.ring_single()
c.plot()
../_images/e4b77559698e3d78da532daed26c77c240e5e5b3c6fbfa70cfc7aba100ab5369.png
c = gf.Component("enclosure2")
r = c << gf.components.ring_single()
p = c.get_polygon_bbox()
c.add_polygon(p, layer=(2, 0))
c.plot()
../_images/f0a2743c1d282ca569462cead2dddff27caf45ffad2dcc0087bcb326ac085e86.png
c = gf.Component("enclosure3")
r = c << gf.components.ring_single()
p = c.get_polygon_bbox(top=3, bottom=3)
c.add_polygon(p, layer=(2, 0))
c.plot()
../_images/1d18f3c88c3cd98dd7f7eb87a7230d104b01e3850e1387b41ec465256185b030.png
c = gf.Component("enclosure3")
r = c << gf.components.ring_single()
p = c.get_polygon_enclosure()
c.add_polygon(p, layer=(2, 0))
c.plot()
../_images/e2feb66609feefd96bcd5ed2de31729bf5476cb1034f553b3f34baa3ce2455b1.png
c = gf.Component("enclosure3")
r = c << gf.components.ring_single()
p = c.get_polygon_enclosure()
p2 = p.buffer(3)
c.add_polygon(p2, layer=(2, 0))
c.plot()
../_images/51579b31ee6c68d2e0479e5615bc1211f9f0061748f9c4b7d3a32fc480c19faa.png

Connect ports#

Components can have a “Port” that allows you to connect ComponentReferences together like legos.

You can write a simple function to make a rectangular straight, assign ports to the ends, and then connect those rectangles together.

Notice that connect transform each reference but things won’t remain connected if you move any of the references afterwards.

@gf.cell
def straight(length=10, width=1, layer=(1, 0)):
    c = gf.Component()
    c.add_polygon([(0, 0), (length, 0), (length, width), (0, width)], layer=layer)
    c.add_port(
        name="o1", center=[0, width / 2], width=width, orientation=180, layer=layer
    )
    c.add_port(
        name="o2", center=[length, width / 2], width=width, orientation=0, layer=layer
    )
    return c


c = gf.Component("straights_not_connected")

wg1 = c << straight(length=6, width=2.5, layer=(1, 0))
wg2 = c << straight(length=6, width=2.5, layer=(2, 0))
wg3 = c << straight(length=15, width=2.5, layer=(3, 0))
wg2.movey(10).rotate(10)
wg3.movey(20).rotate(15)

c.plot()
2024-10-31 18:38:01.173 | WARNING  | gdsfactory.component:_write_library:1934 - UserWarning: Component straights_not_connected has invalid transformations. Try component.flatten_offgrid_references() first.
../_images/050a378bc4c57c5c2accbd96946bbf8d9ffb4d400acdfc91833ea2b25a1b65cc.png

Now we can connect everything together using the ports:

Each straight has two ports: ‘o1’ and ‘o2’, respectively on the East and West sides of the rectangular straight component. These are arbitrary names defined in our straight() function above

# Let's keep wg1 in place on the bottom, and connect the other straights to it.
# To do that, on wg2 we'll grab the "o1" port and connect it to the "o2" on wg1:
wg2.connect("o1", wg1.ports["o2"], allow_layer_mismatch=True)
# Next, on wg3 let's grab the "o1" port and connect it to the "o2" on wg2:
wg3.connect("o1", wg2.ports["o2"], allow_layer_mismatch=True)

c.plot()
../_images/8e8d48eadb9286d2d2f3d8ce71518e1abd3ec61abdb5e2881e7be464effd0de4.png

Ports can be added by copying existing ports. In the example below, ports are added at the component-level on c from the existing ports of children wg1 and wg3 (i.e. eastmost and westmost ports)

c.add_port("o1", port=wg1.ports["o1"])
c.add_port("o2", port=wg3.ports["o2"])
c.plot()
../_images/1853dd6be376deafed636bc0abe10ad9d3267ccca2f6ae58a2844a8ff2990851.png

Move and rotate references#

You can move, rotate, and reflect references to Components.

c = gf.Component("straights_connected")

wg1 = c << straight(length=1, layer=(1, 0))
wg2 = c << straight(length=2, layer=(2, 0))
wg3 = c << straight(length=3, layer=(3, 0))

# Create and add a polygon from separate lists of x points and y points
# e.g. [(x1, x2, x3, ...), (y1, y2, y3, ...)]
poly1 = c.add_polygon([(8, 6, 7, 9), (6, 8, 9, 5)], layer=(4, 0))

# Alternatively, create and add a polygon from a list of points
# e.g. [(x1,y1), (x2,y2), (x3,y3), ...] using the same function
poly2 = c.add_polygon([(0, 0), (1, 1), (1, 3), (-3, 3)], layer=(5, 0))

# Shift the first straight we created over by dx = 10, dy = 5
wg1.move([10, 5])

# Shift the second straight over by dx = 10, dy = 0
wg2.move(origin=[0, 0], destination=[10, 0])

# Shift the third straight over by dx = 0, dy = 4. The translation movement consist of the difference between the values of the destination and the origin and can optionally be limited in a single axis.
wg3.move([1, 1], [5, 5], axis="y")

# Then, move again the third straight "from" x=0 "to" x=10 (dx=10)
wg3.movex(0, 10)

c.plot()
../_images/4a35e1d1afe2a3028c4352ba181ebbf8a738f63e5196c368157c68ed60dd4e9b.png

Ports#

Your straights wg1/wg2/wg3 are references to other waveguide components.

If you want to add ports to the new Component c you can use add_port, where you can create a new port or use a reference to an existing port from the underlying reference.

You can access the ports of a Component or ComponentReference

wg2.ports
{'o1': {'name': 'o1', 'width': 1, 'center': [10.0, 0.5], 'orientation': 180.0, 'layer': [2, 0], 'port_type': 'optical'},
 'o2': {'name': 'o2', 'width': 1, 'center': [12.0, 0.5], 'orientation': 0.0, 'layer': [2, 0], 'port_type': 'optical'}}
wg2.pprint_ports()
┏━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━┓
┃ name  width  center       orientation  layer   port_type ┃
┡━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━┩
│ o1   │ 1     │ [10.0, 0.5] │ 180.0       │ [2, 0] │ optical   │
│ o2   │ 1     │ [12.0, 0.5] │ 0.0         │ [2, 0] │ optical   │
└──────┴───────┴─────────────┴─────────────┴────────┴───────────┘

References#

Now that your component c is a multi-straight component, you can add references to that component in a new blank Component c2, then add two references and shift one to see the movement.

c2 = gf.Component("MultiWaveguides")
wg1 = straight()
wg2 = straight(layer=(2, 0))
mwg1_ref = c2.add_ref(wg1)
mwg2_ref = c2.add_ref(wg2)
mwg2_ref.move(destination=[10, 10])
c2.plot()
../_images/9ab422290b73447398db57732ca48a0d2812c870342cdb814effcdf1bf91b8e9.png
# Like before, let's connect mwg1 and mwg2 together
mwg1_ref.connect(port="o2", destination=mwg2_ref.ports["o1"], allow_layer_mismatch=True)
c2.plot()
../_images/929faead027e2b96fc1d495f0f9d168b402a7de46f5db516942d516d47fac0fd.png

Labels#

You can add abstract GDS labels to annotate your Components, in order to record information directly into the final GDS file without putting any extra geometry onto any layer This label will display in a GDS viewer, but will not be rendered or printed like the polygons created by gf.components.text().

c2.add_label(text="First label", position=mwg1_ref.center)
c2.add_label(text="Second label", position=mwg2_ref.center)

# labels are useful for recording information
c2.add_label(
    text=f"The x size of this\nlayout is {c2.xsize}",
    position=(c2.xmax, c2.ymax),
    layer=(10, 0),
)
c2.plot()
../_images/929faead027e2b96fc1d495f0f9d168b402a7de46f5db516942d516d47fac0fd.png

Another simple example

c = gf.Component("rectangle_with_label")
r = c << gf.components.rectangle(size=(1, 1))
r.x = 0
r.y = 0
c.add_label(
    text="Demo label",
    position=(0, 0),
    layer=(1, 0),
)
c.plot()
../_images/dc9ca43ffd7345d67723b53c0bc836ea359917b3a81fc49f2edcf1c246cfaf41.png

Boolean shapes#

If you want to subtract one shape from another, merge two shapes, or perform an XOR on them, you can do that with the boolean() function.

The operation argument should be {not, and, or, xor, ‘A-B’, ‘B-A’, ‘A+B’}. Note that ‘A+B’ is equivalent to ‘or’, ‘A-B’ is equivalent to ‘not’, and ‘B-A’ is equivalent to ‘not’ with the operands switched

c = gf.Component("boolean_demo")
e1 = c.add_ref(gf.components.ellipse(layer=(2, 0)))
e2 = c.add_ref(gf.components.ellipse(radii=(10, 6), layer=(2, 0))).movex(2)
e3 = c.add_ref(gf.components.ellipse(radii=(10, 4), layer=(2, 0))).movex(5)
c.plot()
../_images/f51e837d57b4963fd2e9e1dce3198de73e848975ca54ceea4d8dcbf98b8f1f52.png
c2 = gf.geometry.boolean(A=[e1, e3], B=e2, operation="A-B", layer=(2, 0))
c2.plot()
../_images/a285f653598a0d05ca28ee8b9885d7cd1cbc2166767bb3a109d378db7c93dbc0.png

Move Reference by port#

c = gf.Component("ref_port_sample")
mmi = c.add_ref(gf.components.mmi1x2())
bend = c.add_ref(gf.components.bend_circular(layer=(1, 0)))
c.plot()
../_images/63ced41b1adbfa122f8685b9d290e970e3b7d755f2e27c9033904352ca72cf9d.png
bend.connect("o1", mmi.ports["o2"])  # connects follow Source, destination syntax
c.plot()
../_images/abb239dd2eb48436b578e5035d017e7b14b4a91f421e38b21a5ae9a0bacb085b.png

Mirror reference#

By default the mirror works along the y-axis.

c = gf.Component("ref_mirror")
mmi = c.add_ref(gf.components.mmi1x2())
bend = c.add_ref(gf.components.bend_circular(layer=(2, 0)))
c.plot()
../_images/8c4a7cd80151c612dd2cc5ee8937e0a8493833e4ead0768688d697a1e457ba3c.png
mmi.mirror()
c.plot()
../_images/253402e50e27ab55b11c477fec869c367374ba005fbe2a3a19bdaec9ce52ad7d.png
mmi.mirror_y()
c.plot()
../_images/253402e50e27ab55b11c477fec869c367374ba005fbe2a3a19bdaec9ce52ad7d.png
mmi.mirror_x()
c.plot()
../_images/ea8e3c536322b8329d192e9897c120f715cdf93ff2328e89aea7ff2cefa1ab46.png

Write#

You can write your Component to:

  • GDS file (Graphical Database System) or OASIS for chips.

  • gerber for PCB.

  • STL for 3d printing.

import gdsfactory as gf

c = gf.components.cross()
c.write_gds("demo.gds")
c.plot()
2024-10-31 18:38:03.110 | INFO     | gdsfactory.component:_write_library:2004 - Wrote to 'demo.gds'
../_images/e0681b3656b35dbfd0a6e2a98989519618bf7bd3caec1c8646c664091f454a57.png

You can see the GDS file in Klayout viewer.

Sometimes you also want to save the GDS together with metadata (settings, port names, widths, locations …) in YAML

c.write_gds("demo.gds", with_metadata=True)
2024-10-31 18:38:03.224 | INFO     | gdsfactory.component:_write_library:2004 - Wrote to 'demo.gds'
2024-10-31 18:38:03.229 | INFO     | gdsfactory.component:_write_library:2008 - Write YAML metadata to 'demo.yml'
PosixPath('demo.gds')

OASIS is a newer format that can store CAD files and that reduces the size.

c.write_oas("demo.oas")
2024-10-31 18:38:03.236 | INFO     | gdsfactory.component:_write_library:2004 - Wrote to 'demo.oas'
PosixPath('demo.oas')

You can also save the image as a PNG image

fig = c.plot_klayout()
fig.savefig("demo.png")
../_images/e0681b3656b35dbfd0a6e2a98989519618bf7bd3caec1c8646c664091f454a57.png

You can also save it as STL for 3D printing or for some simulator, thanks to the LayerStack

c.write_stl("demo.stl")
Write PosixPath('/home/runner/work/gdsfactory7/gdsfactory7/notebooks/demo_1_0.stl') zmin = 0.000, height = 0.220
scene = c.to_3d()
scene.show()