# Copyright (c) 2020-2021 Manfred Moitzi
# License: MIT License

from pathlib import Path
import math
import ezdxf
from ezdxf import zoom
from ezdxf.math import UCS

DIR = Path("~/Desktop/Outbox").expanduser()

p = [
    (0, 0, 0),
    (1, 0, 0),
    (1, 1, 0),
    (0, 1, 0),
    (0, 0, 1),
    (1, 0, 1),
    (1, 1, 1),
    (0, 1, 1),
]

doc = ezdxf.new()
msp = doc.modelspace()
block = doc.blocks.new("block_4m3")

cube = block.add_mesh()
with cube.edit_data() as mesh_data:
    mesh_data.add_face([p[0], p[1], p[2], p[3]])
    mesh_data.add_face([p[4], p[5], p[6], p[7]])
    mesh_data.add_face([p[0], p[1], p[5], p[4]])
    mesh_data.add_face([p[1], p[2], p[6], p[5]])
    mesh_data.add_face([p[3], p[2], p[6], p[7]])
    mesh_data.add_face([p[0], p[3], p[7], p[4]])
    mesh_data.optimize()


# Place untransformed cube, don't use the rotation
# attribute unless you really need it, just
# transform the UCS.
blockref = msp.add_blockref(name="block_4m3", insert=(0, 0, 0))

# First rotation about the local x-axis
ucs = UCS().rotate_local_x(angle=math.radians(45))
# same as a rotation around the WCS x-axis:
# ucs = UCS().rotate(axis=(1, 0, 0), angle=math.radians(45))

# Second rotation about the WCS z-axis
ucs = ucs.rotate(axis=(0, 0, 1), angle=math.radians(45))

# Last step transform block reference from UCS to WCS
blockref.transform(ucs.matrix)

zoom.extents(msp)
doc.saveas(DIR / "cube.dxf")

@@ -17,17 +17,17 @@
 2020-202
-1
+2
  Manfred
@@ -61,33 +61,23 @@
 nse%0A
-%0Afrom pathlib 
 import 
-P
+p
 ath
+lib%0A
 %0Aimp
@@ -153,14 +153,22 @@
 CS%0A%0A
-DIR = 
+CWD = pathlib.
 Path
@@ -200,16 +200,668 @@
 nduser()
+%0Aif not CWD.exists():%0A    CWD = pathlib.Path(%22.%22)%0A%0A%0A# ------------------------------------------------------------------------------%0A# use an UCS to place a cube in 3D%0A#%0A# docs: https://ezdxf.mozman.at/docs/math/core.html#ucs-class%0A# tutorial for UCS based transformations: https://ezdxf.mozman.at/docs/tutorials/ucs_transform.html%0A#%0A# IMPORTANT: the ezdxf.math.UCS is not identical to the UCSTableEntry in%0A# the TABLE section, but you can acquire the USC by UCSTableEntry.ucs():%0A# https://ezdxf.mozman.at/docs/tables/ucs_table_entry.html#ezdxf.entities.UCSTableEntry.ucs%0A# ------------------------------------------------------------------------------
 %0A%0Ap = %5B%0A
@@ -2036,11 +2036,11 @@
 eas(
-DIR
+CWD
  / %22

refactor transform_cube.py