Extract Polygons from a layer, flatten and store it in CSV

Hi, i am new to KLayout python scripting. Given a layer and sublayer I want to extract all polygons, flatten it and store it in CSV format. Is this possible? Also i want to know how to get started with python scripting? I say few examples posted here but don't know it's full potential, what are functionality available. Where can I learn this?


  • Hi!

    You could access polygons from Layout.begin_shapes(). Current layout object is accessible from CellView.active().layout(). See also scripting documentation.

  • Not Python but here is some Ruby, might be of help

     ly = RBA::CellView.active.layout
     cell = RBA::CellView.active.cell
     layers = ly.layer_indexes.collect { |li| info = ly.get_info(li); [ info.layer, info.datatype, li ] }.sort
    # fetch display names
    names = {}
    RBA::LayoutView.current.each_layer do |lref|
     if lref.name != "" && lref.cellview == RBA::CellView.active.index
           names[lref.layer_index] = lref.name
    puts "Non-empty layers of cell #{cell.name} (including subcells):"
           layers.each do |layer,datatype,li|
               if ! cell.bbox_per_layer(li).empty?
                   if names[li]
                      puts "  #{layer}/#{datatype} [" + names[li] + "]"
                puts "  #{layer}/#{datatype} (unnamed)"
  • Thanks for the reply @EugeneZelenko and @tagger5896 . But this script is for printing layer and datatype. But my concern is to extract polygons in a specific layer.

  • Layout.begin_shapes() returns RecursiveShapeIterator object that has shape property. See Shape documentation to learn how to access polygon properties.

  • edited February 4

    Thanks again @EugeneZelenko . I think this is the way to do it

    for c in layout.each_cell():
      iter = layout.begin_shapes(c, layer)
      while not iter.at_end():
        if iter.shape().is_polygon():
          polygon = iter.shape().polygon.transformed(iter.trans())
          print("In cell " + iter.cell().name + ": " + polygon.to_s())
  • Very good, that's exactly how to do it :)

    A remark of mine: in addition to "is_polygon" you may also allow "is_box" and "is_path" (like if iter.shape().is_polygon() or iter.shape().is_box() ...). These types can be converted to a polygon too.

    Thanks to everyone for this discussion!

    Best regards,


  • edited February 9

    Thanks. Is there by any chance we can include sublayer(datatype) also layout.begin_shapes(c, layer). something like this in python instead of DRC
    inp = input(13, 0)
    outp = polygon_layer
    inp.data.each do |p|
    outp.output(1006, 0)


  • Hi, Vandhana!

    layer parameter is index that could be found with Layout.find_layer(). There are several variants of latter that allow to specify layer and datatype.

  • Hi Eugene,
    Sorry. I misunderstood. Instead of layer index, I thought layer was layer number. Thanks for explaining :smile: It's of great help.

  • Very good :)

    Yes, layer is the logical index. It's not related to GDS layer - a layout can have unnamed layers and layers with names only (e.g. from CIF). The layer index is a generic identifier. Think of it as a layer handle.


Sign In or Register to comment.