Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A KiCad BOM script for generating JLCPCB PCBA-compatible files!
<!--XSL style sheet to convert EESCHEMA XML Partlist Format to grouped CSV BOM Format
Copyright (C) 2014, Wolf Walter.
Copyright (C) 2013, Stefan Helmert.
Copyright (C) 2018, Kicad developers.
Copyright (C) 2019, arturo182.
GPL v2.
Functionality:
Generation of JLCPCB PCBA compatible BOM
How to use this is explained in eeschema.pdf chapter 14. You enter a command line into the
netlist exporter using a new (custom) tab in the netlist export dialog.
The command line is
xsltproc -o "%O.csv" "FullPathToFile/bom2grouped_csv_jlcpcb.xsl" "%I"
-->
<!--
@package
Generates a JLCPCB PCBA service compatible BOM
Functionality:
* Generate a comma separated value BOM list (csv file type).
* Components are sorted by ref and grouped by same value+footprint
One value per line
Fields are
Comment,Designator,Footprint,LCSC
The command line is
xsltproc -o "%O.csv" "full_path/bom2grouped_csv_jlcpcb.xsl" "%I"
-->
<!DOCTYPE xsl:stylesheet [
<!ENTITY nl "&#xd;&#xa;"> <!--new line CR, LF, or LF, your choice -->
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:variable name="digits" select="'1234567890'" />
<!-- for matching grouping of footprint and value combination -->
<xsl:key name="partTypeByValueAndFootprint" match="comp" use="concat(footprint, '-', value)" />
<!-- for table head and empty table fields-->
<xsl:key name="headentr" match="field" use="@name"/>
<!-- main part -->
<xsl:template match="/export">
<xsl:text>Comment,Designator,Footprint,LCSC</xsl:text>
<!-- all table entries -->
<xsl:apply-templates select="components"/>
</xsl:template>
<xsl:template match="components">
<!-- for Muenchian grouping of footprint and value combination -->
<xsl:for-each select="comp[count(. | key('partTypeByValueAndFootprint', concat(footprint, '-', value))[1]) = 1]">
<xsl:sort select="@ref" />
<xsl:text>&nl;</xsl:text>
<xsl:text>"</xsl:text><xsl:value-of select="value"/><xsl:text>","</xsl:text>
<!-- list of all references -->
<xsl:for-each select="key('partTypeByValueAndFootprint', concat(footprint, '-', value))">
<!-- strip non-digits from reference and sort based on remaining number -->
<xsl:sort select="translate(@ref, translate(@ref, $digits, ''), '')" data-type="number" />
<xsl:value-of select="@ref"/>
<xsl:if test="position() != last()"><xsl:text>,</xsl:text></xsl:if>
</xsl:for-each>
<xsl:text>","</xsl:text>
<xsl:value-of select="footprint"/><xsl:text>","</xsl:text>
<xsl:value-of select="fields/field[@name='LCSC']"/><xsl:text>"</xsl:text>
</xsl:for-each>
</xsl:template>
<!-- table entries with dynamic table head -->
<xsl:template match="fields">
<!-- remember current fields section -->
<xsl:variable name="fieldvar" select="field"/>
<!-- for all existing head entries -->
<xsl:for-each select="/export/components/comp/fields/field[generate-id(.) = generate-id(key('headentr',@name)[1])]">
<xsl:variable name="allnames" select="@name"/>
<xsl:text>,"</xsl:text>
<!-- for all field entries in the remembered fields section -->
<xsl:for-each select="$fieldvar">
<!-- only if this field entry exists in this fields section -->
<xsl:if test="@name=$allnames">
<!-- content of the field -->
<xsl:value-of select="."/>
</xsl:if>
<!--
If it does not exist, use an empty cell in output for this row.
Every non-blank entry is assigned to its proper column.
-->
</xsl:for-each>
<xsl:text>"</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
@MauroMombelli

This comment has been minimized.

Copy link

@MauroMombelli MauroMombelli commented Jun 2, 2020

Ciao,
my component already have the field LCSC, the file generated does generate an empty "LCSC" column and add a space in front " LCSC".
Eeschema 5.1.6

@buccaneer-jak

This comment has been minimized.

Copy link

@buccaneer-jak buccaneer-jak commented Jun 15, 2020

FIXED. It was the spaces in the path. I just edited the first argument as a test and that succeed.
i.e. This worked (3 spaces removed in the first argument), although by doing this, new folders without spaces are created.

Run command:
xsltproc -o "C:/Users/jak/OneDrive/Documents/TestDir/Arduino/KiCAD/TPS22919V1-0/TPS22919V1-0.csv" "C:\Program Files\KiCad\bin\scripting\plugins\bom2grouped_csv_jlcpcb.xsl" "C:\Users\jak\OneDrive\Documents\Test Dir\Arduino\KiCAD\TPS22919 V1-0\TPS22919 V1-0.xml"

****** My original post ******

Any idea how to get around Onedrive hijacking my system paths? I get the following error message below.
I have tried modifying HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders in regedit to eliminate pointing to Onedrive but still no luck.
Which system path is "%O.csv" using?
Or could it be the spaces in the paths?

Thank you for any help

Run command:
xsltproc -o "C:/Users/jak/OneDrive/Documents/Test Dir/Arduino/KiCAD/TPS22919 V1-0/TPS22919 V1-0.csv" "C:\Program Files\KiCad\bin\scripting\plugins\bom2grouped_csv_jlcpcb.xsl" "C:\Users\jak\OneDrive\Documents\Test Dir\Arduino\KiCAD\TPS22919 V1-0\TPS22919 V1-0.xml"

Command error. Return code 11
Error messages:
I/O error : Invalid argument
I/O error : Invalid argument

@gcormier

This comment has been minimized.

Copy link

@gcormier gcormier commented Jun 24, 2020

Same comment as @MauroMombelli - would be great if the part already has LCSC defined to use that value instead of having an empty "LCSC" column and then a 2nd "LCSC" column.

@adrare

This comment has been minimized.

Copy link

@adrare adrare commented Jun 28, 2020

HI folks,
I get a .xml file instead of the csv...
Here is the log:
`

Run command:
xsltproc -o "/media/gerard/tera/Documents/electronique/projet/carte_12F1840/schéma/CMS/carte_12F1840.csv" "/media/gerard/tera/Documents/electronique/projet/lib_kicad/a8c4a4b96907cfccf616a1edb59d0389-f07258ae45257becd84719d83fdc5f27de38586c/bom2grouped_csv_jlcpcb.xsl" "/media/gerard/tera/Documents/electronique/projet/carte_12F1840/schéma/CMS/carte_12F1840.xml"

Command error. Return code -1

Error messages:
execvp(xsltproc, -o, /media/gerard/tera/Documents/electronique/projet/carte_12F1840/schéma/CMS/carte_12F1840.csv, /media/gerard/tera/Documents/electronique/projet/lib_kicad/a8c4a4b96907cfccf616a1edb59d0389-f07258ae45257becd84719d83fdc5f27de38586c/bom2grouped_csv_jlcpcb.xsl, /media/gerard/tera/Documents/electronique/projet/carte_12F1840/schéma/CMS/carte_12F1840.xml) failed with error 2!

`
Any idea about the mistake I made ?
Thanks in advance,
Gerard

@arturo182

This comment has been minimized.

Copy link
Owner Author

@arturo182 arturo182 commented Jul 5, 2020

Hi everyone, sorry for the late reply, I have now fixed the double LCSC column issue, should be all good :)

