Creating a new layout to view after a region has been created.

I am new to the KLayout API. I am creating a region in Python by taking the XOR of two regions. Once I have the new region, I would like to export it to a new layout using layout.write() so I can see my changes.

****Note: I would like to attach a GDS file that I am using for this. But it does not seem like I have the ability to. Could someone be kind enough to help me here so this question can be answered easily. Thanks!

# Here is my current script:
import pya
import os
import sys

def get_layer_name(lyr):
  layerMap = {
        13: 'ex1',
        15: 'ex2',
      }
  try:
    return True, layerMap[int(lyr)]
  except:
    return False, ''

def get_layer_index(layout, input_name):
  layer_idx = []
  for layer_index in range(layout.layers()):
    # Get layer info
    layer_info = layout.get_info(layer_index)
    layer_info_str = str(layer_info)
    # Only consider layers that are on the base level
    if layer_info_str[-1] != '0':
      continue
    # Determine the layer name
    found_flag, layer_name = get_layer_name(layer_info_str[:-2])
    if not found_flag:
      continue
    # If the layer name matches the desired name, return its index for
    # layout.begin_shapes(cell,index)
    if layer_name == input_name:
      layer_values = [int(x) for x in layer_info_str.split("/")]
      layer_idx = layout.find_layer(layer_values[0], layer_values[1])
      break

  return layer_idx

def main(pwd,device_name):
  gds_files = filter(lambda f: f.endswith( '.gds' ), os.listdir(pwd))
  layout = pya.Layout()
  layout_debugger = pya.Layout()

  # Loop each GDS file
  for gds in gds_files:
    print ( "\nfilename : ", gds )
    # get main window object and load layout file
    layout.read(pwd + '\\' + gds)

    # Determine layer indices
    ex1 = get_layer_index(layout, 'ex1')
    ex2 = get_layer_index(layout, 'ex2')


    # Only look at MOSFET cells 
    cells = layout.cells('*'+device_name+'*')
    if bool(cells):
      for cell in cells:
        region_1 = pya.Region.new(cell.begin_shapes_rec(ex1))
        region_2 = pya.Region.new(cell.begin_shapes_rec(ex2))
        region = region_1 ^ region_2

        # Here is where I would need to add something to layout_debugger
        new_device = layout_debugger.[something_should_go_here]()
        layout_debugger.write("ex.gds")

if __name__=='__main__':
  pwd = r'C:\Users\smanz\Documents\KLayout Learning'
  device_name = '32MOHM_MESHFET'
  main(pwd,device_name)

***** I am aware that I would be overwriting progress upon each loop. I am hoping to have some sort of visual of my results at any given time along the script and I am currently not aware of any other way to do this. Thank you ahead of time!

Comments

  • You can attach GDS files if you pack them into a .zip container.

    Are you sure XOR between two single layers is enough? If so, this is the code I'd put into the loop (disclaimer: not tested):

    # After: "Here is where I would need to add something to layout_debugger"
    # (with corresponding indents)
    
    # move this line from above to here
    layout_debugger = pya.Layout()
    
    debug_top_cell = layout_debugger.create_cell("DEBUG")
    
    # region_1 goes to layer 1/0
    debug_top_cell.shapes(layout_debugger.layer(1, 0)).insert(region_1)
    # region_2 goes to layer 2/0
    debug_top_cell.shapes(layout_debugger.layer(2, 0)).insert(region_2)
    # XOR goes to 100/0
    debug_top_cell.shapes(layout_debugger.layer(100, 0)).insert(region)
    
    layout_debugger.write("ex.gds")
    

    Matthias

  • @Matthias
    Awesome. This is exactly what I ended up doing. Thank you very much..

    As an aside but from a similar vein, let's say I have a numpy array that can be considered to be a set of coordinate pairs, is there a way to create a region for this and plot out into a gds file in the same way?

    Best,
    Steven

  • Yes, you can generate a polygon from an array of points

    array = [ [ 0, 0 ], [ 1000, 1000 ], [ 1000, 0 ] ]
    polygon = pya.Polygon([ pya.Point(x, y) for x, y in array ])
    

    (I guess that also works with numpy arrays)

    Note that the coordinates need to be given in integers (multiples of the layout's database unit, 1nm by default). You can feed a region with multiple polygons using "Region.insert" or directly insert the polygons into the Cell's shapes container like you do with the regions.

    Matthias

Sign In or Register to comment.