how to extract polygons with specific number of points to a new layer?

Hi, Klayout team, this is my first post. I want to extract all the polygons with specific number of points and form a new layer. i tried to do it using DRC, but there is no command to get number of pts in the polygon. so i turned to ruby macro. However, i have some difficulties to output them into a new layer. here is my current code. Can someone help? thanks.

module MyMacro

include RBA

numPts = 64

ly = RBA::CellView::active.layout #active layout
lv = RBA::Application::instance.main_window.current_view
cell = ly.top_cell

#if lv == nil
# raise "No view selected"
# end

index = 0
lv.each_object_selected { |obj|
shape = obj.shape
if shape.is_simple_polygon?
if shape.polygon.num_points_hull == numPts
#extract the polygon or obj to a new layer
# need help here


} #obj



  • updates: I am able to make most of the part work by creating a new layout and cells and insert the shape to the new layout. However, i still have one big problem. There are so many pads on the design, the coordinates are in relative system and the relative coordinates are the same. After i insert all of them, there is only one pad in the final output. In addition, i am not able to get the transformation information using shape.array_trans (All 0s). Can someone help?

  • Hello,

    your code is more readable if you use Markdown formatting and put a single line with a triple backtick before and after your code.

    You can use DRC for your purpose if you know that using the "data" method you can access each layer's underlying "Region" object:

    # DRC script which selects (raw) polygons with 64 points
    # CAUTION: this function will flatten the layer. It will convert paths to 
    # polygons and apply the filter then:
    num_pts = 64
    inp = input(6, 0)
    outp = polygon_layer do |p|
      p.num_points == num_pts &&
    outp.output(1006, 0)

    If you want to just find polygons with 64 points, you can also use the "Search and Replace" feature with this custom query:

    polygons on layer 6/0 from cells * where shape.polygon.num_points == 64


  • Matt, thanks. i also resolved this problem using the shape iterator in Macro and applying the transformation. Thanks so much.

  • edited December 2019

    @Matthias thanks for your DRC script. One thing i feel confused is the relationship between DRC and Macro (Ruby). It seems that in your code provides an interface so that you can extend to use the classes and methods defined in Macro. The DRC has less functionality compared to what Marco offers except for the width and space checking. Am I right?

  • edited December 2019

    @Matthias , in your comments, # CAUTION: this function will flatten the layer. It will convert paths to polygons and apply the filter then.
    when a path is converted to polygon, how many of vertices the new polygon have? In the class Region, is it possible to define a function is_path? in order to extract the path using DRC?

  • Hi,

    no, a path cannot be distinguished from a Polygon. A DRC is a physical verification tool and physically it does not matter whether you draw something as path or polygon.

    The number of points are given by the Polygon that is created from the path.


Sign In or Register to comment.