File size: 92,956 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 | /***************************************************************************
* Copyright (c) 2008 Jürgen Riegel <juergen.riegel@web.de> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <BRep_Tool.hxx>
#include <BRepAdaptor_Surface.hxx>
#include <GeomLib_IsPlanarSurface.hxx>
#include <QMessageBox>
#include <TopExp_Explorer.hxx>
#include <TopLoc_Location.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <App/Origin.h>
#include <App/Part.h>
#include <Base/Tools.h>
#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/CommandT.h>
#include <Gui/Control.h>
#include <Gui/Document.h>
#include <Gui/MainWindow.h>
#include <Gui/Selection/Selection.h>
#include <Gui/Selection/SelectionObject.h>
#include <Mod/Sketcher/App/SketchObject.h>
#include <Mod/PartDesign/App/Body.h>
#include <Mod/PartDesign/App/FeatureBoolean.h>
#include <Mod/PartDesign/App/FeatureGroove.h>
#include <Mod/PartDesign/App/FeatureMultiTransform.h>
#include <Mod/PartDesign/App/FeatureRevolution.h>
#include <Mod/PartDesign/App/FeatureTransformed.h>
#include <Mod/PartDesign/App/DatumLine.h>
#include <Mod/PartDesign/App/DatumPlane.h>
#include <Mod/PartDesign/App/DatumPoint.h>
#include <Mod/PartDesign/App/FeatureDressUp.h>
#include <Mod/PartDesign/App/ShapeBinder.h>
#include "DlgActiveBody.h"
#include "ReferenceSelection.h"
#include "SketchWorkflow.h"
#include "TaskFeaturePick.h"
#include "Utils.h"
#include "WorkflowManager.h"
#include "ViewProvider.h"
#include "ViewProviderBody.h"
// TODO Remove this header after fixing code so it won;t be needed here (2015-10-20, Fat-Zer)
#include "ui_DlgReference.h"
FC_LOG_LEVEL_INIT("PartDesign", true, true)
using namespace std;
using namespace Attacher;
//===========================================================================
// PartDesign_Datum
//===========================================================================
/**
* @brief UnifiedDatumCommand is a common routine called by datum plane, line and point commands
* @param cmd (i/o) command, to have shortcuts to doCommand, etc.
* @param type (input)
* @param name (input). Is used to generate new name for an object, and to fill undo messages.
*
*/
void UnifiedDatumCommand(Gui::Command& cmd, Base::Type type, std::string name)
{
try {
std::string fullTypeName(type.getName());
App::PropertyLinkSubList support;
cmd.getSelection().getAsPropertyLinkSubList(support);
bool bEditSelected = false;
if (support.getSize() == 1 && support.getValue()) {
if (support.getValue()->isDerivedFrom(type)) {
bEditSelected = true;
}
}
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */ true);
if (bEditSelected) {
std::string tmp = std::string("Edit ") + name;
cmd.openCommand(tmp.c_str());
PartDesignGui::setEdit(support.getValue(), pcActiveBody);
}
else if (pcActiveBody) {
// TODO Check how this will work outside of a body (2015-10-20, Fat-Zer)
std::string FeatName = cmd.getUniqueObjectName(name.c_str(), pcActiveBody);
std::string tmp = std::string("Create ") + name;
cmd.openCommand(tmp.c_str());
FCMD_OBJ_CMD(pcActiveBody, "newObject('" << fullTypeName << "','" << FeatName << "')");
// remove the body from links in case it's selected as
// otherwise a cyclic dependency will be created
support.removeValue(pcActiveBody);
auto Feat = pcActiveBody->getDocument()->getObject(FeatName.c_str());
if (!Feat) {
return;
}
// test if current selection fits a mode.
if (support.getSize() > 0) {
Part::AttachExtension* pcDatum = Feat->getExtensionByType<Part::AttachExtension>();
pcDatum->attacher().setReferences(support);
SuggestResult sugr;
pcDatum->attacher().suggestMapModes(sugr);
if (sugr.message == Attacher::SuggestResult::srOK) {
// fits some mode. Populate AttachmentSupport property.
FCMD_OBJ_CMD(Feat, "AttachmentSupport = " << support.getPyReprString());
FCMD_OBJ_CMD(
Feat,
"MapMode = '" << AttachEngine::getModeName(sugr.bestFitMode) << "'"
);
}
else {
QMessageBox::information(
Gui::getMainWindow(),
QObject::tr("Invalid selection"),
QObject::tr("There are no attachment modes that fit selected objects. Select something else.")
);
}
}
cmd.doCommand(
Gui::Command::Doc,
"App.activeDocument().recompute()"
); // recompute the feature based on its references
PartDesignGui::setEdit(Feat, pcActiveBody);
}
else {
QMessageBox::warning(
Gui::getMainWindow(),
QObject::tr("Error"),
QObject::tr("There is no active body. Please activate a body before inserting a datum entity.")
);
}
}
catch (Base::Exception& e) {
QMessageBox::warning(
Gui::getMainWindow(),
QObject::tr("Error"),
QApplication::translate("Exception", e.what())
);
}
catch (Standard_Failure& e) {
QMessageBox::warning(
Gui::getMainWindow(),
QObject::tr("Error"),
QString::fromLatin1(e.GetMessageString())
);
}
}
/* Datum feature commands =======================================================*/
DEF_STD_CMD_A(CmdPartDesignPlane)
CmdPartDesignPlane::CmdPartDesignPlane()
: Command("PartDesign_Plane")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Datum Plane");
sToolTipText = QT_TR_NOOP("Creates a new datum plane");
sWhatsThis = "PartDesign_Plane";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Plane";
}
void CmdPartDesignPlane::activated(int iMsg)
{
Q_UNUSED(iMsg);
UnifiedDatumCommand(*this, Base::Type::fromName("PartDesign::Plane"), "DatumPlane");
}
bool CmdPartDesignPlane::isActive()
{
if (getActiveGuiDocument()) {
return true;
}
else {
return false;
}
}
DEF_STD_CMD_A(CmdPartDesignLine)
CmdPartDesignLine::CmdPartDesignLine()
: Command("PartDesign_Line")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Datum Line");
sToolTipText = QT_TR_NOOP("Creates a new datum line");
sWhatsThis = "PartDesign_Line";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Line";
}
void CmdPartDesignLine::activated(int iMsg)
{
Q_UNUSED(iMsg);
UnifiedDatumCommand(*this, Base::Type::fromName("PartDesign::Line"), "DatumLine");
}
bool CmdPartDesignLine::isActive()
{
if (getActiveGuiDocument()) {
return true;
}
else {
return false;
}
}
DEF_STD_CMD_A(CmdPartDesignPoint)
CmdPartDesignPoint::CmdPartDesignPoint()
: Command("PartDesign_Point")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Datum Point");
sToolTipText = QT_TR_NOOP("Creates a new datum point");
sWhatsThis = "PartDesign_Point";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Point";
}
void CmdPartDesignPoint::activated(int iMsg)
{
Q_UNUSED(iMsg);
UnifiedDatumCommand(*this, Base::Type::fromName("PartDesign::Point"), "DatumPoint");
}
bool CmdPartDesignPoint::isActive()
{
if (getActiveGuiDocument()) {
return true;
}
else {
return false;
}
}
DEF_STD_CMD_A(CmdPartDesignCS)
CmdPartDesignCS::CmdPartDesignCS()
: Command("PartDesign_CoordinateSystem")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Local Coordinate System");
sToolTipText = QT_TR_NOOP("Creates a new local coordinate system");
sWhatsThis = "PartDesign_CoordinateSystem";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_CoordinateSystem";
}
void CmdPartDesignCS::activated(int iMsg)
{
Q_UNUSED(iMsg);
UnifiedDatumCommand(*this, Base::Type::fromName("PartDesign::CoordinateSystem"), "Local_CS");
}
bool CmdPartDesignCS::isActive()
{
if (getActiveGuiDocument()) {
return true;
}
else {
return false;
}
}
//===========================================================================
// PartDesign_ShapeBinder
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignShapeBinder)
CmdPartDesignShapeBinder::CmdPartDesignShapeBinder()
: Command("PartDesign_ShapeBinder")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Shape Binder");
sToolTipText = QT_TR_NOOP("Creates a new shape binder");
sWhatsThis = "PartDesign_ShapeBinder";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_ShapeBinder";
}
void CmdPartDesignShapeBinder::activated(int iMsg)
{
Q_UNUSED(iMsg);
App::PropertyLinkSubList support;
getSelection().getAsPropertyLinkSubList(support);
bool bEditSelected = false;
if (support.getSize() == 1 && support.getValue()) {
if (support.getValue()->isDerivedFrom<PartDesign::ShapeBinder>()) {
bEditSelected = true;
}
}
if (bEditSelected) {
openCommand(QT_TRANSLATE_NOOP("Command", "Edit Shape Binder"));
PartDesignGui::setEdit(support.getValue());
}
else {
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */ true);
if (!pcActiveBody) {
return;
}
std::string FeatName = getUniqueObjectName("ShapeBinder", pcActiveBody);
openCommand(QT_TRANSLATE_NOOP("Command", "Create Shape Binder"));
FCMD_OBJ_CMD(pcActiveBody, "newObject('PartDesign::ShapeBinder','" << FeatName << "')");
// remove the body from links in case it's selected as
// otherwise a cyclic dependency will be created
support.removeValue(pcActiveBody);
auto Feat = pcActiveBody->getObject(FeatName.c_str());
if (!Feat) {
return;
}
// test if current selection fits a mode.
if (support.getSize() > 0) {
FCMD_OBJ_CMD(Feat, "Support = " << support.getPyReprString());
}
updateActive();
PartDesignGui::setEdit(Feat, pcActiveBody);
}
// TODO do a proper error processing (2015-09-11, Fat-Zer)
}
bool CmdPartDesignShapeBinder::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_SubShapeBinder
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignSubShapeBinder)
CmdPartDesignSubShapeBinder::CmdPartDesignSubShapeBinder()
: Command("PartDesign_SubShapeBinder")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Sub-Shape Binder");
sToolTipText = QT_TR_NOOP(
"Creates a reference to geometry from one or more objects, allowing it to be used inside "
"or outside a body. It tracks relative placements, supports multiple geometry types "
"(solids, faces, edges, vertices), and can work with objects in the same or external "
"documents."
);
sWhatsThis = "PartDesign_SubShapeBinder";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_SubShapeBinder";
}
void CmdPartDesignSubShapeBinder::activated(int iMsg)
{
Q_UNUSED(iMsg);
App::DocumentObject* parent = nullptr;
std::string parentSub;
std::map<App::DocumentObject*, std::vector<std::string>> values;
for (auto& sel : Gui::Selection().getCompleteSelection(Gui::ResolveMode::NoResolve)) {
if (!sel.pObject) {
continue;
}
auto& subs = values[sel.pObject];
if (sel.SubName && sel.SubName[0]) {
subs.emplace_back(sel.SubName);
}
}
std::string FeatName;
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(false, true, true, &parent, &parentSub);
FeatName = getUniqueObjectName("Binder", pcActiveBody);
if (parent) {
decltype(values) links;
for (auto& v : values) {
App::DocumentObject* obj = v.first;
if (obj != parent) {
auto& subs = links[obj];
subs.insert(subs.end(), v.second.begin(), v.second.end());
continue;
}
for (auto& sub : v.second) {
auto link = obj;
auto linkSub = parentSub;
parent->resolveRelativeLink(linkSub, link, sub);
if (link && link != pcActiveBody) {
links[link].push_back(sub);
}
}
}
values = std::move(links);
}
PartDesign::SubShapeBinder* binder = nullptr;
try {
openCommand(QT_TRANSLATE_NOOP("Command", "Create Sub-Shape Binder"));
if (pcActiveBody) {
FCMD_OBJ_CMD(pcActiveBody, "newObject('PartDesign::SubShapeBinder','" << FeatName << "')");
binder = dynamic_cast<PartDesign::SubShapeBinder*>(
pcActiveBody->getObject(FeatName.c_str())
);
}
else {
doCommand(
Command::Doc,
"App.ActiveDocument.addObject('PartDesign::SubShapeBinder','%s')",
FeatName.c_str()
);
binder = dynamic_cast<PartDesign::SubShapeBinder*>(
App::GetApplication().getActiveDocument()->getObject(FeatName.c_str())
);
}
if (!binder) {
return;
}
binder->setLinks(std::move(values));
updateActive();
commitCommand();
}
catch (Base::Exception& e) {
e.reportException();
QMessageBox::critical(
Gui::getMainWindow(),
QObject::tr("Sub-shape binder"),
QApplication::translate("Exception", e.what())
);
abortCommand();
}
}
bool CmdPartDesignSubShapeBinder::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Clone
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignClone)
CmdPartDesignClone::CmdPartDesignClone()
: Command("PartDesign_Clone")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Clone");
sToolTipText = QT_TR_NOOP("Copies a solid object parametrically as the base feature of a new body");
sWhatsThis = "PartDesign_Clone";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Clone";
}
void CmdPartDesignClone::activated(int iMsg)
{
Q_UNUSED(iMsg);
std::vector<App::DocumentObject*> objs = getSelection().getObjectsOfType(
Part::Feature::getClassTypeId()
);
if (objs.size() == 1) {
// As suggested in https://forum.freecad.org/viewtopic.php?f=3&t=25265&p=198547#p207336
// put the clone into its own new body.
// This also fixes bug #3447 because the clone is a PD feature and thus
// requires a body where it is part of.
openCommand(QT_TRANSLATE_NOOP("Command", "Create Clone"));
auto obj = objs[0];
auto objCmd = getObjectCmd(obj);
std::string cloneName = getUniqueObjectName("Clone", obj);
std::string bodyName = getUniqueObjectName("Body", obj);
// Create body and clone
Gui::cmdAppDocument(
obj,
std::stringstream() << "addObject('PartDesign::Body','" << bodyName << "')"
);
Gui::cmdAppDocument(
obj,
std::stringstream() << "addObject('PartDesign::FeatureBase','" << cloneName << "')"
);
auto bodyObj = obj->getDocument()->getObject(bodyName.c_str());
auto cloneObj = obj->getDocument()->getObject(cloneName.c_str());
Base::Reference<ParameterGrp> hGrp = App::GetApplication().GetUserParameter().GetGroup(
"BaseApp/Preferences/Mod/PartDesign"
);
bool allowCompound = hGrp->GetBool("AllowCompoundDefault", true);
// In the first step set the group link and tip of the body
Gui::cmdAppObject(bodyObj, std::stringstream() << "Group = [" << getObjectCmd(cloneObj) << "]");
Gui::cmdAppObject(bodyObj, std::stringstream() << "Tip = " << getObjectCmd(cloneObj));
Gui::cmdAppObject(
bodyObj,
std::stringstream() << "AllowCompound = " << (allowCompound ? "True" : "False")
);
// In the second step set the link of the base feature
Gui::cmdAppObject(cloneObj, std::stringstream() << "BaseFeature = " << objCmd);
Gui::cmdAppObject(cloneObj, std::stringstream() << "Placement = " << objCmd << ".Placement");
Gui::cmdAppObject(cloneObj, std::stringstream() << "setEditorMode('Placement', 0)");
updateActive();
copyVisual(cloneObj, "ShapeAppearance", obj);
copyVisual(cloneObj, "LineColor", obj);
copyVisual(cloneObj, "PointColor", obj);
copyVisual(cloneObj, "Transparency", obj);
copyVisual(cloneObj, "DisplayMode", obj);
commitCommand();
}
}
bool CmdPartDesignClone::isActive()
{
return getSelection().countObjectsOfType<Part::Feature>() == 1;
}
//===========================================================================
// PartDesign_Sketch
//===========================================================================
/* Sketch commands =======================================================*/
DEF_STD_CMD_A(CmdPartDesignNewSketch)
CmdPartDesignNewSketch::CmdPartDesignNewSketch()
: Command("PartDesign_NewSketch")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("New Sketch");
sToolTipText = QT_TR_NOOP("Creates a new sketch");
sWhatsThis = "PartDesign_NewSketch";
sStatusTip = sToolTipText;
sPixmap = "Sketcher_NewSketch";
}
void CmdPartDesignNewSketch::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesignGui::SketchWorkflow creator(getActiveGuiDocument());
creator.createSketch();
}
bool CmdPartDesignNewSketch::isActive()
{
if (getActiveGuiDocument()) {
return true;
}
else {
return false;
}
}
//===========================================================================
// Common utility functions for all features creating solids
//===========================================================================
static void finishFeature(
const Gui::Command* cmd,
App::DocumentObject* feature,
App::DocumentObject* prevSolidFeature = nullptr,
const bool hidePrevSolid = true,
const bool updateDocument = true
)
{
PartDesign::Body* activeBody;
if (prevSolidFeature) {
// insert into the same body as the given previous one
activeBody = PartDesignGui::getBodyFor(prevSolidFeature, /*messageIfNot = */ false);
}
else {
activeBody = PartDesignGui::getBody(/*messageIfNot = */ false);
}
if (hidePrevSolid && prevSolidFeature) {
FCMD_OBJ_HIDE(prevSolidFeature);
}
if (updateDocument) {
cmd->updateActive();
}
auto base = dynamic_cast<PartDesign::Feature*>(feature);
if (base) {
base = dynamic_cast<PartDesign::Feature*>(base->getBaseObject(true));
}
App::DocumentObject* obj = base;
if (!obj) {
obj = activeBody;
}
// Do this before calling setEdit to avoid to override the 'Shape preview' mode (#0003621)
if (obj) {
cmd->copyVisual(feature, "ShapeAppearance", obj);
cmd->copyVisual(feature, "LineColor", obj);
cmd->copyVisual(feature, "PointColor", obj);
cmd->copyVisual(feature, "Transparency", obj);
cmd->copyVisual(feature, "DisplayMode", obj);
}
PartDesignGui::setEdit(feature, activeBody);
cmd->doCommand(cmd->Gui, "Gui.Selection.clearSelection()");
}
//===========================================================================
// Common utility functions for ProfileBased features
//===========================================================================
// Take a list of Part2DObjects and classify them for creating a
// ProfileBased feature. FirstFreeSketch is the first free sketch in the same body
// or sketches.end() if non available. The returned number is the amount of free sketches
unsigned validateSketches(
std::vector<App::DocumentObject*>& sketches,
std::vector<PartDesignGui::TaskFeaturePick::featureStatus>& status,
std::vector<App::DocumentObject*>::iterator& firstFreeSketch
)
{
// TODO Review the function for non-part bodies (2015-09-04, Fat-Zer)
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(false);
App::Part* pcActivePart = PartDesignGui::getPartFor(pcActiveBody, false);
// TODO: If the user previously opted to allow multiple use of sketches or use of sketches from
// other bodies, then count these as valid sketches!
unsigned freeSketches = 0;
firstFreeSketch = sketches.end();
for (std::vector<App::DocumentObject*>::iterator s = sketches.begin(); s != sketches.end(); s++) {
if (!pcActiveBody) {
// We work in the old style outside any body
if (PartDesign::Body::findBodyOf(*s)) {
status.push_back(PartDesignGui::TaskFeaturePick::otherPart);
continue;
}
}
else if (!pcActiveBody->hasObject(*s)) {
// Check whether this plane belongs to a body of the same part
PartDesign::Body* b = PartDesign::Body::findBodyOf(*s);
if (!b) {
status.push_back(PartDesignGui::TaskFeaturePick::notInBody);
}
else if (pcActivePart && pcActivePart->hasObject(b, true)) {
status.push_back(PartDesignGui::TaskFeaturePick::otherBody);
}
else {
status.push_back(PartDesignGui::TaskFeaturePick::otherPart);
}
continue;
}
// Base::Console().error("Checking sketch %s\n", (*s)->getNameInDocument());
// Check whether this sketch is already being used by another feature
// Body features don't count...
std::vector<App::DocumentObject*> inList = (*s)->getInList();
std::vector<App::DocumentObject*>::iterator o = inList.begin();
while (o != inList.end()) {
// Base::Console().error("Inlist: %s\n", (*o)->getNameInDocument());
if ((*o)->isDerivedFrom<PartDesign::Body>()) {
o = inList.erase(o); // ignore bodies
}
else if (!((*o)->isDerivedFrom<PartDesign::Feature>())) {
o = inList.erase(o); // ignore non-partDesign
}
else {
++o;
}
}
if (!inList.empty()) {
status.push_back(PartDesignGui::TaskFeaturePick::isUsed);
continue;
}
if (pcActiveBody && pcActiveBody->isAfterInsertPoint(*s)) {
status.push_back(PartDesignGui::TaskFeaturePick::afterTip);
continue;
}
// Check whether the sketch shape is valid
Part::Part2DObject* sketch = static_cast<Part::Part2DObject*>(*s);
const TopoDS_Shape& shape = sketch->Shape.getValue();
if (shape.IsNull()) {
status.push_back(PartDesignGui::TaskFeaturePick::invalidShape);
continue;
}
// count free wires
int ctWires = 0;
TopExp_Explorer ex;
for (ex.Init(shape, TopAbs_WIRE); ex.More(); ex.Next()) {
ctWires++;
}
if (ctWires == 0) {
status.push_back(PartDesignGui::TaskFeaturePick::noWire);
continue;
}
// All checks passed - found a valid sketch
if (firstFreeSketch == sketches.end()) {
firstFreeSketch = s;
}
freeSketches++;
status.push_back(PartDesignGui::TaskFeaturePick::validFeature);
}
return freeSketches;
}
/**
* Partially pulled from Linkstage3 importExternalObjects for toponaming element map
* compatibility with sketches that contain point objects. By adding an empty
* subobject when appropriate, we allow those sketches to be used as profiles without error.
*
* @param prop The property ( generally a Profile link )
* @param _sobjs Subobjects to use
* @param report True if we should raise a dialog, otherwise raise and exception
* @return True if elements were found
*/
bool importExternalElements(App::PropertyLinkSub& prop, std::vector<App::SubObjectT> _sobjs)
{
if (!prop.getName() || !prop.getName()[0]) {
FC_THROWM(Base::RuntimeError, "Invalid property");
}
auto editObj = freecad_cast<App::DocumentObject*>(prop.getContainer());
if (!editObj) {
FC_THROWM(Base::RuntimeError, "Editing object not found");
}
auto body = PartDesign::Body::findBodyOf(editObj);
if (!body) {
FC_THROWM(Base::RuntimeError, "No body for editing object: " << editObj->getNameInDocument());
}
std::map<App::DocumentObject*, std::vector<std::string>> links;
std::vector<App::SubObjectT> sobjs;
auto docName = editObj->getDocument()->getName();
auto inList = editObj->getInListEx(true);
for (auto sobjT : _sobjs) {
auto sobj = sobjT.getSubObject();
if (sobj == editObj) {
continue;
}
if (!sobj) {
FC_THROWM(Base::RuntimeError, "Object not found: " << sobjT.getSubObjectFullName(docName));
}
if (inList.count(sobj)) {
FC_THROWM(
Base::RuntimeError,
"Cyclic dependency on object " << sobjT.getSubObjectFullName(docName)
);
}
sobjT.normalized();
// Make sure that if a subelement is chosen for some object,
// we exclude whole object reference for that object.
auto& subs = links[sobj];
std::string element = sobjT.getOldElementName();
if (element.size()) {
if (subs.size() == 1 && subs.front().empty()) {
for (auto it = sobjs.begin(); it != sobjs.end();) {
if (it->getSubObject() == sobj) {
sobjs.erase(it);
break;
}
}
}
}
else if (subs.size() > 0) {
continue;
}
subs.push_back(std::move(element));
sobjs.push_back(sobjT);
}
int import = 0;
App::DocumentObject* obj = nullptr;
std::vector<std::string> subs;
for (const auto& sobjT : sobjs) {
auto sobj = sobjT.getSubObject();
if (PartDesign::Body::findBodyOf(sobj) != body) {
import = 1;
break;
}
if (!obj) {
obj = sobj;
}
else if (obj != sobj) {
if (!import) {
import = -1;
}
break;
}
subs.push_back(sobjT.getOldElementName());
}
if (!import) {
if (subs.empty()) {
subs.emplace_back();
}
if (obj == prop.getValue() && prop.getSubValues() == subs) {
return false;
}
prop.setValue(obj, std::move(subs));
return true;
}
return false;
}
void prepareProfileBased(
PartDesign::Body* pcActiveBody,
Gui::Command* cmd,
const std::string& which,
std::function<void(Part::Feature*, App::DocumentObject*)> func
)
{
auto base_worker = [=](App::DocumentObject* feature, const std::vector<std::string>& subs) {
if (!feature || !feature->isDerivedFrom<Part::Feature>()) {
return;
}
// Related to #0002760: when an operation can't be performed due to a broken
// profile then make sure that it is recomputed when cancelling the operation
// otherwise it might be impossible to see that it's broken.
if (feature->isTouched()) {
feature->recomputeFeature();
}
std::string FeatName = cmd->getUniqueObjectName(which.c_str(), pcActiveBody);
Gui::Command::openCommand((std::string("Make ") + which).c_str());
FCMD_OBJ_CMD(pcActiveBody, "newObject('PartDesign::" << which << "','" << FeatName << "')");
auto Feat = pcActiveBody->getDocument()->getObject(FeatName.c_str());
auto objCmd = Gui::Command::getObjectCmd(feature);
// Populate the subs parameter by checking for external elements before
// we construct our command.
auto ProfileFeature = freecad_cast<PartDesign::ProfileBased*>(Feat);
std::vector<std::string>& cmdSubs = const_cast<vector<std::string>&>(subs);
if (subs.size() == 0) {
importExternalElements(ProfileFeature->Profile, {feature});
cmdSubs = ProfileFeature->Profile.getSubValues();
}
// run the command in console to set the profile (without selected subelements)
auto runProfileCmd = [=]() {
FCMD_OBJ_CMD(Feat, "Profile = " << objCmd);
};
// run the command in console to set the profile with selected subelements
// useful to set, say, a face of a solid as the "profile"
auto runProfileCmdWithSubs = [=]() {
std::ostringstream ss;
for (auto& s : cmdSubs) {
ss << "'" << s << "',";
}
FCMD_OBJ_CMD(Feat, "Profile = (" << objCmd << ", [" << ss.str() << "])");
};
if (which.compare("AdditiveLoft") == 0 || which.compare("SubtractiveLoft") == 0) {
// for additive and subtractive lofts set subvalues even for sketches
// when a vertex is first selected
auto subName = subs.empty() ? "" : subs.front();
// `ProfileBased::getProfileShape()` and other methods will return
// just the sub-shapes if they are set. So when whole sketches are
// desired, do not set sub-values.
if (feature->isDerivedFrom<Part::Part2DObject>() && subName.compare(0, 6, "Vertex") != 0) {
runProfileCmd();
}
else {
runProfileCmdWithSubs();
}
// for additive and subtractive lofts allow the user to preselect the sections
std::vector<Gui::SelectionObject> selection = cmd->getSelection().getSelectionEx();
if (selection.size() > 1) { // treat additional selected objects as sections
for (std::vector<Gui::SelectionObject>::size_type ii = 1; ii < selection.size();
ii++) {
// Add subvalues even for sketches in case we just want points
auto objCmdSection = Gui::Command::getObjectCmd(selection[ii].getObject());
const auto& subnames = selection[ii].getSubNames();
std::ostringstream ss;
if (!subnames.empty()) {
for (auto& s : subnames) {
ss << "'" << s << "',";
}
}
else {
// an empty string indicates the whole object
ss << "''";
}
FCMD_OBJ_CMD(Feat, "Sections += [(" << objCmdSection << ", [" << ss.str() << "])]");
}
}
}
else if (which.compare("AdditivePipe") == 0 || which.compare("SubtractivePipe") == 0) {
// for additive and subtractive pipes set subvalues even for sketches
// to support point sections
auto subName = subs.empty() ? "" : subs.front();
// `ProfileBased::getProfileShape()` and other methods will return
// just the sub-shapes if they are set. So when whole sketches are
// desired, don't set sub-values.
if (feature->isDerivedFrom<Part::Part2DObject>() && subName.compare(0, 6, "Vertex") != 0) {
runProfileCmd();
}
else {
runProfileCmdWithSubs();
}
// for additive and subtractive pipes allow the user to preselect the spines
std::vector<Gui::SelectionObject> selection = cmd->getSelection().getSelectionEx();
if (selection.size() == 2) { // treat additional selected object as spine
std::vector<string> subnames = selection[1].getSubNames();
auto objCmdSpine = Gui::Command::getObjectCmd(selection[1].getObject());
if (selection[1].getObject()->isDerivedFrom<Part::Part2DObject>()
&& subnames.empty()) {
FCMD_OBJ_CMD(Feat, "Spine = " << objCmdSpine);
}
else {
std::ostringstream ss;
for (auto& s : subnames) {
if (s.find("Edge") != std::string::npos) {
ss << "'" << s << "',";
}
}
FCMD_OBJ_CMD(Feat, "Spine = (" << objCmdSpine << ", [" << ss.str() << "])");
}
}
}
else {
// Always use the subs
runProfileCmdWithSubs();
}
func(static_cast<Part::Feature*>(feature), Feat);
};
// in case of subtractive types, check that there is something to subtract from
if ((which.find("Subtractive") != std::string::npos) || (which.compare("Groove") == 0)
|| (which.compare("Pocket") == 0)) {
if (!pcActiveBody->isSolid()) {
QMessageBox msgBox(Gui::getMainWindow());
msgBox.setText(
QObject::tr("Cannot use this command as there is no solid to subtract from.")
);
msgBox.setInformativeText(
QObject::tr("Ensure that the body contains a feature before attempting a subtractive command.")
);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
return;
}
}
// if a profile is selected we can make our life easy and fast
std::vector<Gui::SelectionObject> selection = cmd->getSelection().getSelectionEx();
if (!selection.empty()) {
bool onlyAllowed = true;
for (const auto& it : selection) {
if (PartDesign::Body::findBodyOf(it.getObject())
!= pcActiveBody) { // the selected objects must belong to the body
onlyAllowed = false;
break;
}
}
if (!onlyAllowed) {
QMessageBox msgBox(Gui::getMainWindow());
msgBox.setText(
QObject::tr("Cannot use selected object. Selected object must belong to the active body")
);
msgBox.setInformativeText(
QObject::tr("Consider using a shape binder or a base feature to reference external geometry in a body")
);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
else {
base_worker(selection.front().getObject(), selection.front().getSubNames());
}
return;
}
// no face profile was selected, do the extended sketch logic
bool bNoSketchWasSelected = false;
// Get a valid sketch from the user
// First check selections
std::vector<App::DocumentObject*> sketches = cmd->getSelection().getObjectsOfType(
Part::Part2DObject::getClassTypeId()
);
if (sketches.empty()) { // no sketches were selected. Let user pick an object from valid ones
// available in document
sketches = cmd->getDocument()->getObjectsOfType(Part::Part2DObject::getClassTypeId());
bNoSketchWasSelected = true;
}
if (sketches.empty()) {
QMessageBox::warning(
Gui::getMainWindow(),
QObject::tr("No sketch to work on"),
QObject::tr("No sketch is available in the document")
);
return;
}
std::vector<PartDesignGui::TaskFeaturePick::featureStatus> status;
std::vector<App::DocumentObject*>::iterator firstFreeSketch;
int freeSketches = validateSketches(sketches, status, firstFreeSketch);
auto accepter = [=](const std::vector<App::DocumentObject*>& features) -> bool {
if (features.empty()) {
return false;
}
return true;
};
auto sketch_worker = [&, base_worker](std::vector<App::DocumentObject*> features) {
base_worker(features.front(), {});
};
// if there is a sketch selected which is from another body or part we need to bring up the
// pick task dialog to decide how those are handled
bool extReference = std::find_if(
status.begin(),
status.end(),
[](const PartDesignGui::TaskFeaturePick::featureStatus& s) {
return s == PartDesignGui::TaskFeaturePick::otherBody
|| s == PartDesignGui::TaskFeaturePick::otherPart
|| s == PartDesignGui::TaskFeaturePick::notInBody;
}
)
!= status.end();
// TODO Clean this up (2015-10-20, Fat-Zer)
if (pcActiveBody && !bNoSketchWasSelected && extReference) {
// Hint: In an older version the function expected the body to be inside
// a Part container and if not an error was raised and the function aborted.
// First of all, for the user this wasn't obvious because the error message
// was quite confusing (and thus the user may have done the wrong thing since
// they may have assumed the that the sketch was meant) and
// Second, there is no need that the body must be inside a Part container.
// For more details see: https://forum.freecad.org/viewtopic.php?f=19&t=32164
// The function has been modified not to expect the body to be in the Part
// and it now directly invokes the 'makeCopy' dialog.
auto* pcActivePart = PartDesignGui::getPartFor(pcActiveBody, false);
QDialog dia(Gui::getMainWindow());
PartDesignGui::Ui_DlgReference dlg;
dlg.setupUi(&dia);
dia.setModal(true);
int result = dia.exec();
if (result == QDialog::DialogCode::Rejected) {
return;
}
if (!dlg.radioXRef->isChecked()) {
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Make Copy"));
auto copy = PartDesignGui::TaskFeaturePick::makeCopy(
sketches[0],
"",
dlg.radioIndependent->isChecked()
);
auto oBody = PartDesignGui::getBodyFor(sketches[0], false);
if (oBody) {
pcActiveBody->addObject(copy);
}
else if (pcActivePart) {
pcActivePart->addObject(copy);
}
sketches[0] = copy;
firstFreeSketch = sketches.begin();
}
}
// Show sketch choose dialog and let user pick sketch if no sketch was selected and no free one
// available or multiple free ones are available
if (bNoSketchWasSelected && (freeSketches != 1)) {
Gui::TaskView::TaskDialog* dlg = Gui::Control().activeDialog();
PartDesignGui::TaskDlgFeaturePick* pickDlg
= qobject_cast<PartDesignGui::TaskDlgFeaturePick*>(dlg);
if (dlg && !pickDlg) {
QMessageBox msgBox(Gui::getMainWindow());
msgBox.setText(QObject::tr("A dialog is already open in the task panel"));
msgBox.setInformativeText(QObject::tr("Close this dialog?"));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setDefaultButton(QMessageBox::Yes);
int ret = msgBox.exec();
if (ret == QMessageBox::Yes) {
Gui::Control().closeDialog();
}
else {
return;
}
}
if (dlg) {
Gui::Control().closeDialog();
}
Gui::Selection().clearSelection();
pickDlg = new PartDesignGui::TaskDlgFeaturePick(sketches, status, accepter, sketch_worker, true);
// Logically dead code because 'bNoSketchWasSelected' must be true
// if (!bNoSketchWasSelected && extReference)
// pickDlg->showExternal(true);
Gui::Control().showDialog(pickDlg);
}
else {
std::vector<App::DocumentObject*> theSketch;
if (!bNoSketchWasSelected) {
theSketch.push_back(sketches[0]);
}
else {
theSketch.push_back(*firstFreeSketch);
}
sketch_worker(theSketch);
}
}
void finishProfileBased(const Gui::Command* cmd, const Part::Feature* sketch, App::DocumentObject* Feat)
{
if (sketch && sketch->isDerivedFrom<Part::Part2DObject>()) {
FCMD_OBJ_HIDE(sketch);
}
finishFeature(cmd, Feat);
}
void prepareProfileBased(Gui::Command* cmd, const std::string& which, double length)
{
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
auto worker = [cmd, length](Part::Feature* profile, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
// specific parameters for Pad/Pocket
FCMD_OBJ_CMD(Feat, "Length = " << length);
Gui::Command::updateActive();
Part::Part2DObject* sketch = dynamic_cast<Part::Part2DObject*>(profile);
if (sketch) {
std::ostringstream str;
Gui::cmdAppObject(
Feat,
str << "ReferenceAxis = (" << Gui::Command::getObjectCmd(sketch) << ",['N_Axis'])"
);
}
finishProfileBased(cmd, sketch, Feat);
};
prepareProfileBased(pcActiveBody, cmd, which, worker);
}
//===========================================================================
// PartDesign_Pad
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignPad)
CmdPartDesignPad::CmdPartDesignPad()
: Command("PartDesign_Pad")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Pad");
sToolTipText = QT_TR_NOOP("Extrudes the selected sketch or profile and adds it to the body");
sWhatsThis = "PartDesign_Pad";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Pad";
}
void CmdPartDesignPad::activated(int iMsg)
{
Q_UNUSED(iMsg);
prepareProfileBased(this, "Pad", 10.0);
}
bool CmdPartDesignPad::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Pocket
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignPocket)
CmdPartDesignPocket::CmdPartDesignPocket()
: Command("PartDesign_Pocket")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Pocket");
sToolTipText = QT_TR_NOOP("Extrudes the selected sketch or profile and removes it from the body");
sWhatsThis = "PartDesign_Pocket";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Pocket";
}
void CmdPartDesignPocket::activated(int iMsg)
{
Q_UNUSED(iMsg);
prepareProfileBased(this, "Pocket", 5.0);
}
bool CmdPartDesignPocket::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Hole
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignHole)
CmdPartDesignHole::CmdPartDesignHole()
: Command("PartDesign_Hole")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Hole");
sToolTipText
= QT_TR_NOOP("Creates holes in the active body at the center points of circles or arcs of the selected sketch or profile");
sWhatsThis = "PartDesign_Hole";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Hole";
}
void CmdPartDesignHole::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd](Part::Feature* sketch, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
finishProfileBased(cmd, sketch, Feat);
};
prepareProfileBased(pcActiveBody, this, "Hole", worker);
}
bool CmdPartDesignHole::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Revolution
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignRevolution)
CmdPartDesignRevolution::CmdPartDesignRevolution()
: Command("PartDesign_Revolution")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Revolve");
sToolTipText = QT_TR_NOOP(
"Revolves the selected sketch or profile around a line or axis and adds it to the body"
);
sWhatsThis = "PartDesign_Revolution";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Revolution";
}
void CmdPartDesignRevolution::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd, &pcActiveBody](Part::Feature* sketch, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
if (sketch->isDerivedFrom<Part::Part2DObject>()) {
FCMD_OBJ_CMD(Feat, "ReferenceAxis = (" << getObjectCmd(sketch) << ",['V_Axis'])");
}
else {
FCMD_OBJ_CMD(
Feat,
"ReferenceAxis = (" << getObjectCmd(pcActiveBody->getOrigin()->getY()) << ",[''])"
);
}
FCMD_OBJ_CMD(Feat, "Angle = 360.0");
PartDesign::Revolution* pcRevolution = dynamic_cast<PartDesign::Revolution*>(Feat);
if (pcRevolution && pcRevolution->suggestReversed()) {
FCMD_OBJ_CMD(Feat, "Reversed = 1");
}
finishProfileBased(cmd, sketch, Feat);
};
prepareProfileBased(pcActiveBody, this, "Revolution", worker);
}
bool CmdPartDesignRevolution::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Groove
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignGroove)
CmdPartDesignGroove::CmdPartDesignGroove()
: Command("PartDesign_Groove")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Groove");
sToolTipText = QT_TR_NOOP(
"Revolves the sketch or profile around a line or axis and removes it from the body"
);
sWhatsThis = "PartDesign_Groove";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Groove";
}
void CmdPartDesignGroove::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd, &pcActiveBody](Part::Feature* sketch, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
if (sketch->isDerivedFrom<Part::Part2DObject>()) {
FCMD_OBJ_CMD(Feat, "ReferenceAxis = (" << getObjectCmd(sketch) << ",['V_Axis'])");
}
else {
FCMD_OBJ_CMD(
Feat,
"ReferenceAxis = (" << getObjectCmd(pcActiveBody->getOrigin()->getY()) << ",[''])"
);
}
FCMD_OBJ_CMD(Feat, "Angle = 360.0");
try {
// This raises as exception if line is perpendicular to sketch/support face.
// Here we should continue to give the user a chance to change the default values.
PartDesign::Groove* pcGroove = dynamic_cast<PartDesign::Groove*>(Feat);
if (pcGroove && pcGroove->suggestReversed()) {
FCMD_OBJ_CMD(Feat, "Reversed = 1");
}
}
catch (const Base::Exception& e) {
e.reportException();
}
finishProfileBased(cmd, sketch, Feat);
};
prepareProfileBased(pcActiveBody, this, "Groove", worker);
}
bool CmdPartDesignGroove::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_AdditivePipe
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignAdditivePipe)
CmdPartDesignAdditivePipe::CmdPartDesignAdditivePipe()
: Command("PartDesign_AdditivePipe")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Additive Pipe");
sToolTipText = QT_TR_NOOP(
"Sweeps the selected sketch or profile along a path and adds it to the body"
);
sWhatsThis = "PartDesign_AdditivePipe";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_AdditivePipe";
}
void CmdPartDesignAdditivePipe::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd](Part::Feature* sketch, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
// specific parameters for pipe
Gui::Command::updateActive();
finishProfileBased(cmd, sketch, Feat);
};
prepareProfileBased(pcActiveBody, this, "AdditivePipe", worker);
}
bool CmdPartDesignAdditivePipe::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_SubtractivePipe
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignSubtractivePipe)
CmdPartDesignSubtractivePipe::CmdPartDesignSubtractivePipe()
: Command("PartDesign_SubtractivePipe")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Subtractive Pipe");
sToolTipText = QT_TR_NOOP(
"Sweeps the selected sketch or profile along a path and removes it from the body"
);
sWhatsThis = "PartDesign_SubtractivePipe";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_SubtractivePipe";
}
void CmdPartDesignSubtractivePipe::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd](Part::Feature* sketch, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
// specific parameters for pipe
Gui::Command::updateActive();
finishProfileBased(cmd, sketch, Feat);
};
prepareProfileBased(pcActiveBody, this, "SubtractivePipe", worker);
}
bool CmdPartDesignSubtractivePipe::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_AdditiveLoft
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignAdditiveLoft)
CmdPartDesignAdditiveLoft::CmdPartDesignAdditiveLoft()
: Command("PartDesign_AdditiveLoft")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Additive Loft");
sToolTipText = QT_TR_NOOP(
"Lofts the selected sketch or profile along a path and adds it to the body"
);
sWhatsThis = "PartDesign_AdditiveLoft";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_AdditiveLoft";
}
void CmdPartDesignAdditiveLoft::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd](Part::Feature* sketch, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
// specific parameters for pipe
Gui::Command::updateActive();
finishProfileBased(cmd, sketch, Feat);
};
prepareProfileBased(pcActiveBody, this, "AdditiveLoft", worker);
}
bool CmdPartDesignAdditiveLoft::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_SubtractiveLoft
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignSubtractiveLoft)
CmdPartDesignSubtractiveLoft::CmdPartDesignSubtractiveLoft()
: Command("PartDesign_SubtractiveLoft")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Subtractive Loft");
sToolTipText = QT_TR_NOOP(
"Lofts the selected sketch or profile along a path and removes it from the body"
);
sWhatsThis = "PartDesign_SubtractiveLoft";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_SubtractiveLoft";
}
void CmdPartDesignSubtractiveLoft::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd](Part::Feature* sketch, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
// specific parameters for pipe
Gui::Command::updateActive();
finishProfileBased(cmd, sketch, Feat);
};
prepareProfileBased(pcActiveBody, this, "SubtractiveLoft", worker);
}
bool CmdPartDesignSubtractiveLoft::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_AdditiveHelix
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignAdditiveHelix)
CmdPartDesignAdditiveHelix::CmdPartDesignAdditiveHelix()
: Command("PartDesign_AdditiveHelix")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Additive Helix");
sToolTipText = QT_TR_NOOP(
"Sweeps the selected sketch or profile along a helix and adds it to the body"
);
sWhatsThis = "PartDesign_AdditiveHelix";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_AdditiveHelix";
}
void CmdPartDesignAdditiveHelix::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd, &pcActiveBody](Part::Feature* sketch, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
// Creating a helix with default values isn't always valid but fixes
// itself when more values are set. So, this guard is used to suppress
// errors before the user is able to change the parameters.
Base::ObjectStatusLocker<App::Document::Status, App::Document> guard(
App::Document::IgnoreErrorOnRecompute,
Feat->getDocument(),
true
);
// specific parameters for helix
Gui::Command::updateActive();
if (sketch->isDerivedFrom<Part::Part2DObject>()) {
FCMD_OBJ_CMD(Feat, "ReferenceAxis = (" << getObjectCmd(sketch) << ",['V_Axis'])");
}
else {
FCMD_OBJ_CMD(
Feat,
"ReferenceAxis = (" << getObjectCmd(pcActiveBody->getOrigin()->getY()) << ",[''])"
);
}
finishProfileBased(cmd, sketch, Feat);
// If the initial helix creation fails then it leaves the base object invisible which makes
// things more difficult for the user. To avoid this the base object will be made tmp.
// visible again.
if (Feat->isError()) {
App::DocumentObject* base = static_cast<PartDesign::Feature*>(Feat)->BaseFeature.getValue();
if (base) {
PartDesignGui::ViewProvider* view = dynamic_cast<PartDesignGui::ViewProvider*>(
Gui::Application::Instance->getViewProvider(base)
);
if (view) {
view->makeTemporaryVisible(true);
}
}
}
};
prepareProfileBased(pcActiveBody, this, "AdditiveHelix", worker);
}
bool CmdPartDesignAdditiveHelix::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_SubtractiveHelix
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignSubtractiveHelix)
CmdPartDesignSubtractiveHelix::CmdPartDesignSubtractiveHelix()
: Command("PartDesign_SubtractiveHelix")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Subtractive Helix");
sToolTipText = QT_TR_NOOP(
"Sweeps the selected sketch or profile along a helix and removes it from the body"
);
sWhatsThis = "PartDesign_SubtractiveHelix";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_SubtractiveHelix";
}
void CmdPartDesignSubtractiveHelix::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd, &pcActiveBody](Part::Feature* sketch, App::DocumentObject* Feat) {
if (!Feat) {
return;
}
// specific parameters for helix
Gui::Command::updateActive();
if (sketch->isDerivedFrom<Part::Part2DObject>()) {
FCMD_OBJ_CMD(Feat, "ReferenceAxis = (" << getObjectCmd(sketch) << ",['V_Axis'])");
}
else {
FCMD_OBJ_CMD(
Feat,
"ReferenceAxis = (" << getObjectCmd(pcActiveBody->getOrigin()->getY()) << ",[''])"
);
}
finishProfileBased(cmd, sketch, Feat);
};
prepareProfileBased(pcActiveBody, this, "SubtractiveHelix", worker);
}
bool CmdPartDesignSubtractiveHelix::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// Common utility functions for Dressup features
//===========================================================================
bool dressupGetSelected(
Gui::Command* cmd,
const std::string& which,
Gui::SelectionObject& selected,
bool& useAllEdges,
bool& noSelection
)
{
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return false;
}
std::vector<Gui::SelectionObject> selection = cmd->getSelection().getSelectionEx();
if (selection.empty()) {
noSelection = true;
return true;
}
else if (selection.size() != 1) {
QMessageBox::warning(
Gui::getMainWindow(),
QObject::tr("Wrong selection"),
QObject::tr("Select an edge, face, or body from a single body.")
);
return false;
}
else if (pcActiveBody != PartDesignGui::getBodyFor(selection[0].getObject(), false)) {
QMessageBox::warning(
Gui::getMainWindow(),
QObject::tr("Selection is not in the active body"),
QObject::tr("Select an edge, face, or body from an active body.")
);
return false;
}
Gui::Selection().clearSelection();
// set the
selected = selection[0];
if (!selected.isObjectTypeOf(Part::Feature::getClassTypeId())) {
QMessageBox::warning(
Gui::getMainWindow(),
QObject::tr("Wrong object type"),
QObject::tr("%1 works only on parts.").arg(QString::fromStdString(which))
);
return false;
}
Part::Feature* base = static_cast<Part::Feature*>(selected.getObject());
const Part::TopoShape& TopShape = base->Shape.getShape();
if (TopShape.getShape().IsNull()) {
QMessageBox::warning(
Gui::getMainWindow(),
QObject::tr("Wrong selection"),
QObject::tr("Shape of the selected part is empty")
);
return false;
}
// if 1 Part::Feature object selected, but no subobjects, select all edges for the user
// but only for fillet and chamfer (not for draft or thickness)
if (selection[0].getSubNames().empty()
&& (which.compare("Fillet") == 0 || which.compare("Chamfer") == 0)) {
useAllEdges = true;
std::string edgeTypeName = Part::TopoShape::shapeName(TopAbs_EDGE); //"Edge"
int count = TopShape.countSubElements(edgeTypeName.c_str());
std::string docName = App::GetApplication().getDocumentName(base->getDocument());
std::string objName = base->getNameInDocument();
for (int ii = 0; ii < count; ii++) {
std::ostringstream edgeName;
edgeName << edgeTypeName << ii + 1;
Gui::Selection().addSelection(docName.c_str(), objName.c_str(), edgeName.str().c_str());
}
selection = cmd->getSelection().getSelectionEx();
if (selection.size() == 1) {
selected = selection[0];
}
}
return true;
}
void finishDressupFeature(
const Gui::Command* cmd,
const std::string& which,
Part::Feature* base,
const std::vector<std::string>& SubNames,
const bool useAllEdges
)
{
std::ostringstream str;
str << '(' << Gui::Command::getObjectCmd(base) << ",[";
for (const auto& SubName : SubNames) {
str << "'" << SubName << "',";
}
str << "])";
std::string FeatName = cmd->getUniqueObjectName(which.c_str(), base);
auto body = PartDesignGui::getBodyFor(base, false);
if (!body) {
return;
}
cmd->openCommand((std::string("Make ") + which).c_str());
FCMD_OBJ_CMD(body, "newObject('PartDesign::" << which << "','" << FeatName << "')");
auto Feat = body->getDocument()->getObject(FeatName.c_str());
FCMD_OBJ_CMD(Feat, "Base = " << str.str());
if (useAllEdges && (which.compare("Fillet") == 0 || which.compare("Chamfer") == 0)) {
FCMD_OBJ_CMD(Feat, "UseAllEdges = True");
}
cmd->doCommand(cmd->Gui, "Gui.Selection.clearSelection()");
finishFeature(cmd, Feat, base);
App::DocumentObject* baseFeature = static_cast<PartDesign::DressUp*>(Feat)->Base.getValue();
if (baseFeature) {
PartDesignGui::ViewProvider* view = dynamic_cast<PartDesignGui::ViewProvider*>(
Gui::Application::Instance->getViewProvider(baseFeature)
);
// in case there is an error, for example when a fillet is larger than the available space
// display the base feature to avoid that the user sees nothing
if (view && Feat->isError()) {
view->Visibility.setValue(true);
}
}
}
void makeChamferOrFillet(Gui::Command* cmd, const std::string& which)
{
bool useAllEdges = false;
bool noSelection = false;
Gui::SelectionObject selected;
if (!dressupGetSelected(cmd, which, selected, useAllEdges, noSelection)) {
return;
}
Part::Feature* base;
std::vector<std::string> SubNames;
if (noSelection) {
base = static_cast<Part::Feature*>(PartDesignGui::getBody(true)->Tip.getValue());
}
else {
base = static_cast<Part::Feature*>(selected.getObject());
SubNames = std::vector<std::string>(selected.getSubNames());
}
finishDressupFeature(cmd, which, base, SubNames, useAllEdges);
}
//===========================================================================
// PartDesign_Fillet
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignFillet)
CmdPartDesignFillet::CmdPartDesignFillet()
: Command("PartDesign_Fillet")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Fillet");
sToolTipText = QT_TR_NOOP("Applies a fillet to the selected edges or faces");
sWhatsThis = "PartDesign_Fillet";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Fillet";
}
void CmdPartDesignFillet::activated(int iMsg)
{
Q_UNUSED(iMsg);
makeChamferOrFillet(this, "Fillet");
}
bool CmdPartDesignFillet::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Chamfer
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignChamfer)
CmdPartDesignChamfer::CmdPartDesignChamfer()
: Command("PartDesign_Chamfer")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Chamfer");
sToolTipText = QT_TR_NOOP("Applies a chamfer to the selected edges or faces");
sWhatsThis = "PartDesign_Chamfer";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Chamfer";
}
void CmdPartDesignChamfer::activated(int iMsg)
{
Q_UNUSED(iMsg);
makeChamferOrFillet(this, "Chamfer");
doCommand(Gui, "Gui.Selection.clearSelection()");
}
bool CmdPartDesignChamfer::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Draft
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignDraft)
CmdPartDesignDraft::CmdPartDesignDraft()
: Command("PartDesign_Draft")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Draft");
sToolTipText = QT_TR_NOOP("Applies a draft to the selected faces");
sWhatsThis = "PartDesign_Draft";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Draft";
}
void CmdPartDesignDraft::activated(int iMsg)
{
Q_UNUSED(iMsg);
Gui::SelectionObject selected;
bool useAllEdges = false;
bool noSelection = false;
if (!dressupGetSelected(this, "Draft", selected, useAllEdges, noSelection)) {
return;
}
Part::Feature* base;
std::vector<std::string> SubNames;
if (noSelection) {
base = static_cast<Part::Feature*>(PartDesignGui::getBody(true)->Tip.getValue());
}
else {
base = static_cast<Part::Feature*>(selected.getObject());
SubNames = std::vector<std::string>(selected.getSubNames());
const Part::TopoShape& TopShape = base->Shape.getShape();
// filter out the edges
size_t i = 0;
while (i < SubNames.size()) {
std::string aSubName = SubNames.at(i);
if (aSubName.compare(0, 4, "Face") == 0) {
// Check for valid face types
TopoDS_Face face = TopoDS::Face(TopShape.getSubShape(aSubName.c_str()));
BRepAdaptor_Surface sf(face);
if ((sf.GetType() != GeomAbs_Plane) && (sf.GetType() != GeomAbs_Cylinder)
&& (sf.GetType() != GeomAbs_Cone)) {
SubNames.erase(SubNames.begin() + i);
}
}
else {
// empty name or any other sub-element
SubNames.erase(SubNames.begin() + i);
}
i++;
}
}
finishDressupFeature(this, "Draft", base, SubNames, useAllEdges);
}
bool CmdPartDesignDraft::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Thickness
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignThickness)
CmdPartDesignThickness::CmdPartDesignThickness()
: Command("PartDesign_Thickness")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Thickness");
sToolTipText = QT_TR_NOOP("Applies thickness and removes the selected faces");
sWhatsThis = "PartDesign_Thickness";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Thickness";
}
void CmdPartDesignThickness::activated(int iMsg)
{
Q_UNUSED(iMsg);
Gui::SelectionObject selected;
bool useAllEdges = false;
bool noSelection = false;
if (!dressupGetSelected(this, "Thickness", selected, useAllEdges, noSelection)) {
return;
}
Part::Feature* base;
std::vector<std::string> SubNames;
if (noSelection) {
base = static_cast<Part::Feature*>(PartDesignGui::getBody(true)->Tip.getValue());
}
else {
base = static_cast<Part::Feature*>(selected.getObject());
SubNames = std::vector<std::string>(selected.getSubNames());
// filter out the edges
size_t i = 0;
while (i < SubNames.size()) {
std::string aSubName = SubNames.at(i);
if (aSubName.compare(0, 4, "Face") != 0) {
// empty name or any other sub-element
SubNames.erase(SubNames.begin() + i);
}
i++;
}
}
finishDressupFeature(this, "Thickness", base, SubNames, useAllEdges);
}
bool CmdPartDesignThickness::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// Common functions for all Transformed features
//===========================================================================
void prepareTransformed(
PartDesign::Body* pcActiveBody,
Gui::Command* cmd,
const std::string& which,
std::function<void(App::DocumentObject*, std::vector<App::DocumentObject*>)> func
)
{
std::string FeatName = cmd->getUniqueObjectName(which.c_str(), pcActiveBody);
auto worker = [=](std::vector<App::DocumentObject*> features) {
std::string msg("Make ");
msg += which;
Gui::Command::openCommand(msg.c_str());
FCMD_OBJ_CMD(pcActiveBody, "newObject('PartDesign::" << which << "','" << FeatName << "')");
// FIXME: There seems to be kind of a race condition here, leading to sporadic errors like
// Exception (Thu Sep 6 11:52:01 2012): 'App.Document' object has no attribute 'Mirrored'
Gui::Command::updateActive(); // Helps to ensure that the object already exists when the
// next command comes up
auto Feat = pcActiveBody->getDocument()->getObject(FeatName.c_str());
if (features.empty()) {
FCMD_OBJ_CMD(Feat, "TransformMode = \"Whole shape\"");
}
else {
std::stringstream str;
str << "Originals = [";
for (auto feature : features) {
str << cmd->getObjectCmd(feature) << ",";
}
str << "]";
FCMD_OBJ_CMD(Feat, str.str().c_str());
}
// TODO What is this function supposed to do? (2015-08-05, Fat-Zer)
func(Feat, features);
// Set the tip of the body
FCMD_OBJ_CMD(pcActiveBody, "Tip = " << Gui::Command::getObjectCmd(Feat));
Gui::Command::updateActive();
};
// Get a valid original from the user
std::vector<App::DocumentObject*> features = cmd->getSelection().getObjectsOfType(
PartDesign::Feature::getClassTypeId()
);
PartDesign::Body* activeBody = PartDesignGui::getBody(true);
for (auto feature : features) {
if (activeBody != PartDesignGui::getBodyFor(feature, false)) {
QMessageBox::warning(
Gui::getMainWindow(),
QObject::tr("Selection is not in the active body"),
QObject::tr("Please select only one feature in an active body.")
);
return;
}
}
worker(features);
}
void finishTransformed(Gui::Command* cmd, App::DocumentObject* Feat)
{
finishFeature(cmd, Feat);
}
//===========================================================================
// PartDesign_Mirrored
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignMirrored)
CmdPartDesignMirrored::CmdPartDesignMirrored()
: Command("PartDesign_Mirrored")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Mirror");
sToolTipText = QT_TR_NOOP("Mirrors the selected features or active body");
sWhatsThis = "PartDesign_Mirrored";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Mirrored";
}
void CmdPartDesignMirrored::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd,
pcActiveBody](App::DocumentObject* Feat, std::vector<App::DocumentObject*> features) {
bool direction = false;
if (!features.empty() && features.front()->isDerivedFrom<PartDesign::ProfileBased>()) {
Part::Part2DObject* sketch = (static_cast<PartDesign::ProfileBased*>(features.front()))
->getVerifiedSketch(/* silent =*/true);
if (sketch) {
FCMD_OBJ_CMD(Feat, "MirrorPlane = (" << getObjectCmd(sketch) << ", ['V_Axis'])");
direction = true;
}
}
if (!direction) {
FCMD_OBJ_CMD(
Feat,
"MirrorPlane = (" << getObjectCmd(pcActiveBody->getOrigin()->getXY()) << ", [''])"
);
}
finishTransformed(cmd, Feat);
};
prepareTransformed(pcActiveBody, this, "Mirrored", worker);
}
bool CmdPartDesignMirrored::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_LinearPattern
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignLinearPattern)
CmdPartDesignLinearPattern::CmdPartDesignLinearPattern()
: Command("PartDesign_LinearPattern")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Linear Pattern");
sToolTipText = QT_TR_NOOP(
"Duplicates the selected features or the active body in a linear pattern"
);
sWhatsThis = "PartDesign_LinearPattern";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_LinearPattern";
}
void CmdPartDesignLinearPattern::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker =
[cmd, pcActiveBody](App::DocumentObject* Feat, std::vector<App::DocumentObject*> features) {
bool direction = false;
if (!features.empty() && features.front()->isDerivedFrom<PartDesign::ProfileBased>()) {
Part::Part2DObject* sketch = (static_cast<PartDesign::ProfileBased*>(features.front()))
->getVerifiedSketch(/* silent =*/true);
if (sketch) {
FCMD_OBJ_CMD(
Feat,
"Direction = (" << Gui::Command::getObjectCmd(sketch) << ", ['H_Axis'])"
);
FCMD_OBJ_CMD(
Feat,
"Direction2 = (" << Gui::Command::getObjectCmd(sketch) << ", ['V_Axis'])"
);
direction = true;
}
}
if (!direction) {
FCMD_OBJ_CMD(
Feat,
"Direction = (" << Gui::Command::getObjectCmd(pcActiveBody->getOrigin()->getX())
<< ",[''])"
);
}
FCMD_OBJ_CMD(Feat, "Length = 100");
FCMD_OBJ_CMD(Feat, "Occurrences = 2");
finishTransformed(cmd, Feat);
};
prepareTransformed(pcActiveBody, this, "LinearPattern", worker);
}
bool CmdPartDesignLinearPattern::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_PolarPattern
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignPolarPattern)
CmdPartDesignPolarPattern::CmdPartDesignPolarPattern()
: Command("PartDesign_PolarPattern")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Polar Pattern");
sToolTipText = QT_TR_NOOP(
"Duplicates the selected features or the active body in a circular pattern"
);
sWhatsThis = "PartDesign_PolarPattern";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_PolarPattern";
}
void CmdPartDesignPolarPattern::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd,
pcActiveBody](App::DocumentObject* Feat, std::vector<App::DocumentObject*> features) {
bool direction = false;
if (!features.empty() && features.front()->isDerivedFrom<PartDesign::ProfileBased>()) {
Part::Part2DObject* sketch = (static_cast<PartDesign::ProfileBased*>(features.front()))
->getVerifiedSketch(/* silent =*/true);
if (sketch) {
FCMD_OBJ_CMD(Feat, "Axis = (" << Gui::Command::getObjectCmd(sketch) << ",['N_Axis'])");
direction = true;
}
}
if (!direction) {
FCMD_OBJ_CMD(
Feat,
"Axis = (" << Gui::Command::getObjectCmd(pcActiveBody->getOrigin()->getZ()) << ",[''])"
);
}
FCMD_OBJ_CMD(Feat, "Angle = 360");
FCMD_OBJ_CMD(Feat, "Occurrences = 2");
finishTransformed(cmd, Feat);
};
prepareTransformed(pcActiveBody, this, "PolarPattern", worker);
}
bool CmdPartDesignPolarPattern::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Scaled
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignScaled)
CmdPartDesignScaled::CmdPartDesignScaled()
: Command("PartDesign_Scaled")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Scale");
sToolTipText = QT_TR_NOOP("Scales the selected features or the active body");
sWhatsThis = "PartDesign_Scaled";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Scaled";
}
void CmdPartDesignScaled::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
Gui::Command* cmd = this;
auto worker = [cmd](App::DocumentObject* Feat, std::vector<App::DocumentObject*> /*features*/) {
FCMD_OBJ_CMD(Feat, "Factor = 2");
FCMD_OBJ_CMD(Feat, "Occurrences = 2");
finishTransformed(cmd, Feat);
};
prepareTransformed(pcActiveBody, this, "Scale", worker);
}
bool CmdPartDesignScaled::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_MultiTransform
//===========================================================================
DEF_STD_CMD_A(CmdPartDesignMultiTransform)
CmdPartDesignMultiTransform::CmdPartDesignMultiTransform()
: Command("PartDesign_MultiTransform")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Multi-Transform");
sToolTipText = QT_TR_NOOP(
"Applies multiple transformations to the selected features or active body"
);
sWhatsThis = "PartDesign_MultiTransform";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_MultiTransform";
}
void CmdPartDesignMultiTransform::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(true);
if (!pcActiveBody) {
return;
}
std::vector<App::DocumentObject*> features;
// Check if a Transformed feature has been selected, convert it to MultiTransform
features = getSelection().getObjectsOfType(PartDesign::Transformed::getClassTypeId());
if (!features.empty()) {
// Throw out MultiTransform features, we don't want to nest them
for (std::vector<App::DocumentObject*>::iterator f = features.begin(); f != features.end();) {
if ((*f)->isDerivedFrom<PartDesign::MultiTransform>()) {
f = features.erase(f);
}
else {
f++;
}
}
if (features.empty()) {
return;
}
// Note: If multiple Transformed features were selected, only the first one is used
PartDesign::Transformed* trFeat = static_cast<PartDesign::Transformed*>(features.front());
// Move the insert point back one feature
App::DocumentObject* oldTip = nullptr;
App::DocumentObject* prevFeature = nullptr;
if (pcActiveBody) {
oldTip = pcActiveBody->Tip.getValue();
prevFeature = pcActiveBody->getPrevSolidFeature(trFeat);
}
Gui::Selection().clearSelection();
if (prevFeature) {
Gui::Selection().addSelection(
prevFeature->getDocument()->getName(),
prevFeature->getNameInDocument()
);
}
openCommand(QT_TRANSLATE_NOOP("Command", "Convert to Multi-Transform feature"));
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
rcCmdMgr.runCommandByName("PartDesign_MoveTip");
// We cannot remove the Transform feature from the body as otherwise
// we will have a PartDesign feature without a body which is not allowed
// and causes to pop up the migration dialog later when adding new features
// to the body.
// Additionally it creates the error message: "Links go out of the allowed scope"
// #0003509
#if 0
// Remove the Transformed feature from the Body
if (pcActiveBody)
FCMD_OBJ_CMD(pcActiveBody,"removeObject("<<getObjectCmd(trFeat)<<")");
#endif
// Create a MultiTransform feature and move the Transformed feature inside it
std::string FeatName = getUniqueObjectName("MultiTransform", pcActiveBody);
FCMD_OBJ_CMD(pcActiveBody, "newObject('PartDesign::MultiTransform','" << FeatName << "')");
auto Feat = pcActiveBody->getDocument()->getObject(FeatName.c_str());
auto objCmd = getObjectCmd(trFeat);
FCMD_OBJ_CMD(Feat, "Originals = " << objCmd << ".Originals");
FCMD_OBJ_CMD(Feat, "TransformMode = " << objCmd << ".TransformMode");
FCMD_OBJ_CMD(Feat, "BaseFeature = " << objCmd << ".BaseFeature");
FCMD_OBJ_CMD(Feat, "Transformations = [" << objCmd << "]");
FCMD_OBJ_CMD(trFeat, "Originals = []");
// Add the MultiTransform into the Body at the current insert point
finishFeature(this, Feat);
// Restore the insert point
if (pcActiveBody && oldTip != trFeat) {
Gui::Selection().clearSelection();
Gui::Selection().addSelection(oldTip->getDocument()->getName(), oldTip->getNameInDocument());
rcCmdMgr.runCommandByName("PartDesign_MoveTip");
Gui::Selection().clearSelection();
} // otherwise the insert point remains at the new MultiTransform, which is fine
}
else {
Gui::Command* cmd = this;
auto worker =
[cmd,
pcActiveBody](App::DocumentObject* Feat, std::vector<App::DocumentObject*> /*features*/) {
// Make sure the user isn't presented with an empty screen because no
// transformations are defined yet...
App::DocumentObject* prevSolid = pcActiveBody->Tip.getValue();
if (prevSolid) {
Part::Feature* feat = static_cast<Part::Feature*>(prevSolid);
FCMD_OBJ_CMD(Feat, "Shape = " << getObjectCmd(feat) << ".Shape");
}
finishFeature(cmd, Feat);
};
prepareTransformed(pcActiveBody, this, "MultiTransform", worker);
}
}
bool CmdPartDesignMultiTransform::isActive()
{
return hasActiveDocument();
}
//===========================================================================
// PartDesign_Boolean
//===========================================================================
/* Boolean commands =======================================================*/
DEF_STD_CMD_A(CmdPartDesignBoolean)
CmdPartDesignBoolean::CmdPartDesignBoolean()
: Command("PartDesign_Boolean")
{
sAppModule = "PartDesign";
sGroup = QT_TR_NOOP("PartDesign");
sMenuText = QT_TR_NOOP("Boolean Operation");
sToolTipText = QT_TR_NOOP(
"Applies boolean operations with the selected objects and the active body"
);
sWhatsThis = "PartDesign_Boolean";
sStatusTip = sToolTipText;
sPixmap = "PartDesign_Boolean";
}
void CmdPartDesignBoolean::activated(int iMsg)
{
Q_UNUSED(iMsg);
PartDesign::Body* pcActiveBody = PartDesignGui::getBody(/*messageIfNot = */ true);
if (!pcActiveBody) {
return;
}
Gui::SelectionFilter BodyFilter("SELECT Part::Feature COUNT 1..");
openCommand(QT_TRANSLATE_NOOP("Command", "Create Boolean"));
std::string FeatName = getUniqueObjectName("Boolean", pcActiveBody);
FCMD_OBJ_CMD(pcActiveBody, "newObject('PartDesign::Boolean','" << FeatName << "')");
auto Feat = pcActiveBody->getDocument()->getObject(FeatName.c_str());
// If we don't add an object to the boolean group then don't update the body
// as otherwise this will fail and it will be marked as invalid
bool updateDocument = false;
if (BodyFilter.match() && !BodyFilter.Result.empty()) {
std::vector<App::DocumentObject*> bodies;
for (auto& results : BodyFilter.Result) {
for (auto& result : results) {
if (result.getObject() != pcActiveBody) {
bodies.push_back(result.getObject());
}
}
}
if (!bodies.empty()) {
updateDocument = true;
std::string bodyString = PartDesignGui::buildLinkListPythonStr(bodies);
FCMD_OBJ_CMD(Feat, "addObjects(" << bodyString << ")");
}
}
finishFeature(this, Feat, nullptr, false, updateDocument);
}
bool CmdPartDesignBoolean::isActive()
{
if (getActiveGuiDocument()) {
return true;
}
else {
return false;
}
}
// Command group for datums =============================================
class CmdPartDesignCompDatums: public Gui::GroupCommand
{
public:
CmdPartDesignCompDatums()
: GroupCommand("PartDesign_CompDatums")
{
sAppModule = "PartDesign";
sGroup = "PartDesign";
sMenuText = QT_TR_NOOP("Create Datum");
sToolTipText = QT_TR_NOOP("Creates a datum object or local coordinate system");
sWhatsThis = "PartDesign_CompDatums";
sStatusTip = sToolTipText;
eType = ForEdit;
setCheckable(false);
addCommand("PartDesign_Plane");
addCommand("PartDesign_Line");
addCommand("PartDesign_Point");
addCommand("PartDesign_CoordinateSystem");
}
const char* className() const override
{
return "CmdPartDesignCompDatums";
}
bool isActive() override
{
return (hasActiveDocument() && !Gui::Control().activeDialog());
}
};
// Command group for datums =============================================
class CmdPartDesignCompSketches: public Gui::GroupCommand
{
public:
CmdPartDesignCompSketches()
: GroupCommand("PartDesign_CompSketches")
{
sAppModule = "PartDesign";
sGroup = "PartDesign";
sMenuText = QT_TR_NOOP("Create Datum");
sToolTipText = QT_TR_NOOP("Creates a datum object or local coordinate system");
sWhatsThis = "PartDesign_CompDatums";
sStatusTip = sToolTipText;
eType = ForEdit;
setCheckable(false);
setRememberLast(false);
addCommand("PartDesign_NewSketch");
addCommand("Sketcher_MapSketch");
addCommand("Sketcher_EditSketch");
}
const char* className() const override
{
return "CmdPartDesignCompSketches";
}
bool isActive() override
{
return (hasActiveDocument() && !Gui::Control().activeDialog());
}
};
//===========================================================================
// Initialization
//===========================================================================
void CreatePartDesignCommands()
{
Gui::CommandManager& rcCmdMgr = Gui::Application::Instance->commandManager();
rcCmdMgr.addCommand(new CmdPartDesignShapeBinder());
rcCmdMgr.addCommand(new CmdPartDesignSubShapeBinder());
rcCmdMgr.addCommand(new CmdPartDesignClone());
rcCmdMgr.addCommand(new CmdPartDesignPlane());
rcCmdMgr.addCommand(new CmdPartDesignLine());
rcCmdMgr.addCommand(new CmdPartDesignPoint());
rcCmdMgr.addCommand(new CmdPartDesignCS());
rcCmdMgr.addCommand(new CmdPartDesignNewSketch());
rcCmdMgr.addCommand(new CmdPartDesignPad());
rcCmdMgr.addCommand(new CmdPartDesignPocket());
rcCmdMgr.addCommand(new CmdPartDesignHole());
rcCmdMgr.addCommand(new CmdPartDesignRevolution());
rcCmdMgr.addCommand(new CmdPartDesignGroove());
rcCmdMgr.addCommand(new CmdPartDesignAdditivePipe);
rcCmdMgr.addCommand(new CmdPartDesignSubtractivePipe);
rcCmdMgr.addCommand(new CmdPartDesignAdditiveLoft);
rcCmdMgr.addCommand(new CmdPartDesignSubtractiveLoft);
rcCmdMgr.addCommand(new CmdPartDesignAdditiveHelix);
rcCmdMgr.addCommand(new CmdPartDesignSubtractiveHelix);
rcCmdMgr.addCommand(new CmdPartDesignFillet());
rcCmdMgr.addCommand(new CmdPartDesignDraft());
rcCmdMgr.addCommand(new CmdPartDesignChamfer());
rcCmdMgr.addCommand(new CmdPartDesignThickness());
rcCmdMgr.addCommand(new CmdPartDesignMirrored());
rcCmdMgr.addCommand(new CmdPartDesignLinearPattern());
rcCmdMgr.addCommand(new CmdPartDesignPolarPattern());
// rcCmdMgr.addCommand(new CmdPartDesignScaled());
rcCmdMgr.addCommand(new CmdPartDesignMultiTransform());
rcCmdMgr.addCommand(new CmdPartDesignBoolean());
rcCmdMgr.addCommand(new CmdPartDesignCompDatums());
rcCmdMgr.addCommand(new CmdPartDesignCompSketches());
}
|