|
| 1 | +import cadquery as cq |
| 2 | + |
| 3 | +from cadquery.occ_impl.geom import Vector |
| 4 | +from cadquery.occ_impl.shape_protocols import ( |
| 5 | + geom_LUT_EDGE, |
| 6 | + geom_LUT_FACE, |
| 7 | +) |
| 8 | + |
| 9 | +from pyparsing import ( |
| 10 | + pyparsing_common, |
| 11 | + Literal, |
| 12 | + Word, |
| 13 | + nums, |
| 14 | + Optional, |
| 15 | + Combine, |
| 16 | + oneOf, |
| 17 | + Group, |
| 18 | + infixNotation, |
| 19 | + opAssoc, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def _makeGrammar(): |
| 24 | + """ |
| 25 | + Define the simple string selector grammar using PyParsing |
| 26 | + """ |
| 27 | + |
| 28 | + # float definition |
| 29 | + point = Literal(".") |
| 30 | + plusmin = Literal("+") | Literal("-") |
| 31 | + number = Word(nums) |
| 32 | + integer = Combine(Optional(plusmin) + number) |
| 33 | + floatn = Combine(integer + Optional(point + Optional(number))) |
| 34 | + |
| 35 | + # vector definition |
| 36 | + lbracket = Literal("(") |
| 37 | + rbracket = Literal(")") |
| 38 | + comma = Literal(",") |
| 39 | + vector = Combine( |
| 40 | + lbracket + floatn("x") + comma + floatn("y") + comma + floatn("z") + rbracket, |
| 41 | + adjacent=False, |
| 42 | + ) |
| 43 | + |
| 44 | + # direction definition |
| 45 | + simple_dir = oneOf( |
| 46 | + ["X", "Y", "Z", "XY", "XZ", "YZ"] + ["x", "y", "z", "xy", "xz", "yz"] |
| 47 | + ) |
| 48 | + direction = simple_dir("simple_dir") | vector("vector_dir") |
| 49 | + |
| 50 | + # CQ type definition |
| 51 | + cqtype = oneOf( |
| 52 | + set(geom_LUT_EDGE.values()) | set(geom_LUT_FACE.values()), caseless=True, |
| 53 | + ) |
| 54 | + cqtype = cqtype.setParseAction(pyparsing_common.upcaseTokens) |
| 55 | + |
| 56 | + # type operator |
| 57 | + type_op = Literal("%") |
| 58 | + |
| 59 | + # direction operator |
| 60 | + direction_op = oneOf([">", "<"]) |
| 61 | + |
| 62 | + # center Nth operator |
| 63 | + center_nth_op = oneOf([">>", "<<"]) |
| 64 | + |
| 65 | + # index definition |
| 66 | + ix_number = Group(Optional("-") + Word(nums)) |
| 67 | + lsqbracket = Literal("[").suppress() |
| 68 | + rsqbracket = Literal("]").suppress() |
| 69 | + |
| 70 | + index = lsqbracket + ix_number("index") + rsqbracket |
| 71 | + |
| 72 | + # other operators |
| 73 | + other_op = oneOf(["|", "#", "+", "-"]) |
| 74 | + |
| 75 | + # named view |
| 76 | + named_view = oneOf(["front", "back", "left", "right", "top", "bottom"]) |
| 77 | + |
| 78 | + return ( |
| 79 | + direction("only_dir") |
| 80 | + | (type_op("type_op") + cqtype("cq_type")) |
| 81 | + | (direction_op("dir_op") + direction("dir") + Optional(index)) |
| 82 | + | (center_nth_op("center_nth_op") + direction("dir") + Optional(index)) |
| 83 | + | (other_op("other_op") + direction("dir")) |
| 84 | + | named_view("named_view") |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +old_getVector = cq.selectors._SimpleStringSyntaxSelector._getVector |
| 89 | + |
| 90 | + |
| 91 | +def _getVector(self, pr): |
| 92 | + if ( |
| 93 | + "simple_dir" in pr |
| 94 | + and pr.simple_dir in cq.selectors._SimpleStringSyntaxSelector.local_axes |
| 95 | + ): |
| 96 | + return cq.selectors._SimpleStringSyntaxSelector.local_axes[pr.simple_dir] |
| 97 | + else: |
| 98 | + return old_getVector(self, pr) |
| 99 | + |
| 100 | + |
| 101 | +class LocalCoordinates: |
| 102 | + def __init__(self, plane): |
| 103 | + self.plane = plane |
| 104 | + self.old_axes = None |
| 105 | + |
| 106 | + def __enter__(self): |
| 107 | + self.old_axes, cq.selectors._SimpleStringSyntaxSelector.local_axes = ( |
| 108 | + cq.selectors._SimpleStringSyntaxSelector.local_axes, |
| 109 | + { |
| 110 | + "x": self.plane.xDir, |
| 111 | + "y": self.plane.yDir, |
| 112 | + "z": self.plane.zDir, |
| 113 | + "xy": self.plane.xDir + self.plane.yDir, |
| 114 | + "yz": self.plane.yDir + self.plane.zDir, |
| 115 | + "xz": self.plane.xDir + self.plane.zDir, |
| 116 | + }, |
| 117 | + ) |
| 118 | + |
| 119 | + def __exit__(self, _exc_type, _exc_value, _traceback): |
| 120 | + cq.selectors._SimpleStringSyntaxSelector.local_axes = self.old_axes |
| 121 | + |
| 122 | + |
| 123 | +def _filter(self, objs, selector): |
| 124 | + selectorObj: Selector |
| 125 | + if selector: |
| 126 | + if isinstance(selector, str): |
| 127 | + with LocalCoordinates(self.plane): |
| 128 | + selectorObj = cq.selectors.StringSyntaxSelector(selector) |
| 129 | + else: |
| 130 | + selectorObj = selector |
| 131 | + toReturn = selectorObj.filter(objs) |
| 132 | + else: |
| 133 | + toReturn = objs |
| 134 | + |
| 135 | + return toReturn |
| 136 | + |
| 137 | + |
| 138 | +cq.selectors._SimpleStringSyntaxSelector.local_axes = { |
| 139 | + "x": Vector(1, 0, 0), |
| 140 | + "y": Vector(0, 1, 0), |
| 141 | + "z": Vector(0, 0, 1), |
| 142 | + "xy": Vector(1, 1, 0), |
| 143 | + "yz": Vector(0, 1, 1), |
| 144 | + "xz": Vector(1, 0, 1), |
| 145 | +} |
| 146 | +cq.selectors._SimpleStringSyntaxSelector._getVector = _getVector |
| 147 | + |
| 148 | +cq.selectors._grammar = _makeGrammar() # make a grammar instance |
| 149 | +cq.selectors._expression_grammar = cq.selectors._makeExpressionGrammar( |
| 150 | + cq.selectors._grammar |
| 151 | +) |
| 152 | + |
| 153 | +cq.Workplane._filter = _filter |
0 commit comments