PCell creation via python procedure


I've created a function which prepares a pcell instance for the user to drag and drop it into the layout from Cells Category. The function:

def createPCellInstance(pcell_name='CIRCLE', lib_name='Basic', params={}) :
    layoutview = pya.LayoutView().current()

    cellview = pya.CellView().active()
    view     = cellview.view()
    layout   = cellview.layout()

    pcell = layout.create_cell(pcell_name, lib_name, params)
# createPCellInstance

The problem is that I can't double-instantiate a pcell with the same pcell parameters.

If I run the above function, place the pcell, and then re-run the function, the second run does not produce a new pcell, neither it fails.

If I make a change in the already created pcell, then the second run seems to succeed. How I can overcome this issue?



  • edited July 2019

    Hi Chris,

    The reason you don't get two cells is that the create_cell function creates a PCell variant. If I understand the KLayout concept of PCells enough, there can only be one PCell variant for a given set of parameters. The PCell variants are calculated and stored in seperate layout. When instantiating the PCell the separate layout is simply referenced and therefore the PCell doesn't really exist multiple times (with the same set of parameters).

    The code below shows how to instantiate a PCell multiple times. What is missing in your code is the layer_index in the default set of paramaters, whitout one you won't see much of the cell.

    import pya
    def createPCellInstance(pcell_name='CIRCLE', lib_name='Basic', params={}) :
        # Get PCell Library
        lib = pya.Library.library_by_name(lib_name)
        # The PCell Declaration. This one will create PCell variants.
        pcell_decl = lib.layout().pcell_declaration(pcell_name)
        layoutview = pya.LayoutView().current()
        cellview = pya.CellView().active()
        view     = cellview.view()
        layout   = cellview.layout()
        # Get the top cell. Assuming only one top cell exists
        top_cell = layout.top_cell()
        # Add a PCell variant
        pcell_var = layout.add_pcell_variant(lib,pcell_decl.id(),[])
        # Insert first instances
        # Insert & move the second instance (Move so we can see it)

    I hope this helps. The instantiation is slightly more complicated, but I hope you can understand it.


  • edited July 2019

    Hi Sebastian,

    Thank you for your response!

    Note that I do not want to insert my pcell into layout, but instead I want the user to select it from the Cell category from the main window, and drag-n-drop it to the layout (intentionally I didn't include the last insert statement in my code).

    Unfortunately, I have the same behaviour that I got from my version, which it seems correct, if we take as granted that both versions are similar.


  • If you don't want to directly insert them, then just create the cell array instance (pya.CellInstArray) and place it with the drag&drop handling.

    If I understand you correctly, you want a similar functionality like the Instance button (but with drag&drop instead).

  • Hi Sebastian,

    Yes ideally that was the idea.

    I think that the step of CellInstArray is happening after the creation of the PCell (create_cell/add_pcell_variant - lists the pcell into the Cells directory), If I didn't do any obvious mistake in the flow, it won't help unfortunately.


  • I think I finally understand what you mean. You mean the this view, right?

    And then by drag&drop you move the cell into the layout? You can just drag&drop the cell multiple times, it will create an instance each time. The cell will move in the hierarchy the first time, but then you can drag&drop it again and it will not move in the hierarchy any more.

    As seen here:

  • edited July 2019

    Hi Sebastian!

    Preferably, I wanted a way either instantiate the pcell overcoming this issue, or somehow, to got a response that will say that the pcell instantiation failed because it is already there.
    Not an ideal scenario, but this is what I will have to prompt in the end, explaining to the User the flow of your last response, which is not very UX convenient unfortunately.


  • edited July 2019

    Hi Jim,

    (@sebastian: thanks for your answers).

    I understand that you want to way to check whether there is already a PCell with the given parameters, right?

    There is no "has_pcell_variant?" method or similar yet (but good suggestion), but you can basically traverse the Cells and check whether there already is a PCell variant of the requested parameter set. Here is a sketch (not tested):

       # given: lib_name, pcell_name, params (dict of name vs. value)
       for cell in layout.each_cell():
          if cell.is_pcell_variant() and 
             cell.is_library_cell() and 
             cell.library().name() == lib_name and 
             cell.pcell_declaration().name() == pcell_name:
            p = cell.pcell_parameters_by_name()
            if compare(p, params):
              # there already is such a PCell

    The "compare" function needs to be a little elaborate as it should take care of double values (compare with a certain fuzzyness) and be aware of parameters in "p" which are not listed in "params".


  • Hi Matthias,

    (I'm Chris by the way :p )

    No, in the original post I was saying that I've created a function which prepares a pcell instance for the user to drag&drop it into the layout from Cells Category. But it seems that I can't double-instantiate the pcell with the same pcell parameters by using that function. If I use the function, place the pcell, and then re-run the function, the second run does not produce a new pcell, and neither it fails.

    As a last resort, I meant that I will produce the "failure" - I've already created something similar to your code, although without the check of is_pcell_variant method, which prompts an error message box-, and I will have to explain what the User should do, to place the pcell into the layout using that flow, which is not so much UX friendly.

    Is there a way to surpass the original problem? I've tried to dump all the cell's attributes in the hope I find something in there, unsuccessfully.


  • Oh sorry - my mistake ...

    I meant with this code you should be able to make the function fail. It's not possible to create two PCell variants with the same parameter set.

    I'd also be a bit careful with these cells you find as PCell variants. KLayout will regard top-level PCell variants as temporay ones and delete them occasionally. So if don't instantiate them (e.g. drag & drop them), they'll vanish sooner or later.


  • Hi Matthias,

    I was hoping that may there is a way.

    Ok, thank you guys for your responses.


Sign In or Register to comment.