"""Region image of a converted drawing (DXF) for the "도면 보기" links of the candidate tables (stage C, 2026-10-19). The same drawing code as the reading aid Code/울산P1판독/dxftools.render, parameterised by a DXF path. Entities that touch the region are drawn black on white; Korean text shows as boxes (no font mapping), numbers are readable. """ import ezdxf from ezdxf import bbox def _in_region(e, x0, y0, x1, y1): try: b = bbox.extents([e], fast=True) except Exception: return False return b.has_data and b.extmax.x >= x0 and b.extmin.x <= x1 and b.extmax.y >= y0 and b.extmin.y <= y1 def render(dxf_path, cx, cy, half, out_png, px=1400, mark=True): """PNG of the square [cx-half, cx+half] x [cy-half, cy+half]; a ring marks the point (cx, cy).""" import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt from ezdxf.addons.drawing import Frontend, RenderContext from ezdxf.addons.drawing.config import BackgroundPolicy, ColorPolicy, Configuration from ezdxf.addons.drawing.matplotlib import MatplotlibBackend doc = ezdxf.readfile(dxf_path) x0, x1, y0, y1 = cx - half, cx + half, cy - half, cy + half keep = [e for e in doc.modelspace() if _in_region(e, x0, y0, x1, y1)] fig = plt.figure(figsize=(px / 100, px / 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) if mark: ax.add_patch(plt.Circle((cx, cy), half * 0.04, fill=False, color="red", linewidth=2)) ax.set_xlim(x0, x1) ax.set_ylim(y0, y1) ax.set_aspect("equal") ax.axis("off") fig.savefig(out_png, facecolor="white") plt.close(fig) return out_png