File: make_hatch.py

package info (click to toggle)
ezdxf 1.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104,528 kB
  • sloc: python: 182,341; makefile: 116; lisp: 20; ansic: 4
file content (71 lines) | stat: -rw-r--r-- 2,215 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Copyright (c) 2025, Manfred Moitzi
# License: MIT License
from __future__ import annotations
from pathlib import Path

import ezdxf

from ezdxf import edgeminer as em
from ezdxf import edgesmith as es

CWD = Path(__file__).parent
OUTBOX = Path("~/Desktop/Outbox").expanduser()


def load(filename: str) -> list[em.Edge]:
    doc = ezdxf.readfile(CWD / filename)
    msp = doc.modelspace()
    edges = list(es.edges_from_entities_2d(msp))
    return edges


def make_hatch_with_polyline_path(edges: list[em.Edge], outname: str) -> None:
    doc = ezdxf.new()
    msp = doc.modelspace()
    dxfattribs = {"layer": "HATCH"}
    deposit = em.Deposit(edges)
    for loop in em.find_all_simple_chains(deposit):
        hatch = msp.add_hatch(color=2, dxfattribs=dxfattribs)
        polyline_path = es.polyline_path_from_chain(loop, max_sagitta=0.1)
        hatch.paths.append(polyline_path)
    doc.saveas(OUTBOX / outname)


def make_hatch_without_arcs(edges: list[em.Edge], outname: str) -> None:
    doc = ezdxf.new()
    msp = doc.modelspace()
    dxfattribs = {"layer": "HATCH"}
    deposit = em.Deposit(edges)
    for loop in em.find_all_simple_chains(deposit):
        hatch = msp.add_hatch(color=2, dxfattribs=dxfattribs)
        # 2D path as intermediate_step:
        path2d = es.path2d_from_chain(loop)
        # flatten everything:
        hatch.paths.add_polyline_path(path2d.flattening(distance=0.1))
    doc.saveas(OUTBOX / outname)


def make_hatch_with_edge_path(edges: list[em.Edge], outname: str) -> None:
    doc = ezdxf.new()
    msp = doc.modelspace()
    dxfattribs = {"layer": "HATCH"}
    deposit = em.Deposit(edges)
    for loop in em.find_all_simple_chains(deposit):
        hatch = msp.add_hatch(color=2, dxfattribs=dxfattribs)
        edge_path = es.edge_path_from_chain(loop, max_sagitta=0.1)
        hatch.paths.append(edge_path)
    doc.saveas(OUTBOX / outname)


FILE_6 = "6_closed_loop_with_arcs.dxf"


def main():
    doc = load(FILE_6)
    make_hatch_with_polyline_path(doc, "make_hatch_with_polyline_path.dxf")
    make_hatch_without_arcs(doc, "make_hatch_without_arcs.dxf")
    make_hatch_with_edge_path(doc, "make_hatch_with_edge_path.dxf")


if __name__ == "__main__":
    main()