kicad_to_neoden/k2n.py

98 lines
3.6 KiB
Python
Raw Permalink Normal View History

2021-10-17 09:05:30 +00:00
#!/usr/bin/python
# This is a version of szczys's kicad-to-neoden, modified for use by the FemtoStar Project, so as to output the
# format expected by the Neoden 3V we use. We admit - it's kind of hackish, and remains a work in progress.
# Right now, this is not likely to work with negative X coordinates in the inputs, but will handle negative Y, which
# KiCAD's .pos files use for objects in the +X/YY quadrant of the board in KiCAD's coordinate system.
# This matches the quadrant KiCAD displays the page layout in, and so should usually be correct.
from __future__ import print_function
#Translator for converting KiCAD .pos files to .csv files for a NEODEN pick and place machine
#Paste relevant rows of the .pos files below and chance the trailing_letter
#to be "T" for top or "B" for bottom
import fileinput
import os
import sys
def transrotate(value):
if value <= 180:
return int(value)
else:
value -= 180
return int(0-(180-value))
def process_pos_lines(pos_lists, min_y):
output_string = "Designator,Footprint,Mid X,Mid Y,Layer,Rotation,Comment\n"
output_string += ",,,,,,\n"
for line in pos_lists:
if line[0][0] == '#':
continue
# fix to output package instead of value, matching order expected by machine
outline = line[0] + "," + line[2] + ","
outline += line[3].split('.')[0] + "." + line[3].split('.')[1][:2] + "mm,"
# fix to force all Y coordinates to be above zero
line[4] = str(float(line[4]) + abs(min_y) + 1)
outline += line[4].split('.')[0] + "." + line[4].split('.')[1][:2] + "mm,"
if line[-1] == "top":
outline += "T,"
else:
outline += "B,"
# fix to output value instead of package, matching order expected by machine
outline += str(transrotate(float(line[5]))) + "," + line[1]
output_string += outline + '\n'
return output_string
# for above fix
def find_min_y(pos_lines):
min_y = 0
for line in pos_lines:
if line[0][0] == '#':
continue
print(float(line[4]))
if float(line[4]) < min_y:
min_y = float(line[4])
return min_y
def main():
if len(sys.argv) != 2:
print("Syntax error!")
print("Usage: python kicad_to_neoden.py your_position_file_from_kicad.pos")
return
#Turn input .pos file into a list of lists
pos_lines = list()
for line in fileinput.input():
pos_lines.append(line.strip('\n').split())
cur_dir = os.getcwd()
filename = fileinput.filename()
if filename[-4:] != ".pos":
print("WARNING: Input file doesn't have expected '.pos' extension")
print("Parsing " + filename)
# get the minimum Y coordinate on the board, to shift components up later
min_y = find_min_y(pos_lines)
print("Minimum Y coordinate on board: " + str(min_y))
neoden_format = process_pos_lines(pos_lines, min_y)
#Strip trailing newline character
if neoden_format[-2:] == '\n':
neoden_format = neoden_format[:-2]
print("Writing CSV file")
output_file = os.path.splitext(os.path.join(cur_dir,filename))[0]+"_neoden.csv"
with open(output_file, 'w') as ofile:
ofile.write(neoden_format)
print("Successfully wrote:",output_file)
if __name__ == '__main__':
main()