File size: 65,419 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 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * *
# * Copyright (c) 2011 Yorik van Havre <yorik@uncreated.net> *
# * *
# * This file is part of FreeCAD. *
# * *
# * FreeCAD is free software: you can redistribute it and/or modify it *
# * under the terms of the GNU Lesser General Public License as *
# * published by the Free Software Foundation, either version 2.1 of the *
# * License, or (at your option) any later version. *
# * *
# * FreeCAD 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 *
# * Lesser General Public License for more details. *
# * *
# * You should have received a copy of the GNU Lesser General Public *
# * License along with FreeCAD. If not, see *
# * <https://www.gnu.org/licenses/>. *
# * *
# ***************************************************************************
## @package ArchSectionPlane
# \ingroup ARCH
# \brief The Section plane object and tools
#
# This module provides tools to build Section plane objects.
# It also contains functionality to produce SVG rendering of
# section planes, to be used in the TechDraw module
import math
import re
import tempfile
import time
import uuid
import FreeCAD
import ArchCommands
import ArchComponent
import Draft
import DraftVecUtils
from FreeCAD import Vector
from draftutils import params
if FreeCAD.GuiUp:
from pivy import coin
from PySide import QtCore, QtGui
from PySide.QtCore import QT_TRANSLATE_NOOP
import FreeCADGui
from draftutils.translate import translate
else:
# \cond
def translate(ctxt, txt):
return txt
def QT_TRANSLATE_NOOP(ctxt, txt):
return txt
# \endcond
ISRENDERING = False # flag to prevent concurrent runs of the coin renderer
def getSectionData(source):
"""Returns some common data from section planes and building parts"""
if hasattr(source, "Objects"):
objs = source.Objects
cutplane = source.Shape
elif hasattr(source, "Group"):
import Part
objs = source.Group
cutplane = Part.makePlane(1000, 1000, FreeCAD.Vector(-500, -500, 0))
m = 1
if source.ViewObject and hasattr(source.ViewObject, "CutMargin"):
m = source.ViewObject.CutMargin.Value
cutplane.translate(FreeCAD.Vector(0, 0, m))
cutplane.Placement = cutplane.Placement.multiply(source.Placement)
onlySolids = True
if hasattr(source, "OnlySolids"):
onlySolids = source.OnlySolids
clip = False
if hasattr(source, "Clip"):
clip = source.Clip
p = FreeCAD.Placement(source.Placement)
direction = p.Rotation.multVec(FreeCAD.Vector(0, 0, 1))
if objs:
objs = Draft.get_group_contents(objs, walls=True, addgroups=True)
return objs, cutplane, onlySolids, clip, direction
def getCutShapes(
objs, cutplane, onlySolids, clip, joinArch, showHidden, groupSshapesByObject=False
):
"""
returns a list of shapes (visible, hidden, cut lines...)
obtained from performing a series of booleans against the given cut plane
"""
import Part
import DraftGeomUtils
shapes = []
hshapes = []
sshapes = []
objectShapes = []
objectSshapes = []
if joinArch:
shtypes = {}
for o in objs:
if Draft.getType(o) in ["Wall", "Structure"]:
if o.Shape.isNull():
pass
elif onlySolids:
shtypes.setdefault(
o.Material.Name if (hasattr(o, "Material") and o.Material) else "None", []
).extend(o.Shape.Solids)
else:
shtypes.setdefault(
o.Material.Name if (hasattr(o, "Material") and o.Material) else "None", []
).append(o.Shape.copy())
elif hasattr(o, "Shape"):
if o.Shape.isNull():
pass
elif onlySolids:
shapes.extend(o.Shape.Solids)
objectShapes.append((o, o.Shape.Solids))
else:
shapes.append(o.Shape.copy())
objectShapes.append((o, [o.Shape.copy()]))
for k, v in shtypes.items():
v1 = v.pop()
if v:
v1 = v1.multiFuse(v)
v1 = v1.removeSplitter()
if v1.Solids:
shapes.extend(v1.Solids)
objectShapes.append((k, v1.Solids))
else:
print("ArchSectionPlane: Fusing Arch objects produced non-solid results")
shapes.append(v1)
objectShapes.append((k, [v1]))
else:
for o in objs:
if hasattr(o, "Shape"):
if o.Shape.isNull():
pass
elif onlySolids:
if o.Shape.isValid():
shapes.extend(o.Shape.Solids)
objectShapes.append((o, o.Shape.Solids))
else:
shapes.append(o.Shape)
objectShapes.append((o, [o.Shape]))
cutface, cutvolume, invcutvolume = ArchCommands.getCutVolume(cutplane, shapes, clip)
shapes = []
for o, shapeList in objectShapes:
tmpSshapes = []
for sh in shapeList:
for sub in (sh.SubShapes if sh.ShapeType == "Compound" else [sh]):
if cutvolume:
if sub.Volume < 0:
sub = sub.reversed() # Use reversed as sub is immutable.
c = sub.cut(cutvolume)
s = sub.section(cutface)
try:
wires = DraftGeomUtils.findWires(s.Edges)
for w in wires:
f = Part.Face(w)
tmpSshapes.append(f)
except Part.OCCError:
# print "ArchView: unable to get a face"
tmpSshapes.append(s)
shapes.extend(c.SubShapes if c.ShapeType == "Compound" else [c])
if showHidden:
c = sub.cut(invcutvolume)
hshapes.extend(c.SubShapes if c.ShapeType == "Compound" else [c])
else:
shapes.append(sub)
if len(tmpSshapes) > 0:
sshapes.extend(tmpSshapes)
if groupSshapesByObject:
objectSshapes.append((o, tmpSshapes))
if groupSshapesByObject:
return shapes, hshapes, sshapes, cutface, cutvolume, invcutvolume, objectSshapes
else:
return shapes, hshapes, sshapes, cutface, cutvolume, invcutvolume
def getFillForObject(o, defaultFill, source):
"""returns a color tuple from an object's material"""
if hasattr(source, "UseMaterialColorForFill") and source.UseMaterialColorForFill:
material = None
if hasattr(o, "Material") and o.Material:
material = o.Material
elif isinstance(o, str):
material = FreeCAD.ActiveDocument.getObject(o)
if material:
if hasattr(material, "SectionColor") and material.SectionColor:
return material.SectionColor
elif hasattr(material, "Color") and material.Color:
return material.Color
elif hasattr(o, "ViewObject") and hasattr(o.ViewObject, "ShapeColor"):
return o.ViewObject.ShapeColor
return defaultFill
def isOriented(obj, plane):
"""determines if an annotation is facing the cutplane or not"""
norm1 = plane.normalAt(0, 0)
if hasattr(obj, "Placement"):
norm2 = obj.Placement.Rotation.multVec(FreeCAD.Vector(0, 0, 1))
elif hasattr(obj, "Normal"):
norm2 = obj.Normal
if norm2.Length < 0.01:
return True
else:
return True
a = norm1.getAngle(norm2)
if (a < 0.01) or (abs(a - math.pi) < 0.01):
return True
return False
def update_svg_cache(source, renderMode, showHidden, showFill, fillSpaces, joinArch, allOn, objs):
"""
Returns None or cached SVG, clears shape cache if required
"""
svgcache = None
if hasattr(source, "Proxy"):
if hasattr(source.Proxy, "svgcache") and source.Proxy.svgcache:
# TODO check array bounds
svgcache = source.Proxy.svgcache[0]
# empty caches if we want to force-recalculate for certain properties
if (
source.Proxy.svgcache[1] != renderMode
or source.Proxy.svgcache[2] != showHidden
or source.Proxy.svgcache[3] != showFill
or source.Proxy.svgcache[4] != fillSpaces
or source.Proxy.svgcache[5] != joinArch
or source.Proxy.svgcache[6] != allOn
or source.Proxy.svgcache[7] != set(objs)
):
svgcache = None
if (
source.Proxy.svgcache[4] != fillSpaces
or source.Proxy.svgcache[5] != joinArch
or source.Proxy.svgcache[6] != allOn
or source.Proxy.svgcache[7] != set(objs)
):
source.Proxy.shapecache = None
return svgcache
def getSVG(
source,
renderMode="Wireframe",
allOn=False,
showHidden=False,
scale=1,
rotation=0,
linewidth=1,
lineColor=(0.0, 0.0, 0.0),
fontsize=1,
linespacing=None,
showFill=False,
fillColor=(1.0, 1.0, 1.0),
techdraw=False,
fillSpaces=False,
cutlinewidth=0,
joinArch=False,
):
"""
Return an SVG fragment from an Arch SectionPlane or BuildingPart.
allOn
If it is `True`, all cut objects are shown, regardless of if they are
visible or not.
renderMode
Can be `'Wireframe'` (default) or `'Solid'` to use the Arch solid
renderer.
showHidden
If it is `True`, the hidden geometry above the section plane
is shown in dashed line.
showFill
If it is `True`, the cut areas get filled with a pattern.
lineColor
Color of lines for the `renderMode` is `'Wireframe'`.
fillColor
If `showFill` is `True` and `renderMode` is `'Wireframe'`,
the cut areas are filled with `fillColor`.
fillSpaces
If `True`, shows space objects as filled surfaces.
"""
import Part
objs, cutplane, onlySolids, clip, direction = getSectionData(source)
if not objs:
return ""
if not allOn:
objs = Draft.removeHidden(objs)
# separate spaces and Draft objects
spaces = []
nonspaces = []
drafts = [] # Only used for annotations.
windows = []
cutface = None
for o in objs:
if Draft.getType(o) == "Space":
spaces.append(o)
elif Draft.getType(o) in [
"Dimension",
"AngularDimension",
"LinearDimension",
"Annotation",
"Label",
"Text",
"DraftText",
"Axis",
]:
if isOriented(o, cutplane):
drafts.append(o)
elif o.isDerivedFrom("App::DocumentObjectGroup"):
# These will have been expanded by getSectionData already
pass
else:
nonspaces.append(o)
if Draft.getType(o.getLinkedObject()) == "Window": # To support Link of Windows(Doors)
windows.append(o)
objs = nonspaces
scaledLineWidth = linewidth / scale
if renderMode in ["Coin", 2, "Coin mono", 3]:
# don't scale linewidths in coin mode
svgLineWidth = str(linewidth) + "px"
else:
svgLineWidth = str(scaledLineWidth) + "px"
if cutlinewidth:
scaledCutLineWidth = cutlinewidth / scale
svgCutLineWidth = str(scaledCutLineWidth) + "px"
else:
st = params.get_param_arch("CutLineThickness")
svgCutLineWidth = str(scaledLineWidth * st) + "px"
yt = params.get_param_arch("SymbolLineThickness")
svgSymbolLineWidth = str(linewidth * yt)
hiddenPattern = params.get_param_arch("archHiddenPattern")
svgHiddenPattern = hiddenPattern.replace(" ", "")
# fillpattern = '<pattern id="sectionfill" patternUnits="userSpaceOnUse" patternTransform="matrix(5,0,0,5,0,0)"'
# fillpattern += ' x="0" y="0" width="10" height="10">'
# fillpattern += '<g>'
# fillpattern += '<rect width="10" height="10" style="stroke:none; fill:#ffffff" /><path style="stroke:#000000; stroke-width:1" d="M0,0 l10,10" /></g></pattern>'
svgLineColor = Draft.getrgb(lineColor)
svg = ""
# reading cached version
svgcache = update_svg_cache(
source, renderMode, showHidden, showFill, fillSpaces, joinArch, allOn, objs
)
should_update_svg_cache = False
if showFill or not svgcache:
should_update_svg_cache = True
# generating SVG
if renderMode in ["Coin", 2, "Coin mono", 3]:
# render using a coin viewer
if hasattr(source.ViewObject, "ViewData") and source.ViewObject.ViewData:
cameradata = None # getCameraData(source.ViewObject.ViewData)
else:
cameradata = None
if should_update_svg_cache:
if renderMode in ["Coin mono", 3]:
svgcache = getCoinSVG(
cutplane, objs, cameradata, linewidth="SVGLINEWIDTH", facecolor="#ffffff"
)
else:
svgcache = getCoinSVG(cutplane, objs, cameradata, linewidth="SVGLINEWIDTH")
elif renderMode in ["Solid", 1]:
if should_update_svg_cache:
svgcache = ""
# render using the Arch Vector Renderer
import ArchVRM
import WorkingPlane
wp = WorkingPlane.PlaneBase()
pl = FreeCAD.Placement(source.Placement)
if source.ViewObject and hasattr(source.ViewObject, "CutMargin"):
mv = pl.multVec(FreeCAD.Vector(0, 0, 1))
mv.multiply(source.ViewObject.CutMargin)
pl.move(mv)
wp.align_to_placement(pl)
# wp.inverse()
render = ArchVRM.Renderer()
render.setWorkingPlane(wp)
render.addObjects(objs)
if showHidden:
render.cut(cutplane, showHidden)
else:
render.cut(cutplane)
g = '<g transform="scale(1,-1)">\n'
if hasattr(source.ViewObject, "RotateSolidRender"):
if source.ViewObject.RotateSolidRender.Value != 0:
g = '<g transform="scale(1,-1) rotate('
g += str(source.ViewObject.RotateSolidRender.Value)
g += ')">\n'
svgcache += g
svgcache += render.getViewSVG(linewidth="SVGLINEWIDTH")
# svgcache += fillpattern
svgcache += render.getSectionSVG(linewidth="SVGCUTLINEWIDTH", fillpattern="#ffffff")
if showHidden:
svgcache += render.getHiddenSVG(linewidth="SVGLINEWIDTH")
svgcache += "</g>\n"
# print(render.info())
else:
# Wireframe (0) mode
if (
hasattr(source, "Proxy")
and hasattr(source.Proxy, "shapecache")
and source.Proxy.shapecache
):
vshapes = source.Proxy.shapecache[0]
hshapes = source.Proxy.shapecache[1]
sshapes = source.Proxy.shapecache[2]
cutface = source.Proxy.shapecache[3]
# cutvolume = source.Proxy.shapecache[4] # Unused
# invcutvolume = source.Proxy.shapecache[5] # Unused
objectSshapes = source.Proxy.shapecache[6]
else:
if showFill:
vshapes, hshapes, sshapes, cutface, cutvolume, invcutvolume, objectSshapes = (
getCutShapes(objs, cutplane, onlySolids, clip, joinArch, showHidden, True)
)
else:
vshapes, hshapes, sshapes, cutface, cutvolume, invcutvolume = getCutShapes(
objs, cutplane, onlySolids, clip, joinArch, showHidden
)
objectSshapes = []
source.Proxy.shapecache = [
vshapes,
hshapes,
sshapes,
cutface,
cutvolume,
invcutvolume,
objectSshapes,
]
if should_update_svg_cache:
svgcache = ""
# render using the TechDraw module
import TechDraw
import Part
if vshapes:
baseshape = Part.makeCompound(vshapes)
style = {"stroke": "SVGLINECOLOR", "stroke-width": "SVGLINEWIDTH"}
svgcache += TechDraw.projectToSVG(
baseshape,
direction,
hStyle=style,
h0Style=style,
h1Style=style,
vStyle=style,
v0Style=style,
v1Style=style,
)
if hshapes:
hshapes = Part.makeCompound(hshapes)
style = {
"stroke": "SVGLINECOLOR",
"stroke-width": "SVGLINEWIDTH",
"stroke-dasharray": "SVGHIDDENPATTERN",
}
svgcache += TechDraw.projectToSVG(
hshapes,
direction,
hStyle=style,
h0Style=style,
h1Style=style,
vStyle=style,
v0Style=style,
v1Style=style,
)
if sshapes:
if showFill:
# svgcache += fillpattern
svgcache += '<g transform="rotate(180)">\n'
for o, shapes in objectSshapes:
for s in shapes:
if s.Edges:
objectFill = getFillForObject(o, fillColor, source)
# svg += Draft.get_svg(s,
# direction=direction.negative(),
# linewidth=0,
# fillstyle="sectionfill",
# color=(0,0,0))
# temporarily disabling fill patterns
svgcache += Draft.get_svg(
s,
linewidth=0,
fillstyle=Draft.getrgb(objectFill, testbw=False),
direction=direction.negative(),
color=lineColor,
)
svgcache += "</g>\n"
sshapes = Part.makeCompound(sshapes)
style = {"stroke": "SVGLINECOLOR", "stroke-width": "SVGCUTLINEWIDTH"}
svgcache += TechDraw.projectToSVG(
sshapes,
direction,
hStyle=style,
h0Style=style,
h1Style=style,
vStyle=style,
v0Style=style,
v1Style=style,
)
if should_update_svg_cache:
if hasattr(source, "Proxy"):
source.Proxy.svgcache = [
svgcache,
renderMode,
showHidden,
showFill,
fillSpaces,
joinArch,
allOn,
set(objs),
]
svgcache = svgcache.replace("SVGLINECOLOR", svgLineColor)
svgcache = svgcache.replace("SVGLINEWIDTH", svgLineWidth)
svgcache = svgcache.replace("SVGHIDDENPATTERN", svgHiddenPattern)
svgcache = svgcache.replace("SVGCUTLINEWIDTH", svgCutLineWidth)
svg += svgcache
if drafts:
if not techdraw:
svg += '<g transform="scale(1,-1)">'
for d in drafts:
svg += Draft.get_svg(
d,
scale=scale,
linewidth=svgSymbolLineWidth,
fontsize=fontsize,
linespacing=linespacing,
direction=direction,
color=lineColor,
techdraw=techdraw,
rotation=rotation,
override=False,
)
if not techdraw:
svg += "</g>"
if not cutface:
# if we didn't calculate anything better, use the cutplane...
cutface = cutplane
# filter out spaces not cut by the source plane
if cutface and spaces:
spaces = [s for s in spaces if s.Shape.BoundBox.intersect(cutface.BoundBox)]
if spaces:
if not techdraw:
svg += '<g transform="scale(1,-1)">'
for s in spaces:
svg += Draft.get_svg(
s,
scale=scale,
linewidth=svgSymbolLineWidth,
fontsize=fontsize,
linespacing=linespacing,
direction=direction,
color=lineColor,
techdraw=techdraw,
rotation=rotation,
fillspaces=fillSpaces,
)
if not techdraw:
svg += "</g>"
# add additional edge symbols from windows
cutwindows = []
if cutface and windows and BoundBoxValid(cutface.BoundBox):
cutwindows = [
w.Name
for w in windows
if BoundBoxValid(w.Shape.BoundBox) and w.Shape.BoundBox.intersect(cutface.BoundBox)
]
if windows:
sh = []
for w in windows:
if w.Name in cutwindows:
wlo = w.getLinkedObject() # To support Link of Windows(Doors)
if hasattr(wlo, "SymbolPlan") and wlo.SymbolPlan:
if not hasattr(wlo.Proxy, "sshapes"):
wlo.Proxy.execute(wlo)
if hasattr(wlo.Proxy, "sshapes") and wlo.Proxy.sshapes:
c = Part.makeCompound(wlo.Proxy.sshapes)
c.Placement = w.Placement
sh.append(c)
if sh:
if not techdraw:
svg += '<g transform="scale(1,-1)">'
for s in sh:
svg += Draft.get_svg(
s,
scale=scale,
linewidth=svgSymbolLineWidth,
fontsize=fontsize,
linespacing=linespacing,
fillstyle="none",
direction=direction,
color=lineColor,
techdraw=techdraw,
rotation=rotation,
)
if not techdraw:
svg += "</g>"
return svg
def BoundBoxValid(boundBox) -> bool:
"""Return true if boundBox has a non-zero volume"""
return boundBox.XLength > 0 and boundBox.YLength > 0 and boundBox.ZLength > 0
def getDXF(obj):
"""Return a DXF representation from a TechDraw view."""
allOn = getattr(obj, "AllOn", True)
showHidden = getattr(obj, "ShowHidden", False)
result = []
import TechDraw
import Part
if not obj.Source:
return result
source = obj.Source
objs, cutplane, onlySolids, clip, direction = getSectionData(source)
if not objs:
return result
if not allOn:
objs = Draft.removeHidden(objs)
objs = [
obj
for obj in objs
if (
not obj.isDerivedFrom("Part::Part2DObject")
and Draft.getType(obj)
not in ["BezCurve", "BSpline", "Wire", "Annotation", "Dimension", "Space"]
)
]
vshapes, hshapes, sshapes, cutface, cutvolume, invcutvolume = getCutShapes(
objs, cutplane, onlySolids, clip, False, showHidden
)
if vshapes:
result.append(TechDraw.projectToDXF(Part.makeCompound(vshapes), direction))
if sshapes:
result.append(TechDraw.projectToDXF(Part.makeCompound(sshapes), direction))
if hshapes:
result.append(TechDraw.projectToDXF(Part.makeCompound(hshapes), direction))
return result
def getCameraData(floatlist):
"""reconstructs a valid camera data string from stored values"""
c = ""
if len(floatlist) >= 12:
d = floatlist
camtype = "orthographic"
if len(floatlist) == 13:
if d[12] == 1:
camtype = "perspective"
if camtype == "orthographic":
c = "#Inventor V2.1 ascii\n\n\nOrthographicCamera {\n viewportMapping ADJUST_CAMERA\n "
else:
c = "#Inventor V2.1 ascii\n\n\nPerspectiveCamera {\n viewportMapping ADJUST_CAMERA\n "
c += "position " + str(d[0]) + " " + str(d[1]) + " " + str(d[2]) + "\n "
c += (
"orientation "
+ str(d[3])
+ " "
+ str(d[4])
+ " "
+ str(d[5])
+ " "
+ str(d[6])
+ "\n "
)
c += "aspectRatio " + str(d[9]) + "\n "
c += "focalDistance " + str(d[10]) + "\n "
if camtype == "orthographic":
c += "height " + str(d[11]) + "\n\n}\n"
else:
c += "heightAngle " + str(d[11]) + "\n\n}\n"
return c
def getCoinSVG(cutplane, objs, cameradata=None, linewidth=0.2, singleface=False, facecolor=None):
"""Returns an SVG fragment generated from a coin view"""
if not FreeCAD.GuiUp:
return ""
# do not allow concurrent runs
# wait until the other rendering has finished
global ISRENDERING
while ISRENDERING:
time.sleep(0.1)
ISRENDERING = True
# a name to save a temp file
svgfile = tempfile.mkstemp(suffix=".svg")[1]
# set object lighting to single face to get black fills
# but this creates artifacts in svg output, triangulation gets visible...
ldict = {}
if singleface:
for obj in objs:
if hasattr(obj, "ViewObject") and hasattr(obj.ViewObject, "Lighting"):
ldict[obj.Name] = obj.ViewObject.Lighting
obj.ViewObject.Lighting = "One side"
# get nodes to render
root_node = coin.SoSeparator()
boundbox = FreeCAD.BoundBox()
for obj in objs:
if hasattr(obj.ViewObject, "RootNode") and obj.ViewObject.RootNode:
old_visibility = obj.ViewObject.isVisible()
# ignore visibility as only visible objects are passed here
obj.ViewObject.show()
node_copy = obj.ViewObject.RootNode.copy()
root_node.addChild(node_copy)
if old_visibility:
obj.ViewObject.show()
else:
obj.ViewObject.hide()
if hasattr(obj, "Shape") and hasattr(obj.Shape, "BoundBox"):
boundbox.add(obj.Shape.BoundBox)
# reset lighting of objects
if ldict:
for obj in objs:
if obj.Name in ldict:
obj.ViewObject.Lighting = ldict[obj.Name]
# create viewer
view_window = FreeCADGui.createViewer()
view_window_name = "Temp" + str(uuid.uuid4().hex[:8])
view_window.setName(view_window_name)
# disable animations to prevent a crash:
# https://github.com/FreeCAD/FreeCAD/issues/24929
view_window.setAnimationEnabled(False)
inventor_view = view_window.getViewer()
inventor_view.setBackgroundColor(1, 1, 1)
view_window.redraw()
# set clip plane
clip = coin.SoClipPlane()
norm = cutplane.normalAt(0, 0).negative()
proj = DraftVecUtils.project(cutplane.CenterOfMass, norm)
dist = proj.Length
if proj.getAngle(norm) > 1:
dist = -dist
clip.on = True
plane = coin.SbPlane(coin.SbVec3f(norm.x, norm.y, norm.z), dist) # dir, position on dir
clip.plane.setValue(plane)
root_node.insertChild(clip, 0)
# add white marker at scene bound box corner
markervec = FreeCAD.Vector(10, 10, 10)
a = cutplane.normalAt(0, 0).getAngle(markervec)
if (a < 0.01) or (abs(a - math.pi) < 0.01):
markervec = FreeCAD.Vector(10, -10, 10)
boundbox.enlarge(10) # so the marker don't overlap the objects
sep = coin.SoSeparator()
mat = coin.SoMaterial()
mat.diffuseColor.setValue([1, 1, 1])
sep.addChild(mat)
coords = coin.SoCoordinate3()
coords.point.setValues(
[
[boundbox.XMin, boundbox.YMin, boundbox.ZMin],
[boundbox.XMin + markervec.x, boundbox.YMin + markervec.y, boundbox.ZMin + markervec.z],
]
)
sep.addChild(coords)
lset = coin.SoIndexedLineSet()
lset.coordIndex.setValues(0, 2, [0, 1])
sep.addChild(lset)
root_node.insertChild(sep, 0)
# set scenegraph
inventor_view.setSceneGraph(root_node)
# set camera
if cameradata:
view_window.setCamera(cameradata)
else:
view_window.setCameraType("Orthographic")
# rot = FreeCAD.Rotation(FreeCAD.Vector(0,0,1),cutplane.normalAt(0,0))
vx = cutplane.Placement.Rotation.multVec(FreeCAD.Vector(1, 0, 0))
vy = cutplane.Placement.Rotation.multVec(FreeCAD.Vector(0, 1, 0))
vz = cutplane.Placement.Rotation.multVec(FreeCAD.Vector(0, 0, 1))
rot = FreeCAD.Rotation(vx, vy, vz, "ZXY")
view_window.setCameraOrientation(rot.Q)
# this is needed to set correct focal depth, otherwise saving doesn't work properly
view_window.fitAll()
# save view
# print("saving to",svgfile)
view_window.saveVectorGraphic(svgfile, 1) # number is pixel size
# set linewidth placeholder
f = open(svgfile, "r")
svg = f.read()
f.close()
svg = svg.replace("stroke-width:1.0;", "stroke-width:" + str(linewidth) + ";")
svg = svg.replace('stroke-width="1px', 'stroke-width="' + str(linewidth))
# find marker and calculate scale factor and translation
# <line x1="284.986" y1="356.166" x2="285.038" y2="356.166" stroke="#ffffff" stroke-width="1px" />
factor = None
trans = None
import WorkingPlane
wp = WorkingPlane.PlaneBase()
wp.align_to_point_and_axis_svg(Vector(0, 0, 0), cutplane.normalAt(0, 0), 0)
p = wp.get_local_coords(markervec)
orlength = FreeCAD.Vector(p.x, p.y, 0).Length
marker = re.findall(r"<line x1=.*?stroke=\"\#ffffff\".*?\/>", svg)
if marker:
marker = marker[0].split('"')
x1 = float(marker[1])
y1 = float(marker[3])
x2 = float(marker[5])
y2 = float(marker[7])
p1 = FreeCAD.Vector(x1, y1, 0)
p2 = FreeCAD.Vector(x2, y2, 0)
factor = orlength / p2.sub(p1).Length
if factor:
orig = wp.get_local_coords(FreeCAD.Vector(boundbox.XMin, boundbox.YMin, boundbox.ZMin))
orig = FreeCAD.Vector(orig.x, -orig.y, 0)
scaledp1 = FreeCAD.Vector(p1.x * factor, p1.y * factor, 0)
trans = orig.sub(scaledp1)
# remove marker
svg = re.sub(r"<line x1=.*?stroke=\"\#ffffff\".*?\/>", "", svg, count=1)
# remove background rectangle
svg = re.sub(r"<path.*?>", "", svg, count=1, flags=re.MULTILINE | re.DOTALL)
# set face color to white
if facecolor:
res = re.findall(r"fill:(.*?); stroke:(.*?);", svg)
pairs = []
for pair in res:
if (pair not in pairs) and (pair[0] == pair[1]) and (pair[0] not in ["#0a0a0a"]):
# coin seems to be rendering a lot of lines as thin triangles with the #0a0a0a color...
pairs.append(pair)
for pair in pairs:
svg = re.sub(
r"fill:" + pair[0] + "; stroke:" + pair[1] + ";",
"fill:" + facecolor + "; stroke:" + facecolor + ";",
svg,
)
# embed everything in a scale group and scale the viewport
if factor:
if trans:
svg = svg.replace(
"<g>",
'<g transform="translate('
+ str(trans.x)
+ " "
+ str(trans.y)
+ ") scale("
+ str(factor)
+ ","
+ str(factor)
+ ')">\n<g>',
1,
)
else:
svg = svg.replace(
"<g>", '<g transform="scale(' + str(factor) + "," + str(factor) + ')">\n<g>', 1
)
svg = svg.replace("</svg>", "</g>\n</svg>")
# trigger viewer close
QtCore.QTimer.singleShot(1, lambda: closeViewer(view_window_name))
# strip svg tags (needed for TD Arch view)
svg = re.sub(r"<\?xml.*?>", "", svg, flags=re.MULTILINE | re.DOTALL)
svg = re.sub(r"<svg.*?>", "", svg, flags=re.MULTILINE | re.DOTALL)
svg = re.sub(r"<\/svg>", "", svg, flags=re.MULTILINE | re.DOTALL)
ISRENDERING = False
return svg
def closeViewer(name):
"""Close temporary viewers"""
mw = FreeCADGui.getMainWindow()
for sw in mw.findChildren(QtGui.QMdiSubWindow):
if sw.windowTitle() == name:
sw.close()
class _SectionPlane:
"A section plane object"
def __init__(self, obj):
obj.Proxy = self
self.Type = "SectionPlane"
self.setProperties(obj)
def setProperties(self, obj):
pl = obj.PropertiesList
if not "Placement" in pl:
obj.addProperty(
"App::PropertyPlacement",
"Placement",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The placement of this object"),
locked=True,
)
if not "Shape" in pl:
obj.addProperty(
"Part::PropertyPartShape",
"Shape",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The shape of this object"),
locked=True,
)
if not "Objects" in pl:
obj.addProperty(
"App::PropertyLinkList",
"Objects",
"SectionPlane",
QT_TRANSLATE_NOOP(
"App::Property",
"The objects that must be considered by this section plane. Empty means the whole document.",
),
locked=True,
)
if not "OnlySolids" in pl:
obj.addProperty(
"App::PropertyBool",
"OnlySolids",
"SectionPlane",
QT_TRANSLATE_NOOP(
"App::Property",
"If false, non-solids will be cut too, with possible wrong results.",
),
locked=True,
)
obj.OnlySolids = True
if not "Clip" in pl:
obj.addProperty(
"App::PropertyBool",
"Clip",
"SectionPlane",
QT_TRANSLATE_NOOP(
"App::Property",
"If True, resulting views will be clipped to the section plane area.",
),
locked=True,
)
if not "UseMaterialColorForFill" in pl:
obj.addProperty(
"App::PropertyBool",
"UseMaterialColorForFill",
"SectionPlane",
QT_TRANSLATE_NOOP(
"App::Property",
"If true, the color of the objects material will be used to fill cut areas.",
),
locked=True,
)
obj.UseMaterialColorForFill = False
if not "Depth" in pl:
obj.addProperty(
"App::PropertyLength",
"Depth",
"SectionPlane",
QT_TRANSLATE_NOOP(
"App::Property",
"Geometry further than this value will be cut off. Keep zero for unlimited.",
),
locked=True,
)
def onDocumentRestored(self, obj):
self.setProperties(obj)
def execute(self, obj):
import math
import Part
l = 1
h = 1
if obj.ViewObject:
if hasattr(obj.ViewObject, "DisplayLength"):
l = obj.ViewObject.DisplayLength.Value
h = obj.ViewObject.DisplayHeight.Value
elif hasattr(obj.ViewObject, "DisplaySize"):
# old objects
l = obj.ViewObject.DisplaySize.Value
h = obj.ViewObject.DisplaySize.Value
if not l:
l = 1
if not h:
h = 1
p = Part.makePlane(l, h, Vector(l / 2, -h / 2, 0), Vector(0, 0, -1))
# make sure the normal direction is pointing outwards, you never know what OCC will decide...
# Apply the object's placement to the new plane first.
p.Placement = obj.Placement
# Now, check if the resulting plane's normal matches the placement's intended direction.
# This robustly handles all rotation angles and potential OCC inconsistencies.
target_normal = obj.Placement.Rotation.multVec(FreeCAD.Vector(0, 0, 1))
if p.normalAt(0, 0).getAngle(target_normal) > math.pi / 2:
p.reverse()
obj.Shape = p
self.svgcache = None
self.shapecache = None
def getNormal(self, obj):
return obj.Shape.Faces[0].normalAt(0, 0)
def dumps(self):
return None
def loads(self, state):
self.Type = "SectionPlane"
class _ViewProviderSectionPlane:
"A View Provider for Section Planes"
def __init__(self, vobj):
vobj.Proxy = self
self.setProperties(vobj)
def setProperties(self, vobj):
pl = vobj.PropertiesList
d = 0
if "DisplaySize" in pl:
d = vobj.DisplaySize.Value
vobj.removeProperty("DisplaySize")
if not "DisplayLength" in pl:
vobj.addProperty(
"App::PropertyLength",
"DisplayLength",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The display length of this section plane"),
locked=True,
)
if d:
vobj.DisplayLength = d
else:
vobj.DisplayLength = 1000
if not "DisplayHeight" in pl:
vobj.addProperty(
"App::PropertyLength",
"DisplayHeight",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The display height of this section plane"),
locked=True,
)
if d:
vobj.DisplayHeight = d
else:
vobj.DisplayHeight = 1000
if not "ArrowSize" in pl:
vobj.addProperty(
"App::PropertyLength",
"ArrowSize",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The size of the arrows of this section plane"),
locked=True,
)
vobj.ArrowSize = 50
if not "Transparency" in pl:
vobj.addProperty(
"App::PropertyPercent",
"Transparency",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The transparency of this object"),
locked=True,
)
vobj.Transparency = 85
if not "LineWidth" in pl:
vobj.addProperty(
"App::PropertyFloat",
"LineWidth",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The line width of this object"),
locked=True,
)
vobj.LineWidth = 1
if not "CutDistance" in pl:
vobj.addProperty(
"App::PropertyLength",
"CutDistance",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "Show the cut in the 3D view"),
locked=True,
)
if not "LineColor" in pl:
vobj.addProperty(
"App::PropertyColor",
"LineColor",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The color of this object"),
locked=True,
)
vobj.LineColor = ArchCommands.getDefaultColor("Helpers")
if not "CutView" in pl:
vobj.addProperty(
"App::PropertyBool",
"CutView",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "Show the cut in the 3D view"),
locked=True,
)
if not "CutMargin" in pl:
vobj.addProperty(
"App::PropertyLength",
"CutMargin",
"SectionPlane",
QT_TRANSLATE_NOOP(
"App::Property",
"The distance between the cut plane and the actual view cut (keep this a very small value but not zero)",
),
locked=True,
)
vobj.CutMargin = 1
if not "ShowLabel" in pl:
vobj.addProperty(
"App::PropertyBool",
"ShowLabel",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "Show the label in the 3D view"),
locked=True,
)
if not "FontName" in pl:
vobj.addProperty(
"App::PropertyFont",
"FontName",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The name of the font"),
locked=True,
)
vobj.FontName = params.get_param("textfont")
if not "FontSize" in pl:
vobj.addProperty(
"App::PropertyLength",
"FontSize",
"SectionPlane",
QT_TRANSLATE_NOOP("App::Property", "The size of the text font"),
locked=True,
)
vobj.FontSize = params.get_param("textheight") * params.get_param(
"DefaultAnnoScaleMultiplier"
)
def onDocumentRestored(self, vobj):
self.setProperties(vobj)
def getIcon(self):
import Arch_rc
return ":/icons/Arch_SectionPlane_Tree.svg"
def claimChildren(self):
# buggy at the moment so it's disabled - it will for ex. swallow a building object directly at the root of the document...
# if hasattr(self,"Object") and hasattr(self.Object,"Objects"):
# return self.Object.Objects
return []
def attach(self, vobj):
self.Object = vobj.Object
self.clip = None
self.mat1 = coin.SoMaterial()
self.mat2 = coin.SoMaterial()
self.fcoords = coin.SoCoordinate3()
# fs = coin.SoType.fromName("SoBrepFaceSet").createInstance() # this causes a FreeCAD freeze for me
fs = coin.SoIndexedFaceSet()
fs.coordIndex.setValues(0, 7, [0, 1, 2, -1, 0, 2, 3])
self.drawstyle = coin.SoDrawStyle()
self.drawstyle.style = coin.SoDrawStyle.LINES
self.lcoords = coin.SoCoordinate3()
import PartGui # Required for "SoBrepEdgeSet" (because a SectionPlane is not a Part::FeaturePython object).
ls = coin.SoType.fromName("SoBrepEdgeSet").createInstance()
ls.coordIndex.setValues(
0,
57,
[
0,
1,
-1,
2,
3,
4,
5,
-1,
6,
7,
8,
9,
-1,
10,
11,
-1,
12,
13,
14,
15,
-1,
16,
17,
18,
19,
-1,
20,
21,
-1,
22,
23,
24,
25,
-1,
26,
27,
28,
29,
-1,
30,
31,
-1,
32,
33,
34,
35,
-1,
36,
37,
38,
39,
-1,
40,
41,
42,
43,
44,
],
)
self.txtcoords = coin.SoTransform()
self.txtfont = coin.SoFont()
self.txtfont.name = ""
self.txt = coin.SoAsciiText()
self.txt.justification = coin.SoText2.LEFT
self.txt.string.setValue(" ")
sep = coin.SoSeparator()
psep = coin.SoSeparator()
fsep = coin.SoSeparator()
tsep = coin.SoSeparator()
fsep.addChild(self.mat2)
fsep.addChild(self.fcoords)
fsep.addChild(fs)
psep.addChild(self.mat1)
psep.addChild(self.drawstyle)
psep.addChild(self.lcoords)
psep.addChild(ls)
tsep.addChild(self.mat1)
tsep.addChild(self.txtcoords)
tsep.addChild(self.txtfont)
tsep.addChild(self.txt)
sep.addChild(fsep)
sep.addChild(psep)
sep.addChild(tsep)
vobj.addDisplayMode(sep, "Default")
self.onChanged(vobj, "DisplayLength")
self.onChanged(vobj, "LineColor")
self.onChanged(vobj, "Transparency")
self.onChanged(vobj, "CutView")
def getDisplayModes(self, vobj):
return ["Default"]
def getDefaultDisplayMode(self):
return "Default"
def setDisplayMode(self, mode):
return mode
def updateData(self, obj, prop):
vobj = obj.ViewObject
if prop in ["Placement"]:
# for some reason the text doesn't rotate with the host placement??
self.txtcoords.rotation.setValue(obj.Placement.Rotation.Q)
self.onChanged(vobj, "DisplayLength")
# Defer the clipping plane update until after the current event
# loop finishes. This ensures the scene graph has been updated with the
# new placement before we try to recalculate the clip plane.
if vobj and hasattr(vobj, "CutView") and vobj.CutView:
from PySide import QtCore
# We use a lambda to pass the vobj argument to the delayed function.
QtCore.QTimer.singleShot(0, lambda: self.refreshCutView(vobj))
elif prop == "Label":
if hasattr(obj.ViewObject, "ShowLabel") and obj.ViewObject.ShowLabel:
self.txt.string = obj.Label
return
def refreshCutView(self, vobj):
"""
Forces a refresh of the SoClipPlane by toggling the CutView property.
This is called with a delay to ensure the object's placement is up-to-date.
"""
if vobj and hasattr(vobj, "CutView") and vobj.CutView:
vobj.CutView = False
vobj.CutView = True
def onChanged(self, vobj, prop):
if prop == "LineColor":
if hasattr(vobj, "LineColor"):
l = vobj.LineColor
self.mat1.diffuseColor.setValue([l[0], l[1], l[2]])
self.mat2.diffuseColor.setValue([l[0], l[1], l[2]])
elif prop == "Transparency":
if hasattr(vobj, "Transparency"):
self.mat2.transparency.setValue(vobj.Transparency / 100.0)
elif prop in ["DisplayLength", "DisplayHeight", "ArrowSize"]:
# for IFC objects: propagate to the object
if prop in ["DisplayLength", "DisplayHeight"]:
if hasattr(vobj.Object.Proxy, "onChanged"):
vobj.Object.Proxy.onChanged(vobj.Object, prop)
if hasattr(vobj, "DisplayLength") and hasattr(vobj, "DisplayHeight"):
ld = vobj.DisplayLength.Value / 2
hd = vobj.DisplayHeight.Value / 2
elif hasattr(vobj, "DisplaySize"):
# old objects
ld = vobj.DisplaySize.Value / 2
hd = vobj.DisplaySize.Value / 2
else:
ld = 1
hd = 1
verts = []
fverts = []
pl = FreeCAD.Placement(vobj.Object.Placement)
if hasattr(vobj, "ArrowSize"):
l1 = vobj.ArrowSize.Value if vobj.ArrowSize.Value > 0 else 0.1
else:
l1 = 0.1
l2 = l1 / 3
for v in [[-ld, -hd], [ld, -hd], [ld, hd], [-ld, hd]]:
p1 = pl.multVec(Vector(v[0], v[1], 0))
p2 = pl.multVec(Vector(v[0], v[1], -l1))
p3 = pl.multVec(Vector(v[0] - l2, v[1], -l1 + l2))
p4 = pl.multVec(Vector(v[0] + l2, v[1], -l1 + l2))
p5 = pl.multVec(Vector(v[0], v[1] - l2, -l1 + l2))
p6 = pl.multVec(Vector(v[0], v[1] + l2, -l1 + l2))
verts.extend([[p1.x, p1.y, p1.z], [p2.x, p2.y, p2.z]])
fverts.append([p1.x, p1.y, p1.z])
verts.extend(
[[p2.x, p2.y, p2.z], [p3.x, p3.y, p3.z], [p4.x, p4.y, p4.z], [p2.x, p2.y, p2.z]]
)
verts.extend(
[[p2.x, p2.y, p2.z], [p5.x, p5.y, p5.z], [p6.x, p6.y, p6.z], [p2.x, p2.y, p2.z]]
)
p7 = pl.multVec(Vector(-ld + l2, -hd + l2, 0)) # text pos
verts.extend(fverts + [fverts[0]])
self.lcoords.point.setValues(verts)
self.fcoords.point.setValues(fverts)
self.txtcoords.translation.setValue([p7.x, p7.y, p7.z])
# self.txtfont.size = l1
elif prop == "LineWidth":
self.drawstyle.lineWidth = vobj.LineWidth
elif prop in ["CutView", "CutMargin"]:
if (
hasattr(vobj, "CutView")
and FreeCADGui.ActiveDocument.ActiveView
and hasattr(FreeCADGui.ActiveDocument.ActiveView, "getSceneGraph")
):
sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
if vobj.CutView:
if self.clip:
sg.removeChild(self.clip)
self.clip = None
for o in Draft.get_group_contents(vobj.Object.Objects, walls=True):
if hasattr(o.ViewObject, "Lighting"):
o.ViewObject.Lighting = "One side"
self.clip = coin.SoClipPlane()
self.clip.on.setValue(True)
norm = vobj.Object.Proxy.getNormal(vobj.Object)
mp = vobj.Object.Shape.CenterOfMass
mp = DraftVecUtils.project(mp, norm)
dist = mp.Length # - 0.1 # to not clip exactly on the section object
norm = norm.negative()
marg = 1
if hasattr(vobj, "CutMargin"):
marg = vobj.CutMargin.Value
if mp.getAngle(norm) > 1:
dist += marg
dist = -dist
else:
dist -= marg
plane = coin.SbPlane(coin.SbVec3f(norm.x, norm.y, norm.z), dist)
self.clip.plane.setValue(plane)
sg.insertChild(self.clip, 0)
else:
if self.clip:
sg.removeChild(self.clip)
self.clip = None
elif prop == "ShowLabel":
if vobj.ShowLabel:
self.txt.string = vobj.Object.Label or " "
else:
self.txt.string = " "
elif prop == "FontName":
if hasattr(self, "txtfont") and hasattr(vobj, "FontName"):
if vobj.FontName:
self.txtfont.name = vobj.FontName
else:
self.txtfont.name = ""
elif prop == "FontSize":
if hasattr(self, "txtfont") and hasattr(vobj, "FontSize"):
self.txtfont.size = vobj.FontSize.Value
return
def dumps(self):
return None
def loads(self, state):
return None
def setEdit(self, vobj, mode):
if mode != 0:
return None
taskd = SectionPlaneTaskPanel()
taskd.obj = vobj.Object
taskd.update()
FreeCADGui.Control.showDialog(taskd)
return True
def unsetEdit(self, vobj, mode):
if mode != 0:
return None
FreeCADGui.Control.closeDialog()
return True
def doubleClicked(self, vobj):
self.edit()
return True
def setupContextMenu(self, vobj, menu):
if FreeCADGui.activeWorkbench().name() != "BIMWorkbench":
return
actionEdit = QtGui.QAction(translate("Arch", "Edit"), menu)
QtCore.QObject.connect(actionEdit, QtCore.SIGNAL("triggered()"), self.edit)
menu.addAction(actionEdit)
actionToggleCutview = QtGui.QAction(
QtGui.QIcon(":/icons/Draft_Edit.svg"), translate("Arch", "Toggle Cutview"), menu
)
actionToggleCutview.triggered.connect(lambda: self.toggleCutview(vobj))
menu.addAction(actionToggleCutview)
def edit(self):
FreeCADGui.ActiveDocument.setEdit(self.Object, 0)
def toggleCutview(self, vobj):
vobj.CutView = not vobj.CutView
class SectionPlaneTaskPanel:
"""A TaskPanel for all the section plane object"""
def __init__(self):
# the panel has a tree widget that contains categories
# for the subcomponents, such as additions, subtractions.
# the categories are shown only if they are not empty.
self.obj = None
# Create the first box for object scope
self.scope_widget = QtGui.QWidget()
scope_layout = QtGui.QGridLayout(self.scope_widget)
self.title = QtGui.QLabel(self.scope_widget)
scope_layout.addWidget(self.title, 0, 0, 1, 2)
# tree
self.tree = QtGui.QTreeWidget(self.scope_widget)
scope_layout.addWidget(self.tree, 1, 0, 1, 2)
self.tree.setColumnCount(1)
self.tree.header().hide()
# add / remove buttons
self.addButton = QtGui.QPushButton(self.scope_widget)
self.addButton.setIcon(QtGui.QIcon(":/icons/Arch_Add.svg"))
scope_layout.addWidget(self.addButton, 2, 0, 1, 1)
self.delButton = QtGui.QPushButton(self.scope_widget)
self.delButton.setIcon(QtGui.QIcon(":/icons/Arch_Remove.svg"))
scope_layout.addWidget(self.delButton, 2, 1, 1, 1)
self.delButton.setEnabled(False)
# Create the second box for tools
self.tools_widget = QtGui.QWidget()
tools_layout = QtGui.QVBoxLayout(self.tools_widget)
# Cut View toggle button
self.cutViewButton = QtGui.QPushButton(self.tools_widget)
self.cutViewButton.setIcon(QtGui.QIcon(":/icons/Arch_CutPlane.svg"))
self.cutViewButton.setObjectName("cutViewButton")
self.cutViewButton.setCheckable(True)
QtCore.QObject.connect(
self.cutViewButton, QtCore.SIGNAL("toggled(bool)"), self.toggleCutView
)
tools_layout.addWidget(self.cutViewButton)
# rotate / resize buttons
self.rotation_label = QtGui.QLabel(self.tools_widget)
tools_layout.addWidget(self.rotation_label)
rotation_layout = QtGui.QHBoxLayout()
self.rotateXButton = QtGui.QPushButton(self.tools_widget)
self.rotateYButton = QtGui.QPushButton(self.tools_widget)
self.rotateZButton = QtGui.QPushButton(self.tools_widget)
rotation_layout.addWidget(self.rotateXButton)
rotation_layout.addWidget(self.rotateYButton)
rotation_layout.addWidget(self.rotateZButton)
tools_layout.addLayout(rotation_layout)
size_pos_layout = QtGui.QHBoxLayout()
self.resizeButton = QtGui.QPushButton(self.tools_widget)
self.recenterButton = QtGui.QPushButton(self.tools_widget)
size_pos_layout.addWidget(self.resizeButton)
size_pos_layout.addWidget(self.recenterButton)
tools_layout.addLayout(size_pos_layout)
QtCore.QObject.connect(self.addButton, QtCore.SIGNAL("clicked()"), self.addElement)
QtCore.QObject.connect(self.delButton, QtCore.SIGNAL("clicked()"), self.removeElement)
QtCore.QObject.connect(self.rotateXButton, QtCore.SIGNAL("clicked()"), self.rotateX)
QtCore.QObject.connect(self.rotateYButton, QtCore.SIGNAL("clicked()"), self.rotateY)
QtCore.QObject.connect(self.rotateZButton, QtCore.SIGNAL("clicked()"), self.rotateZ)
QtCore.QObject.connect(self.resizeButton, QtCore.SIGNAL("clicked()"), self.resize)
QtCore.QObject.connect(self.recenterButton, QtCore.SIGNAL("clicked()"), self.recenter)
QtCore.QObject.connect(self.tree, QtCore.SIGNAL("itemSelectionChanged()"), self.onTreeClick)
self.form = [self.scope_widget, self.tools_widget]
self.update()
def isAllowedAlterSelection(self):
return True
def isAllowedAlterView(self):
return True
def getStandardButtons(self):
return QtGui.QDialogButtonBox.Close
def getIcon(self, obj):
if hasattr(obj.ViewObject, "Proxy"):
return QtGui.QIcon(obj.ViewObject.Proxy.getIcon())
elif obj.isDerivedFrom("Sketcher::SketchObject"):
return QtGui.QIcon(":/icons/Sketcher_Sketch.svg")
elif obj.isDerivedFrom("App::DocumentObjectGroup"):
return QtGui.QApplication.style().standardIcon(QtGui.QStyle.SP_DirIcon)
elif hasattr(obj.ViewObject, "Icon"):
return QtGui.QIcon(obj.ViewObject.Icon)
return QtGui.QIcon(":/icons/Part_3D_object.svg")
def update(self):
"fills the treewidget"
self.tree.clear()
if self.obj:
for o in self.obj.Objects:
item = QtGui.QTreeWidgetItem(self.tree)
item.setText(0, o.Label)
item.setToolTip(0, o.Name)
item.setIcon(0, self.getIcon(o))
if self.obj.ViewObject and hasattr(self.obj.ViewObject, "CutView"):
self.cutViewButton.setChecked(self.obj.ViewObject.CutView)
self.retranslateUi()
def addElement(self):
if self.obj:
added = False
for o in FreeCADGui.Selection.getSelection():
if o != self.obj:
ArchComponent.addToComponent(self.obj, o, "Objects")
added = True
if added:
self.update()
else:
FreeCAD.Console.PrintWarning(
"Select objects in the 3D view or in the model tree before pressing the button\n"
)
def removeElement(self):
if self.obj:
it = self.tree.currentItem()
if it:
comp = FreeCAD.ActiveDocument.getObject(str(it.toolTip(0)))
ArchComponent.removeFromComponent(self.obj, comp)
self.update()
def rotate(self, axis):
if self.obj and self.obj.Shape and self.obj.Shape.Faces:
face = self.obj.Shape.copy()
import Part
local_axis = self.obj.Placement.Rotation.multVec(axis)
face.rotate(self.obj.Placement.Base, local_axis, 90)
self.obj.Placement = face.Placement
self.obj.Proxy.execute(self.obj)
def rotateX(self):
self.rotate(FreeCAD.Vector(1, 0, 0))
def rotateY(self):
self.rotate(FreeCAD.Vector(0, 1, 0))
def rotateZ(self):
self.rotate(FreeCAD.Vector(0, 0, 1))
def getBB(self):
bb = FreeCAD.BoundBox()
if self.obj:
for o in Draft.get_group_contents(self.obj.Objects):
if hasattr(o, "Shape") and hasattr(o.Shape, "BoundBox"):
bb.add(o.Shape.BoundBox)
return bb
def resize(self):
if self.obj and self.obj.ViewObject:
bb = self.getBB()
n = self.obj.Proxy.getNormal(self.obj)
margin = bb.XLength * 0.1
if (n.getAngle(FreeCAD.Vector(1, 0, 0)) < 0.1) or (
n.getAngle(FreeCAD.Vector(-1, 0, 0)) < 0.1
):
self.obj.ViewObject.DisplayLength = bb.YLength + margin
self.obj.ViewObject.DisplayHeight = bb.ZLength + margin
elif (n.getAngle(FreeCAD.Vector(0, 1, 0)) < 0.1) or (
n.getAngle(FreeCAD.Vector(0, -1, 0)) < 0.1
):
self.obj.ViewObject.DisplayLength = bb.XLength + margin
self.obj.ViewObject.DisplayHeight = bb.ZLength + margin
elif (n.getAngle(FreeCAD.Vector(0, 0, 1)) < 0.1) or (
n.getAngle(FreeCAD.Vector(0, 0, -1)) < 0.1
):
self.obj.ViewObject.DisplayLength = bb.XLength + margin
self.obj.ViewObject.DisplayHeight = bb.YLength + margin
self.obj.Proxy.execute(self.obj)
def recenter(self):
if self.obj:
self.obj.Placement.Base = self.getBB().Center
def onTreeClick(self):
if self.tree.selectedItems():
self.delButton.setEnabled(True)
else:
self.delButton.setEnabled(False)
def accept(self):
FreeCAD.ActiveDocument.recompute()
FreeCADGui.ActiveDocument.resetEdit()
return True
def reject(self):
FreeCAD.ActiveDocument.recompute()
FreeCADGui.ActiveDocument.resetEdit()
return True
def toggleCutView(self, checked):
if self.obj and self.obj.ViewObject and hasattr(self.obj.ViewObject, "CutView"):
self.obj.ViewObject.CutView = checked
def retranslateUi(self):
self.scope_widget.setWindowTitle(QtGui.QApplication.translate("Arch", "Scope", None))
self.tools_widget.setWindowTitle(
QtGui.QApplication.translate("Arch", "Placement and Visuals", None)
)
self.title.setText(
QtGui.QApplication.translate("Arch", "Objects seen by this section plane", None)
)
self.delButton.setText(QtGui.QApplication.translate("Arch", "Remove", None))
self.delButton.setToolTip(
QtGui.QApplication.translate(
"Arch", "Removes highlighted objects from the list above", None
)
)
self.addButton.setText(QtGui.QApplication.translate("Arch", "Add Selected", None))
self.addButton.setToolTip(
QtGui.QApplication.translate(
"Arch", "Adds selected objects to the scope of this section plane", None
)
)
self.cutViewButton.setText(QtGui.QApplication.translate("Arch", "Cut View", None))
self.cutViewButton.setToolTip(
QtGui.QApplication.translate(
"Arch",
"Creates a live cut in the 3D view, hiding geometry on one side of the plane to see inside your model",
None,
)
)
self.rotation_label.setText(QtGui.QApplication.translate("Arch", "Rotate by 90°", None))
self.rotateXButton.setText(QtGui.QApplication.translate("Arch", "Rotate X", None))
self.rotateXButton.setToolTip(
QtGui.QApplication.translate("Arch", "Rotates the plane around its local X-axis", None)
)
self.rotateYButton.setText(QtGui.QApplication.translate("Arch", "Rotate Y", None))
self.rotateYButton.setToolTip(
QtGui.QApplication.translate("Arch", "Rotates the plane around its local Y-axis", None)
)
self.rotateZButton.setText(QtGui.QApplication.translate("Arch", "Rotate Z", None))
self.rotateZButton.setToolTip(
QtGui.QApplication.translate("Arch", "Rotates the plane around its local Z-axis", None)
)
self.resizeButton.setText(QtGui.QApplication.translate("Arch", "Resize to Fit", None))
self.resizeButton.setToolTip(
QtGui.QApplication.translate(
"Arch", "Resizes the plane to fit the objects in the list above", None
)
)
self.recenterButton.setText(QtGui.QApplication.translate("Arch", "Recenter Plane", None))
self.recenterButton.setToolTip(
QtGui.QApplication.translate(
"Arch", "Centers the plane on the objects in the list above", None
)
)
|