def test_add_curves3(): path = Path() c1 = Bezier3P(((0, 0), (1, 1), (2, 0))) c2 = Bezier3P(((2, 0), (1, -1), (0, 0))) path.add_curves3([c1, c2]) assert len(path) == 2 assert path.end == (0, 0)
def test_approximate_curves(): path = Path() path.curve_to((2, 0), (0, 1), (2, 1)) vertices = list(path.approximate(10)) assert len(vertices) == 11 assert vertices[0] == (0, 0) assert vertices[-1] == (2, 0)
def test_add_curves(): path = Path() c1 = Bezier4P(((0, 0, 0), (0, 1, 0), (2, 1, 0), (2, 0, 0))) c2 = Bezier4P(((2, 0, 0), (2, -1, 0), (0, -1, 0), (0, 0, 0))) path.add_curves([c1, c2]) assert len(path) == 2 assert path.end == (0, 0, 0)
def test_control_vertices(p1): vertices = list(p1.control_vertices()) assert vertices == Vector.list([(0, 0), (2, 0), (2, 1), (4, 1), (4, 0)]) path = Path() assert len(list(path.control_vertices())) == 0 path = Path.from_vertices([(0, 0), (1, 0)]) assert len(list(path.control_vertices())) == 2
def test_add_curves_with_gap(): path = Path() c1 = Bezier4P(((0, 0, 0), (0, 1, 0), (2, 1, 0), (2, 0, 0))) c2 = Bezier4P(((2, -1, 0), (2, -2, 0), (0, -2, 0), (0, -1, 0))) path.add_curves([c1, c2]) assert len(path) == 3 # added a line segment between curves assert path.end == (0, -1, 0)
def test_add_curves3_with_gap(): path = Path() c1 = Bezier3P(((0, 0), (1, 1), (2, 0))) c2 = Bezier3P(((2, -1), (3, -2), (0, -1))) path.add_curves3([c1, c2]) assert len(path) == 3 # added a line segment between curves assert path.end == (0, -1)
def test_one_path_line_to(self): path = Path() path.line_to((1, 0)) result = transform_paths([path], Matrix44()) path0 = result[0] assert path0[0].type == Command.LINE_TO assert path0.start == (0, 0) assert path0.end == (1, 0)
def test_to_ocs(self): p = Path((0, 1, 1)) p.line_to((0, 1, 3)) ocs = OCS((1, 0, 0)) # x-Axis result = list(transform_paths_to_ocs([p], ocs)) p0 = result[0] assert ocs.from_wcs((0, 1, 1)) == p0.start assert ocs.from_wcs((0, 1, 3)) == p0[0].end
def test_approximate_lines(): path = Path() path.line_to((1, 1)) path.line_to((2, 0)) vertices = list(path.approximate()) assert len(vertices) == 3 assert vertices[0] == path.start == (0, 0) assert vertices[2] == path.end == (2, 0)
def test_one_path_curve4_to(self): path = Path() path.curve4_to((2, 0), (0, 1), (2, 1)) result = transform_paths([path], Matrix44()) path0 = result[0] assert path0[0].type == Command.CURVE4_TO assert len(path0[0]) == 3 assert path0.start == (0, 0) assert path0.end == (2, 0)
def test_one_quadratic_bezier(self): p = Path() p.curve3_to((4, 0), (2, 2)) result = list(to_bsplines_and_vertices(p)) assert len(result) == 1, "expected one B-spline" cpnts = result[0].control_points # A quadratic bezier should be converted to cubic bezier curve, which # has a precise cubic B-spline representation. assert len(cpnts) == 4, "expected 4 control vertices" assert cpnts[0] == (0, 0) assert cpnts[3] == (4, 0)
def test_lwpolyine_lines(): from ezdxf.entities import LWPolyline pline = LWPolyline() pline.append_points([(1, 1), (2, 1), (2, 2)], format='xy') path = Path.from_lwpolyline(pline) assert path.start == (1, 1) assert path.end == (2, 2) assert len(path) == 2 pline.dxf.elevation = 1.0 path = Path.from_lwpolyline(pline) assert path.start == (1, 1, 1) assert path.end == (2, 2, 1)
def test_polyine_lines(): from ezdxf.entities import Polyline pline = Polyline() pline.append_formatted_vertices([(1, 1), (2, 1), (2, 2)], format='xy') path = Path.from_polyline(pline) assert path.start == (1, 1) assert path.end == (2, 2) assert len(path) == 2 pline.dxf.elevation = (0, 0, 1) path = Path.from_polyline(pline) assert path.start == (1, 1, 1) assert path.end == (2, 2, 1)
def test_multiple_segments(self): p = Path() p.curve4_to((4, 0), (1, 2), (3, 2)) p.line_to((6, 0)) p.curve3_to((8, 0), (7, 1)) result = list(to_bsplines_and_vertices(p)) assert len(result) == 3, "expected three segments"
def test_path_from_hatch_polyline_path_without_bulge(): polyline_path = PolylinePath() polyline_path.set_vertices([(0, 0), (0, 1), (1, 1), (1, 0)], is_closed=False) path = Path.from_hatch_polyline_path(polyline_path) assert len(path) == 3 assert path.start == (0, 0) assert path.end == (1, 0) polyline_path.is_closed = True path = Path.from_hatch_polyline_path(polyline_path) assert len(path) == 4 assert path.start == (0, 0) assert path.end == (0, 0)
def test_from_spline(): from ezdxf.entities import Spline spline = Spline.new() spline.fit_points = [(2, 0), (4, 1), (6, -1), (8, 0)] path = Path.from_spline(spline) assert path.start == (2, 0) assert path.end == (8, 0)
def test_lwpolyine_s_shape(): from ezdxf.entities import LWPolyline pline = LWPolyline() pline.append_points(S_SHAPE, format='xyb') path = Path.from_lwpolyline(pline) assert path.start == (0, 0) assert path.end == (5, 2) # closed assert any(cmd.type == Command.CURVE_TO for cmd in path)
def test_from_circle_with_zero_radius(): from ezdxf.entities import Circle circle = Circle.new(dxfattribs={ 'center': (1, 0, 0), 'radius': 0, }) path = Path.from_circle(circle) assert len(path) == 0
def test_3d_polyine(): from ezdxf.entities import Polyline pline = Polyline.new(dxfattribs={'flags': Polyline.POLYLINE_3D}) pline.append_vertices([(1, 1, 1), (2, 1, 3), (2, 2, 2)]) path = Path.from_polyline(pline) assert path.start == (1, 1, 1) assert path.end == (2, 2, 2) assert len(path) == 2
def test_has_clockwise_orientation(): # basic has_clockwise_orientation() function is tested in: # test_617_clockwise_orientation path = Path.from_vertices([(0, 0), (1, 0), (1, 1), (0, 1)]) assert path.has_clockwise_orientation() is True path = Path() path.line_to((2, 0)) path.curve_to((4, 0), (2, 1), (4, 1)) # end, ctrl1, ctrl2 assert path.has_clockwise_orientation() is False
def test_lwpolyine_with_bulges(): from ezdxf.entities import LWPolyline pline = LWPolyline() pline.closed = True pline.append_points(POINTS, format='xyb') path = Path.from_lwpolyline(pline) assert path.start == (0, 0) assert path.end == (0, 0) # closed assert any(cmd[0] == Command.CURVE_TO for cmd in path)
def test_polyine_with_bulges(): from ezdxf.entities import Polyline pline = Polyline() pline.close(True) pline.append_formatted_vertices(POINTS, format='xyb') path = Path.from_polyline(pline) assert path.start == (0, 0) assert path.end == (0, 0) # closed assert any(cmd.type == Command.CURVE_TO for cmd in path)
def test_approximate_curves(): path = Path() path.curve3_to((2, 0), (1, 1)) path.curve4_to((3, 0), (2, 1), (3, 1)) vertices = list(path.approximate(20)) assert len(vertices) == 41 assert vertices[0] == (0, 0) assert vertices[-1] == (3, 0)
def draw_path(self, path: Path, properties) -> None: """ Fall-back implementation, approximate path by line segments. Override in inherited back-end for a more efficient implementation. """ if len(path): if properties.filling: self.draw_filled_polygon( path.approximate(segments=self.bezier_approximation_count), properties, ) else: vertices = iter( path.approximate(segments=self.bezier_approximation_count)) prev = next(vertices) for vertex in vertices: self.draw_line(prev, vertex, properties) prev = vertex
def test_from_circle(radius): from ezdxf.entities import Circle circle = Circle.new(dxfattribs={ 'center': (1, 0, 0), 'radius': radius, }) path = Path.from_circle(circle) assert path.start == (2, 0) assert path.end == (2, 0) assert path.is_closed is True
def test_from_ellipse(): from ezdxf.entities import Ellipse from ezdxf.math import ConstructionEllipse e = ConstructionEllipse(center=(3, 0), major_axis=(1, 0), ratio=0.5, start_param=0, end_param=math.pi) ellipse = Ellipse.new() ellipse.apply_construction_tool(e) path = Path.from_ellipse(ellipse) assert path.start == (4, 0) assert path.end == (2, 0)
def test_path_from_hatch_polyline_path_with_bulge(): polyline_path = PolylinePath() polyline_path.set_vertices([(0, 0), (1, 0, 0.5), (2, 0), (3, 0)], is_closed=False) path = Path.from_hatch_polyline_path(polyline_path) assert len(path) == 4 assert path.start == (0, 0) assert path.end == (3, 0) assert path[1].type == Command.CURVE_TO assert path[1].end == (1.5, -0.25)
def test_one_path_multiple_command(self): path = Path() path.line_to((1, 0)) path.curve3_to((2, 0), (2.5, 1)) path.curve4_to((3, 0), (2, 1), (3, 1)) result = transform_paths([path], Matrix44()) path0 = result[0] assert path0[0].type == Command.LINE_TO assert path0[1].type == Command.CURVE3_TO assert path0[2].type == Command.CURVE4_TO assert path0.start == (0, 0) assert path0.end == (3, 0)
def test_from_arc(): from ezdxf.entities import Arc arc = Arc.new(dxfattribs={ 'center': (1, 0, 0), 'radius': 1, 'start_angle': 0, 'end_angle': 180, }) path = Path.from_arc(arc) assert path.start == (2, 0) assert path.end == (0, 0)
def test_transform(): path = Path() path.line_to((2, 0)) path.curve_to((4, 0), (2, 1), (4, 1)) p2 = path.transform(Matrix44.translate(1, 1, 0)) assert p2.start == (1, 1) assert p2[0][1] == (3, 1) # line to location assert p2[1][1] == (5, 1) # cubic to location assert p2[1][2] == (3, 2) # cubic ctrl1 assert p2[1][3] == (5, 2) # cubic ctrl2 assert p2.end == (5, 1)