@fusselhirn180

This comment has been minimized.

Copy link

@fusselhirn180 fusselhirn180 commented Jul 19, 2020

Using Win10 and KiCad 5.1.5-3 I had to shorten the path length and remove spaces in all arguments to successfully generate a BOM file. The file also did not have any extension after being generated, but once renaming the file with a csv extension, I was able to upload it to JCBPCB successfully.

@OddmarDam

This comment has been minimized.

Copy link

@OddmarDam OddmarDam commented Sep 25, 2020

Hi.

I'm getting this error when doing an export.

I/O error : Invalid argument

@alecz2008

This comment has been minimized.

Copy link

@alecz2008 alecz2008 commented Oct 8, 2020

Having the same problem getting :
Fehler Befehlsausführung. Rückgabewert 11
Fehlermeldungen:
I/O error : Invalid argument
I/O error : Invalid argument

@MauroMombelli

This comment has been minimized.

Copy link

@MauroMombelli MauroMombelli commented Oct 8, 2020

guys, if you have problem, what version of kicad and what os?
it looks like there is nothing wrong on that regard in this script, so maybe is some changes on kicad side that break the script, if you can pinpoint the issue i can fix it

@dellmyer

This comment has been minimized.

Copy link

@dellmyer dellmyer commented Oct 15, 2020

I have the same issue
I/O error : Invalid argument
I/O error : Invalid argument

Running KiCAd 5.1.6-1
Windows 10

@gregsimon

This comment has been minimized.

Copy link

@gregsimon gregsimon commented Oct 26, 2020

The script has trouble with spaces on Windows. Move it to a (full path) without spaces worked for me.

@ignatio

This comment has been minimized.

Copy link

@ignatio ignatio commented Nov 17, 2020

I/O error : Invalid argument
I/O error : Invalid argument

Still present Windows 10 Kicad 5.1.8-1

@arturo182

This comment has been minimized.

Copy link
Owner Author

@arturo182 arturo182 commented Nov 17, 2020

Like mentioned above, it seems xsltproc doesn't like spaces in paths so try removing those.

@ignatio

This comment has been minimized.

Copy link

@ignatio ignatio commented Nov 17, 2020

hey my command line is:
xsltproc -o "D:\test.csv" "D:\bom2grouped_csv_jlcpcb.xsl" "%I"

no spaces there ^_^

@ignatio

This comment has been minimized.

Copy link

@ignatio ignatio commented Nov 17, 2020

haha it was the slashes
use / instead of \

i only realized after typing it out here lol

@heidnerd

This comment has been minimized.

Copy link

@heidnerd heidnerd commented Dec 13, 2020

I've had the same problem with spaces in the file name. Some of the plugin for KiCAD are python scripts - and they run okay. The plugins which depend on xsltproc fail on windows systems that have spaces in the file names. Just changing the directories to remove spaces in file names or altering the directory structure to remove spaces may then break behind the scene links for other parts libraries and custom parts.

Is there a way to pre-process the source and output file name strings before substituting into the command line such that the strings are passed as a URI.?

From the explanation on stackover flow:

"Most XPath and XSLT functions work with URIs and not with system/os dependent file paths so you might try to convert your Windows file path to a URI with e.g. unparsed-text(concat('file:///', replace(replace($pathToCSV, '\', '/'), ' ', '%20')))"

https://stackoverflow.com/questions/61119536/xsl-transform-filepath-with-spaces

@rtek1000

This comment has been minimized.

Copy link

@rtek1000 rtek1000 commented Jan 3, 2021

Hi,

How to get a quantity column?

The components are grouped but have no quantity column.

@rtek1000

This comment has been minimized.

Copy link

@rtek1000 rtek1000 commented Jan 3, 2021

Can the code of the plugin that already has the quantity column be used as an example to add the quantity column in this plugin to JLCPCB?

    # Add the reference of every component in the group and keep a reference
    # to the component so that the other data can be filled in once per group
    for component in group:
        if len(refs) > 0:
            refs += ", "
        refs += component.getRef()
        c = component

    # Fill in the component groups common data
    # columns = ['Item', 'Qty', 'Reference(s)', 'Value', 'LibPart', 'Footprint', 'Datasheet'] + sorted(list(columnset))
    item += 1
    row.append( item )
    row.append( len(group) )

Screenshot_2021-01-03_12-49-13
Screenshot_2021-01-03_12-56-40

@rtek1000

This comment has been minimized.

Copy link

@rtek1000 rtek1000 commented Jan 3, 2021

@tronta

This comment has been minimized.

Copy link

@tronta tronta commented Mar 6, 2021

Hi, newbie here. I'm using the 5.99 version. Should this also work for KiCad 6? If no, does an other script already exist?

@arturo182

This comment has been minimized.

Copy link
Owner Author

@arturo182 arturo182 commented Mar 8, 2021

Yes, I'm also using 5.99 and don't see why it wouldn't work with 6. I'm usually on the latest nightly so will update if I see any problems.

@jj-uk

This comment has been minimized.

Copy link

@jj-uk jj-uk commented Jul 31, 2021

Can't use this. It generates an error. Any way to fix the script?

xsltproc -o "Z:/projects/misc/Door Ctrl/Hardware/v1.0/Door Ctrl.csv" "Z:/Software/Electronics/KiCad/bom2grouped_csv_jlcpcb.xsl" "Z:\projects\misc\Door Ctrl\Hardware\v1.0\Door Ctrl.xml"
Command error. Return code 11.
I/O error : Invalid argument
I/O error : Invalid argument

@IMPULSE-255

This comment has been minimized.

Copy link

@IMPULSE-255 IMPULSE-255 commented Aug 6, 2021

Can't use this. It generates an error. Any way to fix the script?

xsltproc -o "Z:/projects/misc/Door Ctrl/Hardware/v1.0/Door Ctrl.csv" "Z:/Software/Electronics/KiCad/bom2grouped_csv_jlcpcb.xsl" "Z:\projects\misc\Door Ctrl\Hardware\v1.0\Door Ctrl.xml"
Command error. Return code 11.
I/O error : Invalid argument
I/O error : Invalid argument

As a workaround:
If you move your project to a folder without any spaces in the path it'll succeed, Looks like "Door Ctrl" is the issue on yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment