Skip to content

Instantly share code, notes, and snippets.

@silverlyra
Forked from g2hollow/calc_area.lym
Created June 8, 2023 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silverlyra/371f7824658364c681bb59095f807183 to your computer and use it in GitHub Desktop.
Save silverlyra/371f7824658364c681bb59095f807183 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment