Skip to content

Commit f1bf901

Browse files
Fix workplane cylinder center when generated using a custom direction (#1593)
* add test for bugfix * fix * format * refactor * format * add test for ValueError * Simplify the code * update testCylinderCenteringAndDirection * apply suggestions --------- Co-authored-by: AU <[email protected]>
1 parent 6b3b14a commit f1bf901

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

cadquery/cq.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4156,7 +4156,7 @@ def cylinder(
41564156
self: T,
41574157
height: float,
41584158
radius: float,
4159-
direct: Vector = Vector(0, 0, 1),
4159+
direct: Union[Tuple[float, float, float], Vector] = Vector(0, 0, 1),
41604160
angle: float = 360,
41614161
centered: Union[bool, Tuple[bool, bool, bool]] = True,
41624162
combine: CombineMode = True,
@@ -4190,7 +4190,6 @@ def cylinder(
41904190
41914191
If combine is false, the result will be a list of the cylinders produced.
41924192
"""
4193-
41944193
if isinstance(centered, bool):
41954194
centered = (centered, centered, centered)
41964195

@@ -4202,7 +4201,10 @@ def cylinder(
42024201
if centered[2]:
42034202
offset += Vector(0, 0, -height / 2)
42044203

4205-
s = Solid.makeCylinder(radius, height, offset, direct, angle)
4204+
# first center and then apply the direction
4205+
s = Solid.makeCylinder(radius, height, offset, Vector(0, 0, 1), angle).moved(
4206+
Plane(Vector(), normal=direct).location
4207+
)
42064208

42074209
# We want a cylinder for each point on the workplane
42084210
return self.eachpoint(lambda loc: s.moved(loc), True, combine, clean)

tests/test_cadquery.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,6 +2606,45 @@ def testCylinderCentering(self):
26062606
s0.val().Center().toTuple(), s1.val().Center().toTuple(), 3
26072607
)
26082608

2609+
def testCylinderCenteringAndDirection(self):
2610+
radius = 10
2611+
height = 40
2612+
2613+
main_directions = [
2614+
(1, 0, 0),
2615+
(0, 1, 0),
2616+
(0, 0, 1),
2617+
(-1, 0, 0),
2618+
(0, -1, 0),
2619+
(0, 0, -1),
2620+
]
2621+
2622+
centered_values = [
2623+
(True, True, True),
2624+
(False, False, False),
2625+
(True, False, False),
2626+
(False, True, False),
2627+
(False, False, True),
2628+
(True, True, False),
2629+
]
2630+
2631+
expected_results = [
2632+
(0, 0, 0),
2633+
(radius, 0.5 * height, radius),
2634+
(0, radius, 0.5 * height),
2635+
(-0.5 * height, 0, -radius),
2636+
(radius, 0, -radius),
2637+
(0, 0, -0.5 * height),
2638+
]
2639+
2640+
for direction, centered, expected_center in zip(
2641+
main_directions, centered_values, expected_results
2642+
):
2643+
s = Workplane("XY").cylinder(
2644+
height, radius, centered=centered, direct=direction,
2645+
)
2646+
self.assertTupleAlmostEquals(s.val().Center().toTuple(), expected_center, 3)
2647+
26092648
def testWedgeDefaults(self):
26102649
s = Workplane("XY").wedge(10, 10, 10, 5, 5, 5, 5)
26112650
self.saveModel(s)
@@ -2909,7 +2948,11 @@ def testClean(self):
29092948
.faces("<Z")
29102949
.workplane()
29112950
.cylinder(
2912-
2, 0.2, centered=(True, True, False), direct=(0, 0, -1), clean=True
2951+
2,
2952+
0.2,
2953+
centered=(True, True, False),
2954+
direct=Vector(0, 0, -1),
2955+
clean=True,
29132956
)
29142957
)
29152958
assert len(s.edges().vals()) == 15
@@ -2976,7 +3019,11 @@ def testNoClean(self):
29763019
.faces("<Z")
29773020
.workplane()
29783021
.cylinder(
2979-
2, 0.2, centered=(True, True, False), direct=(0, 0, -1), clean=False
3022+
2,
3023+
0.2,
3024+
centered=(True, True, False),
3025+
direct=Vector(0, 0, -1),
3026+
clean=False,
29803027
)
29813028
)
29823029
assert len(s.edges().vals()) == 16

0 commit comments

Comments
 (0)