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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2012-2016 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
Pcbnew PLUGIN for Eagle 6.x XML *.brd and footprint format.
XML parsing and converting:
Getting line numbers and byte offsets from the source XML file is not
possible using currently available XML libraries within KiCad project:
wxXmlDocument and boost::property_tree.
property_tree will give line numbers but no byte offsets, and only during
document loading. This means that if we have a problem after the document is
successfully loaded, there is no way to correlate back to line number and byte
offset of the problem. So a different approach is taken, one which relies on the
XML elements themselves using an XPATH type of reporting mechanism. The path to
the problem is reported in the error messages. This means keeping track of that
path as we traverse the XML document for the sole purpose of accurate error
reporting.
User can load the source XML file into firefox or other xml browser and follow
our error message.
Load() TODO's
*) verify zone fill clearances are correct
*/
#include <errno.h>
#include <wx/string.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <eagle_plugin.h>
#include <common.h>
#include <macros.h>
#include <fctsys.h>
#include <trigo.h>
#include <macros.h>
#include <kicad_string.h>
#include <wx/filename.h>
#include <class_board.h>
#include <class_module.h>
#include <class_track.h>
#include <class_edge_mod.h>
#include <class_zone.h>
#include <class_pcb_text.h>
using namespace boost::property_tree;
using namespace std;
typedef EAGLE_PLUGIN::BIU BIU;
typedef PTREE::const_assoc_iterator CA_ITER;
typedef PTREE::const_iterator CITER;
typedef std::pair<CA_ITER, CA_ITER> CA_ITER_RANGE;
typedef MODULE_MAP::iterator MODULE_ITER;
typedef MODULE_MAP::const_iterator MODULE_CITER;
typedef boost::optional<string> opt_string;
typedef boost::optional<int> opt_int;
typedef boost::optional<double> opt_double;
typedef boost::optional<bool> opt_bool;
/// segment (element) of our XPATH into the Eagle XML document tree in PTREE form.
struct TRIPLET
{
const char* element;
const char* attribute;
const char* value;
TRIPLET( const char* aElement, const char* aAttribute = "", const char* aValue = "" ) :
element( aElement ),
attribute( aAttribute ),
value( aValue )
{}
};
/**
* Class XPATH
* keeps track of what we are working on within a PTREE.
* Then if an exception is thrown, the place within the tree that gave us
* grief can be reported almost accurately. To minimally impact
* speed, merely assign const char* pointers during the tree walking
* expedition. The const char* pointers must be to C strings residing either in
* the data or code segment (i.e. "compiled in") or within the XML document, but
* not on the stack, since the stack is unwound during the throwing of the
* exception. The XML document will not immediately vanish since we capture
* the xpath (using function Contents()) before the XML document tree (PTREE)
* is destroyed.
*/
class XPATH
{
std::vector<TRIPLET> p;
public:
void push( const char* aPathSegment, const char* aAttribute="" )
{
p.push_back( TRIPLET( aPathSegment, aAttribute ) );
}
void clear() { p.clear(); }
void pop() { p.pop_back(); }
/// modify the last path node's value
void Value( const char* aValue )
{
p.back().value = aValue;
}
/// modify the last path node's attribute
void Attribute( const char* aAttribute )
{
p.back().attribute = aAttribute;
}
/// return the contents of the XPATH as a single string
string Contents()
{
typedef std::vector<TRIPLET>::const_iterator CITER;
string ret;
for( CITER it = p.begin(); it != p.end(); ++it )
{
if( it != p.begin() )
ret += '.';
ret += it->element;
if( it->attribute[0] && it->value[0] )
{
ret += '[';
ret += it->attribute;
ret += '=';
ret += it->value;
ret += ']';
}
}
return ret;
}
};
/**
* Function parseOptionalBool
* returns an opt_bool and sets it true or false according to the presence
* and value of an attribute within the CPTREE element.
*/
static opt_bool parseOptionalBool( CPTREE& attribs, const char* aName )
{
opt_bool ret;
opt_string stemp = attribs.get_optional<string>( aName );
if( stemp )
ret = !stemp->compare( "yes" );
return ret;
}
// All of the 'E'STRUCTS below merely hold Eagle XML information verbatim, in binary.
// For maintenance and troubleshooting purposes, it was thought that we'd need to
// separate the conversion process into distinct steps. There is no intent to have KiCad
// forms of information in these 'E'STRUCTS. They are only binary forms
// of the Eagle information in the corresponding Eagle XML nodes.
/// Eagle rotation
struct EROT
{
bool mirror;
bool spin;
double degrees;
EROT() :
mirror( false ),
spin( false ),
degrees( 0 )
{}
EROT( double aDegrees ) :
mirror( false ),
spin( false ),
degrees( aDegrees )
{}
};
typedef boost::optional<EROT> opt_erot;
/// parse an Eagle XML "rot" field. Unfortunately the DTD seems not to explain
/// this format very well. [S][M]R<degrees>. Examples: "R90", "MR180", "SR180"
static EROT erot( const string& aRot )
{
EROT rot;
rot.spin = aRot.find( 'S' ) != aRot.npos;
rot.mirror = aRot.find( 'M' ) != aRot.npos;
rot.degrees = strtod( aRot.c_str()
+ 1 // skip leading 'R'
+ int( rot.spin ) // skip optional leading 'S'
+ int( rot.mirror ), // skip optional leading 'M'
NULL );
return rot;
}
/// Eagle "rot" fields are optional, handle that by returning opt_erot.
static opt_erot parseOptionalEROT( CPTREE& attribs )
{
opt_erot ret;
opt_string stemp = attribs.get_optional<string>( "rot" );
if( stemp )
ret = erot( *stemp );
return ret;
}
/// Eagle wire
struct EWIRE
{
double x1;
double y1;
double x2;
double y2;
double width;
LAYER_NUM layer;
// for style: (continuous | longdash | shortdash | dashdot)
enum {
CONTINUOUS,
LONGDASH,
SHORTDASH,
DASHDOT,
};
opt_int style;
opt_double curve; ///< range is -359.9..359.9
// for cap: (flat | round)
enum {
FLAT,
ROUND,
};
opt_int cap;
EWIRE( CPTREE& aWire );
};
/**
* Constructor EWIRE
* converts a "wire"'s xml attributes ( <wire> )
* to binary without additional conversion.
* This result is an EWIRE with the <wire> textual data merely converted to binary.
*/
EWIRE::EWIRE( CPTREE& aWire )
{
CPTREE& attribs = aWire.get_child( "<xmlattr>" );
/*
<!ELEMENT wire EMPTY>
<!ATTLIST wire
x1 %Coord; #REQUIRED
y1 %Coord; #REQUIRED
x2 %Coord; #REQUIRED
y2 %Coord; #REQUIRED
width %Dimension; #REQUIRED
layer %Layer; #REQUIRED
extent %Extent; #IMPLIED -- only applicable for airwires --
style %WireStyle; "continuous"
curve %WireCurve; "0"
cap %WireCap; "round" -- only applicable if 'curve' is not zero --
>
*/
x1 = attribs.get<double>( "x1" );
y1 = attribs.get<double>( "y1" );
x2 = attribs.get<double>( "x2" );
y2 = attribs.get<double>( "y2" );
width = attribs.get<double>( "width" );
layer = attribs.get<int>( "layer" );
curve = attribs.get_optional<double>( "curve" );
opt_string s = attribs.get_optional<string>( "style" );
if( s )
{
if( !s->compare( "continuous" ) )
style = EWIRE::CONTINUOUS;
else if( !s->compare( "longdash" ) )
style = EWIRE::LONGDASH;
else if( !s->compare( "shortdash" ) )
style = EWIRE::SHORTDASH;
else if( !s->compare( "dashdot" ) )
style = EWIRE::DASHDOT;
}
s = attribs.get_optional<string>( "cap" );
if( s )
{
if( !s->compare( "round" ) )
cap = EWIRE::ROUND;
else if( !s->compare( "flat" ) )
cap = EWIRE::FLAT;
}
// ignoring extent
}
/// Eagle via
struct EVIA
{
double x;
double y;
int layer_front_most; /// < extent
int layer_back_most; /// < inclusive
double drill;
opt_double diam;
opt_string shape;
EVIA( CPTREE& aVia );
};
EVIA::EVIA( CPTREE& aVia )
{
CPTREE& attribs = aVia.get_child( "<xmlattr>" );
/*
<!ELEMENT via EMPTY>
<!ATTLIST via
x %Coord; #REQUIRED
y %Coord; #REQUIRED
extent %Extent; #REQUIRED
drill %Dimension; #REQUIRED
diameter %Dimension; "0"
shape %ViaShape; "round"
alwaysstop %Bool; "no"
>
*/
x = attribs.get<double>( "x" );
y = attribs.get<double>( "y" );
string ext = attribs.get<string>( "extent" );
sscanf( ext.c_str(), "%d-%d", &layer_front_most, &layer_back_most );
drill = attribs.get<double>( "drill" );
diam = attribs.get_optional<double>( "diameter" );
shape = attribs.get_optional<string>( "shape" );
}
/// Eagle circle
struct ECIRCLE
{
double x;
double y;
double radius;
double width;
LAYER_NUM layer;
ECIRCLE( CPTREE& aCircle );
};
ECIRCLE::ECIRCLE( CPTREE& aCircle )
{
CPTREE& attribs = aCircle.get_child( "<xmlattr>" );
/*
<!ELEMENT circle EMPTY>
<!ATTLIST circle
x %Coord; #REQUIRED
y %Coord; #REQUIRED
radius %Coord; #REQUIRED
width %Dimension; #REQUIRED
layer %Layer; #REQUIRED
>
*/
x = attribs.get<double>( "x" );
y = attribs.get<double>( "y" );
radius = attribs.get<double>( "radius" );
width = attribs.get<double>( "width" );
layer = attribs.get<int>( "layer" );
}
/// Eagle XML rectangle in binary
struct ERECT
{
double x1;
double y1;
double x2;
double y2;
int layer;
opt_erot rot;
ERECT( CPTREE& aRect );
};
ERECT::ERECT( CPTREE& aRect )
{
CPTREE& attribs = aRect.get_child( "<xmlattr>" );
/*
<!ELEMENT rectangle EMPTY>
<!ATTLIST rectangle
x1 %Coord; #REQUIRED
y1 %Coord; #REQUIRED
x2 %Coord; #REQUIRED
y2 %Coord; #REQUIRED
layer %Layer; #REQUIRED
rot %Rotation; "R0"
>
*/
x1 = attribs.get<double>( "x1" );
y1 = attribs.get<double>( "y1" );
x2 = attribs.get<double>( "x2" );
y2 = attribs.get<double>( "y2" );
layer = attribs.get<int>( "layer" );
rot = parseOptionalEROT( attribs );
}
/// Eagle "attribute" XML element, no foolin'.
struct EATTR
{
string name;
opt_string value;
opt_double x;
opt_double y;
opt_double size;
opt_int layer;
opt_double ratio;
opt_erot rot;
enum { // for 'display'
Off,
VALUE,
NAME,
BOTH,
};
opt_int display;
EATTR( CPTREE& aTree );
EATTR() {}
};
/**
* Constructor EATTR
* parses an Eagle "attribute" XML element. Note that an attribute element
* is different than an XML element attribute. The attribute element is a
* full XML node in and of itself, and has attributes of its own. Blame Eagle.
*/
EATTR::EATTR( CPTREE& aAttribute )
{
CPTREE& attribs = aAttribute.get_child( "<xmlattr>" );
/*
<!ELEMENT attribute EMPTY>
<!ATTLIST attribute
name %String; #REQUIRED
value %String; #IMPLIED
x %Coord; #IMPLIED
y %Coord; #IMPLIED
size %Dimension; #IMPLIED
layer %Layer; #IMPLIED
font %TextFont; #IMPLIED
ratio %Int; #IMPLIED
rot %Rotation; "R0"
display %AttributeDisplay; "value" -- only in <element> or <instance> context --
constant %Bool; "no" -- only in <device> context --
>
*/
name = attribs.get<string>( "name" ); // #REQUIRED
value = attribs.get_optional<string>( "value" );
x = attribs.get_optional<double>( "x" );
y = attribs.get_optional<double>( "y" );
size = attribs.get_optional<double>( "size" );
// KiCad cannot currently put a TEXTE_MODULE on a different layer than the MODULE
// Eagle can it seems.
layer = attribs.get_optional<int>( "layer" );
ratio = attribs.get_optional<double>( "ratio" );
rot = parseOptionalEROT( attribs );
opt_string stemp = attribs.get_optional<string>( "display" );
if( stemp )
{
// (off | value | name | both)
if( !stemp->compare( "off" ) )
display = EATTR::Off;
else if( !stemp->compare( "value" ) )
display = EATTR::VALUE;
else if( !stemp->compare( "name" ) )
display = EATTR::NAME;
else if( !stemp->compare( "both" ) )
display = EATTR::BOTH;
}
}
/// Eagle text element
struct ETEXT
{
string text;
double x;
double y;
double size;
int layer;
opt_string font;
opt_double ratio;
opt_erot rot;
enum { // for align
CENTER,
CENTER_LEFT,
TOP_CENTER,
TOP_LEFT,
TOP_RIGHT,
// opposites are -1 x above, used by code tricks in here
CENTER_RIGHT = -CENTER_LEFT,
BOTTOM_CENTER = -TOP_CENTER,
BOTTOM_LEFT = -TOP_RIGHT,
BOTTOM_RIGHT = -TOP_LEFT,
};
opt_int align;
ETEXT( CPTREE& aText );
};
ETEXT::ETEXT( CPTREE& aText )
{
CPTREE& attribs = aText.get_child( "<xmlattr>" );
/*
<!ELEMENT text (#PCDATA)>
<!ATTLIST text
x %Coord; #REQUIRED
y %Coord; #REQUIRED
size %Dimension; #REQUIRED
layer %Layer; #REQUIRED
font %TextFont; "proportional"
ratio %Int; "8"
rot %Rotation; "R0"
align %Align; "bottom-left"
>
*/
text = aText.data();
x = attribs.get<double>( "x" );
y = attribs.get<double>( "y" );
size = attribs.get<double>( "size" );
layer = attribs.get<int>( "layer" );
font = attribs.get_optional<string>( "font" );
ratio = attribs.get_optional<double>( "ratio" );
rot = parseOptionalEROT( attribs );
opt_string stemp = attribs.get_optional<string>( "align" );
if( stemp )
{
// (bottom-left | bottom-center | bottom-right | center-left |
// center | center-right | top-left | top-center | top-right)
if( !stemp->compare( "center" ) )
align = ETEXT::CENTER;
else if( !stemp->compare( "center-right" ) )
align = ETEXT::CENTER_RIGHT;
else if( !stemp->compare( "top-left" ) )
align = ETEXT::TOP_LEFT;
else if( !stemp->compare( "top-center" ) )
align = ETEXT::TOP_CENTER;
else if( !stemp->compare( "top-right" ) )
align = ETEXT::TOP_RIGHT;
else if( !stemp->compare( "bottom-left" ) )
align = ETEXT::BOTTOM_LEFT;
else if( !stemp->compare( "bottom-center" ) )
align = ETEXT::BOTTOM_CENTER;
else if( !stemp->compare( "bottom-right" ) )
align = ETEXT::BOTTOM_RIGHT;
else if( !stemp->compare( "center-left" ) )
align = ETEXT::CENTER_LEFT;
}
}
/// Eagle thru hol pad
struct EPAD
{
string name;
double x;
double y;
double drill;
opt_double diameter;
// for shape: (square | round | octagon | long | offset)
enum {
SQUARE,
ROUND,
OCTAGON,
LONG,
OFFSET,
};
opt_int shape;
opt_erot rot;
opt_bool stop;
opt_bool thermals;
opt_bool first;
EPAD( CPTREE& aPad );
};
EPAD::EPAD( CPTREE& aPad )
{
CPTREE& attribs = aPad.get_child( "<xmlattr>" );
/*
<!ELEMENT pad EMPTY>
<!ATTLIST pad
name %String; #REQUIRED
x %Coord; #REQUIRED
y %Coord; #REQUIRED
drill %Dimension; #REQUIRED
diameter %Dimension; "0"
shape %PadShape; "round"
rot %Rotation; "R0"
stop %Bool; "yes"
thermals %Bool; "yes"
first %Bool; "no"
>
*/
// #REQUIRED says DTD, throw exception if not found
name = attribs.get<string>( "name" );
x = attribs.get<double>( "x" );
y = attribs.get<double>( "y" );
drill = attribs.get<double>( "drill" );
diameter = attribs.get_optional<double>( "diameter" );
opt_string s = attribs.get_optional<string>( "shape" );
if( s )
{
// (square | round | octagon | long | offset)
if( !s->compare( "square" ) )
shape = EPAD::SQUARE;
else if( !s->compare( "round" ) )
shape = EPAD::ROUND;
else if( !s->compare( "octagon" ) )
shape = EPAD::OCTAGON;
else if( !s->compare( "long" ) )
shape = EPAD::LONG;
else if( !s->compare( "offset" ) )
shape = EPAD::OFFSET;
}
rot = parseOptionalEROT( attribs );
stop = parseOptionalBool( attribs, "stop" );
thermals = parseOptionalBool( attribs, "thermals" );
first = parseOptionalBool( attribs, "first" );
}
/// Eagle SMD pad
struct ESMD
{
string name;
double x;
double y;
double dx;
double dy;
int layer;
opt_int roundness;
opt_erot rot;
opt_bool stop;
opt_bool thermals;
opt_bool cream;
ESMD( CPTREE& aSMD );
};
ESMD::ESMD( CPTREE& aSMD )
{
CPTREE& attribs = aSMD.get_child( "<xmlattr>" );
/*
<!ATTLIST smd
name %String; #REQUIRED
x %Coord; #REQUIRED
y %Coord; #REQUIRED
dx %Dimension; #REQUIRED
dy %Dimension; #REQUIRED
layer %Layer; #REQUIRED
roundness %Int; "0"
rot %Rotation; "R0"
stop %Bool; "yes"
thermals %Bool; "yes"
cream %Bool; "yes"
>
*/
// DTD #REQUIRED, throw exception if not found
name = attribs.get<string>( "name" );
x = attribs.get<double>( "x" );
y = attribs.get<double>( "y" );
dx = attribs.get<double>( "dx" );
dy = attribs.get<double>( "dy" );
layer = attribs.get<int>( "layer" );
rot = parseOptionalEROT( attribs );
roundness = attribs.get_optional<int>( "roundness" );
thermals = parseOptionalBool( attribs, "thermals" );
stop = parseOptionalBool( attribs, "stop" );
thermals = parseOptionalBool( attribs, "thermals" );
cream = parseOptionalBool( attribs, "cream" );
}
struct EVERTEX
{
double x;
double y;
EVERTEX( CPTREE& aVertex );
};
EVERTEX::EVERTEX( CPTREE& aVertex )
{
CPTREE& attribs = aVertex.get_child( "<xmlattr>" );
/*
<!ELEMENT vertex EMPTY>
<!ATTLIST vertex
x %Coord; #REQUIRED
y %Coord; #REQUIRED
curve %WireCurve; "0" -- the curvature from this vertex to the next one --
>
*/
x = attribs.get<double>( "x" );
y = attribs.get<double>( "y" );
}
/// Eagle polygon, without vertices which are parsed as needed
struct EPOLYGON
{
double width;
int layer;
opt_double spacing;
// KiCad priority is opposite of Eagle rank, that is:
// - Eagle Low rank drawn first
// - KiCad high priority drawn first
// So since Eagle has an upper limit we define this, used for the cases
// where no rank is specified.
static const int max_priority = 6;
enum { // for pour
SOLID,
HATCH,
CUTOUT,
};
int pour;
opt_double isolate;
opt_bool orphans;
opt_bool thermals;
opt_int rank;
EPOLYGON( CPTREE& aPolygon );
};
EPOLYGON::EPOLYGON( CPTREE& aPolygon )
{
CPTREE& attribs = aPolygon.get_child( "<xmlattr>" );
/*
<!ATTLIST polygon
width %Dimension; #REQUIRED
layer %Layer; #REQUIRED
spacing %Dimension; #IMPLIED
pour %PolygonPour; "solid"
isolate %Dimension; #IMPLIED -- only in <signal> or <package> context --
orphans %Bool; "no" -- only in <signal> context --
thermals %Bool; "yes" -- only in <signal> context --
rank %Int; "0" -- 1..6 in <signal> context, 0 or 7 in <package> context --
>
*/
width = attribs.get<double>( "width" );
layer = attribs.get<int>( "layer" );
spacing = attribs.get_optional<double>( "spacing" );
isolate = attribs.get_optional<double>( "isolate" );
// default pour to solid fill
pour = EPOLYGON::SOLID;
opt_string s = attribs.get_optional<string>( "pour" );
if( s )
{
// (solid | hatch | cutout)
if( !s->compare( "hatch" ) )
pour = EPOLYGON::HATCH;
else if( !s->compare( "cutout" ) )
pour = EPOLYGON::CUTOUT;
}
orphans = parseOptionalBool( attribs, "orphans" );
thermals = parseOptionalBool( attribs, "thermals" );
rank = attribs.get_optional<int>( "rank" );
}
/// Eagle hole element
struct EHOLE
{
double x;
double y;
double drill;
EHOLE( CPTREE& aHole );
};
EHOLE::EHOLE( CPTREE& aHole )
{
CPTREE& attribs = aHole.get_child( "<xmlattr>" );
/*
<!ELEMENT hole EMPTY>
<!ATTLIST hole
x %Coord; #REQUIRED
y %Coord; #REQUIRED
drill %Dimension; #REQUIRED
>
*/
// #REQUIRED:
x = attribs.get<double>( "x" );
y = attribs.get<double>( "y" );
drill = attribs.get<double>( "drill" );
}
/// Eagle element element
struct EELEMENT
{
string name;
string library;
string package;
string value;
double x;
double y;
opt_bool locked;
// opt_bool smashed;
opt_erot rot;
EELEMENT( CPTREE& aElement );
};
EELEMENT::EELEMENT( CPTREE& aElement )
{
CPTREE& attribs = aElement.get_child( "<xmlattr>" );
/*
<!ELEMENT element (attribute*, variant*)>
<!ATTLIST element
name %String; #REQUIRED
library %String; #REQUIRED
package %String; #REQUIRED
value %String; #REQUIRED
x %Coord; #REQUIRED
y %Coord; #REQUIRED
locked %Bool; "no"
smashed %Bool; "no"
rot %Rotation; "R0"
>
*/
// #REQUIRED
name = attribs.get<string>( "name" );
library = attribs.get<string>( "library" );
value = attribs.get<string>( "value" );
package = attribs.get<string>( "package" );
ReplaceIllegalFileNameChars( &package );
x = attribs.get<double>( "x" );
y = attribs.get<double>( "y" );
// optional
locked = parseOptionalBool( attribs, "locked" );
// smashed = pasreOptionalBool( attribs, "smashed" );
rot = parseOptionalEROT( attribs );
}
struct ELAYER
{
int number;
string name;
int color;
int fill;
opt_bool visible;
opt_bool active;
ELAYER( CPTREE& aLayer );
};
ELAYER::ELAYER( CPTREE& aLayer )
{
CPTREE& attribs = aLayer.get_child( "<xmlattr>" );
/*
<!ELEMENT layer EMPTY>
<!ATTLIST layer
number %Layer; #REQUIRED
name %String; #REQUIRED
color %Int; #REQUIRED
fill %Int; #REQUIRED
visible %Bool; "yes"
active %Bool; "yes"
>
*/
number = attribs.get<int>( "number" );
name = attribs.get<string>( "name" );
color = attribs.get<int>( "color" );
fill = 1; // Temporary value.
visible = parseOptionalBool( attribs, "visible" );
active = parseOptionalBool( attribs, "active" );
}
/// Parse an eagle distance which is either mm, or mils if there is "mil" suffix.
/// Return is in BIU.
static double parseEagle( const string& aDistance )
{
double ret = strtod( aDistance.c_str(), NULL );
if( aDistance.npos != aDistance.find( "mil" ) )
ret = IU_PER_MILS * ret;
else
ret = IU_PER_MM * ret;
return ret;
}
/// subset of eagle.drawing.board.designrules in the XML document
struct ERULES
{
int psElongationLong; ///< percent over 100%. 0-> not elongated, 100->twice as wide as is tall
///< Goes into making a scaling factor for "long" pads.
int psElongationOffset; ///< the offset of the hole within the "long" pad.
double rvPadTop; ///< top pad size as percent of drill size
// double rvPadBottom; ///< bottom pad size as percent of drill size
double rlMinPadTop; ///< minimum copper annulus on through hole pads
double rlMaxPadTop; ///< maximum copper annulus on through hole pads
double rvViaOuter; ///< copper annulus is this percent of via hole
double rlMinViaOuter; ///< minimum copper annulus on via
double rlMaxViaOuter; ///< maximum copper annulus on via
double mdWireWire; ///< wire to wire spacing I presume.
ERULES() :
psElongationLong ( 100 ),
psElongationOffset ( 0 ),
rvPadTop ( 0.25 ),
// rvPadBottom ( 0.25 ),
rlMinPadTop ( Mils2iu( 10 ) ),
rlMaxPadTop ( Mils2iu( 20 ) ),
rvViaOuter ( 0.25 ),
rlMinViaOuter ( Mils2iu( 10 ) ),
rlMaxViaOuter ( Mils2iu( 20 ) ),
mdWireWire ( 0 )
{}
void parse( CPTREE& aRules );
};
void ERULES::parse( CPTREE& aRules )
{
for( CITER it = aRules.begin(); it != aRules.end(); ++it )
{
if( it->first != "param" )
continue;
CPTREE& attribs = it->second.get_child( "<xmlattr>" );
const string& name = attribs.get<string>( "name" );
if( name == "psElongationLong" )
psElongationLong = attribs.get<int>( "value" );
else if( name == "psElongationOffset" )
psElongationOffset = attribs.get<int>( "value" );
else if( name == "rvPadTop" )
rvPadTop = attribs.get<double>( "value" );
else if( name == "rlMinPadTop" )
rlMinPadTop = parseEagle( attribs.get<string>( "value" ) );
else if( name == "rlMaxPadTop" )
rlMaxPadTop = parseEagle( attribs.get<string>( "value" ) );
else if( name == "rvViaOuter" )
rvViaOuter = attribs.get<double>( "value" );
else if( name == "rlMinViaOuter" )
rlMinViaOuter = parseEagle( attribs.get<string>( "value" ) );
else if( name == "rlMaxViaOuter" )
rlMaxViaOuter = parseEagle( attribs.get<string>( "value" ) );
else if( name == "mdWireWire" )
mdWireWire = parseEagle( attribs.get<string>( "value" ) );
}
}
/// Assemble a two part key as a simple concatonation of aFirst and aSecond parts,
/// using a separator.
static inline string makeKey( const string& aFirst, const string& aSecond )
{
string key = aFirst + '\x02' + aSecond;
return key;
}
/// Make a unique time stamp
static inline unsigned long timeStamp( CPTREE& aTree )
{
// in this case from a unique tree memory location
return (unsigned long)(void*) &aTree;
}
/// Convert an Eagle curve end to a KiCad center for S_ARC
wxPoint kicad_arc_center( wxPoint start, wxPoint end, double angle )
{
// Eagle give us start and end.
// S_ARC wants start to give the center, and end to give the start.
double dx = end.x - start.x, dy = end.y - start.y;
wxPoint mid = (start + end) / 2;
double dlen = sqrt( dx*dx + dy*dy );
double dist = dlen / ( 2 * tan( DEG2RAD( angle ) / 2 ) );
wxPoint center(
mid.x + dist * ( dy / dlen ),
mid.y - dist * ( dx / dlen )
);
return center;
}
EAGLE_PLUGIN::EAGLE_PLUGIN() :
m_rules( new ERULES() ),
m_xpath( new XPATH() ),
m_mod_time( wxDateTime::Now() )
{
init( NULL );
clear_cu_map();
}
EAGLE_PLUGIN::~EAGLE_PLUGIN()
{
delete m_rules;
delete m_xpath;
}
const wxString EAGLE_PLUGIN::PluginName() const
{
return wxT( "Eagle" );
}
const wxString EAGLE_PLUGIN::GetFileExtension() const
{
return wxT( "brd" );
}
int inline EAGLE_PLUGIN::kicad( double d ) const
{
return KiROUND( biu_per_mm * d );
}
wxSize inline EAGLE_PLUGIN::kicad_fontz( double d ) const
{
// texts seem to better match eagle when scaled down by 0.95
int kz = kicad( d ) * 95 / 100;
return wxSize( kz, kz );
}
BOARD* EAGLE_PLUGIN::Load( const wxString& aFileName, BOARD* aAppendToMe, const PROPERTIES* aProperties )
{
LOCALE_IO toggle; // toggles on, then off, the C locale.
PTREE doc;
init( aProperties );
m_board = aAppendToMe ? aAppendToMe : new BOARD();
// Give the filename to the board if it's new
if( !aAppendToMe )
m_board->SetFileName( aFileName );
// delete on exception, iff I own m_board, according to aAppendToMe
auto_ptr<BOARD> deleter( aAppendToMe ? NULL : m_board );
try
{
// 8 bit "filename" should be encoded according to disk filename encoding,
// (maybe this is current locale, maybe not, its a filesystem issue),
// and is not necessarily utf8.
string filename = (const char*) aFileName.char_str( wxConvFile );
read_xml( filename, doc, xml_parser::no_comments );
m_min_trace = INT_MAX;
m_min_via = INT_MAX;
m_min_via_hole = INT_MAX;
loadAllSections( doc );
BOARD_DESIGN_SETTINGS& designSettings = m_board->GetDesignSettings();
if( m_min_trace < designSettings.m_TrackMinWidth )
designSettings.m_TrackMinWidth = m_min_trace;
if( m_min_via < designSettings.m_ViasMinSize )
designSettings.m_ViasMinSize = m_min_via;
if( m_min_via_hole < designSettings.m_ViasMinDrill )
designSettings.m_ViasMinDrill = m_min_via_hole;
if( m_rules->mdWireWire )
{
NETCLASSPTR defaultNetclass = designSettings.GetDefault();
int clearance = KiROUND( m_rules->mdWireWire );
if( clearance < defaultNetclass->GetClearance() )
defaultNetclass->SetClearance( clearance );
}
// should be empty, else missing m_xpath->pop()
wxASSERT( m_xpath->Contents().size() == 0 );
}
catch( file_parser_error fpe )
{
// for xml_parser_error, what() has the line number in it,
// but no byte offset. That should be an adequate error message.
THROW_IO_ERROR( fpe.what() );
}
// Class ptree_error is a base class for xml_parser_error & file_parser_error,
// so one catch should be OK for all errors.
catch( ptree_error pte )
{
string errmsg = pte.what();
errmsg += " @\n";
errmsg += m_xpath->Contents();
THROW_IO_ERROR( errmsg );
}
// IO_ERROR exceptions are left uncaught, they pass upwards from here.
// Ensure the copper layers count is a multiple of 2
// Pcbnew does not like boards with odd layers count
// (these boards cannot exist. they actually have a even layers count)
int lyrcnt = m_board->GetCopperLayerCount();
if( (lyrcnt % 2) != 0 )
{
lyrcnt++;
m_board->SetCopperLayerCount( lyrcnt );
}
centerBoard();
deleter.release();
return m_board;
}
void EAGLE_PLUGIN::init( const PROPERTIES* aProperties )
{
m_hole_count = 0;
m_min_trace = 0;
m_min_via = 0;
m_min_via_hole = 0;
m_xpath->clear();
m_pads_to_nets.clear();
// m_templates.clear(); this is the FOOTPRINT cache too
m_board = NULL;
m_props = aProperties;
mm_per_biu = 1/IU_PER_MM;
biu_per_mm = IU_PER_MM;
delete m_rules;
m_rules = new ERULES();
}
void EAGLE_PLUGIN::clear_cu_map()
{
// All cu layers are invalid until we see them in the <layers> section while
// loading either a board or library. See loadLayerDefs().
for( unsigned i = 0; i < DIM(m_cu_map); ++i )
m_cu_map[i] = -1;
}
void EAGLE_PLUGIN::loadAllSections( CPTREE& aDoc )
{
CPTREE& drawing = aDoc.get_child( "eagle.drawing" );
CPTREE& board = drawing.get_child( "board" );
m_xpath->push( "eagle.drawing" );
{
m_xpath->push( "board" );
CPTREE& designrules = board.get_child( "designrules" );
loadDesignRules( designrules );
m_xpath->pop();
}
{
m_xpath->push( "layers" );
CPTREE& layers = drawing.get_child( "layers" );
loadLayerDefs( layers );
m_xpath->pop();
}
{
m_xpath->push( "board" );
CPTREE& plain = board.get_child( "plain" );
loadPlain( plain );
CPTREE& signals = board.get_child( "signals" );
loadSignals( signals );
CPTREE& libs = board.get_child( "libraries" );
loadLibraries( libs );
CPTREE& elems = board.get_child( "elements" );
loadElements( elems );
m_xpath->pop(); // "board"
}
m_xpath->pop(); // "eagle.drawing"
}
void EAGLE_PLUGIN::loadDesignRules( CPTREE& aDesignRules )
{
m_xpath->push( "designrules" );
m_rules->parse( aDesignRules );
m_xpath->pop(); // "designrules"
}
void EAGLE_PLUGIN::loadLayerDefs( CPTREE& aLayers )
{
typedef std::vector<ELAYER> ELAYERS;
typedef ELAYERS::const_iterator EITER;
ELAYERS cu; // copper layers
// find the subset of layers that are copper, and active
for( CITER layer = aLayers.begin(); layer != aLayers.end(); ++layer )
{
ELAYER elayer( layer->second );
if( elayer.number >= 1 && elayer.number <= 16 && ( !elayer.active || *elayer.active ) )
{
cu.push_back( elayer );
}
}
// establish cu layer map:
int ki_layer_count = 0;
for( EITER it = cu.begin(); it != cu.end(); ++it, ++ki_layer_count )
{
if( ki_layer_count == 0 )
m_cu_map[it->number] = F_Cu;
else if( ki_layer_count == int( cu.size()-1 ) )
m_cu_map[it->number] = B_Cu;
else
{
// some eagle boards do not have contiguous layer number sequences.
#if 0 // pre LAYER_ID & LSET:
m_cu_map[it->number] = cu.size() - 1 - ki_layer_count;
#else
m_cu_map[it->number] = ki_layer_count;
#endif
}
}
#if 0 && defined(DEBUG)
printf( "m_cu_map:\n" );
for( unsigned i=0; i<DIM(m_cu_map); ++i )
{
printf( "\t[%d]:%d\n", i, m_cu_map[i] );
}
#endif
// Set the layer names and cu count iff we're loading a board.
if( m_board )
{
m_board->SetCopperLayerCount( cu.size() );
for( EITER it = cu.begin(); it != cu.end(); ++it )
{
LAYER_ID layer = kicad_layer( it->number );
// these function provide their own protection against UNDEFINED_LAYER:
m_board->SetLayerName( layer, FROM_UTF8( it->name.c_str() ) );
m_board->SetLayerType( layer, LT_SIGNAL );
// could map the colors here
}
}
}
void EAGLE_PLUGIN::loadPlain( CPTREE& aGraphics )
{
m_xpath->push( "plain" );
// (polygon | wire | text | circle | rectangle | frame | hole)*
for( CITER gr = aGraphics.begin(); gr != aGraphics.end(); ++gr )
{
if( gr->first == "wire" )
{
m_xpath->push( "wire" );
EWIRE w( gr->second );
LAYER_ID layer = kicad_layer( w.layer );
wxPoint start( kicad_x( w.x1 ), kicad_y( w.y1 ) );
wxPoint end( kicad_x( w.x2 ), kicad_y( w.y2 ) );
if( layer != UNDEFINED_LAYER )
{
DRAWSEGMENT* dseg = new DRAWSEGMENT( m_board );
m_board->Add( dseg, ADD_APPEND );
if( !w.curve )
{
dseg->SetStart( start );
dseg->SetEnd( end );
}
else
{
wxPoint center = kicad_arc_center( start, end, *w.curve);
dseg->SetShape( S_ARC );
dseg->SetStart( center );
dseg->SetEnd( start );
dseg->SetAngle( *w.curve * -10.0 ); // KiCad rotates the other way
}
dseg->SetTimeStamp( timeStamp( gr->second ) );
dseg->SetLayer( layer );
dseg->SetWidth( Millimeter2iu( DEFAULT_PCB_EDGE_THICKNESS ) );
}
m_xpath->pop();
}
else if( gr->first == "text" )
{
#if defined(DEBUG)
if( gr->second.data() == "ATMEGA328" )
{
int breakhere = 1;
(void) breakhere;
}
#endif
m_xpath->push( "text" );
ETEXT t( gr->second );
LAYER_ID layer = kicad_layer( t.layer );
if( layer != UNDEFINED_LAYER )
{
TEXTE_PCB* pcbtxt = new TEXTE_PCB( m_board );
m_board->Add( pcbtxt, ADD_APPEND );
pcbtxt->SetLayer( layer );
pcbtxt->SetTimeStamp( timeStamp( gr->second ) );
pcbtxt->SetText( FROM_UTF8( t.text.c_str() ) );
pcbtxt->SetTextPosition( wxPoint( kicad_x( t.x ), kicad_y( t.y ) ) );
pcbtxt->SetSize( kicad_fontz( t.size ) );
double ratio = t.ratio ? *t.ratio : 8; // DTD says 8 is default
pcbtxt->SetThickness( kicad( t.size * ratio / 100 ) );
int align = t.align ? *t.align : ETEXT::BOTTOM_LEFT;
if( t.rot )
{
int sign = t.rot->mirror ? -1 : 1;
pcbtxt->SetMirrored( t.rot->mirror );
double degrees = t.rot->degrees;
if( degrees == 90 || t.rot->spin )
pcbtxt->SetOrientation( sign * t.rot->degrees * 10 );
else if( degrees == 180 )
align = ETEXT::TOP_RIGHT;
else if( degrees == 270 )
{
pcbtxt->SetOrientation( sign * 90 * 10 );
align = ETEXT::TOP_RIGHT;
}
}
switch( align )
{
case ETEXT::CENTER:
// this was the default in pcbtxt's constructor
break;
case ETEXT::CENTER_LEFT:
pcbtxt->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
break;
case ETEXT::CENTER_RIGHT:
pcbtxt->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
break;
case ETEXT::TOP_CENTER:
pcbtxt->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
break;
case ETEXT::TOP_LEFT:
pcbtxt->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
pcbtxt->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
break;
case ETEXT::TOP_RIGHT:
pcbtxt->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
pcbtxt->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
break;
case ETEXT::BOTTOM_CENTER:
pcbtxt->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
break;
case ETEXT::BOTTOM_LEFT:
pcbtxt->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
pcbtxt->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
break;
case ETEXT::BOTTOM_RIGHT:
pcbtxt->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
pcbtxt->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
break;
}
}
m_xpath->pop();
}
else if( gr->first == "circle" )
{
m_xpath->push( "circle" );
ECIRCLE c( gr->second );
LAYER_ID layer = kicad_layer( c.layer );
if( layer != UNDEFINED_LAYER ) // unsupported layer
{
DRAWSEGMENT* dseg = new DRAWSEGMENT( m_board );
m_board->Add( dseg, ADD_APPEND );
dseg->SetShape( S_CIRCLE );
dseg->SetTimeStamp( timeStamp( gr->second ) );
dseg->SetLayer( layer );
dseg->SetStart( wxPoint( kicad_x( c.x ), kicad_y( c.y ) ) );
dseg->SetEnd( wxPoint( kicad_x( c.x + c.radius ), kicad_y( c.y ) ) );
dseg->SetWidth( kicad( c.width ) );
}
m_xpath->pop();
}
else if( gr->first == "rectangle" )
{
// This seems to be a simplified rectangular [copper] zone, cannot find any
// net related info on it from the DTD.
m_xpath->push( "rectangle" );
ERECT r( gr->second );
LAYER_ID layer = kicad_layer( r.layer );
if( IsCopperLayer( layer ) )
{
// use a "netcode = 0" type ZONE:
ZONE_CONTAINER* zone = new ZONE_CONTAINER( m_board );
m_board->Add( zone, ADD_APPEND );
zone->SetTimeStamp( timeStamp( gr->second ) );
zone->SetLayer( layer );
zone->SetNetCode( NETINFO_LIST::UNCONNECTED );
CPolyLine::HATCH_STYLE outline_hatch = CPolyLine::DIAGONAL_EDGE;
zone->Outline()->Start( layer, kicad_x( r.x1 ), kicad_y( r.y1 ), outline_hatch );
zone->AppendCorner( wxPoint( kicad_x( r.x2 ), kicad_y( r.y1 ) ) );
zone->AppendCorner( wxPoint( kicad_x( r.x2 ), kicad_y( r.y2 ) ) );
zone->AppendCorner( wxPoint( kicad_x( r.x1 ), kicad_y( r.y2 ) ) );
zone->Outline()->CloseLastContour();
// this is not my fault:
zone->Outline()->SetHatch(
outline_hatch, Mils2iu( zone->Outline()->GetDefaultHatchPitchMils() ), true );
}
m_xpath->pop();
}
else if( gr->first == "hole" )
{
m_xpath->push( "hole" );
EHOLE e( gr->second );
// Fabricate a MODULE with a single PAD_ATTRIB_HOLE_NOT_PLATED pad.
// Use m_hole_count to gen up a unique name.
MODULE* module = new MODULE( m_board );
m_board->Add( module, ADD_APPEND );
char temp[40];
sprintf( temp, "@HOLE%d", m_hole_count++ );
module->SetReference( FROM_UTF8( temp ) );
module->Reference().SetVisible( false );
wxPoint pos( kicad_x( e.x ), kicad_y( e.y ) );
module->SetPosition( pos );
// Add a PAD_ATTRIB_HOLE_NOT_PLATED pad to this module.
D_PAD* pad = new D_PAD( module );
module->Pads().PushBack( pad );
pad->SetShape( PAD_SHAPE_CIRCLE );
pad->SetAttribute( PAD_ATTRIB_HOLE_NOT_PLATED );
/* pad's position is already centered on module at relative (0, 0)
wxPoint padpos( kicad_x( e.x ), kicad_y( e.y ) );
pad->SetPos0( padpos );
pad->SetPosition( padpos + module->GetPosition() );
*/
wxSize sz( kicad( e.drill ), kicad( e.drill ) );
pad->SetDrillSize( sz );
pad->SetSize( sz );
pad->SetLayerSet( LSET::AllCuMask() );
m_xpath->pop();
}
else if( gr->first == "frame" )
{
// picture this
}
else if( gr->first == "polygon" )
{
// could be on a copper layer, could be on another layer.
// copper layer would be done using netCode=0 type of ZONE_CONTAINER.
}
}
m_xpath->pop();
}
void EAGLE_PLUGIN::loadLibrary( CPTREE& aLib, const string* aLibName )
{
m_xpath->push( "packages" );
// library will have <xmlattr> node, skip that and get the single packages node
CPTREE& packages = aLib.get_child( "packages" );
// Create a MODULE for all the eagle packages, for use later via a copy constructor
// to instantiate needed MODULES in our BOARD. Save the MODULE templates in
// a MODULE_MAP using a single lookup key consisting of libname+pkgname.
for( CITER package = packages.begin(); package != packages.end(); ++package )
{
m_xpath->push( "package", "name" );
const string& pack_ref = package->second.get<string>( "<xmlattr>.name" );
string pack_name( pack_ref );
ReplaceIllegalFileNameChars( &pack_name );
#if 0 && defined(DEBUG)
if( pack_name == "TO220H" )
{
int breakhere = 1;
(void) breakhere;
}
#endif
m_xpath->Value( pack_name.c_str() );
string key = aLibName ? makeKey( *aLibName, pack_name ) : pack_name;
MODULE* m = makeModule( package->second, pack_name );
// add the templating MODULE to the MODULE template factory "m_templates"
std::pair<MODULE_ITER, bool> r = m_templates.insert( key, m );
if( !r.second
// && !( m_props && m_props->Value( "ignore_duplicates" ) )
)
{
wxString lib = aLibName ? FROM_UTF8( aLibName->c_str() ) : m_lib_path;
wxString pkg = FROM_UTF8( pack_name.c_str() );
wxString emsg = wxString::Format(
_( "<package> name: '%s' duplicated in eagle <library>: '%s'" ),
GetChars( pkg ),
GetChars( lib )
);
THROW_IO_ERROR( emsg );
}
m_xpath->pop();
}
m_xpath->pop(); // "packages"
}
void EAGLE_PLUGIN::loadLibraries( CPTREE& aLibs )
{
m_xpath->push( "libraries.library", "name" );
for( CITER library = aLibs.begin(); library != aLibs.end(); ++library )
{
const string& lib_name = library->second.get<string>( "<xmlattr>.name" );
m_xpath->Value( lib_name.c_str() );
loadLibrary( library->second, &lib_name );
}
m_xpath->pop();
}
void EAGLE_PLUGIN::loadElements( CPTREE& aElements )
{
m_xpath->push( "elements.element", "name" );
EATTR name;
EATTR value;
for( CITER it = aElements.begin(); it != aElements.end(); ++it )
{
if( it->first != "element" )
continue;
EELEMENT e( it->second );
// use "NULL-ness" as an indication of presence of the attribute:
EATTR* nameAttr = 0;
EATTR* valueAttr = 0;
m_xpath->Value( e.name.c_str() );
string key = makeKey( e.library, e.package );
MODULE_CITER mi = m_templates.find( key );
if( mi == m_templates.end() )
{
wxString emsg = wxString::Format( _( "No '%s' package in library '%s'" ),
GetChars( FROM_UTF8( e.package.c_str() ) ),
GetChars( FROM_UTF8( e.library.c_str() ) ) );
THROW_IO_ERROR( emsg );
}
#if defined(DEBUG)
if( e.name == "ARM_C8" )
{
int breakhere = 1;
(void) breakhere;
}
#endif
// copy constructor to clone the template
MODULE* m = new MODULE( *mi->second );
m_board->Add( m, ADD_APPEND );
// update the nets within the pads of the clone
for( D_PAD* pad = m->Pads(); pad; pad = pad->Next() )
{
string key = makeKey( e.name, TO_UTF8( pad->GetPadName() ) );
NET_MAP_CITER ni = m_pads_to_nets.find( key );
if( ni != m_pads_to_nets.end() )
{
const ENET* enet = &ni->second;
pad->SetNetCode( enet->netcode );
}
}
m->SetPosition( wxPoint( kicad_x( e.x ), kicad_y( e.y ) ) );
m->SetReference( FROM_UTF8( e.name.c_str() ) );
m->SetValue( FROM_UTF8( e.value.c_str() ) );
// m->Value().SetVisible( false );
// initalize these to default values incase the <attribute> elements are not present.
m_xpath->push( "attribute", "name" );
// VALUE and NAME can have something like our text "effects" overrides
// in SWEET and new schematic. Eagle calls these XML elements "attribute".
// There can be one for NAME and/or VALUE both. Features present in the
// EATTR override the ones established in the package only if they are
// present here (except for rot, which if not present means angle zero).
// So the logic is a bit different than in packageText() and in plain text.
for( CITER ait = it->second.begin(); ait != it->second.end(); ++ait )
{
if( ait->first != "attribute" )
continue;
EATTR a( ait->second );
if( a.name == "NAME" )
{
name = a;
nameAttr = &name;
}
else if( a.name == "VALUE" )
{
value = a;
valueAttr = &value;
}
}
m_xpath->pop(); // "attribute"
orientModuleAndText( m, e, nameAttr, valueAttr );
}
m_xpath->pop(); // "elements.element"
}
void EAGLE_PLUGIN::orientModuleAndText( MODULE* m, const EELEMENT& e,
const EATTR* nameAttr, const EATTR* valueAttr )
{
if( e.rot )
{
if( e.rot->mirror )
{
double orientation = e.rot->degrees + 180.0;
m->SetOrientation( orientation * 10 );
m->Flip( m->GetPosition() );
}
else
m->SetOrientation( e.rot->degrees * 10 );
}
orientModuleText( m, e, &m->Reference(), nameAttr );
orientModuleText( m, e, &m->Value(), valueAttr );
}
void EAGLE_PLUGIN::orientModuleText( MODULE* m, const EELEMENT& e,
TEXTE_MODULE* txt, const EATTR* aAttr )
{
if( aAttr )
{
const EATTR& a = *aAttr;
if( a.value )
{
txt->SetText( FROM_UTF8( a.value->c_str() ) );
}
if( a.x && a.y ) // boost::optional
{
wxPoint pos( kicad_x( *a.x ), kicad_y( *a.y ) );
txt->SetTextPosition( pos );
}
// Even though size and ratio are both optional, I am not seeing
// a case where ratio is present but size is not.
double ratio = 8;
wxSize fontz = txt->GetSize();
if( a.size )
{
fontz = kicad_fontz( *a.size );
txt->SetSize( fontz );
if( a.ratio )
ratio = *a.ratio;
}
int lw = int( fontz.y * ratio / 100.0 );
txt->SetThickness( lw );
int align = ETEXT::BOTTOM_LEFT; // bottom-left is eagle default
// The "rot" in a EATTR seems to be assumed to be zero if it is not
// present, and this zero rotation becomes an override to the
// package's text field. If they did not want zero, they specify
// what they want explicitly.
double degrees = a.rot ? a.rot->degrees : 0;
double orient; // relative to parent
int sign = 1;
bool spin = false;
if( a.rot )
{
spin = a.rot->spin;
sign = a.rot->mirror ? -1 : 1;
txt->SetMirrored( a.rot->mirror );
}
if( degrees == 90 || degrees == 0 || spin )
{
orient = degrees - m->GetOrientation() / 10;
txt->SetOrientation( sign * orient * 10 );
}
else if( degrees == 180 )
{
orient = 0 - m->GetOrientation() / 10;
txt->SetOrientation( sign * orient * 10 );
align = ETEXT::TOP_RIGHT;
}
else if( degrees == 270 )
{
orient = 90 - m->GetOrientation() / 10;
align = ETEXT::TOP_RIGHT;
txt->SetOrientation( sign * orient * 10 );
}
else
{
orient = 90 + degrees - m->GetOrientation() / 10;
txt->SetOrientation( sign * orient * 10 );
}
switch( align )
{
case ETEXT::TOP_RIGHT:
txt->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
txt->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
break;
case ETEXT::BOTTOM_LEFT:
txt->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
txt->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
break;
default:
;
}
}
else // the text is per the original package, sans <attribute>
{
double degrees = ( txt->GetOrientation() + m->GetOrientation() ) / 10;
// @todo there are a few more cases than these to contend with:
if( (!txt->IsMirrored() && ( abs( degrees ) == 180 || abs( degrees ) == 270 ))
|| ( txt->IsMirrored() && ( degrees == 360 ) ) )
{
// ETEXT::TOP_RIGHT:
txt->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
txt->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
}
}
}
MODULE* EAGLE_PLUGIN::makeModule( CPTREE& aPackage, const string& aPkgName ) const
{
std::auto_ptr<MODULE> m( new MODULE( m_board ) );
m->SetFPID( FPID( aPkgName ) );
opt_string description = aPackage.get_optional<string>( "description" );
if( description )
m->SetDescription( FROM_UTF8( description->c_str() ) );
for( CITER it = aPackage.begin(); it != aPackage.end(); ++it )
{
CPTREE& t = it->second;
if( it->first == "wire" )
packageWire( m.get(), t );
else if( it->first == "pad" )
packagePad( m.get(), t );
else if( it->first == "text" )
packageText( m.get(), t );
else if( it->first == "rectangle" )
packageRectangle( m.get(), t );
else if( it->first == "polygon" )
packagePolygon( m.get(), t );
else if( it->first == "circle" )
packageCircle( m.get(), t );
else if( it->first == "hole" )
packageHole( m.get(), t );
else if( it->first == "smd" )
packageSMD( m.get(), t );
}
return m.release();
}
void EAGLE_PLUGIN::packageWire( MODULE* aModule, CPTREE& aTree ) const
{
EWIRE w( aTree );
LAYER_ID layer = kicad_layer( w.layer );
if( IsNonCopperLayer( layer ) ) // only valid non-copper wires, skip copper package wires
{
wxPoint start( kicad_x( w.x1 ), kicad_y( w.y1 ) );
wxPoint end( kicad_x( w.x2 ), kicad_y( w.y2 ) );
int width = kicad( w.width );
// FIXME: the cap attribute is ignored because kicad can't create lines
// with flat ends.
EDGE_MODULE* dwg;
if( !w.curve )
{
dwg = new EDGE_MODULE( aModule, S_SEGMENT );
dwg->SetStart0( start );
dwg->SetEnd0( end );
}
else
{
dwg = new EDGE_MODULE( aModule, S_ARC );
wxPoint center = kicad_arc_center( start, end, *w.curve);
dwg->SetStart0( center );
dwg->SetEnd0( start );
dwg->SetAngle( *w.curve * -10.0 ); // KiCad rotates the other way
}
dwg->SetLayer( layer );
dwg->SetWidth( width );
aModule->GraphicalItems().PushBack( dwg );
}
}
void EAGLE_PLUGIN::packagePad( MODULE* aModule, CPTREE& aTree ) const
{
// this is thru hole technology here, no SMDs
EPAD e( aTree );
D_PAD* pad = new D_PAD( aModule );
aModule->Pads().PushBack( pad );
pad->SetPadName( FROM_UTF8( e.name.c_str() ) );
// pad's "Position" is not relative to the module's,
// whereas Pos0 is relative to the module's but is the unrotated coordinate.
wxPoint padpos( kicad_x( e.x ), kicad_y( e.y ) );
pad->SetPos0( padpos );
RotatePoint( &padpos, aModule->GetOrientation() );
pad->SetPosition( padpos + aModule->GetPosition() );
pad->SetDrillSize( wxSize( kicad( e.drill ), kicad( e.drill ) ) );
pad->SetLayerSet( LSET::AllCuMask().set( B_Mask ).set( F_Mask ) );
if( e.shape )
{
switch( *e.shape )
{
case EPAD::ROUND:
wxASSERT( pad->GetShape()==PAD_SHAPE_CIRCLE ); // verify set in D_PAD constructor
break;
case EPAD::OCTAGON:
// no KiCad octagonal pad shape, use PAD_CIRCLE for now.
// pad->SetShape( PAD_OCTAGON );
wxASSERT( pad->GetShape()==PAD_SHAPE_CIRCLE ); // verify set in D_PAD constructor
break;
case EPAD::LONG:
pad->SetShape( PAD_SHAPE_OVAL );
break;
case EPAD::SQUARE:
pad->SetShape( PAD_SHAPE_RECT );
break;
case EPAD::OFFSET:
; // don't know what to do here.
}
}
else
{
// if shape is not present, our default is circle and that matches their default "round"
}
if( e.diameter )
{
int diameter = kicad( *e.diameter );
pad->SetSize( wxSize( diameter, diameter ) );
}
else
{
double drillz = pad->GetDrillSize().x;
double annulus = drillz * m_rules->rvPadTop; // copper annulus, eagle "restring"
annulus = Clamp( m_rules->rlMinPadTop, annulus, m_rules->rlMaxPadTop );
int diameter = KiROUND( drillz + 2 * annulus );
pad->SetSize( wxSize( KiROUND( diameter ), KiROUND( diameter ) ) );
}
if( pad->GetShape() == PAD_SHAPE_OVAL )
{
// The Eagle "long" pad is wider than it is tall,
// m_elongation is percent elongation
wxSize sz = pad->GetSize();
sz.x = ( sz.x * ( 100 + m_rules->psElongationLong ) ) / 100;
pad->SetSize( sz );
}
if( e.rot )
{
pad->SetOrientation( e.rot->degrees * 10 );
}
// @todo: handle stop and thermal
}
void EAGLE_PLUGIN::packageText( MODULE* aModule, CPTREE& aTree ) const
{
ETEXT t( aTree );
LAYER_ID layer = kicad_layer( t.layer );
if( layer == UNDEFINED_LAYER )
{
layer = Cmts_User;
}
TEXTE_MODULE* txt;
if( t.text == ">NAME" || t.text == ">name" )
txt = &aModule->Reference();
else if( t.text == ">VALUE" || t.text == ">value" )
txt = &aModule->Value();
else
{
// FIXME: graphical text items are rotated for some reason.
txt = new TEXTE_MODULE( aModule );
aModule->GraphicalItems().PushBack( txt );
}
txt->SetTimeStamp( timeStamp( aTree ) );
txt->SetText( FROM_UTF8( t.text.c_str() ) );
wxPoint pos( kicad_x( t.x ), kicad_y( t.y ) );
txt->SetTextPosition( pos );
txt->SetPos0( pos - aModule->GetPosition() );
txt->SetLayer( layer );
txt->SetSize( kicad_fontz( t.size ) );
double ratio = t.ratio ? *t.ratio : 8; // DTD says 8 is default
txt->SetThickness( kicad( t.size * ratio / 100 ) );
int align = t.align ? *t.align : ETEXT::BOTTOM_LEFT; // bottom-left is eagle default
// An eagle package is never rotated, the DTD does not allow it.
// angle -= aModule->GetOrienation();
if( t.rot )
{
int sign = t.rot->mirror ? -1 : 1;
txt->SetMirrored( t.rot->mirror );
double degrees = t.rot->degrees;
if( degrees == 90 || t.rot->spin )
txt->SetOrientation( sign * degrees * 10 );
else if( degrees == 180 )
align = ETEXT::TOP_RIGHT;
else if( degrees == 270 )
{
align = ETEXT::TOP_RIGHT;
txt->SetOrientation( sign * 90 * 10 );
}
}
switch( align )
{
case ETEXT::CENTER:
// this was the default in pcbtxt's constructor
break;
case ETEXT::CENTER_LEFT:
txt->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
break;
case ETEXT::CENTER_RIGHT:
txt->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
break;
case ETEXT::TOP_CENTER:
txt->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
break;
case ETEXT::TOP_LEFT:
txt->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
txt->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
break;
case ETEXT::TOP_RIGHT:
txt->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
txt->SetVertJustify( GR_TEXT_VJUSTIFY_TOP );
break;
case ETEXT::BOTTOM_CENTER:
txt->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
break;
case ETEXT::BOTTOM_LEFT:
txt->SetHorizJustify( GR_TEXT_HJUSTIFY_LEFT );
txt->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
break;
case ETEXT::BOTTOM_RIGHT:
txt->SetHorizJustify( GR_TEXT_HJUSTIFY_RIGHT );
txt->SetVertJustify( GR_TEXT_VJUSTIFY_BOTTOM );
break;
}
}
void EAGLE_PLUGIN::packageRectangle( MODULE* aModule, CPTREE& aTree ) const
{
ERECT r( aTree );
LAYER_ID layer = kicad_layer( r.layer );
if( IsNonCopperLayer( layer ) ) // skip copper "package.rectangle"s
{
EDGE_MODULE* dwg = new EDGE_MODULE( aModule, S_POLYGON );
aModule->GraphicalItems().PushBack( dwg );
dwg->SetLayer( layer );
dwg->SetWidth( 0 );
dwg->SetTimeStamp( timeStamp( aTree ) );
std::vector<wxPoint> pts;
wxPoint start( wxPoint( kicad_x( r.x1 ), kicad_y( r.y1 ) ) );
wxPoint end( wxPoint( kicad_x( r.x1 ), kicad_y( r.y2 ) ) );
pts.push_back( start );
pts.push_back( wxPoint( kicad_x( r.x2 ), kicad_y( r.y1 ) ) );
pts.push_back( wxPoint( kicad_x( r.x2 ), kicad_y( r.y2 ) ) );
pts.push_back( end );
dwg->SetPolyPoints( pts );
dwg->SetStart0( start );
dwg->SetEnd0( end );
}
}
void EAGLE_PLUGIN::packagePolygon( MODULE* aModule, CPTREE& aTree ) const
{
EPOLYGON p( aTree );
LAYER_ID layer = kicad_layer( p.layer );
if( IsNonCopperLayer( layer ) ) // skip copper "package.rectangle"s
{
EDGE_MODULE* dwg = new EDGE_MODULE( aModule, S_POLYGON );
aModule->GraphicalItems().PushBack( dwg );
dwg->SetWidth( 0 ); // it's filled, no need for boundary width
/*
switch( layer )
{
case Eco1_User: layer = F_SilkS; break;
case Eco2_User: layer = B_SilkS; break;
// all MODULE templates (created from eagle packages) are on front layer
// until cloned.
case Cmts_User: layer = F_SilkS; break;
}
*/
dwg->SetLayer( layer );
dwg->SetTimeStamp( timeStamp( aTree ) );
std::vector<wxPoint> pts;
pts.reserve( aTree.size() );
for( CITER vi = aTree.begin(); vi != aTree.end(); ++vi )
{
if( vi->first != "vertex" ) // skip <xmlattr> node
continue;
EVERTEX v( vi->second );
pts.push_back( wxPoint( kicad_x( v.x ), kicad_y( v.y ) ) );
}
dwg->SetPolyPoints( pts );
dwg->SetStart0( *pts.begin() );
dwg->SetEnd0( pts.back() );
}
}
void EAGLE_PLUGIN::packageCircle( MODULE* aModule, CPTREE& aTree ) const
{
ECIRCLE e( aTree );
LAYER_ID layer = kicad_layer( e.layer );
EDGE_MODULE* gr = new EDGE_MODULE( aModule, S_CIRCLE );
aModule->GraphicalItems().PushBack( gr );
gr->SetWidth( kicad( e.width ) );
switch( (int) layer )
{
case UNDEFINED_LAYER: layer = Cmts_User; break;
/*
case Eco1_User: layer = F_SilkS; break;
case Eco2_User: layer = B_SilkS; break;
*/
default:
break;
}
gr->SetLayer( layer );
gr->SetTimeStamp( timeStamp( aTree ) );
gr->SetStart0( wxPoint( kicad_x( e.x ), kicad_y( e.y ) ) );
gr->SetEnd0( wxPoint( kicad_x( e.x + e.radius ), kicad_y( e.y ) ) );
}
void EAGLE_PLUGIN::packageHole( MODULE* aModule, CPTREE& aTree ) const
{
EHOLE e( aTree );
// we add a PAD_ATTRIB_HOLE_NOT_PLATED pad to this module.
D_PAD* pad = new D_PAD( aModule );
aModule->Pads().PushBack( pad );
pad->SetShape( PAD_SHAPE_CIRCLE );
pad->SetAttribute( PAD_ATTRIB_HOLE_NOT_PLATED );
// Mechanical purpose only:
// no offset, no net name, no pad name allowed
// pad->SetOffset( wxPoint( 0, 0 ) );
// pad->SetPadName( wxEmptyString );
wxPoint padpos( kicad_x( e.x ), kicad_y( e.y ) );
pad->SetPos0( padpos );
pad->SetPosition( padpos + aModule->GetPosition() );
wxSize sz( kicad( e.drill ), kicad( e.drill ) );
pad->SetDrillSize( sz );
pad->SetSize( sz );
pad->SetLayerSet( LSET::AllCuMask() /* | SOLDERMASK_LAYER_BACK | SOLDERMASK_LAYER_FRONT */ );
}
void EAGLE_PLUGIN::packageSMD( MODULE* aModule, CPTREE& aTree ) const
{
ESMD e( aTree );
LAYER_ID layer = kicad_layer( e.layer );
if( !IsCopperLayer( layer ) )
{
return;
}
D_PAD* pad = new D_PAD( aModule );
aModule->Pads().PushBack( pad );
pad->SetPadName( FROM_UTF8( e.name.c_str() ) );
pad->SetShape( PAD_SHAPE_RECT );
pad->SetAttribute( PAD_ATTRIB_SMD );
// pad's "Position" is not relative to the module's,
// whereas Pos0 is relative to the module's but is the unrotated coordinate.
wxPoint padpos( kicad_x( e.x ), kicad_y( e.y ) );
pad->SetPos0( padpos );
RotatePoint( &padpos, aModule->GetOrientation() );
pad->SetPosition( padpos + aModule->GetPosition() );
pad->SetSize( wxSize( kicad( e.dx ), kicad( e.dy ) ) );
pad->SetLayer( layer );
static const LSET front( 3, F_Cu, F_Paste, F_Mask );
static const LSET back( 3, B_Cu, B_Paste, B_Mask );
if( layer == F_Cu )
pad->SetLayerSet( front );
else if( layer == B_Cu )
pad->SetLayerSet( back );
// Optional according to DTD
if( e.roundness ) // set set shape to PAD_SHAPE_RECT above, in case roundness is not present
{
if( *e.roundness >= 75 ) // roundness goes from 0-100% as integer
{
if( e.dy == e.dx )
pad->SetShape( PAD_SHAPE_CIRCLE );
else
pad->SetShape( PAD_SHAPE_OVAL );
}
}
if( e.rot )
{
pad->SetOrientation( e.rot->degrees * 10 );
}
// don't know what stop, thermals, and cream should look like now.
}
/// non-owning container
typedef std::vector<ZONE_CONTAINER*> ZONES;
void EAGLE_PLUGIN::loadSignals( CPTREE& aSignals )
{
ZONES zones; // per net
m_xpath->push( "signals.signal", "name" );
int netCode = 1;
for( CITER net = aSignals.begin(); net != aSignals.end(); ++net )
{
bool sawPad = false;
zones.clear();
const string& nname = net->second.get<string>( "<xmlattr>.name" );
wxString netName = FROM_UTF8( nname.c_str() );
m_board->AppendNet( new NETINFO_ITEM( m_board, netName, netCode ) );
m_xpath->Value( nname.c_str() );
#if defined(DEBUG)
if( netName == wxT( "N$8" ) )
{
int breakhere = 1;
(void) breakhere;
}
#endif
// (contactref | polygon | wire | via)*
for( CITER it = net->second.begin(); it != net->second.end(); ++it )
{
if( it->first == "wire" )
{
m_xpath->push( "wire" );
EWIRE w( it->second );
LAYER_ID layer = kicad_layer( w.layer );
if( IsCopperLayer( layer ) )
{
TRACK* t = new TRACK( m_board );
t->SetTimeStamp( timeStamp( it->second ) );
t->SetPosition( wxPoint( kicad_x( w.x1 ), kicad_y( w.y1 ) ) );
t->SetEnd( wxPoint( kicad_x( w.x2 ), kicad_y( w.y2 ) ) );
int width = kicad( w.width );
if( width < m_min_trace )
m_min_trace = width;
t->SetWidth( width );
t->SetLayer( layer );
t->SetNetCode( netCode );
m_board->m_Track.Insert( t, NULL );
}
else
{
// put non copper wires where the sun don't shine.
}
m_xpath->pop();
}
else if( it->first == "via" )
{
m_xpath->push( "via" );
EVIA v( it->second );
LAYER_ID layer_front_most = kicad_layer( v.layer_front_most );
LAYER_ID layer_back_most = kicad_layer( v.layer_back_most );
if( IsCopperLayer( layer_front_most ) &&
IsCopperLayer( layer_back_most ) )
{
int kidiam;
int drillz = kicad( v.drill );
VIA* via = new VIA( m_board );
m_board->m_Track.Insert( via, NULL );
via->SetLayerPair( layer_front_most, layer_back_most );
if( v.diam )
{
kidiam = kicad( *v.diam );
via->SetWidth( kidiam );
}
else
{
double annulus = drillz * m_rules->rvViaOuter; // eagle "restring"
annulus = Clamp( m_rules->rlMinViaOuter, annulus, m_rules->rlMaxViaOuter );
kidiam = KiROUND( drillz + 2 * annulus );
via->SetWidth( kidiam );
}
via->SetDrill( drillz );
if( kidiam < m_min_via )
m_min_via = kidiam;
if( drillz < m_min_via_hole )
m_min_via_hole = drillz;
if( layer_front_most == F_Cu && layer_back_most == B_Cu )
via->SetViaType( VIA_THROUGH );
else if( layer_front_most == F_Cu || layer_back_most == B_Cu )
via->SetViaType( VIA_MICROVIA );
else
via->SetViaType( VIA_BLIND_BURIED );
via->SetTimeStamp( timeStamp( it->second ) );
wxPoint pos( kicad_x( v.x ), kicad_y( v.y ) );
via->SetPosition( pos );
via->SetEnd( pos );
via->SetNetCode( netCode );
}
m_xpath->pop();
}
else if( it->first == "contactref" )
{
m_xpath->push( "contactref" );
// <contactref element="RN1" pad="7"/>
CPTREE& attribs = it->second.get_child( "<xmlattr>" );
const string& reference = attribs.get<string>( "element" );
const string& pad = attribs.get<string>( "pad" );
string key = makeKey( reference, pad ) ;
// D(printf( "adding refname:'%s' pad:'%s' netcode:%d netname:'%s'\n", reference.c_str(), pad.c_str(), netCode, nname.c_str() );)
m_pads_to_nets[ key ] = ENET( netCode, nname );
m_xpath->pop();
sawPad = true;
}
else if( it->first == "polygon" )
{
m_xpath->push( "polygon" );
EPOLYGON p( it->second );
LAYER_ID layer = kicad_layer( p.layer );
if( IsCopperLayer( layer ) )
{
// use a "netcode = 0" type ZONE:
ZONE_CONTAINER* zone = new ZONE_CONTAINER( m_board );
m_board->Add( zone, ADD_APPEND );
zones.push_back( zone );
zone->SetTimeStamp( timeStamp( it->second ) );
zone->SetLayer( layer );
zone->SetNetCode( netCode );
bool first = true;
for( CITER vi = it->second.begin(); vi != it->second.end(); ++vi )
{
if( vi->first != "vertex" ) // skip <xmlattr> node
continue;
EVERTEX v( vi->second );
// the ZONE_CONTAINER API needs work, as you can see:
if( first )
{
zone->Outline()->Start( layer, kicad_x( v.x ), kicad_y( v.y ),
CPolyLine::NO_HATCH);
first = false;
}
else
zone->AppendCorner( wxPoint( kicad_x( v.x ), kicad_y( v.y ) ) );
}
zone->Outline()->CloseLastContour();
// If the pour is a cutout it needs to be set to a keepout
if( p.pour == EPOLYGON::CUTOUT )
{
zone->SetIsKeepout( true );
zone->SetDoNotAllowCopperPour( true );
zone->Outline()->SetHatchStyle( CPolyLine::NO_HATCH );
}
// if spacing is set the zone should be hatched
if( p.spacing )
zone->Outline()->SetHatch( CPolyLine::DIAGONAL_EDGE,
*p.spacing,
true );
// clearances, etc.
zone->SetArcSegmentCount( 32 ); // @todo: should be a constructor default?
zone->SetMinThickness( kicad( p.width ) );
// FIXME: KiCad zones have very rounded corners compared to eagle.
// This means that isolation amounts that work well in eagle
// tend to make copper intrude in soldermask free areas around pads.
if( p.isolate )
{
zone->SetZoneClearance( kicad( *p.isolate ) );
}
// missing == yes per DTD.
bool thermals = !p.thermals || *p.thermals;
zone->SetPadConnection( thermals ? PAD_ZONE_CONN_THERMAL : PAD_ZONE_CONN_FULL );
if( thermals )
{
// FIXME: eagle calculates dimensions for thermal spokes
// based on what the zone is connecting to.
// (i.e. width of spoke is half of the smaller side of an smd pad)
// This is a basic workaround
zone->SetThermalReliefGap( kicad( p.width + 0.05 ) );
zone->SetThermalReliefCopperBridge( kicad( p.width + 0.05 ) );
}
int rank = p.rank ? *p.rank : p.max_priority;
zone->SetPriority( rank );
}
m_xpath->pop(); // "polygon"
}
}
if( zones.size() && !sawPad )
{
// KiCad does not support an unconnected zone with its own non-zero netcode,
// but only when assigned netcode = 0 w/o a name...
for( ZONES::iterator it = zones.begin(); it != zones.end(); ++it )
(*it)->SetNetCode( NETINFO_LIST::UNCONNECTED );
// therefore omit this signal/net.
}
else
netCode++;
}
m_xpath->pop(); // "signals.signal"
}
LAYER_ID EAGLE_PLUGIN::kicad_layer( int aEagleLayer ) const
{
/* will assume this is a valid mapping for all eagle boards until I get paid more:
<layers>
<layer number="1" name="Top" color="4" fill="1" visible="yes" active="yes"/>
<layer number="2" name="Route2" color="1" fill="3" visible="no" active="no"/>
<layer number="3" name="Route3" color="4" fill="3" visible="no" active="no"/>
<layer number="4" name="Route4" color="1" fill="4" visible="no" active="no"/>
<layer number="5" name="Route5" color="4" fill="4" visible="no" active="no"/>
<layer number="6" name="Route6" color="1" fill="8" visible="no" active="no"/>
<layer number="7" name="Route7" color="4" fill="8" visible="no" active="no"/>
<layer number="8" name="Route8" color="1" fill="2" visible="no" active="no"/>
<layer number="9" name="Route9" color="4" fill="2" visible="no" active="no"/>
<layer number="10" name="Route10" color="1" fill="7" visible="no" active="no"/>
<layer number="11" name="Route11" color="4" fill="7" visible="no" active="no"/>
<layer number="12" name="Route12" color="1" fill="5" visible="no" active="no"/>
<layer number="13" name="Route13" color="4" fill="5" visible="no" active="no"/>
<layer number="14" name="Route14" color="1" fill="6" visible="no" active="no"/>
<layer number="15" name="Route15" color="4" fill="6" visible="no" active="no"/>
<layer number="16" name="Bottom" color="1" fill="1" visible="yes" active="yes"/>
<layer number="17" name="Pads" color="2" fill="1" visible="yes" active="yes"/>
<layer number="18" name="Vias" color="14" fill="1" visible="yes" active="yes"/>
<layer number="19" name="Unrouted" color="6" fill="1" visible="yes" active="yes"/>
<layer number="20" name="Dimension" color="15" fill="1" visible="yes" active="yes"/>
<layer number="21" name="tPlace" color="7" fill="1" visible="yes" active="yes"/>
<layer number="22" name="bPlace" color="7" fill="1" visible="yes" active="yes"/>
<layer number="23" name="tOrigins" color="15" fill="1" visible="yes" active="yes"/>
<layer number="24" name="bOrigins" color="15" fill="1" visible="yes" active="yes"/>
<layer number="25" name="tNames" color="7" fill="1" visible="yes" active="yes"/>
<layer number="26" name="bNames" color="7" fill="1" visible="yes" active="yes"/>
<layer number="27" name="tValues" color="7" fill="1" visible="no" active="yes"/>
<layer number="28" name="bValues" color="7" fill="1" visible="no" active="yes"/>
<layer number="29" name="tStop" color="2" fill="3" visible="no" active="yes"/>
<layer number="30" name="bStop" color="5" fill="6" visible="no" active="yes"/>
<layer number="31" name="tCream" color="7" fill="4" visible="no" active="yes"/>
<layer number="32" name="bCream" color="7" fill="5" visible="no" active="yes"/>
<layer number="33" name="tFinish" color="6" fill="3" visible="no" active="yes"/>
<layer number="34" name="bFinish" color="6" fill="6" visible="no" active="yes"/>
<layer number="35" name="tGlue" color="7" fill="4" visible="no" active="yes"/>
<layer number="36" name="bGlue" color="7" fill="5" visible="no" active="yes"/>
<layer number="37" name="tTest" color="7" fill="1" visible="no" active="yes"/>
<layer number="38" name="bTest" color="7" fill="1" visible="no" active="yes"/>
<layer number="39" name="tKeepout" color="4" fill="11" visible="no" active="yes"/>
<layer number="40" name="bKeepout" color="1" fill="11" visible="no" active="yes"/>
<layer number="41" name="tRestrict" color="4" fill="10" visible="no" active="yes"/>
<layer number="42" name="bRestrict" color="1" fill="10" visible="no" active="yes"/>
<layer number="43" name="vRestrict" color="2" fill="10" visible="no" active="yes"/>
<layer number="44" name="Drills" color="7" fill="1" visible="no" active="yes"/>
<layer number="45" name="Holes" color="7" fill="1" visible="no" active="yes"/>
<layer number="46" name="Milling" color="3" fill="1" visible="no" active="yes"/>
<layer number="47" name="Measures" color="7" fill="1" visible="no" active="yes"/>
<layer number="48" name="Document" color="7" fill="1" visible="no" active="yes"/>
<layer number="49" name="ReferenceLC" color="13" fill="1" visible="yes" active="yes"/>
<layer number="50" name="ReferenceLS" color="12" fill="1" visible="yes" active="yes"/>
<layer number="51" name="tDocu" color="7" fill="1" visible="yes" active="yes"/>
<layer number="52" name="bDocu" color="7" fill="1" visible="yes" active="yes"/>
* These layers are used only in eagle schematic.
* They should not be found in board files.
* They are listed for info only.
<layer number="91" name="Nets" color="2" fill="1" visible="no" active="no"/>
<layer number="92" name="Busses" color="1" fill="1" visible="no" active="no"/>
<layer number="93" name="Pins" color="2" fill="1" visible="no" active="no"/>
<layer number="94" name="Symbols" color="4" fill="1" visible="no" active="no"/>
<layer number="95" name="Names" color="7" fill="1" visible="no" active="no"/>
<layer number="96" name="Values" color="7" fill="1" visible="no" active="no"/>
<layer number="97" name="Info" color="7" fill="1" visible="no" active="no"/>
<layer number="98" name="Guide" color="6" fill="1" visible="no" active="no"/>
* These layers are user layers
<layer number="160" name="???" color="7" fill="1" visible="yes" active="yes"/>
<layer number="161" name="???" color="7" fill="1" visible="yes" active="yes"/>
</layers>
*/
int kiLayer;
// eagle copper layer:
if( aEagleLayer >= 1 && aEagleLayer < int( DIM( m_cu_map ) ) )
{
kiLayer = m_cu_map[aEagleLayer];
}
else
{
// translate non-copper eagle layer to pcbnew layer
switch( aEagleLayer )
{
case 20: kiLayer = Edge_Cuts; break; // eagle says "Dimension" layer, but it's for board perimeter
case 21: kiLayer = F_SilkS; break;
case 22: kiLayer = B_SilkS; break;
case 25: kiLayer = F_SilkS; break;
case 26: kiLayer = B_SilkS; break;
case 27: kiLayer = F_SilkS; break;
case 28: kiLayer = B_SilkS; break;
case 29: kiLayer = F_Mask; break;
case 30: kiLayer = B_Mask; break;
case 31: kiLayer = F_Paste; break;
case 32: kiLayer = B_Paste; break;
case 33: kiLayer = F_Mask; break;
case 34: kiLayer = B_Mask; break;
case 35: kiLayer = F_Adhes; break;
case 36: kiLayer = B_Adhes; break;
case 49: kiLayer = Cmts_User; break;
case 50: kiLayer = Cmts_User; break;
// Packages show the future chip pins on SMD parts using layer 51.
// This is an area slightly smaller than the PAD/SMD copper area.
// Carry those visual aids into the MODULE on the drawing layer, not silkscreen.
case 51: kiLayer = Dwgs_User; break;
case 52: kiLayer = Dwgs_User; break;
// thes layers are defined as user layers. put them on ECO layers
case 160: kiLayer = Eco1_User; break;
case 161: kiLayer = Eco2_User; break;
default:
// some layers do not map to KiCad
// DBG( printf( "unsupported eagle layer: %d\n", aEagleLayer );)
kiLayer = UNDEFINED_LAYER; break;
}
}
return LAYER_ID( kiLayer );
}
void EAGLE_PLUGIN::centerBoard()
{
if( m_props )
{
UTF8 page_width;
UTF8 page_height;
if( m_props->Value( "page_width", &page_width ) &&
m_props->Value( "page_height", &page_height ) )
{
EDA_RECT bbbox = m_board->ComputeBoundingBox( true );
int w = atoi( page_width.c_str() );
int h = atoi( page_height.c_str() );
int desired_x = ( w - bbbox.GetWidth() ) / 2;
int desired_y = ( h - bbbox.GetHeight() ) / 2;
DBG(printf( "bbox.width:%d bbox.height:%d w:%d h:%d desired_x:%d desired_y:%d\n",
bbbox.GetWidth(), bbbox.GetHeight(), w, h, desired_x, desired_y );)
m_board->Move( wxPoint( desired_x - bbbox.GetX(), desired_y - bbbox.GetY() ) );
}
}
}
wxDateTime EAGLE_PLUGIN::getModificationTime( const wxString& aPath )
{
wxFileName fn( aPath );
// Do not call wxFileName::GetModificationTime() on a non-existent file, because
// if it fails, wx's implementation calls the crap wxLogSysError() which
// eventually infects our UI with an unwanted popup window, so don't let it fail.
if( !fn.IsFileReadable() )
{
wxString msg = wxString::Format(
_( "File '%s' is not readable." ),
GetChars( aPath ) );
THROW_IO_ERROR( msg );
}
/*
// update the writable flag while we have a wxFileName, in a network this
// is possibly quite dynamic anyway.
m_writable = fn.IsFileWritable();
*/
wxDateTime modTime = fn.GetModificationTime();
if( !modTime.IsValid() )
modTime.Now();
return modTime;
}
void EAGLE_PLUGIN::cacheLib( const wxString& aLibPath )
{
try
{
wxDateTime modtime = getModificationTime( aLibPath );
// Fixes assertions in wxWidgets debug builds for the wxDateTime object. Refresh the
// cache if either of the wxDateTime objects are invalid or the last file modification
// time differs from the current file modification time.
bool load = !m_mod_time.IsValid() || !modtime.IsValid() ||
m_mod_time != modtime;
if( aLibPath != m_lib_path || load )
{
PTREE doc;
LOCALE_IO toggle; // toggles on, then off, the C locale.
m_templates.clear();
// Set this before completion of loading, since we rely on it for
// text of an exception. Delay setting m_mod_time until after successful load
// however.
m_lib_path = aLibPath;
// 8 bit "filename" should be encoded according to disk filename encoding,
// (maybe this is current locale, maybe not, its a filesystem issue),
// and is not necessarily utf8.
string filename = (const char*) aLibPath.char_str( wxConvFile );
read_xml( filename, doc, xml_parser::no_comments );
// clear the cu map and then rebuild it.
clear_cu_map();
m_xpath->push( "eagle.drawing.layers" );
CPTREE& layers = doc.get_child( "eagle.drawing.layers" );
loadLayerDefs( layers );
m_xpath->pop();
m_xpath->push( "eagle.drawing.library" );
CPTREE& library = doc.get_child( "eagle.drawing.library" );
loadLibrary( library, NULL );
m_xpath->pop();
m_mod_time = modtime;
}
}
catch( file_parser_error fpe )
{
// for xml_parser_error, what() has the line number in it,
// but no byte offset. That should be an adequate error message.
THROW_IO_ERROR( fpe.what() );
}
// Class ptree_error is a base class for xml_parser_error & file_parser_error,
// so one catch should be OK for all errors.
catch( ptree_error pte )
{
string errmsg = pte.what();
errmsg += " @\n";
errmsg += m_xpath->Contents();
THROW_IO_ERROR( errmsg );
}
}
wxArrayString EAGLE_PLUGIN::FootprintEnumerate( const wxString& aLibraryPath, const PROPERTIES* aProperties )
{
init( aProperties );
cacheLib( aLibraryPath );
wxArrayString ret;
for( MODULE_CITER it = m_templates.begin(); it != m_templates.end(); ++it )
ret.Add( FROM_UTF8( it->first.c_str() ) );
return ret;
}
MODULE* EAGLE_PLUGIN::FootprintLoad( const wxString& aLibraryPath, const wxString& aFootprintName,
const PROPERTIES* aProperties )
{
init( aProperties );
cacheLib( aLibraryPath );
string key = TO_UTF8( aFootprintName );
MODULE_CITER mi = m_templates.find( key );
if( mi == m_templates.end() )
return NULL;
// copy constructor to clone the template
MODULE* ret = new MODULE( *mi->second );
return ret;
}
void EAGLE_PLUGIN::FootprintLibOptions( PROPERTIES* aListToAppendTo ) const
{
PLUGIN::FootprintLibOptions( aListToAppendTo );
/*
(*aListToAppendTo)["ignore_duplicates"] = UTF8( _(
"Ignore duplicately named footprints within the same Eagle library. "
"Only the first similarly named footprint will be loaded."
));
*/
}
/*
void EAGLE_PLUGIN::Save( const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties )
{
// Eagle lovers apply here.
}
void EAGLE_PLUGIN::FootprintSave( const wxString& aLibraryPath, const MODULE* aFootprint, const PROPERTIES* aProperties )
{
}
void EAGLE_PLUGIN::FootprintDelete( const wxString& aLibraryPath, const wxString& aFootprintName )
{
}
void EAGLE_PLUGIN::FootprintLibCreate( const wxString& aLibraryPath, const PROPERTIES* aProperties )
{
}
bool EAGLE_PLUGIN::FootprintLibDelete( const wxString& aLibraryPath, const PROPERTIES* aProperties )
{
}
bool EAGLE_PLUGIN::IsFootprintLibWritable( const wxString& aLibraryPath )
{
return true;
}
*/
|