How to mimic oddPolygon in Python to check for Self-Intersecting Polygons

edited June 13 in Python scripting

So I want to make a python script that also checks a GDS file for self-intersecting polygons. On the Klayout app, I have a DRC deck that uses the oddPolygon function to check for self-intersections. How can I translate that into Python. Does the API support that as a function? Trying my own sweep-line algorithm seems to give me different results to the KLayout checker. Help would be appreciated!

Edit: It should also find self-intersections (parts where they overlap) in paths like oddPolygon seems to do

Comments

  • edited June 19

    HI @aarush,

    Most DRC functions are basically method calls to "Region", "Edges", "EdgePairs" or "Texts" objects.

    In case of "odd_polygon", the method call is "strange_polygon_check" (see here: https://www.klayout.de/doc-qt5/code/class_Region.html#k_293).

    To mimic this check, construct a "Region" object with the layer you like to investigate and execute "strange_polygon_check".

    Matthias

  • So then does something like this work? Or does it iterate over the layers wrong?

        for cell in layout.each_cell():
            for idx in layout.layer_indexes():
                itr    = layout.begin_shapes(cell, idx)
                region = pya.Region(itr)
    
                region.merged_semantics = False
                strange = region.strange_polygon_check()
                strange.merged_semantics = False
    
                if not strange.is_empty():
                    info  = layout.get_info(idx)
                    count = strange.count()
                    issues.append(
                        f"In '{cell.name}' (L={info.layer},DT={info.datatype}): "
                        f"{count} self-intersecting piece(s)"
                    )
    

    Thank you for all the help,
    Aarush

  • No, that is wrong.

    A region is already a full layer - you must not iterate over the cells.
    You will need to start from a top cell instead.

    This should work better:

            cell = layout.top_cell()
            for idx in layout.layer_indexes():
                itr    = layout.begin_shapes(cell, idx)
                region = pya.Region(itr)
    ...
    

    Matthias

  • Thank you Matthias, this is now giving me the same output as the Klayout App!!!!

    Best,
    Aarush

Sign In or Register to comment.