How to modify some polygons with multiple points?

edited September 28 in General

Hi,
I get a question about how to modify polygons with multiple points.
Sometimes, layout needed to be simplified as some information take a lot of time to deal with when used in other simulation software.
For example, a polygon like this:

Stepped graphics takes a lot of time to deal with and those detailes are not very important in some cases.
1. So I want to get those polygons which have multiple points, maybe polygons with points >20 can be selected. Probabily those polygons are what we want.
2. Maybe I can calculate the space between the points of those polygons, if the space of two points are small, for example, 10um, then I can remove those points, so the points can be reduced.
But i am not farmiliar with python in klayout. I have searched a example, but I don't know how to get the point number and points of a polygon. And how to search for the full layout, but this script seems to make function only when I have selected those polygons.

import pya

print('\n\nStarting KLayout Script')
lv = pya.LayoutView.current()
currently_selected_polygons = lv.object_selection # list of ObjectInstPath

if not currently_selected_polygons :
print('no polygons selected in KLayout window')

for o in currently_selected_polygons:
if (o.shape.polygon.is_box()):
bb = o.shape.polygon.bbox()
ll, ur = bb.p1, bb.p2
#points= o.shape.polygon.points()
#points= o.shape.polygon.point_hull()
print(f'{points}' )
#print(f'Selected rect lower left ({ll.x}, {ll.y}) , upper-right ({ur.x}, {ur.y})')
#print(f'{ll.x}')

Could anyone give me some information?

Best regards!
Weiling

Comments

  • edited September 26

    The 'smooth' function might be good to use. It removes a specified 'roughness' of shapes.

    You can do this with the DRC engine here:
    https://www.klayout.de/doc-qt5/about/drc_ref_layer.html#smoothed
    Example

    #input layers
    metal = input(11, 0)
    
    #derived layers
    smooth_metal = metal.smoothed(5.um)
    smooth_metal.output("smoothed_metal")
    

    Or if you want a ruby/python implementation (good for applying to specific shapes):
    https://www.klayout.de/doc-qt5/code/class_Polygon.html#k_91

    Note: If you have geometries that have similar sizes as your roughness, then those will/may change.

    My brain wants to believe I saw some other methods while digging through documentation a few months ago, but I can't recall...

    Hope this helps, though.

  • @blueman_44,

    Thank you very much for your respond.
    It makes effect in an easy way. I can get an approximate pattern in this way.

    Anyway, just to extend the knowledge, do you know how to get polygons points xy of all layers in a gds and to remove some points of those polygons?

    Best regards!
    Weiling

  • In DRC you can do something like this to get all points per polygon in a string output:

    deep    #usually faster but you can remove to perform a flat operation
    
    #Go through each layer in current view
    layers.each {|lay|
        input(lay).data.each {|shape|
            puts "#{lay}|#{shape}"
        }
    }
    

    Here's a python way that can also work:

    cellview = pya.CellView.active()
    ly = cellview.layout()
    cell = cellview.cell
    
    input_layer = ly.layer(5, 0)   
    input_region = pya.Region(cell.begin_shapes_rec(input_layer)).merged()
    
    for shape in input_region.each():    
        poly = shape.to_simple_polygon()
        for point in poly.each_point():
            ...
    

    I don't believe you can remove points from polygons easily. My way would be to make a new polygon and insert the list of points you'd want to keep from the original.

    I did find another method to reduce amount of points on a polygon by removing redundant shapes:
    http://klayout.de/doc-qt5/code/class_Polygon.html#k_29

    You can also reference documentation here for polygon methods:
    https://klayout.de/doc-qt5/code/class_SimplePolygon.html

  • Hi sir,
    I am layman for python, but here is the code I can provide to you for referece.
    maybe that can help you something.
    (check the pattern layer , if the number_points >20 , output it to another layer,
    of couse , you could base on that to process what your code.

    outs = polygon_layer
    dark=input(93,0)
    dark.each do |check_point|
    if check_point.num_points > 20 then
    outs.insert(check_point)
    end
    end
    outs.output(100,1000)
    
  • Maybe you can base on this flow..

    1.check the number of points if that is more than 20 --->type A
    other -->type B

    type B , keep it.
    type A , smooth or remove some of points of each pattern.

Sign In or Register to comment.