How to use Python scripts to implement Boolean operations between shapes on the same layer?

I found relevant examples on the website https://www.klayout.org/klayout-pypi/examples/cheese/, but the problem I am currently facing is that the two shapes that need to be Boolean operated are on the same layer.




How should I solve this problem?

Comments

  • The picture shows the demonstration of the operation directly in the Klayout software

  • edited February 1

    Hello,

    You can make a small drc script:

    layer2 = layer1.raw.merged(2) # this will give you the overlaps

    layer3 = layer1 - layer2

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

    Cheers,

    Tomas

  • Thank you Tomas, I wrote the following code to solve the above problem

    import klayout.db as db

    ly = db.Layout()
    ly.read("contours.gds")

    top_cell = ly.top_cell()

    l1 = ly.layer(1, 0)
    r1 = db.Region(top_cell.begin_shapes_rec(l1))
    r2 = r1.merged(2)
    r3 = r1 - r2

    ly.clear_layer(l1)
    top_cell.shapes(l1).insert(r3)

    ly.write("contour_process.gds")

  • edited February 1

    Indeed, region is similar... Maybe for huge layouts drc is faster?

    It looks fancy, may I know what's it for?

    Cheers,

    Tomas

  • Hi @Emrecan,

    In GDS, the general interpretation of overlapping shapes is to "combine" into a single shape. This is called "merged semantics" in KLayout and is the default assumption in general. So the islands you highlight are basically irrelevant for the boolean operations.

    I assume your sample is not a VLSI layout - it rather looks like geographic or image data? So maybe in that domain the expectation is differently.

    Problem is, that DRC or Python's Region object has limited support for handling overlapping shapes. Please see the reply of @tomas2004 - the "merged(2)" approach is actually the best option to separate overlapping shapes.

    But that leaves the question open, how to handle triple overlaps or partial overlaps. In VLSI, that problem does not exist as shapes are always cleanly separated into layers.

    Matthias

  • Thanks Matthias,I got it.

Sign In or Register to comment.