{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# References and ports\n", "\n", "gdsfactory defines your component once in memory and can add multiple References (Instances) to the same component." ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "As you build components you can include references to other components. Adding a reference is like having a pointer to a component.\n", "\n", "The GDSII specification allows the use of references, and similarly gdsfactory uses them (with the `add_ref()` function).\n", "what is a reference? Simply put: **A reference does not contain any geometry. It only *points* to an existing geometry**.\n", "\n", "Say you have a ridiculously large polygon with 100 billion vertices that you call BigPolygon. It's huge, and you need to use it in your design 250 times.\n", "Well, a single copy of BigPolygon takes up 1MB of memory, so you don't want to make 250 copies of it\n", "You can instead *references* the polygon 250 times.\n", "Each reference only uses a few bytes of memory -- it only needs to know the memory address of BigPolygon, position, rotation and mirror.\n", "This way, you can keep one copy of BigPolygon and use it again and again.\n", "\n", "You can start by making a blank `Component` and add a single polygon to it." ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "import gdsfactory as gf\n", "\n", "# Create a blank Component\n", "p = gf.Component(\"component_with_polygon\")\n", "\n", "# Add a polygon\n", "xpts = [0, 0, 5, 6, 9, 12]\n", "ypts = [0, 1, 1, 2, 2, 0]\n", "p.add_polygon([xpts, ypts], layer=(2, 0))\n", "\n", "# plot the Component with the polygon in it\n", "p.plot()" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "Now, you want to reuse this polygon repeatedly without creating multiple copies of it.\n", "\n", "To do so, you need to make a second blank `Component`, this time called `c`.\n", "\n", "In this new Component you *reference* our Component `p` which contains our polygon." ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"Component_with_references\") # Create a new blank Component\n", "poly_ref = c.add_ref(p) # Reference the Component \"p\" that has the polygon in it\n", "c.plot()" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "you just made a copy of your polygon -- but remember, you didn't actually\n", "make a second polygon, you just made a reference (aka pointer) to the original\n", "polygon. Let's add two more references to `c`:" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "poly_ref2 = c.add_ref(p) # Reference the Component \"p\" that has the polygon in it\n", "poly_ref3 = c.add_ref(p) # Reference the Component \"p\" that has the polygon in it\n", "c.plot()" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "Now you have 3x polygons all on top of each other. Again, this would appear\n", "useless, except that you can manipulate each reference independently. Notice that\n", "when you called `c.add_ref(p)` above, we saved the result to a new variable each\n", "time (`poly_ref`, `poly_ref2`, and `poly_ref3`)? You can use those variables to\n", "reposition the references." ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "poly_ref2.rotate(90) # Rotate the 2nd reference we made 90 degrees\n", "poly_ref3.rotate(180) # Rotate the 3rd reference we made 180 degrees\n", "c.plot()" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "Now you're getting somewhere! You've only had to make the polygon once, but you're\n", "able to reuse it as many times as you want.\n", "\n", "## Modifying the referenced geometry\n", "\n", "What happens when you change the original geometry that the reference points to? In your case, your references in\n", "`c` all point to the Component `p` that with the original polygon. Let's try\n", "adding a second polygon to `p`.\n", "\n", "First you add the second polygon and make sure `P` looks like you expect:" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "# Add a 2nd polygon to \"p\"\n", "xpts = [14, 14, 16, 16]\n", "ypts = [0, 2, 2, 0]\n", "p.add_polygon([xpts, ypts], layer=(1, 0))\n", "p" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "That looks good. Now let's find out what happened to `c` that contains the\n", "three references. Keep in mind that you have not modified `c` or executed any\n", "functions/operations on `c` -- all you have done is modify `p`." ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "c.plot()" ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ " **When you modify the original geometry, all of the\n", "references automatically reflect the modifications.** This is very powerful,\n", "because you can use this to make very complicated designs from relatively simple\n", "elements in a computation- and memory-efficient way.\n", "\n", "Let's try making references a level deeper by referencing `c`. Note here we use\n", "the `<<` operator to add the references -- this is just shorthand, and is\n", "exactly equivalent to using `add_ref()`" ] }, { "cell_type": "code", "execution_count": null, "id": "14", "metadata": {}, "outputs": [], "source": [ "c2 = gf.Component(\"array_sample\") # Create a new blank Component\n", "d_ref1 = c2.add_ref(c) # Reference the Component \"c\" that 3 references in it\n", "d_ref2 = c2 << c # Use the \"<<\" operator to create a 2nd reference to c.plot()\n", "d_ref3 = c2 << c # Use the \"<<\" operator to create a 3rd reference to c.plot()\n", "\n", "d_ref1.move([20, 0])\n", "d_ref2.move([40, 0])\n", "\n", "c2" ] }, { "cell_type": "markdown", "id": "15", "metadata": {}, "source": [ "As you've seen you have two ways to add a reference to our component:\n", "\n", "1. create the reference and add it to the component" ] }, { "cell_type": "code", "execution_count": null, "id": "16", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"reference_sample\")\n", "w = gf.components.straight(width=0.6)\n", "wr = w.ref()\n", "c.add(wr)\n", "c.plot()" ] }, { "cell_type": "markdown", "id": "17", "metadata": {}, "source": [ "2. or do it in a single line" ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"reference_sample_shorter_syntax\")\n", "wr = c << gf.components.straight(width=0.6)\n", "c.plot()" ] }, { "cell_type": "markdown", "id": "19", "metadata": {}, "source": [ "in both cases you can move the reference `wr` after created" ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"two_references\")\n", "wr1 = c << gf.components.straight(width=0.6)\n", "wr2 = c << gf.components.straight(width=0.6)\n", "wr2.movey(10)\n", "c.add_ports(wr1.get_ports_list(), prefix=\"bot_\")\n", "c.add_ports(wr2.get_ports_list(), prefix=\"top_\")" ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "c.ports" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "You can also auto_rename ports using gdsfactory default convention, where ports are numbered clockwise starting from the bottom left" ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "c.auto_rename_ports()" ] }, { "cell_type": "code", "execution_count": null, "id": "24", "metadata": {}, "outputs": [], "source": [ "c.ports" ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [], "source": [ "c.plot()" ] }, { "cell_type": "markdown", "id": "26", "metadata": {}, "source": [ "## Arrays of references\n", "\n", "In GDS, there's a type of structure called a \"ComponentReference\" which takes a cell and repeats it NxM times on a fixed grid spacing. For convenience, `Component` includes this functionality with the add_array() function.\n", "Note that CellArrays are not compatible with ports (since there is no way to access/modify individual elements in a GDS cellarray)\n", "\n", "gdsfactory also provides with more flexible arrangement options if desired, see for example `grid()` and `packer()`.\n", "\n", "As well as `gf.components.array`\n", "\n", "Let's make a new Component and put a big array of our Component `c` in it:" ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "c3 = gf.Component(\"array_of_references\") # Create a new blank Component\n", "aref = c3.add_array(\n", " c, columns=6, rows=3, spacing=[20, 15]\n", ") # Reference the Component \"c\" 3 references in it with a 3 rows, 6 columns array\n", "c3" ] }, { "cell_type": "markdown", "id": "28", "metadata": {}, "source": [ "CellArrays don't have ports and there is no way to access/modify individual elements in a GDS cellarray.\n", "\n", "gdsfactory provides you with similar functions in `gf.components.array` and `gf.components.array_2d`" ] }, { "cell_type": "code", "execution_count": null, "id": "29", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "c4 = gf.Component(\"demo_array\") # Create a new blank Component\n", "aref = c4 << gf.components.array(component=c, columns=3, rows=2)\n", "c4.add_ports(aref.get_ports_list())\n", "c4" ] }, { "cell_type": "code", "execution_count": null, "id": "30", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "help(gf.components.array)" ] }, { "cell_type": "markdown", "id": "31", "metadata": { "lines_to_next_cell": 2 }, "source": [ "You can also create an array of references for periodic structures. Let's create a [Distributed Bragg Reflector](https://picwriter.readthedocs.io/en/latest/components/dbr.html)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "32", "metadata": {}, "outputs": [], "source": [ "@gf.cell\n", "def dbr_period(w1=0.5, w2=0.6, l1=0.2, l2=0.4, straight=gf.components.straight):\n", " \"\"\"Return one DBR period.\"\"\"\n", " c = gf.Component()\n", " r1 = c << straight(length=l1, width=w1)\n", " r2 = c << straight(length=l2, width=w2)\n", " r2.connect(port=\"o1\", destination=r1.ports[\"o2\"])\n", " c.add_port(\"o1\", port=r1.ports[\"o1\"])\n", " c.add_port(\"o2\", port=r2.ports[\"o2\"])\n", " return c\n", "\n", "\n", "l1 = 0.2\n", "l2 = 0.4\n", "n = 3\n", "period = dbr_period(l1=l1, l2=l2)\n", "period" ] }, { "cell_type": "code", "execution_count": null, "id": "33", "metadata": {}, "outputs": [], "source": [ "dbr = gf.Component(\"DBR\")\n", "dbr.add_array(period, columns=n, rows=1, spacing=(l1 + l2, 100))\n", "dbr" ] }, { "cell_type": "markdown", "id": "34", "metadata": {}, "source": [ "Finally we need to add ports to the new component" ] }, { "cell_type": "code", "execution_count": null, "id": "35", "metadata": {}, "outputs": [], "source": [ "p0 = dbr.add_port(\"o1\", port=period.ports[\"o1\"])\n", "p1 = dbr.add_port(\"o2\", port=period.ports[\"o2\"])\n", "\n", "p1.center = [(l1 + l2) * n, 0]\n", "dbr" ] }, { "cell_type": "markdown", "id": "36", "metadata": {}, "source": [ "## Connect references\n", "\n", "We have seen that once you create a reference you can manipulate the reference to move it to a location. Here we are going to connect that reference to a port. Remember that we follow that a certain reference `source` connects to a `destination` port" ] }, { "cell_type": "code", "execution_count": null, "id": "37", "metadata": {}, "outputs": [], "source": [ "bend = gf.components.bend_circular()\n", "bend" ] }, { "cell_type": "code", "execution_count": null, "id": "38", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"sample_reference_connect\")\n", "\n", "mmi = c << gf.components.mmi1x2()\n", "b = c << gf.components.bend_circular()\n", "b.connect(\"o1\", destination=mmi.ports[\"o2\"])\n", "\n", "c.add_port(\"o1\", port=mmi.ports[\"o1\"])\n", "c.add_port(\"o2\", port=b.ports[\"o2\"])\n", "c.add_port(\"o3\", port=mmi.ports[\"o3\"])\n", "c.plot()" ] }, { "cell_type": "markdown", "id": "39", "metadata": {}, "source": [ "You can also access the ports as `reference[port_name]` instead of `reference.ports[port_name]`" ] }, { "cell_type": "code", "execution_count": null, "id": "40", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"sample_reference_connect_simpler\")\n", "\n", "mmi = c << gf.components.mmi1x2()\n", "b = c << gf.components.bend_circular()\n", "b.connect(\"o1\", destination=mmi[\"o2\"])\n", "\n", "c.add_port(\"o1\", port=mmi[\"o1\"])\n", "c.add_port(\"o2\", port=b[\"o2\"])\n", "c.add_port(\"o3\", port=mmi[\"o3\"])\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "41", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"sample_reference_connect_simpler\")\n", "\n", "mmi = c << gf.components.mmi1x2()\n", "b = c.add_ref(gf.components.bend_circular()).connect(\"o1\", destination=mmi[\"o2\"])\n", "\n", "c.add_port(\"o1\", port=mmi[\"o1\"])\n", "c.add_port(\"o2\", port=b[\"o2\"])\n", "c.add_port(\"o3\", port=mmi[\"o3\"])\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "42", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"sample_reference_connect_simpler_with_mirror\")\n", "\n", "mmi = c << gf.components.mmi1x2()\n", "b = (\n", " c.add_ref(gf.components.bend_circular())\n", " .mirror()\n", " .connect(\"o1\", destination=mmi[\"o2\"])\n", ")\n", "\n", "c.add_port(\"o1\", port=mmi[\"o1\"])\n", "c.add_port(\"o2\", port=b[\"o2\"])\n", "c.add_port(\"o3\", port=mmi[\"o3\"])\n", "c.plot()" ] }, { "cell_type": "markdown", "id": "43", "metadata": {}, "source": [ "Notice that `connect` mates two ports together and does not imply that ports will remain connected.\n" ] }, { "cell_type": "markdown", "id": "44", "metadata": {}, "source": [ "## Port\n", "\n", "You can name the ports as you want and use `gf.port.auto_rename_ports(prefix='o')` to rename them later on.\n", "\n", "Here is the default naming convention.\n", "\n", "Ports are numbered clock-wise starting from the bottom left corner.\n", "\n", "Optical ports have `o` prefix and Electrical ports `e` prefix.\n", "\n", "The port naming comes in most cases from the `gdsfactory.cross_section`. For example:\n", "\n", "- `gdsfactory.cross_section.strip` has ports `o1` for input and `o2` for output.\n", "- `gdsfactory.cross_section.metal1` has ports `e1` for input and `e2` for output." ] }, { "cell_type": "code", "execution_count": null, "id": "45", "metadata": {}, "outputs": [], "source": [ "size = 4\n", "c = gf.components.nxn(west=2, south=2, north=2, east=2, xsize=size, ysize=size)\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "46", "metadata": {}, "outputs": [], "source": [ "c = gf.components.straight_heater_metal(length=30)\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "47", "metadata": {}, "outputs": [], "source": [ "c.ports" ] }, { "cell_type": "markdown", "id": "48", "metadata": {}, "source": [ "You can get the optical ports by `layer`" ] }, { "cell_type": "code", "execution_count": null, "id": "49", "metadata": {}, "outputs": [], "source": [ "c.get_ports_dict(layer=(1, 0))" ] }, { "cell_type": "markdown", "id": "50", "metadata": {}, "source": [ "or by `width`" ] }, { "cell_type": "code", "execution_count": null, "id": "51", "metadata": {}, "outputs": [], "source": [ "c.get_ports_dict(width=0.5)" ] }, { "cell_type": "code", "execution_count": null, "id": "52", "metadata": {}, "outputs": [], "source": [ "c0 = gf.components.straight_heater_metal()\n", "c0.ports" ] }, { "cell_type": "code", "execution_count": null, "id": "53", "metadata": {}, "outputs": [], "source": [ "c1 = c0.copy()\n", "c1.auto_rename_ports_layer_orientation()\n", "c1.ports" ] }, { "cell_type": "code", "execution_count": null, "id": "54", "metadata": {}, "outputs": [], "source": [ "c2 = c0.copy()\n", "c2.auto_rename_ports()\n", "c2.ports" ] }, { "cell_type": "markdown", "id": "55", "metadata": {}, "source": [ "You can also rename them with a different port naming convention\n", "\n", "- prefix: add `e` for electrical `o` for optical\n", "- clockwise\n", "- counter-clockwise\n", "- orientation `E` East, `W` West, `N` North, `S` South\n", "\n", "\n", "Here is the default one we use (clockwise starting from bottom left west facing port)\n", "\n", "```\n", " 3 4\n", " |___|_\n", " 2 -| |- 5\n", " | |\n", " 1 -|______|- 6\n", " | |\n", " 8 7\n", "\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "56", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"demo_ports\")\n", "nxn = gf.components.nxn(west=2, north=2, east=2, south=2, xsize=4, ysize=4)\n", "ref = c.add_ref(nxn)\n", "c.add_ports(ref.ports)\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "57", "metadata": {}, "outputs": [], "source": [ "ref.get_ports_list() # by default returns ports clockwise starting from bottom left west facing port" ] }, { "cell_type": "code", "execution_count": null, "id": "58", "metadata": {}, "outputs": [], "source": [ "c.auto_rename_ports()\n", "c.plot()" ] }, { "cell_type": "markdown", "id": "59", "metadata": {}, "source": [ "You can also get the ports counter-clockwise\n", "\n", "```\n", " 4 3\n", " |___|_\n", " 5 -| |- 2\n", " | |\n", " 6 -|______|- 1\n", " | |\n", " 7 8\n", "\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "60", "metadata": {}, "outputs": [], "source": [ "c.auto_rename_ports_counter_clockwise()\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "61", "metadata": {}, "outputs": [], "source": [ "c.get_ports_list(clockwise=False)" ] }, { "cell_type": "code", "execution_count": null, "id": "62", "metadata": {}, "outputs": [], "source": [ "c.ports_layer" ] }, { "cell_type": "code", "execution_count": null, "id": "63", "metadata": {}, "outputs": [], "source": [ "c.port_by_orientation_cw(\"W0\")" ] }, { "cell_type": "code", "execution_count": null, "id": "64", "metadata": {}, "outputs": [], "source": [ "c.port_by_orientation_ccw(\"W1\")" ] }, { "cell_type": "markdown", "id": "65", "metadata": {}, "source": [ "Lets extend the East facing ports (orientation = 0 deg)" ] }, { "cell_type": "code", "execution_count": null, "id": "66", "metadata": {}, "outputs": [], "source": [ "cross_section = gf.cross_section.strip()\n", "\n", "nxn = gf.components.nxn(\n", " west=2, north=2, east=2, south=2, xsize=4, ysize=4, cross_section=cross_section\n", ")\n", "c = gf.components.extension.extend_ports(component=nxn, orientation=0)\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "67", "metadata": {}, "outputs": [], "source": [ "c.ports" ] }, { "cell_type": "code", "execution_count": null, "id": "68", "metadata": {}, "outputs": [], "source": [ "df = c.get_ports_pandas()\n", "df" ] }, { "cell_type": "code", "execution_count": null, "id": "69", "metadata": {}, "outputs": [], "source": [ "df[df.port_type == \"optical\"]" ] }, { "cell_type": "markdown", "id": "70", "metadata": {}, "source": [ "## Port markers (Pins)\n", "\n", "You can add pins (port markers) to each port. Different foundries do this differently, so gdsfactory supports all of them.\n", "\n", "- square with port inside the component.\n", "- square centered (half inside, half outside component).\n", "- triangular pointing towards the outside of the port.\n", "- path (SiEPIC).\n", "\n", "\n", "by default Component.show() will add triangular pins, so you can see the direction of the port in Klayout." ] }, { "cell_type": "code", "execution_count": null, "id": "71", "metadata": {}, "outputs": [], "source": [ "gf.components.mmi1x2()" ] }, { "cell_type": "code", "execution_count": null, "id": "72", "metadata": {}, "outputs": [], "source": [ "mmi = gf.components.mmi1x2()\n", "mmi_with_pins = gf.add_pins.add_pins_container(mmi)\n", "mmi_with_pins.plot()" ] }, { "cell_type": "markdown", "id": "73", "metadata": {}, "source": [ "## Component_sequence\n", "\n", "When you have repetitive connections you can describe the connectivity as an ASCII map" ] }, { "cell_type": "code", "execution_count": null, "id": "74", "metadata": {}, "outputs": [], "source": [ "bend180 = gf.components.bend_circular180()\n", "wg_pin = gf.components.straight_pin(length=40)\n", "wg = gf.components.straight()\n", "\n", "# Define a map between symbols and (component, input port, output port)\n", "symbol_to_component = {\n", " \"D\": (bend180, \"o1\", \"o2\"),\n", " \"C\": (bend180, \"o2\", \"o1\"),\n", " \"P\": (wg_pin, \"o1\", \"o2\"),\n", " \"-\": (wg, \"o1\", \"o2\"),\n", "}\n", "\n", "# Generate a sequence\n", "# This is simply a chain of characters. Each of them represents a component\n", "# with a given input and and a given output\n", "\n", "sequence = \"DC-P-P-P-P-CD\"\n", "component = gf.components.component_sequence(\n", " sequence=sequence, symbol_to_component=symbol_to_component\n", ")\n", "component.name = \"component_sequence\"\n", "component.plot()" ] }, { "cell_type": "markdown", "id": "75", "metadata": {}, "source": [ "As the sequence is defined as a string you can use the string operations to easily build complex sequences" ] }, { "cell_type": "markdown", "id": "76", "metadata": {}, "source": [ "## Movement\n", "\n", "You can move, rotate and mirror ComponentReference as well as `Port`, `Polygon`, `ComponentReference`, `Label`, and `Group`" ] }, { "cell_type": "code", "execution_count": null, "id": "77", "metadata": {}, "outputs": [], "source": [ "import gdsfactory as gf\n", "\n", "# Start with a blank Component\n", "c = gf.Component(\"demo_movement\")\n", "\n", "# Create some more Components with shapes\n", "T = gf.components.text(\"hello\", size=10, layer=(1, 0))\n", "E = gf.components.ellipse(radii=(10, 5), layer=(2, 0))\n", "R = gf.components.rectangle(size=(10, 3), layer=(3, 0))\n", "\n", "# Add the shapes to c as references\n", "text = c << T\n", "ellipse = c << E\n", "rect1 = c << R\n", "rect2 = c << R\n", "\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "78", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "c = gf.Component(\"move_one_ellipse\")\n", "e1 = c << gf.components.ellipse(radii=(10, 5), layer=(2, 0))\n", "e2 = c << gf.components.ellipse(radii=(10, 5), layer=(2, 0))\n", "e1.movex(10)\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "79", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "c = gf.Component(\"move_one_ellipse_xmin\")\n", "e1 = c << gf.components.ellipse(radii=(10, 5), layer=(2, 0))\n", "e2 = c << gf.components.ellipse(radii=(10, 5), layer=(2, 0))\n", "e2.xmin = e1.xmax\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "80", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "# Now you can practice move and rotate the objects.\n", "\n", "c = gf.Component(\"two_ellipses_on_top_of_each_other\")\n", "E = gf.components.ellipse(radii=(10, 5), layer=(2, 0))\n", "e1 = c << E\n", "e2 = c << E\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "81", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "c = gf.Component(\"ellipse_moved\")\n", "e = gf.components.ellipse(radii=(10, 5), layer=(2, 0))\n", "e1 = c << e\n", "e2 = c << e\n", "e2.move(origin=[5, 5], destination=[10, 10]) # Translate by dx = 5, dy = 5\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "82", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"ellipse_moved_v2\")\n", "e = gf.components.ellipse(radii=(10, 5), layer=(2, 0))\n", "e1 = c << e\n", "e2 = c << e\n", "e2.move([5, 5]) # Translate by dx = 5, dy = 5\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "83", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"rectangles\")\n", "r = gf.components.rectangle(size=(10, 5), layer=(2, 0))\n", "rect1 = c << r\n", "rect2 = c << r\n", "\n", "rect1.rotate(45) # Rotate the first straight by 45 degrees around (0,0)\n", "rect2.rotate(\n", " -30, center=[1, 1]\n", ") # Rotate the second straight by -30 degrees around (1,1)\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "84", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "c = gf.Component(\"mirror_demo\")\n", "text = c << gf.components.text(\"hello\")\n", "text.mirror(p1=[1, 1], p2=[1, 3]) # Reflects across the line formed by p1 and p2\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "85", "metadata": {}, "outputs": [], "source": [ "c = gf.Component(\"hello\")\n", "text = c << gf.components.text(\"hello\")\n", "c.plot()" ] }, { "cell_type": "markdown", "id": "86", "metadata": {}, "source": [ "Each Component and ComponentReference object has several properties which can be\n", "used\n", "to learn information about the object (for instance where it's center coordinate\n", "is). Several of these properties can actually be used to move the geometry by\n", "assigning them new values.\n", "\n", "Available properties are:\n", "\n", "- `xmin` / `xmax`: minimum and maximum x-values of all points within the object\n", "- `ymin` / `ymax`: minimum and maximum y-values of all points within the object\n", "- `x`: centerpoint between minimum and maximum x-values of all points within the\n", "object\n", "- `y`: centerpoint between minimum and maximum y-values of all points within the\n", "object\n", "- `bbox`: bounding box (see note below) in format ((xmin,ymin),(xmax,ymax))\n", "- `center`: center of bounding box" ] }, { "cell_type": "code", "execution_count": null, "id": "87", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "print(\"bounding box:\")\n", "print(\n", " text.bbox\n", ") # Will print the bounding box of text in terms of [(xmin, ymin), (xmax, ymax)]\n", "print(\"xsize and ysize:\")\n", "print(text.xsize) # Will print the width of text in the x dimension\n", "print(text.ysize) # Will print the height of text in the y dimension\n", "print(\"center:\")\n", "print(text.center) # Gives you the center coordinate of its bounding box\n", "print(\"xmax\")\n", "print(text.xmax) # Gives you the rightmost (+x) edge of the text bounding box\n", "\n", "# Let's use these properties to manipulate our shapes to arrange them a little\n", "# better" ] }, { "cell_type": "code", "execution_count": null, "id": "88", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "c = gf.Component(\"canvas\")\n", "text = c << gf.components.text(\"hello\")\n", "E = gf.components.ellipse(radii=(10, 5), layer=(3, 0))\n", "R = gf.components.rectangle(size=(10, 5), layer=(2, 0))\n", "rect1 = c << R\n", "rect2 = c << R\n", "ellipse = c << E\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "89", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "ellipse.center = [\n", " 0,\n", " 0,\n", "] # Move the ellipse such that the bounding box center is at (0,0)\n", "\n", "# Next, let's move the text to the left edge of the ellipse\n", "text.y = (\n", " ellipse.y\n", ") # Move the text so that its y-center is equal to the y-center of the ellipse\n", "text.xmax = ellipse.xmin # Moves the ellipse so its xmax == the ellipse's xmin\n", "\n", "# Align the right edge of the rectangles with the x=0 axis\n", "rect1.xmax = 0\n", "rect2.xmax = 0\n", "\n", "# Move the rectangles above and below the ellipse\n", "rect1.ymin = ellipse.ymax + 5\n", "rect2.ymax = ellipse.ymin - 5\n", "\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "90", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "# In addition to working with the properties of the references inside the\n", "# Component,\n", "# we can also manipulate the whole Component if we want. Let's try mirroring the\n", "# whole Component `c`:\n", "\n", "print(c.xmax) # Prints out '10.0'\n", "\n", "c2 = c.mirror((0, 1)) # Mirror across line made by (0,0) and (0,1)\n", "c2.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "91", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "# A bounding box is the smallest enclosing box which contains all points of the geometry.\n", "\n", "c = gf.Component(\"hi_bbox\")\n", "text = c << gf.components.text(\"hi\")\n", "bbox = text.bbox\n", "c << gf.components.bbox(bbox=bbox, layer=(2, 0))\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "92", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "c = gf.Component(\"sample_padding\")\n", "text = c << gf.components.text(\"bye\")\n", "device_bbox = text.bbox\n", "c.add_polygon(gf.get_padding_points(text, default=1), layer=(2, 0))\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "93", "metadata": {}, "outputs": [], "source": [ "# When we query the properties of c, they will be calculated with respect to this bounding-rectangle. For instance:\n", "\n", "print(\"Center of Component c:\")\n", "print(c.center)\n", "\n", "print(\"X-max of Component c:\")\n", "print(c.xmax)" ] }, { "cell_type": "code", "execution_count": null, "id": "94", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "c = gf.Component(\"rect\")\n", "R = gf.components.rectangle(size=(10, 3), layer=(2, 0))\n", "rect1 = c << R\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "95", "metadata": { "lines_to_next_cell": 2 }, "outputs": [], "source": [ "# You can chain many of the movement/manipulation functions because they all return the object they manipulate.\n", "# For instance you can combine two expressions:\n", "\n", "rect1.rotate(angle=37)\n", "rect1.move([10, 20])\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "96", "metadata": {}, "outputs": [], "source": [ "# ...into this single-line expression\n", "\n", "c = gf.Component(\"single_expression\")\n", "R = gf.components.rectangle(size=(10, 3), layer=(2, 0))\n", "rect1 = c << R\n", "rect1.rotate(angle=37).move([10, 20])\n", "c.plot()" ] }, { "cell_type": "code", "execution_count": null, "id": "97", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "custom_cell_magics": "kql" }, "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }