File: using_groupby.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 (64 lines) | stat: -rw-r--r-- 2,035 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Purpose: example for using the groupby feature
# Copyright (c) 2017-2021 Manfred Moitzi
# License: MIT License
import os
import glob
import math
import ezdxf


def outname(fname):
    name, ext = os.path.splitext(fname)
    return name + ".txt"


def length(lines):
    def dist(s, e):
        dx = s[0] - e[0]
        dy = s[1] - e[1]
        return math.sqrt(dx * dx + dy * dy)

    return round(sum(dist(line.dxf.start, line.dxf.end) for line in lines))


# real world application:
# used by myself to collect masses for an effort estimation.
def calc(dxffile):
    def group_key(entity):  # group entities by (dxftype, layer)
        return entity.dxftype(), entity.dxf.layer

    doc = ezdxf.readfile(dxffile)
    msp = doc.modelspace()
    groups = msp.groupby(key=group_key)
    # using get(): returns None if key does not exist
    columns = groups.get(
        ("CIRCLE", "AR_ME_ST")
    )  # all CIRCLE entities on layer 'AR_ME_ST'
    outer_walls = groups.get(
        ("LINE", "AR_ME_AW")
    )  # all LINE entities on layer 'AR_ME_AW'
    inner_walls = groups.get(
        ("LINE", "AR_ME_IW")
    )  # all LINE entities on layer 'AR_ME_IW'
    beams = groups.get(
        ("LINE", "AR_ME_TR")
    )  # all LINE entities on layer 'AR_ME_TR'

    with open(outname(dxffile), "wt", encoding="utf-8") as f:
        f.write("File: {}\n".format(dxffile))
        if columns is not None:
            f.write(f"Stützen Anzahl={len(columns)}\n")
        if outer_walls is not None:
            f.write(f"Aussenwände Anzahl={len(outer_walls)}\n")
            f.write(f"Aussenwände Gesamtlänge={length(outer_walls)}\n")
        if inner_walls is not None:
            f.write(f"Innenwände Anzahl={len(inner_walls)}\n")
            f.write(f"Innenwände Gesamtlänge={length(inner_walls)}\n")
        if beams is not None:
            f.write(f"Träger Anzahl={len(beams)}\n")
            f.write(f"Träger Gesamtlänge={length(beams)}\n")


if __name__ == "__main__":
    for fname in glob.glob("*.dxf"):
        calc(fname)