有人知道将DXF文件转换为PNG或PDF的方法吗?
我有一个庞大的DXF文件列表,我想将它们转换为图像,以便更快地查看它们。
如果可能的话,如何提取DXF文件值,如DXF文件中图形的厚度或测量值。
https://github.com/Hamza442004/DXF2img
import matplotlib.pyplot as plt
import ezdxf
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
#import wx
import glob
import re
class DXF2IMG(object):
default_img_format = '.png'
default_img_res = 300
def convert_dxf2img(self, names, img_format=default_img_format, img_res=default_img_res):
for name in names:
doc = ezdxf.readfile(name)
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)
ctx.current_layout.set_colors(bg='#FFFFFF')
out = MatplotlibBackend(ax)
Frontend(ctx, out).draw_layout(msp, finalize=True)
img_name = re.findall("(S+).",name) # select the image name that is the same as the dxf file name
first_param = ''.join(img_name) + img_format #concatenate list and string
fig.savefig(first_param, dpi=img_res)
以上代码对我不起作用。我不得不替换以下部分:
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ctx = RenderContext(doc)
ctx.set_current_layout(msp)
ctx.current_layout.set_colors(bg='#FFFFFF')
out = MatplotlibBackend(ax)
Frontend(ctx, out).draw_layout(msp, finalize=True)
具有以下内容:
fig = plt.figure()
ctx = RenderContext(doc)
# Better control over the LayoutProperties used by the drawing frontend
layout_properties = LayoutProperties.from_layout(msp)
layout_properties.set_colors(bg='#FFFFFF')
ax = fig.add_axes([0, 0, 1, 1])
out = MatplotlibBackend(ax, params={"lineweight_scaling": 0.1})
Frontend(ctx, out).draw_layout(msp,layout_properties=layout_properties, finalize=True)
注意:请确保在开始时添加此项:
from ezdxf.addons.drawing.properties import Properties, LayoutProperties
EDIT!!!
import matplotlib.pyplot as plt
import ezdxf
from ezdxf.addons.drawing import RenderContext, Frontend
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
# import wx
import glob
import re
class DXF2IMG(object):
default_img_format = '.png'
default_img_res = 300
def convert_dxf2img(self, names, img_format=default_img_format, img_res=default_img_res):
for name in names:
doc = ezdxf.readfile(name)
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)
ctx.current_layout.set_colors(bg='#FFFFFF')
out = MatplotlibBackend(ax)
Frontend(ctx, out).draw_layout(msp, finalize=True)
img_name = re.findall("(S+).",name) # select the image name that is the same as the dxf file name
first_param = ''.join(img_name) + img_format #concatenate list and string
fig.savefig(first_param, dpi=img_res)
if __name__ == '__main__':
first = DXF2IMG()
first.convert_dxf2img(['GT-010.DXF'],img_format='.png')
@哈姆扎穆罕默德分享了一个非常有效的解决方案!
全部学分授予:https://github.com/Hamza442004/DXF2img
以上是没有GUI的版本,以防有人想要真正的交易!
来自GNOME
项目的Dia可以将dxf
转换为png
,它还有一个允许脚本编写的Python
接口。