diff --git a/src/build123d/build_sketch.py b/src/build123d/build_sketch.py index 4c7f723..a8076b8 100644 --- a/src/build123d/build_sketch.py +++ b/src/build123d/build_sketch.py @@ -506,6 +506,10 @@ class RegularPolygon(Compound): context: BuildSketch = BuildSketch._get_context(self) context.validate_inputs(self) + if side_count < 3: + raise ValueError( + f"RegularPolygon must has at least three sides not {side_count}" + ) self.radius = radius self.side_count = side_count self.rotation = rotation diff --git a/tests/build_sketch_tests.py b/tests/build_sketch_tests.py index 5fcf681..527e3b6 100644 --- a/tests/build_sketch_tests.py +++ b/tests/build_sketch_tests.py @@ -157,6 +157,23 @@ class BuildSketchObjects(unittest.TestCase): self.assertEqual(r.centered, (True, True)) self.assertEqual(r.mode, Mode.ADD) self.assertAlmostEqual(test.sketch.area, (3 * sqrt(3) / 2) * 2**2, 5) + self.assertTupleAlmostEquals( + test.sketch.faces()[0].normal_at().to_tuple(), (0, 0, 1), 5 + ) + + def test_regular_polygon_matches_polar(self): + for side_count in range(3, 10): + with BuildSketch(): + regular_poly = RegularPolygon(1, side_count) + poly_pts = [v.to_vector() for v in regular_poly.vertices()] + polar_pts = [p.position for p in PolarLocations(1, side_count)] + for poly_pt, polar_pt in zip(poly_pts, polar_pts): + self.assertTupleAlmostEquals(poly_pt.to_tuple(), polar_pt.to_tuple(), 5) + + def test_regular_polygon_min_sides(self): + with self.assertRaises(ValueError): + with BuildSketch(): + RegularPolygon(1, 2) def test_slot_arc(self): with BuildSketch() as test: