Undo in script

Hi!

I'm trying to add some simple python modules such as draw circles and align cells. Also, I have written some scripts to generate a bunch of cells and shapes.

Is there a way to achieve undo without writing the entire layout file and then reads? I went through a bunch of documents, and I was not able to find them.

Thank you very much!

Comments

  • edited July 28

    So you use pya.Layout objects and we're not talking about UI, right?

    I assume you do something like:

    ly = pya.Layout()
    
    # create part 1 on ly
    ...
    
    # take snapshot
    snapshot = snapshot of ly
    
    # create part 2 on ly
    ...
    
    ly.write(...)  # or something like this
    
    # Restart with part 1 only
    ly = snapshot
    
    ...
    

    The correct way of taking a snapshot without read/write is to use dup:

    snapshot = ly.dup()
    

    Does that answer your question?

    Matthias

  • Thank you very much for your answer!

    I am sort of talking about UI, I tried your method, but it won't update the layout in the window view.

    I wonder is there a simpler way of doing that?

    Thank you very much!

  • I have not noticed (in GUI use) any significant wall time
    for saving entire layout. Maybe two seconds to load and
    paint at 4K, a full wafer layout from disc (4", 4um feature
    size, but still - these are fully functioning IC process and
    circuit coupons). Are you overthinking this, particularly the
    weight given to the anticipated penalty? For fiddling with
    circles, I don't think you'll see it.

  • edited August 2

    Hi there!

    It's not just circles, as I am trying to add a few other implementations, and I find both loading and this new way would not be ideal since they both overwrite the original undo stack.

    But here is the way that I discover to load the snapshot, you can go use view.show_layout(layout, False) but I do wonder if there is a way you can also keep the undo stack.

    view as the layoutview class

    I wonder, is there a way I can add something directly into the undo stack of Klayout itself? or something that won't overwrite the previous undo stack.

    Thank you!

  • The formal way of adding undo to any script of yours is the following:

    view = pya.LayoutView.current()
    view.transaction("Description of your operation")
    
    try:
    
      ly = pya.CellView.active().layout()
      ... your code: do anything on layout "ly" ...
    
    finally:
      view.commit()  # undo/redo support
    

    "transaction" will start a block of recorded operations. "commit" will close it. After commit, a new Undo item will be on the Undo stack which undos/redos the operations within "your code".

    Is that what you are looking for?

    Matthias

  • omg this is amazing! Thank u!

Sign In or Register to comment.