diff --git a/CHANGELOG.md b/CHANGELOG.md index 662bad07..511b788c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [1.6.2] - UNRELEASED ### Added +- Global options: + - `colored_tht_resistors` to disable the 3D colored resistors. + - `field_tolerance` field/s to look for resistor tolerance. + - `default_resistor_tolerance` which tolerance to use when none found. - 3D: colored 3D models for THT resistors ### Fixed diff --git a/kibot/PcbDraw/README.md b/kibot/PcbDraw/README.md index 8b39b626..3b1cba68 100644 --- a/kibot/PcbDraw/README.md +++ b/kibot/PcbDraw/README.md @@ -219,10 +219,18 @@ This file comes from KiKit, but it has too much in common with `populate.py`. ## 2023-03-20 Various fixes and changes in resistor colors diff --git a/kibot/PcbDraw/plot.py b/kibot/PcbDraw/plot.py -index 8ca660e6..a262732b 100644 +index 8ca660e6..9dc45ba9 100644 --- a/kibot/PcbDraw/plot.py +++ b/kibot/PcbDraw/plot.py -@@ -56,6 +56,8 @@ default_style = { +@@ -18,6 +18,7 @@ from . import np + from .unit import read_resistance + from lxml import etree, objectify # type: ignore + from .pcbnew_transition import KICAD_VERSION, isV6, isV7, pcbnew # type: ignore ++from ..gs import GS + + T = TypeVar("T") + Numeric = Union[int, float] +@@ -56,6 +57,8 @@ default_style = { 7: '#cc00cc', 8: '#666666', 9: '#cccccc', @@ -231,7 +239,7 @@ index 8ca660e6..a262732b 100644 '1%': '#805500', '2%': '#ff0000', '0.5%': '#00cc11', -@@ -64,6 +66,7 @@ default_style = { +@@ -64,6 +67,7 @@ default_style = { '0.05%': '#666666', '5%': '#ffc800', '10%': '#d9d9d9', @@ -239,7 +247,7 @@ index 8ca660e6..a262732b 100644 } } -@@ -884,10 +887,15 @@ class PlotComponents(PlotInterface): +@@ -884,10 +888,15 @@ class PlotComponents(PlotInterface): try: res, tolerance = self._get_resistance_from_value(value) power = math.floor(res.log10()) - 1 @@ -258,27 +266,26 @@ index 8ca660e6..a262732b 100644 self._plotter.get_style("tht-resistor-band-colors", int(power)), self._plotter.get_style("tht-resistor-band-colors", tolerance) ] -@@ -914,7 +922,7 @@ class PlotComponents(PlotInterface): +@@ -914,7 +923,7 @@ class PlotComponents(PlotInterface): return def _get_resistance_from_value(self, value: str) -> Tuple[Decimal, str]: - res, tolerance = None, "5%" -+ res, tolerance = None, "20%" ++ res, tolerance = None, str(GS.global_default_resistor_tolerance)+"%" value_l = value.split(" ", maxsplit=1) try: res = read_resistance(value_l[0]) -@@ -1084,6 +1092,11 @@ class PcbPlotter(): +@@ -1084,6 +1093,12 @@ class PcbPlotter(): lib = str(footprint.GetFPID().GetLibNickname()).strip() name = str(footprint.GetFPID().GetLibItemName()).strip() value = footprint.GetValue().strip() -+ # Look for a tolerance in the properties -+ prop = footprint.GetProperties() -+ tol = prop.get('tol', prop.get('tolerance', None)) -+ if tol: -+ value = value+' '+tol ++ if not LEGACY_KICAD: ++ # Look for a tolerance in the properties ++ prop = footprint.GetProperties() ++ tol = next(filter(lambda x: x, map(prop.get, GS.global_field_tolerance)), None) ++ if tol: ++ value = value+' '+tol ref = footprint.GetReference().strip() center = footprint.GetPosition() orient = math.radians(footprint.GetOrientation().AsDegrees()) - - diff --git a/kibot/PcbDraw/plot.py b/kibot/PcbDraw/plot.py index 468f6891..9dc45ba9 100644 --- a/kibot/PcbDraw/plot.py +++ b/kibot/PcbDraw/plot.py @@ -18,6 +18,7 @@ from . import np from .unit import read_resistance from lxml import etree, objectify # type: ignore from .pcbnew_transition import KICAD_VERSION, isV6, isV7, pcbnew # type: ignore +from ..gs import GS T = TypeVar("T") Numeric = Union[int, float] @@ -922,7 +923,7 @@ class PlotComponents(PlotInterface): return def _get_resistance_from_value(self, value: str) -> Tuple[Decimal, str]: - res, tolerance = None, "20%" + res, tolerance = None, str(GS.global_default_resistor_tolerance)+"%" value_l = value.split(" ", maxsplit=1) try: res = read_resistance(value_l[0]) @@ -1095,7 +1096,7 @@ class PcbPlotter(): if not LEGACY_KICAD: # Look for a tolerance in the properties prop = footprint.GetProperties() - tol = prop.get('tol', prop.get('tolerance', None)) + tol = next(filter(lambda x: x, map(prop.get, GS.global_field_tolerance)), None) if tol: value = value+' '+tol ref = footprint.GetReference().strip() diff --git a/kibot/globals.py b/kibot/globals.py index 273f9c6d..42d9cd50 100644 --- a/kibot/globals.py +++ b/kibot/globals.py @@ -266,6 +266,14 @@ class Globals(FiltersOptions): this flag """ self.colored_tht_resistors = True """ Try to add color bands to the 3D models of KiCad THT resistors """ + self.field_tolerance = Optionable + """ [string|list(string)] Name/s of the field/s used for the tolerance. + Used while creating colored resistors. + The default is ['tol', 'tolerance'] """ + self.default_resistor_tolerance = 20 + """ When no tolerance is specified we use this value. + Note that I know 5% is a common default, but technically speaking 20% is the default. + Used while creating colored resistors """ self.set_doc('filters', " [list(dict)] KiBot warnings to be ignored ") self._filter_what = 'KiBot warnings' self.filters = FilterOptionsKiBot @@ -374,6 +382,7 @@ class Globals(FiltersOptions): if GS.ki6 and GS.pcb_file and os.path.isfile(GS.pcb_file): self.get_stack_up() super().config(parent) + self.field_tolerance = Optionable.force_list(self.field_tolerance, default=['tol', 'tolerance']) # Transfer options to the GS globals for option in filter(lambda x: x[0] != '_', self.__dict__.keys()): gl = 'global_'+option diff --git a/kibot/gs.py b/kibot/gs.py index 8059d576..149bada9 100644 --- a/kibot/gs.py +++ b/kibot/gs.py @@ -130,6 +130,7 @@ class GS(object): global_csv_accept_no_ref = None global_date_format = None global_date_time_format = None + global_default_resistor_tolerance = None global_drc_exclusions_workaround = None global_dir = None global_disable_3d_alias_as_env = None @@ -139,6 +140,7 @@ class GS(object): global_extra_pth_drill = None global_field_3D_model = None global_field_lcsc_part = None + global_field_tolerance = None global_hide_excluded = None global_impedance_controlled = None global_kiauto_time_out_scale = None diff --git a/kibot/optionable.py b/kibot/optionable.py index 37138b42..f4e16036 100644 --- a/kibot/optionable.py +++ b/kibot/optionable.py @@ -414,12 +414,12 @@ class Optionable(object): return Optionable.expand_filename_both(self, name) @staticmethod - def force_list(val, comma_sep=True, lower_case=False): + def force_list(val, comma_sep=True, lower_case=False, default=None): """ Used for values that accept a string or a list of strings. The string can be a comma separated list """ if isinstance(val, type): # Not used - val = [] + val = [] if default is None else default elif isinstance(val, str): # A string if val: diff --git a/kibot/out_base_3d.py b/kibot/out_base_3d.py index 1f071494..31bf1c85 100644 --- a/kibot/out_base_3d.py +++ b/kibot/out_base_3d.py @@ -342,11 +342,10 @@ class Base3DOptions(VariantOptions): r_len = float(m.group(1)) # THT Resistor that we want to add colors # Check the tolerance - # TODO: Configurable - tol = c.get_field_value('tol') or c.get_field_value('tolerance') + tol = next(filter(lambda x: x, map(c.get_field_value, GS.global_field_tolerance)), None) if not tol: - tol = 20 - logger.warning(W_BADTOL+'Missing tolerance for {}, using 20%'.format(c.ref)) + tol = GS.global_default_resistor_tolerance + logger.warning(W_BADTOL+'Missing tolerance for {}, using {}%'.format(c.ref, tol)) else: tol = tol.strip() if tol[-1] == '%': @@ -397,7 +396,7 @@ class Base3DOptions(VariantOptions): # Make sure we don't have digits that can't be represented rest = val_str[dig_bars:] if rest and not all(map(lambda x: x == '0', rest)): - logger.warning(W_RESVALISSUE+'Digits not represented in {} {}'.format(c.ref, c.value)) + logger.warning(W_RESVALISSUE+'Digits not represented in {} {} ({} %)'.format(c.ref, c.value, tol)) bars[nbars-1] = tol_color # For 20% remove the last bar if tol_color == 12: diff --git a/tests/yaml_samples/render_3d_tht_res_1.kibot.yaml b/tests/yaml_samples/render_3d_tht_res_1.kibot.yaml index eea209a0..a39e5037 100644 --- a/tests/yaml_samples/render_3d_tht_res_1.kibot.yaml +++ b/tests/yaml_samples/render_3d_tht_res_1.kibot.yaml @@ -2,6 +2,9 @@ kibot: version: 1 +# globals: +# default_resistor_tolerance: 5 + outputs: - name: '3d_default' comment: "3D view"