示例#1
0
                settings.CONFIG_KICAD_CATEGORY_MAP,
                footprint_libraries_test_path)
            if not (symbol_libraries_paths and footprint_libraries_paths):
                method_results = False

            # Add symbol library to user file
            add_symbol_lib = config_interface.add_library_path(
                user_config_path=settings.CONFIG_KICAD_CATEGORY_MAP,
                category='category_test',
                symbol_library='symbol_library_test')
            if not add_symbol_lib:
                method_results = False

            # Add footprint library to user file
            add_footprint_lib = config_interface.add_footprint_library(
                user_config_path=settings.CONFIG_KICAD_CATEGORY_MAP,
                category='category_test',
                library_folder='footprint_folder_test')
            if not add_footprint_lib:
                method_results = False

            # Add supplier category
            categories = {'Capacitors': {'Super': 'Super'}}
            add_category = config_interface.add_supplier_category(
                categories, settings.CONFIG_DIGIKEY_CATEGORIES)
            if not add_category:
                method_results = False

            # Synchronize InvenTree and Supplier categories
            sync_categories = config_interface.sync_inventree_supplier_categories(
                inventree_config_path=settings.CONFIG_CATEGORIES,
                supplier_config_path=settings.CONFIG_DIGIKEY_CATEGORIES)
