heitzmann / gdspy

Python module for creating GDSII stream files, usually CAD layouts.
Boost Software License 1.0
348 stars 126 forks source link

Error with boolean operations between cells or polygonsets #242

Open jayshah15028 opened 1 month ago

jayshah15028 commented 1 month ago

I am trying to use boolean operations for patterning some boxes on some polygons and here is a sample code for it.

from gds_lib import gdsLib
from math import floor,radians,ceil,cos,sin
import numpy as np

def RotatedRectangle(height,width,cell,center,angle):
        point1 = np.array([[-1*(width/2)],[height/2],[1]])
        point2 = np.array([[(width/2)],[height/2],[1]])
        point3 = np.array([[width/2],[-1*(height/2)],[1]])
        point4 = np.array([[-1*width/2],[-1*(height/2)],[1]])
        if center == 0:
            a = 0
            b = 0
        elif center == 1:
            a = -width/2
            b = height/2
        elif center == 2:
            a = 0
            b = height/2
        elif center == 3:
            a = width/2
            b = height/2
        elif center == 4:
            a = width/2
            b = 0
        elif center == 5:
            a = width/2
            b = -height/2
        elif center == 6:
            a = 0
            b = -height/2
        elif center == 7:
            a = -width/2
            b = -height/2
        elif center == 8:
            a = -width/2
            b = 0
        angle = radians(angle)
        transformationMatrix = np.array([ [cos(angle), -sin(angle), a], [sin(angle), cos(angle), b] ])
        point1 = np.matmul(transformationMatrix,point1)
        point2 = np.matmul(transformationMatrix,point2)
        point3 = np.matmul(transformationMatrix,point3)
        point4 = np.matmul(transformationMatrix,point4)
        rect = gdspy.Polygon([point1, point2, point3, point4])
        return rect

gds = gdspy.GdsLibrary()
cell = gds.new_cell('top')

height = 1
width = 1
rect1 = RotatedRectangle(height = height, width = width,cell=cell, center=0, angle=0 )
cell.add(rect1)

checkerboard = gds.new_cell('checkerboard')

checkerbaord_area_height = 20
checkerboard_area_width = 15

n_rows = ceil(checkerbaord_area_height/height)
n_cols = ceil(checkerboard_area_width/width)

start_center_x = -(n_rows-1)*width/2
start_center_y = (n_cols-1)/2
start_center = (start_center_x,start_center_y)

for i in range(n_rows):
    x = start_center_x + (i%2)*width
    y = start_center_y - i*height
    arr = gdspy.CellArray(ref_cell=cell,columns=n_cols,rows=1,spacing=(width*2,0),origin=(x,y))
    checkerboard.add(arr)

rect = RotatedRectangle(height=11,width=5,cell=cell,center=0,angle=0)
new_cell = gds.new_cell('new_cell')
new_cell.add(rect)

checkerboard.flatten()
polysets = checkerboard.get_polygonsets()
print(polysets)
polysets2 = new_cell.get_polygonsets()

ref1 = gdspy.CellReference(new_cell)
ref2 = gdspy.CellReference(cell)
inters = gds.new_cell('inters')
int_result = gdspy.boolean(polysets,polysets2,'not')
inters.add(int_result)
gds.write_gds('sample.gds')

In the boolean operation, I tried polygonsets, reference cells ref1 and ref2. But I keep getting the same error, which is as follows:

TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "c:\Users\js95642\PyCharmProjects\GDS_Generator\sample.py", line 85, in int_result = gdspy.boolean(polysets,polysets2,'not') File "C:\Users\js95642\AppData\Roaming\Python\Python38\site-packages\gdspy\operation.py", line 259, in boolean result = clipper.clip(poly1, poly2, operation, 1 / precision) SystemError: returned a result with an error set

Can you please help me figure out what exactly is the problem here? I believe the clipper algorithm expects inputs in some formats and that might be getting messed up (although the documentation mentions that it can accept polygonsets and cell references). I am using Python 3.8.

Your help is much appreciated. Thanks.

Addition: When I use the .flatten() function, the gds file does not open in KLayout (I checked this by commenting out the boolean operation line). Can you please also suggest why this is happening?