Pick and Place Centroid CSV Converter: Python Script Generator
CAD tools export centroid files in their own column layout and rotation convention, while the pick and place machine expects another. This generator produces a short Python script that filters one side, applies offsets and writes a clean CSV you can load in your programming software.
Fill in
convert_pnp.py
import csv
IN_FILE = 'board_pos.csv'
OUT_FILE = 'machine_pnp.csv'
SIDE = 'top'
X_OFF = 0.0
Y_OFF = 0.0
ROT_OFF = 0
rows = []
with open(IN_FILE, newline='') as f:
reader = csv.DictReader(f)
for r in reader:
if r['Side'].strip().lower() != SIDE:
continue
x = float(r['PosX']) + X_OFF
y = float(r['PosY']) + Y_OFF
rot = (float(r['Rot']) + ROT_OFF) % 360
rows.append([r['Ref'], r['Val'], r['Package'], round(x, 4), round(y, 4), round(rot, 1), SIDE])
rows.sort(key=lambda row: row[0])
with open(OUT_FILE, 'w', newline='') as f:
w = csv.writer(f)
w.writerow(['RefDes', 'Value', 'Package', 'X_mm', 'Y_mm', 'Rotation_deg', 'Side'])
w.writerows(rows)
print('Wrote %d placements to %s' % (len(rows), OUT_FILE))Per package rotation corrections (optional)
# Some machine libraries define zero degrees differently from the CAD library.
# Add package specific corrections before the rows.append call, for example:
PKG_ROT_FIX = {
'SOT-23': 180,
'SOIC-8': 90,
}
# rot = (rot + PKG_ROT_FIX.get(r['Package'], 0)) % 360Verify the output on a first article. Machine vendors define coordinate origin, rotation direction and bottom side mirroring differently, so check the first board on the machine before running a lot. The script assumes millimetre units and a header row.
Automate BOM and placement data flow
Want CAD, BOM and ERP kept in sync? We can talk through an approach.
Optional. The tool and every export work without this. No spam.
How to use the generator
Open your centroid file and note the exact column names. Enter them in the fields, choose top or bottom, and set any X, Y and rotation offsets. Copy the generated script into a file, install Python 3 and run it from the folder containing the CSV. The output is a sorted file with a standard header that most programming software can import with a column mapping.
Coordinates and rotation
Centroid data has an origin, a unit and a rotation direction. The origin may be the board corner or the auxiliary axis. Rotation of zero degrees in the CAD library does not always match zero degrees in the machine library, which is why package based corrections are common. The bottom side is often mirrored, so confirm how your CAD tool writes it.
- Confirm the origin on the fabrication drawing
- Check units: mm or mils
- Confirm clockwise or counter clockwise rotation
- Check polarized parts first on a trial board
Worked example
A KiCad position file lists parts with columns Ref, Val, Package, PosX, PosY, Rot and Side. Setting the side to top and a 0.0 offset produces a sorted file of top side placements. If the first board shows every SOT-23 rotated by 180 degrees, uncomment the package correction block and re-run.
Common mistakes
Common mistakes include forgetting that the file uses a decimal comma in some regions, ignoring parts marked do not populate, and exporting both sides into the same machine program. Always compare the part count in the output with the BOM quantity for the chosen side.
Frequently Asked Questions
Does this script handle bottom side mirroring?
Not automatically. It filters the side and applies offsets. Some machines expect mirrored X for the bottom side, so add that step if your vendor requires it, and verify on a first article.
Which Python version is needed?
Any current Python 3 version works. The script uses only the standard library csv module, so no extra packages are required.
Can I use it with Excel exported CSV files?
Yes, if the file is saved as CSV with a header row and a dot decimal separator. Check the column names exactly, including capitalisation and spaces.
How do I check the result?
Compare the number of lines with the BOM count for that side, load the file into the machine software and run a trial board. Check polarized parts and connectors first.
Netray can help connect CAD, BOM and ERP data so placement files, BOM revisions and work orders stay consistent.
Related Tools
PCBA BOM Staging Table and Validation SQL Generator
Create a staging table and validation queries to clean an electronic BOM before loading it into an ERP.
MES & Shop FloorSMT Line Changeover and Feeder Setup Worksheet
Plan the daily SMT schedule with changeover, feeder setup and run time per job so you can see true line utilization.
Electronics ManufacturingPCBA First Article Inspection Report Template
A fill-in first article report for a new PCB assembly job: setup verification, BOM and placement checks, solder inspection and sign-off.
Electronics ManufacturingPCBA DFM Design for Manufacturing Review Checklist
Checklist to review a PCB design for assembly before release: panel, fiducials, pads, stencil, test and documentation.
More Developer Tools tools
See all- Component Shortage SQL Query Generator (Open Demand vs Supply)
- BOM Scrub Python Script Generator (Duplicate MPN, Missing RefDes, Qty Mismatch)
- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
- SAP QM Usage Decision BAPI Skeleton