def test_non_uniformed_scaled_ucs(): ucs = UCS(origin=(3, 2, 1)).scale(1.5, 2.5, 3.5) assert ucs.is_cartesian is True assert ucs.to_wcs((0, 0, 0)) == (3, 2, 1) assert ucs.to_wcs((1, 0, 0)) == (4.5, 2, 1) assert ucs.to_wcs((0, 1, 0)) == (3, 4.5, 1) assert ucs.to_wcs((0, 0, 1)) == (3, 2, 4.5)
def test_non_uniformed_scaled_ucs_at_origin(): ucs = UCS().scale(1.5, 2.5, 3.5) assert ucs.is_cartesian is True assert ucs.to_wcs((0, 0, 0)) == (0, 0, 0) assert ucs.to_wcs((1, 0, 0)) == (1.5, 0, 0) assert ucs.to_wcs((0, 1, 0)) == (0, 2.5, 0) assert ucs.to_wcs((0, 0, 1)) == (0, 0, 3.5)
def test_uniformed_scaled_ucs(): ucs = UCS(origin=(1, 2, 3)).scale(2, 2, 2) assert ucs.is_cartesian is True assert ucs.to_wcs((0, 0, 0)) == (1, 2, 3) assert ucs.to_wcs((1, 0, 0)) == (3, 2, 3) assert ucs.to_wcs((0, 1, 0)) == (1, 4, 3) assert ucs.to_wcs((0, 0, 1)) == (1, 2, 5)
def test_uniformed_scaled_ucs_at_origin(): ucs = UCS().scale(2, 2, 2) assert ucs.is_cartesian is True assert ucs.to_wcs((0, 0, 0)) == (0, 0, 0) assert ucs.to_wcs((1, 0, 0)) == (2, 0, 0) assert ucs.to_wcs((0, 1, 0)) == (0, 2, 0) assert ucs.to_wcs((0, 0, 1)) == (0, 0, 2)
def transform_to_wcs(self, ucs: UCS) -> None: """ Transform LINE entity from local :class:`~ezdxf.math.UCS` coordinates to :ref:`WCS` coordinates. .. versionadded:: 0.11 """ self.dxf.start = ucs.to_wcs(self.dxf.start) self.dxf.end = ucs.to_wcs(self.dxf.end)
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_to_wcs(): ucs = PassTroughUCS() assert ucs.to_wcs((1, 2, 3)) == Vec3(1, 2, 3) assert ucs.to_wcs((3, 4, 5)) == Vec3(3, 4, 5) ucs2 = UCS() assert ucs.to_wcs((1, 2, 3)) == ucs2.to_wcs((1, 2, 3)) assert ucs.to_wcs((3, 4, 5)) == ucs2.to_wcs((3, 4, 5))
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_matrix44_rotation(): # normalization is not necessary ux = Vec3(1, 2, 0) # only cartesian coord systems work uy = ux.rotate_deg(90) ucs = UCS(ux=ux, uy=uy) m = Matrix44.ucs(ux=ux.normalize(), uy=uy.normalize()) assert m.ux == ux.normalize() assert m.uy == uy.normalize() assert m.uz == (0, 0, 1) assert m.is_cartesian v = m.transform((1, 2, 3)) assert v == ucs.to_wcs((1, 2, 3)) assert m.ucs_vertex_from_wcs(v).isclose((1, 2, 3))
def test_arbitrary_ucs(): origin = Vec3(3, 3, 3) ux = Vec3(1, 2, 0) def_point_in_xy_plane = Vec3(3, 10, 4) uz = ux.cross(def_point_in_xy_plane - origin) ucs = UCS(origin=origin, ux=ux, uz=uz) m = Matrix44.ucs(ucs.ux, ucs.uy, ucs.uz, ucs.origin) def_point_in_ucs = ucs.from_wcs(def_point_in_xy_plane) assert ucs.ux == m.ux assert ucs.uy == m.uy assert ucs.uz == m.uz assert ucs.origin == m.origin assert def_point_in_ucs == m.ucs_vertex_from_wcs(def_point_in_xy_plane) assert def_point_in_ucs.z == 0 assert ucs.to_wcs(def_point_in_ucs).isclose(def_point_in_xy_plane) assert ucs.is_cartesian is True
from pathlib import Path OUT_DIR = Path('~/Desktop/Outbox').expanduser() doc = ezdxf.new('R2010') msp = doc.modelspace() 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), end_angle=ucs.to_ocs_angle_deg(270), dxfattribs={ 'extrusion': ucs.uz, 'color': 1, }) center = ucs.to_wcs((0, 0)) msp.add_line( start=center, end=ucs.to_wcs(Vec3.from_deg_angle(45)), dxfattribs={'color': 1}, ) msp.add_line( start=center, end=ucs.to_wcs(Vec3.from_deg_angle(270)), dxfattribs={'color': 1}, ) ucs.render_axis(msp) doc.saveas(OUT_DIR / 'ocs_arc.dxf')
msp = doc.modelspace() # 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 by UCS transformation_ucs = UCS().rotate_local_z(math.radians(15)) # 1. rotation around z-axis transformation_ucs.shift((0, .333, .333)) # 2. translation (inplace) corners_ucs = list(transformation_ucs.points_to_wcs(corners_ucs)) location_ucs = UCS(origin=(0, 2, 2)).rotate_local_x(math.radians(-45)) msp.add_polyline3d( points=corners_ucs, dxfattribs={ 'closed': True, 'color': 1, } ).transform_to_wcs(location_ucs) # Add lines from the center of the POLYLINE to the corners center_ucs = transformation_ucs.to_wcs((0, 0, 0)) for corner in corners_ucs: msp.add_line( center_ucs, corner, dxfattribs={'color': 1} ).transform_to_wcs(location_ucs) location_ucs.render_axis(msp) doc.saveas(OUT_DIR / 'ucs_polyline3d.dxf')
# 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_vertices(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': 1, }) # 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': 1}) ucs.render_axis(msp) doc.saveas(OUT_DIR / 'ucs_polyline3d.dxf')