Neoden3V | KiCad Pos to Neoden CSV - Python Script - Page 1
Forum section: electronics / general

author: AshW
author_rank: Contributor
timestamp: December 05, 2022, 12:26:00 pm
content: Hey all,I just picked up a Neoden3V and was using femtodev's script to convert a KiCad .pos file to a .csv for the Neoden. I wanted to be able to launch the script without the command line so I modified the code to pop open a gui with the output directory.I'm no coder and it's still a work in progress to please feel free to let me know if you have anything to add or that should be fixed.Python script below:Code:# Ash's KiCad to Neoden3V file conversion code# Last updated 25/08/2024# This script is a modified version of femtodev's version of szczys's python script. It was edited in around 5 minutes, this script remains a perpetual work in progress.# Original script: https://git.femtostar.com/femtodev/kicad_to_neoden/-/blob/master/readme.md# Updated link: https://git.femtostar.com/femtodev/kicad_to_neoden/src/branch/master/k2n.pyfrom __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 bottomimport fileinputimport tkinter as tkfrom tkinter import filedialogimport osimport sysdef main(file_path):# Open the .pos file using the file pathwith open(file_path, 'r') as f:pos_lines = []for line in f:pos_lines.append(line.strip('\n').split())# Get the directory containing the input filefile_dir = os.path.dirname(file_path)filename = os.path.basename(file_path)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 latermin_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 characterif neoden_format[-2:] == '\n':neoden_format = neoden_format[:-2]print("Writing CSV file")# Create the output file path by joining the input file directory with the output file nameoutput_file = os.path.splitext(os.path.join(file_dir, filename)) + "_neoden.csv"with open(output_file, 'w') as g:g.write(neoden_format)print("Conversion complete!")# Print the output file's pathprint("Output file saved as", os.path.abspath(output_file))def select_file():# Create a file selection dialog that only allows the user to select .csv filesfile_path = filedialog.askopenfilename(filetypes=(("KiCad Position Files", "*.pos"), ("All files", "*.*")))# Convert the file using the main functionmain(file_path)class StdoutRedirector(object):def __init__(self, widget):self.widget = widgetdef write(self, string):self.widget.insert("end", string)self.widget.see("end")def transrotate(value):if value <= 180:return int(value)else:value -= 180return 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 == '#':continue# fix to output package instead of value, matching order expected by machineoutline = line + "," + line + ","outline += line.split('.') + "." + line.split('.')[:2] + "mm,"# fix to force all Y coordinates to be above zeroline = str(float(line) + abs(min_y) + 1)outline += line.split('.') + "." + line.split('.')[:2] + "mm,"if line[-1] == "top":outline += "T,"else:outline += "B,"# fix to output value instead of package, matching order expected by machineoutline += str(transrotate(float(line))) + "," + lineoutput_string += outline + '\n'return output_string# for above fixdef find_min_y(pos_lines):min_y = 0for line in pos_lines:if line == '#':continueprint(float(line))if float(line) < min_y:min_y = float(line)return min_y# Create the main windowroot = tk.Tk()# Set the text displayed in the title bar of the windowroot.title("KiCad to Neoden 3V File Converter")# Add a button that allows the user to select a fileselect_button = tk.Button(root, text="Select file", command=select_file)select_button.pack()# Add a text box to display the output from the scripttext_box = tk.Text(root)text_box.pack()# Redirect the print statements from the main function to the text boxsys.stdout = StdoutRedirector(text_box)# Start the main looproot.mainloop()Last updated 25/08/2024I thought this might be handy for the few other Neoden3V owners out there.

author: tboicey
author_rank: Contributor
timestamp: December 30, 2022, 04:53:53 pm
content: Just adding to this thread that this is also the script I use to go from KiCad to Neoden 3V.