Value of begin_shapes_rec variable changes if new shapes added to cell.

Dear Mattias

I am saving the value of begin_shapes_rec into the following variable "dies_ol":

dies_ol = pya.Region(cell.begin_shapes_rec( layer_ol ))

After this I add new shapes in "layer_ol" into the cell.

This changes the value contained in dies_ol variable.

Is this expected or is this a new behaviour ?
Because in the past I think Klayout would keep the original variable value.

Maybe I should not use begin_shapes_rec this way. in this case I wish you could letme know what should I use insteadt.

I am attaching the input gds and the script to reproduce this.

Please let me know
Thank you in advance
Best regards
Giovanni

Comments

  • Hi Giovanni,

    yes, that is intended.

    Actually, when you create a Region with a recursive shape iterator, it becomes a special kind of Region called an "original layer region". It is not a replica of the layer, but represents the actual layer inside the source layout. This feature is preventing unnecessary and expensive copies. Also, such a region remains hierarchical, hence saving memory.

    In order to "freeze" a Region, you can use "flatten". But beware, this will do what it says and may explode your hierarchy.

    For example, this code reproduces your problem:

    ly = pya.Layout()
    l1 = ly.layer(1, 0)
    top = ly.create_cell("TOP")
    
    top.shapes(l1).insert(pya.Box(0, 0, 100, 100))
    
    r1 = pya.Region(top.begin_shapes_rec(l1))
    
    top.shapes(l1).insert(pya.Box(200, 0, 300, 100))
    
    r2 = pya.Region(top.begin_shapes_rec(l1))
    
    print(r1)
    print(r2)
    

    As you experienced, it will print the same shapes, although the second shape was added after the region 1 was created:

    (0,0;0,100;100,100;100,0);(200,0;200,100;300,100;300,0)
    (0,0;0,100;100,100;100,0);(200,0;200,100;300,100;300,0)
    

    With "flatten", the copy is frozen:

    ly = pya.Layout()
    l1 = ly.layer(1, 0)
    top = ly.create_cell("TOP")
    
    top.shapes(l1).insert(pya.Box(0, 0, 100, 100))
    
    r1 = pya.Region(top.begin_shapes_rec(l1)).flatten()
    
    top.shapes(l1).insert(pya.Box(200, 0, 300, 100))
    
    r2 = pya.Region(top.begin_shapes_rec(l1)).flatten()
    
    print(r1)
    print(r2)
    

    and it prints:

    (0,0;0,100;100,100;100,0)
    (0,0;0,100;100,100;100,0);(200,0;200,100;300,100;300,0)
    

    Matthias

  • Thank you Matthias for the detailed explanation.
    I wil change my code and make shure that I add .flatten() when I need to freeze the content of the region.

    Thank you again
    Best regards
    Giovanni

Sign In or Register to comment.