Merge branch 'dev' into callable-predicates

This commit is contained in:
gumyr 2023-04-18 10:50:24 -04:00 committed by GitHub
commit ee85858126
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
225 changed files with 20336 additions and 8546 deletions

View file

@ -34,8 +34,11 @@ from build123d.build_enums import (
SortBy,
Until,
)
from build123d.build_part import Box, BuildPart, Extrude
from build123d.build_sketch import BuildSketch, Circle, Rectangle, RegularPolygon
from build123d.build_part import BuildPart
from build123d.operations_part import extrude
from build123d.objects_part import Box
from build123d.build_sketch import BuildSketch
from build123d.objects_sketch import Circle, Rectangle, RegularPolygon
from build123d.geometry import (
Axis,
BoundBox,
@ -852,7 +855,7 @@ class TestFace(DirectApiTestCase):
with BuildPart() as test:
with BuildSketch():
RegularPolygon(1, 3)
Extrude(amount=1)
extrude(amount=1)
self.assertEqual(test.faces().sort_by(Axis.Z).last.geometry, "POLYGON")
def test_negate(self):
@ -1767,19 +1770,50 @@ class TestPlane(DirectApiTestCase):
self.assertVectorAlmostEquals(plane.z_dir, z_dir, 5)
def test_plane_init(self):
# from origin
o = (0, 0, 0)
x = (1, 0, 0)
y = (0, 1, 0)
z = (0, 0, 1)
planes = [
Plane(o),
Plane(o, x),
Plane(o, x, z),
Plane(o, x, z_dir=z),
Plane(o, x_dir=x, z_dir=z),
Plane(o, x_dir=x),
Plane(o, z_dir=z),
Plane(origin=o, x_dir=x, z_dir=z),
Plane(origin=o, x_dir=x),
Plane(origin=o, z_dir=z),
]
for p in planes:
self.assertVectorAlmostEquals(p.origin, o, 6)
self.assertVectorAlmostEquals(p.x_dir, x, 6)
self.assertVectorAlmostEquals(p.y_dir, y, 6)
self.assertVectorAlmostEquals(p.z_dir, z, 6)
with self.assertRaises(TypeError):
Plane()
with self.assertRaises(TypeError):
Plane(o, z_dir=1)
# rotated location around z
loc = Location((0, 0, 0), (0, 0, 45))
p = Plane(loc)
self.assertVectorAlmostEquals(p.origin, (0, 0, 0), 6)
self.assertVectorAlmostEquals(
p.x_dir, (math.sqrt(2) / 2, math.sqrt(2) / 2, 0), 6
)
self.assertVectorAlmostEquals(
p.y_dir, (-math.sqrt(2) / 2, math.sqrt(2) / 2, 0), 6
)
self.assertVectorAlmostEquals(p.z_dir, (0, 0, 1), 6)
self.assertVectorAlmostEquals(loc.position, p.to_location().position, 6)
self.assertVectorAlmostEquals(loc.orientation, p.to_location().orientation, 6)
p_from_loc = Plane(loc)
p_from_named_loc = Plane(location=loc)
for p in [p_from_loc, p_from_named_loc]:
self.assertVectorAlmostEquals(p.origin, (0, 0, 0), 6)
self.assertVectorAlmostEquals(
p.x_dir, (math.sqrt(2) / 2, math.sqrt(2) / 2, 0), 6
)
self.assertVectorAlmostEquals(
p.y_dir, (-math.sqrt(2) / 2, math.sqrt(2) / 2, 0), 6
)
self.assertVectorAlmostEquals(p.z_dir, (0, 0, 1), 6)
self.assertVectorAlmostEquals(loc.position, p.to_location().position, 6)
self.assertVectorAlmostEquals(
loc.orientation, p.to_location().orientation, 6
)
# rotated location around x and origin <> (0,0,0)
loc = Location((0, 2, -1), (45, 0, 0))
@ -1798,9 +1832,10 @@ class TestPlane(DirectApiTestCase):
# from a face
f = Face.make_rect(1, 2).located(Location((1, 2, 3), (45, 0, 45)))
p_from_face = Plane(f)
p_from_named_face = Plane(face=f)
plane_from_gp_pln = Plane(gp_pln=p_from_face.wrapped)
p_deep_copy = copy.deepcopy(p_from_face)
for p in [p_from_face, plane_from_gp_pln, p_deep_copy]:
for p in [p_from_face, p_from_named_face, plane_from_gp_pln, p_deep_copy]:
self.assertVectorAlmostEquals(p.origin, (1, 2, 3), 6)
self.assertVectorAlmostEquals(p.x_dir, (math.sqrt(2) / 2, 0.5, 0.5), 6)
self.assertVectorAlmostEquals(p.y_dir, (-math.sqrt(2) / 2, 0.5, 0.5), 6)
@ -1814,6 +1849,21 @@ class TestPlane(DirectApiTestCase):
f.location.orientation, p.to_location().orientation, 6
)
# from a face with x_dir
f = Face.make_rect(1, 2)
x = (1, 1)
y = (-1, 1)
planes = [
Plane(f, x),
Plane(f, x_dir=x),
Plane(face=f, x_dir=x),
]
for p in planes:
self.assertVectorAlmostEquals(p.origin, (0, 0, 0), 6)
self.assertVectorAlmostEquals(p.x_dir, Vector(x).normalized(), 6)
self.assertVectorAlmostEquals(p.y_dir, Vector(y).normalized(), 6)
self.assertVectorAlmostEquals(p.z_dir, (0, 0, 1), 6)
with self.assertRaises(TypeError):
Plane(Edge.make_line((0, 0), (0, 1)))
@ -2123,8 +2173,9 @@ class TestShape(DirectApiTestCase):
)
with self.assertRaises(ValueError):
box = Solid.make_box(1, 1, 1)
invalid_object = box.fillet(0.75, box.edges())
invalid_object.max_fillet(invalid_object.edges())
box.fillet(0.75, box.edges())
# invalid_object = box.fillet(0.75, box.edges())
# invalid_object.max_fillet(invalid_object.edges())
def test_locate_bb(self):
bounding_box = Solid.make_cone(1, 2, 1).bounding_box()
@ -2329,6 +2380,32 @@ class TestShapeList(DirectApiTestCase):
with self.assertRaises(KeyError):
result.group("C")
def test_distance(self):
with BuildPart() as box:
Box(1, 2, 3)
obj = (-0.2, 0.1, 0.5)
edges = box.edges().sort_by_distance(obj)
distances = [Vertex(*obj).distance_to(edge) for edge in edges]
self.assertTrue(
all([distances[i] >= distances[i - 1] for i in range(1, len(edges))])
)
def test_distance_reverse(self):
with BuildPart() as box:
Box(1, 2, 3)
obj = (-0.2, 0.1, 0.5)
edges = box.edges().sort_by_distance(obj, reverse=True)
distances = [Vertex(*obj).distance_to(edge) for edge in edges]
self.assertTrue(
all([distances[i] <= distances[i - 1] for i in range(1, len(edges))])
)
def test_distance_equal(self):
with BuildPart() as box:
Box(1, 1, 1)
self.assertEqual(len(box.edges().sort_by_distance((0, 0, 0))), 12)
class TestShell(DirectApiTestCase):
def test_shell_init(self):
@ -2342,7 +2419,7 @@ class TestShell(DirectApiTestCase):
self.assertVectorAlmostEquals(box_shell.center(), (0.5, 0.5, 0.5), 5)
class TestSolid(unittest.TestCase):
class TestSolid(DirectApiTestCase):
def test_make_solid(self):
box_faces = Solid.make_box(1, 1, 1).faces()
box_shell = Shell.make_shell(box_faces)
@ -2353,11 +2430,19 @@ class TestSolid(unittest.TestCase):
def test_extrude_with_taper(self):
base = Face.make_rect(1, 1)
pyramid = Solid.extrude_linear(base, normal=(0, 0, 1), taper=1)
pyramid = Solid.extrude_linear(base, normal=(0, 0, 1), taper=10)
self.assertLess(
pyramid.faces().sort_by(Axis.Z)[-1].area,
pyramid.faces().sort_by(Axis.Z)[0].area,
)
self.assertVectorAlmostEquals(pyramid.bounding_box().size, (1, 1, 1), 5)
inner = Solid.extrude_linear(
Face.make_rect(0.5, 0.5), normal=(0, 0, 1), taper=10
)
hollow_base = Face.make_rect(1, 1).cut(Face.make_rect(0.5, 0.5)).faces()[0]
hollow_pyramid = Solid.extrude_linear(hollow_base, normal=(0, 0, 1), taper=10)
self.assertAlmostEqual(hollow_pyramid.volume + inner.volume, pyramid.volume, 4)
def test_extrude_linear_with_rotation(self):
# Face
@ -2540,6 +2625,26 @@ class TestVector(DirectApiTestCase):
self.assertEqual(a, b)
self.assertEqual(a, c)
def test_vector_distance(self):
"""
Test line distance from plane.
"""
v = Vector(1, 2, 3)
self.assertAlmostEqual(1, v.signed_distance_from_plane(Plane.YZ))
self.assertAlmostEqual(2, v.signed_distance_from_plane(Plane.ZX))
self.assertAlmostEqual(3, v.signed_distance_from_plane(Plane.XY))
self.assertAlmostEqual(-1, v.signed_distance_from_plane(Plane.ZY))
self.assertAlmostEqual(-2, v.signed_distance_from_plane(Plane.XZ))
self.assertAlmostEqual(-3, v.signed_distance_from_plane(Plane.YX))
self.assertAlmostEqual(1, v.distance_to_plane(Plane.YZ))
self.assertAlmostEqual(2, v.distance_to_plane(Plane.ZX))
self.assertAlmostEqual(3, v.distance_to_plane(Plane.XY))
self.assertAlmostEqual(1, v.distance_to_plane(Plane.ZY))
self.assertAlmostEqual(2, v.distance_to_plane(Plane.XZ))
self.assertAlmostEqual(3, v.distance_to_plane(Plane.YX))
def test_vector_project(self):
"""
Test line projection and plane projection methods of Vector
@ -2571,9 +2676,7 @@ class TestVector(DirectApiTestCase):
)
def test_vector_not_implemented(self):
v = Vector(1, 2, 3)
with self.assertRaises(NotImplementedError):
v.distance_to_plane()
pass
def test_vector_special_methods(self):
v = Vector(1, 2, 3)