refactor example exploding_block_references.py
-# Copyright (c) 2020, Manfred Moitzi
+# Copyright (c) 2020-2022, Manfred Moitzi
# License: MIT License
-
+import pathlib
from typing import cast
-from pathlib import Path
import ezdxf
-DIR = Path("~/Desktop/Outbox").expanduser()
+CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
+if not CWD.exists():
+ CWD = pathlib.Path(".")
+
+# ------------------------------------------------------------------------------
+# This example shows how to explode block references (INSERT entities).
+#
+# https://ezdxf.mozman.at/docs/tutorials/blocks.html
+# ------------------------------------------------------------------------------
+
+def main():
doc = ezdxf.new("R2010")
msp = doc.modelspace()
@@ -20,7 +29,10 @@ block.add_text(
).set_pos((0.5, 0.5), align="MIDDLE_CENTER")
block.add_spline(fit_points=[(0, 0), (0.4, 0.3), (0.6, 0.7), (1, 1)])
block.add_ellipse(
- center=(0.5, 0.5), major_axis=(0.5, 0.5), ratio=0.5, dxfattribs={"color": 5}
+ center=(0.5, 0.5),
+ major_axis=(0.5, 0.5),
+ ratio=0.5,
+ dxfattribs={"color": 5},
)
block.add_lwpolyline(
[(0, 1.1), (0.3, 1.1, -0.5), (0.7, 1.1), (1, 1.1)],
@@ -43,10 +55,14 @@ msp.add_blockref(
) # none uniform scaling
doc.set_modelspace_vport(4, center=(3, 3))
-doc.saveas(DIR / "scaling.dxf")
+ doc.saveas(CWD / "scaling.dxf")
# Explode flag block references
for block in msp.query("INSERT[name=='TEST']"):
cast("Insert", block).explode()
-doc.saveas(DIR / "exploded.dxf")
+ doc.saveas(CWD / "exploded.dxf")
+
+
+if __name__ == "__main__":
+ main()
