# 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}, )
# 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}, )
# 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(OUT_DIR / 'ocs_insert.dxf')
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')