import matplotlib.pyplot as plt
import ezdxf
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
import re
doc = ezdxf.new('R2010') # create a new DXF R2010 drawing, official DXF version name: 'AC1024'
msp = doc.modelspace() # add new entities to the modelspace
# 画直线
msp.add_line((0, 0), (10, 0)) # add a LINE entity
# 画粗线
points = [(0, 10, 0, .05), (3, 10, .1, .2, -.5), (6, 10, .1, .05), (9, 0)]
msp.add_lwpolyline(points)
# 画曲线
fit_points = [(0, 0, 0), (7.5, 5.0, 0), (17.5, 5.0, 0), (22.5, 12.5, 0)]
msp = doc.modelspace()
spline = msp.add_spline(fit_points)
# 图像显示
default_img_format = '.png'
default_img_res = 100
default_bg_color = '#000000' # Black
name="temp.dxf"
msp = doc.modelspace()
# Recommended: audit & repair DXF document before rendering
auditor = doc.audit()
# The auditor.errors attribute stores severe errors,
# which *may* raise exceptions when rendering.
if len(auditor.errors) != 0:
raise Exception("The DXF document is damaged and can't be converted!")
else:
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ctx = RenderContext(doc)
ctx.set_current_layout(msp)
ezdxf.addons.drawing.properties.MODEL_SPACE_BG_COLOR = default_bg_color
out = MatplotlibBackend(ax)
Frontend(ctx, out).draw_layout(msp, finalize=True)
plt.show()
# 图像保存
# img_name = re.findall("(S+).", name) # select the image name that is the same as the dxf file name
# first_param = ''.join(img_name) + default_img_format # concatenate list and string
# fig.savefig(first_param, dpi=default_img_res)
print(name, " Converted Successfully")
参考:python:ezdxf——教程_face丶的博客-CSDN博客_ezdxf
Python将DXF文件转换为PDF或PNG或JPEG-python黑洞网
GitHub - Hamza442004/DXF2img: A tool to convert dxf files to images
Insert — ezdxf 0.18b4 documentation



