-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdswriter.py
3607 lines (3159 loc) · 231 KB
/
gdswriter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import gdspy
import numpy as np
from shapely.geometry import box, MultiPolygon, Polygon, Point
from shapely.affinity import translate
from shapely.prepared import prep
from shapely.ops import unary_union
from shapely.strtree import STRtree
import matplotlib.pyplot as plt
import klayout.db as kdb
import geopandas as gpd
from phidl import Device, Path, CrossSection
import phidl.routing as pr
from copy import deepcopy
import math
import a_star_single_direction
TEXT_SPACING_FACTOR = 0.3
class GDSDesign:
def __init__(self, lib_name='default_lib', filename=None, top_cell_names=['TopCell'], bounds=[-np.inf, np.inf, -np.inf, np.inf], unit=1e-6, precision=1e-9,
default_netID=0, default_feature_size=None, default_spacing=None):
"""
Initialize a new GDS design library with an optional top cell, design size, and units.
Args:
- lib_name (str): Name of the GDS library.
- top_cell_name (str): Name of the top-level cell.
- size (tuple): Overall size of the design (width, height).
- unit (str): Units of measurement (e.g., 'um' for micrometers, 'nm' for nanometers).
"""
if filename is None:
self.lib = gdspy.GdsLibrary(name=lib_name, unit=unit, precision=precision)
self.cells = {} # Cells by name
self.top_cell_names = top_cell_names
for top_cell_name in top_cell_names:
self.add_cell(top_cell_name)
self.layers = {} # Layer properties by layer name
self.drc_rules = {} # DRC rules for each layer
self.bounds = bounds # Design size
self.unit = unit # Measurement units
self.precision = precision
else:
self.lib = gdspy.GdsLibrary(infile=filename)
self.cells = {} # Cells by name
unique_layers = set()
for cell_name in self.lib.cells.keys():
if cell_name != '$$$CONTEXT_INFO$$$':
self.cells[cell_name] = {}
self.cells[cell_name]['cell'] = self.lib.cells[cell_name]
self.cells[cell_name]['polygons'] = []
self.cells[cell_name]['netIDs'] = []
polygons_by_spec = self.lib.cells[cell_name].get_polygons(by_spec=True)
# Add polygons to the backend by layer
for (lay, dat), polys in polygons_by_spec.items():
unique_layers.add(lay)
for poly in polys:
self.cells[cell_name]['polygons'].append((lay, poly))
self.cells[cell_name]['netIDs'].append((lay, default_netID))
top_cells = self.lib.top_level()
self.top_cell_names = [cell.name for cell in top_cells if cell.name != '$$$CONTEXT_INFO$$$']
self.layers = {} # Layer properties by layer name
self.drc_rules = {} # DRC rules for each layer
# Define layers based on the present polygons in the design
for layer in unique_layers:
self.define_layer(f"{layer}", layer, min_feature_size=default_feature_size,
min_spacing=default_spacing)
# Calculate the design bounds
max_x, min_x, max_y, min_y = -np.inf, np.inf, -np.inf, np.inf
for cell in top_cells:
for poly in cell.get_polygons():
max_x = max(max_x, np.amax(poly[:, 0]))
min_x = min(min_x, np.amin(poly[:, 0]))
max_y = max(max_y, np.amax(poly[:, 1]))
min_y = min(min_y, np.amin(poly[:, 1]))
self.bounds = [min_x, max_x, min_y, max_y]
self.unit = self.lib.unit
self.precision = self.lib.precision
def add_cell(self, cell_name):
if cell_name in self.cells or cell_name in self.lib.cells or cell_name in gdspy.current_library.cells:
print(f"Warning: Cell '{cell_name}' already exists. Overwriting existing cell.")
self.delete_cell(cell_name)
cell = self.lib.new_cell(cell_name, overwrite_duplicate=True)
self.cells[cell_name] = {}
self.cells[cell_name]['cell'] = cell
self.cells[cell_name]['polygons'] = []
self.cells[cell_name]['netIDs'] = []
return cell
def delete_cell(self, cell_name):
"""
Delete a cell from the GDS library and the internal cell dictionary.
Args:
- cell_name (str): Name of the cell to delete.
"""
if cell_name not in self.cells and cell_name not in self.lib.cells and cell_name not in gdspy.current_library.cells:
raise ValueError(f"Error: Cell '{cell_name}' does not exist.")
# Remove the cell from the internal dictionary
if cell_name in self.cells:
del self.cells[cell_name]
# Remove the cell from the GDS library
if cell_name in self.lib.cells:
del self.lib.cells[cell_name]
self.lib.remove(cell_name)
if cell_name in gdspy.current_library.cells:
del gdspy.current_library.cells[cell_name]
gdspy.current_library.remove(cell_name)
def calculate_cell_size(self, cell_name):
"""
Calculate the bounding box size of a cell based on its polygons.
Args:
- cell_name (str): Name of the cell to calculate the size for.
Returns:
- (width, height): Tuple representing the width and height of the cell.
"""
cell = self.check_cell_exists(cell_name)
# Initialize the bounding box
min_x, min_y = np.inf, np.inf
max_x, max_y = -np.inf, -np.inf
for poly in cell.get_polygons():
points = np.array(poly)
min_x = min(min_x, np.amin(points[:, 0]))
max_x = max(max_x, np.amax(points[:, 0]))
min_y = min(min_y, np.amin(points[:, 1]))
max_y = max(max_y, np.amax(points[:, 1]))
offset = ((max_x + min_x) / 2, (max_y + min_y) / 2)
width = max_x - min_x
height = max_y - min_y
return width, height, offset
def get_layers_on_cell(self, cell_name):
layers = set()
cell = self.check_cell_exists(cell_name)
for (lay, dat), polys in cell.get_polygons(by_spec=True).items():
layers.add(lay)
return list(layers)
def add_MLA_alignment_mark(self, cell_name, layer_name, center, rect_width=500, rect_height=20, width_interior=5,
extent_x_interior=50, extent_y_interior=50, datatype=0, netID=0, add_text=False, text_height=250,
text_angle=0, text_position=None):
# Check that types are valid
assert isinstance(center, tuple), "Error: Center must be a tuple."
assert isinstance(rect_width, (int, float)), "Error: Rectangle width must be a number."
assert isinstance(rect_height, (int, float)), "Error: Rectangle height must be a number."
assert isinstance(width_interior, (int, float)), "Error: Interior cross width must be a number."
assert isinstance(extent_x_interior, (int, float)), "Error: Interior cross extent along x-axis must be a number."
assert isinstance(extent_y_interior, (int, float)), "Error: Interior cross extent along y-axis must be a number."
assert isinstance(datatype, int), "Error: Datatype must be an integer."
assert isinstance(netID, int), "Error: Net ID must be an integer."
assert isinstance(add_text, bool), "Error: Add text must be a boolean."
assert isinstance(text_height, (int, float)), "Error: Text height must be a number."
assert isinstance(text_angle, (int, float)), "Error: Text angle must be a number."
if text_position is not None:
assert isinstance(text_position, tuple), "Error: Text position must be a tuple."
# Check that geometry is valid
assert rect_width > width_interior, "Error: The width of the rectangle must be greater than the thickness of the interior cross."
assert rect_height > width_interior, "Error: The height of the rectangle must be greater than the thickness of the interior cross."
# Check that cell and layer exist
cell = self.check_cell_exists(cell_name)
layer_number = self.get_layer_number(layer_name)
# Calculate the coordinates for the horizontal and vertical parts of the cross
horizontal_lower_left = (center[0] - extent_x_interior / 2, center[1] - width_interior / 2)
horizontal_upper_right = (center[0] + extent_x_interior / 2, center[1] + width_interior / 2)
vertical_lower_left = (center[0] - width_interior / 2, center[1] - extent_y_interior / 2)
vertical_upper_right = (center[0] + width_interior / 2, center[1] + extent_y_interior / 2)
# Create rectangles for the cross
horizontal_rect = gdspy.Rectangle(horizontal_lower_left, horizontal_upper_right, layer=layer_number, datatype=datatype)
vertical_rect = gdspy.Rectangle(vertical_lower_left, vertical_upper_right, layer=layer_number, datatype=datatype)
# Add the rectangles to the cell
self.add_component(cell, cell_name, horizontal_rect, netID, layer_number)
self.add_component(cell, cell_name, vertical_rect, netID, layer_number)
outer_L_lower_left = (center[0] - extent_x_interior / 2 - rect_width, center[1] - rect_height / 2)
outer_L_upper_right = (center[0] - extent_x_interior / 2, center[1] + rect_height / 2)
outer_R_lower_left = (center[0] + extent_x_interior / 2, center[1] - rect_height / 2)
outer_R_upper_right = (center[0] + extent_x_interior / 2 + rect_width, center[1] + rect_height / 2)
outer_T_lower_left = (center[0] - rect_height / 2, center[1] + extent_y_interior / 2)
outer_T_upper_right = (center[0] + rect_height / 2, center[1] + extent_y_interior / 2 + rect_width)
outer_B_lower_left = (center[0] - rect_height / 2, center[1] - extent_y_interior / 2 - rect_width)
outer_B_upper_right = (center[0] + rect_height / 2, center[1] - extent_y_interior / 2)
outer_L = gdspy.Rectangle(outer_L_lower_left, outer_L_upper_right, layer=layer_number, datatype=datatype)
outer_R = gdspy.Rectangle(outer_R_lower_left, outer_R_upper_right, layer=layer_number, datatype=datatype)
outer_T = gdspy.Rectangle(outer_T_lower_left, outer_T_upper_right, layer=layer_number, datatype=datatype)
outer_B = gdspy.Rectangle(outer_B_lower_left, outer_B_upper_right, layer=layer_number, datatype=datatype)
self.add_component(cell, cell_name, outer_L, netID, layer_number)
self.add_component(cell, cell_name, outer_R, netID, layer_number)
self.add_component(cell, cell_name, outer_T, netID, layer_number)
self.add_component(cell, cell_name, outer_B, netID, layer_number)
if add_text:
text = f"{center}"
if text_position is None:
text_position = (center[0] - rect_width/2-len(text)*text_height*TEXT_SPACING_FACTOR, center[1] + rect_width/2)
self.add_text(cell_name, text, layer_name, text_position, text_height, text_angle)
def add_resistance_test_structure(self, cell_name, layer_name, center, probe_pad_width=1000, probe_pad_height=1000,
probe_pad_spacing=3000, plug_width=200, plug_height=200, trace_width=5,
trace_spacing=50, switchbacks=18, x_extent=100, text_height=250, text=None,
text_angle=90, text_position=None, add_interlayer_short=False,
short_text=None, layer_name_short=None):
# Check that types are valid
assert isinstance(center, tuple), "Error: Center must be a tuple."
assert isinstance(probe_pad_width, (int, float)), "Error: Probe pad width must be a number."
assert isinstance(probe_pad_height, (int, float)), "Error: Probe pad height must be a number."
assert isinstance(probe_pad_spacing, (int, float)), "Error: Probe pad spacing must be a number."
assert isinstance(plug_width, (int, float)), "Error: Plug width must be a number."
assert isinstance(plug_height, (int, float)), "Error: Plug height must be a number."
assert isinstance(trace_width, (int, float)), "Error: Trace width must be a number."
assert isinstance(trace_spacing, (int, float)), "Error: Trace spacing must be a number."
assert isinstance(switchbacks, int), "Error: Number of switchbacks must be an integer."
assert isinstance(x_extent, (int, float)), "Error: X extent must be a number."
assert isinstance(text_height, (int, float)), "Error: Text height must be a number."
if text is not None:
assert isinstance(text, str), "Error: Text must be a string."
assert isinstance(text_angle, (int, float)), "Error: Text angle must be a number."
assert isinstance(add_interlayer_short, bool), "Error: Add interlayer short must be a boolean."
if text_position is not None:
assert isinstance(text_position, tuple), "Error: Text position must be a tuple."
if short_text is not None:
assert isinstance(short_text, str), "Error: Short text must be a string."
if layer_name_short is not None:
assert isinstance(layer_name_short, str), "Error: Layer name for the short must be a string."
# Check that the geometry is valid
margin = (probe_pad_spacing - probe_pad_height - trace_width - trace_spacing * (2 * switchbacks - 1))/2
assert margin > trace_spacing, f"Error: Not enough space for the switchbacks. Margin is {margin} and trace spacing is {trace_spacing}."
# Add probe pads and plugs
self.add_rectangle(cell_name, layer_name, center=(center[0], center[1]-probe_pad_spacing/2), width=probe_pad_width, height=probe_pad_height)
self.add_rectangle(cell_name, layer_name, center=(center[0], center[1]+probe_pad_spacing/2), width=probe_pad_width, height=probe_pad_height)
self.add_rectangle(cell_name, layer_name, center=(center[0]-plug_width/2-probe_pad_width/2, center[1]-probe_pad_spacing/2), width=plug_width, height=plug_height)
self.add_rectangle(cell_name, layer_name, center=(center[0]-plug_width/2-probe_pad_width/2, center[1]+probe_pad_spacing/2), width=plug_width, height=plug_height)
# The first segments of the traces are fixed
path_points = []
distance = 0
path_points.append((center[0]-plug_width-probe_pad_width/2, center[1]-probe_pad_spacing/2))
path_points.append((center[0]-plug_width-probe_pad_width/2-x_extent, center[1]-probe_pad_spacing/2))
distance += x_extent
path_points.append((center[0]-plug_width-probe_pad_width/2-x_extent, center[1]-probe_pad_spacing/2+probe_pad_height/2))
distance += probe_pad_height/2
current_x = center[0]-plug_width-probe_pad_width/2-x_extent
current_y = center[1]-probe_pad_spacing/2+probe_pad_height/2+margin+trace_width/2
path_points.append((current_x, current_y))
distance += margin + trace_width/2
for i in range(switchbacks):
current_x += x_extent + plug_width + probe_pad_width
path_points.append((current_x, current_y))
current_y += trace_spacing
path_points.append((current_x, current_y))
current_x -= x_extent + plug_width + probe_pad_width
path_points.append((current_x, current_y))
current_y += trace_spacing
path_points.append((current_x, current_y))
distance += 2 * (trace_spacing + x_extent + plug_width + probe_pad_width)
path_points.append((center[0]-plug_width-probe_pad_width/2-x_extent, center[1]+probe_pad_spacing/2))
distance += center[1]+probe_pad_spacing/2 - current_y
path_points.append((center[0]-plug_width-probe_pad_width/2, center[1]+probe_pad_spacing/2))
distance += x_extent
self.add_path_as_polygon(cell_name, path_points, trace_width, layer_name)
if text is None:
text = f"RESISTANCE {distance/1000}MM TRACE WIDTH {trace_width}UM"
else:
text += f" RESISTANCE {distance/1000}MM TRACE WIDTH {trace_width}UM"
if text_position is None:
text_position = (center[0]+probe_pad_width/2+1.5*text_height, center[1]-len(text)*text_height*TEXT_SPACING_FACTOR)
self.add_text(cell_name, text, layer_name, text_position, text_height, text_angle)
if add_interlayer_short:
assert layer_name_short is not None, "Error: Layer name for the short must be specified."
if short_text is None:
short_text = "INTERLAYER SHORT"
else:
short_text += " INTERLAYER SHORT"
# 0.75 is an arbitrary factor to place the short in a nice spot
self.add_rectangle(cell_name, layer_name_short, center=(center[0]-probe_pad_width/2, center[1]+probe_pad_height*0.75),
width=probe_pad_width, height=probe_pad_height)
self.add_rectangle(cell_name, layer_name_short, center=(center[0]-probe_pad_width/2, center[1]-probe_pad_height*0.75),
width=probe_pad_width, height=probe_pad_height)
self.add_text(cell_name, short_text, layer_name_short, (center[0]-probe_pad_width/2-len(short_text)*text_height*TEXT_SPACING_FACTOR, center[1]), text_height, 0)
def add_line_test_structure(self, cell_name, layer_name, center, text, line_width=800, line_height=80, num_lines=4, line_spacing=80,
text_height=250, text_angle=0, text_position=None):
# Check that types are valid
assert isinstance(center, tuple), "Error: Center must be a tuple."
assert isinstance(line_width, (int, float)), "Error: Line width must be a number."
assert isinstance(line_height, (int, float)), "Error: Line height must be a number."
assert isinstance(num_lines, int), "Error: Number of lines must be an integer."
assert isinstance(line_spacing, (int, float)), "Error: Line spacing must be a number."
assert isinstance(text_height, (int, float)), "Error: Text height must be a number."
assert isinstance(text_angle, (int, float)), "Error: Text angle must be a number."
if text_position is not None:
assert isinstance(text_position, tuple), "Error: Text position must be a tuple."
assert isinstance(text, str), "Error: Text must be a string."
rect_center = (center[0], center[1]+(num_lines-1)*line_spacing)
for i in range(num_lines):
self.add_rectangle(cell_name, layer_name, center=rect_center, width=line_width, height=line_height)
rect_center = (rect_center[0], rect_center[1]-2*line_spacing)
if text_position is None:
text_position = (center[0]-len(text)*text_height*TEXT_SPACING_FACTOR, center[1]+(num_lines-1)*line_spacing+text_height)
self.add_text(cell_name, text, layer_name, text_position, text_height, text_angle)
def add_p_via_test_structure(self, cell_name, layer_name_1, layer_name_2, via_layer, center, text, layer1_rect_spacing=150,
layer1_rect_width=700, layer1_rect_height=250, layer2_rect_width=600, layer2_rect_height=550,
via_width=7, via_height=7, text_height=250, text_angle=90, text_position=None):
# Check that types are valid
assert isinstance(center, tuple), "Error: Center must be a tuple."
assert isinstance(layer1_rect_spacing, (int, float)), "Error: Layer 1 rectangle spacing must be a number."
assert isinstance(layer1_rect_width, (int, float)), "Error: Layer 1 rectangle width must be a number."
assert isinstance(layer1_rect_height, (int, float)), "Error: Layer 1 rectangle height must be a number."
assert isinstance(layer2_rect_width, (int, float)), "Error: Layer 2 rectangle width must be a number."
assert isinstance(layer2_rect_height, (int, float)), "Error: Layer 2 rectangle height must be a number."
assert isinstance(via_width, (int, float)), "Error: Via width must be a number."
assert isinstance(via_height, (int, float)), "Error: Via height must be a number."
assert isinstance(text_height, (int, float)), "Error: Text height must be a number."
assert isinstance(text_angle, (int, float)), "Error: Text angle must be a number."
if text_position is not None:
assert isinstance(text_position, tuple), "Error: Text position must be a tuple."
assert isinstance(text, str), "Error: Text must be a string."
# Add rectangles for the first layer
self.add_rectangle(cell_name, layer_name_1, center=(center[0], center[1]+layer1_rect_spacing/2+layer1_rect_height/2), width=layer1_rect_width, height=layer1_rect_height)
self.add_rectangle(cell_name, layer_name_1, center=(center[0], center[1]-layer1_rect_spacing/2-layer1_rect_height/2), width=layer1_rect_width, height=layer1_rect_height)
# Add rectangle for the second layer
self.add_rectangle(cell_name, layer_name_2, center=(center[0], center[1]), width=layer2_rect_width, height=layer2_rect_height)
# Add vias
self.add_rectangle(cell_name, via_layer, center=(center[0], center[1]+layer1_rect_spacing/2+layer1_rect_height/2), width=via_width, height=via_height)
self.add_rectangle(cell_name, via_layer, center=(center[0], center[1]-layer1_rect_spacing/2-layer1_rect_height/2), width=via_width, height=via_height)
if text_position is None:
text_position = (center[0]-layer1_rect_width/2 - text_height, center[1] - len(text)*text_height*TEXT_SPACING_FACTOR)
self.add_text(cell_name, text, layer_name_1, text_position, text_height, text_angle)
def add_electronics_via_test_structure(self, cell_name, layer_name_1, layer_name_2, via_layer, center, text,
layer_1_rect_width=1550, layer_1_rect_height=700, layer_2_rect_width=600,
layer_2_rect_height=600, layer_2_rect_spacing=250, via_width=7, via_height=7, via_spacing=10,
text_height=250, text_angle=0, text_position=None):
# Check that types are valid
assert isinstance(center, tuple), "Error: Center must be a tuple."
assert isinstance(layer_1_rect_width, (int, float)), "Error: Layer 1 rectangle width must be a number."
assert isinstance(layer_1_rect_height, (int, float)), "Error: Layer 1 rectangle height must be a number."
assert isinstance(layer_2_rect_width, (int, float)), "Error: Layer 2 rectangle width must be a number."
assert isinstance(layer_2_rect_height, (int, float)), "Error: Layer 2 rectangle height must be a number."
assert isinstance(layer_2_rect_spacing, (int, float)), "Error: Layer 2 rectangle spacing must be a number."
assert isinstance(via_width, (int, float)), "Error: Via width must be a number."
assert isinstance(via_height, (int, float)), "Error: Via height must be a number."
assert isinstance(via_spacing, (int, float)), "Error: Via spacing must be a number."
assert isinstance(text_height, (int, float)), "Error: Text height must be a number."
assert isinstance(text_angle, (int, float)), "Error: Text angle must be a number."
if text_position is not None:
assert isinstance(text_position, tuple), "Error: Text position must be a tuple."
assert isinstance(text, str), "Error: Text must be a string."
# Add rectangle for the first layer
self.add_rectangle(cell_name, layer_name_1, center=(center[0], center[1]), width=layer_1_rect_width, height=layer_1_rect_height)
# Add rectangles for the second layer
self.add_rectangle(cell_name, layer_name_2, center=(center[0]-layer_2_rect_spacing/2-layer_2_rect_width/2, center[1]), width=layer_2_rect_width, height=layer_2_rect_height)
self.add_rectangle(cell_name, layer_name_2, center=(center[0]+layer_2_rect_spacing/2+layer_2_rect_width/2, center[1]), width=layer_2_rect_width, height=layer_2_rect_height)
# Add vias
self.add_rectangle(cell_name, via_layer, center=(center[0]-layer_2_rect_spacing/2-via_spacing-via_width/2, center[1]), width=via_width, height=via_height)
self.add_rectangle(cell_name, via_layer, center=(center[0]+layer_2_rect_spacing/2+via_spacing+via_width/2, center[1]), width=via_width, height=via_height)
if text_position is None:
text_position = (center[0]-len(text)*text_height*TEXT_SPACING_FACTOR, center[1] + layer_2_rect_height/2 + text_height)
self.add_text(cell_name, text, via_layer, text_position, text_height, text_angle)
# TODO: decouple trace width and spacing
def add_short_test_structure(self, cell_name, layer_name, center, text, rect_width=1300,
trace_width=5, trace_space=5, num_lines=5, group_spacing=130, num_groups=6,
num_lines_vert=100, text_height=250, text_angle=90, text_position=None):
# Check that types are valid
assert isinstance(center, tuple), "Error: Center must be a tuple."
assert isinstance(rect_width, (int, float)), "Error: Rectangle width must be a number."
assert isinstance(trace_width, (int, float)), "Error: Trace width must be a number."
assert isinstance(trace_space, (int, float)), "Error: Trace spacing must be a number."
assert isinstance(num_lines, int), "Error: Number of lines must be an integer."
assert isinstance(group_spacing, (int, float)), "Error: Group spacing must be a number."
assert isinstance(num_groups, int), "Error: Number of groups must be an integer."
assert isinstance(num_lines_vert, int), "Error: Number of vertical lines must be an integer."
assert isinstance(text_height, (int, float)), "Error: Text height must be a number."
assert isinstance(text_angle, (int, float)), "Error: Text angle must be a number."
if text_position is not None:
assert isinstance(text_position, tuple), "Error: Text position must be a tuple."
assert isinstance(text, str), "Error: Text must be a string."
trace_pitch = trace_width + trace_space
group_height = 2*num_lines*trace_width + (2*num_lines-1)*trace_space + group_spacing + trace_pitch
rect_height = group_height*num_groups+trace_space*(num_groups-1)
rect_spacing = 2*num_lines_vert*trace_width + (2*num_lines_vert-1)*trace_space + 2*trace_space
# Add rectangles
self.add_rectangle(cell_name, layer_name, center=(center[0]-rect_spacing/2-rect_width/2, center[1]), width=rect_width, height=rect_height)
self.add_rectangle(cell_name, layer_name, center=(center[0]+rect_spacing/2+rect_width/2, center[1]), width=rect_width, height=rect_height)
center1 = center[1]+rect_height/2-trace_width/2
center2 = center[1]+rect_height/2-trace_width/2-trace_pitch
for j in range(num_groups):
for i in range(num_lines):
self.add_rectangle(cell_name, layer_name, center=(center[0]-trace_space, center1), width=rect_spacing, height=trace_width)
center1 -= 2*trace_pitch
self.add_rectangle(cell_name, layer_name, center=(center[0]+trace_space, center2), width=rect_spacing, height=trace_width)
center2 -= 2*trace_pitch
center3 = center[0]-rect_spacing/2+trace_space+trace_width/2
center4 = center[0]-rect_spacing/2+trace_space+trace_width/2 + trace_pitch
for k in range(num_lines_vert):
self.add_rectangle(cell_name, layer_name, center=(center3, center1-group_spacing/2+trace_pitch), width=trace_width, height=group_spacing+trace_width)
center3 += 2*trace_pitch
self.add_rectangle(cell_name, layer_name, center=(center4, center1-group_spacing/2), width=trace_width, height=group_spacing+trace_width)
center4 += 2*trace_pitch
center1 -= group_spacing
center2 -= group_spacing
self.add_rectangle(cell_name, layer_name, center=(center[0]-trace_space, center1), width=rect_spacing, height=trace_width)
center1 -= trace_pitch
center2 -= trace_pitch
if text_position is None:
text_position = (center[0]-rect_spacing/2-rect_width-text_height, center[1] - len(text)*text_height*TEXT_SPACING_FACTOR)
self.add_text(cell_name, text, layer_name, text_position, text_height, text_angle)
def add_MLA_alignment_cell(self, box1_layer, box2_layer, cross1_layer, cross2_layer,
box_width=2000, box_height=2000, cell_name="MLA_Alignment"):
# Check that types are valid
assert isinstance(box_width, (int, float)), "Error: Box width must be a number."
assert isinstance(box_height, (int, float)), "Error: Box height must be a number."
assert isinstance(cell_name, str), "Error: Cell name must be a string."
cell = self.add_cell(cell_name)
self.add_rectangle(cell_name, box1_layer, center=(0, 0), width=box_width, height=box_height)
self.add_rectangle(cell_name, box2_layer, center=(0, 0), width=box_width, height=box_height)
self.add_MLA_alignment_mark(cell_name, cross1_layer, center=(-box_width/2, -box_width/2))
self.add_MLA_alignment_mark(cell_name, cross1_layer, center=(box_width/2, box_width/2))
self.add_MLA_alignment_mark(cell_name, cross2_layer, center=(0, 0))
def add_component(self, cell, cell_name, component, netID, layer_number=None):
# Check if component is a polygon or a CellReference
if isinstance(component, gdspy.Polygon) or isinstance(component, gdspy.Rectangle) or isinstance(component, gdspy.Text):
assert layer_number is not None, "Layer number must be specified for polygons."
cell.add(component)
self.cells[cell_name]['polygons'].append((layer_number, component.polygons[0]))
self.cells[cell_name]['netIDs'].append((layer_number, netID))
elif isinstance(component, gdspy.CellReference):
cell.add(component)
polygons_by_spec = component.get_polygons(by_spec=True)
for (lay, dat), polys in polygons_by_spec.items():
for poly in polys:
self.cells[cell_name]['polygons'].append((lay, poly))
self.cells[cell_name]['netIDs'].append((lay, netID))
elif isinstance(component, gdspy.FlexPath):
assert layer_number is not None, "Layer number must be specified for FlexPath."
cell.add(component)
polygons = component.get_polygons()
for poly in polygons:
self.cells[cell_name]['polygons'].append((layer_number, poly))
self.cells[cell_name]['netIDs'].append((layer_number, netID))
else:
raise ValueError(f"Error: Unsupported component type '{type(component)}'. Please use gdspy.Polygon, gdspy.CellReference, or gdspy.FlexPath.")
def define_layer(self, layer_name, layer_number, description=None, min_feature_size=None, min_spacing=None):
"""
Define a layer with a unique number, optional name, description, and DRC rules.
This does not create a physical layer but registers the layer's properties and associated DRC rules.
Args:
- layer_name (str): Name of the layer.
- layer_number (int): Unique number identifying the layer.
- description (str, optional): Description of the layer.
- min_feature_size (float, optional): Minimum feature size for the layer (in micrometers).
- min_spacing (float, optional): Minimum spacing between features on the layer (in micrometers).
"""
# If the layer number is already assigned to a different layer, update the layer name to the new name
existing_layer_numbers = [props['number'] for props in self.layers.values()]
if layer_number in existing_layer_numbers:
existing_layer_name = list(self.layers.keys())[existing_layer_numbers.index(layer_number)]
if layer_name != existing_layer_name:
print(f"Warning: Layer number {layer_number} is already assigned to layer '{existing_layer_name}'. Updating layer name to '{layer_name}'.")
self.layers[layer_name] = self.layers.pop(existing_layer_name)
# Validate DRC parameters
if min_feature_size is not None and min_feature_size <= 0:
raise ValueError("Minimum feature size must be positive.")
if min_spacing is not None and min_spacing <= 0:
raise ValueError("Minimum spacing must be positive.")
# Store layer properties
self.layers[layer_name] = {'number': layer_number, 'description': description}
# Store DRC rules for the layer
self.drc_rules[layer_name] = {'min_feature_size': min_feature_size, 'min_spacing': min_spacing}
def get_layer_number(self, layer_name):
if layer_name not in self.layers:
raise ValueError(f"Error: Layer name '{layer_name}' not defined. Please define layer first.")
return self.layers[layer_name]['number']
def check_cell_exists(self, cell_name):
if cell_name not in self.cells:
raise ValueError(f"Error: Cell '{cell_name}' does not exist. Please add it first.")
return self.cells[cell_name]['cell']
def add_rectangle(self, cell_name, layer_name, center=None, width=None, height=None, lower_left=None, upper_right=None, datatype=0,
rotation=0, netID=0):
"""
Add a rectangle to a cell. The rectangle can be defined either by center point and width/height
or by specifying lower left and upper right corners.
Args:
- cell_name (str): Name of the cell to which the rectangle will be added.
- args: Variable arguments, can be either (center, width, height) or (lower_left, upper_right).
- layer_name (str): Name of the layer.
- datatype (int): Datatype for the layer (default: 0).
"""
cell = self.check_cell_exists(cell_name)
layer_number = self.get_layer_number(layer_name)
if center is not None and width is not None and height is not None:
# Assume center, width, height format
lower_left = (center[0] - width / 2, center[1] - height / 2)
upper_right = (center[0] + width / 2, center[1] + height / 2)
elif lower_left is None or upper_right is None:
raise ValueError("Error: Invalid arguments. Please specify center, width, height or lower_left, upper_right.")
# Create and add the rectangle
rectangle = gdspy.Rectangle(lower_left, upper_right, layer=layer_number, datatype=datatype)
if center is None:
center = ((lower_left[0] + upper_right[0]) / 2, (lower_left[1] + upper_right[1]) / 2)
rectangle.rotate(rotation, center=center)
self.add_component(cell, cell_name, rectangle, netID, layer_number)
def add_alignment_cross(self, cell_name, layer_name, center, width, extent_x, extent_y, datatype=0, netID=0):
"""
Add an alignment cross to the specified cell and layer.
Args:
- cell_name (str): The name of the cell to add the cross to.
- layer_name (str): The name of the layer to add the cross to.
- center (tuple): (x, y) coordinates for the center of the cross.
- width (float): The width of the arms of the cross.
- extent_x (float): The total length of the cross arm along the x-axis.
- extent_y (float): The total length of the cross arm along the y-axis.
- datatype (int): The datatype for the layer (default: 0).
"""
cell = self.check_cell_exists(cell_name)
layer_number = self.get_layer_number(layer_name)
# Calculate the coordinates for the horizontal and vertical parts of the cross
horizontal_lower_left = (center[0] - extent_x / 2, center[1] - width / 2)
horizontal_upper_right = (center[0] + extent_x / 2, center[1] + width / 2)
vertical_lower_left = (center[0] - width / 2, center[1] - extent_y / 2)
vertical_upper_right = (center[0] + width / 2, center[1] + extent_y / 2)
# Create rectangles for the cross
horizontal_rect = gdspy.Rectangle(horizontal_lower_left, horizontal_upper_right, layer=layer_number, datatype=datatype)
vertical_rect = gdspy.Rectangle(vertical_lower_left, vertical_upper_right, layer=layer_number, datatype=datatype)
# Add the rectangles to the cell
self.add_component(cell, cell_name, horizontal_rect, netID, layer_number)
self.add_component(cell, cell_name, vertical_rect, netID, layer_number)
def add_text(self, cell_name, text, layer_name, position, height, angle=0, datatype=0, netID=0):
layer_number = self.get_layer_number(layer_name)
cell = self.check_cell_exists(cell_name)
# Create the text object using KLayout
layout = kdb.Layout()
layout.dbu = 1e-3 # 0.001 microns per layout unit
top_cell = layout.create_cell("TOP")
# Set the layer
layer_info = kdb.LayerInfo(1, 0) # Layer 1 with datatype 0
layer_index = layout.layer(layer_info)
# Create the text
text_shape = kdb.TextGenerator.default_generator().text(text, layout.dbu/height)
# Prepare transformation matrix
angle_rad = np.deg2rad(angle)
rotation_matrix = np.array([
[np.cos(angle_rad), -np.sin(angle_rad)],
[np.sin(angle_rad), np.cos(angle_rad)]
])
translation_vector = np.array(position)
top_cell.shapes(layer_index).insert(text_shape)
# Extract polygons from the text shape and convert to gdspy polygons
for polygon in text_shape.each():
points = []
for edge in polygon.each_edge():
point = np.array([edge.x1 * layout.dbu, edge.y1 * layout.dbu])
rotated_point = rotation_matrix.dot(point)
transformed_point = rotated_point + translation_vector
points.append(tuple(transformed_point))
self.add_polygon(cell_name, points, layer_name, datatype, netID)
def add_polygon(self, cell_name, points, layer_name, datatype=0, netID=0):
layer_number = self.get_layer_number(layer_name)
cell = self.check_cell_exists(cell_name)
polygon = gdspy.Polygon(points, layer=layer_number, datatype=datatype)
self.add_component(cell, cell_name, polygon, netID, layer_number)
def add_path_as_polygon(self, cell_name, points, width, layer_name, datatype=0, netID=0, as_path=True, ends='flush'):
"""
Convert a path defined by a series of points and a width into a polygon and add it to the specified cell and layer.
Args:
- cell_name (str): The name of the cell to which the path will be added.
- points (list of tuples): The waypoints of the path: points along the center of the path.
- width (float): The width of the path.
- layer_name (str): The name of the layer.
- datatype (int): The datatype for the polygon.
"""
# Ensure the layer exists and retrieve its number
layer_number = self.get_layer_number(layer_name)
# Ensure the cell exists
cell = self.check_cell_exists(cell_name)
# Create the path as a polygon
path = gdspy.FlexPath(points, width, layer=layer_number, datatype=datatype, gdsii_path=as_path, ends=ends)
if not as_path:
path_polygons = path.to_polygonset() # Corrected method call here
# Add the generated polygons to the cell
for poly in path_polygons.polygons:
path_polygon = gdspy.Polygon(poly, layer=layer_number, datatype=datatype)
self.add_component(cell, cell_name, path_polygon, netID, layer_number)
else:
self.add_component(cell, cell_name, path, netID, layer_number)
def add_circle_as_polygon(self, cell_name, center, radius, layer_name, num_points=500, datatype=0, netID=0):
"""
Create a circle and immediately approximate it as a polygon with a specified number of points.
The approximated circle (polygon) is then added to the specified cell and layer.
Args:
- cell_name (str): The name of the cell to which the circle will be added.
- center (tuple): The (x, y) coordinates of the circle's center.
- radius (float): The radius of the circle.
- layer_name (str): The name of the layer.
- num_points (int): The number of points to use for the circle approximation.
- datatype (int): The datatype for the polygon.
"""
# Ensure the layer exists and retrieve its number
layer_number = self.get_layer_number(layer_name)
# Ensure the cell exists
cell = self.check_cell_exists(cell_name)
# Calculate the points that approximate the circle
angles = np.linspace(0, 2 * np.pi, num_points, endpoint=False)
x_points = center[0] + np.cos(angles) * radius
y_points = center[1] + np.sin(angles) * radius
points = np.vstack((x_points, y_points)).T # Transpose to get an array of (x, y) points
# Create and add the polygon to the specified cell and layer
polygon = gdspy.Polygon(points, layer=layer_number, datatype=datatype)
self.add_component(cell, cell_name, polygon, netID, layer_number)
def add_cell_reference(self, parent_cell_name, child_cell_name, origin=(0, 0), magnification=1, rotation=0,
x_reflection=False, netID=0):
parent_cell = self.lib.cells[parent_cell_name]
child_cell = self.lib.cells[child_cell_name]
ref = gdspy.CellReference(child_cell, origin=origin, magnification=magnification, rotation=rotation, x_reflection=x_reflection)
self.add_component(parent_cell, parent_cell_name, ref, netID)
def add_cell_array(self, target_cell_name, cell_name_to_array, copies_x, copies_y, spacing_x, spacing_y, origin=(0, 0),
magnification=1, rotation=0, x_reflection=False, netIDs=None):
target_cell = self.lib.cells[target_cell_name]
cell_to_array = self.lib.cells[cell_name_to_array]
# Calculate the start position to center the array around the specified origin
total_length_x = spacing_x * (copies_x - 1)
total_length_y = spacing_y * (copies_y - 1)
start_x = origin[0] - (total_length_x / 2)
start_y = origin[1] - (total_length_y / 2)
cnt = 0
for i in range(copies_x):
for j in range(copies_y):
x_position = start_x + (i * spacing_x)
y_position = start_y + (j * spacing_y)
# Add a cell reference (arrayed cell) at the calculated position to the target cell
ref = gdspy.CellReference(cell_to_array, origin=(x_position, y_position), magnification=magnification,
rotation=rotation, x_reflection=x_reflection)
self.add_component(target_cell, target_cell_name, ref, netIDs[i][j] if netIDs is not None else cnt)
cnt += 1
def check_space_for_traces(self, trace_width, trace_space, num_traces, effective_pitch):
assert round(trace_width*num_traces+trace_space*(num_traces+1), 3) <= effective_pitch, f"Not enough space for {num_traces} traces with trace width {trace_width}, trace spacing {trace_space} and effective pitch {effective_pitch}."
def add_regular_array_escape_two_sided(self, trace_cell_name, center, layer_name, pitch_x, pitch_y, array_size_x, array_size_y, trace_width, pad_diameter, escape_extent=50, routing_angle=45,
escape_y=False, trace_space=None, autorouting_angle=45, cable_tie_routing_angle=45):
self.check_cell_exists(trace_cell_name)
assert isinstance(center, tuple), "Error: Center must be a tuple."
assert isinstance(pitch_x, (int, float)), "Error: Pitch in the x-direction must be a number."
assert isinstance(pitch_y, (int, float)), "Error: Pitch in the y-direction must be a number."
assert isinstance(array_size_x, int), "Error: Array size in the x-direction must be an integer."
assert isinstance(array_size_y, int), "Error: Array size in the y-direction must be an integer."
assert isinstance(trace_width, (int, float)), "Error: Trace width must be a number."
assert isinstance(pad_diameter, (int, float)), "Error: Pad diameter must be a number."
assert isinstance(escape_extent, (int, float)), "Error: Escape extent must be a number."
assert isinstance(routing_angle, (int, float)), "Error: Routing angle must be a number."
assert isinstance(escape_y, bool), "Error: Escape direction must be a boolean."
if trace_space is None:
trace_space = trace_width
assert isinstance(trace_space, (int, float)), "Error: Trace space must be a number."
trace_space = round(trace_space/np.sin(autorouting_angle*np.pi/180), 1)
effective_pitch_y = pitch_y - pad_diameter
effective_pitch_x = pitch_x - pad_diameter
# Create the 2D grid using NumPy
x = np.linspace(-pitch_x*(array_size_x-1)/2, pitch_x*(array_size_x-1)/2, array_size_x)
y = np.linspace(-pitch_y*(array_size_y-1)/2, pitch_y*(array_size_y-1)/2, array_size_y)
xx, yy = np.meshgrid(x, y, indexing='ij')
# Stack the coordinates into a single 3D array
grid = np.stack((xx, yy), axis=-1)
ports = np.full_like(grid, np.nan)
orientations = np.full((grid.shape[0], grid.shape[1], 1), np.nan)
grid[:, :, 0] = grid[:, :, 0] + center[0]
grid[:, :, 1] = grid[:, :, 1] + center[1]
available_length_y = effective_pitch_y - 2*trace_space - trace_width
available_length_x = effective_pitch_x - 2*trace_space - trace_width
if not escape_y:
for j in range(int(array_size_y/2)):
num_traces = int(array_size_x/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_y)
spacing = available_length_y / (num_traces - 1)
cnt = 0
for i in range(int(array_size_x/2)):
hinged_path = create_hinged_path(grid[i][j], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[i][j][0]-grid[0][j][0]+escape_extent, post_rotation=180, post_reflection=False)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[i][j] = np.array(hinged_path[-1])
orientations[i][j] = 180
cnt += 1
num_traces = array_size_x - int(array_size_x/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_y)
spacing = available_length_y / (num_traces - 1)
cnt = 0
iter_inds = np.flip(np.arange(int(array_size_x/2), array_size_x))
for i in iter_inds:
hinged_path = create_hinged_path(grid[i][j], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[-1][j][0]-grid[i][j][0]+escape_extent, post_rotation=180, post_reflection=True)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[i][j] = np.array(hinged_path[-1])
orientations[i][j] = 0
cnt += 1
for j in range(int(array_size_y/2), array_size_y):
num_traces = int(array_size_x/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_y)
spacing = available_length_y / (num_traces - 1)
cnt = 0
for i in range(int(array_size_x/2)):
hinged_path = create_hinged_path(grid[i][j], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[i][j][0]-grid[0][j][0]+escape_extent, post_rotation=0, post_reflection=True)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[i][j] = np.array(hinged_path[-1])
orientations[i][j] = 180
cnt += 1
num_traces = array_size_x - int(array_size_x/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_y)
spacing = available_length_y / (num_traces - 1)
cnt = 0
iter_inds = np.flip(np.arange(int(array_size_x/2), array_size_x))
for i in iter_inds:
hinged_path = create_hinged_path(grid[i][j], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[-1][j][0]-grid[i][j][0]+escape_extent, post_rotation=0, post_reflection=False)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[i][j] = np.array(hinged_path[-1])
orientations[i][j] = 0
cnt += 1
else:
for j in range(int(array_size_x/2)):
num_traces = int(array_size_y/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_x)
spacing = available_length_x / (num_traces - 1)
cnt = 0
for i in range(int(array_size_y/2)):
hinged_path = create_hinged_path(grid[j][i], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[j][i][1]-grid[j][0][1]+escape_extent, post_rotation=90, post_reflection=True)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[j][i] = np.array(hinged_path[-1])
orientations[j][i] = 270
cnt += 1
for j in range(int(array_size_x/2), array_size_x):
num_traces = int(array_size_y/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_x)
spacing = available_length_x / (num_traces - 1)
cnt = 0
for i in range(int(array_size_y/2)):
hinged_path = create_hinged_path(grid[j][i], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[j][i][1]-grid[j][0][1]+escape_extent, post_rotation=-90, post_reflection=False)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[j][i] = np.array(hinged_path[-1])
orientations[j][i] = 270
cnt += 1
for j in range(int(array_size_x/2)):
num_traces = array_size_y - int(array_size_y/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_x)
spacing = available_length_x / (num_traces - 1)
cnt = 0
iter_inds = np.flip(np.arange(int(array_size_y/2), array_size_y))
for i in iter_inds:
hinged_path = create_hinged_path(grid[j][i], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[j][-1][1]-grid[j][i][1]+escape_extent, post_rotation=90, post_reflection=False)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[j][i] = np.array(hinged_path[-1])
orientations[j][i] = 90
cnt += 1
for j in range(int(array_size_x/2), array_size_x):
num_traces = array_size_y - int(array_size_y/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_x)
spacing = available_length_x / (num_traces - 1)
cnt = 0
iter_inds = np.flip(np.arange(int(array_size_y/2), array_size_y))
for i in iter_inds:
hinged_path = create_hinged_path(grid[j][i], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[j][-1][1]-grid[j][i][1]+escape_extent, post_rotation=-90, post_reflection=True)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[j][i] = np.array(hinged_path[-1])
orientations[j][i] = 90
cnt += 1
grid, ports, orientations = np.around(grid.reshape(array_size_x*array_size_y, 2), 3), np.around(ports.reshape(array_size_x*array_size_y, 2), 3), orientations.reshape(array_size_x*array_size_y)
unique_orientations = np.unique(orientations)
return_dict = {}
for val in unique_orientations:
idx = np.where(orientations == val)[0]
wire_ports, wire_orientations = self.cable_tie_ports(trace_cell_name, layer_name, ports[idx], orientations[idx], trace_width, trace_space, routing_angle=cable_tie_routing_angle,
escape_extent=escape_extent)
return_dict[val] = {}
return_dict[val]['ports'] = wire_ports
return_dict[val]['orientations'] = wire_orientations
return_dict[val]['layer_number'] = self.get_layer_number(layer_name)
return_dict[val]['trace_width'] = trace_width
return_dict[val]['trace_space'] = trace_space
return return_dict
# The traces escape from the array on the positive and negative x directions and the positive y direction
def add_regular_array_escape_three_sided(self, trace_cell_name, center, layer_name, pitch_x, pitch_y, array_size_x, array_size_y, trace_width, pad_diameter, escape_extent=50, routing_angle=45,
escape_y=True, escape_negative=False, trace_space=None, autorouting_angle=45, cable_tie_routing_angle=45):
self.check_cell_exists(trace_cell_name)
assert isinstance(center, tuple), "Error: Center must be a tuple."
assert isinstance(pitch_x, (int, float)), "Error: Pitch in the x-direction must be a number."
assert isinstance(pitch_y, (int, float)), "Error: Pitch in the y-direction must be a number."
assert isinstance(array_size_x, int), "Error: Array size in the x-direction must be an integer."
assert isinstance(array_size_y, int), "Error: Array size in the y-direction must be an integer."
assert isinstance(trace_width, (int, float)), "Error: Trace width must be a number."
assert isinstance(pad_diameter, (int, float)), "Error: Pad diameter must be a number."
assert isinstance(escape_extent, (int, float)), "Error: Escape extent must be a number."
assert isinstance(routing_angle, (int, float)), "Error: Routing angle must be a number."
assert isinstance(escape_y, bool), "Error: Escape direction must be a boolean."
assert isinstance(escape_negative, bool), "Error: Escape negative must be a boolean."
if trace_space is None:
trace_space = trace_width
assert isinstance(trace_space, (int, float)), "Error: Trace space must be a number."
trace_space = round(trace_space/np.sin(autorouting_angle*np.pi/180), 1)
effective_pitch_y = pitch_y - pad_diameter
effective_pitch_x = pitch_x - pad_diameter
# Create the 2D grid using NumPy
x = np.linspace(-pitch_x*(array_size_x-1)/2, pitch_x*(array_size_x-1)/2, array_size_x)
y = np.linspace(-pitch_y*(array_size_y-1)/2, pitch_y*(array_size_y-1)/2, array_size_y)
xx, yy = np.meshgrid(x, y, indexing='ij')
# Stack the coordinates into a single 3D array
grid = np.stack((xx, yy), axis=-1)
ports = np.full_like(grid, np.nan)
orientations = np.full((grid.shape[0], grid.shape[1], 1), np.nan)
grid[:, :, 0] = grid[:, :, 0] + center[0]
grid[:, :, 1] = grid[:, :, 1] + center[1]
available_length_y = effective_pitch_y - 2*trace_space - trace_width
available_length_x = effective_pitch_x - 2*trace_space - trace_width
if escape_y:
if not escape_negative:
for j in range(int(array_size_y/2)):
num_traces = int(array_size_x/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_y)
spacing = available_length_y / (num_traces - 1)
cnt = 0
for i in range(int(array_size_x/2)):
hinged_path = create_hinged_path(grid[i][j], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[i][j][0]-grid[0][j][0]+escape_extent, post_rotation=180, post_reflection=False)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[i][j] = np.array(hinged_path[-1])
orientations[i][j] = 180
cnt += 1
num_traces = array_size_x - int(array_size_x/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_y)
spacing = available_length_y/ (num_traces - 1)
cnt = 0
iter_inds = np.flip(np.arange(int(array_size_x/2), array_size_x))
for i in iter_inds:
hinged_path = create_hinged_path(grid[i][j], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[-1][j][0]-grid[i][j][0]+escape_extent, post_rotation=180, post_reflection=True)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[i][j] = np.array(hinged_path[-1])
orientations[i][j] = 0
cnt += 1
for j in range(int(array_size_x/2)):
num_traces = array_size_y - int(array_size_y/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_x)
spacing = available_length_x / (num_traces - 1)
cnt = 0
iter_inds = np.flip(np.arange(int(array_size_y/2), array_size_y))
for i in iter_inds:
hinged_path = create_hinged_path(grid[j][i], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[j][-1][1]-grid[j][i][1]+escape_extent, post_rotation=90, post_reflection=False)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[j][i] = np.array(hinged_path[-1])
orientations[j][i] = 90
cnt += 1
for j in range(int(array_size_x/2), array_size_x):
num_traces = array_size_y - int(array_size_y/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_x)
spacing = available_length_x / (num_traces - 1)
cnt = 0
iter_inds = np.flip(np.arange(int(array_size_y/2), array_size_y))
for i in iter_inds:
hinged_path = create_hinged_path(grid[j][i], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[j][-1][1]-grid[j][i][1]+escape_extent, post_rotation=-90, post_reflection=True)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[j][i] = np.array(hinged_path[-1])
orientations[j][i] = 90
cnt += 1
else:
for j in range(int(array_size_x/2)):
num_traces = int(array_size_y/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_x)
spacing = available_length_x / (num_traces - 1)
cnt = 0
for i in range(int(array_size_y/2)):
hinged_path = create_hinged_path(grid[j][i], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[j][i][1]-grid[j][0][1]+escape_extent, post_rotation=90, post_reflection=True)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[j][i] = np.array(hinged_path[-1])
orientations[j][i] = 270
cnt += 1
for j in range(int(array_size_x/2), array_size_x):
num_traces = int(array_size_y/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_x)
spacing = available_length_x / (num_traces - 1)
cnt = 0
for i in range(int(array_size_y/2)):
hinged_path = create_hinged_path(grid[j][i], routing_angle, cnt*spacing + trace_width/2 + trace_space + pad_diameter/2, grid[j][i][1]-grid[j][0][1]+escape_extent, post_rotation=-90, post_reflection=False)
self.add_path_as_polygon(trace_cell_name, hinged_path, trace_width, layer_name)
ports[j][i] = np.array(hinged_path[-1])
orientations[j][i] = 270
cnt += 1
for j in range(int(array_size_y/2), array_size_y):
num_traces = int(array_size_x/2)
self.check_space_for_traces(trace_width, trace_space, num_traces, effective_pitch_y)