Skip to content

Instantly share code, notes, and snippets.

@g2hollow
Last active November 10, 2023 14:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save g2hollow/fc187c21e94a92a3c213f49661769f94 to your computer and use it in GitHub Desktop.
Save g2hollow/fc187c21e94a92a3c213f49661769f94 to your computer and use it in GitHub Desktop.
klayout macro qt area calculator
import pya
class AreaCalculator(pya.QDialog):
"""
This class implements a dialog for calculating area of shapes
in a layout. The calculator adds up shapes in the currently
selected cell and below.
"""
def button_clicked(self, checked):
""" Event handler: "Calculate area" button clicked """
view = pya.Application.instance().main_window().current_view()
layout = view.cellview(0).layout()
cell = view.cellview(0).cell
#create KLayout region object to use for grabbing shapes
reg = pya.Region()
total_area = 0
#1/0, 2/0
layer_input = self.layer_input.selectedItems()
layer_input = [j.text for j in layer_input]
#layer_input = self.layer_input.text
#layer_input = layer_input.split(",")
#layer_input = [j.strip() for j in layer_input]
layer_indexes = layout.layer_indexes()
for idx,lyr in enumerate(layout.layer_infos()):
if str(lyr) in layer_input:
#Use the Region insert function on a recursive shape iterator
#that is a function of the layout. This handles all the
#complicated work of finding shapes in the current cell and
#all child cells.
reg.insert(layout.begin_shapes(cell,layer_indexes[idx]))
total_area += reg.area()/1e6
# get the area and place it in the qt dialog
self.area.setText("Total Area: "+str(total_area))
def __init__(self, parent = None):
""" Dialog constructor """
super(AreaCalculator, self).__init__()
self.setWindowTitle("Area Calculator")
self.resize(400, 120)
qt_layout = pya.QVBoxLayout(self)
self.setLayout(qt_layout)
self.layer_label = pya.QLabel("Select the layers", self)
qt_layout.addWidget(self.layer_label)
#original implementation using a textbox for getting the layers
#self.layer_input = pya.QLineEdit('',self)
#qt_layout.addWidget(self.layer_input)
#second implementation where we pull layers from the file, and
#display them in a dropdown menu (QListWidget)
view = pya.Application.instance().main_window().current_view()
layout = view.cellview(0).layout()
layers = layout.layer_infos()
layers = [str(j) for j in layers]
self.layer_input = pya.QListWidget(self)
self.layer_input.setSelectionMode(pya.QAbstractItemView.ExtendedSelection)
self.layer_input.addItems(layers)
qt_layout.addWidget(self.layer_input)
self.area = pya.QLabel("Press the button to calculate the area", self)
qt_layout.addWidget(self.area)
button = pya.QPushButton('Calculate area', self)
button.setFont(pya.QFont('Times', 18, pya.QFont.Bold))
qt_layout.addWidget(button)
# attach the event handler
button.clicked(self.button_clicked)
# Instantiate the dialog and make it visible initially.
# Passing the main_window will make it stay on top of the main window.
dialog = AreaCalculator(pya.Application.instance().main_window())
dialog.show()
@venusans
Copy link

Hi. I get a problem with this code. It causes this error :

'NoneType' object has no attribute 'cellview'

I saw the youtube tutorial video and that seems it works properly. But I get this problem and I don't know why.
Could you please help me and tell me why I get this error and how to solve it ?

@g2hollow
Copy link
Author

@venusans This error suggests that there are no 'views' open. You'll need to have a file open in order for the area calculator to have something to look through for shapes and therefore areas. Does this seem like a possible cause for your issue? If not, can provide more information about the file, operating system, and version of KLayout you're using.

@venusans
Copy link

venusans commented Mar 3, 2022

@venusans This error suggests that there are no 'views' open. You'll need to have a file open in order for the area calculator to have something to look through for shapes and therefore areas. Does this seem like a possible cause for your issue? If not, can provide more information about the file, operating system, and version of KLayout you're using.

Thank you. This was exactly my problem and It has been solved.

@mikronmaker
Copy link

@g2hollow, the calculator worked for one instance and doesn't work anymore. The pop up window opens and doesn't give the layer list to select and click to calculate the area. i am using klayout 0.28.7

@mikronmaker
Copy link

"xml parser error: error occurred while parsing element in line 1, column 1" is the error I am getting if I choose to import the .lym

@g2hollow
Copy link
Author

g2hollow commented Jun 6, 2023

"xml parser error: error occurred while parsing element in line 1, column 1" is the error I am getting if I choose to import the .lym

I just tested this on 0.28.8 with no problems. I created new blank macro and copy and pasted the text in. Saving the lym in the appropriate folder would also work. Test structure contained hierarchy of shapes with multiple instances. The layers showed up properly, and the calculated area was correct. I'm not sure how you're importing the .lym, please refer to video, and use the macro development window: https://www.youtube.com/watch?v=JFh-h1YgO2Y.

@mikronmaker
Copy link

I used the regular macro rather than QT option. it works now. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment