Initial Commit of Scripts

main
linuswck 2023-12-12 12:02:09 +08:00
parent d895cf3b76
commit 72686f5556
2 changed files with 80 additions and 0 deletions

View File

@ -1,2 +1,21 @@
# KiCAD_BOM_Generator
## Description
This BOM Generation Script takes KiCAD sch xml file and generate BOM in the following formats and rules:
1. Parts are not included if any of the following fields are checked:
- DNP
- excluded from BOM
2. Parts are Grouped by:
- Value
- Footprint
- MFR_PN
- MFR_ALT
3. Parts are sorted by: Ref
4. Output Fields: Ref, Value, MFR_PN, MFR_PN_ALT, Qnty, LibPart, Footprint, Comment
## Instructions to Use this Script
1. Set `PYTHONPATH` to include the path to the `kicad_netlist_reader` scripts inside KiCad Installation
- Located at `{KiCAD Installation PATH}/share/kicad/plugins`
2. Generate the XML file from KiCad Schematics
3. Run `python -m generate_bom_from_xml input.xml output.csv`

61
generate_bom_from_xml.py Normal file
View File

@ -0,0 +1,61 @@
# Modified from "bom_csv_grouped_by_value_with_fp.py" Example BOM Generation Script
"""
@package
Output: CSV (comma-separated)
The BOM does not include components with DNP or excluded from BOM field(s) checked.
Grouped By: Value, Footprint, MFR_PN, MFR_ALT
Sorted By: Ref
Fields: Ref, Value, MFR_PN, MFR_PN_ALT, Qnty, LibPart, Footprint, Comment
Command line:
python "pathToFile/generate_bom_from_xml.py" "%I" "%O.csv"
"""
import kicad_netlist_reader
import csv
import sys
import os
try:
f = open(sys.argv[2], 'w', encoding='utf-8')
except IOError:
raise IOError("Can't open output file for writing: " + sys.argv[2])
# Custom Equal Operator for "groupComponents" method
def __eq__(self, other):
result = False
if self.getValue() == other.getValue():
if self.getFootprint() == other.getFootprint():
if self.getField("MFR_PN") == other.getField("MFR_PN"):
if self.getField("MFR_PN_ALT") == other.getField("MFR_PN_ALT"):
result = True
return result
kicad_netlist_reader.comp.__eq__ = __eq__
net = kicad_netlist_reader.netlist(sys.argv[1])
out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar='\"', quoting=csv.QUOTE_ALL)
out.writerow(['Source:', net.getSource()])
out.writerow(['Date:', net.getDate()])
out.writerow(['Tool:', net.getTool()])
out.writerow(['Ref', 'Value', 'MFR_PN', 'MFR_PN_ALT', 'Qnty', 'LibPart', 'Footprint', 'Comment'])
grouped = net.groupComponents(components=net.getInterestingComponents(excludeBOM=True, DNP=True))
for group in grouped:
refs = ""
for component in group:
if refs != "":
refs += ", "
refs += component.getRef()
c = component
out.writerow([refs,
c.getValue(),
c.getField("MFR_PN"),
c.getField("MFR_PN_ALT"),
len(group),
c.getLibName() + ":" + c.getPartName(),
c.getFootprint(),
c.getField("Comment")])