File size: 112,040 Bytes
985c397 | 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 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 | // SPDX-License-Identifier: BSD-3-Clause
// dxf.cpp
// Copyright (c) 2009, Dan Heeks
// This program is released under the BSD license. See the file COPYING for details.
// modified 2018 wandererfan
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <exception>
#include <string>
#include "dxf.h"
#include <App/Application.h>
#include <Base/Color.h>
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/FileInfo.h>
#include <Base/Interpreter.h>
#include <Base/Stream.h>
#include <Base/Tools.h>
#include <Base/Vector3D.h>
using namespace std;
namespace
{
std::string DxfUnitToString(DxfUnits::eDxfUnits_t unit)
{
switch (unit) {
case DxfUnits::eInches:
return "Inches";
case DxfUnits::eFeet:
return "Feet";
case DxfUnits::eMiles:
return "Miles";
case DxfUnits::eMillimeters:
return "Millimeters";
case DxfUnits::eCentimeters:
return "Centimeters";
case DxfUnits::eMeters:
return "Meters";
case DxfUnits::eKilometers:
return "Kilometers";
case DxfUnits::eMicroinches:
return "Microinches";
case DxfUnits::eMils:
return "Mils";
case DxfUnits::eYards:
return "Yards";
case DxfUnits::eAngstroms:
return "Angstroms";
case DxfUnits::eNanometers:
return "Nanometers";
case DxfUnits::eMicrons:
return "Microns";
case DxfUnits::eDecimeters:
return "Decimeters";
case DxfUnits::eDekameters:
return "Dekameters";
case DxfUnits::eHectometers:
return "Hectometers";
case DxfUnits::eGigameters:
return "Gigameters";
case DxfUnits::eAstronomicalUnits:
return "Astronomical Units";
case DxfUnits::eLightYears:
return "Light Years";
case DxfUnits::eParsecs:
return "Parsecs";
case DxfUnits::eUnspecified:
default:
return "Unspecified";
}
}
} // namespace
static Base::Vector3d MakeVector3d(const double coordinates[3])
{
// NOLINTNEXTLINE(readability/nolint)
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
return Base::Vector3d(coordinates[0], coordinates[1], coordinates[2]);
}
CDxfWrite::CDxfWrite(const char* filepath)
: // TODO: these should probably be parameters in config file
// handles:
// boilerplate 0 - A00
// used by dxf.cpp A01 - FFFE
// ACAD HANDSEED FFFF
m_fail(false)
, m_ssBlock(new std::ostringstream())
, m_ssBlkRecord(new std::ostringstream())
, m_ssEntity(new std::ostringstream())
, m_ssLayer(new std::ostringstream())
, m_version(12)
, m_handle(0xA00)
, // room for 2560 handles in boilerplate files
// m_entityHandle(0x300), //don't need special ranges for handles
// m_layerHandle(0x30),
// m_blockHandle(0x210),
// m_blkRecordHandle(0x110),
m_polyOverride(false)
, m_layerName("none")
{
// start the file
Base::FileInfo fi(filepath);
m_ofs = new Base::ofstream(fi, ios::out);
if (!(*m_ofs)) {
m_fail = true;
return;
}
m_ofs->imbue(std::locale("C"));
// use lots of digits to avoid rounding errors
m_ssEntity->setf(std::ios::fixed);
m_ssEntity->precision(9);
}
CDxfWrite::~CDxfWrite()
{
delete m_ofs;
delete m_ssBlock;
delete m_ssBlkRecord;
delete m_ssEntity;
delete m_ssLayer;
}
void CDxfWrite::init()
{
writeHeaderSection();
makeBlockRecordTableHead();
makeBlockSectionHead();
}
//! assemble pieces into output file
void CDxfWrite::endRun()
{
makeLayerTable();
makeBlockRecordTableBody();
writeClassesSection();
writeTablesSection();
writeBlocksSection();
writeEntitiesSection();
writeObjectsSection();
(*m_ofs) << " 0" << endl;
(*m_ofs) << "EOF";
}
//***************************
// writeHeaderSection
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeHeaderSection()
{
std::stringstream ss;
ss << "FreeCAD v" << App::Application::Config()["BuildVersionMajor"] << "."
<< App::Application::Config()["BuildVersionMinor"] << " "
<< App::Application::Config()["BuildRevision"];
// header & version
(*m_ofs) << "999" << endl;
(*m_ofs) << ss.str() << endl;
// static header content
ss.str("");
ss.clear();
ss << "header" << m_version << ".rub";
std::string fileSpec = m_dataDir + ss.str();
(*m_ofs) << getPlateFile(fileSpec);
}
//***************************
// writeClassesSection
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeClassesSection()
{
if (m_version < 14) {
return;
}
// static classes section content
std::stringstream ss;
ss << "classes" << m_version << ".rub";
std::string fileSpec = m_dataDir + ss.str();
(*m_ofs) << getPlateFile(fileSpec);
}
//***************************
// writeTablesSection
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeTablesSection()
{
// static tables section head end content
std::stringstream ss;
ss << "tables1" << m_version << ".rub";
std::string fileSpec = m_dataDir + ss.str();
(*m_ofs) << getPlateFile(fileSpec);
(*m_ofs) << (*m_ssLayer).str();
// static tables section tail end content
ss.str("");
ss.clear();
ss << "tables2" << m_version << ".rub";
fileSpec = m_dataDir + ss.str();
(*m_ofs) << getPlateFile(fileSpec);
if (m_version > 12) {
(*m_ofs) << (*m_ssBlkRecord).str();
(*m_ofs) << " 0" << endl;
(*m_ofs) << "ENDTAB" << endl;
}
(*m_ofs) << " 0" << endl;
(*m_ofs) << "ENDSEC" << endl;
}
//***************************
// makeLayerTable
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::makeLayerTable()
{
std::string tablehash = getLayerHandle();
(*m_ssLayer) << " 0" << endl;
(*m_ssLayer) << "TABLE" << endl;
(*m_ssLayer) << " 2" << endl;
(*m_ssLayer) << "LAYER" << endl;
(*m_ssLayer) << " 5" << endl;
(*m_ssLayer) << tablehash << endl;
if (m_version > 12) {
(*m_ssLayer) << "330" << endl;
(*m_ssLayer) << 0 << endl;
(*m_ssLayer) << "100" << endl;
(*m_ssLayer) << "AcDbSymbolTable" << endl;
}
(*m_ssLayer) << " 70" << endl;
(*m_ssLayer) << m_layerList.size() + 1 << endl;
(*m_ssLayer) << " 0" << endl;
(*m_ssLayer) << "LAYER" << endl;
(*m_ssLayer) << " 5" << endl;
(*m_ssLayer) << getLayerHandle() << endl;
if (m_version > 12) {
(*m_ssLayer) << "330" << endl;
(*m_ssLayer) << tablehash << endl;
(*m_ssLayer) << "100" << endl;
(*m_ssLayer) << "AcDbSymbolTableRecord" << endl;
(*m_ssLayer) << "100" << endl;
(*m_ssLayer) << "AcDbLayerTableRecord" << endl;
}
(*m_ssLayer) << " 2" << endl;
(*m_ssLayer) << "0" << endl;
(*m_ssLayer) << " 70" << endl;
(*m_ssLayer) << " 0" << endl;
(*m_ssLayer) << " 62" << endl;
(*m_ssLayer) << " 7" << endl;
(*m_ssLayer) << " 6" << endl;
(*m_ssLayer) << "CONTINUOUS" << endl;
for (auto& l : m_layerList) {
(*m_ssLayer) << " 0" << endl;
(*m_ssLayer) << "LAYER" << endl;
(*m_ssLayer) << " 5" << endl;
(*m_ssLayer) << getLayerHandle() << endl;
if (m_version > 12) {
(*m_ssLayer) << "330" << endl;
(*m_ssLayer) << tablehash << endl;
(*m_ssLayer) << "100" << endl;
(*m_ssLayer) << "AcDbSymbolTableRecord" << endl;
(*m_ssLayer) << "100" << endl;
(*m_ssLayer) << "AcDbLayerTableRecord" << endl;
}
(*m_ssLayer) << " 2" << endl;
(*m_ssLayer) << l << endl;
(*m_ssLayer) << " 70" << endl;
(*m_ssLayer) << " 0" << endl;
(*m_ssLayer) << " 62" << endl;
(*m_ssLayer) << " 7" << endl;
(*m_ssLayer) << " 6" << endl;
(*m_ssLayer) << "CONTINUOUS" << endl;
}
(*m_ssLayer) << " 0" << endl;
(*m_ssLayer) << "ENDTAB" << endl;
}
//***************************
// makeBlockRecordTableHead
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::makeBlockRecordTableHead()
{
if (m_version < 14) {
return;
}
std::string tablehash = getBlkRecordHandle();
m_saveBlockRecordTableHandle = tablehash;
(*m_ssBlkRecord) << " 0" << endl;
(*m_ssBlkRecord) << "TABLE" << endl;
(*m_ssBlkRecord) << " 2" << endl;
(*m_ssBlkRecord) << "BLOCK_RECORD" << endl;
(*m_ssBlkRecord) << " 5" << endl;
(*m_ssBlkRecord) << tablehash << endl;
(*m_ssBlkRecord) << "330" << endl;
(*m_ssBlkRecord) << "0" << endl;
(*m_ssBlkRecord) << "100" << endl;
(*m_ssBlkRecord) << "AcDbSymbolTable" << endl;
(*m_ssBlkRecord) << " 70" << endl;
(*m_ssBlkRecord) << (m_blockList.size() + 5) << endl;
m_saveModelSpaceHandle = getBlkRecordHandle();
(*m_ssBlkRecord) << " 0" << endl;
(*m_ssBlkRecord) << "BLOCK_RECORD" << endl;
(*m_ssBlkRecord) << " 5" << endl;
(*m_ssBlkRecord) << m_saveModelSpaceHandle << endl;
(*m_ssBlkRecord) << "330" << endl;
(*m_ssBlkRecord) << tablehash << endl;
(*m_ssBlkRecord) << "100" << endl;
(*m_ssBlkRecord) << "AcDbSymbolTableRecord" << endl;
(*m_ssBlkRecord) << "100" << endl;
(*m_ssBlkRecord) << "AcDbBlockTableRecord" << endl;
(*m_ssBlkRecord) << " 2" << endl;
(*m_ssBlkRecord) << "*MODEL_SPACE" << endl;
// (*m_ssBlkRecord) << " 1" << endl;
// (*m_ssBlkRecord) << " " << endl;
m_savePaperSpaceHandle = getBlkRecordHandle();
(*m_ssBlkRecord) << " 0" << endl;
(*m_ssBlkRecord) << "BLOCK_RECORD" << endl;
(*m_ssBlkRecord) << " 5" << endl;
(*m_ssBlkRecord) << m_savePaperSpaceHandle << endl;
(*m_ssBlkRecord) << "330" << endl;
(*m_ssBlkRecord) << tablehash << endl;
(*m_ssBlkRecord) << "100" << endl;
(*m_ssBlkRecord) << "AcDbSymbolTableRecord" << endl;
(*m_ssBlkRecord) << "100" << endl;
(*m_ssBlkRecord) << "AcDbBlockTableRecord" << endl;
(*m_ssBlkRecord) << " 2" << endl;
(*m_ssBlkRecord) << "*PAPER_SPACE" << endl;
// (*m_ssBlkRecord) << " 1" << endl;
// (*m_ssBlkRecord) << " " << endl;
}
//***************************
// makeBlockRecordTableBody
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::makeBlockRecordTableBody()
{
if (m_version < 14) {
return;
}
int iBlkRecord = 0;
for (auto& b : m_blockList) {
(*m_ssBlkRecord) << " 0" << endl;
(*m_ssBlkRecord) << "BLOCK_RECORD" << endl;
(*m_ssBlkRecord) << " 5" << endl;
(*m_ssBlkRecord) << m_blkRecordList.at(iBlkRecord) << endl;
(*m_ssBlkRecord) << "330" << endl;
(*m_ssBlkRecord) << m_saveBlockRecordTableHandle << endl;
(*m_ssBlkRecord) << "100" << endl;
(*m_ssBlkRecord) << "AcDbSymbolTableRecord" << endl;
(*m_ssBlkRecord) << "100" << endl;
(*m_ssBlkRecord) << "AcDbBlockTableRecord" << endl;
(*m_ssBlkRecord) << " 2" << endl;
(*m_ssBlkRecord) << b << endl;
// (*m_ssBlkRecord) << " 70" << endl;
// (*m_ssBlkRecord) << " 0" << endl;
iBlkRecord++;
}
}
//***************************
// makeBlockSectionHead
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::makeBlockSectionHead()
{
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << "SECTION" << endl;
(*m_ssBlock) << " 2" << endl;
(*m_ssBlock) << "BLOCKS" << endl;
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << "BLOCK" << endl;
(*m_ssBlock) << " 5" << endl;
m_currentBlock = getBlockHandle();
(*m_ssBlock) << m_currentBlock << endl;
if (m_version > 12) {
(*m_ssBlock) << "330" << endl;
(*m_ssBlock) << m_saveModelSpaceHandle << endl;
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbEntity" << endl;
}
(*m_ssBlock) << " 8" << endl;
(*m_ssBlock) << "0" << endl;
if (m_version > 12) {
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbBlockBegin" << endl;
}
(*m_ssBlock) << " 2" << endl;
(*m_ssBlock) << "*MODEL_SPACE" << endl;
(*m_ssBlock) << " 70" << endl;
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << " 10" << endl;
(*m_ssBlock) << 0.0 << endl;
(*m_ssBlock) << " 20" << endl;
(*m_ssBlock) << 0.0 << endl;
(*m_ssBlock) << " 30" << endl;
(*m_ssBlock) << 0.0 << endl;
(*m_ssBlock) << " 3" << endl;
(*m_ssBlock) << "*MODEL_SPACE" << endl;
(*m_ssBlock) << " 1" << endl;
(*m_ssBlock) << " " << endl;
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << "ENDBLK" << endl;
(*m_ssBlock) << " 5" << endl;
(*m_ssBlock) << getBlockHandle() << endl;
if (m_version > 12) {
(*m_ssBlock) << "330" << endl;
(*m_ssBlock) << m_saveModelSpaceHandle << endl;
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbEntity" << endl;
}
(*m_ssBlock) << " 8" << endl;
(*m_ssBlock) << "0" << endl;
if (m_version > 12) {
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbBlockEnd" << endl;
}
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << "BLOCK" << endl;
(*m_ssBlock) << " 5" << endl;
m_currentBlock = getBlockHandle();
(*m_ssBlock) << m_currentBlock << endl;
if (m_version > 12) {
(*m_ssBlock) << "330" << endl;
(*m_ssBlock) << m_savePaperSpaceHandle << endl;
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbEntity" << endl;
(*m_ssBlock) << " 67" << endl;
(*m_ssBlock) << "1" << endl;
}
(*m_ssBlock) << " 8" << endl;
(*m_ssBlock) << "0" << endl;
if (m_version > 12) {
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbBlockBegin" << endl;
}
(*m_ssBlock) << " 2" << endl;
(*m_ssBlock) << "*PAPER_SPACE" << endl;
(*m_ssBlock) << " 70" << endl;
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << " 10" << endl;
(*m_ssBlock) << 0.0 << endl;
(*m_ssBlock) << " 20" << endl;
(*m_ssBlock) << 0.0 << endl;
(*m_ssBlock) << " 30" << endl;
(*m_ssBlock) << 0.0 << endl;
(*m_ssBlock) << " 3" << endl;
(*m_ssBlock) << "*PAPER_SPACE" << endl;
(*m_ssBlock) << " 1" << endl;
(*m_ssBlock) << " " << endl;
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << "ENDBLK" << endl;
(*m_ssBlock) << " 5" << endl;
(*m_ssBlock) << getBlockHandle() << endl;
if (m_version > 12) {
(*m_ssBlock) << "330" << endl;
(*m_ssBlock) << m_savePaperSpaceHandle << endl;
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbEntity" << endl;
(*m_ssBlock) << " 67" << endl; // paper_space flag
(*m_ssBlock) << " 1" << endl;
}
(*m_ssBlock) << " 8" << endl;
(*m_ssBlock) << "0" << endl;
if (m_version > 12) {
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbBlockEnd" << endl;
}
}
std::string CDxfWrite::getPlateFile(std::string fileSpec)
{
std::stringstream outString;
Base::FileInfo fi(fileSpec);
if (!fi.isReadable()) {
Base::Console().message("dxf unable to open %s!\n", fileSpec.c_str());
}
else {
string line;
ifstream inFile(fi.filePath());
while (!inFile.eof()) {
getline(inFile, line);
if (!inFile.eof()) {
outString << line << '\n';
}
}
}
return outString.str();
}
std::string CDxfWrite::getHandle()
{
m_handle++;
std::stringstream ss;
ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2);
ss << m_handle;
return ss.str();
}
std::string CDxfWrite::getEntityHandle()
{
return getHandle();
// m_entityHandle++;
// std::stringstream ss;
// ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2);
// ss << m_entityHandle;
// return ss.str();
}
std::string CDxfWrite::getLayerHandle()
{
return getHandle();
// m_layerHandle++;
// std::stringstream ss;
// ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2);
// ss << m_layerHandle;
// return ss.str();
}
std::string CDxfWrite::getBlockHandle()
{
return getHandle();
// m_blockHandle++;
// std::stringstream ss;
// ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2);
// ss << m_blockHandle;
// return ss.str();
}
std::string CDxfWrite::getBlkRecordHandle()
{
return getHandle();
// m_blkRecordHandle++;
// std::stringstream ss;
// ss << std::uppercase << std::hex << std::setfill('0') << std::setw(2);
// ss << m_blkRecordHandle;
// return ss.str();
}
void CDxfWrite::addBlockName(const std::string& name, const std::string& blkRecordHandle)
{
m_blockList.push_back(name);
m_blkRecordList.push_back(blkRecordHandle);
}
void CDxfWrite::setLayerName(std::string name)
{
m_layerName = name;
m_layerList.push_back(name);
}
void CDxfWrite::writeLine(const double* start, const double* end)
{
putLine(toVector3d(start), toVector3d(end), m_ssEntity, getEntityHandle(), m_saveModelSpaceHandle);
}
void CDxfWrite::putLine(
const Base::Vector3d& start,
const Base::Vector3d& end,
std::ostringstream* outStream,
const std::string& handle,
const std::string& ownerHandle
)
{
(*outStream) << " 0" << endl;
(*outStream) << "LINE" << endl;
(*outStream) << " 5" << endl;
(*outStream) << handle << endl;
if (m_version > 12) {
(*outStream) << "330" << endl;
(*outStream) << ownerHandle << endl;
(*outStream) << "100" << endl;
(*outStream) << "AcDbEntity" << endl;
}
(*outStream) << " 8" << endl; // Group code for layer name
(*outStream) << getLayerName() << endl; // Layer number
if (m_version > 12) {
(*outStream) << "100" << endl;
(*outStream) << "AcDbLine" << endl;
}
(*outStream) << " 10" << endl; // Start point of line
(*outStream) << start.x << endl; // X in WCS coordinates
(*outStream) << " 20" << endl;
(*outStream) << start.y << endl; // Y in WCS coordinates
(*outStream) << " 30" << endl;
(*outStream) << start.z << endl; // Z in WCS coordinates
(*outStream) << " 11" << endl; // End point of line
(*outStream) << end.x << endl; // X in WCS coordinates
(*outStream) << " 21" << endl;
(*outStream) << end.y << endl; // Y in WCS coordinates
(*outStream) << " 31" << endl;
(*outStream) << end.z << endl; // Z in WCS coordinates
}
//***************************
// writeLWPolyLine (Note: LWPolyline might not be supported in R12
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeLWPolyLine(const LWPolyDataOut& pd)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "LWPOLYLINE" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
if (m_version > 12) {
(*m_ssEntity) << "100" << endl; // 100 groups are not part of R12
(*m_ssEntity) << "AcDbPolyline" << endl;
}
(*m_ssEntity) << " 8" << endl; // Group code for layer name
(*m_ssEntity) << getLayerName() << endl; // Layer name
(*m_ssEntity) << " 90" << endl;
(*m_ssEntity) << pd.nVert << endl; // number of vertices
(*m_ssEntity) << " 70" << endl;
(*m_ssEntity) << pd.Flag << endl;
(*m_ssEntity) << " 43" << endl;
(*m_ssEntity) << "0" << endl; // Constant width opt
// (*m_ssEntity) << pd.Width << endl; //Constant width opt
// (*m_ssEntity) << " 38" << endl;
// (*m_ssEntity) << pd.Elev << endl; // Elevation
// (*m_ssEntity) << " 39" << endl;
// (*m_ssEntity) << pd.Thick << endl; // Thickness
for (auto& p : pd.Verts) {
(*m_ssEntity) << " 10" << endl; // Vertices
(*m_ssEntity) << p.x << endl;
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << p.y << endl;
}
for (auto& s : pd.StartWidth) {
(*m_ssEntity) << " 40" << endl;
(*m_ssEntity) << s << endl; // Start Width
}
for (auto& e : pd.EndWidth) {
(*m_ssEntity) << " 41" << endl;
(*m_ssEntity) << e << endl; // End Width
}
for (auto& b : pd.Bulge) { // Bulge
(*m_ssEntity) << " 42" << endl;
(*m_ssEntity) << b << endl;
}
// (*m_ssEntity) << "210" << endl; //Extrusion dir
// (*m_ssEntity) << pd.Extr.x << endl;
// (*m_ssEntity) << "220" << endl;
// (*m_ssEntity) << pd.Extr.y << endl;
// (*m_ssEntity) << "230" << endl;
// (*m_ssEntity) << pd.Extr.z << endl;
}
//***************************
// writePolyline
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writePolyline(const LWPolyDataOut& pd)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "POLYLINE" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl;
(*m_ssEntity) << getLayerName() << endl; // Layer name
if (m_version > 12) {
(*m_ssEntity) << "100" << endl; // 100 groups are not part of R12
(*m_ssEntity) << "AcDbPolyline" << endl;
}
(*m_ssEntity) << " 66" << endl;
(*m_ssEntity) << " 1" << endl; // vertices follow
(*m_ssEntity) << " 10" << endl;
(*m_ssEntity) << "0.0" << endl;
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << "0.0" << endl;
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << "0.0" << endl;
(*m_ssEntity) << " 70" << endl;
(*m_ssEntity) << "0" << endl;
for (auto& p : pd.Verts) {
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "VERTEX" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
(*m_ssEntity) << " 8" << endl;
(*m_ssEntity) << getLayerName() << endl;
(*m_ssEntity) << " 10" << endl;
(*m_ssEntity) << p.x << endl;
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << p.y << endl;
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << p.z << endl;
}
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "SEQEND" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
(*m_ssEntity) << " 8" << endl;
(*m_ssEntity) << getLayerName() << endl;
}
void CDxfWrite::writePoint(const double* point)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "POINT" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl; // Group code for layer name
(*m_ssEntity) << getLayerName() << endl; // Layer name
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbPoint" << endl;
}
(*m_ssEntity) << " 10" << endl;
(*m_ssEntity) << point[0] << endl; // X in WCS coordinates
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << point[1] << endl; // Y in WCS coordinates
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << point[2] << endl; // Z in WCS coordinates
}
//! arc from 3 points - start, end, center. dir true if arc is AntiClockwise. unspecified assumption
//! is that points are on XY plane in coord system OXYZ.
void CDxfWrite::writeArc(const double* start, const double* end, const double* center, bool dir)
{
double ax = start[0] - center[0];
double ay = start[1] - center[1];
double bx = end[0] - center[0];
double by = end[1] - center[1];
double start_angle = Base::toDegrees(atan2(ay, ax));
double end_angle = Base::toDegrees(atan2(by, bx));
double radius = sqrt(ax * ax + ay * ay);
if (!dir) {
double temp = start_angle;
start_angle = end_angle;
end_angle = temp;
}
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "ARC" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl; // Group code for layer name
(*m_ssEntity) << getLayerName() << endl; // Layer number
// (*m_ssEntity) << " 62" << endl;
// (*m_ssEntity) << " 0" << endl;
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbCircle" << endl;
}
(*m_ssEntity) << " 10" << endl; // Centre X
(*m_ssEntity) << center[0] << endl; // X in WCS coordinates
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << center[1] << endl; // Y in WCS coordinates
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << center[2] << endl; // Z in WCS coordinates
(*m_ssEntity) << " 40" << endl; //
(*m_ssEntity) << radius << endl; // Radius
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbArc" << endl;
}
(*m_ssEntity) << " 50" << endl;
(*m_ssEntity) << start_angle << endl; // Start angle
(*m_ssEntity) << " 51" << endl;
(*m_ssEntity) << end_angle << endl; // End angle
}
void CDxfWrite::writeCircle(const double* center, double radius)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "CIRCLE" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl; // Group code for layer name
(*m_ssEntity) << getLayerName() << endl; // Layer number
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbCircle" << endl;
}
(*m_ssEntity) << " 10" << endl; // Centre X
(*m_ssEntity) << center[0] << endl; // X in WCS coordinates
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << center[1] << endl; // Y in WCS coordinates
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << center[2] << endl; // Z in WCS coordinates
(*m_ssEntity) << " 40" << endl; //
(*m_ssEntity) << radius << endl; // Radius
}
void CDxfWrite::writeEllipse(
const double* center,
double major_radius,
double minor_radius,
double rotation,
double start_angle,
double end_angle,
bool endIsCW
)
{
Base::Vector3d m(major_radius * sin(rotation), major_radius * cos(rotation), 0);
double ratio = minor_radius / major_radius;
if (!endIsCW) { // end is NOT CW from start
double temp = start_angle;
start_angle = end_angle;
end_angle = temp;
}
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "ELLIPSE" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl; // Group code for layer name
(*m_ssEntity) << getLayerName() << endl; // Layer number
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEllipse" << endl;
}
(*m_ssEntity) << " 10" << endl; // Centre X
(*m_ssEntity) << center[0] << endl; // X in WCS coordinates
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << center[1] << endl; // Y in WCS coordinates
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << center[2] << endl; // Z in WCS coordinates
(*m_ssEntity) << " 11" << endl; //
(*m_ssEntity) << m.x << endl; // Major X
(*m_ssEntity) << " 21" << endl;
(*m_ssEntity) << m.y << endl; // Major Y
(*m_ssEntity) << " 31" << endl;
(*m_ssEntity) << m.z << endl; // Major Z
(*m_ssEntity) << " 40" << endl; //
(*m_ssEntity) << ratio << endl; // Ratio
// (*m_ssEntity) << "210" << endl; //extrusion dir??
// (*m_ssEntity) << "0" << endl;
// (*m_ssEntity) << "220" << endl;
// (*m_ssEntity) << "0" << endl;
// (*m_ssEntity) << "230" << endl;
// (*m_ssEntity) << "1" << endl;
(*m_ssEntity) << " 41" << endl;
(*m_ssEntity) << start_angle << endl; // Start angle (radians [0..2pi])
(*m_ssEntity) << " 42" << endl;
(*m_ssEntity) << end_angle << endl; // End angle
}
//***************************
// writeSpline
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeSpline(const SplineDataOut& sd)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "SPLINE" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl; // Group code for layer name
(*m_ssEntity) << getLayerName() << endl; // Layer name
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbSpline" << endl;
}
(*m_ssEntity) << "210" << endl;
(*m_ssEntity) << "0" << endl;
(*m_ssEntity) << "220" << endl;
(*m_ssEntity) << "0" << endl;
(*m_ssEntity) << "230" << endl;
(*m_ssEntity) << "1" << endl;
(*m_ssEntity) << " 70" << endl;
(*m_ssEntity) << sd.flag << endl; // flags
(*m_ssEntity) << " 71" << endl;
(*m_ssEntity) << sd.degree << endl;
(*m_ssEntity) << " 72" << endl;
(*m_ssEntity) << sd.knots << endl;
(*m_ssEntity) << " 73" << endl;
(*m_ssEntity) << sd.control_points << endl;
(*m_ssEntity) << " 74" << endl;
(*m_ssEntity) << 0 << endl;
// (*m_ssEntity) << " 12" << endl;
// (*m_ssEntity) << sd.starttan.x << endl;
// (*m_ssEntity) << " 22" << endl;
// (*m_ssEntity) << sd.starttan.y << endl;
// (*m_ssEntity) << " 32" << endl;
// (*m_ssEntity) << sd.starttan.z << endl;
// (*m_ssEntity) << " 13" << endl;
// (*m_ssEntity) << sd.endtan.x << endl;
// (*m_ssEntity) << " 23" << endl;
// (*m_ssEntity) << sd.endtan.y << endl;
// (*m_ssEntity) << " 33" << endl;
// (*m_ssEntity) << sd.endtan.z << endl;
for (auto& k : sd.knot) {
(*m_ssEntity) << " 40" << endl;
(*m_ssEntity) << k << endl;
}
for (auto& w : sd.weight) {
(*m_ssEntity) << " 41" << endl;
(*m_ssEntity) << w << endl;
}
for (auto& center : sd.control) {
(*m_ssEntity) << " 10" << endl;
(*m_ssEntity) << center.x << endl; // X in WCS coordinates
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << center.y << endl; // Y in WCS coordinates
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << center.z << endl; // Z in WCS coordinates
}
for (auto& f : sd.fit) {
(*m_ssEntity) << " 11" << endl;
(*m_ssEntity) << f.x << endl; // X in WCS coordinates
(*m_ssEntity) << " 21" << endl;
(*m_ssEntity) << f.y << endl; // Y in WCS coordinates
(*m_ssEntity) << " 31" << endl;
(*m_ssEntity) << f.z << endl; // Z in WCS coordinates
}
}
//***************************
// writeVertex
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeVertex(double x, double y, double z)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "VERTEX" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl;
(*m_ssEntity) << getLayerName() << endl;
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbVertex" << endl;
}
(*m_ssEntity) << " 10" << endl;
(*m_ssEntity) << x << endl;
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << y << endl;
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << z << endl;
(*m_ssEntity) << " 70" << endl;
(*m_ssEntity) << 0 << endl;
}
void CDxfWrite::writeText(
const char* text,
const double* location1,
const double* location2,
const double height,
const int horizJust
)
{
putText(
text,
toVector3d(location1),
toVector3d(location2),
height,
horizJust,
m_ssEntity,
getEntityHandle(),
m_saveModelSpaceHandle
);
}
//***************************
// putText
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::putText(
const char* text,
const Base::Vector3d& location1,
const Base::Vector3d& location2,
const double height,
const int horizJust,
std::ostringstream* outStream,
const std::string& handle,
const std::string& ownerHandle
)
{
(void)location2;
(*outStream) << " 0" << endl;
(*outStream) << "TEXT" << endl;
(*outStream) << " 5" << endl;
(*outStream) << handle << endl;
if (m_version > 12) {
(*outStream) << "330" << endl;
(*outStream) << ownerHandle << endl;
(*outStream) << "100" << endl;
(*outStream) << "AcDbEntity" << endl;
}
(*outStream) << " 8" << endl;
(*outStream) << getLayerName() << endl;
if (m_version > 12) {
(*outStream) << "100" << endl;
(*outStream) << "AcDbText" << endl;
}
// (*outStream) << " 39" << endl;
// (*outStream) << 0 << endl; //thickness
(*outStream) << " 10" << endl; // first alignment point
(*outStream) << location1.x << endl;
(*outStream) << " 20" << endl;
(*outStream) << location1.y << endl;
(*outStream) << " 30" << endl;
(*outStream) << location1.z << endl;
(*outStream) << " 40" << endl;
(*outStream) << height << endl;
(*outStream) << " 1" << endl;
(*outStream) << text << endl;
// (*outStream) << " 50" << endl;
// (*outStream) << 0 << endl; //rotation
// (*outStream) << " 41" << endl;
// (*outStream) << 1 << endl;
// (*outStream) << " 51" << endl;
// (*outStream) << 0 << endl;
(*outStream) << " 7" << endl;
(*outStream) << "STANDARD" << endl; // style
// (*outStream) << " 71" << endl; //default
// (*outStream) << "0" << endl;
(*outStream) << " 72" << endl;
(*outStream) << horizJust << endl;
//// (*outStream) << " 73" << endl;
//// (*outStream) << "0" << endl;
(*outStream) << " 11" << endl; // second alignment point
(*outStream) << location2.x << endl;
(*outStream) << " 21" << endl;
(*outStream) << location2.y << endl;
(*outStream) << " 31" << endl;
(*outStream) << location2.z << endl;
// (*outStream) << "210" << endl;
// (*outStream) << "0" << endl;
// (*outStream) << "220" << endl;
// (*outStream) << "0" << endl;
// (*outStream) << "230" << endl;
// (*outStream) << "1" << endl;
if (m_version > 12) {
(*outStream) << "100" << endl;
(*outStream) << "AcDbText" << endl;
}
}
void CDxfWrite::putArrow(
Base::Vector3d& arrowPos,
Base::Vector3d& barb1Pos,
Base::Vector3d& barb2Pos,
std::ostringstream* outStream,
const std::string& handle,
const std::string& ownerHandle
)
{
(*outStream) << " 0" << endl;
(*outStream) << "SOLID" << endl;
(*outStream) << " 5" << endl;
(*outStream) << handle << endl;
if (m_version > 12) {
(*outStream) << "330" << endl;
(*outStream) << ownerHandle << endl;
(*outStream) << "100" << endl;
(*outStream) << "AcDbEntity" << endl;
}
(*outStream) << " 8" << endl;
(*outStream) << "0" << endl;
(*outStream) << " 62" << endl;
(*outStream) << " 0" << endl;
if (m_version > 12) {
(*outStream) << "100" << endl;
(*outStream) << "AcDbTrace" << endl;
}
(*outStream) << " 10" << endl;
(*outStream) << barb1Pos.x << endl;
(*outStream) << " 20" << endl;
(*outStream) << barb1Pos.y << endl;
(*outStream) << " 30" << endl;
(*outStream) << barb1Pos.z << endl;
(*outStream) << " 11" << endl;
(*outStream) << barb2Pos.x << endl;
(*outStream) << " 21" << endl;
(*outStream) << barb2Pos.y << endl;
(*outStream) << " 31" << endl;
(*outStream) << barb2Pos.z << endl;
(*outStream) << " 12" << endl;
(*outStream) << arrowPos.x << endl;
(*outStream) << " 22" << endl;
(*outStream) << arrowPos.y << endl;
(*outStream) << " 32" << endl;
(*outStream) << arrowPos.z << endl;
(*outStream) << " 13" << endl;
(*outStream) << arrowPos.x << endl;
(*outStream) << " 23" << endl;
(*outStream) << arrowPos.y << endl;
(*outStream) << " 33" << endl;
(*outStream) << arrowPos.z << endl;
}
//***************************
// writeLinearDim
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
#define ALIGNED 0
#define HORIZONTAL 1
#define VERTICAL 2
void CDxfWrite::writeLinearDim(
const double* textMidPoint,
const double* lineDefPoint,
const double* extLine1,
const double* extLine2,
const char* dimText,
int type
)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "DIMENSION" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl;
(*m_ssEntity) << getLayerName() << endl;
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbDimension" << endl;
}
(*m_ssEntity) << " 2" << endl;
(*m_ssEntity) << "*" << getLayerName() << endl; // blockName
(*m_ssEntity) << " 10" << endl; // dimension line definition point
(*m_ssEntity) << lineDefPoint[0] << endl;
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << lineDefPoint[1] << endl;
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << lineDefPoint[2] << endl;
(*m_ssEntity) << " 11" << endl; // text mid point
(*m_ssEntity) << textMidPoint[0] << endl;
(*m_ssEntity) << " 21" << endl;
(*m_ssEntity) << textMidPoint[1] << endl;
(*m_ssEntity) << " 31" << endl;
(*m_ssEntity) << textMidPoint[2] << endl;
if (type == ALIGNED) {
(*m_ssEntity) << " 70" << endl;
(*m_ssEntity) << 1 << endl; // dimType1 = Aligned
}
if ((type == HORIZONTAL) || (type == VERTICAL)) {
(*m_ssEntity) << " 70" << endl;
(*m_ssEntity) << 32 << endl; // dimType0 = Aligned + 32 (bit for unique block)?
}
// (*m_ssEntity) << " 71" << endl; // not R12
// (*m_ssEntity) << 1 << endl; // attachPoint ??1 = topleft
(*m_ssEntity) << " 1" << endl;
(*m_ssEntity) << dimText << endl;
(*m_ssEntity) << " 3" << endl;
(*m_ssEntity) << "STANDARD" << endl; // style
// linear dims
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbAlignedDimension" << endl;
}
(*m_ssEntity) << " 13" << endl;
(*m_ssEntity) << extLine1[0] << endl;
(*m_ssEntity) << " 23" << endl;
(*m_ssEntity) << extLine1[1] << endl;
(*m_ssEntity) << " 33" << endl;
(*m_ssEntity) << extLine1[2] << endl;
(*m_ssEntity) << " 14" << endl;
(*m_ssEntity) << extLine2[0] << endl;
(*m_ssEntity) << " 24" << endl;
(*m_ssEntity) << extLine2[1] << endl;
(*m_ssEntity) << " 34" << endl;
(*m_ssEntity) << extLine2[2] << endl;
if (m_version > 12) {
if (type == VERTICAL) {
(*m_ssEntity) << " 50" << endl;
(*m_ssEntity) << "90" << endl;
}
if ((type == HORIZONTAL) || (type == VERTICAL)) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbRotatedDimension" << endl;
}
}
writeDimBlockPreamble();
writeLinearDimBlock(textMidPoint, lineDefPoint, extLine1, extLine2, dimText, type);
writeBlockTrailer();
}
//***************************
// writeAngularDim
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeAngularDim(
const double* textMidPoint,
const double* lineDefPoint,
const double* startExt1,
const double* endExt1,
const double* startExt2,
const double* endExt2,
const char* dimText
)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "DIMENSION" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl;
(*m_ssEntity) << getLayerName() << endl;
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbDimension" << endl;
}
(*m_ssEntity) << " 2" << endl;
(*m_ssEntity) << "*" << getLayerName() << endl; // blockName
(*m_ssEntity) << " 10" << endl;
(*m_ssEntity) << endExt2[0] << endl;
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << endExt2[1] << endl;
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << endExt2[2] << endl;
(*m_ssEntity) << " 11" << endl;
(*m_ssEntity) << textMidPoint[0] << endl;
(*m_ssEntity) << " 21" << endl;
(*m_ssEntity) << textMidPoint[1] << endl;
(*m_ssEntity) << " 31" << endl;
(*m_ssEntity) << textMidPoint[2] << endl;
(*m_ssEntity) << " 70" << endl;
(*m_ssEntity) << 2 << endl; // dimType 2 = Angular 5 = Angular 3 point
// +32 for block?? (not R12)
// (*m_ssEntity) << " 71" << endl; // not R12? not required?
// (*m_ssEntity) << 5 << endl; // attachPoint 5 = middle
(*m_ssEntity) << " 1" << endl;
(*m_ssEntity) << dimText << endl;
(*m_ssEntity) << " 3" << endl;
(*m_ssEntity) << "STANDARD" << endl; // style
// angular dims
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDb2LineAngularDimension" << endl;
}
(*m_ssEntity) << " 13" << endl;
(*m_ssEntity) << startExt1[0] << endl;
(*m_ssEntity) << " 23" << endl;
(*m_ssEntity) << startExt1[1] << endl;
(*m_ssEntity) << " 33" << endl;
(*m_ssEntity) << startExt1[2] << endl;
(*m_ssEntity) << " 14" << endl;
(*m_ssEntity) << endExt1[0] << endl;
(*m_ssEntity) << " 24" << endl;
(*m_ssEntity) << endExt1[1] << endl;
(*m_ssEntity) << " 34" << endl;
(*m_ssEntity) << endExt1[2] << endl;
(*m_ssEntity) << " 15" << endl;
(*m_ssEntity) << startExt2[0] << endl;
(*m_ssEntity) << " 25" << endl;
(*m_ssEntity) << startExt2[1] << endl;
(*m_ssEntity) << " 35" << endl;
(*m_ssEntity) << startExt2[2] << endl;
(*m_ssEntity) << " 16" << endl;
(*m_ssEntity) << lineDefPoint[0] << endl;
(*m_ssEntity) << " 26" << endl;
(*m_ssEntity) << lineDefPoint[1] << endl;
(*m_ssEntity) << " 36" << endl;
(*m_ssEntity) << lineDefPoint[2] << endl;
writeDimBlockPreamble();
writeAngularDimBlock(textMidPoint, lineDefPoint, startExt1, endExt1, startExt2, endExt2, dimText);
writeBlockTrailer();
}
//***************************
// writeRadialDim
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeRadialDim(
const double* centerPoint,
const double* textMidPoint,
const double* arcPoint,
const char* dimText
)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "DIMENSION" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl;
(*m_ssEntity) << getLayerName() << endl;
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbDimension" << endl;
}
(*m_ssEntity) << " 2" << endl;
(*m_ssEntity) << "*" << getLayerName() << endl; // blockName
(*m_ssEntity) << " 10" << endl; // arc center point
(*m_ssEntity) << centerPoint[0] << endl;
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << centerPoint[1] << endl;
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << centerPoint[2] << endl;
(*m_ssEntity) << " 11" << endl; // text mid point
(*m_ssEntity) << textMidPoint[0] << endl;
(*m_ssEntity) << " 21" << endl;
(*m_ssEntity) << textMidPoint[1] << endl;
(*m_ssEntity) << " 31" << endl;
(*m_ssEntity) << textMidPoint[2] << endl;
(*m_ssEntity) << " 70" << endl;
(*m_ssEntity) << 4 << endl; // dimType 4 = Radius
// (*m_ssEntity) << " 71" << endl; // not R12
// (*m_ssEntity) << 1 << endl; // attachPoint 5 = middle center
(*m_ssEntity) << " 1" << endl;
(*m_ssEntity) << dimText << endl;
(*m_ssEntity) << " 3" << endl;
(*m_ssEntity) << "STANDARD" << endl; // style
// radial dims
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbRadialDimension" << endl;
}
(*m_ssEntity) << " 15" << endl;
(*m_ssEntity) << arcPoint[0] << endl;
(*m_ssEntity) << " 25" << endl;
(*m_ssEntity) << arcPoint[1] << endl;
(*m_ssEntity) << " 35" << endl;
(*m_ssEntity) << arcPoint[2] << endl;
(*m_ssEntity) << " 40" << endl; // leader length????
(*m_ssEntity) << 0 << endl;
writeDimBlockPreamble();
writeRadialDimBlock(centerPoint, textMidPoint, arcPoint, dimText);
writeBlockTrailer();
}
//***************************
// writeDiametricDim
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeDiametricDim(
const double* textMidPoint,
const double* arcPoint1,
const double* arcPoint2,
const char* dimText
)
{
(*m_ssEntity) << " 0" << endl;
(*m_ssEntity) << "DIMENSION" << endl;
(*m_ssEntity) << " 5" << endl;
(*m_ssEntity) << getEntityHandle() << endl;
if (m_version > 12) {
(*m_ssEntity) << "330" << endl;
(*m_ssEntity) << m_saveModelSpaceHandle << endl;
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbEntity" << endl;
}
(*m_ssEntity) << " 8" << endl;
(*m_ssEntity) << getLayerName() << endl;
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbDimension" << endl;
}
(*m_ssEntity) << " 2" << endl;
(*m_ssEntity) << "*" << getLayerName() << endl; // blockName
(*m_ssEntity) << " 10" << endl;
(*m_ssEntity) << arcPoint1[0] << endl;
(*m_ssEntity) << " 20" << endl;
(*m_ssEntity) << arcPoint1[1] << endl;
(*m_ssEntity) << " 30" << endl;
(*m_ssEntity) << arcPoint1[2] << endl;
(*m_ssEntity) << " 11" << endl; // text mid point
(*m_ssEntity) << textMidPoint[0] << endl;
(*m_ssEntity) << " 21" << endl;
(*m_ssEntity) << textMidPoint[1] << endl;
(*m_ssEntity) << " 31" << endl;
(*m_ssEntity) << textMidPoint[2] << endl;
(*m_ssEntity) << " 70" << endl;
(*m_ssEntity) << 3 << endl; // dimType 3 = Diameter
// (*m_ssEntity) << " 71" << endl; // not R12
// (*m_ssEntity) << 5 << endl; // attachPoint 5 = middle center
(*m_ssEntity) << " 1" << endl;
(*m_ssEntity) << dimText << endl;
(*m_ssEntity) << " 3" << endl;
(*m_ssEntity) << "STANDARD" << endl; // style
// diametric dims
if (m_version > 12) {
(*m_ssEntity) << "100" << endl;
(*m_ssEntity) << "AcDbDiametricDimension" << endl;
}
(*m_ssEntity) << " 15" << endl;
(*m_ssEntity) << arcPoint2[0] << endl;
(*m_ssEntity) << " 25" << endl;
(*m_ssEntity) << arcPoint2[1] << endl;
(*m_ssEntity) << " 35" << endl;
(*m_ssEntity) << arcPoint2[2] << endl;
(*m_ssEntity) << " 40" << endl; // leader length????
(*m_ssEntity) << 0 << endl;
writeDimBlockPreamble();
writeDiametricDimBlock(textMidPoint, arcPoint1, arcPoint2, dimText);
writeBlockTrailer();
}
//***************************
// writeDimBlockPreamble
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeDimBlockPreamble()
{
if (m_version > 12) {
std::string blockName("*");
blockName += getLayerName();
m_saveBlkRecordHandle = getBlkRecordHandle();
addBlockName(blockName, m_saveBlkRecordHandle);
}
m_currentBlock = getBlockHandle();
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << "BLOCK" << endl;
(*m_ssBlock) << " 5" << endl;
(*m_ssBlock) << m_currentBlock << endl;
if (m_version > 12) {
(*m_ssBlock) << "330" << endl;
(*m_ssBlock) << m_saveBlkRecordHandle << endl;
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbEntity" << endl;
}
(*m_ssBlock) << " 8" << endl;
(*m_ssBlock) << getLayerName() << endl;
if (m_version > 12) {
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbBlockBegin" << endl;
}
(*m_ssBlock) << " 2" << endl;
(*m_ssBlock) << "*" << getLayerName() << endl; // blockName
(*m_ssBlock) << " 70" << endl;
(*m_ssBlock) << " 1" << endl;
(*m_ssBlock) << " 10" << endl;
(*m_ssBlock) << 0.0 << endl;
(*m_ssBlock) << " 20" << endl;
(*m_ssBlock) << 0.0 << endl;
(*m_ssBlock) << " 30" << endl;
(*m_ssBlock) << 0.0 << endl;
(*m_ssBlock) << " 3" << endl;
(*m_ssBlock) << "*" << getLayerName() << endl; // blockName
(*m_ssBlock) << " 1" << endl;
(*m_ssBlock) << " " << endl;
}
//***************************
// writeBlockTrailer
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeBlockTrailer()
{
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << "ENDBLK" << endl;
(*m_ssBlock) << " 5" << endl;
(*m_ssBlock) << getBlockHandle() << endl;
if (m_version > 12) {
(*m_ssBlock) << "330" << endl;
(*m_ssBlock) << m_saveBlkRecordHandle << endl;
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbEntity" << endl;
}
// (*m_ssBlock) << " 67" << endl;
// (*m_ssBlock) << "1" << endl;
(*m_ssBlock) << " 8" << endl;
(*m_ssBlock) << getLayerName() << endl;
if (m_version > 12) {
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbBlockEnd" << endl;
}
}
//***************************
// writeLinearDimBlock
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeLinearDimBlock(
const double* textMidPoint,
const double* lineDefPoint,
const double* extLine1,
const double* extLine2,
const char* dimText,
int type
)
{
Base::Vector3d e1S(MakeVector3d(extLine1));
Base::Vector3d e2S(MakeVector3d(extLine2));
// point on DimLine (somewhere!)
Base::Vector3d dl(MakeVector3d(lineDefPoint));
Base::Vector3d perp = dl.DistanceToLineSegment(e2S, e1S);
Base::Vector3d e1E = e1S - perp;
Base::Vector3d e2E = e2S - perp;
Base::Vector3d para = e1E - e2E;
if (type == ALIGNED) {
// NOP
}
else if (type == HORIZONTAL) {
double x = extLine1[0];
double y = lineDefPoint[1];
e1E = Base::Vector3d(x, y, 0.0);
x = extLine2[0];
e2E = Base::Vector3d(x, y, 0.0);
perp = Base::Vector3d(0, -1, 0); // down
para = Base::Vector3d(1, 0, 0); // right
if (lineDefPoint[1] > extLine1[1]) {
perp = Base::Vector3d(0, 1, 0); // up
}
if (extLine1[0] > extLine2[0]) {
para = Base::Vector3d(-1, 0, 0); // left
}
}
else if (type == VERTICAL) {
double x = lineDefPoint[0];
double y = extLine1[1];
e1E = Base::Vector3d(x, y, 0.0);
y = extLine2[1];
e2E = Base::Vector3d(x, y, 0.0);
perp = Base::Vector3d(1, 0, 0);
para = Base::Vector3d(0, 1, 0);
if (lineDefPoint[0] < extLine1[0]) {
perp = Base::Vector3d(-1, 0, 0);
}
if (extLine1[1] > extLine2[1]) {
para = Base::Vector3d(0, -1, 0);
}
}
double arrowLen = 5.0; // magic number
double arrowWidth = arrowLen / 6.0 / 2.0; // magic number calc!
putLine(e2S, e2E, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
putLine(e1S, e1E, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
putLine(e1E, e2E, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
putText(
dimText,
toVector3d(textMidPoint),
toVector3d(lineDefPoint),
3.5,
1,
m_ssBlock,
getBlockHandle(),
m_saveBlkRecordHandle
);
perp.Normalize();
para.Normalize();
Base::Vector3d arrowStart = e1E;
Base::Vector3d barb1 = arrowStart + perp * arrowWidth - para * arrowLen;
Base::Vector3d barb2 = arrowStart - perp * arrowWidth - para * arrowLen;
putArrow(arrowStart, barb1, barb2, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
arrowStart = e2E;
barb1 = arrowStart + perp * arrowWidth + para * arrowLen;
barb2 = arrowStart - perp * arrowWidth + para * arrowLen;
putArrow(arrowStart, barb1, barb2, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
}
//***************************
// writeAngularDimBlock
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeAngularDimBlock(
const double* textMidPoint,
const double* lineDefPoint,
const double* startExt1,
const double* endExt1,
const double* startExt2,
const double* endExt2,
const char* dimText
)
{
Base::Vector3d e1S(MakeVector3d(startExt1)); // apex
Base::Vector3d e2S(MakeVector3d(startExt2));
Base::Vector3d e1E(MakeVector3d(endExt1));
Base::Vector3d e2E(MakeVector3d(endExt2));
Base::Vector3d e1 = e1E - e1S;
Base::Vector3d e2 = e2E - e2S;
double startAngle = atan2(e2.y, e2.x);
double endAngle = atan2(e1.y, e1.x);
double span = fabs(endAngle - startAngle);
double offset = span * 0.10;
if (startAngle < 0) {
startAngle += 2 * std::numbers::pi;
}
if (endAngle < 0) {
endAngle += 2 * std::numbers::pi;
}
Base::Vector3d startOff(cos(startAngle + offset), sin(startAngle + offset), 0.0);
Base::Vector3d endOff(cos(endAngle - offset), sin(endAngle - offset), 0.0);
startAngle = Base::toDegrees(startAngle);
endAngle = Base::toDegrees(endAngle);
Base::Vector3d linePt(MakeVector3d(lineDefPoint));
double radius = (e2S - linePt).Length();
(*m_ssBlock) << " 0" << endl;
(*m_ssBlock) << "ARC" << endl; // dimline arc
(*m_ssBlock) << " 5" << endl;
(*m_ssBlock) << getBlockHandle() << endl;
if (m_version > 12) {
(*m_ssBlock) << "330" << endl;
(*m_ssBlock) << m_saveBlkRecordHandle << endl;
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbEntity" << endl;
}
(*m_ssBlock) << " 8" << endl;
(*m_ssBlock) << "0" << endl;
// (*m_ssBlock) << " 62" << endl;
// (*m_ssBlock) << " 0" << endl;
if (m_version > 12) {
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbCircle" << endl;
}
(*m_ssBlock) << " 10" << endl;
(*m_ssBlock) << startExt2[0] << endl; // arc center
(*m_ssBlock) << " 20" << endl;
(*m_ssBlock) << startExt2[1] << endl;
(*m_ssBlock) << " 30" << endl;
(*m_ssBlock) << startExt2[2] << endl;
(*m_ssBlock) << " 40" << endl;
(*m_ssBlock) << radius << endl; // radius
if (m_version > 12) {
(*m_ssBlock) << "100" << endl;
(*m_ssBlock) << "AcDbArc" << endl;
}
(*m_ssBlock) << " 50" << endl;
(*m_ssBlock) << startAngle << endl; // start angle
(*m_ssBlock) << " 51" << endl;
(*m_ssBlock) << endAngle << endl; // end angle
putText(
dimText,
toVector3d(textMidPoint),
toVector3d(textMidPoint),
3.5,
1,
m_ssBlock,
getBlockHandle(),
m_saveBlkRecordHandle
);
e1.Normalize();
e2.Normalize();
Base::Vector3d arrow1Start = e1S + e1 * radius;
Base::Vector3d arrow2Start = e2S + e2 * radius;
// wf: idk why the Tan pts have to be reversed. something to do with CW angles in Dxf?
Base::Vector3d endTan = e1S + (startOff * radius);
Base::Vector3d startTan = e2S + (endOff * radius);
Base::Vector3d tanP1 = (arrow1Start - startTan).Normalize();
Base::Vector3d perp1(-tanP1.y, tanP1.x, tanP1.z);
Base::Vector3d tanP2 = (arrow2Start - endTan).Normalize();
Base::Vector3d perp2(-tanP2.y, tanP2.x, tanP2.z);
double arrowLen = 5.0; // magic number
double arrowWidth = arrowLen / 6.0 / 2.0; // magic number calc!
Base::Vector3d barb1 = arrow1Start + perp1 * arrowWidth - tanP1 * arrowLen;
Base::Vector3d barb2 = arrow1Start - perp1 * arrowWidth - tanP1 * arrowLen;
putArrow(arrow1Start, barb1, barb2, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
barb1 = arrow2Start + perp2 * arrowWidth - tanP2 * arrowLen;
barb2 = arrow2Start - perp2 * arrowWidth - tanP2 * arrowLen;
putArrow(arrow2Start, barb1, barb2, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
}
//***************************
// writeRadialDimBlock
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeRadialDimBlock(
const double* centerPoint,
const double* textMidPoint,
const double* arcPoint,
const char* dimText
)
{
putLine(
toVector3d(centerPoint),
toVector3d(arcPoint),
m_ssBlock,
getBlockHandle(),
m_saveBlkRecordHandle
);
putText(
dimText,
toVector3d(textMidPoint),
toVector3d(textMidPoint),
3.5,
1,
m_ssBlock,
getBlockHandle(),
m_saveBlkRecordHandle
);
Base::Vector3d center(MakeVector3d(centerPoint));
Base::Vector3d a(MakeVector3d(arcPoint));
Base::Vector3d para = a - center;
double arrowLen = 5.0; // magic number
double arrowWidth = arrowLen / 6.0 / 2.0; // magic number calc!
para.Normalize();
Base::Vector3d perp(-para.y, para.x, para.z);
Base::Vector3d arrowStart = a;
Base::Vector3d barb1 = arrowStart + perp * arrowWidth - para * arrowLen;
Base::Vector3d barb2 = arrowStart - perp * arrowWidth - para * arrowLen;
putArrow(arrowStart, barb1, barb2, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
}
//***************************
// writeDiametricDimBlock
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeDiametricDimBlock(
const double* textMidPoint,
const double* arcPoint1,
const double* arcPoint2,
const char* dimText
)
{
putLine(toVector3d(arcPoint1), toVector3d(arcPoint2), m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
putText(
dimText,
toVector3d(textMidPoint),
toVector3d(textMidPoint),
3.5,
1,
m_ssBlock,
getBlockHandle(),
m_saveBlkRecordHandle
);
Base::Vector3d a1(MakeVector3d(arcPoint1));
Base::Vector3d a2(MakeVector3d(arcPoint2));
Base::Vector3d para = a2 - a1;
double arrowLen = 5.0; // magic number
double arrowWidth = arrowLen / 6.0 / 2.0; // magic number calc!
para.Normalize();
Base::Vector3d perp(-para.y, para.x, para.z);
Base::Vector3d arrowStart = a1;
Base::Vector3d barb1 = arrowStart + perp * arrowWidth + para * arrowLen;
Base::Vector3d barb2 = arrowStart - perp * arrowWidth + para * arrowLen;
putArrow(arrowStart, barb1, barb2, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
arrowStart = a2;
barb1 = arrowStart + perp * arrowWidth - para * arrowLen;
barb2 = arrowStart - perp * arrowWidth - para * arrowLen;
putArrow(arrowStart, barb1, barb2, m_ssBlock, getBlockHandle(), m_saveBlkRecordHandle);
}
//***************************
// writeBlocksSection
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeBlocksSection()
{
if (m_version < 14) {
std::stringstream ss;
ss << "blocks1" << m_version << ".rub";
std::string fileSpec = m_dataDir + ss.str();
(*m_ofs) << getPlateFile(fileSpec);
}
// write blocks content
(*m_ofs) << (*m_ssBlock).str();
(*m_ofs) << " 0" << endl;
(*m_ofs) << "ENDSEC" << endl;
}
//***************************
// writeEntitiesSection
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeEntitiesSection()
{
std::stringstream ss;
ss << "entities" << m_version << ".rub";
std::string fileSpec = m_dataDir + ss.str();
(*m_ofs) << getPlateFile(fileSpec);
// write entities content
(*m_ofs) << (*m_ssEntity).str();
(*m_ofs) << " 0" << endl;
(*m_ofs) << "ENDSEC" << endl;
}
//***************************
// writeObjectsSection
// added by Wandererfan 2018 (wandererfan@gmail.com) for FreeCAD project
void CDxfWrite::writeObjectsSection()
{
if (m_version < 14) {
return;
}
std::stringstream ss;
ss << "objects" << m_version << ".rub";
std::string fileSpec = m_dataDir + ss.str();
(*m_ofs) << getPlateFile(fileSpec);
}
const DxfUnits DxfUnits::Instance;
CDxfRead::CDxfRead(const std::string& filepath)
: m_ifs(new ifstream(filepath))
{
if (!(*m_ifs)) {
m_fail = true;
ImportError("DXF file didn't load\n");
return;
}
m_ifs->imbue(std::locale("C"));
}
CDxfRead::~CDxfRead()
{
delete m_ifs;
// Delete the Layer objects which are referenced by pointer from the Layers table.
for (auto& pair : Layers) {
delete pair.second;
}
}
// Static member initializers
const std::string CDxfRead::LineTypeByLayer("BYLAYER"); // NOLINT(runtime/string)
const std::string CDxfRead::LineTypeByBlock("BYBLOCK"); // NOLINT(runtime/string)
const std::string CDxfRead::DefaultLineType("CONTINUOUS"); // NOLINT(runtime/string)
//
// Setup for ProcessCommonEntityAttribute
void CDxfRead::Setup3DVectorAttribute(eDXFGroupCode_t x_record_type, Base::Vector3d& destination)
{
SetupScaledDoubleAttribute((eDXFGroupCode_t)(x_record_type + eXOffset), destination.x);
SetupScaledDoubleAttribute((eDXFGroupCode_t)(x_record_type + eYOffset), destination.y);
SetupScaledDoubleAttribute((eDXFGroupCode_t)(x_record_type + eZOffset), destination.z);
}
void CDxfRead::Setup3DCoordinatesIntoLists(
eDXFGroupCode_t x_record_type,
list<double>& x_destination,
list<double>& y_destination,
list<double>& z_destination
)
{
SetupScaledDoubleIntoList((eDXFGroupCode_t)(x_record_type + eXOffset), x_destination);
SetupScaledDoubleIntoList((eDXFGroupCode_t)(x_record_type + eYOffset), y_destination);
SetupScaledDoubleIntoList((eDXFGroupCode_t)(x_record_type + eZOffset), z_destination);
}
void CDxfRead::SetupScaledDoubleAttribute(eDXFGroupCode_t x_record_type, double& destination)
{
m_coordinate_attributes.emplace(x_record_type, std::pair(&ProcessScaledDouble, &destination));
}
void CDxfRead::SetupScaledDoubleIntoList(eDXFGroupCode_t x_record_type, list<double>& destination)
{
m_coordinate_attributes.emplace(
x_record_type,
std::pair(&ProcessScaledDoubleIntoList, &destination)
);
}
void CDxfRead::Setup3DDirectionAttribute(eDXFGroupCode_t x_record_type, Base::Vector3d& destination)
{
SetupValueAttribute((eDXFGroupCode_t)(x_record_type + eXOffset), destination.x);
SetupValueAttribute((eDXFGroupCode_t)(x_record_type + eYOffset), destination.y);
SetupValueAttribute((eDXFGroupCode_t)(x_record_type + eZOffset), destination.z);
}
void CDxfRead::SetupStringAttribute(eDXFGroupCode_t x_record_type, std::string& destination)
{
m_coordinate_attributes.emplace(x_record_type, std::pair(&ProcessStdString, &destination));
}
template<typename T>
void CDxfRead::SetupValueAttribute(eDXFGroupCode_t record_type, T& destination)
{
m_coordinate_attributes.emplace(record_type, std::pair(&ProcessValue<T>, &destination));
}
//
// Static processing helpers for ProcessCommonEntityAttribute
void CDxfRead::ProcessScaledDouble(CDxfRead* object, void* target)
{
std::istringstream ss;
ss.imbue(std::locale("C"));
ss.str(object->m_record_data);
double value = 0;
ss >> value;
if (ss.fail()) {
object->ImportError(
"Unable to parse value '%s', using zero as its value\n",
object->m_record_data
);
}
*static_cast<double*>(target) = object->mm(value);
}
void CDxfRead::ProcessScaledDoubleIntoList(CDxfRead* object, void* target)
{
std::istringstream ss;
ss.imbue(std::locale("C"));
ss.str(object->m_record_data);
double value = 0;
ss >> value;
if (ss.fail()) {
object->ImportError(
"Unable to parse value '%s', using zero as its value\n",
object->m_record_data
);
}
static_cast<std::list<double>*>(target)->push_back(object->mm(value));
}
template<typename T>
bool CDxfRead::ParseValue(CDxfRead* object, void* target)
{
std::istringstream ss;
ss.imbue(std::locale("C"));
ss.str(object->m_record_data);
ss >> *static_cast<T*>(target);
if (ss.fail()) {
object->ImportError(
"Unable to parse value '%s', using zero as its value\n",
object->m_record_data
);
*static_cast<T*>(target) = 0;
return false;
}
// TODO: Verify nothing it left but whitespace in ss.
return true;
}
void CDxfRead::ProcessStdString(CDxfRead* object, void* target)
{
*static_cast<std::string*>(target) = object->m_record_data;
}
void CDxfRead::InitializeAttributes()
{
m_coordinate_attributes.clear();
}
bool CDxfRead::ProcessAttribute()
{
auto found = m_coordinate_attributes.find(m_record_type);
if (found != m_coordinate_attributes.end()) {
(*found->second.first)(this, found->second.second);
return true;
}
return false;
}
void CDxfRead::ProcessAllAttributes()
{
while (get_next_record() && m_record_type != eObjectType) {
ProcessAttribute();
}
repeat_last_record();
}
void CDxfRead::ProcessAllEntityAttributes()
{
ProcessAllAttributes();
ResolveEntityAttributes();
}
void CDxfRead::ResolveEntityAttributes()
{
m_entityAttributes.ResolveBylayerAttributes(*this);
if (m_entityAttributes.m_paperSpace) {
m_stats.entityCounts["ENTITIES_IN_PAPERSPACE"]++;
}
// TODO: Look at the space and layer (hidden/frozen?) and options and return false if the entity
// is not needed.
// TODO: INSERT must not call this because an INSERT on a hidden layer should always be
// honoured.
// Calculate the net entity transformation.
// This is to handle the Object Coordinate System used in many DXF records. Note that versions
// before R13 used the term ECS (Entity Coordinate System) instead. Here's who uses OCS: Lines
// and Points use WCS except they can be extruded (have nonzero Thickness (39)) which occurs in
// the OCS Z direction all 3D objects use WCS entirely Dimensions use a mix of OCS and WCS,
// Circle, Arc, Dolid, Trace, Text, Attib, Attdef, Shape, Insert, (lw)Polyline/Vertex, hatch,
// image all use the OCS
//
// The transformed Z axis is in EntityNormalVector, but we rescale it in case the DXF contains
// an unnormalized value
if (EntityNormalVector.IsNull()) {
ImportError("Entity has zero-length extrusion direction\n");
}
EntityNormalVector.Normalize();
// Apply the Arbitrary Axis Algorithm to determine the X and Y directions
// The purpose of this algorithm is to calculate a conventional 3d orientation based only on a Z
// direction, while avoiding taking the cross product of two vectors that are nearly parallel,
// which would be subject to a lot of numerical inaccuracy. In this case, "close to" the Z axis
// means the X and Y components of EntityNormalVector are less than 1/64, a value chosen because
// it is exactly representable in all binary floating-point systems.
Base::Vector3d xDirection;
if (EntityNormalVector.x < ArbitraryAxisAlgorithmThreshold
&& EntityNormalVector.y < ArbitraryAxisAlgorithmThreshold) {
// The Z axis is close to the UCS Z axis, the X direction is UCSY × OCSZ
static const Base::Vector3d UCSYAxis(0, 1, 0);
xDirection = UCSYAxis % EntityNormalVector;
}
else {
// otherwise, the X direction is UCSZ × OCSZ
static const Base::Vector3d UCSZAxis(0, 0, 1);
xDirection = UCSZAxis % EntityNormalVector;
}
OCSOrientationTransform.setCol(0, xDirection);
// In all cases the Y direction is the Zdirection × XDirection which gives a right-hand
// orthonormal coordinate system
OCSOrientationTransform.setCol(1, EntityNormalVector % xDirection);
// and EntityNormalVector is of course the direction of the Z axis in the UCS.
OCSOrientationTransform.setCol(2, EntityNormalVector);
}
//
// The individual Entity reader functions
// These return false if they catch an exception and ignore it because of ignore_errors.
bool CDxfRead::ReadLine()
{
Base::Vector3d start;
Base::Vector3d end;
Setup3DVectorAttribute(ePrimaryPoint, start);
Setup3DVectorAttribute(ePoint2, end);
ProcessAllEntityAttributes();
OnReadLine(start, end, LineTypeIsHidden());
return true;
}
bool CDxfRead::ReadPoint()
{
Base::Vector3d location;
Setup3DVectorAttribute(ePrimaryPoint, location);
ProcessAllEntityAttributes();
OnReadPoint(location);
return true;
}
bool CDxfRead::ReadArc()
{
double start_angle_degrees = 0;
double end_angle_degrees = 0;
double radius = 0;
Base::Vector3d centre;
Base::Vector3d extrusionDirection(0, 0, 1);
Setup3DVectorAttribute(ePrimaryPoint, centre);
SetupScaledDoubleAttribute(eFloat1, radius);
SetupValueAttribute(eAngleDegrees1, start_angle_degrees);
SetupValueAttribute(eAngleDegrees2, end_angle_degrees);
Setup3DVectorAttribute(eExtrusionDirection, extrusionDirection);
ProcessAllEntityAttributes();
OnReadArc(
start_angle_degrees,
end_angle_degrees,
radius,
centre,
extrusionDirection.z,
LineTypeIsHidden()
);
return true;
}
bool CDxfRead::ReadSpline()
{
struct SplineData sd;
sd.degree = 0;
sd.knots = 0;
sd.flag = 0;
sd.control_points = 0;
sd.fit_points = 0;
Setup3DVectorAttribute(eExtrusionDirection, sd.norm);
SetupValueAttribute(eInteger1, sd.flag);
SetupValueAttribute(eInteger2, sd.degree);
SetupValueAttribute(eInteger3, sd.knots);
SetupValueAttribute(eInteger4, sd.control_points);
SetupValueAttribute(eInteger5, sd.fit_points);
SetupScaledDoubleIntoList(eFloat1, sd.knot);
SetupScaledDoubleIntoList(eFloat2, sd.weight);
Setup3DCoordinatesIntoLists(ePrimaryPoint, sd.controlx, sd.controly, sd.controlz);
Setup3DCoordinatesIntoLists(ePoint2, sd.fitx, sd.fity, sd.fitz);
Setup3DCoordinatesIntoLists(ePoint3, sd.starttanx, sd.starttany, sd.starttanz);
Setup3DCoordinatesIntoLists(ePoint4, sd.endtanx, sd.endtany, sd.endtanz);
ProcessAllEntityAttributes();
OnReadSpline(sd);
return true;
}
bool CDxfRead::ReadCircle()
{
double radius = 0.0;
Base::Vector3d centre;
Setup3DVectorAttribute(ePrimaryPoint, centre);
SetupScaledDoubleAttribute(eFloat1, radius);
ProcessAllEntityAttributes();
OnReadCircle(centre, radius, LineTypeIsHidden());
return true;
}
bool CDxfRead::ReadText()
{
Base::Vector3d insertionPoint;
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers)
double height = 0.03082;
double rotation = 0;
std::string textPrefix;
Setup3DVectorAttribute(ePrimaryPoint, insertionPoint);
SetupScaledDoubleAttribute(eFloat1, height);
SetupValueAttribute(eAngleDegrees1, rotation);
while (get_next_record() && m_record_type != eObjectType) {
if (!ProcessAttribute()) {
switch (m_record_type) {
case eExtraText:
// Additional text that goes before the type 1 text
// Note that if breaking the text into type-3 records splits a UFT-8 encoding we
// do the decoding after splicing the lines together. I'm not sure if this
// actually occurs, but handling the text this way will treat this condition
// properly.
case ePrimaryText:
// final text is treated the same.
// ORDER: We are asusming the type 1 record follows all the type 3's.
textPrefix.append(m_record_data);
break;
default:
break;
}
}
}
ResolveEntityAttributes();
if ((this->*stringToUTF8)(textPrefix)) {
OnReadText(insertionPoint, height, textPrefix, rotation);
}
else {
ImportError("Unable to process encoding for TEXT/MTEXT '%s'\n", textPrefix);
}
repeat_last_record();
return true;
}
bool CDxfRead::ReadEllipse()
{
Base::Vector3d centre;
Base::Vector3d majorAxisEnd; // relative to centre
double eccentricity = 0;
double startAngleRadians = 0;
double endAngleRadians = 2 * std::numbers::pi;
Setup3DVectorAttribute(ePrimaryPoint, centre);
Setup3DVectorAttribute(ePoint2, majorAxisEnd);
SetupValueAttribute(eFloat1, eccentricity);
SetupValueAttribute(eFloat2, startAngleRadians);
SetupValueAttribute(eFloat3, endAngleRadians);
ProcessAllEntityAttributes();
OnReadEllipse(centre, majorAxisEnd, eccentricity, startAngleRadians, endAngleRadians);
return true;
}
bool CDxfRead::ReadLwPolyLine()
{
VertexInfo currentVertex;
list<VertexInfo> vertices;
int flags = 0;
bool have_x = false;
bool have_y = false;
// The documentation for LZPOLYLINE does not specify how you know if you have a new vertex.
// It also does not specify where the bulge, line-width, etc for a particular segment are placed
// relative to the two end vertices.
// We assume here that if we see an X or Y coordinate but we already have the same coordinate,
// a new vertex is starting, and any previous vertex is completely specified. Furthermore, line
// attributes like bulge are placed between the X/Y coordinates for the vertex that starts the
// stroke and the X/Y coordinates for the vertex that ends the stroke or the end of the entity.
// In the latter case the stroke attributes apply to the closure stroke (if any) which ends at
// the first vertex.
Setup3DVectorAttribute(ePrimaryPoint, currentVertex.location);
SetupValueAttribute(eFloat3, currentVertex.bulge);
SetupValueAttribute(eInteger1, flags);
while (get_next_record() && m_record_type != eObjectType) {
if ((m_record_type == ePrimaryPoint + eXOffset && have_x)
|| (m_record_type == ePrimaryPoint + eYOffset && have_y)) {
// Starting a new vertex and there is a previous vertex. Save it and init a new one.
vertices.push_back(currentVertex);
currentVertex.location = Base::Vector3d();
currentVertex.bulge = 0.0;
have_x = m_record_type == ePrimaryPoint + eXOffset;
have_y = m_record_type == ePrimaryPoint + eYOffset;
}
else if (m_record_type == ePrimaryPoint + eXOffset) {
have_x = true;
}
else if (m_record_type == ePrimaryPoint + eYOffset) {
have_y = true;
}
ProcessAttribute();
}
// At the end of the entity if we have vertex information use this as the final vertex.
// (else it was a line with no vertices at all)
if (have_x || have_y) {
vertices.push_back(currentVertex);
}
ResolveEntityAttributes();
OnReadPolyline(vertices, flags);
repeat_last_record();
return true;
}
bool CDxfRead::ReadPolyLine()
{
VertexInfo currentVertex;
list<VertexInfo> vertices;
int flags = 0;
SetupValueAttribute(eInteger1, flags);
ProcessAllEntityAttributes();
// We are now followed by a series of VERTEX entities followed by ENDSEQ.
// To avoid eating and discarding the rest of the entieies if ENDSEQ is missing,
// we quit on any unknown type-0 record.
Setup3DVectorAttribute(ePrimaryPoint, currentVertex.location);
SetupValueAttribute(eFloat3, currentVertex.bulge);
while (get_next_record() && m_record_type == eObjectType && IsObjectName("VERTEX")) {
// Set vertex defaults
currentVertex.location = Base::Vector3d();
currentVertex.bulge = 0.0;
ProcessAllEntityAttributes();
vertices.push_back(currentVertex);
}
if (!IsObjectName("SEQEND")) {
ImportError("POLYLINE ends with '%s' record rather than 'SEQEND'\n", m_record_data);
repeat_last_record();
}
OnReadPolyline(vertices, flags);
return true;
}
bool CDxfRead::ReadInsert()
{
Base::Vector3d center;
Base::Vector3d scale(1, 1, 1);
double rotationDegrees = 0.0;
std::string blockName;
Setup3DVectorAttribute(ePrimaryPoint, center);
SetupValueAttribute(eFloat2, scale.x);
SetupValueAttribute(eFloat3, scale.y);
SetupValueAttribute(eFloat4, scale.z);
SetupValueAttribute(eAngleDegrees1, rotationDegrees);
SetupStringAttribute(eName, blockName);
ProcessAllEntityAttributes();
OnReadInsert(center, scale, blockName, Base::toRadians(rotationDegrees));
return (true);
}
bool CDxfRead::ReadDimension()
{
Base::Vector3d start;
Base::Vector3d end;
Base::Vector3d linePosition;
Base::Vector3d textPosition;
double rotation = 0;
int dimensionType = 0; // This appears to default to zero
// Per documentation:
// 10 is the "dimension line location"
// 11 is the midpoint of the dimension text
// 13 is the start point of the 1st extension line
// 14 is the start point of the 2nd extension line
// 50 is the rotation of the dimension (the direction of the dimension line)
// 52 (if present) is the angle relative to the dimension line for the extension lines; default
// 90 degrees
Setup3DVectorAttribute(ePoint4, start); // WCS
Setup3DVectorAttribute(ePoint5, end); // WCS
Setup3DVectorAttribute(ePrimaryPoint, linePosition); // WCS
Setup3DVectorAttribute(ePoint2, textPosition); // OCS
SetupValueAttribute(eAngleDegrees1, rotation);
SetupValueAttribute(eInteger1, dimensionType);
ProcessAllEntityAttributes();
dimensionType &= eTypeMask; // Remove flags
switch ((eDimensionType_t)dimensionType) {
case eLinear:
case eAligned:
OnReadDimension(start, end, linePosition, dimensionType, Base::toRadians(rotation));
break;
default:
UnsupportedFeature("Dimension type '%d'", dimensionType);
break;
}
return true;
}
bool CDxfRead::ReadUnknownEntity()
{
ProcessAllEntityAttributes();
UnsupportedFeature("Entity type '%s'", m_current_entity_name.c_str());
return true;
}
bool CDxfRead::ReadBlockInfo()
{
int blockType = 0;
std::string blockName;
InitializeAttributes();
// Both 2 and 3 are the block name.
SetupStringAttribute(eName, blockName);
SetupStringAttribute(eExtraText, blockName);
SetupValueAttribute(eInteger1, blockType);
ProcessAllAttributes();
return OnReadBlock(blockName, blockType);
}
bool CDxfRead::ReadBlockContents()
{
while (get_next_record() && m_record_type == eObjectType && !IsObjectName("ENDBLK")) {
if (IgnoreErrors()) {
try {
if (!ReadEntity()) {
return false;
}
}
catch (...) {
}
}
else {
if (!ReadEntity()) {
return false;
}
}
}
return true;
}
bool CDxfRead::SkipBlockContents()
{
while (get_next_record() && m_record_type == eObjectType && !IsObjectName("ENDBLK")) {
if (IgnoreErrors()) {
try {
ProcessAllAttributes();
}
catch (...) {
}
}
else {
ProcessAllAttributes();
}
}
return true;
}
template<typename... args>
void CDxfRead::UnsupportedFeature(const char* format, args&&... argValuess)
{
// NOLINTNEXTLINE(runtime/printf)
std::string formattedMessage = fmt::sprintf(format, std::forward<args>(argValuess)...);
m_stats.unsupportedFeatures[formattedMessage].emplace_back(
m_current_entity_line_number,
m_current_entity_handle
);
}
bool CDxfRead::get_next_record()
{
if (m_repeat_last_record) {
m_repeat_last_record = false;
return m_not_eof;
}
do {
if ((*m_ifs).eof()) {
m_not_eof = false;
return false;
}
std::getline(*m_ifs, m_record_data);
++m_line;
int temp = 0;
if (!ParseValue<int>(this, &temp)) {
ImportError(
"CDxfRead::get_next_record() Failed to get integer record type from '%s'\n",
m_record_data
);
return false;
}
m_record_type = (eDXFGroupCode_t)temp;
if ((*m_ifs).eof()) {
return false;
}
std::getline(*m_ifs, m_record_data);
++m_line;
} while (m_record_type == eComment);
// Remove any carriage return at the end of m_str which may occur because of inconsistent
// handling of LF vs. CRLF line termination.
auto last = m_record_data.rbegin();
if (last != m_record_data.rend() && *last == '\r') {
m_record_data.pop_back();
}
// The code that was here just blindly trimmed leading white space, but if you have, for
// instance, a TEXT entity whose text starts with spaces, or, more plausibly, a long TEXT entity
// where the text is broken into one or more type-3 records with a final type-1 and the break
// happens to be just before a space, this would be wrong.
return true;
}
void CDxfRead::repeat_last_record()
{
m_repeat_last_record = true;
}
//
// Intercepts for On... calls to derived class
// (These have distinct signatures from the ones they call)
bool CDxfRead::ExplodePolyline(std::list<VertexInfo>& vertices, int flags)
{
if (vertices.size() < 2) {
// TODO: Warning
return true;
}
bool closed = ((flags & 1) != 0);
auto startVertex = vertices.end();
if (closed) {
// If the shape is closed, point at the last vertex. The first stroke drawn will be the
// closure.
--startVertex;
}
for (auto endVertex = vertices.begin(); endVertex != vertices.end(); endVertex++) {
if (startVertex != vertices.end()) {
if (startVertex->bulge != 0.0) {
// Bulge is 1/4 tan(arc angle), positive for CCW arc.
double cot = ((1.0 / startVertex->bulge) - startVertex->bulge) / 2;
double cx = ((startVertex->location.x + endVertex->location.x)
- ((endVertex->location.y - startVertex->location.y) * cot))
/ 2;
double cy = ((startVertex->location.y + endVertex->location.y)
+ ((endVertex->location.x - startVertex->location.x) * cot))
/ 2;
Base::Vector3d pc(cx, cy, (startVertex->location.z + endVertex->location.z) / 2);
OnReadArc(startVertex->location, endVertex->location, pc, startVertex->bulge >= 0, false);
}
else {
OnReadLine(startVertex->location, endVertex->location, false);
}
}
// elsethis is the first loop iteration on an open shape, endVertex is the first point, and
// there is no closure line to draw engin there.
startVertex = endVertex;
}
return true;
}
void CDxfRead::OnReadArc(
double start_angle,
double end_angle,
double radius,
const Base::Vector3d& center,
double z_extrusion_dir,
bool hidden
)
{
Base::Vector3d temp(center);
// Calculate the start and end points of the arc
Base::Vector3d start(center);
start.x += radius * cos(Base::toRadians(start_angle));
start.y += radius * sin(Base::toRadians(start_angle));
Base::Vector3d end(center);
end.x += radius * cos(Base::toRadians(end_angle));
end.y += radius * sin(Base::toRadians(end_angle));
if (z_extrusion_dir < 0) {
// This is a dumbed-down handling of general OCS. This only works
// for arcs drawn exactly upside down (i.e. with the extrusion vector
// being (0, 0, <0).
// We treat this as 180-degree mirroring through the YZ plane
// TODO: I don't even think this is correct, but it is functionally what the
// code did before.
temp.x = -temp.x;
start.x = -start.x;
end.x = -end.x;
}
OnReadArc(start, end, temp, true, hidden);
}
void CDxfRead::OnReadCircle(const Base::Vector3d& center, double radius, bool hidden)
{
// OnReadCircle wants a start point, so we pick an arbitrary point on the circumference
Base::Vector3d start(center);
start.x += radius;
OnReadCircle(
start,
center,
false,
hidden
); // false to change direction because otherwise the arc length is zero
}
// NOLINTBEGIN(bugprone-easily-swappable-parameters)
void CDxfRead::OnReadEllipse(
const Base::Vector3d& center,
const Base::Vector3d& majorAxisEnd,
double ratio,
double start_angle,
double end_angle
)
// NOLINTEND(bugprone-easily-swappable-parameters)
{
double major_radius = majorAxisEnd.Length();
double minor_radius = major_radius * ratio;
// Since we only support 2d stuff, we can calculate the rotation from the major axis x and y
// value only, since z is zero, major_radius is the vector length
double rotation = atan2(majorAxisEnd.y, majorAxisEnd.x);
OnReadEllipse(center, major_radius, minor_radius, rotation, start_angle, end_angle, true);
}
bool CDxfRead::ReadVersion()
{
static const std::vector<std::string> VersionNames = {
// This table is indexed by eDXFVersion_t - (ROlder+1)
"AC1006",
"AC1009",
"AC1012",
"AC1014",
"AC1015",
"AC1018",
"AC1021",
"AC1024",
"AC1027",
"AC1032"
};
assert(VersionNames.size() == RNewer - ROlder - 1);
get_next_record(); // Get the value for the variable
auto first = VersionNames.cbegin();
auto last = VersionNames.cend();
auto found = std::lower_bound(first, last, m_record_data);
if (found == last) {
m_version = RNewer;
}
else if (*found == m_record_data) {
m_version = (eDXFVersion_t)((int)std::distance(first, found) + (ROlder + 1));
}
else if (found == first) {
m_version = ROlder;
}
else {
m_version = RUnknown;
}
m_stats.dxfVersion = m_record_data;
return ResolveEncoding();
}
bool CDxfRead::ReadDWGCodePage()
{
get_next_record(); // Get the value for the variable
assert(m_CodePage.empty()); // If not, we have found two DWGCODEPAGE variables or DoRead
// was called twice on the same CDxfRead object.
m_CodePage = m_record_data;
return ResolveEncoding();
}
bool CDxfRead::ResolveEncoding()
{
if (m_version >= R2007) { // Note this does not include RUnknown, but does include RLater
m_encoding = "utf_8";
stringToUTF8 = &CDxfRead::UTF8ToUTF8;
}
else if (m_CodePage.empty()) {
// cp1252
m_encoding = "cp1252";
stringToUTF8 = &CDxfRead::GeneralToUTF8;
}
else {
// Codepage names may be of the form "ansi_1252" which we map to "cp1252" but we don't map
// "ansi_x3xxxx" (which happens to mean "ascii")
// Also some DXF files have the codepage name in uppercase so we lowercase it.
m_encoding = m_CodePage;
std::transform(m_encoding.begin(), m_encoding.end(), m_encoding.begin(), ::tolower);
// Add mapping for common non-standard encoding names.
if (m_encoding == "8859_1") {
m_encoding = "iso-8859-1"; // Replace with a name Python understands
}
// NOLINTNEXTLINE(readability/nolint)
#define ANSI_ENCODING_PREFIX "ansi_" // NOLINT(cppcoreguidelines-macro-usage)
if (m_encoding.rfind(ANSI_ENCODING_PREFIX, 0) == 0 && m_encoding.rfind("ansi_x3", 0) != 0) {
m_encoding.replace(0, (sizeof ANSI_ENCODING_PREFIX) - 1, "cp");
}
// At this point we want to recognize synonyms for "utf_8" and use the custom decoder
// function. This is because this is one of the common cases and our decoder function is a
// fast no-op. We don't actually use the decoder function we get from PyCodec_Decoder
// because to call it we have to convert the (char *) text into a 'bytes' object first so we
// can pass it to the function using PyObject_Callxxx(), getting the PYObject containing the
// Python string, which we then decode back to UTF-8. It is simpler to call
// PyUnicode_DecodeXxxx which takes a (const char *) and is just a direct c++ callable.
Base::PyGILStateLocker lock;
PyObject* pyDecoder = PyCodec_Decoder(m_encoding.c_str());
if (pyDecoder == nullptr) {
// PyCodec_Decoder failed, which means Python could not find the encoding.
// This sets a Python LookupError. We clear this low-level error because
// our caller will throw a more informative, high-level exception.
PyErr_Clear();
return false;
}
PyObject* pyUTF8Decoder = PyCodec_Decoder("utf_8");
assert(pyUTF8Decoder != nullptr);
if (pyDecoder == pyUTF8Decoder) {
stringToUTF8 = &CDxfRead::UTF8ToUTF8;
}
else {
stringToUTF8 = &CDxfRead::GeneralToUTF8;
}
Py_DECREF(pyDecoder);
Py_DECREF(pyUTF8Decoder);
}
m_stats.dxfEncoding = m_encoding;
return !m_encoding.empty();
}
// NOLINTNEXTLINE(readability/nolint)
// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
bool CDxfRead::UTF8ToUTF8(std::string& /*encoded*/) const
{
return true;
}
bool CDxfRead::GeneralToUTF8(std::string& encoded) const
{
Base::PyGILStateLocker lock;
PyObject* decoded
= PyUnicode_Decode(encoded.c_str(), (Py_ssize_t)encoded.length(), m_encoding.c_str(), "strict");
if (decoded == nullptr) {
return false;
}
const char* converted = PyUnicode_AsUTF8(decoded);
// converted has the same lifetime as decoded so we don't have to delete it.
if (converted != nullptr) {
encoded = converted;
}
Py_DECREF(decoded);
return converted != nullptr;
}
void CDxfRead::DoRead(const bool ignore_errors /* = false */)
{
m_ignore_errors = ignore_errors;
if (m_fail) {
return;
}
try {
StartImport();
// Loop reading the sections.
while (get_next_record()) {
if (m_record_type != eObjectType) {
ImportError(
"Found type %d record when expecting start of a SECTION or EOF\n",
(int)m_record_type
);
continue;
}
if (IsObjectName("EOF")) { // TODO: Check for drivel beyond EOF record
break;
}
if (!IsObjectName("SECTION")) {
ImportError(
"Found %s record when expecting start of a SECTION\n",
m_record_data.c_str()
);
continue;
}
if (!ReadSection()) {
throw Base::Exception("Failed to read DXF section (returned false).");
}
}
FinishImport();
}
catch (const Base::Exception& e) {
// This catches specific FreeCAD exceptions and re-throws them.
throw;
}
catch (const std::exception& e) {
// This catches all standard C++ exceptions and converts them
// to a FreeCAD exception, which the binding layer can handle.
throw Base::Exception(e.what());
}
catch (...) {
// This is a catch-all for any other non-standard C++ exceptions.
throw Base::Exception("An unknown, non-standard C++ exception occurred during DXF import.");
}
}
bool CDxfRead::ReadSection()
{
if (!get_next_record()) {
throw Base::Exception("Unexpected end of file after SECTION tag.");
}
if (m_record_type != eName) {
ImportError("Ignored SECTION with no name record\n");
return ReadIgnoredSection();
}
if (IsObjectName("HEADER")) {
if (!ReadHeaderSection()) {
throw Base::Exception("Failed while reading HEADER section.");
}
return true;
}
if (IsObjectName("TABLES")) {
if (!ReadTablesSection()) {
throw Base::Exception("Failed while reading TABLES section.");
}
return true;
}
if (IsObjectName("BLOCKS")) {
if (!ReadBlocksSection()) {
throw Base::Exception("Failed while reading BLOCKS section.");
}
return true;
}
if (IsObjectName("ENTITIES")) {
if (!ReadEntitiesSection()) {
throw Base::Exception("Failed while reading ENTITIES section.");
}
return true;
}
if (!ReadIgnoredSection()) {
throw Base::Exception("Failed while reading an unknown/ignored section.");
}
return true;
}
void CDxfRead::ProcessLayerReference(CDxfRead* object, void* target)
{
if (!object->Layers.contains(object->m_record_data)) {
object->ImportError("First reference to missing Layer '%s'", object->m_record_data);
// Synthesize the Layer so we don't get the same error again.
// We need to take copies of the string arguments because MakeLayer uses them as move
// inputs.
object->Layers[object->m_record_data]
= object->MakeLayer(object->m_record_data, DefaultColor, std::string(DefaultLineType));
}
*static_cast<Layer**>(target) = object->Layers.at(object->m_record_data);
}
bool CDxfRead::ReadEntity()
{
m_current_entity_line_number = m_line;
m_current_entity_name = m_record_data;
InitializeAttributes();
m_entityAttributes.SetDefaults();
m_current_entity_handle.clear();
SetupStringAttribute(eHandle, m_current_entity_handle);
EntityNormalVector.Set(0, 0, 1);
Setup3DVectorAttribute(eExtrusionDirection, EntityNormalVector);
SetupStringAttribute(eLinetypeName, m_entityAttributes.m_LineType);
m_coordinate_attributes.emplace(
eLayerName,
std::pair(&ProcessLayerReference, &m_entityAttributes.m_Layer)
);
SetupValueAttribute(
eCoordinateSpace,
m_entityAttributes.m_paperSpace
); // TODO: Ensure the stream is noboolalpha (for that
// matter ensure the stream has the "C" locale
SetupValueAttribute(eColor, m_entityAttributes.m_Color);
m_stats.entityCounts[m_record_data]++;
// The entity record is already the current record and is already checked as a type 0 record
if (IsObjectName("LINE")) {
return ReadLine();
}
if (IsObjectName("ARC")) {
return ReadArc();
}
if (IsObjectName("CIRCLE")) {
return ReadCircle();
}
if (IsObjectName("MTEXT")) {
return ReadText();
}
if (IsObjectName("TEXT")) {
return ReadText();
}
if (IsObjectName("ELLIPSE")) {
return ReadEllipse();
}
if (IsObjectName("SPLINE")) {
return ReadSpline();
}
if (IsObjectName("LWPOLYLINE")) {
return ReadLwPolyLine();
}
if (IsObjectName("POLYLINE")) {
return ReadPolyLine();
}
if (IsObjectName("POINT")) {
return ReadPoint();
}
if (IsObjectName("INSERT")) {
return ReadInsert();
}
if (IsObjectName("DIMENSION")) {
return ReadDimension();
}
return ReadUnknownEntity();
}
bool CDxfRead::ReadHeaderSection()
{
// Read to the next ENDSEC record marking the end of the section.
// This section contains variables, most of which we ignore. Each one is a type-9 record giving
// the variable name, followed by a single record giving the value; the record type depends on
// the variable's data type.
while (get_next_record()) {
if (m_record_type == eObjectType && IsObjectName("ENDSEC")) {
if (m_unitScalingFactor == 0.0) {
// Neither INSUNITS nor MEASUREMENT found, assume 1 DXF unit = 1mm
// TODO: Perhaps this default should depend on the current project's unit system
m_unitScalingFactor = m_additionalScaling;
m_stats.fileUnits = "Unspecified (Defaulting to 1:1)";
}
m_stats.finalScalingFactor = m_unitScalingFactor;
return true;
}
if (m_record_type != eVariableName) {
continue; // Quietly ignore unknown record types
}
// Store the variable name before we try to read its value.
std::string currentVarName = m_record_data;
if (!ReadVariable()) {
// If ReadVariable returns false, throw an exception with the variable name.
throw Base::Exception("Failed while reading value for HEADER variable: " + currentVarName);
}
}
// If the loop finishes without finding ENDSEC, it's an error.
throw Base::Exception("Unexpected end of file inside HEADER section.");
}
bool CDxfRead::ReadVariable()
{
if (IsVariableName("$INSUNITS")) {
get_next_record(); // Get the value for the variable
int varValue = 0;
if (!ParseValue<int>(this, &varValue)) {
ImportError("Failed to get integer from INSUNITS value '%s'\n", m_record_data);
}
else {
auto units = DxfUnits::eDxfUnits_t(varValue);
if (!DxfUnits::IsValid(units)) {
units = DxfUnits::eUnspecified;
}
m_unitScalingFactor = DxfUnits::Factor(units) * m_additionalScaling;
m_stats.scalingSource = "$INSUNITS";
m_stats.fileUnits = DxfUnitToString(units);
}
return true;
}
if (IsVariableName("$MEASUREMENT")) {
get_next_record();
int varValue = 1;
if (m_unitScalingFactor == 0.0 && ParseValue<int>(this, &varValue)) {
auto units = (varValue != 0 ? DxfUnits::eMillimeters : DxfUnits::eInches);
m_unitScalingFactor = DxfUnits::Factor(units) * m_additionalScaling;
m_stats.scalingSource = "$MEASUREMENT";
m_stats.fileUnits = DxfUnitToString(units);
}
return true;
}
if (IsVariableName("$ACADVER")) {
return ReadVersion();
}
if (IsVariableName("$DWGCODEPAGE")) {
return ReadDWGCodePage();
}
// any other variable, skip its value
return get_next_record();
}
bool CDxfRead::ReadTablesSection()
{
// Read to the next ENDSEC record marking the end of the section.
// This section contains various tables, many of which we ignore. Each one is a type-0 TABLE
// record followed by a type-2 (name) record giving the table name, followed by the table
// contents. Each set of contents is terminates by the next type-0 TABLE or ENDSEC directive.
while (get_next_record()) {
if (m_record_type != eObjectType) {
continue; // Ignore any non-type-0 contents in the section.
}
if (IsObjectName("ENDSEC")) {
return true;
}
if (!IsObjectName("TABLE")) {
continue; // Ignore any type-0 non-TABLE contents in the section
}
get_next_record();
if (m_record_type != eName) {
ImportError("Found unexpected type %d record instead of table name\n", (int)m_record_type);
}
else if (IsObjectName("LAYER")) {
if (!ReadLayerTable()) {
return false;
}
}
else {
if (!ReadIgnoredTable()) {
return false;
}
}
}
return false;
}
bool CDxfRead::ReadIgnoredSection()
{
// Read to the next ENDSEC record marking the end of the section.
while (get_next_record()) {
if (m_record_type == eObjectType && IsObjectName("ENDSEC")) {
return true;
}
}
return false;
}
bool CDxfRead::ReadBlocksSection()
{
// Read to the next ENDSEC record marking the end of the section.
// Within this section we should find type-0 BLOCK groups
while (get_next_record()) {
if (m_record_type != eObjectType) {
continue; // quietly ignore non-type-0 records;
}
if (IsObjectName("ENDSEC")) {
// End of section
return true;
}
if (!IsObjectName("BLOCK")) {
continue; // quietly ignore non-BLOCK records
}
if (!ReadBlockInfo()) {
ImportError("CDxfRead::DoRead() Failed to read block\n");
}
}
return false;
}
bool CDxfRead::ReadEntitiesSection()
{
// Read to the next ENDSEC record marking the end of the section.
// Within this section we should find type-0 BLOCK groups
while (get_next_record()) {
if (m_record_type != eObjectType) {
continue; // quietly ignore non-type-0 records;
}
if (IsObjectName("ENDSEC")) {
// End of section
return true;
}
if (IgnoreErrors()) {
try {
if (!ReadEntity()) {
return false;
}
}
catch (const Base::Exception& e) {
e.reportException();
}
catch (...) {
ImportError("CDxfRead::ReadEntity raised unknown exception\n");
}
}
else {
if (!ReadEntity()) {
return false;
}
}
}
return false;
}
bool CDxfRead::ReadLayer()
{
std::string layername;
ColorIndex_t layerColor = DefaultColor;
int layerFlags = 0;
std::string lineTypeName(DefaultLineType);
InitializeAttributes();
SetupStringAttribute(eName, layername);
SetupValueAttribute(eColor, layerColor);
SetupValueAttribute(eInteger1, layerFlags);
SetupStringAttribute(eLinetypeName, lineTypeName);
ProcessAllAttributes();
if (layername.empty()) {
ImportError("CDxfRead::ReadLayer() - no layer name\n");
return false;
}
if ((layerFlags & 0x01) != 0) {
// Frozen layers are implicitly hidden which we don't do yet.
// TODO: Should have an import option to omit frozen layers.
UnsupportedFeature("Frozen layers");
}
Layers[layername] = MakeLayer(layername, layerColor, std::move(lineTypeName));
return true;
}
CDxfRead::Layer* CDxfRead::MakeLayer(const std::string& name, ColorIndex_t color, std::string&& lineType)
{
return new Layer(name, color, std::move(lineType));
}
bool CDxfRead::ReadLayerTable()
{
// Read to the next TABLE record indicating another table in the TABLES section, or to the
// ENDSEC record marking the end of the TABLES section. This table contains a series of type-0
// LAYER groups
while (get_next_record()) {
if (m_record_type != eObjectType) {
continue; // quietly ignore non-type-0 records; this table has some preamble
}
if (IsObjectName("TABLE") || IsObjectName("ENDSEC")) {
// End of table
repeat_last_record();
return true;
}
if (!IsObjectName("LAYER")) {
continue; // quietly ignore non-LAYER records
}
if (!ReadLayer()) {
ImportError("CDxfRead::DoRead() Failed to read layer\n");
}
}
return false;
}
bool CDxfRead::ReadIgnoredTable()
{
// Read to the next TABLE record indicating another table in the TABLES section, or to the
// ENDSEC record marking the end of the TABLES section.
while (get_next_record()) {
if (m_record_type == eObjectType && (IsObjectName("TABLE") || IsObjectName("ENDSEC"))) {
repeat_last_record();
return true;
}
}
return false;
}
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers)
inline static double level(int distance, double blackLevel)
{
// Distance is the number of 24ths around the color wheel between the desired hue and
// the primary hue in question. Coming in it is a ordinate difference and so can be negative
// so the first thing we do is take its absolute value.
if (distance < 0) {
distance = -distance;
}
// If the distance is greater than 12, it is measuring the long way around the color wheel so we
// reduce it to measuring along the short way instead
if (distance > 12) {
distance = 24 - distance;
}
if (distance <= 4) {
// A distance 4 or less givs full intensity of the primary color
return 1.0;
}
if (distance < 8) {
// Between 4 and 8 gives a blend of the full primary and the black level
return ((8 - distance) + blackLevel * (distance - 4)) / 4;
}
// 8 and beyond yield the black level
return blackLevel;
}
inline static Base::Color wheel(int hue, double blackLevel, double multiplier = 1.0)
{
return Base::Color(
(float)(level(hue - 0, blackLevel) * multiplier),
(float)(level(hue - 8, blackLevel) * multiplier),
(float)(level(hue - 16, blackLevel) * multiplier)
);
}
Base::Color CDxfRead::ObjectColor(ColorIndex_t index)
{
// TODO: If it is ColorByBlock we need to use the color of the INSERT entity.
// This is tricky because a block can itself contain INSERT entities and we don't currently
// record the required information. IIRC INSERT in a block will do something strange like
// try to insert the block into the main drawing instead of into the block being defined.
// The first 7 colors (1-7) have ad hoc names red, yellow, green, cyan, blue, magenta, and
// black. 8, 9, 250-254 are lightening shades of gray. These are rendered by the app in a manner
// to contrast with the background color.
// For others, (color/10) determines the hue around the
// color circle, with even numbers fading to black on the tens digit,
// and odd numberd being blended with AA and again fading to black.
// The fade is FF BD 81 68 4F (100%, 74%, 50%, 40%, 30%) indexed by (index/2)%5
// The AA fades as AA 7E 56 45 35 which is almost the exact same percentages.
// For hue, (index-10)/10 : 0 is ff0000, and each step linearly adds green until 4 is pure
// yellow ffff00, then red starts to fade... until but not including 24 which is back to ff0000.
Base::Color result = Base::Color();
if (index == 0) {
// Technically, 0 is BYBLOCK and not a real color, but all that means is that an object in a
// block cannot specifically ask to be black. These colors are all contrasted to the
// background so there is no objective black colour, through 255 is an objective white.
result = Base::Color();
}
else if (index < 7) {
result = wheel((index - 1) * 4, 0x00);
}
else if (index == 7) {
// DXF color 7 is "black/white" and should adapt to the background.
// Since we cannot easily query the background theme from here, we will use a
// neutral mid-gray, which is visible on both light and dark themes.
result = Base::Color(0.5f, 0.5f, 0.5f);
}
else if (index == 8) {
result = Base::Color(0.5, 0.5, 0.5);
}
else if (index == 9) {
result = Base::Color(0.75, 0.75, 0.75);
}
else if (index >= 250) {
auto brightness = (float)((index - 250 + (255 - index) * 0.2) / 5);
result = Base::Color(brightness, brightness, brightness);
}
else {
static const std::array<float, 5> fades = {1.00F, 0.74F, 0.50F, 0.40F, 0.30F};
return wheel(index / 10 - 1, (index & 1) != 0 ? 0.69 : 0, fades[(index / 2) % 5]);
}
// TODO: These colors are modified to contrast with the background. In the original program
// this is just a rendering feature, but FreeCAD does not support this so the user has the
// option of modifying the colors to contrast with the background at time of import.
return result;
}
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers)
template void CDxfRead::UnsupportedFeature<>(const char*);
|