"""Render a rectangular region of a converted DXF to PNG (visual reading aid only). usage: python render_region.py [px_width] """ import sys from pathlib import Path import ezdxf import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from ezdxf.addons.drawing import Frontend, RenderContext from ezdxf.addons.drawing.config import Configuration, BackgroundPolicy, ColorPolicy from ezdxf.addons.drawing.matplotlib import MatplotlibBackend HERE = Path(__file__).resolve().parent def main(): idx, x0, y0, x1, y1 = int(sys.argv[1]), *map(float, sys.argv[2:6]) out = HERE / "판독" / f"{sys.argv[6]}.png" width_px = int(sys.argv[7]) if len(sys.argv) > 7 else 2400 path = sorted((HERE / "입력DXF").glob("*.dxf"))[idx - 1] doc = ezdxf.readfile(path) msp = doc.modelspace() # Only entities whose bounding box touches the region are drawn (speed). from ezdxf import bbox keep = [] for e in msp: try: b = bbox.extents([e], fast=True) except Exception: continue if b.has_data and b.extmax.x >= x0 and b.extmin.x <= x1 and b.extmax.y >= y0 and b.extmin.y <= y1: keep.append(e) aspect = (y1 - y0) / (x1 - x0) fig = plt.figure(figsize=(width_px / 100, width_px * aspect / 100), dpi=100) ax = fig.add_axes([0, 0, 1, 1]) cfg = Configuration(background_policy=BackgroundPolicy.WHITE, color_policy=ColorPolicy.BLACK) Frontend(RenderContext(doc), MatplotlibBackend(ax), config=cfg).draw_entities(keep) ax.set_xlim(x0, x1) ax.set_ylim(y0, y1) ax.set_aspect("equal") ax.axis("off") fig.savefig(out, facecolor="white") plt.close(fig) print(out, len(keep)) if __name__ == "__main__": main()