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
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2009-2013 Lorenzo Mercantonio
* Copyright (C) 2014-2017 Cirilo Bernardo
* Copyright (C) 2018 Jean-Pierre Charras jp.charras at wanadoo.fr
* Copyright (C) 2004-2018 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <cmath>
#include <exception>
#include <fstream>
#include <iomanip>
#include <vector>
#include <wx/dir.h>
#include "3d_cache/3d_cache.h"
#include "3d_cache/3d_info.h"
#include "class_board.h"
#include "class_edge_mod.h"
#include "class_module.h"
#include "class_pcb_text.h"
#include "class_track.h"
#include "class_zone.h"
#include "convert_to_biu.h"
#include "draw_graphic_text.h"
#include "macros.h"
#include "pgm_base.h"
#include "plugins/3dapi/ifsg_all.h"
#include "streamwrapper.h"
#include "vrml_layer.h"
#include "pcb_edit_frame.h"
#include "../../kicad/kicad.h"
#include <convert_basic_shapes_to_polygon.h>
#include <zone_filler.h>
// minimum width (mm) of a VRML line
#define MIN_VRML_LINEWIDTH 0.05 // previously 0.12
// offset for art layers, mm (silk, paste, etc)
#define ART_OFFSET 0.025
// offset for plating
#define PLATE_OFFSET 0.005
static S3D_CACHE* cache;
static bool USE_INLINES; // true to use legacy inline{} behavior
static bool USE_DEFS; // true to reuse component definitions
static bool USE_RELPATH; // true to use relative paths in VRML inline{}
static double WORLD_SCALE = 1.0; // scaling from 0.1 in to desired VRML unit
static double BOARD_SCALE; // scaling from mm to desired VRML world scale
static const int PRECISION = 6; // legacy precision factor (now set to 6)
static wxString SUBDIR_3D; // legacy 3D subdirectory
static wxString PROJ_DIR; // project directory
struct VRML_COLOR
{
float diffuse_red;
float diffuse_grn;
float diffuse_blu;
float spec_red;
float spec_grn;
float spec_blu;
float emit_red;
float emit_grn;
float emit_blu;
float ambient;
float transp;
float shiny;
VRML_COLOR()
{
// default green
diffuse_red = 0.13;
diffuse_grn = 0.81;
diffuse_blu = 0.22;
spec_red = 0.01;
spec_grn = 0.08;
spec_blu = 0.02;
emit_red = 0.0;
emit_grn = 0.0;
emit_blu = 0.0;
ambient = 0.8;
transp = 0;
shiny = 0.02;
}
VRML_COLOR( float dr, float dg, float db,
float sr, float sg, float sb,
float er, float eg, float eb,
float am, float tr, float sh )
{
diffuse_red = dr;
diffuse_grn = dg;
diffuse_blu = db;
spec_red = sr;
spec_grn = sg;
spec_blu = sb;
emit_red = er;
emit_grn = eg;
emit_blu = eb;
ambient = am;
transp = tr;
shiny = sh;
}
};
enum VRML_COLOR_INDEX
{
VRML_COLOR_NONE = -1,
VRML_COLOR_PCB = 0,
VRML_COLOR_TRACK,
VRML_COLOR_SILK,
VRML_COLOR_TIN,
VRML_COLOR_LAST
};
static VRML_COLOR colors[VRML_COLOR_LAST];
static SGNODE* sgmaterial[VRML_COLOR_LAST] = { NULL };
class MODEL_VRML
{
private:
double m_layer_z[PCB_LAYER_ID_COUNT];
int m_iMaxSeg; // max. sides to a small circle
double m_arcMinLen, m_arcMaxLen; // min and max lengths of an arc chord
public:
IFSG_TRANSFORM m_OutputPCB;
VRML_LAYER m_holes;
VRML_LAYER m_board;
VRML_LAYER m_top_copper;
VRML_LAYER m_bot_copper;
VRML_LAYER m_top_silk;
VRML_LAYER m_bot_silk;
VRML_LAYER m_top_tin;
VRML_LAYER m_bot_tin;
VRML_LAYER m_plated_holes;
std::list< SGNODE* > m_components;
bool m_plainPCB;
double m_minLineWidth; // minimum width of a VRML line segment
double m_tx; // global translation along X
double m_ty; // global translation along Y
double m_brd_thickness; // depth of the PCB
LAYER_NUM m_text_layer;
int m_text_width;
MODEL_VRML() : m_OutputPCB( (SGNODE*) NULL )
{
for( unsigned i = 0; i < DIM( m_layer_z ); ++i )
m_layer_z[i] = 0;
m_holes.GetArcParams( m_iMaxSeg, m_arcMinLen, m_arcMaxLen );
// this default only makes sense if the output is in mm
m_brd_thickness = 1.6;
// pcb green
colors[ VRML_COLOR_PCB ] = VRML_COLOR( .07, .3, .12, .01, .03, .01,
0, 0, 0, 0.8, 0, 0.02 );
// track green
colors[ VRML_COLOR_TRACK ] = VRML_COLOR( .08, .5, .1, .01, .05, .01,
0, 0, 0, 0.8, 0, 0.02 );
// silkscreen white
colors[ VRML_COLOR_SILK ] = VRML_COLOR( .9, .9, .9, .1, .1, .1,
0, 0, 0, 0.9, 0, 0.02 );
// pad silver
colors[ VRML_COLOR_TIN ] = VRML_COLOR( .749, .756, .761, .749, .756, .761,
0, 0, 0, 0.8, 0, 0.8 );
m_plainPCB = false;
SetOffset( 0.0, 0.0 );
m_text_layer = F_Cu;
m_text_width = 1;
m_minLineWidth = MIN_VRML_LINEWIDTH;
}
~MODEL_VRML()
{
// destroy any unassociated material appearances
for( int j = 0; j < VRML_COLOR_LAST; ++j )
{
if( sgmaterial[j] && NULL == S3D::GetSGNodeParent( sgmaterial[j] ) )
S3D::DestroyNode( sgmaterial[j] );
sgmaterial[j] = NULL;
}
if( !m_components.empty() )
{
IFSG_TRANSFORM tmp( false );
for( auto i : m_components )
{
tmp.Attach( i );
tmp.SetParent( NULL );
}
m_components.clear();
m_OutputPCB.Destroy();
}
}
VRML_COLOR& GetColor( VRML_COLOR_INDEX aIndex )
{
return colors[aIndex];
}
void SetOffset( double aXoff, double aYoff )
{
m_tx = aXoff;
m_ty = -aYoff;
m_holes.SetVertexOffsets( aXoff, aYoff );
m_board.SetVertexOffsets( aXoff, aYoff );
m_top_copper.SetVertexOffsets( aXoff, aYoff );
m_bot_copper.SetVertexOffsets( aXoff, aYoff );
m_top_silk.SetVertexOffsets( aXoff, aYoff );
m_bot_silk.SetVertexOffsets( aXoff, aYoff );
m_top_tin.SetVertexOffsets( aXoff, aYoff );
m_bot_tin.SetVertexOffsets( aXoff, aYoff );
m_plated_holes.SetVertexOffsets( aXoff, aYoff );
}
double GetLayerZ( LAYER_NUM aLayer )
{
if( unsigned( aLayer ) >= DIM( m_layer_z ) )
return 0;
return m_layer_z[ aLayer ];
}
void SetLayerZ( LAYER_NUM aLayer, double aValue )
{
m_layer_z[aLayer] = aValue;
}
// set the scaling of the VRML world
bool SetScale( double aWorldScale )
{
if( aWorldScale < 0.001 || aWorldScale > 10.0 )
throw( std::runtime_error( "WorldScale out of range (valid range is 0.001 to 10.0)" ) );
m_OutputPCB.SetScale( aWorldScale * 2.54 );
WORLD_SCALE = aWorldScale * 2.54;
return true;
}
};
// static var. for dealing with text
static MODEL_VRML* model_vrml;
// select the VRML layer object to draw on; return true if
// a layer has been selected.
static bool GetLayer( MODEL_VRML& aModel, LAYER_NUM layer, VRML_LAYER** vlayer )
{
switch( layer )
{
case B_Cu:
*vlayer = &aModel.m_bot_copper;
break;
case F_Cu:
*vlayer = &aModel.m_top_copper;
break;
case B_SilkS:
*vlayer = &aModel.m_bot_silk;
break;
case F_SilkS:
*vlayer = &aModel.m_top_silk;
break;
default:
return false;
}
return true;
}
static void create_vrml_shell( IFSG_TRANSFORM& PcbOutput, VRML_COLOR_INDEX colorID,
VRML_LAYER* layer, double top_z, double bottom_z );
static void create_vrml_plane( IFSG_TRANSFORM& PcbOutput, VRML_COLOR_INDEX colorID,
VRML_LAYER* layer, double aHeight, bool aTopPlane );
static void write_triangle_bag( std::ostream& aOut_file, VRML_COLOR& aColor,
VRML_LAYER* aLayer, bool aPlane, bool aTop,
double aTop_z, double aBottom_z )
{
/* A lot of nodes are not required, but blender sometimes chokes
* without them */
static const char* shape_boiler[] =
{
"Transform {\n",
" children [\n",
" Group {\n",
" children [\n",
" Shape {\n",
" appearance Appearance {\n",
" material Material {\n",
0, // Material marker
" }\n",
" }\n",
" geometry IndexedFaceSet {\n",
" solid TRUE\n",
" coord Coordinate {\n",
" point [\n",
0, // Coordinates marker
" ]\n",
" }\n",
" coordIndex [\n",
0, // Index marker
" ]\n",
" }\n",
" }\n",
" ]\n",
" }\n",
" ]\n",
"}\n",
0 // End marker
};
int marker_found = 0, lineno = 0;
while( marker_found < 4 )
{
if( shape_boiler[lineno] )
aOut_file << shape_boiler[lineno];
else
{
marker_found++;
switch( marker_found )
{
case 1: // Material marker
aOut_file << " diffuseColor " << std::setprecision(3);
aOut_file << aColor.diffuse_red << " ";
aOut_file << aColor.diffuse_grn << " ";
aOut_file << aColor.diffuse_blu << "\n";
aOut_file << " specularColor ";
aOut_file << aColor.spec_red << " ";
aOut_file << aColor.spec_grn << " ";
aOut_file << aColor.spec_blu << "\n";
aOut_file << " emissiveColor ";
aOut_file << aColor.emit_red << " ";
aOut_file << aColor.emit_grn << " ";
aOut_file << aColor.emit_blu << "\n";
aOut_file << " ambientIntensity " << aColor.ambient << "\n";
aOut_file << " transparency " << aColor.transp << "\n";
aOut_file << " shininess " << aColor.shiny << "\n";
break;
case 2:
if( aPlane )
aLayer->WriteVertices( aTop_z, aOut_file, PRECISION );
else
aLayer->Write3DVertices( aTop_z, aBottom_z, aOut_file, PRECISION );
aOut_file << "\n";
break;
case 3:
if( aPlane )
aLayer->WriteIndices( aTop, aOut_file );
else
aLayer->Write3DIndices( aOut_file );
aOut_file << "\n";
break;
default:
break;
}
}
lineno++;
}
}
static void write_layers( MODEL_VRML& aModel, BOARD* aPcb,
const char* aFileName, OSTREAM* aOutputFile )
{
// VRML_LAYER board;
aModel.m_board.Tesselate( &aModel.m_holes );
double brdz = aModel.m_brd_thickness / 2.0
- ( Millimeter2iu( ART_OFFSET / 2.0 ) ) * BOARD_SCALE;
if( USE_INLINES )
{
write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_PCB ),
&aModel.m_board, false, false, brdz, -brdz );
}
else
{
create_vrml_shell( aModel.m_OutputPCB, VRML_COLOR_PCB, &aModel.m_board, brdz, -brdz );
}
if( aModel.m_plainPCB )
{
if( !USE_INLINES )
S3D::WriteVRML( aFileName, true, aModel.m_OutputPCB.GetRawPtr(), USE_DEFS, true );
return;
}
// VRML_LAYER m_top_copper;
aModel.m_top_copper.Tesselate( &aModel.m_holes );
if( USE_INLINES )
{
write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TRACK ),
&aModel.m_top_copper, true, true,
aModel.GetLayerZ( F_Cu ), 0 );
}
else
{
create_vrml_plane( aModel.m_OutputPCB, VRML_COLOR_TRACK, &aModel.m_top_copper,
aModel.GetLayerZ( F_Cu ), true );
}
// VRML_LAYER m_top_tin;
aModel.m_top_tin.Tesselate( &aModel.m_holes );
if( USE_INLINES )
{
write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TIN ),
&aModel.m_top_tin, true, true,
aModel.GetLayerZ( F_Cu ) + Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
0 );
}
else
{
create_vrml_plane( aModel.m_OutputPCB, VRML_COLOR_TIN, &aModel.m_top_tin,
aModel.GetLayerZ( F_Cu ) + Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
true );
}
// VRML_LAYER m_bot_copper;
aModel.m_bot_copper.Tesselate( &aModel.m_holes );
if( USE_INLINES )
{
write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TRACK ),
&aModel.m_bot_copper, true, false,
aModel.GetLayerZ( B_Cu ), 0 );
}
else
{
create_vrml_plane( aModel.m_OutputPCB, VRML_COLOR_TRACK, &aModel.m_bot_copper,
aModel.GetLayerZ( B_Cu ), false );
}
// VRML_LAYER m_bot_tin;
aModel.m_bot_tin.Tesselate( &aModel.m_holes );
if( USE_INLINES )
{
write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TIN ),
&aModel.m_bot_tin, true, false,
aModel.GetLayerZ( B_Cu )
- Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
0 );
}
else
{
create_vrml_plane( aModel.m_OutputPCB, VRML_COLOR_TIN, &aModel.m_bot_tin,
aModel.GetLayerZ( B_Cu ) - Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
false );
}
// VRML_LAYER PTH;
aModel.m_plated_holes.Tesselate( NULL, true );
if( USE_INLINES )
{
write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_TIN ),
&aModel.m_plated_holes, false, false,
aModel.GetLayerZ( F_Cu ) + Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
aModel.GetLayerZ( B_Cu ) - Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE );
}
else
{
create_vrml_shell( aModel.m_OutputPCB, VRML_COLOR_TIN, &aModel.m_plated_holes,
aModel.GetLayerZ( F_Cu ) + Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE,
aModel.GetLayerZ( B_Cu ) - Millimeter2iu( ART_OFFSET / 2.0 ) * BOARD_SCALE );
}
// VRML_LAYER m_top_silk;
aModel.m_top_silk.Tesselate( &aModel.m_holes );
if( USE_INLINES )
{
write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_SILK ), &aModel.m_top_silk,
true, true, aModel.GetLayerZ( F_SilkS ), 0 );
}
else
{
create_vrml_plane( aModel.m_OutputPCB, VRML_COLOR_SILK, &aModel.m_top_silk,
aModel.GetLayerZ( F_SilkS ), true );
}
// VRML_LAYER m_bot_silk;
aModel.m_bot_silk.Tesselate( &aModel.m_holes );
if( USE_INLINES )
{
write_triangle_bag( *aOutputFile, aModel.GetColor( VRML_COLOR_SILK ), &aModel.m_bot_silk,
true, false, aModel.GetLayerZ( B_SilkS ), 0 );
}
else
{
create_vrml_plane( aModel.m_OutputPCB, VRML_COLOR_SILK, &aModel.m_bot_silk,
aModel.GetLayerZ( B_SilkS ), false );
}
if( !USE_INLINES )
S3D::WriteVRML( aFileName, true, aModel.m_OutputPCB.GetRawPtr(), true, true );
}
static void compute_layer_Zs( MODEL_VRML& aModel, BOARD* pcb )
{
int copper_layers = pcb->GetCopperLayerCount();
// We call it 'layer' thickness, but it's the whole board thickness!
aModel.m_brd_thickness = pcb->GetDesignSettings().GetBoardThickness() * BOARD_SCALE;
double half_thickness = aModel.m_brd_thickness / 2;
// Compute each layer's Z value, more or less like the 3d view
for( LSEQ seq = LSET::AllCuMask().Seq(); seq; ++seq )
{
PCB_LAYER_ID i = *seq;
if( i < copper_layers )
aModel.SetLayerZ( i, half_thickness - aModel.m_brd_thickness * i / (copper_layers - 1) );
else
aModel.SetLayerZ( i, - half_thickness ); // bottom layer
}
/* To avoid rounding interference, we apply an epsilon to each
* successive layer */
double epsilon_z = Millimeter2iu( ART_OFFSET ) * BOARD_SCALE;
aModel.SetLayerZ( B_Paste, -half_thickness - epsilon_z * 4 );
aModel.SetLayerZ( B_Adhes, -half_thickness - epsilon_z * 3 );
aModel.SetLayerZ( B_SilkS, -half_thickness - epsilon_z * 2 );
aModel.SetLayerZ( B_Mask, -half_thickness - epsilon_z );
aModel.SetLayerZ( F_Mask, half_thickness + epsilon_z );
aModel.SetLayerZ( F_SilkS, half_thickness + epsilon_z * 2 );
aModel.SetLayerZ( F_Adhes, half_thickness + epsilon_z * 3 );
aModel.SetLayerZ( F_Paste, half_thickness + epsilon_z * 4 );
aModel.SetLayerZ( Dwgs_User, half_thickness + epsilon_z * 5 );
aModel.SetLayerZ( Cmts_User, half_thickness + epsilon_z * 6 );
aModel.SetLayerZ( Eco1_User, half_thickness + epsilon_z * 7 );
aModel.SetLayerZ( Eco2_User, half_thickness + epsilon_z * 8 );
aModel.SetLayerZ( Edge_Cuts, 0 );
}
static void export_vrml_line( MODEL_VRML& aModel, LAYER_NUM layer,
double startx, double starty,
double endx, double endy, double width )
{
VRML_LAYER* vlayer;
if( !GetLayer( aModel, layer, &vlayer ) )
return;
if( width < aModel.m_minLineWidth)
width = aModel.m_minLineWidth;
starty = -starty;
endy = -endy;
double angle = atan2( endy - starty, endx - startx ) * 180.0 / M_PI;
double length = Distance( startx, starty, endx, endy ) + width;
double cx = ( startx + endx ) / 2.0;
double cy = ( starty + endy ) / 2.0;
if( !vlayer->AddSlot( cx, cy, length, width, angle, false ) )
throw( std::runtime_error( vlayer->GetError() ) );
}
static void export_vrml_circle( MODEL_VRML& aModel, LAYER_NUM layer,
double startx, double starty,
double endx, double endy, double width )
{
VRML_LAYER* vlayer;
if( !GetLayer( aModel, layer, &vlayer ) )
return;
if( width < aModel.m_minLineWidth )
width = aModel.m_minLineWidth;
starty = -starty;
endy = -endy;
double hole, radius;
radius = Distance( startx, starty, endx, endy ) + ( width / 2);
hole = radius - width;
if( !vlayer->AddCircle( startx, starty, radius, false ) )
throw( std::runtime_error( vlayer->GetError() ) );
if( hole > 0.0001 )
{
if( !vlayer->AddCircle( startx, starty, hole, true ) )
throw( std::runtime_error( vlayer->GetError() ) );
}
}
static void export_vrml_arc( MODEL_VRML& aModel, LAYER_NUM layer,
double centerx, double centery,
double arc_startx, double arc_starty,
double width, double arc_angle )
{
VRML_LAYER* vlayer;
if( !GetLayer( aModel, layer, &vlayer ) )
return;
if( width < aModel.m_minLineWidth )
width = aModel.m_minLineWidth;
centery = -centery;
arc_starty = -arc_starty;
if( !vlayer->AddArc( centerx, centery, arc_startx, arc_starty, width, -arc_angle, false ) )
throw( std::runtime_error( vlayer->GetError() ) );
}
static void export_vrml_polygon( MODEL_VRML& aModel, LAYER_NUM layer,
DRAWSEGMENT *aOutline, double aOrientation, wxPoint aPos )
{
const int circleSegmentsCount = 32;
if( aOutline->IsPolyShapeValid() )
{
SHAPE_POLY_SET shape = aOutline->GetPolyShape();
VRML_LAYER* vlayer;
if( !GetLayer( aModel, layer, &vlayer ) )
return;
if( aOutline->GetWidth() )
{
shape.Inflate( aOutline->GetWidth()/2, circleSegmentsCount );
shape.Fracture( SHAPE_POLY_SET::PM_STRICTLY_SIMPLE );
}
shape.Rotate( -aOrientation, VECTOR2I( 0, 0 ) );
shape.Move( aPos );
const SHAPE_LINE_CHAIN& outline = shape.COutline( 0 );
int seg = vlayer->NewContour();
for( int j = 0; j < outline.PointCount(); j++ )
{
if( !vlayer->AddVertex( seg, outline.CPoint( j ).x * BOARD_SCALE,
-outline.CPoint( j ).y * BOARD_SCALE ) )
throw( std::runtime_error( vlayer->GetError() ) );
}
vlayer->EnsureWinding( seg, false );
}
}
static void export_vrml_drawsegment( MODEL_VRML& aModel, DRAWSEGMENT* drawseg )
{
LAYER_NUM layer = drawseg->GetLayer();
double w = drawseg->GetWidth() * BOARD_SCALE;
double x = drawseg->GetStart().x * BOARD_SCALE;
double y = drawseg->GetStart().y * BOARD_SCALE;
double xf = drawseg->GetEnd().x * BOARD_SCALE;
double yf = drawseg->GetEnd().y * BOARD_SCALE;
double r = sqrt( pow( x - xf, 2 ) + pow( y - yf, 2 ) );
// Items on the edge layer are handled elsewhere; just return
if( layer == Edge_Cuts )
return;
switch( drawseg->GetShape() )
{
case S_ARC:
export_vrml_arc( aModel, layer,
(double) drawseg->GetCenter().x * BOARD_SCALE,
(double) drawseg->GetCenter().y * BOARD_SCALE,
(double) drawseg->GetArcStart().x * BOARD_SCALE,
(double) drawseg->GetArcStart().y * BOARD_SCALE,
w, drawseg->GetAngle() / 10 );
break;
case S_CIRCLE:
// Break circles into two 180 arcs to prevent the vrml hole from obscuring objects
// within the hole area of the circle.
export_vrml_arc( aModel, layer, x, y, x, y-r, w, 180.0 );
export_vrml_arc( aModel, layer, x, y, x, y+r, w, 180.0 );
break;
case S_POLYGON:
export_vrml_polygon( aModel, layer, drawseg, 0.0, wxPoint( 0, 0 ) );
break;
default:
export_vrml_line( aModel, layer, x, y, xf, yf, w );
break;
}
}
/* C++ doesn't have closures and neither continuation forms... this is
* for coupling the vrml_text_callback with the common parameters */
static void vrml_text_callback( int x0, int y0, int xf, int yf, void* aData )
{
LAYER_NUM m_text_layer = model_vrml->m_text_layer;
int m_text_width = model_vrml->m_text_width;
export_vrml_line( *model_vrml, m_text_layer,
x0 * BOARD_SCALE, y0 * BOARD_SCALE,
xf * BOARD_SCALE, yf * BOARD_SCALE,
m_text_width * BOARD_SCALE );
}
static void export_vrml_pcbtext( MODEL_VRML& aModel, TEXTE_PCB* text )
{
model_vrml->m_text_layer = text->GetLayer();
model_vrml->m_text_width = text->GetThickness();
wxSize size = text->GetTextSize();
if( text->IsMirrored() )
size.x = -size.x;
COLOR4D color = COLOR4D::BLACK; // not actually used, but needed by DrawGraphicText
if( text->IsMultilineAllowed() )
{
wxArrayString strings_list;
wxStringSplit( text->GetShownText(), strings_list, '\n' );
std::vector<wxPoint> positions;
positions.reserve( strings_list.Count() );
text->GetPositionsOfLinesOfMultilineText( positions, strings_list.Count() );
for( unsigned ii = 0; ii < strings_list.Count(); ii++ )
{
wxString& txt = strings_list.Item( ii );
DrawGraphicText( NULL, NULL, positions[ii], color,
txt, text->GetTextAngle(), size,
text->GetHorizJustify(), text->GetVertJustify(),
text->GetThickness(), text->IsItalic(),
true,
vrml_text_callback );
}
}
else
{
DrawGraphicText( NULL, NULL, text->GetTextPos(), color,
text->GetShownText(), text->GetTextAngle(), size,
text->GetHorizJustify(), text->GetVertJustify(),
text->GetThickness(), text->IsItalic(),
true,
vrml_text_callback );
}
}
static void export_vrml_drawings( MODEL_VRML& aModel, BOARD* pcb )
{
// draw graphic items
for( auto drawing : pcb->Drawings() )
{
PCB_LAYER_ID layer = drawing->GetLayer();
if( layer != F_Cu && layer != B_Cu && layer != B_SilkS && layer != F_SilkS )
continue;
switch( drawing->Type() )
{
case PCB_LINE_T:
export_vrml_drawsegment( aModel, (DRAWSEGMENT*) drawing );
break;
case PCB_TEXT_T:
export_vrml_pcbtext( aModel, (TEXTE_PCB*) drawing );
break;
default:
break;
}
}
}
// board edges and cutouts
static void export_vrml_board( MODEL_VRML& aModel, BOARD* aPcb )
{
SHAPE_POLY_SET pcbOutlines; // stores the board main outlines
wxString msg;
if( !aPcb->GetBoardPolygonOutlines( pcbOutlines, &msg ) )
{
msg << "\n\n" <<
_( "Unable to calculate the board outlines; fall back to using the board boundary box." );
wxMessageBox( msg );
}
int seg;
for( int cnt = 0; cnt < pcbOutlines.OutlineCount(); cnt++ )
{
const SHAPE_LINE_CHAIN& outline = pcbOutlines.COutline( cnt );
seg = aModel.m_board.NewContour();
for( int j = 0; j < outline.PointCount(); j++ )
{
aModel.m_board.AddVertex( seg, (double)outline.CPoint(j).x * BOARD_SCALE,
-((double)outline.CPoint(j).y * BOARD_SCALE ) );
}
aModel.m_board.EnsureWinding( seg, false );
// Generate holes:
for( int ii = 0; ii < pcbOutlines.HoleCount( cnt ); ii++ )
{
const SHAPE_LINE_CHAIN& hole = pcbOutlines.Hole( cnt, ii );
seg = aModel.m_holes.NewContour();
if( seg < 0 )
{
msg << "\n\n" <<
_( "VRML Export Failed: Could not add holes to contours." );
wxMessageBox( msg );
return;
}
for( int j = 0; j < hole.PointCount(); j++ )
{
aModel.m_holes.AddVertex( seg, (double)hole.CPoint(j).x * BOARD_SCALE,
-((double)hole.CPoint(j).y * BOARD_SCALE ) );
}
aModel.m_holes.EnsureWinding( seg, true );
}
}
}
static void export_round_padstack( MODEL_VRML& aModel, BOARD* pcb,
double x, double y, double r,
LAYER_NUM bottom_layer, LAYER_NUM top_layer,
double hole )
{
LAYER_NUM layer = top_layer;
bool thru = true;
// if not a thru hole do not put a hole in the board
if( top_layer != F_Cu || bottom_layer != B_Cu )
thru = false;
if( thru && hole > 0 )
aModel.m_holes.AddCircle( x, -y, hole, true );
if( aModel.m_plainPCB )
return;
while( 1 )
{
if( layer == B_Cu )
{
aModel.m_bot_copper.AddCircle( x, -y, r );
if( hole > 0 && !thru )
aModel.m_bot_copper.AddCircle( x, -y, hole, true );
}
else if( layer == F_Cu )
{
aModel.m_top_copper.AddCircle( x, -y, r );
if( hole > 0 && !thru )
aModel.m_top_copper.AddCircle( x, -y, hole, true );
}
if( layer == bottom_layer )
break;
layer = bottom_layer;
}
}
static void export_vrml_via( MODEL_VRML& aModel, BOARD* aPcb, const VIA* aVia )
{
double x, y, r, hole;
PCB_LAYER_ID top_layer, bottom_layer;
hole = aVia->GetDrillValue() * BOARD_SCALE / 2.0;
r = aVia->GetWidth() * BOARD_SCALE / 2.0;
x = aVia->GetStart().x * BOARD_SCALE;
y = aVia->GetStart().y * BOARD_SCALE;
aVia->LayerPair( &top_layer, &bottom_layer );
// do not render a buried via
if( top_layer != F_Cu && bottom_layer != B_Cu )
return;
// Export the via padstack
export_round_padstack( aModel, aPcb, x, y, r, bottom_layer, top_layer, hole );
}
static void export_vrml_tracks( MODEL_VRML& aModel, BOARD* pcb )
{
for( TRACK* track = pcb->m_Track; track; track = track->Next() )
{
if( track->Type() == PCB_VIA_T )
{
export_vrml_via( aModel, pcb, (const VIA*) track );
}
else if( ( track->GetLayer() == B_Cu || track->GetLayer() == F_Cu )
&& !aModel.m_plainPCB )
export_vrml_line( aModel, track->GetLayer(),
track->GetStart().x * BOARD_SCALE,
track->GetStart().y * BOARD_SCALE,
track->GetEnd().x * BOARD_SCALE,
track->GetEnd().y * BOARD_SCALE,
track->GetWidth() * BOARD_SCALE );
}
}
static void export_vrml_zones( MODEL_VRML& aModel, BOARD* aPcb )
{
for( int ii = 0; ii < aPcb->GetAreaCount(); ii++ )
{
ZONE_CONTAINER* zone = aPcb->GetArea( ii );
VRML_LAYER* vl;
if( !GetLayer( aModel, zone->GetLayer(), &vl ) )
continue;
// fixme: this modifies the board where it shouldn't, but I don't have the time
// to clean this up - TW
if( !zone->IsFilled() )
{
ZONE_FILLER filler( aPcb );
zone->SetFillMode( ZFM_POLYGONS ); // use filled polygons
filler.Fill( { zone } );
}
const SHAPE_POLY_SET& poly = zone->GetFilledPolysList();
for( int i = 0; i < poly.OutlineCount(); i++ )
{
const SHAPE_LINE_CHAIN& outline = poly.COutline( i );
int seg = vl->NewContour();
for( int j = 0; j < outline.PointCount(); j++ )
{
if( !vl->AddVertex( seg, (double)outline.CPoint( j ).x * BOARD_SCALE,
-((double)outline.CPoint( j ).y * BOARD_SCALE ) ) )
throw( std::runtime_error( vl->GetError() ) );
}
vl->EnsureWinding( seg, false );
}
}
}
static void export_vrml_text_module( TEXTE_MODULE* module )
{
if( module->IsVisible() )
{
wxSize size = module->GetTextSize();
if( module->IsMirrored() )
size.x = -size.x; // Text is mirrored
model_vrml->m_text_layer = module->GetLayer();
model_vrml->m_text_width = module->GetThickness();
DrawGraphicText( NULL, NULL, module->GetTextPos(), BLACK,
module->GetShownText(), module->GetDrawRotation(), size,
module->GetHorizJustify(), module->GetVertJustify(),
module->GetThickness(), module->IsItalic(),
true,
vrml_text_callback );
}
}
static void export_vrml_edge_module( MODEL_VRML& aModel, EDGE_MODULE* aOutline,
MODULE* aModule )
{
LAYER_NUM layer = aOutline->GetLayer();
double x = aOutline->GetStart().x * BOARD_SCALE;
double y = aOutline->GetStart().y * BOARD_SCALE;
double xf = aOutline->GetEnd().x * BOARD_SCALE;
double yf = aOutline->GetEnd().y * BOARD_SCALE;
double w = aOutline->GetWidth() * BOARD_SCALE;
switch( aOutline->GetShape() )
{
case S_SEGMENT:
export_vrml_line( aModel, layer, x, y, xf, yf, w );
break;
case S_ARC:
export_vrml_arc( aModel, layer, x, y, xf, yf, w, aOutline->GetAngle() / 10 );
break;
case S_CIRCLE:
export_vrml_circle( aModel, layer, x, y, xf, yf, w );
break;
case S_POLYGON:
export_vrml_polygon( aModel, layer, aOutline, aModule->GetOrientationRadians(),
aModule->GetPosition() );
break;
default:
break;
}
}
static void export_vrml_padshape( MODEL_VRML& aModel, VRML_LAYER* aTinLayer, D_PAD* aPad )
{
// The (maybe offset) pad position
wxPoint pad_pos = aPad->ShapePos();
double pad_x = pad_pos.x * BOARD_SCALE;
double pad_y = pad_pos.y * BOARD_SCALE;
wxSize pad_delta = aPad->GetDelta();
double pad_dx = pad_delta.x * BOARD_SCALE / 2.0;
double pad_dy = pad_delta.y * BOARD_SCALE / 2.0;
double pad_w = aPad->GetSize().x * BOARD_SCALE / 2.0;
double pad_h = aPad->GetSize().y * BOARD_SCALE / 2.0;
switch( aPad->GetShape() )
{
case PAD_SHAPE_CIRCLE:
if( !aTinLayer->AddCircle( pad_x, -pad_y, pad_w, false ) )
throw( std::runtime_error( aTinLayer->GetError() ) );
break;
case PAD_SHAPE_OVAL:
if( !aTinLayer->AddSlot( pad_x, -pad_y, pad_w * 2.0, pad_h * 2.0,
aPad->GetOrientation()/10.0, false ) )
throw( std::runtime_error( aTinLayer->GetError() ) );
break;
case PAD_SHAPE_ROUNDRECT:
{
SHAPE_POLY_SET polySet;
int segmentToCircleCount = 32;
const int corner_radius = aPad->GetRoundRectCornerRadius( aPad->GetSize() );
TransformRoundRectToPolygon( polySet, wxPoint( 0, 0 ), aPad->GetSize(),
0.0, corner_radius, segmentToCircleCount );
std::vector< wxRealPoint > cornerList;
// TransformRoundRectToPolygon creates only one convex polygon
SHAPE_LINE_CHAIN poly( polySet.Outline( 0 ) );
for( int ii = 0; ii < poly.PointCount(); ++ii )
cornerList.push_back( wxRealPoint( poly.Point( ii ).x * BOARD_SCALE,
-poly.Point( ii ).y * BOARD_SCALE ) );
// Close polygon
cornerList.push_back( cornerList[0] );
if( !aTinLayer->AddPolygon( cornerList, pad_x, -pad_y, aPad->GetOrientation() ) )
throw( std::runtime_error( aTinLayer->GetError() ) );
break;
}
case PAD_SHAPE_CUSTOM:
{
SHAPE_POLY_SET polySet;
int segmentToCircleCount = 32;
std::vector< wxRealPoint > cornerList;
aPad->MergePrimitivesAsPolygon( &polySet, segmentToCircleCount );
for( int cnt = 0; cnt < polySet.OutlineCount(); ++cnt )
{
SHAPE_LINE_CHAIN& poly = polySet.Outline( cnt );
cornerList.clear();
for( int ii = 0; ii < poly.PointCount(); ++ii )
cornerList.push_back( wxRealPoint( poly.Point( ii ).x * BOARD_SCALE,
-poly.Point( ii ).y * BOARD_SCALE ) );
// Close polygon
cornerList.push_back( cornerList[0] );
if( !aTinLayer->AddPolygon( cornerList, pad_x, -pad_y, aPad->GetOrientation() ) )
throw( std::runtime_error( aTinLayer->GetError() ) );
}
break;
}
case PAD_SHAPE_RECT:
// Just to be sure :D
pad_dx = 0;
pad_dy = 0;
case PAD_SHAPE_TRAPEZOID:
{
double coord[8] =
{
-pad_w + pad_dy, -pad_h - pad_dx,
-pad_w - pad_dy, pad_h + pad_dx,
+pad_w - pad_dy, -pad_h + pad_dx,
+pad_w + pad_dy, pad_h - pad_dx
};
for( int i = 0; i < 4; i++ )
{
RotatePoint( &coord[i * 2], &coord[i * 2 + 1], aPad->GetOrientation() );
coord[i * 2] += pad_x;
coord[i * 2 + 1] += pad_y;
}
int lines;
lines = aTinLayer->NewContour();
if( lines < 0 )
throw( std::runtime_error( aTinLayer->GetError() ) );
if( !aTinLayer->AddVertex( lines, coord[0], -coord[1] ) )
throw( std::runtime_error( aTinLayer->GetError() ) );
if( !aTinLayer->AddVertex( lines, coord[4], -coord[5] ) )
throw( std::runtime_error( aTinLayer->GetError() ) );
if( !aTinLayer->AddVertex( lines, coord[6], -coord[7] ) )
throw( std::runtime_error( aTinLayer->GetError() ) );
if( !aTinLayer->AddVertex( lines, coord[2], -coord[3] ) )
throw( std::runtime_error( aTinLayer->GetError() ) );
if( !aTinLayer->EnsureWinding( lines, false ) )
throw( std::runtime_error( aTinLayer->GetError() ) );
break;
}
default:
break;
}
}
static void export_vrml_pad( MODEL_VRML& aModel, BOARD* aPcb, D_PAD* aPad )
{
double hole_drill_w = (double) aPad->GetDrillSize().x * BOARD_SCALE / 2.0;
double hole_drill_h = (double) aPad->GetDrillSize().y * BOARD_SCALE / 2.0;
double hole_drill = std::min( hole_drill_w, hole_drill_h );
double hole_x = aPad->GetPosition().x * BOARD_SCALE;
double hole_y = aPad->GetPosition().y * BOARD_SCALE;
// Export the hole on the edge layer
if( hole_drill > 0 )
{
bool pth = false;
if( ( aPad->GetAttribute() != PAD_ATTRIB_HOLE_NOT_PLATED )
&& !aModel.m_plainPCB )
pth = true;
if( aPad->GetDrillShape() == PAD_DRILL_SHAPE_OBLONG )
{
// Oblong hole (slot)
if( pth )
{
aModel.m_holes.AddSlot( hole_x, -hole_y, hole_drill_w * 2.0 + PLATE_OFFSET,
hole_drill_h * 2.0 + PLATE_OFFSET,
aPad->GetOrientation()/10.0, true, true );
aModel.m_plated_holes.AddSlot( hole_x, -hole_y,
hole_drill_w * 2.0, hole_drill_h * 2.0,
aPad->GetOrientation()/10.0, true, false );
}
else
{
aModel.m_holes.AddSlot( hole_x, -hole_y, hole_drill_w * 2.0, hole_drill_h * 2.0,
aPad->GetOrientation()/10.0, true, false );
}
}
else
{
// Drill a round hole
if( pth )
{
aModel.m_holes.AddCircle( hole_x, -hole_y, hole_drill + PLATE_OFFSET, true, true );
aModel.m_plated_holes.AddCircle( hole_x, -hole_y, hole_drill, true, false );
}
else
{
aModel.m_holes.AddCircle( hole_x, -hole_y, hole_drill, true, false );
}
}
}
if( aModel.m_plainPCB )
return;
// The pad proper, on the selected layers
LSET layer_mask = aPad->GetLayerSet();
if( layer_mask[B_Cu] )
{
if( layer_mask[B_Mask] )
export_vrml_padshape( aModel, &aModel.m_bot_tin, aPad );
else
export_vrml_padshape( aModel, &aModel.m_bot_copper, aPad );
}
if( layer_mask[F_Cu] )
{
if( layer_mask[F_Mask] )
export_vrml_padshape( aModel, &aModel.m_top_tin, aPad );
else
export_vrml_padshape( aModel, &aModel.m_top_copper, aPad );
}
}
// From axis/rot to quaternion
static void build_quat( double x, double y, double z, double a, double q[4] )
{
double sina = sin( a / 2 );
q[0] = x * sina;
q[1] = y * sina;
q[2] = z * sina;
q[3] = cos( a / 2 );
}
// From quaternion to axis/rot
static void from_quat( double q[4], double rot[4] )
{
rot[3] = acos( q[3] ) * 2;
for( int i = 0; i < 3; i++ )
{
rot[i] = q[i] / sin( rot[3] / 2 );
}
}
// Quaternion composition
static void compose_quat( double q1[4], double q2[4], double qr[4] )
{
double tmp[4];
tmp[0] = q2[3] * q1[0] + q2[0] * q1[3] + q2[1] * q1[2] - q2[2] * q1[1];
tmp[1] = q2[3] * q1[1] + q2[1] * q1[3] + q2[2] * q1[0] - q2[0] * q1[2];
tmp[2] = q2[3] * q1[2] + q2[2] * q1[3] + q2[0] * q1[1] - q2[1] * q1[0];
tmp[3] = q2[3] * q1[3] - q2[0] * q1[0] - q2[1] * q1[1] - q2[2] * q1[2];
qr[0] = tmp[0];
qr[1] = tmp[1];
qr[2] = tmp[2];
qr[3] = tmp[3];
}
static void export_vrml_module( MODEL_VRML& aModel, BOARD* aPcb,
MODULE* aModule, std::ostream* aOutputFile )
{
if( !aModel.m_plainPCB )
{
// Reference and value
if( aModule->Reference().IsVisible() )
export_vrml_text_module( &aModule->Reference() );
if( aModule->Value().IsVisible() )
export_vrml_text_module( &aModule->Value() );
// Export module edges
for( EDA_ITEM* item = aModule->GraphicalItemsList(); item; item = item->Next() )
{
switch( item->Type() )
{
case PCB_MODULE_TEXT_T:
export_vrml_text_module( static_cast<TEXTE_MODULE*>( item ) );
break;
case PCB_MODULE_EDGE_T:
export_vrml_edge_module( aModel, static_cast<EDGE_MODULE*>( item ),
aModule );
break;
default:
break;
}
}
}
// Export pads
for( D_PAD* pad = aModule->PadsList(); pad; pad = pad->Next() )
export_vrml_pad( aModel, aPcb, pad );
bool isFlipped = aModule->GetLayer() == B_Cu;
// Export the object VRML model(s)
auto sM = aModule->Models().begin();
auto eM = aModule->Models().end();
wxFileName subdir( SUBDIR_3D, "" );
while( sM != eM )
{
SGNODE* mod3d = (SGNODE*) cache->Load( sM->m_Filename );
if( NULL == mod3d )
{
++sM;
continue;
}
/* Calculate 3D shape rotation:
* this is the rotation parameters, with an additional 180 deg rotation
* for footprints that are flipped
* When flipped, axis rotation is the horizontal axis (X axis)
*/
double rotx = -sM->m_Rotation.x;
double roty = -sM->m_Rotation.y;
double rotz = -sM->m_Rotation.z;
if( isFlipped )
{
rotx += 180.0;
roty = -roty;
rotz = -rotz;
}
// Do some quaternion munching
double q1[4], q2[4], rot[4];
build_quat( 1, 0, 0, DEG2RAD( rotx ), q1 );
build_quat( 0, 1, 0, DEG2RAD( roty ), q2 );
compose_quat( q1, q2, q1 );
build_quat( 0, 0, 1, DEG2RAD( rotz ), q2 );
compose_quat( q1, q2, q1 );
// Note here aModule->GetOrientation() is in 0.1 degrees,
// so module rotation has to be converted to radians
build_quat( 0, 0, 1, DECIDEG2RAD( aModule->GetOrientation() ), q2 );
compose_quat( q1, q2, q1 );
from_quat( q1, rot );
double offsetFactor = 1000.0f * IU_PER_MILS / 25.4f;
// adjust 3D shape local offset position
// they are given in mm, so they are converted in board IU.
double offsetx = sM->m_Offset.x * offsetFactor;
double offsety = sM->m_Offset.y * offsetFactor;
double offsetz = sM->m_Offset.z * offsetFactor;
if( isFlipped )
offsetz = -offsetz;
else // In normal mode, Y axis is reversed in Pcbnew.
offsety = -offsety;
RotatePoint( &offsetx, &offsety, aModule->GetOrientation() );
SGPOINT trans;
trans.x = ( offsetx + aModule->GetPosition().x ) * BOARD_SCALE + aModel.m_tx;
trans.y = -(offsety + aModule->GetPosition().y) * BOARD_SCALE - aModel.m_ty;
trans.z = (offsetz * BOARD_SCALE ) + aModel.GetLayerZ( aModule->GetLayer() );
if( USE_INLINES )
{
wxFileName srcFile = cache->GetResolver()->ResolvePath( sM->m_Filename );
wxFileName dstFile;
dstFile.SetPath( SUBDIR_3D );
dstFile.SetName( srcFile.GetName() );
dstFile.SetExt( "wrl" );
// copy the file if necessary
wxDateTime srcModTime = srcFile.GetModificationTime();
wxDateTime destModTime = srcModTime;
destModTime.SetToCurrent();
if( dstFile.FileExists() )
destModTime = dstFile.GetModificationTime();
if( srcModTime != destModTime )
{
wxLogDebug( "Copying 3D model %s to %s.",
GetChars( srcFile.GetFullPath() ),
GetChars( dstFile.GetFullPath() ) );
wxString fileExt = srcFile.GetExt();
fileExt.LowerCase();
// copy VRML models and use the scenegraph library to
// translate other model types
if( fileExt == "wrl" )
{
if( !wxCopyFile( srcFile.GetFullPath(), dstFile.GetFullPath() ) )
continue;
}
else
{
if( !S3D::WriteVRML( dstFile.GetFullPath().ToUTF8(), true, mod3d, USE_DEFS, true ) )
continue;
}
}
(*aOutputFile) << "Transform {\n";
// only write a rotation if it is >= 0.1 deg
if( std::abs( rot[3] ) > 0.0001745 )
{
(*aOutputFile) << " rotation " << std::setprecision( 5 );
(*aOutputFile) << rot[0] << " " << rot[1] << " " << rot[2] << " " << rot[3] << "\n";
}
(*aOutputFile) << " translation " << std::setprecision( PRECISION );
(*aOutputFile) << trans.x << " ";
(*aOutputFile) << trans.y << " ";
(*aOutputFile) << trans.z << "\n";
(*aOutputFile) << " scale ";
(*aOutputFile) << sM->m_Scale.x << " ";
(*aOutputFile) << sM->m_Scale.y << " ";
(*aOutputFile) << sM->m_Scale.z << "\n";
(*aOutputFile) << " children [\n Inline {\n url \"";
if( USE_RELPATH )
{
wxFileName tmp = dstFile;
tmp.SetExt( "" );
tmp.SetName( "" );
tmp.RemoveLastDir();
dstFile.MakeRelativeTo( tmp.GetPath() );
}
wxString fn = dstFile.GetFullPath();
fn.Replace( "\\", "/" );
(*aOutputFile) << TO_UTF8( fn ) << "\"\n } ]\n";
(*aOutputFile) << " }\n";
}
else
{
IFSG_TRANSFORM* modelShape = new IFSG_TRANSFORM( aModel.m_OutputPCB.GetRawPtr() );
// only write a rotation if it is >= 0.1 deg
if( std::abs( rot[3] ) > 0.0001745 )
modelShape->SetRotation( SGVECTOR( rot[0], rot[1], rot[2] ), rot[3] );
modelShape->SetTranslation( trans );
modelShape->SetScale( SGPOINT( sM->m_Scale.x, sM->m_Scale.y, sM->m_Scale.z ) );
if( NULL == S3D::GetSGNodeParent( mod3d ) )
{
aModel.m_components.push_back( mod3d );
modelShape->AddChildNode( mod3d );
}
else
{
modelShape->AddRefNode( mod3d );
}
}
++sM;
}
}
bool PCB_EDIT_FRAME::ExportVRML_File( const wxString& aFullFileName, double aMMtoWRMLunit,
bool aExport3DFiles, bool aUseRelativePaths,
bool aUsePlainPCB, const wxString& a3D_Subdir,
double aXRef, double aYRef )
{
BOARD* pcb = GetBoard();
bool ok = true;
USE_INLINES = aExport3DFiles;
USE_DEFS = true;
USE_RELPATH = aUseRelativePaths;
cache = Prj().Get3DCacheManager();
PROJ_DIR = Prj().GetProjectPath();
SUBDIR_3D = a3D_Subdir;
MODEL_VRML model3d;
model_vrml = &model3d;
model3d.SetScale( aMMtoWRMLunit );
if( USE_INLINES )
{
BOARD_SCALE = MM_PER_IU / 2.54;
model3d.SetOffset( -aXRef / 2.54, aYRef / 2.54 );
}
else
{
BOARD_SCALE = MM_PER_IU;
model3d.SetOffset( -aXRef, aYRef );
}
// plain PCB or else PCB with copper and silkscreen
model3d.m_plainPCB = aUsePlainPCB;
try
{
// Preliminary computation: the z value for each layer
compute_layer_Zs(model3d, pcb);
// board edges and cutouts
export_vrml_board(model3d, pcb);
// Drawing and text on the board
if( !aUsePlainPCB )
export_vrml_drawings( model3d, pcb );
// Export vias and trackage
export_vrml_tracks( model3d, pcb );
// Export zone fills
if( !aUsePlainPCB )
export_vrml_zones( model3d, pcb);
if( USE_INLINES )
{
// check if the 3D Subdir exists - create if not
wxFileName subdir( SUBDIR_3D, "" );
if( ! subdir.DirExists() )
{
if( !wxDir::Make( subdir.GetFullPath() ) )
throw( std::runtime_error( "Could not create 3D model subdirectory" ) );
}
OPEN_OSTREAM( output_file, TO_UTF8( aFullFileName ) );
if( output_file.fail() )
{
std::ostringstream ostr;
ostr << "Could not open file '" << TO_UTF8( aFullFileName ) << "'";
throw( std::runtime_error( ostr.str().c_str() ) );
}
output_file.imbue( std::locale( "C" ) );
// Begin with the usual VRML boilerplate
wxString fn = aFullFileName;
fn.Replace( "\\" , "/" );
output_file << "#VRML V2.0 utf8\n";
output_file << "WorldInfo {\n";
output_file << " title \"" << TO_UTF8( fn ) << " - Generated by Pcbnew\"\n";
output_file << "}\n";
output_file << "Transform {\n";
output_file << " scale " << std::setprecision( PRECISION );
output_file << WORLD_SCALE << " ";
output_file << WORLD_SCALE << " ";
output_file << WORLD_SCALE << "\n";
output_file << " children [\n";
// Export footprints
for( MODULE* module = pcb->m_Modules; module != 0; module = module->Next() )
export_vrml_module( model3d, pcb, module, &output_file );
// write out the board and all layers
write_layers( model3d, pcb, TO_UTF8( aFullFileName ), &output_file );
// Close the outer 'transform' node
output_file << "]\n}\n";
CLOSE_STREAM( output_file );
}
else
{
// Export footprints
for( MODULE* module = pcb->m_Modules; module != 0; module = module->Next() )
export_vrml_module( model3d, pcb, module, NULL );
// write out the board and all layers
write_layers( model3d, pcb, TO_UTF8( aFullFileName ), NULL );
}
}
catch( const std::exception& e )
{
wxString msg;
msg << _( "IDF Export Failed:\n" ) << FROM_UTF8( e.what() );
wxMessageBox( msg );
ok = false;
}
return ok;
}
static SGNODE* getSGColor( VRML_COLOR_INDEX colorIdx )
{
if( colorIdx == -1 )
colorIdx = VRML_COLOR_PCB;
else if( colorIdx == VRML_COLOR_LAST )
return NULL;
if( sgmaterial[colorIdx] )
return sgmaterial[colorIdx];
IFSG_APPEARANCE vcolor( (SGNODE*) NULL );
VRML_COLOR* cp = &colors[colorIdx];
vcolor.SetSpecular( cp->spec_red, cp->spec_grn, cp->spec_blu );
vcolor.SetDiffuse( cp->diffuse_red, cp->diffuse_grn, cp->diffuse_blu );
vcolor.SetShininess( cp->shiny );
// NOTE: XXX - replace with a better equation; using this definition
// of ambient will not yield the best results
vcolor.SetAmbient( cp->ambient, cp->ambient, cp->ambient );
vcolor.SetTransparency( cp->transp );
sgmaterial[colorIdx] = vcolor.GetRawPtr();
return sgmaterial[colorIdx];
}
static void create_vrml_plane( IFSG_TRANSFORM& PcbOutput, VRML_COLOR_INDEX colorID,
VRML_LAYER* layer, double top_z, bool aTopPlane )
{
std::vector< double > vertices;
std::vector< int > idxPlane;
std::vector< int > idxSide;
if( !(*layer).Get2DTriangles( vertices, idxPlane, top_z, aTopPlane ) )
{
#ifdef DEBUG
do {
std::ostringstream ostr;
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
ostr << " * [INFO] no vertex data";
wxLogDebug( "%s\n", ostr.str().c_str() );
} while( 0 );
#endif
return;
}
if( ( idxPlane.size() % 3 ) || ( idxSide.size() % 3 ) )
{
#ifdef DEBUG
do {
std::ostringstream ostr;
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
ostr << " * [BUG] index lists are not a multiple of 3 (not a triangle list)";
wxLogDebug( "%s\n", ostr.str().c_str() );
} while( 0 );
#endif
throw( std::runtime_error( "[BUG] index lists are not a multiple of 3 (not a triangle list)" ) );
}
std::vector< SGPOINT > vlist;
size_t nvert = vertices.size() / 3;
size_t j = 0;
for( size_t i = 0; i < nvert; ++i, j+= 3 )
vlist.push_back( SGPOINT( vertices[j], vertices[j+1], vertices[j+2] ) );
// create the intermediate scenegraph
IFSG_TRANSFORM tx0( PcbOutput.GetRawPtr() ); // tx0 = Transform for this outline
IFSG_SHAPE shape( tx0 ); // shape will hold (a) all vertices and (b) a local list of normals
IFSG_FACESET face( shape ); // this face shall represent the top and bottom planes
IFSG_COORDS cp( face ); // coordinates for all faces
cp.SetCoordsList( nvert, &vlist[0] );
IFSG_COORDINDEX coordIdx( face ); // coordinate indices for top and bottom planes only
coordIdx.SetIndices( idxPlane.size(), &idxPlane[0] );
IFSG_NORMALS norms( face ); // normals for the top and bottom planes
// set the normals
if( aTopPlane )
{
for( size_t i = 0; i < nvert; ++i )
norms.AddNormal( 0.0, 0.0, 1.0 );
}
else
{
for( size_t i = 0; i < nvert; ++i )
norms.AddNormal( 0.0, 0.0, -1.0 );
}
// assign a color from the palette
SGNODE* modelColor = getSGColor( colorID );
if( NULL != modelColor )
{
if( NULL == S3D::GetSGNodeParent( modelColor ) )
shape.AddChildNode( modelColor );
else
shape.AddRefNode( modelColor );
}
return;
}
static void create_vrml_shell( IFSG_TRANSFORM& PcbOutput, VRML_COLOR_INDEX colorID,
VRML_LAYER* layer, double top_z, double bottom_z )
{
std::vector< double > vertices;
std::vector< int > idxPlane;
std::vector< int > idxSide;
if( top_z < bottom_z )
{
double tmp = top_z;
top_z = bottom_z;
bottom_z = tmp;
}
if( !(*layer).Get3DTriangles( vertices, idxPlane, idxSide, top_z, bottom_z ) )
{
#ifdef DEBUG
do {
std::ostringstream ostr;
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
ostr << " * [INFO] no vertex data";
wxLogDebug( "%s\n", ostr.str().c_str() );
} while( 0 );
#endif
return;
}
if( ( idxPlane.size() % 3 ) || ( idxSide.size() % 3 ) )
{
#ifdef DEBUG
do {
std::ostringstream ostr;
ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
ostr << " * [BUG] index lists are not a multiple of 3 (not a triangle list)";
wxLogDebug( "%s\n", ostr.str().c_str() );
} while( 0 );
#endif
throw( std::runtime_error( "[BUG] index lists are not a multiple of 3 (not a triangle list)" ) );
}
std::vector< SGPOINT > vlist;
size_t nvert = vertices.size() / 3;
size_t j = 0;
for( size_t i = 0; i < nvert; ++i, j+= 3 )
vlist.push_back( SGPOINT( vertices[j], vertices[j+1], vertices[j+2] ) );
// create the intermediate scenegraph
IFSG_TRANSFORM tx0( PcbOutput.GetRawPtr() ); // tx0 = Transform for this outline
IFSG_SHAPE shape( tx0 ); // shape will hold (a) all vertices and (b) a local list of normals
IFSG_FACESET face( shape ); // this face shall represent the top and bottom planes
IFSG_COORDS cp( face ); // coordinates for all faces
cp.SetCoordsList( nvert, &vlist[0] );
IFSG_COORDINDEX coordIdx( face ); // coordinate indices for top and bottom planes only
coordIdx.SetIndices( idxPlane.size(), &idxPlane[0] );
IFSG_NORMALS norms( face ); // normals for the top and bottom planes
// number of TOP (and bottom) vertices
j = nvert / 2;
// set the TOP normals
for( size_t i = 0; i < j; ++i )
norms.AddNormal( 0.0, 0.0, 1.0 );
// set the BOTTOM normals
for( size_t i = 0; i < j; ++i )
norms.AddNormal( 0.0, 0.0, -1.0 );
// assign a color from the palette
SGNODE* modelColor = getSGColor( colorID );
if( NULL != modelColor )
{
if( NULL == S3D::GetSGNodeParent( modelColor ) )
shape.AddChildNode( modelColor );
else
shape.AddRefNode( modelColor );
}
// create a second shape describing the vertical walls of the extrusion
// using per-vertex-per-face-normals
shape.NewNode( tx0 );
shape.AddRefNode( modelColor ); // set the color to be the same as the top/bottom
face.NewNode( shape );
cp.NewNode( face ); // new vertex list
norms.NewNode( face ); // new normals list
coordIdx.NewNode( face ); // new index list
// populate the new per-face vertex list and its indices and normals
std::vector< int >::iterator sI = idxSide.begin();
std::vector< int >::iterator eI = idxSide.end();
size_t sidx = 0; // index to the new coord set
SGPOINT p1, p2, p3;
SGVECTOR vnorm;
while( sI != eI )
{
p1 = vlist[*sI];
cp.AddCoord( p1 );
++sI;
p2 = vlist[*sI];
cp.AddCoord( p2 );
++sI;
p3 = vlist[*sI];
cp.AddCoord( p3 );
++sI;
vnorm.SetVector( S3D::CalcTriNorm( p1, p2, p3 ) );
norms.AddNormal( vnorm );
norms.AddNormal( vnorm );
norms.AddNormal( vnorm );
coordIdx.AddIndex( (int)sidx );
++sidx;
coordIdx.AddIndex( (int)sidx );
++sidx;
coordIdx.AddIndex( (int)sidx );
++sidx;
}
}
|