Can we automate the process of merging layers?

I follow such steps: Edit -> Layer -> Merge

Then I can merge layers.
However, I want to automate this process by using Macro Development. I'd like to know which class or procudure I should use.

Comments

  • Hello,

    You can do that easily in the DRC environment (Macro Development > DRC):

    https://www.klayout.de/doc-qt5/manual/drc_basic.html

    https://www.klayout.de/doc-qt5/about/drc_ref_layer.html#h2-1534

    layer.merge([overlap_count])

    Cheers,

    Tomas

  • @tomas2004 Thanks for the answer :)

    I agree, but like to add you need to write it too ... so the above example equals to the DRC one-liner:

    input(1, 0).merged.output(1, 0)
    

    ("merge" as a method will merge a copy of the layer, not the layer in the database).

    Matthias

  • edited July 2021

    @Matthias Thank you soooo much!

  • @tomas2004 Thanks!!! It helps a lot.

  • I want to add 'Hierarchy' "Individually for current and subcells (semi hierarchical)" to

    input(2, 0).merged.output(2, 0),

    as the image below:

    Then, I can merge automatically specific layers for multiple cells at the same time.

    Thanks...

  • edited May 2024

    Hi @Wanchus,

    Please consider opening a new discussion rather than reusing an old one.

    The option you ask for is not available in the DRC language. It is a special option for the UI feature.

    You can try using deep mode:

    deep
    input(2, 0).merged.output(2, 0)
    

    This will leave the merged shapes inside the subcells unless the shapes merge with geometry from outside the subcells. In that case, deep mode will propagate the merged shapes into the top cell.

    Matthias

  • Hi @Matthias,

    Thank you. Sorry for the late reply. Is there a way to stop the merging 'propagation' with geometries outside the subcells?

    Thanks,
    Wanchus

  • Hi @Wanchus,

    not with DRC as DRC is follows the "as if flat" paradigm - i.e. what matters is the top-level view, not the details. Merged shapes are put as low in the hierarchy as possible, but are guaranteed to be non-overlapping.

    "cell-local" merge for a specific layer can be achieved with this small Python script which even has undo:

    import pya
    
    ly = pya.CellView.active().layout()
    lv = pya.LayoutView.current()
    
    try:
    
      # undo support
      lv.transaction("Merge shapes")
    
      # Example: merge layer 14/0
      layer_to_merge = ly.layer(14, 0)
    
      for c in ly.each_cell():
        shapes = c.shapes(layer_to_merge)
        r = pya.Region(shapes)
        r.merge()
        shapes.clear()
        shapes.insert(r)
    
    finally:
      lv.commit()
    

    Matthias

  • Hi @Matthias,

    Thank you so much for the Python script example and sorry for the late reply.

    Cheers,
    Wanchus

Sign In or Register to comment.