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
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2014-2017 CERN
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
* @author Tomasz Włostowski <tomasz.wlostowski@cern.ch>
*
* 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 3 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
*/
#include <future>
#include <core/kicad_algo.h>
#include <advanced_config.h>
#include <board.h>
#include <board_design_settings.h>
#include <zone.h>
#include <footprint.h>
#include <pad.h>
#include <pcb_target.h>
#include <pcb_track.h>
#include <pcb_text.h>
#include <pcb_textbox.h>
#include <pcb_tablecell.h>
#include <pcb_table.h>
#include <pcb_dimension.h>
#include <connectivity/connectivity_data.h>
#include <convert_basic_shapes_to_polygon.h>
#include <board_commit.h>
#include <progress_reporter.h>
#include <geometry/shape_poly_set.h>
#include <geometry/convex_hull.h>
#include <geometry/geometry_utils.h>
#include <geometry/vertex_set.h>
#include <kidialog.h>
#include <thread_pool.h>
#include <math/util.h> // for KiROUND
#include "zone_filler.h"
// Helper classes for connect_nearby_polys
class RESULTS
{
public:
RESULTS( int aOutline1, int aOutline2, int aVertex1, int aVertex2 ) :
m_outline1( aOutline1 ), m_outline2( aOutline2 ),
m_vertex1( aVertex1 ), m_vertex2( aVertex2 )
{
}
bool operator<( const RESULTS& aOther ) const
{
if( m_outline1 != aOther.m_outline1 )
return m_outline1 < aOther.m_outline1;
if( m_outline2 != aOther.m_outline2 )
return m_outline2 < aOther.m_outline2;
if( m_vertex1 != aOther.m_vertex1 )
return m_vertex1 < aOther.m_vertex1;
return m_vertex2 < aOther.m_vertex2;
}
int m_outline1;
int m_outline2;
int m_vertex1;
int m_vertex2;
};
class VERTEX_CONNECTOR : protected VERTEX_SET
{
public:
VERTEX_CONNECTOR( const BOX2I& aBBox, const SHAPE_POLY_SET& aPolys, int aDist ) : VERTEX_SET( 0 )
{
SetBoundingBox( aBBox );
VERTEX* tail = nullptr;
for( int i = 0; i < aPolys.OutlineCount(); i++ )
tail = createList( aPolys.Outline( i ), tail, (void*)( intptr_t )( i ) );
if( tail )
tail->updateList();
m_dist = aDist;
}
VERTEX* getPoint( VERTEX* aPt ) const
{
// z-order range for the current point ± limit bounding box
const uint32_t maxZ = zOrder( aPt->x + m_dist, aPt->y + m_dist );
const uint32_t minZ = zOrder( aPt->x - m_dist, aPt->y - m_dist );
const SEG::ecoord limit2 = SEG::Square( m_dist );
// first look for points in increasing z-order
SEG::ecoord min_dist = std::numeric_limits<SEG::ecoord>::max();
VERTEX* retval = nullptr;
auto check_pt = [&]( VERTEX* p )
{
VECTOR2D diff( p->x - aPt->x, p->y - aPt->y );
SEG::ecoord dist2 = diff.SquaredEuclideanNorm();
if( dist2 > 0 && dist2 < limit2 && dist2 < min_dist && p->isEar( true ) )
{
min_dist = dist2;
retval = p;
}
};
VERTEX* p = aPt->nextZ;
while( p && p->z <= maxZ )
{
check_pt( p );
p = p->nextZ;
}
p = aPt->prevZ;
while( p && p->z >= minZ )
{
check_pt( p );
p = p->prevZ;
}
return retval;
}
void FindResults()
{
if( m_vertices.empty() )
return;
VERTEX* p = m_vertices.front().next;
std::set<VERTEX*> visited;
while( p != &m_vertices.front() )
{
// Skip points that are concave
if( !p->isEar() )
{
p = p->next;
continue;
}
VERTEX* q = nullptr;
if( ( visited.empty() || !visited.contains( p ) ) && ( q = getPoint( p ) ) )
{
visited.insert( p );
if( !visited.contains( q ) &&
m_results.emplace( (intptr_t) p->GetUserData(), (intptr_t) q->GetUserData(),
p->i, q->i ).second )
{
// We don't want to connect multiple points in the same vicinity, so skip
// 2 points before and after each point and match.
visited.insert( p->prev );
visited.insert( p->prev->prev );
visited.insert( p->next );
visited.insert( p->next->next );
visited.insert( q->prev );
visited.insert( q->prev->prev );
visited.insert( q->next );
visited.insert( q->next->next );
visited.insert( q );
}
}
p = p->next;
}
}
std::set<RESULTS> GetResults() const
{
return m_results;
}
private:
std::set<RESULTS> m_results;
int m_dist;
};
ZONE_FILLER::ZONE_FILLER( BOARD* aBoard, COMMIT* aCommit ) :
m_board( aBoard ),
m_brdOutlinesValid( false ),
m_commit( aCommit ),
m_progressReporter( nullptr ),
m_maxError( ARC_HIGH_DEF ),
m_worstClearance( 0 )
{
// To enable add "DebugZoneFiller=1" to kicad_advanced settings file.
m_debugZoneFiller = ADVANCED_CFG::GetCfg().m_DebugZoneFiller;
}
ZONE_FILLER::~ZONE_FILLER()
{
}
void ZONE_FILLER::SetProgressReporter( PROGRESS_REPORTER* aReporter )
{
m_progressReporter = aReporter;
wxASSERT_MSG( m_commit, wxT( "ZONE_FILLER must have a valid commit to call "
"SetProgressReporter" ) );
}
/**
* Fills the given list of zones.
*
* NB: Invalidates connectivity - it is up to the caller to obtain a lock on the connectivity
* data before calling Fill to prevent access to stale data by other coroutines (for example,
* ratsnest redraw). This will generally be required if a UI-based progress reporter has been
* installed.
*
* Caller is also responsible for re-building connectivity afterwards.
*/
bool ZONE_FILLER::Fill( const std::vector<ZONE*>& aZones, bool aCheck, wxWindow* aParent )
{
std::lock_guard<KISPINLOCK> lock( m_board->GetConnectivity()->GetLock() );
std::vector<std::pair<ZONE*, PCB_LAYER_ID>> toFill;
std::map<std::pair<ZONE*, PCB_LAYER_ID>, HASH_128> oldFillHashes;
std::map<ZONE*, std::map<PCB_LAYER_ID, ISOLATED_ISLANDS>> isolatedIslandsMap;
std::shared_ptr<CONNECTIVITY_DATA> connectivity = m_board->GetConnectivity();
// Rebuild (from scratch, ignoring dirty flags) just in case. This really needs to be reliable.
connectivity->ClearRatsnest();
connectivity->Build( m_board, m_progressReporter );
m_worstClearance = m_board->GetMaxClearanceValue();
if( m_progressReporter )
{
m_progressReporter->Report( aCheck ? _( "Checking zone fills..." )
: _( "Building zone fills..." ) );
m_progressReporter->SetMaxProgress( aZones.size() );
m_progressReporter->KeepRefreshing();
}
// The board outlines is used to clip solid areas inside the board (when outlines are valid)
m_boardOutline.RemoveAllContours();
m_brdOutlinesValid = m_board->GetBoardPolygonOutlines( m_boardOutline );
// Update and cache zone bounding boxes and pad effective shapes so that we don't have to
// make them thread-safe.
//
for( ZONE* zone : m_board->Zones() )
zone->CacheBoundingBox();
for( FOOTPRINT* footprint : m_board->Footprints() )
{
for( PAD* pad : footprint->Pads() )
{
if( pad->IsDirty() )
{
pad->BuildEffectiveShapes();
pad->BuildEffectivePolygon( ERROR_OUTSIDE );
}
}
for( ZONE* zone : footprint->Zones() )
zone->CacheBoundingBox();
// Rules may depend on insideCourtyard() or other expressions
footprint->BuildCourtyardCaches();
footprint->BuildNetTieCache();
}
LSET boardCuMask = m_board->GetEnabledLayers() & LSET::AllCuMask();
auto findHighestPriorityZone =
[&]( const BOX2I& bbox, PCB_LAYER_ID itemLayer, int netcode,
const std::function<bool( const ZONE* )>& testFn ) -> ZONE*
{
unsigned highestPriority = 0;
ZONE* highestPriorityZone = nullptr;
for( ZONE* zone : m_board->Zones() )
{
// Rule areas are not filled
if( zone->GetIsRuleArea() )
continue;
if( zone->GetAssignedPriority() < highestPriority )
continue;
if( !zone->IsOnLayer( itemLayer ) )
continue;
// Degenerate zones will cause trouble; skip them
if( zone->GetNumCorners() <= 2 )
continue;
if( !zone->GetBoundingBox().Intersects( bbox ) )
continue;
if( !testFn( zone ) )
continue;
// Prefer highest priority and matching netcode
if( zone->GetAssignedPriority() > highestPriority
|| zone->GetNetCode() == netcode )
{
highestPriority = zone->GetAssignedPriority();
highestPriorityZone = zone;
}
}
return highestPriorityZone;
};
auto isInPourKeepoutArea =
[&]( const BOX2I& bbox, PCB_LAYER_ID itemLayer, const VECTOR2I& testPoint ) -> bool
{
for( ZONE* zone : m_board->Zones() )
{
if( !zone->GetIsRuleArea() )
continue;
if( !zone->HasKeepoutParametersSet() )
continue;
if( !zone->GetDoNotAllowCopperPour() )
continue;
if( !zone->IsOnLayer( itemLayer ) )
continue;
// Degenerate zones will cause trouble; skip them
if( zone->GetNumCorners() <= 2 )
continue;
if( !zone->GetBoundingBox().Intersects( bbox ) )
continue;
if( zone->Outline()->Contains( testPoint ) )
return true;
}
return false;
};
// Determine state of conditional via flashing
// This is now done completely deterministically prior to filling due to the pathological
// case presented in https://gitlab.com/kicad/code/kicad/-/issues/12964.
for( PCB_TRACK* track : m_board->Tracks() )
{
if( track->Type() == PCB_VIA_T )
{
PCB_VIA* via = static_cast<PCB_VIA*>( track );
via->ClearZoneLayerOverrides();
if( !via->GetRemoveUnconnected() )
continue;
BOX2I bbox = via->GetBoundingBox();
VECTOR2I center = via->GetPosition();
int holeRadius = via->GetDrillValue() / 2 + 1;
int netcode = via->GetNetCode();
LSET layers = via->GetLayerSet() & boardCuMask;
// Checking if the via hole touches the zone outline
auto viaTestFn =
[&]( const ZONE* aZone ) -> bool
{
return aZone->Outline()->Contains( center, -1, holeRadius );
};
for( PCB_LAYER_ID layer : layers.Seq() )
{
if( !via->ConditionallyFlashed( layer ) )
continue;
if( isInPourKeepoutArea( bbox, layer, center ) )
{
via->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
}
else
{
ZONE* zone = findHighestPriorityZone( bbox, layer, netcode, viaTestFn );
if( zone && zone->GetNetCode() == via->GetNetCode() )
via->SetZoneLayerOverride( layer, ZLO_FORCE_FLASHED );
else
via->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
}
}
}
}
// Determine state of conditional pad flashing
for( FOOTPRINT* footprint : m_board->Footprints() )
{
for( PAD* pad : footprint->Pads() )
{
pad->ClearZoneLayerOverrides();
if( !pad->GetRemoveUnconnected() )
continue;
BOX2I bbox = pad->GetBoundingBox();
VECTOR2I center = pad->GetPosition();
int netcode = pad->GetNetCode();
LSET layers = pad->GetLayerSet() & boardCuMask;
auto padTestFn =
[&]( const ZONE* aZone ) -> bool
{
return aZone->Outline()->Contains( center );
};
for( PCB_LAYER_ID layer : layers.Seq() )
{
if( !pad->ConditionallyFlashed( layer ) )
continue;
if( isInPourKeepoutArea( bbox, layer, center ) )
{
pad->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
}
else
{
ZONE* zone = findHighestPriorityZone( bbox, layer, netcode, padTestFn );
if( zone && zone->GetNetCode() == pad->GetNetCode() )
pad->SetZoneLayerOverride( layer, ZLO_FORCE_FLASHED );
else
pad->SetZoneLayerOverride( layer, ZLO_FORCE_NO_ZONE_CONNECTION );
}
}
}
}
for( ZONE* zone : aZones )
{
// Rule areas are not filled
if( zone->GetIsRuleArea() )
continue;
// Degenerate zones will cause trouble; skip them
if( zone->GetNumCorners() <= 2 )
continue;
if( m_commit )
m_commit->Modify( zone );
// calculate the hash value for filled areas. it will be used later to know if the
// current filled areas are up to date
for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
{
zone->BuildHashValue( layer );
oldFillHashes[ { zone, layer } ] = zone->GetHashValue( layer );
// Add the zone to the list of zones to test or refill
toFill.emplace_back( std::make_pair( zone, layer ) );
isolatedIslandsMap[ zone ][ layer ] = ISOLATED_ISLANDS();
}
// Remove existing fill first to prevent drawing invalid polygons on some platforms
zone->UnFill();
}
auto check_fill_dependency =
[&]( ZONE* aZone, PCB_LAYER_ID aLayer, ZONE* aOtherZone ) -> bool
{
// Check to see if we have to knock-out the filled areas of a higher-priority
// zone. If so we have to wait until said zone is filled before we can fill.
// If the other zone is already filled on the requested layer then we're
// good-to-go
if( aOtherZone->GetFillFlag( aLayer ) )
return false;
// Even if keepouts exclude copper pours, the exclusion is by outline rather than
// filled area, so we're good-to-go here too
if( aOtherZone->GetIsRuleArea() )
return false;
// If the other zone is never going to be filled then don't wait for it
if( aOtherZone->GetNumCorners() <= 2 )
return false;
// If the zones share no common layers
if( !aOtherZone->GetLayerSet().test( aLayer ) )
return false;
if( aZone->HigherPriority( aOtherZone ) )
return false;
// Same-net zones always use outlines to produce determinate results
if( aOtherZone->SameNet( aZone ) )
return false;
// A higher priority zone is found: if we intersect and it's not filled yet
// then we have to wait.
BOX2I inflatedBBox = aZone->GetBoundingBox();
inflatedBBox.Inflate( m_worstClearance );
if( !inflatedBBox.Intersects( aOtherZone->GetBoundingBox() ) )
return false;
return aZone->Outline()->Collide( aOtherZone->Outline(), m_worstClearance );
};
auto fill_lambda =
[&]( std::pair<ZONE*, PCB_LAYER_ID> aFillItem ) -> int
{
PCB_LAYER_ID layer = aFillItem.second;
ZONE* zone = aFillItem.first;
bool canFill = true;
// Check for any fill dependencies. If our zone needs to be clipped by
// another zone then we can't fill until that zone is filled.
for( ZONE* otherZone : aZones )
{
if( otherZone == zone )
continue;
if( check_fill_dependency( zone, layer, otherZone ) )
{
canFill = false;
break;
}
}
if( m_progressReporter && m_progressReporter->IsCancelled() )
return 0;
if( !canFill )
return 0;
// Now we're ready to fill.
{
std::unique_lock<std::mutex> zoneLock( zone->GetLock(), std::try_to_lock );
if( !zoneLock.owns_lock() )
return 0;
SHAPE_POLY_SET fillPolys;
if( !fillSingleZone( zone, layer, fillPolys ) )
return 0;
zone->SetFilledPolysList( layer, fillPolys );
}
if( m_progressReporter )
m_progressReporter->AdvanceProgress();
return 1;
};
auto tesselate_lambda =
[&]( std::pair<ZONE*, PCB_LAYER_ID> aFillItem ) -> int
{
if( m_progressReporter && m_progressReporter->IsCancelled() )
return 0;
PCB_LAYER_ID layer = aFillItem.second;
ZONE* zone = aFillItem.first;
{
std::unique_lock<std::mutex> zoneLock( zone->GetLock(), std::try_to_lock );
if( !zoneLock.owns_lock() )
return 0;
zone->CacheTriangulation( layer );
zone->SetFillFlag( layer, true );
}
return 1;
};
// Calculate the copper fills (NB: this is multi-threaded)
//
std::vector<std::pair<std::future<int>, int>> returns;
returns.reserve( toFill.size() );
size_t finished = 0;
bool cancelled = false;
thread_pool& tp = GetKiCadThreadPool();
for( const std::pair<ZONE*, PCB_LAYER_ID>& fillItem : toFill )
returns.emplace_back( std::make_pair( tp.submit( fill_lambda, fillItem ), 0 ) );
while( !cancelled && finished != 2 * toFill.size() )
{
for( size_t ii = 0; ii < returns.size(); ++ii )
{
auto& ret = returns[ii];
if( ret.second > 1 )
continue;
std::future_status status = ret.first.wait_for( std::chrono::seconds( 0 ) );
if( status == std::future_status::ready )
{
if( ret.first.get() ) // lambda completed
{
++finished;
ret.second++; // go to next step
}
if( !cancelled )
{
// Queue the next step (will re-queue the existing step if it didn't complete)
if( ret.second == 0 )
returns[ii].first = tp.submit( fill_lambda, toFill[ii] );
else if( ret.second == 1 )
returns[ii].first = tp.submit( tesselate_lambda, toFill[ii] );
}
}
}
std::this_thread::sleep_for( std::chrono::milliseconds( 100 ) );
if( m_progressReporter )
{
m_progressReporter->KeepRefreshing();
if( m_progressReporter->IsCancelled() )
cancelled = true;
}
}
// Make sure that all futures have finished.
// This can happen when the user cancels the above operation
for( auto& ret : returns )
{
if( ret.first.valid() )
{
std::future_status status = ret.first.wait_for( std::chrono::seconds( 0 ) );
while( status != std::future_status::ready )
{
if( m_progressReporter )
m_progressReporter->KeepRefreshing();
status = ret.first.wait_for( std::chrono::milliseconds( 100 ) );
}
}
}
// Now update the connectivity to check for isolated copper islands
// (NB: FindIsolatedCopperIslands() is multi-threaded)
//
if( m_progressReporter )
{
if( m_progressReporter->IsCancelled() )
return false;
m_progressReporter->AdvancePhase();
m_progressReporter->Report( _( "Removing isolated copper islands..." ) );
m_progressReporter->KeepRefreshing();
}
connectivity->SetProgressReporter( m_progressReporter );
connectivity->FillIsolatedIslandsMap( isolatedIslandsMap );
connectivity->SetProgressReporter( nullptr );
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
for( ZONE* zone : aZones )
{
// Keepout zones are not filled
if( zone->GetIsRuleArea() )
continue;
zone->SetIsFilled( true );
}
// Now remove isolated copper islands according to the isolated islands strategy assigned
// by the user (always, never, below-certain-size).
//
for( const auto& [ zone, zoneIslands ] : isolatedIslandsMap )
{
// If *all* the polygons are islands, do not remove any of them
bool allIslands = true;
for( const auto& [ layer, layerIslands ] : zoneIslands )
{
if( layerIslands.m_IsolatedOutlines.size()
!= static_cast<size_t>( zone->GetFilledPolysList( layer )->OutlineCount() ) )
{
allIslands = false;
break;
}
}
if( allIslands )
continue;
for( const auto& [ layer, layerIslands ] : zoneIslands )
{
if( m_debugZoneFiller && LSET::InternalCuMask().Contains( layer ) )
continue;
if( layerIslands.m_IsolatedOutlines.empty() )
continue;
std::vector<int> islands = layerIslands.m_IsolatedOutlines;
// The list of polygons to delete must be explored from last to first in list,
// to allow deleting a polygon from list without breaking the remaining of the list
std::sort( islands.begin(), islands.end(), std::greater<int>() );
std::shared_ptr<SHAPE_POLY_SET> poly = zone->GetFilledPolysList( layer );
long long int minArea = zone->GetMinIslandArea();
ISLAND_REMOVAL_MODE mode = zone->GetIslandRemovalMode();
for( int idx : islands )
{
SHAPE_LINE_CHAIN& outline = poly->Outline( idx );
if( mode == ISLAND_REMOVAL_MODE::ALWAYS )
poly->DeletePolygonAndTriangulationData( idx, false );
else if ( mode == ISLAND_REMOVAL_MODE::AREA && outline.Area( true ) < minArea )
poly->DeletePolygonAndTriangulationData( idx, false );
else
zone->SetIsIsland( layer, idx );
}
poly->UpdateTriangulationDataHash();
zone->CalculateFilledArea();
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
}
}
// Now remove islands which are either outside the board edge or fail to meet the minimum
// area requirements
using island_check_return = std::vector<std::pair<std::shared_ptr<SHAPE_POLY_SET>, int>>;
std::vector<std::pair<std::shared_ptr<SHAPE_POLY_SET>, double>> polys_to_check;
// rough estimate to save re-allocation time
polys_to_check.reserve( m_board->GetCopperLayerCount() * aZones.size() );
for( ZONE* zone : aZones )
{
// Don't check for connections on layers that only exist in the zone but
// were disabled in the board
BOARD* board = zone->GetBoard();
LSET zoneCopperLayers = zone->GetLayerSet() & LSET::AllCuMask() & board->GetEnabledLayers();
// Min-thickness is the web thickness. On the other hand, a blob min-thickness by
// min-thickness is not useful. Since there's no obvious definition of web vs. blob, we
// arbitrarily choose "at least 3X the area".
double minArea = (double) zone->GetMinThickness() * zone->GetMinThickness() * 3;
for( PCB_LAYER_ID layer : zoneCopperLayers.Seq() )
{
if( m_debugZoneFiller && LSET::InternalCuMask().Contains( layer ) )
continue;
polys_to_check.emplace_back( zone->GetFilledPolysList( layer ), minArea );
}
}
auto island_lambda =
[&]( int aStart, int aEnd ) -> island_check_return
{
island_check_return retval;
for( int ii = aStart; ii < aEnd && !cancelled; ++ii )
{
auto [poly, minArea] = polys_to_check[ii];
for( int jj = poly->OutlineCount() - 1; jj >= 0; jj-- )
{
SHAPE_POLY_SET island;
SHAPE_POLY_SET intersection;
const SHAPE_LINE_CHAIN& test_poly = poly->Polygon( jj ).front();
double island_area = test_poly.Area();
if( island_area < minArea )
continue;
island.AddOutline( test_poly );
intersection.BooleanIntersection( m_boardOutline, island );
// Nominally, all of these areas should be either inside or outside the
// board outline. So this test should be able to just compare areas (if
// they are equal, you are inside). But in practice, we sometimes have
// slight overlap at the edges, so testing against half-size area acts as
// a fail-safe.
if( intersection.Area() < island_area / 2.0 )
retval.emplace_back( poly, jj );
}
}
return retval;
};
auto island_returns = tp.parallelize_loop( 0, polys_to_check.size(), island_lambda );
cancelled = false;
// Allow island removal threads to finish
for( size_t ii = 0; ii < island_returns.size(); ++ii )
{
std::future<island_check_return>& ret = island_returns[ii];
if( ret.valid() )
{
std::future_status status = ret.wait_for( std::chrono::seconds( 0 ) );
while( status != std::future_status::ready )
{
if( m_progressReporter )
{
m_progressReporter->KeepRefreshing();
if( m_progressReporter->IsCancelled() )
cancelled = true;
}
status = ret.wait_for( std::chrono::milliseconds( 100 ) );
}
}
}
if( cancelled )
return false;
for( size_t ii = 0; ii < island_returns.size(); ++ii )
{
std::future<island_check_return>& ret = island_returns[ii];
if( ret.valid() )
{
for( auto& action_item : ret.get() )
action_item.first->DeletePolygonAndTriangulationData( action_item.second, true );
}
}
for( ZONE* zone : aZones )
zone->CalculateFilledArea();
if( aCheck )
{
bool outOfDate = false;
for( ZONE* zone : aZones )
{
// Keepout zones are not filled
if( zone->GetIsRuleArea() )
continue;
for( PCB_LAYER_ID layer : zone->GetLayerSet().Seq() )
{
zone->BuildHashValue( layer );
if( oldFillHashes[ { zone, layer } ] != zone->GetHashValue( layer ) )
outOfDate = true;
}
}
if( outOfDate )
{
KIDIALOG dlg( aParent, _( "Zone fills are out-of-date. Refill?" ),
_( "Confirmation" ), wxOK | wxCANCEL | wxICON_WARNING );
dlg.SetOKCancelLabels( _( "Refill" ), _( "Continue without Refill" ) );
dlg.DoNotShowCheckbox( __FILE__, __LINE__ );
if( dlg.ShowModal() == wxID_CANCEL )
return false;
}
else
{
// No need to commit something that hasn't changed (and committing will set
// the modified flag).
return false;
}
}
if( m_progressReporter )
{
if( m_progressReporter->IsCancelled() )
return false;
m_progressReporter->AdvancePhase();
m_progressReporter->KeepRefreshing();
}
return true;
}
/**
* Add a knockout for a pad. The knockout is 'aGap' larger than the pad (which might be
* either the thermal clearance or the electrical clearance).
*/
void ZONE_FILLER::addKnockout( PAD* aPad, PCB_LAYER_ID aLayer, int aGap, SHAPE_POLY_SET& aHoles )
{
if( aPad->GetShape( aLayer ) == PAD_SHAPE::CUSTOM )
{
SHAPE_POLY_SET poly;
aPad->TransformShapeToPolygon( poly, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
// the pad shape in zone can be its convex hull or the shape itself
if( aPad->GetCustomShapeInZoneOpt() == PADSTACK::CUSTOM_SHAPE_ZONE_MODE::CONVEXHULL )
{
std::vector<VECTOR2I> convex_hull;
BuildConvexHull( convex_hull, poly );
aHoles.NewOutline();
for( const VECTOR2I& pt : convex_hull )
aHoles.Append( pt );
}
else
aHoles.Append( poly );
}
else
{
aPad->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
}
}
/**
* Add a knockout for a pad's hole.
*/
void ZONE_FILLER::addHoleKnockout( PAD* aPad, int aGap, SHAPE_POLY_SET& aHoles )
{
aPad->TransformHoleToPolygon( aHoles, aGap, m_maxError, ERROR_OUTSIDE );
}
/**
* Add a knockout for a graphic item. The knockout is 'aGap' larger than the item (which
* might be either the electrical clearance or the board edge clearance).
*/
void ZONE_FILLER::addKnockout( BOARD_ITEM* aItem, PCB_LAYER_ID aLayer, int aGap,
bool aIgnoreLineWidth, SHAPE_POLY_SET& aHoles )
{
switch( aItem->Type() )
{
case PCB_FIELD_T:
case PCB_TEXT_T:
{
PCB_TEXT* text = static_cast<PCB_TEXT*>( aItem );
if( text->IsVisible() )
{
if( text->IsKnockout() )
{
// Knockout text should only leave holes where the text is, not where the copper fill
// around it would be.
PCB_TEXT textCopy = *text;
textCopy.SetIsKnockout( false );
textCopy.TransformShapeToPolygon( aHoles, aLayer, 0, m_maxError, ERROR_OUTSIDE );
}
else
{
text->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
}
}
break;
}
case PCB_TEXTBOX_T:
case PCB_TABLE_T:
case PCB_SHAPE_T:
case PCB_TARGET_T:
aItem->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE,
aIgnoreLineWidth );
break;
case PCB_DIM_ALIGNED_T:
case PCB_DIM_LEADER_T:
case PCB_DIM_CENTER_T:
case PCB_DIM_RADIAL_T:
case PCB_DIM_ORTHOGONAL_T:
{
PCB_DIMENSION_BASE* dim = static_cast<PCB_DIMENSION_BASE*>( aItem );
dim->TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE, false );
dim->PCB_TEXT::TransformShapeToPolygon( aHoles, aLayer, aGap, m_maxError, ERROR_OUTSIDE );
break;
}
default:
break;
}
}
/**
* Removes thermal reliefs from the shape for any pads connected to the zone. Does NOT add
* in spokes, which must be done later.
*/
void ZONE_FILLER::knockoutThermalReliefs( const ZONE* aZone, PCB_LAYER_ID aLayer,
SHAPE_POLY_SET& aFill,
std::vector<PAD*>& aThermalConnectionPads,
std::vector<PAD*>& aNoConnectionPads )
{
BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
ZONE_CONNECTION connection;
DRC_CONSTRAINT constraint;
int padClearance;
std::shared_ptr<SHAPE> padShape;
int holeClearance;
SHAPE_POLY_SET holes;
for( FOOTPRINT* footprint : m_board->Footprints() )
{
for( PAD* pad : footprint->Pads() )
{
BOX2I padBBox = pad->GetBoundingBox();
padBBox.Inflate( m_worstClearance );
if( !padBBox.Intersects( aZone->GetBoundingBox() ) )
continue;
bool noConnection = pad->GetNetCode() != aZone->GetNetCode();
if( !aZone->IsTeardropArea() )
{
if( aZone->GetNetCode() == 0
|| pad->GetZoneLayerOverride( aLayer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
{
noConnection = true;
}
}
if( noConnection )
{
// collect these for knockout in buildCopperItemClearances()
aNoConnectionPads.push_back( pad );
continue;
}
if( aZone->IsTeardropArea() )
{
connection = ZONE_CONNECTION::FULL;
}
else
{
constraint = bds.m_DRCEngine->EvalZoneConnection( pad, aZone, aLayer );
connection = constraint.m_ZoneConnection;
}
if( connection == ZONE_CONNECTION::THERMAL && !pad->CanFlashLayer( aLayer ) )
connection = ZONE_CONNECTION::NONE;
switch( connection )
{
case ZONE_CONNECTION::THERMAL:
padShape = pad->GetEffectiveShape( aLayer, FLASHING::ALWAYS_FLASHED );
if( aFill.Collide( padShape.get(), 0 ) )
{
constraint = bds.m_DRCEngine->EvalRules( THERMAL_RELIEF_GAP_CONSTRAINT, pad,
aZone, aLayer );
padClearance = constraint.GetValue().Min();
aThermalConnectionPads.push_back( pad );
addKnockout( pad, aLayer, padClearance, holes );
}
break;
case ZONE_CONNECTION::NONE:
constraint = bds.m_DRCEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT, pad,
aZone, aLayer );
if( constraint.GetValue().Min() > aZone->GetLocalClearance().value() )
padClearance = constraint.GetValue().Min();
else
padClearance = aZone->GetLocalClearance().value();
if( pad->FlashLayer( aLayer ) )
{
addKnockout( pad, aLayer, padClearance, holes );
}
else if( pad->GetDrillSize().x > 0 )
{
constraint = bds.m_DRCEngine->EvalRules( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT,
pad, aZone, aLayer );
if( constraint.GetValue().Min() > padClearance )
holeClearance = constraint.GetValue().Min();
else
holeClearance = padClearance;
pad->TransformHoleToPolygon( holes, holeClearance, m_maxError, ERROR_OUTSIDE );
}
break;
default:
// No knockout
continue;
}
}
}
aFill.BooleanSubtract( holes );
}
/**
* Removes clearance from the shape for copper items which share the zone's layer but are
* not connected to it.
*/
void ZONE_FILLER::buildCopperItemClearances( const ZONE* aZone, PCB_LAYER_ID aLayer,
const std::vector<PAD*>& aNoConnectionPads,
SHAPE_POLY_SET& aHoles )
{
BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
long ticker = 0;
auto checkForCancel =
[&ticker]( PROGRESS_REPORTER* aReporter ) -> bool
{
return aReporter && ( ticker++ % 50 ) == 0 && aReporter->IsCancelled();
};
// A small extra clearance to be sure actual track clearances are not smaller than
// requested clearance due to many approximations in calculations, like arc to segment
// approx, rounding issues, etc.
BOX2I zone_boundingbox = aZone->GetBoundingBox();
int extra_margin = pcbIUScale.mmToIU( ADVANCED_CFG::GetCfg().m_ExtraClearance );
// Items outside the zone bounding box are skipped, so it needs to be inflated by the
// largest clearance value found in the netclasses and rules
zone_boundingbox.Inflate( m_worstClearance + extra_margin );
auto evalRulesForItems =
[&bds]( DRC_CONSTRAINT_T aConstraint, const BOARD_ITEM* a, const BOARD_ITEM* b,
PCB_LAYER_ID aEvalLayer ) -> int
{
DRC_CONSTRAINT c = bds.m_DRCEngine->EvalRules( aConstraint, a, b, aEvalLayer );
if( c.IsNull() )
return -1;
else
return c.GetValue().Min();
};
// Add non-connected pad clearances
//
auto knockoutPadClearance =
[&]( PAD* aPad )
{
int init_gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT, aZone, aPad, aLayer );
int gap = init_gap;
bool hasHole = aPad->GetDrillSize().x > 0;
bool flashLayer = aPad->FlashLayer( aLayer );
bool platedHole = hasHole && aPad->GetAttribute() == PAD_ATTRIB::PTH;
if( flashLayer || platedHole )
{
gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
aZone, aPad, aLayer ) );
}
if( flashLayer && gap >= 0 )
addKnockout( aPad, aLayer, gap + extra_margin, aHoles );
if( hasHole )
{
// NPTH do not need copper clearance gaps to their holes
if( aPad->GetAttribute() == PAD_ATTRIB::NPTH )
gap = init_gap;
gap = std::max( gap, evalRulesForItems( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT,
aZone, aPad, aLayer ) );
gap = std::max( gap, evalRulesForItems( HOLE_CLEARANCE_CONSTRAINT,
aZone, aPad, aLayer ) );
if( gap >= 0 )
addHoleKnockout( aPad, gap + extra_margin, aHoles );
}
};
for( PAD* pad : aNoConnectionPads )
{
if( checkForCancel( m_progressReporter ) )
return;
knockoutPadClearance( pad );
}
// Add non-connected track clearances
//
auto knockoutTrackClearance =
[&]( PCB_TRACK* aTrack )
{
if( aTrack->GetBoundingBox().Intersects( zone_boundingbox ) )
{
bool sameNet = aTrack->GetNetCode() == aZone->GetNetCode();
if( !aZone->IsTeardropArea() && aZone->GetNetCode() == 0 )
sameNet = false;
int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT,
aZone, aTrack, aLayer );
if( aTrack->Type() == PCB_VIA_T )
{
PCB_VIA* via = static_cast<PCB_VIA*>( aTrack );
if( via->GetZoneLayerOverride( aLayer ) == ZLO_FORCE_NO_ZONE_CONNECTION )
sameNet = false;
}
if( !sameNet )
{
gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
aZone, aTrack, aLayer ) );
}
if( aTrack->Type() == PCB_VIA_T )
{
PCB_VIA* via = static_cast<PCB_VIA*>( aTrack );
if( via->FlashLayer( aLayer ) && gap > 0 )
{
via->TransformShapeToPolygon( aHoles, aLayer, gap + extra_margin,
m_maxError, ERROR_OUTSIDE );
}
gap = std::max( gap, evalRulesForItems( PHYSICAL_HOLE_CLEARANCE_CONSTRAINT,
aZone, via, aLayer ) );
if( !sameNet )
{
gap = std::max( gap, evalRulesForItems( HOLE_CLEARANCE_CONSTRAINT,
aZone, via, aLayer ) );
}
if( gap >= 0 )
{
int radius = via->GetDrillValue() / 2;
TransformCircleToPolygon( aHoles, via->GetPosition(),
radius + gap + extra_margin,
m_maxError, ERROR_OUTSIDE );
}
}
else
{
if( gap >= 0 )
{
aTrack->TransformShapeToPolygon( aHoles, aLayer, gap + extra_margin,
m_maxError, ERROR_OUTSIDE );
}
}
}
};
for( PCB_TRACK* track : m_board->Tracks() )
{
if( !track->IsOnLayer( aLayer ) )
continue;
if( checkForCancel( m_progressReporter ) )
return;
knockoutTrackClearance( track );
}
// Add graphic item clearances.
//
auto knockoutGraphicClearance =
[&]( BOARD_ITEM* aItem )
{
int shapeNet = -1;
if( aItem->Type() == PCB_SHAPE_T )
shapeNet = static_cast<PCB_SHAPE*>( aItem )->GetNetCode();
bool sameNet = shapeNet == aZone->GetNetCode();
if( !aZone->IsTeardropArea() && aZone->GetNetCode() == 0 )
sameNet = false;
// A item on the Edge_Cuts or Margin is always seen as on any layer:
if( aItem->IsOnLayer( aLayer )
|| aItem->IsOnLayer( Edge_Cuts )
|| aItem->IsOnLayer( Margin ) )
{
if( aItem->GetBoundingBox().Intersects( zone_boundingbox ) )
{
bool ignoreLineWidths = false;
int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT,
aZone, aItem, aLayer );
if( aItem->IsOnLayer( aLayer ) && !sameNet )
{
gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
aZone, aItem, aLayer ) );
}
else if( aItem->IsOnLayer( Edge_Cuts ) )
{
gap = std::max( gap, evalRulesForItems( EDGE_CLEARANCE_CONSTRAINT,
aZone, aItem, aLayer ) );
ignoreLineWidths = true;
}
else if( aItem->IsOnLayer( Margin ) )
{
gap = std::max( gap, evalRulesForItems( EDGE_CLEARANCE_CONSTRAINT,
aZone, aItem, aLayer ) );
}
if( gap >= 0 )
{
gap += extra_margin;
addKnockout( aItem, aLayer, gap, ignoreLineWidths, aHoles );
}
}
}
};
auto knockoutCourtyardClearance =
[&]( FOOTPRINT* aFootprint )
{
if( aFootprint->GetBoundingBox().Intersects( zone_boundingbox ) )
{
int gap = evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT, aZone,
aFootprint, aLayer );
if( gap == 0 )
{
aHoles.Append( aFootprint->GetCourtyard( aLayer ) );
}
else if( gap > 0 )
{
SHAPE_POLY_SET hole = aFootprint->GetCourtyard( aLayer );
hole.Inflate( gap, CORNER_STRATEGY::ROUND_ALL_CORNERS, m_maxError );
aHoles.Append( hole );
}
}
};
for( FOOTPRINT* footprint : m_board->Footprints() )
{
knockoutCourtyardClearance( footprint );
knockoutGraphicClearance( &footprint->Reference() );
knockoutGraphicClearance( &footprint->Value() );
std::set<PAD*> allowedNetTiePads;
// Don't knock out holes for graphic items which implement a net-tie to the zone's net
// on the layer being filled.
if( footprint->IsNetTie() )
{
for( PAD* pad : footprint->Pads() )
{
bool sameNet = pad->GetNetCode() == aZone->GetNetCode();
if( !aZone->IsTeardropArea() && aZone->GetNetCode() == 0 )
sameNet = false;
if( sameNet )
{
if( pad->IsOnLayer( aLayer ) )
allowedNetTiePads.insert( pad );
for( PAD* other : footprint->GetNetTiePads( pad ) )
{
if( other->IsOnLayer( aLayer ) )
allowedNetTiePads.insert( other );
}
}
}
}
for( BOARD_ITEM* item : footprint->GraphicalItems() )
{
if( checkForCancel( m_progressReporter ) )
return;
BOX2I itemBBox = item->GetBoundingBox();
if( !zone_boundingbox.Intersects( itemBBox ) )
continue;
bool skipItem = false;
if( item->IsOnLayer( aLayer ) )
{
std::shared_ptr<SHAPE> itemShape = item->GetEffectiveShape();
for( PAD* pad : allowedNetTiePads )
{
if( pad->GetBoundingBox().Intersects( itemBBox )
&& pad->GetEffectiveShape( aLayer )->Collide( itemShape.get() ) )
{
skipItem = true;
break;
}
}
}
if( !skipItem )
knockoutGraphicClearance( item );
}
}
for( BOARD_ITEM* item : m_board->Drawings() )
{
if( checkForCancel( m_progressReporter ) )
return;
knockoutGraphicClearance( item );
}
// Add non-connected zone clearances
//
auto knockoutZoneClearance =
[&]( ZONE* aKnockout )
{
// If the zones share no common layers
if( !aKnockout->GetLayerSet().test( aLayer ) )
return;
if( aKnockout->GetBoundingBox().Intersects( zone_boundingbox ) )
{
if( aKnockout->GetIsRuleArea() )
{
// Keepouts use outline with no clearance
aKnockout->TransformSmoothedOutlineToPolygon( aHoles, 0, m_maxError,
ERROR_OUTSIDE, nullptr );
}
else
{
int gap = std::max( 0, evalRulesForItems( PHYSICAL_CLEARANCE_CONSTRAINT,
aZone, aKnockout, aLayer ) );
gap = std::max( gap, evalRulesForItems( CLEARANCE_CONSTRAINT,
aZone, aKnockout, aLayer ) );
SHAPE_POLY_SET poly;
aKnockout->TransformShapeToPolygon( poly, aLayer, gap + extra_margin,
m_maxError, ERROR_OUTSIDE );
aHoles.Append( poly );
}
}
};
for( ZONE* otherZone : m_board->Zones() )
{
if( checkForCancel( m_progressReporter ) )
return;
// Negative clearance permits zones to short
if( evalRulesForItems( CLEARANCE_CONSTRAINT, aZone, otherZone, aLayer ) < 0 )
continue;
if( otherZone->GetIsRuleArea() )
{
if( otherZone->GetDoNotAllowCopperPour() && !aZone->IsTeardropArea() )
knockoutZoneClearance( otherZone );
}
else if( otherZone->HigherPriority( aZone ) )
{
if( !otherZone->SameNet( aZone ) )
knockoutZoneClearance( otherZone );
}
}
for( FOOTPRINT* footprint : m_board->Footprints() )
{
for( ZONE* otherZone : footprint->Zones() )
{
if( checkForCancel( m_progressReporter ) )
return;
if( otherZone->GetIsRuleArea() )
{
if( otherZone->GetDoNotAllowCopperPour() && !aZone->IsTeardropArea() )
knockoutZoneClearance( otherZone );
}
else if( otherZone->HigherPriority( aZone ) )
{
if( !otherZone->SameNet( aZone ) )
knockoutZoneClearance( otherZone );
}
}
}
aHoles.Simplify();
}
/**
* Removes the outlines of higher-proirity zones with the same net. These zones should be
* in charge of the fill parameters within their own outlines.
*/
void ZONE_FILLER::subtractHigherPriorityZones( const ZONE* aZone, PCB_LAYER_ID aLayer,
SHAPE_POLY_SET& aRawFill )
{
BOX2I zoneBBox = aZone->GetBoundingBox();
auto knockoutZoneOutline =
[&]( ZONE* aKnockout )
{
// If the zones share no common layers
if( !aKnockout->GetLayerSet().test( aLayer ) )
return;
if( aKnockout->GetBoundingBox().Intersects( zoneBBox ) )
{
// Processing of arc shapes in zones is not yet supported because Clipper
// can't do boolean operations on them. The poly outline must be converted to
// segments first.
SHAPE_POLY_SET outline = aKnockout->Outline()->CloneDropTriangulation();
outline.ClearArcs();
aRawFill.BooleanSubtract( outline );
}
};
for( ZONE* otherZone : m_board->Zones() )
{
// Don't use the `HigherPriority()` check here because we _only_ want to knock out zones
// with explicitly higher priorities, not those with equal priorities
if( otherZone->SameNet( aZone )
&& otherZone->GetAssignedPriority() > aZone->GetAssignedPriority() )
{
// Do not remove teardrop area: it is not useful and not good
if( !otherZone->IsTeardropArea() )
knockoutZoneOutline( otherZone );
}
}
for( FOOTPRINT* footprint : m_board->Footprints() )
{
for( ZONE* otherZone : footprint->Zones() )
{
if( otherZone->SameNet( aZone ) && otherZone->HigherPriority( aZone ) )
{
// Do not remove teardrop area: it is not useful and not good
if( !otherZone->IsTeardropArea() )
knockoutZoneOutline( otherZone );
}
}
}
}
void ZONE_FILLER::connect_nearby_polys( SHAPE_POLY_SET& aPolys, double aDistance )
{
if( aPolys.OutlineCount() < 1 )
return;
VERTEX_CONNECTOR vs( aPolys.BBoxFromCaches(), aPolys, aDistance );
vs.FindResults();
// This cannot be a reference because we need to do the comparison below while
// changing the values
std::map<int, std::vector<std::pair<int, VECTOR2I>>> insertion_points;
for( const RESULTS& result : vs.GetResults() )
{
SHAPE_LINE_CHAIN& line1 = aPolys.Outline( result.m_outline1 );
SHAPE_LINE_CHAIN& line2 = aPolys.Outline( result.m_outline2 );
VECTOR2I pt1 = line1.CPoint( result.m_vertex1 );
VECTOR2I pt2 = line2.CPoint( result.m_vertex2 );
// We want to insert the existing point first so that we can place the new point
// between the two points at the same location.
insertion_points[result.m_outline1].push_back( { result.m_vertex1, pt1 } );
insertion_points[result.m_outline1].push_back( { result.m_vertex1, pt2 } );
}
for( auto& [outline, vertices] : insertion_points )
{
SHAPE_LINE_CHAIN& line = aPolys.Outline( outline );
// Stable sort here because we want to make sure that we are inserting pt1 first and
// pt2 second but still sorting the rest of the indices from highest to lowest.
// This allows us to insert into the existing polygon without modifying the future
// insertion points.
std::stable_sort( vertices.begin(), vertices.end(),
[]( const std::pair<int, VECTOR2I>& a, const std::pair<int, VECTOR2I>& b )
{
return a.first > b.first;
} );
for( const auto& [vertex, pt] : vertices )
line.Insert( vertex + 1, pt ); // +1 here because we want to insert after the existing point
}
}
#define DUMP_POLYS_TO_COPPER_LAYER( a, b, c ) \
{ if( m_debugZoneFiller && aDebugLayer == b ) \
{ \
m_board->SetLayerName( b, c ); \
SHAPE_POLY_SET d = a; \
d.Fracture(); \
aFillPolys = d; \
return false; \
} \
}
/*
* Note that aSmoothedOutline is larger than the zone where it intersects with other, same-net
* zones. This is to prevent the re-inflation post min-width trimming from createing divots
* between adjacent zones. The final aMaxExtents trimming will remove these areas from the final
* fill.
*/
bool ZONE_FILLER::fillCopperZone( const ZONE* aZone, PCB_LAYER_ID aLayer, PCB_LAYER_ID aDebugLayer,
const SHAPE_POLY_SET& aSmoothedOutline,
const SHAPE_POLY_SET& aMaxExtents, SHAPE_POLY_SET& aFillPolys )
{
m_maxError = m_board->GetDesignSettings().m_MaxError;
// Features which are min_width should survive pruning; features that are *less* than
// min_width should not. Therefore we subtract epsilon from the min_width when
// deflating/inflating.
int half_min_width = aZone->GetMinThickness() / 2;
int epsilon = pcbIUScale.mmToIU( 0.001 );
// Solid polygons are deflated and inflated during calculations. Deflating doesn't cause
// issues, but inflate is tricky as it can create excessively long and narrow spikes for
// acute angles.
// ALLOW_ACUTE_CORNERS cannot be used due to the spike problem.
// CHAMFER_ACUTE_CORNERS is tempting, but can still produce spikes in some unusual
// circumstances (https://gitlab.com/kicad/code/kicad/-/issues/5581).
// It's unclear if ROUND_ACUTE_CORNERS would have the same issues, but is currently avoided
// as a "less-safe" option.
// ROUND_ALL_CORNERS produces the uniformly nicest shapes, but also a lot of segments.
// CHAMFER_ALL_CORNERS improves the segment count.
CORNER_STRATEGY fastCornerStrategy = CORNER_STRATEGY::CHAMFER_ALL_CORNERS;
CORNER_STRATEGY cornerStrategy = CORNER_STRATEGY::ROUND_ALL_CORNERS;
std::vector<PAD*> thermalConnectionPads;
std::vector<PAD*> noConnectionPads;
std::deque<SHAPE_LINE_CHAIN> thermalSpokes;
SHAPE_POLY_SET clearanceHoles;
aFillPolys = aSmoothedOutline;
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In1_Cu, wxT( "smoothed-outline" ) );
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
/* -------------------------------------------------------------------------------------
* Knockout thermal reliefs.
*/
knockoutThermalReliefs( aZone, aLayer, aFillPolys, thermalConnectionPads, noConnectionPads );
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In2_Cu, wxT( "minus-thermal-reliefs" ) );
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
/* -------------------------------------------------------------------------------------
* Knockout electrical clearances.
*/
buildCopperItemClearances( aZone, aLayer, noConnectionPads, clearanceHoles );
DUMP_POLYS_TO_COPPER_LAYER( clearanceHoles, In3_Cu, wxT( "clearance-holes" ) );
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
/* -------------------------------------------------------------------------------------
* Add thermal relief spokes.
*/
buildThermalSpokes( aZone, aLayer, thermalConnectionPads, thermalSpokes );
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
// Create a temporary zone that we can hit-test spoke-ends against. It's only temporary
// because the "real" subtract-clearance-holes has to be done after the spokes are added.
static const bool USE_BBOX_CACHES = true;
SHAPE_POLY_SET testAreas = aFillPolys.CloneDropTriangulation();
testAreas.BooleanSubtract( clearanceHoles );
DUMP_POLYS_TO_COPPER_LAYER( testAreas, In4_Cu, wxT( "minus-clearance-holes" ) );
// Prune features that don't meet minimum-width criteria
if( half_min_width - epsilon > epsilon )
{
testAreas.Deflate( half_min_width - epsilon, fastCornerStrategy, m_maxError );
DUMP_POLYS_TO_COPPER_LAYER( testAreas, In5_Cu, wxT( "spoke-test-deflated" ) );
testAreas.Inflate( half_min_width - epsilon, fastCornerStrategy, m_maxError );
DUMP_POLYS_TO_COPPER_LAYER( testAreas, In6_Cu, wxT( "spoke-test-reinflated" ) );
}
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
// Spoke-end-testing is hugely expensive so we generate cached bounding-boxes to speed
// things up a bit.
testAreas.BuildBBoxCaches();
int interval = 0;
SHAPE_POLY_SET debugSpokes;
for( const SHAPE_LINE_CHAIN& spoke : thermalSpokes )
{
const VECTOR2I& testPt = spoke.CPoint( 3 );
// Hit-test against zone body
if( testAreas.Contains( testPt, -1, 1, USE_BBOX_CACHES ) )
{
if( m_debugZoneFiller )
debugSpokes.AddOutline( spoke );
aFillPolys.AddOutline( spoke );
continue;
}
if( interval++ > 400 )
{
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
interval = 0;
}
// Hit-test against other spokes
for( const SHAPE_LINE_CHAIN& other : thermalSpokes )
{
// Hit test in both directions to avoid interactions with round-off errors.
// (See https://gitlab.com/kicad/code/kicad/-/issues/13316.)
if( &other != &spoke
&& other.PointInside( testPt, 1, USE_BBOX_CACHES )
&& spoke.PointInside( other.CPoint( 3 ), 1, USE_BBOX_CACHES ) )
{
if( m_debugZoneFiller )
debugSpokes.AddOutline( spoke );
aFillPolys.AddOutline( spoke );
break;
}
}
}
DUMP_POLYS_TO_COPPER_LAYER( debugSpokes, In7_Cu, wxT( "spokes" ) );
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
aFillPolys.BooleanSubtract( clearanceHoles );
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In8_Cu, wxT( "after-spoke-trimming" ) );
/* -------------------------------------------------------------------------------------
* Prune features that don't meet minimum-width criteria
*/
if( half_min_width - epsilon > epsilon )
aFillPolys.Deflate( half_min_width - epsilon, fastCornerStrategy, m_maxError );
// Min-thickness is the web thickness. On the other hand, a blob min-thickness by
// min-thickness is not useful. Since there's no obvious definition of web vs. blob, we
// arbitrarily choose "at least 2X min-thickness on one axis". (Since we're doing this
// during the deflated state, that means we test for "at least min-thickness".)
for( int ii = aFillPolys.OutlineCount() - 1; ii >= 0; ii-- )
{
std::vector<SHAPE_LINE_CHAIN>& island = aFillPolys.Polygon( ii );
BOX2I islandExtents;
for( const VECTOR2I& pt : island.front().CPoints() )
{
islandExtents.Merge( pt );
if( islandExtents.GetSizeMax() > aZone->GetMinThickness() )
break;
}
if( islandExtents.GetSizeMax() < aZone->GetMinThickness() )
aFillPolys.DeletePolygon( ii );
}
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In9_Cu, wxT( "deflated" ) );
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
/* -------------------------------------------------------------------------------------
* Process the hatch pattern (note that we do this while deflated)
*/
if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
{
if( !addHatchFillTypeOnZone( aZone, aLayer, aDebugLayer, aFillPolys ) )
return false;
}
else
{
/* ---------------------------------------------------------------------------------
* Connect nearby polygons with zero-width lines in order to ensure correct
* re-inflation.
*/
aFillPolys.Fracture();
connect_nearby_polys( aFillPolys, aZone->GetMinThickness() );
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In10_Cu, wxT( "connected-nearby-polys" ) );
}
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
/* -------------------------------------------------------------------------------------
* Finish minimum-width pruning by re-inflating
*/
if( half_min_width - epsilon > epsilon )
aFillPolys.Inflate( half_min_width - epsilon, cornerStrategy, m_maxError, true );
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In15_Cu, wxT( "after-reinflating" ) );
/* -------------------------------------------------------------------------------------
* Ensure additive changes (thermal stubs and inflating acute corners) do not add copper
* outside the zone boundary, inside the clearance holes, or between otherwise isolated
* islands
*/
for( PAD* pad : thermalConnectionPads )
addHoleKnockout( pad, 0, clearanceHoles );
aFillPolys.BooleanIntersection( aMaxExtents );
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In16_Cu, wxT( "after-trim-to-outline" ) );
aFillPolys.BooleanSubtract( clearanceHoles );
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In17_Cu, wxT( "after-trim-to-clearance-holes" ) );
/* -------------------------------------------------------------------------------------
* Lastly give any same-net but higher-priority zones control over their own area.
*/
subtractHigherPriorityZones( aZone, aLayer, aFillPolys );
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In18_Cu, wxT( "minus-higher-priority-zones" ) );
aFillPolys.Fracture();
return true;
}
bool ZONE_FILLER::fillNonCopperZone( const ZONE* aZone, PCB_LAYER_ID aLayer,
const SHAPE_POLY_SET& aSmoothedOutline,
SHAPE_POLY_SET& aFillPolys )
{
BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
BOX2I zone_boundingbox = aZone->GetBoundingBox();
SHAPE_POLY_SET clearanceHoles;
long ticker = 0;
auto checkForCancel =
[&ticker]( PROGRESS_REPORTER* aReporter ) -> bool
{
return aReporter && ( ticker++ % 50 ) == 0 && aReporter->IsCancelled();
};
auto knockoutGraphicClearance =
[&]( BOARD_ITEM* aItem )
{
if( aItem->IsKnockout() && aItem->IsOnLayer( aLayer )
&& aItem->GetBoundingBox().Intersects( zone_boundingbox ) )
{
DRC_CONSTRAINT cc = bds.m_DRCEngine->EvalRules( PHYSICAL_CLEARANCE_CONSTRAINT,
aZone, aItem, aLayer );
addKnockout( aItem, aLayer, cc.GetValue().Min(), false, clearanceHoles );
}
};
for( FOOTPRINT* footprint : m_board->Footprints() )
{
if( checkForCancel( m_progressReporter ) )
return false;
knockoutGraphicClearance( &footprint->Reference() );
knockoutGraphicClearance( &footprint->Value() );
for( BOARD_ITEM* item : footprint->GraphicalItems() )
knockoutGraphicClearance( item );
}
for( BOARD_ITEM* item : m_board->Drawings() )
{
if( checkForCancel( m_progressReporter ) )
return false;
knockoutGraphicClearance( item );
}
aFillPolys = aSmoothedOutline;
aFillPolys.BooleanSubtract( clearanceHoles );
auto subtractKeepout =
[&]( ZONE* candidate )
{
if( !candidate->GetIsRuleArea() )
return;
if( !candidate->HasKeepoutParametersSet() )
return;
if( candidate->GetDoNotAllowCopperPour() && candidate->IsOnLayer( aLayer ) )
{
if( candidate->GetBoundingBox().Intersects( zone_boundingbox ) )
{
if( candidate->Outline()->ArcCount() == 0 )
{
aFillPolys.BooleanSubtract( *candidate->Outline() );
}
else
{
SHAPE_POLY_SET keepoutOutline( *candidate->Outline() );
keepoutOutline.ClearArcs();
aFillPolys.BooleanSubtract( keepoutOutline );
}
}
}
};
for( ZONE* keepout : m_board->Zones() )
{
if( checkForCancel( m_progressReporter ) )
return false;
subtractKeepout( keepout );
}
for( FOOTPRINT* footprint : m_board->Footprints() )
{
if( checkForCancel( m_progressReporter ) )
return false;
for( ZONE* keepout : footprint->Zones() )
subtractKeepout( keepout );
}
// Features which are min_width should survive pruning; features that are *less* than
// min_width should not. Therefore we subtract epsilon from the min_width when
// deflating/inflating.
int half_min_width = aZone->GetMinThickness() / 2;
int epsilon = pcbIUScale.mmToIU( 0.001 );
aFillPolys.Deflate( half_min_width - epsilon, CORNER_STRATEGY::CHAMFER_ALL_CORNERS, m_maxError );
// Remove the non filled areas due to the hatch pattern
if( aZone->GetFillMode() == ZONE_FILL_MODE::HATCH_PATTERN )
{
if( !addHatchFillTypeOnZone( aZone, aLayer, aLayer, aFillPolys ) )
return false;
}
// Re-inflate after pruning of areas that don't meet minimum-width criteria
if( half_min_width - epsilon > epsilon )
aFillPolys.Inflate( half_min_width - epsilon, CORNER_STRATEGY::ROUND_ALL_CORNERS, m_maxError );
aFillPolys.Fracture();
return true;
}
/*
* Build the filled solid areas data from real outlines (stored in m_Poly)
* The solid areas can be more than one on copper layers, and do not have holes
* ( holes are linked by overlapping segments to the main outline)
*/
bool ZONE_FILLER::fillSingleZone( ZONE* aZone, PCB_LAYER_ID aLayer, SHAPE_POLY_SET& aFillPolys )
{
SHAPE_POLY_SET* boardOutline = m_brdOutlinesValid ? &m_boardOutline : nullptr;
SHAPE_POLY_SET maxExtents;
SHAPE_POLY_SET smoothedPoly;
PCB_LAYER_ID debugLayer = UNDEFINED_LAYER;
if( m_debugZoneFiller && LSET::InternalCuMask().Contains( aLayer ) )
{
debugLayer = aLayer;
aLayer = F_Cu;
}
if( !aZone->BuildSmoothedPoly( maxExtents, aLayer, boardOutline, &smoothedPoly ) )
return false;
if( m_progressReporter && m_progressReporter->IsCancelled() )
return false;
if( aZone->IsOnCopperLayer() )
{
if( fillCopperZone( aZone, aLayer, debugLayer, smoothedPoly, maxExtents, aFillPolys ) )
aZone->SetNeedRefill( false );
}
else
{
if( fillNonCopperZone( aZone, aLayer, smoothedPoly, aFillPolys ) )
aZone->SetNeedRefill( false );
}
return true;
}
/**
* Function buildThermalSpokes
*/
void ZONE_FILLER::buildThermalSpokes( const ZONE* aZone, PCB_LAYER_ID aLayer,
const std::vector<PAD*>& aSpokedPadsList,
std::deque<SHAPE_LINE_CHAIN>& aSpokesList )
{
BOARD_DESIGN_SETTINGS& bds = m_board->GetDesignSettings();
BOX2I zoneBB = aZone->GetBoundingBox();
DRC_CONSTRAINT constraint;
int zone_half_width = aZone->GetMinThickness() / 2;
zoneBB.Inflate( std::max( bds.GetBiggestClearanceValue(), aZone->GetLocalClearance().value() ) );
// Is a point on the boundary of the polygon inside or outside?
// The boundary may be off by MaxError
int epsilon = bds.m_MaxError;
for( PAD* pad : aSpokedPadsList )
{
// We currently only connect to pads, not pad holes
if( !pad->IsOnLayer( aLayer ) )
continue;
constraint = bds.m_DRCEngine->EvalRules( THERMAL_RELIEF_GAP_CONSTRAINT, pad, aZone, aLayer );
int thermalReliefGap = constraint.GetValue().Min();
constraint = bds.m_DRCEngine->EvalRules( THERMAL_SPOKE_WIDTH_CONSTRAINT, pad, aZone, aLayer );
int spoke_w = constraint.GetValue().Opt();
// Spoke width should ideally be smaller than the pad minor axis.
// Otherwise the thermal shape is not really a thermal relief,
// and the algo to count the actual number of spokes can fail
int spoke_max_allowed_w = std::min( pad->GetSize( aLayer ).x, pad->GetSize( aLayer ).y );
spoke_w = std::clamp( spoke_w, constraint.Value().Min(), constraint.Value().Max() );
// ensure the spoke width is smaller than the pad minor size
spoke_w = std::min( spoke_w, spoke_max_allowed_w );
// Cannot create stubs having a width < zone min thickness
if( spoke_w < aZone->GetMinThickness() )
continue;
int spoke_half_w = spoke_w / 2;
// Quick test here to possibly save us some work
BOX2I itemBB = pad->GetBoundingBox();
itemBB.Inflate( thermalReliefGap + epsilon );
if( !( itemBB.Intersects( zoneBB ) ) )
continue;
bool customSpokes = false;
if( pad->GetShape( aLayer ) == PAD_SHAPE::CUSTOM )
{
for( const std::shared_ptr<PCB_SHAPE>& primitive : pad->GetPrimitives( aLayer ) )
{
if( primitive->IsProxyItem() && primitive->GetShape() == SHAPE_T::SEGMENT )
{
customSpokes = true;
break;
}
}
}
// Thermal spokes consist of square-ended segments from the pad center to points just
// outside the thermal relief. The outside end has an extra center point (which must be
// at idx 3) which is used for testing whether or not the spoke connects to copper in the
// parent zone.
auto buildSpokesFromOrigin =
[&]( const BOX2I& box, EDA_ANGLE angle )
{
VECTOR2I center = box.GetCenter();
VECTOR2I half_size( box.GetWidth() / 2, box.GetHeight() / 2 );
// Function to find intersection of line with box edge
auto intersectLineBox =
[&](const VECTOR2D& direction) -> VECTOR2I
{
double dx = direction.x;
double dy = direction.y;
// Short-circuit the axis cases because they will be degenerate in the
// intersection test
if( direction.x == 0 )
return VECTOR2I( 0, dy * half_size.y );
else if( direction.y == 0 )
return VECTOR2I( dx * half_size.x, 0 );
// We are going to intersect with one side or the other. Whichever
// we hit first is the fraction of the spoke length we keep
double tx = std::min( half_size.x / std::abs( dx ),
half_size.y / std::abs( dy ) );
return VECTOR2I( dx * tx, dy * tx );
};
// Precalculate angles for four cardinal directions
const EDA_ANGLE angles[4] = {
EDA_ANGLE( 0.0, DEGREES_T ) + angle, // Right
EDA_ANGLE( 90.0, DEGREES_T ) + angle, // Up
EDA_ANGLE( 180.0, DEGREES_T ) + angle, // Left
EDA_ANGLE( 270.0, DEGREES_T ) + angle // Down
};
// Generate four spokes in cardinal directions
for( const EDA_ANGLE& spokeAngle : angles )
{
VECTOR2D direction( spokeAngle.Cos(), spokeAngle.Sin() );
VECTOR2D perpendicular = direction.Perpendicular();
VECTOR2I intersection = intersectLineBox( direction );
VECTOR2I spoke_side = perpendicular.Resize( spoke_half_w );
SHAPE_LINE_CHAIN spoke;
spoke.Append( center + spoke_side );
spoke.Append( center - spoke_side );
spoke.Append( center + intersection - spoke_side );
spoke.Append( center + intersection ); // test pt
spoke.Append( center + intersection + spoke_side );
spoke.SetClosed( true );
aSpokesList.push_back( std::move( spoke ) );
}
};
if( customSpokes )
{
SHAPE_POLY_SET thermalPoly;
SHAPE_LINE_CHAIN thermalOutline;
pad->TransformShapeToPolygon( thermalPoly, aLayer, thermalReliefGap + epsilon,
m_maxError, ERROR_OUTSIDE );
if( thermalPoly.OutlineCount() )
thermalOutline = thermalPoly.Outline( 0 );
SHAPE_LINE_CHAIN padOutline = pad->GetEffectivePolygon( aLayer, ERROR_OUTSIDE )->Outline( 0 );
auto trimToOutline = [&]( SEG& aSegment )
{
SHAPE_LINE_CHAIN::INTERSECTIONS intersections;
if( padOutline.Intersect( aSegment, intersections ) )
{
intersections.clear();
// Trim the segment to the thermal outline
if( thermalOutline.Intersect( aSegment, intersections ) )
{
aSegment.B = intersections.front().p;
return true;
}
}
return false;
};
for( const std::shared_ptr<PCB_SHAPE>& primitive : pad->GetPrimitives( aLayer ) )
{
if( primitive->IsProxyItem() && primitive->GetShape() == SHAPE_T::SEGMENT )
{
SEG seg( primitive->GetStart(), primitive->GetEnd() );
SHAPE_LINE_CHAIN::INTERSECTIONS intersections;
RotatePoint( seg.A, pad->GetOrientation() );
RotatePoint( seg.B, pad->GetOrientation() );
seg.A += pad->ShapePos( aLayer );
seg.B += pad->ShapePos( aLayer );
// Make sure seg.A is the origin
if( !pad->GetEffectivePolygon( aLayer, ERROR_OUTSIDE )->Contains( seg.A ) )
{
// Do not create this spoke if neither point is in the pad.
if( !pad->GetEffectivePolygon( aLayer, ERROR_OUTSIDE )->Contains( seg.B ) )
continue;
seg.Reverse();
}
// Trim segment to pad and thermal outline polygon.
// If there is no intersection with the pad, don't create the spoke.
if( trimToOutline( seg ) )
{
VECTOR2I direction = ( seg.B - seg.A ).Resize( spoke_half_w );
VECTOR2I offset = direction.Perpendicular().Resize( spoke_half_w );
// Extend the spoke edges by half the spoke width to capture convex pad shapes
// with a maximum of 45 degrees.
SEG segL( seg.A - direction - offset, seg.B + direction - offset );
SEG segR( seg.A - direction + offset, seg.B + direction + offset );
// Only create this spoke if both edges intersect the pad and thermal outline
if( trimToOutline( segL ) && trimToOutline( segR ) )
{
// Extend the spoke by the minimum thickness for the zone to ensure full
// connection width
direction = direction.Resize( aZone->GetMinThickness() );
SHAPE_LINE_CHAIN spoke;
spoke.Append( seg.A + offset );
spoke.Append( seg.A - offset );
spoke.Append( segL.B + direction );
spoke.Append( seg.B + direction ); // test pt at index 3.
spoke.Append( segR.B + direction );
spoke.SetClosed( true );
aSpokesList.push_back( std::move( spoke ) );
}
}
}
}
}
else
{
// Since the bounding-box needs to be correclty rotated we use a dummy pad to keep
// from dirtying the real pad's cached shapes.
PAD dummy_pad( *pad );
dummy_pad.SetOrientation( ANGLE_0 );
// Spokes are from center of pad shape, not from hole. So the dummy pad has no shape
// offset and is at position 0,0
dummy_pad.SetPosition( VECTOR2I( 0, 0 ) );
dummy_pad.SetOffset( aLayer, VECTOR2I( 0, 0 ) );
BOX2I spokesBox = dummy_pad.GetBoundingBox( aLayer );
VECTOR2I padSize = pad->GetSize( aLayer );
// Add the half width of the zone mininum width to the inflate amount to account for
// the fact that the deflation procedure will shrink the results by half the half the
// zone min width
spokesBox.Inflate( thermalReliefGap + epsilon + zone_half_width );
// This is a touchy case because the bounding box for circles overshoots the mark
// when rotated at 45 degrees. So we just build spokes at 0 degrees and rotate
// them later.
if( pad->GetShape( aLayer ) == PAD_SHAPE::CIRCLE
|| ( pad->GetShape( aLayer ) == PAD_SHAPE::OVAL && padSize.x == padSize.y ) )
{
buildSpokesFromOrigin( spokesBox, ANGLE_0 );
if( pad->GetThermalSpokeAngle() != ANGLE_0 )
{
//Rotate the last four elements of aspokeslist
for( auto it = aSpokesList.rbegin(); it != aSpokesList.rbegin() + 4; ++it )
it->Rotate( pad->GetThermalSpokeAngle() );
}
}
else
{
buildSpokesFromOrigin( spokesBox, pad->GetThermalSpokeAngle() );
}
auto spokeIter = aSpokesList.rbegin();
for( int ii = 0; ii < 4; ++ii, ++spokeIter )
{
spokeIter->Rotate( pad->GetOrientation() );
spokeIter->Move( pad->ShapePos( aLayer ) );
}
// Remove group membership from dummy item before deleting
dummy_pad.SetParentGroup( nullptr );
}
}
for( size_t ii = 0; ii < aSpokesList.size(); ++ii )
aSpokesList[ii].GenerateBBoxCache();
}
bool ZONE_FILLER::addHatchFillTypeOnZone( const ZONE* aZone, PCB_LAYER_ID aLayer,
PCB_LAYER_ID aDebugLayer, SHAPE_POLY_SET& aFillPolys )
{
// Build grid:
// obviously line thickness must be > zone min thickness.
// It can happens if a board file was edited by hand by a python script
// Use 1 micron margin to be *sure* there is no issue in Gerber files
// (Gbr file unit = 1 or 10 nm) due to some truncation in coordinates or calculations
// This margin also avoid problems due to rounding coordinates in next calculations
// that can create incorrect polygons
int thickness = std::max( aZone->GetHatchThickness(),
aZone->GetMinThickness() + pcbIUScale.mmToIU( 0.001 ) );
int linethickness = thickness - aZone->GetMinThickness();
int gridsize = thickness + aZone->GetHatchGap();
int maxError = m_board->GetDesignSettings().m_MaxError;
SHAPE_POLY_SET filledPolys = aFillPolys.CloneDropTriangulation();
// Use a area that contains the rotated bbox by orientation, and after rotate the result
// by -orientation.
if( !aZone->GetHatchOrientation().IsZero() )
filledPolys.Rotate( - aZone->GetHatchOrientation() );
BOX2I bbox = filledPolys.BBox( 0 );
// Build hole shape
// the hole size is aZone->GetHatchGap(), but because the outline thickness
// is aZone->GetMinThickness(), the hole shape size must be larger
SHAPE_LINE_CHAIN hole_base;
int hole_size = aZone->GetHatchGap() + aZone->GetMinThickness();
VECTOR2I corner( 0, 0 );;
hole_base.Append( corner );
corner.x += hole_size;
hole_base.Append( corner );
corner.y += hole_size;
hole_base.Append( corner );
corner.x = 0;
hole_base.Append( corner );
hole_base.SetClosed( true );
// Calculate minimal area of a grid hole.
// All holes smaller than a threshold will be removed
double minimal_hole_area = hole_base.Area() * aZone->GetHatchHoleMinArea();
// Now convert this hole to a smoothed shape:
if( aZone->GetHatchSmoothingLevel() > 0 )
{
// the actual size of chamfer, or rounded corner radius is the half size
// of the HatchFillTypeGap scaled by aZone->GetHatchSmoothingValue()
// aZone->GetHatchSmoothingValue() = 1.0 is the max value for the chamfer or the
// radius of corner (radius = half size of the hole)
int smooth_value = KiROUND( aZone->GetHatchGap()
* aZone->GetHatchSmoothingValue() / 2 );
// Minimal optimization:
// make smoothing only for reasonable smooth values, to avoid a lot of useless segments
// and if the smooth value is small, use chamfer even if fillet is requested
#define SMOOTH_MIN_VAL_MM 0.02
#define SMOOTH_SMALL_VAL_MM 0.04
if( smooth_value > pcbIUScale.mmToIU( SMOOTH_MIN_VAL_MM ) )
{
SHAPE_POLY_SET smooth_hole;
smooth_hole.AddOutline( hole_base );
int smooth_level = aZone->GetHatchSmoothingLevel();
if( smooth_value < pcbIUScale.mmToIU( SMOOTH_SMALL_VAL_MM ) && smooth_level > 1 )
smooth_level = 1;
// Use a larger smooth_value to compensate the outline tickness
// (chamfer is not visible is smooth value < outline thickess)
smooth_value += aZone->GetMinThickness() / 2;
// smooth_value cannot be bigger than the half size oh the hole:
smooth_value = std::min( smooth_value, aZone->GetHatchGap() / 2 );
// the error to approximate a circle by segments when smoothing corners by a arc
maxError = std::max( maxError * 2, smooth_value / 20 );
switch( smooth_level )
{
case 1:
// Chamfer() uses the distance from a corner to create a end point
// for the chamfer.
hole_base = smooth_hole.Chamfer( smooth_value ).Outline( 0 );
break;
default:
if( aZone->GetHatchSmoothingLevel() > 2 )
maxError /= 2; // Force better smoothing
hole_base = smooth_hole.Fillet( smooth_value, maxError ).Outline( 0 );
break;
case 0:
break;
};
}
}
// Build holes
SHAPE_POLY_SET holes;
for( int xx = 0; ; xx++ )
{
int xpos = xx * gridsize;
if( xpos > bbox.GetWidth() )
break;
for( int yy = 0; ; yy++ )
{
int ypos = yy * gridsize;
if( ypos > bbox.GetHeight() )
break;
// Generate hole
SHAPE_LINE_CHAIN hole( hole_base );
hole.Move( VECTOR2I( xpos, ypos ) );
holes.AddOutline( hole );
}
}
holes.Move( bbox.GetPosition() );
if( !aZone->GetHatchOrientation().IsZero() )
holes.Rotate( aZone->GetHatchOrientation() );
DUMP_POLYS_TO_COPPER_LAYER( holes, In10_Cu, wxT( "hatch-holes" ) );
int outline_margin = aZone->GetMinThickness() * 1.1;
// Using GetHatchThickness() can look more consistent than GetMinThickness().
if( aZone->GetHatchBorderAlgorithm() && aZone->GetHatchThickness() > outline_margin )
outline_margin = aZone->GetHatchThickness();
// The fill has already been deflated to ensure GetMinThickness() so we just have to
// account for anything beyond that.
SHAPE_POLY_SET deflatedFilledPolys = aFillPolys.CloneDropTriangulation();
deflatedFilledPolys.Deflate( outline_margin - aZone->GetMinThickness(),
CORNER_STRATEGY::CHAMFER_ALL_CORNERS, maxError );
holes.BooleanIntersection( deflatedFilledPolys );
DUMP_POLYS_TO_COPPER_LAYER( holes, In11_Cu, wxT( "fill-clipped-hatch-holes" ) );
SHAPE_POLY_SET deflatedOutline = aZone->Outline()->CloneDropTriangulation();
deflatedOutline.Deflate( outline_margin, CORNER_STRATEGY::CHAMFER_ALL_CORNERS, maxError );
holes.BooleanIntersection( deflatedOutline );
DUMP_POLYS_TO_COPPER_LAYER( holes, In12_Cu, wxT( "outline-clipped-hatch-holes" ) );
if( aZone->GetNetCode() != 0 )
{
// Vias and pads connected to the zone must not be allowed to become isolated inside
// one of the holes. Effectively this means their copper outline needs to be expanded
// to be at least as wide as the gap so that it is guaranteed to touch at least one
// edge.
BOX2I zone_boundingbox = aZone->GetBoundingBox();
SHAPE_POLY_SET aprons;
int min_apron_radius = ( aZone->GetHatchGap() * 10 ) / 19;
for( PCB_TRACK* track : m_board->Tracks() )
{
if( track->Type() == PCB_VIA_T )
{
PCB_VIA* via = static_cast<PCB_VIA*>( track );
if( via->GetNetCode() == aZone->GetNetCode()
&& via->IsOnLayer( aLayer )
&& via->GetBoundingBox().Intersects( zone_boundingbox ) )
{
int r = std::max( min_apron_radius,
via->GetDrillValue() / 2 + outline_margin );
TransformCircleToPolygon( aprons, via->GetPosition(), r, maxError,
ERROR_OUTSIDE );
}
}
}
for( FOOTPRINT* footprint : m_board->Footprints() )
{
for( PAD* pad : footprint->Pads() )
{
if( pad->GetNetCode() == aZone->GetNetCode()
&& pad->IsOnLayer( aLayer )
&& pad->GetBoundingBox().Intersects( zone_boundingbox ) )
{
// What we want is to bulk up the pad shape so that the narrowest bit of
// copper between the hole and the apron edge is at least outline_margin
// wide (and that the apron itself meets min_apron_radius. But that would
// take a lot of code and math, and the following approximation is close
// enough.
int pad_width = std::min( pad->GetSize( aLayer ).x, pad->GetSize( aLayer ).y );
int slot_width = std::min( pad->GetDrillSize().x, pad->GetDrillSize().y );
int min_annular_ring_width = ( pad_width - slot_width ) / 2;
int clearance = std::max( min_apron_radius - pad_width / 2,
outline_margin - min_annular_ring_width );
clearance = std::max( 0, clearance - linethickness / 2 );
pad->TransformShapeToPolygon( aprons, aLayer, clearance, maxError,
ERROR_OUTSIDE );
}
}
}
holes.BooleanSubtract( aprons );
}
DUMP_POLYS_TO_COPPER_LAYER( holes, In13_Cu, wxT( "pad-via-clipped-hatch-holes" ) );
// Now filter truncated holes to avoid small holes in pattern
// It happens for holes near the zone outline
for( int ii = 0; ii < holes.OutlineCount(); )
{
double area = holes.Outline( ii ).Area();
if( area < minimal_hole_area ) // The current hole is too small: remove it
holes.DeletePolygon( ii );
else
++ii;
}
// create grid. Useto
// generate strictly simple polygons needed by Gerber files and Fracture()
aFillPolys.BooleanSubtract( aFillPolys, holes );
DUMP_POLYS_TO_COPPER_LAYER( aFillPolys, In14_Cu, wxT( "after-hatching" ) );
return true;
}
|