def test_translation(): ucs = UCS(origin=(3, 4, 5)) assert ucs.origin == (3, 4, 5) assert ucs.ux == (1, 0, 0) assert ucs.uy == (0, 1, 0) assert ucs.uz == (0, 0, 1) assert ucs.from_wcs((3, 4, 5)) == (0, 0, 0) assert ucs.to_wcs((1, 1, 1)) == (4, 5, 6)
def test_ucs_init(): ucs = UCS() assert ucs.origin == (0, 0, 0) assert ucs.ux == (1, 0, 0) assert ucs.uy == (0, 1, 0) assert ucs.uz == (0, 0, 1) assert ucs.from_wcs((3, 4, 5)) == (3, 4, 5) assert ucs.to_wcs((5, 4, 3)) == (5, 4, 3)
def test_arbitrary_ucs(): origin = Vector(3, 3, 3) ux = Vector(1, 2, 0) def_point_in_xy_plane = Vector(3, 10, 4) uz = ux.cross(def_point_in_xy_plane - origin) ucs = UCS(origin=origin, ux=ux, uz=uz) def_point_in_ucs = ucs.from_wcs(def_point_in_xy_plane) assert def_point_in_ucs.z == 0 assert ucs.to_wcs(def_point_in_ucs) == def_point_in_xy_plane assert ucs.is_cartesian is True
def test_rotation(): # normalization is not necessary ux = Vector(1, 2, 0) # only cartesian coord systems work uy = ux.rot_z_deg(90) ucs = UCS(ux=ux, uy=uy) assert ucs.ux == ux.normalize() assert ucs.uy == uy.normalize() assert ucs.uz == (0, 0, 1) assert ucs.is_cartesian is True
def test_constructor_functions(): # does not check the math, because tis would just duplicate the implementation code origin = (3, 3, 3) axis = (1, 0, -1) def_point = (3, 10, 4) ucs = UCS.from_x_axis_and_point_in_xy(origin, axis=axis, point=def_point) assert ucs.is_cartesian assert is_close(ucs.from_wcs(def_point).z, 0) ucs = UCS.from_x_axis_and_point_in_xz(origin, axis=axis, point=def_point) assert ucs.is_cartesian assert is_close(ucs.from_wcs(def_point).y, 0) ucs = UCS.from_y_axis_and_point_in_xy(origin, axis=axis, point=def_point) assert ucs.is_cartesian assert is_close(ucs.from_wcs(def_point).z, 0) ucs = UCS.from_y_axis_and_point_in_yz(origin, axis=axis, point=def_point) assert ucs.is_cartesian assert is_close(ucs.from_wcs(def_point).x, 0) ucs = UCS.from_z_axis_and_point_in_xz(origin, axis=axis, point=def_point) assert ucs.is_cartesian assert is_close(ucs.from_wcs(def_point).y, 0) ucs = UCS.from_z_axis_and_point_in_yz(origin, axis=axis, point=def_point) assert ucs.is_cartesian assert is_close(ucs.from_wcs(def_point).x, 0)
def main(filename): dwg = ezdxf.new('R2010') msp = dwg.modelspace() origin = (3, 3, 3) axis = (1, 0, -1) def_point = (3, 10, 4) ucs = UCS.from_z_axis_and_point_in_yz(origin, axis=axis, point=def_point) ucs.render_axis(msp, length=5) msp.add_point(location=def_point, dxfattribs={'color': 2}) ocs = OCS(ucs.uz) msp.add_circle(center=ocs.from_wcs(origin), radius=1, dxfattribs={ 'color': 2, 'extrusion': ucs.uz, }) dwg.saveas(filename)
def test_spatial_arc_from_3p(): start_point_wcs = Vector(0, 1, 0) end_point_wcs = Vector(1, 0, 0) def_point_wcs = Vector(0, 0, 1) ucs = UCS.from_x_axis_and_point_in_xy(origin=def_point_wcs, axis=end_point_wcs - def_point_wcs, point=start_point_wcs) start_point_ucs = ucs.from_wcs(start_point_wcs) end_point_ucs = ucs.from_wcs(end_point_wcs) def_point_ucs = Vector(0, 0) arc = Arc.from_3p(start_point_ucs, end_point_ucs, def_point_ucs) dwg = ezdxf.new('R12') msp = dwg.modelspace() dxf_arc = arc.add_to_layout(msp, ucs) assert dxf_arc.dxftype() == 'ARC' assert equals_almost(dxf_arc.dxf.radius, 0.81649658) assert equals_almost(dxf_arc.dxf.start_angle, -30.) assert equals_almost(dxf_arc.dxf.end_angle, -150.) assert almost_equal_points(dxf_arc.dxf.extrusion, (0.57735027, 0.57735027, 0.57735027))
dwg = ezdxf.new('R2000') modelspace = dwg.modelspace() # create a 2D arcs in xy-plane delta = 30 for count in range(12): modelspace.add_arc(center=(0, 0), radius=10+count, start_angle=count*delta, end_angle=(count+1)*delta) # create a 3D arc from 3 points in WCS start_point_wcs = Vector(3, 0, 0) end_point_wcs = Vector(0, 3, 0) def_point_wcs = Vector(0, 0, 3) # create UCS ucs = UCS.from_x_axis_and_point_in_xy(origin=def_point_wcs, axis=start_point_wcs-def_point_wcs, point=end_point_wcs) start_point_ucs = ucs.from_wcs(start_point_wcs) end_point_ucs = ucs.from_wcs(end_point_wcs) def_point_ucs = Vector(0, 0) # origin of UCS # create arc in the xy-plane of the UCS arc = Arc.from_3p(start_point_ucs, end_point_ucs, def_point_ucs) arc.add_to_layout(modelspace, ucs, dxfattribs={'color': 1}) # red arc arc = Arc.from_3p(end_point_ucs, start_point_ucs, def_point_ucs) arc.add_to_layout(modelspace, ucs, dxfattribs={'color': 2}) # yellow arc p1 = Vector(0, -18) p2 = Vector(0, +18) arc = Arc.from_2p_angle(p1, p2, 90) arc.add_to_layout(modelspace, dxfattribs={'color': 1})
# Copyright (c) 2018 Manfred Moitzi # License: MIT License import ezdxf from ezdxf.algebra import UCS, Vector dwg = ezdxf.new('R2010') msp = dwg.modelspace() # include-start ucs = UCS(origin=(0, 2, 2), ux=(1, 0, 0), uz=(0, 1, 1)) msp.add_arc( center=ucs.to_ocs((0, 0)), radius=1, start_angle=ucs.to_ocs_angle_deg(45), # shortcut end_angle=ucs.to_ocs_angle_deg(270), # shortcut dxfattribs={ 'extrusion': ucs.uz, 'color': 2, }) center = ucs.to_wcs((0, 0)) msp.add_line( start=center, end=ucs.to_wcs(Vector.from_deg_angle(45)), dxfattribs={'color': 2}, ) msp.add_line( start=center, end=ucs.to_wcs(Vector.from_deg_angle(270)), dxfattribs={'color': 2}, )
# closed cubic b-spline spline_points = next_frame.transform_vectors(spline_points) draw(spline_points) msp.add_text("Closed Cubic R12Spline", dxfattribs={ 'height': .1 }).set_pos(spline_points[0]) R12Spline(spline_points, degree=3, closed=True).render(msp, segments=SEGMENTS, dxfattribs={'color': 3}) if dwg.dxfversion > 'AC1009': msp.add_closed_spline(control_points=spline_points, degree=3, dxfattribs={'color': 4}) # place open cubic b-spline in 3D space ucs = UCS(origin=(10, 3, 3), ux=(1, 0, 0), uz=(0, 1, 1)) # 45 deg rotated around x-axis assert ucs.is_cartesian draw(ucs.points_to_wcs(base_spline_points), extrusion=ucs.uz) msp.add_text("Open Cubic R12Spline in 3D space", dxfattribs={ 'height': .1, 'extrusion': ucs.uz, }).set_pos(ucs.to_ocs(base_spline_points[0])) R12Spline(base_spline_points, degree=3, closed=False).render(msp, segments=SEGMENTS, ucs=ucs, dxfattribs={'color': 3}) dwg.saveas(NAME) print("drawing '%s' created.\n" % NAME)
def test_none_cartesian(): ucs = UCS(ux=(1, 2), uy=(0, 2)) assert ucs.is_cartesian is False
def test_ucs_init_uy_uz(): ucs = UCS(uy=Y_AXIS, uz=Z_AXIS) assert ucs.ux == X_AXIS ucs = UCS(uz=X_AXIS, uy=Z_AXIS) assert ucs.ux == Y_AXIS
def test_ucs_init_ux_uz(): ucs = UCS(ux=X_AXIS, uz=Z_AXIS) assert ucs.uy == Y_AXIS
def test_ucs_init_ux_uy(): ucs = UCS(ux=X_AXIS, uy=Y_AXIS) assert ucs.uz == Z_AXIS ucs = UCS(ux=Y_AXIS, uy=X_AXIS) assert ucs.uz == -Z_AXIS
# Copyright (c) 2018 Manfred Moitzi # License: MIT License # include-start import ezdxf from ezdxf.algebra import UCS, Vector dwg = ezdxf.new('R2010') msp = dwg.modelspace() # thickness for text works only with shx fonts not with true type fonts dwg.styles.new('TXT', dxfattribs={'font': 'romans.shx'}) ucs = UCS(origin=(0, 2, 2), ux=(1, 0, 0), uz=(0, 1, 1)) # calculation of text direction as angle in OCS: # convert text rotation in degree into a vector in UCS text_direction = Vector.from_deg_angle(-45) # transform vector into OCS and get angle of vector in xy-plane rotation = ucs.to_ocs(text_direction).angle_deg text = msp.add_text( text="TEXT", dxfattribs={ # text rotation angle in degrees in OCS 'rotation': rotation, 'extrusion': ucs.uz, 'thickness': .333, 'color': 2, 'style': 'TXT', }) # set text position in OCS
# using an UCS simplifies 3D operations, but UCS definition can happen later # calculating corner points in local (UCS) coordinates without Vector class angle = math.radians(360 / 5) corners_ucs = [(math.cos(angle * n), math.sin(angle * n), 0) for n in range(5)] # let's do some transformations tmatrix = Matrix44.chain( # creating a transformation matrix Matrix44.z_rotate(math.radians(15)), # 1. rotation around z-axis Matrix44.translate(0, .333, .333), # 2. translation ) transformed_corners_ucs = tmatrix.transform_vectors(corners_ucs) # transform UCS into WCS ucs = UCS( origin=(0, 2, 2), # center of pentagon ux=(1, 0, 0), # x-axis parallel to WCS x-axis uz=(0, 1, 1), # z-axis ) corners_wcs = list(ucs.points_to_wcs(transformed_corners_ucs)) msp.add_polyline3d(points=corners_wcs, dxfattribs={ 'closed': True, 'color': 2, }) # add lines from center to corners center_wcs = ucs.to_wcs((0, .333, .333)) for corner in corners_wcs: msp.add_line(center_wcs, corner, dxfattribs={'color': 2}) # include-end
# Copyright (c) 2018 Manfred Moitzi # License: MIT License # include-start import ezdxf from ezdxf.algebra import Vector, UCS dwg = ezdxf.new('R2010') msp = dwg.modelspace() # center point of the pentagon should be (0, 2, 2), and the shape is # rotated around x-axis about 45 degree, to accomplish this I use an # UCS with z-axis (0, 1, 1) and an x-axis parallel to WCS x-axis. ucs = UCS( origin=(0, 2, 2), # center of pentagon ux=(1, 0, 0), # x-axis parallel to WCS x-axis uz=(0, 1, 1), # z-axis ) # calculating corner points in local (UCS) coordinates points = [Vector.from_deg_angle((360 / 5) * n) for n in range(5)] # converting UCS into OCS coordinates ocs_points = list(ucs.points_to_ocs(points)) # LWPOLYLINE accepts only 2D points and has an separated DXF attribute elevation. # All points have the same z-axis (elevation) in OCS! elevation = ocs_points[0].z msp.add_lwpolyline( # LWPOLYLINE point format: (x, y, [start_width, [end_width, [bulge]]]) # the z-axis would be start_width, so remove it points=[p[:2] for p in ocs_points], dxfattribs={