import sys, os
import numpy as np
import gdspy as gp
import gds_tools as gdst

file_authors = "<NAME>"
last_author_email = "<EMAIL>"
created_on_date = "14 November 2019"

filename = os.path.basename(__file__)
filepath = __file__.replace(filename, "")

source_file = "example_design.gds"
top_cell_name = 'TOP'

print("-- Importing file --")

imported_library = gp.GdsLibrary()
imported_library.read_gds(filepath + source_file)
cell = imported_library.cells[top_cell_name].flatten()
cell.remove_paths(lambda x: True)
cell.remove_labels(lambda x: True)

grid_size = 20
polygon_buffer = grid_size / 2
mapped_layers = [13, 14, 20]
map_precision = 0.001

GdsMap = gdst.GdsMap(
    cell,
    grid_size = grid_size,
    buffer = polygon_buffer,
    layers = mapped_layers,
    precision = map_precision
    )

routing_pairs = [
    [(-200,1903.57),(-1000,1000)],
    [(-400,2308.36),(-1000,1000)],
    [(-650,1903.57),(-1000,500)],
    [(-850,2308.36),(-1000,500)],
    [(-1300,2308.36),(-1000,0)],
    [(-1100,1903.57),(-1000,0)],
    [(-1750,2308.36),(-1000,-500)],
    [(-1550,1903.57),(-1000,-500)],
    [(-2200,2308.36),(-1000,-1000)],
    [(-2000,1903.57),(-1000,-1000)],
    [(50,2308.36),(-500,1000)],
    [(250,1903.57),(-500,1000)],
    [(500,2308.36),(-500,500)],
    [(700,1903.57),(-500,500)],
    [(950,2308.36),(-500,0)],
    [(1150,1903.57),(-500,0)],
    [(1400,2308.36),(-500,-500)],
    [(1600,1903.57),(-500,-500)]
    ]

routing_directions = [
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0]
    ]

# routing_pairs, routing_directions = gdst.routing.suggest_order(routing_pairs, routing_directions)

segment_widths = [3, 2, 1]
segment_distances = [1000, 500, 0]
segment_buffers = [2, 2, 2]
segment_layers = [3, 2, 1]

for i, pair in enumerate(routing_pairs):
    
    route_segments = gdst.router(
        GdsMap,
        end_points = pair,
        end_directions = routing_directions[i],
        segment_widths = segment_widths,
        segment_distances = segment_distances,
        segment_buffers = segment_buffers)

    path_list = []
    try:
        for j, segment in enumerate(route_segments):
            path_list.append(
                gp.FlexPath(
                    points = segment,
                    width = segment_widths[j],
                    layer = segment_layers[j],
                    bend_radius = segment_widths[j]
                    )
                )

        cell.add([path for path in path_list])
    except:
        pass

cell.add(GdsMap.draw_map())

print('-- Writing resulting paths to file --')
gdst.save(cell, filepath + 'routing_results.gds')
import sys, os
import numpy as np
import gdspy as gp
import gds_tools as gdst

file_authors = "<NAME>"
last_author_email = "<EMAIL>"
created_on_date = "14 November 2019"

filename = os.path.basename(__file__)
filepath = __file__.replace(filename, "")

source_file = "example_design.gds"
top_cell_name = 'TOP'

print("-- Importing file --")

imported_library = gp.GdsLibrary()
imported_library.read_gds(filepath + source_file)
cell = imported_library.cells[top_cell_name].flatten()
cell.remove_paths(lambda x: True)
cell.remove_labels(lambda x: True)

grid_size = 20
polygon_buffer = grid_size / 2
mapped_layers = [13, 14, 20]
map_precision = 0.001

GdsMap = gdst.GdsMap(
    cell,
    grid_size = grid_size,
    buffer = polygon_buffer,
    layers = mapped_layers,
    precision = map_precision
    )

routing_pairs = [
    [(-200,1903.57),(-1000,1000)],
    [(-400,2308.36),(-1000,1000)],
    [(-650,1903.57),(-1000,500)],
    [(-850,2308.36),(-1000,500)],
    [(-1300,2308.36),(-1000,0)],
    [(-1100,1903.57),(-1000,0)],
    [(-1750,2308.36),(-1000,-500)],
    [(-1550,1903.57),(-1000,-500)],
    [(-2200,2308.36),(-1000,-1000)],
    [(-2000,1903.57),(-1000,-1000)],
    [(50,2308.36),(-500,1000)],
    [(250,1903.57),(-500,1000)],
    [(500,2308.36),(-500,500)],
    [(700,1903.57),(-500,500)],
    [(950,2308.36),(-500,0)],
    [(1150,1903.57),(-500,0)],
    [(1400,2308.36),(-500,-500)],
    [(1600,1903.57),(-500,-500)]
    ]

routing_directions = [
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0],
    [3 * np.pi / 2, np.pi],
    [3 * np.pi / 2, 0]
    ]

# routing_pairs, routing_directions = gdst.routing.suggest_order(routing_pairs, routing_directions)

segment_widths = [3, 2, 1]
segment_distances = [1000, 500, 0]
segment_buffers = [2, 2, 2]
segment_layers = [3, 2, 1]

for i, pair in enumerate(routing_pairs):
    
    route_segments = gdst.router(
        GdsMap,
        end_points = pair,
        end_directions = routing_directions[i],
        segment_widths = segment_widths,
        segment_distances = segment_distances,
        segment_buffers = segment_buffers)

    path_list = []
    try:
        for j, segment in enumerate(route_segments):
            path_list.append(
                gp.FlexPath(
                    points = segment,
                    width = segment_widths[j],
                    layer = segment_layers[j],
                    bend_radius = segment_widths[j]
                    )
                )

        cell.add([path for path in path_list])
    except:
        pass

cell.add(GdsMap.draw_map())

print('-- Writing resulting paths to file --')
gdst.save(cell, filepath + 'routing_results.gds')
# routing_pairs, routing_directions = gdst.routing.suggest_order(routing_pairs, routing_directions)