Cosplay/nhf/utils.py

53 lines
1.4 KiB
Python
Raw Normal View History

"""
Marking utilities for `Cq.Workplane`
Adds the functions to `Cq.Workplane`:
1. `tagPoint`
2. `tagPlane`
"""
import cadquery as Cq
2024-07-07 21:01:40 -07:00
from typing import Union, Tuple
def tagPoint(self, tag: str):
"""
Adds a vertex that can be used in `Point` constraints.
"""
vertex = Cq.Vertex.makeVertex(0, 0, 0)
self.eachpoint(vertex.moved, useLocalCoordinates=True).tag(tag)
Cq.Workplane.tagPoint = tagPoint
2024-07-07 21:01:40 -07:00
def tagPlane(self, tag: str,
direction: Union[str, Cq.Vector, Tuple[float, float, float]] = '+Z'):
"""
Adds a phantom `Cq.Edge` in the given location which can be referenced in a
`Axis`, `Point`, or `Plane` constraint.
"""
2024-07-07 21:01:40 -07:00
if isinstance(direction, str):
x, y, z = 0, 0, 0
assert len(direction) == 2
sign, axis = direction
if axis in ('z', 'Z'):
z = 1
elif axis in ('y', 'Y'):
y = 1
elif axis in ('x', 'X'):
x = 1
else:
assert False, "Axis must be one of x,y,z"
if sign == '+':
sign = 1
elif sign == '-':
sign = -1
else:
assert False, "Sign must be one of +/-"
v = Cq.Vector(x, y, z) * sign
else:
2024-07-07 21:01:40 -07:00
v = Cq.Vector(direction)
edge = Cq.Edge.makeLine(v * (-1), v)
self.eachpoint(edge.moved, useLocalCoordinates=True).tag(tag)
Cq.Workplane.tagPlane = tagPlane