heitzmann / gdstk

Gdstk (GDSII Tool Kit) is a C++/Python library for creation and manipulation of GDSII and OASIS files.
https://heitzmann.github.io/gdstk/
Boost Software License 1.0
324 stars 78 forks source link

Absolute Coordinates of Reference #236

Open Rishabhgoyal07 opened 5 months ago

Rishabhgoyal07 commented 5 months ago

Hello @heitzmann, I want to ask that, how can I get the absolute coordinates of a reference according to its placement in a defined layout. Can we directly access a reference down the hierarchy?

nmz787-intel commented 4 months ago

I think you have a few options:

I think for the latter two cases, you want to operate on a cell's .references, not on a Reference object that you just created.:

import math
import gdstk

lib = gdstk.Library()

# Create a cell with collection of shapes that is used repeatedly
triangle = gdstk.regular_polygon((0, 6), 2, 3, rotation=math.pi)
single_triangle_cell = lib.new_cell("TRIANGLE_SINGLE")
single_triangle_cell.add(triangle)

def add_array(input_cell, output_cell, degrees_sweep, total_triangles):
    number_repetitions = total_triangles
    num_extra_triangles_from_full_rotation_sweep = degrees_sweep//360
    degrees_separation = degrees_sweep/(number_repetitions-1+num_extra_triangles_from_full_rotation_sweep)
    assert number_repetitions>0
    for i in range(number_repetitions):
        rotated_ref = gdstk.Reference(input_cell, (0, 0), rotation=-math.radians(degrees_separation*i))
        output_cell.add(rotated_ref)
    return output_cell

triangle_array_4_over_90_deg = add_array(single_triangle_cell, lib.new_cell("4_TRIANGLE_ARRAY"), 90, 4)
triangle_array_3_over_90_deg = add_array(single_triangle_cell, lib.new_cell("3_TRIANGLE_ARRAY"), 90, 3)
triangle_array_8_over_360_deg = add_array(single_triangle_cell, lib.new_cell("8_TRIANGLE_ARRAY"), 360, 8)

# now check references
ref_ids1 = set(triangle_array_8_over_360_deg.references)
ref_ids2 = set(triangle_array_4_over_90_deg.references)
print(ref_ids1)
print(ref_ids2)
common_refs = ref_ids1.intersection(ref_ids2)
print(f'common references: {common_refs}')
assert not common_refs

for ref in triangle_array_4_over_90_deg.references:
    print(f'reference BB: {ref.bounding_box()}')
    for i, p in enumerate(ref.get_polygons()):
        print(f'\t polygon #{i} points: {",".join([str(pts) for pts in p.points])}')
lib.write_gds("testissue.gds")

for me the output is:

{<gdstk.Reference object at 0x2aaae13d5c10>, <gdstk.Reference object at 0x2aaae13d5b30>, <gdstk.Reference object at 0x2aaae13d5b50>, <gdstk.Reference object at 0x2aaae13d5b70>, <gdstk.Reference object at 0x2aaae13d5b90>, <gdstk.Reference object at 0x2aaae13d5bb0>, <gdstk.Reference object at 0x2aaae13d5bd0>, <gdstk.Reference object at 0x2aaae13d5bf0>}
{<gdstk.Reference object at 0x2aaae13d5a70>, <gdstk.Reference object at 0x2aaae13d5a30>, <gdstk.Reference object at 0x2aaae13d5a50>, <gdstk.Reference object at 0x2aaae13d59f0>}
common references: set()
reference BB: ((-1.0, 4.8452994616207485), (1.0000000000000007, 6.577350269189626))
     polygon #0 points: [-1.          6.57735027],[-2.12115048e-16  4.84529946e+00],[1.         6.57735027]
reference BB: ((2.422649730810374, 4.196152422706632), (4.1547005383792515, 6.196152422706633))
     polygon #0 points: [2.42264973 6.19615242],[2.42264973 4.19615242],[4.15470054 5.19615242]
reference BB: ((4.196152422706632, 2.4226497308103743), (6.196152422706632, 4.154700538379252))
     polygon #0 points: [5.19615242 4.15470054],[4.19615242 2.42264973],[6.19615242 2.42264973]
reference BB: ((4.8452994616207485, -1.0000000000000004), (6.577350269189626, 1.0000000000000004))
     polygon #0 points: [6.57735027 1.        ],[4.84529946e+00 5.08804072e-16],[ 6.57735027 -1.        ]