Added fallback for values+units we can't understand

This commit is contained in:
Salvador E. Tropea 2021-10-18 16:51:32 -03:00
parent 3eebc04e4b
commit 5e20206da4
3 changed files with 28 additions and 7 deletions

View File

@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
https://forum.kicad.info/t/fab-drawing-for-only-through-hole-parts/
- Internal BoM: option to avoid merging components with empty fields.
Is named `merge_both_blank` and defaults to true.
- Internal BoM: when a `Value` field can't be interpreted as a `number+unit`,
and it contain at least one space, now we try to use the text before the
space. This helps for cases like "10K 1%".
### Changed
- Internal BoM: now components with different Tolerance, Voltage, Current

View File

@ -12,10 +12,10 @@ All the logic to convert a list of components into the rows and columns used to
import locale
from copy import deepcopy
from math import ceil
from .units import compare_values, comp_match
from .units import compare_values, comp_match, get_last_warning
from .bom_writer import write_bom
from .columnlist import ColumnList
from ..misc import DNF, W_FIELDCONF
from ..misc import DNF, W_FIELDCONF, W_BADVAL1
from .. import log
logger = log.get_logger(__name__)
@ -403,7 +403,14 @@ def group_components(cfg, components):
continue
# Cache the value used to sort
if c.ref_prefix in RLC_PREFIX and c.value.lower() not in DNF:
c.value_sort = comp_match(c.value, c.ref_prefix)
c.value_sort = comp_match(c.value, c.ref_prefix, c.ref)
if c.value_sort is None and (' ' in c.value):
# Try with the data before a space
value = c.value.split(' ')[0]
value_sort = comp_match(value, c.ref_prefix)
if value_sort is not None:
c.value_sort = value_sort
logger.warning(get_last_warning() + "Using `{}` for {} instead".format(value, c.ref))
else:
c.value_sort = None
# Try to add the component to an existing group

View File

@ -43,6 +43,12 @@ UNIT_ALL = UNIT_R + UNIT_C + UNIT_L
match = None
# Current locale decimal point value
decimal_point = None
# Last warning
last_warning = ''
def get_last_warning():
return last_warning
def get_unit(unit, ref_prefix):
@ -98,12 +104,13 @@ def match_string():
return r"(\d*\.?\d*)\s*(" + group_string(PREFIX_ALL) + ")*(" + group_string(UNIT_ALL) + r")*(\d*)$"
def comp_match(component, ref_prefix):
def comp_match(component, ref_prefix, ref=None):
"""
Return a normalized value and units for a given component value string
e.g. comp_match('10R2') returns (10, R)
e.g. comp_match('3.3mOhm') returns (0.0033, R)
"""
global last_warning
original = component
# Remove useless spaces
component = component.strip()
@ -130,14 +137,17 @@ def comp_match(component, ref_prefix):
# Ignore case
match = re.compile(match_string(), flags=re.IGNORECASE)
where = ' in {}'.format(ref) if ref is not None else ''
result = match.match(component)
if not result:
logger.warning(W_BADVAL1 + "Malformed value: `{}` (no match)".format(original))
last_warning = W_BADVAL1
logger.warning(W_BADVAL1 + "Malformed value: `{}` (no match{})".format(original, where))
return None
value, prefix, units, post = result.groups()
if value == '.':
logger.warning(W_BADVAL2 + "Malformed value: `{}` (reduced to decimal point)".format(original))
last_warning = W_BADVAL2
logger.warning(W_BADVAL2 + "Malformed value: `{}` (reduced to decimal point{})".format(original, where))
return None
if value == '':
value = '0'
@ -148,7 +158,8 @@ def comp_match(component, ref_prefix):
# We will also have a trailing number
if post:
if "." in value:
logger.warning(W_BADVAL3 + "Malformed value: `{}` (unit split, but contains decimal point)".format(original))
last_warning = W_BADVAL3
logger.warning(W_BADVAL3 + "Malformed value: `{}` (unit split, but contains decimal point)".format(original, where))
return None
value = float(value)
postValue = float(post) / (10 ** len(post))