From 9f8ecff5d10223daf353489c90a17bc21662cc05 Mon Sep 17 00:00:00 2001 From: "Salvador E. Tropea" Date: Wed, 23 Dec 2020 12:11:39 -0300 Subject: [PATCH] Added support to rename fields matching the variant to value. --- CHANGELOG.md | 2 +- README.md | 1 + kibot/fil_var_rename.py | 9 +- kibot/kicad/v5_sch.py | 14 ++ tests/board_samples/kicad_5/f_rename_1.sch | 172 ++++++++++++++++++ tests/test_plot/test_int_bom.py | 13 ++ .../int_bom_var_rename_1_csv.kibot.yaml | 54 ++++++ 7 files changed, 263 insertions(+), 2 deletions(-) create mode 100644 tests/board_samples/kicad_5/f_rename_1.sch create mode 100644 tests/yaml_samples/int_bom_var_rename_1_csv.kibot.yaml diff --git a/CHANGELOG.md b/CHANGELOG.md index 86dd0ea0..00b0feb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - File name patterns: %F is the name of the source file without extension, but with the path. - A hint for pip installations without using `--no-compile`. -- KiBoM variants: support to field overwrite according to variant (experimental). +- Support to field overwrite according to variant. ### Fixed - Now we support missing field names in schematic library entries. diff --git a/README.md b/README.md index 2f12bebc..e8620620 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,7 @@ Currently the only type available is `generic`. - `comment`: [string=''] A comment for documentation purposes. - `name`: [string=''] Used to identify this particular filter definition. - `separator`: [string=':'] Separator used between the variant and the field name. + - `variant_to_value`: [boolean=false] Rename fields matching the variant to the value of the component. diff --git a/kibot/fil_var_rename.py b/kibot/fil_var_rename.py index e69072b5..eb776e64 100644 --- a/kibot/fil_var_rename.py +++ b/kibot/fil_var_rename.py @@ -27,6 +27,8 @@ class Var_Rename(BaseFilter): # noqa: F821 with document: self.separator = ':' """ Separator used between the variant and the field name """ + self.variant_to_value = False + """ Rename fields matching the variant to the value of the component """ def config(self): super().config() @@ -46,5 +48,10 @@ class Var_Rename(BaseFilter): # noqa: F821 f_field = res[1].lower() if f_variant == variant: if GS.debug_level > 2: - logger.debug('ref: {} value: {} -> {}'.format(comp.ref, comp.get_field_value(f_field), value)) + logger.debug('ref: {} {}: {} -> {}'. + format(comp.ref, f_field, comp.get_field_value(f_field), value)) comp.set_field(f_field, value) + elif self.variant_to_value and name.lower() == variant: + if GS.debug_level > 2: + logger.debug('ref: {} value: {} -> {}'.format(comp.ref, comp.value, value)) + comp.set_field('value', value) diff --git a/kibot/kicad/v5_sch.py b/kibot/kicad/v5_sch.py index 4fe1df6b..9b52ec68 100644 --- a/kibot/kicad/v5_sch.py +++ b/kibot/kicad/v5_sch.py @@ -821,6 +821,14 @@ class SchematicComponent(object): return self.dfields[field].value return '' + def get_free_field_number(self): + """ Looks for a field number that isn't currently in use """ + max_num = -1 + for f in self.fields: + if f.number > max_num: + max_num = f.number + return max_num+1 + def set_field(self, field, value): """ Change the value for an existing field """ if field in self.dfields: @@ -829,6 +837,12 @@ class SchematicComponent(object): # Adjust special fields if target.number < 4: self._solve_fields(LineReader(None, '**Internal**')) + else: + f = SchematicField() + f.name = field + f.value = value + f.number = self.get_free_field_number() + self.add_field(f) # def get_field_names(self): # return [f.name for f in self.fields] diff --git a/tests/board_samples/kicad_5/f_rename_1.sch b/tests/board_samples/kicad_5/f_rename_1.sch new file mode 100644 index 00000000..00f8c73d --- /dev/null +++ b/tests/board_samples/kicad_5/f_rename_1.sch @@ -0,0 +1,172 @@ +EESchema Schematic File Version 4 +EELAYER 30 0 +EELAYER END +$Descr A4 11693 8268 +encoding utf-8 +Sheet 1 1 +Title "" +Date "" +Rev "" +Comp "" +Comment1 "" +Comment2 "" +Comment3 "" +Comment4 "" +$EndDescr +$Comp +L Device:R_US R1 +U 1 1 5FDD0409 +P 4850 3100 +F 0 "R1" V 4950 3125 50 0000 C CNN +F 1 "RES-000007-00" H 4850 2650 50 0001 C CNN +F 2 "Resistors:R0402" H 4850 2350 50 0001 C CNN +F 3 "" H 4850 2750 50 0001 C CNN +F 4 "Stackpole Electronics Inc" H 4850 2550 50 0001 C CNN "Manufacturer" +F 5 "RMCF0402FT4K70" H 4850 2450 50 0001 C CNN "Manufacturer Part Number" +F 6 "4.7K" V 4700 3125 50 0000 C CNN "Resistance (Ohms)" +F 7 "±1%" H 5000 3000 50 0001 L CNN "Tolerance (%)" +F 8 "0402" V 4775 3125 50 0000 C CNN "Package Type" +F 9 "1/16W" H 4850 2800 50 0001 C CNN "Power (Watts)" +F 10 "RES-000045-00" H 4850 3100 50 0001 C CNN "DEV:Value" +F 11 "Yageo" H 4850 3100 50 0001 C CNN "DEV:Manufacturer" +F 12 "RC0402FR-071KL" H 4850 3100 50 0001 C CNN "DEV:Manufacturer Part Number" +F 13 "RES SMD 1K OHM 1% 1/16W 0402" H 4850 3100 50 0001 C CNN "DEV:Description" + 1 4850 3100 + 0 1 1 0 +$EndComp +$Comp +L Device:LED_ALT D1 +U 1 1 5FDD1589 +P 5400 3100 +F 0 "D1" H 5400 3410 50 0000 C CNN +F 1 "DIO-000062-00" H 5400 2600 50 0001 C CNN +F 2 "Diodes:LED0603-YELLOW" H 5400 2300 50 0001 C CNN +F 3 "" H 5400 2700 50 0001 C CNN +F 4 "Würth Elektronik" H 5400 2500 50 0001 C CNN "Manufacturer" +F 5 "150060YS75000" H 5400 2400 50 0001 C CNN "Manufacturer Part Number" +F 6 "150060YS75000" H 5400 2700 50 0001 C CNN "Part Number" +F 7 "2V" H 5400 3319 50 0000 C CNN "Forward Voltage (Volt)" +F 8 "Yellow" H 5400 3228 50 0000 C CNN "LED Color" + 1 5400 3100 + -1 0 0 1 +$EndComp +$Comp +L power:+3.3V #PWR0101 +U 1 1 5FDD2CE3 +P 4500 3000 +F 0 "#PWR0101" H 4500 2850 50 0001 C CNN +F 1 "+3.3V" H 4500 3173 50 0000 C CNN +F 2 "" H 4500 3000 60 0000 C CNN +F 3 "" H 4500 3000 60 0000 C CNN + 1 4500 3000 + 1 0 0 -1 +$EndComp +$Comp +L power:GND #PWR0102 +U 1 1 5FDD32E7 +P 5750 3200 +F 0 "#PWR0102" H 5750 3030 50 0001 C CNN +F 1 "GND" H 5750 3050 50 0000 C CNN +F 2 "" H 5750 3300 60 0000 C CNN +F 3 "" H 5730 3110 60 0000 C CNN + 1 5750 3200 + 1 0 0 -1 +$EndComp +Wire Wire Line + 4500 3000 4500 3100 +Wire Wire Line + 4500 3100 4700 3100 +Wire Wire Line + 5000 3100 5250 3100 +Wire Wire Line + 5550 3100 5750 3100 +Wire Wire Line + 5750 3100 5750 3200 +$Comp +L Device:R_US R2 +U 1 1 5FDD4C1B +P 6500 3100 +F 0 "R2" V 6600 3100 50 0000 C CNN +F 1 "RES-000007-00" H 6500 2650 50 0001 C CNN +F 2 "Resistors:R0402" H 6500 2350 50 0001 C CNN +F 3 "" H 6500 2750 50 0001 C CNN +F 4 "Stackpole Electronics Inc" H 6500 2550 50 0001 C CNN "Manufacturer" +F 5 "RMCF0402FT4K70" H 6500 2450 50 0001 C CNN "Manufacturer Part Number" +F 6 "4.7K" V 6325 3100 50 0000 C CNN "Resistance (Ohms)" +F 7 "±1%" H 6650 3000 50 0001 L CNN "Tolerance (%)" +F 8 "0402" V 6400 3100 50 0000 C CNN "Package Type" +F 9 "1/16W" H 6500 2800 50 0001 C CNN "Power (Watts)" +F 10 "dnp" H 6500 3100 50 0001 C CNN "PROD" + 1 6500 3100 + 0 1 1 0 +$EndComp +$Comp +L Device:LED_ALT D2 +U 1 1 5FDD4C26 +P 7050 3100 +F 0 "D2" H 7050 3410 50 0000 C CNN +F 1 "DIO-000062-00" H 7050 2600 50 0001 C CNN +F 2 "Diodes:LED0603-YELLOW" H 7050 2300 50 0001 C CNN +F 3 "" H 7050 2700 50 0001 C CNN +F 4 "Würth Elektronik" H 7050 2500 50 0001 C CNN "Manufacturer" +F 5 "150060YS75000" H 7050 2400 50 0001 C CNN "Manufacturer Part Number" +F 6 "150060YS75000" H 7050 2700 50 0001 C CNN "Part Number" +F 7 "2V" H 7050 3319 50 0000 C CNN "Forward Voltage (Volt)" +F 8 "Yellow" H 7050 3228 50 0000 C CNN "LED Color" +F 9 "dnp" H 7050 3100 50 0001 C CNN "PROD" + 1 7050 3100 + -1 0 0 1 +$EndComp +$Comp +L power:+3.3V #PWR0103 +U 1 1 5FDD4C2C +P 6150 3000 +F 0 "#PWR0103" H 6150 2850 50 0001 C CNN +F 1 "+3.3V" H 6150 3173 50 0000 C CNN +F 2 "" H 6150 3000 60 0000 C CNN +F 3 "" H 6150 3000 60 0000 C CNN + 1 6150 3000 + 1 0 0 -1 +$EndComp +$Comp +L power:GND #PWR0104 +U 1 1 5FDD4C32 +P 7400 3200 +F 0 "#PWR0104" H 7400 3030 50 0001 C CNN +F 1 "GND" H 7400 3050 50 0000 C CNN +F 2 "" H 7400 3300 60 0000 C CNN +F 3 "" H 7380 3110 60 0000 C CNN + 1 7400 3200 + 1 0 0 -1 +$EndComp +Wire Wire Line + 6150 3000 6150 3100 +Wire Wire Line + 6150 3100 6350 3100 +Wire Wire Line + 6650 3100 6900 3100 +Wire Wire Line + 7200 3100 7400 3100 +Wire Wire Line + 7400 3100 7400 3200 +Wire Notes Line + 4350 2700 5900 2700 +Wire Notes Line + 5900 2700 5900 3500 +Wire Notes Line + 5900 3500 4350 3500 +Wire Notes Line + 4350 3500 4350 2700 +Wire Notes Line + 6000 2700 7550 2700 +Wire Notes Line + 7550 2700 7550 3500 +Wire Notes Line + 7550 3500 6000 3500 +Wire Notes Line + 6000 3500 6000 2700 +Text Notes 4350 2700 0 50 ~ 0 +[Prod] R1=4.7K / [Dev] R1=1K +Text Notes 6000 2700 0 50 ~ 0 +[Prod] R2, D2=dnp +$EndSCHEMATC diff --git a/tests/test_plot/test_int_bom.py b/tests/test_plot/test_int_bom.py index e0762767..2d8b8dd8 100644 --- a/tests/test_plot/test_int_bom.py +++ b/tests/test_plot/test_int_bom.py @@ -1238,6 +1238,19 @@ def test_int_bom_variant_t2c(): ctx.clean_up() +def test_int_bom_variant_rename_1(): + prj = 'f_rename_1' + ctx = context.TestContextSCH('test_int_bom_variant_rename_1', prj, 'int_bom_var_rename_1_csv', BOM_DIR) + 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']) + ctx.clean_up() + + def test_int_bom_variant_t2s(): prj = 'kibom-variant_2' ctx = context.TestContextSCH('test_int_bom_variant_t2s', prj, 'int_bom_var_t2s_csv', BOM_DIR) diff --git a/tests/yaml_samples/int_bom_var_rename_1_csv.kibot.yaml b/tests/yaml_samples/int_bom_var_rename_1_csv.kibot.yaml new file mode 100644 index 00000000..77105d17 --- /dev/null +++ b/tests/yaml_samples/int_bom_var_rename_1_csv.kibot.yaml @@ -0,0 +1,54 @@ +# Example KiBot config file +kibot: + version: 1 + +filters: + - name: 'Variant rename' + type: var_rename + separator: ':' + variant_to_value: true + +variants: + - name: 'production' + comment: 'Production variant' + type: kibom + file_id: '_(PROD)' + variant: PROD + pre_transform: 'Variant rename' + + - name: 'development' + comment: 'Development variant' + type: kibom + file_id: '_(DEV)' + variant: DEV + pre_transform: 'Variant rename' + +outputs: + - name: 'bom_internal_production' + comment: "Bill of Materials in CSV format for production" + type: bom + dir: BoM + options: + variant: production + columns: + - Row + - References + - Value + - Description + - Manufacturer + - Manufacturer Part Number + + - name: 'bom_internal_test' + comment: "Bill of Materials in CSV format for development" + type: bom + dir: BoM + options: + variant: development + columns: + - Row + - References + - Value + - Description + - Manufacturer + - Manufacturer Part Number +