refactor example hatch_from_entities.py
-# Copyright (c) 2021, Manfred Moitzi
+# Copyright (c) 2021-2022, Manfred Moitzi
# License: MIT License
-from pathlib import Path
+import pathlib
import ezdxf
from ezdxf.render.forms import box, translate
from ezdxf import disassemble
-from ezdxf.render import path
+from ezdxf import path
-DIR = Path("~/Desktop/Outbox").expanduser()
+CWD = pathlib.Path("~/Desktop/Outbox").expanduser()
+if not CWD.exists():
+ CWD = pathlib.Path(".")
+# ------------------------------------------------------------------------------
+# This example shows how to create a new HATCH entity from existing
+# DXF entities, which is similar to applying then HATCH command to some DXF
+# entities in CAD applications
+# ------------------------------------------------------------------------------
+
+
+def main():
doc = ezdxf.new()
doc.layers.new("FORMS", dxfattribs={"color": 1})
doc.layers.new("HATCHES")
@@ -34,8 +44,12 @@ primitives = disassemble.to_primitives(msp)
# Collect paths from primitives:
paths = [p.path for p in primitives if p.path]
-# Render this paths as HATCH entities
+ # Render these paths as HATCH entities
path.render_hatches(msp, paths, dxfattribs={"layer": "HATCHES", "color": 2})
doc.set_modelspace_vport(15, (4, 4))
-doc.saveas(DIR / "hatches_from_entities.dxf")
+ doc.saveas(CWD / "hatches_from_entities.dxf")
+
+
+if __name__ == "__main__":
+ main()
