Keeping hierarchy after boolean operations with python

Hi Matthias,
I am trying to make boolean operations on cells, and I would like to keep the hierarchy when using python.
With ruby I figured that it's easy, just by enabling the deep hierarchy mode. It does exactly what I would like to do, putting the result of boolean operations in the child cells as much as possible.
With python, I tried to add a DeepShapeStore to my regions, but the result of the boolean operations goes to the top cell, instead of the child cells.
I guess I'm missing something in my implementation.
Could you please help me with this?

Here are the two scripts that I try to make identical (the source and results are attached in the zip file):

RUBY:

# Script applying booleans to cells, and keeping the hierarchy in the output file

# specifies input (file, top cell) from klayout command line
src = source("test_hierarchy.GDS", "TOP")

# specifies output
target("test_hierarchy_bool.GDS", src.cell_name)

# enable deep (hierarchical) operations
deep

data_A = input(100, 0)
data_B = input(101, 0)
data_C = input(26, 0)

source.layers.each do |lp|
# copy all other layers
input(lp).output(lp)
end

(data_A - data_B).output(102, 0)
(data_A - data_C).output(105, 0)


PYTHON:

import pya

ly = pya.Layout()
ly.read("test_hierarchy.GDS")

dss = pya.DeepShapeStore()

rA = pya.Region(ly.top_cell().begin_shapes_rec(ly.layer(100, 0)), dss)
rB = pya.Region(ly.top_cell().begin_shapes_rec(ly.layer(101, 0)), dss)
rC = pya.Region(ly.top_cell().begin_shapes_rec(ly.layer(26, 0)), dss)

r1 = (rA - rB)
ly.top_cell().shapes(ly.layer(102, 0)).insert(r1)
r2 = (rA - rC)
ly.top_cell().shapes(ly.layer(105, 0)).insert(r2)

ly.write("test_hierarchy_bool.GDS")


Maybe I can also find somewhere how the "deep", "input", and "output" functions are implemented with the API? I tried to find it on github but I didn't manage.

Thanks in advance!
Florian

Comments

  • Hi Florian,

    the above script is Ruby, but actually it is DRC language. So it looks differently by intention :)

    The Python code is basically correct. The DeepShapeStore thing is what is used internally by DRC and it actually works, but needs a specific way to store back the results of an operation.

    If you use

    ly.top_cell().shapes(ly.layer(102, 0)).insert(r1)
    

    The contents of r1 will be inserted into the top cell's shape container. As the shape container is for the top cell only, the results will become flattened.

    The hierarchical way of doing that is the other way round:

    r1.insert_into(ly, ly.top_cell().cell_index(), ly.layer(102, 0))
    

    In contrast to Shapes#insert, this version has a license to use sub-hierarchy below the given cell.

    Matthias

  • Hi Matthias,

    Thank you so much for the quick and detailed answer!
    It's difficult for me to understand exactly how to manipulate layouts, shapes, regions, layers, and cells, and how they are interconnected in the API functions.
    But I also don't need complex manipulations, so it's OK for now.
    Now I'm back on track thanks to you :)

    Have a good day,
    Florian

Sign In or Register to comment.