-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpl2def.py
114 lines (83 loc) · 3.36 KB
/
pl2def.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
import re
def parse_line(line):
pattern = r'(\S+)\s+(\d+)\s+(\d+)\s*:\s*(\w)'
match = re.match(pattern, line)
if match:
name = match.group(1)
x_coord = match.group(2)
y_coord = match.group(3)
direction = match.group(4)
return [name, x_coord, y_coord, ":", direction]
else:
return None
def read_pl(pl_file):
pl_dict={}
with open(pl_file, "r") as f:
lines = f.readlines()
for line in lines:
line = parse_line(line)
#print(line)
if len(line) >= 4:
# x, y, orientation
#print(line[0])
pl_dict[line[0]] = [float(line[1]), float(line[2]), line[4]]
return pl_dict
def unscale_xy(node_x,node_y, shift_factor, scale_factor):
unscale_factor = 1.0 / scale_factor
if shift_factor[0] == 0 and shift_factor[1] == 0 and unscale_factor == 1.0:
x = node_x
y = node_y
else:
x = node_x * unscale_factor + shift_factor[0]
y = node_y * unscale_factor + shift_factor[1]
return x,y
def write2def(pl_dict,def_file,target_filename):
def_lines = []
with open(def_file, "r") as f:
lines = f.readlines()
start = False
for line in lines:
if line.startswith("COMPONENTS"):
#print(line)
start = True
if "END COMPONENTS" in line:
#print(line)
start = False
if start and line.lstrip().startswith("-"):
#print(line)
split = line.lstrip().split(" ")
#print(split)
instance = (
split[1].replace("\[", "[").replace("\]", "]").replace("\/", "/")
)
#print(instance)
if instance in pl_dict and len(split)> 5 :
x, y, o = pl_dict[instance]
x=round(x / 10) * 10
y=round(y / 10) * 10
old_pos = line[line.find("(") : line.find(")") + 1]
line = line.replace(old_pos, "( %d %d )" % (x, y))
line = re.sub(
r"(.*\) )(N|S|W|E|FN|FS|FW|FE)($| )", r"\1%s\3" % o, line
)
def_lines.append(line)
with open(target_filename, "w") as f:
f.writelines(def_lines)
'''
If the node coordinates in the pl file have been scaled by DreamPlace,
you can specify shift_factor and scale_factor in the function to unscale them.
These are generally the params.shift_factor and params.scale_factor in DreamPlace.
'''
def pl2def(pl_input,def_input,final_def,shift_factor=[0,0],scale_factor=1):
pl_dict=read_pl(pl_input)
for key in pl_dict:
pl_dict[key][0],pl_dict[key][1]=unscale_xy(pl_dict[key][0],pl_dict[key][1],shift_factor, scale_factor)
write2def(pl_dict,def_input,final_def)
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process some files.')
parser.add_argument('--pl', required=True, help='Path to the .pl file')
parser.add_argument('--inputdef', required=True, help='Path to the input .def file')
parser.add_argument('--outputdef', required=True, help='Path to the output .def file')
args = parser.parse_args()
pl2def(args.pl, args.inputdef, args.outputdef)