Skip to content
Snippets Groups Projects
mesh_edgetools.py 69.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • 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
            row.alignment = 'EXPAND'
            row.prop(self, "pos")
            row.prop(self, "neg")
    
            layout.separator()
    
            col = layout.column(align=True)
            col.prop(self, "angle")
            col.prop(self, "length")
    
        @classmethod
        def poll(cls, context):
            ob = context.active_object
            return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
    
        def invoke(self, context, event):
            return self.execute(context)
    
        def execute(self, context):
            try:
                me = context.object.data
                bm = bmesh.from_edit_mesh(me)
                bm.normal_update()
    
                bVerts = bm.verts
                bEdges = bm.edges
                edges = [e for e in bEdges if e.select]
                vectors = []
    
                if not is_selected_enough(self, edges, 0, edges_n=2, verts_n=0, types="Edge"):
                    return {'CANCELLED'}
    
                verts = [edges[0].verts[0],
                         edges[0].verts[1],
                         edges[1].verts[0],
                         edges[1].verts[1]]
    
                cos = intersect_line_line(verts[0].co, verts[1].co, verts[2].co, verts[3].co)
    
                # If the two edges are parallel:
                if cos is None:
                    self.report({'WARNING'},
                                "Selected lines are parallel: results may be unpredictable")
                    vectors.append(verts[0].co - verts[1].co)
                    vectors.append(verts[0].co - verts[2].co)
                    vectors.append(vectors[0].cross(vectors[1]))
                    vectors.append(vectors[2].cross(vectors[0]))
                    vectors.append(-vectors[3])
                else:
                    # Warn the user if they have not chosen two planar edges:
                    if not is_same_co(cos[0], cos[1]):
                        self.report({'WARNING'},
                                    "Selected lines are not planar: results may be unpredictable")
    
                    # This makes the +/- behavior predictable:
                    if (verts[0].co - cos[0]).length < (verts[1].co - cos[0]).length:
                        verts[0], verts[1] = verts[1], verts[0]
                    if (verts[2].co - cos[0]).length < (verts[3].co - cos[0]).length:
                        verts[2], verts[3] = verts[3], verts[2]
    
                    vectors.append(verts[0].co - verts[1].co)
                    vectors.append(verts[2].co - verts[3].co)
    
                    # Normal of the plane formed by vector1 and vector2:
                    vectors.append(vectors[0].cross(vectors[1]))
    
                    # Possible directions:
                    vectors.append(vectors[2].cross(vectors[0]))
                    vectors.append(vectors[1].cross(vectors[2]))
    
                # Set the length:
                vectors[3].length = self.length
                vectors[4].length = self.length
    
                # Perform any additional rotations:
                matrix = Matrix.Rotation(radians(90 + self.angle), 3, vectors[2])
                vectors.append(matrix @ -vectors[3])    # vectors[5]
                matrix = Matrix.Rotation(radians(90 - self.angle), 3, vectors[2])
                vectors.append(matrix @ vectors[4])     # vectors[6]
                vectors.append(matrix @ vectors[3])     # vectors[7]
                matrix = Matrix.Rotation(radians(90 + self.angle), 3, vectors[2])
                vectors.append(matrix @ -vectors[4])    # vectors[8]
    
                # Perform extrusions and displacements:
                # There will be a total of 8 extrusions.  One for each vert of each edge.
                # It looks like an extrusion will add the new vert to the end of the verts
                # list and leave the rest in the same location.
                # -- EDIT --
                # It looks like I might be able to do this within "bpy.data" with the ".add" function
    
                for v in range(len(verts)):
                    vert = verts[v]
                    if ((v == 0 and self.vert1) or (v == 1 and self.vert2) or
                       (v == 2 and self.vert3) or (v == 3 and self.vert4)):
    
                        if self.pos:
                            new = bVerts.new()
                            new.co = vert.co - vectors[5 + (v // 2) + ((v % 2) * 2)]
                            bVerts.ensure_lookup_table()
                            bEdges.new((vert, new))
                            bEdges.ensure_lookup_table()
                        if self.neg:
                            new = bVerts.new()
                            new.co = vert.co + vectors[5 + (v // 2) + ((v % 2) * 2)]
                            bVerts.ensure_lookup_table()
                            bEdges.new((vert, new))
                            bEdges.ensure_lookup_table()
    
                bmesh.update_edit_mesh(me)
            except Exception as e:
                error_handlers(self, "mesh.edgetools_ortho", e,
                               reports="Angle Off Edge Operator failed", func=False)
                return {'CANCELLED'}
    
            return {'FINISHED'}
    
    
    # Usage:
    # Select an edge and a point or an edge and specify the radius (default is 1 BU)
    # You can select two edges but it might be unpredictable which edge it revolves
    # around so you might have to play with the switch
    
    class Shaft(Operator):
        bl_idname = "mesh.edgetools_shaft"
        bl_label = "Shaft"
        bl_description = "Create a shaft mesh around an axis"
        bl_options = {'REGISTER', 'UNDO'}
    
        # Selection defaults:
        shaftType = 0
    
        # For tracking if the user has changed selection:
        last_edge: IntProperty(
                name="Last Edge",
                description="Tracks if user has changed selected edges",
                min=0, max=1,
                default=0
                )
        last_flip = False
    
        edge: IntProperty(
                name="Edge",
                description="Edge to shaft around",
                min=0, max=1,
                default=0
                )
        flip: BoolProperty(
                name="Flip Second Edge",
                description="Flip the perceived direction of the second edge",
                default=False
                )
        radius: FloatProperty(
                name="Radius",
                description="Shaft Radius",
                min=0.0, max=1024.0,
                default=1.0
                )
        start: FloatProperty(
                name="Starting Angle",
                description="Angle to start the shaft at",
                min=-360.0, max=360.0,
                default=0.0
                )
        finish: FloatProperty(
                name="Ending Angle",
                description="Angle to end the shaft at",
                min=-360.0, max=360.0,
                default=360.0
                )
        segments: IntProperty(
                name="Shaft Segments",
                description="Number of segments to use in the shaft",
                min=1, max=4096,
                soft_max=512,
                default=32
                )
    
        def draw(self, context):
            layout = self.layout
    
            if self.shaftType == 0:
                layout.prop(self, "edge")
                layout.prop(self, "flip")
            elif self.shaftType == 3:
                layout.prop(self, "radius")
    
            layout.prop(self, "segments")
            layout.prop(self, "start")
            layout.prop(self, "finish")
    
        @classmethod
        def poll(cls, context):
            ob = context.active_object
            return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
    
        def invoke(self, context, event):
            # Make sure these get reset each time we run:
            self.last_edge = 0
            self.edge = 0
    
            return self.execute(context)
    
        def execute(self, context):
            try:
                me = context.object.data
                bm = bmesh.from_edit_mesh(me)
                bm.normal_update()
    
                bFaces = bm.faces
                bEdges = bm.edges
                bVerts = bm.verts
    
                active = None
                edges, verts = [], []
    
                # Pre-caclulated values:
                rotRange = [radians(self.start), radians(self.finish)]
                rads = radians((self.finish - self.start) / self.segments)
    
                numV = self.segments + 1
                numE = self.segments
    
                edges = [e for e in bEdges if e.select]
    
                # Robustness check: there should at least be one edge selected
                if not is_selected_enough(self, edges, 0, edges_n=1, verts_n=0, types="Edge"):
                    return {'CANCELLED'}
    
                # If two edges are selected:
                if len(edges) == 2:
                    # default:
                    edge = [0, 1]
                    vert = [0, 1]
    
                    # By default, we want to shaft around the last selected edge (it
                    # will be the active edge). We know we are using the default if
                    # the user has not changed which edge is being shafted around (as
                    # is tracked by self.last_edge). When they are not the same, then
                    # the user has changed selection.
                    # We then need to make sure that the active object really is an edge
                    # (robustness check)
                    # Finally, if the active edge is not the initial one, we flip them
                    # and have the GUI reflect that
                    if self.last_edge == self.edge:
                        if isinstance(bm.select_history.active, bmesh.types.BMEdge):
                            if bm.select_history.active != edges[edge[0]]:
                                self.last_edge, self.edge = edge[1], edge[1]
                                edge = [edge[1], edge[0]]
                        else:
                            flip_edit_mode()
                            self.report({'WARNING'},
                                        "Active geometry is not an edge. Operation Cancelled")
                            return {'CANCELLED'}
                    elif self.edge == 1:
                        edge = [1, 0]
    
                    verts.append(edges[edge[0]].verts[0])
                    verts.append(edges[edge[0]].verts[1])
    
                    if self.flip:
                        verts = [1, 0]
    
                    verts.append(edges[edge[1]].verts[vert[0]])
                    verts.append(edges[edge[1]].verts[vert[1]])
    
                    self.shaftType = 0
                # If there is more than one edge selected:
                # There are some issues with it ATM, so don't expose is it to normal users
                # @todo Fix edge connection ordering issue
                elif ENABLE_DEBUG and len(edges) > 2:
                    if isinstance(bm.select_history.active, bmesh.types.BMEdge):
                        active = bm.select_history.active
                        edges.remove(active)
                        # Get all the verts:
                        # edges = order_joined_edges(edges[0])
                        verts = []
                        for e in edges:
                            if verts.count(e.verts[0]) == 0:
                                verts.append(e.verts[0])
                            if verts.count(e.verts[1]) == 0:
                                verts.append(e.verts[1])
                    else:
                        flip_edit_mode()
                        self.report({'WARNING'},
                                    "Active geometry is not an edge. Operation Cancelled")
                        return {'CANCELLED'}
                    self.shaftType = 1
                else:
                    verts.append(edges[0].verts[0])
                    verts.append(edges[0].verts[1])
    
                    for v in bVerts:
                        if v.select and verts.count(v) == 0:
                            verts.append(v)
                        v.select = False
                    if len(verts) == 2:
                        self.shaftType = 3
                    else:
                        self.shaftType = 2
    
                # The vector denoting the axis of rotation:
                if self.shaftType == 1:
                    axis = active.verts[1].co - active.verts[0].co
                else:
                    axis = verts[1].co - verts[0].co
    
                # We will need a series of rotation matrices. We could use one which
                # would be faster but also might cause propagation of error
                # matrices = []
                # for i in range(numV):
                #    matrices.append(Matrix.Rotation((rads * i) + rotRange[0], 3, axis))
                matrices = [Matrix.Rotation((rads * i) + rotRange[0], 3, axis) for i in range(numV)]
    
                # New vertice coordinates:
                verts_out = []
    
                # If two edges were selected:
                #  - If the lines are not parallel, then it will create a cone-like shaft
                if self.shaftType == 0:
                    for i in range(len(verts) - 2):
                        init_vec = distance_point_line(verts[i + 2].co, verts[0].co, verts[1].co)
                        co = init_vec + verts[i + 2].co
                        # These will be rotated about the origin so will need to be shifted:
                        for j in range(numV):
                            verts_out.append(co - (matrices[j] @ init_vec))
                elif self.shaftType == 1:
                    for i in verts:
                        init_vec = distance_point_line(i.co, active.verts[0].co, active.verts[1].co)
                        co = init_vec + i.co
                        # These will be rotated about the origin so will need to be shifted:
                        for j in range(numV):
                            verts_out.append(co - (matrices[j] @ init_vec))
                # Else if a line and a point was selected:
                elif self.shaftType == 2:
                    init_vec = distance_point_line(verts[2].co, verts[0].co, verts[1].co)
                    # These will be rotated about the origin so will need to be shifted:
                    verts_out = [
                        (verts[i].co - (matrices[j] @ init_vec)) for i in range(2) for j in range(numV)
                        ]
                else:
                    # Else the above are not possible, so we will just use the edge:
                    #  - The vector defined by the edge is the normal of the plane for the shaft
                    #  - The shaft will have radius "radius"
                    if is_axial(verts[0].co, verts[1].co) is None:
                        proj = (verts[1].co - verts[0].co)
                        proj[2] = 0
                        norm = proj.cross(verts[1].co - verts[0].co)
                        vec = norm.cross(verts[1].co - verts[0].co)
                        vec.length = self.radius
                    elif is_axial(verts[0].co, verts[1].co) == 'Z':
                        vec = verts[0].co + Vector((0, 0, self.radius))
                    else:
                        vec = verts[0].co + Vector((0, self.radius, 0))
                    init_vec = distance_point_line(vec, verts[0].co, verts[1].co)
                    # These will be rotated about the origin so will need to be shifted:
                    verts_out = [
                        (verts[i].co - (matrices[j] @ init_vec)) for i in range(2) for j in range(numV)
                        ]
    
                # We should have the coordinates for a bunch of new verts
                # Now add the verts and build the edges and then the faces
    
                newVerts = []
    
                if self.shaftType == 1:
                    # Vertices:
                    for i in range(numV * len(verts)):
                        new = bVerts.new()
                        new.co = verts_out[i]
                        bVerts.ensure_lookup_table()
                        new.select = True
                        newVerts.append(new)
                    # Edges:
                    for i in range(numE):
                        for j in range(len(verts)):
                            e = bEdges.new((newVerts[i + (numV * j)], newVerts[i + (numV * j) + 1]))
                            bEdges.ensure_lookup_table()
                            e.select = True
                    for i in range(numV):
                        for j in range(len(verts) - 1):
                            e = bEdges.new((newVerts[i + (numV * j)], newVerts[i + (numV * (j + 1))]))
                            bEdges.ensure_lookup_table()
                            e.select = True
    
                    # Faces: There is a problem with this right now
                    """
                    for i in range(len(edges)):
                        for j in range(numE):
                            f = bFaces.new((newVerts[i], newVerts[i + 1],
                                           newVerts[i + (numV * j) + 1], newVerts[i + (numV * j)]))
                            f.normal_update()
                    """
                else:
                    # Vertices:
                    for i in range(numV * 2):
                        new = bVerts.new()
                        new.co = verts_out[i]
                        new.select = True
                        bVerts.ensure_lookup_table()
                        newVerts.append(new)
                    # Edges:
                    for i in range(numE):
                        e = bEdges.new((newVerts[i], newVerts[i + 1]))
                        e.select = True
                        bEdges.ensure_lookup_table()
                        e = bEdges.new((newVerts[i + numV], newVerts[i + numV + 1]))
                        e.select = True
                        bEdges.ensure_lookup_table()
                    for i in range(numV):
                        e = bEdges.new((newVerts[i], newVerts[i + numV]))
                        e.select = True
                        bEdges.ensure_lookup_table()
                    # Faces:
                    for i in range(numE):
                        f = bFaces.new((newVerts[i], newVerts[i + 1],
                                        newVerts[i + numV + 1], newVerts[i + numV]))
                        bFaces.ensure_lookup_table()
                        f.normal_update()
    
                bmesh.update_edit_mesh(me)
    
            except Exception as e:
                error_handlers(self, "mesh.edgetools_shaft", e,
                               reports="Shaft Operator failed", func=False)
                return {'CANCELLED'}
    
            return {'FINISHED'}
    
    
    # "Slices" edges crossing a plane defined by a face
    
    class Slice(Operator):
        bl_idname = "mesh.edgetools_slice"
        bl_label = "Slice"
        bl_description = "Cut edges at the plane defined by a selected face"
        bl_options = {'REGISTER', 'UNDO'}
    
        make_copy: BoolProperty(
                name="Make Copy",
                description="Make new vertices at intersection points instead of splitting the edge",
                default=False
                )
        rip: BoolProperty(
                name="Rip",
                description="Split into two edges that DO NOT share an intersection vertex",
                default=True
                )
        pos: BoolProperty(
                name="Positive",
                description="Remove the portion on the side of the face normal",
                default=False
                )
        neg: BoolProperty(
                name="Negative",
                description="Remove the portion on the side opposite of the face normal",
                default=False
                )
    
        def draw(self, context):
            layout = self.layout
    
            layout.prop(self, "make_copy")
            if not self.make_copy:
                layout.prop(self, "rip")
                layout.label(text="Remove Side:")
                layout.prop(self, "pos")
                layout.prop(self, "neg")
    
        @classmethod
        def poll(cls, context):
            ob = context.active_object
            return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
    
        def invoke(self, context, event):
            return self.execute(context)
    
        def execute(self, context):
            try:
                me = context.object.data
                bm = bmesh.from_edit_mesh(me)
                bm.normal_update()
    
                bVerts = bm.verts
                bEdges = bm.edges
                bFaces = bm.faces
    
                face, normal = None, None
    
                # Find the selected face. This will provide the plane to project onto:
                #  - First check to use the active face. Allows users to just
                #    select a bunch of faces with the last being the cutting plane
                #  - If that fails, then use the first found selected face in the BMesh face list
                if isinstance(bm.select_history.active, bmesh.types.BMFace):
                    face = bm.select_history.active
                    normal = bm.select_history.active.normal
                    bm.select_history.active.select = False
                else:
                    for f in bFaces:
                        if f.select:
                            face = f
                            normal = f.normal
                            f.select = False
                            break
    
                # If we don't find a selected face exit:
                if face is None:
                    flip_edit_mode()
                    self.report({'WARNING'},
                                "Please select a face as the cutting plane. Operation Cancelled")
                    return {'CANCELLED'}
    
                # Warn the user if they are using an n-gon might lead to some odd results
                elif len(face.verts) > 4 and not is_face_planar(face):
                    self.report({'WARNING'},
                                "Selected face is an N-gon.  Results may be unpredictable")
    
                if ENABLE_DEBUG:
                    dbg = 0
                    print("Number of Edges: ", len(bEdges))
    
                for e in bEdges:
                    if ENABLE_DEBUG:
                        print("Looping through Edges - ", dbg)
                        dbg = dbg + 1
    
                    # Get the end verts on the edge:
                    v1 = e.verts[0]
                    v2 = e.verts[1]
    
                    # Make sure that verts are not a part of the cutting plane:
                    if e.select and (v1 not in face.verts and v2 not in face.verts):
                        if len(face.verts) < 5:  # Not an n-gon
                            intersection = intersect_line_face(e, face, True)
                        else:
                            intersection = intersect_line_plane(v1.co, v2.co, face.verts[0].co, normal)
    
                        if ENABLE_DEBUG:
                            print("Intersection: ", intersection)
    
                        # If an intersection exists find the distance of each of the end
                        # points from the plane, with "positive" being in the direction
                        # of the cutting plane's normal. If the points are on opposite
                        # side of the plane, then it intersects and we need to cut it
                        if intersection is not None:
                            bVerts.ensure_lookup_table()
                            bEdges.ensure_lookup_table()
                            bFaces.ensure_lookup_table()
    
                            d1 = distance_point_to_plane(v1.co, face.verts[0].co, normal)
                            d2 = distance_point_to_plane(v2.co, face.verts[0].co, normal)
                            # If they have different signs, then the edge crosses the cutting plane:
                            if abs(d1 + d2) < abs(d1 - d2):
                                # Make the first vertex the positive one:
                                if d1 < d2:
                                    v2, v1 = v1, v2
    
                                if self.make_copy:
                                    new = bVerts.new()
                                    new.co = intersection
                                    new.select = True
                                    bVerts.ensure_lookup_table()
                                elif self.rip:
                                    if ENABLE_DEBUG:
                                        print("Branch rip engaged")
                                    newV1 = bVerts.new()
                                    newV1.co = intersection
                                    bVerts.ensure_lookup_table()
                                    if ENABLE_DEBUG:
                                        print("newV1 created", end='; ')
    
                                    newV2 = bVerts.new()
                                    newV2.co = intersection
                                    bVerts.ensure_lookup_table()
    
                                    if ENABLE_DEBUG:
                                        print("newV2 created", end='; ')
    
                                    newE1 = bEdges.new((v1, newV1))
                                    newE2 = bEdges.new((v2, newV2))
                                    bEdges.ensure_lookup_table()
    
                                    if ENABLE_DEBUG:
                                        print("new edges created", end='; ')
    
                                    if e.is_valid:
                                        bEdges.remove(e)
    
                                    bEdges.ensure_lookup_table()
    
                                    if ENABLE_DEBUG:
                                        print("Old edge removed.\nWe're done with this edge")
                                else:
                                    new = list(bmesh.utils.edge_split(e, v1, 0.5))
                                    bEdges.ensure_lookup_table()
                                    new[1].co = intersection
                                    e.select = False
                                    new[0].select = False
                                    if self.pos:
                                        bEdges.remove(new[0])
                                    if self.neg:
                                        bEdges.remove(e)
                                    bEdges.ensure_lookup_table()
    
                if ENABLE_DEBUG:
                    print("The Edge Loop has exited. Now to update the bmesh")
                    dbg = 0
    
                bmesh.update_edit_mesh(me)
    
            except Exception as e:
                error_handlers(self, "mesh.edgetools_slice", e,
                               reports="Slice Operator failed", func=False)
                return {'CANCELLED'}
    
            return {'FINISHED'}
    
    
    # This projects the selected edges onto the selected plane
    # and/or both points on the selected edge
    
    class Project(Operator):
        bl_idname = "mesh.edgetools_project"
        bl_label = "Project"
        bl_description = ("Projects the selected Vertices/Edges onto a selected plane\n"
                          "(Active is projected onto the rest)")
        bl_options = {'REGISTER', 'UNDO'}
    
        make_copy: BoolProperty(
                name="Make Copy",
                description="Make duplicates of the vertices instead of altering them",
                default=False
                )
    
        def draw(self, context):
            layout = self.layout
            layout.prop(self, "make_copy")
    
        @classmethod
        def poll(cls, context):
            ob = context.active_object
            return (ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
    
        def invoke(self, context, event):
            return self.execute(context)
    
        def execute(self, context):
            try:
                me = context.object.data
                bm = bmesh.from_edit_mesh(me)
                bm.normal_update()
    
                bFaces = bm.faces
                bVerts = bm.verts
    
                fVerts = []
    
                # Find the selected face.  This will provide the plane to project onto:
                # @todo Check first for an active face
                for f in bFaces:
                    if f.select:
                        for v in f.verts:
                            fVerts.append(v)
                        normal = f.normal
                        f.select = False
                        break
    
                for v in bVerts:
                    if v.select:
                        if v in fVerts:
                            v.select = False
                            continue
                        d = distance_point_to_plane(v.co, fVerts[0].co, normal)
                        if self.make_copy:
                            temp = v
                            v = bVerts.new()
                            v.co = temp.co
                            bVerts.ensure_lookup_table()
                        vector = normal
                        vector.length = abs(d)
                        v.co = v.co - (vector * sign(d))
                        v.select = False
    
                bmesh.update_edit_mesh(me)
    
            except Exception as e:
                error_handlers(self, "mesh.edgetools_project", e,
                               reports="Project Operator failed", func=False)
    
                return {'CANCELLED'}
    
            return {'FINISHED'}
    
    
    # Project_End is for projecting/extending an edge to meet a plane
    # This is used be selecting a face to define the plane then all the edges
    # Then move the vertices in the edge that is closest to the
    # plane to the coordinates of the intersection of the edge and the plane
    
    class Project_End(Operator):
        bl_idname = "mesh.edgetools_project_end"
        bl_label = "Project (End Point)"
        bl_description = ("Projects the vertices of the selected\n"
                          "edges closest to a plane onto that plane")
        bl_options = {'REGISTER', 'UNDO'}
    
        make_copy: BoolProperty(
                name="Make Copy",
                description="Make a duplicate of the vertice instead of moving it",
                default=False
                )
        keep_length: BoolProperty(
                name="Keep Edge Length",
                description="Maintain edge lengths",
                default=False
                )
        use_force: BoolProperty(
                name="Use opposite vertices",
                description="Force the usage of the vertices at the other end of the edge",
                default=False
                )
        use_normal: BoolProperty(
                name="Project along normal",
                description="Use the plane's normal as the projection direction",
                default=False
                )
    
        def draw(self, context):
            layout = self.layout
    
            if not self.keep_length:
                layout.prop(self, "use_normal")
            layout.prop(self, "make_copy")
            layout.prop(self, "use_force")
    
        @classmethod
        def poll(cls, context):
            ob = context.active_object
            return(ob and ob.type == 'MESH' and context.mode == 'EDIT_MESH')
    
        def invoke(self, context, event):
            return self.execute(context)
    
        def execute(self, context):
            try:
                me = context.object.data
                bm = bmesh.from_edit_mesh(me)
                bm.normal_update()
    
                bFaces = bm.faces
                bEdges = bm.edges
                bVerts = bm.verts
    
                fVerts = []
    
                # Find the selected face. This will provide the plane to project onto:
                for f in bFaces:
                    if f.select:
                        for v in f.verts:
                            fVerts.append(v)
                        normal = f.normal
                        f.select = False
                        break
    
                for e in bEdges:
                    if e.select:
                        v1 = e.verts[0]
                        v2 = e.verts[1]
                        if v1 in fVerts or v2 in fVerts:
                            e.select = False
                            continue
                        intersection = intersect_line_plane(v1.co, v2.co, fVerts[0].co, normal)
                        if intersection is not None:
                            # Use abs because we don't care what side of plane we're on:
                            d1 = distance_point_to_plane(v1.co, fVerts[0].co, normal)
                            d2 = distance_point_to_plane(v2.co, fVerts[0].co, normal)
                            # If d1 is closer than we use v1 as our vertice:
                            # "xor" with 'use_force':
                            if (abs(d1) < abs(d2)) is not self.use_force:
                                if self.make_copy:
                                    v1 = bVerts.new()
                                    v1.co = e.verts[0].co
                                    bVerts.ensure_lookup_table()
                                    bEdges.ensure_lookup_table()
                                if self.keep_length:
                                    v1.co = intersection
                                elif self.use_normal:
                                    vector = normal
                                    vector.length = abs(d1)
                                    v1.co = v1.co - (vector * sign(d1))
                                else:
                                    v1.co = intersection
                            else:
                                if self.make_copy:
                                    v2 = bVerts.new()
                                    v2.co = e.verts[1].co
                                    bVerts.ensure_lookup_table()
                                    bEdges.ensure_lookup_table()
                                if self.keep_length:
                                    v2.co = intersection
                                elif self.use_normal:
                                    vector = normal
                                    vector.length = abs(d2)
                                    v2.co = v2.co - (vector * sign(d2))
                                else:
                                    v2.co = intersection
                        e.select = False
    
                bmesh.update_edit_mesh(me)
    
            except Exception as e:
                error_handlers(self, "mesh.edgetools_project_end", e,
                               reports="Project (End Point) Operator failed", func=False)
                return {'CANCELLED'}
    
            return {'FINISHED'}
    
    
    class VIEW3D_MT_edit_mesh_edgetools(Menu):
        bl_label = "Edge Tools"
        bl_description = "Various tools for manipulating edges"
    
        def draw(self, context):
            layout = self.layout
    
            layout.operator("mesh.edgetools_extend")
            layout.operator("mesh.edgetools_spline")
            layout.operator("mesh.edgetools_ortho")
            layout.operator("mesh.edgetools_shaft")
            layout.operator("mesh.edgetools_slice")
            layout.separator()
    
            layout.operator("mesh.edgetools_project")
            layout.operator("mesh.edgetools_project_end")
    
    def menu_func(self, context):
        self.layout.menu("VIEW3D_MT_edit_mesh_edgetools")
    
    # define classes for registration
    classes = (
        VIEW3D_MT_edit_mesh_edgetools,
        Extend,
        Spline,
        Ortho,
        Shaft,
        Slice,
        Project,
        Project_End,
        )
    
    
    # registering and menu integration
    def register():
        for cls in classes:
            bpy.utils.register_class(cls)
        bpy.types.VIEW3D_MT_edit_mesh_context_menu.prepend(menu_func)
    
    # unregistering and removing menus
    def unregister():
        for cls in classes:
            bpy.utils.unregister_class(cls)
        bpy.types.VIEW3D_MT_edit_mesh_context_menu.remove(menu_func)
    
    if __name__ == "__main__":
        register()