def setup_csys(blk, size=3): # draw axis blk.add_line((0, 0), (size, 0), dxfattribs={'color': RED}) # x-axis blk.add_line((0, 0), (0, size), dxfattribs={'color': GREEN}) # y-axis blk.add_line((0, 0), (0, 0, size), dxfattribs={'color': BLUE}) # z-axis # place text size2 = size / 2 txt_props = { 'style': 'OpenSans', 'height': size / 2, 'color': RED, } # XY-plane text blk.add_text('XY', dxfattribs=txt_props).set_pos((size2, size2), align='MIDDLE_CENTER') # YZ-plane text ucs = UCS(ux=(0, 1, 0), uy=(0, 0, 1)) txt_props['extrusion'] = ucs.uz txt_props['color'] = GREEN blk.add_text('YZ', dxfattribs=txt_props).set_pos(ucs.to_ocs( (size2, size2)), align='MIDDLE_CENTER') # XZ-plane text ucs = UCS(ux=(1, 0, 0), uy=(0, 0, 1)) txt_props['extrusion'] = ucs.uz txt_props['color'] = BLUE txt_props['text_generation_flag'] = MIRROR_X blk.add_text('XZ', dxfattribs=txt_props).set_pos(ucs.to_ocs( (size2, size2)), align='MIDDLE_CENTER')
# Copyright (c) 2018-2020 Manfred Moitzi # License: MIT License import ezdxf from ezdxf.math import UCS, Vec3 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}, )
# include-start import ezdxf from ezdxf.math import UCS, Vector doc = ezdxf.new('R2010') msp = doc.modelspace() # thickness for text works only with shx fonts not with true type fonts doc.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 text.set_pos(ucs.to_ocs((0, 0, 0)), align='MIDDLE_CENTER') # include-end
# Copyright (c) 2018 Manfred Moitzi # License: MIT License import ezdxf from ezdxf.math 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}, )
'extrusion': ucs.uz, 'rotation': rotation, }) # To rotate a block reference around the block x-axis, # you have to find the rotated z-axis (= extrusion vector) # of the rotated block reference: # t is a transformation matrix to rotate 15 degree around the x-axis t = Matrix44.axis_rotate(axis=X_AXIS, angle=math.radians(15)) # transform block z-axis into new UCS z-axis (= extrusion vector) uz = Vec3(t.transform(Z_AXIS)) # create new UCS at the insertion point, because we are rotating around the x-axis, # ux is the same as the WCS x-axis and uz is the rotated z-axis. ucs = UCS(origin=(1, 2, 0), ux=X_AXIS, uz=uz) # transform insert location to OCS, block base_point=(0, 0, 0) insert = ucs.to_ocs((0, 0, 0)) # for this case a rotation around the z-axis is not required rotation = 0 blockref = msp.add_blockref('CSYS', insert, dxfattribs={ 'extrusion': ucs.uz, 'rotation': rotation, }) # translate a block references with an established OCS translation = Vec3(-3, -1, 1) # get established OCS ocs = blockref.ocs() # get insert location in WCS actual_wcs_location = ocs.to_wcs(blockref.dxf.insert)
def main(): doc = ezdxf.new('R2010', setup=True) blk = doc.blocks.new('CSYS') setup_csys(blk) msp = doc.modelspace() # The DXF attribute `rotation` rotates a block reference always around the block z-axis: # To rotate the block reference around the WCS x-axis, # you have to transform the block z-axis into the WCS x-axis: # rotate block axis 90 deg ccw around y-axis, by using an UCS ucs = ucs_rotation(UCS(), axis=Y_AXIS, angle=90) # transform insert location, not required for (0, 0, 0) insert = ucs.to_ocs((0, 0, 0)) # rotation angle about the z-axis (= WCS x-axis) rotation = ucs.to_ocs_angle_deg(15) # msp.add_blockref('CSYS', insert, dxfattribs={ # 'extrusion': ucs.uz, # 'rotation': rotation, # }) # To rotate a block reference around the block x-axis, # you have to find the rotated z-axis (= extrusion vector) # of the rotated block reference: # t is a transformation matrix to rotate 15 degree around the x-axis t = Matrix44.axis_rotate(axis=X_AXIS, angle=math.radians(15)) # transform block z-axis into new UCS z-axis (= extrusion vector) uz = Vector(t.transform(Z_AXIS)) # create new UCS at the insertion point, because we are rotating around the x-axis, # ux is the same as the WCS x-axis and uz is the rotated z-axis. ucs = UCS(origin=(1, 2, 0), ux=X_AXIS, uz=uz) # transform insert location to OCS, block base_point=(0, 0, 0) insert = ucs.to_ocs((0, 0, 0)) # for this case a rotation around the z-axis is not required rotation = 0 blockref = msp.add_blockref('CSYS', insert, dxfattribs={ 'extrusion': ucs.uz, 'rotation': rotation, }) # translate a block references with an established OCS translation = Vector(-3, -1, 1) # get established OCS ocs = blockref.ocs() # get insert location in WCS actual_wcs_location = ocs.to_wcs(blockref.dxf.insert) # translate location new_wcs_location = actual_wcs_location + translation # convert WCS location to OCS location blockref.dxf.insert = ocs.from_wcs(new_wcs_location) # rotate a block references with an established OCS around the block y-axis about 90 degree ocs = blockref.ocs() # convert block y-axis (= rotation axis) into WCS vector rotation_axis = ocs.to_wcs((0, 1, 0)) # convert local z-axis (=extrusion vector) into WCS vector local_z_axis = ocs.to_wcs((0, 0, 1)) # build transformation matrix t = Matrix44.axis_rotate(axis=rotation_axis, angle=math.radians(-90)) uz = t.transform(local_z_axis) uy = rotation_axis # the block reference origin stays at the same location, no rotation needed wcs_insert = ocs.to_wcs(blockref.dxf.insert) # build new UCS to convert WCS locations and angles into OCS ucs = UCS(origin=wcs_insert, uy=uy, uz=uz) # set new OCS blockref.dxf.extrusion = ucs.uz # set new insert blockref.dxf.insert = ucs.to_ocs((0, 0, 0)) # set new rotation: we do not rotate the block reference around the local z-axis, # but the new block x-axis (0 deg) differs from OCS x-axis and has to be adjusted blockref.dxf.rotation = ucs.to_ocs_angle_deg(0) doc.set_modelspace_vport(5) doc.saveas('ocs_insert.dxf')
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)
"height": 0.1 }).set_pos(spline_points[0]) R12Spline(spline_points, degree=3, closed=True).render(msp, segments=SEGMENTS, dxfattribs={"color": 3}) if doc.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(spline_points), extrusion=ucs.uz) msp.add_text( "Open Cubic R12Spline in 3D space", dxfattribs={ "height": 0.1, "extrusion": ucs.uz, }, ).set_pos(ucs.to_ocs(Vec3(spline_points[0]))) R12Spline(spline_points, degree=3, closed=False).render(msp, segments=SEGMENTS, ucs=ucs, dxfattribs={"color": 3}) doc.saveas(NAME) print("drawing '%s' created.\n" % NAME)
def test_points_to_ocs(): ucs = UCS(ux=(0, 0, -1), uz=(1, 0, 0)) points = [(1, 2, 3), (4, 5, 6), (9, 8, 7)] expected = [ucs.to_ocs(p) for p in points] result = list(ucs.points_to_ocs(points)) assert result == expected
def test_to_ocs(): ucs = UCS(ux=(0, 0, -1), uz=(1, 0, 0)) assert ucs.is_cartesian is True assert ucs.to_ocs((1, 0, 0)).isclose((0, -1, 0))