Get shapes from all subcells of cell

edited September 9 in Python scripting

Hi -- I would like to pull all shapes on a given layer of a cell.
When the cell has no subcells, this works easily.

SourceShapes = ParentCell.Shapes(SourceLayer)

However, say ParentCell has no shapes in the top level, but instead instances of ChildCell1, ChildCell2, and ChildCell3. The three child cells all have shapes in SourceLayer, but if I run

SourceShapes = ParentCell.Shapes(SourceLayer)

in this case then no shapes are returned.
Is there a simple method get all the shapes in all levels of the cell hierarchy within ParentCell?


  • If anyone encounters this themselves, my cumbersome workaround is:

    SourceShapes= flatshapes.dup() #this line seems particularly clumsy but I have not found a way to avoiding needing it
  • edited September 16
    What you are looking for is the `RecursiveShapeIterator`.

    If you purely want a shapes object you can do it with this:

    SourceShapes = pya.Shapes()

    What I suspect is what you want is a region in the end. You can directly construct the region from the iterator

    region = pya.Region(ParentCell.begin_shapes_rec(SourceLayer))

    P.S. not sure why code rendering is not working. I will try to fix it when I find out what I did to break it
  • @sebastian Code rendering needs lines with three backticks only. Like this:

    And thanks for the discussion. The RecursiveShapeIterator is the right approach.

    If you want to just read the shapes, you do not need to collect them in a Region object. As the name implies, the RecursiveShapeIterator iterates over the shapes while visiting all child cells in the hierarchy. It is a quite powerful class that has many options - e.g. confining the search to a region or selecting/deselecting certain cell paths in the hierarchy. There is more documentation here:

    The Region class is similar to Shapes, but much more powerful. It stores polygons only (or the polygon versions of "polygonizable" objects). It will contain a flat set of polygons derived from the RecursiveShapeIterator. Pulling this expanded set of polygons may be expensive if you have a deep hierarchy with many cell repetitions. More information about the Region class is found here:


  • Thank you very much! begin_shapes_rec does exactly what I needed.

  • edited September 16

    @Matthias I am not sure why it didn't work, that's exactly how I wrote it (the first time). Could the problem be that I authored it on mobile?

    Test Code

    In here it works now (from desktop). But I cannot even edit the comment anymore to allow it to use proper markdown :tongue:

  • edited September 16

    I could fix it for you but then the comment does not make sense :)

    But I also don't see a problem. Some weird encoding issue or "alternative" backticks on mobile browsers? No idea ...


  • I just tried editing to see whether that was the case, but no, even if I replace them in FF on desktop, it doesn't fix it ¯\_(ツ)_/¯

  • Yet another case of magic :)

Sign In or Register to comment.