Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix workplane cylinder center when generated using a custom direction #1593

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cadquery/cq.py
Original file line number Diff line number Diff line change
Expand Up @@ -4120,6 +4120,18 @@ def cylinder(
If combine is false, the result will be a list of the cylinders produced.
"""

if isinstance(centered, bool):
centered = (centered, centered, centered)

xyz = [
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(-1, 0, 0),
(0, -1, 0),
(0, 0, -1),
]

if isinstance(centered, bool):
centered = (centered, centered, centered)

Expand All @@ -4131,7 +4143,10 @@ def cylinder(
if centered[2]:
offset += Vector(0, 0, -height / 2)

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

# We want a cylinder for each point on the workplane
return self.eachpoint(lambda loc: s.moved(loc), True, combine, clean)
Expand Down
49 changes: 47 additions & 2 deletions tests/test_cadquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2603,6 +2603,43 @@ def testCylinderCentering(self):
s0.val().Center().toTuple(), s1.val().Center().toTuple(), 3
)

def testCylinderCenteringAndDirection(self):
radius = 10
height = 40

for direction in [
(1, 0, 0),
(0, 1, 0),
(0, 0, 1),
(-1, 0, 0),
(0, -1, 0),
(0, 0, -1),
]:
for centered in product([True, False], repeat=3):
s = Workplane("XY").cylinder(
height, radius, direct=Vector(direction), centered=centered,
)
expected_xyz = tuple(
radius * (1 - abs(direction[i])) + 0.5 * height * direction[i]
if not centered[i]
else 0
for i in range(3)
)
self.assertTupleAlmostEquals(
s.val().Center().toTuple(), expected_xyz, 3
)

s = Workplane("XY").cylinder(
height, radius, direct=Vector(-1, 1, 0.5), centered=True
)
expected_xyz = (0, 0, 0)
self.assertTupleAlmostEquals(s.val().Center().toTuple(), expected_xyz, 3)

with raises(ValueError):
Workplane("XY").cylinder(
height, radius, direct=Vector(1, 1, 1), centered=False
)

def testWedgeDefaults(self):
s = Workplane("XY").wedge(10, 10, 10, 5, 5, 5, 5)
self.saveModel(s)
Expand Down Expand Up @@ -2906,7 +2943,11 @@ def testClean(self):
.faces("<Z")
.workplane()
.cylinder(
2, 0.2, centered=(True, True, False), direct=(0, 0, -1), clean=True
2,
0.2,
centered=(True, True, False),
direct=Vector(0, 0, -1),
clean=True,
)
)
assert len(s.edges().vals()) == 15
Expand Down Expand Up @@ -2973,7 +3014,11 @@ def testNoClean(self):
.faces("<Z")
.workplane()
.cylinder(
2, 0.2, centered=(True, True, False), direct=(0, 0, -1), clean=False
2,
0.2,
centered=(True, True, False),
direct=Vector(0, 0, -1),
clean=False,
)
)
assert len(s.edges().vals()) == 16
Expand Down
Loading