The results are different for "Adjust Origin" between GUI and pymacro

edited January 2022 in General

Hi all,

I'm a new beginner in Klayout pymacro.
I'd like to transform the gds to center(0,0). I used to execute this function in GUI, and recently I try to execute by pymacro, but I have no idea why the results are different between these 2 approaches.

  1. execute in GUI: "edit" >> "cell" >> "Adjust Origin" >> "chose center"

And the result by GUI is as my expected, that the gds is completed shifted to the center (0,0).

  1. execute in pymacro: I refer to andyL's code in the discussion "Adjust Cell Origin using the Python API". (Many thanks for andyL)
    Please check the following code:

      import pya    
      layout_file = "/.../layout_path/input.gds"
      output_file = "/.../layout_path/input_shift.gds"
      layout =
      def adjustOrigin(layoutB,topcell):
                bbox= topcell.bbox()
                trans =  # center point
                for inst in layoutB.top_cell().each_inst():
                for li in layoutB.layer_indices():
                          for shape in layoutB.top_cell().each_shape(li):
      layout = adjustOrigin(layout,layout.top_cell())

I checked the new output_file from pymacro, and it did transform but the final bbox/layout placements are not as same as the result by GUI.
It's just my guess that the code would shift the layout in sub-cell coordinate due to the instance for loop.

I'd like to ask which part should I modify this code to get the same result as GUI...
(The Klayout version is 0.25)

Thank you so much!


  • edited January 2022

    Just update some information.

    I found that this code doesn't work for some instances, that some instances in the gds can't be shifted and left on the original position.
    I still try to figure out what the difference are between these instances...

  • First of all, I'd propose to use a more recent version (0.27.x). With my version, the code pretty much does what "Adjust origin" does (minus the undo and "adjust instances in parents" capability).

    The code you pasted is not quite clean as inside the function you refer to the actual topcell ("layoutB.top_cell()"), not the one passed in the "topcell" parameter. That makes a difference when you try to adjust a cell which is not the top cell.

    With the 0.27 version, the function can be reduced to:

    def adjustOrigin(layoutB,topcell):
      bbox= topcell.bbox()
      trans =  # center point

    I don't see how this may not work.


  • Hi Matthias,

    Actually, I've tried klayout-0.26.7 with the newer transform function in my own computer, and it works for another simple layout. But the issue layout is only in another special work environment and it is difficult to install other klayout build in the environment, so currently I only get klayout-0.25 to use.

    And as your suggestion, I modified "layoutB.top_cell()" to "topcell" in adjustOriginCenter(), and it works fine.
    (But I actually I don't fully understand about the difference between these 2 conditions, I thought they are the same...)

    Thank you so much!

  • Calling "topcell" inside the transformation loop will continuously trigger the internal sort & hierarchy tree update which may change the order of the instances while you're iterating. That's why calling the "topcell" method will no just spoil performance but also give you unexpected results or even application crashes.


  • Got it.
    Thank you so much!


  • Uups, 3 lines only - just read this lines. I updated my code. Thanks Matthias !
    And also for this great piece of SW.
    Best Regards

Sign In or Register to comment.