我正在尝试在ezdxf中绘制一些基本的圆形和正方形,然后写入pdf。 当我只绘制几个单位时,情况看起来很好。 但当我画很多的时候,线条就会变得更粗。 dfxattribs={'lineweight':0} 没有任何乱七八糟的效果。 我认为 ezdxf 或 pdf 转换器在某种程度上可以缩放东西。 它似乎固定了 PDF 的大小,然后将其缩小以适合,但线宽被固定为其他缩放比例,最终结果是粗线。
这里有一些例子。
def test():
from numpy import arange
doc = ezdxf.new(dxfversion="R2010")
doc.units = units.MM
doc.header['$LWDISPLAY'] = 1
msp = doc.modelspace()
blk = doc.blocks.new(name='MYBLOCK')
blk.add_circle((0,0), 1, dxfattribs={'lineweight':0})
blk.add_polyline2d(((-1,-1),(1,-1),(1,1),(-1,1)))
pos_step = 2.0
blockscale = 1.0
for x in arange(0, pos_step*5, pos_step):
for y in arange(0, pos_step*3, pos_step):
msp.add_blockref('MYBLOCK',
(x, y),
dxfattribs={'xscale': blockscale,
'yscale': blockscale})
msp.add_linear_dim(
base = (0,-pos_step/2),
p1 = (0,0),
p2 = (pos_step,0),
override = {'dimblk': ezdxf.ARROWS.closed_filled}
).render()
matplotlib.qsave(msp, 'test.pdf')
这里的输出看起来不错。 pdf 为 6.5x4.8 [英寸]:
如果我跨过 50 个单位:
for x in arange(0, pos_step*50, pos_step):
我觉得线条变粗了,但看起来还不错。 pdf 为 16x2 [英寸]:
如果我跨过 500 个单位:
for x in arange(0, pos_step*500, pos_step):
如果我们疯狂地拥有 5000 个单位:
for x in arange(0, pos_step*5000, pos_step):
线条太粗了,只是一块白色。 pdf 为 16x2 [英寸]:
所以我认为这与 pdf 表大小保持不变有关,即使放置了越来越多的块。 如何获得此测试所有版本的一些粗细线? 是否可以手动缩放PDF输出的大小,或者设置一些更好的自动参数?
新的测试代码,源自:
https://ezdxf.mozman.at/docs/howto/drawing-addon.html#how-to-set-the-page-size-in-inches https://github.com/mozman/ezdxf/discussions/797
def export_to_scale(doc, msp, filename, paper_size, minxy, maxxy, dpi = 300):
"""Render the modelspace content with to a specific paper size and scale.
Args:
doc -- the ezpdf doc
msp -- the modelspace
filename -- the filename
paper_size: paper size in inches
minxy: lower left corner tuple of the modelspace area to render
maxxy: upper right corner tuple of the modelspace area to render
dpi: pixel density on paper as dots per inch
"""
ctx = RenderContext(doc)
fig: plt.Figure = plt.figure(dpi=dpi)
ax: plt.Axes = fig.add_axes([0, 0, 1, 1])
# disable all margins
ax.margins(0)
# Set the final render limits in drawing units:
ax.set_xlim(minxy[0], maxxy[0])
ax.set_ylim(minxy[1], maxxy[1])
mspp = LayoutProperties.from_layout(msp)
mspp.set_colors("#eaeaea")
config = Configuration.defaults()
config = config.with_changes(min_lineweight = 0.1)
out = MatplotlibBackend(ax)
# finalizing invokes auto-scaling by default!
Frontend(ctx, out, config=config).draw_layout(msp, finalize=False, layout_properties=mspp)
fig.set_size_inches(paper_size[0], paper_size[1], forward=True)
fig.savefig(filename, dpi=dpi)
plt.close(fig)
def test():
from numpy import arange
doc = ezdxf.new(dxfversion="R2010")
doc.units = units.MM
doc.header['$LWDISPLAY'] = 0
msp = doc.modelspace()
blk = doc.blocks.new(name='MYBLOCK')
blk.add_circle((0,0), 1, dxfattribs={'lineweight': -2})
blk.add_polyline2d(((-1,-1),(1,-1),(1,1),(-1,1)), close=True, dxfattribs={'lineweight': -2})
pos_step = 2.0
blockscale = 1.0
for x in arange(0, pos_step*500, pos_step):
for y in arange(0, pos_step*3, pos_step):
msp.add_blockref('MYBLOCK',
(x, y),
dxfattribs={'xscale': blockscale,
'yscale': blockscale,
'lineweight': 100})
msp.add_linear_dim(
base = (0,-pos_step),
p1 = (0, 0),
p2 = (pos_step,0),
override = {'dimblk': ezdxf.ARROWS.closed_filled}
).render()
export_to_scale(doc, msp, 'test.pdf', (17,11), (-10,-10), (pos_step*501,pos_step*4), 300)