Last active
July 10, 2024 18:46
-
-
Save aavogt/69de763ea7e9d525b1abfc84ceb27096 to your computer and use it in GitHub Desktop.
decorator version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add_last_vertex(f): | |
"""Call a function with the newest vertex of the workplane as the first argument. | |
For example, add_last_vertex(Line)(p) is equivalent to Line((0,0), p), | |
if there was no last vertex, or if the last vertex was (0,0) | |
""" | |
def wrapped(*args, **kwargs): | |
context = Builder._get_context() | |
loclastpt = Vector() | |
try: | |
lastpt = context.edges().vertices()[-1] | |
loclastpt = context.workplanes[-1].to_local_coords(lastpt) | |
except: | |
pass | |
return f(loclastpt, *args, **kwargs) | |
return wrapped | |
def add_accum(f): | |
"""Turns the argument sequence of offsets into a sequence of points. | |
For example, add_accum(Line)((1,2), (3,4)) becomes Line((1,2), (4,6))""" | |
def wrapped(*args, **kwargs): | |
def add_vec(a,b): | |
return Vector(a) + Vector(b) | |
# TODO only accumulate points that are int/float/VectorLike? | |
pts = itertools.accumulate(flatten_sequence(*args), add_vec) | |
return f(*pts, **kwargs) | |
return wrapped | |
def add_zero_x(f): | |
"""Call a function with a sequence of int/float instead of | |
a sequence of (0,y) tuples. | |
For example, add_zero_x(Line)(1,2) becomes Line((0,1), (0,2)) | |
""" | |
def wrapped(*args, **kwargs): | |
def as_y(y): | |
if isinstance(y, float) or isinstance(y, int): | |
return (0,y) | |
else: | |
return y | |
pts = map(as_y, flatten_sequence(*args)) | |
return f(*pts, **kwargs) | |
return wrapped | |
# relative lines that start at the last point | |
@add_last_vertex | |
@add_accum | |
def rline(*args, **kwargs): | |
"""rline uses relative coordinates to draw a line | |
starting at the previous vertex | |
rline(3) is a horizontal line 3 units long | |
rline((0,3)) is a vertical line, vrline(3) is preferable | |
rline((3,3)) is a diagonal line | |
rline(3, (0,3), 3, (0,3)) is a zigzag | |
""" | |
Line(*args, **kwargs) | |
@add_zero_x | |
@add_last_vertex | |
@add_accum | |
def vrline(*args, **kwargs): | |
Line(*args, **kwargs) | |
# absolute lines that start at the last point | |
@add_zero_x | |
@add_last_vertex | |
def vline(*args, **kwargs): | |
"""vline(y) draws a line to the (0, y) point from the last vertex | |
rather uselessly can take more than two parameters, ie | |
vline(1,2,3) is no different than vline(1,3) | |
""" | |
Line(*args, **kwargs) | |
@add_last_vertex | |
def line(*args, **kwargs): | |
"""draw a line from the last vertex to the given absolute coordinate""" | |
Line(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment