Skip to content

Commit

Permalink
Merge pull request #76 from sparkmicro/0.5.0
Browse files Browse the repository at this point in the history
0.5.0 - Migration to KiCad v6
  • Loading branch information
eeintech committed Jan 7, 2022
2 parents ef1dd46 + 668712a commit 4f5923e
Show file tree
Hide file tree
Showing 92 changed files with 3,867 additions and 1,876 deletions.
6 changes: 4 additions & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ omit =
env-kintree/*
.venv/*
# Skip KiCad library tool
kintree/kicad/schlib.py
kintree/kicad/lib_utils/*
# Skip UI coverage
kintree/kintree_gui.py
kintree/common/progress.py
# Skip wrapt_timeout_decorator
kintree/wrapt_timeout_decorator/*
# Skip Mouser API base files
kintree/search/mouser/*
kintree/search/mouser/*
# Skip test script
run_tests.py
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pip install dist/kintree-0.4.99-py3-none-any.whl
> Priority goes to implementing new features over GUI improvements
> Open to new ideas and pull requests :smiley:
#### Versions 0.4.x or later
#### Versions 0.5.x or later
##### New Features

- Allow user to decide the category code to use for IPN
Expand Down
2 changes: 1 addition & 1 deletion kintree/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# VERSION INFORMATION
version_info = {
'MAJOR_REVISION': 0,
'MINOR_REVISION': 4,
'MINOR_REVISION': 5,
'RELEASE_STATUS': 99, # Dev = 99
}

Expand Down
10 changes: 5 additions & 5 deletions kintree/config/config_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ def load_libraries_paths(user_config_path: str, library_path: str) -> dict:

found_library_files = []
for file in os.listdir(library_path):
if file.endswith('.lib'):
found_library_files.append(file.replace('.lib', ''))
if file.endswith('.kicad_sym'):
found_library_files.append(file.replace('.kicad_sym', ''))

symbol_libraries_paths = {}
assigned_files = []
Expand All @@ -209,7 +209,7 @@ def load_libraries_paths(user_config_path: str, library_path: str) -> dict:
for library in libraries:
if library in found_library_files:
symbol_libraries_paths[category][library] = library_path + \
library + '.lib'
library + '.kicad_sym'
assigned_files.append(library)
except:
pass
Expand Down Expand Up @@ -256,10 +256,10 @@ def load_templates_paths(user_config_path: str, template_path: str) -> dict:
if file_name:
try:
symbol_templates_paths[category][subcategory] = template_path + \
file_name + '.lib'
file_name + '.kicad_sym'
except:
symbol_templates_paths[category] = {
subcategory: template_path + file_name + '.lib'}
subcategory: template_path + file_name + '.kicad_sym'}
except:
pass

Expand Down
2 changes: 1 addition & 1 deletion kintree/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def set_kicad_enable_flag(value: bool, save=False):

AUTO_GENERATE_LIB = True
symbol_template_lib = os.path.join(
PROJECT_DIR, 'kicad', 'templates', 'library_template.lib')
PROJECT_DIR, 'kicad', 'templates', 'library_template.kicad_sym')


# INVENTREE
Expand Down
23 changes: 10 additions & 13 deletions kintree/kicad/kicad_interface.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from . import kicad_schlib

klib = kicad_schlib.ComponentLibManager()
from . import kicad_symbol


def inventree_to_kicad(part_data: dict, library_path: str, template_path=None, show_progress=True) -> bool:
''' Create KiCad symbol from InvenTree part data '''
return klib.add_component_to_library_from_inventree(component_data=part_data,
library_path=library_path,
template_path=template_path,
show_progress=show_progress)


def delete_part(part_number: str, library_path: str) -> bool:
''' Delete KiCad symbol from library '''
return klib.delete_component_from_lib(part_number=part_number,
library_path=library_path)
klib = kicad_symbol.ComponentLibManager(library_path)
return klib.add_symbol_to_library_from_inventree(symbol_data=part_data,
template_path=template_path,
show_progress=show_progress)

# NOT YET SUPPORTED - REMOVE?
# def delete_part(part_number: str, library_path: str) -> bool:
# ''' Delete KiCad symbol from library '''
# pass
137 changes: 0 additions & 137 deletions kintree/kicad/kicad_schlib.py

This file was deleted.

124 changes: 124 additions & 0 deletions kintree/kicad/kicad_symbol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import os

from ..config import settings
from ..common import progress
from ..common.tools import cprint
from lib_utils import kicad_sym


# KiCad Component Library Manager
class ComponentLibManager(object):
def __init__(self, library_path):
# Load library and template paths
cprint(f'[KCAD]\tlibrary_path: {library_path}', silent=settings.SILENT)

# Check files exist
if not os.path.isfile(library_path):
cprint(f'[KCAD]\tError loading library file ({library_path})', silent=settings.SILENT)
return None

# Load library
self.kicad_lib = kicad_sym.KicadLibrary.from_file(library_path)
self.library_name = library_path.split(os.sep)[-1]
cprint('[KCAD]\tNumber of parts in library ' + self.library_name + ': ' + str(len(self.kicad_lib.symbols)), silent=settings.SILENT)

def is_symbol_in_library(self, symbol_part_number):
''' Check if symbol already exists in library '''
for symbol in self.kicad_lib.symbols:
cprint(f'[DBUG]\t{symbol.name} ?= {symbol_part_number}', silent=settings.HIDE_DEBUG)
if symbol.name == symbol_part_number:
cprint(f'[KCAD]\tWarning: Component {symbol_part_number} already in library', silent=settings.SILENT)
return True

return False

def add_symbol_to_library_from_inventree(self, symbol_data, template_path=None, show_progress=True):
''' Create symbol in KiCad library '''
part_in_lib = False
new_part = False
category = symbol_data['category'][0]
subcategory = symbol_data['category'][1]

if not template_path:
# Fetch template path
try:
template_path = settings.symbol_templates_paths[category][subcategory]
except:
template_path = settings.symbol_templates_paths[category]['Default']

# Check files exist
if not self.kicad_lib:
return part_in_lib, new_part
if not os.path.isfile(template_path):
cprint(f'[KCAD]\tError loading template file ({template_path})', silent=settings.SILENT)
return part_in_lib, new_part

# Check if part already in library
try:
is_symbol_in_library = self.is_symbol_in_library(symbol_data['IPN'])
part_in_lib = True
except:
is_symbol_in_library = False
if is_symbol_in_library:
return part_in_lib, new_part

# Progress Update
if show_progress and not progress.update_progress_bar_window():
return part_in_lib, new_part

# Load template
templatelib = kicad_sym.KicadLibrary.from_file(template_path)
# Load new symbol
if len(templatelib.symbols) == 1:
for symbol in templatelib.symbols:
new_symbol = symbol
else:
cprint('[KCAD]\tError: Found more than 1 symbol template in template file, aborting', silent=settings.SILENT)
return part_in_lib, new_part

# Update name
new_symbol.name = symbol_data['IPN']

# Update properties
for property in new_symbol.properties:
# Main data
if property.value in symbol_data.keys():
property.value = symbol_data[property.value]
continue

# Parameters
if property.value in symbol_data['parameters'].keys():
property.value = symbol_data['parameters'][property.value]
continue

# Special properties
if property.name in ['Value', 'Manufacturer', 'Manufacturer Part Number']:
if property.name == 'Value':
property.value = symbol_data['IPN']
elif property.name == 'Manufacturer':
property.value = list(symbol_data['manufacturer'].keys())[0]
elif property.name == 'Manufacturer Part Number':
property.value = list(symbol_data['manufacturer'].values())[0][0]
continue

# Add symbol to library
self.kicad_lib.symbols.append(new_symbol)
# Update generator version
self.kicad_lib.version = '20211014'
# Write library
self.kicad_lib.write()

cprint(f'[KCAD]\tSuccess: Component added to library {self.library_name}', silent=settings.SILENT)
part_in_lib = True
new_part = True

# Progress Update
if show_progress and not progress.update_progress_bar_window():
pass

return part_in_lib, new_part

# NOT YET SUPPORTED - REMOVE?
# def delete_symbol_from_lib(self, part_number):
# ''' Remove symbol from KiCad library '''
# pass
Loading

0 comments on commit 4f5923e

Please sign in to comment.