示例#2
0
def user_defined_symbol_template_footprint(categories: list,
										   part_number: str,
										   symbol_lib=None,
										   template=None,
										   footprint_lib=None,
										   symbol_confirm=False,
										   footprint_confirm=False):
	''' Symbol and Footprint user defined window '''
	symbol = None
	footprint = None

	if symbol_confirm and '---' not in symbol_lib:
		if not config_interface.add_library_path(user_config_path=settings.CONFIG_KICAD_CATEGORY_MAP,
												 category=categories[0],
												 symbol_library=symbol_lib):
			cprint(f'[INFO]\tWarning: Failed to add symbol library to {categories[0]} category', silent=settings.SILENT)

	if footprint_confirm and '---' not in footprint_lib:
		if not config_interface.add_footprint_library(user_config_path=settings.CONFIG_KICAD_CATEGORY_MAP,
													  category=categories[0],
													  library_folder=footprint_lib):
			cprint(f'[INFO]\tWarning: Failed to add footprint library to {categories[0]} category', silent=settings.SILENT)
	
	# Load user settings
	settings.load_kicad_settings()

	def fuzzy_default(value:str, choices:list) -> str:
		match = None
		LIMIT = 85

		for item in choices:
			fuzzy_match = fuzz.partial_ratio(value, item)
			display_result = f'"{value}" ?= "{item}"'.ljust(50)
			cprint(f'{display_result} => {fuzzy_match}', silent=settings.HIDE_DEBUG)
			if fuzzy_match >= LIMIT:
				match = item
				break

		return match

	def build_choices(items: dict, category: str, subcategory=None) -> list:
		choices = []

		try:
			for key, value in items[category].items():
				if value:
					choices.append(key)
		except:
			if subcategory:
				error_message = f'Warning: No templates defined for "{category}"'
				cprint(f'[INFO]\t{error_message}', silent=settings.SILENT)
				# sg.popup_ok(error_message, title='No Templates', location=(500, 500))

		if subcategory:
			# Load templates only for given category/subcategory pair
			return sorted(choices)

		# Separate libraries not officially assigned to category
		if choices:
			choices.append('-' * 10)

		more_choices = []
		for cat in items.keys():
			if cat != category and cat != 'uncategorized':
				for key in items[cat].keys():
					more_choices.append(key)

		# Process uncategorized entries
		try:
			for item in items['uncategorized']:
				more_choices.append(item)
		except:
			# error_message = f'Warning: No libraries defined for "{category}"'
			# cprint(f'[INFO]\t{error_message}', silent=settings.SILENT)
			# sg.popup_ok(error_message, title='No Libraries', location=(500, 500))
			pass
		try:
			choices.extend(sorted(more_choices))
		except:
			pass

		return choices

	# Load symbol libraries
	if not settings.KICAD_SYMBOLS_PATH:
		sg.popup_ok(f'Error: KiCad symbol library folder path is not defined ("Settings > KiCad")',
					title='KiCad Symbol Library Folder',
					location=(500, 500))
		return symbol, template, footprint

	symbol_library = config_interface.load_libraries_paths(user_config_path=settings.CONFIG_KICAD_CATEGORY_MAP,
														   library_path=settings.KICAD_SYMBOLS_PATH)
	# cprint(symbol_library)
	if not symbol_library:
		sg.popup_ok(f'Error: Symbol library files were not found in {settings.KICAD_SYMBOLS_PATH}',
					title='KiCad Symbol Library Folder',
					location=(500, 500))
		return symbol, template, footprint
	
	# Build symbol choices
	symbol_lib_choices = build_choices(symbol_library, categories[0])
	if symbol_lib:
		symbol_lib_default = symbol_lib
	else:
		# Try fuzzy matching
		symbol_lib_default = fuzzy_default(categories[0], symbol_lib_choices)
		
		if not symbol_lib_default:
			symbol_lib_default = symbol_lib_choices[0]

	# Load templates
	if not settings.KICAD_TEMPLATES_PATH:
		sg.popup_ok(f'Error: KiCad template folder path is not defined ("Settings > KiCad")',
					title='KiCad Template Folder',
					location=(500, 500))
		return symbol, template, footprint

	templates = config_interface.load_templates_paths(user_config_path=settings.CONFIG_KICAD_CATEGORY_MAP,
													  template_path=settings.KICAD_TEMPLATES_PATH)
	# cprint(templates)
	if not templates:
		sg.popup_ok(f'Error: Template files were not found in {settings.KICAD_TEMPLATES_PATH}',
					title='KiCad Template Folder',
					location=(500, 500))
		return symbol, template, footprint
	
	# Build template choices
	if not categories[0]:
		category = symbol_lib_choices[0]
	else:
		category = categories[0]
	if not categories[1]:
		subcategory = 'None'
	else:
		subcategory = categories[1]
	try:
		template_choices = build_choices(templates, category, subcategory)
		
		if template:
			template_default = template
		else:
			template_default = template_choices[0]
	except:
		pass

	if not template_choices:
		template_choices = ['None']
		template_default = template_choices[0]

	# Load footprint libraries
	if not settings.KICAD_FOOTPRINTS_PATH:
		sg.popup_ok(f'Error: KiCad footprint library folder path is not defined ("Settings > KiCad")',
					title='KiCad Footprint Library Folder',
					location=(500, 500))
		return symbol, template, footprint

	footprint_library = config_interface.load_footprint_paths(user_config_path=settings.CONFIG_KICAD_CATEGORY_MAP,
															  footprint_path=settings.KICAD_FOOTPRINTS_PATH)
	# cprint(f'{footprint_library=}')
	if not footprint_library:
		sg.popup_ok(f'Error: Footprint library files were not found in {settings.KICAD_FOOTPRINTS_PATH}',
					title='KiCad Footprint Library Folder',
					location=(500, 500))
		return symbol, template, footprint
	
	# Build symbol choices
	footprint_lib_choices = build_choices(footprint_library, categories[0])

	# Footprint mod list
	footprint_mod_choices = []
	if footprint_lib:
		footprint_lib_default = footprint_lib
		try:
			footprint_lib_path = footprint_library[categories[0]][footprint_lib]
		except:
			pass

		try:
			footprint_mod_choices = [ item.replace('.kicad_mod','') \
									  for item in sorted(os.listdir(footprint_lib_path)) \
									  if os.path.isfile(os.path.join(footprint_lib_path, item)) ]
		except:
			cprint(f'[INFO]\tWarning: Failed fetching footprint mod files for {footprint_lib}', silent=settings.SILENT)
			# cprint(f'{footprint_lib=}\t{categories[0]}', silent=settings.HIDE_DEBUG)
			cprint(footprint_library, silent=settings.HIDE_DEBUG)
	else:
		# Try fuzzy matching
		footprint_lib_default = fuzzy_default(categories[0], footprint_lib_choices)

		if not footprint_lib_default:
			footprint_lib_default = footprint_lib_choices[0]
		try:
			footprint_lib_path = footprint_library[categories[0]][footprint_lib_default]
			footprint_mod_choices = [ item.replace('.kicad_mod','') \
									  for item in sorted(os.listdir(footprint_lib_path)) \
									  if os.path.isfile(os.path.join(footprint_lib_path, item)) ]
		except:
			cprint(f'[INFO]\tWarning: Failed fetching footprint mod files for {footprint_lib_default}', silent=settings.SILENT)
			# cprint(f'{footprint_lib_default=}\t{categories[0]}', silent=settings.HIDE_DEBUG)
			cprint(footprint_library, silent=settings.HIDE_DEBUG)

	if not footprint_mod_choices:
		footprint_mod_choices = ['None']
		footprint_mod_default = footprint_mod_choices[0]
	else:
		footprint_mod_default = None

	library_layout = [
		[
			sg.Text('Select Symbol Library:'),
			sg.Combo(symbol_lib_choices, default_value=symbol_lib_default, key='symbol_lib'),
			sg.Button('Confirm'),
		],
		[
			sg.Text(f'Select Symbol Template ({categories[0]}):'),
			sg.Combo(template_choices, default_value=template_default, key='template'),
		],
		[
			sg.Text('Select Footprint Library:'),
			sg.Combo(footprint_lib_choices, default_value=footprint_lib_default, key='footprint_lib'),
			sg.Button('Confirm'),
		],
		[
			sg.Text('Select Footprint:'),
			sg.Combo(footprint_mod_choices, default_value=footprint_mod_default, key='footprint_mod_sel'),
			sg.Text('Or Enter Name:'),
			sg.In(size=(20,1),key='footprint_mod_man'),
		],
		[ sg.Text('') ],
	]

	if part_number:
		library_layout.append([ sg.Button('Check SnapEDA'), sg.Button('Submit') ])
	else:
		library_layout.append([ sg.Button('Submit') ])

	library_window = sg.Window('KiCad Libraries', library_layout, location=(500, 500))
	lib_event, lib_values = library_window.read()
	library_window.close()

	if lib_event == sg.WIN_CLOSED:
		return symbol, template, footprint
	elif lib_event == 'Check SnapEDA':
		# SnapEDA window
		snapeda_window(part_number)
		return user_defined_symbol_template_footprint(categories=categories,
													  part_number=part_number,
													  symbol_lib=lib_values['symbol_lib'],
													  template=lib_values['template'],
													  footprint_lib=lib_values['footprint_lib'])
	elif lib_event == 'Confirm':
		return user_defined_symbol_template_footprint(categories=categories,
													  part_number=part_number,
													  symbol_lib=lib_values['symbol_lib'],
													  template=lib_values['template'],
													  footprint_lib=lib_values['footprint_lib'],
													  symbol_confirm=True)
	elif lib_event == 'Confirm0':
		return user_defined_symbol_template_footprint(categories=categories,
													  part_number=part_number,
													  symbol_lib=lib_values['symbol_lib'],
													  template=lib_values['template'],
													  footprint_lib=lib_values['footprint_lib'],
													  footprint_confirm=True)
	else:
		symbol = lib_values['symbol_lib']
		template = lib_values['template']
		if lib_values['footprint_mod_man']:
			footprint = lib_values['footprint_lib'] + ':' + lib_values['footprint_mod_man']
		elif lib_values['footprint_mod_sel'] and lib_values['footprint_mod_sel'] != 'None':
			footprint = lib_values['footprint_lib'] + ':' + lib_values['footprint_mod_sel']
		
		if not footprint:
			footprint = lib_values['footprint_lib'] + ':' + settings.footprint_name_default

		# Save paths
		if not config_interface.add_library_path(user_config_path=settings.CONFIG_KICAD_CATEGORY_MAP,
												 category=categories[0],
												 symbol_library=lib_values['symbol_lib']):
			cprint(f'[INFO]\tWarning: Failed to add symbol library to {categories[0]} category', silent=settings.SILENT)

		if not config_interface.add_footprint_library(user_config_path=settings.CONFIG_KICAD_CATEGORY_MAP,
													  category=categories[0],
													  library_folder=lib_values['footprint_lib']):
			cprint(f'[INFO]\tWarning: Failed to add footprint library to {categories[0]} category', silent=settings.SILENT)

		return symbol, template, footprint