How to add fields to the python script as in the example image?

Hi all, my name is William.
I am new to KiCad.
I am an hardware design engineer, i have a broad level of knowledge in electronics and i can also do c programming.
I have experience with several schematics & pcb CAD programs like Cadence Orcad , Altium , Protel.
And i would like to familiarize myself with KiCad.
For work purposes and to make pcb’s for the hobby.
I am very fond of the pretty intuitive and simple user interface.

I would like to create a copy of the python script to make a version that generates extra fields in the csv script. I am not a python wizard, i can write a script with some trial and error. But i am not able to understand how the kicad netlist reader functions or the kicad utils. I can ask friends and colleagues how to do python but in order to do so i need a basic understanding how the python script is retrieving the fields from the schematic .
It is about the python script : bom_csv_grouped_extra.py
See the end of the post for an excerpt.

Does anybody know how to do this ?
Seems like a standard question.
But i have no idea.

Thank you very much in advance.
William.

Excerpt form the python script :
"

@package
Output: CSV (comma-separated)
Grouped By: Value, Footprint, DNP, specified extra fields
Sorted By: Reference
Fields: #, Reference, Qty, Value, Footprint, DNP, specified extra fields

Outputs components grouped by Value, Footprint, and specified extra fields.
Extra fields can be passed as command line arguments at the end, one field per argument.

Command line:
python "pathToFile/bom_csv_grouped_extra.py" "%I" "%O.csv" "Extra_Field1" "Extra_Field2"

Import the KiCad python helper module and the csv formatter

import kicad_netlist_reader
import kicad_utils
import csv
import sys

Get extra fields from the command line

extra_fields = sys.argv[3:]

comp_fields = [‘Value’, ‘Footprint’, ‘DNP’] + extra_fields
header_names = [‘#’, ‘Reference’, ‘Qty’] + comp_fields

def getComponentString(comp, field_name):
if field_name == “Value”:
return comp.getValue()
elif field_name == “Footprint”:
return comp.getFootprint()
elif field_name == “DNP”:
return comp.getDNPString()
elif field_name == “Datasheet”:
return comp.getDatasheet()
else:
return comp.getField( field_name )
"

because that script is not processing the netlist, the other script “kicad_netlist_reader” should be in the same folder, is doing the heavy lifting and neatly putting everything in the structures used by the BOM script. Take a look at it.

If you just want to add your Partnumber and other fields, add field names to the command line in the BOM dialog, like the script description says, for example:

python3 "/usr/share/kicad/plugins/bom_csv_grouped_extra.py" "%I" "%O.csv" "Manufacturer Partnumber" "Farnell Number" "Mouser Number" "Digikey Number" "Cost"

(Change the path to the script for your system accordingly)

Thank you dsa-t. I had such a feeling while reading the script but i thought it would be safe to ask first.
I tried adding fields and it works but i would like to remove the footprint column. I assume i can just remove that option from the python script after making a backup first. That would be oke or is there something i need to take into account ?

.

Yes, you can just remove 'Footprint', from the array.

Thank you. I will do so.

I removed the footprint from the array and from the getcomponentstring.
But i also would like to change the sort order.

Unfortunately, i cannot just adjust the array. Because of these lines in the python script :
"

Get all of the components in groups of matching parts + values

(see kicad_netlist_reader.py)

grouped = net.groupComponents()
"
This part in the script is part of the kicad_netlist_reader.py and i assume it is there that the sorting takes place. Modifying only the header is not enough as can be seen in the screenshot. And i have not enough experience with python to modify that. I have to see how to solve that. I have no idea. I have to modify the kicad_netlist_reader.py script as well. And that is bit much for me. Any suggestions would be nice.

What would be an even better script, is a script where all the fields are added at the command line and there are 0 predefined fields. That would be a universal usable script. For everybody.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.