Using ROUND_PATH Pcell from BASIC library in making another PCell

Hi KLayout Community,
I would like to instantiate ROUND_PATH cell in another pcell that I trying to write. I see that it is doable from other posts in the forum. However, there is no clear example of instantiating ROUND_PATH in creation of other Pcell.
I have looked at other posts and tried to do. I have not been successful in doing so.

Trial #1==> using create_cell method : this method deletes the object as soon as it is created

path_points = [,0),,0),,20)]
path_width = 1
path =, path_width)
param = { "layer" : self.l_layer, "radius" : 5.0, "npoints" : 16, "path" : path}
pcell =self.create_cell("ROUND_PATH", "Basic", param)
t = pya.Trans(0, 0)
self.cell.insert(pya.CellInstArray(pcell, t))

Trial #2 ==>using add_pcell_variant method : this crashes the whole klayout

 basicLib =  pya.Library.library_by_name("Basic")
rPath =basicLib.layout().pcell_declaration("ROUND_PATH")
pcell =pya.CellView().active().layout().add_pcell_variant(basicLib,, param)
t = pya.Trans(0, 0)
self.cell.insert(pya.CellInstArray(pcell, t))

Can someone help with the code. Thanks for your attention.


  • edited June 16


    As you just provide snippets and let me guess the rest of your code, here is a working example that demonstrates the solution based on the second attempt (the first one isn't working because of reasons you can look up in this forum in other places):

    import pya
    class PCell1536(pya.PCellDeclarationHelper):
      def __init__(self):
        super(PCell1536, self).__init__()
        self.param("layer", self.TypeLayer, "Layer", default = pya.LayerInfo(1, 0))
      def display_text_impl(self):
        return "any"
      def produce_impl(self):
        path_points = [,0),,0),,20)]
        path_width = 1
        path =, path_width)
        param = { "layer" : self.layer, "radius" : 5.0, "npoints" : 16, "path" : path}
        basicLib =  pya.Library.library_by_name("Basic")
        rPath = basicLib.layout().pcell_declaration("ROUND_PATH")
        pcell = self.layout.add_pcell_variant(basicLib,, param)
        t = pya.Trans(0, 0)
        self.cell.insert(pya.CellInstArray(pcell, t))
    class PCell5136Lib(pya.Library):
      def __init__(self):
        self.description = "Discussion 1536"
        self.layout().register_pcell("PCell1536", PCell1536())

    You made three mistakes:

    First using "l_layer" for the layer parameter: this is the actual layer index to use in "cell.shapes(layer_index)". For passing to another PCell you need the "LayerInfo" object which is stored in "layer" (without the "l_" prefix). This is explained here:

    Second, using "pya.CellView().active().layout()" for the target layout. Use "self.layout()" for this purpose as the PCell is generated in a separate layout, not the client layout. Essentially your code instantiates a cell from the from wrong layout object which is why the application crashes.

    The final mistake is to try this at all. There are a couple of discussions in this forum about the topic of instantiating PCells inside other PCells.
    I'm going to repeat my reply now once again: just don't do it. A PCell is just an extremely complicated way to call a procedure which creates geometry. Structure your code in procedures creating the proper subelements and code the rounding of corners explicitly (that's fairly simple math). You don't need a PCell to do something like a rounded path. A few lines of code will do the same and without the overhead implied by an indirect PCell call.

    There is one exception which is the TEXT PCell which is useful to generate labels. In order to avoid having to create TEXT PCells inside other PCells I have supplied the TextGenerator class which does text generation without need for a PCell call.


  • Hi Matthias, Thanks for the working code and a clear explanation of why it doesn't work. Appreciate your time and attention. I will take your third suggestion and try to build the functionality within the pCell. Apologies for not going through the other questions thoroughly.

Sign In or Register to comment.