-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheagle.py
1375 lines (1200 loc) · 51 KB
/
eagle.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 sys, os
import struct
def dump_hex(data):
print ' '.join('%02x' % (ord(byte),) for byte in data)
def dump_hex_ascii(data):
print ' '.join('%02x' % (ord(byte),) for byte in data), ''.join((byte if 32 <= ord(byte) < 127 else '.') for byte in data)
def dump_dec_hex_ascii(data):
print ' '.join('%03d' % (ord(byte),) for byte in data)
print ' '.join(' %02x' % (ord(byte),) for byte in data)
print ' '.join(' %s ' % (byte if 32 <= ord(byte) <= 127 else '.',) for byte in data)
print
# Values are in tenths of a micrometre
u2mm = lambda val: val/10/1000
u2in = lambda val: val/2.54/100/1000
mm2u = lambda val: val*10*1000
in2u = lambda val: val*254*1000
class Section:
sectype = None
secname = None
subsec_names = []
def __init__(self, eaglefile, parent, data):
self.eaglefile = eaglefile
self.parent = parent
self.data = data
# Default is unknown, but self.unknown lists known unknowns
self.unknown = [0x00] * 24
self.known = [0x00] * 24
self.zero = [0x00] * 24
self.subsec_counts = []
self._get_uint8(0)
self._get_unknown(1, 1)
self.parse()
def _get_color(self, known, zero = 0, unknown = 0):
assert known & zero == 0x00, "Bit can't be known and zero at the same time! (known = %s, zero = %s, known & zero = %s)" % (hex(known), hex(zero), hex(known & zero))
assert known & unknown == 0x00, "Bit can't be known and unknown at the same time! (known = %s, unknown = %s, known & unknown = %s)" % (hex(known), hex(unknown), hex(known & unknown))
assert unknown & zero == 0x00, "Bit can't be unknown and zero at the same time! (unknown = %s, zero = %s, unknown & zero = %s)" % (hex(unknown), hex(zero), hex(unknown & zero))
if unknown == 0xff:
color = '1;31' # unknown, red
elif known == 0xff:
color = '1;32' # known, green
elif zero == 0xff:
color = '1;34' # zero, blue
elif known and unknown and not zero:
color = '1;33' # known/unknown, yellow
elif unknown and zero and not known:
color = '1;35' # unknown/zero, purple
elif known and zero and not unknown:
color = '1;36' # known/zero, cyan
elif known and unknown and zero:
color = '1;37' # known/unknown/zero, white
else:
color = '30'
if known | unknown | zero != 0xff:
# Unknown unknown, red background, but not red on red
if color == '1;31':
color = '30;41'
else:
color += ';41'
return color
def hexdump(self):
colorhex = ' '.join('\x1b[%sm%02x\x1b[m' % (self._get_color(known, zero, unknown), ord(byte)) for byte, known, zero, unknown in zip(self.data, self.known, self.zero, self.unknown))
colorascii = ''.join('\x1b[%sm%s\x1b[m' % (self._get_color(known, zero, unknown), byte if 32 <= ord(byte) < 127 else '.') for byte, known, zero, unknown in zip(self.data, self.known, self.zero, self.unknown))
print colorhex, colorascii
def _get_bytes(self, pos, size):
self.known[pos:pos+size] = [0xff] * size
return self.data[pos:pos+size]
def _get_unknown(self, pos, size):
self.unknown[pos:pos+size] = [0xff] * size
def _get_unknown_mask(self, pos, mask):
self.unknown[pos] |= mask
def _get_zero(self, pos, size):
self.zero[pos:pos+size] = [0xff] * size
assert self.data[pos:pos+size] == '\x00' * size, 'Unknown data in ' + self.secname + ': ' + repr(self.data[pos:pos+size])
def _get_zero_mask(self, pos, mask):
self.zero[pos] |= mask
assert ord(self.data[pos]) & mask == 0x00, 'Unknown bits in ' + self.secname + ': ' + hex(ord(self.data[pos]) & mask)
def _get_zero16_mask(self, pos, mask):
self.zero[pos] |= mask & 0xff
self.zero[pos+1] |= (mask >> 8) & 0xff
value = struct.unpack('<H', self.data[pos:pos+2])[0] & mask
assert value == 0x00, 'Unknown bits in ' + self.secname + ': ' + hex(value)
def _get_uint32(self, pos): return struct.unpack('<I', self._get_bytes(pos, 4))[0]
def _get_uint24(self, pos): return struct.unpack('<I', self._get_bytes(pos, 3) + '\x00')[0]
def _get_uint16(self, pos): return struct.unpack('<H', self._get_bytes(pos, 2))[0]
def _get_uint8(self, pos): return struct.unpack('<B', self._get_bytes(pos, 1))[0]
def _get_int32(self, pos): return struct.unpack('<i', self._get_bytes(pos, 4))[0]
def _get_int16(self, pos): return struct.unpack('<h', self._get_bytes(pos, 2))[0]
def _get_int8(self, pos): return struct.unpack('<b', self._get_bytes(pos, 1))[0]
def _get_double(self, pos): return struct.unpack('<d', self._get_bytes(pos, 8))[0]
def _get_uint8_mask(self, pos, mask):
self.known[pos] |= mask
return ord(self.data[pos]) & mask
def _get_uint16_mask(self, pos, mask):
self.known[pos] |= mask & 0xff
self.known[pos+1] |= (mask >> 8) & 0xff
return struct.unpack('<H', self.data[pos:pos+2])[0] & mask
def _get_name(self, pos, size): return self._parse_string(self._get_bytes(pos, size))
def _parse_string(self, name):
# Apparently there is no better way to do this..
# There are some random bytes after 0x7f, but that's just what they are - random
# Making a text longer, then shorter again does not result in the same random bytes
# Making a text from the beginning longer does not affect the random bytes of the following entries
# Deleting a text from the beginning does not affect the random bytes of the following entries
# Thus, there really can't be any position information there
# Thus, we'd just better handle every possible text field...
if __name__ == '__main__':
if name[0] != '\x7f':
return '\x1b[32m' + repr(name.rstrip('\x00')) + '\x1b[m'
return '\x1b[31m' + repr(self.eaglefile._get_next_string()) + '\x1b[m'
else:
if name[0] != '\x7f':
return name.rstrip('\x00')
return self.eaglefile._get_next_string()
class UnknownSection(Section):
secname = '???'
def parse(self):
self._get_unknown(2, 22)
def __str__(self):
return self.secname
class TextBaseSection(Section):
def parse(self):
self.font = self._get_uint8_mask(2, 0x03)
self._get_zero_mask(2, 0xfc)
self.layer = self._get_uint8(3)
self.x = self._get_int32(4)
self.y = self._get_int32(8)
self.size_2 = self._get_uint16(12)
self.ratio = self._get_uint8_mask(14, 0x7c) >> 2
self._get_zero_mask(14, 0x03)
self._get_unknown_mask(14, 0x80)
self._get_unknown(15, 1)
self.angle = self._get_uint16_mask(16, 0x0fff)
self.mirrored = bool(self._get_uint16_mask(16, 0x1000))
self.spin = bool(self._get_uint16_mask(16, 0x4000))
self._get_zero16_mask(16, 0xa000)
if self.sectype in (0x31, 0x41):
self.text = self._get_name(18, 6)
else:
self._get_bytes(18, 6)
self.text = ''
# Mostly true, but 0x01 has been spotted once
#assert self.sectype in (0x31, 0x41) or self.data[18:24] == '\x00'*6, 'This section shouldn\'t contain any text?'
assert self.sectype in (0x31, 0x41) or self.data[18] != '\x7f', 'This section shouldn\'t contain any text?'
def __str__(self):
font = 'vector proportional fixed'.split()[self.font]
angle = 360 * self.angle / 4096.
if self.sectype == 0x31:
extra = ', text ' + self.text
elif self.sectype == 0x41:
extra = ', name ' + self.text
else:
extra = ''
return '%s: at (%f", %f") size %f", angle %s, mirror %d, spin %d, layer %d, ratio %d%%, font %s%s' % (self.secname, u2in(self.x), u2in(self.y), u2in(self.size_2*2), angle, self.mirrored, self.spin, self.layer, self.ratio, font, extra)
def subsection_property(which, many = True, secname = None):
if not many and not secname:
return property(lambda self: self.subsections[which][0])
elif not many and secname:
return property(lambda self: [subsec for subsec in self.subsections[which] if subsec.secname == secname][0])
elif many and not secname:
return property(lambda self: self.subsections[which])
elif many and secname:
return property(lambda self: [subsec for subsec in self.subsections[which] if subsec.secname == secname])
assert False
class StartSection(Section):
sectype = 0x10
secname = 'Start'
subsec_names = ['settings', 'drawing']
def parse(self):
self.subsecs = self._get_uint16(2)
self.numsecs = self._get_uint32(4)
self.major = self._get_uint8(8)
self.minor = self._get_uint8(9)
self.version = (self.major, self.minor)
self._get_unknown(10, 16)
# XXX: hack
self.subsec_counts = [self.subsecs, self.numsecs - self.subsecs - 1]
settings = subsection_property(0, True, None)
grid = subsection_property(0, False, 'Grid')
layers = subsection_property(0, True, 'Layer')
drawing = subsection_property(1, False, None)
def __str__(self):
return '%s: version %s, subsecs %d, numsecs %d' % (self.secname, self.version, self.subsecs, self.numsecs)
class Unknown11Section(UnknownSection):
sectype = 0x11
# 4 bits of unit
# higher 2 bits tell which unit is used for display
# lower 2 bits tell which unit is used for saving
grid_units2 = 'mic mm mil in'.split()
grid_units4 = [save if save == disp else '%s as %s' % (save, disp) for disp in grid_units2 for save in grid_units2]
class GridSection(Section):
sectype = 0x12
secname = 'Grid'
def parse(self):
self.display = self._get_uint8_mask(2, 0x01)
self.style = self._get_uint8_mask(2, 0x02) >> 1
self._get_zero_mask(2, 0xfc)
self.unit = self._get_uint8_mask(3, 0x0f)
self.altunit = self._get_uint8_mask(3, 0xf0) >> 4
self.multiple = self._get_uint24(4)
self._get_zero(7, 1)
self.size = self._get_double(8)
self.altsize = self._get_double(16)
def __str__(self):
style = 'lines dots'.split()[self.style]
unit = grid_units4[self.unit]
altunit = grid_units4[self.altunit]
return '%s: display %s, style %s, size %s%s, multiple %d, alt %s%s' % (self.secname, self.display, style, self.size, unit, self.multiple, self.altsize, altunit)
layer_fills = dict(enumerate('stroke fill horiz thinslash thickslash thickback thinback square diamond dither coarse fine bottomleft bottomright topright topleft'.split()))
layer_colors = dict(enumerate('black darkblue darkgreen darkcyan darkred darkpurple darkyellow grey darkgrey blue green cyan red purple yellow lightgrey'.split()))
class LayerSection(Section):
sectype = 0x13
secname = 'Layer'
def parse(self):
self.side = self._get_uint8_mask(2, 0x10)
visible = self._get_uint8_mask(2, 0x0c) # whether objects on this layer are currently shown
self.available = bool(self._get_uint8_mask(2, 0x02)) # not available => not visible in layer display dialog at all
self._get_zero_mask(2, 0xe0)
self._get_unknown_mask(2, 0x01)
self.layer = self._get_uint8(3)
self.other = self._get_uint8(4) # the number of the matching layer on the other side
self.fill = self._get_uint8_mask(5, 0x0f)
self._get_zero_mask(5, 0xf0)
self.color = self._get_uint8_mask(6, 0x3f)
self._get_zero_mask(6, 0xc0)
self._get_unknown(7, 1)
self._get_zero(8, 7)
self.name = self._get_name(15, 9)
# This is usually true, but on schemas some layers not used on schemas may have 0x04 here
#assert visible in (0x00, 0x0c), 'I thought visibility always set two bits?'
self.visible = bool(visible)
# The ulp "visible" flag is basically "visible and not hidden", or "flags & 0x0e == 0x0e"
self.ulpvisible = self.visible and self.available
def __str__(self):
side = 'bottom' if self.side else 'top'
fill = layer_fills[self.fill]
color = layer_colors.get(self.color, '?grey?')
return '%s %s: layer %d, other %d, side %s, visible %d, fill %d %s, color %d %s' % (self.secname, self.name, self.layer, self.other, self.side, self.ulpvisible, self.fill, fill, self.color, color)
class SchemaSection(Section):
sectype = 0x14
secname = 'Schema'
subsec_names = ['attributes', 'libraries', 'sheets']
def parse(self):
self._get_unknown(2, 1)
self._get_zero(3, 1)
self.libsubsecs = self._get_uint32(4)
self.shtsubsecs = self._get_uint32(8)
if self.eaglefile.root.major >= 5:
self.atrsubsecs = self._get_uint32(12)
else:
self.atrsubsecs = 0
self._get_zero(16, 3)
self.xref_format = self._get_name(19, 5)
self.subsec_counts = [self.atrsubsecs, self.libsubsecs, self.shtsubsecs]
attributes = subsection_property(0, True, None)
libraries = subsection_property(1, True, None)
sheets = subsection_property(2, True, None)
def __str__(self):
return '%s: xref format %s, attrsubsecs %d, libsubsecs %d, sheetsubsecs %d' % (self.secname, self.xref_format, self.atrsubsecs, self.libsubsecs, self.shtsubsecs)
class LibrarySection(Section):
sectype = 0x15
secname = 'Library'
subsec_names = ['devices', 'symbols', 'packages']
def parse(self):
self._get_zero(2, 2)
self.devsubsecs = self._get_uint32(4)
self.symsubsecs = self._get_uint32(8)
self.pacsubsecs = self._get_uint32(12)
self.name = self._get_name(16, 8)
self.subsec_counts = [self.devsubsecs, self.symsubsecs, self.pacsubsecs]
devices = subsection_property(0, False, None)
symbols = subsection_property(1, False, None)
packages = subsection_property(2, False, None)
def __str__(self):
return '%s %s: devsubsecs %d, symsubsecs %d, pacsubsecs %d' % (self.secname, self.name, self.devsubsecs, self.symsubsecs, self.pacsubsecs)
class DevicesSection(Section):
sectype = 0x17
secname = 'Devices'
subsec_names = ['devices']
def parse(self):
self._get_zero(2, 2)
self.subsecs = self._get_uint32(4)
self.children = self._get_uint32(8)
self._get_zero(12, 4)
self.libname = self._get_name(16, 8)
self.subsec_counts = [self.subsecs]
devices = subsection_property(0, True, None)
def __str__(self):
return '%s: libname %s, subsecs %d, children %d' % (self.secname, self.libname, self.subsecs, self.children)
class SymbolsSection(Section):
sectype = 0x18
secname = 'Symbols'
subsec_names = ['symbols']
def parse(self):
self._get_zero(2, 2)
self.subsecs = self._get_uint32(4)
self.children = self._get_uint32(8)
self._get_zero(12, 4)
self.libname = self._get_name(16, 8)
self.subsec_counts = [self.subsecs]
symbols = subsection_property(0, True, None)
def __str__(self):
return '%s: libname %s, subsecs %d, children %d' % (self.secname, self.libname, self.subsecs, self.children)
class PackagesSection(Section):
sectype = 0x19
secname = 'Packages'
subsec_names = ['packages']
def parse(self):
self._get_zero(2, 2)
self.subsecs = self._get_uint32(4)
self.children = self._get_uint16(8)
self.libname = self._get_name(16, 8)
self.desc = self._get_name(10, 6)
self.subsec_counts = [self.subsecs]
packages = subsection_property(0, True, None)
def __str__(self):
return '%s: libname %s, desc %s, subsecs %d, children %d' % (self.secname, self.libname, self.desc, self.subsecs, self.children)
class SchemaSheetSection(Section):
sectype = 0x1a
secname = 'Schema/sheet'
subsec_names = ['drawables', 'parts', 'buses', 'nets']
def parse(self):
self.drawsubsecs = self._get_uint16(2)
self.minx = self._get_int16(4)
self.miny = self._get_int16(6)
self.maxx = self._get_int16(8)
self.maxy = self._get_int16(10)
self.partsubsecs = self._get_uint32(12)
self.bussubsecs = self._get_uint32(16)
self.netsubsecs = self._get_uint32(20)
self.subsec_counts = [self.drawsubsecs, self.partsubsecs, self.bussubsecs, self.netsubsecs]
drawables = subsection_property(0, True, None)
parts = subsection_property(1, True, None)
buses = subsection_property(2, True, None)
nets = subsection_property(3, True, None)
def __str__(self):
return '%s: limits (%dmil, %dmil), (%dmil, %dmil), drawsubsecs %d, symsubsecs %d, bussubsecs %d, netsubsecs %d' % (self.secname, self.minx, self.miny, self.maxx, self.maxy, self.drawsubsecs, self.partsubsecs, self.bussubsecs, self.netsubsecs)
class BoardSection(Section):
sectype = 0x1b
secname = 'Board'
subsec_names = ['definitions', 'drawables', 'packages', 'nets']
def parse(self):
self.drawsubsecs = self._get_uint16(2)
self.minx = self._get_int16(4)
self.miny = self._get_int16(6)
self.maxx = self._get_int16(8)
self.maxy = self._get_int16(10)
self.defsubsecs = self._get_uint32(12)
self.pacsubsecs = self._get_uint32(16)
self.netsubsecs = self._get_uint32(20)
self.subsec_counts = [self.defsubsecs, self.drawsubsecs, self.pacsubsecs, self.netsubsecs]
definitions = subsection_property(0, True, None)
drawables = subsection_property(1, True, None)
packages = subsection_property(2, True, None)
nets = subsection_property(3, True, None)
def __str__(self):
return '%s: limits (%dmil, %dmil), (%dmil, %dmil), defsubsecs %d, drawsubsecs %d, pacsubsecs %d, netsubsecs %d' % (self.secname, self.minx, self.miny, self.maxx, self.maxy, self.defsubsecs, self.drawsubsecs, self.pacsubsecs, self.netsubsecs)
class BoardNetSection(Section):
sectype = 0x1c
secname = 'Board/net'
subsec_names = ['drawables']
def parse(self):
self.subsecs = self._get_uint16(2)
self.minx = self._get_int16(4)
self.miny = self._get_int16(6)
self.maxx = self._get_int16(8)
self.maxy = self._get_int16(10)
self.airwires = not bool(self._get_uint8_mask(12, 0x02))
self._get_zero_mask(12, 0xfd)
self.netclass = self._get_uint8_mask(13, 0x07)
self._get_zero_mask(13, 0xf8)
self._get_zero(14, 2)
self.name = self._get_name(16, 8)
self.subsec_counts = [self.subsecs]
drawables = subsection_property(0, True, None)
def __str__(self):
return '%s %s: limits (%dmil, %dmil), (%dmil, %dmil), netclass %d, airwires %d, subsecs %d' % (self.secname, self.name, self.minx, self.miny, self.maxx, self.maxy, self.netclass, self.airwires, self.subsecs)
class SymbolSection(Section):
sectype = 0x1d
secname = 'Symbol'
subsec_names = ['drawables']
def parse(self):
self.subsecs = self._get_uint16(2)
self.minx = self._get_int16(4)
self.miny = self._get_int16(6)
self.maxx = self._get_int16(8)
self.maxy = self._get_int16(10)
self._get_zero(12, 4)
self.name = self._get_name(16, 8)
self.subsec_counts = [self.subsecs]
drawables = subsection_property(0, True, None)
def __str__(self):
return '%s %s: limits (%dmil, %dmil), (%dmil, %dmil), subsecs %d' % (self.secname, self.name, self.minx, self.miny, self.maxx, self.maxy, self.subsecs)
class PackageSection(Section):
sectype = 0x1e
secname = 'Package'
subsec_names = ['drawables']
def parse(self):
self.subsecs = self._get_uint16(2)
self.minx = self._get_int16(4)
self.miny = self._get_int16(6)
self.maxx = self._get_int16(8)
self.maxy = self._get_int16(10)
self._get_zero(12, 1)
self.name = self._get_name(18, 6)
self.desc = self._get_name(13, 5)
self.subsec_counts = [self.subsecs]
drawables = subsection_property(0, True, None)
def __str__(self):
return '%s %s: limits (%dmil, %dmil), (%dmil, %dmil), desc %s, subsecs %d' % (self.secname, self.name, self.minx, self.miny, self.maxx, self.maxy, self.desc, self.subsecs)
class SchemaNetSection(Section):
sectype = 0x1f
secname = 'Schema/net'
subsec_names = ['paths']
def parse(self):
self.subsecs = self._get_uint16(2)
# Seem to always be (32767mil, 32767mil), (-32768mil, -32768mil)
self.minx = self._get_int16(4)
self.miny = self._get_int16(6)
self.maxx = self._get_int16(8)
self.maxy = self._get_int16(10)
self._get_zero(12, 1)
self.netclass = self._get_uint8_mask(13, 0x07)
self._get_zero_mask(13, 0xf8)
self._get_zero(14, 2)
self.name = self._get_name(16, 8)
self.subsec_counts = [self.subsecs]
paths = subsection_property(0, True, None)
def __str__(self):
return '%s %s: limits (%dmil, %dmil), (%dmil, %dmil), netclass %d, subsecs %s' % (self.secname, self.name, self.minx, self.miny, self.maxx, self.maxy, self.netclass, self.subsecs)
class PathSection(Section):
sectype = 0x20
secname = 'Path'
subsec_names = ['drawables']
def parse(self):
self.subsecs = self._get_uint16(2)
self.minx = self._get_int16(4)
self.miny = self._get_int16(6)
self.maxx = self._get_int16(8)
self.maxy = self._get_int16(10)
self._get_zero(12, 12)
self.subsec_counts = [self.subsecs]
drawables = subsection_property(0, True, None)
def __str__(self):
return '%s: limits (%dmil, %dmil), (%dmil, %dmil), subsecs %d' % (self.secname, self.minx, self.miny, self.maxx, self.maxy, self.subsecs)
class PolygonSection(Section):
sectype = 0x21
secname = 'Polygon'
subsec_names = ['drawables']
def parse(self):
self.subsecs = self._get_uint16(2)
self.minx = self._get_int16(4)
self.miny = self._get_int16(6)
self.maxx = self._get_int16(8)
self.maxy = self._get_int16(10)
self.width_2 = self._get_uint16(12)
self.spacing_2 = self._get_uint16(14)
self.isolate_2 = self._get_uint16(16)
self.layer = self._get_uint8(18)
self.pour = 'hatch' if self._get_uint8_mask(19, 0x01) else 'solid'
self.rank = self._get_uint8_mask(19, 0x0e) >> 1
assert 0 <= self.rank <= 7, 'Unknown rank: %d' % (self.rank, ) # 7 for schema polygons, 0 seen in package
self.thermals = bool(self._get_uint8_mask(19, 0x80))
self.orphans = bool(self._get_uint8_mask(19, 0x40))
self._get_zero_mask(19, 0x30)
# These unknown bytes seem to somehow relate to whether the polygon is currently calculated or not (they are all zero when it's not calculated)
self._get_unknown(20, 4)
self.subsec_counts = [self.subsecs]
drawables = subsection_property(0, True, None)
def __str__(self):
return '%s: limits (%dmil, %dmil), (%dmil, %dmil), width %f", spacing %f", isolate %f", pour %s, rank %d, thermals %d, orphans %d, layer %d, subsecs %d' % (self.secname, self.minx, self.miny, self.maxx, self.maxy, u2in(self.width_2*2), u2in(self.spacing_2*2), u2in(self.isolate_2*2), self.pour, self.rank, self.thermals, self.orphans, self.layer, self.subsecs)
class LineSection(Section):
sectype = 0x22
secname = 'Line'
def parse(self):
self._get_unknown(2, 1)
self.layer = self._get_uint8(3)
self.width_2 = self._get_uint16(20)
self.linetype = self._get_uint8(23)
assert self.linetype in (0x00, 0x01, 0x81, 0x7e, 0x7f, 0x7b, 0x79, 0x78, 0x7a, 0x7d, 0x7c, 0x77), 'Unknown line type: ' + hex(self.linetype)
if self.linetype != 0x01:
self.stflags = self._get_uint8_mask(22, 0x33)
self._get_zero_mask(22, 0xc0)
self._get_unknown_mask(22, 0x0c)
# Cap style and positive curve are present on bare lines too, that's probably a bug
self.clockwise = bool(self.stflags & 0x20)
self.style = {0x00: 'continuous', 0x01: 'longdash', 0x02: 'shortdash', 0x03: 'dashdot'}[self.stflags & 0x03]
self.cap = {0x00: 'round', 0x10: 'flat'}[self.stflags & 0x10]
else:
self._get_unknown(22, 1)
if self.linetype == 0x81:
# 4 4-byte fields each contain 3 bytes of x1, y1, x2, y2 respectively.
# The 4th bytes of these fields combine to a 4-byte field whichs contains 3 bytes of c,
# which is the x or y coordinate of the arc center point, depending on the slope of the line.
# The 4th byte of c contains flags which tell which of the fields are negative (two's complement)
self._get_bytes(4, 15)
# Extend 3-byte coordinate fields to 4 bytes, taking the negative-flags into account
negflags = self._get_uint8_mask(19, 0x1f)
self._get_zero_mask(19, 0xe0)
ext = ['\xff' if negflags & (1 << i) else '\x00' for i in range(5)]
xydata = self.data[7:16:4] + ext[0] + self.data[4:7] + ext[1] + self.data[8:11] + ext[2] + self.data[12:15] + ext[3] + self.data[16:19] + ext[4]
c, x1, y1, x2, y2 = struct.unpack('<iiiii', xydata)
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
x3, y3 = (x1+x2)/2., (y1+y2)/2.
if abs(x2-x1) < abs(y2-y1):
self.cx = cx = c
self.cy = (x3-cx)*(x2-x1)/float(y2-y1)+y3
xst, yst = '', '?'
else:
self.cy = cy = c
self.cx = (y3-cy)*(y2-y1)/float(x2-x1)+x3
xst, yst = '?', ''
else:
self.x1 = self._get_int32(4)
self.y1 = self._get_int32(8)
self.x2 = self._get_int32(12)
self.y2 = self._get_int32(16)
if self.linetype == 0x78:
self.cx = min(self.x1, self.x2)
self.cy = min(self.y1, self.y2)
elif self.linetype == 0x79:
self.cx = max(self.x1, self.x2)
self.cy = min(self.y1, self.y2)
elif self.linetype == 0x7a:
self.cx = max(self.x1, self.x2)
self.cy = max(self.y1, self.y2)
elif self.linetype == 0x7b:
self.cx = min(self.x1, self.x2)
self.cy = max(self.y1, self.y2)
elif self.linetype in (0x7c, 0x7d, 0x7e, 0x7f):
self.cx = (self.x1 + self.x2) / 2.
self.cy = (self.y1 + self.y2) / 2.
def __str__(self):
coords = 'from (%f", %f") to (%f", %f")' % (u2in(self.x1), u2in(self.y1), u2in(self.x2), u2in(self.y2))
if self.linetype == 0x00:
return 'Line: %s, width %f", layer %d, style %s' % (coords, u2in(self.width_2*2), self.layer, self.style)
elif self.linetype == 0x01:
return 'Airwire: %s, width %f", layer %d' % (coords, u2in(self.width_2*2), self.layer)
elif self.linetype == 0x77:
return '??Line??: %s, width %f", layer %d, style %s' % (coords, u2in(self.width_2*2), self.layer, self.style)
else:
center = 'center at (%f", %f")' % (u2in(self.cx), u2in(self.cy))
arctype = {0x78: '90 downleft', 0x79: '90 downright', 0x7a: '90 upright', 0x7b: '90 upleft', 0x7c: '180 left', 0x7d: '180 right', 0x7e: '180 down', 0x7f: '180 up', 0x81: ''}[self.linetype]
arctype = (' ' if arctype else '') + arctype
return 'Arc%s: %s, %s, width %f", layer %d, style %s, cap %s' % (arctype, coords, center, u2in(self.width_2*2), self.layer, self.style, self.cap)
# Old arc section, used in eagle 4.x
class ArcSection(Section):
sectype = 0x24
secname = 'Arc'
def parse(self):
self._get_unknown(2, 1)
self.layer = self._get_uint8(3)
self.width_2 = self._get_uint16(20)
assert self._get_uint8_mask(22, 0x10), 'I thought this bit was supposed to be always set?'
self.clockwise = bool(self._get_uint8_mask(22, 0x20))
self._get_zero_mask(22, 0xcf)
self.arctype = self._get_uint8(23)
if self.arctype == 0x00:
self._get_bytes(4, 15)
negflags = self._get_uint8_mask(19, 0x1f)
self._get_zero_mask(19, 0xe0)
ext = ['\xff' if negflags & (1 << i) else '\x00' for i in range(5)]
xydata = self.data[7:16:4] + ext[0] + self.data[4:7] + ext[1] + self.data[8:11] + ext[2] + self.data[12:15] + ext[3] + self.data[16:19] + ext[4]
c, x1, y1, x2, y2 = struct.unpack('<iiiii', xydata)
self.x1 = x1
self.y1 = y1
self.x2 = x2
self.y2 = y2
x3, y3 = (x1+x2)/2., (y1+y2)/2.
if abs(x2-x1) < abs(y2-y1):
self.cx = cx = c
self.cy = (x3-cx)*(x2-x1)/float(y2-y1)+y3
else:
self.cy = cy = c
self.cx = (y3-cy)*(y2-y1)/float(x2-x1)+x3
else:
self.x1 = self._get_int32(4)
self.y1 = self._get_int32(8)
self.x2 = self._get_int32(12)
self.y2 = self._get_int32(16)
if self.arctype == 0x01:
self.cx = min(self.x1, self.x2)
self.cy = min(self.y1, self.y2)
elif self.arctype == 0x02:
self.cx = max(self.x1, self.x2)
self.cy = min(self.y1, self.y2)
elif self.arctype == 0x03:
self.cx = max(self.x1, self.x2)
self.cy = max(self.y1, self.y2)
elif self.arctype == 0x04:
self.cx = min(self.x1, self.x2)
self.cy = max(self.y1, self.y2)
elif self.arctype in (0x05, 0x06, 0x07, 0x08):
self.cx = (self.x1 + self.x2) / 2.
self.cy = (self.y1 + self.y2) / 2.
def __str__(self):
arctype = {0x00: '', 0x01: '90 downleft', 0x02: '90 downright', 0x03: '90 upright', 0x04: '90 upleft', 0x05: '180 left', 0x06: '180 right', 0x07: '180 down', 0x08: '180 up'}[self.arctype]
arctype = (' ' if arctype else '') + arctype
return '%s%s: from (%f", %f") to (%f", %f"), center at (%f", %f"), layer %d, width %f"' % (self.secname, arctype, u2in(self.x1), u2in(self.y1), u2in(self.x2), u2in(self.y2), u2in(self.cx), u2in(self.cy), self.layer, u2in(self.width_2*2))
class CircleSection(Section):
sectype = 0x25
secname = 'Circle'
def parse(self):
self._get_unknown(2, 1)
self.layer = self._get_uint8(3)
self.x1 = self._get_int32(4)
self.y1 = self._get_int32(8)
self.r = self._get_int32(12)
self._get_unknown(16, 4) # Almost always the same as bytes 12-15
self.width_2 = self._get_uint32(20)
#assert r == _get_int32(16) # Almost always the same...
def __str__(self):
return '%s: at (%f", %f"), radius %f", width %f", layer %d' % (self.secname, u2in(self.x1), u2in(self.y1), u2in(self.r), u2in(self.width_2*2), self.layer)
class RectangleSection(Section):
sectype = 0x26
secname = 'Rectangle'
def parse(self):
self._get_unknown(2, 1)
self.layer = self._get_uint8(3)
self.x1 = self._get_int32(4)
self.y1 = self._get_int32(8)
self.x2 = self._get_int32(12)
self.y2 = self._get_int32(16)
self.angle = self._get_uint16_mask(20, 0x0fff)
self._get_zero16_mask(20, 0xf000)
self._get_zero(22, 2)
def __str__(self):
return '%s: from (%f", %f") to (%f", %f"), angle %f, layer %d' % (self.secname, u2in(self.x1), u2in(self.y1), u2in(self.x2), u2in(self.y2), 360 * self.angle / 4096., self.layer)
class JunctionSection(Section):
sectype = 0x27
secname = 'Junction'
def parse(self):
self._get_zero(2, 1)
# Junctions are always on the "Nets" layer, and you can't change this
# inside of eagle, but if you give Eagle a file that has this set to some
# other layer, Eagle uses that layers color for rendering the junction
self.layer = self._get_uint8(3)
assert self.layer == 0x5b
self.x = self._get_int32(4)
self.y = self._get_int32(8)
# You can't change this inside of Eagle, and Eagle does not react to you
# changing this manually, but this always gets 0x13d8 (0.2") written
# to it, which is exactly the same as the size junctions are rendered as
self.width_2 = self._get_uint16(12)
assert self.width_2 == 0x13d8
self._get_zero(14, 10)
def __str__(self):
return '%s: at (%f", %f"), width %f", layer %d' % (self.secname, u2in(self.x), u2in(self.y), u2in(self.width_2*2), self.layer)
class HoleSection(Section):
sectype = 0x28
secname = 'Hole'
def parse(self):
self._get_zero(2, 2)
self.x = self._get_int32(4)
self.y = self._get_int32(8)
self.width_2 = self._get_uint32(12)
self._get_zero(16, 2) # Unknown?
self._get_zero(18, 6)
def __str__(self):
return '%s: at (%f", %f") drill %f"' % (self.secname, u2in(self.x), u2in(self.y), u2in(self.width_2*2))
class ViaSection(Section):
sectype = 0x29
secname = 'Via'
def parse(self):
self.shape = self._get_uint8_mask(2, 0x03)
self._get_zero_mask(2, 0xfc)
self._get_unknown(3, 1)
self.x = self._get_int32(4)
self.y = self._get_int32(8)
self.drill_2 = self._get_uint16(12)
self.diameter_2 = self._get_uint16(14)
self.layers = self._get_uint8_mask(16, 0x0f) + 1, (self._get_uint8_mask(16, 0xf0) >> 4) + 1
self.stop = self._get_uint8_mask(17, 0x01)
self._get_zero_mask(17, 0xfe)
self._get_zero(18, 6)
def __str__(self):
shape = 'square round octagon'.split()[self.shape]
return '%s: at (%f", %f"), diameter %f", drill %f", shape %s, layers %d-%d, stop %d' % (self.secname, u2in(self.x), u2in(self.y), u2in(self.diameter_2*2), u2in(self.drill_2*2), shape, self.layers[0], self.layers[1], self.stop)
class PadSection(Section):
sectype = 0x2a
secname = 'Pad'
def parse(self):
self.shape = self._get_uint8_mask(2, 0x07)
self._get_zero_mask(2, 0xf8)
self._get_zero(3, 1)
self.x = self._get_int32(4)
self.y = self._get_int32(8)
self.drill_2 = self._get_uint16(12)
self.diameter_2 = self._get_uint16(14)
self.angle = self._get_uint16_mask(16, 0x0fff)
self._get_zero16_mask(16, 0xf000)
self.stop = not bool(self._get_uint8_mask(18, 0x01))
self.thermals = not bool(self._get_uint8_mask(18, 0x04))
self.first = bool(self._get_uint8_mask(18, 0x08))
self._get_zero_mask(18, 0xf2)
self.name = self._get_name(19, 5)
def __str__(self):
shape = 'square round octagon long offset'.split()[self.shape]
return '%s: at (%f", %f"), diameter %f", drill %f", angle %f, shape %s, first %d, stop %d, thermals %d, name %s' % (self.secname, u2in(self.x), u2in(self.y), u2in(self.diameter_2*2), u2in(self.drill_2*2), 360 * self.angle / 4096., shape, self.first, self.stop, self.thermals, self.name)
class SmdSection(Section):
sectype = 0x2b
secname = 'SMD pad'
def parse(self):
self.roundness = self._get_uint8(2)
self.layer = self._get_uint8(3)
self.x = self._get_int32(4)
self.y = self._get_int32(8)
self.width_2 = self._get_uint16(12)
self.height_2 = self._get_uint16(14)
self.angle = self._get_uint16_mask(16, 0x0fff)
self._get_zero16_mask(16, 0xf000)
self.stop = not bool(self._get_uint8_mask(18, 0x01))
self.cream = not bool(self._get_uint8_mask(18, 0x02))
self.thermals = not bool(self._get_uint8_mask(18, 0x04))
self._get_zero_mask(18, 0xf8)
self.name = self._get_name(19, 5)
def __str__(self):
return '%s: at (%f", %f"), size %f" x %f", angle %f, layer %d, roundness %d%%, stop %d, cream %d, thermals %d, name %s' % (self.secname, u2in(self.x), u2in(self.y), u2in(self.width_2*2), u2in(self.height_2*2), 360 * self.angle / 4096., self.layer, self.roundness, self.stop, self.cream, self.thermals, self.name)
class PinSection(Section):
sectype = 0x2c
secname = 'Pin'
def parse(self):
self.function = self._get_uint8_mask(2, 0x03)
self.visible = self._get_uint8_mask(2, 0xc0) >> 6
self._get_zero_mask(2, 0x3c)
self._get_zero(3, 1)
self.x = self._get_int32(4)
self.y = self._get_int32(8)
self.direction = self._get_uint8_mask(12, 0x0f)
self.length = self._get_uint8_mask(12, 0x30) >> 4
self.angle = self._get_uint8_mask(12, 0xc0) << 4
self.swaplevel = self._get_uint8(13)
self.name = self._get_name(14, 10)
def __str__(self):
func = 'None Dot Clk DotClk'.split()[self.function]
vis = 'Off Pad Pin Both'.split()[self.visible]
dir_ = 'Nc In Out I/O OC Pwr Pas Hiz Sup'.split()[self.direction]
len_ = 'Point Short Middle Long'.split()[self.length]
angle = 360 * self.angle / 4096.
return '%s: at (%f", %f"), name %s, angle %s, direction %s, swaplevel %s, length %s, function %s, visible %s' % (self.secname, u2in(self.x), u2in(self.y), self.name, angle, dir_, self.swaplevel, len_, func, vis)
class GateSection(Section):
sectype = 0x2d
secname = 'Gate'
def parse(self):
self._get_zero(2, 2)
self.x = self._get_int32(4)
self.y = self._get_int32(8)
self.addlevel = self._get_uint8(12)
self.swap = self._get_uint8(13)
self.symno = self._get_uint16(14)
self.name = self._get_name(16, 8)
def __str__(self):
addlevel = 'Must Can Next Request Always'.split()[self.addlevel]
return '%s %s: at (%f", %f"), symbol %d, swap %d, addlevel %s' % (self.secname, self.name, u2in(self.x), u2in(self.y), self.symno, self.swap, addlevel)
class BoardPackageSection(Section):
sectype = 0x2e
secname = 'Board/package'
subsec_names = ['attributes']
def parse(self):
self.subsecs = self._get_uint16(2)
self.x = self._get_int32(4)
self.y = self._get_int32(8)
self.libno = self._get_uint16(12)
self.pacno = self._get_uint16(14)
self.angle = self._get_uint16_mask(16, 0x0fff)
self.mirrored = bool(self._get_uint16_mask(16, 0x1000))
self.spin = bool(self._get_uint16_mask(16, 0x4000))
self._get_zero16_mask(16, 0xa000)
self._get_unknown(18, 1)
self._get_zero(19, 1)
self._get_unknown(20, 4)
self.subsec_counts = [self.subsecs]
attributes = subsection_property(0, True, None)
def __str__(self):
return '%s %d@%d: at (%f", %f"), angle %f, mirror %d, spin %d, subsecs %d' % (self.secname, self.pacno, self.libno, u2in(self.x), u2in(self.y), 360 * self.angle / 4096., self.mirrored, self.spin, self.subsecs)
class BoardPackage2Section(Section):
sectype = 0x2f
secname = 'Board/package'
def parse(self):
self.value = self._get_name(10, 14)
self.name = self._get_name(2, 8)
def __str__(self):
return '%s: name %s, value %s' % (self.secname, self.name, self.value)
class InstanceSection(Section):
sectype = 0x30
secname = 'Instance'
subsec_names = ['attributes']
def parse(self):
self.subsecs = self._get_uint16(2)
self.x = self._get_int32(4)
self.y = self._get_int32(8)
placed = self._get_int16(12)
if self.eaglefile.root.major >= 5:
assert placed in (0, -1), 'What\'s this?'
self.placed = placed == -1
else:
self.placed = True
self.gateno = self._get_uint16(14)
self.angle = self._get_uint16_mask(16, 0x0c00)
self.mirrored = bool(self._get_uint16_mask(16, 0x1000))
self._get_zero16_mask(16, 0xe3ff)
self.smashed = bool(self._get_uint8_mask(18, 0x01))
self._get_zero_mask(18, 0xfe)
self._get_zero(19, 1)
self._get_unknown(20, 4)
self.subsec_counts = [self.subsecs]
attributes = subsection_property(0, True, None)
def __str__(self):
return '%s: at (%f", %f"), gate %d, angle %f, mirror %d, smashed %d, placed %d' % (self.secname, u2in(self.x), u2in(self.y), self.gateno, 360 * self.angle / 4096., self.mirrored, self.smashed, self.placed)
class TextSection(TextBaseSection):
sectype = 0x31
secname = 'Text'
class NetBusLabelSection(TextBaseSection):
sectype = 0x33
secname = 'Net/bus label'
class SmashedNameSection(TextBaseSection):
sectype = 0x34
secname = 'Smashed name'
class SmashedValueSection(TextBaseSection):
sectype = 0x35
secname = 'Smashed value'
class PackageVariantSection(Section):
sectype = 0x36
secname = 'Package variant'
subsec_names = ['connections']
def parse(self):
self.subsecs = self._get_uint16(2)
self.pacno = self._get_uint16(4)
self.name = self._get_name(19, 5)
self.table = self._get_name(6, 13)
self.subsec_counts = [self.subsecs]
connections = subsection_property(0, True, None)
def __str__(self):
return '%s %s: package %d, table %r, subsecs %d' % (self.secname, self.name, self.pacno, self.table, self.subsecs)
class DeviceSection(Section):
sectype = 0x37
secname = 'Device'
subsec_names = ['variants', 'gates']
def parse(self):
self.gatsubsecs = self._get_uint16(2)
self.varsubsecs = self._get_uint16(4)
self.value_on = bool(self._get_uint8_mask(6, 0x01))
self._get_unknown_mask(6, 0x02)
self._get_zero_mask(6, 0xfc)
self.con_byte = self._get_uint8_mask(7, 0x80) >> 7
self.pin_bits = self._get_uint8_mask(7, 0x0f)
self._get_zero_mask(7, 0x70)
self.name = self._get_name(18, 6)
self.desc = self._get_name(13, 5)
self.prefix = self._get_name(8, 5)
self.subsec_counts = [self.varsubsecs, self.gatsubsecs]
variants = subsection_property(0, True, None)
gates = subsection_property(1, True, None)
def __str__(self):
return '%s %s: prefix %s, desc %s, con_byte %d, pin_bits %d, value_on %d, varsubsecs %d, gatsubsecs %d' % (self.secname, self.name, self.prefix, self.desc, self.con_byte, self.pin_bits, self.value_on, self.varsubsecs, self.gatsubsecs)
class PartSection(Section):
sectype = 0x38
secname = 'Part'
subsec_names = ['instances']
def parse(self):
self.subsecs = self._get_uint16(2)
self.libno = self._get_uint16(4)
self.devno = self._get_uint16(6)
self.varno = self._get_uint8(8)
self.tecno = self._get_uint16(9)
self.value = self._get_name(16, 8)
self.name = self._get_name(11, 5)
self.subsec_counts = [self.subsecs]
instances = subsection_property(0, True, None)
def __str__(self):
return '%s %s: library %d, device %d, variant %d, technology %d, value %s, subsecs %d' % (self.secname, self.name, self.libno, self.devno, self.varno, self.tecno, self.value, self.subsecs)