You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.6 KiB

import os
import glob
def getFootprinLibrarys(pathTofootprints):
cwd = os.getcwd()
os.chdir(footprintPath)
footprintList = []
files = os.listdir()
# append all .pretty folders
for f in files:
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)
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)
cwd = os.getcwd()
print(pathToProjectFolder)
print("Curent Working Path")
print(cwd)
# Kiced Folder
os.chdir("..")
cwdOneBack = os.getcwd()
os.chdir(cwd)
# find common path
commonPath = os.path.commonprefix([cwd, pathToProjectFolder])
# KicED library paths
footprintPath = os.path.join(cwdOneBack, "footprints")
symbolsPath = os.path.join(cwdOneBack, "symbols")
# create relative path from KiCad project and the KicED library
relative_path = os.path.relpath(cwd, pathToProjectFolder)
print(relative_path)
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))
footprintLibPaths = temp
temp = []
for f in symbolLibPaths:
pth = os.path.relpath(f,cwd)
pth = os.path.join(baseRelativeBasePath,pth)
temp.append(pth)
symbolLibPaths = temp
if 1:
for f in footprintLibPaths:
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")
if 1:
for p in footprintLibPaths:
p = p.replace("\\","/")
# example
# (lib (name "000_SDM_Resistor")(type "KiCad")(uri "${KIPRJMOD}/test_lib/footprints/000_SDM_Resistor.pretty")(options "")(descr ""))
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)
except FileNotFoundError:
print("fail")
os.chdir(cwd)
# creating symbol lib link file
os.chdir(pathToProjectFolder)
try:
with open("sym-lib-table",'w') as file:
file.write("(sym_lib_table\n (version 7)\n")
if 1:
for p in symbolLibPaths:
p = p.replace("\\","/")
# example
# (lib (name "000_SDM_Resistor")(type "KiCad")(uri "C:/Users/Edwin/Documents/19_git/00_KiCad_Projects/KicED/footprints/000_SDM_Resistor.pretty")(options "")(descr ""))
file.write(" (lib (name \"")
temp = os.path.basename(p)
file.write(temp[:-len(".kicad_sym")])
file.write("\")(type \"KiCad\")(uri \"")
file.write(p)
file.write("\")(options \"\")(descr \"\"))\n")
file.write(")")
os.chdir(cwd)
except FileNotFoundError:
print("fail")
os.chdir(cwd)