def import_oas(filename, cellname = None, flatten = False): if filename.lower().endswith('.gds'): # you are looking for import_gds retval = pg.import_gds(filename, cellname = cellname, flatten = flatten) return retval try: import klayout.db as pya except ImportError as err: err.args = ('[PHIDL] klayout package needed to import OASIS. pip install klayout\n' + err.args[0], ) + err.args[1:] raise if not filename.lower().endswith('.oas'): filename += '.oas' fileroot = os.path.splitext(filename)[0] tempfilename = fileroot + '-tmp.gds' layout = pya.Layout() layout.read(filename) # We want to end up with one Device. If the imported layout has multiple top cells, # a new toplevel is created, and they go into the second level if len(layout.top_cells()) > 1: topcell = layout.create_cell('toplevel') rot_DTrans = pya.DTrans.R0 origin = pya.DPoint(0, 0) for childcell in layout.top_cells(): if childcell == topcell: continue topcell.insert(pya.DCellInstArray(childcell.cell_index(), pya.DTrans(rot_DTrans, origin))) else: topcell = layout.top_cell() topcell.write(tempfilename) retval = pg.import_gds(tempfilename, cellname = cellname, flatten = flatten) os.remove(tempfilename) return retval
def getverticesGdsii(name, markerlayer=[]): global GDS GDS = pg.import_gds(filename=name) if len(GDS.layers) > 1: print('Multiple layers. Type layer and confirm with enter \n') for layer in GDS.layers: print('#' + str(layer)) markerlayer = [int(input())] else: for thelayer in GDS.layers: markerlayer = [thelayer] GDS.flatten() GDSLayered = pg.extract(GDS, layers=markerlayer) vertices = [] for polygon in GDSLayered: #print(polygon) for vertex in polygon.polygons[0]: vertices.append(vertex) #print(vertex) return np.array(vertices).tolist()
def read_port_markers(gdspath, layers=[(69, 0)]): """loads a GDS and returns the extracted device for a particular layer Args: gdspath: gdspath or Component layer: GDS layer """ D = gdspath if isinstance(gdspath, Device) else pg.import_gds(gdspath) return pg.extract(D, layers=layers)
def read_port_markers(gdspath, layer=69): """ loads a GDS and read port Args: gdspath: layer: GDS layer """ D = pg.import_gds(gdspath) D = pg.extract(D, layers=[layer]) for e in D.elements: print(e.x, e.y)
def load_gds(cells): if isinstance(cells, str): cells = pathlib.Path(cells) elif isinstance(cells, list) or isinstance(cells, tuple): cells_logo = [] for p in cells: if isinstance(p, str): # import pdb ; pdb.set_trace() cells_logo.append(pg.import_gds(p)) elif isinstance(p, pathlib.Path): cells_logo.append(pg.import_gds(str(p.absolute()))) g = Group(cells_logo) g.distribute(direction='x', spacing=150) g.align(alignment='y') logo_cell = dl.Device(name="cells") for c in cells_logo: logo_cell.add(c) logo_cell.flatten() logo_cell.name = 'Logos' return logo_cell
def test_phidlXOR(): ref_file = os.path.join(os.path.dirname(phidlib.__file__), 'test_phidl', 'ref_layouts', 'Boxy.gds') TOP1 = phidlib.box() TOP2 = pg.import_gds(ref_file) TOP_different = phidlib.box(width=100) for hash_geom in [True, False]: XOR = xor_polygons_phidl(TOP1, TOP2, hash_geom=hash_geom) if len(XOR.flatten().get_polygons()) > 0: raise GeometryDifference( "Differences found between phidl Devices.") XOR_different = xor_polygons_phidl(TOP_different, TOP2, hash_geom=hash_geom) assert len(XOR_different.flatten().get_polygons()) > 0
def import_gds(path, cellname=None, flatten=True, **kwargs): if isinstance(path, str): path = pathlib.Path(path) cell = pg.import_gds(str(path.absolute())) if flatten == True: cell.flatten() if cellname is not None: cell._internal_name = cellname return cell
def resistivity_test_cell(): """Create cell with standard Resisistivity Test Returns ------ cell : phidl.Device """ with open(str(pathlib.Path(__file__).parent / 'ResistivityTest.gds'), 'rb') as infile: cell = pg.import_gds(infile, cellname='TestCell') cell = pt.join(cell) cell.name = 'ResistivityTest' return cell
def test_write_and_import_gds(): D = Device() D.add_ref(pg.rectangle(size=[1.5, 2.7], layer=[3, 2])) D.add_ref(pg.rectangle(size=[0.8, 2.5], layer=[9, 7])) D.add_array(pg.rectangle(size=[1, 2], layer=[4, 66]), rows=3, columns=2, spacing=[14, 7.5]) D.add_array(pg.rectangle(size=[1.5, 2.5], layer=[4, 67]), rows=1, columns=2, spacing=[14, 7.5]) D.add_polygon([[3, 4, 5], [6.7, 8.9, 10.15]], layer=[7, 8]) D.add_polygon([[3, 4, 5], [1.7, 8.9, 10.15]], layer=[7, 9]) precision = 1e-4 unit = 1e-6 h1 = D.hash_geometry(precision=precision) D.write_gds('temp.gds', precision=unit * precision, unit=1e-6) Dimport = pg.import_gds('temp.gds', flatten=False) h2 = Dimport.hash_geometry(precision=precision) assert (h1 == h2)
#============================================================================== # Importing GDS files #============================================================================== # The phidl.geometry module is responsible for generating premade Devices. # This includes imported geometry from other GDS files too. When you import # a GDS, you specify which layers you want, and it will import those layers # as a new Device. The new device can then be manipulated like any other. # Let's import the GDS we just saved in the previous step. Although generally # you must specify which cell in the GDS file you want to import using the # argument `cellname`, if the GDS file has only one top-level cell (like our # MyLayerSetPreview.gds file does), the cellname argument can be left out and # import_gds() will import that top-level cell. # Let's first just import the entire GDS as-is E = pg.import_gds(filename='MyNewGDS.gds') qp(E) # Similarly, we can import the same file but flatten the entire cell # heirarchy E2 = pg.import_gds(filename='MyNewGDS.gds', flatten=True) #============================================================================== # Using Layers #============================================================================== # Let's make a new blank device DL and add some text to it, but this time on # different layers DL = Device() # You can specify any layer in one of three ways: # 1) as a single number 0-255 representing the gds layer number, e.g. layer = 1
from phidl import quickplot as qp X = Device() X << pg.ellipse(layer = 0) X << pg.ellipse(layer = 1) D = pg.copy_layer(X, layer = 1, new_layer = 2) qp(D) # quickplot the geometry create_image(D, 'copy_layer') # example-import_gds import phidl.geometry as pg from phidl import quickplot as qp D = pg.ellipse() D.write_gds('myoutput.gds') D = pg.import_gds(filename = 'myoutput.gds', cellname = None, flatten = False) qp(D) # quickplot the geometry create_image(D, 'import_gds') # example-preview_layerset import phidl.geometry as pg from phidl import quickplot as qp from phidl import LayerSet lys = LayerSet() lys.add_layer('p', color = 'lightblue', gds_layer = 1, gds_datatype = 0) lys.add_layer('p+', color = 'blue', gds_layer = 2, gds_datatype = 0) lys.add_layer('p++', color = 'darkblue', gds_layer = 3, gds_datatype = 0) lys.add_layer('n', color = 'lightgreen', gds_layer = 4, gds_datatype = 0) lys.add_layer('n+', color = 'green', gds_layer = 4, gds_datatype = 98) lys.add_layer('n++', color = 'darkgreen', gds_layer = 5, gds_datatype = 99)
#============================================================================== # Importing GDS files #============================================================================== # The phidl.geometry module is responsible for generating premade Devices. # This includes imported geometry from other GDS files too. When you import # a GDS, you specify which layers you want, and it will import those layers # as a new Device. The new device can then be manipulated like any other. # Let's import the GDS we just saved in the previous step. Although generally # you must specify which cell in the GDS file you want to import using the # argument `cellname`, if the GDS file has only one top-level cell (like our # MyNewGDS.gds file does), the cellname argument can be left out and # import_gds() will import that top-level cell. # Let's first just import the entire GDS as-is E = pg.import_gds(filename='MyNewGDS.gds') quickplot(E) # Now say we only wanted to get layers 2 and 3 from the file. We can specify # a list of layers using the `layers` argument E2 = pg.import_gds(filename='MyNewGDS.gds', layers=[2, 3]) quickplot(E2) # We can also use the `layers` argument to map layers arbitrarily. Say we # wanted to combine shapes on layer 1 with those on layer 3, but leave layer 2 # on layer 2. We can map layers 1->3, 3->3, 2->2 by passing a dict to the # `layers` argument E3 = pg.import_gds(filename='MyNewGDS.gds', layers={1: 3, 3: 3, 2: 2}) quickplot(E3) #==============================================================================