Find the nearest polygon to a layout corner

Hi everyone,

I have a question about DRC.
I'm trying to figure out how to get the nearest polygon to a layout corner.
As you can see in the picture, I'm looking to get these four polygons.

Thanks for your help.

Comments

  • edited August 27

    Hi,

    How about this implementation with the KLayout Python Module?
    I'm not using the DRC function.


  • Hi @sekigawa,

    Thanks for the reply.
    Your solution is great. It does solve my question.
    But I'm wondering if there's a way to solve this with the DRC function.
    Thanks again for getting back to me.

    Best regards,

    JSS

  • edited August 27

    @sekigawa - thanks for your nice solution as usual :)

    And @JSS, no, there is no plain DRC solution as DRC is made for finding things in a specific distance, not the closest thing.

    But DRC can be mixed with Ruby code. Inside a DRC script, you can eventually define your own functions. Like this:

    def find_closest_to_corners(bbox, layer)
    
      # prepare a set of four corners and closest object collectors
      corners = RBA::DSimplePolygon::new(bbox).each_point.to_a
      closest = corners.collect { |c| [ nil, nil ] }
    
      # for each (merged) polygon on the input layer
      layer.each do |poly|
    
        # for each corner of the bounding box
        corners.each_with_index do |c,corner_index|
    
          # look if any point of the polygon is closer to any
          # closest point we found so far for this corner
          poly.each_point_hull do |p|
    
            # euclidian distance
            d = c.distance(p)
    
            # collect the closest objects
            if !closest[corner_index][0] || d < closest[corner_index][0]
              closest[corner_index] = [ d, poly ]
            end
    
          end
    
        end
    
      end
    
      # returns the closest objects
      result = polygons
      closest.each do |d,poly|
        poly && result.insert(poly)
      end
    
      result
    
    end
    
    report("Discussion 2578")
    
    l1 = input(1, 0)    # border
    l2 = input(2, 0)    # objects
    
    find_closest_to_corners(l1.bbox, l2).output("Objects closest to corners")
    

    Which gives this:

    Please note however, that this function is slow on large cases.

    Matthias

  • Hi @Matthias,

    Thank you for your reply, it is perfect for my needs!

    Cheers,

    JSS

Sign In or Register to comment.