File size: 95,620 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 | /***************************************************************************
* Copyright (c) 2009 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 <Python.h>
#include <cstdlib>
#include <memory>
#include <BRepBndLib.hxx>
#include <BRepBuilderAPI_MakeVertex.hxx>
#include <BRepExtrema_DistShapeShape.hxx>
#include <BRep_Tool.hxx>
#include <Bnd_Box.hxx>
#include <SMDS_MeshGroup.hxx>
#include <SMESHDS_Group.hxx>
#include <SMESHDS_GroupBase.hxx>
#include <SMESHDS_Mesh.hxx>
#include <SMESH_Gen.hxx>
#include <SMESH_Group.hxx>
#include <SMESH_Mesh.hxx>
#include <SMESH_MeshEditor.hxx>
#include <ShapeAnalysis_ShapeTolerance.hxx>
#include <StdMeshers_Deflection1D.hxx>
#include <StdMeshers_LocalLength.hxx>
#include <StdMeshers_MaxElementArea.hxx>
#include <StdMeshers_MaxLength.hxx>
#include <StdMeshers_NumberOfSegments.hxx>
#include <StdMeshers_QuadranglePreference.hxx>
#include <StdMeshers_Quadrangle_2D.hxx>
#include <StdMeshers_Regular_1D.hxx>
#include <StdMeshers_StartEndLength.hxx>
#include <TopoDS_Face.hxx>
#include <TopoDS_Shape.hxx>
#include <TopoDS_Solid.hxx>
#include <TopoDS_Vertex.hxx>
#include <gp_Pnt.hxx>
#include <boost/assign/list_of.hpp>
#include <boost/tokenizer.hpp> //to simplify parsing input files we use the boost lib
#include <App/Application.h>
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/FileInfo.h>
#include <Base/Reader.h>
#include <Base/Stream.h>
#include <Base/TimeInfo.h>
#include <Base/Writer.h>
#include <Mod/Mesh/App/Core/Iterator.h>
#include "FemMesh.h"
#include <FemMeshPy.h>
#ifdef FC_USE_VTK
# include "FemVTKTools.h"
#endif
using namespace Fem;
using namespace Base;
using namespace boost;
SMESH_Gen* FemMesh::_mesh_gen = nullptr;
TYPESYSTEM_SOURCE(Fem::FemMesh, Base::Persistence)
FemMesh::FemMesh()
: myMesh(nullptr)
#if SMESH_VERSION_MAJOR < 9
, myStudyId(0)
#endif
{
#if SMESH_VERSION_MAJOR >= 9
myMesh = getGenerator()->CreateMesh(false);
#else
myMesh = getGenerator()->CreateMesh(myStudyId, false);
#endif
}
FemMesh::FemMesh(const FemMesh& mesh)
: myMesh(nullptr)
#if SMESH_VERSION_MAJOR < 9
, myStudyId(0)
#endif
{
#if SMESH_VERSION_MAJOR >= 9
myMesh = getGenerator()->CreateMesh(false);
#else
myMesh = getGenerator()->CreateMesh(myStudyId, false);
#endif
copyMeshData(mesh);
}
FemMesh::~FemMesh()
{
try {
TopoDS_Shape aNull;
myMesh->ShapeToMesh(aNull);
myMesh->Clear();
// myMesh->ClearLog();
delete myMesh;
}
catch (...) {
}
}
FemMesh& FemMesh::operator=(const FemMesh& mesh)
{
if (this != &mesh) {
#if SMESH_VERSION_MAJOR >= 9
myMesh = getGenerator()->CreateMesh(true);
#else
myMesh = getGenerator()->CreateMesh(myStudyId, true);
#endif
copyMeshData(mesh);
}
return *this;
}
void FemMesh::copyMeshData(const FemMesh& mesh)
{
_Mtrx = mesh._Mtrx;
// 1. Get source mesh
SMESHDS_Mesh* srcMeshDS = mesh.myMesh->GetMeshDS();
// 2. Get target mesh
SMESHDS_Mesh* newMeshDS = this->myMesh->GetMeshDS();
SMESH_MeshEditor editor(this->myMesh);
// 3. Get elements to copy
SMDS_ElemIteratorPtr srcElemIt;
SMDS_NodeIteratorPtr srcNodeIt;
srcElemIt = srcMeshDS->elementsIterator();
srcNodeIt = srcMeshDS->nodesIterator();
// 4. Copy elements
int iN;
const SMDS_MeshNode *nSrc, *nTgt;
std::vector<const SMDS_MeshNode*> nodes;
while (srcElemIt->more()) {
const SMDS_MeshElement* elem = srcElemIt->next();
// find / add nodes
nodes.resize(elem->NbNodes());
SMDS_ElemIteratorPtr nIt = elem->nodesIterator();
for (iN = 0; nIt->more(); ++iN) {
nSrc = static_cast<const SMDS_MeshNode*>(nIt->next());
nTgt = newMeshDS->FindNode(nSrc->GetID());
if (!nTgt) {
nTgt = newMeshDS->AddNodeWithID(nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
}
nodes[iN] = nTgt;
}
// add elements
if (elem->GetType() != SMDSAbs_Node) {
int ID = elem->GetID();
switch (elem->GetEntityType()) {
case SMDSEntity_Polyhedra:
#if SMESH_VERSION_MAJOR >= 9
editor.GetMeshDS()->AddPolyhedralVolumeWithID(
nodes,
static_cast<const SMDS_MeshVolume*>(elem)->GetQuantities(),
ID
);
#else
editor.GetMeshDS()->AddPolyhedralVolumeWithID(
nodes,
static_cast<const SMDS_VtkVolume*>(elem)->GetQuantities(),
ID
);
#endif
break;
case SMDSEntity_Ball: {
SMESH_MeshEditor::ElemFeatures elemFeat;
elemFeat.Init(static_cast<const SMDS_BallElement*>(elem)->GetDiameter());
elemFeat.SetID(ID);
editor.AddElement(nodes, elemFeat);
break;
}
default: {
SMESH_MeshEditor::ElemFeatures elemFeat(elem->GetType(), elem->IsPoly());
elemFeat.SetID(ID);
editor.AddElement(nodes, elemFeat);
break;
}
}
}
}
// 4(b). Copy free nodes
if (srcNodeIt && srcMeshDS->NbNodes() != newMeshDS->NbNodes()) {
while (srcNodeIt->more()) {
nSrc = srcNodeIt->next();
if (nSrc->NbInverseElements() == 0) {
nTgt = newMeshDS->AddNodeWithID(nSrc->X(), nSrc->Y(), nSrc->Z(), nSrc->GetID());
}
}
}
// 5. Copy groups
SMESH_Mesh::GroupIteratorPtr gIt = mesh.myMesh->GetGroups();
while (gIt->more()) {
SMESH_Group* group = gIt->next();
const SMESHDS_GroupBase* groupDS = group->GetGroupDS();
// Check group type. We copy nodal groups containing nodes of copied element
SMDSAbs_ElementType groupType = groupDS->GetType();
if (groupType != SMDSAbs_Node && newMeshDS->GetMeshInfo().NbElements(groupType) == 0) {
continue; // group type differs from types of meshPart
}
// Find copied elements in the group
std::vector<const SMDS_MeshElement*> groupElems;
SMDS_ElemIteratorPtr eIt = groupDS->GetElements();
const SMDS_MeshElement* foundElem;
if (groupType == SMDSAbs_Node) {
while (eIt->more()) {
if ((foundElem = newMeshDS->FindNode(eIt->next()->GetID()))) {
groupElems.push_back(foundElem);
}
}
}
else {
while (eIt->more()) {
if ((foundElem = newMeshDS->FindElement(eIt->next()->GetID()))) {
groupElems.push_back(foundElem);
}
}
}
// Make a new group
if (!groupElems.empty()) {
int aId = -1;
SMESH_Group* newGroupObj = this->myMesh->AddGroup(groupType, group->GetName(), aId);
SMESHDS_Group* newGroupDS = dynamic_cast<SMESHDS_Group*>(newGroupObj->GetGroupDS());
if (newGroupDS) {
SMDS_MeshGroup& smdsGroup = ((SMESHDS_Group*)newGroupDS)->SMDSGroup();
for (auto it : groupElems) {
smdsGroup.Add(it);
}
}
}
}
newMeshDS->Modified();
}
const SMESH_Mesh* FemMesh::getSMesh() const
{
return myMesh;
}
SMESH_Mesh* FemMesh::getSMesh()
{
return myMesh;
}
SMESH_Gen* FemMesh::getGenerator()
{
if (!FemMesh::_mesh_gen) {
FemMesh::_mesh_gen = new SMESH_Gen();
}
return FemMesh::_mesh_gen;
}
void FemMesh::addHypothesis(const TopoDS_Shape& aSubShape, SMESH_HypothesisPtr hyp)
{
myMesh->AddHypothesis(aSubShape, hyp->GetID());
SMESH_HypothesisPtr ptr(hyp);
hypoth.push_back(ptr);
}
void FemMesh::setStandardHypotheses()
{
TopoDS_Shape shape = getSMesh()->GetShapeToMesh();
if (shape.IsNull()) {
return;
}
int hyp = 0;
auto len = createHypothesis<StdMeshers_MaxLength>(hyp++);
static_cast<StdMeshers_MaxLength*>(len.get())->SetLength(1.0);
addHypothesis(shape, len);
auto loc = createHypothesis<StdMeshers_LocalLength>(hyp++);
static_cast<StdMeshers_LocalLength*>(loc.get())->SetLength(1.0);
addHypothesis(shape, loc);
auto area = createHypothesis<StdMeshers_MaxElementArea>(hyp++);
static_cast<StdMeshers_MaxElementArea*>(area.get())->SetMaxArea(1.0);
addHypothesis(shape, area);
auto segm = createHypothesis<StdMeshers_NumberOfSegments>(hyp++);
static_cast<StdMeshers_NumberOfSegments*>(segm.get())->SetNumberOfSegments(1);
addHypothesis(shape, segm);
auto defl = createHypothesis<StdMeshers_Deflection1D>(hyp++);
static_cast<StdMeshers_Deflection1D*>(defl.get())->SetDeflection(0.01);
addHypothesis(shape, defl);
auto reg = createHypothesis<StdMeshers_Regular_1D>(hyp++);
addHypothesis(shape, reg);
auto qdp = createHypothesis<StdMeshers_QuadranglePreference>(hyp++);
addHypothesis(shape, qdp);
auto q2d = createHypothesis<StdMeshers_Quadrangle_2D>(hyp++);
addHypothesis(shape, q2d);
}
void FemMesh::compute()
{
getGenerator()->Compute(*myMesh, myMesh->GetShapeToMesh());
}
std::set<long> FemMesh::getSurfaceNodes(long /*ElemId*/, short /*FaceId*/, float /*Angle*/) const
{
std::set<long> result;
return result;
}
/*! That function returns map containing volume ID and face ID.
*/
std::list<std::pair<int, int>> FemMesh::getVolumesByFace(const TopoDS_Face& face) const
{
std::list<std::pair<int, int>> result;
std::set<int> nodes_on_face = getNodesByFace(face);
// SMDS_MeshVolume::facesIterator() is broken with SMESH7 as it is impossible
// to iterate volume faces
// In SMESH9 this function has been removed
//
std::map<int, std::set<int>> face_nodes;
// get faces that contribute to 'nodes_on_face' with all of its nodes
SMDS_FaceIteratorPtr face_iter = myMesh->GetMeshDS()->facesIterator();
while (face_iter && face_iter->more()) {
const SMDS_MeshFace* face = face_iter->next();
SMDS_NodeIteratorPtr node_iter = face->nodeIterator();
// all nodes of the current face must be part of 'nodes_on_face'
std::set<int> node_ids;
while (node_iter && node_iter->more()) {
const SMDS_MeshNode* node = node_iter->next();
node_ids.insert(node->GetID());
}
std::vector<int> element_face_nodes;
std::set_intersection(
nodes_on_face.begin(),
nodes_on_face.end(),
node_ids.begin(),
node_ids.end(),
std::back_insert_iterator<std::vector<int>>(element_face_nodes)
);
if (element_face_nodes.size() == node_ids.size()) {
face_nodes[face->GetID()] = node_ids;
}
}
// get all nodes of a volume and check which faces contribute to it with all of its nodes
SMDS_VolumeIteratorPtr vol_iter = myMesh->GetMeshDS()->volumesIterator();
while (vol_iter->more()) {
const SMDS_MeshVolume* vol = vol_iter->next();
SMDS_NodeIteratorPtr node_iter = vol->nodeIterator();
std::set<int> node_ids;
while (node_iter && node_iter->more()) {
const SMDS_MeshNode* node = node_iter->next();
node_ids.insert(node->GetID());
}
for (const auto& it : face_nodes) {
std::vector<int> element_face_nodes;
std::set_intersection(
node_ids.begin(),
node_ids.end(),
it.second.begin(),
it.second.end(),
std::back_insert_iterator<std::vector<int>>(element_face_nodes)
);
// For curved faces it is possible that a volume contributes more than one face
if (element_face_nodes.size() == it.second.size()) {
result.emplace_back(vol->GetID(), it.first);
}
}
}
result.sort();
return result;
}
/*! That function returns a list of face IDs.
*/
std::list<int> FemMesh::getFacesByFace(const TopoDS_Face& face) const
{
// TODO: This function is broken with SMESH7 as it is impossible to iterate volume faces
std::list<int> result;
std::set<int> nodes_on_face = getNodesByFace(face);
SMDS_FaceIteratorPtr face_iter = myMesh->GetMeshDS()->facesIterator();
while (face_iter->more()) {
const SMDS_MeshFace* face = static_cast<const SMDS_MeshFace*>(face_iter->next());
int numNodes = face->NbNodes();
std::set<int> face_nodes;
for (int i = 0; i < numNodes; i++) {
face_nodes.insert(face->GetNode(i)->GetID());
}
std::vector<int> element_face_nodes;
std::set_intersection(
nodes_on_face.begin(),
nodes_on_face.end(),
face_nodes.begin(),
face_nodes.end(),
std::back_insert_iterator<std::vector<int>>(element_face_nodes)
);
// For curved faces it is possible that a volume contributes more than one face
if (element_face_nodes.size() == static_cast<std::size_t>(numNodes)) {
result.push_back(face->GetID());
}
}
result.sort();
return result;
}
std::list<int> FemMesh::getEdgesByEdge(const TopoDS_Edge& edge) const
{
std::list<int> result;
std::set<int> nodes_on_edge = getNodesByEdge(edge);
SMDS_EdgeIteratorPtr edge_iter = myMesh->GetMeshDS()->edgesIterator();
while (edge_iter->more()) {
const SMDS_MeshEdge* edge = static_cast<const SMDS_MeshEdge*>(edge_iter->next());
int numNodes = edge->NbNodes();
std::set<int> edge_nodes;
for (int i = 0; i < numNodes; i++) {
edge_nodes.insert(edge->GetNode(i)->GetID());
}
std::vector<int> element_edge_nodes;
std::set_intersection(
nodes_on_edge.begin(),
nodes_on_edge.end(),
edge_nodes.begin(),
edge_nodes.end(),
std::back_insert_iterator<std::vector<int>>(element_edge_nodes)
);
if (element_edge_nodes.size() == static_cast<std::size_t>(numNodes)) {
result.push_back(edge->GetID());
}
}
result.sort();
return result;
}
/*! That function returns map containing volume ID and face number
* as per CalculiX definition for tetrahedral elements. See CalculiX
* documentation for the details.
*/
std::map<int, int> FemMesh::getccxVolumesByFace(const TopoDS_Face& face) const
{
std::map<int, int> result;
std::set<int> nodes_on_face = getNodesByFace(face);
static std::map<int, std::vector<int>> elem_order;
if (elem_order.empty()) {
std::vector<int> c3d4 = boost::assign::list_of(1)(0)(2)(3);
std::vector<int> c3d10 = boost::assign::list_of(1)(0)(2)(3)(4)(6)(5)(8)(7)(9);
elem_order.insert(std::make_pair(c3d4.size(), c3d4));
elem_order.insert(std::make_pair(c3d10.size(), c3d10));
}
SMDS_VolumeIteratorPtr vol_iter = myMesh->GetMeshDS()->volumesIterator();
std::set<int> element_nodes;
int num_of_nodes;
while (vol_iter->more()) {
const SMDS_MeshVolume* vol = vol_iter->next();
num_of_nodes = vol->NbNodes();
std::pair<int, std::vector<int>> apair;
apair.first = vol->GetID();
std::map<int, std::vector<int>>::iterator it = elem_order.find(num_of_nodes);
if (it != elem_order.end()) {
const std::vector<int>& order = it->second;
for (int jt : order) {
int vid = vol->GetNode(jt)->GetID();
apair.second.push_back(vid);
}
}
// Get volume nodes on face
std::vector<int> element_face_nodes;
std::set<int> element_nodes;
element_nodes.insert(apair.second.begin(), apair.second.end());
std::set_intersection(
nodes_on_face.begin(),
nodes_on_face.end(),
element_nodes.begin(),
element_nodes.end(),
std::back_insert_iterator<std::vector<int>>(element_face_nodes)
);
if ((element_face_nodes.size() == 3 && num_of_nodes == 4)
|| (element_face_nodes.size() == 6 && num_of_nodes == 10)) {
int missing_node = 0;
for (int i = 0; i < 4; i++) {
// search for the ID of the volume which is not part of 'element_face_nodes'
if (std::ranges::find(element_face_nodes, apair.second[i])
== element_face_nodes.end()) {
missing_node = i + 1;
break;
}
}
/* for tetrahedral elements as per CalculiX definition:
Face 1: 1-2-3, missing point 4 means it's face P1
Face 2: 1-4-2, missing point 3 means it's face P2
Face 3: 2-4-3, missing point 1 means it's face P3
Face 4: 3-4-1, missing point 2 means it's face P4 */
int face_ccx = 0;
switch (missing_node) {
case 1:
face_ccx = 3;
break;
case 2:
face_ccx = 4;
break;
case 3:
face_ccx = 2;
break;
case 4:
face_ccx = 1;
break;
default:
assert(false); // should never happen
break;
}
result[apair.first] = face_ccx;
}
}
return result;
}
std::set<int> FemMesh::getNodesBySolid(const TopoDS_Solid& solid) const
{
std::set<int> result;
Bnd_Box box;
BRepBndLib::Add(solid, box);
// limit where the mesh node belongs to the solid
TopAbs_ShapeEnum shapetype = TopAbs_SHAPE;
ShapeAnalysis_ShapeTolerance analysis;
double limit = analysis.Tolerance(solid, 1, shapetype);
Base::Console().log("The limit if a node is in or out: %.12lf in scientific: %.4e \n", limit, limit);
// get the current transform of the FemMesh
const Base::Matrix4D Mtrx(getTransform());
std::vector<const SMDS_MeshNode*> nodes;
SMDS_NodeIteratorPtr aNodeIter = myMesh->GetMeshDS()->nodesIterator();
while (aNodeIter->more()) {
const SMDS_MeshNode* aNode = aNodeIter->next();
nodes.push_back(aNode);
}
#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < nodes.size(); ++i) {
const SMDS_MeshNode* aNode = nodes[i];
double xyz[3];
aNode->GetXYZ(xyz);
Base::Vector3d vec(xyz[0], xyz[1], xyz[2]);
// Apply the matrix to hold the BoundBox in absolute space.
vec = Mtrx * vec;
if (!box.IsOut(gp_Pnt(vec.x, vec.y, vec.z))) {
// create a vertex
BRepBuilderAPI_MakeVertex aBuilder(gp_Pnt(vec.x, vec.y, vec.z));
TopoDS_Shape s = aBuilder.Vertex();
// measure distance
BRepExtrema_DistShapeShape measure(solid, s);
measure.Perform();
if (!measure.IsDone() || measure.NbSolution() < 1) {
continue;
}
if (measure.Value() < limit)
#pragma omp critical
{
result.insert(aNode->GetID());
}
}
}
return result;
}
std::set<int> FemMesh::getNodesByFace(const TopoDS_Face& face) const
{
std::set<int> result;
Bnd_Box box;
BRepBndLib::Add(
face,
box,
Standard_False
); // https://forum.freecad.org/viewtopic.php?f=18&t=21571&start=70#p221591
// limit where the mesh node belongs to the face:
double limit = BRep_Tool::Tolerance(face);
box.Enlarge(limit);
// get the current transform of the FemMesh
const Base::Matrix4D Mtrx(getTransform());
std::vector<const SMDS_MeshNode*> nodes;
SMDS_NodeIteratorPtr aNodeIter = myMesh->GetMeshDS()->nodesIterator();
while (aNodeIter->more()) {
const SMDS_MeshNode* aNode = aNodeIter->next();
nodes.push_back(aNode);
}
#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < nodes.size(); ++i) {
const SMDS_MeshNode* aNode = nodes[i];
double xyz[3];
aNode->GetXYZ(xyz);
Base::Vector3d vec(xyz[0], xyz[1], xyz[2]);
// Apply the matrix to hold the BoundBox in absolute space.
vec = Mtrx * vec;
if (!box.IsOut(gp_Pnt(vec.x, vec.y, vec.z))) {
// create a vertex
BRepBuilderAPI_MakeVertex aBuilder(gp_Pnt(vec.x, vec.y, vec.z));
TopoDS_Shape s = aBuilder.Vertex();
// measure distance
BRepExtrema_DistShapeShape measure(face, s);
measure.Perform();
if (!measure.IsDone() || measure.NbSolution() < 1) {
continue;
}
if (measure.Value() < limit)
#pragma omp critical
{
result.insert(aNode->GetID());
}
}
}
return result;
}
std::set<int> FemMesh::getNodesByEdge(const TopoDS_Edge& edge) const
{
std::set<int> result;
Bnd_Box box;
BRepBndLib::Add(edge, box);
// limit where the mesh node belongs to the edge:
double limit = BRep_Tool::Tolerance(edge);
box.Enlarge(limit);
// get the current transform of the FemMesh
const Base::Matrix4D Mtrx(getTransform());
std::vector<const SMDS_MeshNode*> nodes;
SMDS_NodeIteratorPtr aNodeIter = myMesh->GetMeshDS()->nodesIterator();
while (aNodeIter->more()) {
const SMDS_MeshNode* aNode = aNodeIter->next();
nodes.push_back(aNode);
}
#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < nodes.size(); ++i) {
const SMDS_MeshNode* aNode = nodes[i];
double xyz[3];
aNode->GetXYZ(xyz);
Base::Vector3d vec(xyz[0], xyz[1], xyz[2]);
// Apply the matrix to hold the BoundBox in absolute space.
vec = Mtrx * vec;
if (!box.IsOut(gp_Pnt(vec.x, vec.y, vec.z))) {
// create a vertex
BRepBuilderAPI_MakeVertex aBuilder(gp_Pnt(vec.x, vec.y, vec.z));
TopoDS_Shape s = aBuilder.Vertex();
// measure distance
BRepExtrema_DistShapeShape measure(edge, s);
measure.Perform();
if (!measure.IsDone() || measure.NbSolution() < 1) {
continue;
}
if (measure.Value() < limit)
#pragma omp critical
{
result.insert(aNode->GetID());
}
}
}
return result;
}
std::set<int> FemMesh::getNodesByVertex(const TopoDS_Vertex& vertex) const
{
std::set<int> result;
double limit = BRep_Tool::Tolerance(vertex);
limit *= limit; // use square to improve speed
gp_Pnt pnt = BRep_Tool::Pnt(vertex);
Base::Vector3d node(pnt.X(), pnt.Y(), pnt.Z());
// get the current transform of the FemMesh
const Base::Matrix4D Mtrx(getTransform());
std::vector<const SMDS_MeshNode*> nodes;
SMDS_NodeIteratorPtr aNodeIter = myMesh->GetMeshDS()->nodesIterator();
while (aNodeIter->more()) {
const SMDS_MeshNode* aNode = aNodeIter->next();
nodes.push_back(aNode);
}
#pragma omp parallel for schedule(dynamic)
for (size_t i = 0; i < nodes.size(); ++i) {
const SMDS_MeshNode* aNode = nodes[i];
double xyz[3];
aNode->GetXYZ(xyz);
Base::Vector3d vec(xyz[0], xyz[1], xyz[2]);
vec = Mtrx * vec;
if (Base::DistanceP2(node, vec) <= limit)
#pragma omp critical
{
result.insert(aNode->GetID());
}
}
return result;
}
std::list<int> FemMesh::getElementNodes(int id) const
{
std::list<int> result;
const SMDS_MeshElement* elem = myMesh->GetMeshDS()->FindElement(id);
if (elem) {
for (int i = 0; i < elem->NbNodes(); i++) {
result.push_back(elem->GetNode(i)->GetID());
}
}
return result;
}
std::list<int> FemMesh::getNodeElements(int id, SMDSAbs_ElementType type) const
{
std::list<int> result;
const SMDS_MeshNode* node = myMesh->GetMeshDS()->FindNode(id);
if (node) {
SMDS_ElemIteratorPtr it = node->GetInverseElementIterator(type);
while (it->more()) {
const SMDS_MeshElement* elem = it->next();
result.push_back(elem->GetID());
}
}
return result;
}
std::set<int> FemMesh::getEdgesOnly() const
{
std::set<int> resultIDs;
// edges
SMDS_EdgeIteratorPtr aEdgeIter = myMesh->GetMeshDS()->edgesIterator();
while (aEdgeIter->more()) {
const SMDS_MeshEdge* aEdge = aEdgeIter->next();
std::list<int> enodes = getElementNodes(aEdge->GetID());
std::set<int> aEdgeNodes(enodes.begin(), enodes.end()); // convert list to set
bool edgeBelongsToAFace = false;
// faces
SMDS_FaceIteratorPtr aFaceIter = myMesh->GetMeshDS()->facesIterator();
while (aFaceIter->more()) {
const SMDS_MeshFace* aFace = aFaceIter->next();
std::list<int> fnodes = getElementNodes(aFace->GetID());
std::set<int> aFaceNodes(fnodes.begin(), fnodes.end()); // convert list to set
// if aEdgeNodes is not a subset of any aFaceNodes --> aEdge does not belong to any Face
std::vector<int> inodes;
std::set_intersection(
aFaceNodes.begin(),
aFaceNodes.end(),
aEdgeNodes.begin(),
aEdgeNodes.end(),
std::back_inserter(inodes)
);
std::set<int> intersection_nodes(
inodes.begin(),
inodes.end()
); // convert vector to set
if (aEdgeNodes == intersection_nodes) {
edgeBelongsToAFace = true;
break;
}
}
if (!edgeBelongsToAFace) {
resultIDs.insert(aEdge->GetID());
}
}
return resultIDs;
}
std::set<int> FemMesh::getFacesOnly() const
{
// How it works ATM:
// for each face
// get the face nodes
// for each volume
// get the volume nodes
// if the face nodes are a subset of the volume nodes
// add the face to the volume faces and break
// if face doesn't belong to a volume
// add it to faces only
//
// This means it is iterated over a lot of volumes many times, this is quite expensive!
//
// TODO make this faster
// Idea:
// for each volume
// get the faces and add them to the volume faces
// for each face
// if not in volume faces
// add it to the faces only
//
// but the volume faces do not seem to know their global mesh ID, I could not find any method in
// SMESH
std::set<int> resultIDs;
// faces
SMDS_FaceIteratorPtr aFaceIter = myMesh->GetMeshDS()->facesIterator();
while (aFaceIter->more()) {
const SMDS_MeshFace* aFace = aFaceIter->next();
std::list<int> fnodes = getElementNodes(aFace->GetID());
std::set<int> aFaceNodes(fnodes.begin(), fnodes.end()); // convert list to set
bool faceBelongsToAVolume = false;
// volumes
SMDS_VolumeIteratorPtr aVolIter = myMesh->GetMeshDS()->volumesIterator();
while (aVolIter->more()) {
const SMDS_MeshVolume* aVol = aVolIter->next();
std::list<int> vnodes = getElementNodes(aVol->GetID());
std::set<int> aVolNodes(vnodes.begin(), vnodes.end()); // convert list to set
// if aFaceNodes is not a subset of any aVolNodes --> aFace does not belong to any
// Volume
std::vector<int> inodes;
std::set_intersection(
aVolNodes.begin(),
aVolNodes.end(),
aFaceNodes.begin(),
aFaceNodes.end(),
std::back_inserter(inodes)
);
std::set<int> intersection_nodes(
inodes.begin(),
inodes.end()
); // convert vector to set
if (aFaceNodes == intersection_nodes) {
faceBelongsToAVolume = true;
break;
}
}
if (!faceBelongsToAVolume) {
resultIDs.insert(aFace->GetID());
}
}
return resultIDs;
}
namespace
{
class NastranElement
{
public:
virtual ~NastranElement() = default;
bool isValid() const
{
return element_id >= 0;
}
virtual void read(const std::string& str1, const std::string& str2) = 0;
virtual void addToMesh(SMESHDS_Mesh* meshds) = 0;
protected:
int element_id = -1;
std::vector<int> elements;
};
using NastranElementPtr = std::shared_ptr<NastranElement>;
class GRIDElement: public NastranElement
{
void addToMesh(SMESHDS_Mesh* meshds) override
{
meshds->AddNodeWithID(node.x, node.y, node.z, element_id);
}
protected:
Base::Vector3d node;
};
class GRIDFreeFieldElement: public GRIDElement
{
void read(const std::string& str, const std::string&) override
{
char_separator<char> sep(",");
tokenizer<char_separator<char>> tokens(str, sep);
std::vector<std::string> token_results;
token_results.assign(tokens.begin(), tokens.end());
if (token_results.size() < 6) {
return; // Line does not include Nodal coordinates
}
element_id = atoi(token_results[1].c_str());
node.x = atof(token_results[3].c_str());
node.y = atof(token_results[4].c_str());
node.z = atof(token_results[5].c_str());
}
};
class GRIDLongFieldElement: public GRIDElement
{
void read(const std::string& str1, const std::string& str2) override
{
element_id = atoi(str1.substr(8, 24).c_str());
node.x = atof(str1.substr(40, 56).c_str());
node.y = atof(str1.substr(56, 72).c_str());
node.z = atof(str2.substr(8, 24).c_str());
}
};
class GRIDSmallFieldElement: public GRIDElement
{
void read(const std::string&, const std::string&) override
{}
};
class CTRIA3Element: public NastranElement
{
public:
void addToMesh(SMESHDS_Mesh* meshds) override
{
const SMDS_MeshNode* n0 = meshds->FindNode(elements[0]);
const SMDS_MeshNode* n1 = meshds->FindNode(elements[1]);
const SMDS_MeshNode* n2 = meshds->FindNode(elements[2]);
if (n0 && n1 && n2) {
meshds->AddFaceWithID(n0, n1, n2, element_id);
}
else {
Base::Console().warning(
"NASTRAN: Failed to add face %d from nodes: (%d, %d, %d,)\n",
element_id,
elements[0],
elements[1],
elements[2]
);
}
}
};
class CTRIA3FreeFieldElement: public CTRIA3Element
{
public:
void read(const std::string& str, const std::string&) override
{
char_separator<char> sep(",");
tokenizer<char_separator<char>> tokens(str, sep);
std::vector<std::string> token_results;
token_results.assign(tokens.begin(), tokens.end());
if (token_results.size() < 6) {
return; // Line does not include enough nodal IDs
}
element_id = atoi(token_results[1].c_str());
elements.push_back(atoi(token_results[3].c_str()));
elements.push_back(atoi(token_results[4].c_str()));
elements.push_back(atoi(token_results[5].c_str()));
}
};
class CTRIA3LongFieldElement: public CTRIA3Element
{
public:
void read(const std::string& str, const std::string&) override
{
element_id = atoi(str.substr(8, 16).c_str());
elements.push_back(atoi(str.substr(24, 32).c_str()));
elements.push_back(atoi(str.substr(32, 40).c_str()));
elements.push_back(atoi(str.substr(40, 48).c_str()));
}
};
class CTRIA3SmallFieldElement: public CTRIA3Element
{
public:
void read(const std::string&, const std::string&) override
{}
};
class CTETRAElement: public NastranElement
{
public:
void addToMesh(SMESHDS_Mesh* meshds) override
{
const SMDS_MeshNode* n0 = meshds->FindNode(elements[1]);
const SMDS_MeshNode* n1 = meshds->FindNode(elements[0]);
const SMDS_MeshNode* n2 = meshds->FindNode(elements[2]);
const SMDS_MeshNode* n3 = meshds->FindNode(elements[3]);
const SMDS_MeshNode* n4 = meshds->FindNode(elements[4]);
const SMDS_MeshNode* n5 = meshds->FindNode(elements[6]);
const SMDS_MeshNode* n6 = meshds->FindNode(elements[5]);
const SMDS_MeshNode* n7 = meshds->FindNode(elements[8]);
const SMDS_MeshNode* n8 = meshds->FindNode(elements[7]);
const SMDS_MeshNode* n9 = meshds->FindNode(elements[9]);
if (n0 && n1 && n2 && n3 && n4 && n5 && n6 && n7 && n8 && n9) {
meshds->AddVolumeWithID(n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, element_id);
}
else {
Base::Console().warning(
"NASTRAN: Failed to add volume %d from nodes: (%d, %d, %d, %d, "
"%d, %d, %d, %d, %d, %d)\n",
element_id,
elements[1],
elements[0],
elements[2],
elements[3],
elements[4],
elements[6],
elements[5],
elements[8],
elements[7],
elements[9]
);
}
}
};
class CTETRAFreeFieldElement: public CTETRAElement
{
public:
void read(const std::string& str, const std::string&) override
{
char_separator<char> sep(",");
tokenizer<char_separator<char>> tokens(str, sep);
std::vector<std::string> token_results;
token_results.assign(tokens.begin(), tokens.end());
if (token_results.size() < 14) {
return; // Line does not include enough nodal IDs
}
element_id = atoi(token_results[1].c_str());
elements.push_back(atoi(token_results[3].c_str()));
elements.push_back(atoi(token_results[4].c_str()));
elements.push_back(atoi(token_results[5].c_str()));
elements.push_back(atoi(token_results[6].c_str()));
elements.push_back(atoi(token_results[7].c_str()));
elements.push_back(atoi(token_results[8].c_str()));
elements.push_back(atoi(token_results[10].c_str()));
elements.push_back(atoi(token_results[11].c_str()));
elements.push_back(atoi(token_results[12].c_str()));
elements.push_back(atoi(token_results[13].c_str()));
}
};
class CTETRALongFieldElement: public CTETRAElement
{
public:
void read(const std::string& str1, const std::string& str2) override
{
int id = atoi(str1.substr(8, 16).c_str());
int offset = 0;
if (id < 1000000) {
offset = 0;
}
else if (id < 10000000) {
offset = 1;
}
else if (id < 100000000) {
offset = 2;
}
element_id = id;
elements.push_back(atoi(str1.substr(24, 32).c_str()));
elements.push_back(atoi(str1.substr(32, 40).c_str()));
elements.push_back(atoi(str1.substr(40, 48).c_str()));
elements.push_back(atoi(str1.substr(48, 56).c_str()));
elements.push_back(atoi(str1.substr(56, 64).c_str()));
elements.push_back(atoi(str1.substr(64, 72).c_str()));
elements.push_back(atoi(str2.substr(8 + offset, 16 + offset).c_str()));
elements.push_back(atoi(str2.substr(16 + offset, 24 + offset).c_str()));
elements.push_back(atoi(str2.substr(24 + offset, 32 + offset).c_str()));
elements.push_back(atoi(str2.substr(32 + offset, 40 + offset).c_str()));
}
};
class CTETRASmallFieldElement: public CTETRAElement
{
public:
void read(const std::string&, const std::string&) override
{}
};
// NASTRAN-95
class GRIDNastran95Element: public GRIDElement
{
void read(const std::string& str, const std::string&) override
{
element_id = atoi(str.substr(8, 16).c_str());
node.x = atof(str.substr(24, 32).c_str());
node.y = atof(str.substr(32, 40).c_str());
node.z = atof(str.substr(40, 48).c_str());
}
};
class CBARElement: public NastranElement
{
void read(const std::string& str, const std::string&) override
{
element_id = atoi(str.substr(8, 16).c_str());
elements.push_back(atoi(str.substr(24, 32).c_str()));
elements.push_back(atoi(str.substr(32, 40).c_str()));
}
void addToMesh(SMESHDS_Mesh* meshds) override
{
meshds->AddEdgeWithID(elements[0], elements[1], element_id);
}
};
class CTRMEMElement: public NastranElement
{
void read(const std::string& str, const std::string&) override
{
element_id = atoi(str.substr(8, 16).c_str());
elements.push_back(atoi(str.substr(24, 32).c_str()));
elements.push_back(atoi(str.substr(32, 40).c_str()));
elements.push_back(atoi(str.substr(40, 48).c_str()));
}
void addToMesh(SMESHDS_Mesh* meshds) override
{
meshds->AddFaceWithID(elements[0], elements[1], elements[2], element_id);
}
};
class CTRIA1Element: public NastranElement
{
void read(const std::string& str, const std::string&) override
{
element_id = atoi(str.substr(8, 16).c_str());
elements.push_back(atoi(str.substr(24, 32).c_str()));
elements.push_back(atoi(str.substr(32, 40).c_str()));
elements.push_back(atoi(str.substr(40, 48).c_str()));
}
void addToMesh(SMESHDS_Mesh* meshds) override
{
meshds->AddFaceWithID(elements[0], elements[1], elements[2], element_id);
}
};
class CQUAD1Element: public NastranElement
{
void read(const std::string& str, const std::string&) override
{
element_id = atoi(str.substr(8, 16).c_str());
elements.push_back(atoi(str.substr(24, 32).c_str()));
elements.push_back(atoi(str.substr(32, 40).c_str()));
elements.push_back(atoi(str.substr(40, 48).c_str()));
elements.push_back(atoi(str.substr(48, 56).c_str()));
}
void addToMesh(SMESHDS_Mesh* meshds) override
{
meshds->AddFaceWithID(elements[0], elements[1], elements[2], elements[3], element_id);
}
};
class CTETRANastran95Element: public NastranElement
{
void read(const std::string& str, const std::string&) override
{
element_id = atoi(str.substr(8, 16).c_str());
elements.push_back(atoi(str.substr(24, 32).c_str()));
elements.push_back(atoi(str.substr(32, 40).c_str()));
elements.push_back(atoi(str.substr(40, 48).c_str()));
elements.push_back(atoi(str.substr(48, 56).c_str()));
}
void addToMesh(SMESHDS_Mesh* meshds) override
{
meshds->AddFaceWithID(elements[0], elements[1], elements[2], elements[3], element_id);
}
};
class CWEDGEElement: public NastranElement
{
void read(const std::string& str, const std::string&) override
{
element_id = atoi(str.substr(8, 16).c_str());
elements.push_back(atoi(str.substr(24, 32).c_str()));
elements.push_back(atoi(str.substr(32, 40).c_str()));
elements.push_back(atoi(str.substr(40, 48).c_str()));
elements.push_back(atoi(str.substr(48, 56).c_str()));
elements.push_back(atoi(str.substr(56, 64).c_str()));
elements.push_back(atoi(str.substr(64, 72).c_str()));
}
void addToMesh(SMESHDS_Mesh* meshds) override
{
meshds->AddVolumeWithID(
elements[0],
elements[1],
elements[2],
elements[3],
elements[4],
elements[5],
element_id
);
}
};
class CHEXA1Element: public NastranElement
{
void read(const std::string& str1, const std::string& str2) override
{
element_id = atoi(str1.substr(8, 16).c_str());
elements.push_back(atoi(str1.substr(24, 32).c_str()));
elements.push_back(atoi(str1.substr(32, 40).c_str()));
elements.push_back(atoi(str1.substr(40, 48).c_str()));
elements.push_back(atoi(str1.substr(48, 56).c_str()));
elements.push_back(atoi(str1.substr(56, 64).c_str()));
elements.push_back(atoi(str1.substr(64, 72).c_str()));
elements.push_back(atoi(str2.substr(8, 16).c_str()));
elements.push_back(atoi(str2.substr(16, 24).c_str()));
}
void addToMesh(SMESHDS_Mesh* meshds) override
{
meshds->AddVolumeWithID(
elements[0],
elements[1],
elements[2],
elements[3],
elements[4],
elements[5],
elements[6],
elements[7],
element_id
);
}
};
class CHEXA2Element: public NastranElement
{
void read(const std::string& str1, const std::string& str2) override
{
element_id = atoi(str1.substr(8, 16).c_str());
elements.push_back(atoi(str1.substr(24, 32).c_str()));
elements.push_back(atoi(str1.substr(32, 40).c_str()));
elements.push_back(atoi(str1.substr(40, 48).c_str()));
elements.push_back(atoi(str1.substr(48, 56).c_str()));
elements.push_back(atoi(str1.substr(56, 64).c_str()));
elements.push_back(atoi(str1.substr(64, 72).c_str()));
elements.push_back(atoi(str2.substr(8, 16).c_str()));
elements.push_back(atoi(str2.substr(16, 24).c_str()));
}
void addToMesh(SMESHDS_Mesh* meshds) override
{
meshds->AddVolumeWithID(
elements[0],
elements[1],
elements[2],
elements[3],
elements[4],
elements[5],
elements[6],
elements[7],
element_id
);
}
};
} // namespace
void FemMesh::readNastran(const std::string& Filename)
{
Base::TimeElapsed Start;
Base::Console().log("Start: FemMesh::readNastran() =================================\n");
_Mtrx = Base::Matrix4D();
Base::FileInfo fi(Filename);
Base::ifstream inputfile;
inputfile.open(fi);
inputfile.seekg(std::ifstream::beg);
std::string line1, line2;
std::vector<NastranElementPtr> mesh_elements;
enum Format
{
FreeField,
SmallField,
LongField
};
Format nastranFormat = Format::LongField;
do {
std::getline(inputfile, line1);
if (line1.empty()) {
continue;
}
if (line1.find(',') != std::string::npos) {
nastranFormat = Format::FreeField;
}
NastranElementPtr ptr;
if (line1.find("GRID*") != std::string::npos) { // We found a Grid line
// Now lets extract the GRID Points = Nodes
// As each GRID Line consists of two subsequent lines we have to
// take care of that as well
if (nastranFormat == Format::LongField) {
std::getline(inputfile, line2);
ptr = std::make_shared<GRIDLongFieldElement>();
ptr->read(line1, line2);
}
}
else if (line1.find("GRID") != std::string::npos) { // We found a Grid line
if (nastranFormat == Format::FreeField) {
ptr = std::make_shared<GRIDFreeFieldElement>();
ptr->read(line1, "");
}
}
else if (line1.find("CTRIA3") != std::string::npos) {
if (nastranFormat == Format::FreeField) {
ptr = std::make_shared<CTRIA3FreeFieldElement>();
ptr->read(line1, "");
}
else {
ptr = std::make_shared<CTRIA3LongFieldElement>();
ptr->read(line1, "");
}
}
else if (line1.find("CTETRA") != std::string::npos) {
// Lets extract the elements
// As each Element Line consists of two subsequent lines as well
// we have to take care of that
// At a first step we only extract Quadratic Tetrahedral Elements
std::getline(inputfile, line2);
if (nastranFormat == Format::FreeField) {
ptr = std::make_shared<CTETRAFreeFieldElement>();
ptr->read(line1.append(line2), "");
}
else {
ptr = std::make_shared<CTETRALongFieldElement>();
ptr->read(line1, line2);
}
}
if (ptr && ptr->isValid()) {
mesh_elements.push_back(ptr);
}
} while (inputfile.good());
inputfile.close();
Base::Console().log(
" %f: File read, start building mesh\n",
Base::TimeElapsed::diffTimeF(Start, Base::TimeElapsed())
);
// Now fill the SMESH datastructure
SMESHDS_Mesh* meshds = this->myMesh->GetMeshDS();
meshds->ClearMesh();
for (auto it : mesh_elements) {
it->addToMesh(meshds);
}
Base::Console().log(" %f: Done \n", Base::TimeElapsed::diffTimeF(Start, Base::TimeElapsed()));
}
void FemMesh::readNastran95(const std::string& Filename)
{
Base::TimeElapsed Start;
Base::Console().log("Start: FemMesh::readNastran95() =================================\n");
_Mtrx = Base::Matrix4D();
Base::FileInfo fi(Filename);
Base::ifstream inputfile;
inputfile.open(fi);
inputfile.seekg(std::ifstream::beg);
std::string line1, line2, tcard;
std::vector<NastranElementPtr> mesh_nodes;
std::vector<NastranElementPtr> mesh_elements;
do {
NastranElementPtr node;
NastranElementPtr elem;
std::getline(inputfile, line1);
// cout << line1 << endl;
if (line1.empty()) {
continue;
}
tcard = line1.substr(0, 8).c_str();
// boost::algorithm::trim(tcard);
if (line1.find("GRID*") != std::string::npos) // We found a Grid line
{
// Now lets extract the GRID Points = Nodes
// As each GRID Line consists of two subsequent lines we have to
// take care of that as well
std::getline(inputfile, line2);
node = std::make_shared<GRIDLongFieldElement>();
node->read(line1, line2);
}
else if (line1.find("GRID") != std::string::npos) // We found a Grid line
{
// Base::Console().log("Found a GRID\n");
// D06.inp
// GRID 109 .9 .7
// Now lets extract the GRID Points = Nodes
// Get the Nodal ID
node = std::make_shared<GRIDNastran95Element>();
node->read(line1, "");
}
// 1D
else if (line1.substr(0, 6) == "CBAR") {
elem = std::make_shared<CBARElement>();
elem->read(line1, "");
}
// 2d
else if (line1.substr(0, 6) == "CTRMEM") {
// D06
// CTRMEM 322 1 179 180 185
elem = std::make_shared<CTRMEMElement>();
elem->read(line1, "");
}
else if (line1.substr(0, 6) == "CTRIA1") {
// D06
// CTRMEM 322 1 179 180 185
elem = std::make_shared<CTRIA1Element>();
elem->read(line1, "");
}
else if (line1.substr(0, 6) == "CQUAD1") {
// D06
// CTRMEM 322 1 179 180 185
elem = std::make_shared<CQUAD1Element>();
elem->read(line1, "");
}
// 3d element
else if (line1.find("CTETRA") != std::string::npos) {
// d011121a.inp
// CTETRA 3 200 104 114 3 103
elem = std::make_shared<CTETRANastran95Element>();
elem->read(line1, "");
}
else if (line1.find("CWEDGE") != std::string::npos) {
// d011121a.inp
// CWEDGE 11 200 6 17 16 106 117 116
elem = std::make_shared<CTETRANastran95Element>();
elem->read(line1, "");
}
else if (line1.find("CHEXA1") != std::string::npos) {
// d011121a.inp
// CHEXA1 1 200 1 2 13 12 101 102 +SOL1
//+SOL1 113 112
std::getline(inputfile, line2);
elem = std::make_shared<CHEXA1Element>();
elem->read(line1, line2);
}
else if (line1.find("CHEXA2") != std::string::npos) {
// d011121a.inp
// CHEXA1 1 200 1 2 13 12 101 102 +SOL1
//+SOL1 113 112
std::getline(inputfile, line2);
elem = std::make_shared<CHEXA2Element>();
elem->read(line1, line2);
}
if (node && node->isValid()) {
mesh_nodes.push_back(node);
}
if (elem && elem->isValid()) {
mesh_elements.push_back(elem);
}
} while (inputfile.good());
inputfile.close();
Base::Console().log(
" %f: File read, start building mesh\n",
Base::TimeElapsed::diffTimeF(Start, Base::TimeElapsed())
);
// Now fill the SMESH datastructure
SMESHDS_Mesh* meshds = this->myMesh->GetMeshDS();
meshds->ClearMesh();
for (auto it : mesh_nodes) {
it->addToMesh(meshds);
}
for (auto it : mesh_elements) {
it->addToMesh(meshds);
}
Base::Console().log(" %f: Done \n", Base::TimeElapsed::diffTimeF(Start, Base::TimeElapsed()));
}
void FemMesh::readAbaqus(const std::string& FileName)
{
Base::TimeElapsed Start;
Base::Console().log("Start: FemMesh::readAbaqus() =================================\n");
/*
Python command to read Abaqus inp mesh file from test suite:
from feminout.importInpMesh import read as read_inp
femmesh = read_inp(FreeCAD.ConfigGet("AppHomePath") +
'Mod/Fem/femtest/data/mesh/tetra10_mesh.inp')
*/
Base::PyGILStateLocker lock;
PyObject* module = PyImport_ImportModule("feminout.importInpMesh");
if (!module) {
return;
}
try {
Py::Module abaqusmod(module, true);
Py::Callable method(abaqusmod.getAttr("read"));
Py::Tuple args(1);
args.setItem(0, Py::String(FileName));
Py::Object mesh(method.apply(args));
if (PyObject_TypeCheck(mesh.ptr(), &FemMeshPy::Type)) {
FemMeshPy* fempy = static_cast<FemMeshPy*>(mesh.ptr());
FemMesh* fem = fempy->getFemMeshPtr();
*this = *fem; // the deep copy should be avoided, a pointer swap method could be
// implemented see
// https://forum.freecad.org/viewtopic.php?f=10&t=31999&start=10#p274241
}
else {
throw Base::FileException("Problems reading file");
}
}
catch (Py::Exception& e) {
e.clear();
}
Base::Console().log(" %f: Done \n", Base::TimeElapsed::diffTimeF(Start, Base::TimeElapsed()));
}
void FemMesh::readZ88(const std::string& FileName)
{
Base::TimeElapsed Start;
Base::Console().log("Start: FemMesh::readZ88() =================================\n");
/*
Python command to read Z88 mesh file from test suite:
from feminout.importZ88Mesh import read as read_z88
femmesh = read_z88(FreeCAD.ConfigGet("AppHomePath") +
'Mod/Fem/femtest/data/mesh/tetra10_mesh.z88')
*/
Base::PyGILStateLocker lock;
PyObject* module = PyImport_ImportModule("feminout.importZ88Mesh");
if (!module) {
return;
}
try {
Py::Module z88mod(module, true);
Py::Callable method(z88mod.getAttr("read"));
Py::Tuple args(1);
args.setItem(0, Py::String(FileName));
Py::Object mesh(method.apply(args));
if (PyObject_TypeCheck(mesh.ptr(), &FemMeshPy::Type)) {
FemMeshPy* fempy = static_cast<FemMeshPy*>(mesh.ptr());
FemMesh* fem = fempy->getFemMeshPtr();
*this = *fem; // the deep copy should be avoided, a pointer swap method could be
// implemented see
// https://forum.freecad.org/viewtopic.php?f=10&t=31999&start=10#p274241
}
else {
throw Base::FileException("Problems reading file");
}
}
catch (Py::Exception& e) {
e.clear();
}
Base::Console().log(" %f: Done \n", Base::TimeElapsed::diffTimeF(Start, Base::TimeElapsed()));
}
void FemMesh::read(const char* FileName)
{
Base::FileInfo File(FileName);
_Mtrx = Base::Matrix4D();
// checking on the file
if (!File.isReadable()) {
throw Base::FileException("File to load not existing or not readable", File);
}
if (File.hasExtension("unv")) {
// read UNV file
myMesh->UNVToMesh(File.filePath().c_str());
}
else if (File.hasExtension("med")) {
myMesh->MEDToMesh(File.filePath().c_str(), File.fileNamePure().c_str());
}
else if (File.hasExtension("inp")) {
// read Abaqus inp mesh file
readAbaqus(File.filePath());
// if the file doesn't contain supported geometries try Nastran95
SMESHDS_Mesh* meshds = this->myMesh->GetMeshDS();
if (meshds->NbNodes() == 0) {
readNastran95(File.filePath());
}
}
else if (File.hasExtension("stl")) {
// read brep-file
myMesh->STLToMesh(File.filePath().c_str());
}
else if (File.hasExtension("bdf")) {
// read Nastran-file
readNastran(File.filePath());
}
#ifdef FC_USE_VTK
else if (File.hasExtension({"vtk", "vtu", "pvtu"})) {
// read *.vtk legacy format or *.vtu XML unstructure Mesh
FemVTKTools::readVTKMesh(File.filePath().c_str(), this);
}
#endif
else if (File.hasExtension("z88")) {
// read Z88 mesh file
readZ88(File.filePath());
}
else {
throw Base::FileException("Unknown extension");
}
}
void FemMesh::writeVTK(const std::string& fileName, bool highest) const
{
#ifdef FC_USE_VTK
FemVTKTools::writeVTKMesh(fileName.c_str(), this, highest);
#endif
}
void FemMesh::writeABAQUS(
const std::string& Filename,
int elemParam,
bool groupParam,
ABAQUS_VolumeVariant volVariant,
ABAQUS_FaceVariant faceVariant,
ABAQUS_EdgeVariant edgeVariant
) const
{
/*
* elemParam:
* 0 = all elements
* 1 = highest elements only
* 2 = FEM elements only (only edges not belonging to faces and faces not belonging to volumes)
*
* groupParam:
* true = write group data
* false = do not write group data
* volVariant, faceVariant, edgeVariant:
* Element type according to availability in CalculiX
*/
std::map<std::string, std::string> variants;
// volume elements
variants["Tetra4"] = "C3D4";
variants["Penta6"] = "C3D6";
variants["Hexa8"] = "C3D8";
variants["Tetra10"] = "C3D10";
variants["Penta15"] = "C3D15";
variants["Hexa20"] = "C3D20";
switch (volVariant) {
case ABAQUS_VolumeVariant::Standard:
break;
case ABAQUS_VolumeVariant::Reduced:
variants["Hexa8"] = "C3D8R";
variants["Hexa20"] = "C3D20R";
break;
case ABAQUS_VolumeVariant::Incompatible:
variants["Hexa8"] = "C3D8I";
break;
case ABAQUS_VolumeVariant::Modified:
variants["Tetra10"] = "C3D10T";
break;
case ABAQUS_VolumeVariant::Fluid:
variants["Tetra4"] = "F3D4";
variants["Penta6"] = "F3D6";
variants["Hexa8"] = "F3D8";
break;
}
// face elements
switch (faceVariant) {
case ABAQUS_FaceVariant::Shell:
variants["Tria3"] = "S3";
variants["Quad4"] = "S4";
variants["Tria6"] = "S6";
variants["Quad8"] = "S8";
break;
case ABAQUS_FaceVariant::Shell_Reduced:
variants["Tria3"] = "S3";
variants["Quad4"] = "S4R";
variants["Tria6"] = "S6";
variants["Quad8"] = "S8R";
break;
case ABAQUS_FaceVariant::Membrane:
variants["Tria3"] = "M3D3";
variants["Quad4"] = "M3D4";
variants["Tria6"] = "M3D6";
variants["Quad8"] = "M3D8";
break;
case ABAQUS_FaceVariant::Membrane_Reduced:
variants["Tria3"] = "M3D3";
variants["Quad4"] = "M3D4R";
variants["Tria6"] = "M3D6";
variants["Quad8"] = "M3D8R";
break;
case ABAQUS_FaceVariant::Stress:
variants["Tria3"] = "CPS3";
variants["Quad4"] = "CPS4";
variants["Tria6"] = "CPS6";
variants["Quad8"] = "CPS8";
break;
case ABAQUS_FaceVariant::Stress_Reduced:
variants["Tria3"] = "CPS3";
variants["Quad4"] = "CPS4R";
variants["Tria6"] = "CPS6";
variants["Quad8"] = "CPS8R";
break;
case ABAQUS_FaceVariant::Strain:
variants["Tria3"] = "CPE3";
variants["Quad4"] = "CPE4";
variants["Tria6"] = "CPE6";
variants["Quad8"] = "CPE8";
break;
case ABAQUS_FaceVariant::Strain_Reduced:
variants["Tria3"] = "CPE3";
variants["Quad4"] = "CPE4R";
variants["Tria6"] = "CPE6";
variants["Quad8"] = "CPE8R";
break;
case ABAQUS_FaceVariant::Axisymmetric:
variants["Tria3"] = "CAX3";
variants["Quad4"] = "CAX4";
variants["Tria6"] = "CAX6";
variants["Quad8"] = "CAX8";
break;
case ABAQUS_FaceVariant::Axisymmetric_Reduced:
variants["Tria3"] = "CAX3";
variants["Quad4"] = "CAX4R";
variants["Tria6"] = "CAX6";
variants["Quad8"] = "CAX8R";
break;
}
// edge elements
switch (edgeVariant) {
case ABAQUS_EdgeVariant::Beam:
variants["Seg2"] = "B31";
variants["Seg3"] = "B32";
break;
case ABAQUS_EdgeVariant::Beam_Reduced:
variants["Seg2"] = "B31R";
variants["Seg3"] = "B32R";
break;
case ABAQUS_EdgeVariant::Truss:
variants["Seg2"] = "T3D2";
variants["Seg3"] = "T3D3";
break;
case ABAQUS_EdgeVariant::Network:
variants["Seg2"] = "B31";
variants["Seg3"] = "D";
break;
}
std::map<std::string, std::vector<int>> elemOrderMap;
std::map<int, std::string> edgeTypeMap;
std::map<int, std::string> faceTypeMap;
std::map<int, std::string> volTypeMap;
// node order fits with node order in importCcxFrdResults.py module to import
// CalculiX result meshes
// dimension 1
//
// seg2 FreeCAD --> B31, B31R, T3D2 CalculiX
// N1, N2
std::vector<int> seg2 = boost::assign::list_of(0)(1);
//
// seg3 FreeCAD --> B32, B32R, T3D3 D CalculiX
// N1, N3, N2
std::vector<int> seg3 = boost::assign::list_of(0)(2)(1);
elemOrderMap.insert(std::make_pair(variants["Seg2"], seg2));
edgeTypeMap.insert(std::make_pair(seg2.size(), variants["Seg2"]));
elemOrderMap.insert(std::make_pair(variants["Seg3"], seg3));
edgeTypeMap.insert(std::make_pair(seg3.size(), variants["Seg3"]));
// dimension 2
//
// tria3 FreeCAD --> S3, M3D3, CPS3, CPE3, CAX3 CalculiX
// N1, N2, N3
std::vector<int> tria3 = boost::assign::list_of(0)(1)(2);
//
// tria6 FreeCAD --> S6 M3D6, CPS6, CPE6, CAX6 CalculiX
// N1, N2, N3, N4, N5, N6
std::vector<int> tria6 = boost::assign::list_of(0)(1)(2)(3)(4)(5);
//
// quad4 FreeCAD --> S4, S4R, M3D4, M3D4R, CPS4, CPS4R, CPE4, CPE4R, CAX4, CAX4R CalculiX
// N1, N2, N3, N4
std::vector<int> quad4 = boost::assign::list_of(0)(1)(2)(3);
//
// quad8 FreeCAD --> S8, S8R, M3D8, M3D8R, CPS8, CPS8R, CPE8, CPE8R, CAX8, CAX8R CalculiX
// N1, N2, N3, N4, N5, N6, N7, N8
std::vector<int> quad8 = boost::assign::list_of(0)(1)(2)(3)(4)(5)(6)(7);
elemOrderMap.insert(std::make_pair(variants["Tria3"], tria3));
faceTypeMap.insert(std::make_pair(tria3.size(), variants["Tria3"]));
elemOrderMap.insert(std::make_pair(variants["Tria6"], tria6));
faceTypeMap.insert(std::make_pair(tria6.size(), variants["Tria6"]));
elemOrderMap.insert(std::make_pair(variants["Quad4"], quad4));
faceTypeMap.insert(std::make_pair(quad4.size(), variants["Quad4"]));
elemOrderMap.insert(std::make_pair(variants["Quad8"], quad8));
faceTypeMap.insert(std::make_pair(quad8.size(), variants["Quad8"]));
// dimension 3
//
// tetra4 FreeCAD --> C3D4, F3D4 CalculiX
// N2, N1, N3, N4
std::vector<int> tetra4 = boost::assign::list_of(1)(0)(2)(3);
// tetra10: FreeCAD --> C3D10, C3D10T CalculiX
// N2, N1, N3, N4, N5, N7, N6, N9, N8, N10
std::vector<int> tetra10 = boost::assign::list_of(1)(0)(2)(3)(4)(6)(5)(8)(7)(9);
// tetra node order for the system which is used for hexa8, hexa20, penta6 and penta15
// be careful with activating because of method getccxVolumesByFace())
// hexa8 FreeCAD --> C3D8, C3D8R, C3D8I, F3D8 CalculiX
// N6, N7, N8, N5, N2, N3, N4, N1
std::vector<int> hexa8 = boost::assign::list_of(5)(6)(7)(4)(1)(2)(3)(0);
//
// hexa20 FreeCAD --> C3D20, C3D20R CalculiX
// N6, N7, N8, N5, N2, N3, N4, N1, N14, N15, N16, N13, N10, N11, N12, N9, N18, N19, N20, N17
std::vector<int> hexa20
= boost::assign::list_of(5)(6)(7)(4)(1)(2)(3)(0)(13)(14)(15)(12)(9)(10)(11)(8)(17)(18)(19)(16);
//
// penta6 FreeCAD --> C3D6, F3D6 CalculiX
// N5, N6, N4, N2, N3, N1
std::vector<int> penta6 = boost::assign::list_of(4)(5)(3)(1)(2)(0);
//
// penta15 FreeCAD --> C3D15 CalculiX
// N5, N6, N4, N2, N3, N1, N11, N12, N10, N8, N9, N7, N14, N15, N13
std::vector<int> penta15 = boost::assign::list_of(4)(5)(3)(1)(2)(0)(10)(11)(9)(7)(8)(6)(13)(14)(12);
elemOrderMap.insert(std::make_pair(variants["Tetra4"], tetra4));
volTypeMap.insert(std::make_pair(tetra4.size(), variants["Tetra4"]));
elemOrderMap.insert(std::make_pair(variants["Tetra10"], tetra10));
volTypeMap.insert(std::make_pair(tetra10.size(), variants["Tetra10"]));
elemOrderMap.insert(std::make_pair(variants["Hexa8"], hexa8));
volTypeMap.insert(std::make_pair(hexa8.size(), variants["Hexa8"]));
elemOrderMap.insert(std::make_pair(variants["Hexa20"], hexa20));
volTypeMap.insert(std::make_pair(hexa20.size(), variants["Hexa20"]));
elemOrderMap.insert(std::make_pair(variants["Penta6"], penta6));
volTypeMap.insert(std::make_pair(penta6.size(), variants["Penta6"]));
elemOrderMap.insert(std::make_pair(variants["Penta15"], penta15));
volTypeMap.insert(std::make_pair(penta15.size(), variants["Penta15"]));
// get all data --> Extract Nodes and Elements of the current SMESH datastructure
using VertexMap = std::map<int, Base::Vector3d>;
using NodesMap = std::map<int, std::vector<int>>;
using ElementsMap = std::map<std::string, NodesMap>;
// get nodes
VertexMap vertexMap; // empty nodes map
SMDS_NodeIteratorPtr aNodeIter = myMesh->GetMeshDS()->nodesIterator();
Base::Vector3d current_node;
while (aNodeIter->more()) {
const SMDS_MeshNode* aNode = aNodeIter->next();
current_node.Set(aNode->X(), aNode->Y(), aNode->Z());
current_node = _Mtrx * current_node;
vertexMap[aNode->GetID()] = current_node;
}
// get volumes
ElementsMap elementsMapVol; // empty volumes map
SMDS_VolumeIteratorPtr aVolIter = myMesh->GetMeshDS()->volumesIterator();
while (aVolIter->more()) {
const SMDS_MeshVolume* aVol = aVolIter->next();
std::pair<int, std::vector<int>> apair;
apair.first = aVol->GetID();
int numNodes = aVol->NbNodes();
std::map<int, std::string>::iterator it = volTypeMap.find(numNodes);
if (it != volTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (int jt : order) {
apair.second.push_back(aVol->GetNode(jt)->GetID());
}
elementsMapVol[it->second].insert(apair);
}
}
// get faces
ElementsMap elementsMapFac; // empty faces map used for elemParam = 1
// and elementsMapVol is not empty
if ((elemParam == 0) || (elemParam == 1 && elementsMapVol.empty())) {
// for elemParam = 1 we only fill the elementsMapFac if the elmentsMapVol is empty
// we're going to fill the elementsMapFac with all faces
SMDS_FaceIteratorPtr aFaceIter = myMesh->GetMeshDS()->facesIterator();
while (aFaceIter->more()) {
const SMDS_MeshFace* aFace = aFaceIter->next();
std::pair<int, std::vector<int>> apair;
apair.first = aFace->GetID();
int numNodes = aFace->NbNodes();
std::map<int, std::string>::iterator it = faceTypeMap.find(numNodes);
if (it != faceTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (int jt : order) {
apair.second.push_back(aFace->GetNode(jt)->GetID());
}
elementsMapFac[it->second].insert(apair);
}
}
}
if (elemParam == 2) {
// we're going to fill the elementsMapFac with the facesOnly
std::set<int> facesOnly = getFacesOnly();
for (int itfa : facesOnly) {
std::pair<int, std::vector<int>> apair;
apair.first = itfa;
const SMDS_MeshElement* aFace = myMesh->GetMeshDS()->FindElement(itfa);
int numNodes = aFace->NbNodes();
std::map<int, std::string>::iterator it = faceTypeMap.find(numNodes);
if (it != faceTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (int jt : order) {
apair.second.push_back(aFace->GetNode(jt)->GetID());
}
elementsMapFac[it->second].insert(apair);
}
}
}
// get edges
ElementsMap elementsMapEdg; // empty edges map used for elemParam == 1
// and either elementMapVol or elementsMapFac are not empty
if ((elemParam == 0) || (elemParam == 1 && elementsMapVol.empty() && elementsMapFac.empty())) {
// for elemParam = 1 we only fill the elementsMapEdg if the elmentsMapVol
// and elmentsMapFac are empty we're going to fill the elementsMapEdg with all edges
SMDS_EdgeIteratorPtr aEdgeIter = myMesh->GetMeshDS()->edgesIterator();
while (aEdgeIter->more()) {
const SMDS_MeshEdge* aEdge = aEdgeIter->next();
std::pair<int, std::vector<int>> apair;
apair.first = aEdge->GetID();
int numNodes = aEdge->NbNodes();
std::map<int, std::string>::iterator it = edgeTypeMap.find(numNodes);
if (it != edgeTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (int jt : order) {
apair.second.push_back(aEdge->GetNode(jt)->GetID());
}
elementsMapEdg[it->second].insert(apair);
}
}
}
if (elemParam == 2) {
// we're going to fill the elementsMapEdg with the edgesOnly
std::set<int> edgesOnly = getEdgesOnly();
for (int ited : edgesOnly) {
std::pair<int, std::vector<int>> apair;
apair.first = ited;
const SMDS_MeshElement* aEdge = myMesh->GetMeshDS()->FindElement(ited);
int numNodes = aEdge->NbNodes();
std::map<int, std::string>::iterator it = edgeTypeMap.find(numNodes);
if (it != edgeTypeMap.end()) {
const std::vector<int>& order = elemOrderMap[it->second];
for (int jt : order) {
apair.second.push_back(aEdge->GetNode(jt)->GetID());
}
elementsMapEdg[it->second].insert(apair);
}
}
}
// write all data to file
// take also care of special characters in path
// https://forum.freecad.org/viewtopic.php?f=10&t=37436
Base::FileInfo fi(Filename);
Base::ofstream anABAQUS_Output(fi);
// https://forum.freecad.org/viewtopic.php?f=18&t=22759#p176669
anABAQUS_Output.precision(13);
// add some text and make sure one of the known elemParam values is used
anABAQUS_Output << "** written by FreeCAD inp file writer for CalculiX,Abaqus meshes"
<< std::endl;
switch (elemParam) {
case 0:
anABAQUS_Output << "** all mesh elements." << std::endl << std::endl;
break;
case 1:
anABAQUS_Output << "** highest dimension mesh elements only." << std::endl << std::endl;
break;
case 2:
anABAQUS_Output << "** FEM mesh elements only (edges if they do not belong to faces "
"and faces if they do not belong to volumes)."
<< std::endl
<< std::endl;
break;
default:
anABAQUS_Output << "** Problem on writing" << std::endl;
anABAQUS_Output.close();
throw std::runtime_error("Unknown ABAQUS element choice parameter, [0|1|2] are allowed.");
}
// write nodes
anABAQUS_Output << "** Nodes" << std::endl;
anABAQUS_Output << "*Node, NSET=Nall" << std::endl;
// Axisymmetric, plane strain and plane stress elements expect nodes in the plane z=0.
// Set the z coordinate to 0 to avoid possible rounding errors.
switch (faceVariant) {
case ABAQUS_FaceVariant::Stress:
case ABAQUS_FaceVariant::Stress_Reduced:
case ABAQUS_FaceVariant::Strain:
case ABAQUS_FaceVariant::Strain_Reduced:
case ABAQUS_FaceVariant::Axisymmetric:
case ABAQUS_FaceVariant::Axisymmetric_Reduced:
for (const auto& elMap : elementsMapFac) {
const NodesMap& nodeMap = elMap.second;
for (const auto& nodes : nodeMap) {
for (int n : nodes.second) {
Base::Vector3d& vertex = vertexMap[n];
vertex.z = 0.0;
}
}
}
break;
default:
break;
}
// This way we get sorted output.
// See https://forum.freecad.org/viewtopic.php?f=18&t=12646&start=40#p103004
for (const auto& it : vertexMap) {
anABAQUS_Output << it.first << ", " << it.second.x << ", " << it.second.y << ", "
<< it.second.z << std::endl;
}
anABAQUS_Output << std::endl << std::endl;
;
// write volumes to file
std::string elsetname;
if (!elementsMapVol.empty()) {
for (const auto& it : elementsMapVol) {
anABAQUS_Output << "** Volume elements" << std::endl;
anABAQUS_Output << "*Element, TYPE=" << it.first << ", ELSET=Evolumes" << std::endl;
for (const auto& jt : it.second) {
anABAQUS_Output << jt.first;
// Calculix allows max 16 entries in one line, a hexa20 has more !
int ct = 0; // counter
bool first_line = true;
for (auto kt = jt.second.begin(); kt != jt.second.end(); ++kt, ++ct) {
if (ct < 15) {
anABAQUS_Output << ", " << *kt;
}
else {
if (first_line) {
anABAQUS_Output << "," << std::endl << *kt;
first_line = false;
}
else {
anABAQUS_Output << ", " << *kt;
}
}
}
anABAQUS_Output << std::endl;
}
}
elsetname += "Evolumes";
anABAQUS_Output << std::endl;
}
// write faces to file
if (!elementsMapFac.empty()) {
for (const auto& it : elementsMapFac) {
anABAQUS_Output << "** Face elements" << std::endl;
anABAQUS_Output << "*Element, TYPE=" << it.first << ", ELSET=Efaces" << std::endl;
for (const auto& jt : it.second) {
anABAQUS_Output << jt.first;
for (int kt : jt.second) {
anABAQUS_Output << ", " << kt;
}
anABAQUS_Output << std::endl;
}
}
if (elsetname.empty()) {
elsetname += "Efaces";
}
else {
elsetname += ", Efaces";
}
anABAQUS_Output << std::endl;
}
// write edges to file
if (!elementsMapEdg.empty()) {
for (const auto& it : elementsMapEdg) {
anABAQUS_Output << "** Edge elements" << std::endl;
anABAQUS_Output << "*Element, TYPE=" << it.first << ", ELSET=Eedges" << std::endl;
for (const auto& jt : it.second) {
anABAQUS_Output << jt.first;
for (int kt : jt.second) {
anABAQUS_Output << ", " << kt;
}
anABAQUS_Output << std::endl;
}
}
if (elsetname.empty()) {
elsetname += "Eedges";
}
else {
elsetname += ", Eedges";
}
anABAQUS_Output << std::endl;
}
// write elset Eall
anABAQUS_Output << "** Define element set Eall" << std::endl;
anABAQUS_Output << "*ELSET, ELSET=Eall" << std::endl;
anABAQUS_Output << elsetname << std::endl;
// groups
if (!groupParam) {
anABAQUS_Output.close();
}
else {
// get and write group data
anABAQUS_Output << std::endl << "** Group data" << std::endl;
std::list<int> groupIDs = myMesh->GetGroupIds();
for (int it : groupIDs) {
// get and write group info and group definition
// TODO group element type code has duplicate code of
// PyObject* FemMeshPy::getGroupElementType()
SMDSAbs_ElementType aElementType = myMesh->GetGroup(it)->GetGroupDS()->GetType();
const char* groupElementType = "";
switch (aElementType) {
case SMDSAbs_All:
groupElementType = "All";
break;
case SMDSAbs_Node:
groupElementType = "Node";
break;
case SMDSAbs_Edge:
groupElementType = "Edge";
break;
case SMDSAbs_Face:
groupElementType = "Face";
break;
case SMDSAbs_Volume:
groupElementType = "Volume";
break;
case SMDSAbs_0DElement:
groupElementType = "0DElement";
break;
case SMDSAbs_Ball:
groupElementType = "Ball";
break;
default:
groupElementType = "Unknown";
break;
}
const char* groupName = myMesh->GetGroup(it)->GetName();
anABAQUS_Output << "** GroupID: " << (it) << " --> GroupName: " << groupName
<< " --> GroupElementType: " << groupElementType << std::endl;
if (aElementType == SMDSAbs_Node) {
anABAQUS_Output << "*NSET, NSET=" << groupName << std::endl;
}
else {
anABAQUS_Output << "*ELSET, ELSET=" << groupName << std::endl;
}
// get and write group elements
std::set<int> ids;
SMDS_ElemIteratorPtr aElemIter = myMesh->GetGroup(it)->GetGroupDS()->GetElements();
while (aElemIter->more()) {
const SMDS_MeshElement* aElement = aElemIter->next();
ids.insert(aElement->GetID());
}
for (int it : ids) {
anABAQUS_Output << it << std::endl;
}
// write newline after each group
anABAQUS_Output << std::endl;
}
anABAQUS_Output.close();
}
}
void FemMesh::writeZ88(const std::string& FileName) const
{
Base::TimeElapsed Start;
Base::Console().log("Start: FemMesh::writeZ88() =================================\n");
/*
Python command to export FemMesh from StartWB FEM 3D example:
import feminout.importZ88Mesh
feminout.importZ88Mesh.write(App.ActiveDocument.Box_Mesh.FemMesh, '/tmp/mesh.z88')
*/
Base::PyGILStateLocker lock;
PyObject* module = PyImport_ImportModule("feminout.importZ88Mesh");
if (!module) {
return;
}
// Make sure the reference counter won't become 0 when passing this mesh to its wrapper
FemMesh* self = const_cast<FemMesh*>(this);
self->ref();
try {
Py::Module z88mod(module, true);
Py::Object mesh = Py::asObject(new FemMeshPy(self));
Py::Callable method(z88mod.getAttr("write"));
Py::Tuple args(2);
args.setItem(0, mesh);
args.setItem(1, Py::String(FileName));
method.apply(args);
}
catch (Py::Exception& e) {
e.clear();
}
// Safely decrease the reference counter without destroying this mesh
self->unrefNoDelete();
}
void FemMesh::write(const char* FileName) const
{
Base::FileInfo File(FileName);
if (File.hasExtension("unv")) {
Base::Console().log("FEM mesh object will be exported to unv format.\n");
// write UNV file
myMesh->ExportUNV(File.filePath().c_str());
}
else if (File.hasExtension("med")) {
Base::Console().log("FEM mesh object will be exported to med format.\n");
myMesh->ExportMED(
File.filePath().c_str(),
File.fileNamePure().c_str(),
false,
2
); // 2 means MED_V2_2 version!
}
else if (File.hasExtension("stl")) {
Base::Console().log("FEM mesh object will be exported to stl format.\n");
// export to stl file
myMesh->ExportSTL(File.filePath().c_str(), false);
}
else if (File.hasExtension("dat")) {
Base::Console().log("FEM mesh object will be exported to dat format.\n");
// export to dat file
myMesh->ExportDAT(File.filePath().c_str());
}
else if (File.hasExtension("inp")) {
Base::Console().log("FEM mesh object will be exported to inp format.\n");
// write ABAQUS Output
writeABAQUS(File.filePath(), 1, false);
}
#ifdef FC_USE_VTK
else if (File.hasExtension({"vtk", "vtu"})) {
Base::Console().log("FEM mesh object will be exported to either vtk or vtu format.\n");
// write unstructure mesh to VTK format *.vtk and *.vtu
writeVTK(File.filePath().c_str());
}
#endif
else if (File.hasExtension("z88")) {
Base::Console().log("FEM mesh object will be exported to z88 format.\n");
// write z88 file
writeZ88(File.filePath());
}
else {
throw Base::FileException("An unknown file extension was added!");
}
}
// ==== Base class implementer ==============================================================
unsigned int FemMesh::getMemSize() const
{
return 0;
}
void FemMesh::Save(Base::Writer& writer) const
{
if (!writer.isForceXML()) {
// See SaveDocFile(), RestoreDocFile()
writer.Stream() << writer.ind() << "<FemMesh file=\"";
writer.Stream() << writer.addFile("FemMesh.unv", this) << "\"";
writer.Stream() << " a11=\"" << _Mtrx[0][0] << "\" a12=\"" << _Mtrx[0][1] << "\" a13=\""
<< _Mtrx[0][2] << "\" a14=\"" << _Mtrx[0][3] << "\"";
writer.Stream() << " a21=\"" << _Mtrx[1][0] << "\" a22=\"" << _Mtrx[1][1] << "\" a23=\""
<< _Mtrx[1][2] << "\" a24=\"" << _Mtrx[1][3] << "\"";
writer.Stream() << " a31=\"" << _Mtrx[2][0] << "\" a32=\"" << _Mtrx[2][1] << "\" a33=\""
<< _Mtrx[2][2] << "\" a34=\"" << _Mtrx[2][3] << "\"";
writer.Stream() << " a41=\"" << _Mtrx[3][0] << "\" a42=\"" << _Mtrx[3][1] << "\" a43=\""
<< _Mtrx[3][2] << "\" a44=\"" << _Mtrx[3][3] << "\"";
writer.Stream() << "/>" << std::endl;
}
else {
writer.Stream() << writer.ind() << "<FemMesh file=\"\"";
writer.Stream() << " a11=\"" << _Mtrx[0][0] << "\" a12=\"" << _Mtrx[0][1] << "\" a13=\""
<< _Mtrx[0][2] << "\" a14=\"" << _Mtrx[0][3] << "\"";
writer.Stream() << " a21=\"" << _Mtrx[1][0] << "\" a22=\"" << _Mtrx[1][1] << "\" a23=\""
<< _Mtrx[1][2] << "\" a24=\"" << _Mtrx[1][3] << "\"";
writer.Stream() << " a31=\"" << _Mtrx[2][0] << "\" a32=\"" << _Mtrx[2][1] << "\" a33=\""
<< _Mtrx[2][2] << "\" a34=\"" << _Mtrx[2][3] << "\"";
writer.Stream() << " a41=\"" << _Mtrx[3][0] << "\" a42=\"" << _Mtrx[3][1] << "\" a43=\""
<< _Mtrx[3][2] << "\" a44=\"" << _Mtrx[3][3] << "\"";
writer.Stream() << "/>" << std::endl;
}
}
void FemMesh::Restore(Base::XMLReader& reader)
{
reader.readElement("FemMesh");
std::string file(reader.getAttribute<const char*>("file"));
if (!file.empty()) {
// initiate a file read
reader.addFile(file.c_str(), this);
}
if (reader.hasAttribute("a11")) {
_Mtrx[0][0] = reader.getAttribute<double>("a11");
_Mtrx[0][1] = reader.getAttribute<double>("a12");
_Mtrx[0][2] = reader.getAttribute<double>("a13");
_Mtrx[0][3] = reader.getAttribute<double>("a14");
_Mtrx[1][0] = reader.getAttribute<double>("a21");
_Mtrx[1][1] = reader.getAttribute<double>("a22");
_Mtrx[1][2] = reader.getAttribute<double>("a23");
_Mtrx[1][3] = reader.getAttribute<double>("a24");
_Mtrx[2][0] = reader.getAttribute<double>("a31");
_Mtrx[2][1] = reader.getAttribute<double>("a32");
_Mtrx[2][2] = reader.getAttribute<double>("a33");
_Mtrx[2][3] = reader.getAttribute<double>("a34");
_Mtrx[3][0] = reader.getAttribute<double>("a41");
_Mtrx[3][1] = reader.getAttribute<double>("a42");
_Mtrx[3][2] = reader.getAttribute<double>("a43");
_Mtrx[3][3] = reader.getAttribute<double>("a44");
}
}
void FemMesh::SaveDocFile(Base::Writer& writer) const
{
// create a temporary file and copy the content to the zip stream
Base::FileInfo fi(App::Application::getTempFileName().c_str());
myMesh->ExportUNV(fi.filePath().c_str());
Base::ifstream file(fi, std::ios::in | std::ios::binary);
if (file) {
std::streambuf* buf = file.rdbuf();
writer.Stream() << buf;
}
file.close();
// remove temp file
fi.deleteFile();
}
void FemMesh::RestoreDocFile(Base::Reader& reader)
{
// create a temporary file and copy the content from the zip stream
Base::FileInfo fi(App::Application::getTempFileName().c_str());
// read in the ASCII file and write back to the file stream
Base::ofstream file(fi, std::ios::out | std::ios::binary);
if (reader) {
reader >> file.rdbuf();
}
file.close();
// read the shape from the temp file
myMesh->UNVToMesh(fi.filePath().c_str());
// delete the temp file
fi.deleteFile();
}
void FemMesh::transformGeometry(const Base::Matrix4D& rclTrf)
{
// We perform a translation and rotation of the current active Mesh object
Base::Matrix4D clMatrix(rclTrf);
SMDS_NodeIteratorPtr aNodeIter = myMesh->GetMeshDS()->nodesIterator();
Base::Vector3d current_node;
for (; aNodeIter->more();) {
const SMDS_MeshNode* aNode = aNodeIter->next();
current_node.Set(aNode->X(), aNode->Y(), aNode->Z());
current_node = clMatrix * current_node;
myMesh->GetMeshDS()->MoveNode(aNode, current_node.x, current_node.y, current_node.z);
}
}
void FemMesh::setTransform(const Base::Matrix4D& rclTrf)
{
// Placement handling, no geometric transformation
_Mtrx = rclTrf;
}
Base::Matrix4D FemMesh::getTransform() const
{
return _Mtrx;
}
Base::BoundBox3d FemMesh::getBoundBox() const
{
Base::BoundBox3d box;
const SMESHDS_Mesh* data = getSMesh()->GetMeshDS();
SMDS_NodeIteratorPtr aNodeIter = data->nodesIterator();
for (; aNodeIter->more();) {
const SMDS_MeshNode* aNode = aNodeIter->next();
Base::Vector3d vec(aNode->X(), aNode->Y(), aNode->Z());
// Apply the matrix to hold the BoundBox in absolute space.
vec = _Mtrx * vec;
box.Add(vec);
}
return box;
}
std::vector<const char*> FemMesh::getElementTypes() const
{
std::vector<const char*> temp;
temp.push_back("Vertex");
temp.push_back("Edge");
temp.push_back("Face");
temp.push_back("Volume");
return temp;
}
unsigned long FemMesh::countSubElements(const char* /*Type*/) const
{
return 0;
}
Data::Segment* FemMesh::getSubElement(const char* /*Type*/, unsigned long /*n*/) const
{
// FIXME implement subelement interface
// std::stringstream str;
// str << Type << n;
// std::string temp = str.str();
// return new ShapeSegment(getSubShape(temp.c_str()));
return nullptr;
}
void FemMesh::getPoints(
std::vector<Base::Vector3d>& Points,
std::vector<Base::Vector3d>& /*Normals*/,
double /*Accuracy*/,
uint16_t /*flags*/
) const
{
const SMESHDS_Mesh* data = getSMesh()->GetMeshDS();
std::vector<Base::Vector3d> nodes;
nodes.reserve(data->NbNodes());
SMDS_NodeIteratorPtr aNodeIter = data->nodesIterator();
for (; aNodeIter->more();) {
const SMDS_MeshNode* aNode = aNodeIter->next();
nodes.emplace_back(aNode->X(), aNode->Y(), aNode->Z());
}
Points = transformPointsToOutside(nodes);
}
struct Fem::FemMesh::FemMeshInfo FemMesh::getInfo() const
{
struct FemMeshInfo rtrn;
const SMESHDS_Mesh* data = getSMesh()->GetMeshDS();
const SMDS_MeshInfo& info = data->GetMeshInfo();
rtrn.numFaces = data->NbFaces();
rtrn.numNode = info.NbNodes();
rtrn.numTria = info.NbTriangles();
rtrn.numQuad = info.NbQuadrangles();
rtrn.numPoly = info.NbPolygons();
rtrn.numVolu = info.NbVolumes();
rtrn.numTetr = info.NbTetras();
rtrn.numHexa = info.NbHexas();
rtrn.numPyrd = info.NbPyramids();
rtrn.numPris = info.NbPrisms();
rtrn.numHedr = info.NbPolyhedrons();
return rtrn;
}
Base::Quantity FemMesh::getVolume() const
{
SMDS_VolumeIteratorPtr aVolIter = myMesh->GetMeshDS()->volumesIterator();
// Calculate Mesh Volume
// For an accurate Volume Calculation of a quadratic Tetrahedron
// we have to calculate the Volume of 8 Sub-Tetrahedrons
Base::Vector3d a, b, c, a_b_product;
double volume = 0.0;
for (; aVolIter->more();) {
const SMDS_MeshVolume* aVol = aVolIter->next();
if (aVol->NbNodes() != 10) {
continue;
}
Base::Vector3d v1(aVol->GetNode(1)->X(), aVol->GetNode(1)->Y(), aVol->GetNode(1)->Z());
Base::Vector3d v0(aVol->GetNode(0)->X(), aVol->GetNode(0)->Y(), aVol->GetNode(0)->Z());
Base::Vector3d v2(aVol->GetNode(2)->X(), aVol->GetNode(2)->Y(), aVol->GetNode(2)->Z());
Base::Vector3d v3(aVol->GetNode(3)->X(), aVol->GetNode(3)->Y(), aVol->GetNode(3)->Z());
Base::Vector3d v4(aVol->GetNode(4)->X(), aVol->GetNode(4)->Y(), aVol->GetNode(4)->Z());
Base::Vector3d v6(aVol->GetNode(6)->X(), aVol->GetNode(6)->Y(), aVol->GetNode(6)->Z());
Base::Vector3d v5(aVol->GetNode(5)->X(), aVol->GetNode(5)->Y(), aVol->GetNode(5)->Z());
Base::Vector3d v8(aVol->GetNode(8)->X(), aVol->GetNode(8)->Y(), aVol->GetNode(8)->Z());
Base::Vector3d v7(aVol->GetNode(7)->X(), aVol->GetNode(7)->Y(), aVol->GetNode(7)->Z());
Base::Vector3d v9(aVol->GetNode(9)->X(), aVol->GetNode(9)->Y(), aVol->GetNode(9)->Z());
// 1,5,8,7
a = v4 - v0;
b = v7 - v0;
c = v6 - v0;
a_b_product.x = a.y * b.z - b.y * a.z;
a_b_product.y = a.z * b.x - b.z * a.x;
a_b_product.z = a.x * b.y - b.x * a.y;
volume += 1.0 / 6.0
* fabs((a_b_product.x * c.x) + (a_b_product.y * c.y) + (a_b_product.z * c.z));
// 5,9,8,7
a = v8 - v4;
b = v7 - v4;
c = v6 - v4;
a_b_product.x = a.y * b.z - b.y * a.z;
a_b_product.y = a.z * b.x - b.z * a.x;
a_b_product.z = a.x * b.y - b.x * a.y;
volume += 1.0 / 6.0
* fabs((a_b_product.x * c.x) + (a_b_product.y * c.y) + (a_b_product.z * c.z));
// 5,2,9,7
a = v1 - v4;
b = v8 - v4;
c = v6 - v4;
a_b_product.x = a.y * b.z - b.y * a.z;
a_b_product.y = a.z * b.x - b.z * a.x;
a_b_product.z = a.x * b.y - b.x * a.y;
volume += 1.0 / 6.0
* fabs((a_b_product.x * c.x) + (a_b_product.y * c.y) + (a_b_product.z * c.z));
// 2,6,9,7
a = v5 - v1;
b = v8 - v1;
c = v6 - v1;
a_b_product.x = a.y * b.z - b.y * a.z;
a_b_product.y = a.z * b.x - b.z * a.x;
a_b_product.z = a.x * b.y - b.x * a.y;
volume += 1.0 / 6.0
* fabs((a_b_product.x * c.x) + (a_b_product.y * c.y) + (a_b_product.z * c.z));
// 9,6,10,7
a = v5 - v8;
b = v9 - v8;
c = v6 - v8;
a_b_product.x = a.y * b.z - b.y * a.z;
a_b_product.y = a.z * b.x - b.z * a.x;
a_b_product.z = a.x * b.y - b.x * a.y;
volume += 1.0 / 6.0
* fabs((a_b_product.x * c.x) + (a_b_product.y * c.y) + (a_b_product.z * c.z));
// 6,3,10,7
a = v2 - v5;
b = v9 - v5;
c = v6 - v5;
a_b_product.x = a.y * b.z - b.y * a.z;
a_b_product.y = a.z * b.x - b.z * a.x;
a_b_product.z = a.x * b.y - b.x * a.y;
volume += 1.0 / 6.0
* fabs((a_b_product.x * c.x) + (a_b_product.y * c.y) + (a_b_product.z * c.z));
// 8,9,10,7
a = v8 - v7;
b = v9 - v7;
c = v6 - v7;
a_b_product.x = a.y * b.z - b.y * a.z;
a_b_product.y = a.z * b.x - b.z * a.x;
a_b_product.z = a.x * b.y - b.x * a.y;
volume += 1.0 / 6.0
* fabs((a_b_product.x * c.x) + (a_b_product.y * c.y) + (a_b_product.z * c.z));
// 8,9,10,4
a = v8 - v7;
b = v9 - v7;
c = v3 - v7;
a_b_product.x = a.y * b.z - b.y * a.z;
a_b_product.y = a.z * b.x - b.z * a.x;
a_b_product.z = a.x * b.y - b.x * a.y;
volume += 1.0 / 6.0
* fabs((a_b_product.x * c.x) + (a_b_product.y * c.y) + (a_b_product.z * c.z));
}
return Base::Quantity(volume, Unit::Volume);
}
int FemMesh::addGroup(const std::string TypeString, const std::string Name, const int theId)
{
// define mapping between typestring and ElementType
// TODO: remove code doubling by providing mappings for all FemMesh functions
using string_eltype_map = std::map<std::string, SMDSAbs_ElementType>;
string_eltype_map mapping;
mapping["All"] = SMDSAbs_All;
mapping["Node"] = SMDSAbs_Node;
mapping["Edge"] = SMDSAbs_Edge;
mapping["Face"] = SMDSAbs_Face;
mapping["Volume"] = SMDSAbs_Volume;
mapping["0DElement"] = SMDSAbs_0DElement;
mapping["Ball"] = SMDSAbs_Ball;
int aId = theId;
// check whether typestring is valid
bool typeStringValid = false;
for (string_eltype_map::const_iterator it = mapping.begin(); it != mapping.end(); ++it) {
std::string key = it->first;
if (key == TypeString) {
typeStringValid = true;
}
}
if (!typeStringValid) {
throw std::runtime_error(
"AddGroup: Invalid type string! Allowed: All, Node, Edge, Face, "
"Volume, 0DElement, Ball"
);
}
// add group to mesh
SMESH_Group* group = this->getSMesh()->AddGroup(mapping[TypeString], Name.c_str(), aId);
if (!group) {
throw std::runtime_error("AddGroup: Failed to create new group.");
}
#if SMESH_VERSION_MAJOR >= 9
return group->GetID();
#else
return aId;
#endif
}
void FemMesh::addGroupElements(int GroupId, const std::set<int>& ElementIds)
{
SMESH_Group* group = this->getSMesh()->GetGroup(GroupId);
if (!group) {
throw std::runtime_error("AddGroupElements: No group for given id.");
}
SMESHDS_Group* groupDS = dynamic_cast<SMESHDS_Group*>(group->GetGroupDS());
if (!groupDS) {
throw std::runtime_error("addGroupElements: Failed to add group elements.");
}
// Traverse the full mesh and add elements to group if id is in set 'ids'
// and if group type is compatible with element
SMDSAbs_ElementType aElementType = groupDS->GetType();
SMDS_ElemIteratorPtr aElemIter = this->getSMesh()->GetMeshDS()->elementsIterator(aElementType);
while (aElemIter->more()) {
const SMDS_MeshElement* aElem = aElemIter->next();
std::set<int>::iterator it;
it = ElementIds.find(aElem->GetID());
if (it != ElementIds.end()) {
// the element was in the list
if (!groupDS->Contains(aElem)) { // check whether element is already in group
groupDS->Add(aElem); // if not, add it
}
}
}
}
bool FemMesh::removeGroup(int GroupId)
{
return this->getSMesh()->RemoveGroup(GroupId);
}
void FemMesh::renameGroup(int id, const std::string& name)
{
SMESH_Group* grp = this->getSMesh()->GetGroup(id);
if (grp) {
grp->SetName(name.c_str());
}
}
|