Files
kicad-ci/.scripts/rot_pick.py
Andrew Collins 51cacff367 Inventree
2025-01-16 07:12:50 +00:00

70 lines
2.0 KiB
Python
Executable File

import csv
import sys
import math
import ast
if len(sys.argv) != 4:
print("Invalid num args:", len(sys.argv),", should be 4")
exit(1)
file = sys.argv[1]
rot_deg = float(sys.argv[2])
dims = [float(i) for i in ast.literal_eval(sys.argv[3])]
corrected_rows = []
with open(file,'r', newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
# Read until the header row
for row in spamreader:
corrected_rows.append(row)
# Header row
# Designator,Comment,Footprint,Mid X(mm),Mid Y(mm) ,Rotation,Head ,FeederNo,Mount Speed(%),Pick Height(mm),Place Height(mm),Mode,Skip
if "Designator" in row:
break;
# Now the remaining rows should all be placement items
# e.g
# U2,AH1806-W,SC-59,220.50,11.10,-0.00,0,53,50,0.0,0.0,1,0
for row in spamreader:
# Skip empty rows
if len(row) < 10:
continue
# To rotate we need to change cols: 3,4,5 (x,y,rot)
# Easisest way is to conver to polar
x = float(row[3])
y = float(row[4])
print("Before: ", x,y)
# rot = float(row[5])
r = (x**2 + y**2)**0.5
theta = math.atan(y/x)
# Add the rot
rot_rad = math.pi * rot_deg/180
theta += rot_rad
# Convert back to cartesian
x = r * math.cos(theta)
y = r * math.sin(theta)
# Shift x so that the origin can be maintained
x += math.sin(rot_rad) * dims[1]
print("After: ", x,y)
# Add the rotation to the component rotation
comp_rot = float(row[5]) + rot_deg
row[3] = str(x)
row[4] = str(y)
row[5] = str(comp_rot)
corrected_rows.append(row)
with open(file.split('.csv')[0] + '_rot' + '.csv','w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in corrected_rows:
spamwriter.writerow(row)