New revision of the library import tool

master
Kerem Yollu 2 years ago
parent 454704bc06
commit e13dac3c53

@ -1,82 +1,102 @@
import os import os
import glob import glob
def getFootprinLibrarys(pathTofootprints): scriptDir = os.getcwd()
cwd = os.getcwd()
os.chdir(footprintPath) def getKickedAbsPath():
'''
Returns Kicked's absolute path and goes back to the scrip's path
'''
os.chdir('..')
path = os.getcwd()
os.chdir(scriptDir)
return path
def getFootprinLibraries():
'''
Returns the list of all .pretty footprint files and goes back to the scrip's path
'''
footpintPath = os.path.join(getKickedAbsPath(), "footprints")
os.chdir(footpintPath)
footprintList = [] footprintList = []
files = os.listdir() prettyFiles = os.listdir()
# append all .pretty folders # append all .pretty folders
for f in files: for f in prettyFiles:
if f.endswith(".pretty"): if f.endswith(".pretty"):
footprintList.append(f) footprintList.append(f)
os.chdir(cwd)
return footprintList
def getSymbolsLibrarys(pathToSymbols): os.chdir(scriptDir)
search_pattern = os.path.join(pathToSymbols, "*.kicad_sym") return footprintList
return glob.glob(search_pattern)
pathToProjectFolder = input("enter path to KiCad project folder:")
# move one directory back into the main KicED library folder def getSymbolLibraries():
# https://stackoverflow.com/questions/12280143/how-to-move-to-one-folder-back-in-python '''
os.path.normpath(os.getcwd() + os.sep + os.pardir) Returns the list of all .kicad_sym simbol files and goes back to the scrip's path
'''
symbolPath = os.path.join(getKickedAbsPath(), "symbols")
os.chdir(symbolPath)
cwd = os.getcwd() symboList = []
symFiles = os.listdir()
symbolList = []
# append all .pretty folders
for f in symFiles:
if f.endswith(".kicad_sym"):
symbolList.append(f)
print(pathToProjectFolder) os.chdir(scriptDir)
print("Curent Working Path") return symbolList
print(cwd)
# Kiced Folder
os.chdir("..")
cwdOneBack = os.getcwd()
os.chdir(cwd)
# find common path # Gets the inital paths required
commonPath = os.path.commonprefix([cwd, pathToProjectFolder]) footprintLibraries = getFootprinLibraries()
symbolLibraries = getSymbolLibraries()
# KicED library paths # User input to get the path to the project
footprintPath = os.path.join(cwdOneBack, "footprints") pathToProjectFolder = input("enter path to KiCad project folder:")
symbolsPath = os.path.join(cwdOneBack, "symbols")
# create relative path from KiCad project and the KicED library # create relative path from KiCad project and the KicED library
relative_path = os.path.relpath(cwd, pathToProjectFolder) relative_path = os.path.relpath(getKickedAbsPath(),pathToProjectFolder)
print(relative_path)
# Translates the relative paht ot what kicad project needs from kiced
baseRelativeBasePath = os.path.join("${KIPRJMOD}", relative_path) baseRelativeBasePath = os.path.join("${KIPRJMOD}", relative_path)
print(baseRelativeBasePath)
footprintLibPaths = getFootprinLibrarys(footprintPath)
symbolLibPaths = getSymbolsLibrarys(symbolsPath)
temp = [] temp = []
for f in footprintLibPaths: for item in footprintLibraries:
pth = os.path.join(baseRelativeBasePath, "symbols") pth = os.path.join(baseRelativeBasePath, "footprints")
temp.append(os.path.join(pth, f)) tmp = os.path.join(pth, item)
temp.append(tmp)
footprintLibPaths = temp footprintLibPaths = temp
temp = [] temp = []
for f in symbolLibPaths: for item in symbolLibraries:
pth = os.path.relpath(f,cwd) pth = os.path.join(baseRelativeBasePath, "symbols")
pth = os.path.join(baseRelativeBasePath,pth) tmp = os.path.join(pth,item)
temp.append(pth) temp.append(tmp)
symbolLibPaths = temp symbolLibPaths = temp
if 1: # INFO PRINT #
for f in footprintLibPaths: print("Selected Project Path:")
print(f) print(pathToProjectFolder)
print("Curent Working Path:")
print(scriptDir)
print("Relative path from Project to Kiced:")
print(relative_path)
print("Formated relative path to kiced library relative to project directory")
print(baseRelativeBasePath)
print("Including following libraries:")
for f in footprintLibPaths:
print(f)
for f in symbolLibPaths:
print(f)
for f in symbolLibPaths:
print(f)
# creating footprint lib link file # creating footprint lib link file
os.chdir(pathToProjectFolder) os.chdir(pathToProjectFolder)
#print(pathToProjectFolder)
try: try:
with open("fp-lib-table",'w') as file: with open("fp-lib-table",'w') as file:
file.write("(fp_lib_table\n (version 7)\n") file.write("(fp_lib_table\n (version 7)\n")
@ -89,16 +109,15 @@ try:
file.write(" (lib (name \"") file.write(" (lib (name \"")
temp = os.path.basename(p) temp = os.path.basename(p)
file.write(temp[:-len(".pretty")]) file.write(temp[:-len(".pretty")])
file.write("\")(type \"KiCad\")(uri \"") file.write("\")(type \"KiCad\")(uri \"")
file.write(p) file.write(p)
file.write("\")(options \"\")(descr \"\"))\n") file.write("\")(options \"\")(descr \"\"))\n")
file.write(")") file.write(")")
os.chdir(cwd) os.chdir(scriptDir)
except FileNotFoundError: except FileNotFoundError:
print("fail") print("fail")
os.chdir(cwd) os.chdir(scriptDir)
# creating symbol lib link file # creating symbol lib link file
os.chdir(pathToProjectFolder) os.chdir(pathToProjectFolder)
@ -118,8 +137,10 @@ try:
file.write(p) file.write(p)
file.write("\")(options \"\")(descr \"\"))\n") file.write("\")(options \"\")(descr \"\"))\n")
file.write(")") file.write(")")
os.chdir(cwd) os.chdir(scriptDir)
except FileNotFoundError: except FileNotFoundError:
print("fail") print("fail")
os.chdir(cwd) os.chdir(scriptDir)
print("Import Done!")

@ -1,23 +0,0 @@
import tkinter as tk
from tkinter import filedialog
def select_folder():
folder_path = filedialog.askdirectory()
folder_label.config(text="Selected Folder: " + folder_path)
# Create the main window
root = tk.Tk()
root.title("KicED library linker")
# Create a label to display the selected folder
folder_label = tk.Label(root, text="Selected KiCad project folder: ")
folder_label.pack(pady=10)
# Create a button to open the file dialog
select_button = tk.Button(root, text="Select Folder", command=select_folder)
select_button.pack()
# Start the GUI application
root.mainloop()
Loading…
Cancel
Save