-
Notifications
You must be signed in to change notification settings - Fork 10
/
renumber.rb
184 lines (160 loc) · 3.69 KB
/
renumber.rb
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
class BrdCmd < Array
RegExp = /([^"\s]+|"[^"]*")\s*/
def initialize(str)
self.replace(str.chomp.scan(RegExp).flatten)
@xargs = []
end
def <<(xarg)
@xargs << xarg.chomp
end
def to_s
s = self.join(" ") + "\n"
s += "\t" + @xargs.join("\n\t") + "\n" unless @xargs.empty?
s
end
def inspect
"<BrdCmd: #{self.join(' ')}#{@xargs.empty? ? "" : " ..."}>"
end
end
class BrdElm < Array
RegExp = /^[$](.*)$/
RegExpEnd = /^[$]End(.*)$/
def initialize(str, root=false)
@root = root
m = RegExp.match(str.chomp)
raise RuntimeError, "invalid arg" if not m
@elms = m[1].split
end
def elm
@elms[0]
end
def check_end(str)
m = RegExpEnd.match(str.chomp)
raise RuntimeError, "invalid arg" if not m || m[1..-1] != @elms
true
end
def each_elm(name=nil)
self.each do |e|
next if not BrdElm === e
next if name and not e.elm == name
yield e
end
end
def to_s
pre = "$%s\n" % @elms.join(" ")
pre = "" if @root
pre +
self.map(&:to_s).join +
("$End%s\n" % @elms.join(" "))
end
def inspect
"<BrdElm #{@elms.join(' ')}: #{@self.map(&:inspect).join(', ')}>"
end
end
class BrdRewrite
def initialize(f, delim='BOARD')
@f = f
@b = nil
@delim = '$' + delim
end
def parse
@b = BrdElm.new(@delim, true)
s = [@b]
@f.each do |l|
case l
when /^\s*$/
# nothing
when /^\s+/
# continuation arguments
s.last.last << l
when BrdElm::RegExpEnd
begin
s.last.check_end(l)
rescue Exception, e
$stderr.puts "incorrect order: #{e}"
end
s.pop
when BrdElm::RegExp
e = BrdElm.new(l)
s.last << e
s << e
else
s.last << BrdCmd.new(l)
end
end
@b
end
end
class SchRewrite
def initialize(f)
@f = f
end
def each_elm_recursive(name=nil, &blk)
recurse(@f, '', name, &blk)
end
def recurse(fn, reclvl, name, &blk)
puts "recursing: #{fn}"
sch = BrdRewrite.new(File.readlines(fn), 'SCHEMATC').parse
sch.each_elm('Sheet') do |e|
rfn = e.assoc('F1')[1][1..-2]
ts = e.assoc('U')[1]
recurse(rfn, '/'+ts, name, &blk)
end
sch.each_elm(name) do |e|
next if e.elm == 'Sheet'
next if name && name != e.elm
yield e, reclvl
end
puts "writing: #{fn}"
File.open(fn + '-renumbered', 'w') do |f|
f.print sch
end
end
end
if $0 == __FILE__
brd = BrdRewrite.new(File.readlines(ARGV[0])).parse
modules = {}
brd.each_elm('MODULE') do |m|
po = m.assoc('Po')
id = m.assoc('AR')[1]
modname = m.assoc('T0')
nm = modname[11][1..-2]
layer = po[4].to_i
if layer == 15
layer = 0
else
layer = 1
end
modules[id] = {:layer => layer, :x => po[1].to_i, :y => po[2].to_i, :ref => nm, :id => id}
end
refnums = Hash.new{|h, k| h[k] = 1}
modules.sort_by{|_, e| [e[:layer], e[:x], e[:y]]}.each do |_, m|
pref = m[:ref][/^[[:alpha:]]+/]
num = m[:ref][/[[:digit:]]+$/]
newnum = refnums[pref]
refnums[pref] += 1
m[:newref] = "#{pref}#{newnum}"
end
sch = SchRewrite.new(ARGV[1])
sch.each_elm_recursive('Comp') do |c, rec|
l = c.assoc('L')
nm = l[2]
next if nm[0] == '#'
u = c.assoc('U')
id = u[3]
rid = rec + '/' + id
mod = modules[rid]
next unless mod
nmfield = c.select{|a| a[0] == 'F' && a[1] == '0'}
if !nmfield.empty?
nmtext = nmfield.first[2][1..-2]
end
newref = mod[:newref]
next if nm == newref && (nmfield.empty? || nmtext == newref)
puts "renumbering #{nm} to #{newref}"
l[2] = newref
nmfield.each do |f|
f[2] = %{"#{newref}"}
end
end
end