Skip to content

Commit 5147dd8

Browse files
authored
Merge pull request #1005 from lorenzncode/sketch-parray
Sketch parray fix
2 parents 9e9b452 + d4070b4 commit 5147dd8

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

cadquery/sketch.py

Lines changed: 24 additions & 24 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,36 +336,27 @@ 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-6:
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:
@@ -374,9 +365,18 @@ def parray(self: T, r: Real, a1: Real, a2: Real, n: int, rotate: bool = True) ->
374365
selection = [Vector()]
375366

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

382382
def distribute(
@@ -387,7 +387,7 @@ def distribute(
387387
"""
388388

389389
if not self._selection:
390-
raise ValueError("Nothing selected to distirbute over")
390+
raise ValueError("Nothing selected to distribute over")
391391

392392
params = [start + i * (stop - start) / n for i in range(n + 1)]
393393

tests/test_sketch.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,68 @@ 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+
192+
s13 = (
193+
Sketch()
194+
.push([(-4, 1)])
195+
.circle(0.1)
196+
.reset()
197+
.faces()
198+
.parray(2, 10, 50, 3)
199+
.rect(1.0, 0.5, 40, "a", "rects")
200+
)
201+
202+
assert len(s13._faces.Faces()) == 4
203+
204+
s13.reset().vertices(">(-1,0,0)", tag="rects")
205+
206+
assert s13._selection[0].toTuple() == approx(
207+
(-3.3330260270865173, 3.1810426396582487, 0.0)
208+
)
209+
158210

159211
def test_each():
160212

0 commit comments

Comments
 (0)