Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Merge e0d6aa0 into ae8eaed
Browse files Browse the repository at this point in the history
  • Loading branch information
bweissinger committed Nov 27, 2018
2 parents ae8eaed + e0d6aa0 commit dcef6fc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
55 changes: 55 additions & 0 deletions cadquery/cq.py
Expand Up @@ -1082,6 +1082,61 @@ def rarray(self, xSpacing, ySpacing, xCount, yCount, center=True):

return self.pushPoints(lpoints)

def polarArray(self, radius, startAngle, angle, count, fill=True):
"""
Creates an polar array of points and pushes them onto the stack.
The 0 degree reference angle is located along the local X-axis.
:param radius: Radius of the array. ( > 0)
:param startAngle: Starting angle (degrees) of array. 0 degrees is
situated along local X-axis. (-360 to 360)
:param angle: The angle (degrees) to fill with elements. A positive
value will fill in the counter-clockwise direction, negative
value fills clockwise. If fill is false, it is the angle
between elements. (!= 0)
:param count: Number of elements in array. ( > 1 )
"""

if radius <= 0:
raise ValueError("Radius must be > 0 ")

if count <= 1:
raise ValueError("Must have more than 1 element in array")

if angle == 0:
raise ValueError("Angle cannot be 0")

if startAngle < -360 or startAngle > 360:
raise ValueError("Start angle must be in range: -360 to 360")

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

"""
Calculate angle between elements, not needed if fill == False. Due to
the first element being located at 0, the actual angle between
elements must be angle / (count - 1). Example, a fill angle of 90 with
4 elements will set elements at 0, 30, 60, and 90 as expected. Angle /
count would place elements at 0, 22.5, 45, and 67.5. If fill angle
is a full circle, the correction is not needed.
"""
if fill is True:
if angle == 360 or angle == -360:
angle = angle / count
else:
angle = angle / (count - 1)

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

return self.pushPoints(points)

def pushPoints(self, pntList):
"""
Pushes a list of points onto the stack as vertices.
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCadQuery.py
Expand Up @@ -519,6 +519,35 @@ def testRectArray(self):
self.saveModel(s)
self.assertEqual(6+NUMX*NUMY*2,s.faces().size()) #6 faces for the box, 2 faces for each cylinder

def testPolarArray(self):
"""
Test for proper placement of elements
"""
elements = 3
radius = 10
size = [2, 2, 2]

# Create box at 0 degree location
reference = Workplane("XY").moveTo(radius, 0).box(*size)

# 2 elements at 0 deg (in reference), and 1 at 180 deg
s = Workplane("XY").polarArray(radius, 0, 180, elements, fill = False) \
.box(*size).union(reference)
self.assertEqual(6 + 6 * (elements - 2), s.faces().size())

# Add box at 90 degree location
reference = Workplane("XY").moveTo(0, -radius).box(*size)

# Elements at 0 (in reference), -90 (in reference), and 180 degrees
s = Workplane("XY").polarArray(radius, 0, -180, elements) \
.box(*size).union(reference)
self.assertEqual(12 + 6 * (elements - 2), s.faces().size())

# Elements at 0 (in box), 120, and 240
s = Workplane("XY").polarArray(radius, 0, 360, elements) \
.box(*size).union(reference)
self.assertEqual(12 + 6 * (elements - 1), s.faces().size())

def testNestedCircle(self):
s = Workplane("XY").box(40,40,5).pushPoints([(10,0),(0,10)]).circle(4).circle(2).extrude(4)
self.saveModel(s)
Expand Down

0 comments on commit dcef6fc

Please sign in to comment.