DXF Engineering Drawing Generator (图片转DXF工程图)
Overview
从工程图片自动生成 DXF 文件。支持矩形、圆形、多边形、槽孔、圆弧等常见机械零件图形。
Workflow
完整流程分为 5 个阶段:
Phase 1: 图片分析 & 参数提取
- 读取用户发送的工程图片
- 识别图形类型(矩形板、带孔零件、法兰、异形件等)
- 提取关键参数:尺寸、位置、孔径、角度等
- 生成结构化 JSON 参数
Phase 2: 预览确认(带尺寸标注)
- 用提取的参数生成带标注的 DXF
- 生成 PNG 预览图(
--preview)
- 必须展示预览给用户确认
- 列出所有识别到的参数供用户核对
Phase 3: 参数修正
根据用户反馈修正参数:
- 调整尺寸数值
- 增减孔/槽等特征
- 修改位置/角度
- 修正单位(mm/inch)
Phase 4: 生成正式 DXF(带标注)
- 参数确认后,执行
scripts/generate_dxf.py 生成完整 DXF
- 使用
--preview 生成最终预览
Phase 5: 生成纯净版 DXF
- 执行
scripts/clean_dxf.py 去除所有标注、中心线、文字
- 只保留轮廓几何体(OUTLINE、INNER、HOLE 图层)
- 生成纯净版预览
- 必须展示纯净版给用户最终确认
Scripts
generate_dxf.py - DXF 生成主脚本
# 从参数 JSON 生成 DXF
python scripts/generate_dxf.py --params params.json --output output.dxf
# 同时生成预览图
python scripts/generate_dxf.py --params params.json --output output.dxf --preview
# 直接生成纯净版(无标注)
python scripts/generate_dxf.py --params params.json --output output.dxf --clean
参数 JSON 格式:
{
"title": "零件名称",
"drawing_number": "DWG-001",
"material": "Steel 304",
"scale": "1:1",
"shapes": [
{"type": "rectangle", "x": 0, "y": 0, "width": 100, "height": 50, "layer": "OUTLINE"},
{"type": "circle", "cx": 25, "cy": 25, "radius": 5, "layer": "HOLE"},
{"type": "slot", "cx": 75, "cy": 25, "length": 20, "width": 8, "angle": 0, "layer": "HOLE"},
{"type": "arc", "cx": 50, "cy": 0, "radius": 50, "start_angle": 0, "end_angle": 180, "layer": "OUTLINE"}
],
"dimensions": [
{"type": "linear", "p1": [0, 0], "p2": [100, 0], "offset": 10, "text": "100"},
{"type": "diameter", "center": [25, 25], "radius": 5, "text": "10"}
],
"settings": {
"unit": "mm",
"line_width": 1.0
}
}
支持的图形类型:
| 类型 |
必填参数 |
可选参数 |
rectangle |
x, y, width, height |
angle, layer |
circle |
cx, cy, radius |
layer |
polygon |
points (数组 [[x,y],...]) |
layer, closed |
slot |
cx, cy, length, width |
angle, layer |
arc |
cx, cy, radius, start_angle, end_angle |
layer |
line |
x1, y1, x2, y2 |
layer |
支持的标注类型:
| 类型 |
必填参数 |
说明 |
linear |
p1, p2 |
线性尺寸 |
diameter |
center, radius |
直径标注(自动加%%C前缀) |
radius |
center, radius |
半径标注(自动加R前缀) |
clean_dxf.py - 纯净版生成
# 去除标注,只保留轮廓
python scripts/clean_dxf.py input.dxf output_clean.dxf
保留的图层: 0, OUTLINE, INNER, HOLE
删除的图层: DIMENSION, CENTER, TEXT, HIDDEN
extract_params.py - 参数辅助工具
# 查看可用模板
python scripts/extract_params.py --list-templates
# 从模板生成参数文件
python scripts/extract_params.py --template plate_with_holes --output params.json
# 验证已有参数文件
python scripts/extract_params.py --validate params.json
可用模板:
rectangular_plate - 矩形板
plate_with_holes - 带孔板
flanged_part - 法兰件
DXF Layer 规范
| 图层 |
颜色 |
线型 |
线宽 |
用途 |
| 0 |
白(7) |
实线 |
1.0 |
默认轮廓 |
| OUTLINE |
白(7) |
实线 |
1.0 |
外轮廓 |
| INNER |
白(7) |
实线 |
0.5 |
内轮廓 |
| HOLE |
白(7) |
实线 |
0.5 |
孔 |
| CENTER |
红(1) |
中心线 |
0.18 |
中心线(纯净版删除) |
| DIMENSION |
绿(3) |
实线 |
0.18 |
标注(纯净版删除) |
| TEXT |
白(7) |
实线 |
0.25 |
文字(纯净版删除) |
Dependencies
运行脚本前需确保安装以下 Python 依赖:
pip install ezdxf matplotlib
- ezdxf - DXF 文件读写(必需)
- matplotlib - 预览图生成(可选,仅 --preview 时需要)
Notes
- 输出 DXF 格式为 AutoCAD R2010 兼容
- 所有尺寸默认单位为 mm
- 纯净版只保留黑色实线轮廓,无多余图层
- 生成预览时使用 PNG 格式,150 DPI
1---2name: dxf-engineering3description: "图片转DXF工程图生成器。用户发来工程图片,自动提取参数,生成带尺寸标注的预览供确认,确认后生成1:1 DXF文件,再去除标注生成纯净版。触发词:DXF、工程图、dxf、转DXF、生成工程图、图片转DXF、CAD文件。"4description_zh: "图片转DXF工程图生成器"5description_en: "Image to DXF Engineering Drawing Generator"6version: 1.0.07metadata:8 source: https://skillmd.com/skills/jinhong270/dxf-engineering9---1011# DXF Engineering Drawing Generator (图片转DXF工程图)1213## Overview1415从工程图片自动生成 DXF 文件。支持矩形、圆形、多边形、槽孔、圆弧等常见机械零件图形。1617## Workflow1819完整流程分为 5 个阶段:2021### Phase 1: 图片分析 & 参数提取22231. 读取用户发送的工程图片242. 识别图形类型(矩形板、带孔零件、法兰、异形件等)253. 提取关键参数:尺寸、位置、孔径、角度等264. 生成结构化 JSON 参数2728### Phase 2: 预览确认(带尺寸标注)29301. 用提取的参数生成带标注的 DXF312. 生成 PNG 预览图(`--preview`)323. **必须展示预览给用户确认**334. 列出所有识别到的参数供用户核对3435### Phase 3: 参数修正3637根据用户反馈修正参数:38- 调整尺寸数值39- 增减孔/槽等特征40- 修改位置/角度41- 修正单位(mm/inch)4243### Phase 4: 生成正式 DXF(带标注)44451. 参数确认后,执行 `scripts/generate_dxf.py` 生成完整 DXF462. 使用 `--preview` 生成最终预览4748### Phase 5: 生成纯净版 DXF49501. 执行 `scripts/clean_dxf.py` 去除所有标注、中心线、文字512. 只保留轮廓几何体(OUTLINE、INNER、HOLE 图层)523. 生成纯净版预览534. **必须展示纯净版给用户最终确认**5455## Scripts5657### generate_dxf.py - DXF 生成主脚本5859```bash60# 从参数 JSON 生成 DXF61python scripts/generate_dxf.py --params params.json --output output.dxf6263# 同时生成预览图64python scripts/generate_dxf.py --params params.json --output output.dxf --preview6566# 直接生成纯净版(无标注)67python scripts/generate_dxf.py --params params.json --output output.dxf --clean68```6970**参数 JSON 格式:**71```json72{73 "title": "零件名称",74 "drawing_number": "DWG-001",75 "material": "Steel 304",76 "scale": "1:1",77 "shapes": [78 {"type": "rectangle", "x": 0, "y": 0, "width": 100, "height": 50, "layer": "OUTLINE"},79 {"type": "circle", "cx": 25, "cy": 25, "radius": 5, "layer": "HOLE"},80 {"type": "slot", "cx": 75, "cy": 25, "length": 20, "width": 8, "angle": 0, "layer": "HOLE"},81 {"type": "arc", "cx": 50, "cy": 0, "radius": 50, "start_angle": 0, "end_angle": 180, "layer": "OUTLINE"}82 ],83 "dimensions": [84 {"type": "linear", "p1": [0, 0], "p2": [100, 0], "offset": 10, "text": "100"},85 {"type": "diameter", "center": [25, 25], "radius": 5, "text": "10"}86 ],87 "settings": {88 "unit": "mm",89 "line_width": 1.090 }91}92```9394**支持的图形类型:**9596| 类型 | 必填参数 | 可选参数 |97|------|---------|---------|98| `rectangle` | x, y, width, height | angle, layer |99| `circle` | cx, cy, radius | layer |100| `polygon` | points (数组 [[x,y],...]) | layer, closed |101| `slot` | cx, cy, length, width | angle, layer |102| `arc` | cx, cy, radius, start_angle, end_angle | layer |103| `line` | x1, y1, x2, y2 | layer |104105**支持的标注类型:**106107| 类型 | 必填参数 | 说明 |108|------|---------|------|109| `linear` | p1, p2 | 线性尺寸 |110| `diameter` | center, radius | 直径标注(自动加%%C前缀) |111| `radius` | center, radius | 半径标注(自动加R前缀) |112113### clean_dxf.py - 纯净版生成114115```bash116# 去除标注,只保留轮廓117python scripts/clean_dxf.py input.dxf output_clean.dxf118```119120**保留的图层:** 0, OUTLINE, INNER, HOLE121**删除的图层:** DIMENSION, CENTER, TEXT, HIDDEN122123### extract_params.py - 参数辅助工具124125```bash126# 查看可用模板127python scripts/extract_params.py --list-templates128129# 从模板生成参数文件130python scripts/extract_params.py --template plate_with_holes --output params.json131132# 验证已有参数文件133python scripts/extract_params.py --validate params.json134```135136**可用模板:**137- `rectangular_plate` - 矩形板138- `plate_with_holes` - 带孔板139- `flanged_part` - 法兰件140141## DXF Layer 规范142143| 图层 | 颜色 | 线型 | 线宽 | 用途 |144|------|------|------|------|------|145| 0 | 白(7) | 实线 | 1.0 | 默认轮廓 |146| OUTLINE | 白(7) | 实线 | 1.0 | 外轮廓 |147| INNER | 白(7) | 实线 | 0.5 | 内轮廓 |148| HOLE | 白(7) | 实线 | 0.5 | 孔 |149| CENTER | 红(1) | 中心线 | 0.18 | 中心线(纯净版删除) |150| DIMENSION | 绿(3) | 实线 | 0.18 | 标注(纯净版删除) |151| TEXT | 白(7) | 实线 | 0.25 | 文字(纯净版删除) |152153## Dependencies154155运行脚本前需确保安装以下 Python 依赖:156157```bash158pip install ezdxf matplotlib159```160161- **ezdxf** - DXF 文件读写(必需)162- **matplotlib** - 预览图生成(可选,仅 --preview 时需要)163164## Notes165166- 输出 DXF 格式为 AutoCAD R2010 兼容167- 所有尺寸默认单位为 mm168- 纯净版只保留黑色实线轮廓,无多余图层169- 生成预览时使用 PNG 格式,150 DPI