Starting to add an output to create PCB variants

- WIP, some part is working
This commit is contained in:
Salvador E. Tropea 2022-09-08 13:54:37 -03:00
parent 42aa142a56
commit 409640c681
5 changed files with 73 additions and 45 deletions

View File

@ -94,18 +94,6 @@ class AnyLayerOptions(VariantOptions):
# We'll come back to this on a per-layer basis
po.SetSkipPlotNPTH_Pads(False)
def filter_components(self, board):
""" Apply the variants and filters """
if not self._comps:
return None
self.comps_hash = self.get_refs_hash()
self.cross_modules(board, self.comps_hash)
return self.remove_paste_and_glue(board, self.comps_hash)
def unfilter_components(self, board):
self.uncross_modules(board, self.comps_hash)
self.restore_paste_and_glue(board, self.comps_hash)
def compute_name(self, k_filename, output_dir, output, id, suffix):
if output:
filename = self.expand_filename(output_dir, output, suffix, os.path.splitext(k_filename)[1][1:])
@ -136,7 +124,7 @@ class AnyLayerOptions(VariantOptions):
jobfile_writer = GERBER_JOBFILE_WRITER(GS.board)
plot_ctrl.SetColorMode(True)
# Apply the variants and filters
exclude = self.filter_components(GS.board)
exclude = self.filter_pcb_components(GS.board)
# Plot every layer in the output
generated = {}
layers = Layer.solve(layers)
@ -193,7 +181,7 @@ class AnyLayerOptions(VariantOptions):
f.write(content)
# Restore the eliminated layers
if exclude:
self.unfilter_components(GS.board)
self.unfilter_pcb_components(GS.board)
def solve_extension(self, layer):
if self._plot_format == PLOT_FORMAT_GERBER and self.use_protel_extensions:

View File

@ -53,20 +53,13 @@ class Any_PCB_PrintOptions(VariantOptions):
super().config(parent)
self.drill_marks = DRILL_MARKS_MAP[self.drill_marks]
def filter_components(self, board, force_copy):
def filter_components(self, force_copy):
if not self._comps and not force_copy:
return GS.pcb_file, None
comps_hash = self.get_refs_hash()
self.cross_modules(board, comps_hash)
self.remove_paste_and_glue(board, comps_hash)
if self.hide_excluded:
self.remove_fab(board, comps_hash)
self.filter_pcb_components(GS.board)
# Save the PCB to a temporal dir
fname, pcb_dir = self.save_tmp_dir_board('pdf_pcb_print')
self.uncross_modules(board, comps_hash)
self.restore_paste_and_glue(board, comps_hash)
if self.hide_excluded:
self.restore_fab(board, comps_hash)
self.unfilter_pcb_components(GS.board)
return fname, pcb_dir
def get_targets(self, out_dir):
@ -93,7 +86,7 @@ class Any_PCB_PrintOptions(VariantOptions):
if svg:
cmd.append('--svg')
self.set_title(self.title)
board_name, board_dir = self.filter_components(GS.board, self.title != '')
board_name, board_dir = self.filter_components(self.title != '')
cmd.extend([board_name, os.path.dirname(output)])
cmd, video_remove = add_extra_options(cmd)
# Add the layers

View File

@ -408,6 +408,24 @@ class VariantOptions(BaseOptions):
for gi in self.old_bfab:
gi.SetLayer(self.bfab)
def filter_pcb_components(self, board):
if not self._comps:
return False
self.comps_hash = self.get_refs_hash()
self.cross_modules(board, self.comps_hash)
self.remove_paste_and_glue(board, self.comps_hash)
if hasattr(self, 'hide_excluded') and self.hide_excluded:
self.remove_fab(board, self.comps_hash)
return True
def unfilter_pcb_components(self, board):
if not self._comps:
return
self.uncross_modules(board, self.comps_hash)
self.restore_paste_and_glue(board, self.comps_hash)
if hasattr(self, 'hide_excluded') and self.hide_excluded:
self.restore_fab(board, self.comps_hash)
def set_title(self, title):
self.old_title = None
if title:

View File

@ -337,24 +337,6 @@ class PCB_PrintOptions(VariantOptions):
if not ln.startswith('<?xml') and not ln.startswith('<!DOCTYPE svg'):
raise KiPlotConfigurationError("Background image must be an SVG ({})".format(self.background_image))
def filter_components(self):
if not self._comps:
return
comps_hash = self.get_refs_hash()
self.cross_modules(GS.board, comps_hash)
self.remove_paste_and_glue(GS.board, comps_hash)
if self.hide_excluded:
self.remove_fab(GS.board, comps_hash)
def unfilter_components(self):
if not self._comps:
return
comps_hash = self.get_refs_hash()
self.uncross_modules(GS.board, comps_hash)
self.restore_paste_and_glue(GS.board, comps_hash)
if self.hide_excluded:
self.restore_fab(GS.board, comps_hash)
def get_id_and_ext(self, n=None, id='%02d'):
try:
pn_str = id % (n+1) if n is not None else id
@ -1086,9 +1068,9 @@ class PCB_PrintOptions(VariantOptions):
svgutils = importlib.import_module('.svgutils.transform', package=__package__)
global kicad_worksheet
kicad_worksheet = importlib.import_module('.kicad.worksheet', package=__package__)
self.filter_components()
self.filter_pcb_components(GS.board)
self.generate_output(output)
self.unfilter_components()
self.unfilter_pcb_components(GS.board)
@output_class

47
kibot/out_pcb_variant.py Normal file
View File

@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2022 Salvador E. Tropea
# Copyright (c) 2022 Instituto Nacional de Tecnología Industrial
# License: GPL-3.0
# Project: KiBot (formerly KiPlot)
from .gs import GS
from .out_base import VariantOptions
from .macros import macros, document, output_class # noqa: F401
from . import log
logger = log.get_logger()
class PCB_Variant_Options(VariantOptions):
def __init__(self):
with document:
self.hide_excluded = False
""" Hide components in the Fab layer that are marked as excluded by a variant """
self.output = GS.def_global_output
""" *Filename for the output (%i=variant, %x=kicad_pcb) """
super().__init__()
self._expand_id = 'variant'
self._expand_ext = 'kicad_pcb'
def get_targets(self, out_dir):
return [self._parent.expand_filename(out_dir, self.output)]
def run(self, output):
super().run(output)
self.filter_pcb_components(GS.board)
logger.error('Saving PCB to '+output)
GS.board.Save(output)
GS.copy_project(output)
self.unfilter_pcb_components(GS.board)
@output_class
class PCB_Variant(BaseOutput): # noqa: F821
""" PCB with variant generator
Creates a copy of the PCB with all the filters and variants applied.
This copy isn't intended for development.
Is just a tweaked version of the original where you can look at the results. """
def __init__(self):
super().__init__()
with document:
self.options = PCB_Variant_Options
""" *[dict] Options for the `pcb_variant` output """