-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathdraw_cad.py
executable file
·96 lines (83 loc) · 2.89 KB
/
draw_cad.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# Copyright (c) 2020-2023, Matthew Broadway
# License: MIT License
import argparse
import sys
import matplotlib.pyplot as plt
import ezdxf
from ezdxf import recover
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
from ezdxf.addons.drawing.config import Configuration
# ------------------------------------------------------------------------------
# The "draw_cad.py" viewer can be executed by the new ezdxf command line
# launcher:
#
# C:\> ezdxf draw FILE
#
# This file remains as an example for the usage of the Matplotlib backend.
#
# docs: https://ezdxf.mozman.at/docs/addons/drawing.html
# ------------------------------------------------------------------------------
def _main():
parser = argparse.ArgumentParser(
description="draw the given CAD file and save it to a file or view it"
)
parser.add_argument("cad_file", nargs="?")
parser.add_argument("--supported_formats", action="store_true")
parser.add_argument("-l", "--layout", default="Model")
parser.add_argument("--out", required=False)
parser.add_argument("--dpi", type=int, default=300)
args = parser.parse_args()
if args.supported_formats:
fig = plt.figure()
for (
extension,
description,
) in fig.canvas.get_supported_filetypes().items():
print(f"{extension}: {description}")
sys.exit()
if args.cad_file is None:
print("no CAD file specified")
sys.exit(1)
try:
doc = ezdxf.readfile(args.cad_file)
except IOError:
print(f"Not a DXF file or a generic I/O error.")
sys.exit(2)
except ezdxf.DXFError:
try:
doc, auditor = recover.readfile(args.cad_file)
except ezdxf.DXFStructureError:
print(f"Invalid or corrupted DXF file: {args.cad_file}")
sys.exit(3)
else:
auditor = doc.audit()
if auditor.has_errors:
# But is most likely good enough for rendering.
print(f"Found {len(auditor.errors)} unrecoverable errors.")
if auditor.has_fixes:
print(f"Fixed {len(auditor.fixes)} errors.")
try:
layout = doc.layouts.get(args.layout)
except KeyError:
print(
f'Could not find layout "{args.layout}". '
f"Valid layouts: {[l.name for l in doc.layouts]}"
)
sys.exit(4)
# setup drawing add-on configuration
config = Configuration()
fig: plt.Figure = plt.figure(dpi=args.dpi)
ax: plt.Axes = fig.add_axes([0, 0, 1, 1])
ctx = RenderContext(doc)
out = MatplotlibBackend(ax)
Frontend(ctx, out, config=config).draw_layout(layout, finalize=True)
if args.out is not None:
print(f'saving to "{args.out}"')
fig.savefig(args.out, dpi=args.dpi)
plt.close(fig)
else:
plt.show()
if __name__ == "__main__":
_main()