Local rotation + test fixes

This commit is contained in:
adam-urbanczyk
2020-07-09 18:16:17 +02:00
parent 0a3b4c1621
commit 48c75cff3c
2 changed files with 35 additions and 18 deletions

View File

@ -1344,6 +1344,7 @@ class Workplane(object):
angle: float, angle: float,
count: int, count: int,
fill: bool = True, fill: bool = True,
rotate=True,
) -> "Workplane": ) -> "Workplane":
""" """
Creates an polar array of points and pushes them onto the stack. Creates an polar array of points and pushes them onto the stack.
@ -1356,15 +1357,23 @@ class Workplane(object):
value will fill in the counter-clockwise direction. If fill is value will fill in the counter-clockwise direction. If fill is
false, angle is the angle between elements. false, angle is the angle between elements.
:param count: Number of elements in array. ( > 0 ) :param count: Number of elements in array. ( > 0 )
:param fill: Interpret the angle as total if True (default: True).
:param rotate: Rorate every item (default: True).
""" """
if count <= 0: if count <= 0:
raise ValueError("No elements in array") raise ValueError("No elements in array")
# First element at start angle, convert to cartesian coords # First element at start angle, convert to cartesian coords
x = radius * math.cos(math.radians(startAngle)) x = radius * math.sin(math.radians(startAngle))
y = radius * math.sin(math.radians(startAngle)) y = radius * math.cos(math.radians(startAngle))
points = [Location(Vector(x, y), Vector(0, 0, 1), startAngle)]
if rotate:
loc = Location(Vector(x, y), Vector(0, 0, 1), -startAngle)
else:
loc = Location(Vector(x, y))
locs = [loc]
# Calculate angle between elements # Calculate angle between elements
if fill: if fill:
@ -1376,14 +1385,19 @@ class Workplane(object):
# Add additional elements # Add additional elements
for i in range(1, count): for i in range(1, count):
phi = math.radians(startAngle + (angle * i)) phi_deg = startAngle + (angle * i)
x = radius * math.cos(phi) phi = math.radians(phi_deg)
y = radius * math.sin(phi) x = radius * math.sin(phi)
points.append( y = radius * math.cos(phi)
Location(Vector(x, y), Vector(0, 0, 1), startAngle + (angle * i))
)
return self.pushPoints(points) if rotate:
loc = Location(Vector(x, y), Vector(0, 0, 1), -phi_deg)
else:
loc = Location(Vector(x, y))
locs.append(loc)
return self.pushPoints(locs)
def pushPoints(self, pntList: Iterable[Union[VectorLike, Location]]) -> "Workplane": def pushPoints(self, pntList: Iterable[Union[VectorLike, Location]]) -> "Workplane":
""" """

View File

@ -969,25 +969,28 @@ class TestCadQuery(BaseTest):
s = Workplane("XY").polarArray(radius, 0, 180, 6) s = Workplane("XY").polarArray(radius, 0, 180, 6)
self.assertEqual(6, s.size()) self.assertEqual(6, s.size())
to_x = lambda l: l.wrapped.Transformation().TranslationPart().X()
to_y = lambda l: l.wrapped.Transformation().TranslationPart().Y()
# Test for proper placement when fill == True # Test for proper placement when fill == True
s = Workplane("XY").polarArray(radius, 0, 180, 3) s = Workplane("XY").polarArray(radius, 0, 180, 3)
self.assertAlmostEqual(0, s.objects[1].x) self.assertAlmostEqual(0, to_y(s.objects[1]))
self.assertAlmostEqual(radius, s.objects[1].y) self.assertAlmostEqual(radius, to_x(s.objects[1]))
# Test for proper placement when angle to fill is multiple of 360 deg # Test for proper placement when angle to fill is multiple of 360 deg
s = Workplane("XY").polarArray(radius, 0, 360, 4) s = Workplane("XY").polarArray(radius, 0, 360, 4)
self.assertAlmostEqual(0, s.objects[1].x) self.assertAlmostEqual(0, to_y(s.objects[1]))
self.assertAlmostEqual(radius, s.objects[1].y) self.assertAlmostEqual(radius, to_x(s.objects[1]))
# Test for proper placement when fill == False # Test for proper placement when fill == False
s = Workplane("XY").polarArray(radius, 0, 90, 3, fill=False) s = Workplane("XY").polarArray(radius, 0, 90, 3, fill=False)
self.assertAlmostEqual(0, s.objects[1].x) self.assertAlmostEqual(0, to_y(s.objects[1]))
self.assertAlmostEqual(radius, s.objects[1].y) self.assertAlmostEqual(radius, to_x(s.objects[1]))
# Test for proper operation of startAngle # Test for proper operation of startAngle
s = Workplane("XY").polarArray(radius, 90, 180, 3) s = Workplane("XY").polarArray(radius, 90, 180, 3)
self.assertAlmostEqual(0, s.objects[0].x) self.assertAlmostEqual(radius, to_x(s.objects[0]))
self.assertAlmostEqual(radius, s.objects[0].y) self.assertAlmostEqual(0, to_y(s.objects[0]))
def testNestedCircle(self): def testNestedCircle(self):
s = ( s = (