From 19003424bdf2ee177b7b1091f6bdbbca1c925aa3 Mon Sep 17 00:00:00 2001 From: andrewc Date: Thu, 8 Aug 2024 15:39:27 +1000 Subject: [PATCH] Added script to rotate a pnp file --- .scripts/rot_pick.py | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100755 .scripts/rot_pick.py diff --git a/.scripts/rot_pick.py b/.scripts/rot_pick.py new file mode 100755 index 0000000..e0c6805 --- /dev/null +++ b/.scripts/rot_pick.py @@ -0,0 +1,69 @@ +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)