Enhancing Triangle Issue#765

This commit is contained in:
gumyr 2024-11-06 20:53:50 -05:00
parent 3dbc873c4f
commit f3fa230c52
2 changed files with 48 additions and 3 deletions

View file

@ -37,7 +37,17 @@ from build123d.build_common import LocationList, flatten_sequence, validate_inpu
from build123d.build_enums import Align, FontStyle, Mode
from build123d.build_sketch import BuildSketch
from build123d.geometry import Axis, Location, Rotation, Vector, VectorLike
from build123d.topology import Compound, Edge, Face, ShapeList, Sketch, Wire, tuplify
from build123d.topology import (
Compound,
Edge,
Face,
ShapeList,
Sketch,
Wire,
tuplify,
TOLERANCE,
topo_explore_common_vertex,
)
class BaseSketchObject(Sketch):
@ -720,3 +730,25 @@ class Triangle(BaseSketchObject):
triangle.move(Location(-center_of_geometry))
alignment = None if align is None else tuplify(align, 2)
super().__init__(obj=triangle, rotation=rotation, align=alignment, mode=mode)
self.edge_a = self.edges().filter_by(lambda e: abs(e.length - a) < TOLERANCE)[
0
] #: edge 'a'
self.edge_b = self.edges().filter_by(
lambda e: abs(e.length - b) < TOLERANCE and e not in [self.edge_a]
)[
0
] #: edge 'b'
self.edge_c = self.edges().filter_by(
lambda e: e not in [self.edge_a, self.edge_b]
)[
0
] #: edge 'c'
self.vertex_A = topo_explore_common_vertex(
self.edge_b, self.edge_c
) #: vertex 'A'
self.vertex_B = topo_explore_common_vertex(
self.edge_a, self.edge_c
) #: vertex 'B'
self.vertex_C = topo_explore_common_vertex(
self.edge_a, self.edge_b
) #: vertex 'C'

View file

@ -27,7 +27,7 @@ license:
"""
import unittest
from math import pi, sqrt
from math import pi, sqrt, atan2, degrees
from build123d import *
@ -395,8 +395,21 @@ class TestBuildSketchObjects(unittest.TestCase):
self.assertAlmostEqual(test.sketch.area, 8 * (12 + 4) / 2, 5)
def test_triangle(self):
tri = Triangle(a=3, b=4, c=5)
tri = Triangle(a=3, b=4, c=5, align=Align.MIN)
self.assertAlmostEqual(tri.area, (3 * 4) / 2, 5)
self.assertAlmostEqual(tri.A, degrees(atan2(3, 4)), 5)
self.assertAlmostEqual(tri.B, degrees(atan2(4, 3)), 5)
self.assertAlmostEqual(tri.C, 90, 5)
self.assertAlmostEqual(tri.a, 3, 5)
self.assertAlmostEqual(tri.b, 4, 5)
self.assertAlmostEqual(tri.c, 5, 5)
self.assertAlmostEqual(tri.edge_a.length, 3, 5)
self.assertAlmostEqual(tri.edge_b.length, 4, 5)
self.assertAlmostEqual(tri.edge_c.length, 5, 5)
self.assertTupleAlmostEquals(tri.vertex_A, (3, 4, 0), 5)
self.assertTupleAlmostEquals(tri.vertex_B, (0, 0, 0), 5)
self.assertTupleAlmostEquals(tri.vertex_C, (3, 0, 0), 5)
tri = Triangle(c=5, C=90, a=3)
self.assertAlmostEqual(tri.area, (3 * 4) / 2, 5)