Script to give coordinates of shapes and instances

edited September 2021 in Python scripting

Hi!
I am trying to write some automation for a GDS file. The script should extract coordinates of shapes and instances .
I have one test case that works fine (testCase.gds) :)
When choosing layer 0/0 in the layer list and running the macro, I get the location of the 2 polygons of layer 0/0, and the instance cell ("child") exactly as I would expect:
_found shape at dtrans= r0 *1 100,100 bbox lower left=0,0, so overall location is at 100,100
found shape at dtrans= r0 *1 100,100 bbox lower left=40,40, so overall location is at 140,140
found instance of cell child at inst_dtrans=r0 *1 100,100
_

Now, I have created another file, called testCase_fixed.gds. For that I have rotated the original file (testCase.gds) by 90degs and placed the origin at the center.
Running the script again seems to provide strange results: :|
found shape at dtrans= r0 *1 100,-100 bbox lower left=-500,400, so overall location is at -400,300
found shape at dtrans= r0 *1 100,-100 bbox lower left=-460,440, so overall location is at -360,340
found instance of cell child at inst_dtrans=r0 *1 100,-100

So I have 3 questions:
1. the shape coordinates seem strange. I do manage to get a correct location by doing some math (-400,300 and -360,340). Am I doing the math correctly?
2. the "child" instance location is not understood. I get (100,-100). But I would expect to get (-400,400). I think Klayout API ignores the fact that the layout origin is now shifted. Right? Any way to overcome this?
3. I also need to get the coordinates of each shape with respect to the "child" cell and not to the top cell. i.e, I would like to get (0,0) and (40,40). Is there any way to do it?

Here is the code:

#choosing layer 0/0 and running gives good reault when using testCase.gds. But results are not OK when using testCase_fixed.gds
import pya
ly=pya.CellView.active().layout() #Gets the activew layout
mw= pya.Application.instance().main_window() #main window
lv = mw.current_view()
if lv is None:
  raise ValueError("No view selected")

#Now look for shapes of the selected layer
layer_to_look_for=lv.current_layer.current().layer_index() #please choose layer 0/0
shape_iter = ly.top_cell().begin_shapes_rec(layer_to_look_for) #RecursiveShapeIterator
while shape_iter.at_end() is False:
  print("found shape at dtrans= {0}\t bbox lower left={1}, so overall location is at \
  {2}".format(shape_iter.dtrans(),shape_iter.shape().box_dp1,shape_iter.dtrans()*shape_iter.shape().box_dp1))
  shape_iter.next()

#Now, trying to see the location of instances.
instance_iter=ly.top_cell().begin_instances_rec()
while instance_iter.at_end() is False:
  print("found instance of cell {0} at inst_dtrans={1}".format(instance_iter.inst_cell().name,instance_iter.inst_dtrans()))
  instance_iter.next()

Thanks!
Allon

PS: Please note attached file testCase.zip with the 2 GDS files inside

Comments

  • Hi Allon,

    one thing that comes to mind is that you're just transforming the bounding boxes lower left corner. So when you rotate, this point become the lower right corner (when rotating 90 degree counterclockwise).

    Maybe it's better to use "shape().box_dcenter" instead of "shape().box_dp1". The center will still be the center of the box after rotation.

    Anyway, I don't know how you adjusted the origin, but the effect was that in "TestCase_fixed.gds" the rectangle is no longer aligned with the origin inside "child". The lower left corner of the rectangle at layer 0 is at -500,400 inside that cell. And the cell instance is not rotated and placed at 100,-100. So the coordinates of -400,300 is correct. That is how instances work in GDS.

    A way to get "TestCase_fixed" without spoiling the child cell is to enable "Select top level objects only" in the "View" menu, select all from "TOP", use "Edit/Selection/Rotate Counterclockwise" and "Edit/Cell/Adjust origin". This will leave the child cell untouched.

    Matthias

  • Hi Matthias.
    Thanks for your answer.
    I have tried to follow your instructions and it works fine!
    So, summarized my steps and your solution in the table below
    I also uploaded testCase_fixed_2.gds.
    Layouts of testCase_fixed.gds and testCase_fixed_2.gds look the same, but apparantly the child cell coordinate system is different.

    What I did What should be done
    rotation Edit--> Layout--> Rotate 1. Select "top level objects only" in the "View" menu|
    2. Select all from "TOP"
    3. Select"Edit/Selection/Rotate Counterclockwise"
    translation Edit--> Layout --> Move by 3. "Edit/Cell/Adjust Origin"
    GDS testCase_fixed.gds testCase_fixed_2.gds

    Thank!
    Allon

Sign In or Register to comment.