From 007fc36d1efbc9bd003f75b9140ef25af316b428 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Wed, 23 Dec 2020 18:47:11 -0300 Subject: [PATCH] Fixed the components fields reset mechanism. My previous approach was incomplete. --- kibot/fil_base.py | 9 +++++++++ kibot/kicad/v5_sch.py | 16 ++++++++++++++++ kibot/out_base.py | 7 +++---- kibot/out_bom.py | 6 ++---- tests/test_plot/test_int_bom.py | 1 - 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/kibot/fil_base.py b/kibot/fil_base.py index 65c3e62d..d1d3c54d 100644 --- a/kibot/fil_base.py +++ b/kibot/fil_base.py @@ -80,6 +80,15 @@ def apply_exclude_filter(comps, filter): c.included = filter.filter(c) +def reset_filters(comps): + logger.debug('Filters reset') + for c in comps: + c.included = True + c.fitted = True + c.fixed = False + c.back_up_fields() + + def apply_fitted_filter(comps, filter): if filter: logger.debug('Applying filter `{}` to fitted'.format(filter.name)) diff --git a/kibot/kicad/v5_sch.py b/kibot/kicad/v5_sch.py index 9b52ec68..4e6626d4 100644 --- a/kibot/kicad/v5_sch.py +++ b/kibot/kicad/v5_sch.py @@ -11,6 +11,7 @@ Currently oriented to collect the components for the BoM. # Encapsulate file/line import re import os +from copy import deepcopy from collections import OrderedDict from .config import KiConf, un_quote from ..gs import GS @@ -855,6 +856,19 @@ class SchematicComponent(object): self.fields.append(field) self.dfields[field.name.lower()] = field + def back_up_fields(self): + """ First call makes a back-up of the fields. + Next calls restores the back-up. """ + if self.fields_bkp: + # We have a back-up, restore from it + self.fields = deepcopy(self.fields_bkp) + self.dfields = {f.name.lower(): f for f in self.fields} + self._solve_fields(LineReader(None, '**Internal**')) + else: + # No back-up. Make one for the next reset + self.fields_bkp = deepcopy(self.fields) + self.dfields_bkp = {f.name.lower(): f for f in self.fields_bkp} + def _solve_ref(self, path): """ Look fo the correct reference for this path. Returns the default reference if no paths defined. @@ -966,6 +980,8 @@ class SchematicComponent(object): # F field_number "text" orientation posX posY size Flags (see below) hjustify vjustify/italic/bold "name" comp.fields = [] comp.dfields = {} + comp.fields_bkp = None + comp.dfields_bkp = None while line[0] == 'F': field = SchematicField.parse(line, f) name_lc = field.name.lower() diff --git a/kibot/out_base.py b/kibot/out_base.py index 39d0842e..80da1a73 100644 --- a/kibot/out_base.py +++ b/kibot/out_base.py @@ -3,7 +3,6 @@ # Copyright (c) 2020 Instituto Nacional de TecnologĂ­a Industrial # License: GPL-3.0 # Project: KiBot (formerly KiPlot) -from copy import deepcopy from .gs import GS from .kiplot import load_sch from .misc import Rect, KICAD_VERSION_5_99, W_WRONGPASTE @@ -14,7 +13,7 @@ else: from pcbnew import EDGE_MODULE, wxPoint, LSET from .registrable import RegOutput from .optionable import Optionable, BaseOptions -from .fil_base import BaseFilter, apply_fitted_filter +from .fil_base import BaseFilter, apply_fitted_filter, reset_filters from .macros import macros, document # noqa: F401 from . import log @@ -260,9 +259,9 @@ class VariantOptions(BaseOptions): return load_sch() # Get the components list from the schematic - # Note: we work with a copy to avoid changes by filters affecting other outputs - comps = deepcopy(GS.sch.get_components()) + comps = GS.sch.get_components() # Apply the filter + reset_filters(comps) apply_fitted_filter(comps, self.dnf_filter) # Apply the variant if self.variant: diff --git a/kibot/out_bom.py b/kibot/out_bom.py index aad3a563..502570a1 100644 --- a/kibot/out_bom.py +++ b/kibot/out_bom.py @@ -8,7 +8,6 @@ Internal BoM (Bill of Materials) output for KiBot. This is somehow compatible with KiBoM. """ import os -from copy import deepcopy from .gs import GS from .optionable import Optionable, BaseOptions from .registrable import RegOutput @@ -18,7 +17,7 @@ from .macros import macros, document, output_class # noqa: F401 from .bom.columnlist import ColumnList, BoMError from .bom.bom import do_bom from .var_kibom import KiBoM -from .fil_base import BaseFilter, apply_exclude_filter, apply_fitted_filter, apply_fixed_filter +from .fil_base import BaseFilter, apply_exclude_filter, apply_fitted_filter, apply_fixed_filter, reset_filters from . import log # To debug the `with document` we can use: # from .mcpyrate.debug import macros, step_expansion @@ -374,9 +373,8 @@ class BoMOptions(BaseOptions): # Get the components list from the schematic comps = GS.sch.get_components() get_board_comps_data(comps) - # We work with a copy to avoid changes by filters affecting other outputs - comps = deepcopy(comps) # Apply all the filters + reset_filters(comps) apply_exclude_filter(comps, self.exclude_filter) apply_fitted_filter(comps, self.dnf_filter) apply_fixed_filter(comps, self.dnc_filter) diff --git a/tests/test_plot/test_int_bom.py b/tests/test_plot/test_int_bom.py index 2d8b8dd8..19346547 100644 --- a/tests/test_plot/test_int_bom.py +++ b/tests/test_plot/test_int_bom.py @@ -1244,7 +1244,6 @@ def test_int_bom_variant_rename_1(): ctx.run(extra_debug=True) rows, header, info = ctx.load_csv(prj+'-bom_(PROD).csv') ref_column = header.index(REF_COLUMN_NAME) - val_column = header.index(VALUE_COLUMN_NAME) check_kibom_test_netlist(rows, ref_column, 2, ['R2', 'D2'], ['R1', 'D1']) rows, header, info = ctx.load_csv(prj+'-bom_(DEV).csv') check_kibom_test_netlist(rows, ref_column, 3, None, ['R1', 'R2', 'D1', 'D2'])