下载地址
https://www.zhengdianxue.com/wp-content/uploads/skills/user_37327b1a--dwg-to-pdf-export.zip将提示词发送给你的 AI 安装该 skills
请根据 https://skillhub.cn/install/skillhub.md,安装 @user_37327b1a/dwg-to-pdf-export。DWG/DXF to PDF/PNG Export 帮助 AI 将 AutoCAD DWG/DXF 图纸导出为矢量 PDF 和高清 PNG 图片,完整保留图纸中的文字标注(设备名称、尺寸标注、管线编号)。工程师、设计师、造价人员及 AI 自动化流程运维者,在没有安装 AutoCAD 的 macOS/Windows/Linux 环境中,将 DWG/DXF 图纸转换为可查看、可打印、可归档的 PDF/PNG 文件。
阅读全文 (展开查看完整说明)
概述
DWG/DXF to PDF/PNG Export
Export AutoCAD DWG/DXF drawings to vector PDF and high-resolution PNG using ezdxf + MatplotlibBackend. Handles coordinate frame detection, bounding-box entity filtering, toxic entity exclusion, and monochrome/color rendering.
When to Use This Skill
Trigger this skill when the user asks to:
- Export or convert DWG/DXF to PDF or PNG
- Render CAD drawings as images
- Print or preview AutoCAD files without AutoCAD installed
- Fix missing lines, text, or blank pages in CAD exports
Dependencies
pip install ezdxf matplotlib pymupdf PillowWorkflow
1. Locate the Real Content Area
Do not rely solely on LWPOLYLINE rectangles to determine the drawing frame. After DWG-to-DXF conversion, frames may be displaced or multiple frames may exist. Instead, scan MTEXT entity coordinates to identify the true content zone:
import ezdxf
doc = ezdxf.readfile('input.dxf')
msp = doc.modelspace()
for e in msp.query('MTEXT'):
x, y = e.dxf.insert.x, e.dxf.insert.y
print(e.text[:30], x, y)Use the equipment-label coordinates to set the target zone (xmin, ymin, xmax, ymax).
2. Filter Entities by Bounding-Box Intersection
Filtering by a single representative point (e.g., line start, insert point, circle center) drops entities that cross the frame boundary. Use bounding-box intersection instead:
from ezdxf import bbox
from ezdxf.math import BoundingBox2d
zone = (xmin, ymin, xmax, ymax)
selected = []
for e in msp:
try:
ext = bbox.extents([e], fast=True) # pass a list
if ext and ext.has_data:
b2 = BoundingBox2d([
(ext.extmin.x, ext.extmin.y),
(ext.extmax.x, ext.extmax.y)
])
if not (
b2.extmax.x < zone[0] or b2.extmin.x > zone[2]
or b2.extmax.y < zone[1] or b2.extmin.y > zone[3]
):
selected.append(e)
except Exception:
pass3. Exclude Toxic Entities
Certain entities cause ezdxf rendering to hang indefinitely (60-second timeouts do not help). Typical culprits:
| Type | Cause | Fix |
|---|---|---|
IMAGE | References a Windows BMP path that does not exist on macOS | Skip all IMAGE entities |
INSERT on DIM_ELEV layer | Anonymous blocks with insert points far outside the drawing (e.g., -1,000,000) | Test each INSERT individually with a timeout; add hanging handles to a blacklist |
BLACKLIST = {'1729', '1769', '1772', '1784', ...}
for e in selected:
if e.dxftype() == 'IMAGE':
continue
if e.dxf.handle.upper() in BLACKLIST:
continue4. Render with MatplotlibBackend
PyMuPdfBackend does not implement draw_text, so all MTEXT/TEXT labels are lost. Use MatplotlibBackend instead.
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.family'] = 'PingFang SC'
mpl.rcParams['axes.unicode_minus'] = False
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.config import Configuration, BackgroundPolicy, ColorPolicy
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
fig = plt.figure(figsize=(20, 13))
ax = fig.add_axes([0, 0, 1, 1])
ax.set_xlim(zone[0], zone[2])
ax.set_ylim(zone[1], zone[3])
ax.set_aspect('equal')
ax.axis('off')
ax.set_facecolor('white')
fig.patch.set_facecolor('white')
config = Configuration().with_changes(
background_policy=BackgroundPolicy.WHITE,
color_policy=ColorPolicy.BLACK
)
backend = MatplotlibBackend(ax)
frontend = Frontend(RenderContext(doc), backend, config=config)
frontend.draw_entities(selected)
# Post-process for crisp monochrome printing
for patch in ax.patches:
patch.set_edgecolor('black')
patch.set_facecolor('none')
patch.set_linewidth(0.5)
for text in ax.texts:
text.set_color('black')
fig.savefig('output.pdf', facecolor='white', edgecolor='none')
fig.savefig('output.png', dpi=200, facecolor='white', edgecolor='none')
plt.close(fig)Color Policies
| Policy | Background | Lines | Use Case |
|---|---|---|---|
ColorPolicy.COLOR | Any | Original CAD colors | Screen viewing when the CAD file uses dark-theme colors |
ColorPolicy.MONOCHROME_LIGHT_BG | White | Gray | General printing |
ColorPolicy.BLACK | White | Pure black | Best for printing and formal submissions |
Known Pitfalls
| Pitfall | Symptom | Solution |
|---|---|---|
bbox.extents(e) single entity | TypeError: 'Insert' object is not iterable | Pass a list: bbox.extents([e]) |
BoundingBox2d(x1,y1,x2,y2) | TypeError: takes 1 or 2 positional arguments | Pass two-point list: [(x1,y1), (x2,y2)] |
Import matplotlib.pyplot before bbox calculation | All bboxes return zero size | Calculate bboxes first, then import pyplot |
Reuse PyMuPdfBackend for multiple outputs | Second output has coordinates outside the page; blank PDF | Create a new backend for every output |
| White lines on white background | Lines appear missing | Use ColorPolicy.BLACK or switch to a black background |
| Missing text labels | PDF shows only geometry, no equipment names | Switch from PyMuPdfBackend to MatplotlibBackend |
Scripts
scripts/export_dwg_to_pdf.py— Standalone CLI tool that performs the full export pipeline with automatic zone detection, optional toxic-entity scanning, and configurable color/background.