Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polarArray start angle, and rotation fix (#1006) #1016

Merged
merged 2 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions cadquery/cq.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
CQObject = Union[Vector, Location, Shape, Sketch]
VectorLike = Union[Tuple[float, float], Tuple[float, float, float], Vector]
CombineMode = Union[bool, Literal["cut", "a", "s"]] # a : additive, s: subtractive
TOL = 1e-6

T = TypeVar("T", bound="Workplane")
"""A type variable used to make the return type of a method the same as the
Expand Down Expand Up @@ -1486,51 +1487,42 @@ def polarArray(
rotate: bool = True,
) -> T:
"""
Creates an polar array of points and pushes them onto the stack.
The 0 degree reference angle is located along the local X-axis.
Creates a polar array of points and pushes them onto the stack.
The zero degree reference angle is located along the local X-axis.

:param radius: Radius of the array.
:param startAngle: Starting angle (degrees) of array. 0 degrees is
situated along local X-axis.
:param startAngle: Starting angle (degrees) of array. Zero degrees is
situated along the local X-axis.
:param angle: The angle (degrees) to fill with elements. A positive
value will fill in the counter-clockwise direction. If fill is
false, angle is the angle between elements.
:param count: Number of elements in array. ( > 0 )
False, angle is the angle between elements.
:param count: Number of elements in array. (count >= 1)
:param fill: Interpret the angle as total if True (default: True).
:param rotate: Rotate every item (default: True).
"""

if count <= 0:
raise ValueError("No elements in array")

# First element at start angle, convert to cartesian coords
x = radius * math.sin(math.radians(startAngle))
y = radius * math.cos(math.radians(startAngle))

if rotate:
loc = Location(Vector(x, y), Vector(0, 0, 1), -startAngle)
else:
loc = Location(Vector(x, y))

locs = [loc]
if count < 1:
raise ValueError(f"At least 1 element required, requested {count}")

# Calculate angle between elements
if fill:
if angle % 360 == 0:
if abs(math.remainder(angle, 360)) < TOL:
angle = angle / count
elif count > 1:
else:
# Inclusive start and end
angle = angle / (count - 1)
angle = angle / (count - 1) if count > 1 else startAngle

locs = []

# Add additional elements
for i in range(1, count):
# Add elements
for i in range(0, count):
phi_deg = startAngle + (angle * i)
phi = math.radians(phi_deg)
x = radius * math.sin(phi)
y = radius * math.cos(phi)
x = radius * math.cos(phi)
y = radius * math.sin(phi)

if rotate:
loc = Location(Vector(x, y), Vector(0, 0, 1), -phi_deg)
loc = Location(Vector(x, y), Vector(0, 0, 1), phi_deg)
else:
loc = Location(Vector(x, y))

Expand Down
50 changes: 36 additions & 14 deletions tests/test_cadquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -1392,12 +1392,6 @@ def testRectArray(self):
def testPolarArray(self):
radius = 10

# Test for proper number of elements
s = Workplane("XY").polarArray(radius, 0, 180, 1)
self.assertEqual(1, s.size())
s = Workplane("XY").polarArray(radius, 0, 180, 6)
self.assertEqual(6, s.size())

to_x = lambda l: l.wrapped.Transformation().TranslationPart().X()
to_y = lambda l: l.wrapped.Transformation().TranslationPart().Y()
to_angle = (
Expand All @@ -1408,23 +1402,27 @@ def testPolarArray(self):

# Test for proper placement when fill == True
s = Workplane("XY").polarArray(radius, 0, 180, 3)
self.assertAlmostEqual(0, to_y(s.objects[1]))
self.assertAlmostEqual(radius, to_x(s.objects[1]))
self.assertEqual(3, s.size())
self.assertAlmostEqual(radius, to_x(s.objects[0]))
self.assertAlmostEqual(0, to_y(s.objects[0]))

# Test for proper placement when angle to fill is multiple of 360 deg
s = Workplane("XY").polarArray(radius, 0, 360, 4)
self.assertAlmostEqual(0, to_y(s.objects[1]))
self.assertAlmostEqual(radius, to_x(s.objects[1]))
self.assertEqual(4, s.size())
self.assertAlmostEqual(radius, to_x(s.objects[0]))
self.assertAlmostEqual(0, to_y(s.objects[0]))

# Test for proper placement when fill == False
s = Workplane("XY").polarArray(radius, 0, 90, 3, fill=False)
self.assertAlmostEqual(0, to_y(s.objects[1]))
self.assertAlmostEqual(radius, to_x(s.objects[1]))
self.assertEqual(3, s.size())
self.assertAlmostEqual(-radius, to_x(s.objects[2]))
self.assertAlmostEqual(0, to_y(s.objects[2]))

# Test for proper operation of startAngle
s = Workplane("XY").polarArray(radius, 90, 180, 3)
self.assertAlmostEqual(radius, to_x(s.objects[0]))
self.assertAlmostEqual(0, to_y(s.objects[0]))
self.assertEqual(3, s.size())
self.assertAlmostEqual(0, to_x(s.objects[0]))
self.assertAlmostEqual(radius, to_y(s.objects[0]))

# Test for local rotation
s = Workplane().polarArray(radius, 0, 180, 3)
Expand All @@ -1435,6 +1433,21 @@ def testPolarArray(self):
self.assertAlmostEqual(0, to_angle(s.objects[0]))
self.assertAlmostEqual(0, to_angle(s.objects[1]))

with raises(ValueError):
Workplane().polarArray(radius, 20, 180, 0)

s = Workplane().polarArray(radius, 20, 0, 1)
assert s.size() == 1
assert Workplane().polarLine(radius, 20).val().positionAt(
1
).toTuple() == approx(s.val().toTuple()[0])

s = Workplane().center(2, -4).polarArray(2, 10, 50, 3).rect(1.0, 0.5).extrude(1)
assert s.solids().size() == 3
assert s.vertices(">Y and >Z").val().toTuple() == approx(
(3.0334936490538906, -1.7099364905389036, 1.0)
)

def testNestedCircle(self):
s = (
Workplane("XY")
Expand Down Expand Up @@ -2003,6 +2016,15 @@ def testPolarLines(self):
self.assertEqual(1, r.wires().size())
self.assertEqual(5, r.wires().edges().size())

r = Workplane().polarLineTo(1, 20)
assert r.val().positionAt(1).toTuple() == approx(
(0.9396926207859084, 0.3420201433256687, 0.0)
)
r = Workplane().move(1, 1).polarLine(1, 20)
assert r.val().positionAt(1).toTuple() == approx(
(1.9396926207859084, 1.3420201433256687, 0.0)
)

def testLargestDimension(self):
"""
Tests the largestDimension function when no solids are on the stack and when there are
Expand Down