Skip to content

Commit 5e95846

Browse files
committed
Sketch parray fix
* Start angle and rotation convention to be consistent with arc #1001 * Fix parray bug when used with push * Return n locations for 360 degree array * Fix n=1
1 parent 9e9b452 commit 5e95846

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

cadquery/sketch.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
cast as tcast,
1414
)
1515
from typing_extensions import Literal
16-
from math import tan, sin, cos, pi, radians
16+
from math import tan, sin, cos, pi, radians, remainder
1717
from itertools import product, chain
1818
from multimethod import multimethod
1919
from typish import instance_of, get_type
@@ -336,46 +336,43 @@ def rarray(self: T, xs: Real, ys: Real, nx: int, ny: int) -> T:
336336
for el in selection
337337
)
338338

339-
def parray(self: T, r: Real, a1: Real, a2: Real, n: int, rotate: bool = True) -> T:
339+
def parray(self: T, r: Real, a1: Real, da: Real, n: int, rotate: bool = True) -> T:
340340
"""
341341
Generate a polar array of locations.
342342
"""
343343

344344
if n < 1:
345-
raise ValueError(f"At least 1 elements required, requested {n}")
345+
raise ValueError(f"At least 1 element required, requested {n}")
346346

347-
x = r * sin(radians(a1))
348-
y = r * cos(radians(a1))
347+
locs = []
349348

350-
if rotate:
351-
loc = Location(Vector(x, y), Vector(0, 0, 1), -a1)
349+
if abs(remainder(da, 360)) < 1e-3:
350+
angle = da / n
352351
else:
353-
loc = Location(Vector(x, y))
354-
355-
locs = [loc]
352+
angle = da / (n - 1) if n > 1 else a1
356353

357-
angle = (a2 - a1) / (n - 1)
358-
359-
for i in range(1, n):
354+
for i in range(0, n):
360355
phi = a1 + (angle * i)
361-
x = r * sin(radians(phi))
362-
y = r * cos(radians(phi))
363-
364-
if rotate:
365-
loc = Location(Vector(x, y), Vector(0, 0, 1), -phi)
366-
else:
367-
loc = Location(Vector(x, y))
356+
x = r * cos(radians(phi))
357+
y = r * sin(radians(phi))
368358

359+
loc = Location(Vector(x, y))
369360
locs.append(loc)
370361

371362
if self._selection:
372363
selection: Sequence[Union[Shape, Location, Vector]] = self._selection
373364
else:
374-
selection = [Vector()]
365+
selection = [Location()]
375366

376367
return self.push(
377-
(l * el if isinstance(el, Location) else l * Location(el.Center()))
378-
for l in locs
368+
(
369+
l
370+
* el
371+
* Location(
372+
Vector(0, 0), Vector(0, 0, 1), (a1 + (angle * i)) * int(rotate)
373+
)
374+
)
375+
for i, l in enumerate(locs)
379376
for el in selection
380377
)
381378

tests/test_sketch.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,50 @@ def test_distribute():
145145
s7 = Sketch().parray(2, 0, 90, 3, False).rect(0.5, 0.5).reset().vertices(">(1,1,0)")
146146

147147
assert len(s7._selection) == 1
148+
assert s7._selection[0].toTuple() == approx(
149+
(1.6642135623730951, 1.664213562373095, 0.0)
150+
)
148151

149152
s8 = Sketch().push([(0, 0), (0, 1)]).parray(2, 0, 90, 3).rect(0.5, 0.5)
150-
s8.reset().faces(">(1,1,0)")
153+
s8.reset().faces(">(0,1,0)")
151154

152155
assert s8._selection[0].Center().Length == approx(3)
153156

154157
s9 = Sketch().push([(0, 1)], tag="loc")
155158

156159
assert len(s9._tags["loc"]) == 1
157160

161+
s10 = Sketch().push([(-4, 1), (0, 0), (4, -1)]).parray(2, 10, 50, 3).rect(1.0, 0.5)
162+
s10.reset().vertices(">(-1,0,0)")
163+
164+
assert s10._selection[0].toTuple() == approx(
165+
(-3.46650635094611, 2.424038105676658, 0.0)
166+
)
167+
168+
s10.reset().vertices(">(1,0,0)")
169+
170+
assert s10._selection[0].toTuple() == approx(
171+
(6.505431426947252, -0.8120814940857262, 0.0)
172+
)
173+
174+
s11 = Sketch().parray(1, 135, 0, 1).circle(0.1)
175+
s11.reset().faces()
176+
177+
assert len(s11._selection) == 1
178+
assert s11._selection[0].Center().toTuple() == approx(
179+
(-0.7071067811865475, 0.7071067811865476, 0.0)
180+
)
181+
182+
s12 = Sketch().parray(4, 20, 360, 6).rect(1.0, 0.5)
183+
184+
assert len(s12._faces.Faces()) == 6
185+
186+
s12.reset().vertices(">(0,-1,0)")
187+
188+
assert s12._selection[0].toTuple() == approx(
189+
(-0.5352148612481344, -4.475046932971669, 0.0)
190+
)
191+
158192

159193
def test_each():
160194

0 commit comments

Comments
 (0)