New revision of the library import tool

master
Kerem Yollu 2 years ago
parent 454704bc06
commit e13dac3c53

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