It looks like you're new here. If you want to get involved, click one of these buttons!
Hi Matthias, Hi KLayout community,
I'd like to iterate through all instances of "baseCell" in the "top" cell and remove the instance from the "top" cell if the bounding box of the instance crosses the hull of a polygon "poly2".
Example code:
import pya
import math
import numpy as np
layout = pya.Application.instance().main_window().current_view().active_cellview().create_layout()
top = pya.Application.instance().main_window().current_view().active_cellview().create_cell()
L1 = layout.layer(1, 0)
dbu = layout.dbu
poly = pya.DPolygon.new([pya.DPoint(-5,-5),pya.DPoint(-5,-5),pya.DPoint(-5,-5), pya.DPoint(-5,-5)])
poly.round_corners(1,1,32)
baseCell = layout.create_cell('baseCell')
baseCell.shapes(L1).insert(poly)
transl = pya.DCplxTrans(15/dbu, 15/dbu)
# create an array instance of baseCells
instArray = pya.CellInstArray.new(baseCell.cell_index(), transl, 20, 20)
# insert the instance into "top" layer
top.insert(instArray)
# create a polygon for the instance removal
poly2 = pya.DPolygon.new([pya.DPoint(0,0),pya.DPoint(200,0),pya.DPoint(200,200), pya.DPoint(0,200)])
poly2.round_corners(10,10,32)
# insert the polygon into "top" cell
top.shapes(L1).insert(poly2)
# all the instances of baseCell in the the "top" cell should now be removed if the bounding box of the instance crosses the hull of poly2
#....
I'd very much appreciate if someone could help to solve this problem.
Maybe looping through dpoly.each_point() to check, whether the instance crosses the hull points of the polygon "poly2", could do the trick. However I can't figure out the implementation of a loop through all the instances.
Additionally, running the code above as is does nothing. Do macros always have to be included in a library and instantiated as a new instance to create a layout in KLayout?
Comments
Hi @Sepbe,
The solution gets somewhat difficult if you consider the case of array instances: if one instance of an array cross the polygon boundary, you first need to flatten the array.
Without considering array instances, here is some code. It uses
Region
objects and specifically a combination of "not_inside" and "not_outside" to check if an instance is on the boundary:And here is the effect:
Before:
After:
If you want to consider arrays, you can add a selective explode step before the collection of the "to_remove" instances:
Matthias
Hi @Matthias,
the instances in my application are actually single instances and are not array instances.
Therefore, your code works perfectly, thank you so much.
Kind regards
Sepbe