You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何开发基于用户输入的定尺寸坐标圆绘制及打印程序?

可行方案:用Python生成精确公制尺寸的钻孔定位PDF

针对你的需求,以下几种Python方案均可精确处理毫米单位,生成符合A4标准的钻孔定位PDF,完全消除手动绘制的误差:

方案1:ReportLab(专业PDF生成工具,首选)

ReportLab是Python生态中专门用于生成PDF的库,原生支持毫米、厘米等公制单位,能精准控制元素的位置和尺寸,完全适配你的场景。

操作步骤

  1. 安装库:pip install reportlab
  2. 核心逻辑:
    • 初始化A4尺寸画布(210mm×297mm)
    • 根据输入的x/y坐标、圆直径绘制钻孔标记
    • 可选添加固定步长的辅助网格,还原原Excel的定位参考
    • 保存或直接导出PDF

示例代码

from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm

def generate_drill_pdf(x_coords, y_coords, diameters, output_path="drill_layout.pdf"):
    # 初始化A4画布,默认左下角为坐标原点
    c = canvas.Canvas(output_path, pagesize=A4)
    # 设置网格线条宽度
    c.setLineWidth(0.1*mm)
    
    # 绘制10mm步长的辅助网格(可自定义步长)
    grid_step = 10*mm
    max_x, max_y = 210*mm, 297*mm
    # 横向网格线
    for y in range(0, int(max_y/mm)+1, int(grid_step/mm)):
        c.line(0, y*mm, max_x, y*mm)
    # 纵向网格线
    for x in range(0, int(max_x/mm)+1, int(grid_step/mm)):
        c.line(x*mm, 0, x*mm, max_y)
    
    # 绘制钻孔圆
    c.setLineWidth(0.3*mm)  # 钻孔标记线宽
    for x, y, dia in zip(x_coords, y_coords, diameters):
        cx = x * mm
        cy = y * mm
        radius = (dia / 2) * mm
        c.circle(cx, cy, radius, stroke=1, fill=0)
    
    c.save()

# 示例调用:输入x/y坐标(单位mm)、钻孔直径(单位mm)
generate_drill_pdf([50, 100, 150], [70, 140, 210], [5, 8, 5])

方案2:Pycairo(高精度矢量绘图)

Pycairo基于Cairo矢量绘图库,支持PDF等多种输出格式,完全通过坐标和单位控制绘图,精度极高,适合对尺寸要求严苛的场景。

操作步骤

  1. 安装库:pip install pycairo
  2. 核心逻辑:
    • 将A4毫米尺寸转换为PDF标准的“点”单位(1mm≈2.83465点)
    • 创建PDF绘图表面,绘制网格和钻孔圆
    • 导出最终PDF

示例代码

import cairo

def generate_drill_pdf_cairo(x_coords, y_coords, diameters, output_path="drill_layout_cairo.pdf"):
    # 毫米转PDF点:1mm = 72/25.4 点
    mm_to_pt = 72 / 25.4
    width = 210 * mm_to_pt
    height = 297 * mm_to_pt
    
    surface = cairo.PDFSurface(output_path, width, height)
    ctx = cairo.Context(surface)
    
    # 绘制10mm步长网格
    grid_step = 10 * mm_to_pt
    ctx.set_line_width(0.1 * mm_to_pt)
    # 横向网格
    for y in range(0, int(height/grid_step)+1):
        ctx.move_to(0, y*grid_step)
        ctx.line_to(width, y*grid_step)
        ctx.stroke()
    # 纵向网格
    for x in range(0, int(width/grid_step)+1):
        ctx.move_to(x*grid_step, 0)
        ctx.line_to(x*grid_step, height)
        ctx.stroke()
    
    # 绘制钻孔圆
    ctx.set_line_width(0.3 * mm_to_pt)
    for x, y, dia in zip(x_coords, y_coords, diameters):
        cx = x * mm_to_pt
        cy = y * mm_to_pt
        radius = (dia / 2) * mm_to_pt
        ctx.arc(cx, cy, radius, 0, 2 * 3.14159)
        ctx.stroke()
    
    surface.finish()

# 示例调用
generate_drill_pdf_cairo([50, 100, 150], [70, 140, 210], [5, 8, 5])

方案3:Matplotlib(适配数据可视化习惯)

Matplotlib虽主打数据可视化,但可精确控制画布尺寸和元素位置,生成的PDF为矢量格式,打印精度有保障,适合习惯用绘图库的场景。

操作步骤

  1. 安装库:pip install matplotlib
  2. 核心逻辑:
    • 将A4毫米尺寸转换为英寸(1英寸=25.4mm)设置画布
    • 关闭冗余坐标轴,绘制网格和钻孔圆
    • 导出无空白边距的PDF

示例代码

import matplotlib.pyplot as plt

def generate_drill_pdf_matplotlib(x_coords, y_coords, diameters, output_path="drill_layout_matplotlib.pdf"):
    # A4尺寸转换为英寸:210/25.4 ≈8.27英寸,297/25.4≈11.69英寸
    fig, ax = plt.subplots(figsize=(210/25.4, 297/25.4), dpi=300)
    
    # 设置坐标轴范围对应A4毫米尺寸
    ax.set_xlim(0, 210)
    ax.set_ylim(0, 297)
    # 反转y轴,将原点设为左上角(符合日常绘图习惯,可选)
    ax.invert_yaxis()
    
    # 绘制10mm步长网格
    ax.grid(True, linestyle='-', linewidth=0.5, color='gray')
    ax.set_xticks(range(0, 211, 10))
    ax.set_yticks(range(0, 298, 10))
    
    # 绘制钻孔圆
    for x, y, dia in zip(x_coords, y_coords, diameters):
        circle = plt.Circle((x, y), dia/2, color='black', fill=False, linewidth=1)
        ax.add_patch(circle)
    
    # 隐藏坐标轴边框和刻度(可选)
    ax.set_frame_on(False)
    ax.tick_params(left=False, bottom=False, labelleft=False, labelbottom=False)
    
    plt.savefig(output_path, bbox_inches='tight', pad_inches=0)
    plt.close()

# 示例调用
generate_drill_pdf_matplotlib([50, 100, 150], [70, 140, 210], [5, 8, 5])

注意事项

  • 坐标原点:不同库默认原点位置不同(ReportLab/Pycairo为左下角,Matplotlib可反转y轴至左上角),需根据你的习惯调整
  • 打印设置:导出PDF后,打印时必须选择「实际尺寸」或「100%缩放」,避免打印机自动缩放导致尺寸偏差
  • 精度验证:可先打印测试页,用卡尺测量圆的位置和直径,确认精度符合要求

内容的提问来源于stack exchange,提问作者ProfessorBenz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 13:25:09