File: acis-extractor.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 (55 lines) | stat: -rw-r--r-- 1,388 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
#  Copyright (c) 2022, Manfred Moitzi
#  License: MIT License
import sys
from pathlib import Path
from argparse import ArgumentParser
import ezdxf
from ezdxf.entities import Body

DIR = Path("~/Desktop/Outbox").expanduser()
if not DIR.exists():
    DIR = Path(".")

DEFAULT_FILE = DIR / "acis.dxf"


def export_acis(entity: Body, folder: Path):
    version = entity.doc.dxfversion
    fname = f"{version}-{entity.dxftype()}-{entity.dxf.handle}"

    if entity.has_binary_data:
        with open(folder / (fname + ".sab"), "wb") as fp:
            print(f"Exporting: {fp.name}")
            fp.write(entity.sab)
    else:
        with open(folder / (fname + ".sat"), "wt") as fp:
            print(f"Exporting: {fp.name}")
            fp.write(entity.tostring())


def extract_acis(filepath: Path):
    print(f"processing file: {filepath}")
    try:
        doc = ezdxf.readfile(filepath)
    except IOError as e:
        print(str(e))
        sys.exit(1)
    msp = doc.modelspace()
    for e in msp:
        if isinstance(e, Body):
            export_acis(e, folder=filepath.parent)


def main():
    parser = ArgumentParser()
    parser.add_argument("files", nargs="*")
    args = parser.parse_args()
    if len(args.files):
        for filename in args.files:
            extract_acis(Path(filename))
    else:
        extract_acis(DEFAULT_FILE)


if __name__ == "__main__":
    main()