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

Trapezoid width is now fully controlled by width param #430

Merged
merged 2 commits into from
Dec 11, 2023
Merged
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
29 changes: 24 additions & 5 deletions src/build123d/objects_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,13 +621,32 @@ def __init__(
reduction_right = (
0 if right_side_angle == 90 else height / tan(radians(right_side_angle))
)
if reduction_left + reduction_right >= width:

top_width_left = width / 2
top_width_right = width / 2
bot_width_left = width / 2
bot_width_right = width / 2

if reduction_left > 0:
top_width_left -= reduction_left
else:
bot_width_left += reduction_left

if reduction_right > 0:
top_width_right -= reduction_right
else:
bot_width_right += reduction_right

if (bot_width_left + bot_width_right) < 0:
raise ValueError("Trapezoid bottom invalid - change angles")
if (top_width_left + top_width_right) < 0:
raise ValueError("Trapezoid top invalid - change angles")

pts = []
pts.append(Vector(-width / 2, -height / 2))
pts.append(Vector(width / 2, -height / 2))
pts.append(Vector(width / 2 - reduction_right, height / 2))
pts.append(Vector(-width / 2 + reduction_left, height / 2))
pts.append(Vector(-bot_width_left, -height / 2))
pts.append(Vector(bot_width_right, -height / 2))
pts.append(Vector(top_width_right, height / 2))
pts.append(Vector(-top_width_left, height / 2))
pts.append(pts[0])
face = Face.make_from_wires(Wire.make_polygon(pts))
super().__init__(face, rotation, self.align, mode)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_build_sketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,18 @@ def test_trapezoid(self):
with BuildSketch() as test:
Trapezoid(6, 2, 30)

with self.assertRaises(ValueError):
with BuildSketch() as test:
Trapezoid(6, 2, 150)

with BuildSketch() as test:
t = Trapezoid(12,8,135,90)
self.assertEqual(t.width, 12)
self.assertEqual(t.trapezoid_height, 8)
self.assertEqual(t.left_side_angle, 135)
self.assertEqual(t.right_side_angle, 90)
self.assertAlmostEqual(test.sketch.area, 8 * (12 + 4) / 2, 5)

def test_triangle(self):
tri = Triangle(a=3, b=4, c=5)
self.assertAlmostEqual(tri.area, (3 * 4) / 2, 5)
Expand Down
Loading