File: dump_hatch_pattern.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 (42 lines) | stat: -rw-r--r-- 1,028 bytes parent folder | download | duplicates (2)
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
# Copyright (c) 2011-2020, Manfred Moitzi
# License: MIT License
import sys
from pathlib import Path
import ezdxf


def main(filename):
    doc = ezdxf.readfile(filename)
    msp = doc.modelspace()
    hatches = msp.query("HATCH[solid_fill==0]")
    filename.with_suffix(".py")
    dump_pattern(filename.with_suffix(".py"), hatches)


def dump_pattern(filename, hatches):
    with open(filename, "wt") as f:
        f.write(FILE_HEADER)
        for hatch in hatches:
            f.write(get_pattern_definition_string(hatch))
        f.write(FILE_TAIL)


FILE_HEADER = """# DXF pattern definition file
# Do not edit this file, because this file was generated by 'dump_hatch_pattern.py'
# 'dump_hatch_pattern.py' is part of the Python package 'ezdxf'

Pattern = {
"""

FILE_TAIL = "}\n"


def get_pattern_definition_string(hatch):
    name = hatch.dxf.pattern_name
    with hatch.edit_pattern() as p:
        pattern = str(p)
    return "'{}': {},\n".format(name, pattern)


if __name__ == "__main__":
    main(Path(sys.argv[1]))