Add angle parameter to chamfer

This commit is contained in:
Mayhem 2023-10-09 19:52:03 +08:00
parent cbb827d271
commit 2b8d3678b2
2 changed files with 33 additions and 0 deletions

View file

@ -28,6 +28,7 @@ license:
"""
import copy
import logging
from math import radians, tan
from typing import Union, Iterable
from build123d.build_common import Builder, LocationList, WorkplaneList, validate_inputs
@ -256,6 +257,7 @@ def chamfer(
objects: Union[ChamferFilletType, Iterable[ChamferFilletType]],
length: float,
length2: float = None,
angle: float = None,
) -> Union[Sketch, Part]:
"""Generic Operation: chamfer
@ -267,6 +269,7 @@ def chamfer(
objects (Union[Edge,Vertex] or Iterable of): edges or vertices to chamfer
length (float): chamfer size
length2 (float, optional): asymmetric chamfer size. Defaults to None.
angle (float, optional): chamfer angle in degrees. Defaults to None.
Raises:
ValueError: no objects provided
@ -274,6 +277,12 @@ def chamfer(
ValueError: objects must be Vertices
"""
context: Builder = Builder._get_context("chamfer")
if length2 and angle:
raise ValueError("Only one of length2 or angle should be provided")
if angle:
length2 = length * tan(radians(angle))
length2 = length if length2 is None else length2
if (objects is None and context is None) or (

View file

@ -216,6 +216,18 @@ class ChamferTests(unittest.TestCase):
chamfer(test.edges(), length=1)
self.assertLess(test.part.volume, 1000)
def test_part_chamfer_asym_length(self):
with BuildPart() as test:
Box(10, 10, 10)
chamfer(test.edges().filter_by(Axis.Z), length=1, length2=sqrt(3))
self.assertAlmostEqual(test.part.volume, 1000 - 4 * 0.5 * 10 * sqrt(3), 5)
def test_part_chamfer_asym_angle(self):
with BuildPart() as test:
Box(10, 10, 10)
chamfer(test.edges().filter_by(Axis.Z), length=1, angle=60)
self.assertAlmostEqual(test.part.volume, 1000 - 4 * 0.5 * 10 * sqrt(3), 5)
def test_sketch_chamfer(self):
with BuildSketch() as test:
Rectangle(10, 10)
@ -231,6 +243,18 @@ class ChamferTests(unittest.TestCase):
)
self.assertAlmostEqual(test.sketch.area, 200 - 4 * 0.5, 5)
def test_sketch_chamfer_asym_length(self):
with BuildSketch() as test:
Rectangle(10, 10)
chamfer(test.vertices(), length=1, length2=sqrt(3))
self.assertAlmostEqual(test.sketch.area, 100 - 4 * 0.5 * sqrt(3), 5)
def test_sketch_chamfer_asym_angle(self):
with BuildSketch() as test:
Rectangle(10, 10)
chamfer(test.vertices(), length=1, angle=60)
self.assertAlmostEqual(test.sketch.area, 100 - 4 * 0.5 * sqrt(3), 5)
def test_errors(self):
with self.assertRaises(TypeError):
with BuildLine():