KiCad学习日记

KiCad学习与使用日记-Kicad version8.0

KiCad学习日记

导出Gerber文件-JLC打样

1 导出Gerber的设置:

从b站搬运:https://www.bilibili.com/read/cv22773523/?spm_id_from=333.976.0.0
(KiCAD设计使用立创免费打样的方法及KiCAD插件)
生成gerber文件
设置1
生成钻孔文件
钻孔文件
生成的gerber文件最终结构如下:
设置2

2 修改Gerber文件使得可在JLC打样

创建两个文件
文件

1
2
from .give_me_free_PCB import GiveMeFreePCB # Note the relative import!
GiveMeFreePCB().register() # Instantiate and register to Pcbnew
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import pcbnew
import os
import shutil
import zipfile
import datetime

# 工程目录下Gerber和钻孔文件的存放位置
path_out = "out"
# 下单用的文件的位置
path_final = "out/final"

# 用于检查文件是否为Gerber文件以判断是否进行替换操作
file_filter = ('.gbl','.gbs','.gbp','.gbo','.gm1','gm13',
               '.gtl','.gts','.gtp','.gto','.drl','.G1',
               '.G2','.gko')

jlc_header="""G04 Layer: BottomSilkscreenLayer*
G04 EasyEDA v6.5.25, 2023-03-20 21:11:36*
**********************************至少这一行要换成自己的************************************
G04 Gerber Generator version 0.2*
G04 Scale: 100 percent, Rotated: No, Reflected: No *
G04 Dimensions in millimeters  *
G04 leading zeros omitted , absolute positions ,3 integer and 6 decimal *\n"""

# 两张对应表,分别根据结尾和文件名来判断该给什么生成的文件什么名称
replace_list_end = [('.gbl',"Gerber_BottomLayer.GBL"),
                    ('.gko',"Gerber_BoardOutlineLayer.GKO"),
                    ('.gbp',"Gerber_BottomPasteMaskLayer.GBP"),
                    ('.gbo',"Gerber_BottomSilkscreenLayer.GBO"),
                    ('.gbs',"Gerber_BottomSolderMaskLayer.GBS"),
                    ('.gtl',"Gerber_TopLayer.GTL"),
                    ('.gtp',"Gerber_TopPasteMaskLayer.GTP"),
                    ('.gto',"Gerber_TopSilkscreenLayer.GTO"),
                    ('.gts',"Gerber_TopSolderMaskLayer.GTS"),
                    ('.gd1',"Drill_Through.GD1"),
                    ('.gm1',"Gerber_MechanicalLayer1.GM1"),
                    ('.gm13',"Gerber_MechanicalLayer13.GM13")]

replace_list_contain = [('_PCB-PTH', "Drill_PTH_Through.DRL"),
                        ('_PCB-NPTH', "Drill_NPTH_Through.DRL"),
                        ('-PTH', "Drill_PTH_Through.DRL"),
                        ('-NPTH', "Drill_NPTH_Through.DRL"),
                        ('_PCB-In1_Cu', "Gerber_InnerLayer1.G1"),
                        ('_PCB-In2_Cu', "Gerber_InnerLayer2.G2"),
                        ('_PCB-Edge_Cuts', "Gerber_BoardOutlineLayer.GKO")]

def zipFolder(folder_path, output_path):
    """
    压缩指定路径下的文件夹
    :param folder_path: 要压缩的文件夹路径
    :param output_path: 压缩文件的输出路径
    """
    with zipfile.ZipFile(output_path, "w", zipfile.ZIP_DEFLATED) as zip:
        for root, dirs, files in os.walk(folder_path):
            for file in files:
                file_path = os.path.join(root, file)
                zip.write(file_path, os.path.relpath(file_path, folder_path))

# 读取Gerber文件和钻孔文件,修改名称并给Gerber文件内容添加识别头后写入到输出文件夹
def fileTransform(filename, path_out):
    # 按行读取文件内容
    lines = open(filename).readlines()
    # 检查文件类型并给新文件取好相应的名称,写入识别头和原来的文件内容
    hit_flag = 0
    for replace_couple in replace_list_end:
        if filename.endswith(replace_couple[0]):
            file_new = open(path_out + '/' + replace_couple[1], 'w')
            hit_flag = 1
            break
    if hit_flag == 0:
        for replace_couple in replace_list_contain:
            if filename.find(replace_couple[0]) != -1:
                file_new = open(path_out + '/' + replace_couple[1], 'w')
                hit_flag = 1
                break

    if hit_flag == 1:
        hit_flag = 0
        file_new.write(jlc_header)
        for line in lines:
            file_new.write(line)
        file_new.close()
def pathInit(path_out):
    # 检查下目录是否存在,没有就创建
    folder_out = os.path.exists(path_out)
    if not folder_out:
        os.makedirs(path_out)
        print("Folder %s created!" % path_out)
    else:
        print("Folder \"%s\" already exists!" % path_out)

    # 清空目录
    for files in os.listdir(path_out):
        path = os.path.join(path_out, files)
        try:
            shutil.rmtree(path)
        except OSError:
            os.remove(path)

    print("Folder \"%s\" clean!" % path_out)

class GiveMeFreePCB(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Give me free PCB!"
        self.category = "A descriptive category name"
        self.description = "A description of the plugin"
        self.show_toolbar_button = True # Optional, defaults to False
        self.icon_file_name = os.path.join(os.path.dirname(__file__), 'icon.png') # Optional

    # 关于路径,写的是处理工程目录下out目录里的文件,
    def Run(self):
        # 获取当前工程路径
        path_workdir = os.environ.get('KIPRJMOD')

        # 把工程根目录设为工作目录
        os.chdir(path_workdir)

        path_out_abs = os.path.join(os.getcwd(), path_out)

        pathInit(os.path.join(path_workdir, path_final))

        file_count = 0

        path_files = os.listdir(os.path.join(os.getcwd(), path_out))

        # 遍历out目录下的文件,识别类型并进行相应的处理
        for p in path_files:
            if(os.path.isfile(os.path.join(path_out_abs, p))):
                if(p.endswith(file_filter)):
                    print("Gerber file %s found." % p)
                    fileTransform(os.path.join(path_out_abs, p), os.path.join(os.getcwd(), path_final))
                    file_count += 1

        timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')

        board = pcbnew.GetBoard()
        project_name = os.path.splitext(os.path.basename(board.GetFileName()))[0]

        zipFolder(path_out_abs + '/' + "final", path_out_abs + '/' + "out_" + project_name + '-' + timestamp + ".zip")

        # 打开资源管理器
        os.system("explorer.exe %s" % path_out_abs)    

对源文件进行替换,把标*的行之上换为自己的工程的。

在Kicad的脚本控制台中输入如下:来引入脚本
脚本
任意选择其中一个地址放入两个.py文件。之后点击工具-外部插件-刷新插件。出现新的插件即表示成功,注意文件编码为UTF-8,不然插件刷新不出来
输出时候需要输出Gerber的时候在路径那里敲个./out,不然找不到文件。
然后点击插件即可输出JLC支持的Gerber文件!

Licensed MIT OR GPL3.0 WHATEVERS ON GITHUB_PAGE SHOW YOU