|
|
|
@ -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)
|
|
|
|
|
return footprintList
|
|
|
|
|
|
|
|
|
|
def getSymbolsLibrarys(pathToSymbols):
|
|
|
|
|
search_pattern = os.path.join(pathToSymbols, "*.kicad_sym")
|
|
|
|
|
return glob.glob(search_pattern)
|
|
|
|
|
os.chdir(scriptDir)
|
|
|
|
|
return footprintList
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
print(f)
|
|
|
|
|
# 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:
|
|
|
|
|
print(f)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
file.write(")")
|
|
|
|
|
os.chdir(scriptDir)
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
print("fail")
|
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
os.chdir(scriptDir)
|
|
|
|
|
|
|
|
|
|
# creating symbol lib link file
|
|
|
|
|
os.chdir(pathToProjectFolder)
|
|
|
|
@ -118,8 +137,10 @@ try:
|
|
|
|
|
file.write(p)
|
|
|
|
|
file.write("\")(options \"\")(descr \"\"))\n")
|
|
|
|
|
|
|
|
|
|
file.write(")")
|
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
file.write(")")
|
|
|
|
|
os.chdir(scriptDir)
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
print("fail")
|
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
os.chdir(scriptDir)
|
|
|
|
|
|
|
|
|
|
print("Import Done!")
|
|
|
|
|