how to calculate layer density in particular area by using python script

i have one doubt how to calculate density of layer i write one script that give every layer area now i want to total area of gds and density will be print layer name,area,density in on txt. formate.
import pya
gds_files = ["/openlane/work/cliped100.gds"]
KLAYOUT = pya.Layout()

for each_gds in gds_files:

KLAYOUT.read(each_gds)
print("Cells : ",KLAYOUT.top_cell().name)
print("Layers : ",KLAYOUT.layers())

#print("each",KLAYOUT.each_cell())
for ly_id in KLAYOUT.layer_indices():

#print ("Layer_info: ",KLAYOUT.get_info(ly_id))
layer_name =KLAYOUT.get_info(ly_id)
print('layer_name:',layer_name)
#sh = KLAYOUT.top_cell().each_shape(0)

# print ("shape area : ", sh)
layer = KLAYOUT.layer(layer_name)
#print(layer)
area1 = 0
for cell in KLAYOUT.each_cell():
#print(cell)
for shape in cell.each_shape(layer):
area1 += shape.area()
print(area1)

Comments

  • edited December 2023

    HI ajaout,

    Please format your code example using Markdown functuon, expecially for python code which requires correct indentation to work properly.

    You'll need to put all shapes in given layers into a region and merge shapes to get an accurate result.

    Use cell.begin_shapes_rec instead of cell.each_shape because cell.begin_shapes_rec returns a recursive iter, this includes the transformation info for shapes in instance.

    following code example basically covers the questions from these two repeated post
    2438 HOW TO EXTRACT KLAYOUT CONSOLE OUTPUT IN ONE FILE TXT FORMATE.
    2439 how to calculate area of my gds by python script.

    import pya
    outputTxt = "out.txt"
    filePaths = ["some path.gds"]
    results   = []
    for filePath in filePaths:
        layout   = pya.Layout()
        layerMap = layout.read(filePath)
        topCells = [cell for cell in layout.top_cells()]
        unit     = layout.dbu
    
        for cell in topCells:
            for layer_id in layout.layer_indexes():
                info   = layout.get_info(layer_id)
                region = pya.Region([rsi.shape().polygon.transformed(rsi.trans()) for rsi in cell.begin_shapes_rec(layer_id) if rsi.shape().polygon])
                result = f"{filePath}, {cell.name}, L({info.layer}, {info.datatype}), {region.merged().area()*unit*unit : .3f} um-sq"
                results.append(result)
    
    resultsStr = "\n".join(results)
    
    print(resultsStr)
    f = open(outputTxt, "x")
    with f : f.write(resultsStr)
    
Sign In or Register to comment.